blob: 5b7ae70e821cea66ba2e6c1df20c3a296157abb6 [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"
21
22#include <log/log.h>
23#include <utils/Trace.h>
24#include <atomic>
25
26namespace android::uirenderer {
27
28RenderMode 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
Bo Liud6668e72018-12-14 19:37:41 -080040int WebViewFunctor_create(void* data, const WebViewFunctorCallbacks& prototype,
41 RenderMode functorMode) {
John Reck283bb462018-12-13 16:40:14 -080042 if (functorMode != RenderMode::OpenGL_ES && functorMode != RenderMode::Vulkan) {
43 ALOGW("Unknown rendermode %d", (int)functorMode);
44 return -1;
45 }
46 if (functorMode == RenderMode::Vulkan &&
47 WebViewFunctor_queryPlatformRenderMode() != RenderMode::Vulkan) {
48 ALOGW("Unable to map from GLES platform to a vulkan functor");
49 return -1;
50 }
Bo Liud6668e72018-12-14 19:37:41 -080051 return WebViewFunctorManager::instance().createFunctor(data, prototype, functorMode);
John Reck283bb462018-12-13 16:40:14 -080052}
53
54void WebViewFunctor_release(int functor) {
55 WebViewFunctorManager::instance().releaseFunctor(functor);
56}
57
58static std::atomic_int sNextId{1};
59
Bo Liud6668e72018-12-14 19:37:41 -080060WebViewFunctor::WebViewFunctor(void* data, const WebViewFunctorCallbacks& callbacks,
61 RenderMode functorMode)
62 : mData(data) {
John Reck283bb462018-12-13 16:40:14 -080063 mFunctor = sNextId++;
64 mCallbacks = callbacks;
65 mMode = functorMode;
66}
67
68WebViewFunctor::~WebViewFunctor() {
69 destroyContext();
70
71 ATRACE_NAME("WebViewFunctor::onDestroy");
Bo Liud6668e72018-12-14 19:37:41 -080072 mCallbacks.onDestroyed(mFunctor, mData);
John Reck283bb462018-12-13 16:40:14 -080073}
74
75void WebViewFunctor::sync(const WebViewSyncData& syncData) const {
76 ATRACE_NAME("WebViewFunctor::sync");
Bo Liud6668e72018-12-14 19:37:41 -080077 mCallbacks.onSync(mFunctor, mData, syncData);
John Reck283bb462018-12-13 16:40:14 -080078}
79
80void WebViewFunctor::drawGl(const DrawGlInfo& drawInfo) {
81 ATRACE_NAME("WebViewFunctor::drawGl");
82 if (!mHasContext) {
83 mHasContext = true;
84 }
Bo Liud6668e72018-12-14 19:37:41 -080085 mCallbacks.gles.draw(mFunctor, mData, drawInfo);
John Reck283bb462018-12-13 16:40:14 -080086}
87
88void WebViewFunctor::destroyContext() {
89 if (mHasContext) {
90 mHasContext = false;
91 ATRACE_NAME("WebViewFunctor::onContextDestroyed");
Bo Liud6668e72018-12-14 19:37:41 -080092 mCallbacks.onContextDestroyed(mFunctor, mData);
John Reck283bb462018-12-13 16:40:14 -080093 }
94}
95
96WebViewFunctorManager& WebViewFunctorManager::instance() {
97 static WebViewFunctorManager sInstance;
98 return sInstance;
99}
100
Bo Liud6668e72018-12-14 19:37:41 -0800101int WebViewFunctorManager::createFunctor(void* data, const WebViewFunctorCallbacks& callbacks,
John Reck283bb462018-12-13 16:40:14 -0800102 RenderMode functorMode) {
Bo Liud6668e72018-12-14 19:37:41 -0800103 auto object = std::make_unique<WebViewFunctor>(data, callbacks, functorMode);
John Reck283bb462018-12-13 16:40:14 -0800104 int id = object->id();
105 auto handle = object->createHandle();
106 {
107 std::lock_guard _lock{mLock};
108 mActiveFunctors.push_back(std::move(handle));
109 mFunctors.push_back(std::move(object));
110 }
111 return id;
112}
113
114void WebViewFunctorManager::releaseFunctor(int functor) {
115 sp<WebViewFunctor::Handle> toRelease;
116 {
117 std::lock_guard _lock{mLock};
118 for (auto iter = mActiveFunctors.begin(); iter != mActiveFunctors.end(); iter++) {
119 if ((*iter)->id() == functor) {
120 toRelease = std::move(*iter);
121 mActiveFunctors.erase(iter);
122 break;
123 }
124 }
125 }
126}
127
128void WebViewFunctorManager::onContextDestroyed() {
129 // WARNING: SKETCHY
130 // Because we know that we always remove from mFunctors on RenderThread, the same
131 // thread that always invokes onContextDestroyed, we know that the functor pointers
132 // will remain valid without the lock held.
133 // However, we won't block new functors from being added in the meantime.
134 mLock.lock();
135 const size_t size = mFunctors.size();
136 WebViewFunctor* toDestroyContext[size];
137 for (size_t i = 0; i < size; i++) {
138 toDestroyContext[i] = mFunctors[i].get();
139 }
140 mLock.unlock();
141 for (size_t i = 0; i < size; i++) {
142 toDestroyContext[i]->destroyContext();
143 }
144}
145
146void WebViewFunctorManager::destroyFunctor(int functor) {
147 std::unique_ptr<WebViewFunctor> toRelease;
148 {
149 std::lock_guard _lock{mLock};
150 for (auto iter = mFunctors.begin(); iter != mFunctors.end(); iter++) {
151 if ((*iter)->id() == functor) {
152 toRelease = std::move(*iter);
153 mFunctors.erase(iter);
154 break;
155 }
156 }
157 }
158}
159
160sp<WebViewFunctor::Handle> WebViewFunctorManager::handleFor(int functor) {
161 std::lock_guard _lock{mLock};
162 for (auto& iter : mActiveFunctors) {
163 if (iter->id() == functor) {
164 return iter;
165 }
166 }
167 return nullptr;
168}
169
Bo Liud6668e72018-12-14 19:37:41 -0800170} // namespace android::uirenderer