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 #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 
26 namespace android::uirenderer {
27 
28 class WebViewFunctorManager;
29 
30 class WebViewFunctor {
31 public:
32     WebViewFunctor(void* data, const WebViewFunctorCallbacks& callbacks, RenderMode functorMode);
33     ~WebViewFunctor();
34 
35     class Handle : public LightRefBase<Handle> {
36     public:
~Handle()37         ~Handle() { renderthread::RenderProxy::destroyFunctor(id()); }
38 
id()39         int id() const { return mReference.id(); }
40 
sync(const WebViewSyncData & syncData)41         void sync(const WebViewSyncData& syncData) const { mReference.sync(syncData); }
42 
drawGl(const DrawGlInfo & drawInfo)43         void drawGl(const DrawGlInfo& drawInfo) const { mReference.drawGl(drawInfo); }
44 
initVk(const VkFunctorInitParams & params)45         void initVk(const VkFunctorInitParams& params) { mReference.initVk(params); }
46 
drawVk(const VkFunctorDrawParams & params)47         void drawVk(const VkFunctorDrawParams& params) { mReference.drawVk(params); }
48 
postDrawVk()49         void postDrawVk() { mReference.postDrawVk(); }
50 
51     private:
52         friend class WebViewFunctor;
53 
Handle(WebViewFunctor & ref)54         Handle(WebViewFunctor& ref) : mReference(ref) {}
55 
56         WebViewFunctor& mReference;
57     };
58 
id()59     int id() const { return mFunctor; }
60     void sync(const WebViewSyncData& syncData) const;
61     void drawGl(const DrawGlInfo& drawInfo);
62     void initVk(const VkFunctorInitParams& params);
63     void drawVk(const VkFunctorDrawParams& params);
64     void postDrawVk();
65     void destroyContext();
66 
createHandle()67     sp<Handle> createHandle() {
68         LOG_ALWAYS_FATAL_IF(mCreatedHandle);
69         mCreatedHandle = true;
70         return sp<Handle>{new Handle(*this)};
71     }
72 
73 private:
74     WebViewFunctorCallbacks mCallbacks;
75     void* const mData;
76     int mFunctor;
77     RenderMode mMode;
78     bool mHasContext = false;
79     bool mCreatedHandle = false;
80 };
81 
82 class WebViewFunctorManager {
83 public:
84     static WebViewFunctorManager& instance();
85 
86     int createFunctor(void* data, const WebViewFunctorCallbacks& callbacks, RenderMode functorMode);
87     void releaseFunctor(int functor);
88     void onContextDestroyed();
89     void destroyFunctor(int functor);
90 
91     sp<WebViewFunctor::Handle> handleFor(int functor);
92 
93 private:
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
103