Skip to content

Commit 591f55b

Browse files
authored
Allow embedder controlled composition of Flutter layers. (flutter-team-archive/engine#10195)
This patch allows embedders to split the Flutter layer tree into multiple chunks. These chunks are meant to be composed one on top of another. This gives embedders a chance to interleave their own contents between these chunks. The Flutter embedder API already provides hooks for the specification of textures for the Flutter engine to compose within its own hierarchy (for camera feeds, video, etc..). However, not all embedders can render the contents of such sources into textures the Flutter engine can accept. Moreover, this composition model may have overheads that are non-trivial for certain use cases. In such cases, the embedder may choose to specify multiple render target for Flutter to render into instead of just one. The use of this API allows embedders to perform composition very similar to the iOS embedder. This composition model is used on that platform for the embedding of UIKit view such and web view and map views within the Flutter hierarchy. However, do note that iOS also has threading configurations that are currently not available to custom embedders. The embedder API updates in this patch are ABI stable and existing embedders will continue to work are normal. For embedders that want to enable this composition mode, the API is designed to make it easy to opt into the same in an incremental manner. Rendering of contents into the “root” rendering surface remains unchanged. However, now the application can push “platform views” via a scene builder. These platform views need to handled by a FlutterCompositor specified in a new field at the end of the FlutterProjectArgs struct. When a new platform view in introduced within the layer tree, the compositor will ask the embedder to create a new render target for that platform view. Render targets can currently be OpenGL framebuffers, OpenGL textures or software buffers. The type of the render target returned by the embedder must be compatible with the root render surface. That is, if the root render surface is an OpenGL framebuffer, the render target for each platform view must either be a texture or a framebuffer in the same OpenGL context. New render target types as well as root renderers for newer APIs like Metal & Vulkan can and will be added in the future. The addition of these APIs will be done in an ABI & API stable manner. As Flutter renders frames, it gives the embedder a callback with information about the position of the various platform views in the effective hierarchy. The embedder is then meant to put the contents of the render targets that it setup and had previously given to the engine onto the screen (of course interleaving the contents of the platform views). Unit-tests have been added that test not only the structure and properties of layer hierarchy given to the compositor, but also the contents of the texels rendered by a test compositor using both the OpenGL and software rendering backends. Fixes b/132812775 Fixes #35410
1 parent fb9bb69 commit 591f55b

58 files changed

Lines changed: 2942 additions & 209 deletions

Some content is hidden

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

engine/src/flutter/ci/licenses_golden/licenses_flutter

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,8 @@ FILE: ../../../flutter/shell/gpu/gpu_surface_metal.h
521521
FILE: ../../../flutter/shell/gpu/gpu_surface_metal.mm
522522
FILE: ../../../flutter/shell/gpu/gpu_surface_software.cc
523523
FILE: ../../../flutter/shell/gpu/gpu_surface_software.h
524+
FILE: ../../../flutter/shell/gpu/gpu_surface_software_delegate.cc
525+
FILE: ../../../flutter/shell/gpu/gpu_surface_software_delegate.h
524526
FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan.cc
525527
FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan.h
526528
FILE: ../../../flutter/shell/platform/android/AndroidManifest.xml
@@ -810,9 +812,13 @@ FILE: ../../../flutter/shell/platform/embedder/embedder_engine.cc
810812
FILE: ../../../flutter/shell/platform/embedder/embedder_engine.h
811813
FILE: ../../../flutter/shell/platform/embedder/embedder_external_texture_gl.cc
812814
FILE: ../../../flutter/shell/platform/embedder/embedder_external_texture_gl.h
815+
FILE: ../../../flutter/shell/platform/embedder/embedder_external_view_embedder.cc
816+
FILE: ../../../flutter/shell/platform/embedder/embedder_external_view_embedder.h
813817
FILE: ../../../flutter/shell/platform/embedder/embedder_include.c
814818
FILE: ../../../flutter/shell/platform/embedder/embedder_platform_message_response.cc
815819
FILE: ../../../flutter/shell/platform/embedder/embedder_platform_message_response.h
820+
FILE: ../../../flutter/shell/platform/embedder/embedder_render_target.cc
821+
FILE: ../../../flutter/shell/platform/embedder/embedder_render_target.h
816822
FILE: ../../../flutter/shell/platform/embedder/embedder_safe_access.h
817823
FILE: ../../../flutter/shell/platform/embedder/embedder_surface.cc
818824
FILE: ../../../flutter/shell/platform/embedder/embedder_surface.h
@@ -824,6 +830,7 @@ FILE: ../../../flutter/shell/platform/embedder/embedder_task_runner.cc
824830
FILE: ../../../flutter/shell/platform/embedder/embedder_task_runner.h
825831
FILE: ../../../flutter/shell/platform/embedder/embedder_thread_host.cc
826832
FILE: ../../../flutter/shell/platform/embedder/embedder_thread_host.h
833+
FILE: ../../../flutter/shell/platform/embedder/fixtures/compositor.png
827834
FILE: ../../../flutter/shell/platform/embedder/fixtures/main.dart
828835
FILE: ../../../flutter/shell/platform/embedder/platform_view_embedder.cc
829836
FILE: ../../../flutter/shell/platform/embedder/platform_view_embedder.h

engine/src/flutter/flow/embedded_views.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "third_party/skia/include/core/SkRRect.h"
1616
#include "third_party/skia/include/core/SkRect.h"
1717
#include "third_party/skia/include/core/SkSize.h"
18+
#include "third_party/skia/include/core/SkSurface.h"
1819

1920
namespace flutter {
2021

@@ -194,11 +195,19 @@ class ExternalViewEmbedder {
194195
public:
195196
ExternalViewEmbedder() = default;
196197

198+
virtual ~ExternalViewEmbedder() = default;
199+
200+
// Usually, the root surface is not owned by the view embedder. However, if
201+
// the view embedder wants to provide a surface to the rasterizer, it may
202+
// return one here. This surface takes priority over the surface materialized
203+
// from the on-screen render target.
204+
virtual sk_sp<SkSurface> GetRootSurface() = 0;
205+
197206
// Call this in-lieu of |SubmitFrame| to clear pre-roll state and
198207
// sets the stage for the next pre-roll.
199208
virtual void CancelFrame() = 0;
200209

201-
virtual void BeginFrame(SkISize frame_size) = 0;
210+
virtual void BeginFrame(SkISize frame_size, GrContext* context) = 0;
202211

203212
virtual void PrerollCompositeEmbeddedView(
204213
int view_id,
@@ -220,8 +229,6 @@ class ExternalViewEmbedder {
220229

221230
virtual bool SubmitFrame(GrContext* context);
222231

223-
virtual ~ExternalViewEmbedder() = default;
224-
225232
FML_DISALLOW_COPY_AND_ASSIGN(ExternalViewEmbedder);
226233

227234
}; // ExternalViewEmbedder

engine/src/flutter/fml/closure.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,50 @@
77

88
#include <functional>
99

10+
#include "flutter/fml/macros.h"
11+
1012
namespace fml {
1113

1214
using closure = std::function<void()>;
1315

16+
//------------------------------------------------------------------------------
17+
/// @brief Wraps a closure that is invoked in the destructor unless
18+
/// released by the caller.
19+
///
20+
/// This is especially useful in dealing with APIs that return a
21+
/// resource by accepting ownership of a sub-resource and a closure
22+
/// that releases that resource. When such APIs are chained, each
23+
/// link in the chain must check that the next member in the chain
24+
/// has accepted the resource. If not, it must invoke the closure
25+
/// eagerly. Not doing this results in a resource leak in the
26+
/// erroneous case. Using this wrapper, the closure can be released
27+
/// once the next call in the chain has successfully accepted
28+
/// ownership of the resource. If not, the closure gets invoked
29+
/// automatically at the end of the scope. This covers the cases
30+
/// where there are early returns as well.
31+
///
32+
class ScopedCleanupClosure {
33+
public:
34+
ScopedCleanupClosure(fml::closure closure) : closure_(closure) {}
35+
36+
~ScopedCleanupClosure() {
37+
if (closure_) {
38+
closure_();
39+
}
40+
}
41+
42+
fml::closure Release() {
43+
fml::closure closure = closure_;
44+
closure_ = nullptr;
45+
return closure;
46+
}
47+
48+
private:
49+
fml::closure closure_;
50+
51+
FML_DISALLOW_COPY_AND_ASSIGN(ScopedCleanupClosure);
52+
};
53+
1454
} // namespace fml
1555

1656
#endif // FLUTTER_FML_CLOSURE_H_

engine/src/flutter/fml/mapping.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ const uint8_t* DataMapping::GetMapping() const {
7979

8080
// NonOwnedMapping
8181

82+
NonOwnedMapping::NonOwnedMapping(const uint8_t* data,
83+
size_t size,
84+
ReleaseProc release_proc)
85+
: data_(data), size_(size), release_proc_(release_proc) {}
86+
87+
NonOwnedMapping::~NonOwnedMapping() {
88+
if (release_proc_) {
89+
release_proc_(data_, size_);
90+
}
91+
}
92+
8293
size_t NonOwnedMapping::GetSize() const {
8394
return size_;
8495
}

engine/src/flutter/fml/mapping.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,12 @@ class DataMapping final : public Mapping {
102102

103103
class NonOwnedMapping final : public Mapping {
104104
public:
105-
NonOwnedMapping(const uint8_t* data, size_t size)
106-
: data_(data), size_(size) {}
105+
using ReleaseProc = std::function<void(const uint8_t* data, size_t size)>;
106+
NonOwnedMapping(const uint8_t* data,
107+
size_t size,
108+
ReleaseProc release_proc = nullptr);
109+
110+
~NonOwnedMapping() override;
107111

108112
// |Mapping|
109113
size_t GetSize() const override;
@@ -114,6 +118,7 @@ class NonOwnedMapping final : public Mapping {
114118
private:
115119
const uint8_t* const data_;
116120
const size_t size_;
121+
const ReleaseProc release_proc_;
117122

118123
FML_DISALLOW_COPY_AND_ASSIGN(NonOwnedMapping);
119124
};

engine/src/flutter/lib/ui/painting/image_decoder_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TestIOManager final : public IOManager {
1919
public:
2020
TestIOManager(fml::RefPtr<fml::TaskRunner> task_runner,
2121
bool has_gpu_context = true)
22-
: gl_context_(has_gpu_context ? gl_surface_.CreateContext() : nullptr),
22+
: gl_context_(has_gpu_context ? gl_surface_.CreateGrContext() : nullptr),
2323
weak_gl_context_factory_(
2424
has_gpu_context ? std::make_unique<fml::WeakPtrFactory<GrContext>>(
2525
gl_context_.get())

engine/src/flutter/shell/common/rasterizer.cc

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,34 @@ RasterStatus Rasterizer::DrawToSurface(flutter::LayerTree& layer_tree) {
223223
// for instrumentation.
224224
compositor_context_->ui_time().SetLapTime(layer_tree.build_time());
225225

226-
auto* canvas = frame->SkiaCanvas();
227-
228226
auto* external_view_embedder = surface_->GetExternalViewEmbedder();
229227

228+
sk_sp<SkSurface> embedder_root_surface;
229+
230230
if (external_view_embedder != nullptr) {
231-
external_view_embedder->BeginFrame(layer_tree.frame_size());
231+
external_view_embedder->BeginFrame(layer_tree.frame_size(),
232+
surface_->GetContext());
233+
embedder_root_surface = external_view_embedder->GetRootSurface();
232234
}
233235

236+
// If the external view embedder has specified an optional root surface, the
237+
// root surface transformation is set by the embedder instead of
238+
// having to apply it here.
239+
SkMatrix root_surface_transformation =
240+
embedder_root_surface ? SkMatrix{} : surface_->GetRootTransformation();
241+
242+
auto root_surface_canvas = embedder_root_surface
243+
? embedder_root_surface->getCanvas()
244+
: frame->SkiaCanvas();
245+
234246
auto compositor_frame = compositor_context_->AcquireFrame(
235-
surface_->GetContext(), canvas, external_view_embedder,
236-
surface_->GetRootTransformation(), true, gpu_thread_merger_);
247+
surface_->GetContext(), // skia GrContext
248+
root_surface_canvas, // root surface canvas
249+
external_view_embedder, // external view embedder
250+
root_surface_transformation, // root surface transformation
251+
true, // instrumentation enabled
252+
gpu_thread_merger_ // thread merger
253+
);
237254

238255
if (compositor_frame) {
239256
RasterStatus raster_status = compositor_frame->Raster(layer_tree, false);
@@ -244,10 +261,12 @@ RasterStatus Rasterizer::DrawToSurface(flutter::LayerTree& layer_tree) {
244261
if (external_view_embedder != nullptr) {
245262
external_view_embedder->SubmitFrame(surface_->GetContext());
246263
}
264+
247265
FireNextFrameCallbackIfPresent();
248266

249-
if (surface_->GetContext())
267+
if (surface_->GetContext()) {
250268
surface_->GetContext()->performDeferredCleanup(kSkiaCleanupExpiration);
269+
}
251270

252271
return raster_status;
253272
}

engine/src/flutter/shell/common/shell_test.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ ShellTestPlatformView::~ShellTestPlatformView() = default;
217217

218218
// |PlatformView|
219219
std::unique_ptr<Surface> ShellTestPlatformView::CreateRenderingSurface() {
220-
return std::make_unique<GPUSurfaceGL>(this);
220+
return std::make_unique<GPUSurfaceGL>(this, true);
221221
}
222222

223223
// |GPUSurfaceGLDelegate|
@@ -248,5 +248,10 @@ GPUSurfaceGLDelegate::GLProcResolver ShellTestPlatformView::GetGLProcResolver()
248248
};
249249
}
250250

251+
// |GPUSurfaceGLDelegate|
252+
ExternalViewEmbedder* ShellTestPlatformView::GetExternalViewEmbedder() {
253+
return nullptr;
254+
}
255+
251256
} // namespace testing
252257
} // namespace flutter

engine/src/flutter/shell/common/shell_test.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ class ShellTestPlatformView : public PlatformView, public GPUSurfaceGLDelegate {
9999
// |GPUSurfaceGLDelegate|
100100
GLProcResolver GetGLProcResolver() const override;
101101

102+
// |GPUSurfaceGLDelegate|
103+
ExternalViewEmbedder* GetExternalViewEmbedder() override;
104+
102105
FML_DISALLOW_COPY_AND_ASSIGN(ShellTestPlatformView);
103106
};
104107

engine/src/flutter/shell/gpu/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ source_set("gpu_surface_software") {
1818
sources = [
1919
"$gpu_dir/gpu_surface_software.cc",
2020
"$gpu_dir/gpu_surface_software.h",
21+
"$gpu_dir/gpu_surface_software_delegate.cc",
22+
"$gpu_dir/gpu_surface_software_delegate.h",
2123
]
2224

2325
deps = gpu_common_deps

0 commit comments

Comments
 (0)