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 | // FLUTTER_NOLINT |
5 | |
6 | #include "flutter/shell/common/platform_view.h" |
7 | |
8 | #include <utility> |
9 | |
10 | #include "flutter/fml/make_copyable.h" |
11 | #include "flutter/fml/synchronization/waitable_event.h" |
12 | #include "flutter/shell/common/rasterizer.h" |
13 | #include "flutter/shell/common/shell.h" |
14 | #include "flutter/shell/common/vsync_waiter_fallback.h" |
15 | #include "third_party/skia/include/gpu/GrContextOptions.h" |
16 | #include "third_party/skia/include/gpu/gl/GrGLInterface.h" |
17 | |
18 | namespace flutter { |
19 | |
20 | PlatformView::PlatformView(Delegate& delegate, TaskRunners task_runners) |
21 | : delegate_(delegate), |
22 | task_runners_(std::move(task_runners)), |
23 | size_(SkISize::Make(0, 0)), |
24 | weak_factory_(this) {} |
25 | |
26 | PlatformView::~PlatformView() = default; |
27 | |
28 | std::unique_ptr<VsyncWaiter> PlatformView::CreateVSyncWaiter() { |
29 | FML_DLOG(WARNING) |
30 | << "This platform does not provide a Vsync waiter implementation. A " |
31 | "simple timer based fallback is being used." ; |
32 | return std::make_unique<VsyncWaiterFallback>(task_runners_); |
33 | } |
34 | |
35 | void PlatformView::DispatchPlatformMessage( |
36 | fml::RefPtr<PlatformMessage> message) { |
37 | delegate_.OnPlatformViewDispatchPlatformMessage(std::move(message)); |
38 | } |
39 | |
40 | void PlatformView::DispatchPointerDataPacket( |
41 | std::unique_ptr<PointerDataPacket> packet) { |
42 | delegate_.OnPlatformViewDispatchPointerDataPacket( |
43 | pointer_data_packet_converter_.Convert(std::move(packet))); |
44 | } |
45 | |
46 | void PlatformView::DispatchSemanticsAction(int32_t id, |
47 | SemanticsAction action, |
48 | std::vector<uint8_t> args) { |
49 | delegate_.OnPlatformViewDispatchSemanticsAction(id, action, std::move(args)); |
50 | } |
51 | |
52 | void PlatformView::SetSemanticsEnabled(bool enabled) { |
53 | delegate_.OnPlatformViewSetSemanticsEnabled(enabled); |
54 | } |
55 | |
56 | void PlatformView::SetAccessibilityFeatures(int32_t flags) { |
57 | delegate_.OnPlatformViewSetAccessibilityFeatures(flags); |
58 | } |
59 | |
60 | void PlatformView::SetViewportMetrics(const ViewportMetrics& metrics) { |
61 | delegate_.OnPlatformViewSetViewportMetrics(metrics); |
62 | } |
63 | |
64 | void PlatformView::NotifyCreated() { |
65 | std::unique_ptr<Surface> surface; |
66 | |
67 | // Threading: We want to use the platform view on the non-platform thread. |
68 | // Using the weak pointer is illegal. But, we are going to introduce a latch |
69 | // so that the platform view is not collected till the surface is obtained. |
70 | auto* platform_view = this; |
71 | fml::ManualResetWaitableEvent latch; |
72 | fml::TaskRunner::RunNowOrPostTask( |
73 | task_runners_.GetRasterTaskRunner(), [platform_view, &surface, &latch]() { |
74 | surface = platform_view->CreateRenderingSurface(); |
75 | latch.Signal(); |
76 | }); |
77 | latch.Wait(); |
78 | delegate_.OnPlatformViewCreated(std::move(surface)); |
79 | } |
80 | |
81 | void PlatformView::NotifyDestroyed() { |
82 | delegate_.OnPlatformViewDestroyed(); |
83 | } |
84 | |
85 | sk_sp<GrDirectContext> PlatformView::CreateResourceContext() const { |
86 | FML_DLOG(WARNING) << "This platform does not setup the resource " |
87 | "context on the IO thread for async texture uploads." ; |
88 | return nullptr; |
89 | } |
90 | |
91 | void PlatformView::ReleaseResourceContext() const {} |
92 | |
93 | PointerDataDispatcherMaker PlatformView::GetDispatcherMaker() { |
94 | return [](DefaultPointerDataDispatcher::Delegate& delegate) { |
95 | return std::make_unique<DefaultPointerDataDispatcher>(delegate); |
96 | }; |
97 | } |
98 | |
99 | fml::WeakPtr<PlatformView> PlatformView::GetWeakPtr() const { |
100 | return weak_factory_.GetWeakPtr(); |
101 | } |
102 | |
103 | void PlatformView::UpdateSemantics(SemanticsNodeUpdates update, |
104 | CustomAccessibilityActionUpdates actions) {} |
105 | |
106 | void PlatformView::HandlePlatformMessage(fml::RefPtr<PlatformMessage> message) { |
107 | if (auto response = message->response()) |
108 | response->CompleteEmpty(); |
109 | } |
110 | |
111 | void PlatformView::OnPreEngineRestart() const {} |
112 | |
113 | void PlatformView::RegisterTexture(std::shared_ptr<flutter::Texture> texture) { |
114 | delegate_.OnPlatformViewRegisterTexture(std::move(texture)); |
115 | } |
116 | |
117 | void PlatformView::UnregisterTexture(int64_t texture_id) { |
118 | delegate_.OnPlatformViewUnregisterTexture(texture_id); |
119 | } |
120 | |
121 | void PlatformView::MarkTextureFrameAvailable(int64_t texture_id) { |
122 | delegate_.OnPlatformViewMarkTextureFrameAvailable(texture_id); |
123 | } |
124 | |
125 | std::unique_ptr<Surface> PlatformView::CreateRenderingSurface() { |
126 | // We have a default implementation because tests create a platform view but |
127 | // never a rendering surface. |
128 | FML_DCHECK(false) << "This platform does not provide a rendering surface but " |
129 | "it was notified of surface rendering surface creation." ; |
130 | return nullptr; |
131 | } |
132 | |
133 | void PlatformView::SetNextFrameCallback(const fml::closure& closure) { |
134 | if (!closure) { |
135 | return; |
136 | } |
137 | |
138 | delegate_.OnPlatformViewSetNextFrameCallback(closure); |
139 | } |
140 | |
141 | std::unique_ptr<std::vector<std::string>> |
142 | PlatformView::ComputePlatformResolvedLocales( |
143 | const std::vector<std::string>& supported_locale_data) { |
144 | std::unique_ptr<std::vector<std::string>> out = |
145 | std::make_unique<std::vector<std::string>>(); |
146 | return out; |
147 | } |
148 | |
149 | } // namespace flutter |
150 | |