blob: 77a8f9c79b8f2929fbd9f51c5d5800ab42205a20 [file] [log] [blame]
Sally Qi147581b2023-06-27 11:55:34 -07001/**
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
24using ::std::literals::chrono_literals::operator""s;
25
26static constexpr int kHdrSdrRatioOverlayCode = 1043;
27static constexpr int kHdrSdrRatioOverlayEnable = 1;
28static constexpr int kHdrSdrRatioOverlayDisable = 0;
29static constexpr int kHdrSdrRatioOverlayQuery = 2;
30
31// These values must match the ones we used for developer options in
32// com.android.settings.development.ShowHdrSdrRatioPreferenceController
33static_assert(kHdrSdrRatioOverlayCode == 1043);
34static_assert(kHdrSdrRatioOverlayEnable == 1);
35static_assert(kHdrSdrRatioOverlayDisable == 0);
36static_assert(kHdrSdrRatioOverlayQuery == 2);
37
38namespace android {
39
40namespace {
41void 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
50bool isOverlayEnabled() {
51 Parcel reply;
52 sendCommandToSf(kHdrSdrRatioOverlayQuery, reply);
53 return reply.readBool();
54}
55
56void 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
67void 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
81TEST(HdrSdrRatioOverlayTest, enableAndDisableOverlay) {
82 toggleOverlay(true);
83 toggleOverlay(false);
84
85 toggleOverlay(true);
86 toggleOverlay(false);
87}
88
89} // namespace android