blob: 737d60525aa9c3bef1ea9c1dd6729dbf5e4b775c [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>
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010020#ifdef __ANDROID__ // Layoutlib does not support render thread
John Reck283bb462018-12-13 16:40:14 -080021#include <renderthread/RenderProxy.h>
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010022#else
23#include <utils/Log.h>
24#endif
John Reck283bb462018-12-13 16:40:14 -080025
26#include <utils/LightRefBase.h>
27#include <mutex>
28#include <vector>
29
30namespace android::uirenderer {
31
32class WebViewFunctorManager;
33
34class WebViewFunctor {
35public:
Bo Liud6668e72018-12-14 19:37:41 -080036 WebViewFunctor(void* data, const WebViewFunctorCallbacks& callbacks, RenderMode functorMode);
John Reck283bb462018-12-13 16:40:14 -080037 ~WebViewFunctor();
38
39 class Handle : public LightRefBase<Handle> {
40 public:
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010041 ~Handle() {
42#ifdef __ANDROID__ // Layoutlib does not support render thread
43 renderthread::RenderProxy::destroyFunctor(id());
44#endif
45 }
John Reck283bb462018-12-13 16:40:14 -080046
47 int id() const { return mReference.id(); }
48
49 void sync(const WebViewSyncData& syncData) const { mReference.sync(syncData); }
50
51 void drawGl(const DrawGlInfo& drawInfo) const { mReference.drawGl(drawInfo); }
52
Bo Liu7b8c1eb2019-01-08 20:17:55 -080053 void initVk(const VkFunctorInitParams& params) { mReference.initVk(params); }
54
55 void drawVk(const VkFunctorDrawParams& params) { mReference.drawVk(params); }
56
57 void postDrawVk() { mReference.postDrawVk(); }
58
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -050059 void removeOverlays() { mReference.removeOverlays(); }
60
John Reck283bb462018-12-13 16:40:14 -080061 private:
62 friend class WebViewFunctor;
63
64 Handle(WebViewFunctor& ref) : mReference(ref) {}
65
66 WebViewFunctor& mReference;
67 };
68
69 int id() const { return mFunctor; }
70 void sync(const WebViewSyncData& syncData) const;
71 void drawGl(const DrawGlInfo& drawInfo);
Bo Liu7b8c1eb2019-01-08 20:17:55 -080072 void initVk(const VkFunctorInitParams& params);
73 void drawVk(const VkFunctorDrawParams& params);
74 void postDrawVk();
John Reck283bb462018-12-13 16:40:14 -080075 void destroyContext();
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -050076 void removeOverlays();
77
78 ASurfaceControl* getSurfaceControl();
79 void mergeTransaction(ASurfaceTransaction* transaction);
John Reck283bb462018-12-13 16:40:14 -080080
81 sp<Handle> createHandle() {
82 LOG_ALWAYS_FATAL_IF(mCreatedHandle);
83 mCreatedHandle = true;
84 return sp<Handle>{new Handle(*this)};
85 }
86
87private:
88 WebViewFunctorCallbacks mCallbacks;
Bo Liud6668e72018-12-14 19:37:41 -080089 void* const mData;
John Reck283bb462018-12-13 16:40:14 -080090 int mFunctor;
91 RenderMode mMode;
92 bool mHasContext = false;
93 bool mCreatedHandle = false;
94};
95
96class WebViewFunctorManager {
97public:
98 static WebViewFunctorManager& instance();
99
Bo Liud6668e72018-12-14 19:37:41 -0800100 int createFunctor(void* data, const WebViewFunctorCallbacks& callbacks, RenderMode functorMode);
John Reck283bb462018-12-13 16:40:14 -0800101 void releaseFunctor(int functor);
102 void onContextDestroyed();
103 void destroyFunctor(int functor);
104
105 sp<WebViewFunctor::Handle> handleFor(int functor);
106
107private:
108 WebViewFunctorManager() = default;
109 ~WebViewFunctorManager() = default;
110
111 std::mutex mLock;
112 std::vector<std::unique_ptr<WebViewFunctor>> mFunctors;
113 std::vector<sp<WebViewFunctor::Handle>> mActiveFunctors;
114};
115
116} // namespace android::uirenderer