1717
1818namespace 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|.
2320class 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
5443struct 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-
9445class 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