1 /*
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"
21 #include "renderthread/CanvasContext.h"
22 #include "renderthread/RenderThread.h"
23
24 #include <log/log.h>
25 #include <utils/Trace.h>
26 #include <atomic>
27
28 namespace android::uirenderer {
29
30 namespace {
31 class ScopedCurrentFunctor {
32 public:
ScopedCurrentFunctor(WebViewFunctor * functor)33 ScopedCurrentFunctor(WebViewFunctor* functor) {
34 ALOG_ASSERT(!sCurrentFunctor);
35 ALOG_ASSERT(functor);
36 sCurrentFunctor = functor;
37 }
~ScopedCurrentFunctor()38 ~ScopedCurrentFunctor() {
39 ALOG_ASSERT(sCurrentFunctor);
40 sCurrentFunctor = nullptr;
41 }
42
getSurfaceControl()43 static ASurfaceControl* getSurfaceControl() {
44 ALOG_ASSERT(sCurrentFunctor);
45 return sCurrentFunctor->getSurfaceControl();
46 }
mergeTransaction(ASurfaceTransaction * transaction)47 static void mergeTransaction(ASurfaceTransaction* transaction) {
48 ALOG_ASSERT(sCurrentFunctor);
49 sCurrentFunctor->mergeTransaction(transaction);
50 }
51
52 private:
53 static WebViewFunctor* sCurrentFunctor;
54 };
55
56 WebViewFunctor* ScopedCurrentFunctor::sCurrentFunctor = nullptr;
57 } // namespace
58
WebViewFunctor_queryPlatformRenderMode()59 RenderMode 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
WebViewFunctor_create(void * data,const WebViewFunctorCallbacks & prototype,RenderMode functorMode)71 int WebViewFunctor_create(void* data, const WebViewFunctorCallbacks& prototype,
72 RenderMode functorMode) {
73 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 }
82 return WebViewFunctorManager::instance().createFunctor(data, prototype, functorMode);
83 }
84
WebViewFunctor_release(int functor)85 void WebViewFunctor_release(int functor) {
86 WebViewFunctorManager::instance().releaseFunctor(functor);
87 }
88
89 static std::atomic_int sNextId{1};
90
WebViewFunctor(void * data,const WebViewFunctorCallbacks & callbacks,RenderMode functorMode)91 WebViewFunctor::WebViewFunctor(void* data, const WebViewFunctorCallbacks& callbacks,
92 RenderMode functorMode)
93 : mData(data) {
94 mFunctor = sNextId++;
95 mCallbacks = callbacks;
96 mMode = functorMode;
97 }
98
~WebViewFunctor()99 WebViewFunctor::~WebViewFunctor() {
100 destroyContext();
101
102 ATRACE_NAME("WebViewFunctor::onDestroy");
103 if (mSurfaceControl) {
104 removeOverlays();
105 }
106 mCallbacks.onDestroyed(mFunctor, mData);
107 }
108
sync(const WebViewSyncData & syncData) const109 void WebViewFunctor::sync(const WebViewSyncData& syncData) const {
110 ATRACE_NAME("WebViewFunctor::sync");
111 mCallbacks.onSync(mFunctor, mData, syncData);
112 }
113
onRemovedFromTree()114 void WebViewFunctor::onRemovedFromTree() {
115 ATRACE_NAME("WebViewFunctor::onRemovedFromTree");
116 if (mSurfaceControl) {
117 removeOverlays();
118 }
119 }
120
drawGl(const DrawGlInfo & drawInfo)121 void WebViewFunctor::drawGl(const DrawGlInfo& drawInfo) {
122 ATRACE_NAME("WebViewFunctor::drawGl");
123 if (!mHasContext) {
124 mHasContext = true;
125 }
126 ScopedCurrentFunctor currentFunctor(this);
127
128 WebViewOverlayData overlayParams = {
129 .overlaysMode = OverlaysMode::Disabled,
130 .getSurfaceControl = currentFunctor.getSurfaceControl,
131 .mergeTransaction = currentFunctor.mergeTransaction,
132 };
133
134 if (Properties::enableWebViewOverlays && !drawInfo.isLayer) {
135 renderthread::CanvasContext* activeContext =
136 renderthread::CanvasContext::getActiveContext();
137 if (activeContext != nullptr) {
138 ASurfaceControl* rootSurfaceControl = activeContext->getSurfaceControl();
139 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 }
147 }
148 }
149
150 mCallbacks.gles.draw(mFunctor, mData, drawInfo, overlayParams);
151 }
152
initVk(const VkFunctorInitParams & params)153 void 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
drawVk(const VkFunctorDrawParams & params)163 void WebViewFunctor::drawVk(const VkFunctorDrawParams& params) {
164 ATRACE_NAME("WebViewFunctor::drawVk");
165 ScopedCurrentFunctor currentFunctor(this);
166
167 WebViewOverlayData overlayParams = {
168 .overlaysMode = OverlaysMode::Disabled,
169 .getSurfaceControl = currentFunctor.getSurfaceControl,
170 .mergeTransaction = currentFunctor.mergeTransaction,
171 };
172
173 // TODO, enable surface control once offscreen mode figured out
174 mCallbacks.vk.draw(mFunctor, mData, params, overlayParams);
175 }
176
postDrawVk()177 void WebViewFunctor::postDrawVk() {
178 ATRACE_NAME("WebViewFunctor::postDrawVk");
179 mCallbacks.vk.postDraw(mFunctor, mData);
180 }
181
destroyContext()182 void WebViewFunctor::destroyContext() {
183 if (mHasContext) {
184 mHasContext = false;
185 ATRACE_NAME("WebViewFunctor::onContextDestroyed");
186 mCallbacks.onContextDestroyed(mFunctor, mData);
187
188 // grContext may be null in unit tests.
189 auto* grContext = renderthread::RenderThread::getInstance().getGrContext();
190 if (grContext) grContext->resetContext();
191 }
192 }
193
removeOverlays()194 void WebViewFunctor::removeOverlays() {
195 ScopedCurrentFunctor currentFunctor(this);
196 mCallbacks.removeOverlays(mFunctor, mData, currentFunctor.mergeTransaction);
197 if (mSurfaceControl) {
198 reparentSurfaceControl(nullptr);
199 auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions();
200 funcs.releaseFunc(mSurfaceControl);
201 mSurfaceControl = nullptr;
202 }
203 }
204
getSurfaceControl()205 ASurfaceControl* WebViewFunctor::getSurfaceControl() {
206 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();
216 mParentSurfaceControlGenerationId = activeContext->getSurfaceControlGenerationId();
217 mSurfaceControl = funcs.createFunc(rootSurfaceControl, "Webview Overlay SurfaceControl");
218 ASurfaceTransaction* transaction = funcs.transactionCreateFunc();
219 activeContext->prepareSurfaceControlForWebview();
220 funcs.transactionSetZOrderFunc(transaction, mSurfaceControl, -1);
221 funcs.transactionSetVisibilityFunc(transaction, mSurfaceControl,
222 ASURFACE_TRANSACTION_VISIBILITY_SHOW);
223 funcs.transactionApplyFunc(transaction);
224 funcs.transactionDeleteFunc(transaction);
225 return mSurfaceControl;
226 }
227
mergeTransaction(ASurfaceTransaction * transaction)228 void WebViewFunctor::mergeTransaction(ASurfaceTransaction* transaction) {
229 ATRACE_NAME("WebViewFunctor::mergeTransaction");
230 if (transaction == nullptr) return;
231 bool done = false;
232 renderthread::CanvasContext* activeContext = renderthread::CanvasContext::getActiveContext();
233 // activeContext might be null when called from mCallbacks.removeOverlays()
234 if (activeContext != nullptr) {
235 done = activeContext->mergeTransaction(transaction, mSurfaceControl);
236 }
237 if (!done) {
238 auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions();
239 funcs.transactionApplyFunc(transaction);
240 }
241 }
242
reparentSurfaceControl(ASurfaceControl * parent)243 void 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
instance()254 WebViewFunctorManager& WebViewFunctorManager::instance() {
255 static WebViewFunctorManager sInstance;
256 return sInstance;
257 }
258
validateCallbacks(const WebViewFunctorCallbacks & callbacks)259 static 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
createFunctor(void * data,const WebViewFunctorCallbacks & callbacks,RenderMode functorMode)280 int WebViewFunctorManager::createFunctor(void* data, const WebViewFunctorCallbacks& callbacks,
281 RenderMode functorMode) {
282 validateCallbacks(callbacks);
283 auto object = std::make_unique<WebViewFunctor>(data, callbacks, functorMode);
284 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
releaseFunctor(int functor)294 void 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
onContextDestroyed()308 void 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
destroyFunctor(int functor)326 void 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
handleFor(int functor)340 sp<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
350 } // namespace android::uirenderer
351