Flutter Engine
flutter::RasterCache Class Reference

#include <raster_cache.h>

Inheritance diagram for flutter::RasterCache:
flutter::testing::MockRasterCache

Public Member Functions

 RasterCache (size_t access_threshold=3, size_t picture_cache_limit_per_frame=kDefaultPictureCacheLimitPerFrame)
 
virtual ~RasterCache ()=default
 
virtual std::unique_ptr< RasterCacheResultRasterizePicture (SkPicture *picture, GrDirectContext *context, const SkMatrix &ctm, SkColorSpace *dst_color_space, bool checkerboard) const
 Rasterize a picture object and produce a RasterCacheResult to be stored in the cache. More...
 
virtual std::unique_ptr< RasterCacheResultRasterizeLayer (PrerollContext *context, Layer *layer, const SkMatrix &ctm, bool checkerboard) const
 Rasterize an engine Layer and produce a RasterCacheResult to be stored in the cache. More...
 
bool Prepare (GrDirectContext *context, SkPicture *picture, const SkMatrix &transformation_matrix, SkColorSpace *dst_color_space, bool is_complex, bool will_change)
 
void Prepare (PrerollContext *context, Layer *layer, const SkMatrix &ctm)
 
bool Draw (const SkPicture &picture, SkCanvas &canvas) const
 
bool Draw (const Layer *layer, SkCanvas &canvas, SkPaint *paint=nullptr) const
 
void SweepAfterFrame ()
 
void Clear ()
 
void SetCheckboardCacheImages (bool checkerboard)
 
size_t GetCachedEntriesCount () const
 
size_t GetLayerCachedEntriesCount () const
 
size_t GetPictureCachedEntriesCount () const
 
size_t EstimatePictureCacheByteSize () const
 Estimate how much memory is used by picture raster cache entries in bytes. More...
 
size_t EstimateLayerCacheByteSize () const
 Estimate how much memory is used by layer raster cache entries in bytes. More...
 

Static Public Member Functions

static SkIRect GetDeviceBounds (const SkRect &rect, const SkMatrix &ctm)
 
static SkMatrix GetIntegralTransCTM (const SkMatrix &ctm)
 Snap the translation components of the matrix to integers. More...
 

Static Public Attributes

static constexpr int kDefaultPictureCacheLimitPerFrame = 3
 

Detailed Description

Definition at line 42 of file raster_cache.h.

Constructor & Destructor Documentation

◆ RasterCache()

flutter::RasterCache::RasterCache ( size_t  access_threshold = 3,
size_t  picture_cache_limit_per_frame = kDefaultPictureCacheLimitPerFrame 
)
explicit

Definition at line 38 of file raster_cache.cc.

40  : access_threshold_(access_threshold),
41  picture_cache_limit_per_frame_(picture_cache_limit_per_frame),
42  checkerboard_images_(false) {}

◆ ~RasterCache()

virtual flutter::RasterCache::~RasterCache ( )
virtualdefault

Member Function Documentation

◆ Clear()

void flutter::RasterCache::Clear ( )

Definition at line 271 of file raster_cache.cc.

Referenced by SetCheckboardCacheImages().

271  {
272  picture_cache_.clear();
273  layer_cache_.clear();
274 }

◆ Draw() [1/2]

bool flutter::RasterCache::Draw ( const SkPicture &  picture,
SkCanvas &  canvas 
) const

Definition at line 224 of file raster_cache.cc.

Referenced by flutter::ImageFilterLayer::Paint(), flutter::PictureLayer::Paint(), flutter::OpacityLayer::Paint(), and flutter::testing::TEST().

224  {
225  PictureRasterCacheKey cache_key(picture.uniqueID(), canvas.getTotalMatrix());
226  auto it = picture_cache_.find(cache_key);
227  if (it == picture_cache_.end()) {
228  return false;
229  }
230 
231  Entry& entry = it->second;
232  entry.access_count++;
233  entry.used_this_frame = true;
234 
235  if (entry.image) {
236  entry.image->draw(canvas, nullptr);
237  return true;
238  }
239 
240  return false;
241 }
RasterCacheKey< uint32_t > PictureRasterCacheKey

◆ Draw() [2/2]

bool flutter::RasterCache::Draw ( const Layer layer,
SkCanvas &  canvas,
SkPaint *  paint = nullptr 
) const

Definition at line 243 of file raster_cache.cc.

References flutter::Layer::unique_id().

