Flutter Engine
The Flutter Engine
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 ()
 
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. More...
 
size_t EstimateLayerCacheByteSize () const
 Estimate how much memory is used by layer raster cache entries in bytes. More...
 
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. More...
 
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. More...
 
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 118 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 73 of file raster_cache.cc.

75 : access_threshold_(access_threshold),
76 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....
Definition: raster_cache.h:216

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

195 {
196 display_list_cached_this_frame_ = 0;
197 picture_metrics_ = {};
198 layer_metrics_ = {};
199}

◆ Clear()

void flutter::RasterCache::Clear ( )

Definition at line 239 of file raster_cache.cc.

239 {
240 cache_.clear();
241 picture_metrics_ = {};
242 layer_metrics_ = {};
243}

◆ Draw()

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

Definition at line 176 of file raster_cache.cc.

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

◆ EndFrame()

void flutter::RasterCache::EndFrame ( )

Definition at line 234 of file raster_cache.cc.

234 {
235 UpdateMetrics();
236 TraceStatsToTimeline();
237}

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

282 {
283 size_t layer_cache_bytes = 0;
284 for (const auto& item : cache_) {
285 if (item.first.kind() == RasterCacheKeyKind::kLayerMetrics &&
286 item.second.image) {
287 layer_cache_bytes += item.second.image->image_bytes();
288 }
289 }
290 return layer_cache_bytes;
291}

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

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

◆ EvictUnusedCacheEntries()

void flutter::RasterCache::EvictUnusedCacheEntries ( )

Definition at line 214 of file raster_cache.cc.

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

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

158 {
159 RasterCacheKey key = RasterCacheKey(id, matrix);
160 auto entry = cache_.find(key);
161 if (entry != cache_.cend()) {
162 return entry->second.accesses_since_visible;
163 }
164 return -1;
165}
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258

◆ GetCachedEntriesCount()

size_t flutter::RasterCache::GetCachedEntriesCount ( ) const

Definition at line 245 of file raster_cache.cc.

245 {
246 return cache_.size();
247}

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

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

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

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

◆ HasEntry()

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

Definition at line 167 of file raster_cache.cc.

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

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

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

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

84 {
85 auto matrix = RasterCacheUtil::GetIntegralTransCTM(context.matrix);
86 SkRect dest_rect =
88
89 const SkImageInfo image_info = SkImageInfo::MakeN32Premul(
90 dest_rect.width(), dest_rect.height(), context.dst_color_space);
91
93 context.gr_context
95 image_info)
96 : SkSurfaces::Raster(image_info);
97
98 if (!surface) {
99 return nullptr;
100 }
101
102 DlSkCanvasAdapter canvas(surface->getCanvas());
103 canvas.Clear(DlColor::kTransparent());
104
105 canvas.Translate(-dest_rect.left(), -dest_rect.top());
106 canvas.Transform(matrix);
107 draw_function(&canvas);
108
109 if (checkerboard_images_) {
110 draw_checkerboard(&canvas, context.logical_rect);
111 }
112
113 auto image = DlImage::Make(surface->makeImageSnapshot());
114 return std::make_unique<RasterCacheResult>(
115 image, context.logical_rect, context.flow_type, std::move(rtree));
116}
static sk_sp< DlImage > Make(const SkImage *image)
Definition: dl_image.cc:11
VkSurfaceKHR surface
Definition: main.cc:49
sk_sp< const SkImage > image
Definition: SkRecords.h:269
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)
Definition: ToolUtils.cpp:174
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)

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

122 {
123 RasterCacheKey key = RasterCacheKey(id, raster_cache_context.matrix);
124 Entry& entry = cache_[key];
125 if (!entry.image) {
126 void (*func)(DlCanvas*, const SkRect& rect) = DrawCheckerboard;
127 entry.image = Rasterize(raster_cache_context, std::move(rtree),
128 render_function, func);
129 if (entry.image != nullptr) {
130 switch (id.type()) {
132 display_list_cached_this_frame_++;
133 break;
134 }
135 default:
136 break;
137 }
138 return true;
139 }
140 }
141 return entry.image != nullptr;
142}
GLenum type
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
Definition: raster_cache.cc:79
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
void DrawCheckerboard(DlCanvas *canvas, const SkRect &rect)
Definition: paint_utils.cc:32
flutter::DlCanvas DlCanvas

Friends And Related Function 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: