blob: 2d77dd8d09bcf105f49675edcb1604e4331a49a3 [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#endif
John Reck283bb462018-12-13 16:40:14 -080023
24#include <utils/LightRefBase.h>
John Reck844516c2021-01-13 10:59:10 -050025#include <utils/Log.h>
26#include <utils/StrongPointer.h>
John Reck283bb462018-12-13 16:40:14 -080027#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
Huihong Luoec68b7c2021-06-25 21:54:16 -070061 void onRemovedFromTree() { mReference.onRemovedFromTree(); }
62
Igor Kraskevich2cec1582024-03-13 11:23:40 +000063 const std::vector<int32_t>& getRenderingThreads() const {
64 return mReference.getRenderingThreads();
65 }
66
John Reck283bb462018-12-13 16:40:14 -080067 private:
68 friend class WebViewFunctor;
69
70 Handle(WebViewFunctor& ref) : mReference(ref) {}
71
72 WebViewFunctor& mReference;
73 };
74
75 int id() const { return mFunctor; }
76 void sync(const WebViewSyncData& syncData) const;
77 void drawGl(const DrawGlInfo& drawInfo);
Bo Liu7b8c1eb2019-01-08 20:17:55 -080078 void initVk(const VkFunctorInitParams& params);
79 void drawVk(const VkFunctorDrawParams& params);
80 void postDrawVk();
John Reck283bb462018-12-13 16:40:14 -080081 void destroyContext();
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -050082 void removeOverlays();
Huihong Luoec68b7c2021-06-25 21:54:16 -070083 void onRemovedFromTree();
Vasiliy Telezhnikov6b237642020-11-12 18:14:58 -050084
85 ASurfaceControl* getSurfaceControl();
86 void mergeTransaction(ASurfaceTransaction* transaction);
John Reck283bb462018-12-13 16:40:14 -080087
Igor Kraskevich63ebd832024-03-12 11:51:35 +000088 void reportRenderingThreads(const int32_t* thread_ids, size_t size);
Igor Kraskevich2cec1582024-03-13 11:23:40 +000089 const std::vector<int32_t>& getRenderingThreads() const { return mRenderingThreads; }
Igor Kraskevich63ebd832024-03-12 11:51:35 +000090
John Reck283bb462018-12-13 16:40:14 -080091 sp<Handle> createHandle() {
92 LOG_ALWAYS_FATAL_IF(mCreatedHandle);
93 mCreatedHandle = true;
94 return sp<Handle>{new Handle(*this)};
95 }
96
97private:
Vasiliy Telezhnikov372a21b2021-11-17 13:50:56 -050098 bool prepareRootSurfaceControl();
Huihong Luo540fdf82021-06-25 13:59:39 -070099 void reparentSurfaceControl(ASurfaceControl* parent);
100
101private:
John Reck283bb462018-12-13 16:40:14 -0800102 WebViewFunctorCallbacks mCallbacks;
Bo Liud6668e72018-12-14 19:37:41 -0800103 void* const mData;
John Reck283bb462018-12-13 16:40:14 -0800104 int mFunctor;
105 RenderMode mMode;
106 bool mHasContext = false;
107 bool mCreatedHandle = false;
Huihong Luo540fdf82021-06-25 13:59:39 -0700108 int32_t mParentSurfaceControlGenerationId = 0;
Huihong Luo054b8d32021-02-24 18:48:12 -0800109 ASurfaceControl* mSurfaceControl = nullptr;
Igor Kraskevich2cec1582024-03-13 11:23:40 +0000110 std::vector<int32_t> mRenderingThreads;
John Reck283bb462018-12-13 16:40:14 -0800111};
112
113class WebViewFunctorManager {
114public:
115 static WebViewFunctorManager& instance();
116
Bo Liud6668e72018-12-14 19:37:41 -0800117 int createFunctor(void* data, const WebViewFunctorCallbacks& callbacks, RenderMode functorMode);
John Reck283bb462018-12-13 16:40:14 -0800118 void releaseFunctor(int functor);
119 void onContextDestroyed();
120 void destroyFunctor(int functor);
Igor Kraskevich63ebd832024-03-12 11:51:35 +0000121 void reportRenderingThreads(int functor, const int32_t* thread_ids, size_t size);
Igor Kraskevich2cec1582024-03-13 11:23:40 +0000122 std::vector<int32_t> getRenderingThreadsForActiveFunctors();
John Reck283bb462018-12-13 16:40:14 -0800123
124 sp<WebViewFunctor::Handle> handleFor(int functor);
125
126private:
127 WebViewFunctorManager() = default;
128 ~WebViewFunctorManager() = default;
129
130 std::mutex mLock;
131 std::vector<std::unique_ptr<WebViewFunctor>> mFunctors;
132 std::vector<sp<WebViewFunctor::Handle>> mActiveFunctors;
133};
134
135} // namespace android::uirenderer