• 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_IMAGE_LOADER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_LOADER_H
18 
19 #include <condition_variable>
20 #include <regex>
21 #include <string>
22 
23 #include "include/core/SkImage.h"
24 
25 #include "base/geometry/size.h"
26 #include "base/memory/ace_type.h"
27 #include "base/network/download_manager.h"
28 #include "base/resource/internal_resource.h"
29 #include "base/resource/shared_image_manager.h"
30 #include "core/components/common/layout/constants.h"
31 #include "core/components_ng/image_provider/image_data.h"
32 #ifdef USE_ROSEN_DRAWING
33 #include "core/components_ng/render/drawing_forward.h"
34 #endif
35 #include "core/image/image_source_info.h"
36 #include "core/pipeline/pipeline_base.h"
37 namespace OHOS::Ace {
38 
39 class ImageLoader : public virtual AceType {
40     DECLARE_ACE_TYPE(ImageLoader, AceType);
41 
42 public:
43 #ifndef USE_ROSEN_DRAWING
44     virtual sk_sp<SkData> LoadImageData(
45         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context) = 0;
46 #else
47     virtual std::shared_ptr<RSData> LoadImageData(
48         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context) = 0;
49 #endif
LoadDecodedImageData(const ImageSourceInfo &,const WeakPtr<PipelineBase> &)50     virtual RefPtr<NG::ImageData> LoadDecodedImageData(
51         const ImageSourceInfo& /*imageSourceInfo*/, const WeakPtr<PipelineBase>& /*context*/)
52     {
53         return nullptr;
54     }
55     RefPtr<NG::ImageData> GetImageData(
56         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr);
57     static std::string RemovePathHead(const std::string& uri);
58     static RefPtr<ImageLoader> CreateImageLoader(const ImageSourceInfo& imageSourceInfo);
59 #ifndef USE_ROSEN_DRAWING
60     static sk_sp<SkData> LoadDataFromCachedFile(const std::string& uri);
61 #else
62     static std::shared_ptr<RSData> LoadDataFromCachedFile(const std::string& uri);
63 #endif
64     // TODO: maybe it's more approriate to move these interfaces to [ImageCache] with definition in abstract layer and
65     // implementation in adapter layer
66 #ifndef USE_ROSEN_DRAWING
67     static sk_sp<SkData> QueryImageDataFromImageCache(const ImageSourceInfo& sourceInfo);
68 #else
69     static std::shared_ptr<RSData> QueryImageDataFromImageCache(const ImageSourceInfo& sourceInfo);
70 #endif
71     static void CacheImageData(const std::string& key, const RefPtr<NG::ImageData>& data);
72     static RefPtr<NG::ImageData> LoadImageDataFromFileCache(const std::string& key, const std::string& suffix);
73 
74     static void WriteCacheToFile(const std::string& uri, const std::vector<uint8_t>& imageData);
75     static void WriteCacheToFile(const std::string& uri, const std::string& imageData);
76 };
77 
78 // File image provider: read image from file.
79 class FileImageLoader : public ImageLoader {
80 public:
81     FileImageLoader() = default;
82     ~FileImageLoader() override = default;
83 #ifndef USE_ROSEN_DRAWING
84     sk_sp<SkData> LoadImageData(
85         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
86 #else
87     std::shared_ptr<RSData> LoadImageData(
88         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
89 #endif
90 };
91 
92 // data provider image loader.
93 class DataProviderImageLoader : public ImageLoader {
94 public:
95     DataProviderImageLoader() = default;
96     ~DataProviderImageLoader() override = default;
97 #ifndef USE_ROSEN_DRAWING
98     sk_sp<SkData> LoadImageData(
99         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
100 #else
101     std::shared_ptr<RSData> LoadImageData(
102         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
103 #endif
104 };
105 
106 class DecodedDataProviderImageLoader : public ImageLoader {
107 public:
108     DecodedDataProviderImageLoader() = default;
109     ~DecodedDataProviderImageLoader() override = default;
110 #ifndef USE_ROSEN_DRAWING
111     sk_sp<SkData> LoadImageData(
112         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
113 #else
114     std::shared_ptr<RSData> LoadImageData(
115         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
116 #endif
117     RefPtr<NG::ImageData> LoadDecodedImageData(
118         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
119 
120 private:
121     static std::string GetThumbnailOrientation(const ImageSourceInfo& src);
122 };
123 
124 class AssetImageLoader final : public ImageLoader {
125 public:
126     AssetImageLoader() = default;
127     ~AssetImageLoader() override = default;
128 #ifndef USE_ROSEN_DRAWING
129     sk_sp<SkData> LoadImageData(
130         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
131 #else
132     std::shared_ptr<RSData> LoadImageData(
133         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
134 #endif
135     std::string LoadJsonData(const std::string& src, const WeakPtr<PipelineBase> context = nullptr);
136 };
137 
138 // Network image provider: read image from network.
139 class NetworkImageLoader final : public ImageLoader {
140 public:
141     NetworkImageLoader() = default;
142     ~NetworkImageLoader() override = default;
143 #ifndef USE_ROSEN_DRAWING
144     sk_sp<SkData> LoadImageData(
145         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
146 #else
147     std::shared_ptr<RSData> LoadImageData(
148         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
149 #endif
150     static bool DownloadImage(DownloadCallback&& downloadCallback, const std::string& src, bool sync);
151 };
152 
153 class InternalImageLoader final : public ImageLoader {
154 public:
155     InternalImageLoader() = default;
156     ~InternalImageLoader() override = default;
157 #ifndef USE_ROSEN_DRAWING
158     sk_sp<SkData> LoadImageData(
159         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
160 #else
161     std::shared_ptr<RSData> LoadImageData(
162         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
163 #endif
164 };
165 
166 class Base64ImageLoader final : public ImageLoader {
167 public:
168     Base64ImageLoader() = default;
169     ~Base64ImageLoader() override = default;
170     static std::string_view GetBase64ImageCode(const std::string& uri);
171 #ifndef USE_ROSEN_DRAWING
172     sk_sp<SkData> LoadImageData(
173         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
174 #else
175     std::shared_ptr<RSData> LoadImageData(
176         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
177 #endif
178 };
179 
180 class ResourceImageLoader final : public ImageLoader {
181 public:
182     ResourceImageLoader() = default;
183     ~ResourceImageLoader() override = default;
184 #ifndef USE_ROSEN_DRAWING
185     sk_sp<SkData> LoadImageData(
186         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
187 #else
188     std::shared_ptr<RSData> LoadImageData(
189         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
190 #endif
191 
192 private:
193     bool GetResourceId(const std::string& uri, uint32_t& resId) const;
194     bool GetResourceId(const std::string& uri, std::string& path) const;
195     bool GetResourceName(const std::string& uri, std::string& resName) const;
196 };
197 
198 class PixelMapImageLoader : public ImageLoader {
199 public:
200     PixelMapImageLoader() = default;
201     ~PixelMapImageLoader() override = default;
202 #ifndef USE_ROSEN_DRAWING
203     sk_sp<SkData> LoadImageData(
204         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
205 #else
206     std::shared_ptr<RSData> LoadImageData(
207         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
208 #endif
209     RefPtr<NG::ImageData> LoadDecodedImageData(
210         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
211 };
212 
213 class SharedMemoryImageLoader : public ImageLoader, public ImageProviderLoader {
214     DECLARE_ACE_TYPE(SharedMemoryImageLoader, ImageLoader);
215 
216 public:
217     SharedMemoryImageLoader() = default;
218     ~SharedMemoryImageLoader() override = default;
219 #ifndef USE_ROSEN_DRAWING
220     sk_sp<SkData> LoadImageData(const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context) override;
221 #else
222     std::shared_ptr<RSData> LoadImageData(
223         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context) override;
224 #endif
225     void UpdateData(const std::string& uri, const std::vector<uint8_t>& memData) override;
226 
227 private:
228     std::condition_variable cv_;
229     std::mutex mtx_;
230     std::vector<uint8_t> data_;
231 };
232 class AstcImageLoader : public ImageLoader {
233 public:
234     AstcImageLoader() = default;
235     ~AstcImageLoader() override = default;
236 #ifndef USE_ROSEN_DRAWING
237     sk_sp<SkData> LoadImageData(
238         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
239 #else
240     std::shared_ptr<RSData> LoadImageData(
241         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
242 #endif
243     RefPtr<NG::ImageData> LoadDecodedImageData(
244         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
245 
246 private:
247     static std::string GetThumbnailOrientation(const ImageSourceInfo& src);
248 };
249 } // namespace OHOS::Ace
250 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_LOADER_H
251