1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "shaders/shaders.h"
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include <math/mat4.h>
21 #include <tonemap/tonemap.h>
22 #include <ui/ColorSpace.h>
23 #include <cmath>
24 
25 namespace android {
26 
27 using testing::Contains;
28 using testing::HasSubstr;
29 
30 struct ShadersTest : public ::testing::Test {};
31 
32 namespace {
33 
34 MATCHER_P2(UniformEq, name, value, "") {
35     return arg.name == name && arg.value == value;
36 }
37 
38 MATCHER_P(UniformNameEq, name, "") {
39     return arg.name == name;
40 }
41 
42 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, bool> = true>
buildUniformValue(T value)43 std::vector<uint8_t> buildUniformValue(T value) {
44     std::vector<uint8_t> result;
45     result.resize(sizeof(value));
46     std::memcpy(result.data(), &value, sizeof(value));
47     return result;
48 }
49 
50 } // namespace
51 
TEST_F(ShadersTest,buildLinearEffectUniforms_selectsNoOpGamutMatrices)52 TEST_F(ShadersTest, buildLinearEffectUniforms_selectsNoOpGamutMatrices) {
53     shaders::LinearEffect effect =
54             shaders::LinearEffect{.inputDataspace = ui::Dataspace::V0_SRGB_LINEAR,
55                                   .outputDataspace = ui::Dataspace::V0_SRGB_LINEAR,
56                                   .fakeOutputDataspace = ui::Dataspace::UNKNOWN};
57 
58     mat4 colorTransform = mat4::scale(vec4(.9, .9, .9, 1.));
59     auto uniforms =
60             shaders::buildLinearEffectUniforms(effect, colorTransform, 1.f, 1.f, 1.f, nullptr,
61                                                aidl::android::hardware::graphics::composer3::
62                                                        RenderIntent::COLORIMETRIC);
63     EXPECT_THAT(uniforms,
64                 Contains(UniformEq("in_rgbToXyz",
65                                    buildUniformValue<mat3>(
66                                            ColorSpace::linearExtendedSRGB().getRGBtoXYZ()))));
67     EXPECT_THAT(uniforms,
68                 Contains(UniformEq("in_xyzToSrcRgb",
69                                    buildUniformValue<mat3>(
70                                            ColorSpace::linearSRGB().getXYZtoRGB()))));
71     // color transforms are already tested in renderengine's tests
72     EXPECT_THAT(uniforms, Contains(UniformNameEq("in_colorTransform")));
73 }
74 
TEST_F(ShadersTest,buildLinearEffectUniforms_selectsGamutTransformMatrices)75 TEST_F(ShadersTest, buildLinearEffectUniforms_selectsGamutTransformMatrices) {
76     shaders::LinearEffect effect =
77             shaders::LinearEffect{.inputDataspace = ui::Dataspace::V0_SRGB,
78                                   .outputDataspace = ui::Dataspace::DISPLAY_P3,
79                                   .fakeOutputDataspace = ui::Dataspace::UNKNOWN};
80 
81     ColorSpace inputColorSpace = ColorSpace::sRGB();
82     auto uniforms =
83             shaders::buildLinearEffectUniforms(effect, mat4(), 1.f, 1.f, 1.f, nullptr,
84                                                aidl::android::hardware::graphics::composer3::
85                                                        RenderIntent::COLORIMETRIC);
86     EXPECT_THAT(uniforms,
87                 Contains(UniformEq("in_rgbToXyz",
88                                    buildUniformValue<mat3>(
89                                            ColorSpace::linearExtendedSRGB().getRGBtoXYZ()))));
90     EXPECT_THAT(uniforms,
91                 Contains(UniformEq("in_xyzToSrcRgb",
92                                    buildUniformValue<mat3>(inputColorSpace.getXYZtoRGB()))));
93     EXPECT_THAT(uniforms, Contains(UniformNameEq("in_colorTransform")));
94 }
95 
96 } // namespace android
97