@@ -126,6 +126,8 @@ namespace {
126126struct PlatformView {
127127 EmbedderExternalView::ViewIdentifier view_identifier;
128128 const EmbeddedViewParams* params;
129+
130+ // The frame of the platform view, after clipping, in screen coordinates.
129131 SkRect clipped_frame;
130132
131133 explicit PlatformView (const EmbedderExternalView* view) {
@@ -160,8 +162,15 @@ struct PlatformView {
160162 }
161163};
162164
165+ // / Each layer will result in a single physical surface that contains Flutter
166+ // / contents. It may contain multiple platform views and the slices
167+ // / that would be otherwise rendered between these platform views will be
168+ // / collapsed into this layer, as long as it does not intersect any of the
169+ // / platform views.
163170class Layer {
164171 public:
172+ // / Returns whether the rectangle intersects any of the platform views of
173+ // / this layer.
165174 bool IntersectsPlatformView (const SkRect& rect) {
166175 for (auto & platform_view : platform_views_) {
167176 if (platform_view.clipped_frame .intersects (rect)) {
@@ -171,6 +180,8 @@ class Layer {
171180 return false ;
172181 }
173182
183+ // / Returns whether the region intersects any of the platform views of this
184+ // / layer.
174185 bool IntersectsPlatformView (const DlRegion& region) {
175186 for (auto & platform_view : platform_views_) {
176187 if (region.intersects (platform_view.clipped_frame .roundOut ())) {
@@ -180,18 +191,24 @@ class Layer {
180191 return false ;
181192 }
182193
194+ // / Returns whether the rectangle intersects any of the Flutter contents of
195+ // / this layer.
183196 bool IntersectsFlutterContents (const SkRect& rect) {
184197 return flutter_contents_region_.intersects (rect.roundOut ());
185198 }
186199
200+ // / Returns whether the region intersects any of the Flutter contents of this
201+ // / layer.
187202 bool IntersectsFlutterContents (const DlRegion& region) {
188203 return flutter_contents_region_.intersects (region);
189204 }
190205
206+ // / Adds a platform view to this layer.
191207 void AddPlatformView (const PlatformView& platform_view) {
192208 platform_views_.push_back (platform_view);
193209 }
194210
211+ // / Adds Flutter contents to this layer.
195212 void AddFlutterContents (EmbedderExternalView* contents,
196213 const DlRegion& contents_region) {
197214 flutter_contents_.push_back (contents);
@@ -207,6 +224,8 @@ class Layer {
207224 render_target_ = std::move (target);
208225 }
209226
227+ // / Renders this layer Flutter contents to the render target previously
228+ // / assigned with SetRenderTarget.
210229 void RenderFlutterContents () {
211230 FML_DCHECK (has_flutter_contents ());
212231 if (render_target_) {
@@ -218,6 +237,8 @@ class Layer {
218237 }
219238 }
220239
240+ // / Returns platform views for this layer. In Z-order the platform views are
241+ // / positioned *below* this layer's Flutter contents.
221242 const std::vector<PlatformView>& platform_views () const {
222243 return platform_views_;
223244 }
@@ -236,12 +257,24 @@ class Layer {
236257 std::unique_ptr<EmbedderRenderTarget> render_target_;
237258};
238259
260+ // / A layout builder is responsible for building an optimized list of Layers
261+ // / from a list of `EmbedderExternalView`s. Single EmbedderExternalView contains
262+ // / at most one platform view and at most one layer of Flutter contents.
263+ // / LayerBuilder is responsible for producing as few Layers from the list of
264+ // / EmbedderExternalViews as possible while maintaining identical visual result.
265+ // /
266+ // / Implements https://flutter.dev/go/optimized-platform-view-layers
239267class LayerBuilder {
240268 public:
241269 explicit LayerBuilder (SkISize frame_size) : frame_size_(frame_size) {
242270 layers_.push_back (Layer ());
243271 }
244272
273+ // / Adds the platform view and/or flutter contents from the
274+ // / EmbedderExternalView instance.
275+ // /
276+ // / This will try to add the content and platform view to an existing layer
277+ // / if possible. If not, a new layer will be created.
245278 void AddExternalView (EmbedderExternalView* view) {
246279 if (view->HasPlatformView ()) {
247280 PlatformView platform_view (view);
@@ -252,6 +285,7 @@ class LayerBuilder {
252285 }
253286 }
254287
288+ // / Prepares the render targets for all layers that have Flutter contents.
255289 void PrepareBackingStore (
256290 const std::function<std::unique_ptr<EmbedderRenderTarget>(
257291 FlutterBackingStoreConfig)>& target_provider) {
@@ -263,6 +297,8 @@ class LayerBuilder {
263297 }
264298 }
265299
300+ // / Renders all layers with Flutter contents to their respective render
301+ // / targets.
266302 void Render () {
267303 for (auto & layer : layers_) {
268304 if (layer.has_flutter_contents ()) {
@@ -271,6 +307,7 @@ class LayerBuilder {
271307 }
272308 }
273309
310+ // / Populates EmbedderLayers from layer builder's layers.
274311 void PushLayers (EmbedderLayers& layers) {
275312 for (auto & layer : layers_) {
276313 for (auto & view : layer.platform_views ()) {
@@ -286,6 +323,7 @@ class LayerBuilder {
286323 }
287324 }
288325
326+ // / Removes the render targets from layers and returns them for collection.
289327 std::vector<std::unique_ptr<EmbedderRenderTarget>>
290328 ClearAndCollectRenderTargets () {
291329 std::vector<std::unique_ptr<EmbedderRenderTarget>> result;
@@ -311,8 +349,16 @@ class LayerBuilder {
311349 region);
312350 }
313351
352+ // / Returns the deepest layer to which the platform view can be added. That
353+ // / would be (whichever comes first):
354+ // / - First layer from back that has platform view that intersects with this
355+ // / view
356+ // / - Very last layer from back that has surface that doesn't intersect with
357+ // / this. That is because layer content renders on top of the platform view.
314358 Layer& GetLayerForPlatformView (PlatformView view) {
315359 for (auto iter = layers_.rbegin (); iter != layers_.rend (); ++iter) {
360+ // This layer has surface that intersects with this view. That means we
361+ // went one too far and need the layer before this.
316362 if (iter->IntersectsFlutterContents (view.clipped_frame )) {
317363 if (iter == layers_.rbegin ()) {
318364 layers_.emplace_back ();
@@ -329,6 +375,8 @@ class LayerBuilder {
329375 return layers_.front ();
330376 }
331377
378+ // / Finds layer to which the Flutter content can be added. That would
379+ // / be first layer from back that has any intersection with this region.
332380 Layer& GetLayerForFlutterContentsRegion (const DlRegion& region) {
333381 for (auto iter = layers_.rbegin (); iter != layers_.rend (); ++iter) {
334382 if (iter->IntersectsPlatformView (region) ||
@@ -345,7 +393,6 @@ class LayerBuilder {
345393
346394}; // namespace
347395
348- // https://flutter.dev/go/optimized-platform-view-layers
349396void EmbedderExternalViewEmbedder::SubmitFrame (
350397 GrDirectContext* context,
351398 const std::shared_ptr<impeller::AiksContext>& aiks_context,
0 commit comments