Flutter Engine
The Flutter Engine
platform_view_embedder.cc
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#include "flutter/shell/platform/embedder/platform_view_embedder.h"
6
7#include <utility>
8
9#include "flutter/fml/make_copyable.h"
10
11namespace flutter {
12
14 : public PlatformMessageHandler {
15 public:
18 fml::RefPtr<fml::TaskRunner> platform_task_runner)
19 : parent_(std::move(parent)),
20 platform_task_runner_(std::move(platform_task_runner)) {}
21
22 virtual void HandlePlatformMessage(std::unique_ptr<PlatformMessage> message) {
23 platform_task_runner_->PostTask(fml::MakeCopyable(
24 [parent = parent_, message = std::move(message)]() mutable {
25 if (parent) {
26 parent->HandlePlatformMessage(std::move(message));
27 } else {
28 FML_DLOG(WARNING) << "Deleted engine dropping message on channel "
29 << message->channel();
30 }
31 }));
32 }
33
35 return true;
36 }
37
39 int response_id,
40 std::unique_ptr<fml::Mapping> mapping) {}
41 virtual void InvokePlatformMessageEmptyResponseCallback(int response_id) {}
42
43 private:
45 fml::RefPtr<fml::TaskRunner> platform_task_runner_;
46};
47
49 PlatformView::Delegate& delegate,
50 const flutter::TaskRunners& task_runners,
52 software_dispatch_table,
53 PlatformDispatchTable platform_dispatch_table,
54 std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder)
55 : PlatformView(delegate, task_runners),
56 external_view_embedder_(std::move(external_view_embedder)),
57 embedder_surface_(
58 std::make_unique<EmbedderSurfaceSoftware>(software_dispatch_table,
59 external_view_embedder_)),
60 platform_message_handler_(new EmbedderPlatformMessageHandler(
61 GetWeakPtr(),
62 task_runners.GetPlatformTaskRunner())),
63 platform_dispatch_table_(std::move(platform_dispatch_table)) {}
64
65#ifdef SHELL_ENABLE_GL
67 PlatformView::Delegate& delegate,
68 const flutter::TaskRunners& task_runners,
69 std::unique_ptr<EmbedderSurface> embedder_surface,
70 PlatformDispatchTable platform_dispatch_table,
71 std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder)
72 : PlatformView(delegate, task_runners),
73 external_view_embedder_(std::move(external_view_embedder)),
74 embedder_surface_(std::move(embedder_surface)),
75 platform_message_handler_(new EmbedderPlatformMessageHandler(
76 GetWeakPtr(),
77 task_runners.GetPlatformTaskRunner())),
78 platform_dispatch_table_(std::move(platform_dispatch_table)) {}
79#endif
80
81#ifdef SHELL_ENABLE_METAL
83 PlatformView::Delegate& delegate,
84 const flutter::TaskRunners& task_runners,
85 std::unique_ptr<EmbedderSurface> embedder_surface,
86 PlatformDispatchTable platform_dispatch_table,
87 std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder)
88 : PlatformView(delegate, task_runners),
89 external_view_embedder_(std::move(external_view_embedder)),
90 embedder_surface_(std::move(embedder_surface)),
91 platform_message_handler_(new EmbedderPlatformMessageHandler(
92 GetWeakPtr(),
93 task_runners.GetPlatformTaskRunner())),
94 platform_dispatch_table_(std::move(platform_dispatch_table)) {}
95#endif
96
97#ifdef SHELL_ENABLE_VULKAN
99 PlatformView::Delegate& delegate,
100 const flutter::TaskRunners& task_runners,
101 std::unique_ptr<EmbedderSurfaceVulkan> embedder_surface,
102 PlatformDispatchTable platform_dispatch_table,
103 std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder)
104 : PlatformView(delegate, task_runners),
105 external_view_embedder_(std::move(external_view_embedder)),
106 embedder_surface_(std::move(embedder_surface)),
107 platform_message_handler_(new EmbedderPlatformMessageHandler(
108 GetWeakPtr(),
109 task_runners.GetPlatformTaskRunner())),
110 platform_dispatch_table_(std::move(platform_dispatch_table)) {}
111#endif
112
114
118 if (platform_dispatch_table_.update_semantics_callback != nullptr) {
119 platform_dispatch_table_.update_semantics_callback(std::move(update),
120 std::move(actions));
121 }
122}
123
125 std::unique_ptr<flutter::PlatformMessage> message) {
126 if (!message) {
127 return;
128 }
129
130 if (platform_dispatch_table_.platform_message_response_callback == nullptr) {
131 if (message->response()) {
132 message->response()->CompleteEmpty();
133 }
134 return;
135 }
136
137 platform_dispatch_table_.platform_message_response_callback(
138 std::move(message));
139}
140
141// |PlatformView|
142std::unique_ptr<Surface> PlatformViewEmbedder::CreateRenderingSurface() {
143 if (embedder_surface_ == nullptr) {
144 FML_LOG(ERROR) << "Embedder surface was null.";
145 return nullptr;
146 }
147 return embedder_surface_->CreateGPUSurface();
148}
149
150// |PlatformView|
151std::shared_ptr<ExternalViewEmbedder>
152PlatformViewEmbedder::CreateExternalViewEmbedder() {
153 return external_view_embedder_;
154}
155
156std::shared_ptr<impeller::Context> PlatformViewEmbedder::GetImpellerContext()
157 const {
158 return embedder_surface_->CreateImpellerContext();
159}
160
161// |PlatformView|
162sk_sp<GrDirectContext> PlatformViewEmbedder::CreateResourceContext() const {
163 if (embedder_surface_ == nullptr) {
164 FML_LOG(ERROR) << "Embedder surface was null.";
165 return nullptr;
166 }
167 return embedder_surface_->CreateResourceContext();
168}
169
170// |PlatformView|
171std::unique_ptr<VsyncWaiter> PlatformViewEmbedder::CreateVSyncWaiter() {
172 if (!platform_dispatch_table_.vsync_callback) {
173 // Superclass implementation creates a timer based fallback.
175 }
176
177 return std::make_unique<VsyncWaiterEmbedder>(
178 platform_dispatch_table_.vsync_callback, task_runners_);
179}
180
181// |PlatformView|
182std::unique_ptr<std::vector<std::string>>
183PlatformViewEmbedder::ComputePlatformResolvedLocales(
184 const std::vector<std::string>& supported_locale_data) {
185 if (platform_dispatch_table_.compute_platform_resolved_locale_callback !=
186 nullptr) {
187 return platform_dispatch_table_.compute_platform_resolved_locale_callback(
188 supported_locale_data);
189 }
190 std::unique_ptr<std::vector<std::string>> out =
191 std::make_unique<std::vector<std::string>>();
192 return out;
193}
194
195// |PlatformView|
196void PlatformViewEmbedder::OnPreEngineRestart() const {
197 if (platform_dispatch_table_.on_pre_engine_restart_callback != nullptr) {
198 platform_dispatch_table_.on_pre_engine_restart_callback();
199 }
200}
201
202// |PlatformView|
203void PlatformViewEmbedder::SendChannelUpdate(const std::string& name,
204 bool listening) {
205 if (platform_dispatch_table_.on_channel_update != nullptr) {
206 platform_dispatch_table_.on_channel_update(name, listening);
207 }
208}
209
210std::shared_ptr<PlatformMessageHandler>
212 return platform_message_handler_;
213}
214
215} // namespace flutter
virtual void InvokePlatformMessageResponseCallback(int response_id, std::unique_ptr< fml::Mapping > mapping)
EmbedderPlatformMessageHandler(fml::WeakPtr< PlatformView > parent, fml::RefPtr< fml::TaskRunner > platform_task_runner)
virtual void HandlePlatformMessage(std::unique_ptr< PlatformMessage > message)
PlatformViewEmbedder(PlatformView::Delegate &delegate, const flutter::TaskRunners &task_runners, const EmbedderSurfaceSoftware::SoftwareDispatchTable &software_dispatch_table, PlatformDispatchTable platform_dispatch_table, std::shared_ptr< EmbedderExternalViewEmbedder > external_view_embedder)
void HandlePlatformMessage(std::unique_ptr< PlatformMessage > message) override
Overridden by embedders to perform actions in response to platform messages sent from the framework t...
void UpdateSemantics(flutter::SemanticsNodeUpdates update, flutter::CustomAccessibilityActionUpdates actions) override
Used by the framework to tell the embedder to apply the specified semantics node updates....
std::shared_ptr< PlatformMessageHandler > GetPlatformMessageHandler() const override
Specifies a delegate that will receive PlatformMessages from Flutter to the host platform.
Used to forward events from the platform view to interested subsystems. This forwarding is done by th...
Definition: platform_view.h:60
Platform views are created by the shell on the platform task runner. Unless explicitly specified,...
Definition: platform_view.h:51
virtual std::unique_ptr< VsyncWaiter > CreateVSyncWaiter()
Invoked by the shell to obtain a platform specific vsync waiter. It is optional for platforms to over...
const TaskRunners task_runners_
virtual void PostTask(const fml::closure &task) override
Definition: task_runner.cc:24
#define FML_DLOG(severity)
Definition: logging.h:102
#define FML_LOG(severity)
Definition: logging.h:82
Win32Message message
std::unordered_map< int32_t, SemanticsNode > SemanticsNodeUpdates
std::unordered_map< int32_t, CustomAccessibilityAction > CustomAccessibilityActionUpdates
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57
Definition: ref_ptr.h:256
Definition: update.py:1
ComputePlatformResolvedLocaleCallback compute_platform_resolved_locale_callback
PlatformMessageResponseCallback platform_message_response_callback
#define ERROR(message)
Definition: elf_loader.cc:260