Alec Mouri | 6ba7f2b | 2022-06-02 22:55:05 +0000 | [diff] [blame^] | 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 | template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, bool> = true> |
| 39 | std::vector<uint8_t> buildUniformValue(T value) { |
| 40 | std::vector<uint8_t> result; |
| 41 | result.resize(sizeof(value)); |
| 42 | std::memcpy(result.data(), &value, sizeof(value)); |
| 43 | return result; |
| 44 | } |
| 45 | |
| 46 | } // namespace |
| 47 | |
| 48 | TEST_F(ShadersTest, buildLinearEffectUniforms_selectsNoOpGamutMatrices) { |
| 49 | shaders::LinearEffect effect = |
| 50 | shaders::LinearEffect{.inputDataspace = ui::Dataspace::V0_SRGB_LINEAR, |
| 51 | .outputDataspace = ui::Dataspace::V0_SRGB_LINEAR, |
| 52 | .fakeInputDataspace = ui::Dataspace::UNKNOWN}; |
| 53 | |
| 54 | mat4 colorTransform = mat4::scale(vec4(.9, .9, .9, 1.)); |
| 55 | auto uniforms = |
| 56 | shaders::buildLinearEffectUniforms(effect, colorTransform, 1.f, 1.f, 1.f, nullptr, |
| 57 | aidl::android::hardware::graphics::composer3:: |
| 58 | RenderIntent::COLORIMETRIC); |
| 59 | EXPECT_THAT(uniforms, Contains(UniformEq("in_rgbToXyz", buildUniformValue<mat4>(mat4())))); |
| 60 | EXPECT_THAT(uniforms, |
| 61 | Contains(UniformEq("in_xyzToRgb", buildUniformValue<mat4>(colorTransform)))); |
| 62 | } |
| 63 | |
| 64 | TEST_F(ShadersTest, buildLinearEffectUniforms_selectsGamutTransformMatrices) { |
| 65 | shaders::LinearEffect effect = |
| 66 | shaders::LinearEffect{.inputDataspace = ui::Dataspace::V0_SRGB, |
| 67 | .outputDataspace = ui::Dataspace::DISPLAY_P3, |
| 68 | .fakeInputDataspace = ui::Dataspace::UNKNOWN}; |
| 69 | |
| 70 | ColorSpace inputColorSpace = ColorSpace::sRGB(); |
| 71 | ColorSpace outputColorSpace = ColorSpace::DisplayP3(); |
| 72 | auto uniforms = |
| 73 | shaders::buildLinearEffectUniforms(effect, mat4(), 1.f, 1.f, 1.f, nullptr, |
| 74 | aidl::android::hardware::graphics::composer3:: |
| 75 | RenderIntent::COLORIMETRIC); |
| 76 | EXPECT_THAT(uniforms, |
| 77 | Contains(UniformEq("in_rgbToXyz", |
| 78 | buildUniformValue<mat4>(mat4(inputColorSpace.getRGBtoXYZ()))))); |
| 79 | EXPECT_THAT(uniforms, |
| 80 | Contains(UniformEq("in_xyzToRgb", |
| 81 | buildUniformValue<mat4>(mat4(outputColorSpace.getXYZtoRGB()))))); |
| 82 | } |
| 83 | |
| 84 | TEST_F(ShadersTest, buildLinearEffectUniforms_respectsFakeInputDataspace) { |
| 85 | shaders::LinearEffect effect = |
| 86 | shaders::LinearEffect{.inputDataspace = ui::Dataspace::V0_SRGB, |
| 87 | .outputDataspace = ui::Dataspace::DISPLAY_P3, |
| 88 | .fakeInputDataspace = ui::Dataspace::DISPLAY_P3}; |
| 89 | |
| 90 | auto uniforms = |
| 91 | shaders::buildLinearEffectUniforms(effect, mat4(), 1.f, 1.f, 1.f, nullptr, |
| 92 | aidl::android::hardware::graphics::composer3:: |
| 93 | RenderIntent::COLORIMETRIC); |
| 94 | EXPECT_THAT(uniforms, Contains(UniformEq("in_rgbToXyz", buildUniformValue<mat4>(mat4())))); |
| 95 | EXPECT_THAT(uniforms, Contains(UniformEq("in_xyzToRgb", buildUniformValue<mat4>(mat4())))); |
| 96 | } |
| 97 | |
| 98 | } // namespace android |