Flutter Engine
The Flutter Engine
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 std::shared_ptr<Layer>& root_layer,
22 const SkISize& frame_size)
23 : root_layer_(root_layer), frame_size_(frame_size) {}
24
26 return canvas ? canvas->GetImageInfo().colorSpace() : nullptr;
27}
28
30 bool ignore_raster_cache,
31 SkRect cull_rect) {
32 TRACE_EVENT0("flutter", "LayerTree::Preroll");
33
34 if (!root_layer_) {
35 FML_LOG(ERROR) << "The scene did not specify any layers.";
36 return false;
37 }
38
39 SkColorSpace* color_space = GetColorSpace(frame.canvas());
40 LayerStateStack state_stack;
41 state_stack.set_preroll_delegate(cull_rect,
42 frame.root_surface_transformation());
43
44 raster_cache_items_.clear();
45
46 PrerollContext context = {
47#if !SLIMPELLER
48 .raster_cache =
49 ignore_raster_cache ? nullptr : &frame.context().raster_cache(),
50#endif // !SLIMPELLER
51 .gr_context = frame.gr_context(),
52 .view_embedder = frame.view_embedder(),
53 .state_stack = state_stack,
54 .dst_color_space = sk_ref_sp<SkColorSpace>(color_space),
55 .surface_needs_readback = false,
56 .raster_time = frame.context().raster_time(),
57 .ui_time = frame.context().ui_time(),
58 .texture_registry = frame.context().texture_registry(),
59 .raster_cached_entries = &raster_cache_items_,
60 };
61
62 root_layer_->Preroll(&context);
63
64 return context.surface_needs_readback;
65}
66
67#if !SLIMPELLER
69 const std::vector<RasterCacheItem*>& raster_cached_items,
70 const PaintContext* paint_context,
71 bool ignore_raster_cache) {
72 unsigned i = 0;
73 const auto item_size = raster_cached_items.size();
74 while (i < item_size) {
75 auto* item = raster_cached_items[i];
76 if (item->need_caching()) {
77 // try to cache current layer
78 // If parent failed to cache, just proceed to the next entry
79 // cache current entry, this entry's parent must not cache
80 if (item->TryToPrepareRasterCache(*paint_context, false)) {
81 // if parent cached, then foreach child layer to touch them.
82 for (unsigned j = 0; j < item->child_items(); j++) {
83 auto* child_item = raster_cached_items[i + j + 1];
84 if (child_item->need_caching()) {
85 child_item->TryToPrepareRasterCache(*paint_context, true);
86 }
87 }
88 i += item->child_items() + 1;
89 continue;
90 }
91 }
92 i++;
93 }
94}
95#endif // !SLIMPELLER
96
98 bool ignore_raster_cache) const {
99 TRACE_EVENT0("flutter", "LayerTree::Paint");
100
101 if (!root_layer_) {
102 FML_LOG(ERROR) << "The scene did not specify any layers to paint.";
103 return;
104 }
105
106 LayerStateStack state_stack;
107
108 DlCanvas* canvas = frame.canvas();
109 state_stack.set_delegate(canvas);
110
111 // clear the previous snapshots.
112 LayerSnapshotStore* snapshot_store = nullptr;
113 if (enable_leaf_layer_tracing_) {
114 frame.context().snapshot_store().Clear();
115 snapshot_store = &frame.context().snapshot_store();
116 }
117
118 SkColorSpace* color_space = GetColorSpace(frame.canvas());
119
120#if !SLIMPELLER
122 ignore_raster_cache ? nullptr : &frame.context().raster_cache();
123#endif // !SLIMPELLER
124
125 PaintContext context = {
126 // clang-format off
127 .state_stack = state_stack,
128 .canvas = canvas,
129 .gr_context = frame.gr_context(),
130 .dst_color_space = sk_ref_sp(color_space),
131 .view_embedder = frame.view_embedder(),
132 .raster_time = frame.context().raster_time(),
133 .ui_time = frame.context().ui_time(),
134 .texture_registry = frame.context().texture_registry(),
135#if !SLIMPELLER
136 .raster_cache = cache,
137#endif // !SLIMPELLER
138 .layer_snapshot_store = snapshot_store,
139 .enable_leaf_layer_tracing = enable_leaf_layer_tracing_,
140 .impeller_enabled = !!frame.aiks_context(),
141 .aiks_context = frame.aiks_context(),
142 // clang-format on
143 };
144
145#if !SLIMPELLER
146 if (cache) {
147 cache->EvictUnusedCacheEntries();
148 TryToRasterCache(raster_cache_items_, &context, ignore_raster_cache);
149 }
150#endif // !SLIMPELLER
151
152 if (root_layer_->needs_painting(context)) {
153 root_layer_->Paint(context);
154 }
155}
156
158 const SkRect& bounds,
159 const std::shared_ptr<TextureRegistry>& texture_registry,
160 GrDirectContext* gr_context) {
161 TRACE_EVENT0("flutter", "LayerTree::Flatten");
162
164
165 const FixedRefreshRateStopwatch unused_stopwatch;
166
167 LayerStateStack preroll_state_stack;
168 // No root surface transformation. So assume identity.
169 preroll_state_stack.set_preroll_delegate(bounds);
170 PrerollContext preroll_context{
171 // clang-format off
172#if !SLIMPELLER
173 .raster_cache = nullptr,
174#endif // !SLIMPELLER
175 .gr_context = gr_context,
176 .view_embedder = nullptr,
177 .state_stack = preroll_state_stack,
178 .dst_color_space = nullptr,
179 .surface_needs_readback = false,
180 .raster_time = unused_stopwatch,
181 .ui_time = unused_stopwatch,
182 .texture_registry = texture_registry,
183 // clang-format on
184 };
185
186 LayerStateStack paint_state_stack;
187 paint_state_stack.set_delegate(&builder);
188 PaintContext paint_context = {
189 // clang-format off
190 .state_stack = paint_state_stack,
191 .canvas = &builder,
192 .gr_context = gr_context,
193 .dst_color_space = nullptr,
194 .view_embedder = nullptr,
195 .raster_time = unused_stopwatch,
196 .ui_time = unused_stopwatch,
197 .texture_registry = texture_registry,
198#if !SLIMPELLER
199 .raster_cache = nullptr,
200#endif // !SLIMPELLER
201 .layer_snapshot_store = nullptr,
202 .enable_leaf_layer_tracing = false,
203 // clang-format on
204 };
205
206 // Even if we don't have a root layer, we still need to create an empty
207 // picture.
208 if (root_layer_) {
209 root_layer_->Preroll(&preroll_context);
210
211 // The needs painting flag may be set after the preroll. So check it after.
212 if (root_layer_->needs_painting(paint_context)) {
213 root_layer_->Paint(paint_context);
214 }
215 }
216
217 return builder.Build();
218}
219
220} // 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:38
virtual SkImageInfo GetImageInfo() const =0
Used for fixed refresh rate cases.
Definition: stopwatch.h:77
Collects snapshots of layers during frame rasterization.
void set_preroll_delegate(const SkRect &cull_rect, const SkMatrix &matrix)
void set_delegate(DlCanvas *canvas)
bool Preroll(CompositorContext::ScopedFrame &frame, bool ignore_raster_cache=false, SkRect cull_rect=kGiantRect)
Definition: layer_tree.cc:29
LayerTree(const std::shared_ptr< Layer > &root_layer, const SkISize &frame_size)
Definition: layer_tree.cc:21
void Paint(CompositorContext::ScopedFrame &frame, bool ignore_raster_cache=false) const
Definition: layer_tree.cc:97
sk_sp< DisplayList > Flatten(const SkRect &bounds, const std::shared_ptr< TextureRegistry > &texture_registry=nullptr, GrDirectContext *gr_context=nullptr)
Definition: layer_tree.cc:157
static void TryToRasterCache(const std::vector< RasterCacheItem * > &raster_cached_entries, const PaintContext *paint_context, bool ignore_raster_cache=false)
Definition: layer_tree.cc:68
double frame
Definition: examples.cpp:31
#define FML_LOG(severity)
Definition: logging.h:82
Optional< SkRect > bounds
Definition: SkRecords.h:189
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:25
Definition: SkSize.h:16
SkColorSpace * colorSpace() const
LayerStateStack & state_stack
Definition: layer.h:101
GrDirectContext * gr_context
Definition: layer.h:57
bool surface_needs_readback
Definition: layer.h:61
#define ERROR(message)
Definition: elf_loader.cc:260
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:131