From 1eecbf12568d7f48be65ede5cebe73180eae2b40 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 30 Jan 2025 10:04:40 -0800 Subject: [PATCH] [Android] HC++ plumbing. (#162407) We'll need the JNI fascade to create the surface transaction for HC++ mode. Split into a separate PR so that the "real" HC++ PR isn't 5000 lines long. --- .../android/android_context_gl_unittests.cc | 2 +- .../platform/android/android_surface_gl_impeller.cc | 3 ++- .../platform/android/android_surface_gl_impeller.h | 4 +++- .../platform/android/android_surface_gl_skia.cc | 3 ++- .../platform/android/android_surface_gl_skia.h | 4 +++- .../platform/android/android_surface_software.cc | 3 ++- .../platform/android/android_surface_software.h | 4 +++- .../platform/android/android_surface_vk_impeller.cc | 3 ++- .../platform/android/android_surface_vk_impeller.h | 4 +++- .../external_view_embedder_unittests.cc | 13 +++++++------ .../android/external_view_embedder/surface_pool.cc | 2 +- .../surface_pool_unittests.cc | 13 +++++++------ .../shell/platform/android/platform_view_android.cc | 8 ++++---- .../platform/android/surface/android_surface.h | 4 +++- .../platform/android/surface/android_surface_mock.h | 3 ++- 15 files changed, 45 insertions(+), 28 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/android_context_gl_unittests.cc b/engine/src/flutter/shell/platform/android/android_context_gl_unittests.cc index 33416e01822..049f3f0745f 100644 --- a/engine/src/flutter/shell/platform/android/android_context_gl_unittests.cc +++ b/engine/src/flutter/shell/platform/android/android_context_gl_unittests.cc @@ -170,7 +170,7 @@ TEST(AndroidSurfaceGL, CreateSnapshopSurfaceWhenOnscreenSurfaceIsNotNull) { std::make_unique(android_context); auto window = fml::MakeRefCounted( nullptr, /*is_fake_window=*/true); - android_surface->SetNativeWindow(window); + android_surface->SetNativeWindow(window, nullptr); auto onscreen_surface = android_surface->GetOnscreenSurface(); EXPECT_NE(onscreen_surface, nullptr); android_surface->CreateSnapshotSurface(); 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 405f7f2711f..918bbef6655 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 @@ -74,7 +74,8 @@ bool AndroidSurfaceGLImpeller::ResourceContextClearCurrent() { // |AndroidSurface| bool AndroidSurfaceGLImpeller::SetNativeWindow( - fml::RefPtr window) { + fml::RefPtr window, + const std::shared_ptr& jni_facade) { native_window_ = std::move(window); return RecreateOnscreenSurfaceAndMakeOnscreenContextCurrent(); } 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 3066a8552ee..b300aae9feb 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 @@ -43,7 +43,9 @@ class AndroidSurfaceGLImpeller final : public GPUSurfaceGLDelegate, bool ResourceContextClearCurrent() override; // |AndroidSurface| - bool SetNativeWindow(fml::RefPtr window) override; + bool SetNativeWindow( + fml::RefPtr window, + const std::shared_ptr& jni_facade) override; // |AndroidSurface| std::unique_ptr CreateSnapshotSurface() override; diff --git a/engine/src/flutter/shell/platform/android/android_surface_gl_skia.cc b/engine/src/flutter/shell/platform/android/android_surface_gl_skia.cc index 6a5dda8a060..69eab845b20 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_gl_skia.cc +++ b/engine/src/flutter/shell/platform/android/android_surface_gl_skia.cc @@ -100,7 +100,8 @@ bool AndroidSurfaceGLSkia::ResourceContextClearCurrent() { } bool AndroidSurfaceGLSkia::SetNativeWindow( - fml::RefPtr window) { + fml::RefPtr window, + const std::shared_ptr& jni_facade) { FML_DCHECK(IsValid()); FML_DCHECK(window); native_window_ = window; diff --git a/engine/src/flutter/shell/platform/android/android_surface_gl_skia.h b/engine/src/flutter/shell/platform/android/android_surface_gl_skia.h index 1c35692dc3a..976f26ef559 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_gl_skia.h +++ b/engine/src/flutter/shell/platform/android/android_surface_gl_skia.h @@ -46,7 +46,9 @@ class AndroidSurfaceGLSkia final : public GPUSurfaceGLDelegate, bool ResourceContextClearCurrent() override; // |AndroidSurface| - bool SetNativeWindow(fml::RefPtr window) override; + bool SetNativeWindow( + fml::RefPtr window, + const std::shared_ptr& jni_facade) override; // |AndroidSurface| virtual std::unique_ptr CreateSnapshotSurface() override; diff --git a/engine/src/flutter/shell/platform/android/android_surface_software.cc b/engine/src/flutter/shell/platform/android/android_surface_software.cc index db5881c684b..11e2dc385df 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_software.cc +++ b/engine/src/flutter/shell/platform/android/android_surface_software.cc @@ -150,7 +150,8 @@ bool AndroidSurfaceSoftware::OnScreenSurfaceResize(const SkISize& size) { } bool AndroidSurfaceSoftware::SetNativeWindow( - fml::RefPtr window) { + fml::RefPtr window, + const std::shared_ptr& jni_facade) { native_window_ = std::move(window); if (!(native_window_ && native_window_->IsValid())) { return false; diff --git a/engine/src/flutter/shell/platform/android/android_surface_software.h b/engine/src/flutter/shell/platform/android/android_surface_software.h index 1b7083aa7dd..fd62c8053d6 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_software.h +++ b/engine/src/flutter/shell/platform/android/android_surface_software.h @@ -43,7 +43,9 @@ class AndroidSurfaceSoftware final : public AndroidSurface, bool OnScreenSurfaceResize(const SkISize& size) override; // |AndroidSurface| - bool SetNativeWindow(fml::RefPtr window) override; + bool SetNativeWindow( + fml::RefPtr window, + const std::shared_ptr& jni_facade) override; // |GPUSurfaceSoftwareDelegate| sk_sp AcquireBackingStore(const SkISize& size) override; diff --git a/engine/src/flutter/shell/platform/android/android_surface_vk_impeller.cc b/engine/src/flutter/shell/platform/android/android_surface_vk_impeller.cc index 69d95751280..c0338281dba 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_vk_impeller.cc +++ b/engine/src/flutter/shell/platform/android/android_surface_vk_impeller.cc @@ -81,7 +81,8 @@ bool AndroidSurfaceVKImpeller::ResourceContextClearCurrent() { } bool AndroidSurfaceVKImpeller::SetNativeWindow( - fml::RefPtr window) { + fml::RefPtr window, + const std::shared_ptr& jni_facade) { if (window && (native_window_ == window)) { return OnScreenSurfaceResize(window->GetSize()); } diff --git a/engine/src/flutter/shell/platform/android/android_surface_vk_impeller.h b/engine/src/flutter/shell/platform/android/android_surface_vk_impeller.h index 01dde084402..b962270321f 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_vk_impeller.h +++ b/engine/src/flutter/shell/platform/android/android_surface_vk_impeller.h @@ -45,7 +45,9 @@ class AndroidSurfaceVKImpeller : public AndroidSurface { std::shared_ptr GetImpellerContext() override; // |AndroidSurface| - bool SetNativeWindow(fml::RefPtr window) override; + bool SetNativeWindow( + fml::RefPtr window, + const std::shared_ptr& jni_facade) override; private: std::shared_ptr surface_context_vk_; diff --git a/engine/src/flutter/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc b/engine/src/flutter/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc index 0dfa040fb0a..5a75d8fb0c9 100644 --- a/engine/src/flutter/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc +++ b/engine/src/flutter/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc @@ -24,6 +24,7 @@ namespace flutter { namespace testing { +using ::testing::_; using ::testing::ByMove; using ::testing::Return; @@ -295,7 +296,7 @@ TEST(AndroidExternalViewEmbedder, SubmitFlutterView) { EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get())) .WillOnce(Return(ByMove(std::move(surface_mock)))); - EXPECT_CALL(*android_surface_mock, SetNativeWindow(window)); + EXPECT_CALL(*android_surface_mock, SetNativeWindow(window, _)); return android_surface_mock; }); @@ -504,7 +505,7 @@ TEST(AndroidExternalViewEmbedder, OverlayCoverTwoPlatformViews) { EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get())) .WillOnce(Return(ByMove(std::move(surface_mock)))); - EXPECT_CALL(*android_surface_mock, SetNativeWindow(window)); + EXPECT_CALL(*android_surface_mock, SetNativeWindow(window, _)); return android_surface_mock; }); auto embedder = std::make_unique( @@ -606,7 +607,7 @@ TEST(AndroidExternalViewEmbedder, SubmitFrameOverlayComposition) { EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get())) .WillOnce(Return(ByMove(std::move(surface_mock)))); - EXPECT_CALL(*android_surface_mock, SetNativeWindow(window)); + EXPECT_CALL(*android_surface_mock, SetNativeWindow(window, _)); return android_surface_mock; }); auto embedder = std::make_unique( @@ -713,7 +714,7 @@ TEST(AndroidExternalViewEmbedder, SubmitFramePlatformViewWithoutAnyOverlay) { EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get())) .WillOnce(Return(ByMove(std::move(surface_mock)))); - EXPECT_CALL(*android_surface_mock, SetNativeWindow(window)); + EXPECT_CALL(*android_surface_mock, SetNativeWindow(window, _)); return android_surface_mock; }); auto embedder = std::make_unique( @@ -804,7 +805,7 @@ TEST(AndroidExternalViewEmbedder, DestroyOverlayLayersOnSizeChange) { EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get())) .WillOnce(Return(ByMove(std::move(surface_mock)))); - EXPECT_CALL(*android_surface_mock, SetNativeWindow(window)); + EXPECT_CALL(*android_surface_mock, SetNativeWindow(window, _)); return android_surface_mock; }); @@ -896,7 +897,7 @@ TEST(AndroidExternalViewEmbedder, DoesNotDestroyOverlayLayersOnSizeChange) { EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get())) .WillOnce(Return(ByMove(std::move(surface_mock)))); - EXPECT_CALL(*android_surface_mock, SetNativeWindow(window)); + EXPECT_CALL(*android_surface_mock, SetNativeWindow(window, _)); return android_surface_mock; }); diff --git a/engine/src/flutter/shell/platform/android/external_view_embedder/surface_pool.cc b/engine/src/flutter/shell/platform/android/external_view_embedder/surface_pool.cc index 6e71e97247c..7290a5816bf 100644 --- a/engine/src/flutter/shell/platform/android/external_view_embedder/surface_pool.cc +++ b/engine/src/flutter/shell/platform/android/external_view_embedder/surface_pool.cc @@ -45,7 +45,7 @@ std::shared_ptr SurfacePool::GetLayer( jni_facade->FlutterViewCreateOverlaySurface(); FML_CHECK(java_metadata->window); - android_surface->SetNativeWindow(java_metadata->window); + android_surface->SetNativeWindow(java_metadata->window, jni_facade); std::unique_ptr surface = android_surface->CreateGPUSurface(gr_context); diff --git a/engine/src/flutter/shell/platform/android/external_view_embedder/surface_pool_unittests.cc b/engine/src/flutter/shell/platform/android/external_view_embedder/surface_pool_unittests.cc index 1dbe342afaa..09cf54bcfca 100644 --- a/engine/src/flutter/shell/platform/android/external_view_embedder/surface_pool_unittests.cc +++ b/engine/src/flutter/shell/platform/android/external_view_embedder/surface_pool_unittests.cc @@ -15,6 +15,7 @@ namespace flutter { namespace testing { +using ::testing::_; using ::testing::ByMove; using ::testing::Return; @@ -54,7 +55,7 @@ TEST(SurfacePool, GetLayerAllocateOneLayer) { std::make_shared([gr_context, window]() { auto android_surface_mock = std::make_unique(); EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get())); - EXPECT_CALL(*android_surface_mock, SetNativeWindow(window)); + EXPECT_CALL(*android_surface_mock, SetNativeWindow(window, _)); EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true)); return android_surface_mock; }); @@ -85,7 +86,7 @@ TEST(SurfacePool, GetUnusedLayers) { std::make_shared([gr_context, window]() { auto android_surface_mock = std::make_unique(); EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get())); - EXPECT_CALL(*android_surface_mock, SetNativeWindow(window)); + EXPECT_CALL(*android_surface_mock, SetNativeWindow(window, _)); EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true)); return android_surface_mock; }); @@ -124,7 +125,7 @@ TEST(SurfacePool, GetLayerRecycle) { EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context_2.get())); // Set the native window once. - EXPECT_CALL(*android_surface_mock, SetNativeWindow(window)); + EXPECT_CALL(*android_surface_mock, SetNativeWindow(window, _)); EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true)); return android_surface_mock; }); @@ -167,7 +168,7 @@ TEST(SurfacePool, GetLayerAllocateTwoLayers) { std::make_shared([gr_context, window]() { auto android_surface_mock = std::make_unique(); EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get())); - EXPECT_CALL(*android_surface_mock, SetNativeWindow(window)); + EXPECT_CALL(*android_surface_mock, SetNativeWindow(window, _)); EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true)); return android_surface_mock; }); @@ -206,7 +207,7 @@ TEST(SurfacePool, DestroyLayers) { std::make_shared([gr_context, window]() { auto android_surface_mock = std::make_unique(); EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get())); - EXPECT_CALL(*android_surface_mock, SetNativeWindow(window)); + EXPECT_CALL(*android_surface_mock, SetNativeWindow(window, _)); EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true)); return android_surface_mock; }); @@ -235,7 +236,7 @@ TEST(SurfacePool, DestroyLayersFrameSizeChanged) { std::make_shared([gr_context, window]() { auto android_surface_mock = std::make_unique(); EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get())); - EXPECT_CALL(*android_surface_mock, SetNativeWindow(window)); + EXPECT_CALL(*android_surface_mock, SetNativeWindow(window, _)); EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true)); return android_surface_mock; }); 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 9a848aa1682..694b3ebc13f 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android.cc @@ -148,8 +148,8 @@ void PlatformViewAndroid::NotifyCreated( fml::TaskRunner::RunNowOrPostTask( task_runners_.GetRasterTaskRunner(), [&latch, surface = android_surface_.get(), - native_window = std::move(native_window)]() { - surface->SetNativeWindow(native_window); + native_window = std::move(native_window), jni_facade = jni_facade_]() { + surface->SetNativeWindow(native_window, jni_facade); latch.Signal(); }); latch.Wait(); @@ -165,9 +165,9 @@ void PlatformViewAndroid::NotifySurfaceWindowChanged( fml::TaskRunner::RunNowOrPostTask( task_runners_.GetRasterTaskRunner(), [&latch, surface = android_surface_.get(), - native_window = std::move(native_window)]() { + native_window = std::move(native_window), jni_facade = jni_facade_]() { surface->TeardownOnScreenContext(); - surface->SetNativeWindow(native_window); + surface->SetNativeWindow(native_window, jni_facade); latch.Signal(); }); latch.Wait(); diff --git a/engine/src/flutter/shell/platform/android/surface/android_surface.h b/engine/src/flutter/shell/platform/android/surface/android_surface.h index 23636d39f87..16013504cb3 100644 --- a/engine/src/flutter/shell/platform/android/surface/android_surface.h +++ b/engine/src/flutter/shell/platform/android/surface/android_surface.h @@ -39,7 +39,9 @@ class AndroidSurface { virtual bool ResourceContextClearCurrent() = 0; - virtual bool SetNativeWindow(fml::RefPtr window) = 0; + virtual bool SetNativeWindow( + fml::RefPtr window, + const std::shared_ptr& jni_facade) = 0; virtual std::unique_ptr CreateSnapshotSurface(); diff --git a/engine/src/flutter/shell/platform/android/surface/android_surface_mock.h b/engine/src/flutter/shell/platform/android/surface/android_surface_mock.h index 6bb1628e7dd..b43f6895108 100644 --- a/engine/src/flutter/shell/platform/android/surface/android_surface_mock.h +++ b/engine/src/flutter/shell/platform/android/surface/android_surface_mock.h @@ -35,7 +35,8 @@ class AndroidSurfaceMock final : public GPUSurfaceGLDelegate, MOCK_METHOD(bool, SetNativeWindow, - (fml::RefPtr window), + (fml::RefPtr window, + const std::shared_ptr& jni_facade), (override)); // |GPUSurfaceGLDelegate|