33// found in the LICENSE file.
44
55#include " flutter/flow/layers/layer_raster_cache_item.h"
6+ #include < cstddef>
67#include " flutter/flow/layers/container_layer.h"
78#include " flutter/flow/raster_cache_item.h"
89
@@ -42,18 +43,11 @@ void LayerRasterCacheItem::PrerollFinalize(PrerollContext* context,
4243 if (!context->raster_cache || !context->raster_cached_entries ) {
4344 return ;
4445 }
45- // We've marked the cache entry that we would like to cache so it stays
46- // alive, but if the following conditions apply then we need to set our
47- // state back to kDoNotCache so that we don't populate the entry later.
48- if (context->has_platform_view || context->has_texture_layer ||
49- !SkRect::Intersects (context->cull_rect , layer_->paint_bounds ())) {
50- return ;
51- }
5246 child_items_ = context->raster_cached_entries ->size () - child_items_;
47+ // Determine cache current layer or cache children.
5348 if (num_cache_attempts_ >= layer_cached_threshold_) {
5449 // the layer can be cached
5550 cache_state_ = CacheState::kCurrent ;
56- context->raster_cache ->MarkSeen (key_id_, matrix_);
5751 } else {
5852 num_cache_attempts_++;
5953 // access current layer
@@ -67,9 +61,25 @@ void LayerRasterCacheItem::PrerollFinalize(PrerollContext* context,
6761 RasterCacheKeyType::kLayerChildren );
6862 }
6963 cache_state_ = CacheState::kChildren ;
70- context->raster_cache ->MarkSeen (layer_children_id_.value (), matrix_);
7164 }
7265 }
66+ // We've marked the cache entry that we would like to cache so it stays
67+ // alive, but if the following conditions apply then we need to set our
68+ // state back to kDoNotCache so that we don't populate the entry later.
69+ if (context->has_platform_view || context->has_texture_layer ||
70+ !SkRect::Intersects (context->cull_rect , layer_->paint_bounds ())) {
71+ if (cache_state_ != CacheState::kNone ) {
72+ // Only touch the entry, do not to cache the layer or children.
73+ context->raster_cache ->Touch (GetId ().value (), matrix_);
74+ cache_state_ = CacheState::kNone ;
75+ }
76+ return ;
77+ }
78+ // The layer can be cache, so we should create a entry.
79+ if (cache_state_ != CacheState::kNone ) {
80+ // Create Cache entry
81+ context->raster_cache ->MarkSeen (GetId ().value (), matrix_);
82+ }
7383}
7484
7585std::optional<RasterCacheKeyID> LayerRasterCacheItem::GetId () const {
@@ -111,19 +121,20 @@ bool Rasterize(RasterCacheItem::CacheState cache_state,
111121 .leaf_nodes_canvas = canvas,
112122 .gr_context = paint_context.gr_context ,
113123 .dst_color_space = paint_context.dst_color_space ,
114- .view_embedder = paint_context. view_embedder ,
124+ .view_embedder = nullptr ,
115125 .raster_time = paint_context.raster_time ,
116126 .ui_time = paint_context.ui_time ,
117127 .texture_registry = paint_context.texture_registry ,
118- .raster_cache = paint_context.raster_cache ,
128+ .raster_cache = layer-> subtree_has_platform_view () ? nullptr : paint_context.raster_cache ,
119129 .checkerboard_offscreen_layers = paint_context.checkerboard_offscreen_layers ,
120130 .frame_device_pixel_ratio = paint_context.frame_device_pixel_ratio ,
121131 // clang-format on
122132 };
123-
124133 switch (cache_state) {
125134 case RasterCacheItem::CacheState::kCurrent :
126- FML_DCHECK (layer->needs_painting (context));
135+ if (!layer->needs_painting (context)) {
136+ return false ;
137+ }
127138 layer->Paint (context);
128139 break ;
129140 case RasterCacheItem::CacheState::kChildren :
@@ -138,9 +149,9 @@ bool Rasterize(RasterCacheItem::CacheState cache_state,
138149
139150static const auto * flow_type = " RasterCacheFlow::Layer" ;
140151
141- bool LayerRasterCacheItem::TryToPrepareRasterCache (const PaintContext& context,
142- bool parent_cached ) const {
143- if (!context.raster_cache || parent_cached ) {
152+ bool LayerRasterCacheItem::TryToPrepareRasterCache (
153+ const PaintContext& context ) const {
154+ if (!context.raster_cache ) {
144155 return false ;
145156 }
146157 if (cache_state_ != kNone ) {
@@ -159,7 +170,7 @@ bool LayerRasterCacheItem::TryToPrepareRasterCache(const PaintContext& context,
159170 GetId ().value (), r_context,
160171 [ctx = context, cache_state = cache_state_,
161172 layer = layer_](SkCanvas* canvas) {
162- Rasterize (cache_state, layer, ctx, canvas);
173+ return Rasterize (cache_state, layer, ctx, canvas);
163174 });
164175 }
165176 }
@@ -171,23 +182,17 @@ bool LayerRasterCacheItem::Draw(const PaintContext& context,
171182 return Draw (context, context.leaf_nodes_canvas , paint);
172183}
173184
185+ bool LayerRasterCacheItem::Touch (const PaintContext* context) const {
186+ return context->raster_cache ->Touch (GetId ().value (), matrix_);
187+ }
188+
174189bool LayerRasterCacheItem::Draw (const PaintContext& context,
175190 SkCanvas* canvas,
176191 const SkPaint* paint) const {
177- if (!context.raster_cache || !canvas) {
192+ if (!context.raster_cache || !canvas || cache_state_ == CacheState:: kNone ) {
178193 return false ;
179194 }
180- switch (cache_state_) {
181- case RasterCacheItem::kNone :
182- return false ;
183- case RasterCacheItem::kCurrent : {
184- return context.raster_cache ->Draw (key_id_, *canvas, paint);
185- }
186- case RasterCacheItem::kChildren : {
187- return context.raster_cache ->Draw (layer_children_id_.value (), *canvas,
188- paint);
189- }
190- }
195+ return context.raster_cache ->Draw (GetId ().value (), *canvas, paint);
191196}
192197
193198} // namespace flutter
0 commit comments