• 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 }
59 
ConverToRGBA(PixelFormat fmt,uint32_t color)60 static uint32_t ConverToRGBA(PixelFormat fmt, uint32_t color)
61 {
62     switch (fmt) {
63         case PIXEL_FMT_BGRA_8888:
64             return BGRAToRGBA(color);
65         case PIXEL_FMT_RGBA_8888:
66             return color;
67         default:
68             DISPLAY_TEST_LOGE("the fmt can not covert %d", fmt);
69     }
70     return color;
71 }
72 
GetPixelValue(const BufferHandle & handle,int x,int y)73 uint32_t GetPixelValue(const BufferHandle &handle, int x, int y)
74 {
75     const int32_t pixelBytes = 4;
76     int32_t bpp = GetPixelFormatBpp((PixelFormat)handle.format);
77     DISPLAY_TEST_CHK_RETURN((bpp <= 0), 0, DISPLAY_TEST_LOGE("CheckPixel do not support format %d", handle.format));
78     DISPLAY_TEST_CHK_RETURN((handle.virAddr == nullptr), 0,
79         DISPLAY_TEST_LOGE("CheckPixel viraddr is null must map it"));
80     DISPLAY_TEST_CHK_RETURN((x < 0 || x >= handle.width), 0,
81         DISPLAY_TEST_LOGE("CheckPixel invalid parameter x:%d width:%d", x, handle.width));
82     DISPLAY_TEST_CHK_RETURN((y < 0 || y >= handle.height), 0,
83         DISPLAY_TEST_LOGE("CheckPixel invalid parameter y:%d height:%d", y, handle.height));
84 
85     int32_t position = y * handle.width + x;
86     if ((position * pixelBytes) > handle.size) {
87         DISPLAY_TEST_LOGE("the pixel postion outside\n");
88     }
89     uint32_t *pixel = reinterpret_cast<uint32_t *>(handle.virAddr) + position;
90     return *pixel;
91 }
92 
GetUint32(uint32_t value)93 uint32_t GetUint32(uint32_t value)
94 {
95     uint32_t dst;
96     uint8_t *data = reinterpret_cast<uint8_t *>(&dst);
97     for (uint8_t i = 0; i < sizeof(uint32_t); i++) {
98         *(data + i) = (value >> ((sizeof(uint32_t) - i - 1) * BITS_PER_BYTE)) & 0xff;
99     }
100     return dst;
101 }
102 
CheckPixel(const BufferHandle & handle,int x,int y,uint32_t color)103 uint32_t CheckPixel(const BufferHandle &handle, int x, int y, uint32_t color)
104 {
105     const int32_t pixelBytes = 4;
106     int32_t bpp = GetPixelFormatBpp((PixelFormat)handle.format);
107     DISPLAY_TEST_CHK_RETURN((bpp <= 0), 0, DISPLAY_TEST_LOGE("CheckPixel do not support format %d", handle.format));
108     DISPLAY_TEST_CHK_RETURN((handle.virAddr == nullptr), 0,
109         DISPLAY_TEST_LOGE("CheckPixel viraddr is null must map it"));
110     DISPLAY_TEST_CHK_RETURN((x < 0 || x >= handle.width), 0,
111         DISPLAY_TEST_LOGE("CheckPixel invalid parameter x:%d width:%d", x, handle.width));
112     DISPLAY_TEST_CHK_RETURN((y < 0 || y >= handle.height), 0,
113         DISPLAY_TEST_LOGE("CheckPixel invalid parameter y:%d height:%d", y, handle.height));
114 
115     int32_t position = y * handle.width + x;
116     if ((position * pixelBytes) > handle.size) {
117         DISPLAY_TEST_LOGE("the pixel postion outside\n");
118     }
119     uint32_t *pixel = reinterpret_cast<uint32_t *>(handle.virAddr) + position;
120     uint32_t checkColor = ConverToRGBA(static_cast<PixelFormat>(handle.format), GetUint32(*pixel));
121     if (checkColor != color) {
122         DISPLAY_TEST_LOGD("the pixel color not match vAddr:%p position:%d pixel:%08x color:%08x", handle.virAddr,
123             position, checkColor, color);
124         DISPLAY_TEST_LOGD("x:%d y:%d width:%d", x, y, handle.width);
125         SaveFile("/data/display_test_bitmap_", static_cast<uint8_t *>(handle.virAddr), handle.size);
126         return DISPLAY_FAILURE;
127     }
128     return DISPLAY_SUCCESS;
129 }
130 
SetUint32(uint32_t & dst,uint32_t value)131 void SetUint32(uint32_t &dst, uint32_t value)
132 {
133     constexpr uint8_t BITS_PER_BYTE = 8;
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 postion 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,IRect & rect)170 void ClearColorRect(const BufferHandle &handle, uint32_t color, 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 %d, colNum %d cellWidth %d cellHeight %d", 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