blob: 9170d6d1dc508ac678cdb175f97ebbeb536219f9 [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"
Bo Liu1b0278c2019-01-03 16:36:24 -080021#include "renderthread/RenderThread.h"
John Reck283bb462018-12-13 16:40:14 -080022
23#include <log/log.h>
24#include <utils/Trace.h>
25#include <atomic>
26
27namespace android::uirenderer {
28
29RenderMode WebViewFunctor_queryPlatformRenderMode() {
30 auto pipelineType = Properties::getRenderPipelineType();
31 switch (pipelineType) {
32 case RenderPipelineType::SkiaGL:
33 return RenderMode::OpenGL_ES;
34 case RenderPipelineType::SkiaVulkan:
35 return RenderMode::Vulkan;
36 default:
37 LOG_ALWAYS_FATAL("Unknown render pipeline type: %d", (int)pipelineType);
38 }
39}
40
Bo Liud6668e72018-12-14 19:37:41 -080041int WebViewFunctor_create(void* data, const WebViewFunctorCallbacks& prototype,
42 RenderMode functorMode) {
John Reck283bb462018-12-13 16:40:14 -080043 if (functorMode != RenderMode::OpenGL_ES && functorMode != RenderMode::Vulkan) {
44 ALOGW("Unknown rendermode %d", (int)functorMode);
45 return -1;
46 }
47 if (functorMode == RenderMode::Vulkan &&
48 WebViewFunctor_queryPlatformRenderMode() != RenderMode::Vulkan) {
49 ALOGW("Unable to map from GLES platform to a vulkan functor");
50 return -1;
51 }
Bo Liud6668e72018-12-14 19:37:41 -080052 return WebViewFunctorManager::instance().createFunctor(data, prototype, functorMode);
John Reck283bb462018-12-13 16:40:14 -080053}
54
55void WebViewFunctor_release(int functor) {
56 WebViewFunctorManager::instance().releaseFunctor(functor);
57}
58
59static std::atomic_int sNextId{1};
60
Bo Liud6668e72018-12-14 19:37:41 -080061WebViewFunctor::WebViewFunctor(void* data, const WebViewFunctorCallbacks& callbacks,
62 RenderMode functorMode)
63 : mData(data) {
John Reck283bb462018-12-13 16:40:14 -080064 mFunctor = sNextId++;
65 mCallbacks = callbacks;
66 mMode = functorMode;
67}
68
69WebViewFunctor::~WebViewFunctor() {
70 destroyContext();
71
72 ATRACE_NAME("WebViewFunctor::onDestroy");
Bo Liud6668e72018-12-14 19:37:41 -080073 mCallbacks.onDestroyed(mFunctor, mData);
John Reck283bb462018-12-13 16:40:14 -080074}
75
76void WebViewFunctor::sync(const WebViewSyncData& syncData) const {
77 ATRACE_NAME("WebViewFunctor::sync");
Bo Liud6668e72018-12-14 19:37:41 -080078 mCallbacks.onSync(mFunctor, mData, syncData);
John Reck283bb462018-12-13 16:40:14 -080079}
80
81void WebViewFunctor::drawGl(const DrawGlInfo& drawInfo) {
82 ATRACE_NAME("WebViewFunctor::drawGl");
83 if (!mHasContext) {
84 mHasContext = true;
85 }
Bo Liud6668e72018-12-14 19:37:41 -080086 mCallbacks.gles.draw(mFunctor, mData, drawInfo);
John Reck283bb462018-12-13 16:40:14 -080087}
88
89void WebViewFunctor::destroyContext() {
90 if (mHasContext) {
91 mHasContext = false;
92 ATRACE_NAME("WebViewFunctor::onContextDestroyed");
Bo Liud6668e72018-12-14 19:37:41 -080093 mCallbacks.onContextDestroyed(mFunctor, mData);
Bo Liu1b0278c2019-01-03 16:36:24 -080094
95 // grContext may be null in unit tests.
96 auto* grContext = renderthread::RenderThread::getInstance().getGrContext();
97 if (grContext) grContext->resetContext();
John Reck283bb462018-12-13 16:40:14 -080098 }
99}
100
101WebViewFunctorManager& WebViewFunctorManager::instance() {
102 static WebViewFunctorManager sInstance;
103 return sInstance;
104}
105
Bo Liud6668e72018-12-14 19:37:41 -0800106int WebViewFunctorManager::createFunctor(void* data, const WebViewFunctorCallbacks& callbacks,
John Reck283bb462018-12-13 16:40:14 -0800107 RenderMode functorMode) {
Bo Liud6668e72018-12-14 19:37:41 -0800108 auto object = std::make_unique<WebViewFunctor>(data, callbacks, functorMode);
John Reck283bb462018-12-13 16:40:14 -0800109 int id = object->id();
110 auto handle = object->createHandle();
111 {
112 std::lock_guard _lock{mLock};
113 mActiveFunctors.push_back(std::move(handle));
114 mFunctors.push_back(std::move(object));
115 }
116 return id;
117}
118
119void WebViewFunctorManager::releaseFunctor(int functor) {
120 sp<WebViewFunctor::Handle> toRelease;
121 {
122 std::lock_guard _lock{mLock};
123 for (auto iter = mActiveFunctors.begin(); iter != mActiveFunctors.end(); iter++) {
124 if ((*iter)->id() == functor) {
125 toRelease = std::move(*iter);
126 mActiveFunctors.erase(iter);
127 break;
128 }
129 }
130 }
131}
132
133void WebViewFunctorManager::onContextDestroyed() {
134 // WARNING: SKETCHY
135 // Because we know that we always remove from mFunctors on RenderThread, the same
136 // thread that always invokes onContextDestroyed, we know that the functor pointers
137 // will remain valid without the lock held.
138 // However, we won't block new functors from being added in the meantime.
139 mLock.lock();
140 const size_t size = mFunctors.size();
141 WebViewFunctor* toDestroyContext[size];
142 for (size_t i = 0; i < size; i++) {
143 toDestroyContext[i] = mFunctors[i].get();
144 }
145 mLock.unlock();
146 for (size_t i = 0; i < size; i++) {
147 toDestroyContext[i]->destroyContext();
148 }
149}
150
151void WebViewFunctorManager::destroyFunctor(int functor) {
152 std::unique_ptr<WebViewFunctor> toRelease;
153 {
154 std::lock_guard _lock{mLock};
155 for (auto iter = mFunctors.begin(); iter != mFunctors.end(); iter++) {
156 if ((*iter)->id() == functor) {
157 toRelease = std::move(*iter);
158 mFunctors.erase(iter);
159 break;
160 }
161 }
162 }
163}
164
165sp<WebViewFunctor::Handle> WebViewFunctorManager::handleFor(int functor) {
166 std::lock_guard _lock{mLock};
167 for (auto& iter : mActiveFunctors) {
168 if (iter->id() == functor) {
169 return iter;
170 }
171 }
172 return nullptr;
173}
174
Bo Liud6668e72018-12-14 19:37:41 -0800175} // namespace android::uirenderer