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 #ifdef __ANDROID__ // Layoutlib does not support render thread 21 #include <renderthread/RenderProxy.h> 22 #endif 23 24 #include <utils/LightRefBase.h> 25 #include <utils/Log.h> 26 #include <utils/StrongPointer.h> 27 #include <mutex> 28 #include <vector> 29 30 namespace android::uirenderer { 31 32 class WebViewFunctorManager; 33 34 class WebViewFunctor { 35 public: 36 WebViewFunctor(void* data, const WebViewFunctorCallbacks& callbacks, RenderMode functorMode); 37 ~WebViewFunctor(); 38 39 class Handle : public LightRefBase<Handle> { 40 public: ~Handle()41 ~Handle() { 42 #ifdef __ANDROID__ // Layoutlib does not support render thread 43 renderthread::RenderProxy::destroyFunctor(id()); 44 #endif 45 } 46 id()47 int id() const { return mReference.id(); } 48 sync(const WebViewSyncData & syncData)49 void sync(const WebViewSyncData& syncData) const { mReference.sync(syncData); } 50 drawGl(const DrawGlInfo & drawInfo)51 void drawGl(const DrawGlInfo& drawInfo) const { mReference.drawGl(drawInfo); } 52 initVk(const VkFunctorInitParams & params)53 void initVk(const VkFunctorInitParams& params) { mReference.initVk(params); } 54 drawVk(const VkFunctorDrawParams & params)55 void drawVk(const VkFunctorDrawParams& params) { mReference.drawVk(params); } 56 postDrawVk()57 void postDrawVk() { mReference.postDrawVk(); } 58 removeOverlays()59 void removeOverlays() { mReference.removeOverlays(); } 60 onRemovedFromTree()61 void onRemovedFromTree() { mReference.onRemovedFromTree(); } 62 63 private: 64 friend class WebViewFunctor; 65 Handle(WebViewFunctor & ref)66 Handle(WebViewFunctor& ref) : mReference(ref) {} 67 68 WebViewFunctor& mReference; 69 }; 70 id()71 int id() const { return mFunctor; } 72 void sync(const WebViewSyncData& syncData) const; 73 void drawGl(const DrawGlInfo& drawInfo); 74 void initVk(const VkFunctorInitParams& params); 75 void drawVk(const VkFunctorDrawParams& params); 76 void postDrawVk(); 77 void destroyContext(); 78 void removeOverlays(); 79 void onRemovedFromTree(); 80 81 ASurfaceControl* getSurfaceControl(); 82 void mergeTransaction(ASurfaceTransaction* transaction); 83 createHandle()84 sp<Handle> createHandle() { 85 LOG_ALWAYS_FATAL_IF(mCreatedHandle); 86 mCreatedHandle = true; 87 return sp<Handle>{new Handle(*this)}; 88 } 89 90 private: 91 void reparentSurfaceControl(ASurfaceControl* parent); 92 93 private: 94 WebViewFunctorCallbacks mCallbacks; 95 void* const mData; 96 int mFunctor; 97 RenderMode mMode; 98 bool mHasContext = false; 99 bool mCreatedHandle = false; 100 int32_t mParentSurfaceControlGenerationId = 0; 101 ASurfaceControl* mSurfaceControl = nullptr; 102 }; 103 104 class WebViewFunctorManager { 105 public: 106 static WebViewFunctorManager& instance(); 107 108 int createFunctor(void* data, const WebViewFunctorCallbacks& callbacks, RenderMode functorMode); 109 void releaseFunctor(int functor); 110 void onContextDestroyed(); 111 void destroyFunctor(int functor); 112 113 sp<WebViewFunctor::Handle> handleFor(int functor); 114 115 private: 116 WebViewFunctorManager() = default; 117 ~WebViewFunctorManager() = default; 118 119 std::mutex mLock; 120 std::vector<std::unique_ptr<WebViewFunctor>> mFunctors; 121 std::vector<sp<WebViewFunctor::Handle>> mActiveFunctors; 122 }; 123 124 } // namespace android::uirenderer 125