19762338dSopenharmony_ci/*
29762338dSopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49762338dSopenharmony_ci * you may not use this file except in compliance with the License.
59762338dSopenharmony_ci * You may obtain a copy of the License at
69762338dSopenharmony_ci *
79762338dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
89762338dSopenharmony_ci *
99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129762338dSopenharmony_ci * See the License for the specific language governing permissions and
139762338dSopenharmony_ci * limitations under the License.
149762338dSopenharmony_ci */
159762338dSopenharmony_ci
169762338dSopenharmony_ci#include <cmath>
179762338dSopenharmony_ci#include <vector>
189762338dSopenharmony_ci#include <fcntl.h>
199762338dSopenharmony_ci#include <unistd.h>
209762338dSopenharmony_ci#include "display_test.h"
219762338dSopenharmony_ci#include "buffer_handle.h"
229762338dSopenharmony_ci
239762338dSopenharmony_ci#include "v1_1/display_composer_type.h"
249762338dSopenharmony_ci#include "v1_0/include/idisplay_buffer.h"
259762338dSopenharmony_ci
269762338dSopenharmony_cinamespace OHOS {
279762338dSopenharmony_cinamespace HDI {
289762338dSopenharmony_cinamespace Display {
299762338dSopenharmony_cinamespace TEST {
309762338dSopenharmony_ciusing namespace OHOS::HDI::Display::Composer::V1_0;
319762338dSopenharmony_ciusing namespace OHOS::HDI::Display::Buffer::V1_0;
329762338dSopenharmony_ci
339762338dSopenharmony_ciconst uint8_t BITS_PER_BYTE = 8;
349762338dSopenharmony_ci
359762338dSopenharmony_cistatic uint32_t BGRAToRGBA(uint32_t bgra)
369762338dSopenharmony_ci{
379762338dSopenharmony_ci    uint32_t rgba = 0;
389762338dSopenharmony_ci    const uint32_t COLOR_RED = 0x0000ff00;
399762338dSopenharmony_ci    const uint32_t COLOR_GREEN = 0x00ff0000;
409762338dSopenharmony_ci    const uint32_t COLOR_BLUE = 0xff000000;
419762338dSopenharmony_ci    const uint32_t ALPHA = 0x000000ff;
429762338dSopenharmony_ci    const int32_t TWO_BYTE_OFFSET = 16;
439762338dSopenharmony_ci
449762338dSopenharmony_ci    rgba |= (bgra & COLOR_RED) << TWO_BYTE_OFFSET; // get red then move to rgba
459762338dSopenharmony_ci    rgba |= (bgra & COLOR_GREEN);                  // get green
469762338dSopenharmony_ci    rgba |= (bgra & COLOR_BLUE) >> TWO_BYTE_OFFSET; // get blue then move to rgba
479762338dSopenharmony_ci    rgba |= (bgra & ALPHA);                  // get alpha
489762338dSopenharmony_ci
499762338dSopenharmony_ci    return rgba;
509762338dSopenharmony_ci}
519762338dSopenharmony_ci
529762338dSopenharmony_cistatic int32_t GetPixelFormatBpp(Composer::V1_0::PixelFormat format)
539762338dSopenharmony_ci{
549762338dSopenharmony_ci    const int32_t BPP_RGBA_8888 = 32;
559762338dSopenharmony_ci    switch (format) {
569762338dSopenharmony_ci        case Composer::V1_0::PIXEL_FMT_RGBA_8888:
579762338dSopenharmony_ci            return BPP_RGBA_8888;
589762338dSopenharmony_ci        case Composer::V1_0::PIXEL_FMT_BGRA_8888:
599762338dSopenharmony_ci            return BPP_RGBA_8888;
609762338dSopenharmony_ci        default:
619762338dSopenharmony_ci            return -1;
629762338dSopenharmony_ci    }
639762338dSopenharmony_ci}
649762338dSopenharmony_ci
659762338dSopenharmony_civoid SaveFile(const char *fileName, uint8_t *data, int size)
669762338dSopenharmony_ci{
679762338dSopenharmony_ci    if (fileName != nullptr && data != nullptr) {
689762338dSopenharmony_ci        int fileFd = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
699762338dSopenharmony_ci        if (fileFd <= 0) {
709762338dSopenharmony_ci            DISPLAY_TEST_LOGE("Open file failed %{public}d", fileFd);
719762338dSopenharmony_ci            return;
729762338dSopenharmony_ci        }
739762338dSopenharmony_ci
749762338dSopenharmony_ci        int hasWriten = write(fileFd, data, size);
759762338dSopenharmony_ci        DISPLAY_TEST_LOGE("SaveFile hasWriten %{public}d", hasWriten);
769762338dSopenharmony_ci        close(fileFd);
779762338dSopenharmony_ci    } else {
789762338dSopenharmony_ci        DISPLAY_TEST_LOGE("SaveFile failed");
799762338dSopenharmony_ci    }
809762338dSopenharmony_ci}
819762338dSopenharmony_ci
829762338dSopenharmony_cistatic uint32_t ConverToRGBA(Composer::V1_0::PixelFormat fmt, uint32_t color)
839762338dSopenharmony_ci{
849762338dSopenharmony_ci    switch (fmt) {
859762338dSopenharmony_ci        case Composer::V1_0::PIXEL_FMT_BGRA_8888:
869762338dSopenharmony_ci            return BGRAToRGBA(color);
879762338dSopenharmony_ci        case Composer::V1_0::PIXEL_FMT_RGBA_8888:
889762338dSopenharmony_ci            return color;
899762338dSopenharmony_ci        default:
909762338dSopenharmony_ci            DISPLAY_TEST_LOGE("the fmt can not convert %{public}d", fmt);
919762338dSopenharmony_ci    }
929762338dSopenharmony_ci    return color;
939762338dSopenharmony_ci}
949762338dSopenharmony_ci
959762338dSopenharmony_ciuint32_t GetPixelValue(const BufferHandle &handle, int x, int y)
969762338dSopenharmony_ci{
979762338dSopenharmony_ci    const int32_t PIXEL_BYTES = 4;
989762338dSopenharmony_ci    int32_t bpp = GetPixelFormatBpp((Composer::V1_0::PixelFormat)handle.format);
999762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN((bpp <= 0), 0, DISPLAY_TEST_LOGE("CheckPixel do not support format %{public}d",
1009762338dSopenharmony_ci        handle.format));
1019762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN((handle.virAddr == nullptr), 0,
1029762338dSopenharmony_ci        DISPLAY_TEST_LOGE("CheckPixel viraddr is null must map it"));
1039762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN((x < 0 || x >= handle.width), 0,
1049762338dSopenharmony_ci        DISPLAY_TEST_LOGE("CheckPixel invalid parameter x:%{public}d width:%{public}d", x, handle.width));
1059762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN((y < 0 || y >= handle.height), 0,
1069762338dSopenharmony_ci        DISPLAY_TEST_LOGE("CheckPixel invalid parameter y:%{public}d height:%{public}d", y, handle.height));
1079762338dSopenharmony_ci
1089762338dSopenharmony_ci    int32_t position = y * handle.width + x;
1099762338dSopenharmony_ci    if ((position * PIXEL_BYTES) > handle.size) {
1109762338dSopenharmony_ci        DISPLAY_TEST_LOGE("the pixel position outside\n");
1119762338dSopenharmony_ci    }
1129762338dSopenharmony_ci    uint32_t *pixel = reinterpret_cast<uint32_t *>(handle.virAddr) + position;
1139762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN((pixel == nullptr), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("get pixel failed"));
1149762338dSopenharmony_ci
1159762338dSopenharmony_ci    return *pixel;
1169762338dSopenharmony_ci}
1179762338dSopenharmony_ci
1189762338dSopenharmony_ciuint32_t GetUint32(uint32_t value)
1199762338dSopenharmony_ci{
1209762338dSopenharmony_ci    uint32_t dst;
1219762338dSopenharmony_ci    uint8_t *data = reinterpret_cast<uint8_t *>(&dst);
1229762338dSopenharmony_ci    for (uint8_t i = 0; i < sizeof(uint32_t); i++) {
1239762338dSopenharmony_ci        *(data + i) = (value >> ((sizeof(uint32_t) - i - 1) * BITS_PER_BYTE)) & 0xff;
1249762338dSopenharmony_ci    }
1259762338dSopenharmony_ci    return dst;
1269762338dSopenharmony_ci}
1279762338dSopenharmony_ci
1289762338dSopenharmony_ciuint32_t CheckPixel(const BufferHandle &handle, int x, int y, uint32_t color)
1299762338dSopenharmony_ci{
1309762338dSopenharmony_ci    const int32_t PIXEL_BYTES = 4;
1319762338dSopenharmony_ci    int32_t bpp = GetPixelFormatBpp(static_cast<Composer::V1_0::PixelFormat>(handle.format));
1329762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN((bpp <= 0), 0, DISPLAY_TEST_LOGE("CheckPixel do not support format %{public}d",
1339762338dSopenharmony_ci        handle.format));
1349762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN((handle.virAddr == nullptr), 0,
1359762338dSopenharmony_ci        DISPLAY_TEST_LOGE("CheckPixel viraddr is null must map it"));
1369762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN((x < 0 || x >= handle.width), 0,
1379762338dSopenharmony_ci        DISPLAY_TEST_LOGE("CheckPixel invalid parameter x:%{public}d width:%{public}d", x, handle.width));
1389762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN((y < 0 || y >= handle.height), 0,
1399762338dSopenharmony_ci        DISPLAY_TEST_LOGE("CheckPixel invalid parameter y:%{public}d height:%{public}d", y, handle.height));
1409762338dSopenharmony_ci
1419762338dSopenharmony_ci    int32_t position = y * handle.width + x;
1429762338dSopenharmony_ci    if ((position * PIXEL_BYTES) > handle.size) {
1439762338dSopenharmony_ci        DISPLAY_TEST_LOGE("the pixel position outside\n");
1449762338dSopenharmony_ci    }
1459762338dSopenharmony_ci    uint32_t *pixel = reinterpret_cast<uint32_t *>(handle.virAddr) + position;
1469762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN((pixel == nullptr), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("get pixel failed"));
1479762338dSopenharmony_ci
1489762338dSopenharmony_ci    uint32_t checkColor = ConverToRGBA(static_cast<Composer::V1_0::PixelFormat>(handle.format), GetUint32(*pixel));
1499762338dSopenharmony_ci    if (checkColor != color) {
1509762338dSopenharmony_ci        DISPLAY_TEST_LOGE("x:%{public}d y:%{public}d width:%{public}d", x, y, handle.width);
1519762338dSopenharmony_ci        SaveFile("/data/display_test_bitmap_", static_cast<uint8_t *>(handle.virAddr), handle.size);
1529762338dSopenharmony_ci        return DISPLAY_FAILURE;
1539762338dSopenharmony_ci    }
1549762338dSopenharmony_ci    return DISPLAY_SUCCESS;
1559762338dSopenharmony_ci}
1569762338dSopenharmony_ci
1579762338dSopenharmony_civoid SetUint32(uint32_t &dst, uint32_t value)
1589762338dSopenharmony_ci{
1599762338dSopenharmony_ci    uint8_t *data = reinterpret_cast<uint8_t *>(&dst);
1609762338dSopenharmony_ci    if (data != nullptr) {
1619762338dSopenharmony_ci        for (uint8_t i = 0; i < sizeof(uint32_t); i++) {
1629762338dSopenharmony_ci            *(data + i) = (value >> ((sizeof(uint32_t) - i - 1) * BITS_PER_BYTE)) & 0xff;
1639762338dSopenharmony_ci        }
1649762338dSopenharmony_ci    } else {
1659762338dSopenharmony_ci        DISPLAY_TEST_LOGE("SetUint32 failed");
1669762338dSopenharmony_ci    }
1679762338dSopenharmony_ci}
1689762338dSopenharmony_ci
1699762338dSopenharmony_civoid SetPixel(const BufferHandle &handle, int x, int y, uint32_t color)
1709762338dSopenharmony_ci{
1719762338dSopenharmony_ci    const int32_t PIXEL_BYTES = 4;
1729762338dSopenharmony_ci    const int32_t BPP = 32;
1739762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN_NOT_VALUE((BPP <= 0),
1749762338dSopenharmony_ci        DISPLAY_TEST_LOGE("CheckPixel do not support format %{public}d", handle.format));
1759762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN_NOT_VALUE((handle.virAddr == nullptr),
1769762338dSopenharmony_ci        DISPLAY_TEST_LOGE("CheckPixel viraddr is null must map it"));
1779762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN_NOT_VALUE((x < 0 || x >= handle.width),
1789762338dSopenharmony_ci        DISPLAY_TEST_LOGE("CheckPixel invalid parameter x:%{public}d width:%{public}d", x, handle.width));
1799762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN_NOT_VALUE((y < 0 || y >= handle.height),
1809762338dSopenharmony_ci        DISPLAY_TEST_LOGE("CheckPixel invalid parameter y:%{public}d height:%{public}d", y, handle.height));
1819762338dSopenharmony_ci
1829762338dSopenharmony_ci    int32_t position = y * handle.stride / PIXEL_BYTES + x;
1839762338dSopenharmony_ci    if ((position * PIXEL_BYTES) > handle.size) {
1849762338dSopenharmony_ci        DISPLAY_TEST_LOGE("the pixel position outside\n");
1859762338dSopenharmony_ci    }
1869762338dSopenharmony_ci    uint32_t *pixel = reinterpret_cast<uint32_t *>(handle.virAddr) + position;
1879762338dSopenharmony_ci    DISPLAY_TEST_CHK_RETURN_NOT_VALUE((pixel == nullptr), DISPLAY_TEST_LOGE("get pixel failed"));
1889762338dSopenharmony_ci
1899762338dSopenharmony_ci    SetUint32(*pixel, color);
1909762338dSopenharmony_ci}
1919762338dSopenharmony_ci
1929762338dSopenharmony_civoid ClearColor(const BufferHandle &handle, uint32_t color)
1939762338dSopenharmony_ci{
1949762338dSopenharmony_ci    for (int32_t x = 0; x < handle.width; x++) {
1959762338dSopenharmony_ci        for (int32_t y = 0; y < handle.height; y++) {
1969762338dSopenharmony_ci            SetPixel(handle, x, y, color);
1979762338dSopenharmony_ci        }
1989762338dSopenharmony_ci    }
1999762338dSopenharmony_ci}
2009762338dSopenharmony_ci
2019762338dSopenharmony_civoid ClearColorRect(const BufferHandle &handle, uint32_t color, const IRect &rect)
2029762338dSopenharmony_ci{
2039762338dSopenharmony_ci    DISPLAY_TEST_LOGE("x %{public}d, y %{public}d w %{public}d h %{public}d color %x ", rect.x, rect.y, rect.w, rect.h,
2049762338dSopenharmony_ci        color);
2059762338dSopenharmony_ci    for (int32_t x = 0; x < rect.w; x++) {
2069762338dSopenharmony_ci        for (int32_t y = 0; y < rect.h; y++) {
2079762338dSopenharmony_ci            SetPixel(handle, x + rect.x, y + rect.y, color);
2089762338dSopenharmony_ci        }
2099762338dSopenharmony_ci    }
2109762338dSopenharmony_ci}
2119762338dSopenharmony_ci
2129762338dSopenharmony_cistd::vector<IRect> SplitBuffer(const BufferHandle &handle, std::vector<uint32_t> &colors)
2139762338dSopenharmony_ci{
2149762338dSopenharmony_ci    std::vector<IRect> splitRects;
2159762338dSopenharmony_ci    if (colors.empty()) {
2169762338dSopenharmony_ci        DISPLAY_TEST_LOGE("the colors empty");
2179762338dSopenharmony_ci    }
2189762338dSopenharmony_ci    const uint32_t ROW_NUM = sqrt(colors.size());
2199762338dSopenharmony_ci    const uint32_t COL_NUM = ROW_NUM;
2209762338dSopenharmony_ci    if (ROW_NUM == 0) {
2219762338dSopenharmony_ci        DISPLAY_TEST_LOGE("ROW_NUM is zero");
2229762338dSopenharmony_ci        return splitRects;
2239762338dSopenharmony_ci    }
2249762338dSopenharmony_ci
2259762338dSopenharmony_ci    const uint32_t CELL_WIDTH = handle.width / ROW_NUM;
2269762338dSopenharmony_ci    const uint32_t CELL_HEIGHT = handle.height / COL_NUM;
2279762338dSopenharmony_ci    IRect rect = { 0, 0, CELL_WIDTH, CELL_HEIGHT };
2289762338dSopenharmony_ci    DISPLAY_TEST_LOGE("ROW_NUM %{public}u, COL_NUM %{public}u CELL_WIDTH %{public}u CELL_HEIGHT %{public}u",
2299762338dSopenharmony_ci        ROW_NUM, COL_NUM, CELL_WIDTH, CELL_HEIGHT);
2309762338dSopenharmony_ci    uint32_t count = 0;
2319762338dSopenharmony_ci    for (uint32_t x = 0; x < ROW_NUM; x++) {
2329762338dSopenharmony_ci        for (uint32_t y = 0; y < COL_NUM; y++) {
2339762338dSopenharmony_ci            rect.x = x * CELL_WIDTH;
2349762338dSopenharmony_ci            rect.y = y * CELL_HEIGHT;
2359762338dSopenharmony_ci            ClearColorRect(handle, colors[count++], rect);
2369762338dSopenharmony_ci            splitRects.push_back(rect);
2379762338dSopenharmony_ci        }
2389762338dSopenharmony_ci    }
2399762338dSopenharmony_ci    SaveFile("/data/splitbuffer_data_", static_cast<uint8_t *>(handle.virAddr), handle.size);
2409762338dSopenharmony_ci    return splitRects;
2419762338dSopenharmony_ci}
2429762338dSopenharmony_ci} // OHOS
2439762338dSopenharmony_ci} // HDI
2449762338dSopenharmony_ci} // Display
2459762338dSopenharmony_ci} // TEST
246