blob: 2846cb1f087bfa708d8eda138adc0c935928577d [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#pragma once
18
19#include <private/hwui/WebViewFunctor.h>
20#include <renderthread/RenderProxy.h>
21
22#include <utils/LightRefBase.h>
23#include <mutex>
24#include <vector>
25
26namespace android::uirenderer {
27
28class WebViewFunctorManager;
29
30class WebViewFunctor {
31public:
Bo Liud6668e72018-12-14 19:37:41 -080032 WebViewFunctor(void* data, const WebViewFunctorCallbacks& callbacks, RenderMode functorMode);
John Reck283bb462018-12-13 16:40:14 -080033 ~WebViewFunctor();
34
35 class Handle : public LightRefBase<Handle> {
36 public:
37 ~Handle() { renderthread::RenderProxy::destroyFunctor(id()); }
38
39 int id() const { return mReference.id(); }
40
41 void sync(const WebViewSyncData& syncData) const { mReference.sync(syncData); }
42
43 void drawGl(const DrawGlInfo& drawInfo) const { mReference.drawGl(drawInfo); }
44
Bo Liu7b8c1eb2019-01-08 20:17:55 -080045 void initVk(const VkFunctorInitParams& params) { mReference.initVk(params); }
46
47 void drawVk(const VkFunctorDrawParams& params) { mReference.drawVk(params); }
48
49 void postDrawVk() { mReference.postDrawVk(); }
50
John Reck283bb462018-12-13 16:40:14 -080051 private:
52 friend class WebViewFunctor;
53
54 Handle(WebViewFunctor& ref) : mReference(ref) {}
55
56 WebViewFunctor& mReference;
57 };
58
59 int id() const { return mFunctor; }
60 void sync(const WebViewSyncData& syncData) const;
61 void drawGl(const DrawGlInfo& drawInfo);
Bo Liu7b8c1eb2019-01-08 20:17:55 -080062 void initVk(const VkFunctorInitParams& params);
63 void drawVk(const VkFunctorDrawParams& params);
64 void postDrawVk();
John Reck283bb462018-12-13 16:40:14 -080065 void destroyContext();
66
67 sp<Handle> createHandle() {
68 LOG_ALWAYS_FATAL_IF(mCreatedHandle);
69 mCreatedHandle = true;
70 return sp<Handle>{new Handle(*this)};
71 }
72
73private:
74 WebViewFunctorCallbacks mCallbacks;
Bo Liud6668e72018-12-14 19:37:41 -080075 void* const mData;
John Reck283bb462018-12-13 16:40:14 -080076 int mFunctor;
77 RenderMode mMode;
78 bool mHasContext = false;
79 bool mCreatedHandle = false;
80};
81
82class WebViewFunctorManager {
83public:
84 static WebViewFunctorManager& instance();
85
Bo Liud6668e72018-12-14 19:37:41 -080086 int createFunctor(void* data, const WebViewFunctorCallbacks& callbacks, RenderMode functorMode);
John Reck283bb462018-12-13 16:40:14 -080087 void releaseFunctor(int functor);
88 void onContextDestroyed();
89 void destroyFunctor(int functor);
90
91 sp<WebViewFunctor::Handle> handleFor(int functor);
92
93private:
94 WebViewFunctorManager() = default;
95 ~WebViewFunctorManager() = default;
96
97 std::mutex mLock;
98 std::vector<std::unique_ptr<WebViewFunctor>> mFunctors;
99 std::vector<sp<WebViewFunctor::Handle>> mActiveFunctors;
100};
101
102} // namespace android::uirenderer