• 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_LOGD("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 
116     return *pixel;
117 }
118 
GetUint32(uint32_t value)119 uint32_t GetUint32(uint32_t value)
120 {
121     uint32_t dst;
122     uint8_t *data = reinterpret_cast<uint8_t *>(&dst);
123     for (uint8_t i = 0; i < sizeof(uint32_t); i++) {
124         *(data + i) = (value >> ((sizeof(uint32_t) - i - 1) * BITS_PER_BYTE)) & 0xff;
125     }
126     return dst;
127 }
128 
CheckPixel(const BufferHandle & handle,int x,int y,uint32_t color)129 uint32_t CheckPixel(const BufferHandle &handle, int x, int y, uint32_t color)
130 {
131     const int32_t PIXEL_BYTES = 4;
132     int32_t bpp = GetPixelFormatBpp(static_cast<PixelFormat>(handle.format));
133     DISPLAY_TEST_CHK_RETURN((bpp <= 0), 0, DISPLAY_TEST_LOGE("CheckPixel do not support format %{public}d",
134         handle.format));
135     DISPLAY_TEST_CHK_RETURN((handle.virAddr == nullptr), 0,
136         DISPLAY_TEST_LOGE("CheckPixel viraddr is null must map it"));
137     DISPLAY_TEST_CHK_RETURN((x < 0 || x >= handle.width), 0,
138         DISPLAY_TEST_LOGE("CheckPixel invalid parameter x:%{public}d width:%{public}d", x, handle.width));
139     DISPLAY_TEST_CHK_RETURN((y < 0 || y >= handle.height), 0,
140         DISPLAY_TEST_LOGE("CheckPixel invalid parameter y:%{public}d height:%{public}d", y, handle.height));
141 
142     int32_t position = y * handle.width + x;
143     if ((position * PIXEL_BYTES) > handle.size) {
144         DISPLAY_TEST_LOGE("the pixel position outside\n");
145     }
146     uint32_t *pixel = reinterpret_cast<uint32_t *>(handle.virAddr) + position;
147     DISPLAY_TEST_CHK_RETURN((pixel == nullptr), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("get pixel failed"));
148 
149     uint32_t checkColor = ConverToRGBA(static_cast<PixelFormat>(handle.format), GetUint32(*pixel));
150     if (checkColor != color) {
151         DISPLAY_TEST_LOGD("x:%{public}d y:%{public}d width:%{public}d", x, y, handle.width);
152         SaveFile("/data/display_test_bitmap_", static_cast<uint8_t *>(handle.virAddr), handle.size);
153         return DISPLAY_FAILURE;
154     }
155     return DISPLAY_SUCCESS;
156 }
157 
SetUint32(uint32_t & dst,uint32_t value)158 void SetUint32(uint32_t &dst, uint32_t value)
159 {
160     uint8_t *data = reinterpret_cast<uint8_t *>(&dst);
161     if (data != nullptr) {
162         for (uint8_t i = 0; i < sizeof(uint32_t); i++) {
163             *(data + i) = (value >> ((sizeof(uint32_t) - i - 1) * BITS_PER_BYTE)) & 0xff;
164         }
165     } else {
166         DISPLAY_TEST_LOGE("SetUint32 failed");
167     }
168 }
169 
SetPixel(const BufferHandle & handle,int x,int y,uint32_t color)170 void SetPixel(const BufferHandle &handle, int x, int y, uint32_t color)
171 {
172     const int32_t PIXEL_BYTES = 4;
173     const int32_t BPP = 32;
174     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((BPP <= 0),
175         DISPLAY_TEST_LOGE("CheckPixel do not support format %{public}d", handle.format));
176     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((handle.virAddr == nullptr),
177         DISPLAY_TEST_LOGE("CheckPixel viraddr is null must map it"));
178     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((x < 0 || x >= handle.width),
179         DISPLAY_TEST_LOGE("CheckPixel invalid parameter x:%{public}d width:%{public}d", x, handle.width));
180     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((y < 0 || y >= handle.height),
181         DISPLAY_TEST_LOGE("CheckPixel invalid parameter y:%{public}d height:%{public}d", y, handle.height));
182 
183     int32_t position = y * handle.width + x;
184     if ((position * PIXEL_BYTES) > handle.size) {
185         DISPLAY_TEST_LOGE("the pixel position outside\n");
186     }
187     uint32_t *pixel = reinterpret_cast<uint32_t *>(handle.virAddr) + position;
188     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((pixel == nullptr), DISPLAY_TEST_LOGE("get pixel failed"));
189 
190     SetUint32(*pixel, color);
191 }
192 
ClearColor(const BufferHandle & handle,uint32_t color)193 void ClearColor(const BufferHandle &handle, uint32_t color)
194 {
195     for (int32_t x = 0; x < handle.width; x++) {
196         for (int32_t y = 0; y < handle.height; y++) {
197             SetPixel(handle, x, y, color);
198         }
199     }
200 }
201 
ClearColorRect(const BufferHandle & handle,uint32_t color,const IRect & rect)202 void ClearColorRect(const BufferHandle &handle, uint32_t color, const IRect &rect)
203 {
204     DISPLAY_TEST_LOGD("x %{public}d, y %{public}d w %{public}d h %{public}d color %x ", rect.x, rect.y, rect.w, rect.h,
205         color);
206     for (int32_t x = 0; x < rect.w; x++) {
207         for (int32_t y = 0; y < rect.h; y++) {
208             SetPixel(handle, x + rect.x, y + rect.y, color);
209         }
210     }
211 }
212 
SplitBuffer(const BufferHandle & handle,std::vector<uint32_t> & colors)213 std::vector<IRect> SplitBuffer(const BufferHandle &handle, std::vector<uint32_t> &colors)
214 {
215     std::vector<IRect> splitRects;
216     if (colors.empty()) {
217         DISPLAY_TEST_LOGD("the colors empty");
218     }
219     const uint32_t ROW_NUM = sqrt(colors.size());
220     const uint32_t COL_NUM = ROW_NUM;
221     if (ROW_NUM == 0) {
222         DISPLAY_TEST_LOGD("ROW_NUM is zero");
223         return splitRects;
224     }
225 
226     const uint32_t CELL_WIDTH = handle.width / ROW_NUM;
227     const uint32_t CELL_HEIGHT = handle.height / COL_NUM;
228     IRect rect = { 0, 0, CELL_WIDTH, CELL_HEIGHT };
229     DISPLAY_TEST_LOGD("ROW_NUM %{public}u, COL_NUM %{public}u CELL_WIDTH %{public}u CELL_HEIGHT %{public}u",
230         ROW_NUM, COL_NUM, CELL_WIDTH, CELL_HEIGHT);
231     uint32_t count = 0;
232     for (uint32_t x = 0; x < ROW_NUM; x++) {
233         for (uint32_t y = 0; y < COL_NUM; y++) {
234             rect.x = x * CELL_WIDTH;
235             rect.y = y * CELL_HEIGHT;
236             ClearColorRect(handle, colors[count++], rect);
237             splitRects.push_back(rect);
238         }
239     }
240     SaveFile("/data/splitbuffer_data_", static_cast<uint8_t *>(handle.virAddr), handle.size);
241     return splitRects;
242 }
243 } // OHOS
244 } // HDI
245 } // Display
246 } // TEST
247