• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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_OBJECT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_OBJECT_H
18 
19 #include "modules/svg/include/SkSVGDOM.h"
20 
21 #include "base/image/pixel_map.h"
22 #include "core/components/svg/parse/svg_dom.h"
23 #include "core/image/animated_image_player.h"
24 #include "core/image/image_source_info.h"
25 #ifdef USE_NEW_SKIA
26 #include "include/core/SkStream.h"
27 #endif
28 
29 namespace OHOS::Ace {
30 
31 class RenderImage;
32 class ImageObject : public virtual AceType {
33     DECLARE_ACE_TYPE(ImageObject, AceType);
34 
35 public:
36     static RefPtr<ImageObject> BuildImageObject(ImageSourceInfo source, const RefPtr<PipelineBase> context,
37         const std::shared_ptr<RSData>& rsData, bool useSkiaSvg);
38 
39     ImageObject() = default;
ImageObject(ImageSourceInfo source)40     explicit ImageObject(ImageSourceInfo source) : imageSource_(source) {}
41 
42     ImageObject(ImageSourceInfo source, const Size& imageSize, int32_t frameCount, bool isSvg = false)
imageSource_(source)43         : imageSource_(source), imageSize_(imageSize), frameCount_(frameCount), isSvg_(isSvg)
44     {}
45     virtual ~ImageObject() = default;
46 
47     static std::string GenerateCacheKey(const ImageSourceInfo& srcInfo, Size targetSize);
48 
GetImageSize()49     Size GetImageSize()
50     {
51         return imageSize_;
52     }
53 
SetImageSize(Size & size)54     void SetImageSize(Size& size)
55     {
56         imageSize_ = size;
57     }
58 
GetFrameCount()59     int32_t GetFrameCount()
60     {
61         return frameCount_;
62     }
63 
SetFrameCount(int32_t frameCount)64     void SetFrameCount(int32_t frameCount)
65     {
66         frameCount_ = frameCount;
67     }
68 
IsSingleFrame()69     bool IsSingleFrame() const
70     {
71         return frameCount_ == 1;
72     }
73 
GetSourceInfo()74     ImageSourceInfo GetSourceInfo()
75     {
76         return imageSource_;
77     }
78 
IsSvg()79     bool IsSvg() const
80     {
81         return isSvg_;
82     }
83 
84     virtual void UploadToGpuForRender(const WeakPtr<PipelineBase>& context,
85         const UploadSuccessCallback& successCallback, const FailedCallback& failedCallback, const Size& imageSize,
86         bool forceResize, bool syncMode = false)
87     {}
88 
Pause()89     virtual void Pause() {}
Resume()90     virtual void Resume() {}
ClearData()91     virtual void ClearData() {}
92 
93     // this will be called on ui thread when renderImage do perform layout for different image objects.
PerformLayoutImageObject(RefPtr<RenderImage> image)94     virtual void PerformLayoutImageObject(RefPtr<RenderImage> image) {}
95 
96     // this will be called on ui thread when renderImage do measure for different image objects.
97     virtual Size MeasureForImage(RefPtr<RenderImage> image);
98 
CancelBackgroundTasks()99     virtual bool CancelBackgroundTasks()
100     {
101         return false;
102     }
103 
Clone()104     virtual RefPtr<ImageObject> Clone()
105     {
106         return MakeRefPtr<ImageObject>(imageSource_, imageSize_, frameCount_, isSvg_);
107     }
108 
109 protected:
110     ImageSourceInfo imageSource_;
111     Size imageSize_;
112     int32_t frameCount_ = 1;
113     bool isSvg_ = false;
114 };
115 
116 class SvgSkiaImageObject : public ImageObject {
117     DECLARE_ACE_TYPE(SvgSkiaImageObject, ImageObject);
118 
119 public:
SvgSkiaImageObject(ImageSourceInfo source,const Size & imageSize,int32_t frameCount,const sk_sp<SkSVGDOM> & skiaDom)120     SvgSkiaImageObject(
121         ImageSourceInfo source, const Size& imageSize, int32_t frameCount, const sk_sp<SkSVGDOM>& skiaDom)
122         : ImageObject(source, imageSize, frameCount, true), skiaDom_(skiaDom)
123     {}
124 
125     ~SvgSkiaImageObject() override = default;
126 
GetSkiaDom()127     const sk_sp<SkSVGDOM>& GetSkiaDom()
128     {
129         return skiaDom_;
130     }
131 
132     void PerformLayoutImageObject(RefPtr<RenderImage> image) override;
133     Size MeasureForImage(RefPtr<RenderImage> image) override;
134 
Clone()135     RefPtr<ImageObject> Clone() override
136     {
137         return MakeRefPtr<SvgSkiaImageObject>(imageSource_, Size(), frameCount_, skiaDom_);
138     }
139 
140 private:
141     sk_sp<SkSVGDOM> skiaDom_;
142 };
143 
144 class SvgImageObject : public ImageObject {
145     DECLARE_ACE_TYPE(SvgImageObject, ImageObject);
146 
147 public:
SvgImageObject(ImageSourceInfo source,const Size & imageSize,int32_t frameCount,const RefPtr<SvgDom> & svgDom)148     SvgImageObject(ImageSourceInfo source, const Size& imageSize, int32_t frameCount, const RefPtr<SvgDom>& svgDom)
149         : ImageObject(source, imageSize, frameCount, true), svgDom_(svgDom)
150     {}
151 
152     ~SvgImageObject() override = default;
153 
GetSvgDom()154     const RefPtr<SvgDom>& GetSvgDom()
155     {
156         return svgDom_;
157     }
158 
159     void PerformLayoutImageObject(RefPtr<RenderImage> image) override;
160     Size MeasureForImage(RefPtr<RenderImage> image) override;
161 
Clone()162     RefPtr<ImageObject> Clone() override
163     {
164         return MakeRefPtr<SvgImageObject>(imageSource_, Size(), frameCount_, svgDom_);
165     }
166 
167 private:
168     RefPtr<SvgDom> svgDom_;
169 };
170 
171 class StaticImageObject : public ImageObject {
172     DECLARE_ACE_TYPE(StaticImageObject, ImageObject);
173 
174 public:
175     using CancelableTask = CancelableCallback<void()>;
StaticImageObject(ImageSourceInfo source,const Size & imageSize,int32_t frameCount,const std::shared_ptr<RSData> & data)176     StaticImageObject(
177         ImageSourceInfo source, const Size& imageSize, int32_t frameCount, const std::shared_ptr<RSData>& data)
178         : ImageObject(source, imageSize, frameCount), data_(data)
179     {}
180 
181     ~StaticImageObject() override = default;
182 
183     void UploadToGpuForRender(const WeakPtr<PipelineBase>& context, const UploadSuccessCallback& successCallback,
184         const FailedCallback& failedCallback, const Size& imageSize, bool forceResize, bool syncMode = false) override;
185 
ClearData()186     void ClearData() override
187     {
188         data_ = nullptr;
189     }
190 
191     bool CancelBackgroundTasks() override;
192 
Clone()193     RefPtr<ImageObject> Clone() override
194     {
195         return MakeRefPtr<StaticImageObject>(imageSource_, imageSize_, frameCount_, data_);
196     }
197 
198 private:
199     std::shared_ptr<RSData> data_;
200     CancelableTask uploadForPaintTask_;
201 };
202 
203 RefPtr<ImageObject> CreateAnimatedImageObject(
204     ImageSourceInfo source, const Size& imageSize, int32_t frameCount, const std::shared_ptr<RSData>& data);
205 
206 class PixelMapImageObject : public ImageObject {
207     DECLARE_ACE_TYPE(PixelMapImageObject, ImageObject);
208 
209 public:
PixelMapImageObject(const RefPtr<PixelMap> & pixmap)210     explicit PixelMapImageObject(const RefPtr<PixelMap>& pixmap) : pixmap_(pixmap)
211     {
212         imageSize_ = Size(pixmap_->GetWidth(), pixmap_->GetHeight());
213     }
214 
215     ~PixelMapImageObject() override = default;
216 
GetRawPixelMapPtr()217     void* GetRawPixelMapPtr()
218     {
219         return pixmap_->GetRawPixelMapPtr();
220     }
221 
222     void PerformLayoutImageObject(RefPtr<RenderImage> image) override;
223     Size MeasureForImage(RefPtr<RenderImage> image) override;
224 
ClearData()225     void ClearData() override
226     {
227         pixmap_ = nullptr;
228     }
229 
GetPixmap()230     const RefPtr<PixelMap>& GetPixmap() const
231     {
232         return pixmap_;
233     }
234 
Clone()235     RefPtr<ImageObject> Clone() override
236     {
237         return MakeRefPtr<PixelMapImageObject>(pixmap_);
238     }
239 
240 private:
241     RefPtr<PixelMap> pixmap_;
242 };
243 RefPtr<ImageObject> GetImageSvgDomObj(ImageSourceInfo source, const std::unique_ptr<SkMemoryStream>& svgStream,
244     const RefPtr<PipelineBase>& context, std::optional<Color>& color);
245 } // namespace OHOS::Ace
246 
247 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_OBJECT_H
248