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 "hdi_composition_check.h"
17#include "display_test.h"
18namespace OHOS {
19namespace HDI {
20namespace Display {
21namespace TEST {
22using namespace OHOS::HDI::Display::Composer::V1_1;
23static void GetCheckPoints(Point center, std::vector<Point> &points)
24{
25    const uint32_t STEP = 3;
26    points.push_back(center);
27    points.push_back({center.x + STEP, center.y});
28    points.push_back({center.x + STEP, center.y + STEP});
29    points.push_back({center.x + STEP, center.y - STEP});
30    points.push_back({center.x, center.y + STEP});
31    points.push_back({center.x - STEP, center.y});
32    points.push_back({center.x - STEP, center.y - STEP});
33    points.push_back({center.x - STEP, center.y + STEP});
34    points.push_back({center.x, center.y - STEP});
35}
36// simple hande the alpha it may not compatible with all scenarios
37static void SimpleHandleAlpha(const LayerSettings& layers, uint32_t& color)
38{
39    const float INV = 1.0f / 255.0f;
40    const uint32_t WHITE_TRANSPARENT = 0xffffff00;
41    const int32_t ALPHA = 0xff;
42    if (layers.alpha != -1) {
43        switch (layers.blendType) {
44            case BLEND_SRC:
45                color = (color & WHITE_TRANSPARENT) | (layers.alpha & ALPHA); // get the alpha
46                break;
47            case BLEND_SRCOVER:
48                color = color * (layers.alpha * INV);
49                color = (color & WHITE_TRANSPARENT) | (layers.alpha & ALPHA); // get the alpha
50                break;
51            default:
52                break;
53        }
54    }
55}
56
57static std::vector<uint32_t> GetCheckColors(const std::vector<LayerSettings> &layers, const std::vector<Point> &points)
58{
59    std::vector<uint32_t> colors;
60    for (auto point : points) {
61        uint32_t color = 0;
62        for (uint32_t i = layers.size(); i > 0; i--) {
63            auto layer = layers[i - 1];
64            const IRect& RECT = layer.displayRect;
65            // check whether the point is inside the rect
66            if ((point.x >= RECT.x) && (point.x < (RECT.x + RECT.w)) && (point.y >= RECT.y) &&
67                (point.y < (RECT.y + RECT.h))) {
68                if (layer.compositionType != Composer::V1_0::CompositionType::COMPOSITION_VIDEO) {
69                    color = layer.color;
70                    SimpleHandleAlpha(layer, color);
71                }
72                break;
73            }
74        }
75        colors.push_back(color);
76    }
77    return colors;
78}
79
80int32_t HdiCompositionCheck::Check(const std::vector<LayerSettings> &layers,
81    const BufferHandle& clientBuffer, uint32_t checkType) const
82{
83    int ret = DISPLAY_SUCCESS;
84    const int MID_POS = 2;
85    // get the all check point
86    std::vector<Point> points;
87    for (auto layer : layers) {
88        const IRect& RECT = layer.displayRect;
89        if (checkType == CHECK_VERTEX) {
90            GetCheckPoints({RECT.x, RECT.y}, points);
91            GetCheckPoints({RECT.x, RECT.y + RECT.h}, points);
92            GetCheckPoints({RECT.x + RECT.w, RECT.y}, points);
93            GetCheckPoints({RECT.x + RECT.w, RECT.y + RECT.h}, points);
94        } else {
95            GetCheckPoints({RECT.x + RECT.w / MID_POS, RECT.y + RECT.h / MID_POS}, points); // center point
96        }
97    }
98
99    // get all the check color
100    std::vector<uint32_t> colors = GetCheckColors(layers, points);
101    DISPLAY_TEST_CHK_RETURN((colors.size() != points.size()), DISPLAY_FAILURE,
102        DISPLAY_TEST_LOGE("Points and colors don't match"));
103    for (uint32_t i = 0; i < points.size(); i++) {
104        if ((points[i].x >= clientBuffer.width) || (points[i].x < 0) || (points[i].y < 0) ||
105            (points[i].y >= clientBuffer.height)) {
106            continue;
107        }
108        ret = CheckPixel(clientBuffer, points[i].x, points[i].y, colors[i]);
109        if (ret != DISPLAY_SUCCESS) {
110            DISPLAY_TEST_LOGE("check failed");
111            break;
112        }
113    }
114    return ret;
115}
116} // OHOS
117} // HDI
118} // Display
119} // TEST
120