Flutter Engine
platform_view.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/common/platform_view.h"
6 
7 #include <utility>
8 
9 #include "flutter/fml/make_copyable.h"
10 #include "flutter/fml/synchronization/waitable_event.h"
11 #include "flutter/shell/common/vsync_waiter_fallback.h"
12 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
13 
14 namespace flutter {
15 
17  : delegate_(delegate),
18  task_runners_(std::move(task_runners)),
19  size_(SkISize::Make(0, 0)),
20  weak_factory_(this) {}
21 
22 PlatformView::~PlatformView() = default;
23 
24 std::unique_ptr<VsyncWaiter> PlatformView::CreateVSyncWaiter() {
25  FML_DLOG(WARNING)
26  << "This platform does not provide a Vsync waiter implementation. A "
27  "simple timer based fallback is being used.";
28  return std::make_unique<VsyncWaiterFallback>(task_runners_);
29 }
30 
32  std::unique_ptr<PlatformMessage> message) {
34 }
35 
37  std::unique_ptr<PointerDataPacket> packet) {
39  pointer_data_packet_converter_.Convert(std::move(packet)));
40 }
41 
42 void PlatformView::DispatchKeyDataPacket(std::unique_ptr<KeyDataPacket> packet,
45  std::move(callback));
46 }
47 
51  delegate_.OnPlatformViewDispatchSemanticsAction(id, action, std::move(args));
52 }
53 
56 }
57 
60 }
61 
64 }
65 
67  std::unique_ptr<Surface> surface;
68  // Threading: We want to use the platform view on the non-platform thread.
69  // Using the weak pointer is illegal. But, we are going to introduce a latch
70  // so that the platform view is not collected till the surface is obtained.
71  auto* platform_view = this;
74  task_runners_.GetRasterTaskRunner(), [platform_view, &surface, &latch]() {
75  surface = platform_view->CreateRenderingSurface();
76  if (surface && !surface->IsValid()) {
77  surface.reset();
78  }
79  latch.Signal();
80  });
81  latch.Wait();
82  if (!surface) {
83  FML_LOG(ERROR) << "Failed to create platform view rendering surface";
84  return;
85  }
86  delegate_.OnPlatformViewCreated(std::move(surface));
87 }
88 
91 }
92 
93 sk_sp<GrDirectContext> PlatformView::CreateResourceContext() const {
94  FML_DLOG(WARNING) << "This platform does not set up the resource "
95  "context on the IO thread for async texture uploads.";
96  return nullptr;
97 }
98 
100 
102  return [](DefaultPointerDataDispatcher::Delegate& delegate) {
103  return std::make_unique<DefaultPointerDataDispatcher>(delegate);
104  };
105 }
106 
108  return weak_factory_.GetWeakPtr();
109 }
110 
113 
115  std::unique_ptr<PlatformMessage> message) {
116  if (auto response = message->response())
117  response->CompleteEmpty();
118 }
119 
121 
122 void PlatformView::RegisterTexture(std::shared_ptr<flutter::Texture> texture) {
123  delegate_.OnPlatformViewRegisterTexture(std::move(texture));
124 }
125 
128 }
129 
132 }
133 
134 std::unique_ptr<Surface> PlatformView::CreateRenderingSurface() {
135  // We have a default implementation because tests create a platform view but
136  // never a rendering surface.
137  FML_DCHECK(false) << "This platform does not provide a rendering surface but "
138  "it was notified of surface rendering surface creation.";
139  return nullptr;
140 }
141 
142 std::shared_ptr<ExternalViewEmbedder>
144  FML_DLOG(WARNING)
145  << "This platform doesn't support embedding external views.";
146  return nullptr;
147 }
148 
150  if (!closure) {
151  return;
152  }
153 
155 }
156 
157 std::unique_ptr<std::vector<std::string>>
159  const std::vector<std::string>& supported_locale_data) {
160  std::unique_ptr<std::vector<std::string>> out =
161  std::make_unique<std::vector<std::string>>();
162  return out;
163 }
164 
165 void PlatformView::RequestDartDeferredLibrary(intptr_t loading_unit_id) {}
166 
168  intptr_t loading_unit_id,
169  std::unique_ptr<const fml::Mapping> snapshot_data,
170  std::unique_ptr<const fml::Mapping> snapshot_instructions) {}
171 
172 void PlatformView::LoadDartDeferredLibraryError(intptr_t loading_unit_id,
173  const std::string error_message,
174  bool transient) {}
175 
177  std::unique_ptr<AssetResolver> updated_asset_resolver,
179  delegate_.UpdateAssetResolverByType(std::move(updated_asset_resolver), type);
180 }
181 
182 std::unique_ptr<SnapshotSurfaceProducer>
184  return nullptr;
185 }
186 
187 } // namespace flutter
G_BEGIN_DECLS FlValue * args
virtual void OnPreEngineRestart() const
Gives embedders a chance to react to a "cold restart" of the running isolate. The default implementat...
KeyCallType type
fml::WeakPtrFactory< PlatformView > weak_factory_
G_BEGIN_DECLS FlTexture * texture
void MarkTextureFrameAvailable(int64_t texture_id)
Used by the embedder to notify the rasterizer that the context of the previously registered texture h...
void DispatchSemanticsAction(int32_t id, SemanticsAction action, fml::MallocMapping args)
Used by embedders to dispatch an accessibility action to a running isolate hosted by the engine...
MockDelegate delegate_
virtual std::unique_ptr< std::vector< std::string > > ComputePlatformResolvedLocales(const std::vector< std::string > &supported_locale_data)
Directly invokes platform-specific APIs to compute the locale the platform would have natively resolv...
#define FML_DCHECK(condition)
Definition: logging.h:86
std::unique_ptr< flutter::PlatformViewIOS > platform_view
virtual void LoadDartDeferredLibrary(intptr_t loading_unit_id, std::unique_ptr< const fml::Mapping > snapshot_data, std::unique_ptr< const fml::Mapping > snapshot_instructions)
Loads the Dart shared library into the Dart VM. When the Dart library is loaded successfully, the Dart future returned by the originating loadLibrary() call completes.
const TaskRunners task_runners_
virtual void OnPlatformViewSetSemanticsEnabled(bool enabled)=0
Notifies the delegate that the embedder has expressed an opinion about whether the accessibility tree...
virtual std::unique_ptr< VsyncWaiter > CreateVSyncWaiter()
Invoked by the shell to obtain a platform specific vsync waiter. It is optional for platforms to over...
virtual void OnPlatformViewDispatchPlatformMessage(std::unique_ptr< PlatformMessage > message)=0
Notifies the delegate that the platform has dispatched a platform message from the embedder to the Fl...
virtual void SetAccessibilityFeatures(int32_t flags)
Used by the embedder to specify the features to enable in the accessibility tree generated by the iso...
static void RunNowOrPostTask(fml::RefPtr< fml::TaskRunner > runner, const fml::closure &task)
Definition: task_runner.cc:55
virtual void OnPlatformViewMarkTextureFrameAvailable(int64_t texture_id)=0
Notifies the delegate that the embedder has updated the contents of the texture with the specified id...
PlatformView(Delegate &delegate, TaskRunners task_runners)
Creates a platform view with the specified delegate and task runner. The base class by itself does no...
std::unique_ptr< PointerDataPacket > Convert(std::unique_ptr< PointerDataPacket > packet)
Converts pointer data packet into a form that framework understands. The raw pointer data packet from...
Definition: ref_ptr.h:252
The interface for Engine to implement.
std::unordered_map< int32_t, SemanticsNode > SemanticsNodeUpdates
int64_t texture_id
virtual void UpdateSemantics(SemanticsNodeUpdates updates, CustomAccessibilityActionUpdates actions)
Used by the framework to tell the embedder to apply the specified semantics node updates. The default implementation of this method does nothing.
#define FML_LOG(severity)
Definition: logging.h:65
std::function< std::unique_ptr< PointerDataDispatcher >(PointerDataDispatcher::Delegate &)> PointerDataDispatcherMaker
Signature for constructing PointerDataDispatcher.
virtual std::unique_ptr< SnapshotSurfaceProducer > CreateSnapshotSurfaceProducer()
Creates an object that produces surfaces suitable for raster snapshotting. The rasterizer will reques...
fml::RefPtr< fml::TaskRunner > GetRasterTaskRunner() const
Definition: task_runners.cc:42
FlKeyEvent FlKeyResponderAsyncCallback callback
void SetNextFrameCallback(const fml::closure &closure)
Sets a callback that gets executed when the rasterizer renders the next frame. Due to the asynchronou...
void DispatchPointerDataPacket(std::unique_ptr< PointerDataPacket > packet)
Dispatches pointer events from the embedder to the framework. Each pointer data packet may contain mu...
virtual void OnPlatformViewRegisterTexture(std::shared_ptr< Texture > texture)=0
Notifies the delegate that the embedder has specified a texture that it want the rasterizer to compos...
virtual ~PlatformView()
Destroys the platform view. The platform view is owned by the shell and will be destroyed by the same...
AssetResolverType
Identifies the type of AssetResolver an instance is.
virtual PointerDataDispatcherMaker GetDispatcherMaker()
Returns a platform-specific PointerDataDispatcherMaker so the Engine can construct the PointerDataPac...
std::function< void()> closure
Definition: closure.h:14
virtual void ReleaseResourceContext() const
Used by the shell to notify the embedder that the resource context previously obtained via a call to ...
virtual void SetSemanticsEnabled(bool enabled)
Used by embedder to notify the running isolate hosted by the engine on the UI thread that the accessi...
void DispatchPlatformMessage(std::unique_ptr< PlatformMessage > message)
Used by embedders to dispatch a platform message to a running root isolate hosted by the engine...
void DispatchKeyDataPacket(std::unique_ptr< KeyDataPacket > packet, Delegate::KeyDataResponse callback)
Dispatches key events from the embedder to the framework. Each key data packet contains one physical ...
PlatformView::Delegate & delegate_
virtual void RequestDartDeferredLibrary(intptr_t loading_unit_id)
Invoked when the dart VM requests that a deferred library be loaded. Notifies the engine that the def...
SemanticsAction action
virtual void OnPlatformViewDispatchSemanticsAction(int32_t id, SemanticsAction action, fml::MallocMapping args)=0
Notifies the delegate that the platform view has encountered an accessibility related action on the s...
virtual sk_sp< GrDirectContext > CreateResourceContext() const
Used by the shell to obtain a Skia GPU context that is capable of operating on the IO thread...
virtual void UpdateAssetResolverByType(std::unique_ptr< AssetResolver > updated_asset_resolver, AssetResolver::AssetResolverType type)=0
Replaces the asset resolver handled by the engine&#39;s AssetManager of the specified type with updated_a...
void SetViewportMetrics(const ViewportMetrics &metrics)
Used by embedders to specify the updated viewport metrics. In response to this call, on the raster thread, the rasterizer may need to be reconfigured to the updated viewport dimensions. On the UI thread, the framework may need to start generating a new frame for the updated viewport metrics as well.
virtual void OnPlatformViewDestroyed()=0
Notifies the delegate that the platform view was destroyed. This is usually a sign to the rasterizer ...
PointerDataPacketConverter pointer_data_packet_converter_
virtual void OnPlatformViewCreated(std::unique_ptr< Surface > surface)=0
Notifies the delegate that the platform view was created with the given render surface. This surface is platform (iOS, Android) and client-rendering API (OpenGL, Software, Metal, Vulkan) specific. This is usually a sign to the rasterizer to set up and begin rendering to that surface.
virtual void OnPlatformViewSetAccessibilityFeatures(int32_t flags)=0
Notifies the delegate that the embedder has expressed an opinion about the features to enable in the ...
void NotifyCreated()
Used by embedders to notify the shell that a platform view has been created. This notification is use...
virtual void LoadDartDeferredLibraryError(intptr_t loading_unit_id, const std::string error_message, bool transient)
Indicates to the dart VM that the request to load a deferred library with the specified loading unit ...
virtual void OnPlatformViewDispatchPointerDataPacket(std::unique_ptr< PointerDataPacket > packet)=0
Notifies the delegate that the platform view has encountered a pointer event. This pointer event need...
fml::WeakPtr< PlatformView > GetWeakPtr() const
Returns a weak pointer to the platform view. Since the platform view may only be created, accessed and destroyed on the platform thread, any access to the platform view from a non-platform task runner needs a weak pointer to the platform view along with a reference to the platform task runner. A task must be posted to the platform task runner with the weak pointer captured in the same. The platform view method may only be called in the posted task once the weak pointer validity has been checked. This method is used by callers to obtain that weak pointer.
virtual void NotifyDestroyed()
Used by embedders to notify the shell that the platform view has been destroyed. This notification us...
virtual std::unique_ptr< Surface > CreateRenderingSurface()
void RegisterTexture(std::shared_ptr< flutter::Texture > texture)
Used by the embedder to specify a texture that it wants the rasterizer to composite within the Flutte...
virtual void UpdateAssetResolverByType(std::unique_ptr< AssetResolver > updated_asset_resolver, AssetResolver::AssetResolverType type)
Replaces the asset resolver handled by the engine&#39;s AssetManager of the specified type with updated_a...
A Mapping like NonOwnedMapping, but uses Free as its release proc.
Definition: mapping.h:129
TaskRunners task_runners_
std::function< void(bool)> KeyDataResponse
#define FML_DLOG(severity)
Definition: logging.h:85
std::unordered_map< int32_t, CustomAccessibilityAction > CustomAccessibilityActionUpdates
virtual void OnPlatformViewSetNextFrameCallback(const fml::closure &closure)=0
Notifies the delegate that the specified callback needs to be invoked after the rasterizer is done re...
Used to forward events from the platform view to interested subsystems. This forwarding is done by th...
Definition: platform_view.h:53
void UnregisterTexture(int64_t texture_id)
Used by the embedder to notify the rasterizer that it will no longer attempt to composite the specifi...
virtual void HandlePlatformMessage(std::unique_ptr< PlatformMessage > message)
Overridden by embedders to perform actions in response to platform messages sent from the framework t...
virtual void OnPlatformViewUnregisterTexture(int64_t texture_id)=0
Notifies the delegate that the embedder will no longer attempt to composite the specified texture wit...
virtual void OnPlatformViewSetViewportMetrics(const ViewportMetrics &metrics)=0
Notifies the delegate the viewport metrics of the platform view have been updated. The rasterizer will need to be reconfigured to render the frame in the updated viewport metrics.
virtual void OnPlatformViewDispatchKeyDataPacket(std::unique_ptr< KeyDataPacket > packet, std::function< void(bool)> callback)=0
Notifies the delegate that the platform view has encountered a key event. This key event and the call...
DEF_SWITCHES_START snapshot asset Path to the directory containing the four files specified by VmSnapshotInstructions and IsolateSnapshotInstructions vm snapshot The VM instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present isolate snapshot The isolate instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present icu symbol Prefix for the symbols representing ICU data linked into the Flutter library dart flags
Definition: switches.h:66
virtual std::shared_ptr< ExternalViewEmbedder > CreateExternalViewEmbedder()