245  {
246  LayerRasterCacheKey cache_key(layer->unique_id(), canvas.getTotalMatrix());
247  auto it = layer_cache_.find(cache_key);
248  if (it == layer_cache_.end()) {
249  return false;
250  }
251 
252  Entry& entry = it->second;
253  entry.access_count++;
254  entry.used_this_frame = true;
255 
256  if (entry.image) {
257  entry.image->draw(canvas, paint);
258  return true;
259  }
260 
261  return false;
262 }
RasterCacheKey< uint64_t > LayerRasterCacheKey

◆ EstimateLayerCacheByteSize()

size_t flutter::RasterCache::EstimateLayerCacheByteSize ( ) const

Estimate how much memory is used by layer raster cache entries in bytes.

Only SkImage's memory usage is counted as other objects are often much smaller compared to SkImage. SkImageInfo::computeMinByteSize is used to estimate the SkImage memory usage.

Definition at line 311 of file raster_cache.cc.

Referenced by SetCheckboardCacheImages().

311  {
312  size_t layer_cache_bytes = 0;
313  for (const auto& item : layer_cache_) {
314  if (item.second.image) {
315  layer_cache_bytes += item.second.image->image_bytes();
316  }
317  }
318  return layer_cache_bytes;
319 }

◆ EstimatePictureCacheByteSize()

size_t flutter::RasterCache::EstimatePictureCacheByteSize ( ) const

Estimate how much memory is used by picture raster cache entries in bytes.

Only SkImage's memory usage is counted as other objects are often much smaller compared to SkImage. SkImageInfo::computeMinByteSize is used to estimate the SkImage memory usage.

Definition at line 321 of file raster_cache.cc.

Referenced by SetCheckboardCacheImages().

321  {
322  size_t picture_cache_bytes = 0;
323  for (const auto& item : picture_cache_) {
324  if (item.second.image) {
325  picture_cache_bytes += item.second.image->image_bytes();
326  }
327  }
328  return picture_cache_bytes;
329 }

◆ GetCachedEntriesCount()

size_t flutter::RasterCache::GetCachedEntriesCount ( ) const

Definition at line 276 of file raster_cache.cc.

Referenced by flutter::testing::TEST_F().

276  {
277  return layer_cache_.size() + picture_cache_.size();
278 }

◆ GetDeviceBounds()

static SkIRect flutter::RasterCache::GetDeviceBounds ( const SkRect &  rect,
const SkMatrix &  ctm 
)
inlinestatic

Definition at line 98 of file raster_cache.h.

Referenced by flutter::RasterCacheResult::draw(), flutter::Rasterize(), flutter::testing::MockRasterCache::RasterizeLayer(), and flutter::testing::MockRasterCache::RasterizePicture().

98  {
99  SkRect device_rect;
100  ctm.mapRect(&device_rect, rect);
101  SkIRect bounds;
102  device_rect.roundOut(&bounds);
103  return bounds;
104  }

◆ GetIntegralTransCTM()

static SkMatrix flutter::RasterCache::GetIntegralTransCTM ( const SkMatrix &  ctm)
inlinestatic

Snap the translation components of the matrix to integers.

The snapping will only happen if the matrix only has scale and translation transformations.

Parameters
ctmthe current transformation matrix.
Returns
SkMatrix the snapped transformation matrix.

Definition at line 115 of file raster_cache.h.

References FML_DISALLOW_COPY_AND_ASSIGN.

Referenced by flutter::PictureLayer::Paint(), flutter::OpacityLayer::Paint(), flutter::PictureLayer::Preroll(), flutter::OpacityLayer::Preroll(), and flutter::testing::TEST_F().

115  {
116  // Avoid integral snapping if the matrix has complex transformation to avoid
117  // the artifact observed in https://github.com/flutter/flutter/issues/41654.
118  if (!ctm.isScaleTranslate()) {
119  return ctm;
120  }
121  SkMatrix result = ctm;
122  result[SkMatrix::kMTransX] = SkScalarRoundToScalar(ctm.getTranslateX());
123  result[SkMatrix::kMTransY] = SkScalarRoundToScalar(ctm.getTranslateY());
124  return result;
125  }

◆ GetLayerCachedEntriesCount()

size_t flutter::RasterCache::GetLayerCachedEntriesCount ( ) const

Definition at line 280 of file raster_cache.cc.

280  {
281  return layer_cache_.size();
282 }

◆ GetPictureCachedEntriesCount()

size_t flutter::RasterCache::GetPictureCachedEntriesCount ( ) const

Definition at line 284 of file raster_cache.cc.

