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 "display_test.h"
17 #include "buffer_handle.h"
18 namespace OHOS {
19 namespace HDI {
20 namespace Display {
21 namespace TEST {
SetUint32(uint32_t & dst,uint32_t value)22 void SetUint32(uint32_t& dst, uint32_t value)
23 {
24     uint8_t* data = reinterpret_cast<uint8_t *>(&dst);
25     for (uint8_t i = 0; i < sizeof(uint32_t); i++) {
26         *(data + i) = (value >> ((sizeof(uint32_t) - i - 1) * BITS_PER_BYTE)) & 0xff;
27     }
28 }
29 
SetPixel(const BufferHandle & handle,int x,int y,uint32_t color)30 void SetPixel(const BufferHandle& handle, int x, int y, uint32_t color)
31 {
32     const int32_t PIXEL_BYTES = 4;
33     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((handle.format <= 0),
34         DISPLAY_TEST_LOGE("CheckPixel do not support format %{public}d", handle.format));
35     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((handle.virAddr == nullptr),
36         DISPLAY_TEST_LOGE("CheckPixel viraddr is null must map it"));
37     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((x < 0 || x >= handle.width),
38         DISPLAY_TEST_LOGE("CheckPixel invalid parameter x:%{public}d width:%{public}d", x, handle.width));
39     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((y < 0 || y >= handle.height),
40         DISPLAY_TEST_LOGE("CheckPixel invalid parameter y:%{public}d height:%{public}d", y, handle.height));
41 
42     int32_t position = y * handle.width + x;
43     if ((position * PIXEL_BYTES) > handle.size) {
44         DISPLAY_TEST_LOGE("the pixel position outside\n");
45     }
46     uint32_t* pixel = reinterpret_cast<uint32_t *>(handle.virAddr) + position;
47     DISPLAY_TEST_CHK_RETURN_NOT_VALUE((pixel == nullptr), DISPLAY_TEST_LOGE("get pixel failed"));
48     SetUint32(*pixel, color);
49 }
50 
ClearColor(const BufferHandle & handle,uint32_t color)51 void ClearColor(const BufferHandle& handle, uint32_t color)
52 {
53     for (int32_t x = 0; x < handle.width; x++) {
54         for (int32_t y = 0; y < handle.height; y++) {
55             SetPixel(handle, x, y, color);
56         }
57     }
58 }
59 } // OHOS
60 } // HDI
61 } // Display
62 } // TEST
63