1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_FLUTTER_IMAGE_CACHE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_FLUTTER_IMAGE_CACHE_H 18 19 #include <utility> 20 21 #include "core/components_ng/render/canvas_image.h" 22 #include "core/image/image_cache.h" 23 #include "core/image/image_object.h" 24 25 namespace OHOS::Ace { 26 27 struct CachedImage { 28 #ifndef USE_ROSEN_DRAWING CachedImageCachedImage29 explicit CachedImage(const sk_sp<SkImage>& image) : imagePtr(image) {} 30 sk_sp<SkImage> imagePtr; 31 #else 32 explicit CachedImage(const std::shared_ptr<RSImage>& image) : imagePtr(image) {} 33 std::shared_ptr<RSImage> imagePtr; 34 #endif 35 uint32_t uniqueId = 0; 36 }; 37 38 #ifndef USE_ROSEN_DRAWING 39 struct SkiaCachedImageData : public CachedImageData { 40 DECLARE_ACE_TYPE(SkiaCachedImageData, CachedImageData); 41 42 public: SkiaCachedImageDataSkiaCachedImageData43 explicit SkiaCachedImageData(const sk_sp<SkData>& data) : imageData(data) {} 44 ~SkiaCachedImageData() override = default; 45 #else 46 struct RosenCachedImageData : public CachedImageData { 47 DECLARE_ACE_TYPE(RosenCachedImageData, CachedImageData); 48 49 public: 50 explicit RosenCachedImageData(const std::shared_ptr<RSData>& data) : imageData(data) {} 51 ~RosenCachedImageData() override = default; 52 #endif 53 GetSizeSkiaCachedImageData54 size_t GetSize() override 55 { 56 #ifndef USE_ROSEN_DRAWING 57 return imageData ? imageData->size() : 0; 58 #else 59 return imageData ? imageData->GetSize() : 0; 60 #endif 61 } 62 GetDataSkiaCachedImageData63 const uint8_t* GetData() override 64 { 65 #ifndef USE_ROSEN_DRAWING 66 return imageData ? imageData->bytes() : nullptr; 67 #else 68 return imageData ? static_cast<const uint8_t*>(imageData->GetData()) : nullptr; 69 #endif 70 } 71 72 #ifndef USE_ROSEN_DRAWING 73 sk_sp<SkData> imageData; 74 #else 75 std::shared_ptr<RSData> imageData; 76 #endif 77 }; 78 79 class FlutterImageCache : public ImageCache { 80 DECLARE_ACE_TYPE(FlutterImageCache, ImageCache); 81 82 public: 83 FlutterImageCache() = default; 84 ~FlutterImageCache() override = default; 85 void Clear() override; 86 RefPtr<CachedImageData> GetDataFromCacheFile(const std::string& filePath) override; 87 }; 88 89 } // namespace OHOS::Ace 90 91 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_FLUTTER_IMAGE_CACHE_H 92