John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 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> |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 21 | #include <utils/LightRefBase.h> |
John Reck | 844516c | 2021-01-13 10:59:10 -0500 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | #include <utils/StrongPointer.h> |
Jerome Gaillard | 8ab756d | 2024-04-02 17:38:20 +0100 | [diff] [blame] | 24 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 25 | #include <mutex> |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace android::uirenderer { |
| 29 | |
| 30 | class WebViewFunctorManager; |
| 31 | |
| 32 | class WebViewFunctor { |
| 33 | public: |
Bo Liu | d6668e7 | 2018-12-14 19:37:41 -0800 | [diff] [blame] | 34 | WebViewFunctor(void* data, const WebViewFunctorCallbacks& callbacks, RenderMode functorMode); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 35 | ~WebViewFunctor(); |
| 36 | |
| 37 | class Handle : public LightRefBase<Handle> { |
| 38 | public: |
Jerome Gaillard | 8ab756d | 2024-04-02 17:38:20 +0100 | [diff] [blame] | 39 | ~Handle() { renderthread::RenderProxy::destroyFunctor(id()); } |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 40 | |
| 41 | int id() const { return mReference.id(); } |
| 42 | |
| 43 | void sync(const WebViewSyncData& syncData) const { mReference.sync(syncData); } |
| 44 | |
| 45 | void drawGl(const DrawGlInfo& drawInfo) const { mReference.drawGl(drawInfo); } |
| 46 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 47 | void initVk(const VkFunctorInitParams& params) { mReference.initVk(params); } |
| 48 | |
| 49 | void drawVk(const VkFunctorDrawParams& params) { mReference.drawVk(params); } |
| 50 | |
| 51 | void postDrawVk() { mReference.postDrawVk(); } |
| 52 | |
Vasiliy Telezhnikov | 6b23764 | 2020-11-12 18:14:58 -0500 | [diff] [blame] | 53 | void removeOverlays() { mReference.removeOverlays(); } |
| 54 | |
Huihong Luo | ec68b7c | 2021-06-25 21:54:16 -0700 | [diff] [blame] | 55 | void onRemovedFromTree() { mReference.onRemovedFromTree(); } |
| 56 | |
Igor Kraskevich | 2cec158 | 2024-03-13 11:23:40 +0000 | [diff] [blame] | 57 | const std::vector<int32_t>& getRenderingThreads() const { |
| 58 | return mReference.getRenderingThreads(); |
| 59 | } |
| 60 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 61 | 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 Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 72 | void initVk(const VkFunctorInitParams& params); |
| 73 | void drawVk(const VkFunctorDrawParams& params); |
| 74 | void postDrawVk(); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 75 | void destroyContext(); |
Vasiliy Telezhnikov | 6b23764 | 2020-11-12 18:14:58 -0500 | [diff] [blame] | 76 | void removeOverlays(); |
Huihong Luo | ec68b7c | 2021-06-25 21:54:16 -0700 | [diff] [blame] | 77 | void onRemovedFromTree(); |
Vasiliy Telezhnikov | 6b23764 | 2020-11-12 18:14:58 -0500 | [diff] [blame] | 78 | |
| 79 | ASurfaceControl* getSurfaceControl(); |
| 80 | void mergeTransaction(ASurfaceTransaction* transaction); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 81 | |
Igor Kraskevich | 63ebd83 | 2024-03-12 11:51:35 +0000 | [diff] [blame] | 82 | void reportRenderingThreads(const int32_t* thread_ids, size_t size); |
Igor Kraskevich | 2cec158 | 2024-03-13 11:23:40 +0000 | [diff] [blame] | 83 | const std::vector<int32_t>& getRenderingThreads() const { return mRenderingThreads; } |
Igor Kraskevich | 63ebd83 | 2024-03-12 11:51:35 +0000 | [diff] [blame] | 84 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 85 | sp<Handle> createHandle() { |
| 86 | LOG_ALWAYS_FATAL_IF(mCreatedHandle); |
| 87 | mCreatedHandle = true; |
| 88 | return sp<Handle>{new Handle(*this)}; |
| 89 | } |
| 90 | |
| 91 | private: |
Vasiliy Telezhnikov | 372a21b | 2021-11-17 13:50:56 -0500 | [diff] [blame] | 92 | bool prepareRootSurfaceControl(); |
Huihong Luo | 540fdf8 | 2021-06-25 13:59:39 -0700 | [diff] [blame] | 93 | void reparentSurfaceControl(ASurfaceControl* parent); |
| 94 | |
| 95 | private: |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 96 | WebViewFunctorCallbacks mCallbacks; |
Bo Liu | d6668e7 | 2018-12-14 19:37:41 -0800 | [diff] [blame] | 97 | void* const mData; |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 98 | int mFunctor; |
| 99 | RenderMode mMode; |
| 100 | bool mHasContext = false; |
| 101 | bool mCreatedHandle = false; |
Huihong Luo | 540fdf8 | 2021-06-25 13:59:39 -0700 | [diff] [blame] | 102 | int32_t mParentSurfaceControlGenerationId = 0; |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 103 | ASurfaceControl* mSurfaceControl = nullptr; |
Igor Kraskevich | 2cec158 | 2024-03-13 11:23:40 +0000 | [diff] [blame] | 104 | std::vector<int32_t> mRenderingThreads; |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | class WebViewFunctorManager { |
| 108 | public: |
| 109 | static WebViewFunctorManager& instance(); |
| 110 | |
Bo Liu | d6668e7 | 2018-12-14 19:37:41 -0800 | [diff] [blame] | 111 | int createFunctor(void* data, const WebViewFunctorCallbacks& callbacks, RenderMode functorMode); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 112 | void releaseFunctor(int functor); |
| 113 | void onContextDestroyed(); |
| 114 | void destroyFunctor(int functor); |
Igor Kraskevich | 63ebd83 | 2024-03-12 11:51:35 +0000 | [diff] [blame] | 115 | void reportRenderingThreads(int functor, const int32_t* thread_ids, size_t size); |
Igor Kraskevich | 2cec158 | 2024-03-13 11:23:40 +0000 | [diff] [blame] | 116 | std::vector<int32_t> getRenderingThreadsForActiveFunctors(); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 117 | |
| 118 | sp<WebViewFunctor::Handle> handleFor(int functor); |
| 119 | |
| 120 | private: |
| 121 | WebViewFunctorManager() = default; |
| 122 | ~WebViewFunctorManager() = default; |
| 123 | |
| 124 | std::mutex mLock; |
| 125 | std::vector<std::unique_ptr<WebViewFunctor>> mFunctors; |
| 126 | std::vector<sp<WebViewFunctor::Handle>> mActiveFunctors; |
| 127 | }; |
| 128 | |
| 129 | } // namespace android::uirenderer |