1 /*
2  * Copyright (c) 2024 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 #include "draw/canvas.h"
17 #include "recording/record_cmd.h"
18 #include "utils/log.h"
19 
20 namespace OHOS {
21 namespace Rosen {
22 namespace Drawing {
AutoCanvasMatrixBrush(Canvas * canvas,const Matrix * matrix,const Brush * brush,const Rect & bounds)23 AutoCanvasMatrixBrush::AutoCanvasMatrixBrush(Canvas* canvas,
24     const Matrix* matrix, const Brush* brush, const Rect& bounds)
25     : canvas_(canvas), saveCount_(canvas->Save()),
26     paintPen_(canvas->GetMutablePen()), paintBrush_(canvas->GetMutableBrush())
27 {
28     if (brush) {
29         Rect newBounds = bounds;
30         if (matrix) {
31             matrix->MapRect(newBounds, bounds);
32         }
33         SaveLayerOps slr(&newBounds, brush);
34         canvas->SaveLayer(slr);
35     } else if (matrix) {
36         canvas->Save();
37     }
38 
39     if (matrix) {
40         canvas->ConcatMatrix(*matrix);
41     }
42 }
43 
~AutoCanvasMatrixBrush()44 AutoCanvasMatrixBrush::~AutoCanvasMatrixBrush()
45 {
46     canvas_->GetMutablePen() = paintPen_;
47     canvas_->GetMutableBrush() = paintBrush_;
48     canvas_->RestoreToCount(saveCount_);
49 }
50 
GetRecordingCanvas() const51 Canvas* Canvas::GetRecordingCanvas() const
52 {
53     return nullptr;
54 }
55 
AddCanvas(Canvas * canvas)56 void Canvas::AddCanvas(Canvas* canvas)
57 {
58     if (canvas != nullptr) {
59         pCanvasList_.push_back(canvas);
60     }
61 }
62 
RemoveAll()63 void Canvas::RemoveAll()
64 {
65     pCanvasList_.clear();
66 }
67 
~Canvas()68 Canvas::~Canvas()
69 {
70     RemoveAll();
71 }
72 
RestoreToCount(uint32_t count)73 void Canvas::RestoreToCount(uint32_t count)
74 {
75     for (uint32_t i = this->GetSaveCount(); i > count; i--) {
76         this->Restore();
77     }
78 }
79 
DrawRecordCmd(const std::shared_ptr<RecordCmd> recordCmd,const Matrix * matrix,const Brush * brush)80 void Canvas::DrawRecordCmd(const std::shared_ptr<RecordCmd> recordCmd,
81     const Matrix* matrix, const Brush* brush)
82 {
83     if (recordCmd == nullptr) {
84         LOGE("Canvas::DrawRecordCmd, recordCmd is nullptr!");
85         return;
86     }
87     if (matrix && matrix->IsIdentity()) {
88         matrix = nullptr;
89     }
90 
91     AutoCanvasMatrixBrush autoCanvasMatrixBrush(this, matrix, brush, recordCmd->GetCullRect());
92     recordCmd->Playback(this);
93 }
94 
GetRecordingState() const95 bool Canvas::GetRecordingState() const
96 {
97     return recordingState_;
98 }
99 
SetRecordingState(bool flag)100 void Canvas::SetRecordingState(bool flag)
101 {
102     recordingState_ = flag;
103 }
104 
SetOffscreen(bool isOffscreen)105 void Canvas::SetOffscreen(bool isOffscreen)
106 {
107     isOffscreen_ = isOffscreen;
108 }
109 
GetOffscreen() const110 bool Canvas::GetOffscreen() const
111 {
112     return isOffscreen_;
113 }
114 
SetUICapture(bool isUICapture)115 void Canvas::SetUICapture(bool isUICapture)
116 {
117     isUICapture_ = isUICapture;
118 }
119 
GetUICapture() const120 bool Canvas::GetUICapture() const
121 {
122     return isUICapture_;
123 }
124 } // namespace Drawing
125 } // namespace Rosen
126 } // namespace OHOS