1 /*
2  * Copyright (c) 2022-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_NG_RENDER_CANVAS_IMAGE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_CANVAS_IMAGE_H
18 
19 #include <memory>
20 
21 #include "base/geometry/ng/rect_t.h"
22 #include "base/image/drawing_color_filter.h"
23 #include "base/image/drawing_lattice.h"
24 #include "base/image/pixel_map.h"
25 #include "base/memory/ace_type.h"
26 #include "base/utils/noncopyable.h"
27 #include "core/components/common/layout/constants.h"
28 #include "core/components/common/properties/decoration.h"
29 #include "core/components_ng/render/drawing_forward.h"
30 #include "core/image/image_source_info.h"
31 
32 namespace OHOS::Ace::NG {
33 using BorderRadiusArray = std::array<PointF, 4>;
34 struct ImageColorFilter {
35     std::shared_ptr<std::vector<float>> colorFilterMatrix_;
36     RefPtr<DrawingColorFilter> colorFilterDrawing_;
ResetImageColorFilter37     void Reset()
38     {
39         colorFilterMatrix_.reset();
40         colorFilterDrawing_.Reset();
41     }
42 };
43 struct ImagePaintConfig {
44     RectF srcRect_;
45     RectF dstRect_;
46     ImageColorFilter colorFilter_;
47     std::shared_ptr<BorderRadiusArray> borderRadiusXY_ = nullptr;
48     float scaleX_ = 1.0f;
49     float scaleY_ = 1.0f;
50     ImageRenderMode renderMode_ = ImageRenderMode::ORIGINAL;
51     ImageInterpolation imageInterpolation_ = ImageInterpolation::NONE;
52     ImageRepeat imageRepeat_ = ImageRepeat::NO_REPEAT;
53     ImageFit imageFit_ = ImageFit::COVER;
54     float smoothEdge_ = 0.0f;
55     ImageRotateOrientation orientation_ = ImageRotateOrientation::UP;
56     DynamicRangeMode dynamicMode = DynamicRangeMode::STANDARD;
57     bool flipHorizontally_ = false;
58     bool isSvg_ = false;
59     int32_t frameCount_ = 1;
60     std::vector<ObscuredReasons> obscuredReasons_;
61     ImageResizableSlice resizableSlice_;
62     RefPtr<DrawingLattice> resizableLattice_ = nullptr;
63     ImageSourceInfo sourceInfo_;
64 };
65 
66 // CanvasImage is interface for drawing image.
67 class CanvasImage : public virtual AceType {
68     DECLARE_ACE_TYPE(CanvasImage, AceType)
69 
70 public:
71     CanvasImage() = default;
72     ~CanvasImage() override = default;
73     virtual void DrawToRSCanvas(
74         RSCanvas& canvas, const RSRect& srcRect, const RSRect& dstRect, const BorderRadiusArray& radiusXY) = 0;
DrawRect(RSCanvas & canvas,const RSRect & srcRect,const RSRect & dstRect)75     virtual void DrawRect(RSCanvas& canvas, const RSRect& srcRect, const RSRect& dstRect) {}
76 
77     static RefPtr<CanvasImage> Create(void* rawImage);
78     static RefPtr<CanvasImage> Create();
79     static RefPtr<CanvasImage> Create(const RefPtr<PixelMap>& pixelMap);
80 
GetPixelMap()81     virtual RefPtr<PixelMap> GetPixelMap() const
82     {
83         return nullptr;
84     }
85 
86     virtual int32_t GetWidth() const = 0;
87     virtual int32_t GetHeight() const = 0;
88 
Clone()89     virtual RefPtr<CanvasImage> Clone()
90     {
91         return Claim(this);
92     }
93 
HasData()94     virtual bool HasData() const
95     {
96         return false;
97     }
98 
99     // cache this CanvasImage
Cache(const std::string & key)100     virtual void Cache(const std::string& key) {}
101 
SetPaintConfig(const ImagePaintConfig & config)102     void SetPaintConfig(const ImagePaintConfig& config)
103     {
104         paintConfig_ = std::make_unique<ImagePaintConfig>(config);
105     }
106 
SetIsDrawAnimate(bool isDrawAnimate)107     void SetIsDrawAnimate(bool isDrawAnimate)
108     {
109         isDrawAnimate_ = isDrawAnimate;
110     }
111 
GetPaintConfig()112     inline ImagePaintConfig& GetPaintConfig()
113     {
114         if (!paintConfig_) {
115             paintConfig_ = std::make_unique<ImagePaintConfig>();
116         }
117         return *paintConfig_;
118     }
119 
IsStatic()120     virtual bool IsStatic()
121     {
122         return true;
123     }
SetRedrawCallback(std::function<void ()> && callback)124     virtual void SetRedrawCallback(std::function<void()>&& callback) {}
125 
SetOnFinishCallback(std::function<void ()> && callback)126     virtual void SetOnFinishCallback(std::function<void()>&& callback) {}
127 
ControlAnimation(bool play)128     virtual void ControlAnimation(bool play) {}
129 
SetRawCompressData(void * dataPtr,int32_t w,int32_t h)130     virtual void SetRawCompressData(void* dataPtr, int32_t w, int32_t h) {}
131 
132 protected:
133     bool isDrawAnimate_ = false;
134 
135 private:
136     std::unique_ptr<ImagePaintConfig> paintConfig_;
137 
138     ACE_DISALLOW_COPY_AND_MOVE(CanvasImage);
139 };
140 } // namespace OHOS::Ace::NG
141 
142 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_CANVAS_IMAGE_H
143