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