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 #ifndef TEST_COMMON_H
16 #define TEST_COMMON_H
17 
18 #include "napi/native_api.h"
19 #include <native_drawing/drawing_types.h>
20 #include <string>
21 #include <bits/alltypes.h>
22 
23 #define TMUL  1664525
24 #define TADD 1013904223
25 
26 #define DRAW_COLORWHITE 0xFFFFFFFF
27 #define DRAW_COLORBLACK 0xFF000000
28 #define DRAW_COLORRED 0xFFFF0000
29 #define DRAW_COLORGREEN 0xFF00FF00
30 #define DRAW_COLORBLUE 0xFF0000FF
31 #define DRAW_COLORYELLOW 0xFFFFFF00
32 #define DRAW_COLORCYAN 0xFF00FFFF
33 #define DRAW_COLORMAGENTA 0xFFFF00FF
34 #define DRAW_COLORGRAY 0xFF888888
35 #define DRAW_COLORTRANSPARENT 0x00000000
36 
37 class TestRend {
38 public:
TestRend()39     TestRend() { init(0); }
TestRend(uint32_t seed)40     explicit TestRend(uint32_t seed) { init(seed); }
41     ~TestRend() = default;
42 
43     uint32_t nextU();
44     float_t nextUScalar1();
45     uint32_t nextULessThan(uint32_t count);
46     float_t nextF();
47     float_t nextRangeF(float_t min, float_t max);
48     uint32_t nextBits(unsigned bitCount);
49 protected:
50     void init(uint32_t seed);
51     uint32_t next(uint32_t seed);
52 
53     uint32_t a;
54     uint32_t b;
55 };
56 
57 uint32_t color_to_565(uint32_t color);
58 
59 struct DrawRect {
60     float left;
61     float top;
62     float right;
63     float bottom;
ContainsDrawRect64     bool Contains(float x, float y) const { return x >= left && x < right && y >= top && y < bottom; }
WidthDrawRect65     float Width(){ return (right - left); }
HeightDrawRect66     float Height(){ return (bottom - top); }
CenterXDrawRect67     float CenterX(){ return (right - left) / 2; } // 2 for mid
CenterYDrawRect68     float CenterY(){ return (bottom - top) / 2; } // 2 for mid
InsetDrawRect69     bool Inset(float dx, float dy)
70     {
71         float l = left + dx;
72         float t = top + dy;
73         float r = right - dx;
74         float b = bottom - dy;
75         if ((r <= l) || (b <= t)) {
76             return false;
77         }
78         left = l;
79         top = t;
80         right = r;
81         bottom = b;
82         return true;
83     }
OffsetDrawRect84     void Offset(float dx, float dy)
85     {
86         left += dx;
87         top += dy;
88         right += dx;
89         bottom += dy;
90     }
SetXYWHDrawRect91     void SetXYWH(float x, float y, float width, float height)
92     {
93         left = x;
94         top = y;
95         right = x + width;
96         bottom = y + height;
97     }
98 };
99 
100 OH_Drawing_Rect* DrawCreateRect(DrawRect r);
101 
102 void DrawPathAddCircle(OH_Drawing_Path* path, float centerX, float centerY, float radius);
103 uint8_t* DrawBitmapGetAddr8(OH_Drawing_Bitmap* bitmap, int x, int y);
104 uint16_t* DrawBitmapGetAddr16(OH_Drawing_Bitmap* bitmap, int x, int y);
105 uint32_t* DrawBitmapGetAddr32(OH_Drawing_Bitmap* bitmap, int x, int y);
106 
107 void DrawPathGetBound(DrawRect& r, float x, float y);
108 
109 bool ConvertStringFromJsValue(napi_env env, napi_value jsValue, std::string &value);
110 bool ConvertIntFromJsValue(napi_env env, napi_value jsValue, uint32_t &value);
111 #endif // TEST_COMMON_H