284  {
285  return picture_cache_.size();
286 }

◆ Prepare() [1/2]

bool flutter::RasterCache::Prepare ( GrDirectContext *  context,
SkPicture *  picture,
const SkMatrix &  transformation_matrix,
SkColorSpace *  dst_color_space,
bool  is_complex,
bool  will_change 
)

Definition at line 180 of file raster_cache.cc.

References flutter::IsPictureWorthRasterizing(), flutter::MatrixDecomposition::IsValid(), and RasterizePicture().

Referenced by flutter::testing::TEST(), and flutter::ContainerLayer::TryToPrepareRasterCache().

185  {
186  // Disabling caching when access_threshold is zero is historic behavior.
187  if (access_threshold_ == 0) {
188  return false;
189  }
190  if (picture_cached_this_frame_ >= picture_cache_limit_per_frame_) {
191  return false;
192  }
193  if (!IsPictureWorthRasterizing(picture, will_change, is_complex)) {
194  // We only deal with pictures that are worthy of rasterization.
195  return false;
196  }
197 
198  // Decompose the matrix (once) for all subsequent operations. We want to make
199  // sure to avoid volumetric distortions while accounting for scaling.
200  const MatrixDecomposition matrix(transformation_matrix);
201 
202  if (!matrix.IsValid()) {
203  // The matrix was singular. No point in going further.
204  return false;
205  }
206 
207  PictureRasterCacheKey cache_key(picture->uniqueID(), transformation_matrix);
208 
209  // Creates an entry, if not present prior.
210  Entry& entry = picture_cache_[cache_key];
211  if (entry.access_count < access_threshold_) {
212  // Frame threshold has not yet been reached.
213  return false;
214  }
215 
216  if (!entry.image) {
217  entry.image = RasterizePicture(picture, context, transformation_matrix,
218  dst_color_space, checkerboard_images_);
219  picture_cached_this_frame_++;
220  }
221  return true;
222 }
virtual std::unique_ptr< RasterCacheResult > RasterizePicture(SkPicture *picture, GrDirectContext *context, const SkMatrix &ctm, SkColorSpace *dst_color_space, bool checkerboard) const
Rasterize a picture object and produce a RasterCacheResult to be stored in the cache.
static bool IsPictureWorthRasterizing(SkPicture *picture, bool will_change, bool is_complex)
Definition: raster_cache.cc:64
RasterCacheKey< uint32_t > PictureRasterCacheKey

◆ Prepare() [2/2]

void flutter::RasterCache::Prepare ( PrerollContext context,
Layer layer,
const SkMatrix &  ctm 
)

Definition at line 138 of file raster_cache.cc.

References RasterizeLayer(), and flutter::Layer::unique_id().

140  {
141  LayerRasterCacheKey cache_key(layer->unique_id(), ctm);
142  Entry& entry = layer_cache_[cache_key];
143  entry.access_count++;
144  entry.used_this_frame = true;
145  if (!entry.image) {
146  entry.image = RasterizeLayer(context, layer, ctm, checkerboard_images_);
147  }
148 }
virtual std::unique_ptr< RasterCacheResult > RasterizeLayer(PrerollContext *context, Layer *layer, const SkMatrix &ctm, bool checkerboard) const
Rasterize an engine Layer and produce a RasterCacheResult to be stored in the cache.
RasterCacheKey< uint64_t > LayerRasterCacheKey

◆ RasterizeLayer()

std::unique_ptr< RasterCacheResult > flutter::RasterCache::RasterizeLayer ( PrerollContext context,
Layer layer,
const SkMatrix &  ctm,
bool  checkerboard 
) const
virtual

Rasterize an engine Layer and produce a RasterCacheResult to be stored in the cache.

Parameters
contextthe PrerollContext containing important information needed for rendering a layer.
layerthe Layer object to be cached.
ctmthe transformation matrix used for rendering.
checkerboarda flag indicating whether or not a checkerboard pattern should be rendered into the cached image for debug analysis
Returns
a RasterCacheResult that can draw the rendered layer into the destination using a simple image blit

Reimplemented in flutter::testing::MockRasterCache.

Definition at line 150 of file raster_cache.cc.

References flutter::PrerollContext::checkerboard_offscreen_layers, flutter::PrerollContext::dst_color_space, flutter::PrerollContext::frame_device_pixel_ratio, flutter::PrerollContext::gr_context, flutter::PrerollContext::has_platform_view, flutter::Layer::needs_painting(), flutter::Layer::Paint(), flutter::Layer::paint_bounds(), flutter::PrerollContext::raster_cache, flutter::PrerollContext::raster_time, flutter::Rasterize(), flutter::PrerollContext::texture_registry, and flutter::PrerollContext::ui_time.

