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 all 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
2 changes: 1 addition & 1 deletion ci/licenses_golden/excluded_files
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@
../../../flutter/shell/common/variable_refresh_rate_display_unittests.cc
../../../flutter/shell/common/vsync_waiter_unittests.cc
../../../flutter/shell/platform/android/.gitignore
../../../flutter/shell/platform/android/android_context_gl_impeller_unittests.cc
../../../flutter/shell/platform/android/android_context_gl_unittests.cc
../../../flutter/shell/platform/android/android_shell_holder_unittests.cc
../../../flutter/shell/platform/android/android_surface_gl_impeller_unittests.cc
../../../flutter/shell/platform/android/apk_asset_provider_unittests.cc
../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc
../../../flutter/shell/platform/android/external_view_embedder/surface_pool_unittests.cc
Expand Down
4 changes: 4 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -2226,6 +2226,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 @@ -4843,6 +4845,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
4 changes: 3 additions & 1 deletion shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ executable("flutter_shell_native_unittests") {
visibility = [ "*" ]
testonly = true
sources = [
"android_context_gl_impeller_unittests.cc",
"android_context_gl_unittests.cc",
"android_shell_holder_unittests.cc",
"android_surface_gl_impeller_unittests.cc",
"apk_asset_provider_unittests.cc",
"flutter_shell_native_unittests.cc",
]
Expand Down Expand Up @@ -72,6 +72,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
219 changes: 216 additions & 3 deletions shell/platform/android/android_context_gl_impeller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,228 @@

#include "flutter/shell/platform/android/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 {

AndroidContextGLImpeller::AndroidContextGLImpeller()
: AndroidContext(AndroidRenderingAPI::kOpenGLES) {}
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(
std::unique_ptr<impeller::egl::Display> display)
: AndroidContext(AndroidRenderingAPI::kOpenGLES),
reactor_worker_(std::shared_ptr<ReactorWorker>(new ReactorWorker())),
display_(std::move(display)) {
if (!display_ || !display_->IsValid()) {
FML_DLOG(ERROR) << "Could not create context with invalid 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";
}

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
22 changes: 21 additions & 1 deletion shell/platform/android/android_context_gl_impeller.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,40 @@
#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 {

class AndroidContextGLImpeller : public AndroidContext {
public:
AndroidContextGLImpeller();
explicit AndroidContextGLImpeller(
std::unique_ptr<impeller::egl::Display> display);

~AndroidContextGLImpeller();

// |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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaaclarke FYI - these are tests you added that got moved here now because the context now is in charge of choosing the config instead of the surface.

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "flutter/shell/platform/android/android_surface_gl_impeller.h"
#include "flutter/shell/platform/android/jni/jni_mock.h"
#include "flutter/shell/platform/android/android_context_gl_impeller.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand All @@ -24,10 +23,7 @@ class MockDisplay : public impeller::egl::Display {
};
} // namespace

TEST(AndroidSurfaceGLImpeller, MSAAFirstAttempt) {
auto context =
std::make_shared<AndroidContext>(AndroidRenderingAPI::kSoftware);
auto jni = std::make_shared<JNIMock>();
TEST(AndroidContextGLImpeller, MSAAFirstAttempt) {
auto display = std::make_unique<MockDisplay>();
EXPECT_CALL(*display, IsValid).WillRepeatedly(Return(true));
auto first_result = std::make_unique<Config>(ConfigDescriptor(), EGLConfig());
Expand All @@ -46,15 +42,11 @@ TEST(AndroidSurfaceGLImpeller, MSAAFirstAttempt) {
.WillOnce(Return(ByMove(std::move(second_result))));
ON_CALL(*display, ChooseConfig(_))
.WillByDefault(Return(ByMove(std::unique_ptr<Config>())));
auto surface = std::make_unique<AndroidSurfaceGLImpeller>(context, jni,
std::move(display));
ASSERT_TRUE(surface);
auto context = std::make_unique<AndroidContextGLImpeller>(std::move(display));
ASSERT_TRUE(context);
}

TEST(AndroidSurfaceGLImpeller, FallbackForEmulator) {
auto context =
std::make_shared<AndroidContext>(AndroidRenderingAPI::kSoftware);
auto jni = std::make_shared<JNIMock>();
TEST(AndroidContextGLImpeller, FallbackForEmulator) {
auto display = std::make_unique<MockDisplay>();
EXPECT_CALL(*display, IsValid).WillRepeatedly(Return(true));
std::unique_ptr<Config> first_result;
Expand All @@ -81,9 +73,8 @@ TEST(AndroidSurfaceGLImpeller, FallbackForEmulator) {
.WillOnce(Return(ByMove(std::move(third_result))));
ON_CALL(*display, ChooseConfig(_))
.WillByDefault(Return(ByMove(std::unique_ptr<Config>())));
auto surface = std::make_unique<AndroidSurfaceGLImpeller>(context, jni,
std::move(display));
ASSERT_TRUE(surface);
auto context = std::make_unique<AndroidContextGLImpeller>(std::move(display));
ASSERT_TRUE(context);
}
} // namespace testing
} // namespace flutter
Loading