Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Member Functions | Friends | List of all members
flutter::RasterCache Class Reference

#include <raster_cache.h>

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

Classes

struct  CacheInfo
 
struct  Context
 

Public Member Functions

std::unique_ptr< RasterCacheResultRasterize (const RasterCache::Context &context, sk_sp< const DlRTree > rtree, const std::function< void(DlCanvas *)> &draw_function, const std::function< void(DlCanvas *, const SkRect &rect)> &draw_checkerboard) const
 
 RasterCache (size_t access_threshold=3, size_t picture_and_display_list_cache_limit_per_frame=RasterCacheUtil::kDefaultPictureAndDisplayListCacheLimitPerFrame)
 
virtual ~RasterCache ()=default
 
bool Draw (const RasterCacheKeyID &id, DlCanvas &canvas, const DlPaint *paint, bool preserve_rtree=false) const
 
bool HasEntry (const RasterCacheKeyID &id, const SkMatrix &) const
 
void BeginFrame ()
 
void EvictUnusedCacheEntries ()
 
void EndFrame ()
 
void Clear ()
 
void SetCheckboardCacheImages (bool checkerboard)
 
const RasterCacheMetricspicture_metrics () const
 
const RasterCacheMetricslayer_metrics () const
 
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.
 
size_t EstimateLayerCacheByteSize () const
 Estimate how much memory is used by layer raster cache entries in bytes.
 
size_t access_threshold () const
 Return the number of frames that a picture must be prepared before it will be cached. If the number is 0, then no picture will ever be cached.
 
bool GenerateNewCacheInThisFrame () const
 
CacheInfo MarkSeen (const RasterCacheKeyID &id, const SkMatrix &matrix, bool visible) const
 The entry whose RasterCacheKey is generated by RasterCacheKeyID and matrix is marked as encountered by the current frame. The entry will be created if it does not exist. Optionally the entry will be marked as visible in the current frame if the caller determines that it intersects the cull rect. The access_count of the entry will be increased if it is visible, or if it was ever visible.
 
int GetAccessCount (const RasterCacheKeyID &id, const SkMatrix &matrix) const
 
bool UpdateCacheEntry (const RasterCacheKeyID &id, const Context &raster_cache_context, const std::function< void(DlCanvas *)> &render_function, sk_sp< const DlRTree > rtree=nullptr) const
 

Friends

class RasterCacheItem
 
class LayerRasterCacheItem
 

Detailed Description

RasterCache is used to cache rasterized layers or display lists to improve performance.

Life cycle of RasterCache methods:

Definition at line 116 of file raster_cache.h.

Constructor & Destructor Documentation

◆ RasterCache()

flutter::RasterCache::RasterCache ( size_t  access_threshold = 3,
size_t  picture_and_display_list_cache_limit_per_frame = RasterCacheUtil::kDefaultPictureAndDisplayListCacheLimitPerFrame 
)
explicit

Definition at line 71 of file raster_cache.cc.

73 : access_threshold_(access_threshold),
74 display_list_cache_limit_per_frame_(display_list_cache_limit_per_frame) {}
size_t access_threshold() const
Return the number of frames that a picture must be prepared before it will be cached....

◆ ~RasterCache()

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

Member Function Documentation

◆ access_threshold()

size_t flutter::RasterCache::access_threshold ( ) const
inline

Return the number of frames that a picture must be prepared before it will be cached. If the number is 0, then no picture will ever be cached.

If the number is one, then it must be prepared and drawn on 1 frame and it will then be cached on the next frame if it is prepared.

Definition at line 216 of file raster_cache.h.

216{ return access_threshold_; }

◆ BeginFrame()

void flutter::RasterCache::BeginFrame ( )

Definition at line 193 of file raster_cache.cc.

193 {
194 display_list_cached_this_frame_ = 0;
195 picture_metrics_ = {};
196 layer_metrics_ = {};
197}

◆ Clear()

void flutter::RasterCache::Clear ( )

Definition at line 237 of file raster_cache.cc.

237 {
238 cache_.clear();
239 picture_metrics_ = {};
240 layer_metrics_ = {};
241}

◆ Draw()

bool flutter::RasterCache::Draw ( const RasterCacheKeyID id,
DlCanvas canvas,
const DlPaint paint,
bool  preserve_rtree = false 
) const

Definition at line 174 of file raster_cache.cc.

177 {
178 auto it = cache_.find(RasterCacheKey(id, canvas.GetTransform()));
179 if (it == cache_.end()) {
180 return false;
181 }
182
183 Entry& entry = it->second;
184
185 if (entry.image) {
186 entry.image->draw(canvas, paint, preserve_rtree);
187 return true;
188 }
189
190 return false;
191}
const Paint & paint

◆ EndFrame()

void flutter::RasterCache::EndFrame ( )

Definition at line 232 of file raster_cache.cc.

232 {
233 UpdateMetrics();
234 TraceStatsToTimeline();
235}

◆ 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 292 of file raster_cache.cc.

