• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_PROVIDER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_PROVIDER_H
18 
19 #include <string>
20 
21 #include "flutter/fml/memory/ref_counted.h"
22 #ifdef NG_BUILD
23 #include "flutter/lib/ui/io_manager.h"
24 #else
25 #include "flutter/lib/ui/painting/image.h"
26 #endif
27 #include "third_party/skia/include/codec/SkCodec.h"
28 
29 #include "base/memory/ace_type.h"
30 #include "base/resource/internal_resource.h"
31 #include "core/components/common/layout/constants.h"
32 #include "core/image/image_source_info.h"
33 #include "core/image/image_loader.h"
34 #include "core/pipeline/pipeline_base.h"
35 
36 #ifdef NG_BUILD
37 #include "core/components_ng/render/canvas_image.h"
38 #endif
39 
40 class SkSVGDOM;
41 namespace OHOS::Ace {
42 
43 class ImageObject;
44 struct FlutterRenderTaskHolder : public virtual AceType {
45     DECLARE_ACE_TYPE(FlutterRenderTaskHolder, AceType);
46 public:
FlutterRenderTaskHolderFlutterRenderTaskHolder47     FlutterRenderTaskHolder(
48         fml::RefPtr<flutter::SkiaUnrefQueue> queue,
49         fml::WeakPtr<flutter::IOManager> manager,
50         fml::RefPtr<fml::TaskRunner> taskRunner)
51         : unrefQueue(queue),
52           ioManager(manager),
53           ioTaskRunner(taskRunner)
54         {}
55 
56     fml::RefPtr<flutter::SkiaUnrefQueue> unrefQueue;
57     // weak reference of io manager must be check and used on io thread, because io manager is created on io thread.
58     fml::WeakPtr<flutter::IOManager> ioManager;
59     fml::RefPtr<fml::TaskRunner> ioTaskRunner;
60 };
61 
62 class SvgDom;
63 using ImageObjSuccessCallback = std::function<void(ImageSourceInfo, const RefPtr<ImageObject>)>;
64 #ifdef NG_BUILD
65 using UploadSuccessCallback = std::function<void(ImageSourceInfo, const RefPtr<NG::CanvasImage>&)>;
66 #else
67 using UploadSuccessCallback = std::function<void(ImageSourceInfo, const fml::RefPtr<flutter::CanvasImage>&)>;
68 #endif
69 using SvgDomSuccessCallback =  std::function<void(ImageSourceInfo, const RefPtr<SvgDom>&)>;
70 using FailedCallback = std::function<void(ImageSourceInfo, const std::string&)>;
71 using CancelableTask = CancelableCallback<void()>;
72 using OnPostBackgroundTask = std::function<void(CancelableTask)>;
73 
74 struct LoadCallback {
LoadCallbackLoadCallback75     LoadCallback(
76         const ImageObjSuccessCallback& success,
77         const UploadSuccessCallback& upload,
78         const FailedCallback& failed)
79         : successCallback(success), uploadCallback(upload), failedCallback(failed) {}
80     ~LoadCallback() = default;
81 
82     ImageObjSuccessCallback successCallback;
83     UploadSuccessCallback   uploadCallback;
84     FailedCallback          failedCallback;
85 };
86 
87 class FlutterRenderImage;
88 class ImageProvider {
89 public:
90     static void TryLoadImageInfo(const RefPtr<PipelineBase>& context, const std::string& src,
91         std::function<void(bool, int32_t, int32_t)>&& loadCallback);
92 
93 #ifndef NG_BUILD
94     static void GetSVGImageDOMAsyncFromSrc(
95         const std::string& src,
96         std::function<void(const sk_sp<SkSVGDOM>&)> callback,
97         std::function<void()> failedCallback,
98         const WeakPtr<PipelineBase> context,
99         uint64_t svgThemeColor = 0,
100         OnPostBackgroundTask onBackgroundTaskPostCallback = nullptr);
101 
102     static void GetSVGImageDOMAsyncFromData(
103         const sk_sp<SkData>& skData,
104         std::function<void(const sk_sp<SkSVGDOM>&)> callback,
105         std::function<void()> failedCallback,
106         const WeakPtr<PipelineBase> context,
107         uint64_t svgThemeColor = 0,
108         OnPostBackgroundTask onBackgroundTaskPostCallback = nullptr);
109 #endif
110 
111     // upload image data to gpu context for painting asynchronously.
112     static void UploadImageToGPUForRender(
113         const WeakPtr<PipelineBase> context,
114         const sk_sp<SkImage>& image,
115         const sk_sp<SkData>& data,
116         const std::function<void(flutter::SkiaGPUObject<SkImage>, sk_sp<SkData>)>&& callback,
117         const RefPtr<FlutterRenderTaskHolder>& renderTaskHolder,
118         const std::string src);
119 
120     // get out source image data asynchronously.
121     static void FetchImageObject(
122         const ImageSourceInfo& imageInfo,
123         const ImageObjSuccessCallback& successCallback,
124         const UploadSuccessCallback& uploadSuccessCallback,
125         const FailedCallback& failedCallback,
126         const WeakPtr<PipelineBase>& context,
127         bool syncMode,
128         bool useSkiaSvg,
129         bool needAutoResize,
130         const RefPtr<FlutterRenderTaskHolder>& renderTaskHolder,
131         const OnPostBackgroundTask& onBackgroundTaskPostCallback = nullptr);
132 
133     static sk_sp<SkImage> ResizeSkImage(
134         const sk_sp<SkImage>& rawImage,
135         const std::string& src,
136         Size imageSize,
137         bool forceResize = false);
138 
139     static sk_sp<SkImage> ApplySizeToSkImage(
140         const sk_sp<SkImage>& rawImage,
141         int32_t dstWidth,
142         int32_t dstHeight,
143         const std::string& srcKey = std::string());
144 
145     static bool IsWideGamut(const sk_sp<SkColorSpace>& colorSpace);
146 
147     static bool NeedExchangeWidthAndHeight(SkEncodedOrigin origin);
148 
149     // This is a synchronization interface for getting out source image.
150     static sk_sp<SkImage> GetSkImage(
151         const std::string& src,
152         const WeakPtr<PipelineBase> context,
153         Size targetSize = Size());
154 
155     static RefPtr<ImageObject> GeneratorAceImageObject(
156         const ImageSourceInfo& imageInfo,
157         const RefPtr<PipelineBase> context,
158         bool useSkiaSvg);
159 
160     static sk_sp<SkData> LoadImageRawData(
161         const ImageSourceInfo& imageInfo,
162         const RefPtr<PipelineBase> context);
163 
164     static sk_sp<SkData> LoadImageRawDataFromFileCache(
165         const RefPtr<PipelineBase> context,
166         const std::string key,
167         const std::string suffix = "");
168 
169     static RefPtr<ImageObject> QueryImageObjectFromCache(
170         const ImageSourceInfo& imageInfo, const RefPtr<PipelineBase>& pipelineContext);
171     static SkColorType PixelFormatToSkColorType(const RefPtr<PixelMap>& pixmap);
172     static SkAlphaType AlphaTypeToSkAlphaType(const RefPtr<PixelMap>& pixmap);
173     static SkImageInfo MakeSkImageInfoFromPixelMap(const RefPtr<PixelMap>& pixmap);
174     static sk_sp<SkColorSpace> ColorSpaceToSkColorSpace(const RefPtr<PixelMap>& pixmap);
175 
176     static bool TrySetLoadingImage(
177         const ImageSourceInfo& imageInfo,
178         const ImageObjSuccessCallback& successCallback,
179         const UploadSuccessCallback& uploadCallback,
180         const FailedCallback& failedCallback);
181 
182     static void ProccessLoadingResult(
183         const RefPtr<TaskExecutor>& taskExecutor,
184         const ImageSourceInfo& imageInfo,
185         bool canStartUploadImageObj,
186         const RefPtr<ImageObject>& imageObj,
187         const RefPtr<PipelineBase>& context,
188         const RefPtr<FlutterRenderTaskHolder>& renderTaskHolder,
189         const std::string& errorMsg = "");
190 
191     static bool TryUploadingImage(
192         const std::string& key,
193         const UploadSuccessCallback& successCallback,
194         const FailedCallback& failedCallback);
195 
196     static void ProccessUploadResult(
197         const RefPtr<TaskExecutor>& taskExecutor,
198         const ImageSourceInfo& imageInfo,
199         const Size& imageSize,
200 #ifdef NG_BUILD
201         const RefPtr<NG::CanvasImage>& canvasImage,
202 #else
203         const fml::RefPtr<flutter::CanvasImage>& canvasImage,
204 #endif
205         const std::string& errorMsg = "");
206 
207 private:
208     static std::mutex loadingImageMutex_;
209     static std::unordered_map<std::string, std::vector<LoadCallback>> loadingImage_;
210 
211     static std::mutex uploadMutex_;
212     static std::unordered_map<std::string, std::vector<LoadCallback>> uploadingImage_;
213 };
214 
215 } // namespace OHOS::Ace
216 
217 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_PROVIDER_H
218