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
 
- Public Member Functions inherited from flutter::ContainerLayer
 ContainerLayer ()
 
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 ()
 
bool needs_system_composite () const
 
void set_needs_system_composite (bool value)
 
const SkRect & paint_bounds () const
 
void set_paint_bounds (const SkRect &paint_bounds)
 
bool needs_painting () const
 
uint64_t unique_id () 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 100 of file container_layer.h.

Constructor & Destructor Documentation

◆ MergedContainerLayer()

flutter::MergedContainerLayer::MergedContainerLayer ( )

Definition at line 126 of file container_layer.cc.

References flutter::ContainerLayer::Add().

126  {
127  // Ensure the layer has only one direct child.
128  //
129  // Any children will actually be added as children of this empty
130  // ContainerLayer which can be accessed via ::GetContainerLayer().
131  // If only one child is ever added to this layer then that child
132  // will become the layer returned from ::GetCacheableChild().
133  // If multiple child layers are added, then this implicit container
134  // child becomes the cacheable child, but at the potential cost of
135  // not being as stable in the raster cache from frame to frame.
136  ContainerLayer::Add(std::make_shared<ContainerLayer>());
137 }
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 139 of file container_layer.cc.

139  {
140  GetChildContainer()->Add(std::move(layer));
141 }
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)

◆ 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 149 of file container_layer.cc.

References flutter::ContainerLayer::layers().

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

149  {
150  ContainerLayer* child_container = GetChildContainer();
151  if (child_container->layers().size() == 1) {
152  return child_container->layers()[0].get();
153  }
154 
155  return child_container;
156 }
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 143 of file container_layer.cc.

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

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

143  {
144  FML_DCHECK(layers().size() == 1);
145 
146  return static_cast<ContainerLayer*>(layers()[0].get());
147 }
#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: