• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     Matrix4 imageMatrix_;
65 };
66 
67 // CanvasImage is interface for drawing image.
68 class CanvasImage : public virtual AceType {
69     DECLARE_ACE_TYPE(CanvasImage, AceType)
70 
71 public:
72     CanvasImage() = default;
73     ~CanvasImage() override = default;
74     virtual void DrawToRSCanvas(
75         RSCanvas& canvas, const RSRect& srcRect, const RSRect& dstRect, const BorderRadiusArray& radiusXY) = 0;
DrawRect(RSCanvas & canvas,const RSRect & srcRect,const RSRect & dstRect)76     virtual void DrawRect(RSCanvas& canvas, const RSRect& srcRect, const RSRect& dstRect) {}
77 
78     static RefPtr<CanvasImage> Create(void* rawImage);
79     static RefPtr<CanvasImage> Create();
80     static RefPtr<CanvasImage> Create(const RefPtr<PixelMap>& pixelMap);
81 
GetPixelMap()82     virtual RefPtr<PixelMap> GetPixelMap() const
83     {
84         return nullptr;
85     }
86 
87     virtual int32_t GetWidth() const = 0;
88     virtual int32_t GetHeight() const = 0;
89 
Clone()90     virtual RefPtr<CanvasImage> Clone()
91     {
92         return Claim(this);
93     }
94 
HasData()95     virtual bool HasData() const
96     {
97         return false;
98     }
99 
100     // cache this CanvasImage
Cache(const std::string & key)101     virtual void Cache(const std::string& key) {}
102 
SetPaintConfig(const ImagePaintConfig & config)103     void SetPaintConfig(const ImagePaintConfig& config)
104     {
105         paintConfig_ = std::make_unique<ImagePaintConfig>(config);
106     }
107 
SetIsDrawAnimate(bool isDrawAnimate)108     void SetIsDrawAnimate(bool isDrawAnimate)
109     {
110         isDrawAnimate_ = isDrawAnimate;
111     }
112 
GetPaintConfig()113     inline ImagePaintConfig& GetPaintConfig()
114     {
115         if (!paintConfig_) {
116             paintConfig_ = std::make_unique<ImagePaintConfig>();
117         }
118         return *paintConfig_;
119     }
120 
IsStatic()121     virtual bool IsStatic()
122     {
123         return true;
124     }
SetRedrawCallback(std::function<void ()> && callback)125     virtual void SetRedrawCallback(std::function<void()>&& callback) {}
126 
SetOnFinishCallback(std::function<void ()> && callback)127     virtual void SetOnFinishCallback(std::function<void()>&& callback) {}
128 
ControlAnimation(bool play)129     virtual void ControlAnimation(bool play) {}
130 
SetRawCompressData(void * dataPtr,int32_t w,int32_t h)131     virtual void SetRawCompressData(void* dataPtr, int32_t w, int32_t h) {}
132 
133 protected:
134     bool isDrawAnimate_ = false;
135 
136 private:
137     std::unique_ptr<ImagePaintConfig> paintConfig_;
138 
139     ACE_DISALLOW_COPY_AND_MOVE(CanvasImage);
140 };
141 } // namespace OHOS::Ace::NG
142 
143 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_CANVAS_IMAGE_H
144