blob: df4101109a18d60684e8e66f4bf4b8de541de82f [file] [log] [blame]
John Reck283bb462018-12-13 16:40:14 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "WebViewFunctorManager.h"
18
19#include <private/hwui/WebViewFunctor.h>
20#include "Properties.h"
Huihong Luo054b8d32021-02-24 18:48:12 -080021#include "renderthread/CanvasContext.h"
Bo Liu1b0278c2019-01-03 16:36:24 -080022#include "renderthread/RenderThread.h"
John Reck283bb462018-12-13 16:40:14 -080023
24#include <log/log.h>
25#include <utils/Trace.h>
26#include <atomic>
27
28namespace android::uirenderer {
29
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -050030namespace {
31class ScopedCurrentFunctor {
32public:
33 ScopedCurrentFunctor(WebViewFunctor* functor) {
34 ALOG_ASSERT(!sCurrentFunctor);
35 ALOG_ASSERT(functor);
36 sCurrentFunctor = functor;
37 }
38 ~ScopedCurrentFunctor() {
39 ALOG_ASSERT(sCurrentFunctor);
40 sCurrentFunctor = nullptr;
41 }
42
43 static ASurfaceControl* getSurfaceControl() {
44 ALOG_ASSERT(sCurrentFunctor);
45 return sCurrentFunctor->getSurfaceControl();
46 }
47 static void mergeTransaction(ASurfaceTransaction* transaction) {
48 ALOG_ASSERT(sCurrentFunctor);
49 sCurrentFunctor->mergeTransaction(transaction);
50 }
51
52private:
53 static WebViewFunctor* sCurrentFunctor;
54};
55
56WebViewFunctor* ScopedCurrentFunctor::sCurrentFunctor = nullptr;
57} // namespace
58
John Reck283bb462018-12-13 16:40:14 -080059RenderMode WebViewFunctor_queryPlatformRenderMode() {
60 auto pipelineType = Properties::getRenderPipelineType();
61 switch (pipelineType) {
62 case RenderPipelineType::SkiaGL:
63 return RenderMode::OpenGL_ES;
64 case RenderPipelineType::SkiaVulkan:
65 return RenderMode::Vulkan;
66 default:
67 LOG_ALWAYS_FATAL("Unknown render pipeline type: %d", (int)pipelineType);
68 }
69}
70
Bo Liud6668e72018-12-14 19:37:41 -080071int WebViewFunctor_create(void* data, const WebViewFunctorCallbacks& prototype,
72 RenderMode functorMode) {
John Reck283bb462018-12-13 16:40:14 -080073 if (functorMode != RenderMode::OpenGL_ES && functorMode != RenderMode::Vulkan) {
74 ALOGW("Unknown rendermode %d", (int)functorMode);
75 return -1;
76 }
77 if (functorMode == RenderMode::Vulkan &&
78 WebViewFunctor_queryPlatformRenderMode() != RenderMode::Vulkan) {
79 ALOGW("Unable to map from GLES platform to a vulkan functor");
80 return -1;
81 }
Bo Liud6668e72018-12-14 19:37:41 -080082 return WebViewFunctorManager::instance().createFunctor(data, prototype, functorMode);
John Reck283bb462018-12-13 16:40:14 -080083}
84
85void WebViewFunctor_release(int functor) {
86 WebViewFunctorManager::instance().releaseFunctor(functor);
87}
88
89static std::atomic_int sNextId{1};
90
Bo Liud6668e72018-12-14 19:37:41 -080091WebViewFunctor::WebViewFunctor(void* data, const WebViewFunctorCallbacks& callbacks,
92 RenderMode functorMode)
93 : mData(data) {
John Reck283bb462018-12-13 16:40:14 -080094 mFunctor = sNextId++;
95 mCallbacks = callbacks;
96 mMode = functorMode;
97}
98
99WebViewFunctor::~WebViewFunctor() {
100 destroyContext();
101
102 ATRACE_NAME("WebViewFunctor::onDestroy");
Bo Liud6668e72018-12-14 19:37:41 -0800103 mCallbacks.onDestroyed(mFunctor, mData);
John Reck283bb462018-12-13 16:40:14 -0800104}
105
106void WebViewFunctor::sync(const WebViewSyncData& syncData) const {
107 ATRACE_NAME("WebViewFunctor::sync");
Bo Liud6668e72018-12-14 19:37:41 -0800108 mCallbacks.onSync(mFunctor, mData, syncData);
John Reck283bb462018-12-13 16:40:14 -0800109}
110
Huihong Luoec68b7c2021-06-25 21:54:16 -0700111void WebViewFunctor::onRemovedFromTree() {
112 ATRACE_NAME("WebViewFunctor::onRemovedFromTree");
113 if (mSurfaceControl) {
114 removeOverlays();
115 }
116}
117
John Reck283bb462018-12-13 16:40:14 -0800118void WebViewFunctor::drawGl(const DrawGlInfo& drawInfo) {
119 ATRACE_NAME("WebViewFunctor::drawGl");
120 if (!mHasContext) {
121 mHasContext = true;
122 }
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500123 ScopedCurrentFunctor currentFunctor(this);
124
125 WebViewOverlayData overlayParams = {
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500126 .overlaysMode = OverlaysMode::Disabled,
127 .getSurfaceControl = currentFunctor.getSurfaceControl,
128 .mergeTransaction = currentFunctor.mergeTransaction,
129 };
Huihong Luo054b8d32021-02-24 18:48:12 -0800130
Huihong Luoeb931072021-06-30 10:12:17 -0700131 if (Properties::enableWebViewOverlays && !drawInfo.isLayer) {
Huihong Luo054b8d32021-02-24 18:48:12 -0800132 renderthread::CanvasContext* activeContext =
133 renderthread::CanvasContext::getActiveContext();
134 if (activeContext != nullptr) {
135 ASurfaceControl* rootSurfaceControl = activeContext->getSurfaceControl();
Huihong Luo540fdf82021-06-25 13:59:39 -0700136 if (rootSurfaceControl) {
137 overlayParams.overlaysMode = OverlaysMode::Enabled;
138 int32_t rgid = activeContext->getSurfaceControlGenerationId();
139 if (mParentSurfaceControlGenerationId != rgid) {
140 reparentSurfaceControl(rootSurfaceControl);
141 mParentSurfaceControlGenerationId = rgid;
142 }
143 }
Huihong Luo054b8d32021-02-24 18:48:12 -0800144 }
145 }
146
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500147 mCallbacks.gles.draw(mFunctor, mData, drawInfo, overlayParams);
John Reck283bb462018-12-13 16:40:14 -0800148}
149
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800150void WebViewFunctor::initVk(const VkFunctorInitParams& params) {
151 ATRACE_NAME("WebViewFunctor::initVk");
152 if (!mHasContext) {
153 mHasContext = true;
154 } else {
155 return;
156 }
157 mCallbacks.vk.initialize(mFunctor, mData, params);
158}
159
160void WebViewFunctor::drawVk(const VkFunctorDrawParams& params) {
161 ATRACE_NAME("WebViewFunctor::drawVk");
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500162 ScopedCurrentFunctor currentFunctor(this);
163
164 WebViewOverlayData overlayParams = {
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500165 .overlaysMode = OverlaysMode::Disabled,
166 .getSurfaceControl = currentFunctor.getSurfaceControl,
167 .mergeTransaction = currentFunctor.mergeTransaction,
168 };
Huihong Luo054b8d32021-02-24 18:48:12 -0800169
170 // TODO, enable surface control once offscreen mode figured out
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500171 mCallbacks.vk.draw(mFunctor, mData, params, overlayParams);
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800172}
173
174void WebViewFunctor::postDrawVk() {
175 ATRACE_NAME("WebViewFunctor::postDrawVk");
176 mCallbacks.vk.postDraw(mFunctor, mData);
177}
178
John Reck283bb462018-12-13 16:40:14 -0800179void WebViewFunctor::destroyContext() {
180 if (mHasContext) {
181 mHasContext = false;
182 ATRACE_NAME("WebViewFunctor::onContextDestroyed");
Bo Liud6668e72018-12-14 19:37:41 -0800183 mCallbacks.onContextDestroyed(mFunctor, mData);
Bo Liu1b0278c2019-01-03 16:36:24 -0800184
185 // grContext may be null in unit tests.
186 auto* grContext = renderthread::RenderThread::getInstance().getGrContext();
187 if (grContext) grContext->resetContext();
John Reck283bb462018-12-13 16:40:14 -0800188 }
189}
190
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500191void WebViewFunctor::removeOverlays() {
192 ScopedCurrentFunctor currentFunctor(this);
193 mCallbacks.removeOverlays(mFunctor, mData, currentFunctor.mergeTransaction);
Huihong Luo054b8d32021-02-24 18:48:12 -0800194 if (mSurfaceControl) {
Huihong Luoec68b7c2021-06-25 21:54:16 -0700195 reparentSurfaceControl(nullptr);
Huihong Luo054b8d32021-02-24 18:48:12 -0800196 auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions();
197 funcs.releaseFunc(mSurfaceControl);
198 mSurfaceControl = nullptr;
199 }
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500200}
201
202ASurfaceControl* WebViewFunctor::getSurfaceControl() {
Huihong Luo054b8d32021-02-24 18:48:12 -0800203 ATRACE_NAME("WebViewFunctor::getSurfaceControl");
204 if (mSurfaceControl != nullptr) return mSurfaceControl;
205
206 renderthread::CanvasContext* activeContext = renderthread::CanvasContext::getActiveContext();
207 LOG_ALWAYS_FATAL_IF(activeContext == nullptr, "Null active canvas context!");
208
209 ASurfaceControl* rootSurfaceControl = activeContext->getSurfaceControl();
210 LOG_ALWAYS_FATAL_IF(rootSurfaceControl == nullptr, "Null root surface control!");
211
212 auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions();
Huihong Luo540fdf82021-06-25 13:59:39 -0700213 mParentSurfaceControlGenerationId = activeContext->getSurfaceControlGenerationId();
Huihong Luo054b8d32021-02-24 18:48:12 -0800214 mSurfaceControl = funcs.createFunc(rootSurfaceControl, "Webview Overlay SurfaceControl");
215 ASurfaceTransaction* transaction = funcs.transactionCreateFunc();
Huihong Luo34f42fd2021-05-03 14:47:36 -0700216 activeContext->prepareSurfaceControlForWebview();
217 funcs.transactionSetZOrderFunc(transaction, mSurfaceControl, -1);
Huihong Luo054b8d32021-02-24 18:48:12 -0800218 funcs.transactionSetVisibilityFunc(transaction, mSurfaceControl,
219 ASURFACE_TRANSACTION_VISIBILITY_SHOW);
220 funcs.transactionApplyFunc(transaction);
221 funcs.transactionDeleteFunc(transaction);
222 return mSurfaceControl;
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500223}
224
225void WebViewFunctor::mergeTransaction(ASurfaceTransaction* transaction) {
Huihong Luo054b8d32021-02-24 18:48:12 -0800226 ATRACE_NAME("WebViewFunctor::mergeTransaction");
227 if (transaction == nullptr) return;
Huihong Luoec68b7c2021-06-25 21:54:16 -0700228 bool done = false;
Huihong Luo054b8d32021-02-24 18:48:12 -0800229 renderthread::CanvasContext* activeContext = renderthread::CanvasContext::getActiveContext();
Huihong Luoec68b7c2021-06-25 21:54:16 -0700230 // activeContext might be null when called from mCallbacks.removeOverlays()
231 if (activeContext != nullptr) {
232 done = activeContext->mergeTransaction(transaction, mSurfaceControl);
233 }
Huihong Luo054b8d32021-02-24 18:48:12 -0800234 if (!done) {
235 auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions();
236 funcs.transactionApplyFunc(transaction);
237 }
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500238}
239
Huihong Luo540fdf82021-06-25 13:59:39 -0700240void WebViewFunctor::reparentSurfaceControl(ASurfaceControl* parent) {
241 ATRACE_NAME("WebViewFunctor::reparentSurfaceControl");
242 if (mSurfaceControl == nullptr) return;
243
244 auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions();
245 ASurfaceTransaction* transaction = funcs.transactionCreateFunc();
246 funcs.transactionReparentFunc(transaction, mSurfaceControl, parent);
247 mergeTransaction(transaction);
248 funcs.transactionDeleteFunc(transaction);
249}
250
John Reck283bb462018-12-13 16:40:14 -0800251WebViewFunctorManager& WebViewFunctorManager::instance() {
252 static WebViewFunctorManager sInstance;
253 return sInstance;
254}
255
John Reckfaa1b0a2021-05-13 10:28:38 -0400256static void validateCallbacks(const WebViewFunctorCallbacks& callbacks) {
257 // TODO: Should we do a stack peek to see if this is really webview?
258 LOG_ALWAYS_FATAL_IF(callbacks.onSync == nullptr, "onSync is null");
259 LOG_ALWAYS_FATAL_IF(callbacks.onContextDestroyed == nullptr, "onContextDestroyed is null");
260 LOG_ALWAYS_FATAL_IF(callbacks.onDestroyed == nullptr, "onDestroyed is null");
261 LOG_ALWAYS_FATAL_IF(callbacks.removeOverlays == nullptr, "removeOverlays is null");
262 switch (auto mode = WebViewFunctor_queryPlatformRenderMode()) {
263 case RenderMode::OpenGL_ES:
264 LOG_ALWAYS_FATAL_IF(callbacks.gles.draw == nullptr, "gles.draw is null");
265 break;
266 case RenderMode::Vulkan:
267 LOG_ALWAYS_FATAL_IF(callbacks.vk.initialize == nullptr, "vk.initialize is null");
268 LOG_ALWAYS_FATAL_IF(callbacks.vk.draw == nullptr, "vk.draw is null");
269 LOG_ALWAYS_FATAL_IF(callbacks.vk.postDraw == nullptr, "vk.postDraw is null");
270 break;
271 default:
272 LOG_ALWAYS_FATAL("unknown platform mode? %d", (int)mode);
273 break;
274 }
275}
276
Bo Liud6668e72018-12-14 19:37:41 -0800277int WebViewFunctorManager::createFunctor(void* data, const WebViewFunctorCallbacks& callbacks,
John Reck283bb462018-12-13 16:40:14 -0800278 RenderMode functorMode) {
John Reckfaa1b0a2021-05-13 10:28:38 -0400279 validateCallbacks(callbacks);
Bo Liud6668e72018-12-14 19:37:41 -0800280 auto object = std::make_unique<WebViewFunctor>(data, callbacks, functorMode);
John Reck283bb462018-12-13 16:40:14 -0800281 int id = object->id();
282 auto handle = object->createHandle();
283 {
284 std::lock_guard _lock{mLock};
285 mActiveFunctors.push_back(std::move(handle));
286 mFunctors.push_back(std::move(object));
287 }
288 return id;
289}
290
291void WebViewFunctorManager::releaseFunctor(int functor) {
292 sp<WebViewFunctor::Handle> toRelease;
293 {
294 std::lock_guard _lock{mLock};
295 for (auto iter = mActiveFunctors.begin(); iter != mActiveFunctors.end(); iter++) {
296 if ((*iter)->id() == functor) {
297 toRelease = std::move(*iter);
298 mActiveFunctors.erase(iter);
299 break;
300 }
301 }
302 }
303}
304
305void WebViewFunctorManager::onContextDestroyed() {
306 // WARNING: SKETCHY
307 // Because we know that we always remove from mFunctors on RenderThread, the same
308 // thread that always invokes onContextDestroyed, we know that the functor pointers
309 // will remain valid without the lock held.
310 // However, we won't block new functors from being added in the meantime.
311 mLock.lock();
312 const size_t size = mFunctors.size();
313 WebViewFunctor* toDestroyContext[size];
314 for (size_t i = 0; i < size; i++) {
315 toDestroyContext[i] = mFunctors[i].get();
316 }
317 mLock.unlock();
318 for (size_t i = 0; i < size; i++) {
319 toDestroyContext[i]->destroyContext();
320 }
321}
322
323void WebViewFunctorManager::destroyFunctor(int functor) {
324 std::unique_ptr<WebViewFunctor> toRelease;
325 {
326 std::lock_guard _lock{mLock};
327 for (auto iter = mFunctors.begin(); iter != mFunctors.end(); iter++) {
328 if ((*iter)->id() == functor) {
329 toRelease = std::move(*iter);
330 mFunctors.erase(iter);
331 break;
332 }
333 }
334 }
335}
336
337sp<WebViewFunctor::Handle> WebViewFunctorManager::handleFor(int functor) {
338 std::lock_guard _lock{mLock};
339 for (auto& iter : mActiveFunctors) {
340 if (iter->id() == functor) {
341 return iter;
342 }
343 }
344 return nullptr;
345}
346
Bo Liud6668e72018-12-14 19:37:41 -0800347} // namespace android::uirenderer