Home | History | Annotate | Download | only in hwui
      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(void* data, 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         void initVk(const VkFunctorInitParams& params) { mReference.initVk(params); }
     46 
     47         void drawVk(const VkFunctorDrawParams& params) { mReference.drawVk(params); }
     48 
     49         void postDrawVk() { mReference.postDrawVk(); }
     50 
     51     private:
     52         friend class WebViewFunctor;
     53 
     54         Handle(WebViewFunctor& ref) : mReference(ref) {}
     55 
     56         WebViewFunctor& mReference;
     57     };
     58 
     59     int id() const { return mFunctor; }
     60     void sync(const WebViewSyncData& syncData) const;
     61     void drawGl(const DrawGlInfo& drawInfo);
     62     void initVk(const VkFunctorInitParams& params);
     63     void drawVk(const VkFunctorDrawParams& params);
     64     void postDrawVk();
     65     void destroyContext();
     66 
     67     sp<Handle> createHandle() {
     68         LOG_ALWAYS_FATAL_IF(mCreatedHandle);
     69         mCreatedHandle = true;
     70         return sp<Handle>{new Handle(*this)};
     71     }
     72 
     73 private:
     74     WebViewFunctorCallbacks mCallbacks;
     75     void* const mData;
     76     int mFunctor;
     77     RenderMode mMode;
     78     bool mHasContext = false;
     79     bool mCreatedHandle = false;
     80 };
     81 
     82 class WebViewFunctorManager {
     83 public:
     84     static WebViewFunctorManager& instance();
     85 
     86     int createFunctor(void* data, const WebViewFunctorCallbacks& callbacks, RenderMode functorMode);
     87     void releaseFunctor(int functor);
     88     void onContextDestroyed();
     89     void destroyFunctor(int functor);
     90 
     91     sp<WebViewFunctor::Handle> handleFor(int functor);
     92 
     93 private:
     94     WebViewFunctorManager() = default;
     95     ~WebViewFunctorManager() = default;
     96 
     97     std::mutex mLock;
     98     std::vector<std::unique_ptr<WebViewFunctor>> mFunctors;
     99     std::vector<sp<WebViewFunctor::Handle>> mActiveFunctors;
    100 };
    101 
    102 }  // namespace android::uirenderer
    103