forked from firka/flutter
Share the GrContext between lightweight engines (flutter/engine#28809)
This commit is contained in:
@@ -148,10 +148,12 @@ SkISize AndroidEGLSurface::GetSize() const {
|
||||
|
||||
AndroidContextGL::AndroidContextGL(
|
||||
AndroidRenderingAPI rendering_api,
|
||||
fml::RefPtr<AndroidEnvironmentGL> environment)
|
||||
fml::RefPtr<AndroidEnvironmentGL> environment,
|
||||
const TaskRunners& task_runners)
|
||||
: AndroidContext(AndroidRenderingAPI::kOpenGLES),
|
||||
environment_(environment),
|
||||
config_(nullptr) {
|
||||
config_(nullptr),
|
||||
task_runners_(task_runners) {
|
||||
if (!environment_->IsValid()) {
|
||||
FML_LOG(ERROR) << "Could not create an Android GL environment.";
|
||||
return;
|
||||
@@ -189,6 +191,26 @@ AndroidContextGL::AndroidContextGL(
|
||||
}
|
||||
|
||||
AndroidContextGL::~AndroidContextGL() {
|
||||
FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread());
|
||||
sk_sp<GrDirectContext> main_context = GetMainSkiaContext();
|
||||
SetMainSkiaContext(nullptr);
|
||||
fml::AutoResetWaitableEvent latch;
|
||||
// This context needs to be deallocated from the raster thread in order to
|
||||
// keep a coherent usage of egl from a single thread.
|
||||
fml::TaskRunner::RunNowOrPostTask(task_runners_.GetRasterTaskRunner(), [&] {
|
||||
if (main_context) {
|
||||
std::unique_ptr<AndroidEGLSurface> pbuffer_surface =
|
||||
CreatePbufferSurface();
|
||||
if (pbuffer_surface->MakeCurrent()) {
|
||||
main_context->releaseResourcesAndAbandonContext();
|
||||
main_context.reset();
|
||||
ClearCurrent();
|
||||
}
|
||||
}
|
||||
latch.Signal();
|
||||
});
|
||||
latch.Wait();
|
||||
|
||||
if (!TeardownContext(environment_->Display(), context_)) {
|
||||
FML_LOG(ERROR)
|
||||
<< "Could not tear down the EGL context. Possible resource leak.";
|
||||
|
||||
@@ -72,7 +72,8 @@ class AndroidEGLSurface {
|
||||
class AndroidContextGL : public AndroidContext {
|
||||
public:
|
||||
AndroidContextGL(AndroidRenderingAPI rendering_api,
|
||||
fml::RefPtr<AndroidEnvironmentGL> environment);
|
||||
fml::RefPtr<AndroidEnvironmentGL> environment,
|
||||
const TaskRunners& taskRunners);
|
||||
|
||||
~AndroidContextGL();
|
||||
|
||||
@@ -131,6 +132,7 @@ class AndroidContextGL : public AndroidContext {
|
||||
EGLContext context_;
|
||||
EGLContext resource_context_;
|
||||
bool valid_ = false;
|
||||
TaskRunners task_runners_;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(AndroidContextGL);
|
||||
};
|
||||
|
||||
@@ -1,11 +1,67 @@
|
||||
#define FML_USED_ON_EMBEDDER
|
||||
|
||||
#include <memory>
|
||||
#include "flutter/shell/common/thread_host.h"
|
||||
#include "flutter/shell/platform/android/android_context_gl.h"
|
||||
#include "flutter/shell/platform/android/android_environment_gl.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
TEST(AndroidContextGl, Create) {
|
||||
auto environment = fml::MakeRefCounted<flutter::AndroidEnvironmentGL>();
|
||||
auto context = std::make_unique<flutter::AndroidContextGL>(
|
||||
flutter::AndroidRenderingAPI::kOpenGLES, environment);
|
||||
EXPECT_NE(context.get(), nullptr);
|
||||
namespace flutter {
|
||||
namespace testing {
|
||||
namespace android {
|
||||
namespace {
|
||||
TaskRunners MakeTaskRunners(const std::string& thread_label,
|
||||
const ThreadHost& thread_host) {
|
||||
fml::MessageLoop::EnsureInitializedForCurrentThread();
|
||||
fml::RefPtr<fml::TaskRunner> platform_runner =
|
||||
fml::MessageLoop::GetCurrent().GetTaskRunner();
|
||||
|
||||
return TaskRunners(thread_label, platform_runner,
|
||||
thread_host.raster_thread->GetTaskRunner(),
|
||||
thread_host.ui_thread->GetTaskRunner(),
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST(AndroidContextGl, Create) {
|
||||
GrMockOptions main_context_options;
|
||||
sk_sp<GrDirectContext> main_context =
|
||||
GrDirectContext::MakeMock(&main_context_options);
|
||||
auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
|
||||
std::string thread_label =
|
||||
::testing::UnitTest::GetInstance()->current_test_info()->name();
|
||||
ThreadHost thread_host(thread_label, ThreadHost::Type::UI |
|
||||
ThreadHost::Type::RASTER |
|
||||
ThreadHost::Type::IO);
|
||||
TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host);
|
||||
auto context = std::make_unique<AndroidContextGL>(
|
||||
AndroidRenderingAPI::kOpenGLES, environment, task_runners);
|
||||
context->SetMainSkiaContext(main_context);
|
||||
EXPECT_NE(context.get(), nullptr);
|
||||
context.reset();
|
||||
EXPECT_TRUE(main_context->abandoned());
|
||||
}
|
||||
|
||||
TEST(AndroidContextGl, CreateSingleThread) {
|
||||
GrMockOptions main_context_options;
|
||||
sk_sp<GrDirectContext> main_context =
|
||||
GrDirectContext::MakeMock(&main_context_options);
|
||||
auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
|
||||
std::string thread_label =
|
||||
::testing::UnitTest::GetInstance()->current_test_info()->name();
|
||||
fml::MessageLoop::EnsureInitializedForCurrentThread();
|
||||
fml::RefPtr<fml::TaskRunner> platform_runner =
|
||||
fml::MessageLoop::GetCurrent().GetTaskRunner();
|
||||
TaskRunners task_runners =
|
||||
TaskRunners(thread_label, platform_runner, platform_runner,
|
||||
platform_runner, platform_runner);
|
||||
auto context = std::make_unique<AndroidContextGL>(
|
||||
AndroidRenderingAPI::kOpenGLES, environment, task_runners);
|
||||
context->SetMainSkiaContext(main_context);
|
||||
EXPECT_NE(context.get(), nullptr);
|
||||
context.reset();
|
||||
EXPECT_TRUE(main_context->abandoned());
|
||||
}
|
||||
} // namespace android
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
|
||||
@@ -52,7 +52,13 @@ std::unique_ptr<Surface> AndroidSurfaceGL::CreateGPUSurface(
|
||||
if (gr_context) {
|
||||
return std::make_unique<GPUSurfaceGL>(sk_ref_sp(gr_context), this, true);
|
||||
} else {
|
||||
return std::make_unique<GPUSurfaceGL>(this, true);
|
||||
sk_sp<GrDirectContext> main_skia_context =
|
||||
GLContextPtr()->GetMainSkiaContext();
|
||||
if (!main_skia_context) {
|
||||
main_skia_context = GPUSurfaceGL::MakeGLContext(this);
|
||||
GLContextPtr()->SetMainSkiaContext(main_skia_context);
|
||||
}
|
||||
return std::make_unique<GPUSurfaceGL>(main_skia_context, this, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,7 +177,14 @@ AndroidContextGL* AndroidSurfaceGL::GLContextPtr() const {
|
||||
|
||||
std::unique_ptr<Surface> AndroidSurfaceGL::CreatePbufferSurface() {
|
||||
onscreen_surface_ = GLContextPtr()->CreatePbufferSurface();
|
||||
return std::make_unique<GPUSurfaceGL>(this, true);
|
||||
sk_sp<GrDirectContext> main_skia_context =
|
||||
GLContextPtr()->GetMainSkiaContext();
|
||||
if (!main_skia_context) {
|
||||
main_skia_context = GPUSurfaceGL::MakeGLContext(this);
|
||||
GLContextPtr()->SetMainSkiaContext(main_skia_context);
|
||||
}
|
||||
|
||||
return std::make_unique<GPUSurfaceGL>(main_skia_context, this, true);
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
@@ -9,7 +9,11 @@ namespace flutter {
|
||||
AndroidContext::AndroidContext(AndroidRenderingAPI rendering_api)
|
||||
: rendering_api_(rendering_api) {}
|
||||
|
||||
AndroidContext::~AndroidContext() = default;
|
||||
AndroidContext::~AndroidContext() {
|
||||
if (main_context_) {
|
||||
main_context_->releaseResourcesAndAbandonContext();
|
||||
}
|
||||
};
|
||||
|
||||
AndroidRenderingAPI AndroidContext::RenderingApi() const {
|
||||
return rendering_api_;
|
||||
@@ -19,4 +23,13 @@ bool AndroidContext::IsValid() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
void AndroidContext::SetMainSkiaContext(
|
||||
const sk_sp<GrDirectContext>& main_context) {
|
||||
main_context_ = main_context;
|
||||
}
|
||||
|
||||
sk_sp<GrDirectContext> AndroidContext::GetMainSkiaContext() const {
|
||||
return main_context_;
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#define FLUTTER_SHELL_PLATFORM_ANDROID_ANDROID_CONTEXT_H_
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "flutter/fml/task_runner.h"
|
||||
#include "third_party/skia/include/gpu/GrDirectContext.h"
|
||||
|
||||
namespace flutter {
|
||||
@@ -28,9 +29,37 @@ class AndroidContext {
|
||||
|
||||
virtual bool IsValid() const;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Setter for the Skia context to be used by subsequent
|
||||
/// AndroidSurfaces.
|
||||
/// @details This is useful to reduce memory consumption when creating
|
||||
/// multiple AndroidSurfaces for the same AndroidContext.
|
||||
///
|
||||
/// The first AndroidSurface should set this for the
|
||||
/// AndroidContext if the AndroidContext does not yet have a
|
||||
/// Skia context to share via GetMainSkiaContext.
|
||||
///
|
||||
void SetMainSkiaContext(const sk_sp<GrDirectContext>& main_context);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Accessor for the Skia context associated with AndroidSurfaces
|
||||
/// and the raster thread.
|
||||
/// @details This context is created lazily by the AndroidSurface based
|
||||
/// on their respective rendering backend and set on this
|
||||
/// AndroidContext to share via SetMainSkiaContext.
|
||||
/// @returns `nullptr` when no Skia context has been set yet by its
|
||||
/// AndroidSurface via SetMainSkiaContext.
|
||||
/// @attention The software context doesn't have a Skia context, so this
|
||||
/// value will be nullptr.
|
||||
///
|
||||
sk_sp<GrDirectContext> GetMainSkiaContext() const;
|
||||
|
||||
private:
|
||||
const AndroidRenderingAPI rendering_api_;
|
||||
|
||||
// This is the Skia context used for on-screen rendering.
|
||||
sk_sp<GrDirectContext> main_context_;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(AndroidContext);
|
||||
};
|
||||
|
||||
|
||||
@@ -46,13 +46,14 @@ std::unique_ptr<AndroidSurface> AndroidSurfaceFactoryImpl::CreateSurface() {
|
||||
}
|
||||
|
||||
static std::shared_ptr<flutter::AndroidContext> CreateAndroidContext(
|
||||
bool use_software_rendering) {
|
||||
bool use_software_rendering,
|
||||
const flutter::TaskRunners task_runners) {
|
||||
if (use_software_rendering) {
|
||||
return std::make_shared<AndroidContext>(AndroidRenderingAPI::kSoftware);
|
||||
}
|
||||
return std::make_unique<AndroidContextGL>(
|
||||
AndroidRenderingAPI::kOpenGLES,
|
||||
fml::MakeRefCounted<AndroidEnvironmentGL>());
|
||||
fml::MakeRefCounted<AndroidEnvironmentGL>(), task_runners);
|
||||
}
|
||||
|
||||
PlatformViewAndroid::PlatformViewAndroid(
|
||||
@@ -60,10 +61,11 @@ PlatformViewAndroid::PlatformViewAndroid(
|
||||
flutter::TaskRunners task_runners,
|
||||
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
|
||||
bool use_software_rendering)
|
||||
: PlatformViewAndroid(delegate,
|
||||
std::move(task_runners),
|
||||
std::move(jni_facade),
|
||||
CreateAndroidContext(use_software_rendering)) {}
|
||||
: PlatformViewAndroid(
|
||||
delegate,
|
||||
std::move(task_runners),
|
||||
std::move(jni_facade),
|
||||
CreateAndroidContext(use_software_rendering, task_runners)) {}
|
||||
|
||||
PlatformViewAndroid::PlatformViewAndroid(
|
||||
PlatformView::Delegate& delegate,
|
||||
@@ -247,7 +249,8 @@ std::unique_ptr<Surface> PlatformViewAndroid::CreateRenderingSurface() {
|
||||
if (!android_surface_) {
|
||||
return nullptr;
|
||||
}
|
||||
return android_surface_->CreateGPUSurface();
|
||||
return android_surface_->CreateGPUSurface(
|
||||
android_context_->GetMainSkiaContext().get());
|
||||
}
|
||||
|
||||
// |PlatformView|
|
||||
|
||||
Reference in New Issue
Block a user