Skip to content

Commit e532e4b

Browse files
authored
Multiview ExternalViewEmbedder (flutter#46169)
This PR adds multiview support for `ExternalViewEmbedder`. ## Nomenclature The term **view** can be ambiguous in `ExternalViewEmbedder`, and therefore the following terms are used: * A **native view** refers to the final drawing surface that to composite layers to. It is the "view" used in other places of the engine, such as `Shell::AddView`. * A **platform view** refers a platform view, a layer that holds content to be embedded. ## Change The lifecycle of `ExternalViewEmbedder` is changed: <table> <thead> <tr> <th>Before PR</th> <th>After PR</th> <th>How it's called</th> </tr> </thead> <tbody> <tr> <td rowspan=2>BeginFrame</td> <td>BeginFrame</td> <td>Once per frame</td> </tr> <tr> <td>PrepareFlutterView</td> <td>Once per flutter view</td> </tr> <tr> <td>SubmitFrame</td> <td>SubmitFlutterView (renamed)</td> <td>Once per flutter view</td> </tr> <tr> <td>EndFrame</td> <td>EndFrame (unchanged)</td> <td>Once per frame</td> </tr> </tbody> </table> * `BeginFrame` should perform per-frame actions, such as merge-unmerging threads. * `PrepareView` should perform per-native-view preparations, such as recording the view ID and view size. This change is necessary because some actions in `PrepareView` needs to be refreshed at the beginning of drawing every native view. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent e22d34f commit e532e4b

21 files changed

Lines changed: 380 additions & 172 deletions

‎flow/embedded_views.cc‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,19 @@ bool DisplayListEmbedderViewSlice::recording_ended() {
4242
return builder_ == nullptr;
4343
}
4444

45-
void ExternalViewEmbedder::SubmitFrame(
45+
void ExternalViewEmbedder::SubmitFlutterView(
4646
GrDirectContext* context,
4747
const std::shared_ptr<impeller::AiksContext>& aiks_context,
4848
std::unique_ptr<SurfaceFrame> frame) {
4949
frame->Submit();
5050
}
5151

52+
bool ExternalViewEmbedder::SupportsDynamicThreadMerging() {
53+
return false;
54+
}
55+
56+
void ExternalViewEmbedder::Teardown() {}
57+
5258
void MutatorsStack::PushClipRect(const SkRect& rect) {
5359
std::shared_ptr<Mutator> element = std::make_shared<Mutator>(rect);
5460
vector_.push_back(element);
@@ -112,10 +118,4 @@ const std::vector<std::shared_ptr<Mutator>>::const_iterator MutatorsStack::End()
112118
return vector_.end();
113119
}
114120

115-
bool ExternalViewEmbedder::SupportsDynamicThreadMerging() {
116-
return false;
117-
}
118-
119-
void ExternalViewEmbedder::Teardown() {}
120-
121121
} // namespace flutter

‎flow/embedded_views.h‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,29 @@ class DisplayListEmbedderViewSlice : public EmbedderViewSlice {
369369
//
370370
// Used on iOS, Android (hybrid composite mode), and on embedded platforms
371371
// that provide a system compositor as part of the project arguments.
372+
//
373+
// There are two kinds of "view IDs" in the context of ExternalViewEmbedder, and
374+
// specific names are used to avoid ambiguation:
375+
//
376+
// * ExternalViewEmbedder composites a stack of layers. Each layer's content
377+
// might be from Flutter widgets, or a platform view, which displays platform
378+
// native components. Each platform view is labeled by a view ID, which
379+
// corresponds to the ID from `PlatformViewsRegistry.getNextPlatformViewId`
380+
// from the framework. In the context of `ExternalViewEmbedder`, this ID is
381+
// called platform_view_id.
382+
// * The layers are compositied into a single rectangular surface, displayed by
383+
// taking up an entire native window or part of a window. Each such surface
384+
// is labeled by a view ID, which corresponds to `FlutterView.viewID` from
385+
// dart:ui. In the context of `ExternalViewEmbedder`, this ID is called
386+
// flutter_view_id.
387+
//
388+
// The lifecycle of drawing a frame using ExternalViewEmbedder is:
389+
//
390+
// 1. At the start of a frame, call |BeginFrame|, then |SetUsedThisFrame| to
391+
// true.
392+
// 2. For each view to be drawn, call |PrepareFlutterView|, then
393+
// |SubmitFlutterView|.
394+
// 3. At the end of a frame, if |GetUsedThisFrame| is true, call |EndFrame|.
372395
class ExternalViewEmbedder {
373396
// TODO(cyanglaz): Make embedder own the `EmbeddedViewParams`.
374397

@@ -383,7 +406,7 @@ class ExternalViewEmbedder {
383406
// from the on-screen render target.
384407
virtual DlCanvas* GetRootCanvas() = 0;
385408

386-
// Call this in-lieu of |SubmitFrame| to clear pre-roll state and
409+
// Call this in-lieu of |SubmitFlutterView| to clear pre-roll state and
387410
// sets the stage for the next pre-roll.
388411
virtual void CancelFrame() = 0;
389412

@@ -392,13 +415,11 @@ class ExternalViewEmbedder {
392415
// The `raster_thread_merger` will be null if |SupportsDynamicThreadMerging|
393416
// returns false.
394417
virtual void BeginFrame(
395-
SkISize frame_size,
396418
GrDirectContext* context,
397-
double device_pixel_ratio,
398419
const fml::RefPtr<fml::RasterThreadMerger>& raster_thread_merger) = 0;
399420

400421
virtual void PrerollCompositeEmbeddedView(
401-
int64_t view_id,
422+
int64_t platform_view_id,
402423
std::unique_ptr<EmbeddedViewParams> params) = 0;
403424

404425
// This needs to get called after |Preroll| finishes on the layer tree.
@@ -411,14 +432,19 @@ class ExternalViewEmbedder {
411432
}
412433

413434
// Must be called on the UI thread.
414-
virtual DlCanvas* CompositeEmbeddedView(int64_t view_id) = 0;
435+
virtual DlCanvas* CompositeEmbeddedView(int64_t platform_view_id) = 0;
436+
437+
// Prepare for a view to be drawn.
438+
virtual void PrepareFlutterView(int64_t flutter_view_id,
439+
SkISize frame_size,
440+
double device_pixel_ratio) = 0;
415441

416442
// Implementers must submit the frame by calling frame.Submit().
417443
//
418444
// This method can mutate the root Skia canvas before submitting the frame.
419445
//
420446
// It can also allocate frames for overlay surfaces to compose hybrid views.
421-
virtual void SubmitFrame(
447+
virtual void SubmitFlutterView(
422448
GrDirectContext* context,
423449
const std::shared_ptr<impeller::AiksContext>& aiks_context,
424450
std::unique_ptr<SurfaceFrame> frame);
@@ -462,7 +488,7 @@ class ExternalViewEmbedder {
462488

463489
// Pushes the platform view id of a visited platform view to a list of
464490
// visited platform views.
465-
virtual void PushVisitedPlatformView(int64_t view_id) {}
491+
virtual void PushVisitedPlatformView(int64_t platform_view_id) {}
466492

467493
// Pushes a DlImageFilter object to each platform view within a list of
468494
// visited platform views.

‎flow/testing/mock_embedder.cc‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ void MockViewEmbedder::CancelFrame() {}
2525

2626
// |ExternalViewEmbedder|
2727
void MockViewEmbedder::BeginFrame(
28-
SkISize frame_size,
2928
GrDirectContext* context,
30-
double device_pixel_ratio,
3129
const fml::RefPtr<fml::RasterThreadMerger>& raster_thread_merger) {}
3230

31+
// |ExternalViewEmbedder|
32+
void MockViewEmbedder::PrepareFlutterView(int64_t flutter_view_id,
33+
SkISize frame_size,
34+
double device_pixel_ratio) {}
35+
3336
// |ExternalViewEmbedder|
3437
void MockViewEmbedder::PrerollCompositeEmbeddedView(
3538
int64_t view_id,

‎flow/testing/mock_embedder.h‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ class MockViewEmbedder : public ExternalViewEmbedder {
2525
void CancelFrame() override;
2626

2727
// |ExternalViewEmbedder|
28-
void BeginFrame(SkISize frame_size,
29-
GrDirectContext* context,
30-
double device_pixel_ratio,
28+
void BeginFrame(GrDirectContext* context,
3129
const fml::RefPtr<fml::RasterThreadMerger>&
3230
raster_thread_merger) override;
3331

32+
// |ExternalViewEmbedder|
33+
void PrepareFlutterView(int64_t flutter_view_id,
34+
SkISize frame_size,
35+
double device_pixel_ratio) override;
36+
3437
// |ExternalViewEmbedder|
3538
void PrerollCompositeEmbeddedView(
3639
int64_t view_id,

‎lib/web_ui/test/engine/hash_codes_test.dart‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ void main() {
1616
internalBootstrapBrowserTest(() => testMain);
1717
}
1818

19+
// Ignoring the deprecated member use because we're specifically testing
20+
// deprecated API.
21+
// ignore: deprecated_member_use
22+
1923
void testMain() {
2024
test('hashValues and hashList can hash lots of huge values effectively', () {
2125
final int hashValueFromArgs = hashValues(

‎shell/common/rasterizer.cc‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,6 @@ Rasterizer::DoDrawResult Rasterizer::DrawToSurfaces(
562562
std::unique_ptr<FrameItem> Rasterizer::DrawToSurfacesUnsafe(
563563
FrameTimingsRecorder& frame_timings_recorder,
564564
std::vector<std::unique_ptr<LayerTreeTask>> tasks) {
565-
// TODO(dkwingsmt): The rasterizer only supports rendering a single view
566-
// and that view must be the implicit view. Properly support multi-view
567-
// in the future.
568-
// See https://github.com/flutter/flutter/issues/135530, item 2 & 4.
569-
FML_CHECK(tasks.size() == 1u) << "Unexpected size of " << tasks.size();
570-
FML_DCHECK(tasks.front()->view_id == kFlutterImplicitViewId);
571-
572565
compositor_context_->ui_time().SetLapTime(
573566
frame_timings_recorder.GetBuildDuration());
574567

@@ -593,11 +586,8 @@ std::unique_ptr<FrameItem> Rasterizer::DrawToSurfacesUnsafe(
593586
if (external_view_embedder_) {
594587
FML_DCHECK(!external_view_embedder_->GetUsedThisFrame());
595588
external_view_embedder_->SetUsedThisFrame(true);
596-
external_view_embedder_->BeginFrame(
597-
// TODO(dkwingsmt): Add all views here.
598-
// See https://github.com/flutter/flutter/issues/135530, item 4.
599-
tasks.front()->layer_tree->frame_size(), surface_->GetContext(),
600-
tasks.front()->device_pixel_ratio, raster_thread_merger_);
589+
external_view_embedder_->BeginFrame(surface_->GetContext(),
590+
raster_thread_merger_);
601591
}
602592

603593
std::optional<fml::TimePoint> presentation_time = std::nullopt;
@@ -664,6 +654,8 @@ DrawSurfaceStatus Rasterizer::DrawToSurfaceUnsafe(
664654

665655
DlCanvas* embedder_root_canvas = nullptr;
666656
if (external_view_embedder_) {
657+
external_view_embedder_->PrepareFlutterView(
658+
view_id, layer_tree.frame_size(), device_pixel_ratio);
667659
// TODO(dkwingsmt): Add view ID here.
668660
embedder_root_canvas = external_view_embedder_->GetRootCanvas();
669661
}
@@ -704,7 +696,7 @@ DrawSurfaceStatus Rasterizer::DrawToSurfaceUnsafe(
704696
// for accurate performance metrics.
705697
if (frame->framebuffer_info().supports_partial_repaint &&
706698
!layer_tree.is_leaf_layer_tracing_enabled()) {
707-
// Disable partial repaint if external_view_embedder_ SubmitFrame is
699+
// Disable partial repaint if external_view_embedder_ SubmitFlutterView is
708700
// involved - ExternalViewEmbedder unconditionally clears the entire
709701
// surface and also partial repaint with platform view present is
710702
// something that still need to be figured out.
@@ -750,7 +742,7 @@ DrawSurfaceStatus Rasterizer::DrawToSurfaceUnsafe(
750742
if (external_view_embedder_ &&
751743
(!raster_thread_merger_ || raster_thread_merger_->IsMerged())) {
752744
FML_DCHECK(!frame->IsSubmitted());
753-
external_view_embedder_->SubmitFrame(
745+
external_view_embedder_->SubmitFlutterView(
754746
surface_->GetContext(), surface_->GetAiksContext(), std::move(frame));
755747
} else {
756748
frame->Submit();

0 commit comments

Comments
 (0)