Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
layer_tree.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/layer_tree.h"
6
7#include "flutter/display_list/skia/dl_sk_canvas.h"
8#include "flutter/flow/embedded_views.h"
9#include "flutter/flow/frame_timings.h"
10#include "flutter/flow/layer_snapshot_store.h"
11#include "flutter/flow/layers/layer.h"
12#include "flutter/flow/paint_utils.h"
13#include "flutter/flow/raster_cache.h"
14#include "flutter/flow/raster_cache_item.h"
15#include "flutter/fml/time/time_point.h"
16#include "flutter/fml/trace_event.h"
18
19namespace flutter {
20
21LayerTree::LayerTree(const Config& config, const SkISize& frame_size)
22 : root_layer_(config.root_layer),
23 frame_size_(frame_size),
24 rasterizer_tracing_threshold_(config.rasterizer_tracing_threshold),
25 checkerboard_raster_cache_images_(
26 config.checkerboard_raster_cache_images),
27 checkerboard_offscreen_layers_(config.checkerboard_offscreen_layers) {}
28
30 return canvas ? canvas->GetImageInfo().colorSpace() : nullptr;
31}
32
34 bool ignore_raster_cache,
35 SkRect cull_rect) {
36 TRACE_EVENT0("flutter", "LayerTree::Preroll");
37
38 if (!root_layer_) {
39 FML_LOG(ERROR) << "The scene did not specify any layers.";
40 return false;
41 }
42
43 SkColorSpace* color_space = GetColorSpace(frame.canvas());
44 frame.context().raster_cache().SetCheckboardCacheImages(
45 checkerboard_raster_cache_images_);
46 LayerStateStack state_stack;
47 state_stack.set_preroll_delegate(cull_rect,
48 frame.root_surface_transformation());
50 ignore_raster_cache ? nullptr : &frame.context().raster_cache();
51 raster_cache_items_.clear();
52
53 PrerollContext context = {
54 // clang-format off
56 .gr_context = frame.gr_context(),
57 .view_embedder = frame.view_embedder(),
58 .state_stack = state_stack,
59 .dst_color_space = sk_ref_sp<SkColorSpace>(color_space),
60 .surface_needs_readback = false,
61 .raster_time = frame.context().raster_time(),
62 .ui_time = frame.context().ui_time(),
63 .texture_registry = frame.context().texture_registry(),
64 .raster_cached_entries = &raster_cache_items_,
65 // clang-format on
66 };
67
68 root_layer_->Preroll(&context);
69
70 return context.surface_needs_readback;
71}
72
74 const std::vector<RasterCacheItem*>& raster_cached_items,
75 const PaintContext* paint_context,
76 bool ignore_raster_cache) {
77 unsigned i = 0;
78 const auto item_size = raster_cached_items.size();
79 while (i < item_size) {
80 auto* item = raster_cached_items[i];
81 if (item->need_caching()) {
82 // try to cache current layer
83 // If parent failed to cache, just proceed to the next entry
84 // cache current entry, this entry's parent must not cache
85 if (item->TryToPrepareRasterCache(*paint_context, false)) {
86 // if parent cached, then foreach child layer to touch them.
87 for (unsigned j = 0; j < item->child_items(); j++) {
88 auto* child_item = raster_cached_items[i + j + 1];
89 if (child_item->need_caching()) {
90 child_item->TryToPrepareRasterCache(*paint_context, true);
91 }
92 }
93 i += item->child_items() + 1;
94 continue;
95 }
96 }
97 i++;
98 }
99}
100
102 bool ignore_raster_cache) const {
103 TRACE_EVENT0("flutter", "LayerTree::Paint");
104
105 if (!root_layer_) {
106 FML_LOG(ERROR) << "The scene did not specify any layers to paint.";
107 return;
108 }
109
110 LayerStateStack state_stack;
111
112 // DrawCheckerboard is not supported on Impeller.
113 if (checkerboard_offscreen_layers_ && !frame.aiks_context()) {
115 }
116
117 DlCanvas* canvas = frame.canvas();
118 state_stack.set_delegate(canvas);
119
120 // clear the previous snapshots.
121 LayerSnapshotStore* snapshot_store = nullptr;
122 if (enable_leaf_layer_tracing_) {
123 frame.context().snapshot_store().Clear();
124 snapshot_store = &frame.context().snapshot_store();
125 }
126
127 SkColorSpace* color_space = GetColorSpace(frame.canvas());
129 ignore_raster_cache ? nullptr : &frame.context().raster_cache();
130 PaintContext context = {
131 // clang-format off
132 .state_stack = state_stack,
133 .canvas = canvas,
134 .gr_context = frame.gr_context(),
135 .dst_color_space = sk_ref_sp(color_space),
136 .view_embedder = frame.view_embedder(),
137 .raster_time = frame.context().raster_time(),
138 .ui_time = frame.context().ui_time(),
139 .texture_registry = frame.context().texture_registry(),
140 .raster_cache = cache,
141 .layer_snapshot_store = snapshot_store,
142 .enable_leaf_layer_tracing = enable_leaf_layer_tracing_,
143 .impeller_enabled = !!frame.aiks_context(),
144 .aiks_context = frame.aiks_context(),
145 // clang-format on
146 };
147
148 if (cache) {
149 cache->EvictUnusedCacheEntries();
150 TryToRasterCache(raster_cache_items_, &context, ignore_raster_cache);
151 }
152
153 if (root_layer_->needs_painting(context)) {
154 root_layer_->Paint(context);
155 }
156}
157
159 const SkRect& bounds,
160 const std::shared_ptr<TextureRegistry>& texture_registry,
161 GrDirectContext* gr_context) {
162 TRACE_EVENT0("flutter", "LayerTree::Flatten");
163
164 DisplayListBuilder builder(bounds);
165
166 const FixedRefreshRateStopwatch unused_stopwatch;
167
168 LayerStateStack preroll_state_stack;
169 // No root surface transformation. So assume identity.
170 preroll_state_stack.set_preroll_delegate(bounds);
171 PrerollContext preroll_context{
172 // clang-format off
173 .raster_cache = nullptr,
174 .gr_context = gr_context,
175 .view_embedder = nullptr,
176 .state_stack = preroll_state_stack,
177 .dst_color_space = nullptr,
178 .surface_needs_readback = false,
179 .raster_time = unused_stopwatch,
180 .ui_time = unused_stopwatch,
181 .texture_registry = texture_registry,
182 // clang-format on
183 };
184
185 LayerStateStack paint_state_stack;
186 paint_state_stack.set_delegate(&builder);
187 PaintContext paint_context = {
188 // clang-format off
189 .state_stack = paint_state_stack,
190 .canvas = &builder,
191 .gr_context = gr_context,
192 .dst_color_space = nullptr,
193 .view_embedder = nullptr,
194 .raster_time = unused_stopwatch,
195 .ui_time = unused_stopwatch,
196 .texture_registry = texture_registry,
197 .raster_cache = nullptr,
198 .layer_snapshot_store = nullptr,
199 .enable_leaf_layer_tracing = false,
200 // clang-format on
201 };
202
203 // Even if we don't have a root layer, we still need to create an empty
204 // picture.
205 if (root_layer_) {
206 root_layer_->Preroll(&preroll_context);
207
208 // The needs painting flag may be set after the preroll. So check it after.
209 if (root_layer_->needs_painting(paint_context)) {
210 root_layer_->Paint(paint_context);
211 }
212 }
213
214 return builder.Build();
215}
216
217} // namespace flutter
sk_sp< T > sk_ref_sp(T *obj)
Definition SkRefCnt.h:381
Developer-facing API for rendering anything within the engine.
Definition dl_canvas.h:37
virtual SkImageInfo GetImageInfo() const =0
Used for fixed refresh rate cases.
Definition stopwatch.h:77
Collects snapshots of layers during frame rasterization.
void Clear()
Clears all the stored snapshots.
void set_preroll_delegate(const SkRect &cull_rect, const SkMatrix &matrix)
void set_checkerboard_func(CheckerboardFunc checkerboard_func)
void set_delegate(DlCanvas *canvas)
bool Preroll(CompositorContext::ScopedFrame &frame, bool ignore_raster_cache=false, SkRect cull_rect=kGiantRect)
Definition layer_tree.cc:33
void Paint(CompositorContext::ScopedFrame &frame, bool ignore_raster_cache=false) const
sk_sp< DisplayList > Flatten(const SkRect &bounds, const std::shared_ptr< TextureRegistry > &texture_registry=nullptr, GrDirectContext *gr_context=nullptr)
LayerTree(const Config &config, const SkISize &frame_size)
Definition layer_tree.cc:21
static void TryToRasterCache(const std::vector< RasterCacheItem * > &raster_cached_entries, const PaintContext *paint_context, bool ignore_raster_cache=false)
Definition layer_tree.cc:73
double frame
Definition examples.cpp:31
#define FML_LOG(severity)
Definition logging.h:82
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets Path to the Flutter assets directory enable service port Allow the VM service to fallback to automatic port selection if binding to a specified port fails trace Trace early application lifecycle Automatically switches to an endless trace buffer trace skia Filters out all Skia trace event categories except those that are specified in this comma separated list dump skp on shader Automatically dump the skp that triggers new shader compilations This is useful for writing custom ShaderWarmUp to reduce jank By this is not enabled to reduce the overhead purge persistent cache
Definition switches.h:191
SkColorSpace * GetColorSpace(DlCanvas *canvas)
Definition layer_tree.cc:29
void DrawCheckerboard(DlCanvas *canvas, const SkRect &rect)
SkColorSpace * colorSpace() const
LayerStateStack & state_stack
Definition layer.h:100
RasterCache * raster_cache
Definition layer.h:55
bool surface_needs_readback
Definition layer.h:60
#define ERROR(message)
#define TRACE_EVENT0(category_group, name)