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 "test_common.h"
16 #include <native_drawing/drawing_bitmap.h>
17 #include <native_drawing/drawing_path.h>
18 #include <native_drawing/drawing_rect.h>
19 #include "common/log_common.h"
20 
21 union drawFloatIntUnion {
22     float   fFloat;
23     int32_t fSignBitInt;
24 };
25 
drawBits2Float(int32_t floatAsBits)26 static inline float drawBits2Float(int32_t floatAsBits)
27 {
28     drawFloatIntUnion data;
29     data.fSignBitInt = floatAsBits;
30     return data.fFloat;
31 }
32 
next(uint32_t seed)33 uint32_t TestRend::next(uint32_t seed)
34 {
35     return TMUL*seed + TADD;
36 }
37 
init(uint32_t seed)38 void TestRend::init(uint32_t seed)
39 {
40     a = next(seed);
41     if (0 == a) {
42         a = next(a);
43     }
44     b = next(a);
45     if (0 == b) {
46         b = next(b);
47     }
48 }
49 
nextU()50 uint32_t TestRend::nextU()
51 {
52     a = 30345*(a & 0xffff) + (a >> 16); // 16, 30345 for rand
53     b = 18000*(b & 0xffff) + (b >> 16); // 16, 18000 for rand
54     return (((a << 16) | (a >> 16)) + b); // 16 for rand
55 }
56 
nextUScalar1()57 float_t TestRend::nextUScalar1()
58 {
59     return (this->nextU()>>16) * 1.52587890625e-5f; // 1.52587890625e-5f:int to float, high 16 bits
60 }
61 
nextULessThan(uint32_t count)62 uint32_t TestRend::nextULessThan(uint32_t count)
63 {
64     uint32_t min = 0;
65     uint32_t max = count-1;
66     uint32_t range = max-min+1;
67     if (0 == range) {
68         return this->nextU();
69     } else {
70         return (min+this->nextU()%range);
71     }
72 }
73 
nextF()74 float_t TestRend::nextF()
75 {
76     int i = (this->nextU() >> 9);
77     int floatint = 0x3f800000 | i;
78     float f = drawBits2Float(floatint) - 1.0f;
79     return f;
80 }
81 
nextRangeF(float_t min,float_t max)82 float_t TestRend::nextRangeF(float_t min, float_t max)
83 {
84     return this->nextUScalar1() * (max - min) + min;
85 }
86 
nextBits(unsigned bitCount)87 uint32_t TestRend::nextBits(unsigned bitCount)
88 {
89     return this->nextU() >> (32 - bitCount); // 32 : size
90 }
91 
color_to_565(uint32_t color)92 uint32_t color_to_565(uint32_t color)
93 {
94     unsigned r = ((color >> 19) & 0x1F) << 11;
95     unsigned g = ((color >> 10) & 0x3F) << 5;
96     unsigned b = ((color >> 3) & 0x1F);
97 
98     uint16_t rgb565 = r | g | b;
99 
100     // 将RGB通道的值缩放到565格式
101     unsigned r1 = ((unsigned)rgb565 >> 11) & 0x1F;
102     unsigned g1 = ((unsigned)rgb565 >> 5) & 0x3F;
103     unsigned b1 = (unsigned)rgb565 & 0x1F;
104 
105     unsigned r2 = (r1 << 3) | (r1 >> 2);
106     unsigned g2 = (g1 << 2) | (g1 >> 4);
107     unsigned b2 = (b1 << 3) | (b1 >> 2);
108 
109     uint32_t argb = (0xFF << 24) | (r2 << 16) | (g2 << 8) | b2;
110     return argb;
111 }
112 
DrawCreateRect(DrawRect r)113 OH_Drawing_Rect* DrawCreateRect(DrawRect r)
114 {
115     OH_Drawing_Rect* rect = OH_Drawing_RectCreate(r.left, r.top, r.right, r.bottom);
116     return rect;
117 }
118 
DrawPathAddCircle(OH_Drawing_Path * path,float centerX,float centerY,float radius)119 void DrawPathAddCircle(OH_Drawing_Path* path, float centerX, float centerY, float radius)
120 {
121     OH_Drawing_Rect *rc = DrawCreateRect({centerX - radius, centerY - radius, centerX + radius, centerY + radius});
122     OH_Drawing_PathAddArc(path, rc, 0, 360); // 0,360 arc param
123     OH_Drawing_RectDestroy(rc);
124 }
125 
DrawBitmapGetAddr8(OH_Drawing_Bitmap * bitmap,int x,int y)126 uint8_t* DrawBitmapGetAddr8(OH_Drawing_Bitmap* bitmap, int x, int y)
127 {
128     uint32_t H = OH_Drawing_BitmapGetHeight(bitmap);
129     uint32_t W = OH_Drawing_BitmapGetWidth(bitmap);
130     void* pixel = OH_Drawing_BitmapGetPixels(bitmap);
131     uint8_t* ptr = (uint8_t*)pixel + (size_t)y*W +x;
132     return ptr;
133 }
134 
DrawBitmapGetAddr16(OH_Drawing_Bitmap * bitmap,int x,int y)135 uint16_t* DrawBitmapGetAddr16(OH_Drawing_Bitmap* bitmap, int x, int y)
136 {
137     uint32_t H = OH_Drawing_BitmapGetHeight(bitmap);
138     uint32_t W = OH_Drawing_BitmapGetWidth(bitmap);
139     void* pixel = OH_Drawing_BitmapGetPixels(bitmap);
140     uint16_t* ptr = (uint16_t*)pixel + (uint16_t)(y*W) +(x);
141     return ptr;
142 }
143 
DrawBitmapGetAddr32(OH_Drawing_Bitmap * bitmap,int x,int y)144 uint32_t* DrawBitmapGetAddr32(OH_Drawing_Bitmap* bitmap, int x, int y)
145 {
146     uint32_t H = OH_Drawing_BitmapGetHeight(bitmap);
147     uint32_t W = OH_Drawing_BitmapGetWidth(bitmap);
148     void* pixel = OH_Drawing_BitmapGetPixels(bitmap);
149     uint32_t* ptr = (uint32_t*)pixel + (uint32_t)(y*W) +(x);
150     return ptr;
151 }
152 
DrawPathGetBound(DrawRect & r,float x,float y)153 void DrawPathGetBound(DrawRect& r, float x, float y)
154 {
155     // 比最小的小,比最大的大就设置
156     if (x < r.left) {
157         r.left = x;
158     }
159     if (x > r.right) {
160         r.right = x;
161     }
162     if (y < r.top) {
163         r.top = y;
164     }
165     if (y > r.bottom) {
166         r.bottom = y;
167     }
168 }
169 
170 
ConvertStringFromJsValue(napi_env env,napi_value jsValue,std::string & value)171 bool ConvertStringFromJsValue(napi_env env, napi_value jsValue, std::string &value)
172 {
173     size_t len = 0;
174     if (napi_get_value_string_utf8(env, jsValue, nullptr, 0, &len) != napi_ok) {
175         return false;
176     }
177     auto buffer = std::make_unique<char[]>(len + 1);
178     size_t strLength = 0;
179     if (napi_get_value_string_utf8(env, jsValue, buffer.get(), len + 1, &strLength) == napi_ok) {
180         value = buffer.get();
181         return true;
182     }
183     return false;
184 }
185 
ConvertIntFromJsValue(napi_env env,napi_value jsValue,uint32_t & value)186 bool ConvertIntFromJsValue(napi_env env, napi_value jsValue, uint32_t &value)
187 {
188     return napi_get_value_uint32(env, jsValue, &value) == napi_ok;
189 }