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 "GlFunctorLifecycleListener.h"
20 
21 #include <SkCanvas.h>
22 #include <SkDrawable.h>
23 
24 #include <WebViewFunctorManager.h>
25 #include <utils/Functor.h>
26 #include <variant>
27 
28 namespace android {
29 namespace uirenderer {
30 
31 namespace skiapipeline {
32 
33 /**
34  * This drawable wraps a functor enabling it to be recorded into a list
35  * of Skia drawing commands.
36  */
37 class FunctorDrawable : public SkDrawable {
38 public:
FunctorDrawable(Functor * functor,GlFunctorLifecycleListener * listener,SkCanvas * canvas)39     FunctorDrawable(Functor* functor, GlFunctorLifecycleListener* listener, SkCanvas* canvas)
40             : mBounds(canvas->getLocalClipBounds())
41             , mAnyFunctor(std::in_place_type<LegacyFunctor>, functor, listener) {}
42 
FunctorDrawable(int functor,SkCanvas * canvas)43     FunctorDrawable(int functor, SkCanvas* canvas)
44             : mBounds(canvas->getLocalClipBounds())
45             , mAnyFunctor(std::in_place_type<NewFunctor>, functor) {}
46 
~FunctorDrawable()47     virtual ~FunctorDrawable() {}
48 
syncFunctor(const WebViewSyncData & data)49     virtual void syncFunctor(const WebViewSyncData& data) const {
50         if (mAnyFunctor.index() == 0) {
51             std::get<0>(mAnyFunctor).handle->sync(data);
52         } else {
53             (*(std::get<1>(mAnyFunctor).functor))(DrawGlInfo::kModeSync, nullptr);
54         }
55     }
56 
57 protected:
onGetBounds()58     virtual SkRect onGetBounds() override { return mBounds; }
59 
60     const SkRect mBounds;
61 
62     struct LegacyFunctor {
LegacyFunctorLegacyFunctor63         explicit LegacyFunctor(Functor* functor, GlFunctorLifecycleListener* listener)
64                 : functor(functor), listener(listener) {}
65         Functor* functor;
66         sp<GlFunctorLifecycleListener> listener;
67     };
68 
69     struct NewFunctor {
NewFunctorNewFunctor70         explicit NewFunctor(int functor) {
71             handle = WebViewFunctorManager::instance().handleFor(functor);
72         }
73         sp<WebViewFunctor::Handle> handle;
74     };
75 
76     std::variant<NewFunctor, LegacyFunctor> mAnyFunctor;
77 };
78 
79 }  // namespace skiapipeline
80 }  // namespace uirenderer
81 }  // namespace android
82