RendererContextManager (flutter/engine#17941)

This commit is contained in:
Chris Yang
2020-04-30 09:39:33 -07:00
committed by GitHub
parent c65ab74a1c
commit 532f988f67
7 changed files with 423 additions and 0 deletions

View File

@@ -586,6 +586,11 @@ FILE: ../../../flutter/shell/common/pointer_data_dispatcher.cc
FILE: ../../../flutter/shell/common/pointer_data_dispatcher.h
FILE: ../../../flutter/shell/common/rasterizer.cc
FILE: ../../../flutter/shell/common/rasterizer.h
FILE: ../../../flutter/shell/common/renderer_context_manager.cc
FILE: ../../../flutter/shell/common/renderer_context_manager.h
FILE: ../../../flutter/shell/common/renderer_context_manager_unittests.cc
FILE: ../../../flutter/shell/common/renderer_context_test.cc
FILE: ../../../flutter/shell/common/renderer_context_test.h
FILE: ../../../flutter/shell/common/run_configuration.cc
FILE: ../../../flutter/shell/common/run_configuration.h
FILE: ../../../flutter/shell/common/shell.cc

View File

@@ -97,6 +97,8 @@ source_set("common") {
"pointer_data_dispatcher.h",
"rasterizer.cc",
"rasterizer.h",
"renderer_context_manager.cc",
"renderer_context_manager.h",
"run_configuration.cc",
"run_configuration.h",
"shell.cc",
@@ -190,6 +192,9 @@ if (enable_unittests) {
"input_events_unittests.cc",
"persistent_cache_unittests.cc",
"pipeline_unittests.cc",
"renderer_context_manager_unittests.cc",
"renderer_context_test.cc",
"renderer_context_test.h",
"shell_test.cc",
"shell_test.h",
"shell_test_external_view_embedder.cc",

View File

@@ -0,0 +1,74 @@
// 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 "renderer_context_manager.h"
namespace flutter {
RendererContext::RendererContext() = default;
RendererContext::~RendererContext() = default;
RendererContextManager::RendererContextManager(
std::unique_ptr<RendererContext> context,
std::unique_ptr<RendererContext> resource_context)
: context_(std::move(context)),
resource_context_(std::move(resource_context)){};
RendererContextManager::~RendererContextManager() = default;
RendererContextManager::RendererContextSwitch
RendererContextManager::MakeCurrent(std::unique_ptr<RendererContext> context) {
return RendererContextManager::RendererContextSwitch(*this,
std::move(context));
};
RendererContextManager::RendererContextSwitch
RendererContextManager::FlutterMakeCurrent() {
return MakeCurrent(std::move(context_));
};
RendererContextManager::RendererContextSwitch
RendererContextManager::FlutterResourceMakeCurrent() {
return MakeCurrent(std::move(resource_context_));
};
bool RendererContextManager::PushContext(
std::unique_ptr<RendererContext> context) {
if (current_ == nullptr) {
current_ = std::move(context);
return current_->SetCurrent();
}
stored_.push_back(std::move(current_));
bool result = context->SetCurrent();
current_ = std::move(context);
return result;
};
void RendererContextManager::PopContext() {
if (stored_.empty()) {
current_->RemoveCurrent();
return;
}
current_ = std::move(stored_.back());
current_->SetCurrent();
stored_.pop_back();
};
RendererContextManager::RendererContextSwitch::RendererContextSwitch(
RendererContextManager& manager,
std::unique_ptr<RendererContext> context)
: manager_(manager) {
bool result = manager_.PushContext(std::move(context));
result_ = result;
};
RendererContextManager::RendererContextSwitch::~RendererContextSwitch() {
manager_.PopContext();
};
bool RendererContextManager::RendererContextSwitch::GetResult() {
return result_;
};
} // namespace flutter

View File

@@ -0,0 +1,131 @@
// 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_RENDERER_CONTEXT_MANAGER_H_
#define FLUTTER_SHELL_COMMON_RENDERER_CONTEXT_MANAGER_H_
#include <functional>
#include <memory>
#include <vector>
#include "flutter/fml/logging.h"
#include "flutter/fml/macros.h"
namespace flutter {
//------------------------------------------------------------------------------
/// An abstract class represents a renderer context
///
/// 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 RendererContext {
public:
//------------------------------------------------------------------------------
/// Implement this to set the context wrapped by this |RendererContext| object
/// to the current context.
virtual bool SetCurrent() = 0;
//------------------------------------------------------------------------------
/// Implement this to remove the context wrapped by this |RendererContext|
/// object from current context;
virtual void RemoveCurrent() = 0;
RendererContext();
virtual ~RendererContext();
FML_DISALLOW_COPY_AND_ASSIGN(RendererContext);
};
//------------------------------------------------------------------------------
/// Manages the renderer context.
///
/// Initially, the |RendererContextManager| doesn't know any context that is set
/// by other programs (e.g. a plugin). To make sure not to pollute the other
/// context, use `MakeCurrent` to set other context, so |RendererContextManager|
/// has a reference of the other context. Ideally, this process has to be done
/// everytime before a context used by Flutter is set, if the current context at
/// the moment is not null or used by Flutter.
class RendererContextManager {
public:
//------------------------------------------------------------------------------
/// Switches the renderer context to the a context that is passed in the
/// constructor.
///
/// In destruction, it should reset the current context back to what was
/// before the construction of this switch.
///
class RendererContextSwitch {
public:
//----------------------------------------------------------------------------
/// Constructs a |RendererContextSwitch|.
///
/// @param manager A reference to the manager.
/// @param context The context that is going to be set as the current
/// context.
RendererContextSwitch(RendererContextManager& manager,
std::unique_ptr<RendererContext> context);
~RendererContextSwitch();
//----------------------------------------------------------------------------
// Returns true if the context switching was successful.
bool GetResult();
private:
RendererContextManager& manager_;
bool result_;
FML_DISALLOW_COPY_AND_ASSIGN(RendererContextSwitch);
};
RendererContextManager(std::unique_ptr<RendererContext> context,
std::unique_ptr<RendererContext> resource_context);
~RendererContextManager();
//----------------------------------------------------------------------------
/// @brief Make the `context` to be the current context. It can be used
/// to set a context set by other programs to be the current context in
/// |RendererContextManager|. Which make sure Flutter doesn't pollute other
/// contexts.
///
/// @return A `RendererContextSwitch` which ensures the `context` is the
/// current context.
RendererContextSwitch MakeCurrent(std::unique_ptr<RendererContext> context);
//----------------------------------------------------------------------------
/// @brief Make the context Flutter uses on the raster thread for
/// onscreen rendering to be the current context.
///
/// @return A `RendererContextSwitch` which ensures the current context is
/// the `context` that used in the constructor.
RendererContextSwitch FlutterMakeCurrent();
//----------------------------------------------------------------------------
/// @brief Make the context Flutter uses for performing texture upload on
/// the IO thread to be the current context.
///
/// @return A `RendererContextSwitch` which ensures the current context is
/// the `resource_context` that used in the constructor.
RendererContextSwitch FlutterResourceMakeCurrent();
private:
std::unique_ptr<RendererContext> context_;
std::unique_ptr<RendererContext> resource_context_;
std::unique_ptr<RendererContext> current_;
std::vector<std::unique_ptr<RendererContext>> stored_;
bool PushContext(std::unique_ptr<RendererContext> context);
void PopContext();
FML_DISALLOW_COPY_AND_ASSIGN(RendererContextManager);
};
} // namespace flutter
#endif

View File

@@ -0,0 +1,119 @@
// 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/renderer_context_manager.h"
#include "gtest/gtest.h"
#include "renderer_context_test.h"
namespace flutter {
namespace testing {
TEST_F(RendererContextTest, StartWithNoContextFlutterMakeCurrent) {
auto flutter_context = std::make_unique<TestRendererContext>(0);
auto flutter_resource_context = std::make_unique<TestRendererContext>(1);
RendererContextManager manager(std::move(flutter_context),
std::move(flutter_resource_context));
// started with current_context as -1
TestRendererContext::SetCurrentContext(-1);
ASSERT_EQ(TestRendererContext::GetCurrentContext(), -1);
{
// made flutter context to be the current
auto context_switch = manager.FlutterMakeCurrent();
ASSERT_EQ(TestRendererContext::GetCurrentContext(), 0);
}
// flutter context is popped
ASSERT_EQ(TestRendererContext::GetCurrentContext(), -1);
}
TEST_F(RendererContextTest, StartWithNoContextFlutterResourceMakeCurrent) {
auto flutter_context = std::make_unique<TestRendererContext>(0);
auto flutter_resource_context = std::make_unique<TestRendererContext>(1);
RendererContextManager manager(std::move(flutter_context),
std::move(flutter_resource_context));
// started with current_context as -1
TestRendererContext::SetCurrentContext(-1);
ASSERT_EQ(TestRendererContext::GetCurrentContext(), -1);
{
// made resource context to be the current
auto context_switch = manager.FlutterResourceMakeCurrent();
ASSERT_EQ(TestRendererContext::GetCurrentContext(), 1);
}
// flutter context is popped
ASSERT_EQ(TestRendererContext::GetCurrentContext(), -1);
}
TEST_F(RendererContextTest, StartWithSomeContextFlutterMakeCurrent) {
auto flutter_context = std::make_unique<TestRendererContext>(0);
auto flutter_resource_context = std::make_unique<TestRendererContext>(1);
auto some_context = std::make_unique<TestRendererContext>(2);
RendererContextManager manager(std::move(flutter_context),
std::move(flutter_resource_context));
// started with some_context
auto context_switch = manager.MakeCurrent(std::move(some_context));
ASSERT_EQ(TestRendererContext::GetCurrentContext(), 2);
{
// made flutter context to be the current
auto context_switch2 = manager.FlutterMakeCurrent();
ASSERT_EQ(TestRendererContext::GetCurrentContext(), 0);
}
// flutter context is popped
ASSERT_EQ(TestRendererContext::GetCurrentContext(), 2);
}
TEST_F(RendererContextTest, StartWithSomeContextFlutterResourceMakeCurrent) {
auto flutter_context = std::make_unique<TestRendererContext>(0);
auto flutter_resource_context = std::make_unique<TestRendererContext>(1);
auto some_context = std::make_unique<TestRendererContext>(2);
RendererContextManager manager(std::move(flutter_context),
std::move(flutter_resource_context));
// started with some_context
auto context_switch = manager.MakeCurrent(std::move(some_context));
ASSERT_EQ(TestRendererContext::GetCurrentContext(), 2);
{
// made resource context to be the current
auto context_switch2 = manager.FlutterResourceMakeCurrent();
ASSERT_EQ(TestRendererContext::GetCurrentContext(), 1);
}
// flutter context is popped
ASSERT_EQ(TestRendererContext::GetCurrentContext(), 2);
}
TEST_F(RendererContextTest, Nested) {
auto flutter_context = std::make_unique<TestRendererContext>(0);
auto flutter_resource_context = std::make_unique<TestRendererContext>(1);
auto some_context = std::make_unique<TestRendererContext>(2);
RendererContextManager manager(std::move(flutter_context),
std::move(flutter_resource_context));
// started with some_context
auto context_switch = manager.MakeCurrent(std::move(some_context));
ASSERT_EQ(TestRendererContext::GetCurrentContext(), 2);
{
// made flutter context to be the current
auto context_switch2 = manager.FlutterMakeCurrent();
ASSERT_EQ(TestRendererContext::GetCurrentContext(), 0);
{
// made resource context to be the current
auto context_switch2 = manager.FlutterResourceMakeCurrent();
ASSERT_EQ(TestRendererContext::GetCurrentContext(), 1);
}
// resource context is popped
ASSERT_EQ(TestRendererContext::GetCurrentContext(), 0);
}
// flutter context is popped
ASSERT_EQ(TestRendererContext::GetCurrentContext(), 2);
}
} // namespace testing
} // namespace flutter

View File

@@ -0,0 +1,39 @@
// 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 "renderer_context_test.h"
namespace flutter {
namespace testing {
FML_THREAD_LOCAL fml::ThreadLocalUniquePtr<int> current_context;
RendererContextTest::RendererContextTest() = default;
TestRendererContext::TestRendererContext(int context) : context_(context){};
TestRendererContext::~TestRendererContext() = default;
bool TestRendererContext::SetCurrent() {
SetCurrentContext(context_);
return true;
};
void TestRendererContext::RemoveCurrent() {
SetCurrentContext(-1);
};
int TestRendererContext::GetContext() {
return context_;
};
int TestRendererContext::GetCurrentContext() {
return *(current_context.get());
};
void TestRendererContext::SetCurrentContext(int context) {
current_context.reset(new int(context));
};
} // namespace testing
} // namespace flutter

View File

@@ -0,0 +1,50 @@
// 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_RENDERER_CONTEXT_TEST_H_
#define FLUTTER_SHELL_RENDERER_CONTEXT_TEST_H_
#include "flutter/fml/thread_local.h"
#include "gtest/gtest.h"
#include "renderer_context_manager.h"
namespace flutter {
namespace testing {
class RendererContextTest : public ::testing::Test {
public:
RendererContextTest();
};
//------------------------------------------------------------------------------
/// The renderer context used for testing
class TestRendererContext : public RendererContext {
public:
TestRendererContext(int context);
~TestRendererContext() override;
bool SetCurrent() override;
void RemoveCurrent() override;
int GetContext();
static int GetCurrentContext();
//------------------------------------------------------------------------------
/// Set the current context without going through the
/// |RendererContextManager|.
///
/// This is to mimic how other programs outside flutter sets the context.
static void SetCurrentContext(int context);
private:
int context_;
FML_DISALLOW_COPY_AND_ASSIGN(TestRendererContext);
};
} // namespace testing
} // namespace flutter
#endif // FLUTTER_SHELL_RENDERER_CONTEXT_TEST_H_