Flutter Engine
flutter::MergedContainerLayer Class Reference

#include <container_layer.h>

Inheritance diagram for flutter::MergedContainerLayer:
flutter::ContainerLayer flutter::Layer flutter::ImageFilterLayer flutter::OpacityLayer

Public Member Functions

 MergedContainerLayer ()
 
void Add (std::shared_ptr< Layer > layer) override
 
void DiffChildren (DiffContext *context, const ContainerLayer *old_layer) override
 
- Public Member Functions inherited from flutter::ContainerLayer
 ContainerLayer ()
 
void Diff (DiffContext *context, const Layer *old_layer) override
 
void PreservePaintRegion (DiffContext *context) override
 
void Preroll (PrerollContext *context, const SkMatrix &matrix) override
 
void Paint (PaintContext &context) const override
 
const std::vector< std::shared_ptr< Layer > > & layers () const
 
- Public Member Functions inherited from flutter::Layer
 Layer ()
 
virtual ~Layer ()
 
void AssignOldLayer (Layer *old_layer)
 
virtual bool IsReplacing (DiffContext *context, const Layer *old_layer) const
 
bool subtree_has_platform_view () const
 
void set_subtree_has_platform_view (bool value)
 
const SkRect & paint_bounds () const
 
void set_paint_bounds (const SkRect &paint_bounds)
 
bool is_empty () const
 
bool needs_painting (PaintContext &context) const
 
uint64_t original_layer_id () const
 
uint64_t unique_id () const
 
virtual const PictureLayeras_picture_layer () const
 
virtual const DisplayListLayeras_display_list_layer () const
 
virtual const TextureLayeras_texture_layer () const
 
virtual const PerformanceOverlayLayeras_performance_overlay_layer () const
 
virtual const testing::MockLayeras_mock_layer () const
 

Protected Member Functions

ContainerLayerGetChildContainer () const
 Returns the ContainerLayer used to hold all of the children of the MergedContainerLayer. Note that this may not be the best layer to use for caching the children. More...
 
LayerGetCacheableChild () const
 Returns the best choice for a Layer object that can be used in RasterCache operations to cache the children. More...
 
- Protected Member Functions inherited from flutter::ContainerLayer
void PrerollChildren (PrerollContext *context, const SkMatrix &child_matrix, SkRect *child_paint_bounds)
 
void PaintChildren (PaintContext &context) const
 

Additional Inherited Members

- Static Protected Member Functions inherited from flutter::ContainerLayer
static void TryToPrepareRasterCache (PrerollContext *context, Layer *layer, const SkMatrix &matrix)
 

Detailed Description

Some ContainerLayer objects perform a rendering operation or filter on the rendered output of their children. Often that operation is changed slightly from frame to frame as part of an animation. During such an animation, the children can be cached if they are stable to avoid having to render them on every frame. Even if the children are not stable, rendering them into the raster cache during a Preroll operation will save an extra change of rendering surface during the Paint phase as compared to using the SaveLayer that would otherwise be needed with no caching.

Typically the Flutter Widget objects that lead to the creation of these layers will try to enforce only a single child Widget by their design. Unfortunately, the process of turning Widgets eventually into engine layers is not a 1:1 process so this layer might end up with multiple child layers even if the Widget only had a single child Widget.

When such a layer goes to cache the output of its children, it will need to supply a single layer to the cache mechanism since the raster cache uses a layer unique_id() as part of the cache key. If this layer ended up with multiple children, then it must first collect them into one layer for the cache mechanism. In order to provide a single layer for all of the children, this utility class will implicitly collect the children into a secondary ContainerLayer called the child container.

A by-product of creating a hidden child container, though, is that the child container is created new every time this layer is created with different properties, such as during an animation. In that scenario, it would be best to cache the single real child of this layer if it is unique and if it is stable from frame to frame. To facilitate this optimal caching strategy, this class implements two accessor methods to be used for different purposes:

When the layer needs to recurse to perform some operation on its children, it can call GetChildContainer() to return the hidden container containing all of the real children.

When the layer wants to cache the rendered contents of its children, it should call GetCacheableChild() for best performance. This method may end up returning the same layer as GetChildContainer(), but only if the conditions for optimal caching of a single child are not met.

Definition at line 98 of file container_layer.h.

Constructor & Destructor Documentation

◆ MergedContainerLayer()

flutter::MergedContainerLayer::MergedContainerLayer ( )

Definition at line 181 of file container_layer.cc.

References flutter::ContainerLayer::Add().

