Flutter Engine
The Flutter Engine
raster_cache.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_FLOW_RASTER_CACHE_H_
6#define FLUTTER_FLOW_RASTER_CACHE_H_
7
8#if !SLIMPELLER
9
10#include <memory>
11#include <unordered_map>
12
13#include "flutter/display_list/dl_canvas.h"
14#include "flutter/flow/raster_cache_key.h"
15#include "flutter/flow/raster_cache_util.h"
16#include "flutter/fml/macros.h"
17#include "flutter/fml/memory/weak_ptr.h"
18#include "flutter/fml/trace_event.h"
21
22class GrDirectContext;
23class SkColorSpace;
24
25namespace flutter {
26
28
30 public:
32 const SkRect& logical_rect,
33 const char* type,
34 sk_sp<const DlRTree> rtree = nullptr);
35
36 virtual ~RasterCacheResult() = default;
37
38 virtual void draw(DlCanvas& canvas,
39 const DlPaint* paint,
40 bool preserve_rtree) const;
41
42 virtual SkISize image_dimensions() const {
43 return image_ ? image_->dimensions() : SkISize::Make(0, 0);
44 };
45
46 virtual int64_t image_bytes() const {
47 return image_ ? image_->GetApproximateByteSize() : 0;
48 };
49
50 private:
51 sk_sp<DlImage> image_;
52 SkRect logical_rect_;
55};
56
57class Layer;
58class RasterCacheItem;
59struct PrerollContext;
60struct PaintContext;
61
63 /**
64 * The number of cache entries with images evicted in this frame.
65 */
66 size_t eviction_count = 0;
67
68 /**
69 * The size of all of the images evicted in this frame.
70 */
71 size_t eviction_bytes = 0;
72
73 /**
74 * The number of cache entries with images used in this frame.
75 */
76 size_t in_use_count = 0;
77
78 /**
79 * The size of all of the images used in this frame.
80 */
81 size_t in_use_bytes = 0;
82
83 /**
84 * The total cache entries that had images during this frame.
85 */
86 size_t total_count() const { return in_use_count; }
87
88 /**
89 * The size of all of the cached images during this frame.
90 */
91 size_t total_bytes() const { return in_use_bytes; }
92};
93
94/**
95 * RasterCache is used to cache rasterized layers or display lists to improve
96 * performance.
97 *
98 * Life cycle of RasterCache methods:
99 * - Preroll stage
100 * - LayerTree::Preroll - for each Layer in the tree:
101 * - RasterCacheItem::PrerollSetup
102 * At the start of each layer's preroll, add cache items to
103 * `PrerollContext::raster_cached_entries`.
104 * - RasterCacheItem::PrerollFinalize
105 * At the end of each layer's preroll, may mark cache entries as
106 * encountered by the current frame.
107 * - Paint stage
108 * - RasterCache::EvictUnusedCacheEntries
109 * Evict cached images that are no longer used.
110 * - LayerTree::TryToPrepareRasterCache
111 * Create cache image for each cache entry if it does not exist.
112 * - LayerTree::Paint - for each layer in the tree:
113 * If layers or display lists are cached as cached images, the method
114 * `RasterCache::Draw` will be used to draw those cache images.
115 * - RasterCache::EndFrame:
116 * Computes used counts and memory then reports cache metrics.
117 */
119 public:
120 struct Context {
125 const char* flow_type;
126 };
127 struct CacheInfo {
129 const bool has_image;
130 };
131
132 std::unique_ptr<RasterCacheResult> Rasterize(
133 const RasterCache::Context& context,
135 const std::function<void(DlCanvas*)>& draw_function,
136 const std::function<void(DlCanvas*, const SkRect& rect)>&
137 draw_checkerboard) const;
138
139 explicit RasterCache(
140 size_t access_threshold = 3,
141 size_t picture_and_display_list_cache_limit_per_frame =
143
144 virtual ~RasterCache() = default;
145
146 // Draws this item if it should be rendered from the cache and returns
147 // true iff it was successfully drawn. Typically this should only fail
148 // if the item was disabled due to conditions discovered during |Preroll|
149 // or if the attempt to populate the entry failed due to bounds overflow
150 // conditions.
151 // If |preserve_rtree| is true, the raster cache will preserve the original
152 // RTree of cached content by blitting individual rectangles from the cached
153 // image to the canvas according to the original layer R-Tree (if present).
154 // This is to ensure that the target surface R-Tree will not be clobbered with
155 // one large blit as it can affect platform view overlays and hit testing.
156 bool Draw(const RasterCacheKeyID& id,
157 DlCanvas& canvas,
158 const DlPaint* paint,
159 bool preserve_rtree = false) const;
160
161 bool HasEntry(const RasterCacheKeyID& id, const SkMatrix&) const;
162
163 void BeginFrame();
164
166
167 void EndFrame();
168
169 void Clear();
170
171 const RasterCacheMetrics& picture_metrics() const { return picture_metrics_; }
172 const RasterCacheMetrics& layer_metrics() const { return layer_metrics_; }
173
174 size_t GetCachedEntriesCount() const;
175
176 /**
177 * Return the number of map entries in the layer cache regardless of whether
178 * the entries have been populated with an image.
179 */
180 size_t GetLayerCachedEntriesCount() const;
181
182 /**
183 * Return the number of map entries in the picture (DisplayList) cache
184 * regardless of whether the entries have been populated with an image.
185 */
186 size_t GetPictureCachedEntriesCount() const;
187
188 /**
189 * @brief Estimate how much memory is used by picture raster cache entries in
190 * bytes.
191 *
192 * Only SkImage's memory usage is counted as other objects are often much
193 * smaller compared to SkImage. SkImageInfo::computeMinByteSize is used to
194 * estimate the SkImage memory usage.
195 */
196 size_t EstimatePictureCacheByteSize() const;
197
198 /**
199 * @brief Estimate how much memory is used by layer raster cache entries in
200 * bytes.
201 *
202 * Only SkImage's memory usage is counted as other objects are often much
203 * smaller compared to SkImage. SkImageInfo::computeMinByteSize is used to
204 * estimate the SkImage memory usage.
205 */
206 size_t EstimateLayerCacheByteSize() const;
207
208 /**
209 * @brief Return the number of frames that a picture must be prepared
210 * before it will be cached. If the number is 0, then no picture will
211 * ever be cached.
212 *
213 * If the number is one, then it must be prepared and drawn on 1 frame
214 * and it will then be cached on the next frame if it is prepared.
215 */
216 size_t access_threshold() const { return access_threshold_; }
217
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 }
223
224 /**
225 * @brief The entry whose RasterCacheKey is generated by RasterCacheKeyID
226 * and matrix is marked as encountered by the current frame. The entry
227 * will be created if it does not exist. Optionally the entry will be marked
228 * as visible in the current frame if the caller determines that it
229 * intersects the cull rect. The access_count of the entry will be
230 * increased if it is visible, or if it was ever visible.
231 * @return the number of times the entry has been hit since it was created.
232 * For a new entry that will be 1 if it is visible, or zero if non-visible.
233 */
234 CacheInfo MarkSeen(const RasterCacheKeyID& id,
235 const SkMatrix& matrix,
236 bool visible) const;
237
238 /**
239 * Returns the access count (i.e. accesses_since_visible) for the given
240 * entry in the cache, or -1 if no such entry exists.
241 */
242 int GetAccessCount(const RasterCacheKeyID& id, const SkMatrix& matrix) const;
243
244 bool UpdateCacheEntry(const RasterCacheKeyID& id,
245 const Context& raster_cache_context,
246 const std::function<void(DlCanvas*)>& render_function,
247 sk_sp<const DlRTree> rtree = nullptr) const;
248
249 private:
250 struct Entry {
251 bool encountered_this_frame = false;
252 bool visible_this_frame = false;
253 size_t accesses_since_visible = 0;
254 std::unique_ptr<RasterCacheResult> image;
255 };
256
257 void UpdateMetrics();
258
259 RasterCacheMetrics& GetMetricsForKind(RasterCacheKeyKind kind);
260
261 const size_t access_threshold_;
262 const size_t display_list_cache_limit_per_frame_;
263 mutable size_t display_list_cached_this_frame_ = 0;
264 RasterCacheMetrics layer_metrics_;
265 RasterCacheMetrics picture_metrics_;
266 mutable RasterCacheKey::Map<Entry> cache_;
267 bool checkerboard_images_ = false;
268
269 void TraceStatsToTimeline() const;
270
271 friend class RasterCacheItem;
273
275};
276
277} // namespace flutter
278
279#else // !SLIMPELLER
280
281class RasterCache;
282
283#endif // !SLIMPELLER
284
285#endif // FLUTTER_FLOW_RASTER_CACHE_H_
GLenum type
Developer-facing API for rendering anything within the engine.
Definition: dl_canvas.h:38
RasterCacheResult(sk_sp< DlImage > image, const SkRect &logical_rect, const char *type, sk_sp< const DlRTree > rtree=nullptr)
Definition: raster_cache.cc:29
virtual SkISize image_dimensions() const
Definition: raster_cache.h:42
virtual void draw(DlCanvas &canvas, const DlPaint *paint, bool preserve_rtree) const
Definition: raster_cache.cc:38
virtual int64_t image_bytes() const
Definition: raster_cache.h:46
virtual ~RasterCacheResult()=default
virtual ~RasterCache()=default
bool Draw(const RasterCacheKeyID &id, DlCanvas &canvas, const DlPaint *paint, bool preserve_rtree=false) 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 b...
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
const RasterCacheMetrics & picture_metrics() const
Definition: raster_cache.h:171
size_t EstimatePictureCacheByteSize() const
Estimate how much memory is used by picture raster cache entries in bytes.
const RasterCacheMetrics & layer_metrics() const
Definition: raster_cache.h:172
bool HasEntry(const RasterCacheKeyID &id, const SkMatrix &) 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
bool GenerateNewCacheInThisFrame() const
Definition: raster_cache.h:218
RasterCache(size_t access_threshold=3, size_t picture_and_display_list_cache_limit_per_frame=RasterCacheUtil::kDefaultPictureAndDisplayListCacheLimitPerFrame)
Definition: raster_cache.cc:73
int GetAccessCount(const RasterCacheKeyID &id, const SkMatrix &matrix) const
size_t GetCachedEntriesCount() const
size_t GetPictureCachedEntriesCount() const
size_t EstimateLayerCacheByteSize() const
Estimate how much memory is used by layer raster cache entries in bytes.
size_t GetLayerCachedEntriesCount() const
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
const Paint & paint
Definition: color_source.cc:38
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
Dart_NativeFunction function
Definition: fuchsia.cc:51
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
void draw_checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)
Definition: ToolUtils.cpp:174
RasterCacheLayerStrategy
Definition: raster_cache.h:27
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
size_t total_count() const
Definition: raster_cache.h:86
size_t total_bytes() const
Definition: raster_cache.h:91
static constexpr int kDefaultPictureAndDisplayListCacheLimitPerFrame
GrDirectContext * gr_context
Definition: raster_cache.h:121
const sk_sp< SkColorSpace > dst_color_space
Definition: raster_cache.h:122