Referenced by Prepare().

154  {
155  return Rasterize(
156  context->gr_context, ctm, context->dst_color_space, checkerboard,
157  layer->paint_bounds(), [layer, context](SkCanvas* canvas) {
158  SkISize canvas_size = canvas->getBaseLayerSize();
159  SkNWayCanvas internal_nodes_canvas(canvas_size.width(),
160  canvas_size.height());
161  internal_nodes_canvas.addCanvas(canvas);
162  Layer::PaintContext paintContext = {
163  /* internal_nodes_canvas= */ static_cast<SkCanvas*>(
164  &internal_nodes_canvas),
165  /* leaf_nodes_canvas= */ canvas,
166  /* gr_context= */ context->gr_context,
167  /* view_embedder= */ nullptr,
168  context->raster_time,
169  context->ui_time,
170  context->texture_registry,
171  context->has_platform_view ? nullptr : context->raster_cache,
172  context->checkerboard_offscreen_layers,
173  context->frame_device_pixel_ratio};
174  if (layer->needs_painting()) {
175  layer->Paint(paintContext);
176  }
177  });
178 }
static std::unique_ptr< RasterCacheResult > Rasterize(GrDirectContext *context, const SkMatrix &ctm, SkColorSpace *dst_color_space, bool checkerboard, const SkRect &logical_rect, const std::function< void(SkCanvas *)> &draw_function)
Definition: raster_cache.cc:91

◆ RasterizePicture()

std::unique_ptr< RasterCacheResult > flutter::RasterCache::RasterizePicture ( SkPicture *  picture,
GrDirectContext *  context,
const SkMatrix &  ctm,
SkColorSpace *  dst_color_space,
bool  checkerboard 
) const
virtual

Rasterize a picture object and produce a RasterCacheResult to be stored in the cache.

Parameters
picturethe SkPicture object to be cached.
contextthe GrDirectContext used for rendering.
ctmthe transformation matrix used for rendering.
dst_color_spacethe destination color space that the cached rendering will be drawn into
checkerboarda flag indicating whether or not a checkerboard pattern should be rendered into the cached image for debug analysis
Returns
a RasterCacheResult that can draw the rendered picture into the destination using a simple image blit

Reimplemented in flutter::testing::MockRasterCache.

Definition at line 127 of file raster_cache.cc.

References flutter::Rasterize().

Referenced by Prepare().

132  {
133  return Rasterize(context, ctm, dst_color_space, checkerboard,
134  picture->cullRect(),
135  [=](SkCanvas* canvas) { canvas->drawPicture(picture); });
136 }
static std::unique_ptr< RasterCacheResult > Rasterize(GrDirectContext *context, const SkMatrix &ctm, SkColorSpace *dst_color_space, bool checkerboard, const SkRect &logical_rect, const std::function< void(SkCanvas *)> &draw_function)
Definition: raster_cache.cc:91

◆ SetCheckboardCacheImages()

void flutter::RasterCache::SetCheckboardCacheImages ( bool  checkerboard)

Definition at line 288 of file raster_cache.cc.

References Clear(), EstimateLayerCacheByteSize(), EstimatePictureCacheByteSize(), FML_TRACE_COUNTER, and flutter::kMegaByteSizeInBytes.

Referenced by flutter::LayerTree::Preroll().

288  {
289  if (checkerboard_images_ == checkerboard) {
290  return;
291  }
292 
293  checkerboard_images_ = checkerboard;
294 
295  // Clear all existing entries so previously rasterized items (with or without
296  // a checkerboard) will be refreshed in subsequent passes.
297  Clear();
298 }

◆ SweepAfterFrame()

void flutter::RasterCache::SweepAfterFrame ( )

Definition at line 264 of file raster_cache.cc.

Referenced by flutter::CompositorContext::CompositorContext(), and flutter::testing::TEST().

264  {
265  SweepOneCacheAfterFrame(picture_cache_);
266  SweepOneCacheAfterFrame(layer_cache_);
267  picture_cached_this_frame_ = 0;
268  TraceStatsToTimeline();
269 }

Member Data Documentation

◆ kDefaultPictureCacheLimitPerFrame

constexpr int flutter::RasterCache::kDefaultPictureCacheLimitPerFrame = 3
static

Definition at line 48 of file raster_cache.h.


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