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