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 "points_mask_filter.h"
17 
18 #include <bits/alltypes.h>
19 #include <hilog/log.h>
20 #include <native_drawing/drawing_filter.h>
21 #include <native_drawing/drawing_mask_filter.h>
22 #include <native_drawing/drawing_path_effect.h>
23 #include <native_drawing/drawing_pen.h>
24 #include <native_drawing/drawing_rect.h>
25 
26 #include "test_common.h"
27 
28 #include "common/log_common.h"
29 
30 enum {
31     K_W = 512,
32     K_H = 256,
33     N = 30,
34 };
35 
PointsMaskFilter()36 PointsMaskFilter::PointsMaskFilter()
37 {
38     bitmapWidth_ = K_W;
39     bitmapHeight_ = K_H;
40     fileName_ = "points_maskfilter"; // 对标 points.cpp
41 }
42 
43 // 用例名: pointsmaskfilter 测试 OH_Drawing_MaskFilterCreateBlur
OnTestFunction(OH_Drawing_Canvas * canvas)44 void PointsMaskFilter::OnTestFunction(OH_Drawing_Canvas* canvas)
45 {
46     float fRange = 220.0f;   // 点的x坐标范围
47     float fOffset = 18.0f;   // 点的x坐标偏移量
48     float fRadius = 6.0f;    // 模糊半径
49     float fOffsetX = 256.0f; // 画布平移的x坐标量
50     float fWidth = 10.0f;
51     OH_Drawing_Point2D pts[N];
52     TestRend testRend;
53     for (OH_Drawing_Point2D& p : pts) {
54         p.x = testRend.nextF() * fRange + fOffset;
55         p.y = testRend.nextF() * fRange + fOffset;
56     }
57 
58     OH_Drawing_MaskFilter* maskFilter = OH_Drawing_MaskFilterCreateBlur(OH_Drawing_BlurType::NORMAL, fRadius, true);
59 
60     const OH_Drawing_PenLineCapStyle caps[] = { OH_Drawing_PenLineCapStyle::LINE_SQUARE_CAP,
61         OH_Drawing_PenLineCapStyle::LINE_ROUND_CAP };
62     // 创建一个pen对象
63     OH_Drawing_Pen* pen = OH_Drawing_PenCreate();
64     OH_Drawing_PenSetAntiAlias(pen, true);
65     OH_Drawing_PenSetWidth(pen, fWidth);
66     OH_Drawing_Filter* filter = OH_Drawing_FilterCreate();
67     for (auto cap : caps) {
68         OH_Drawing_PenSetCap(pen, cap);
69         OH_Drawing_FilterSetMaskFilter(filter, maskFilter);
70         OH_Drawing_PenSetFilter(pen, filter);
71         OH_Drawing_PenSetColor(pen, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
72 
73         OH_Drawing_CanvasAttachPen(canvas, pen);
74         OH_Drawing_CanvasDrawPoints(canvas, OH_Drawing_PointMode::POINT_MODE_POINTS, N, pts);
75         OH_Drawing_FilterSetMaskFilter(filter, nullptr);
76         OH_Drawing_PenSetFilter(pen, filter);
77         OH_Drawing_PenSetColor(pen, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
78 
79         OH_Drawing_CanvasAttachPen(canvas, pen);
80         OH_Drawing_CanvasDrawPoints(canvas, OH_Drawing_PointMode::POINT_MODE_POINTS, N, pts);
81         OH_Drawing_CanvasTranslate(canvas, fOffsetX, 0);
82     }
83     OH_Drawing_PenDestroy(pen);
84     OH_Drawing_FilterDestroy(filter);
85     OH_Drawing_MaskFilterDestroy(maskFilter);
86 
87     DRAWING_LOGI("pointsmaskfilter::OnTestFunction end");
88 }
89