blob: 5aad821ad59ffb014e38d36bf7f332b0ed2fe552 [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
John Reck283bb462018-12-13 16:40:14 -0800121void WebViewFunctor::drawGl(const DrawGlInfo& drawInfo) {
122 ATRACE_NAME("WebViewFunctor::drawGl");
123 if (!mHasContext) {
124 mHasContext = true;
125 }
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500126 ScopedCurrentFunctor currentFunctor(this);
127
128 WebViewOverlayData overlayParams = {
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500129 .overlaysMode = OverlaysMode::Disabled,
130 .getSurfaceControl = currentFunctor.getSurfaceControl,
131 .mergeTransaction = currentFunctor.mergeTransaction,
132 };
Huihong Luo054b8d32021-02-24 18:48:12 -0800133
Huihong Luoeb931072021-06-30 10:12:17 -0700134 if (Properties::enableWebViewOverlays && !drawInfo.isLayer) {
Huihong Luo054b8d32021-02-24 18:48:12 -0800135 renderthread::CanvasContext* activeContext =
136 renderthread::CanvasContext::getActiveContext();
137 if (activeContext != nullptr) {
138 ASurfaceControl* rootSurfaceControl = activeContext->getSurfaceControl();
Huihong Luo540fdf82021-06-25 13:59:39 -0700139 if (rootSurfaceControl) {
140 overlayParams.overlaysMode = OverlaysMode::Enabled;
141 int32_t rgid = activeContext->getSurfaceControlGenerationId();
142 if (mParentSurfaceControlGenerationId != rgid) {
143 reparentSurfaceControl(rootSurfaceControl);
144 mParentSurfaceControlGenerationId = rgid;
145 }
146 }
Huihong Luo054b8d32021-02-24 18:48:12 -0800147 }
148 }
149
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500150 mCallbacks.gles.draw(mFunctor, mData, drawInfo, overlayParams);
John Reck283bb462018-12-13 16:40:14 -0800151}
152
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800153void WebViewFunctor::initVk(const VkFunctorInitParams& params) {
154 ATRACE_NAME("WebViewFunctor::initVk");
155 if (!mHasContext) {
156 mHasContext = true;
157 } else {
158 return;
159 }
160 mCallbacks.vk.initialize(mFunctor, mData, params);
161}
162
163void WebViewFunctor::drawVk(const VkFunctorDrawParams& params) {
164 ATRACE_NAME("WebViewFunctor::drawVk");
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500165 ScopedCurrentFunctor currentFunctor(this);
166
167 WebViewOverlayData overlayParams = {
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500168 .overlaysMode = OverlaysMode::Disabled,
169 .getSurfaceControl = currentFunctor.getSurfaceControl,
170 .mergeTransaction = currentFunctor.mergeTransaction,
171 };
Huihong Luo054b8d32021-02-24 18:48:12 -0800172
173 // TODO, enable surface control once offscreen mode figured out
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500174 mCallbacks.vk.draw(mFunctor, mData, params, overlayParams);
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800175}
176
177void WebViewFunctor::postDrawVk() {
178 ATRACE_NAME("WebViewFunctor::postDrawVk");
179 mCallbacks.vk.postDraw(mFunctor, mData);
180}
181
John Reck283bb462018-12-13 16:40:14 -0800182void WebViewFunctor::destroyContext() {
183 if (mHasContext) {
184 mHasContext = false;
185 ATRACE_NAME("WebViewFunctor::onContextDestroyed");
Bo Liud6668e72018-12-14 19:37:41 -0800186 mCallbacks.onContextDestroyed(mFunctor, mData);
Bo Liu1b0278c2019-01-03 16:36:24 -0800187
188 // grContext may be null in unit tests.
189 auto* grContext = renderthread::RenderThread::getInstance().getGrContext();
190 if (grContext) grContext->resetContext();
John Reck283bb462018-12-13 16:40:14 -0800191 }
192}
193
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500194void WebViewFunctor::removeOverlays() {
195 ScopedCurrentFunctor currentFunctor(this);
196 mCallbacks.removeOverlays(mFunctor, mData, currentFunctor.mergeTransaction);
Huihong Luo054b8d32021-02-24 18:48:12 -0800197 if (mSurfaceControl) {
Huihong Luoec68b7c2021-06-25 21:54:16 -0700198 reparentSurfaceControl(nullptr);
Huihong Luo054b8d32021-02-24 18:48:12 -0800199 auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions();
200 funcs.releaseFunc(mSurfaceControl);
201 mSurfaceControl = nullptr;
202 }
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500203}
204
205ASurfaceControl* WebViewFunctor::getSurfaceControl() {
Huihong Luo054b8d32021-02-24 18:48:12 -0800206 ATRACE_NAME("WebViewFunctor::getSurfaceControl");
207 if (mSurfaceControl != nullptr) return mSurfaceControl;
208
209 renderthread::CanvasContext* activeContext = renderthread::CanvasContext::getActiveContext();
210 LOG_ALWAYS_FATAL_IF(activeContext == nullptr, "Null active canvas context!");
211
212 ASurfaceControl* rootSurfaceControl = activeContext->getSurfaceControl();
213 LOG_ALWAYS_FATAL_IF(rootSurfaceControl == nullptr, "Null root surface control!");
214
215 auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions();
Huihong Luo540fdf82021-06-25 13:59:39 -0700216 mParentSurfaceControlGenerationId = activeContext->getSurfaceControlGenerationId();
Huihong Luo054b8d32021-02-24 18:48:12 -0800217 mSurfaceControl = funcs.createFunc(rootSurfaceControl, "Webview Overlay SurfaceControl");
218 ASurfaceTransaction* transaction = funcs.transactionCreateFunc();
Huihong Luo34f42fd2021-05-03 14:47:36 -0700219 activeContext->prepareSurfaceControlForWebview();
220 funcs.transactionSetZOrderFunc(transaction, mSurfaceControl, -1);
Huihong Luo054b8d32021-02-24 18:48:12 -0800221 funcs.transactionSetVisibilityFunc(transaction, mSurfaceControl,
222 ASURFACE_TRANSACTION_VISIBILITY_SHOW);
223 funcs.transactionApplyFunc(transaction);
224 funcs.transactionDeleteFunc(transaction);
225 return mSurfaceControl;
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500226}
227
228void WebViewFunctor::mergeTransaction(ASurfaceTransaction* transaction) {
Huihong Luo054b8d32021-02-24 18:48:12 -0800229 ATRACE_NAME("WebViewFunctor::mergeTransaction");
230 if (transaction == nullptr) return;
Huihong Luoec68b7c2021-06-25 21:54:16 -0700231 bool done = false;
Huihong Luo054b8d32021-02-24 18:48:12 -0800232 renderthread::CanvasContext* activeContext = renderthread::CanvasContext::getActiveContext();
Huihong Luoec68b7c2021-06-25 21:54:16 -0700233 // activeContext might be null when called from mCallbacks.removeOverlays()
234 if (activeContext != nullptr) {
235 done = activeContext->mergeTransaction(transaction, mSurfaceControl);
236 }
Huihong Luo054b8d32021-02-24 18:48:12 -0800237 if (!done) {
238 auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions();
239 funcs.transactionApplyFunc(transaction);
240 }
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -0500241}
242
Huihong Luo540fdf82021-06-25 13:59:39 -0700243void WebViewFunctor::reparentSurfaceControl(ASurfaceControl* parent) {
244 ATRACE_NAME("WebViewFunctor::reparentSurfaceControl");
245 if (mSurfaceControl == nullptr) return;
246
247 auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions();
248 ASurfaceTransaction* transaction = funcs.transactionCreateFunc();
249 funcs.transactionReparentFunc(transaction, mSurfaceControl, parent);
250 mergeTransaction(transaction);
251 funcs.transactionDeleteFunc(transaction);
252}
253
John Reck283bb462018-12-13 16:40:14 -0800254WebViewFunctorManager& WebViewFunctorManager::instance() {
255 static WebViewFunctorManager sInstance;
256 return sInstance;
257}
258
John Reckfaa1b0a2021-05-13 10:28:38 -0400259static void validateCallbacks(const WebViewFunctorCallbacks& callbacks) {
260 // TODO: Should we do a stack peek to see if this is really webview?
261 LOG_ALWAYS_FATAL_IF(callbacks.onSync == nullptr, "onSync is null");
262 LOG_ALWAYS_FATAL_IF(callbacks.onContextDestroyed == nullptr, "onContextDestroyed is null");
263 LOG_ALWAYS_FATAL_IF(callbacks.onDestroyed == nullptr, "onDestroyed is null");
264 LOG_ALWAYS_FATAL_IF(callbacks.removeOverlays == nullptr, "removeOverlays is null");
265 switch (auto mode = WebViewFunctor_queryPlatformRenderMode()) {
266 case RenderMode::OpenGL_ES:
267 LOG_ALWAYS_FATAL_IF(callbacks.gles.draw == nullptr, "gles.draw is null");
268 break;
269 case RenderMode::Vulkan:
270 LOG_ALWAYS_FATAL_IF(callbacks.vk.initialize == nullptr, "vk.initialize is null");
271 LOG_ALWAYS_FATAL_IF(callbacks.vk.draw == nullptr, "vk.draw is null");
272 LOG_ALWAYS_FATAL_IF(callbacks.vk.postDraw == nullptr, "vk.postDraw is null");
273 break;
274 default:
275 LOG_ALWAYS_FATAL("unknown platform mode? %d", (int)mode);
276 break;
277 }
278}
279
Bo Liud6668e72018-12-14 19:37:41 -0800280int WebViewFunctorManager::createFunctor(void* data, const WebViewFunctorCallbacks& callbacks,
John Reck283bb462018-12-13 16:40:14 -0800281 RenderMode functorMode) {
John Reckfaa1b0a2021-05-13 10:28:38 -0400282 validateCallbacks(callbacks);
Bo Liud6668e72018-12-14 19:37:41 -0800283 auto object = std::make_unique<WebViewFunctor>(data, callbacks, functorMode);
John Reck283bb462018-12-13 16:40:14 -0800284 int id = object->id();
285 auto handle = object->createHandle();
286 {
287 std::lock_guard _lock{mLock};
288 mActiveFunctors.push_back(std::move(handle));
289 mFunctors.push_back(std::move(object));
290 }
291 return id;
292}
293
294void WebViewFunctorManager::releaseFunctor(int functor) {
295 sp<WebViewFunctor::Handle> toRelease;
296 {
297 std::lock_guard _lock{mLock};
298 for (auto iter = mActiveFunctors.begin(); iter != mActiveFunctors.end(); iter++) {
299 if ((*iter)->id() == functor) {
300 toRelease = std::move(*iter);
301 mActiveFunctors.erase(iter);
302 break;
303 }
304 }
305 }
306}
307
308void WebViewFunctorManager::onContextDestroyed() {
309 // WARNING: SKETCHY
310 // Because we know that we always remove from mFunctors on RenderThread, the same
311 // thread that always invokes onContextDestroyed, we know that the functor pointers
312 // will remain valid without the lock held.
313 // However, we won't block new functors from being added in the meantime.
314 mLock.lock();
315 const size_t size = mFunctors.size();
316 WebViewFunctor* toDestroyContext[size];
317 for (size_t i = 0; i < size; i++) {
318 toDestroyContext[i] = mFunctors[i].get();
319 }
320 mLock.unlock();
321 for (size_t i = 0; i < size; i++) {
322 toDestroyContext[i]->destroyContext();
323 }
324}
325
326void WebViewFunctorManager::destroyFunctor(int functor) {
327 std::unique_ptr<WebViewFunctor> toRelease;
328 {
329 std::lock_guard _lock{mLock};
330 for (auto iter = mFunctors.begin(); iter != mFunctors.end(); iter++) {
331 if ((*iter)->id() == functor) {
332 toRelease = std::move(*iter);
333 mFunctors.erase(iter);
334 break;
335 }
336 }
337 }
338}
339
340sp<WebViewFunctor::Handle> WebViewFunctorManager::handleFor(int functor) {
341 std::lock_guard _lock{mLock};
342 for (auto& iter : mActiveFunctors) {
343 if (iter->id() == functor) {
344 return iter;
345 }
346 }
347 return nullptr;
348}
349
Bo Liud6668e72018-12-14 19:37:41 -0800350} // namespace android::uirenderer