Flutter Engine
 
Loading...
Searching...
No Matches
display_list_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
8
9#include <optional>
10#include <utility>
11
19#include "flutter/third_party/skia/include/core/SkColorSpace.h"
20#include "flutter/third_party/skia/include/gpu/ganesh/GrDirectContext.h"
21
22namespace flutter {
23
25 const DisplayList* display_list,
26 bool will_change,
27 bool is_complex,
28 DisplayListComplexityCalculator* complexity_calculator) {
29 if (will_change) {
30 // If the display list is going to change in the future, there is no point
31 // in doing to extra work to rasterize.
32 return false;
33 }
34
35 if (display_list == nullptr ||
37 // No point in deciding whether the display list is worth rasterizing if it
38 // cannot be rasterized at all.
39 return false;
40 }
41
42 if (is_complex) {
43 // The caller seems to have extra information about the display list and
44 // thinks the display list is always worth rasterizing.
45 return true;
46 }
47
48 unsigned int complexity_score = complexity_calculator->Compute(display_list);
49 return complexity_calculator->ShouldBeCached(complexity_score);
50}
51
53 const sk_sp<DisplayList>& display_list,
54 const SkPoint& offset,
55 bool is_complex,
56 bool will_change)
57 : RasterCacheItem(RasterCacheKeyID(display_list->unique_id(),
59 CacheState::kCurrent),
60 display_list_(display_list),
61 offset_(offset),
62 is_complex_(is_complex),
63 will_change_(will_change) {}
64
65std::unique_ptr<DisplayListRasterCacheItem> DisplayListRasterCacheItem::Make(
66 const sk_sp<DisplayList>& display_list,
67 const SkPoint& offset,
68 bool is_complex,
69 bool will_change) {
70 return std::make_unique<DisplayListRasterCacheItem>(display_list, offset,
71 is_complex, will_change);
72}
73
75 const DlMatrix& matrix) {
77 DisplayListComplexityCalculator* complexity_calculator =
79 context->gr_context->backend())
81
82 if (!IsDisplayListWorthRasterizing(display_list(), will_change_, is_complex_,
83 complexity_calculator)) {
84 // We only deal with display lists that are worthy of rasterization.
85 return;
86 }
87
88 transformation_matrix_ = ToSkMatrix(matrix);
89 transformation_matrix_.preTranslate(offset_.x(), offset_.y());
90
91 if (!transformation_matrix_.invert(nullptr)) {
92 // The matrix was singular. No point in going further.
93 return;
94 }
95
96 if (context->raster_cached_entries && context->raster_cache) {
97 context->raster_cached_entries->push_back(this);
99 }
100 return;
101}
102
104 const DlMatrix& matrix) {
105 if (cache_state_ == CacheState::kNone || !context->raster_cache ||
106 !context->raster_cached_entries) {
107 return;
108 }
109 auto* raster_cache = context->raster_cache;
110 DlRect bounds = display_list_->GetBounds().Shift(offset_.x(), offset_.y());
111 bool visible = !context->state_stack.content_culled(bounds);
112 RasterCache::CacheInfo cache_info =
113 raster_cache->MarkSeen(key_id_, ToSkMatrix(matrix), visible);
114 if (!visible ||
115 cache_info.accesses_since_visible <= raster_cache->access_threshold()) {
117 } else {
118 if (cache_info.has_image) {
119 context->renderable_state_flags |=
121 }
123 }
124 return;
125}
126
128 const DlPaint* paint) const {
129 return Draw(context, context.canvas, paint);
130}
131
133 DlCanvas* canvas,
134 const DlPaint* paint) const {
135 if (!context.raster_cache || !canvas) {
136 return false;
137 }
139 return context.raster_cache->Draw(key_id_, *canvas, paint,
141 }
142 return false;
143}
144
145static const auto* flow_type = "RasterCacheFlow::DisplayList";
146
148 const PaintContext& context,
149 bool parent_cached) const {
150 // If we don't have raster_cache we should not cache the current display_list.
151 // If the current node's ancestor has been cached we also should not cache the
152 // current node. In the current frame, the raster_cache will collect all
153 // display_list or picture_list to calculate the memory they used, we
154 // shouldn't cache the current node if the memory is more significant than the
155 // limit.
156 auto id = GetId();
157 FML_DCHECK(id.has_value());
158 if (cache_state_ == kNone || !context.raster_cache || parent_cached ||
159 !context.raster_cache->GenerateNewCacheInThisFrame() || !id.has_value()) {
160 return false;
161 }
162 SkRect bounds =
163 ToSkRect(display_list_->GetBounds()).makeOffset(offset_.x(), offset_.y());
164 RasterCache::Context r_context = {
165 // clang-format off
166 .gr_context = context.gr_context,
167 .dst_color_space = context.dst_color_space,
168 .matrix = transformation_matrix_,
169 .logical_rect = bounds,
170 .flow_type = flow_type,
171 // clang-format on
172 };
173 return context.raster_cache->UpdateCacheEntry(
174 id.value(), r_context,
175 [display_list = display_list_](DlCanvas* canvas) {
177 },
178 display_list_->rtree());
179}
180} // namespace flutter
181
182#endif // !SLIMPELLER
static DisplayListComplexityCalculator * GetForBackend(GrBackendApi backend)
static DisplayListComplexityCalculator * GetForSoftware()
virtual bool ShouldBeCached(unsigned int complexity_score)=0
virtual unsigned int Compute(const DisplayList *display_list)=0
const DlRect & GetBounds() const
void PrerollSetup(PrerollContext *context, const DlMatrix &matrix) override
void PrerollFinalize(PrerollContext *context, const DlMatrix &matrix) override
bool TryToPrepareRasterCache(const PaintContext &context, bool parent_cached=false) const override
static std::unique_ptr< DisplayListRasterCacheItem > Make(const sk_sp< DisplayList > &, const SkPoint &offset, bool is_complex, bool will_change)
DisplayListRasterCacheItem(const sk_sp< DisplayList > &display_list, const SkPoint &offset, bool is_complex=true, bool will_change=false)
bool Draw(const PaintContext &context, const DlPaint *paint) const override
Developer-facing API for rendering anything within the engine.
Definition dl_canvas.h:32
virtual void DrawDisplayList(const sk_sp< DisplayList > display_list, DlScalar opacity=SK_Scalar1)=0
bool content_culled(const DlRect &content_bounds) const
static constexpr int kCallerCanApplyOpacity
virtual std::optional< RasterCacheKeyID > GetId() const
int32_t value
#define FML_DCHECK(condition)
Definition logging.h:122
static bool IsDisplayListWorthRasterizing(const DisplayList *display_list, bool will_change, bool is_complex, DisplayListComplexityCalculator *complexity_calculator)
SkMatrix ToSkMatrix(const DlMatrix &matrix)
static const auto * flow_type
const SkRect & ToSkRect(const DlRect &rect)
bool rendering_above_platform_view
Definition layer.h:97
sk_sp< SkColorSpace > dst_color_space
Definition layer.h:100
DlCanvas * canvas
Definition layer.h:92
GrDirectContext * gr_context
Definition layer.h:99
GrDirectContext * gr_context
Definition layer.h:47
std::vector< RasterCacheItem * > * raster_cached_entries
Definition layer.h:75
LayerStateStack & state_stack
Definition layer.h:49
static bool CanRasterizeRect(const SkRect &cull_rect)
A 4x4 matrix using column-major storage.
Definition matrix.h:37