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 #include "clip_cubic.h"
16 
17 #include <native_drawing/drawing_brush.h>
18 #include <native_drawing/drawing_matrix.h>
19 #include <native_drawing/drawing_path.h>
20 #include <native_drawing/drawing_pen.h>
21 #include <native_drawing/drawing_rect.h>
22 
23 #include "test_common.h"
24 
25 #include "common/log_common.h"
26 
27 const float W = 100;
28 const float H = 240;
29 
ClipCubic()30 ClipCubic::ClipCubic()
31 {
32     // 对标 aaclip.cpp
33     bitmapWidth_ = 400;  // 400宽度
34     bitmapHeight_ = 410; // 410高度
35     fileName_ = "clipcubic";
36 }
37 
DoDraw(OH_Drawing_Canvas * canvas,const OH_Drawing_Path * path)38 void DoDraw(OH_Drawing_Canvas* canvas, const OH_Drawing_Path* path)
39 {
40     OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
41     OH_Drawing_Pen* pen = OH_Drawing_PenCreate();
42 
43     OH_Drawing_BrushSetAntiAlias(brush, true);
44     OH_Drawing_PenSetAntiAlias(pen, true);
45     OH_Drawing_BrushSetColor(brush, OH_Drawing_ColorSetArgb(0xFF, 0xCC, 0xCC, 0xCC));
46     OH_Drawing_CanvasAttachBrush(canvas, brush);
47     OH_Drawing_CanvasDrawPath(canvas, path);
48     OH_Drawing_CanvasDetachBrush(canvas);
49 
50     OH_Drawing_PenSetColor(pen, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
51     OH_Drawing_PenSetAntiAlias(pen, true);
52     OH_Drawing_CanvasAttachPen(canvas, pen);
53     OH_Drawing_CanvasDrawPath(canvas, path);
54     OH_Drawing_CanvasDetachPen(canvas);
55 
56     OH_Drawing_PenDestroy(pen);
57     OH_Drawing_BrushDestroy(brush);
58 }
59 
DrawAndClip(OH_Drawing_Canvas * canvas,const OH_Drawing_Path * path,float dx,float dy)60 void DrawAndClip(OH_Drawing_Canvas* canvas, const OH_Drawing_Path* path, float dx, float dy)
61 {
62     uint32_t fSaveCount = 0;
63     fSaveCount = OH_Drawing_CanvasGetSaveCount(canvas);
64     DRAWING_LOGI("get fSaveCount=%{public}d ", fSaveCount);
65     OH_Drawing_CanvasSave(canvas);
66 
67     OH_Drawing_Rect* rect = OH_Drawing_RectCreate(0, H / 4, W, H / 4 + H / 2); // 4,2正方形
68     OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
69     OH_Drawing_BrushSetColor(brush, color_to_565(0xFF8888FF));
70     OH_Drawing_CanvasAttachBrush(canvas, brush);
71 
72     OH_Drawing_CanvasDrawRect(canvas, rect);
73     OH_Drawing_CanvasDetachBrush(canvas);
74     DoDraw(canvas, path);
75 
76     OH_Drawing_CanvasTranslate(canvas, dx, dy);
77 
78     OH_Drawing_CanvasAttachBrush(canvas, brush);
79     OH_Drawing_CanvasDrawRect(canvas, rect);
80     OH_Drawing_CanvasClipRect(canvas, rect, OH_Drawing_CanvasClipOp::INTERSECT, false);
81     OH_Drawing_CanvasDetachBrush(canvas);
82     DoDraw(canvas, path);
83 
84     OH_Drawing_CanvasRestoreToCount(canvas, fSaveCount);
85     DRAWING_LOGI("ClipCubicGm::GetSaveCount count=%{public}d", fSaveCount);
86 
87     OH_Drawing_RectDestroy(rect);
88     OH_Drawing_BrushDestroy(brush);
89 }
90 
OnTestFunction(OH_Drawing_Canvas * canvas)91 void ClipCubic::OnTestFunction(OH_Drawing_Canvas* canvas)
92 {
93     OH_Drawing_Path* fVPath = OH_Drawing_PathCreate();
94     OH_Drawing_Path* fHPath = OH_Drawing_PathCreate();
95 
96     OH_Drawing_PathMoveTo(fVPath, W, 0);
97     OH_Drawing_PathCubicTo(fVPath, W, H - 10, 0, 10, 0, H); // 10坐标
98     OH_Drawing_Matrix* matrix = OH_Drawing_MatrixCreate();
99     OH_Drawing_MatrixRotate(matrix, 90, W / 2, H / 2); // 90 2 坐标
100 
101     OH_Drawing_PathTransformWithPerspectiveClip(fVPath, matrix, fHPath, true);
102 
103     OH_Drawing_CanvasTranslate(canvas, 80, 10); // 80,10距离
104     DrawAndClip(canvas, fVPath, 200, 0);        // 200坐标
105     OH_Drawing_CanvasTranslate(canvas, 0, 200); // 200距离
106     DrawAndClip(canvas, fHPath, 200, 0);        // 200坐标
107 
108     OH_Drawing_PathDestroy(fVPath);
109     OH_Drawing_PathDestroy(fHPath);
110     OH_Drawing_MatrixDestroy(matrix);
111 }