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