Flutter Engine
The Flutter Engine
display_list_layer.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_layer.h"
6
7#include <utility>
8
9#include "flutter/display_list/dl_builder.h"
10#include "flutter/flow/layer_snapshot_store.h"
11#include "flutter/flow/layers/cacheable_layer.h"
12#include "flutter/flow/layers/offscreen_surface.h"
13#include "flutter/flow/raster_cache.h"
14#include "flutter/flow/raster_cache_util.h"
15
16namespace flutter {
17
19 sk_sp<DisplayList> display_list,
20 bool is_complex,
21 bool will_change)
22 : offset_(offset), display_list_(std::move(display_list)) {
23 if (display_list_) {
24 bounds_ = display_list_->bounds().makeOffset(offset_.x(), offset_.y());
25#if !SLIMPELLER
26 display_list_raster_cache_item_ = DisplayListRasterCacheItem::Make(
27 display_list_, offset_, is_complex, will_change);
28#endif // !SLIMPELLER
29 }
30}
31
33 const Layer* layer) const {
34 // Only return true for identical display lists; This way
35 // ContainerLayer::DiffChildren can detect when a display list layer
36 // got inserted between other display list layers
37 auto old_layer = layer->as_display_list_layer();
38 return old_layer != nullptr && offset_ == old_layer->offset_ &&
39 Compare(context->statistics(), this, old_layer);
40}
41
42void DisplayListLayer::Diff(DiffContext* context, const Layer* old_layer) {
43 DiffContext::AutoSubtreeRestore subtree(context);
44 if (!context->IsSubtreeDirty()) {
45#ifndef NDEBUG
46 FML_DCHECK(old_layer);
47 auto prev = old_layer->as_display_list_layer();
48 DiffContext::Statistics dummy_statistics;
49 // IsReplacing has already determined that the display list is same
50 FML_DCHECK(prev->offset_ == offset_ &&
51 Compare(dummy_statistics, this, prev));
52#endif
53 }
54 context->PushTransform(SkMatrix::Translate(offset_.x(), offset_.y()));
55 if (context->has_raster_cache()) {
57 }
58 context->AddLayerBounds(display_list()->bounds());
59 context->SetLayerPaintRegion(this, context->CurrentSubtreeRegion());
60}
61
62bool DisplayListLayer::Compare(DiffContext::Statistics& statistics,
63 const DisplayListLayer* l1,
64 const DisplayListLayer* l2) {
65 const auto& dl1 = l1->display_list_;
66 const auto& dl2 = l2->display_list_;
67 if (dl1.get() == dl2.get()) {
68 statistics.AddSameInstancePicture();
69 return true;
70 }
71 const auto op_cnt_1 = dl1->op_count();
72 const auto op_cnt_2 = dl2->op_count();
73 const auto op_bytes_1 = dl1->bytes();
74 const auto op_bytes_2 = dl2->bytes();
75 if (op_cnt_1 != op_cnt_2 || op_bytes_1 != op_bytes_2 ||
76 dl1->bounds() != dl2->bounds()) {
77 statistics.AddNewPicture();
78 return false;
79 }
80
81 if (op_bytes_1 > kMaxBytesToCompare) {
83 return false;
84 }
85
86 statistics.AddDeepComparePicture();
87
88 auto res = dl1->Equals(*dl2);
89 if (res) {
91 } else {
92 statistics.AddNewPicture();
93 }
94 return res;
95}
96
98 DisplayList* disp_list = display_list();
99
100#if !SLIMPELLER
101 AutoCache cache = AutoCache(display_list_raster_cache_item_.get(), context,
102 context->state_stack.transform_3x3());
103#endif // !SLIMPELLER
104 if (disp_list->can_apply_group_opacity()) {
106 }
107 set_paint_bounds(bounds_);
108}
109
111 FML_DCHECK(display_list_);
112 FML_DCHECK(needs_painting(context));
113
114 auto mutator = context.state_stack.save();
115 mutator.translate(offset_.x(), offset_.y());
116
117#if !SLIMPELLER
118 if (context.raster_cache) {
119 // Always apply the integral transform in the presence of a raster cache
120 // whether or not we successfully draw from the cache
121 mutator.integralTransform();
122
123 if (display_list_raster_cache_item_) {
125 if (display_list_raster_cache_item_->Draw(
126 context, context.state_stack.fill(paint))) {
127 TRACE_EVENT_INSTANT0("flutter", "raster cache hit");
128 return;
129 }
130 }
131 }
132#endif // !SLIMPELLER
133
134 SkScalar opacity = context.state_stack.outstanding_opacity();
135
136#if !SLIMPELLER
137 // Leaf layer tracing was never supported in the Impeller backend.
138 if (context.enable_leaf_layer_tracing) {
139 const auto canvas_size = context.canvas->GetBaseLayerSize();
140 auto offscreen_surface =
141 std::make_unique<OffscreenSurface>(context.gr_context, canvas_size);
142
143 const auto& ctm = context.canvas->GetTransform();
144
145 const auto start_time = fml::TimePoint::Now();
146 {
147 // render display list to offscreen surface.
148 auto* canvas = offscreen_surface->GetCanvas();
149 {
150 DlAutoCanvasRestore save(canvas, true);
151 canvas->Clear(DlColor::kTransparent());
152 canvas->SetTransform(ctm);
153 canvas->DrawDisplayList(display_list_, opacity);
154 }
155 canvas->Flush();
156 }
157 const fml::TimeDelta offscreen_render_time =
158 fml::TimePoint::Now() - start_time;
159
160 const SkRect device_bounds =
162 sk_sp<SkData> raster_data = offscreen_surface->GetRasterData(true);
163 LayerSnapshotData snapshot_data(unique_id(), offscreen_render_time,
164 raster_data, device_bounds);
165 context.layer_snapshot_store->Add(snapshot_data);
166 }
167#endif // !SLIMPELLER
168
169 context.canvas->DrawDisplayList(display_list_, opacity);
170}
171
172} // namespace flutter
static float prev(float f)
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.h:91
void PushTransform(const SkMatrix &transform)
Definition: diff_context.cc:49
void WillPaintWithIntegralTransform()
Definition: diff_context.h:89
void AddLayerBounds(const SkRect &rect)
void SetLayerPaintRegion(const Layer *layer, const PaintRegion &region)
Statistics & statistics()
Definition: diff_context.h:203
PaintRegion CurrentSubtreeRegion() const
bool has_raster_cache() const
Definition: diff_context.h:167
bool IsSubtreeDirty() const
Definition: diff_context.h:108
bool IsReplacing(DiffContext *context, const Layer *layer) const override
void Preroll(PrerollContext *frame) override
void Diff(DiffContext *context, const Layer *old_layer) override
DisplayList * display_list() const
DisplayListLayer(const SkPoint &offset, sk_sp< DisplayList > display_list, bool is_complex, bool will_change)
static constexpr size_t kMaxBytesToCompare
void Paint(PaintContext &context) const override
static std::unique_ptr< DisplayListRasterCacheItem > Make(const sk_sp< DisplayList > &, const SkPoint &offset, bool is_complex, bool will_change)
bool can_apply_group_opacity() const
Definition: display_list.h:305
virtual SkISize GetBaseLayerSize() const =0
virtual void DrawDisplayList(const sk_sp< DisplayList > display_list, SkScalar opacity=SK_Scalar1)=0
virtual SkMatrix GetTransform() const =0
void Add(const LayerSnapshotData &data)
void translate(SkScalar tx, SkScalar ty)
SkMatrix transform_3x3() const
void fill(MutatorsStack *mutators)
SkScalar outstanding_opacity() const
static constexpr int kCallerCanApplyOpacity
const SkRect & paint_bounds() const
Definition: layer.h:210
bool needs_painting(PaintContext &context) const
Definition: layer.h:232
virtual const DisplayListLayer * as_display_list_layer() const
Definition: layer.h:259
uint64_t unique_id() const
Definition: layer.h:251
void set_paint_bounds(const SkRect &paint_bounds)
Definition: layer.h:223
static TimePoint Now()
Definition: time_point.cc:49
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
#define FML_DCHECK(condition)
Definition: logging.h:103
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
Definition: ref_ptr.h:256
SeparatedVector2 offset
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181
static constexpr DlColor kTransparent()
Definition: dl_color.h:21
bool enable_leaf_layer_tracing
Definition: layer.h:120
LayerSnapshotStore * layer_snapshot_store
Definition: layer.h:119
DlCanvas * canvas
Definition: layer.h:102
GrDirectContext * gr_context
Definition: layer.h:109
LayerStateStack & state_stack
Definition: layer.h:101
LayerStateStack & state_stack
Definition: layer.h:59
int renderable_state_flags
Definition: layer.h:83
static SkRect GetDeviceBounds(const SkRect &rect, const SkMatrix &ctm)
#define TRACE_EVENT_INSTANT0(category_group, name)
Definition: trace_event.h:175