diff --git a/engine/src/flutter/ci/licenses_golden/excluded_files b/engine/src/flutter/ci/licenses_golden/excluded_files index 6edeb2f7d3..963bb80d50 100644 --- a/engine/src/flutter/ci/licenses_golden/excluded_files +++ b/engine/src/flutter/ci/licenses_golden/excluded_files @@ -224,6 +224,7 @@ ../../../flutter/shell/platform/android/.gitignore ../../../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 diff --git a/engine/src/flutter/impeller/toolkit/egl/display.h b/engine/src/flutter/impeller/toolkit/egl/display.h index 07f5d25129..7970c9e198 100644 --- a/engine/src/flutter/impeller/toolkit/egl/display.h +++ b/engine/src/flutter/impeller/toolkit/egl/display.h @@ -21,21 +21,21 @@ class Display { public: Display(); - ~Display(); + virtual ~Display(); - bool IsValid() const; + virtual bool IsValid() const; - std::unique_ptr ChooseConfig(ConfigDescriptor config) const; + virtual std::unique_ptr ChooseConfig(ConfigDescriptor config) const; - std::unique_ptr CreateContext(const Config& config, - const Context* share_context); + virtual std::unique_ptr CreateContext(const Config& config, + const Context* share_context); - std::unique_ptr CreateWindowSurface(const Config& config, - EGLNativeWindowType window); + virtual std::unique_ptr CreateWindowSurface( + const Config& config, + EGLNativeWindowType window); - std::unique_ptr CreatePixelBufferSurface(const Config& config, - size_t width, - size_t height); + virtual std::unique_ptr + CreatePixelBufferSurface(const Config& config, size_t width, size_t height); private: EGLDisplay display_ = EGL_NO_DISPLAY; diff --git a/engine/src/flutter/shell/platform/android/BUILD.gn b/engine/src/flutter/shell/platform/android/BUILD.gn index e056731dd6..37ad46e02e 100644 --- a/engine/src/flutter/shell/platform/android/BUILD.gn +++ b/engine/src/flutter/shell/platform/android/BUILD.gn @@ -41,12 +41,14 @@ executable("flutter_shell_native_unittests") { sources = [ "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", ] public_configs = [ "//flutter:config" ] deps = [ ":flutter_shell_native_src", + "//flutter/shell/platform/android/jni:jni_mock", "//third_party/googletest:gmock", "//third_party/googletest:gtest", ] diff --git a/engine/src/flutter/shell/platform/android/android_surface_gl_impeller.cc b/engine/src/flutter/shell/platform/android/android_surface_gl_impeller.cc index ca4040ec5e..fb2abdff6d 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_gl_impeller.cc +++ b/engine/src/flutter/shell/platform/android/android_surface_gl_impeller.cc @@ -81,12 +81,13 @@ static std::shared_ptr CreateImpellerContext( AndroidSurfaceGLImpeller::AndroidSurfaceGLImpeller( const std::shared_ptr& android_context, - const std::shared_ptr& jni_facade) + const std::shared_ptr& jni_facade, + std::unique_ptr display) : AndroidSurface(android_context), - reactor_worker_(std::shared_ptr(new ReactorWorker())) { - auto display = std::make_unique(); - if (!display->IsValid()) { - FML_DLOG(ERROR) << "Could not create EGL display."; + reactor_worker_(std::shared_ptr(new ReactorWorker())), + display_(std::move(display)) { + if (!display_->IsValid()) { + FML_DLOG(ERROR) << "Could not create surface with invalid Display."; return; } @@ -99,11 +100,11 @@ AndroidSurfaceGLImpeller::AndroidSurfaceGLImpeller( desc.surface_type = impeller::egl::SurfaceType::kWindow; std::unique_ptr onscreen_config = - display->ChooseConfig(desc); + display_->ChooseConfig(desc); if (!onscreen_config) { // Fallback for Android emulator. desc.samples = impeller::egl::Samples::kOne; - onscreen_config = display->ChooseConfig(desc); + 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."; @@ -114,27 +115,27 @@ AndroidSurfaceGLImpeller::AndroidSurfaceGLImpeller( } desc.surface_type = impeller::egl::SurfaceType::kPBuffer; - auto offscreen_config = display->ChooseConfig(desc); + 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); + 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()); + display_->CreateContext(*offscreen_config, onscreen_context.get()); if (!offscreen_context) { FML_DLOG(ERROR) << "Could not create offscreen context."; return; } auto offscreen_surface = - display->CreatePixelBufferSurface(*offscreen_config, 1u, 1u); + display_->CreatePixelBufferSurface(*offscreen_config, 1u, 1u); if (!offscreen_surface) { FML_DLOG(ERROR) << "Could not create offscreen surface."; return; @@ -175,7 +176,6 @@ AndroidSurfaceGLImpeller::AndroidSurfaceGLImpeller( FML_DLOG(ERROR) << "Could not add lifecycle listeners"; } - display_ = std::move(display); onscreen_config_ = std::move(onscreen_config); offscreen_config_ = std::move(offscreen_config); offscreen_surface_ = std::move(offscreen_surface); diff --git a/engine/src/flutter/shell/platform/android/android_surface_gl_impeller.h b/engine/src/flutter/shell/platform/android/android_surface_gl_impeller.h index b8ca423072..19109743f8 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_gl_impeller.h +++ b/engine/src/flutter/shell/platform/android/android_surface_gl_impeller.h @@ -19,7 +19,8 @@ class AndroidSurfaceGLImpeller final : public GPUSurfaceGLDelegate, public: AndroidSurfaceGLImpeller( const std::shared_ptr& android_context, - const std::shared_ptr& jni_facade); + const std::shared_ptr& jni_facade, + std::unique_ptr display); // |AndroidSurface| ~AndroidSurfaceGLImpeller() override; diff --git a/engine/src/flutter/shell/platform/android/android_surface_gl_impeller_unittests.cc b/engine/src/flutter/shell/platform/android/android_surface_gl_impeller_unittests.cc new file mode 100644 index 0000000000..37f11aaa3f --- /dev/null +++ b/engine/src/flutter/shell/platform/android/android_surface_gl_impeller_unittests.cc @@ -0,0 +1,89 @@ +#include "flutter/shell/platform/android/android_surface_gl_impeller.h" +#include "flutter/shell/platform/android/jni/jni_mock.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { + +using ::testing::_; +using ::testing::AllOf; +using ::testing::ByMove; +using ::testing::Field; +using ::testing::Matcher; +using ::testing::Return; + +using ::impeller::egl::Config; +using ::impeller::egl::ConfigDescriptor; + +namespace { +class MockDisplay : public impeller::egl::Display { + public: + MOCK_CONST_METHOD0(IsValid, bool()); + MOCK_CONST_METHOD1(ChooseConfig, std::unique_ptr(ConfigDescriptor)); +}; +} // namespace + +TEST(AndroidSurfaceGLImpeller, MSAAFirstAttempt) { + auto context = + std::make_shared(AndroidRenderingAPI::kSoftware); + auto jni = std::make_shared(); + auto display = std::make_unique(); + EXPECT_CALL(*display, IsValid).WillRepeatedly(Return(true)); + auto first_result = std::make_unique(ConfigDescriptor(), EGLConfig()); + auto second_result = + std::make_unique(ConfigDescriptor(), EGLConfig()); + EXPECT_CALL( + *display, + ChooseConfig(Matcher(AllOf( + Field(&ConfigDescriptor::samples, impeller::egl::Samples::kFour), + Field(&ConfigDescriptor::surface_type, + impeller::egl::SurfaceType::kWindow))))) + .WillOnce(Return(ByMove(std::move(first_result)))); + EXPECT_CALL(*display, ChooseConfig(Matcher( + Field(&ConfigDescriptor::surface_type, + impeller::egl::SurfaceType::kPBuffer)))) + .WillOnce(Return(ByMove(std::move(second_result)))); + ON_CALL(*display, ChooseConfig(_)) + .WillByDefault(Return(ByMove(std::unique_ptr()))); + auto surface = std::make_unique(context, jni, + std::move(display)); + ASSERT_TRUE(surface); +} + +TEST(AndroidSurfaceGLImpeller, FallbackForEmulator) { + auto context = + std::make_shared(AndroidRenderingAPI::kSoftware); + auto jni = std::make_shared(); + auto display = std::make_unique(); + EXPECT_CALL(*display, IsValid).WillRepeatedly(Return(true)); + std::unique_ptr first_result; + auto second_result = + std::make_unique(ConfigDescriptor(), EGLConfig()); + auto third_result = std::make_unique(ConfigDescriptor(), EGLConfig()); + EXPECT_CALL( + *display, + ChooseConfig(Matcher(AllOf( + Field(&ConfigDescriptor::samples, impeller::egl::Samples::kFour), + Field(&ConfigDescriptor::surface_type, + impeller::egl::SurfaceType::kWindow))))) + .WillOnce(Return(ByMove(std::move(first_result)))); + EXPECT_CALL( + *display, + ChooseConfig(Matcher( + AllOf(Field(&ConfigDescriptor::samples, impeller::egl::Samples::kOne), + Field(&ConfigDescriptor::surface_type, + impeller::egl::SurfaceType::kWindow))))) + .WillOnce(Return(ByMove(std::move(second_result)))); + EXPECT_CALL(*display, ChooseConfig(Matcher( + Field(&ConfigDescriptor::surface_type, + impeller::egl::SurfaceType::kPBuffer)))) + .WillOnce(Return(ByMove(std::move(third_result)))); + ON_CALL(*display, ChooseConfig(_)) + .WillByDefault(Return(ByMove(std::unique_ptr()))); + auto surface = std::make_unique(context, jni, + std::move(display)); + ASSERT_TRUE(surface); +} +} // namespace testing +} // namespace flutter diff --git a/engine/src/flutter/shell/platform/android/platform_view_android.cc b/engine/src/flutter/shell/platform/android/platform_view_android.cc index 804b0f111d..bb34af3cf2 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android.cc @@ -54,8 +54,9 @@ std::unique_ptr AndroidSurfaceFactoryImpl::CreateSurface() { android_context_, jni_facade_, enable_vulkan_validation_); #else - return std::make_unique(android_context_, - jni_facade_); + return std::make_unique( + android_context_, jni_facade_, + std::make_unique()); #endif } else { return std::make_unique(android_context_,