292 {
293 size_t layer_cache_bytes = 0;
294 for (const auto& item : cache_) {
295 if (item.first.kind() == RasterCacheKeyKind::kLayerMetrics &&
296 item.second.image) {
297 layer_cache_bytes += item.second.image->image_bytes();
298 }
299 }
300 return layer_cache_bytes;
301}

◆ 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 303 of file raster_cache.cc.

303 {
304 size_t picture_cache_bytes = 0;
305 for (const auto& item : cache_) {
306 if (item.first.kind() == RasterCacheKeyKind::kDisplayListMetrics &&
307 item.second.image) {
308 picture_cache_bytes += item.second.image->image_bytes();
309 }
310 }
311 return picture_cache_bytes;
312}

◆ EvictUnusedCacheEntries()

void flutter::RasterCache::EvictUnusedCacheEntries ( )

Definition at line 212 of file raster_cache.cc.

212 {
213 std::vector<RasterCacheKey::Map<Entry>::iterator> dead;
214
215 for (auto it = cache_.begin(); it != cache_.end(); ++it) {
216 Entry& entry = it->second;
217 if (!entry.encountered_this_frame) {
218 dead.push_back(it);
219 }
220 }
221
222 for (auto it : dead) {
223 if (it->second.image) {
224 RasterCacheMetrics& metrics = GetMetricsForKind(it->first.kind());
225 metrics.eviction_count++;
226 metrics.eviction_bytes += it->second.image->image_bytes();
227 }
228 cache_.erase(it);
229 }
230}

◆ GenerateNewCacheInThisFrame()

bool flutter::RasterCache::GenerateNewCacheInThisFrame ( ) const
inline

Definition at line 218 of file raster_cache.h.

218 {
219 // Disabling caching when access_threshold is zero is historic behavior.
220 return access_threshold_ != 0 && display_list_cached_this_frame_ <
221 display_list_cache_limit_per_frame_;
222 }

◆ GetAccessCount()

int flutter::RasterCache::GetAccessCount ( const RasterCacheKeyID id,
const SkMatrix matrix 
) const

Returns the access count (i.e. accesses_since_visible) for the given entry in the cache, or -1 if no such entry exists.

Definition at line 155 of file raster_cache.cc.

156 {
157 RasterCacheKey key = RasterCacheKey(id, matrix);
158 auto entry = cache_.find(key);
159 if (entry != cache_.cend()) {
160 return entry->second.accesses_since_visible;
161 }
162 return -1;
163}

◆ GetCachedEntriesCount()

size_t flutter::RasterCache::GetCachedEntriesCount ( ) const

Definition at line 243 of file raster_cache.cc.

243 {
244 return cache_.size();
245}

◆ GetLayerCachedEntriesCount()

size_t flutter::RasterCache::GetLayerCachedEntriesCount ( ) const

Return the number of map entries in the layer cache regardless of whether the entries have been populated with an image.

Definition at line 247 of file raster_cache.cc.

247 {
248 size_t layer_cached_entries_count = 0;
249 for (const auto& item : cache_) {
250 if (item.first.kind() == RasterCacheKeyKind::kLayerMetrics) {
251 layer_cached_entries_count++;
252 }
253 }
254 return layer_cached_entries_count;
255}

◆ GetPictureCachedEntriesCount()

size_t flutter::RasterCache::GetPictureCachedEntriesCount ( ) const

Return the number of map entries in the picture (DisplayList) cache regardless of whether the entries have been populated with an image.

Definition at line 257 of file raster_cache.cc.

257 {
258 size_t display_list_cached_entries_count = 0;
259 for (const auto& item : cache_) {
260 if (item.first.kind() == RasterCacheKeyKind::kDisplayListMetrics) {
261 display_list_cached_entries_count++;
262 }
263 }
264 return display_list_cached_entries_count;
265}

◆ HasEntry()

bool flutter::RasterCache::HasEntry ( const RasterCacheKeyID id,
const SkMatrix matrix 
) const

Definition at line 165 of file raster_cache.cc.

166 {
167 RasterCacheKey key = RasterCacheKey(id, matrix);
168 if (cache_.find(key) != cache_.cend()) {
169 return true;
170 }
171 return false;
172}

◆ layer_metrics()

const RasterCacheMetrics & flutter::RasterCache::layer_metrics ( ) const
inline

Definition at line 172 of file raster_cache.h.

172{ return layer_metrics_; }

◆ MarkSeen()

RasterCache::CacheInfo flutter::RasterCache::MarkSeen ( const RasterCacheKeyID id,
const SkMatrix matrix,
bool  visible 
) const

The entry whose RasterCacheKey is generated by RasterCacheKeyID and matrix is marked as encountered by the current frame. The entry will be created if it does not exist. Optionally the entry will be marked as visible in the current frame if the caller determines that it intersects the cull rect. The access_count of the entry will be increased if it is visible, or if it was ever visible.

Returns
the number of times the entry has been hit since it was created. For a new entry that will be 1 if it is visible, or zero if non-visible.

Definition at line 142 of file raster_cache.cc.

