22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5- #include " flutter/flow/layers/layer.h"
65#include " flutter/testing/mock_raster_cache.h"
6+ #include " flutter/flow/layers/layer.h"
77
88namespace flutter {
99namespace testing {
1010
11- // RasterCacheResult::RasterCacheResult(sk_sp<SkImage> image,
12- // const SkRect& logical_rect)
13- // : image_(std::move(image)), logical_rect_(logical_rect) {}
14-
15- // void RasterCacheResult::draw(SkCanvas& canvas, const SkPaint* paint) const {
16- // TRACE_EVENT0("flutter", "RasterCacheResult::draw");
17- // SkAutoCanvasRestore auto_restore(&canvas, true);
18- // SkIRect bounds =
19- // RasterCache::GetDeviceBounds(logical_rect_, canvas.getTotalMatrix());
20- // FML_DCHECK(
21- // std::abs(bounds.size().width() - image_->dimensions().width()) <= 1 &&
22- // std::abs(bounds.size().height() - image_->dimensions().height()) <= 1);
23- // canvas.resetMatrix();
24- // canvas.drawImage(image_, bounds.fLeft, bounds.fTop, paint);
25- // }
26-
27- MockRasterCache::MockRasterCache ()
28- : RasterCache::RasterCache(1 , 1000000 ) { }
11+ MockRasterCache::MockRasterCache () : RasterCache::RasterCache(1 , 1000000 ) {}
2912
3013static bool CanRasterizePicture (SkPicture* picture) {
3114 if (picture == nullptr ) {
@@ -73,55 +56,22 @@ static bool IsPictureWorthRasterizing(SkPicture* picture,
7356 return picture->approximateOpCount () > 5 ;
7457}
7558
76- // / @note Procedure doesn't copy all closures.
77- static RasterCacheResult Rasterize (
78- const SkMatrix& ctm,
79- SkColorSpace* dst_color_space,
80- const SkRect& logical_rect) {
81- TRACE_EVENT0 (" flutter" , " RasterCachePopulate" );
82- SkIRect cache_rect = RasterCache::GetDeviceBounds (logical_rect, ctm);
83-
84- const SkImageInfo image_info = SkImageInfo::MakeN32Premul (
85- cache_rect.width (), cache_rect.height (), sk_ref_sp (dst_color_space));
86-
87- sk_sp<SkData> data = SkData::MakeUninitialized (image_info.computeMinByteSize ());
88- sk_sp<SkImage> image = SkImage::MakeRasterData (image_info, data, image_info.minRowBytes ());
89-
90- return {image, logical_rect};
91- }
92-
93- static RasterCacheResult RasterizePicture (SkPicture* picture,
94- const SkMatrix& ctm,
95- SkColorSpace* dst_color_space) {
96- return Rasterize (ctm, dst_color_space, picture->cullRect ());
97- }
98-
9959void MockRasterCache::Prepare (PrerollContext* context,
10060 Layer* layer,
10161 const SkMatrix& ctm) {
10262 LayerRasterCacheKey cache_key (layer->unique_id (), ctm);
103- Entry & entry = layer_cache_[cache_key];
63+ MockEntry & entry = layer_cache_[cache_key];
10464 entry.access_count ++;
10565 entry.used_this_frame = true ;
106- if (!entry.image .is_valid ()) {
107- entry.image = Rasterize (
108- ctm, context->dst_color_space ,
109- layer->paint_bounds ());
110- }
111- }
112-
113- bool MockRasterCache::WasPrepared (Layer* layer, const SkMatrix& ctm) {
114- LayerRasterCacheKey cache_key (layer->unique_id (), ctm);
115- // return layer_cache_.contains(cache_key); - Requires STD_VER > 17, C++20
116- return layer_cache_.find (cache_key) != layer_cache_.end ();
66+ entry.rasterized = true ;
11767}
11868
11969bool MockRasterCache::Prepare (GrContext* context,
120- SkPicture* picture,
121- const SkMatrix& transformation_matrix,
122- SkColorSpace* dst_color_space,
123- bool is_complex,
124- bool will_change) {
70+ SkPicture* picture,
71+ const SkMatrix& transformation_matrix,
72+ SkColorSpace* dst_color_space,
73+ bool is_complex,
74+ bool will_change) {
12575 if (!IsPictureWorthRasterizing (picture, will_change, is_complex)) {
12676 // We only deal with pictures that are worthy of rasterization.
12777 return false ;
@@ -139,42 +89,40 @@ bool MockRasterCache::Prepare(GrContext* context,
13989 PictureRasterCacheKey cache_key (picture->uniqueID (), transformation_matrix);
14090
14191 // Creates an entry, if not present prior.
142- Entry& entry = picture_cache_[cache_key];
92+ MockEntry& entry = picture_cache_[cache_key];
93+ entry.rasterized = true ;
14394
144- if (!entry.image .is_valid ()) {
145- entry.image = RasterizePicture (picture, transformation_matrix,
146- dst_color_space);
147- }
14895 return true ;
14996}
15097
151- RasterCacheResult MockRasterCache::Get (const SkPicture& picture,
152- const SkMatrix& ctm) const {
153- PictureRasterCacheKey cache_key (picture.uniqueID (), ctm);
98+ bool MockRasterCache::Draw (const SkPicture& picture, SkCanvas& canvas) const {
99+ PictureRasterCacheKey cache_key (picture.uniqueID (), canvas.getTotalMatrix ());
154100 auto it = picture_cache_.find (cache_key);
155101 if (it == picture_cache_.end ()) {
156- return RasterCacheResult () ;
102+ return false ;
157103 }
158104
159- Entry & entry = it->second ;
105+ MockEntry & entry = it->second ;
160106 entry.access_count ++;
161107 entry.used_this_frame = true ;
162108
163- return entry.image ;
109+ return entry.rasterized ;
164110}
165111
166- RasterCacheResult MockRasterCache::Get (Layer* layer, const SkMatrix& ctm) const {
167- LayerRasterCacheKey cache_key (layer->unique_id (), ctm);
112+ bool MockRasterCache::Draw (const Layer* layer,
113+ SkCanvas& canvas,
114+ SkPaint* paint) const {
115+ LayerRasterCacheKey cache_key (layer->unique_id (), canvas.getTotalMatrix ());
168116 auto it = layer_cache_.find (cache_key);
169117 if (it == layer_cache_.end ()) {
170- return RasterCacheResult () ;
118+ return false ;
171119 }
172120
173- Entry & entry = it->second ;
121+ MockEntry & entry = it->second ;
174122 entry.access_count ++;
175123 entry.used_this_frame = true ;
176124
177- return entry.image ;
125+ return entry.rasterized ;
178126}
179127
180128void MockRasterCache::SweepAfterFrame () {
@@ -191,8 +139,7 @@ size_t MockRasterCache::GetCachedEntriesCount() const {
191139 return layer_cache_.size () + picture_cache_.size ();
192140}
193141
194- void MockRasterCache::SetCheckboardCacheImages (bool checkerboard) {
195- }
142+ void MockRasterCache::SetCheckboardCacheImages (bool checkerboard) {}
196143
197144} // namespace testing
198145} // namespace flutter
0 commit comments