Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit b990ad1

Browse files
author
Emmanuel Garcia
authored
Implement external view embedder on Android (#19033)
The external view embedder allows to embed Android views in a Flutter app.
1 parent 0b3b82d commit b990ad1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1443
-235
lines changed

BUILD.gn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ group("flutter") {
7474
"//flutter/lib/ui:ui_unittests",
7575
"//flutter/runtime:runtime_unittests",
7676
"//flutter/shell/common:shell_unittests",
77-
"//flutter/shell/platform/android/external_view_embedder:android_external_view_embedder_unittests",
7877
"//flutter/shell/platform/embedder:embedder_unittests",
7978
"//flutter/testing:testing_unittests",
8079
"//flutter/third_party/txt:txt_unittests",
@@ -85,6 +84,8 @@ group("flutter") {
8584
"//flutter/fml:fml_benchmarks",
8685
"//flutter/lib/ui:ui_benchmarks",
8786
"//flutter/shell/common:shell_benchmarks",
87+
"//flutter/shell/platform/android/external_view_embedder:android_external_view_embedder_unittests",
88+
"//flutter/shell/platform/android/jni:jni_unittests",
8889
"//flutter/third_party/txt:txt_benchmarks",
8990
]
9091
}

ci/licenses_golden/licenses_flutter

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -645,21 +645,15 @@ FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan.h
645645
FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_delegate.cc
646646
FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_delegate.h
647647
FILE: ../../../flutter/shell/platform/android/AndroidManifest.xml
648-
FILE: ../../../flutter/shell/platform/android/android_context.cc
649-
FILE: ../../../flutter/shell/platform/android/android_context.h
650648
FILE: ../../../flutter/shell/platform/android/android_context_gl.cc
651649
FILE: ../../../flutter/shell/platform/android/android_context_gl.h
652650
FILE: ../../../flutter/shell/platform/android/android_environment_gl.cc
653651
FILE: ../../../flutter/shell/platform/android/android_environment_gl.h
654652
FILE: ../../../flutter/shell/platform/android/android_exports.lst
655653
FILE: ../../../flutter/shell/platform/android/android_external_texture_gl.cc
656654
FILE: ../../../flutter/shell/platform/android/android_external_texture_gl.h
657-
FILE: ../../../flutter/shell/platform/android/android_native_window.cc
658-
FILE: ../../../flutter/shell/platform/android/android_native_window.h
659655
FILE: ../../../flutter/shell/platform/android/android_shell_holder.cc
660656
FILE: ../../../flutter/shell/platform/android/android_shell_holder.h
661-
FILE: ../../../flutter/shell/platform/android/android_surface.cc
662-
FILE: ../../../flutter/shell/platform/android/android_surface.h
663657
FILE: ../../../flutter/shell/platform/android/android_surface_gl.cc
664658
FILE: ../../../flutter/shell/platform/android/android_surface_gl.h
665659
FILE: ../../../flutter/shell/platform/android/android_surface_software.cc
@@ -668,9 +662,14 @@ FILE: ../../../flutter/shell/platform/android/android_surface_vulkan.cc
668662
FILE: ../../../flutter/shell/platform/android/android_surface_vulkan.h
669663
FILE: ../../../flutter/shell/platform/android/apk_asset_provider.cc
670664
FILE: ../../../flutter/shell/platform/android/apk_asset_provider.h
665+
FILE: ../../../flutter/shell/platform/android/context/android_context.cc
666+
FILE: ../../../flutter/shell/platform/android/context/android_context.h
671667
FILE: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder.cc
672668
FILE: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder.h
673669
FILE: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc
670+
FILE: ../../../flutter/shell/platform/android/external_view_embedder/surface_pool.cc
671+
FILE: ../../../flutter/shell/platform/android/external_view_embedder/surface_pool.h
672+
FILE: ../../../flutter/shell/platform/android/external_view_embedder/surface_pool_unittests.cc
674673
FILE: ../../../flutter/shell/platform/android/flutter_main.cc
675674
FILE: ../../../flutter/shell/platform/android/flutter_main.h
676675
FILE: ../../../flutter/shell/platform/android/io/flutter/Log.java
@@ -786,6 +785,8 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/view/FlutterRunArgument
786785
FILE: ../../../flutter/shell/platform/android/io/flutter/view/FlutterView.java
787786
FILE: ../../../flutter/shell/platform/android/io/flutter/view/TextureRegistry.java
788787
FILE: ../../../flutter/shell/platform/android/io/flutter/view/VsyncWaiter.java
788+
FILE: ../../../flutter/shell/platform/android/jni/jni_mock.h
789+
FILE: ../../../flutter/shell/platform/android/jni/jni_mock_unittest.cc
789790
FILE: ../../../flutter/shell/platform/android/jni/platform_view_android_jni.cc
790791
FILE: ../../../flutter/shell/platform/android/jni/platform_view_android_jni.h
791792
FILE: ../../../flutter/shell/platform/android/library_loader.cc
@@ -796,6 +797,12 @@ FILE: ../../../flutter/shell/platform/android/platform_view_android.h
796797
FILE: ../../../flutter/shell/platform/android/platform_view_android_jni_impl.cc
797798
FILE: ../../../flutter/shell/platform/android/platform_view_android_jni_impl.h
798799
FILE: ../../../flutter/shell/platform/android/robolectric.properties
800+
FILE: ../../../flutter/shell/platform/android/surface/android_native_window.cc
801+
FILE: ../../../flutter/shell/platform/android/surface/android_native_window.h
802+
FILE: ../../../flutter/shell/platform/android/surface/android_surface.cc
803+
FILE: ../../../flutter/shell/platform/android/surface/android_surface.h
804+
FILE: ../../../flutter/shell/platform/android/surface/android_surface_mock.cc
805+
FILE: ../../../flutter/shell/platform/android/surface/android_surface_mock.h
799806
FILE: ../../../flutter/shell/platform/android/vsync_waiter_android.cc
800807
FILE: ../../../flutter/shell/platform/android/vsync_waiter_android.h
801808
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/basic_message_channel_unittests.cc

flow/embedded_views.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,11 @@ class ExternalViewEmbedder {
227227
// sets the stage for the next pre-roll.
228228
virtual void CancelFrame() = 0;
229229

230-
virtual void BeginFrame(SkISize frame_size,
231-
GrContext* context,
232-
double device_pixel_ratio) = 0;
230+
virtual void BeginFrame(
231+
SkISize frame_size,
232+
GrContext* context,
233+
double device_pixel_ratio,
234+
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) = 0;
233235

234236
virtual void PrerollCompositeEmbeddedView(
235237
int view_id,

fml/raster_thread_merger.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ void RasterThreadMerger::MergeWithLease(size_t lease_term) {
2828
}
2929
}
3030

31-
bool RasterThreadMerger::IsOnRasterizingThread() {
32-
const auto current_queue_id = MessageLoop::GetCurrentTaskQueueId();
31+
bool RasterThreadMerger::IsOnPlatformThread() const {
32+
return MessageLoop::GetCurrentTaskQueueId() == platform_queue_id_;
33+
}
34+
35+
bool RasterThreadMerger::IsOnRasterizingThread() const {
3336
if (is_merged_) {
34-
return current_queue_id == platform_queue_id_;
37+
return IsOnPlatformThread();
3538
} else {
36-
return current_queue_id == gpu_queue_id_;
39+
return !IsOnPlatformThread();
3740
}
3841
}
3942

fml/raster_thread_merger.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ class RasterThreadMerger
4444
// Returns true if the current thread owns rasterizing.
4545
// When the threads are merged, platform thread owns rasterizing.
4646
// When un-merged, raster thread owns rasterizing.
47-
bool IsOnRasterizingThread();
47+
bool IsOnRasterizingThread() const;
48+
49+
// Returns true if the current thread is the platform thread.
50+
bool IsOnPlatformThread() const;
4851

4952
private:
5053
static const int kLeaseNotSet;

fml/raster_thread_merger_unittests.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ TEST(RasterThreadMerger, IsNotOnRasterizingThread) {
9292

9393
loop1->GetTaskRunner()->PostTask([&]() {
9494
ASSERT_FALSE(raster_thread_merger_->IsOnRasterizingThread());
95+
ASSERT_TRUE(raster_thread_merger_->IsOnPlatformThread());
9596
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid1);
9697
pre_merge.CountDown();
9798
});
9899

99100
loop2->GetTaskRunner()->PostTask([&]() {
100101
ASSERT_TRUE(raster_thread_merger_->IsOnRasterizingThread());
102+
ASSERT_FALSE(raster_thread_merger_->IsOnPlatformThread());
101103
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid2);
102104
pre_merge.CountDown();
103105
});
@@ -108,6 +110,7 @@ TEST(RasterThreadMerger, IsNotOnRasterizingThread) {
108110

109111
loop1->GetTaskRunner()->PostTask([&]() {
110112
ASSERT_TRUE(raster_thread_merger_->IsOnRasterizingThread());
113+
ASSERT_TRUE(raster_thread_merger_->IsOnPlatformThread());
111114
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid1);
112115
post_merge.CountDown();
113116
});
@@ -116,6 +119,7 @@ TEST(RasterThreadMerger, IsNotOnRasterizingThread) {
116119
// this will be false since this is going to be run
117120
// on loop1 really.
118121
ASSERT_TRUE(raster_thread_merger_->IsOnRasterizingThread());
122+
ASSERT_TRUE(raster_thread_merger_->IsOnPlatformThread());
119123
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid1);
120124
post_merge.CountDown();
121125
});
@@ -126,12 +130,14 @@ TEST(RasterThreadMerger, IsNotOnRasterizingThread) {
126130

127131
loop1->GetTaskRunner()->PostTask([&]() {
128132
ASSERT_FALSE(raster_thread_merger_->IsOnRasterizingThread());
133+
ASSERT_TRUE(raster_thread_merger_->IsOnPlatformThread());
129134
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid1);
130135
post_unmerge.CountDown();
131136
});
132137

