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"); |
Bo Liu | d6668e7 | 2018-12-14 19:37:41 -0800 | [diff] [blame] | 103 | mCallbacks.onDestroyed(mFunctor, mData); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void WebViewFunctor::sync(const WebViewSyncData& syncData) const { |
| 107 | ATRACE_NAME("WebViewFunctor::sync"); |
Bo Liu | d6668e7 | 2018-12-14 19:37:41 -0800 | [diff] [blame] | 108 | mCallbacks.onSync(mFunctor, mData, syncData); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void WebViewFunctor::drawGl(const DrawGlInfo& drawInfo) { |
| 112 | ATRACE_NAME("WebViewFunctor::drawGl"); |
| 113 | if (!mHasContext) { |
| 114 | mHasContext = true; |
| 115 | } |
Vasiliy Telezhnikov | 6b23764 | 2020-11-12 18:14:58 -0500 | [diff] [blame] | 116 | ScopedCurrentFunctor currentFunctor(this); |
| 117 | |
| 118 | WebViewOverlayData overlayParams = { |
Vasiliy Telezhnikov | 6b23764 | 2020-11-12 18:14:58 -0500 | [diff] [blame] | 119 | .overlaysMode = OverlaysMode::Disabled, |
| 120 | .getSurfaceControl = currentFunctor.getSurfaceControl, |
| 121 | .mergeTransaction = currentFunctor.mergeTransaction, |
| 122 | }; |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 123 | |
| 124 | if (!drawInfo.isLayer) { |
| 125 | renderthread::CanvasContext* activeContext = |
| 126 | renderthread::CanvasContext::getActiveContext(); |
| 127 | if (activeContext != nullptr) { |
| 128 | ASurfaceControl* rootSurfaceControl = activeContext->getSurfaceControl(); |
Huihong Luo | 540fdf8 | 2021-06-25 13:59:39 -0700 | [diff] [blame] | 129 | if (rootSurfaceControl) { |
| 130 | overlayParams.overlaysMode = OverlaysMode::Enabled; |
| 131 | int32_t rgid = activeContext->getSurfaceControlGenerationId(); |
| 132 | if (mParentSurfaceControlGenerationId != rgid) { |
| 133 | reparentSurfaceControl(rootSurfaceControl); |
| 134 | mParentSurfaceControlGenerationId = rgid; |
| 135 | } |
| 136 | } |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Vasiliy Telezhnikov | 6b23764 | 2020-11-12 18:14:58 -0500 | [diff] [blame] | 140 | mCallbacks.gles.draw(mFunctor, mData, drawInfo, overlayParams); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 143 | void WebViewFunctor::initVk(const VkFunctorInitParams& params) { |
| 144 | ATRACE_NAME("WebViewFunctor::initVk"); |
| 145 | if (!mHasContext) { |
| 146 | mHasContext = true; |
| 147 | } else { |
| 148 | return; |
| 149 | } |
| 150 | mCallbacks.vk.initialize(mFunctor, mData, params); |
| 151 | } |
| 152 | |
| 153 | void WebViewFunctor::drawVk(const VkFunctorDrawParams& params) { |
| 154 | ATRACE_NAME("WebViewFunctor::drawVk"); |
Vasiliy Telezhnikov | 6b23764 | 2020-11-12 18:14:58 -0500 | [diff] [blame] | 155 | ScopedCurrentFunctor currentFunctor(this); |
| 156 | |
| 157 | WebViewOverlayData overlayParams = { |
Vasiliy Telezhnikov | 6b23764 | 2020-11-12 18:14:58 -0500 | [diff] [blame] | 158 | .overlaysMode = OverlaysMode::Disabled, |
| 159 | .getSurfaceControl = currentFunctor.getSurfaceControl, |
| 160 | .mergeTransaction = currentFunctor.mergeTransaction, |
| 161 | }; |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 162 | |
| 163 | // TODO, enable surface control once offscreen mode figured out |
Vasiliy Telezhnikov | 6b23764 | 2020-11-12 18:14:58 -0500 | [diff] [blame] | 164 | mCallbacks.vk.draw(mFunctor, mData, params, overlayParams); |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void WebViewFunctor::postDrawVk() { |
| 168 | ATRACE_NAME("WebViewFunctor::postDrawVk"); |
| 169 | mCallbacks.vk.postDraw(mFunctor, mData); |
| 170 | } |
| 171 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 172 | void WebViewFunctor::destroyContext() { |
| 173 | if (mHasContext) { |
| 174 | mHasContext = false; |
| 175 | ATRACE_NAME("WebViewFunctor::onContextDestroyed"); |
Bo Liu | d6668e7 | 2018-12-14 19:37:41 -0800 | [diff] [blame] | 176 | mCallbacks.onContextDestroyed(mFunctor, mData); |
Bo Liu | 1b0278c | 2019-01-03 16:36:24 -0800 | [diff] [blame] | 177 | |
| 178 | // grContext may be null in unit tests. |
| 179 | auto* grContext = renderthread::RenderThread::getInstance().getGrContext(); |
| 180 | if (grContext) grContext->resetContext(); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Vasiliy Telezhnikov | 6b23764 | 2020-11-12 18:14:58 -0500 | [diff] [blame] | 184 | void WebViewFunctor::removeOverlays() { |
| 185 | ScopedCurrentFunctor currentFunctor(this); |
| 186 | mCallbacks.removeOverlays(mFunctor, mData, currentFunctor.mergeTransaction); |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 187 | if (mSurfaceControl) { |
| 188 | auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions(); |
| 189 | funcs.releaseFunc(mSurfaceControl); |
| 190 | mSurfaceControl = nullptr; |
| 191 | } |
Vasiliy Telezhnikov | 6b23764 | 2020-11-12 18:14:58 -0500 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | ASurfaceControl* WebViewFunctor::getSurfaceControl() { |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 195 | ATRACE_NAME("WebViewFunctor::getSurfaceControl"); |
| 196 | if (mSurfaceControl != nullptr) return mSurfaceControl; |
| 197 | |
| 198 | renderthread::CanvasContext* activeContext = renderthread::CanvasContext::getActiveContext(); |
| 199 | LOG_ALWAYS_FATAL_IF(activeContext == nullptr, "Null active canvas context!"); |
| 200 | |
| 201 | ASurfaceControl* rootSurfaceControl = activeContext->getSurfaceControl(); |
| 202 | LOG_ALWAYS_FATAL_IF(rootSurfaceControl == nullptr, "Null root surface control!"); |
| 203 | |
| 204 | auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions(); |
Huihong Luo | 540fdf8 | 2021-06-25 13:59:39 -0700 | [diff] [blame] | 205 | mParentSurfaceControlGenerationId = activeContext->getSurfaceControlGenerationId(); |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 206 | mSurfaceControl = funcs.createFunc(rootSurfaceControl, "Webview Overlay SurfaceControl"); |
| 207 | ASurfaceTransaction* transaction = funcs.transactionCreateFunc(); |
Huihong Luo | 34f42fd | 2021-05-03 14:47:36 -0700 | [diff] [blame] | 208 | activeContext->prepareSurfaceControlForWebview(); |
| 209 | funcs.transactionSetZOrderFunc(transaction, mSurfaceControl, -1); |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 210 | funcs.transactionSetVisibilityFunc(transaction, mSurfaceControl, |
| 211 | ASURFACE_TRANSACTION_VISIBILITY_SHOW); |
| 212 | funcs.transactionApplyFunc(transaction); |
| 213 | funcs.transactionDeleteFunc(transaction); |
| 214 | return mSurfaceControl; |
Vasiliy Telezhnikov | 6b23764 | 2020-11-12 18:14:58 -0500 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void WebViewFunctor::mergeTransaction(ASurfaceTransaction* transaction) { |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 218 | ATRACE_NAME("WebViewFunctor::mergeTransaction"); |
| 219 | if (transaction == nullptr) return; |
| 220 | renderthread::CanvasContext* activeContext = renderthread::CanvasContext::getActiveContext(); |
| 221 | LOG_ALWAYS_FATAL_IF(activeContext == nullptr, "Null active canvas context!"); |
| 222 | bool done = activeContext->mergeTransaction(transaction, mSurfaceControl); |
| 223 | if (!done) { |
| 224 | auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions(); |
| 225 | funcs.transactionApplyFunc(transaction); |
| 226 | } |
Vasiliy Telezhnikov | 6b23764 | 2020-11-12 18:14:58 -0500 | [diff] [blame] | 227 | } |
| 228 | |
Huihong Luo | 540fdf8 | 2021-06-25 13:59:39 -0700 | [diff] [blame] | 229 | void WebViewFunctor::reparentSurfaceControl(ASurfaceControl* parent) { |
| 230 | ATRACE_NAME("WebViewFunctor::reparentSurfaceControl"); |
| 231 | if (mSurfaceControl == nullptr) return; |
| 232 | |
| 233 | auto funcs = renderthread::RenderThread::getInstance().getASurfaceControlFunctions(); |
| 234 | ASurfaceTransaction* transaction = funcs.transactionCreateFunc(); |
| 235 | funcs.transactionReparentFunc(transaction, mSurfaceControl, parent); |
| 236 | mergeTransaction(transaction); |
| 237 | funcs.transactionDeleteFunc(transaction); |
| 238 | } |
| 239 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 240 | WebViewFunctorManager& WebViewFunctorManager::instance() { |
| 241 | static WebViewFunctorManager sInstance; |
| 242 | return sInstance; |
| 243 | } |
| 244 | |
John Reck | faa1b0a | 2021-05-13 10:28:38 -0400 | [diff] [blame] | 245 | static void validateCallbacks(const WebViewFunctorCallbacks& callbacks) { |
| 246 | // TODO: Should we do a stack peek to see if this is really webview? |
| 247 | LOG_ALWAYS_FATAL_IF(callbacks.onSync == nullptr, "onSync is null"); |
| 248 | LOG_ALWAYS_FATAL_IF(callbacks.onContextDestroyed == nullptr, "onContextDestroyed is null"); |
| 249 | LOG_ALWAYS_FATAL_IF(callbacks.onDestroyed == nullptr, "onDestroyed is null"); |
| 250 | LOG_ALWAYS_FATAL_IF(callbacks.removeOverlays == nullptr, "removeOverlays is null"); |
| 251 | switch (auto mode = WebViewFunctor_queryPlatformRenderMode()) { |
| 252 | case RenderMode::OpenGL_ES: |
| 253 | LOG_ALWAYS_FATAL_IF(callbacks.gles.draw == nullptr, "gles.draw is null"); |
| 254 | break; |
| 255 | case RenderMode::Vulkan: |
| 256 | LOG_ALWAYS_FATAL_IF(callbacks.vk.initialize == nullptr, "vk.initialize is null"); |
| 257 | LOG_ALWAYS_FATAL_IF(callbacks.vk.draw == nullptr, "vk.draw is null"); |
| 258 | LOG_ALWAYS_FATAL_IF(callbacks.vk.postDraw == nullptr, "vk.postDraw is null"); |
| 259 | break; |
| 260 | default: |
| 261 | LOG_ALWAYS_FATAL("unknown platform mode? %d", (int)mode); |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | |
Bo Liu | d6668e7 | 2018-12-14 19:37:41 -0800 | [diff] [blame] | 266 | int WebViewFunctorManager::createFunctor(void* data, const WebViewFunctorCallbacks& callbacks, |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 267 | RenderMode functorMode) { |
John Reck | faa1b0a | 2021-05-13 10:28:38 -0400 | [diff] [blame] | 268 | validateCallbacks(callbacks); |
Bo Liu | d6668e7 | 2018-12-14 19:37:41 -0800 | [diff] [blame] | 269 | auto object = std::make_unique<WebViewFunctor>(data, callbacks, functorMode); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 270 | int id = object->id(); |
| 271 | auto handle = object->createHandle(); |
| 272 | { |
| 273 | std::lock_guard _lock{mLock}; |
| 274 | mActiveFunctors.push_back(std::move(handle)); |
| 275 | mFunctors.push_back(std::move(object)); |
| 276 | } |
| 277 | return id; |
| 278 | } |
| 279 | |
| 280 | void WebViewFunctorManager::releaseFunctor(int functor) { |
| 281 | sp<WebViewFunctor::Handle> toRelease; |
| 282 | { |
| 283 | std::lock_guard _lock{mLock}; |
| 284 | for (auto iter = mActiveFunctors.begin(); iter != mActiveFunctors.end(); iter++) { |
| 285 | if ((*iter)->id() == functor) { |
| 286 | toRelease = std::move(*iter); |
| 287 | mActiveFunctors.erase(iter); |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | void WebViewFunctorManager::onContextDestroyed() { |
| 295 | // WARNING: SKETCHY |
| 296 | // Because we know that we always remove from mFunctors on RenderThread, the same |
| 297 | // thread that always invokes onContextDestroyed, we know that the functor pointers |
| 298 | // will remain valid without the lock held. |
| 299 | // However, we won't block new functors from being added in the meantime. |
| 300 | mLock.lock(); |
| 301 | const size_t size = mFunctors.size(); |
| 302 | WebViewFunctor* toDestroyContext[size]; |
| 303 | for (size_t i = 0; i < size; i++) { |
| 304 | toDestroyContext[i] = mFunctors[i].get(); |
| 305 | } |
| 306 | mLock.unlock(); |
| 307 | for (size_t i = 0; i < size; i++) { |
| 308 | toDestroyContext[i]->destroyContext(); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void WebViewFunctorManager::destroyFunctor(int functor) { |
| 313 | std::unique_ptr<WebViewFunctor> toRelease; |
| 314 | { |
| 315 | std::lock_guard _lock{mLock}; |
| 316 | for (auto iter = mFunctors.begin(); iter != mFunctors.end(); iter++) { |
| 317 | if ((*iter)->id() == functor) { |
| 318 | toRelease = std::move(*iter); |
| 319 | mFunctors.erase(iter); |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | sp<WebViewFunctor::Handle> WebViewFunctorManager::handleFor(int functor) { |
| 327 | std::lock_guard _lock{mLock}; |
| 328 | for (auto& iter : mActiveFunctors) { |
| 329 | if (iter->id() == functor) { |
| 330 | return iter; |
| 331 | } |
| 332 | } |
| 333 | return nullptr; |
| 334 | } |
| 335 | |
Bo Liu | d6668e7 | 2018-12-14 19:37:41 -0800 | [diff] [blame] | 336 | } // namespace android::uirenderer |