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