blob: 6fc251dc815ce62c3472356b646b44caa2c08b82 [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");
Huihong Luo9c5158f72021-07-23 12:35:59 -0700103 if (mSurfaceControl) {
104 removeOverlays();
105 }
Bo Liud6668e72018-12-14 19:37:41 -0800106 mCallbacks.onDestroyed(mFunctor, mData);
John Reck283bb462018-12-13 16:40:14 -0800107}
108
109void WebViewFunctor::sync(const WebViewSyncData& syncData) const {
110 ATRACE_NAME("WebViewFunctor::sync");
Bo Liud6668e72018-12-14 19:37:41 -0800111 mCallbacks.onSync(mFunctor, mData, syncData);
John Reck283bb462018-12-13 16:40:14 -0800112}
113
Huihong Luoec68b7c2021-06-25 21:54:16 -0700114void WebViewFunctor::onRemovedFromTree() {
115 ATRACE_NAME("WebViewFunctor::onRemovedFromTree");
116 if (mSurfaceControl) {
117 removeOverlays();
118 }
119}
120
Vasiliy Telezhnikov372a21b2021-11-17 13:50:56 -0500121bool WebViewFunctor::prepareRootSurfaceControl() {
122 if (!Properties::enableWebViewOverlays) return false;
123
124 renderthread::CanvasContext* activeContext = renderthread::CanvasContext::getActiveContext();
125 if (!activeContext) return false;
126
127 ASurfaceControl* rootSurfaceControl = activeContext->getSurfaceControl();
128 if (!rootSurfaceControl) return false;
129
130 int32_t rgid = activeContext->getSurfaceControlGenerationId();
131 if (mParentSurfaceControlGenerationId != rgid) {
132 reparentSurfaceControl(rootSurfaceControl);
133 mParentSurfaceControlGenerationId = rgid;
134 }
135
136 return true;
137}
138
John Reck283bb462018-12-13 16:40:14 -0800139void WebViewFunctor::drawGl(const DrawGlInfo& drawInfo) {
140 ATRACE_NAME("WebViewFunctor::drawGl");
141 if (!mHasContext) {
142 mHasContext = true;
143 }
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500144 ScopedCurrentFunctor currentFunctor(this);
145
146 WebViewOverlayData overlayParams = {
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500147 .overlaysMode = OverlaysMode::Disabled,
148 .getSurfaceControl = currentFunctor.getSurfaceControl,
149 .mergeTransaction = currentFunctor.mergeTransaction,
150 };
Huihong Luo054b8d32021-02-24 18:48:12 -0800151
Vasiliy Telezhnikov372a21b2021-11-17 13:50:56 -0500152 if (!drawInfo.isLayer && prepareRootSurfaceControl()) {
153 overlayParams.overlaysMode = OverlaysMode::Enabled;
Huihong Luo054b8d32021-02-24 18:48:12 -0800154 }
155
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500156 mCallbacks.gles.draw(mFunctor, mData, drawInfo, overlayParams);
John Reck283bb462018-12-13 16:40:14 -0800157}
158
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800159void WebViewFunctor::initVk(const VkFunctorInitParams& params) {
160 ATRACE_NAME("WebViewFunctor::initVk");
161 if (!mHasContext) {
162 mHasContext = true;
163 } else {
164 return;
165 }
166 mCallbacks.vk.initialize(mFunctor, mData, params);
167}
168
169void WebViewFunctor::drawVk(const VkFunctorDrawParams& params) {
170 ATRACE_NAME("WebViewFunctor::drawVk");
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500171 ScopedCurrentFunctor currentFunctor(this);
172
173 WebViewOverlayData overlayParams = {
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500174 .overlaysMode = OverlaysMode::Disabled,
175 .getSurfaceControl = currentFunctor.getSurfaceControl,
176 .mergeTransaction = currentFunctor.mergeTransaction,
177 };
Huihong Luo054b8d32021-02-24 18:48:12 -0800178
Vasiliy Telezhnikov372a21b2021-11-17 13:50:56 -0500179 if (!params.is_layer && prepareRootSurfaceControl()) {
180 overlayParams.overlaysMode = OverlaysMode::Enabled;
181 }
182
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500183 mCallbacks.vk.draw(mFunctor, mData, params, overlayParams);
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800184}
185
186void WebViewFunctor::postDrawVk() {
187 ATRACE_NAME("WebViewFunctor::postDrawVk");
188 mCallbacks.vk.postDraw(mFunctor, mData);
189}
190
John Reck283bb462018-12-13 16:40:14 -0800191void WebViewFunctor::destroyContext() {
192 if (mHasContext) {
193 mHasContext = false;
194 ATRACE_NAME("WebViewFunctor::onContextDestroyed");
Bo Liud6668e72018-12-14 19:37:41 -0800195 mCallbacks.onContextDestroyed(mFunctor, mData);
Bo Liu1b0278c2019-01-03 16:36:24 -0800196
197 // grContext may be null in unit tests.
198 auto* grContext = renderthread::RenderThread::getInstance().getGrContext();
199 if (grContext) grContext->resetContext();
John Reck283bb462018-12-13 16:40:14 -0800200 }
201}
202
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500203void WebViewFunctor::removeOverlays() {
204 ScopedCurrentFunctor currentFunctor(this);
205 mCallbacks.removeOverlays(mFunctor, mData, currentFunctor.mergeTransaction);
Huihong Luo054b8d32021-02-24 18:48:12 -0800206 if (mSurfaceControl) {
Huihong Luoec68b7c2021-06-25 21:54:16 -0700207 reparentSurfaceControl(nullptr);
Huihong Luo054b8d32021-02-24 18:48:12 -0800208 auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions();
209 funcs.releaseFunc(mSurfaceControl);
210 mSurfaceControl = nullptr;
211 }
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500212}
213
214ASurfaceControl* WebViewFunctor::getSurfaceControl() {
Huihong Luo054b8d32021-02-24 18:48:12 -0800215 ATRACE_NAME("WebViewFunctor::getSurfaceControl");
216 if (mSurfaceControl != nullptr) return mSurfaceControl;
217
218 renderthread::CanvasContext* activeContext = renderthread::CanvasContext::getActiveContext();
219 LOG_ALWAYS_FATAL_IF(activeContext == nullptr, "Null active canvas context!");
220
221 ASurfaceControl* rootSurfaceControl = activeContext->getSurfaceControl();
222 LOG_ALWAYS_FATAL_IF(rootSurfaceControl == nullptr, "Null root surface control!");
223
224 auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions();
Huihong Luo540fdf82021-06-25 13:59:39 -0700225 mParentSurfaceControlGenerationId = activeContext->getSurfaceControlGenerationId();
Huihong Luo054b8d32021-02-24 18:48:12 -0800226 mSurfaceControl = funcs.createFunc(rootSurfaceControl, "Webview Overlay SurfaceControl");
227 ASurfaceTransaction* transaction = funcs.transactionCreateFunc();
Huihong Luo34f42fd2021-05-03 14:47:36 -0700228 activeContext->prepareSurfaceControlForWebview();
229 funcs.transactionSetZOrderFunc(transaction, mSurfaceControl, -1);
Huihong Luo054b8d32021-02-24 18:48:12 -0800230 funcs.transactionSetVisibilityFunc(transaction, mSurfaceControl,
231 ASURFACE_TRANSACTION_VISIBILITY_SHOW);
232 funcs.transactionApplyFunc(transaction);
233 funcs.transactionDeleteFunc(transaction);
234 return mSurfaceControl;
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500235}
236
237void WebViewFunctor::mergeTransaction(ASurfaceTransaction* transaction) {
Huihong Luo054b8d32021-02-24 18:48:12 -0800238 ATRACE_NAME("WebViewFunctor::mergeTransaction");
239 if (transaction == nullptr) return;
Huihong Luoec68b7c2021-06-25 21:54:16 -0700240 bool done = false;
Huihong Luo054b8d32021-02-24 18:48:12 -0800241 renderthread::CanvasContext* activeContext = renderthread::CanvasContext::getActiveContext();
Huihong Luoec68b7c2021-06-25 21:54:16 -0700242 // activeContext might be null when called from mCallbacks.removeOverlays()
243 if (activeContext != nullptr) {
244 done = activeContext->mergeTransaction(transaction, mSurfaceControl);
245 }
Huihong Luo054b8d32021-02-24 18:48:12 -0800246 if (!done) {
247 auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions();
248 funcs.transactionApplyFunc(transaction);
249 }
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500250}
251
Huihong Luo540fdf82021-06-25 13:59:39 -0700252void WebViewFunctor::reparentSurfaceControl(ASurfaceControl* parent) {
253 ATRACE_NAME("WebViewFunctor::reparentSurfaceControl");
254 if (mSurfaceControl == nullptr) return;
255
256 auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions();
257 ASurfaceTransaction* transaction = funcs.transactionCreateFunc();
258 funcs.transactionReparentFunc(transaction, mSurfaceControl, parent);
259 mergeTransaction(transaction);
260 funcs.transactionDeleteFunc(transaction);
261}
262
John Reck283bb462018-12-13 16:40:14 -0800263WebViewFunctorManager& WebViewFunctorManager::instance() {
264 static WebViewFunctorManager sInstance;
265 return sInstance;
266}
267
John Reckfaa1b0a2021-05-13 10:28:38 -0400268static void validateCallbacks(const WebViewFunctorCallbacks& callbacks) {
269 // TODO: Should we do a stack peek to see if this is really webview?
270 LOG_ALWAYS_FATAL_IF(callbacks.onSync == nullptr, "onSync is null");
271 LOG_ALWAYS_FATAL_IF(callbacks.onContextDestroyed == nullptr, "onContextDestroyed is null");
272 LOG_ALWAYS_FATAL_IF(callbacks.onDestroyed == nullptr, "onDestroyed is null");
273 LOG_ALWAYS_FATAL_IF(callbacks.removeOverlays == nullptr, "removeOverlays is null");
274 switch (auto mode = WebViewFunctor_queryPlatformRenderMode()) {
275 case RenderMode::OpenGL_ES:
276 LOG_ALWAYS_FATAL_IF(callbacks.gles.draw == nullptr, "gles.draw is null");
277 break;
278 case RenderMode::Vulkan:
279 LOG_ALWAYS_FATAL_IF(callbacks.vk.initialize == nullptr, "vk.initialize is null");
280 LOG_ALWAYS_FATAL_IF(callbacks.vk.draw == nullptr, "vk.draw is null");
281 LOG_ALWAYS_FATAL_IF(callbacks.vk.postDraw == nullptr, "vk.postDraw is null");
282 break;
283 default:
284 LOG_ALWAYS_FATAL("unknown platform mode? %d", (int)mode);
285 break;
286 }
287}
288
Bo Liud6668e72018-12-14 19:37:41 -0800289int WebViewFunctorManager::createFunctor(void* data, const WebViewFunctorCallbacks& callbacks,
John Reck283bb462018-12-13 16:40:14 -0800290 RenderMode functorMode) {
John Reckfaa1b0a2021-05-13 10:28:38 -0400291 validateCallbacks(callbacks);
Bo Liud6668e72018-12-14 19:37:41 -0800292 auto object = std::make_unique<WebViewFunctor>(data, callbacks, functorMode);
John Reck283bb462018-12-13 16:40:14 -0800293 int id = object->id();
294 auto handle = object->createHandle();
295 {
296 std::lock_guard _lock{mLock};
297 mActiveFunctors.push_back(std::move(handle));
298 mFunctors.push_back(std::move(object));
299 }
300 return id;
301}
302
303void WebViewFunctorManager::releaseFunctor(int functor) {
304 sp<WebViewFunctor::Handle> toRelease;
305 {
306 std::lock_guard _lock{mLock};
307 for (auto iter = mActiveFunctors.begin(); iter != mActiveFunctors.end(); iter++) {
308 if ((*iter)->id() == functor) {
309 toRelease = std::move(*iter);
310 mActiveFunctors.erase(iter);
311 break;
312 }
313 }
314 }
315}
316
317void WebViewFunctorManager::onContextDestroyed() {
318 // WARNING: SKETCHY
319 // Because we know that we always remove from mFunctors on RenderThread, the same
320 // thread that always invokes onContextDestroyed, we know that the functor pointers
321 // will remain valid without the lock held.
322 // However, we won't block new functors from being added in the meantime.
323 mLock.lock();
324 const size_t size = mFunctors.size();
325 WebViewFunctor* toDestroyContext[size];
326 for (size_t i = 0; i < size; i++) {
327 toDestroyContext[i] = mFunctors[i].get();
328 }
329 mLock.unlock();
330 for (size_t i = 0; i < size; i++) {
331 toDestroyContext[i]->destroyContext();
332 }
333}
334
335void WebViewFunctorManager::destroyFunctor(int functor) {
336 std::unique_ptr<WebViewFunctor> toRelease;
337 {
338 std::lock_guard _lock{mLock};
339 for (auto iter = mFunctors.begin(); iter != mFunctors.end(); iter++) {
340 if ((*iter)->id() == functor) {
341 toRelease = std::move(*iter);
342 mFunctors.erase(iter);
343 break;
344 }
345 }
346 }
347}
348
349sp<WebViewFunctor::Handle> WebViewFunctorManager::handleFor(int functor) {
350 std::lock_guard _lock{mLock};
351 for (auto& iter : mActiveFunctors) {
352 if (iter->id() == functor) {
353 return iter;
354 }
355 }
356 return nullptr;
357}
358
Bo Liud6668e72018-12-14 19:37:41 -0800359} // namespace android::uirenderer