• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <cmath>
17 #include <vector>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include "display_test.h"
21 #include "buffer_handle.h"
22 
23 #include "v1_0/display_composer_type.h"
24 #include "v1_0/include/idisplay_buffer.h"
25 
26 namespace OHOS {
27 namespace HDI {
28 namespace Display {
29 namespace TEST {
30 using namespace OHOS::HDI::Display::Composer::V1_0;
31 using namespace OHOS::HDI::Display::Buffer::V1_0;
32 using namespace OHOS::HDI::Display::Composer::V1_0;
33 
34 const uint8_t BITS_PER_BYTE = 8;
35 
BGRAToRGBA(uint32_t bgra)36 static uint32_t BGRAToRGBA(uint32_t bgra)
37 {
38     uint32_t rgba = 0;
39     const uint32_t COLOR_RED = 0x0000ff00;
40     const uint32_t COLOR_GREEN = 0x00ff0000;
41     const uint32_t COLOR_BLUE = 0xff000000;
42     const uint32_t ALPHA = 0x000000ff;
43     const int32_t TWO_BYTE_OFFSET = 16;
44 
45     rgba |= (bgra & COLOR_RED) << TWO_BYTE_OFFSET; // get red then move to rgba
46     rgba |= (bgra & COLOR_GREEN);                  // get green
47     rgba |= (bgra & COLOR_BLUE) >> TWO_BYTE_OFFSET; // get blue then move to rgba
48     rgba |= (bgra & ALPHA);                  // get alpha
49 
50     return rgba;
51 }
52 
GetPixelFormatBpp(PixelFormat format)53 static int32_t GetPixelFormatBpp(PixelFormat format)
54 {
55     const int32_t BPP_RGBA_8888 = 32;
56     switch (format) {
57         case PIXEL_FMT_RGBA_8888:
58             return BPP_RGBA_8888;
59         case PIXEL_FMT_BGRA_8888:
60             return BPP_RGBA_8888;
61         default:
62             return -1;
63     }
64 }
65 
SaveFile(const char * fileName,uint8_t * data,int size)66 void SaveFile(const char *fileName, uint8_t *data, int size)
67 {
68     if (fileName != nullptr && data != nullptr) {
69         int fileFd = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
70         if (fileFd <= 0) {
71             DISPLAY_TEST_LOGE("Open file failed %{public}d", fileFd);
72             return;
73         }
74 
75         int hasWriten = write(fileFd, data, size);
76         DISPLAY_TEST_LOGE("SaveFile hasWriten %{public}d", hasWriten);
77         close(fileFd);
78     } else {
79         DISPLAY_TEST_LOGE("SaveFile failed");
80     }
81 }
82 
ConverToRGBA(PixelFormat fmt,uint32_t color)83 static uint32_t ConverToRGBA(PixelFormat fmt, uint32_t color)
84 {
85     switch (fmt) {
86         case PIXEL_FMT_BGRA_8888:
87             return BGRAToRGBA(color);
88         case PIXEL_FMT_RGBA_8888:
89             return color;
90         default:
91             DISPLAY_TEST_LOGE("the fmt can not convert %{public}d", fmt);
92     }
93     return color;
94 }
95 
GetPixelValue(const BufferHandle & handle,int x,int y)96 uint32_t GetPixelValue(const BufferHandle &handle, int x, int y)
97 {
98     const int32_t PIXEL_BYTES = 4;
99     int32_t bpp = GetPixelFormatBpp((PixelFormat)handle.format);
100     DISPLAY_TEST_CHK_RETURN((bpp <= 0), 0, DISPLAY_TEST_LOGE("CheckPixel do not support format %{public}d",
101         handle.format));
102     DISPLAY_TEST_CHK_RETURN((handle.virAddr == nullptr), 0,
103         DISPLAY_TEST_LOGE("CheckPixel viraddr is null must map it"));
104     DISPLAY_TEST_CHK_RETURN((x < 0 || x >= handle.width), 0,
105         DISPLAY_TEST_LOGE("CheckPixel invalid parameter x:%{public}d width:%{public}d", x, handle.width));
106     DISPLAY_TEST_CHK_RETURN((y < 0 || y >= handle.height), 0,
107         DISPLAY_TEST_LOGE("CheckPixel invalid parameter y:%{public}d height:%{public}d", y, handle.height));
108 
109     int32_t position = y * handle.width + x;
110     if ((position * PIXEL_BYTES) > handle.size) {
111         DISPLAY_TEST_LOGE("the pixel position outside\n");
112     }
113     uint32_t *pixel = reinterpret_cast<uint32_t *>(handle.virAddr) + position;
114     DISPLAY_TEST_CHK_RETURN((pixel == nullptr), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("get pixel failed"));
115     return *pixel;
116 }
117 
GetUint32(uint32_t value)118 uint32_t GetUint32(uint32_t value)
119 {
120     uint32_t dst;
121     uint8_t *data = reinterpret_cast<uint8_t *>(&dst);
122     for (uint8_t i = 0; i < sizeof(uint32_t); i++) {
123         *(data + i) = (value >> ((sizeof(uint32_t) - i - 1) * BITS_PER_BYTE)) & 0xff;
124     }
125     return dst;
126 }
127 
CheckPixel(const BufferHandle & handle,int x,int y,uint32_t color)128 uint32_t CheckPixel(const BufferHandle &handle, int x, int y, uint32_t color)
129 {
130     const int32_t PIXEL_BYTES = 4;
131     int32_t bpp = GetPixelFormatBpp(static_cast<PixelFormat>(handle.format));
132     DISPLAY_TEST_CHK_RETURN((bpp <= 0), 0, DISPLAY_TEST_LOGE("CheckPixel do not support format %{public}d",
133         handle.format));
134     DISPLAY_TEST_CHK_RETURN((handle.virAddr == nullptr), 0,
135         DISPLAY_TEST_LOGE("CheckPixel viraddr is null must map it"));
136     DISPLAY_TEST_CHK_RETURN((x < 0 || x >= handle.width), 0,
137         DISPLAY_TEST_LOGE("CheckPixel invalid parameter x:%{public}d width:%{public}d", x, handle.width));
138     DISPLAY_TEST_CHK_RETURN((y < 0 || y >= handle.height), 0,
139         DISPLAY_TEST_LOGE("CheckPixel invalid parameter y:%{public}d height:%{public}d", y, handle.height));
140 
141     int32_t position = y * handle.width + x;
142     if ((position * PIXEL_BYTES) > handle.size) {
143         DISPLAY_TEST_LOGE("the pixel position outside\n");
144     }
145     uint32_t *pixel = reinterpret_cast<uint32_t *>(handle.virAddr) + position;
146     DISPLAY_TEST_CHK_RETURN((pixel == nullptr), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("get pixel failed"));
147     uint32_t checkColor = ConverToRGBA(static_cast<PixelFormat>(handle.format), GetUint32(*pixel));
148     if (checkColor != color) {
149         DISPLAY_TEST_LOGE("x:%{public}d y:%{public}d width:%{public}d", x, y, handle.width);
150         SaveFile("/data/display_test_bitmap_", static_cast<uint8_t *>(handle.virAddr), handle.size);
151         return DISPLAY_FAILURE;
152     }
153     return DISPLAY_SUCCESS;
154 }
155 
SetUint32(uint32_t & dst,uint32_t value)156 void SetUint32(uint32_t &dst, uint32_t value)
157 {
158     uint8_t *data = reinterpret_cast<uint8_t *>(&dst);
159     if (data != nullptr) {
160     for (uint8_t i = 0; i < sizeof(uint32_t); i++) {
161         *(data + i) = (value >> ((sizeof(uint32_t) - i - 1) * BITS_PER_BYTE)) & 0xff;
162         }
163     } else {
164         DISPLAY_TEST_LOGE("SetUint32 failed");
165     }
166 }
167 
SetPixel(const BufferHandle & handle,int x,int y,uint32_t color)168 void SetPixel(const BufferHandle &handle, int x, int y, uint32_t color)
169 {
170     const int32_t PIXEL_BYTES = 4;
171     const int32_t BPP = 32;
172     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((BPP <= 0),
173         DISPLAY_TEST_LOGE("CheckPixel do not support format %{public}d", handle.format));
174     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((handle.virAddr == nullptr),
175         DISPLAY_TEST_LOGE("CheckPixel viraddr is null must map it"));
176     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((x < 0 || x >= handle.width),
177         DISPLAY_TEST_LOGE("CheckPixel invalid parameter x:%{public}d width:%{public}d", x, handle.width));
178     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((y < 0 || y >= handle.height),
179         DISPLAY_TEST_LOGE("CheckPixel invalid parameter y:%{public}d height:%{public}d", y, handle.height));
180 
181     int32_t position = y * handle.width + x;
182     if ((position * PIXEL_BYTES) > handle.size) {
183         DISPLAY_TEST_LOGE("the pixel position outside\n");
184     }
185     uint32_t *pixel = reinterpret_cast<uint32_t *>(handle.virAddr) + position;
186     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((pixel == nullptr), DISPLAY_TEST_LOGE("get pixel failed"));
187     SetUint32(*pixel, color);
188 }
189 
ClearColor(const BufferHandle & handle,uint32_t color)190 void ClearColor(const BufferHandle &handle, uint32_t color)
191 {
192     for (int32_t x = 0; x < handle.width; x++) {
193         for (int32_t y = 0; y < handle.height; y++) {
194             SetPixel(handle, x, y, color);
195         }
196     }
197 }
198 
ClearColorRect(const BufferHandle & handle,uint32_t color,const IRect & rect)199 void ClearColorRect(const BufferHandle &handle, uint32_t color, const IRect &rect)
200 {
201     DISPLAY_TEST_LOGE("x %{public}d, y %{public}d w %{public}d h %{public}d color %x ", rect.x, rect.y, rect.w, rect.h,
202         color);
203     for (int32_t x = 0; x < rect.w; x++) {
204         for (int32_t y = 0; y < rect.h; y++) {
205             SetPixel(handle, x + rect.x, y + rect.y, color);
206         }
207     }
208 }
209 
SplitBuffer(const BufferHandle & handle,std::vector<uint32_t> & colors)210 std::vector<IRect> SplitBuffer(const BufferHandle &handle, std::vector<uint32_t> &colors)
211 {
212     std::vector<IRect> splitRects;
213     if (colors.empty()) {
214         DISPLAY_TEST_LOGE("the colors empty");
215     }
216     const uint32_t ROW_NUM = sqrt(colors.size());
217     const uint32_t COL_NUM = ROW_NUM;
218     if (ROW_NUM == 0) {
219         DISPLAY_TEST_LOGE("ROW_NUM is zero");
220         return splitRects;
221     }
222 
223     const uint32_t CELL_WIDTH = handle.width / ROW_NUM;
224     const uint32_t CELL_HEIGHT = handle.height / COL_NUM;
225     IRect rect = { 0, 0, CELL_WIDTH, CELL_HEIGHT };
226     DISPLAY_TEST_LOGE("ROW_NUM %{public}u, COL_NUM %{public}u CELL_WIDTH %{public}u CELL_HEIGHT %{public}u",
227         ROW_NUM, COL_NUM, CELL_WIDTH, CELL_HEIGHT);
228     uint32_t count = 0;
229     for (uint32_t x = 0; x < ROW_NUM; x++) {
230         for (uint32_t y = 0; y < COL_NUM; y++) {
231             rect.x = x * CELL_WIDTH;
232             rect.y = y * CELL_HEIGHT;
233             ClearColorRect(handle, colors[count++], rect);
234             splitRects.push_back(rect);
235         }
236     }
237     SaveFile("/data/splitbuffer_data_", static_cast<uint8_t *>(handle.virAddr), handle.size);
238     return splitRects;
239 }
240 } // OHOS
241 } // HDI
242 } // Display
243 } // TEST
244