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