Flutter Engine
The Flutter Engine
layer_raster_cache_item.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#if !SLIMPELLER
6
7#include "flutter/flow/layers/layer_raster_cache_item.h"
8#include "flutter/flow/layers/container_layer.h"
9#include "flutter/flow/raster_cache_item.h"
10#include "flutter/flow/raster_cache_util.h"
12
13namespace flutter {
14
16 int layer_cached_threshold,
17 bool can_cache_children)
20 // The layer raster_cache_item's cache state default value is none.
22 layer_(layer),
23 layer_cached_threshold_(layer_cached_threshold),
24 can_cache_children_(can_cache_children) {}
25
27 const SkMatrix& matrix) {
28 cache_state_ = CacheState::kNone;
29 if (context->raster_cache && context->raster_cached_entries) {
30 context->raster_cached_entries->push_back(this);
31 child_items_ = context->raster_cached_entries->size();
32 matrix_ = matrix;
33 }
34}
35
36std::unique_ptr<LayerRasterCacheItem> LayerRasterCacheItem::Make(
37 Layer* layer,
38 int layer_cache_threshold,
39 bool can_cache_children) {
40 return std::make_unique<LayerRasterCacheItem>(layer, layer_cache_threshold,
41 can_cache_children);
42}
43
45 const SkMatrix& matrix) {
46 if (!context->raster_cache || !context->raster_cached_entries) {
47 return;
48 }
49 // We've marked the cache entry that we would like to cache so it stays
50 // alive, but if the following conditions apply then we need to set our
51 // state back to kDoNotCache so that we don't populate the entry later.
52 if (context->has_platform_view || context->has_texture_layer ||
53 context->state_stack.content_culled(layer_->paint_bounds())) {
54 return;
55 }
56 child_items_ = context->raster_cached_entries->size() - child_items_;
57 if (num_cache_attempts_ >= layer_cached_threshold_) {
58 // the layer can be cached
59 cache_state_ = CacheState::kCurrent;
60 context->raster_cache->MarkSeen(key_id_, matrix_, true);
61 } else {
62 num_cache_attempts_++;
63 // access current layer
64 if (can_cache_children_) {
65 if (!layer_children_id_.has_value()) {
66 auto ids = RasterCacheKeyID::LayerChildrenIds(layer_);
67 if (!ids.has_value()) {
68 return;
69 }
70 layer_children_id_.emplace(std::move(ids.value()),
72 }
73 cache_state_ = CacheState::kChildren;
74 context->raster_cache->MarkSeen(layer_children_id_.value(), matrix_,
75 true);
76 }
77 }
78}
79
80std::optional<RasterCacheKeyID> LayerRasterCacheItem::GetId() const {
81 switch (cache_state_) {
82 case kCurrent:
83 return key_id_;
84 case kChildren:
85 return layer_children_id_;
86 default:
87 return {};
88 }
89}
90
92 switch (cache_state_) {
93 case CacheState::kCurrent:
94 return &(layer_->paint_bounds());
95 case CacheState::kChildren:
96 FML_DCHECK(layer_->as_container_layer());
97 return &(layer_->as_container_layer()->child_paint_bounds());
98 default:
99 FML_DCHECK(cache_state_ != CacheState::kNone);
100 return nullptr;
101 }
102}
103
105 Layer* layer,
106 const PaintContext& paint_context,
107 DlCanvas* canvas) {
109 LayerStateStack state_stack;
110 state_stack.set_delegate(canvas);
111 PaintContext context = {
112 // clang-format off
113 .state_stack = state_stack,
114 .canvas = canvas,
115 .gr_context = paint_context.gr_context,
116 .dst_color_space = paint_context.dst_color_space,
117 .view_embedder = paint_context.view_embedder,
118 .raster_time = paint_context.raster_time,
119 .ui_time = paint_context.ui_time,
120 .texture_registry = paint_context.texture_registry,
121 .raster_cache = paint_context.raster_cache,
122 // clang-format on
123 };
124
125 switch (cache_state) {
126 case RasterCacheItem::CacheState::kCurrent:
127 FML_DCHECK(layer->needs_painting(context));
128 layer->Paint(context);
129 break;
130 case RasterCacheItem::CacheState::kChildren:
131 layer->PaintChildren(context);
132 break;
135 return false;
136 }
137 return true;
138}
139
140static const auto* flow_type = "RasterCacheFlow::Layer";
141
143 bool parent_cached) const {
144 auto maybe_id = GetId();
145 if (!maybe_id.has_value() || !context.raster_cache || parent_cached) {
146 return false;
147 }
148 if (cache_state_ != kNone) {
149 if (const SkRect* paint_bounds = GetPaintBoundsFromLayer()) {
150 RasterCache::Context r_context = {
151 // clang-format off
152 .gr_context = context.gr_context,
153 .dst_color_space = context.dst_color_space,
154 .matrix = matrix_,
155 .logical_rect = *paint_bounds,
156 .flow_type = flow_type,
157 // clang-format on
158 };
159 auto id = maybe_id.value();
160 return context.raster_cache->UpdateCacheEntry(
161 id, r_context,
162 [ctx = context, cache_state = cache_state_,
163 layer = layer_](DlCanvas* canvas) {
164 Rasterize(cache_state, layer, ctx, canvas);
165 });
166 }
167 }
168 return false;
169}
170
172 const DlPaint* paint) const {
173 return Draw(context, context.canvas, paint);
174}
175
177 DlCanvas* canvas,
178 const DlPaint* paint) const {
179 if (!context.raster_cache || !canvas) {
180 return false;
181 }
182 switch (cache_state_) {
184 return false;
186 return context.raster_cache->Draw(key_id_, *canvas, paint,
188 }
190 if (!layer_children_id_.has_value()) {
191 return false;
192 }
193 return context.raster_cache->Draw(layer_children_id_.value(), *canvas,
194 paint,
196 }
197 }
198}
199
200} // namespace flutter
201
202#endif // !SLIMPELLER
Developer-facing API for rendering anything within the engine.
Definition: dl_canvas.h:38
static std::unique_ptr< LayerRasterCacheItem > Make(Layer *, int layer_cache_threshold, bool can_cache_children=false)
Create a LayerRasterCacheItem, connect a layer and manage the Layer's raster cache.
std::optional< RasterCacheKeyID > GetId() const override
void PrerollSetup(PrerollContext *context, const SkMatrix &matrix) override
LayerRasterCacheItem(Layer *layer, int layer_cached_threshold=1, bool can_cache_children=false)
bool Draw(const PaintContext &context, const DlPaint *paint) const override
bool TryToPrepareRasterCache(const PaintContext &context, bool parent_cached=false) const override
const SkRect * GetPaintBoundsFromLayer() const
void PrerollFinalize(PrerollContext *context, const SkMatrix &matrix) override
void set_delegate(DlCanvas *canvas)
bool content_culled(const SkRect &content_bounds) const
virtual void PaintChildren(PaintContext &context) const
Definition: layer.h:199
bool needs_painting(PaintContext &context) const
Definition: layer.h:232
virtual void Paint(PaintContext &context) const =0
static std::optional< std::vector< RasterCacheKeyID > > LayerChildrenIds(const Layer *layer)
static void Draw(SkCanvas *canvas, const SkRect &rect)
const Paint & paint
Definition: color_source.cc:38
#define FML_DCHECK(condition)
Definition: logging.h:103
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
@ kNone
Definition: layer.h:53
static const auto * flow_type
bool Rasterize(RasterCacheItem::CacheState cache_state, Layer *layer, const PaintContext &paint_context, DlCanvas *canvas)
ExternalViewEmbedder * view_embedder
Definition: layer.h:111
bool rendering_above_platform_view
Definition: layer.h:107
sk_sp< SkColorSpace > dst_color_space
Definition: layer.h:110
const Stopwatch & raster_time
Definition: layer.h:112
const Stopwatch & ui_time
Definition: layer.h:113
std::shared_ptr< TextureRegistry > texture_registry
Definition: layer.h:114
DlCanvas * canvas
Definition: layer.h:102
GrDirectContext * gr_context
Definition: layer.h:109
LayerStateStack & state_stack
Definition: layer.h:101
std::vector< RasterCacheItem * > * raster_cached_entries
Definition: layer.h:85
LayerStateStack & state_stack
Definition: layer.h:59
GrDirectContext * gr_context
Definition: raster_cache.h:121