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
PlatformView(Delegate & delegate,TaskRunners task_runners)19 PlatformView::PlatformView(Delegate& delegate, TaskRunners task_runners)
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
CreateVSyncWaiter()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
DispatchPlatformMessage(fml::RefPtr<PlatformMessage> message)34 void PlatformView::DispatchPlatformMessage(
35 fml::RefPtr<PlatformMessage> message) {
36 delegate_.OnPlatformViewDispatchPlatformMessage(std::move(message));
37 }
38
DispatchPointerDataPacket(std::unique_ptr<PointerDataPacket> packet)39 void PlatformView::DispatchPointerDataPacket(
40 std::unique_ptr<PointerDataPacket> packet) {
41 delegate_.OnPlatformViewDispatchPointerDataPacket(std::move(packet));
42 }
43
DispatchSemanticsAction(int32_t id,SemanticsAction action,std::vector<uint8_t> args)44 void PlatformView::DispatchSemanticsAction(int32_t id,
45 SemanticsAction action,
46 std::vector<uint8_t> args) {
47 delegate_.OnPlatformViewDispatchSemanticsAction(id, action, std::move(args));
48 }
49
SetSemanticsEnabled(bool enabled)50 void PlatformView::SetSemanticsEnabled(bool enabled) {
51 delegate_.OnPlatformViewSetSemanticsEnabled(enabled);
52 }
53
SetAccessibilityFeatures(int32_t flags)54 void PlatformView::SetAccessibilityFeatures(int32_t flags) {
55 delegate_.OnPlatformViewSetAccessibilityFeatures(flags);
56 }
57
SetViewportMetrics(const ViewportMetrics & metrics)58 void PlatformView::SetViewportMetrics(const ViewportMetrics& metrics) {
59 delegate_.OnPlatformViewSetViewportMetrics(metrics);
60 }
61
NotifyCreated()62 void PlatformView::NotifyCreated() {
63 std::unique_ptr<Surface> surface;
64
65 // Threading: We want to use the platform view on the non-platform thread.
66 // Using the weak pointer is illegal. But, we are going to introduce a latch
67 // so that the platform view is not collected till the surface is obtained.
68 auto* platform_view = this;
69 fml::ManualResetWaitableEvent latch;
70 fml::TaskRunner::RunNowOrPostTask(
71 task_runners_.GetGPUTaskRunner(), [platform_view, &surface, &latch]() {
72 surface = platform_view->CreateRenderingSurface();
73 latch.Signal();
74 });
75 latch.Wait();
76 delegate_.OnPlatformViewCreated(std::move(surface));
77 }
78
NotifyDestroyed()79 void PlatformView::NotifyDestroyed() {
80 delegate_.OnPlatformViewDestroyed();
81 }
82
CreateResourceContext() const83 sk_sp<GrContext> PlatformView::CreateResourceContext() const {
84 FML_DLOG(WARNING) << "This platform does not setup the resource "
85 "context on the IO thread for async texture uploads.";
86 return nullptr;
87 }
88
ReleaseResourceContext() const89 void PlatformView::ReleaseResourceContext() const {}
90
GetWeakPtr() const91 fml::WeakPtr<PlatformView> PlatformView::GetWeakPtr() const {
92 return weak_factory_.GetWeakPtr();
93 }
94
UpdateSemantics(SemanticsNodeUpdates update,CustomAccessibilityActionUpdates actions)95 void PlatformView::UpdateSemantics(SemanticsNodeUpdates update,
96 CustomAccessibilityActionUpdates actions) {}
97
HandlePlatformMessage(fml::RefPtr<PlatformMessage> message)98 void PlatformView::HandlePlatformMessage(fml::RefPtr<PlatformMessage> message) {
99 if (auto response = message->response())
100 response->CompleteEmpty();
101 }
102
OnPreEngineRestart() const103 void PlatformView::OnPreEngineRestart() const {}
104
RegisterTexture(std::shared_ptr<flutter::Texture> texture)105 void PlatformView::RegisterTexture(std::shared_ptr<flutter::Texture> texture) {
106 delegate_.OnPlatformViewRegisterTexture(std::move(texture));
107 }
108
UnregisterTexture(int64_t texture_id)109 void PlatformView::UnregisterTexture(int64_t texture_id) {
110 delegate_.OnPlatformViewUnregisterTexture(texture_id);
111 }
112
MarkTextureFrameAvailable(int64_t texture_id)113 void PlatformView::MarkTextureFrameAvailable(int64_t texture_id) {
114 delegate_.OnPlatformViewMarkTextureFrameAvailable(texture_id);
115 }
116
CreateRenderingSurface()117 std::unique_ptr<Surface> PlatformView::CreateRenderingSurface() {
118 // We have a default implementation because tests create a platform view but
119 // never a rendering surface.
120 FML_DCHECK(false) << "This platform does not provide a rendering surface but "
121 "it was notified of surface rendering surface creation.";
122 return nullptr;
123 }
124
SetNextFrameCallback(fml::closure closure)125 void PlatformView::SetNextFrameCallback(fml::closure closure) {
126 if (!closure) {
127 return;
128 }
129
130 delegate_.OnPlatformViewSetNextFrameCallback(std::move(closure));
131 }
132
133 } // namespace flutter
134