Flutter Engine
scene_update_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_SCENE_UPDATE_CONTEXT_H_
6 #define FLUTTER_FLOW_SCENE_UPDATE_CONTEXT_H_
7 
8 #include <fuchsia/ui/views/cpp/fidl.h>
9 #include <lib/ui/scenic/cpp/resources.h>
10 #include <lib/ui/scenic/cpp/session.h>
11 #include <lib/ui/scenic/cpp/view_ref_pair.h>
12 
13 #include <cfloat>
14 #include <memory>
15 #include <set>
16 #include <vector>
17 
18 #include "flutter/flow/embedded_views.h"
19 #include "flutter/fml/logging.h"
20 #include "flutter/fml/macros.h"
21 #include "third_party/skia/include/core/SkRect.h"
22 #include "third_party/skia/include/core/SkSurface.h"
23 
24 namespace flutter {
25 
26 class Layer;
27 
28 // Scenic currently lacks an API to enable rendering of alpha channel; this only
29 // happens if there is a OpacityNode higher in the tree with opacity != 1. For
30 // now, clamp to a infinitesimally smaller value than 1, which does not cause
31 // visual problems in practice.
32 constexpr float kOneMinusEpsilon = 1 - FLT_EPSILON;
33 
34 // How much layers are separated in Scenic z elevation.
35 constexpr float kScenicZElevationBetweenLayers = 10.f;
36 
38  public:
39  virtual ~SessionWrapper() {}
40 
41  virtual scenic::Session* get() = 0;
42  virtual void Present() = 0;
43 };
44 
46  public:
47  class Entity {
48  public:
49  Entity(std::shared_ptr<SceneUpdateContext> context);
50  virtual ~Entity();
51 
52  std::shared_ptr<SceneUpdateContext> context() { return context_; }
53  scenic::EntityNode& entity_node() { return entity_node_; }
54  virtual scenic::ContainerNode& embedder_node() { return entity_node_; }
55 
56  private:
57  std::shared_ptr<SceneUpdateContext> context_;
58  Entity* const previous_entity_;
59 
60  scenic::EntityNode entity_node_;
61  };
62 
63  class Transform : public Entity {
64  public:
65  Transform(std::shared_ptr<SceneUpdateContext> context,
66  const SkMatrix& transform);
67  Transform(std::shared_ptr<SceneUpdateContext> context,
68  float scale_x,
69  float scale_y,
70  float scale_z);
71  virtual ~Transform();
72 
73  private:
74  float const previous_scale_x_;
75  float const previous_scale_y_;
76  };
77 
78  class Frame : public Entity {
79  public:
80  // When layer is not nullptr, the frame is associated with a layer subtree
81  // rooted with that layer. The frame may then create a surface that will be
82  // retained for that layer.
83  Frame(std::shared_ptr<SceneUpdateContext> context,
84  const SkRRect& rrect,
85  SkColor color,
86  SkAlpha opacity,
87  std::string label);
88  virtual ~Frame();
89 
90  scenic::ContainerNode& embedder_node() override { return opacity_node_; }
91 
92  void AddPaintLayer(Layer* layer);
93 
94  private:
95  const float previous_elevation_;
96 
97  const SkRRect rrect_;
98  SkColor const color_;
99  SkAlpha const opacity_;
100 
101  scenic::OpacityNodeHACK opacity_node_;
102  std::vector<Layer*> paint_layers_;
103  SkRect paint_bounds_;
104  };
105 
106  class Clip : public Entity {
107  public:
108  Clip(std::shared_ptr<SceneUpdateContext> context,
109  const SkRect& shape_bounds);
110  ~Clip() = default;
111  };
112 
113  struct PaintTask {
114  SkRect paint_bounds;
115  SkScalar scale_x;
116  SkScalar scale_y;
118  scenic::Material material;
119  std::vector<Layer*> layers;
120  };
121 
122  SceneUpdateContext(std::string debug_label,
123  fuchsia::ui::views::ViewToken view_token,
124  scenic::ViewRefPair view_ref_pair,
125  SessionWrapper& session);
126  ~SceneUpdateContext() = default;
127 
128  scenic::ContainerNode& root_node() { return root_node_; }
129 
130  // The cumulative alpha value based on all the parent OpacityLayers.
131  void set_alphaf(float alpha) { alpha_ = alpha; }
132  float alphaf() { return alpha_; }
133 
134  // Returns all `PaintTask`s generated for the current frame.
135  std::vector<PaintTask> GetPaintTasks();
136 
137  // Enable/disable wireframe rendering around the root view bounds.
138  void EnableWireframe(bool enable);
139 
140  // Reset state for a new frame.
141  void Reset();
142 
143  // |ExternalViewEmbedder|
144  SkCanvas* GetRootCanvas() override { return nullptr; }
145 
146  // |ExternalViewEmbedder|
147  void CancelFrame() override {}
148 
149  // |ExternalViewEmbedder|
151  SkISize frame_size,
152  GrDirectContext* context,
153  double device_pixel_ratio,
154  fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) override {}
155 
156  // |ExternalViewEmbedder|
158  int view_id,
159  std::unique_ptr<EmbeddedViewParams> params) override {}
160 
161  // |ExternalViewEmbedder|
162  std::vector<SkCanvas*> GetCurrentCanvases() override {
163  return std::vector<SkCanvas*>();
164  }
165 
166  // |ExternalViewEmbedder|
167  virtual SkCanvas* CompositeEmbeddedView(int view_id) override {
168  return nullptr;
169  }
170 
171  void CreateView(int64_t view_id, bool hit_testable, bool focusable);
172  void UpdateView(int64_t view_id, bool hit_testable, bool focusable);
173  void DestroyView(int64_t view_id);
174  void UpdateView(int64_t view_id,
175  const SkPoint& offset,
176  const SkSize& size,
177  std::optional<bool> override_hit_testable = std::nullopt);
178 
179  private:
180  void CreateFrame(scenic::EntityNode& entity_node,
181  const SkRRect& rrect,
182  SkColor color,
183  SkAlpha opacity,
184  const SkRect& paint_bounds,
185  std::vector<Layer*> paint_layers);
186 
187  SessionWrapper& session_;
188 
189  scenic::View root_view_;
190  scenic::EntityNode root_node_;
191 
192  std::vector<PaintTask> paint_tasks_;
193 
194  Entity* top_entity_ = nullptr;
195  float top_scale_x_ = 1.f;
196  float top_scale_y_ = 1.f;
197  float top_elevation_ = 0.f;
198 
199  float next_elevation_ = 0.f;
200  float alpha_ = 1.f;
201 
203 };
204 
205 } // namespace flutter
206 
207 #endif // FLUTTER_FLOW_SCENE_UPDATE_CONTEXT_H_
virtual void Present()=0
SkCanvas * GetRootCanvas() override
scenic::ContainerNode & root_node()
virtual SkCanvas * CompositeEmbeddedView(int view_id) override
void PrerollCompositeEmbeddedView(int view_id, std::unique_ptr< EmbeddedViewParams > params) override
constexpr float kOneMinusEpsilon
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
void BeginFrame(SkISize frame_size, GrDirectContext *context, double device_pixel_ratio, fml::RefPtr< fml::RasterThreadMerger > raster_thread_merger) override
constexpr float kScenicZElevationBetweenLayers
Clip
Definition: layer.h:41
std::shared_ptr< SceneUpdateContext > context()
scenic::ContainerNode & embedder_node() override
std::vector< SkCanvas * > GetCurrentCanvases() override
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
virtual scenic::ContainerNode & embedder_node()