1 /**
2 * Copyright (C) 2023 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 #include <thread>
17
18 #include <gtest/gtest.h>
19
20 #include <gui/SurfaceComposerClient.h>
21 #include <private/gui/ComposerService.h>
22 #include <chrono>
23
24 using ::std::literals::chrono_literals::operator""s;
25
26 static constexpr int kHdrSdrRatioOverlayCode = 1043;
27 static constexpr int kHdrSdrRatioOverlayEnable = 1;
28 static constexpr int kHdrSdrRatioOverlayDisable = 0;
29 static constexpr int kHdrSdrRatioOverlayQuery = 2;
30
31 // These values must match the ones we used for developer options in
32 // com.android.settings.development.ShowHdrSdrRatioPreferenceController
33 static_assert(kHdrSdrRatioOverlayCode == 1043);
34 static_assert(kHdrSdrRatioOverlayEnable == 1);
35 static_assert(kHdrSdrRatioOverlayDisable == 0);
36 static_assert(kHdrSdrRatioOverlayQuery == 2);
37
38 namespace android {
39
40 namespace {
sendCommandToSf(int command,Parcel & reply)41 void sendCommandToSf(int command, Parcel& reply) {
42 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
43 Parcel request;
44 request.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
45 request.writeInt32(command);
46 ASSERT_EQ(NO_ERROR,
47 IInterface::asBinder(sf)->transact(kHdrSdrRatioOverlayCode, request, &reply));
48 }
49
isOverlayEnabled()50 bool isOverlayEnabled() {
51 Parcel reply;
52 sendCommandToSf(kHdrSdrRatioOverlayQuery, reply);
53 return reply.readBool();
54 }
55
waitForOverlay(bool enabled)56 void waitForOverlay(bool enabled) {
57 static constexpr auto kTimeout = std::chrono::nanoseconds(1s);
58 static constexpr auto kIterations = 10;
59 for (int i = 0; i < kIterations; i++) {
60 if (enabled == isOverlayEnabled()) {
61 return;
62 }
63 std::this_thread::sleep_for(kTimeout / kIterations);
64 }
65 }
66
toggleOverlay(bool enabled)67 void toggleOverlay(bool enabled) {
68 if (enabled == isOverlayEnabled()) {
69 return;
70 }
71
72 Parcel reply;
73 const auto command = enabled ? kHdrSdrRatioOverlayEnable : kHdrSdrRatioOverlayDisable;
74 sendCommandToSf(command, reply);
75 waitForOverlay(enabled);
76 ASSERT_EQ(enabled, isOverlayEnabled());
77 }
78
79 } // namespace
80
TEST(HdrSdrRatioOverlayTest,enableAndDisableOverlay)81 TEST(HdrSdrRatioOverlayTest, enableAndDisableOverlay) {
82 toggleOverlay(true);
83 toggleOverlay(false);
84
85 toggleOverlay(true);
86 toggleOverlay(false);
87 }
88
89 } // namespace android
90