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