Flutter Engine
The Flutter Engine
compositor_context.h
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#ifndef FLUTTER_FLOW_COMPOSITOR_CONTEXT_H_
6#define FLUTTER_FLOW_COMPOSITOR_CONTEXT_H_
7
8#include <memory>
9#include <string>
10
11#include "flutter/common/graphics/texture.h"
12#include "flutter/common/macros.h"
13#include "flutter/flow/diff_context.h"
14#include "flutter/flow/embedded_views.h"
15#include "flutter/flow/layer_snapshot_store.h"
16#include "flutter/flow/raster_cache.h"
17#include "flutter/flow/stopwatch.h"
18#include "flutter/fml/macros.h"
19#include "flutter/fml/raster_thread_merger.h"
22
23namespace flutter {
24
25class LayerTree;
26
27// The result status of CompositorContext::ScopedFrame::Raster.
28enum class RasterStatus {
29 // Frame has been successfully rasterized.
31 // Frame has been submited, but must be submitted again. This is only used
32 // on Android when switching the background surface to FlutterImageView.
33 //
34 // On Android, the first frame doesn't make the image available
35 // to the ImageReader right away. The second frame does.
36 //
37 // TODO(egarciad): https://github.com/flutter/flutter/issues/65652
39 // Frame has be dropped and a new frame with the same layer tree must be
40 // attempted.
41 //
42 // This is currently used to wait for the thread merger to merge
43 // the raster and platform threads.
44 //
45 // Since the thread merger may be disabled, the system will proceed
46 // with separate threads for rasterization and platform tasks,
47 // potentially leading to different performance characteristics.
49};
50
52 public:
53 // Sets previous layer tree for calculating frame damage. If not set, entire
54 // frame will be repainted.
55 void SetPreviousLayerTree(const LayerTree* prev_layer_tree) {
56 prev_layer_tree_ = prev_layer_tree;
57 }
58
59 // Adds additional damage (accumulated for double / triple buffering).
60 // This is area that will be repainted alongside any changed part.
61 void AddAdditionalDamage(const SkIRect& damage) {
62 additional_damage_.join(damage);
63 }
64
65 // Specifies clip rect alignment.
66 void SetClipAlignment(int horizontal, int vertical) {
67 horizontal_clip_alignment_ = horizontal;
68 vertical_clip_alignment_ = vertical;
69 }
70
71 // Calculates clip rect for current rasterization. This is diff of layer tree
72 // and previous layer tree + any additional provided damage.
73 // If previous layer tree is not specified, clip rect will be nullopt,
74 // but the paint region of layer_tree will be calculated so that it can be
75 // used for diffing of subsequent frames.
76 std::optional<SkRect> ComputeClipRect(flutter::LayerTree& layer_tree,
77 bool has_raster_cache,
78 bool impeller_enabled);
79
80 // See Damage::frame_damage.
81 std::optional<SkIRect> GetFrameDamage() const {
82 return damage_ ? std::make_optional(damage_->frame_damage) : std::nullopt;
83 }
84
85 // See Damage::buffer_damage.
86 std::optional<SkIRect> GetBufferDamage() {
87 return (damage_ && !ignore_damage_)
88 ? std::make_optional(damage_->buffer_damage)
89 : std::nullopt;
90 }
91
92 // Remove reported buffer_damage to inform clients that a partial repaint
93 // should not be performed on this frame.
94 // frame_damage is required to correctly track accumulated damage for
95 // subsequent frames.
96 void Reset() { ignore_damage_ = true; }
97
98 private:
99 SkIRect additional_damage_ = SkIRect::MakeEmpty();
100 std::optional<Damage> damage_;
101 const LayerTree* prev_layer_tree_ = nullptr;
102 int vertical_clip_alignment_ = 1;
103 int horizontal_clip_alignment_ = 1;
104 bool ignore_damage_ = false;
105};
106
108 public:
110 public:
116 bool instrumentation_enabled,
118 fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger,
120
121 virtual ~ScopedFrame();
122
123 DlCanvas* canvas() { return canvas_; }
124
125 ExternalViewEmbedder* view_embedder() { return view_embedder_; }
126
127 CompositorContext& context() const { return context_; }
128
130 return root_surface_transformation_;
131 }
132
133 bool surface_supports_readback() { return surface_supports_readback_; }
134
135 GrDirectContext* gr_context() const { return gr_context_; }
136
137 impeller::AiksContext* aiks_context() const { return aiks_context_; }
138
139 virtual RasterStatus Raster(LayerTree& layer_tree,
140 bool ignore_raster_cache,
141 FrameDamage* frame_damage);
142
143 private:
144 void PaintLayerTreeSkia(flutter::LayerTree& layer_tree,
145 std::optional<SkRect> clip_rect,
146 bool needs_save_layer,
147 bool ignore_raster_cache);
148
149 void PaintLayerTreeImpeller(flutter::LayerTree& layer_tree,
150 std::optional<SkRect> clip_rect,
151 bool ignore_raster_cache);
152
153 CompositorContext& context_;
154 GrDirectContext* gr_context_;
155 DlCanvas* canvas_;
156 impeller::AiksContext* aiks_context_;
157 ExternalViewEmbedder* view_embedder_;
158 const SkMatrix& root_surface_transformation_;
159 const bool instrumentation_enabled_;
160 const bool surface_supports_readback_;
161 fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger_;
162
163 FML_DISALLOW_COPY_AND_ASSIGN(ScopedFrame);
164 };
165
167
169
171
172 virtual std::unique_ptr<ScopedFrame> AcquireFrame(
173 GrDirectContext* gr_context,
174 DlCanvas* canvas,
175 ExternalViewEmbedder* view_embedder,
176 const SkMatrix& root_surface_transformation,
177 bool instrumentation_enabled,
178 bool surface_supports_readback,
179 fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger,
180 impeller::AiksContext* aiks_context);
181
182 void OnGrContextCreated();
183
185
186#if !SLIMPELLER
187 RasterCache& raster_cache() { return raster_cache_; }
188#endif // !SLIMPELLER
189
190 std::shared_ptr<TextureRegistry> texture_registry() {
191 return texture_registry_;
192 }
193
194 const Stopwatch& raster_time() const { return raster_time_; }
195
196 Stopwatch& ui_time() { return ui_time_; }
197
198 LayerSnapshotStore& snapshot_store() { return layer_snapshot_store_; }
199
200 private:
201 NOT_SLIMPELLER(RasterCache raster_cache_);
202 std::shared_ptr<TextureRegistry> texture_registry_;
203 Stopwatch raster_time_;
204 Stopwatch ui_time_;
205 LayerSnapshotStore layer_snapshot_store_;
206
207 /// Only used by default constructor of `CompositorContext`.
208 FixedRefreshRateUpdater fixed_refresh_rate_updater_;
209
210 void BeginFrame(ScopedFrame& frame, bool enable_instrumentation);
211
212 void EndFrame(ScopedFrame& frame, bool enable_instrumentation);
213
214 /// @brief Whether Impeller shouild attempt a partial repaint.
215 /// The Impeller backend requires an additional blit pass, which may
216 /// not be worthwhile if the damage region is large.
217 static bool ShouldPerformPartialRepaint(std::optional<SkRect> damage_rect,
218 SkISize layer_tree_size);
219
220 FML_DISALLOW_COPY_AND_ASSIGN(CompositorContext);
221};
222
223} // namespace flutter
224
225#endif // FLUTTER_FLOW_COMPOSITOR_CONTEXT_H_
ScopedFrame(CompositorContext &context, GrDirectContext *gr_context, DlCanvas *canvas, ExternalViewEmbedder *view_embedder, const SkMatrix &root_surface_transformation, bool instrumentation_enabled, bool surface_supports_readback, fml::RefPtr< fml::RasterThreadMerger > raster_thread_merger, impeller::AiksContext *aiks_context)
virtual RasterStatus Raster(LayerTree &layer_tree, bool ignore_raster_cache, FrameDamage *frame_damage)
impeller::AiksContext * aiks_context() const
const SkMatrix & root_surface_transformation() const
const Stopwatch & raster_time() const
std::shared_ptr< TextureRegistry > texture_registry()
LayerSnapshotStore & snapshot_store()
virtual std::unique_ptr< ScopedFrame > AcquireFrame(GrDirectContext *gr_context, DlCanvas *canvas, ExternalViewEmbedder *view_embedder, const SkMatrix &root_surface_transformation, bool instrumentation_enabled, bool surface_supports_readback, fml::RefPtr< fml::RasterThreadMerger > raster_thread_merger, impeller::AiksContext *aiks_context)
Developer-facing API for rendering anything within the engine.
Definition: dl_canvas.h:38
Used for fixed refresh rate query cases.
Definition: stopwatch.h:65
void SetPreviousLayerTree(const LayerTree *prev_layer_tree)
void SetClipAlignment(int horizontal, int vertical)
std::optional< SkIRect > GetFrameDamage() const
std::optional< SkRect > ComputeClipRect(flutter::LayerTree &layer_tree, bool has_raster_cache, bool impeller_enabled)
std::optional< SkIRect > GetBufferDamage()
void AddAdditionalDamage(const SkIRect &damage)
Collects snapshots of layers during frame rasterization.
The refresh rate interface for Stopwatch.
Definition: stopwatch.h:20
double frame
Definition: examples.cpp:31
Definition: SkRect.h:32
void join(const SkIRect &r)
Definition: SkRect.cpp:31
static constexpr SkIRect MakeEmpty()
Definition: SkRect.h:45
Definition: SkSize.h:16