Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 3daf579

Browse files
committed
simplify specialization of [Mock]RasterCache and doc formatting
1 parent 44afe9b commit 3daf579

File tree

6 files changed

+200
-193
lines changed

6 files changed

+200
-193
lines changed

flow/layers/opacity_layer.h

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,40 @@ class OpacityLayer : public ContainerLayer {
3838
#endif // defined(OS_FUCHSIA)
3939

4040
private:
41-
// Returns the |ContainerLayer| used to hold all of the children of the
42-
// |OpacityLayer|. Typically these layers should only have a single child
43-
// according to the contract of the associated Flutter widget, but the
44-
// engine API cannot enforce that contract and must be prepared to support
45-
// the possibility of adding more than one child. In either case, the
46-
// child container will hold all children. Note that since the child
47-
// container is created fresh with each new |OpacityLayer|, it may not
48-
// be the best choice to cache to speed up rendering during an animation.
49-
//
50-
// @see |GetCacheableChild()|
41+
/**
42+
* @brief Returns the ContainerLayer used to hold all of the children
43+
* of the OpacityLayer.
44+
*
45+
* Typically these layers should only have a single child according to
46+
* the contract of the associated Flutter widget, but the engine API
47+
* cannot enforce that contract and must be prepared to support the
48+
* possibility of adding more than one child. In either case, the
49+
* child container will hold all children. Note that since the child
50+
* container is created fresh with each new OpacityLayer, it may not
51+
* be the best choice to cache to speed up rendering during an animation.
52+
*
53+
* @see GetCacheableChild()
54+
* @return the ContainerLayer child used to hold the children
55+
*/
5156
ContainerLayer* GetChildContainer() const;
5257

53-
// Returns the best choice for a |Layer| object that can represent all
54-
// children and remain stable if the |OpacityLayer| is reconstructed in
55-
// new scenes. The |OpacityLayer| needs to be recreated on frames where
56-
// its opacity values change, but it often contains the same child on
57-
// each such frame. Since the child container is created fresh when
58-
// each |OpacityLayer| is constructed, that |Layer| will not be stable
59-
// even if the same child is used. This method will determine if there
60-
// is a stable child and return that |Layer| so that caching will be
61-
// more successful.
58+
/**
59+
* @brief Returns the best choice for a Layer object that can be used
60+
* in RasterCache operations to cache the children of the OpacityLayer.
61+
*
62+
* The returned Layer must represent all children and try to remain stable
63+
* if the OpacityLayer is reconstructed in subsequent frames of the scene.
64+
* The OpacityLayer needs to be recreated on frames where its opacity
65+
* values change, but it often contains the same child on each such frame.
66+
* Since the child container is created fresh each time the OpacityLayer
67+
* is constructed, the return value from GetChildContainer will be different
68+
* on each such frame even if the same child is used. This method will
69+
* determine if there is a (single) stable child and return that Layer
70+
* so that caching will be more successful.
71+
*
72+
* @see GetChildContainer()
73+
* @return the best candidate Layer for caching the children
74+
*/
6275
Layer* GetCacheableChild() const;
6376

6477
SkAlpha alpha_;

flow/raster_cache.cc

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
namespace flutter {
1919

20-
SkRasterCacheResult::SkRasterCacheResult(sk_sp<SkImage> image,
21-
const SkRect& logical_rect)
20+
RasterCacheResult::RasterCacheResult(sk_sp<SkImage> image,
21+
const SkRect& logical_rect)
2222
: image_(std::move(image)), logical_rect_(logical_rect) {}
2323

24-
void SkRasterCacheResult::draw(SkCanvas& canvas, const SkPaint* paint) const {
24+
void RasterCacheResult::draw(SkCanvas& canvas, const SkPaint* paint) const {
2525
TRACE_EVENT0("flutter", "RasterCacheResult::draw");
2626
SkAutoCanvasRestore auto_restore(&canvas, true);
2727
SkIRect bounds =
@@ -35,16 +35,7 @@ void SkRasterCacheResult::draw(SkCanvas& canvas, const SkPaint* paint) const {
3535

3636
RasterCache::RasterCache(size_t access_threshold,
3737
size_t picture_cache_limit_per_frame)
38-
: delegate_(&SkRasterCacheImageDelegate::instance),
39-
access_threshold_(access_threshold),
40-
picture_cache_limit_per_frame_(picture_cache_limit_per_frame),
41-
checkerboard_images_(false) {}
42-
43-
RasterCache::RasterCache(RasterCacheImageDelegate* delegate,
44-
size_t access_threshold,
45-
size_t picture_cache_limit_per_frame)
46-
: delegate_(delegate),
47-
access_threshold_(access_threshold),
38+
: access_threshold_(access_threshold),
4839
picture_cache_limit_per_frame_(picture_cache_limit_per_frame),
4940
checkerboard_images_(false) {}
5041

@@ -127,13 +118,11 @@ static std::unique_ptr<RasterCacheResult> Rasterize(
127118
DrawCheckerboard(canvas, logical_rect);
128119
}
129120

130-
return std::make_unique<SkRasterCacheResult>(surface->makeImageSnapshot(),
131-
logical_rect);
121+
return std::make_unique<RasterCacheResult>(surface->makeImageSnapshot(),
122+
logical_rect);
132123
}
133124

134-
SkRasterCacheImageDelegate SkRasterCacheImageDelegate::instance;
135-
136-
std::unique_ptr<RasterCacheResult> SkRasterCacheImageDelegate::RasterizePicture(
125+
std::unique_ptr<RasterCacheResult> RasterCache::RasterizePicture(
137126
SkPicture* picture,
138127
GrContext* context,
139128
const SkMatrix& ctm,
@@ -144,7 +133,19 @@ std::unique_ptr<RasterCacheResult> SkRasterCacheImageDelegate::RasterizePicture(
144133
[=](SkCanvas* canvas) { canvas->drawPicture(picture); });
145134
}
146135

147-
std::unique_ptr<RasterCacheResult> SkRasterCacheImageDelegate::RasterizeLayer(
136+
void RasterCache::Prepare(PrerollContext* context,
137+
Layer* layer,
138+
const SkMatrix& ctm) {
139+
LayerRasterCacheKey cache_key(layer->unique_id(), ctm);
140+
Entry& entry = layer_cache_[cache_key];
141+
entry.access_count++;
142+
entry.used_this_frame = true;
143+
if (!entry.image) {
144+
entry.image = RasterizeLayer(context, layer, ctm, checkerboard_images_);
145+
}
146+
}
147+
148+
std::unique_ptr<RasterCacheResult> RasterCache::RasterizeLayer(
148149
PrerollContext* context,
149150
Layer* layer,
150151
const SkMatrix& ctm,
@@ -157,10 +158,10 @@ std::unique_ptr<RasterCacheResult> SkRasterCacheImageDelegate::RasterizeLayer(
157158
canvas_size.height());
158159
internal_nodes_canvas.addCanvas(canvas);
159160
Layer::PaintContext paintContext = {
160-
(SkCanvas*)&internal_nodes_canvas,
161-
canvas,
162-
context->gr_context,
163-
nullptr,
161+
(SkCanvas*)&internal_nodes_canvas, // internal_nodes_canvas
162+
canvas, // leaf_nodes_canvas
163+
context->gr_context, // gr_context
164+
nullptr, // view_embedder
164165
context->raster_time,
165166
context->ui_time,
166167
context->texture_registry,
@@ -174,19 +175,6 @@ std::unique_ptr<RasterCacheResult> SkRasterCacheImageDelegate::RasterizeLayer(
174175
});
175176
}
176177

177-
void RasterCache::Prepare(PrerollContext* context,
178-
Layer* layer,
179-
const SkMatrix& ctm) {
180-
LayerRasterCacheKey cache_key(layer->unique_id(), ctm);
181-
Entry& entry = layer_cache_[cache_key];
182-
entry.access_count++;
183-
entry.used_this_frame = true;
184-
if (!entry.image) {
185-
entry.image =
186-
delegate_->RasterizeLayer(context, layer, ctm, checkerboard_images_);
187-
}
188-
}
189-
190178
bool RasterCache::Prepare(GrContext* context,
191179
SkPicture* picture,
192180
const SkMatrix& transformation_matrix,
@@ -224,9 +212,8 @@ bool RasterCache::Prepare(GrContext* context,
224212
}
225213

226214
if (!entry.image) {
227-
entry.image =
228-
delegate_->RasterizePicture(picture, context, transformation_matrix,
229-
dst_color_space, checkerboard_images_);
215+
entry.image = RasterizePicture(picture, context, transformation_matrix,
216+
dst_color_space, checkerboard_images_);
230217
picture_cached_this_frame_++;
231218
}
232219
return true;
@@ -319,16 +306,14 @@ void RasterCache::TraceStatsToTimeline() const {
319306
for (const auto& item : layer_cache_) {
320307
layer_cache_count++;
321308
if (item.second.image) {
322-
const auto dimensions = item.second.image->image_dimensions();
323-
layer_cache_bytes += dimensions.width() * dimensions.height() * 4;
309+
layer_cache_bytes += item.second.image->image_bytes();
324310
}
325311
}
326312

327313
for (const auto& item : picture_cache_) {
328314
picture_cache_count++;
329315
if (item.second.image) {
330-
const auto dimensions = item.second.image->image_dimensions();
331-
picture_cache_bytes += dimensions.width() * dimensions.height() * 4;
316+
picture_cache_bytes += item.second.image->image_bytes();
332317
}
333318
}
334319

flow/raster_cache.h

Lines changed: 53 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -17,80 +17,31 @@
1717

1818
namespace flutter {
1919

20-
// |RasterCacheResult| contains the result of rendering a layer or a picture
21-
// at a given transform by an instance of |RasterCacheImageDelegate| and
22-
// provides the capability to render the result to a given |SkCanvas|.
2320
class RasterCacheResult {
2421
public:
25-
virtual ~RasterCacheResult() = default;
26-
27-
// Draw the rendered result to the |SkCanvas| using the properties of the
28-
// |SkPaint|.
29-
virtual void draw(SkCanvas& canvas, const SkPaint* paint = nullptr) const = 0;
30-
31-
// Return the size of the cached result to help manage the cache memory
32-
// consumption.
33-
virtual SkISize image_dimensions() const = 0;
34-
};
22+
RasterCacheResult(sk_sp<SkImage> image, const SkRect& logical_rect);
3523

36-
// |SkRasterCacheResult| is the typical implementation of |SkRasterCacheResult|
37-
// created from an |SkRasterCacheImageDelegate| and storing the result as
38-
// pixels in an |SkImage|.
39-
class SkRasterCacheResult : public RasterCacheResult {
40-
public:
41-
SkRasterCacheResult(sk_sp<SkImage> image, const SkRect& logical_rect);
24+
virtual ~RasterCacheResult() = default;
4225

43-
void draw(SkCanvas& canvas, const SkPaint* paint = nullptr) const override;
26+
virtual void draw(SkCanvas& canvas, const SkPaint* paint = nullptr) const;
4427

45-
SkISize image_dimensions() const override {
28+
virtual SkISize image_dimensions() const {
4629
return image_ ? image_->dimensions() : SkISize::Make(0, 0);
4730
};
4831

32+
virtual int64_t image_bytes() const {
33+
return image_ ? image_->dimensions().area() *
34+
image_->imageInfo().bytesPerPixel()
35+
: 0;
36+
};
37+
4938
private:
5039
sk_sp<SkImage> image_;
5140
SkRect logical_rect_;
5241
};
5342

5443
struct PrerollContext;
5544

56-
// The |RasterCacheImageDelegate| creates and retruns a cached result of
57-
// rendering the supplied |Layer| or |SkPicture|.
58-
class RasterCacheImageDelegate {
59-
public:
60-
virtual std::unique_ptr<RasterCacheResult> RasterizePicture(
61-
SkPicture* picture,
62-
GrContext* context,
63-
const SkMatrix& ctm,
64-
SkColorSpace* dst_color_space,
65-
bool checkerboard) const = 0;
66-
67-
virtual std::unique_ptr<RasterCacheResult> RasterizeLayer(
68-
PrerollContext* context,
69-
Layer* layer,
70-
const SkMatrix& ctm,
71-
bool checkerboard) const = 0;
72-
};
73-
74-
// The |SkRasterCacheImageDelegate| creates and returns rendered results
75-
// for |Layer| and |SkPicture| objects stored as |SkImage| objects.
76-
class SkRasterCacheImageDelegate : public RasterCacheImageDelegate {
77-
public:
78-
std::unique_ptr<RasterCacheResult> RasterizePicture(
79-
SkPicture* picture,
80-
GrContext* context,
81-
const SkMatrix& ctm,
82-
SkColorSpace* dst_color_space,
83-
bool checkerboard) const override;
84-
85-
std::unique_ptr<RasterCacheResult> RasterizeLayer(
86-
PrerollContext* context,
87-
Layer* layer,
88-
const SkMatrix& ctm,
89-
bool checkerboard) const override;
90-
91-
static SkRasterCacheImageDelegate instance;
92-
};
93-
9445
class RasterCache {
9546
public:
9647
// The default max number of picture raster caches to be generated per frame.
@@ -99,31 +50,53 @@ class RasterCache {
9950
// multiple frames.
10051
static constexpr int kDefaultPictureCacheLimitPerFrame = 3;
10152

102-
// Create a default |RasterCache| object that caches |Layer| and |SkPicture|
103-
// objects using a |SkRasterCacheImageDelegate|.
104-
//
105-
// @param access_threshold
106-
// the number of attempts to cache a given |SkPicture| before inserting
107-
// it into the cache. The defalt value of 3 will only cache a picture
108-
// on the 3rd time it appears in consecutive frames.
109-
// @param picture_cache_limit_per_frame
110-
// the maximum number of |SkPicture| objects to render into the cache
111-
// per frame. The default value of 3 will rasterize at most 3 pictures
112-
// in a given frame and leave the rest for caching in subsequent frames.
113-
//
114-
// @see |kDefaultPictureCacheLimitPerFrame|
11553
explicit RasterCache(
11654
size_t access_threshold = 3,
11755
size_t picture_cache_limit_per_frame = kDefaultPictureCacheLimitPerFrame);
11856

119-
// Create a |RasterCache| object that uses a specified implementation of
120-
// |RasterCacheImageDelegate|.
121-
//
122-
// @see |RasterCache(size_t, size_t)|
123-
explicit RasterCache(
124-
RasterCacheImageDelegate* delegate,
125-
size_t access_threshold = 3,
126-
size_t picture_cache_limit_per_frame = kDefaultPictureCacheLimitPerFrame);
57+
virtual ~RasterCache() = default;
58+
59+
/**
60+
* @brief Rasterize a picture object and produce a RasterCacheResult
61+
* to be stored in the cache.
62+
*
63+
* @param picture the SkPicture object to be cached.
64+
* @param context the GrContext used for rendering.
65+
* @param ctm the transformation matrix used for rendering.
66+
* @param dst_color_space the destination color space that the cached
67+
* rendering will be drawn into
68+
* @param checkerboard a flag indicating whether or not a checkerboard
69+
* pattern should be rendered into the cached image for debug
70+
* analysis
71+
* @return a RasterCacheResult that can draw the rendered picture into
72+
* the destination using a simple image blit
73+
*/
74+
virtual std::unique_ptr<RasterCacheResult> RasterizePicture(
75+
SkPicture* picture,
76+
GrContext* context,
77+
const SkMatrix& ctm,
78+
SkColorSpace* dst_color_space,
79+
bool checkerboard) const;
80+
81+
/**
82+
* @brief Rasterize an engine Layer and produce a RasterCacheResult
83+
* to be stored in the cache.
84+
*
85+
* @param context the PrerollContext containing important information
86+
* needed for rendering a layer.
87+
* @param layer the Layer object to be cached.
88+
* @param ctm the transformation matrix used for rendering.
89+
* @param checkerboard a flag indicating whether or not a checkerboard
90+
* pattern should be rendered into the cached image for debug
91+
* analysis
92+
* @return a RasterCacheResult that can draw the rendered layer into
93+
* the destination using a simple image blit
94+
*/
95+
virtual std::unique_ptr<RasterCacheResult> RasterizeLayer(
96+
PrerollContext* context,
97+
Layer* layer,
98+
const SkMatrix& ctm,
99+
bool checkerboard) const;
127100

128101
static SkIRect GetDeviceBounds(const SkRect& rect, const SkMatrix& ctm) {
129102
SkRect device_rect;
@@ -222,7 +195,6 @@ class RasterCache {
222195
}
223196
}
224197

225-
const RasterCacheImageDelegate* delegate_;
226198
const size_t access_threshold_;
227199
const size_t picture_cache_limit_per_frame_;
228200
size_t picture_cached_this_frame_ = 0;

0 commit comments

Comments
 (0)