144 {
145 RasterCacheKey key = RasterCacheKey(id, matrix);
146 Entry& entry = cache_[key];
147 entry.encountered_this_frame = true;
148 entry.visible_this_frame = visible;
149 if (visible || entry.accesses_since_visible > 0) {
150 entry.accesses_since_visible++;
151 }
152 return {entry.accesses_since_visible, entry.image != nullptr};
153}

◆ picture_metrics()

const RasterCacheMetrics & flutter::RasterCache::picture_metrics ( ) const
inline

Definition at line 171 of file raster_cache.h.

171{ return picture_metrics_; }

◆ Rasterize()

std::unique_ptr< RasterCacheResult > flutter::RasterCache::Rasterize ( const RasterCache::Context context,
sk_sp< const DlRTree rtree,
const std::function< void(DlCanvas *)> &  draw_function,
const std::function< void(DlCanvas *, const SkRect &rect)> &  draw_checkerboard 
) const
Note
Procedure doesn't copy all closures.

Definition at line 77 of file raster_cache.cc.

82 {
83 auto matrix = RasterCacheUtil::GetIntegralTransCTM(context.matrix);
84 SkRect dest_rect =
85 RasterCacheUtil::GetRoundedOutDeviceBounds(context.logical_rect, matrix);
86
87 const SkImageInfo image_info = SkImageInfo::MakeN32Premul(
88 dest_rect.width(), dest_rect.height(), context.dst_color_space);
89
91 context.gr_context
93 image_info)
94 : SkSurfaces::Raster(image_info);
95
96 if (!surface) {
97 return nullptr;
98 }
99
100 DlSkCanvasAdapter canvas(surface->getCanvas());
101 canvas.Clear(DlColor::kTransparent());
102
103 canvas.Translate(-dest_rect.left(), -dest_rect.top());
104 canvas.Transform(matrix);
105 draw_function(&canvas);
106
107 if (checkerboard_images_) {
108 draw_checkerboard(&canvas, context.logical_rect);
109 }
110
111 auto image = DlImage::Make(surface->makeImageSnapshot());
112 return std::make_unique<RasterCacheResult>(
113 image, context.logical_rect, context.flow_type, std::move(rtree));
114}
static sk_sp< DlImage > Make(const SkImage *image)
Definition dl_image.cc:11
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkImage > image
Definition examples.cpp:29
unsigned useCenter Optional< SkMatrix > matrix
Definition SkRecords.h:258
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
void draw_checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)
static SkImageInfo MakeN32Premul(int width, int height)
constexpr float left() const
Definition SkRect.h:734
constexpr float top() const
Definition SkRect.h:741
constexpr float height() const
Definition SkRect.h:769
constexpr float width() const
Definition SkRect.h:762
static constexpr DlColor kTransparent()
Definition dl_color.h:21
static SkMatrix GetIntegralTransCTM(const SkMatrix &ctm)
Snap the translation components of the matrix to integers.
static SkRect GetRoundedOutDeviceBounds(const SkRect &rect, const SkMatrix &ctm)

◆ SetCheckboardCacheImages()

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

Definition at line 267 of file raster_cache.cc.

267 {
268 if (checkerboard_images_ == checkerboard) {
269 return;
270 }
271
272 checkerboard_images_ = checkerboard;
273
274 // Clear all existing entries so previously rasterized items (with or without
275 // a checkerboard) will be refreshed in subsequent passes.
276 Clear();
277}
static void checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)

◆ UpdateCacheEntry()

bool flutter::RasterCache::UpdateCacheEntry ( const RasterCacheKeyID id,
const Context raster_cache_context,
const std::function< void(DlCanvas *)> &  render_function,
sk_sp< const DlRTree rtree = nullptr 
) const

Definition at line 116 of file raster_cache.cc.

120 {
121 RasterCacheKey key = RasterCacheKey(id, raster_cache_context.matrix);
122 Entry& entry = cache_[key];
123 if (!entry.image) {
124 void (*func)(DlCanvas*, const SkRect& rect) = DrawCheckerboard;
125 entry.image = Rasterize(raster_cache_context, std::move(rtree),
126 render_function, func);
127 if (entry.image != nullptr) {
128 switch (id.type()) {
130 display_list_cached_this_frame_++;
131 break;
132 }
133 default:
134 break;
135 }
136 return true;
137 }
138 }
139 return entry.image != nullptr;
140}
std::unique_ptr< RasterCacheResult > Rasterize(const RasterCache::Context &context, sk_sp< const DlRTree > rtree, const std::function< void(DlCanvas *)> &draw_function, const std::function< void(DlCanvas *, const SkRect &rect)> &draw_checkerboard) const
sk_sp< SkBlender > blender SkRect rect
Definition SkRecords.h:350
void DrawCheckerboard(DlCanvas *canvas, const SkRect &rect)

Friends And Related Symbol Documentation

◆ LayerRasterCacheItem

friend class LayerRasterCacheItem
friend

Definition at line 272 of file raster_cache.h.

◆ RasterCacheItem

friend class RasterCacheItem
friend

Definition at line 271 of file raster_cache.h.


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