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" |
| 21 | |
| 22 | #include <log/log.h> |
| 23 | #include <utils/Trace.h> |
| 24 | #include <atomic> |
| 25 | |
| 26 | namespace android::uirenderer { |
| 27 | |
| 28 | RenderMode WebViewFunctor_queryPlatformRenderMode() { |
| 29 | auto pipelineType = Properties::getRenderPipelineType(); |
| 30 | switch (pipelineType) { |
| 31 | case RenderPipelineType::SkiaGL: |
| 32 | return RenderMode::OpenGL_ES; |
| 33 | case RenderPipelineType::SkiaVulkan: |
| 34 | return RenderMode::Vulkan; |
| 35 | default: |
| 36 | LOG_ALWAYS_FATAL("Unknown render pipeline type: %d", (int)pipelineType); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | int WebViewFunctor_create(const WebViewFunctorCallbacks& prototype, RenderMode functorMode) { |
| 41 | if (functorMode != RenderMode::OpenGL_ES && functorMode != RenderMode::Vulkan) { |
| 42 | ALOGW("Unknown rendermode %d", (int)functorMode); |
| 43 | return -1; |
| 44 | } |
| 45 | if (functorMode == RenderMode::Vulkan && |
| 46 | WebViewFunctor_queryPlatformRenderMode() != RenderMode::Vulkan) { |
| 47 | ALOGW("Unable to map from GLES platform to a vulkan functor"); |
| 48 | return -1; |
| 49 | } |
| 50 | return WebViewFunctorManager::instance().createFunctor(prototype, functorMode); |
| 51 | } |
| 52 | |
| 53 | void WebViewFunctor_release(int functor) { |
| 54 | WebViewFunctorManager::instance().releaseFunctor(functor); |
| 55 | } |
| 56 | |
| 57 | static std::atomic_int sNextId{1}; |
| 58 | |
| 59 | WebViewFunctor::WebViewFunctor(const WebViewFunctorCallbacks& callbacks, RenderMode functorMode) { |
| 60 | mFunctor = sNextId++; |
| 61 | mCallbacks = callbacks; |
| 62 | mMode = functorMode; |
| 63 | } |
| 64 | |
| 65 | WebViewFunctor::~WebViewFunctor() { |
| 66 | destroyContext(); |
| 67 | |
| 68 | ATRACE_NAME("WebViewFunctor::onDestroy"); |
| 69 | mCallbacks.onDestroyed(mFunctor); |
| 70 | } |
| 71 | |
| 72 | void WebViewFunctor::sync(const WebViewSyncData& syncData) const { |
| 73 | ATRACE_NAME("WebViewFunctor::sync"); |
| 74 | mCallbacks.onSync(mFunctor, syncData); |
| 75 | } |
| 76 | |
| 77 | void WebViewFunctor::drawGl(const DrawGlInfo& drawInfo) { |
| 78 | ATRACE_NAME("WebViewFunctor::drawGl"); |
| 79 | if (!mHasContext) { |
| 80 | mHasContext = true; |
| 81 | } |
| 82 | mCallbacks.gles.draw(mFunctor, drawInfo); |
| 83 | } |
| 84 | |
| 85 | void WebViewFunctor::destroyContext() { |
| 86 | if (mHasContext) { |
| 87 | mHasContext = false; |
| 88 | ATRACE_NAME("WebViewFunctor::onContextDestroyed"); |
| 89 | mCallbacks.onContextDestroyed(mFunctor); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | WebViewFunctorManager& WebViewFunctorManager::instance() { |
| 94 | static WebViewFunctorManager sInstance; |
| 95 | return sInstance; |
| 96 | } |
| 97 | |
| 98 | int WebViewFunctorManager::createFunctor(const WebViewFunctorCallbacks& callbacks, |
| 99 | RenderMode functorMode) { |
| 100 | auto object = std::make_unique<WebViewFunctor>(callbacks, functorMode); |
| 101 | int id = object->id(); |
| 102 | auto handle = object->createHandle(); |
| 103 | { |
| 104 | std::lock_guard _lock{mLock}; |
| 105 | mActiveFunctors.push_back(std::move(handle)); |
| 106 | mFunctors.push_back(std::move(object)); |
| 107 | } |
| 108 | return id; |
| 109 | } |
| 110 | |
| 111 | void WebViewFunctorManager::releaseFunctor(int functor) { |
| 112 | sp<WebViewFunctor::Handle> toRelease; |
| 113 | { |
| 114 | std::lock_guard _lock{mLock}; |
| 115 | for (auto iter = mActiveFunctors.begin(); iter != mActiveFunctors.end(); iter++) { |
| 116 | if ((*iter)->id() == functor) { |
| 117 | toRelease = std::move(*iter); |
| 118 | mActiveFunctors.erase(iter); |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void WebViewFunctorManager::onContextDestroyed() { |
| 126 | // WARNING: SKETCHY |
| 127 | // Because we know that we always remove from mFunctors on RenderThread, the same |
| 128 | // thread that always invokes onContextDestroyed, we know that the functor pointers |
| 129 | // will remain valid without the lock held. |
| 130 | // However, we won't block new functors from being added in the meantime. |
| 131 | mLock.lock(); |
| 132 | const size_t size = mFunctors.size(); |
| 133 | WebViewFunctor* toDestroyContext[size]; |
| 134 | for (size_t i = 0; i < size; i++) { |
| 135 | toDestroyContext[i] = mFunctors[i].get(); |
| 136 | } |
| 137 | mLock.unlock(); |
| 138 | for (size_t i = 0; i < size; i++) { |
| 139 | toDestroyContext[i]->destroyContext(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void WebViewFunctorManager::destroyFunctor(int functor) { |
| 144 | std::unique_ptr<WebViewFunctor> toRelease; |
| 145 | { |
| 146 | std::lock_guard _lock{mLock}; |
| 147 | for (auto iter = mFunctors.begin(); iter != mFunctors.end(); iter++) { |
| 148 | if ((*iter)->id() == functor) { |
| 149 | toRelease = std::move(*iter); |
| 150 | mFunctors.erase(iter); |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | sp<WebViewFunctor::Handle> WebViewFunctorManager::handleFor(int functor) { |
| 158 | std::lock_guard _lock{mLock}; |
| 159 | for (auto& iter : mActiveFunctors) { |
| 160 | if (iter->id() == functor) { |
| 161 | return iter; |
| 162 | } |
| 163 | } |
| 164 | return nullptr; |
| 165 | } |
| 166 | |
| 167 | } // namespace android::uirenderer |