• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "flutter/fml/memory/ref_counted.h"
22 #ifdef NG_BUILD
23 #include "core/components_ng/render/canvas_image.h"
24 #else
25 #include "flutter/lib/ui/painting/image.h"
26 #endif
27 
28 #include "core/image/image_cache.h"
29 #include "core/image/image_object.h"
30 
31 namespace OHOS::Ace {
32 
33 struct CachedImage {
34 #ifdef NG_BUILD
CachedImageCachedImage35     explicit CachedImage(const RefPtr<NG::CanvasImage>& image) : imagePtr(image) {}
36     RefPtr<NG::CanvasImage> imagePtr;
37 #else
38     explicit CachedImage(fml::RefPtr<flutter::CanvasImage> image) : imagePtr(std::move(image)) {}
39     fml::RefPtr<flutter::CanvasImage> imagePtr;
40 #endif
41     uint32_t uniqueId = 0;
42 };
43 
44 namespace NG {
45 struct CachedImage {
CachedImageCachedImage46     explicit CachedImage(sk_sp<SkImage> image) : imagePtr(std::move(image)) {}
47     sk_sp<SkImage> imagePtr;
48 };
49 } // namespace NG
50 
51 struct SkiaCachedImageData : public CachedImageData {
52     DECLARE_ACE_TYPE(SkiaCachedImageData, CachedImageData);
53 
54 public:
SkiaCachedImageDataSkiaCachedImageData55     explicit SkiaCachedImageData(const sk_sp<SkData>& data) : imageData(data) {}
56     ~SkiaCachedImageData() override = default;
57 
GetSizeSkiaCachedImageData58     size_t GetSize() override
59     {
60         return imageData ? imageData->size() : 0;
61     }
62 
GetDataSkiaCachedImageData63     const uint8_t* GetData() override
64     {
65         return imageData ? imageData->bytes() : nullptr;
66     }
67 
68     sk_sp<SkData> imageData;
69 };
70 
71 class FlutterImageCache : public ImageCache {
72     DECLARE_ACE_TYPE(FlutterImageCache, ImageCache);
73 
74 public:
75     FlutterImageCache() = default;
76     ~FlutterImageCache() override = default;
77     void Clear() override;
78     RefPtr<CachedImageData> GetDataFromCacheFile(const std::string& filePath) override;
79 };
80 
81 } // namespace OHOS::Ace
82 
83 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_FLUTTER_IMAGE_CACHE_H
84