1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include "display_test.h"
17094332d3Sopenharmony_ci#include "buffer_handle.h"
18094332d3Sopenharmony_cinamespace OHOS {
19094332d3Sopenharmony_cinamespace HDI {
20094332d3Sopenharmony_cinamespace Display {
21094332d3Sopenharmony_cinamespace TEST {
22094332d3Sopenharmony_civoid SetUint32(uint32_t& dst, uint32_t value)
23094332d3Sopenharmony_ci{
24094332d3Sopenharmony_ci    uint8_t* data = reinterpret_cast<uint8_t *>(&dst);
25094332d3Sopenharmony_ci    for (uint8_t i = 0; i < sizeof(uint32_t); i++) {
26094332d3Sopenharmony_ci        *(data + i) = (value >> ((sizeof(uint32_t) - i - 1) * BITS_PER_BYTE)) & 0xff;
27094332d3Sopenharmony_ci    }
28094332d3Sopenharmony_ci}
29094332d3Sopenharmony_ci
30094332d3Sopenharmony_civoid SetPixel(const BufferHandle& handle, int x, int y, uint32_t color)
31094332d3Sopenharmony_ci{
32094332d3Sopenharmony_ci    const int32_t PIXEL_BYTES = 4;
33094332d3Sopenharmony_ci    DISPLAY_TEST_CHK_RETURN_NOT_VALUE((handle.format <= 0),
34094332d3Sopenharmony_ci        DISPLAY_TEST_LOGE("CheckPixel do not support format %{public}d", handle.format));
35094332d3Sopenharmony_ci    DISPLAY_TEST_CHK_RETURN_NOT_VALUE((handle.virAddr == nullptr),
36094332d3Sopenharmony_ci        DISPLAY_TEST_LOGE("CheckPixel viraddr is null must map it"));
37094332d3Sopenharmony_ci    DISPLAY_TEST_CHK_RETURN_NOT_VALUE((x < 0 || x >= handle.width),
38094332d3Sopenharmony_ci        DISPLAY_TEST_LOGE("CheckPixel invalid parameter x:%{public}d width:%{public}d", x, handle.width));
39094332d3Sopenharmony_ci    DISPLAY_TEST_CHK_RETURN_NOT_VALUE((y < 0 || y >= handle.height),
40094332d3Sopenharmony_ci        DISPLAY_TEST_LOGE("CheckPixel invalid parameter y:%{public}d height:%{public}d", y, handle.height));
41094332d3Sopenharmony_ci
42094332d3Sopenharmony_ci    int32_t position = y * handle.width + x;
43094332d3Sopenharmony_ci    if ((position * PIXEL_BYTES) > handle.size) {
44094332d3Sopenharmony_ci        DISPLAY_TEST_LOGE("the pixel position outside\n");
45094332d3Sopenharmony_ci    }
46094332d3Sopenharmony_ci    uint32_t* pixel = reinterpret_cast<uint32_t *>(handle.virAddr) + position;
47094332d3Sopenharmony_ci    DISPLAY_TEST_CHK_RETURN_NOT_VALUE((pixel == nullptr), DISPLAY_TEST_LOGE("get pixel failed"));
48094332d3Sopenharmony_ci    SetUint32(*pixel, color);
49094332d3Sopenharmony_ci}
50094332d3Sopenharmony_ci
51094332d3Sopenharmony_civoid ClearColor(const BufferHandle& handle, uint32_t color)
52094332d3Sopenharmony_ci{
53094332d3Sopenharmony_ci    for (int32_t x = 0; x < handle.width; x++) {
54094332d3Sopenharmony_ci        for (int32_t y = 0; y < handle.height; y++) {
55094332d3Sopenharmony_ci            SetPixel(handle, x, y, color);
56094332d3Sopenharmony_ci        }
57094332d3Sopenharmony_ci    }
58094332d3Sopenharmony_ci}
59094332d3Sopenharmony_ci} // OHOS
60094332d3Sopenharmony_ci} // HDI
61094332d3Sopenharmony_ci} // Display
62094332d3Sopenharmony_ci} // TEST
63