Flutter Engine
The 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#include "flutter/flow/layers/display_list_raster_cache_item.h"
6
7#include <optional>
8#include <utility>
9
10#include "flutter/display_list/benchmarking/dl_complexity.h"
11#include "flutter/display_list/display_list.h"
12#include "flutter/flow/layers/layer.h"
13#include "flutter/flow/raster_cache.h"
14#include "flutter/flow/raster_cache_item.h"
15#include "flutter/flow/raster_cache_key.h"
16#include "flutter/flow/raster_cache_util.h"
18
19namespace flutter {
20
22 const DisplayList* display_list,
23 bool will_change,
24 bool is_complex,
25 DisplayListComplexityCalculator* complexity_calculator) {
26 if (will_change) {
27 // If the display list is going to change in the future, there is no point
28 // in doing to extra work to rasterize.
29 return false;
30 }
31
32 if (display_list == nullptr ||
33 !RasterCacheUtil::CanRasterizeRect(display_list->bounds())) {
34 // No point in deciding whether the display list is worth rasterizing if it
35 // cannot be rasterized at all.
36 return false;
37 }
38
39 if (is_complex) {
40 // The caller seems to have extra information about the display list and
41 // thinks the display list is always worth rasterizing.
42 return true;
43 }
44
45 unsigned int complexity_score = complexity_calculator->Compute(display_list);
46 return complexity_calculator->ShouldBeCached(complexity_score);
47}
48
50 const sk_sp<DisplayList>& display_list,
51 const SkPoint& offset,
52 bool is_complex,
53 bool will_change)
54 : RasterCacheItem(RasterCacheKeyID(display_list->unique_id(),
56 CacheState::kCurrent),
57 display_list_(display_list),
58 offset_(offset),
59 is_complex_(is_complex),
60 will_change_(will_change) {}
61
62std::unique_ptr<DisplayListRasterCacheItem> DisplayListRasterCacheItem::Make(
63 const sk_sp<DisplayList>& display_list,
64 const SkPoint& offset,
65 bool is_complex,
66 bool will_change) {
67 return std::make_unique<DisplayListRasterCacheItem>(display_list, offset,
68 is_complex, will_change);
69}
70
72 const SkMatrix& matrix) {
74 DisplayListComplexityCalculator* complexity_calculator =
76 context->gr_context->backend())
78
79 if (!IsDisplayListWorthRasterizing(display_list(), will_change_, is_complex_,
80 complexity_calculator)) {
81 // We only deal with display lists that are worthy of rasterization.
82 return;
83 }
84
85 transformation_matrix_ = matrix;
86 transformation_matrix_.preTranslate(offset_.x(), offset_.y());
87
88 if (!transformation_matrix_.invert(nullptr)) {
89 // The matrix was singular. No point in going further.
90 return;
91 }
92
93 if (context->raster_cached_entries && context->raster_cache) {
94 context->raster_cached_entries->push_back(this);
96 }
97 return;
98}
99
101 const SkMatrix& matrix) {
102 if (cache_state_ == CacheState::kNone || !context->raster_cache ||
103 !context->raster_cached_entries) {
104 return;
105 }
106 auto* raster_cache = context->raster_cache;
107 SkRect bounds = display_list_->bounds().makeOffset(offset_.x(), offset_.y());
108 bool visible = !context->state_stack.content_culled(bounds);
109 RasterCache::CacheInfo cache_info =
110 raster_cache->MarkSeen(key_id_, matrix, visible);
111 if (!visible ||
112 cache_info.accesses_since_visible <= raster_cache->access_threshold()) {
114 } else {
115 if (cache_info.has_image) {
116 context->renderable_state_flags |=
118 }
120 }
121 return;
122}
123
125 const DlPaint* paint) const {
126 return Draw(context, context.canvas, paint);
127}
128
130 DlCanvas* canvas,
131 const DlPaint* paint) const {
132 if (!context.raster_cache || !canvas) {
133 return false;
134 }
136 return context.raster_cache->Draw(key_id_, *canvas, paint,
138 }
139 return false;
140}
141
142static const auto* flow_type = "RasterCacheFlow::DisplayList";
143
145 const PaintContext& context,
146 bool parent_cached) const {
147 // If we don't have raster_cache we should not cache the current display_list.
148 // If the current node's ancestor has been cached we also should not cache the
149 // current node. In the current frame, the raster_cache will collect all
150 // display_list or picture_list to calculate the memory they used, we
151 // shouldn't cache the current node if the memory is more significant than the
152 // limit.
153 auto id = GetId();
154 FML_DCHECK(id.has_value());
155 if (cache_state_ == kNone || !context.raster_cache || parent_cached ||
156 !context.raster_cache->GenerateNewCacheInThisFrame() || !id.has_value()) {
157 return false;
158 }
159 SkRect bounds = display_list_->bounds().makeOffset(offset_.x(), offset_.y());
160 RasterCache::Context r_context = {
161 // clang-format off
162 .gr_context = context.gr_context,
163 .dst_color_space = context.dst_color_space,
164 .matrix = transformation_matrix_,
165 .logical_rect = bounds,
166 .flow_type = flow_type,
167 // clang-format on
168 };
169 return context.raster_cache->UpdateCacheEntry(
170 id.value(), r_context,
171 [display_list = display_list_](DlCanvas* canvas) {
173 },
174 display_list_->rtree());
175}
176} // namespace flutter
SK_API GrBackendApi backend() const
bool invert(SkMatrix *inverse) const
Definition SkMatrix.h:1206
SkMatrix & preTranslate(SkScalar dx, SkScalar dy)
Definition SkMatrix.cpp:263
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
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)
void PrerollSetup(PrerollContext *context, const SkMatrix &matrix) override
DisplayListRasterCacheItem(const sk_sp< DisplayList > &display_list, const SkPoint &offset, bool is_complex=true, bool will_change=false)
void PrerollFinalize(PrerollContext *context, const SkMatrix &matrix) override
bool Draw(const PaintContext &context, const DlPaint *paint) const override
const SkRect & bounds() const
Developer-facing API for rendering anything within the engine.
Definition dl_canvas.h:37
virtual void DrawDisplayList(const sk_sp< DisplayList > display_list, SkScalar opacity=SK_Scalar1)=0
bool content_culled(const SkRect &content_bounds) const
static constexpr int kCallerCanApplyOpacity
virtual std::optional< RasterCacheKeyID > GetId() const
bool Draw(const RasterCacheKeyID &id, DlCanvas &canvas, const DlPaint *paint, bool preserve_rtree=false) const
bool UpdateCacheEntry(const RasterCacheKeyID &id, const Context &raster_cache_context, const std::function< void(DlCanvas *)> &render_function, sk_sp< const DlRTree > rtree=nullptr) const
bool GenerateNewCacheInThisFrame() const
const Paint & paint
uint8_t value
#define FML_DCHECK(condition)
Definition logging.h:103
static bool IsDisplayListWorthRasterizing(const DisplayList *display_list, bool will_change, bool is_complex, DisplayListComplexityCalculator *complexity_calculator)
static const auto * flow_type
Point offset
constexpr float y() const
constexpr float x() const
bool rendering_above_platform_view
Definition layer.h:106
sk_sp< SkColorSpace > dst_color_space
Definition layer.h:109
DlCanvas * canvas
Definition layer.h:101
GrDirectContext * gr_context
Definition layer.h:108
const RasterCache * raster_cache
Definition layer.h:114
GrDirectContext * gr_context
Definition layer.h:56
std::vector< RasterCacheItem * > * raster_cached_entries
Definition layer.h:84
RasterCache * raster_cache
Definition layer.h:55
LayerStateStack & state_stack
Definition layer.h:58
static bool CanRasterizeRect(const SkRect &cull_rect)