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