1 /*
2  * Copyright (c) 2021-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 CANVAS_H
17 #define CANVAS_H
18 
19 #include <iostream>
20 #include <string>
21 #include <vector>
22 
23 #include "drawing/draw/core_canvas.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
28 class RecordCmd;
29 
30 class AutoCanvasMatrixBrush {
31 public:
32     AutoCanvasMatrixBrush(Canvas* canvas,
33         const Matrix* matrix, const Brush* brush, const Rect& bounds);
34     ~AutoCanvasMatrixBrush();
35 
36     AutoCanvasMatrixBrush(AutoCanvasMatrixBrush&&) = delete;
37     AutoCanvasMatrixBrush(const AutoCanvasMatrixBrush&) = delete;
38     AutoCanvasMatrixBrush& operator=(AutoCanvasMatrixBrush&&) = delete;
39     AutoCanvasMatrixBrush& operator=(const AutoCanvasMatrixBrush&) = delete;
40 
41 private:
42     Canvas* canvas_;
43     uint32_t saveCount_;
44     Paint paintPen_;
45     Paint paintBrush_;
46 };
47 
48 class DRAWING_API Canvas : public CoreCanvas {
49 public:
Canvas()50     Canvas() {}
Canvas(DrawingType type)51     Canvas(DrawingType type) : CoreCanvas(type) {}
Canvas(int32_t width, int32_t height)52     Canvas(int32_t width, int32_t height) : CoreCanvas(width, height) {}
53 
54     virtual Canvas* GetRecordingCanvas() const;
55 
56     void AddCanvas(Canvas* canvas);
57 
58     void RemoveAll();
59     // constructor adopt a raw canvas ptr, using for ArkUI, should remove after rosen modifier provide drawing Canvas.
Canvas(void* rawCanvas)60     explicit Canvas(void* rawCanvas) : CoreCanvas(rawCanvas) {}
61     virtual ~Canvas();
62 
63     /*
64      * @brief        Restores Canvas Matrix and clip value state to count.
65      * @param count  Depth of state stack to restore.
66      */
67     void RestoreToCount(uint32_t count);
68 
69     /*
70      * @brief  Draw recordcmd.
71      * @param recordCmd  Record command.
72      * @param matrix  Matrix to rotate, scale, translate, and so on; may be nullptr.
73      * @param brush Brush to apply transparency, filtering, and so on; must be nullptr now.
74      */
75     virtual void DrawRecordCmd(const std::shared_ptr<RecordCmd> recordCmd,
76         const Matrix* matrix = nullptr, const Brush* brush = nullptr);
77 
78     virtual bool GetRecordingState() const;
79 
80     virtual void SetRecordingState(bool flag);
81 
82     virtual void SetOffscreen(bool isOffscreen);
83 
84     virtual bool GetOffscreen() const;
85 
86     virtual void SetUICapture(bool isUICapture);
87 
88     virtual bool GetUICapture() const;
89 protected:
90     std::vector<Canvas*> pCanvasList_;
91     bool recordingState_ = false;
92     bool isOffscreen_ = false;
93     bool isUICapture_ = false;
94 };
95 
96 class DRAWING_API OverDrawCanvas : public Canvas {
97 public:
OverDrawCanvas(std::shared_ptr<Drawing::Canvas> canvas)98     OverDrawCanvas(std::shared_ptr<Drawing::Canvas> canvas) : Canvas(DrawingType::OVER_DRAW)
99     {
100         BuildOverDraw(canvas);
101     }
~OverDrawCanvas()102     virtual ~OverDrawCanvas() {}
GetDrawingType() const103     virtual DrawingType GetDrawingType() const
104     {
105         return DrawingType::OVER_DRAW;
106     }
107 };
108 
109 class DRAWING_API NoDrawCanvas : public Canvas {
110 public:
NoDrawCanvas(int32_t width, int32_t height)111     NoDrawCanvas(int32_t width, int32_t height) : Canvas(DrawingType::NO_DRAW)
112     {
113         BuildNoDraw(width, height);
114     }
115     ~NoDrawCanvas() override = default;
116     DrawingType GetDrawingType() const override
117     {
118         return DrawingType::NO_DRAW;
119     }
120 };
121 
122 class AutoCanvasRestore {
123 public:
AutoCanvasRestore(Canvas& canvas, bool doSave)124     AutoCanvasRestore(Canvas& canvas, bool doSave) : canvas_(canvas)
125     {
126         saveCount_ = canvas_.GetSaveCount();
127         if (doSave) {
128             canvas_.Save();
129         }
130     }
~AutoCanvasRestore()131     ~AutoCanvasRestore()
132     {
133         canvas_.RestoreToCount(saveCount_);
134     }
135 
136     AutoCanvasRestore(AutoCanvasRestore&&) = delete;
137     AutoCanvasRestore(const AutoCanvasRestore&) = delete;
138     AutoCanvasRestore& operator=(AutoCanvasRestore&&) = delete;
139     AutoCanvasRestore& operator=(const AutoCanvasRestore&) = delete;
140 
141 private:
142     Canvas& canvas_;
143     uint32_t saveCount_;
144 };
145 } // namespace Drawing
146 } // namespace Rosen
147 } // namespace OHOS
148 #endif
149