Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,8 @@ ORIGIN: ../../../flutter/shell/platform/android/android_context_gl_impeller.cc +
ORIGIN: ../../../flutter/shell/platform/android/android_context_gl_impeller.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/android_context_gl_skia.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/android_context_gl_skia.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/android_context_vulkan_impeller.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/android_context_vulkan_impeller.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/android_display.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/android_display.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/android_egl_surface.cc + ../../../flutter/LICENSE
Expand Down Expand Up @@ -4791,6 +4793,8 @@ FILE: ../../../flutter/shell/platform/android/android_context_gl_impeller.cc
FILE: ../../../flutter/shell/platform/android/android_context_gl_impeller.h
FILE: ../../../flutter/shell/platform/android/android_context_gl_skia.cc
FILE: ../../../flutter/shell/platform/android/android_context_gl_skia.h
FILE: ../../../flutter/shell/platform/android/android_context_vulkan_impeller.cc
FILE: ../../../flutter/shell/platform/android/android_context_vulkan_impeller.h
FILE: ../../../flutter/shell/platform/android/android_display.cc
FILE: ../../../flutter/shell/platform/android/android_display.h
FILE: ../../../flutter/shell/platform/android/android_egl_surface.cc
Expand Down
2 changes: 2 additions & 0 deletions shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ source_set("flutter_shell_native_src") {
"android_context_gl_impeller.h",
"android_context_gl_skia.cc",
"android_context_gl_skia.h",
"android_context_vulkan_impeller.cc",
"android_context_vulkan_impeller.h",
"android_display.cc",
"android_display.h",
"android_egl_surface.cc",
Expand Down
218 changes: 216 additions & 2 deletions shell/platform/android/android_context_gl_impeller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,229 @@

#include "flutter/shell/platform/android/android_context_gl_impeller.h"

#include "android_context_gl_impeller.h"
#include "flutter/impeller/renderer/backend/gles/context_gles.h"
#include "flutter/impeller/renderer/backend/gles/proc_table_gles.h"
#include "flutter/impeller/renderer/backend/gles/reactor_gles.h"
#include "flutter/impeller/toolkit/egl/context.h"
#include "flutter/impeller/toolkit/egl/surface.h"
#include "impeller/entity/gles/entity_shaders_gles.h"
#include "impeller/scene/shaders/gles/scene_shaders_gles.h"

namespace flutter {

class AndroidContextGLImpeller::ReactorWorker final
: public impeller::ReactorGLES::Worker {
public:
ReactorWorker() = default;

// |impeller::ReactorGLES::Worker|
~ReactorWorker() override = default;

// |impeller::ReactorGLES::Worker|
bool CanReactorReactOnCurrentThreadNow(
const impeller::ReactorGLES& reactor) const override {
impeller::ReaderLock lock(mutex_);
auto found = reactions_allowed_.find(std::this_thread::get_id());
if (found == reactions_allowed_.end()) {
return false;
}
return found->second;
}

void SetReactionsAllowedOnCurrentThread(bool allowed) {
impeller::WriterLock lock(mutex_);
reactions_allowed_[std::this_thread::get_id()] = allowed;
}

private:
mutable impeller::RWMutex mutex_;
std::map<std::thread::id, bool> reactions_allowed_ IPLR_GUARDED_BY(mutex_);

FML_DISALLOW_COPY_AND_ASSIGN(ReactorWorker);
};

static std::shared_ptr<impeller::Context> CreateImpellerContext(
const std::shared_ptr<impeller::ReactorGLES::Worker>& worker) {
auto proc_table = std::make_unique<impeller::ProcTableGLES>(
impeller::egl::CreateProcAddressResolver());

if (!proc_table->IsValid()) {
FML_LOG(ERROR) << "Could not create OpenGL proc table.";
return nullptr;
}

std::vector<std::shared_ptr<fml::Mapping>> shader_mappings = {
std::make_shared<fml::NonOwnedMapping>(
impeller_entity_shaders_gles_data,
impeller_entity_shaders_gles_length),
std::make_shared<fml::NonOwnedMapping>(
impeller_scene_shaders_gles_data, impeller_scene_shaders_gles_length),
};

auto context =
impeller::ContextGLES::Create(std::move(proc_table), shader_mappings);
if (!context) {
FML_LOG(ERROR) << "Could not create OpenGLES Impeller Context.";
return nullptr;
}

if (!context->AddReactorWorker(worker).has_value()) {
FML_LOG(ERROR) << "Could not add reactor worker.";
return nullptr;
}
FML_LOG(ERROR) << "Using the Impeller rendering backend.";
return context;
}

AndroidContextGLImpeller::AndroidContextGLImpeller()
: AndroidContext(AndroidRenderingAPI::kOpenGLES) {}
: AndroidContext(AndroidRenderingAPI::kOpenGLES),
reactor_worker_(std::shared_ptr<ReactorWorker>(new ReactorWorker())) {
auto display = std::make_unique<impeller::egl::Display>();
if (!display->IsValid()) {
FML_DLOG(ERROR) << "Could not create EGL display.";
return;
}

impeller::egl::ConfigDescriptor desc;
desc.api = impeller::egl::API::kOpenGLES2;
desc.color_format = impeller::egl::ColorFormat::kRGBA8888;
desc.depth_bits = impeller::egl::DepthBits::kZero;
desc.stencil_bits = impeller::egl::StencilBits::kEight;
desc.samples = impeller::egl::Samples::kFour;

desc.surface_type = impeller::egl::SurfaceType::kWindow;
std::unique_ptr<impeller::egl::Config> onscreen_config =
display->ChooseConfig(desc);
if (!onscreen_config) {
// Fallback for Android emulator.
desc.samples = impeller::egl::Samples::kOne;
onscreen_config = display->ChooseConfig(desc);
if (onscreen_config) {
FML_LOG(INFO) << "Warning: This device doesn't support MSAA for onscreen "
"framebuffers. Falling back to a single sample.";
} else {
FML_DLOG(ERROR) << "Could not choose onscreen config.";
return;
}
}

desc.surface_type = impeller::egl::SurfaceType::kPBuffer;
auto offscreen_config = display->ChooseConfig(desc);
if (!offscreen_config) {
FML_DLOG(ERROR) << "Could not choose offscreen config.";
return;
}

auto onscreen_context = display->CreateContext(*onscreen_config, nullptr);
if (!onscreen_context) {
FML_DLOG(ERROR) << "Could not create onscreen context.";
return;
}

auto offscreen_context =
display->CreateContext(*offscreen_config, onscreen_context.get());
if (!offscreen_context) {
FML_DLOG(ERROR) << "Could not create offscreen context.";
return;
}

// Creating the impeller::Context requires a current context, which requires
// some surface.
auto offscreen_surface =
display->CreatePixelBufferSurface(*offscreen_config, 1u, 1u);
if (!offscreen_context->MakeCurrent(*offscreen_surface)) {
FML_DLOG(ERROR) << "Could not make offscreen context current.";
return;
}

auto impeller_context = CreateImpellerContext(reactor_worker_);

if (!impeller_context) {
FML_DLOG(ERROR) << "Could not create Impeller context.";
return;
}

if (!offscreen_context->ClearCurrent()) {
FML_DLOG(ERROR) << "Could not clear offscreen context.";
return;
}
// Setup context listeners.
impeller::egl::Context::LifecycleListener listener =
[worker =
reactor_worker_](impeller::egl ::Context::LifecycleEvent event) {
switch (event) {
case impeller::egl::Context::LifecycleEvent::kDidMakeCurrent:
worker->SetReactionsAllowedOnCurrentThread(true);
break;
case impeller::egl::Context::LifecycleEvent::kWillClearCurrent:
worker->SetReactionsAllowedOnCurrentThread(false);
break;
}
};
if (!onscreen_context->AddLifecycleListener(listener).has_value() ||
!offscreen_context->AddLifecycleListener(listener).has_value()) {
FML_DLOG(ERROR) << "Could not add lifecycle listeners";
}

display_ = std::move(display);
onscreen_config_ = std::move(onscreen_config);
offscreen_config_ = std::move(offscreen_config);
onscreen_context_ = std::move(onscreen_context);
offscreen_context_ = std::move(offscreen_context);
SetImpellerContext(impeller_context);

is_valid_ = true;
}

AndroidContextGLImpeller::~AndroidContextGLImpeller() = default;

bool AndroidContextGLImpeller::IsValid() const {
return true;
return is_valid_;
}

bool AndroidContextGLImpeller::ResourceContextClearCurrent() {
if (!offscreen_context_) {
return false;
}

return offscreen_context_->ClearCurrent();
}

bool AndroidContextGLImpeller::ResourceContextMakeCurrent(
impeller::egl::Surface* offscreen_surface) {
if (!offscreen_context_ || !offscreen_surface) {
return false;
}

return offscreen_context_->MakeCurrent(*offscreen_surface);
}

std::unique_ptr<impeller::egl::Surface>
AndroidContextGLImpeller::CreateOffscreenSurface() {
return display_->CreatePixelBufferSurface(*offscreen_config_, 1u, 1u);
}

bool AndroidContextGLImpeller::OnscreenContextMakeCurrent(
impeller::egl::Surface* onscreen_surface) {
if (!onscreen_surface || !onscreen_context_) {
return false;
}

return onscreen_context_->MakeCurrent(*onscreen_surface);
}

bool AndroidContextGLImpeller::OnscreenContextClearCurrent() {
if (!onscreen_context_) {
return false;
}

return onscreen_context_->ClearCurrent();
}

std::unique_ptr<impeller::egl::Surface>
AndroidContextGLImpeller::CreateOnscreenSurface(EGLNativeWindowType window) {
return display_->CreateWindowSurface(*onscreen_config_, window);
}

} // namespace flutter
19 changes: 19 additions & 0 deletions shell/platform/android/android_context_gl_impeller.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define FLUTTER_SHELL_PLATFORM_ANDROID_ANDROID_CONTEXT_GL_IMPELLER_H_

#include "flutter/fml/macros.h"
#include "flutter/impeller/toolkit/egl/display.h"
#include "flutter/shell/platform/android/context/android_context.h"

namespace flutter {
Expand All @@ -19,7 +20,25 @@ class AndroidContextGLImpeller : public AndroidContext {
// |AndroidContext|
bool IsValid() const override;

bool ResourceContextMakeCurrent(impeller::egl::Surface* offscreen_surface);
bool ResourceContextClearCurrent();
std::unique_ptr<impeller::egl::Surface> CreateOffscreenSurface();
bool OnscreenContextMakeCurrent(impeller::egl::Surface* onscreen_surface);
bool OnscreenContextClearCurrent();
std::unique_ptr<impeller::egl::Surface> CreateOnscreenSurface(
EGLNativeWindowType window);

private:
class ReactorWorker;

std::shared_ptr<ReactorWorker> reactor_worker_;
std::unique_ptr<impeller::egl::Display> display_;
std::unique_ptr<impeller::egl::Config> onscreen_config_;
std::unique_ptr<impeller::egl::Config> offscreen_config_;
std::unique_ptr<impeller::egl::Context> onscreen_context_;
std::unique_ptr<impeller::egl::Context> offscreen_context_;
bool is_valid_ = false;

FML_DISALLOW_COPY_AND_ASSIGN(AndroidContextGLImpeller);
};

Expand Down
53 changes: 2 additions & 51 deletions shell/platform/android/android_context_gl_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,13 @@
#include "flutter/shell/platform/android/android_egl_surface.h"
#include "flutter/shell/platform/android/android_environment_gl.h"
#include "flutter/shell/platform/android/android_surface_gl_skia.h"
#include "flutter/shell/platform/android/jni/platform_view_android_jni.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace flutter {
namespace testing {
namespace android {
namespace {
class MockPlatformViewAndroidJNI : public PlatformViewAndroidJNI {
public:
MOCK_METHOD2(FlutterViewHandlePlatformMessage,
void(std::unique_ptr<flutter::PlatformMessage> message,
int responseId));
MOCK_METHOD2(FlutterViewHandlePlatformMessageResponse,
void(int responseId, std::unique_ptr<fml::Mapping> data));
MOCK_METHOD3(FlutterViewUpdateSemantics,
void(std::vector<uint8_t> buffer,
std::vector<std::string> strings,
std::vector<std::vector<uint8_t>> string_attribute_args));
MOCK_METHOD2(FlutterViewUpdateCustomAccessibilityActions,
void(std::vector<uint8_t> actions_buffer,
std::vector<std::string> strings));
MOCK_METHOD0(FlutterViewOnFirstFrame, void());
MOCK_METHOD0(FlutterViewOnPreEngineRestart, void());
MOCK_METHOD2(SurfaceTextureAttachToGLContext,
void(JavaLocalRef surface_texture, int textureId));
MOCK_METHOD1(SurfaceTextureUpdateTexImage,
void(JavaLocalRef surface_texture));
MOCK_METHOD2(SurfaceTextureGetTransformMatrix,
void(JavaLocalRef surface_texture, SkMatrix& transform));
MOCK_METHOD1(SurfaceTextureDetachFromGLContext,
void(JavaLocalRef surface_texture));
MOCK_METHOD8(FlutterViewOnDisplayPlatformView,
void(int view_id,
int x,
int y,
int width,
int height,
int viewWidth,
int viewHeight,
MutatorsStack mutators_stack));
MOCK_METHOD5(FlutterViewDisplayOverlaySurface,
void(int surface_id, int x, int y, int width, int height));
MOCK_METHOD0(FlutterViewBeginFrame, void());
MOCK_METHOD0(FlutterViewEndFrame, void());
MOCK_METHOD0(FlutterViewCreateOverlaySurface,
std::unique_ptr<PlatformViewAndroidJNI::OverlayMetadata>());
MOCK_METHOD0(FlutterViewDestroyOverlaySurfaces, void());
MOCK_METHOD1(FlutterViewComputePlatformResolvedLocale,
std::unique_ptr<std::vector<std::string>>(
std::vector<std::string> supported_locales_data));
MOCK_METHOD0(GetDisplayRefreshRate, double());
MOCK_METHOD1(RequestDartDeferredLibrary, bool(int loading_unit_id));
};

TaskRunners MakeTaskRunners(const std::string& thread_label,
const ThreadHost& thread_host) {
Expand Down Expand Up @@ -128,9 +81,8 @@ TEST(AndroidSurfaceGL, CreateSnapshopSurfaceWhenOnscreenSurfaceIsNotNull) {
TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host);
auto android_context = std::make_shared<AndroidContextGLSkia>(
AndroidRenderingAPI::kOpenGLES, environment, task_runners, 0);
auto jni = std::make_shared<MockPlatformViewAndroidJNI>();
auto android_surface =
std::make_unique<AndroidSurfaceGLSkia>(android_context, jni);
std::make_unique<AndroidSurfaceGLSkia>(android_context);
auto window = fml::MakeRefCounted<AndroidNativeWindow>(
nullptr, /*is_fake_window=*/true);
android_surface->SetNativeWindow(window);
Expand All @@ -156,9 +108,8 @@ TEST(AndroidSurfaceGL, CreateSnapshopSurfaceWhenOnscreenSurfaceIsNull) {
TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host);
auto android_context = std::make_shared<AndroidContextGLSkia>(
AndroidRenderingAPI::kOpenGLES, environment, task_runners, 0);
auto jni = std::make_shared<MockPlatformViewAndroidJNI>();
auto android_surface =
std::make_unique<AndroidSurfaceGLSkia>(android_context, jni);
std::make_unique<AndroidSurfaceGLSkia>(android_context);
EXPECT_EQ(android_surface->GetOnscreenSurface(), nullptr);
android_surface->CreateSnapshotSurface();
EXPECT_NE(android_surface->GetOnscreenSurface(), nullptr);
Expand Down
Loading