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> |
| 21 | |
| 22 | #include <utils/LightRefBase.h> |
| 23 | #include <mutex> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace android::uirenderer { |
| 27 | |
| 28 | class WebViewFunctorManager; |
| 29 | |
| 30 | class WebViewFunctor { |
| 31 | public: |
| 32 | WebViewFunctor(const WebViewFunctorCallbacks& callbacks, RenderMode functorMode); |
| 33 | ~WebViewFunctor(); |
| 34 | |
| 35 | class Handle : public LightRefBase<Handle> { |
| 36 | public: |
| 37 | ~Handle() { renderthread::RenderProxy::destroyFunctor(id()); } |
| 38 | |
| 39 | int id() const { return mReference.id(); } |
| 40 | |
| 41 | void sync(const WebViewSyncData& syncData) const { mReference.sync(syncData); } |
| 42 | |
| 43 | void drawGl(const DrawGlInfo& drawInfo) const { mReference.drawGl(drawInfo); } |
| 44 | |
| 45 | private: |
| 46 | friend class WebViewFunctor; |
| 47 | |
| 48 | Handle(WebViewFunctor& ref) : mReference(ref) {} |
| 49 | |
| 50 | WebViewFunctor& mReference; |
| 51 | }; |
| 52 | |
| 53 | int id() const { return mFunctor; } |
| 54 | void sync(const WebViewSyncData& syncData) const; |
| 55 | void drawGl(const DrawGlInfo& drawInfo); |
| 56 | void destroyContext(); |
| 57 | |
| 58 | sp<Handle> createHandle() { |
| 59 | LOG_ALWAYS_FATAL_IF(mCreatedHandle); |
| 60 | mCreatedHandle = true; |
| 61 | return sp<Handle>{new Handle(*this)}; |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | WebViewFunctorCallbacks mCallbacks; |
| 66 | int mFunctor; |
| 67 | RenderMode mMode; |
| 68 | bool mHasContext = false; |
| 69 | bool mCreatedHandle = false; |
| 70 | }; |
| 71 | |
| 72 | class WebViewFunctorManager { |
| 73 | public: |
| 74 | static WebViewFunctorManager& instance(); |
| 75 | |
| 76 | int createFunctor(const WebViewFunctorCallbacks& callbacks, RenderMode functorMode); |
| 77 | void releaseFunctor(int functor); |
| 78 | void onContextDestroyed(); |
| 79 | void destroyFunctor(int functor); |
| 80 | |
| 81 | sp<WebViewFunctor::Handle> handleFor(int functor); |
| 82 | |
| 83 | private: |
| 84 | WebViewFunctorManager() = default; |
| 85 | ~WebViewFunctorManager() = default; |
| 86 | |
| 87 | std::mutex mLock; |
| 88 | std::vector<std::unique_ptr<WebViewFunctor>> mFunctors; |
| 89 | std::vector<sp<WebViewFunctor::Handle>> mActiveFunctors; |
| 90 | }; |
| 91 | |
| 92 | } // namespace android::uirenderer |