Flutter Engine
fuchsia_external_view_embedder.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_SHELL_PLATFORM_FUCHSIA_FLUTTER_FUCHSIA_EXTERNAL_VIEW_EMBEDDER_H_
6 #define FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_FUCHSIA_EXTERNAL_VIEW_EMBEDDER_H_
7 
8 #include <fuchsia/ui/views/cpp/fidl.h>
9 #include <lib/ui/scenic/cpp/resources.h>
10 #include <lib/ui/scenic/cpp/view_ref_pair.h>
11 
12 #include <cstdint> // For uint32_t & uint64_t
13 #include <memory>
14 #include <string>
15 #include <unordered_map>
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 "flutter/shell/common/canvas_spy.h"
22 #include "third_party/skia/include/core/SkCanvas.h"
23 #include "third_party/skia/include/core/SkPictureRecorder.h"
24 #include "third_party/skia/include/core/SkPoint.h"
25 #include "third_party/skia/include/core/SkSize.h"
26 #include "third_party/skia/include/gpu/GrDirectContext.h"
27 
28 #include "session_connection.h"
30 
31 namespace flutter_runner {
32 
33 // This class orchestrates interaction with the Scenic compositor on Fuchsia. It
34 // ensures that flutter content and platform view content are both rendered
35 // correctly in a unified scene.
37  public:
38  FuchsiaExternalViewEmbedder(std::string debug_label,
39  fuchsia::ui::views::ViewToken view_token,
40  scenic::ViewRefPair view_ref_pair,
41  SessionConnection& session,
42  VulkanSurfaceProducer& surface_producer);
44 
45  // |ExternalViewEmbedder|
46  SkCanvas* GetRootCanvas() override;
47 
48  // |ExternalViewEmbedder|
49  std::vector<SkCanvas*> GetCurrentCanvases() override;
50 
51  // |ExternalViewEmbedder|
53  int view_id,
54  std::unique_ptr<flutter::EmbeddedViewParams> params) override;
55 
56  // |ExternalViewEmbedder|
57  SkCanvas* CompositeEmbeddedView(int view_id) override;
58 
59  // |ExternalViewEmbedder|
61  fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) override;
62 
63  // |ExternalViewEmbedder|
64  void BeginFrame(
65  SkISize frame_size,
66  GrDirectContext* context,
67  double device_pixel_ratio,
68  fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) override;
69 
70  // |ExternalViewEmbedder|
71  void EndFrame(
72  bool should_resubmit_frame,
73  fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) override;
74 
75  // |ExternalViewEmbedder|
76  void SubmitFrame(GrDirectContext* context,
77  std::unique_ptr<flutter::SurfaceFrame> frame) override;
78 
79  // |ExternalViewEmbedder|
80  void CancelFrame() override { Reset(); }
81 
82  // |ExternalViewEmbedder|
83  bool SupportsDynamicThreadMerging() override { return false; }
84 
85  // View manipulation.
86  // |SetViewProperties| doesn't manipulate the view directly -- it sets
87  // prending properties for the next |UpdateView| call.
88  void EnableWireframe(bool enable);
89  void CreateView(int64_t view_id);
90  void DestroyView(int64_t view_id);
91  void SetViewProperties(int64_t view_id, bool hit_testable, bool focusable);
92 
93  private:
94  // Reset state for a new frame.
95  void Reset();
96 
97  struct EmbedderLayer {
98  EmbedderLayer(const SkISize& frame_size,
99  std::optional<flutter::EmbeddedViewParams> view_params)
100  : embedded_view_params(std::move(view_params)),
101  recorder(std::make_unique<SkPictureRecorder>()),
102  canvas_spy(std::make_unique<flutter::CanvasSpy>(
103  recorder->beginRecording(frame_size.width(),
104  frame_size.height()))),
105  surface_size(frame_size) {}
106 
107  std::optional<flutter::EmbeddedViewParams> embedded_view_params;
108  std::unique_ptr<SkPictureRecorder> recorder;
109  std::unique_ptr<flutter::CanvasSpy> canvas_spy;
110  SkISize surface_size;
111  };
112 
113  struct ScenicView {
114  scenic::OpacityNodeHACK opacity_node;
115  scenic::EntityNode entity_node;
116  scenic::ViewHolder view_holder;
117 
118  SkPoint offset = SkPoint::Make(0.f, 0.f);
119  SkSize size = SkSize::MakeEmpty();
120  float elevation = 0.f;
121  float opacity = 1.f;
122  bool hit_testable = false;
123  bool focusable = false;
124 
125  bool pending_hit_testable = false;
126  bool pending_focusable = false;
127  };
128 
129  struct ScenicLayer {
130  scenic::ShapeNode shape_node;
131  scenic::Material material;
132  };
133 
134  using EmbedderLayerId = std::optional<uint32_t>;
135  constexpr static EmbedderLayerId kRootLayerId = EmbedderLayerId{};
136 
137  SessionConnection& session_;
138  VulkanSurfaceProducer& surface_producer_;
139 
140  scenic::View root_view_;
141  scenic::EntityNode metrics_node_;
142  scenic::EntityNode root_node_;
143 
144  std::unordered_map<uint64_t, std::vector<scenic::Rectangle>> scenic_rects_;
145  std::unordered_map<int64_t, ScenicView> scenic_views_;
146  std::vector<ScenicLayer> scenic_layers_;
147 
148  std::unordered_map<EmbedderLayerId, EmbedderLayer> frame_layers_;
149  std::vector<EmbedderLayerId> frame_composition_order_;
150  SkISize frame_size_ = SkISize::Make(0, 0);
151  float frame_dpr_ = 1.f;
152 
154 };
155 
156 } // namespace flutter_runner
157 
158 #endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_FUCHSIA_EXTERNAL_VIEW_EMBEDDER_H_
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
std::vector< SkCanvas * > GetCurrentCanvases() override
FuchsiaExternalViewEmbedder(std::string debug_label, fuchsia::ui::views::ViewToken view_token, scenic::ViewRefPair view_ref_pair, SessionConnection &session, VulkanSurfaceProducer &surface_producer)
void EndFrame(bool should_resubmit_frame, fml::RefPtr< fml::RasterThreadMerger > raster_thread_merger) override
void SetViewProperties(int64_t view_id, bool hit_testable, bool focusable)
flutter::PostPrerollResult PostPrerollAction(fml::RefPtr< fml::RasterThreadMerger > raster_thread_merger) override
void SubmitFrame(GrDirectContext *context, std::unique_ptr< flutter::SurfaceFrame > frame) override
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
void PrerollCompositeEmbeddedView(int view_id, std::unique_ptr< flutter::EmbeddedViewParams > params) override