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 "bitmaprect.h"
16 #include <native_drawing/drawing_color.h>
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_round_rect.h>
22 #include <native_drawing/drawing_image.h>
23 #include <native_drawing/drawing_shader_effect.h>
24 #include <native_drawing/drawing_point.h>
25 #include <native_drawing/drawing_sampling_options.h>
26 #include "test_common.h"
27 #include "common/log_common.h"
28 
29 enum {
30     K_W = 640,
31     K_H = 480,
32 };
33 
make_big_bitmap()34 OH_Drawing_Bitmap *make_big_bitmap()
35 {
36     int gXSize = 4096;
37     int gYSize = 4096;
38     int gBorderWidth = 10;
39 
40     OH_Drawing_Bitmap *bm = OH_Drawing_BitmapCreate();
41     OH_Drawing_BitmapFormat format = { OH_Drawing_ColorFormat::COLOR_FORMAT_RGBA_8888,
42                                        OH_Drawing_AlphaFormat::ALPHA_FORMAT_OPAQUE };
43     OH_Drawing_BitmapBuild(bm, gXSize, gXSize, &format);
44 
45     void *pixel = OH_Drawing_BitmapGetPixels(bm);
46     uint32_t *ptr = (uint32_t *)pixel;
47 
48     for (int y = 0; y < gYSize; ++y) {
49         for (int x = 0; x < gYSize; ++x) {
50             if (x <= gBorderWidth || x >= gXSize - gBorderWidth || y <= gBorderWidth || y >= gYSize - gBorderWidth) {
51                 //                    *DrawBitmapGetAddr32(bm, x, y) = 0xFFFFFF88;
52                 ptr = (uint32_t *)pixel + (uint32_t)y * gYSize + (x);
53                 *ptr = 0xFFFFFF88;
54             } else {
55                 //                    *DrawBitmapGetAddr32(bm, x, y) = 0xFF000088;
56                 ptr = (uint32_t *)pixel + (uint32_t)y * gYSize + (x);
57                 *ptr = 0xFF000088;
58             }
59         }
60     }
61 
62     return bm;
63 }
64 
DrawBitmapRect4(bool useIRect)65 DrawBitmapRect4::DrawBitmapRect4(bool useIRect) : fUseIRect(useIRect)
66 {
67     bitmapWidth_ = K_W;
68     bitmapHeight_ = K_H;
69     fileName_ = fUseIRect ? "bigbitmaprect_i" : "bigbitmaprect_s";
70     fBigBitmap = make_big_bitmap();
71 }
72 
~DrawBitmapRect4()73 DrawBitmapRect4::~DrawBitmapRect4() {}
74 
OnTestFunction(OH_Drawing_Canvas * canvas)75 void DrawBitmapRect4::OnTestFunction(OH_Drawing_Canvas *canvas)
76 {
77     OH_Drawing_CanvasClear(canvas, 0xFF444444);
78     OH_Drawing_Brush *brush = OH_Drawing_BrushCreate();
79     OH_Drawing_BrushSetBlendMode(brush, OH_Drawing_BlendMode::BLEND_MODE_XOR);
80 
81     DrawRect srcR1 = { 0.0f, 0.0f, 4096.0f, 2040.0f };
82     DrawRect dstR1 = { 10.1f, 10.1f, 629.9f, 400.9f };
83 
84     DrawRect srcR2 = { 4085.0f, 10.0f, 4087.0f, 12.0f };
85     DrawRect dstR2 = { 10, 410, 30, 430 };
86 
87     OH_Drawing_SamplingOptions *option = OH_Drawing_SamplingOptionsCreate(OH_Drawing_FilterMode::FILTER_MODE_NEAREST,
88         OH_Drawing_MipmapMode::MIPMAP_MODE_NONE);
89     OH_Drawing_CanvasAttachBrush(canvas, brush);
90     if (!fUseIRect) {
91         OH_Drawing_CanvasDrawBitmapRect(canvas, fBigBitmap, DrawCreateRect(srcR1), DrawCreateRect(dstR1), option);
92         OH_Drawing_CanvasDrawBitmapRect(canvas, fBigBitmap, DrawCreateRect(srcR2), DrawCreateRect(dstR2), option);
93     } else {
94         DRAWING_LOGI("OnTestFunction  DrawBitmapRect4 Rect::roundOut接口缺失");
95     }
96     OH_Drawing_CanvasDetachBrush(canvas);
97     OH_Drawing_BrushDestroy(brush);
98     OH_Drawing_BitmapDestroy(fBigBitmap);
99     OH_Drawing_SamplingOptionsDestroy(option);
100 }
101