Kevin DuBois | 9c0a176 | 2018-10-16 13:32:31 -0700 | [diff] [blame] | 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: |
| 32 | void SetUp() { |
| 33 | mComposerClient = new SurfaceComposerClient; |
| 34 | ASSERT_EQ(OK, mComposerClient->initCheck()); |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 35 | const auto ids = SurfaceComposerClient::getPhysicalDisplayIds(); |
| 36 | ASSERT_FALSE(ids.empty()); |
| 37 | // display 0 is picked for now, can extend to support all displays if needed |
| 38 | mDisplayToken = SurfaceComposerClient::getPhysicalDisplayToken(ids.front()); |
Kevin DuBois | 9c0a176 | 2018-10-16 13:32:31 -0700 | [diff] [blame] | 39 | ASSERT_TRUE(mDisplayToken); |
| 40 | } |
| 41 | |
Kevin DuBois | 74e5377 | 2018-11-19 10:52:38 -0800 | [diff] [blame] | 42 | bool shouldSkipTest() { |
| 43 | ui::PixelFormat format; |
| 44 | ui::Dataspace dataspace; |
| 45 | status_t status = |
| 46 | mComposerClient->getDisplayedContentSamplingAttributes(mDisplayToken, &format, |
| 47 | &dataspace, &componentMask); |
Kevin DuBois | 9c0a176 | 2018-10-16 13:32:31 -0700 | [diff] [blame] | 48 | if (status == PERMISSION_DENIED) { |
| 49 | SUCCEED() << "permissions denial, skipping test"; |
| 50 | return true; |
| 51 | } |
| 52 | if (status == INVALID_OPERATION) { |
| 53 | SUCCEED() << "optional function not supported, skipping test"; |
| 54 | return true; |
| 55 | } |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | sp<SurfaceComposerClient> mComposerClient; |
| 60 | sp<IBinder> mDisplayToken; |
Kevin DuBois | 74e5377 | 2018-11-19 10:52:38 -0800 | [diff] [blame] | 61 | uint8_t componentMask = 0; |
Kevin DuBois | 9c0a176 | 2018-10-16 13:32:31 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | TEST_F(DisplayedContentSamplingTest, GetDisplayedContentSamplingAttributesAreSane) { |
Kevin DuBois | 74e5377 | 2018-11-19 10:52:38 -0800 | [diff] [blame] | 65 | // tradefed infrastructure does not support use of GTEST_SKIP |
| 66 | if (shouldSkipTest()) return; |
| 67 | |
Kevin DuBois | 9c0a176 | 2018-10-16 13:32:31 -0700 | [diff] [blame] | 68 | ui::PixelFormat format; |
| 69 | ui::Dataspace dataspace; |
Kevin DuBois | 9c0a176 | 2018-10-16 13:32:31 -0700 | [diff] [blame] | 70 | status_t status = |
| 71 | mComposerClient->getDisplayedContentSamplingAttributes(mDisplayToken, &format, |
| 72 | &dataspace, &componentMask); |
Kevin DuBois | 9c0a176 | 2018-10-16 13:32:31 -0700 | [diff] [blame] | 73 | EXPECT_EQ(OK, status); |
| 74 | EXPECT_LE(componentMask, INVALID_MASK); |
| 75 | } |
Kevin DuBois | 74e5377 | 2018-11-19 10:52:38 -0800 | [diff] [blame] | 76 | |
| 77 | TEST_F(DisplayedContentSamplingTest, EnableWithInvalidMaskReturnsBadValue) { |
| 78 | if (shouldSkipTest()) return; |
| 79 | |
| 80 | status_t status = |
| 81 | mComposerClient->setDisplayContentSamplingEnabled(mDisplayToken, true, INVALID_MASK, 0); |
| 82 | EXPECT_EQ(BAD_VALUE, status); |
| 83 | } |
| 84 | |
| 85 | TEST_F(DisplayedContentSamplingTest, EnableAndDisableSucceed) { |
| 86 | if (shouldSkipTest()) return; |
| 87 | |
| 88 | status_t status = mComposerClient->setDisplayContentSamplingEnabled(mDisplayToken, true, |
| 89 | componentMask, 10); |
| 90 | EXPECT_EQ(OK, status); |
| 91 | |
| 92 | status = mComposerClient->setDisplayContentSamplingEnabled(mDisplayToken, false, componentMask, |
| 93 | 0); |
| 94 | EXPECT_EQ(OK, status); |
| 95 | } |
| 96 | |
| 97 | TEST_F(DisplayedContentSamplingTest, SelectivelyDisableComponentOk) { |
| 98 | if (shouldSkipTest()) return; |
| 99 | |
| 100 | status_t status = mComposerClient->setDisplayContentSamplingEnabled(mDisplayToken, true, |
| 101 | componentMask, 0); |
| 102 | EXPECT_EQ(OK, status); |
| 103 | |
| 104 | // Clear the lowest bit. |
| 105 | componentMask &= (componentMask - 1); |
| 106 | status = mComposerClient->setDisplayContentSamplingEnabled(mDisplayToken, false, componentMask, |
| 107 | 0); |
| 108 | EXPECT_EQ(OK, status); |
| 109 | } |
Kevin DuBois | 1d4249a | 2018-08-29 10:45:14 -0700 | [diff] [blame] | 110 | |
| 111 | TEST_F(DisplayedContentSamplingTest, SampleCollectionCoherentWithSupportMask) { |
| 112 | if (shouldSkipTest()) return; |
| 113 | |
| 114 | DisplayedFrameStats stats; |
| 115 | status_t status = mComposerClient->getDisplayedContentSample(mDisplayToken, 0, 0, &stats); |
| 116 | EXPECT_EQ(OK, status); |
| 117 | if (stats.numFrames <= 0) return; |
| 118 | |
| 119 | if (componentMask & (0x1 << 0)) EXPECT_NE(0, stats.component_0_sample.size()); |
| 120 | if (componentMask & (0x1 << 1)) EXPECT_NE(0, stats.component_1_sample.size()); |
| 121 | if (componentMask & (0x1 << 2)) EXPECT_NE(0, stats.component_2_sample.size()); |
| 122 | if (componentMask & (0x1 << 3)) EXPECT_NE(0, stats.component_3_sample.size()); |
| 123 | } |
| 124 | |
Kevin DuBois | 9c0a176 | 2018-10-16 13:32:31 -0700 | [diff] [blame] | 125 | } // namespace android |