• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_LOGD("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_LOGD("the pixel color not match vAddr:%p position:%d pixel:%08x color:%08x", handle.virAddr,
124             position, checkColor, color);
125         DISPLAY_TEST_LOGD("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     uint8_t *data = reinterpret_cast<uint8_t *>(&dst);
135     for (uint8_t i = 0; i < sizeof(uint32_t); i++) {
136         *(data + i) = (value >> ((sizeof(uint32_t) - i - 1) * BITS_PER_BYTE)) & 0xff;
137     }
138 }
139 
SetPixel(const BufferHandle & handle,int x,int y,uint32_t color)140 void SetPixel(const BufferHandle &handle, int x, int y, uint32_t color)
141 {
142     constexpr int32_t pixelBytes = 4;
143     constexpr int32_t bpp = 32;
144     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((bpp <= 0),
145         DISPLAY_TEST_LOGE("CheckPixel do not support format %d", handle.format));
146     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((handle.virAddr == nullptr),
147         DISPLAY_TEST_LOGE("CheckPixel viraddr is null must map it"));
148     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((x < 0 || x >= handle.width),
149         DISPLAY_TEST_LOGE("CheckPixel invalid parameter x:%d width:%d", x, handle.width));
150     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((y < 0 || y >= handle.height),
151         DISPLAY_TEST_LOGE("CheckPixel invalid parameter y:%d height:%d", y, handle.height));
152 
153     int32_t position = y * handle.width + x;
154     if ((position * pixelBytes) > handle.size) {
155         DISPLAY_TEST_LOGE("the pixel position outside\n");
156     }
157     uint32_t *pixel = reinterpret_cast<uint32_t *>(handle.virAddr) + position;
158     SetUint32(*pixel, color);
159 }
160 
ClearColor(const BufferHandle & handle,uint32_t color)161 void ClearColor(const BufferHandle &handle, uint32_t color)
162 {
163     for (int32_t x = 0; x < handle.width; x++) {
164         for (int32_t y = 0; y < handle.height; y++) {
165             SetPixel(handle, x, y, color);
166         }
167     }
168 }
169 
ClearColorRect(const BufferHandle & handle,uint32_t color,const IRect & rect)170 void ClearColorRect(const BufferHandle &handle, uint32_t color, const IRect &rect)
171 {
172     DISPLAY_TEST_LOGD("x %d, y %d w %d h %d color %x ", rect.x, rect.y, rect.w, rect.h, color);
173     for (int32_t x = 0; x < rect.w; x++) {
174         for (int32_t y = 0; y < rect.h; y++) {
175             SetPixel(handle, x + rect.x, y + rect.y, color);
176         }
177     }
178 }
179 
SplitBuffer(const BufferHandle & handle,std::vector<uint32_t> & colors)180 std::vector<IRect> SplitBuffer(const BufferHandle &handle, std::vector<uint32_t> &colors)
181 {
182     std::vector<IRect> splitRects;
183     if (colors.empty()) {
184         DISPLAY_TEST_LOGD("the colors empty");
185     }
186     const uint32_t rowNum = sqrt(colors.size());
187     const uint32_t colNum = rowNum;
188     if (colNum == 0) {
189         DISPLAY_TEST_LOGD("rowNum and colNum are zero");
190         return splitRects;
191     }
192     const uint32_t cellWidth = handle.width / rowNum;
193     const uint32_t cellHeight = handle.height / colNum;
194     IRect rect = { 0, 0, cellWidth, cellHeight };
195     DISPLAY_TEST_LOGD("rowNum %u, colNum %u cellWidth %u cellHeight %u", rowNum, colNum, cellWidth, cellHeight);
196     uint32_t count = 0;
197     for (uint32_t x = 0; x < rowNum; x++) {
198         for (uint32_t y = 0; y < colNum; y++) {
199             rect.x = x * cellWidth;
200             rect.y = y * cellHeight;
201             ClearColorRect(handle, colors[count++], rect);
202             splitRects.push_back(rect);
203         }
204     }
205     SaveFile("/data/splitbuffer_data_", static_cast<uint8_t *>(handle.virAddr), handle.size);
206     return splitRects;
207 }
208 } // OHOS
209 } // HDI
210 } // DISPLAY
211 } // TEST
212