forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework GLContextSwitch, get rid of RendererContextManager (flutter#18601
- Loading branch information
Chris Yang
authored
Jun 4, 2020
1 parent
4ce831e
commit 1482d9b
Showing
44 changed files
with
418 additions
and
452 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "gl_context_switch.h" | ||
|
||
namespace flutter { | ||
|
||
SwitchableGLContext::SwitchableGLContext() = default; | ||
|
||
SwitchableGLContext::~SwitchableGLContext() = default; | ||
|
||
GLContextResult::GLContextResult() = default; | ||
GLContextResult::~GLContextResult() = default; | ||
GLContextResult::GLContextResult(bool static_result) : result_(static_result){}; | ||
bool GLContextResult::GetResult() { | ||
return result_; | ||
}; | ||
|
||
GLContextDefaultResult::~GLContextDefaultResult() = default; | ||
GLContextDefaultResult::GLContextDefaultResult(bool static_result) | ||
: GLContextResult(static_result){}; | ||
|
||
GLContextSwitch::GLContextSwitch(std::unique_ptr<SwitchableGLContext> context) | ||
: context_(std::move(context)) { | ||
FML_CHECK(context_ != nullptr); | ||
result_ = context_->SetCurrent(); | ||
}; | ||
|
||
GLContextSwitch::~GLContextSwitch() { | ||
context_->RemoveCurrent(); | ||
}; | ||
|
||
} // namespace flutter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_SHELL_COMMON_GL_CONTEXT_SWITCH_H_ | ||
#define FLUTTER_SHELL_COMMON_GL_CONTEXT_SWITCH_H_ | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <vector> | ||
|
||
#include "flutter/fml/logging.h" | ||
#include "flutter/fml/macros.h" | ||
|
||
namespace flutter { | ||
|
||
//------------------------------------------------------------------------------ | ||
/// An abstract class represents a gl context that can be switched by | ||
/// GLContextSwitch | ||
/// | ||
/// The subclass should wrap a "Context" object inside this class. For example, | ||
/// in iOS while using GL rendering surface, the subclass should wrap an | ||
/// |EAGLContext|. | ||
class SwitchableGLContext { | ||
public: | ||
//------------------------------------------------------------------------------ | ||
/// Implement this to set the context wrapped by this |SwitchableGLContext| | ||
/// object to the current context. | ||
virtual bool SetCurrent() = 0; | ||
|
||
//------------------------------------------------------------------------------ | ||
/// Implement this to remove the context wrapped by this |SwitchableGLContext| | ||
/// object from current context; | ||
virtual bool RemoveCurrent() = 0; | ||
|
||
SwitchableGLContext(); | ||
|
||
virtual ~SwitchableGLContext(); | ||
|
||
FML_DISALLOW_COPY_AND_ASSIGN(SwitchableGLContext); | ||
}; | ||
|
||
//------------------------------------------------------------------------------ | ||
/// Represents the result of setting a gl context. | ||
/// This class exists because context results are used in places applies to all | ||
/// the platforms. On certain platforms(for example lower end iOS devices that | ||
/// uses gl), a |GLContextSwitch| is required to protect flutter's gl contect | ||
/// from being polluted by other programs(embedded platform views). A | ||
/// |GLContextSwitch| is a subclass of |GLContextResult|, which can be returned | ||
/// on platforms that requires context switching. A |GLContextDefaultResult| is | ||
/// also a subclass of |GLContextResult|, which can be returned on platforms | ||
/// that doesn't require context switching. | ||
class GLContextResult { | ||
public: | ||
GLContextResult(); | ||
virtual ~GLContextResult(); | ||
|
||
//---------------------------------------------------------------------------- | ||
// Returns true if the gl context is set successfully. | ||
bool GetResult(); | ||
|
||
protected: | ||
GLContextResult(bool static_result); | ||
bool result_; | ||
|
||
FML_DISALLOW_COPY_AND_ASSIGN(GLContextResult); | ||
}; | ||
|
||
//------------------------------------------------------------------------------ | ||
/// The default implementation of |GLContextResult|. | ||
/// | ||
/// Use this class on platforms that doesn't require gl context switching. | ||
/// * See also |GLContextSwitch| if the platform requires gl context switching. | ||
class GLContextDefaultResult : public GLContextResult { | ||
public: | ||
//---------------------------------------------------------------------------- | ||
/// Constructs a |GLContextDefaultResult| with a static result. | ||
/// | ||
/// Used this on platforms that doesn't require gl context switching. (For | ||
/// example, metal on iOS) | ||
/// | ||
/// @param static_result a static value that will be returned from | ||
/// |GetResult| | ||
GLContextDefaultResult(bool static_result); | ||
|
||
~GLContextDefaultResult() override; | ||
|
||
FML_DISALLOW_COPY_AND_ASSIGN(GLContextDefaultResult); | ||
}; | ||
|
||
//------------------------------------------------------------------------------ | ||
/// Switches the gl context to the a context that is passed in the | ||
/// constructor. | ||
/// | ||
/// In destruction, it should restore the current context to what was | ||
/// before the construction of this switch. | ||
class GLContextSwitch final : public GLContextResult { | ||
public: | ||
//---------------------------------------------------------------------------- | ||
/// Constructs a |GLContextSwitch|. | ||
/// | ||
/// @param context The context that is going to be set as the current | ||
/// context. The |GLContextSwitch| should not outlive the owner of the gl | ||
/// context wrapped inside the `context`. | ||
GLContextSwitch(std::unique_ptr<SwitchableGLContext> context); | ||
|
||
~GLContextSwitch() override; | ||
|
||
private: | ||
std::unique_ptr<SwitchableGLContext> context_; | ||
|
||
FML_DISALLOW_COPY_AND_ASSIGN(GLContextSwitch); | ||
}; | ||
|
||
} // namespace flutter | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "gl_context_switch_test.h" | ||
|
||
#include "flutter/fml/thread_local.h" | ||
|
||
namespace flutter { | ||
namespace testing { | ||
|
||
FML_THREAD_LOCAL fml::ThreadLocalUniquePtr<int> current_context; | ||
|
||
GLContextSwitchTest::GLContextSwitchTest() = default; | ||
|
||
TestSwitchableGLContext::TestSwitchableGLContext(int context) | ||
: context_(context){}; | ||
|
||
TestSwitchableGLContext::~TestSwitchableGLContext() = default; | ||
|
||
bool TestSwitchableGLContext::SetCurrent() { | ||
SetCurrentContext(context_); | ||
return true; | ||
}; | ||
|
||
bool TestSwitchableGLContext::RemoveCurrent() { | ||
SetCurrentContext(-1); | ||
return true; | ||
}; | ||
|
||
int TestSwitchableGLContext::GetContext() { | ||
return context_; | ||
}; | ||
|
||
int TestSwitchableGLContext::GetCurrentContext() { | ||
return *(current_context.get()); | ||
}; | ||
|
||
void TestSwitchableGLContext::SetCurrentContext(int context) { | ||
current_context.reset(new int(context)); | ||
}; | ||
} // namespace testing | ||
} // namespace flutter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
#define FML_USED_ON_EMBEDDER | ||
|
||
#include <functional> | ||
#include <future> | ||
#include <memory> | ||
|
||
#include "flutter/shell/common/gl_context_switch.h" | ||
#include "gl_context_switch_test.h" | ||
#include "gtest/gtest.h" | ||
|
||
namespace flutter { | ||
namespace testing { | ||
|
||
TEST_F(GLContextSwitchTest, SwitchKeepsContextCurrentWhileInScope) { | ||
{ | ||
auto test_gl_context = std::make_unique<TestSwitchableGLContext>(0); | ||
auto context_switch = GLContextSwitch(std::move(test_gl_context)); | ||
ASSERT_EQ(TestSwitchableGLContext::GetCurrentContext(), 0); | ||
} | ||
ASSERT_EQ(TestSwitchableGLContext::GetCurrentContext(), -1); | ||
} | ||
|
||
} // namespace testing | ||
} // namespace flutter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.