1 /*
2 * Copyright 2021 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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <tonemap/tonemap.h>
20 #include <cmath>
21
22 namespace android {
23
24 using testing::HasSubstr;
25
26 struct TonemapTest : public ::testing::Test {};
27
TEST_F(TonemapTest,generateShaderSkSLUniforms_containsDefaultUniforms)28 TEST_F(TonemapTest, generateShaderSkSLUniforms_containsDefaultUniforms) {
29 static const constexpr float kDisplayMaxLuminance = 1.f;
30 static const constexpr float kContentMaxLuminance = 2.f;
31 tonemap::Metadata metadata{.displayMaxLuminance = kDisplayMaxLuminance,
32 .contentMaxLuminance = kContentMaxLuminance};
33 const auto uniforms = tonemap::getToneMapper()->generateShaderSkSLUniforms(metadata);
34
35 ASSERT_EQ(1, std::count_if(uniforms.cbegin(), uniforms.cend(), [](const auto& data) {
36 return data.name == "in_libtonemap_displayMaxLuminance";
37 }));
38 ASSERT_EQ(1, std::count_if(uniforms.cbegin(), uniforms.cend(), [](const auto& data) {
39 return data.name == "in_libtonemap_inputMaxLuminance";
40 }));
41
42 // Smoke check that metadata values are "real", specifically that they're non-zero and actually
43 // numbers. This is to help avoid shaders using these uniforms from dividing by zero or other
44 // catastrophic errors.
45 const auto& displayLum = std::find_if(uniforms.cbegin(), uniforms.cend(), [](const auto& data) {
46 return data.name == "in_libtonemap_displayMaxLuminance";
47 })->value;
48
49 float displayLumFloat = 0.f;
50 std::memcpy(&displayLumFloat, displayLum.data(), displayLum.size());
51 EXPECT_FALSE(std::isnan(displayLumFloat));
52 EXPECT_GT(displayLumFloat, 0);
53
54 const auto& contentLum = std::find_if(uniforms.cbegin(), uniforms.cend(), [](const auto& data) {
55 return data.name == "in_libtonemap_inputMaxLuminance";
56 })->value;
57
58 float contentLumFloat = 0.f;
59 std::memcpy(&contentLumFloat, contentLum.data(), contentLum.size());
60 EXPECT_FALSE(std::isnan(contentLumFloat));
61 EXPECT_GT(contentLumFloat, 0);
62 }
63
TEST_F(TonemapTest,generateTonemapGainShaderSkSL_containsEntryPointForPQ)64 TEST_F(TonemapTest, generateTonemapGainShaderSkSL_containsEntryPointForPQ) {
65 const auto shader =
66 tonemap::getToneMapper()
67 ->generateTonemapGainShaderSkSL(aidl::android::hardware::graphics::common::
68 Dataspace::BT2020_ITU_PQ,
69 aidl::android::hardware::graphics::common::
70 Dataspace::DISPLAY_P3);
71
72 // Other tests such as librenderengine_test will plug in the shader to check compilation.
73 EXPECT_THAT(shader, HasSubstr("float libtonemap_LookupTonemapGain(vec3 linearRGB, vec3 xyz)"));
74 }
75
TEST_F(TonemapTest,generateTonemapGainShaderSkSL_containsEntryPointForHLG)76 TEST_F(TonemapTest, generateTonemapGainShaderSkSL_containsEntryPointForHLG) {
77 const auto shader =
78 tonemap::getToneMapper()
79 ->generateTonemapGainShaderSkSL(aidl::android::hardware::graphics::common::
80 Dataspace::BT2020_ITU_HLG,
81 aidl::android::hardware::graphics::common::
82 Dataspace::DISPLAY_P3);
83
84 // Other tests such as librenderengine_test will plug in the shader to check compilation.
85 EXPECT_THAT(shader, HasSubstr("float libtonemap_LookupTonemapGain(vec3 linearRGB, vec3 xyz)"));
86 }
87
88 } // namespace android
89