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/rasterizer.h"
12 #include "flutter/shell/common/shell.h"
13 #include "flutter/shell/common/vsync_waiter_fallback.h"
14 #include "third_party/skia/include/gpu/GrContextOptions.h"
15 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
16 
17 namespace flutter {
18 
20  : delegate_(delegate),
21  task_runners_(std::move(task_runners)),
22  size_(SkISize::Make(0, 0)),
23  weak_factory_(this) {}
24 
25 PlatformView::~PlatformView() = default;
26 
27 std::unique_ptr<VsyncWaiter> PlatformView::CreateVSyncWaiter() {
28  FML_DLOG(WARNING)
29  << "This platform does not provide a Vsync waiter implementation. A "
30  "simple timer based fallback is being used.";
31  return std::make_unique<VsyncWaiterFallback>(task_runners_);
32 }
33 
37 }
38 
40  std::unique_ptr<PointerDataPacket> packet) {
42  pointer_data_packet_converter_.Convert(std::move(packet)));
43 }
44 
47  std::vector<uint8_t> args) {
48  delegate_.OnPlatformViewDispatchSemanticsAction(id, action, std::move(args));
49 }
50 
53 }
54 
57 }
58 
61 }
62 
64  std::unique_ptr<Surface> surface;
65 
66  // Threading: We want to use the platform view on the non-platform thread.
67  // Using the weak pointer is illegal. But, we are going to introduce a latch
68  // so that the platform view is not collected till the surface is obtained.
69  auto* platform_view = this;
72  task_runners_.GetRasterTaskRunner(), [platform_view, &surface, &latch]() {
73  surface = platform_view->CreateRenderingSurface();
74  if (surface && !surface->IsValid()) {
75  surface.reset();
76  }
77  latch.Signal();
78  });
79  latch.Wait();
80  if (!surface) {
81  FML_LOG(ERROR) << "Failed to create platform view rendering surface";
82  return;
83  }
84  delegate_.OnPlatformViewCreated(std::move(surface));
85 }
86 
89 }
90 
91 sk_sp<GrDirectContext> PlatformView::CreateResourceContext() const {
92  FML_DLOG(WARNING) << "This platform does not setup the resource "
93  "context on the IO thread for async texture uploads.";
94  return nullptr;
95 }
96 
98 
100  return [](DefaultPointerDataDispatcher::Delegate& delegate) {
101  return std::make_unique<DefaultPointerDataDispatcher>(delegate);
102  };
103 }
104 
106  return weak_factory_.GetWeakPtr();
107 }
108 
111 
113  if (auto response = message->response())
114  response->CompleteEmpty();
115 }
116 
118 
119 void PlatformView::RegisterTexture(std::shared_ptr<flutter::Texture> texture) {
120  delegate_.OnPlatformViewRegisterTexture(std::move(texture));
121 }
122 
123 void PlatformView::UnregisterTexture(int64_t texture_id) {
125 }
126 
127 void PlatformView::MarkTextureFrameAvailable(int64_t texture_id) {
129 }
130 
131 std::unique_ptr<Surface> PlatformView::CreateRenderingSurface() {
132  // We have a default implementation because tests create a platform view but
133  // never a rendering surface.
134  FML_DCHECK(false) << "This platform does not provide a rendering surface but "
135  "it was notified of surface rendering surface creation.";
136  return nullptr;
137 }
138 
140  if (!closure) {
141  return;
142  }
143 
145 }
146 
147 std::unique_ptr<std::vector<std::string>>
149  const std::vector<std::string>& supported_locale_data) {
150  std::unique_ptr<std::vector<std::string>> out =
151  std::make_unique<std::vector<std::string>>();
152  return out;
153 }
154 
155 } // namespace flutter
virtual void OnPlatformViewDispatchPlatformMessage(fml::RefPtr< PlatformMessage > message)=0
Notifies the delegate that the platform has dispatched a platform message from the embedder to the Fl...
virtual void OnPreEngineRestart() const
Gives embedders a chance to react to a "cold restart" of the running isolate. The default implementat...
fml::WeakPtrFactory< PlatformView > weak_factory_
void MarkTextureFrameAvailable(int64_t texture_id)
Used by the embedder to notify the rasterizer that the context of the previously registered texture h...
virtual void HandlePlatformMessage(fml::RefPtr< PlatformMessage > message)
Overridden by embedders to perform actions in response to platform messages sent from the framework t...
void DispatchPlatformMessage(fml::RefPtr< PlatformMessage > message)
Used by embedders to dispatch a platform message to a running root isolate hosted by the engine...
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
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 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
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.
fml::RefPtr< fml::TaskRunner > GetRasterTaskRunner() const
Definition: task_runners.cc:42
virtual void OnPlatformViewDispatchSemanticsAction(int32_t id, SemanticsAction action, std::vector< uint8_t > args)=0
Notifies the delegate that the platform view has encountered an accessibility related action on the s...
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...
TaskRunners task_runners_
virtual ~PlatformView()
Destroys the platform view. The platform view is owned by the shell and will be destroyed by the same...
MockDelegate delegate_
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...
PlatformView::Delegate & delegate_
SemanticsAction action
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...
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_
void DispatchSemanticsAction(int32_t id, SemanticsAction action, std::vector< uint8_t > args)
Used by embedders to dispatch an accessibility action to a running isolate hosted by the engine...
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 setup 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 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...
#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:51
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 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.
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