• 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 needFlipCanvasHorizontally_ = false;
42     bool isSvg_ = false;
43 };
44 
45 struct RenderTaskHolder;
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     // TODO: use [PixelMap] as data source when rs provides interface like
59     // DrawBitmapRect(Media::PixelMap* pixelMap, const Rect& dstRect, const Rect& srcRect, ...)
60     // now we make [SkImage] from [PixelMap] and use [drawImageRect] to draw image
61     static RefPtr<CanvasImage> Create(const RefPtr<PixelMap>& pixelMap);
62 
GetPixelMap()63     virtual RefPtr<PixelMap> GetPixelMap()
64     {
65         return nullptr;
66     }
67 
68     virtual int32_t GetWidth() const = 0;
69     virtual int32_t GetHeight() const = 0;
70 
Clone()71     virtual RefPtr<CanvasImage> Clone()
72     {
73         return Claim(this);
74     }
75 
SetPaintConfig(const ImagePaintConfig & config)76     void SetPaintConfig(const ImagePaintConfig& config)
77     {
78         paintConfig_ = std::make_unique<ImagePaintConfig>(config);
79     }
80 
GetPaintConfig()81     ImagePaintConfig& GetPaintConfig()
82     {
83         if (!paintConfig_) {
84             LOGI("image paint config is null");
85         }
86         return *paintConfig_;
87     }
88 
IsStatic()89     virtual bool IsStatic()
90     {
91         return true;
92     }
SetRedrawCallback(std::function<void ()> && callback)93     virtual void SetRedrawCallback(std::function<void()>&& callback) {}
94 
ControlAnimation(bool play)95     virtual void ControlAnimation(bool play) {}
96 
97 private:
98     std::unique_ptr<ImagePaintConfig> paintConfig_;
99 
100     ACE_DISALLOW_COPY_AND_MOVE(CanvasImage);
101 };
102 } // namespace OHOS::Ace::NG
103 
104 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_CANVAS_IMAGE_H
105