133138
loop2->GetTaskRunner()->PostTask([&]() {
134139
ASSERT_TRUE(raster_thread_merger_->IsOnRasterizingThread());
140+
ASSERT_FALSE(raster_thread_merger_->IsOnPlatformThread());
135141
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid2);
136142
post_unmerge.CountDown();
137143
});

shell/common/rasterizer.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,9 @@ RasterStatus Rasterizer::DrawToSurface(flutter::LayerTree& layer_tree) {
391391

392392
SkCanvas* embedder_root_canvas = nullptr;
393393
if (external_view_embedder != nullptr) {
394-
external_view_embedder->BeginFrame(layer_tree.frame_size(),
395-
surface_->GetContext(),
396-
layer_tree.device_pixel_ratio());
394+
external_view_embedder->BeginFrame(
395+
layer_tree.frame_size(), surface_->GetContext(),
396+
layer_tree.device_pixel_ratio(), raster_thread_merger_);
397397
embedder_root_canvas = external_view_embedder->GetRootCanvas();
398398
}
399399

shell/common/shell_test_external_view_embedder.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ namespace flutter {
66
void ShellTestExternalViewEmbedder::CancelFrame() {}
77

88
// |ExternalViewEmbedder|
9-
void ShellTestExternalViewEmbedder::BeginFrame(SkISize frame_size,
10-
GrContext* context,
11-
double device_pixel_ratio) {}
9+
void ShellTestExternalViewEmbedder::BeginFrame(
10+
SkISize frame_size,
11+
GrContext* context,
12+
double device_pixel_ratio,
13+
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) {}
1214

1315
// |ExternalViewEmbedder|
1416
void ShellTestExternalViewEmbedder::PrerollCompositeEmbeddedView(

shell/common/shell_test_external_view_embedder.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ class ShellTestExternalViewEmbedder final : public ExternalViewEmbedder {
2929
void CancelFrame() override;
3030

3131
// |ExternalViewEmbedder|
32-
void BeginFrame(SkISize frame_size,
33-
GrContext* context,
34-
double device_pixel_ratio) override;
32+
void BeginFrame(
33+
SkISize frame_size,
34+
GrContext* context,
35+
double device_pixel_ratio,
36+
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) override;
3537

3638
// |ExternalViewEmbedder|
3739
void PrerollCompositeEmbeddedView(

shell/platform/android/BUILD.gn

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,14 @@ shared_library("flutter_shell_native") {
2323

2424
sources = [
2525
"$root_build_dir/flutter_icu/icudtl.o",
26-
"android_context.cc",
27-
"android_context.h",
2826
"android_context_gl.cc",
2927
"android_context_gl.h",
3028
"android_environment_gl.cc",
3129
"android_environment_gl.h",
3230
"android_external_texture_gl.cc",
3331
"android_external_texture_gl.h",
34-
"android_native_window.cc",
35-
"android_native_window.h",
3632
"android_shell_holder.cc",
3733
"android_shell_holder.h",
38-
"android_surface.cc",
39-
"android_surface.h",
4034
"android_surface_gl.cc",
4135
"android_surface_gl.h",
4236
"android_surface_software.cc",
@@ -67,8 +61,11 @@ shared_library("flutter_shell_native") {
6761
"//flutter/runtime",
6862
"//flutter/runtime:libdart",
6963
"//flutter/shell/common",
64+
"//flutter/shell/platform/android/context",
7065
"//flutter/shell/platform/android/external_view_embedder",
7166
"//flutter/shell/platform/android/jni",
67+
"//flutter/shell/platform/android/surface",
68+
"//flutter/shell/platform/android/surface:native_window",
7269
"//third_party/skia",
7370
]
7471

0 commit comments

Comments
 (0)