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