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