181  {
182  // Ensure the layer has only one direct child.
183  //
184  // Any children will actually be added as children of this empty
185  // ContainerLayer which can be accessed via ::GetContainerLayer().
186  // If only one child is ever added to this layer then that child
187  // will become the layer returned from ::GetCacheableChild().
188  // If multiple child layers are added, then this implicit container
189  // child becomes the cacheable child, but at the potential cost of
190  // not being as stable in the raster cache from frame to frame.
191  ContainerLayer::Add(std::make_shared<ContainerLayer>());
192 }
virtual void Add(std::shared_ptr< Layer > layer)

Member Function Documentation

◆ Add()

void flutter::MergedContainerLayer::Add ( std::shared_ptr< Layer layer)
overridevirtual

Reimplemented from flutter::ContainerLayer.

Definition at line 210 of file container_layer.cc.

210  {
211  GetChildContainer()->Add(std::move(layer));
212 }
ContainerLayer * GetChildContainer() const
Returns the ContainerLayer used to hold all of the children of the MergedContainerLayer. Note that this may not be the best layer to use for caching the children.
virtual void Add(std::shared_ptr< Layer > layer)

◆ DiffChildren()

void flutter::MergedContainerLayer::DiffChildren ( DiffContext context,
const ContainerLayer old_layer 
)
overridevirtual

Reimplemented from flutter::ContainerLayer.

Definition at line 194 of file container_layer.cc.

References DiffChildren(), FML_DCHECK, and flutter::DiffContext::IsSubtreeDirty().

Referenced by flutter::ImageFilterLayer::Diff(), flutter::OpacityLayer::Diff(), and DiffChildren().

195  {
196  if (context->IsSubtreeDirty()) {
197  GetChildContainer()->Diff(context, nullptr);
198  return;
199  }
200  FML_DCHECK(old_layer);
201 
202  // For MergedContainerLayer we want to diff children of ChildContainer
203  // instead of the ChildContainer itself. This works around the fact
204  // that ChildContainerLayer is ephemeral and its original_layer_id_ is always
205  // different.
206  auto layer = static_cast<const MergedContainerLayer*>(old_layer);
207  GetChildContainer()->DiffChildren(context, layer->GetChildContainer());
208 }
#define FML_DCHECK(condition)
Definition: logging.h:86
void Diff(DiffContext *context, const Layer *old_layer) override
virtual void DiffChildren(DiffContext *context, const ContainerLayer *old_layer)
ContainerLayer * GetChildContainer() const
Returns the ContainerLayer used to hold all of the children of the MergedContainerLayer. Note that this may not be the best layer to use for caching the children.

◆ GetCacheableChild()

Layer * flutter::MergedContainerLayer::GetCacheableChild ( ) const
protected

Returns the best choice for a Layer object that can be used in RasterCache operations to cache the children.

The returned Layer must represent all children and try to remain stable if the MergedContainerLayer is reconstructed in subsequent frames of the scene.

See also
GetChildContainer()
Returns
the best candidate Layer for caching the children

Definition at line 220 of file container_layer.cc.

References flutter::ContainerLayer::layers().

Referenced by flutter::ImageFilterLayer::Paint(), flutter::OpacityLayer::Paint(), flutter::ImageFilterLayer::Preroll(), and flutter::OpacityLayer::Preroll().

220  {
221  ContainerLayer* child_container = GetChildContainer();
222  if (child_container->layers().size() == 1) {
223  return child_container->layers()[0].get();
224  }
225 
226  return child_container;
227 }
ContainerLayer * GetChildContainer() const
Returns the ContainerLayer used to hold all of the children of the MergedContainerLayer. Note that this may not be the best layer to use for caching the children.

◆ GetChildContainer()

ContainerLayer * flutter::MergedContainerLayer::GetChildContainer ( ) const
protected

Returns the ContainerLayer used to hold all of the children of the MergedContainerLayer. Note that this may not be the best layer to use for caching the children.

See also
GetCacheableChild()
Returns
the ContainerLayer child used to hold the children

Definition at line 214 of file container_layer.cc.

References FML_DCHECK, flutter::ContainerLayer::layers(), and fml::size().

Referenced by flutter::ImageFilterLayer::Paint(), and flutter::OpacityLayer::Preroll().

214  {
215  FML_DCHECK(layers().size() == 1);
216 
217  return static_cast<ContainerLayer*>(layers()[0].get());
218 }
#define FML_DCHECK(condition)
Definition: logging.h:86
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
const std::vector< std::shared_ptr< Layer > > & layers() const

The documentation for this class was generated from the following files: