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_COMPONENTS_IMAGE_ROSEN_RENDER_IMAGE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_IMAGE_ROSEN_RENDER_IMAGE_H
18 
19 #ifndef USE_ROSEN_DRAWING
20 #include "modules/svg/include/SkSVGDOM.h"
21 
22 #endif
23 
24 #include "core/components/image/render_image.h"
25 #include "core/image/image_provider.h"
26 #include "frameworks/core/components/svg/parse/svg_dom.h"
27 
28 namespace OHOS::Ace {
29 
30 class RosenRenderImage final : public RenderImage, public ImageProviderLoader {
31     DECLARE_ACE_TYPE(RosenRenderImage, RenderImage);
32 
33 public:
34     RosenRenderImage();
35     ~RosenRenderImage() override;
36 
37     void Update(const RefPtr<Component>& component) override;
38     void Paint(RenderContext& context, const Offset& offset) override;
39     void UpdateData(const std::string& uri, const std::vector<uint8_t>& memData) override;
40 
41     void OnHiddenChanged(bool hidden) override;
42 
43     void UpLoadImageDataForPaint();
44 
45     void PaintBgImage(const std::shared_ptr<RSNode>& rsNode);
46 
47     void FetchImageObject() override;
48 
SupportOpacity()49     bool SupportOpacity() override
50     {
51         return true;
52     }
53 
54     bool IsSourceWideGamut() const override;
55     bool RetryLoading() override;
56 #ifndef USE_ROSEN_DRAWING
57     static SkColorType PixelFormatToSkColorType(const RefPtr<PixelMap>& pixmap);
58     static SkAlphaType AlphaTypeToSkAlphaType(const RefPtr<PixelMap>& pixmap);
59     static SkImageInfo MakeSkImageInfoFromPixelMap(const RefPtr<PixelMap>& pixmap);
60     static sk_sp<SkColorSpace> ColorSpaceToSkColorSpace(const RefPtr<PixelMap>& pixmap);
61 #else
62     static RSColorType PixelFormatToRSColorType(const RefPtr<PixelMap>& pixmap);
63     static RSAlphaType AlphaTypeToRSAlphaType(const RefPtr<PixelMap>& pixmap);
64     static RSBitmapFormat MakeRSBitmapFormatInfoFromPixelMap(const RefPtr<PixelMap>& pixmap);
65 #endif
66     static void UploadImageObjToGpuForRender(const RefPtr<ImageObject>& imageObj,
67         const WeakPtr<PipelineContext> context, UploadSuccessCallback uploadSuccessCallback,
68         FailedCallback failedCallback, Size resizeTarget, bool forceResize, bool syncMode = false);
69 
70     void ImageDataPaintSuccess(const RefPtr<NG::CanvasImage>& image);
71     void ImageObjReady(const RefPtr<ImageObject>& imageObj);
72     void ImageObjFailed(const std::string& errorMsg);
73     bool NeedUploadImageObjToGpu();
74 
SetFetchImageObjBackgroundTask(CancelableTask task)75     void SetFetchImageObjBackgroundTask(CancelableTask task)
76     {
77         if (fetchImageObjTask_) {
78             fetchImageObjTask_.Cancel(false);
79         }
80         fetchImageObjTask_ = task;
81     }
82 
83     void OnAppHide() override;
84     void OnAppShow() override;
85     void OnVisibleChanged() override;
86 
87     void PerformLayoutPixmap() override;
88     void PerformLayoutSvgImage() override;
89 
90     Size MeasureForPixmap() override;
91     Size MeasureForSvgImage() override;
92     Size MeasureForNormalImage() override;
93 
94 protected:
95     bool MaybeRelease() override;
96     void ClearRenderObject() override;
97     void LayoutImageObject() override;
98 #ifndef USE_ROSEN_DRAWING
GetSkImage()99     void* GetSkImage() override
100     {
101         return reinterpret_cast<void*>(&image_);
102     }
103 
104     RefPtr<PixelMap> GetPixmapFromSkImage() override;
105     SkPixmap CloneSkPixmap(SkPixmap& srcPixmap);
106 #else
GetDrawingImage()107     void* GetDrawingImage() override
108     {
109         return reinterpret_cast<void*>(&image_);
110     }
111     RefPtr<PixelMap> GetPixmapFromDrawingImage() override;
112 #endif
113 private:
114     void InitializeCallbacks();
115     Size Measure() override;
116     void UpdateRenderAltImage(const RefPtr<Component>& component);
117 #ifndef USE_ROSEN_DRAWING
118     void SetSkRadii(const Radius& radius, SkVector& radii);
119 #else
120     void SetRadii(const Radius& radius, RSPoint& radii);
121 #endif
122     void SetClipRadius();
123 #ifndef USE_ROSEN_DRAWING
124     void CanvasDrawImageRect(SkPaint& paint, const Offset& offset, SkCanvas* canvas, const Rect& paintRect);
125     void DrawImageOnCanvas(const Rect& srcRect, const Rect& dstRect, const SkPaint& paint, SkCanvas* canvas) const;
126     void PaintSVGImage(const sk_sp<SkData>& skData, bool onlyLayoutSelf = false);
127     void DrawSVGImage(const Offset& offset, SkCanvas* canvas);
128 #else
129     void CanvasDrawImageRect(RSBrush& brush, const Offset& offset, RSCanvas* canvas, const Rect& paintRect);
130     void DrawImageOnCanvas(const Rect& srcRect, const Rect& dstRect, const RSBrush& brush, RSCanvas* canvas) const;
131     void PaintSVGImage(const std::shared_ptr<RSData>& drawingData, bool onlyLayoutSelf = false);
132     void DrawSVGImage(const Offset& offset, RSCanvas* canvas);
133 #endif
134     void DrawSVGImageCustom(RenderContext& context, const Offset& offset);
135     void UpdateLoadSuccessState();
136     Rect RecalculateSrcRect(const Size& realImageSize);
137 #ifndef USE_ROSEN_DRAWING
138     void ApplyColorFilter(SkPaint& paint);
139     void ApplyInterpolation(SkPaint& paint);
140     void ApplyBorderRadius(const Offset& offset, const Rect& paintRect, SkCanvas* canvas);
141 #else
142     void ApplyColorFilter(RSBrush& brush);
143     void ApplyInterpolation(RSBrush& brush);
144     void ApplyBorderRadius(const Offset& offset, const Rect& paintRect, RSCanvas* canvas);
145 #endif
146     void AddSvgChild();
147     void CreateAnimatedPlayer(const RefPtr<ImageProvider>& provider, SkCodec* codecPtr, bool forceResize);
148 #ifndef USE_ROSEN_DRAWING
149     bool VerifySkImageDataFromPixmap(const RefPtr<PixelMap>& pixmap) const;
150 #else
151     bool VerifyRSImageDataFromPixmap(const RefPtr<PixelMap>& pixmap) const;
152 #endif
153     void CreateSvgNodes();
154     void SyncCreateSvgNodes(bool isReady = false);
155     void RebuildSvgRenderTree(const SvgRenderTree& svgRenderTree, const RefPtr<SvgDom>& svgDom);
156     void CancelBackgroundTasks();
157     void CacheImageObject();
158     void CacheSvgImageObject();
159     RefPtr<ImageObject> QueryCacheSvgImageObject();
160     void UpdatePixmap(const RefPtr<PixelMap>& pixmap);
161     void UpdateSharedMemoryImage(const RefPtr<PipelineContext>& context);
162     void ProcessPixmapForPaint();
163     std::function<void()> GenerateThumbnailLoadTask();
164 
165     RefPtr<ImageObject> imageObj_;
166 #ifndef USE_ROSEN_DRAWING
167     SkSamplingOptions options_;
168 #else
169     RSSamplingOptions options_;
170 #endif
171     sk_sp<SkSVGDOM> skiaDom_;
172     RefPtr<SvgDom> svgDom_;
173     RefPtr<NG::CanvasImage> image_;
174 #ifndef USE_ROSEN_DRAWING
175     SkVector radii_[4] = { { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 } };
176 #else
177     std::vector<RSPoint> radii_ = { { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 } };
178 #endif
179     Size formerRawImageSize_;
180     bool imageDataNotReady_ = false;
181 
182     ImageSourceInfo curSourceInfo_;
183     ImageObjSuccessCallback imageObjSuccessCallback_;
184     UploadSuccessCallback uploadSuccessCallback_;
185     FailedCallback failedCallback_;
186     OnPostBackgroundTask onPostBackgroundTask_;
187 
188     SvgRenderTree svgRenderTree_;
189 
190     CancelableTask fetchImageObjTask_;
191     bool backgroundTaskCanceled_ = false;
192 
193     bool contentChanged_ = false;
194 };
195 
196 } // namespace OHOS::Ace
197 
198 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_IMAGE_ROSEN_RENDER_IMAGE_H
199