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