• 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/resource/internal_resource.h"
28 #include "base/resource/shared_image_manager.h"
29 #include "core/components/common/layout/constants.h"
30 #include "core/components_ng/image_provider/image_data.h"
31 #ifdef USE_ROSEN_DRAWING
32 #include "core/components_ng/render/drawing_forward.h"
33 #endif
34 #include "core/image/image_source_info.h"
35 #include "core/pipeline/pipeline_base.h"
36 
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 CacheImageDataToImageCache(const std::string& key, const RefPtr<CachedImageData>& imageData);
72     static RefPtr<NG::ImageData> LoadImageDataFromFileCache(const std::string& key, const std::string& suffix);
73 };
74 
75 // File image provider: read image from file.
76 class FileImageLoader : public ImageLoader {
77 public:
78     FileImageLoader() = default;
79     ~FileImageLoader() override = default;
80 #ifndef USE_ROSEN_DRAWING
81     sk_sp<SkData> LoadImageData(
82         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
83 #else
84     std::shared_ptr<RSData> LoadImageData(
85         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
86 #endif
87 };
88 
89 // data provider image loader.
90 class DataProviderImageLoader : public ImageLoader {
91 public:
92     DataProviderImageLoader() = default;
93     ~DataProviderImageLoader() override = default;
94 #ifndef USE_ROSEN_DRAWING
95     sk_sp<SkData> LoadImageData(
96         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
97 #else
98     std::shared_ptr<RSData> LoadImageData(
99         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
100 #endif
101 };
102 
103 class DecodedDataProviderImageLoader : public ImageLoader {
104 public:
105     DecodedDataProviderImageLoader() = default;
106     ~DecodedDataProviderImageLoader() override = default;
107 #ifndef USE_ROSEN_DRAWING
108     sk_sp<SkData> LoadImageData(
109         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
110 #else
111     std::shared_ptr<RSData> LoadImageData(
112         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
113 #endif
114     RefPtr<NG::ImageData> LoadDecodedImageData(
115         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
116 private:
117     static std::string GetThumbnailOrientation(const ImageSourceInfo& src);
118 };
119 
120 class AssetImageLoader final : public ImageLoader {
121 public:
122     AssetImageLoader() = default;
123     ~AssetImageLoader() override = default;
124 #ifndef USE_ROSEN_DRAWING
125     sk_sp<SkData> LoadImageData(
126         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
127 #else
128     std::shared_ptr<RSData> LoadImageData(
129         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
130 #endif
131     std::string LoadJsonData(const std::string& src, const WeakPtr<PipelineBase> context = nullptr);
132 };
133 
134 // Network image provider: read image from network.
135 class NetworkImageLoader final : public ImageLoader {
136 public:
137     NetworkImageLoader() = default;
138     ~NetworkImageLoader() override = default;
139 #ifndef USE_ROSEN_DRAWING
140     sk_sp<SkData> LoadImageData(
141         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
142 #else
143     std::shared_ptr<RSData> LoadImageData(
144         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
145 #endif
146 };
147 
148 class InternalImageLoader final : public ImageLoader {
149 public:
150     InternalImageLoader() = default;
151     ~InternalImageLoader() override = default;
152 #ifndef USE_ROSEN_DRAWING
153     sk_sp<SkData> LoadImageData(
154         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
155 #else
156     std::shared_ptr<RSData> LoadImageData(
157         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
158 #endif
159 };
160 
161 class Base64ImageLoader final : public ImageLoader {
162 public:
163     Base64ImageLoader() = default;
164     ~Base64ImageLoader() override = default;
165     static std::string_view GetBase64ImageCode(const std::string& uri);
166 #ifndef USE_ROSEN_DRAWING
167     sk_sp<SkData> LoadImageData(
168         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
169 #else
170     std::shared_ptr<RSData> LoadImageData(
171         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
172 #endif
173 };
174 
175 class ResourceImageLoader final : public ImageLoader {
176 public:
177     ResourceImageLoader() = default;
178     ~ResourceImageLoader() override = default;
179 #ifndef USE_ROSEN_DRAWING
180     sk_sp<SkData> LoadImageData(
181         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
182 #else
183     std::shared_ptr<RSData> LoadImageData(
184         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
185 #endif
186 
187 private:
188     bool GetResourceId(const std::string& uri, uint32_t& resId) const;
189     bool GetResourceId(const std::string& uri, std::string& path) const;
190     bool GetResourceName(const std::string& uri, std::string& resName) const;
191 };
192 
193 class PixelMapImageLoader : public ImageLoader {
194 public:
195     PixelMapImageLoader() = default;
196     ~PixelMapImageLoader() override = default;
197 #ifndef USE_ROSEN_DRAWING
198     sk_sp<SkData> LoadImageData(
199         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
200 #else
201     std::shared_ptr<RSData> LoadImageData(
202         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
203 #endif
204     RefPtr<NG::ImageData> LoadDecodedImageData(
205         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override;
206 };
207 
208 class SharedMemoryImageLoader : public ImageLoader, public ImageProviderLoader {
209     DECLARE_ACE_TYPE(SharedMemoryImageLoader, ImageLoader);
210 
211 public:
212     SharedMemoryImageLoader() = default;
213     ~SharedMemoryImageLoader() override = default;
214 #ifndef USE_ROSEN_DRAWING
215     sk_sp<SkData> LoadImageData(const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context) override;
216 #else
217     std::shared_ptr<RSData> LoadImageData(
218         const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context) override;
219 #endif
220     void UpdateData(const std::string& uri, const std::vector<uint8_t>& memData) override;
221 
222 private:
223     std::condition_variable cv_;
224     std::mutex mtx_;
225     std::vector<uint8_t> data_;
226 };
227 } // namespace OHOS::Ace
228 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_LOADER_H
229