1 /*
2  * Copyright 2018 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 <gtest/gtest.h>
18 
19 #include <binder/ProcessState.h>
20 #include <gui/ISurfaceComposer.h>
21 #include <gui/Surface.h>
22 #include <gui/SurfaceComposerClient.h>
23 #include <inttypes.h>
24 
25 namespace android {
26 
27 using Transaction = SurfaceComposerClient::Transaction;
28 
29 static constexpr uint32_t INVALID_MASK = 0x10;
30 class DisplayedContentSamplingTest : public ::testing::Test {
31 protected:
SetUp()32     void SetUp() {
33         mComposerClient = new SurfaceComposerClient;
34         ASSERT_EQ(OK, mComposerClient->initCheck());
35         mDisplayToken = mComposerClient->getInternalDisplayToken();
36         ASSERT_TRUE(mDisplayToken);
37     }
38 
shouldSkipTest()39     bool shouldSkipTest() {
40         ui::PixelFormat format;
41         ui::Dataspace dataspace;
42         status_t status =
43                 mComposerClient->getDisplayedContentSamplingAttributes(mDisplayToken, &format,
44                                                                        &dataspace, &componentMask);
45         if (status == PERMISSION_DENIED) {
46             SUCCEED() << "permissions denial, skipping test";
47             return true;
48         }
49         if (status == INVALID_OPERATION) {
50             SUCCEED() << "optional function not supported, skipping test";
51             return true;
52         }
53         return false;
54     }
55 
56     sp<SurfaceComposerClient> mComposerClient;
57     sp<IBinder> mDisplayToken;
58     uint8_t componentMask = 0;
59 };
60 
TEST_F(DisplayedContentSamplingTest,GetDisplayedContentSamplingAttributesAreSane)61 TEST_F(DisplayedContentSamplingTest, GetDisplayedContentSamplingAttributesAreSane) {
62     // tradefed infrastructure does not support use of GTEST_SKIP
63     if (shouldSkipTest()) return;
64 
65     ui::PixelFormat format;
66     ui::Dataspace dataspace;
67     status_t status =
68             mComposerClient->getDisplayedContentSamplingAttributes(mDisplayToken, &format,
69                                                                    &dataspace, &componentMask);
70     EXPECT_EQ(OK, status);
71     EXPECT_LE(componentMask, INVALID_MASK);
72 }
73 
TEST_F(DisplayedContentSamplingTest,EnableWithInvalidMaskReturnsBadValue)74 TEST_F(DisplayedContentSamplingTest, EnableWithInvalidMaskReturnsBadValue) {
75     if (shouldSkipTest()) return;
76 
77     status_t status =
78             mComposerClient->setDisplayContentSamplingEnabled(mDisplayToken, true, INVALID_MASK, 0);
79     EXPECT_EQ(BAD_VALUE, status);
80 }
81 
TEST_F(DisplayedContentSamplingTest,EnableAndDisableSucceed)82 TEST_F(DisplayedContentSamplingTest, EnableAndDisableSucceed) {
83     if (shouldSkipTest()) return;
84 
85     status_t status = mComposerClient->setDisplayContentSamplingEnabled(mDisplayToken, true,
86                                                                         componentMask, 10);
87     EXPECT_EQ(OK, status);
88 
89     status = mComposerClient->setDisplayContentSamplingEnabled(mDisplayToken, false, componentMask,
90                                                                0);
91     EXPECT_EQ(OK, status);
92 }
93 
TEST_F(DisplayedContentSamplingTest,SelectivelyDisableComponentOk)94 TEST_F(DisplayedContentSamplingTest, SelectivelyDisableComponentOk) {
95     if (shouldSkipTest()) return;
96 
97     status_t status = mComposerClient->setDisplayContentSamplingEnabled(mDisplayToken, true,
98                                                                         componentMask, 0);
99     EXPECT_EQ(OK, status);
100 
101     // Clear the lowest bit.
102     componentMask &= (componentMask - 1);
103     status = mComposerClient->setDisplayContentSamplingEnabled(mDisplayToken, false, componentMask,
104                                                                0);
105     EXPECT_EQ(OK, status);
106 }
107 
TEST_F(DisplayedContentSamplingTest,SampleCollectionCoherentWithSupportMask)108 TEST_F(DisplayedContentSamplingTest, SampleCollectionCoherentWithSupportMask) {
109     if (shouldSkipTest()) return;
110 
111     DisplayedFrameStats stats;
112     status_t status = mComposerClient->getDisplayedContentSample(mDisplayToken, 0, 0, &stats);
113     EXPECT_EQ(OK, status);
114     if (stats.numFrames <= 0) return;
115 
116     if (componentMask & (0x1 << 0)) EXPECT_NE(0, stats.component_0_sample.size());
117     if (componentMask & (0x1 << 1)) EXPECT_NE(0, stats.component_1_sample.size());
118     if (componentMask & (0x1 << 2)) EXPECT_NE(0, stats.component_2_sample.size());
119     if (componentMask & (0x1 << 3)) EXPECT_NE(0, stats.component_3_sample.size());
120 }
121 
122 } // namespace android
123