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