Nader Jawad | f0ea9e1 | 2023-05-02 15:18:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | #ifndef SKIA_WRAPPER_H_ |
| 18 | #define SKIA_WRAPPER_H_ |
| 19 | |
| 20 | #include <SkRefCnt.h> |
| 21 | #include <utils/RefBase.h> |
| 22 | |
| 23 | namespace android::uirenderer { |
| 24 | |
| 25 | template <typename T> |
| 26 | class SkiaWrapper : public VirtualLightRefBase { |
| 27 | public: |
| 28 | sk_sp<T> getInstance() { |
| 29 | if (mInstance != nullptr && shouldDiscardInstance()) { |
| 30 | mInstance = nullptr; |
| 31 | } |
| 32 | |
| 33 | if (mInstance == nullptr) { |
| 34 | mInstance = createInstance(); |
| 35 | mGenerationId++; |
| 36 | } |
| 37 | return mInstance; |
| 38 | } |
| 39 | |
| 40 | virtual bool shouldDiscardInstance() const { return false; } |
| 41 | |
| 42 | void discardInstance() { mInstance = nullptr; } |
| 43 | |
| 44 | [[nodiscard]] int32_t getGenerationId() const { return mGenerationId; } |
| 45 | |
| 46 | protected: |
| 47 | virtual sk_sp<T> createInstance() = 0; |
| 48 | |
| 49 | private: |
| 50 | sk_sp<T> mInstance = nullptr; |
| 51 | int32_t mGenerationId = 0; |
| 52 | }; |
| 53 | |
| 54 | } // namespace android::uirenderer |
| 55 | |
| 56 | #endif // SKIA_WRAPPER_H_ |