Skip to content

Commit

Permalink
Implement external view embedder on Android (flutter#19033)
Browse files Browse the repository at this point in the history
The external view embedder allows to embed Android views in a Flutter app.
  • Loading branch information
Emmanuel Garcia authored Jun 20, 2020
1 parent 0b3b82d commit b990ad1
Show file tree
Hide file tree
Showing 51 changed files with 1,443 additions and 235 deletions.
3 changes: 2 additions & 1 deletion BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ group("flutter") {
"//flutter/lib/ui:ui_unittests",
"//flutter/runtime:runtime_unittests",
"//flutter/shell/common:shell_unittests",
"//flutter/shell/platform/android/external_view_embedder:android_external_view_embedder_unittests",
"//flutter/shell/platform/embedder:embedder_unittests",
"//flutter/testing:testing_unittests",
"//flutter/third_party/txt:txt_unittests",
Expand All @@ -85,6 +84,8 @@ group("flutter") {
"//flutter/fml:fml_benchmarks",
"//flutter/lib/ui:ui_benchmarks",
"//flutter/shell/common:shell_benchmarks",
"//flutter/shell/platform/android/external_view_embedder:android_external_view_embedder_unittests",
"//flutter/shell/platform/android/jni:jni_unittests",
"//flutter/third_party/txt:txt_benchmarks",
]
}
Expand Down
19 changes: 13 additions & 6 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -645,21 +645,15 @@ FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan.h
FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_delegate.cc
FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_delegate.h
FILE: ../../../flutter/shell/platform/android/AndroidManifest.xml
FILE: ../../../flutter/shell/platform/android/android_context.cc
FILE: ../../../flutter/shell/platform/android/android_context.h
FILE: ../../../flutter/shell/platform/android/android_context_gl.cc
FILE: ../../../flutter/shell/platform/android/android_context_gl.h
FILE: ../../../flutter/shell/platform/android/android_environment_gl.cc
FILE: ../../../flutter/shell/platform/android/android_environment_gl.h
FILE: ../../../flutter/shell/platform/android/android_exports.lst
FILE: ../../../flutter/shell/platform/android/android_external_texture_gl.cc
FILE: ../../../flutter/shell/platform/android/android_external_texture_gl.h
FILE: ../../../flutter/shell/platform/android/android_native_window.cc
FILE: ../../../flutter/shell/platform/android/android_native_window.h
FILE: ../../../flutter/shell/platform/android/android_shell_holder.cc
FILE: ../../../flutter/shell/platform/android/android_shell_holder.h
FILE: ../../../flutter/shell/platform/android/android_surface.cc
FILE: ../../../flutter/shell/platform/android/android_surface.h
FILE: ../../../flutter/shell/platform/android/android_surface_gl.cc
FILE: ../../../flutter/shell/platform/android/android_surface_gl.h
FILE: ../../../flutter/shell/platform/android/android_surface_software.cc
Expand All @@ -668,9 +662,14 @@ FILE: ../../../flutter/shell/platform/android/android_surface_vulkan.cc
FILE: ../../../flutter/shell/platform/android/android_surface_vulkan.h
FILE: ../../../flutter/shell/platform/android/apk_asset_provider.cc
FILE: ../../../flutter/shell/platform/android/apk_asset_provider.h
FILE: ../../../flutter/shell/platform/android/context/android_context.cc
FILE: ../../../flutter/shell/platform/android/context/android_context.h
FILE: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder.cc
FILE: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder.h
FILE: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc
FILE: ../../../flutter/shell/platform/android/external_view_embedder/surface_pool.cc
FILE: ../../../flutter/shell/platform/android/external_view_embedder/surface_pool.h
FILE: ../../../flutter/shell/platform/android/external_view_embedder/surface_pool_unittests.cc
FILE: ../../../flutter/shell/platform/android/flutter_main.cc
FILE: ../../../flutter/shell/platform/android/flutter_main.h
FILE: ../../../flutter/shell/platform/android/io/flutter/Log.java
Expand Down Expand Up @@ -786,6 +785,8 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/view/FlutterRunArgument
FILE: ../../../flutter/shell/platform/android/io/flutter/view/FlutterView.java
FILE: ../../../flutter/shell/platform/android/io/flutter/view/TextureRegistry.java
FILE: ../../../flutter/shell/platform/android/io/flutter/view/VsyncWaiter.java
FILE: ../../../flutter/shell/platform/android/jni/jni_mock.h
FILE: ../../../flutter/shell/platform/android/jni/jni_mock_unittest.cc
FILE: ../../../flutter/shell/platform/android/jni/platform_view_android_jni.cc
FILE: ../../../flutter/shell/platform/android/jni/platform_view_android_jni.h
FILE: ../../../flutter/shell/platform/android/library_loader.cc
Expand All @@ -796,6 +797,12 @@ FILE: ../../../flutter/shell/platform/android/platform_view_android.h
FILE: ../../../flutter/shell/platform/android/platform_view_android_jni_impl.cc
FILE: ../../../flutter/shell/platform/android/platform_view_android_jni_impl.h
FILE: ../../../flutter/shell/platform/android/robolectric.properties
FILE: ../../../flutter/shell/platform/android/surface/android_native_window.cc
FILE: ../../../flutter/shell/platform/android/surface/android_native_window.h
FILE: ../../../flutter/shell/platform/android/surface/android_surface.cc
FILE: ../../../flutter/shell/platform/android/surface/android_surface.h
FILE: ../../../flutter/shell/platform/android/surface/android_surface_mock.cc
FILE: ../../../flutter/shell/platform/android/surface/android_surface_mock.h
FILE: ../../../flutter/shell/platform/android/vsync_waiter_android.cc
FILE: ../../../flutter/shell/platform/android/vsync_waiter_android.h
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/basic_message_channel_unittests.cc
Expand Down
8 changes: 5 additions & 3 deletions flow/embedded_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,11 @@ class ExternalViewEmbedder {
// sets the stage for the next pre-roll.
virtual void CancelFrame() = 0;

virtual void BeginFrame(SkISize frame_size,
GrContext* context,
double device_pixel_ratio) = 0;
virtual void BeginFrame(
SkISize frame_size,
GrContext* context,
double device_pixel_ratio,
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) = 0;

virtual void PrerollCompositeEmbeddedView(
int view_id,
Expand Down
11 changes: 7 additions & 4 deletions fml/raster_thread_merger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ void RasterThreadMerger::MergeWithLease(size_t lease_term) {
}
}

bool RasterThreadMerger::IsOnRasterizingThread() {
const auto current_queue_id = MessageLoop::GetCurrentTaskQueueId();
bool RasterThreadMerger::IsOnPlatformThread() const {
return MessageLoop::GetCurrentTaskQueueId() == platform_queue_id_;
}

bool RasterThreadMerger::IsOnRasterizingThread() const {
if (is_merged_) {
return current_queue_id == platform_queue_id_;
return IsOnPlatformThread();
} else {
return current_queue_id == gpu_queue_id_;
return !IsOnPlatformThread();
}
}

Expand Down
5 changes: 4 additions & 1 deletion fml/raster_thread_merger.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ class RasterThreadMerger
// Returns true if the current thread owns rasterizing.
// When the threads are merged, platform thread owns rasterizing.
// When un-merged, raster thread owns rasterizing.
bool IsOnRasterizingThread();
bool IsOnRasterizingThread() const;

// Returns true if the current thread is the platform thread.
bool IsOnPlatformThread() const;

private:
static const int kLeaseNotSet;
Expand Down
6 changes: 6 additions & 0 deletions fml/raster_thread_merger_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ TEST(RasterThreadMerger, IsNotOnRasterizingThread) {

loop1->GetTaskRunner()->PostTask([&]() {
ASSERT_FALSE(raster_thread_merger_->IsOnRasterizingThread());
ASSERT_TRUE(raster_thread_merger_->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid1);
pre_merge.CountDown();
});

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

loop1->GetTaskRunner()->PostTask([&]() {
ASSERT_TRUE(raster_thread_merger_->IsOnRasterizingThread());
ASSERT_TRUE(raster_thread_merger_->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid1);
post_merge.CountDown();
});
Expand All @@ -116,6 +119,7 @@ TEST(RasterThreadMerger, IsNotOnRasterizingThread) {
// this will be false since this is going to be run
// on loop1 really.
ASSERT_TRUE(raster_thread_merger_->IsOnRasterizingThread());
ASSERT_TRUE(raster_thread_merger_->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid1);
post_merge.CountDown();
});
Expand All @@ -126,12 +130,14 @@ TEST(RasterThreadMerger, IsNotOnRasterizingThread) {

loop1->GetTaskRunner()->PostTask([&]() {
ASSERT_FALSE(raster_thread_merger_->IsOnRasterizingThread());
ASSERT_TRUE(raster_thread_merger_->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid1);
post_unmerge.CountDown();
});

loop2->GetTaskRunner()->PostTask([&]() {
ASSERT_TRUE(raster_thread_merger_->IsOnRasterizingThread());
ASSERT_FALSE(raster_thread_merger_->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid2);
post_unmerge.CountDown();
});
Expand Down
6 changes: 3 additions & 3 deletions shell/common/rasterizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ RasterStatus Rasterizer::DrawToSurface(flutter::LayerTree& layer_tree) {

SkCanvas* embedder_root_canvas = nullptr;
if (external_view_embedder != nullptr) {
external_view_embedder->BeginFrame(layer_tree.frame_size(),
surface_->GetContext(),
layer_tree.device_pixel_ratio());
external_view_embedder->BeginFrame(
layer_tree.frame_size(), surface_->GetContext(),
layer_tree.device_pixel_ratio(), raster_thread_merger_);
embedder_root_canvas = external_view_embedder->GetRootCanvas();
}

Expand Down
8 changes: 5 additions & 3 deletions shell/common/shell_test_external_view_embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ namespace flutter {
void ShellTestExternalViewEmbedder::CancelFrame() {}

// |ExternalViewEmbedder|
void ShellTestExternalViewEmbedder::BeginFrame(SkISize frame_size,
GrContext* context,
double device_pixel_ratio) {}
void ShellTestExternalViewEmbedder::BeginFrame(
SkISize frame_size,
GrContext* context,
double device_pixel_ratio,
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) {}

// |ExternalViewEmbedder|
void ShellTestExternalViewEmbedder::PrerollCompositeEmbeddedView(
Expand Down
8 changes: 5 additions & 3 deletions shell/common/shell_test_external_view_embedder.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ class ShellTestExternalViewEmbedder final : public ExternalViewEmbedder {
void CancelFrame() override;

// |ExternalViewEmbedder|
void BeginFrame(SkISize frame_size,
GrContext* context,
double device_pixel_ratio) override;
void BeginFrame(
SkISize frame_size,
GrContext* context,
double device_pixel_ratio,
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) override;

// |ExternalViewEmbedder|
void PrerollCompositeEmbeddedView(
Expand Down
9 changes: 3 additions & 6 deletions shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,14 @@ shared_library("flutter_shell_native") {

sources = [
"$root_build_dir/flutter_icu/icudtl.o",
"android_context.cc",
"android_context.h",
"android_context_gl.cc",
"android_context_gl.h",
"android_environment_gl.cc",
"android_environment_gl.h",
"android_external_texture_gl.cc",
"android_external_texture_gl.h",
"android_native_window.cc",
"android_native_window.h",
"android_shell_holder.cc",
"android_shell_holder.h",
"android_surface.cc",
"android_surface.h",
"android_surface_gl.cc",
"android_surface_gl.h",
"android_surface_software.cc",
Expand Down Expand Up @@ -67,8 +61,11 @@ shared_library("flutter_shell_native") {
"//flutter/runtime",
"//flutter/runtime:libdart",
"//flutter/shell/common",
"//flutter/shell/platform/android/context",
"//flutter/shell/platform/android/external_view_embedder",
"//flutter/shell/platform/android/jni",
"//flutter/shell/platform/android/surface",
"//flutter/shell/platform/android/surface:native_window",
"//third_party/skia",
]

Expand Down
37 changes: 0 additions & 37 deletions shell/platform/android/android_context.cc

This file was deleted.

4 changes: 2 additions & 2 deletions shell/platform/android/android_context_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include "flutter/fml/memory/ref_counted.h"
#include "flutter/fml/memory/ref_ptr.h"
#include "flutter/shell/common/platform_view.h"
#include "flutter/shell/platform/android/android_context.h"
#include "flutter/shell/platform/android/android_environment_gl.h"
#include "flutter/shell/platform/android/android_native_window.h"
#include "flutter/shell/platform/android/context/android_context.h"
#include "flutter/shell/platform/android/surface/android_native_window.h"
#include "third_party/skia/include/core/SkSize.h"

namespace flutter {
Expand Down
1 change: 1 addition & 0 deletions shell/platform/android/android_shell_holder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "flutter/fml/make_copyable.h"
#include "flutter/fml/message_loop.h"
#include "flutter/fml/platform/android/jni_util.h"
#include "flutter/shell/common/rasterizer.h"
#include "flutter/shell/platform/android/platform_view_android.h"

Expand Down
41 changes: 0 additions & 41 deletions shell/platform/android/android_surface.cc

This file was deleted.

15 changes: 9 additions & 6 deletions shell/platform/android/android_surface_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ namespace flutter {

AndroidSurfaceGL::AndroidSurfaceGL(
std::shared_ptr<AndroidContext> android_context,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade)
: native_window_(nullptr),
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
const AndroidSurface::Factory& surface_factory)
: external_view_embedder_(
std::make_unique<AndroidExternalViewEmbedder>(android_context,
jni_facade,
surface_factory)),
android_context_(
std::static_pointer_cast<AndroidContextGL>(android_context)),
native_window_(nullptr),
onscreen_surface_(nullptr),
offscreen_surface_(nullptr) {
android_context_ =
std::static_pointer_cast<AndroidContextGL>(android_context);
// Acquire the offscreen surface.
offscreen_surface_ = android_context_->CreateOffscreenSurface();
if (!offscreen_surface_->IsValid()) {
offscreen_surface_ = nullptr;
}
external_view_embedder_ =
std::make_unique<AndroidExternalViewEmbedder>(jni_facade);
}

AndroidSurfaceGL::~AndroidSurfaceGL() = default;
Expand Down
Loading

0 comments on commit b990ad1

Please sign in to comment.