| Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 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 | #undef LOG_TAG | 
|  | 18 | #define LOG_TAG "LibSurfaceFlingerUnittests" | 
|  | 19 |  | 
|  | 20 | #include <gmock/gmock.h> | 
|  | 21 | #include <gtest/gtest.h> | 
|  | 22 |  | 
|  | 23 | #include <log/log.h> | 
|  | 24 |  | 
| Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 25 | #include "MockComposer.h" | 
|  | 26 | #include "MockEventThread.h" | 
| Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 27 | #include "MockGraphicBufferConsumer.h" | 
|  | 28 | #include "MockGraphicBufferProducer.h" | 
| Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 29 | #include "MockRenderEngine.h" | 
| Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 30 | #include "TestableSurfaceFlinger.h" | 
|  | 31 |  | 
|  | 32 | namespace android { | 
|  | 33 | namespace { | 
|  | 34 |  | 
| Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 35 | using testing::_; | 
|  | 36 | using testing::ByMove; | 
|  | 37 | using testing::DoAll; | 
|  | 38 | using testing::Mock; | 
|  | 39 | using testing::Return; | 
|  | 40 | using testing::SetArgPointee; | 
|  | 41 |  | 
|  | 42 | using android::hardware::graphics::common::V1_0::Hdr; | 
|  | 43 | using android::Hwc2::Error; | 
|  | 44 | using android::Hwc2::IComposer; | 
|  | 45 | using android::Hwc2::IComposerClient; | 
|  | 46 |  | 
|  | 47 | constexpr int32_t DEFAULT_REFRESH_RATE = 1666666666; | 
|  | 48 | constexpr int32_t DEFAULT_DPI = 320; | 
|  | 49 |  | 
| Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 50 | class DisplayTransactionTest : public testing::Test { | 
|  | 51 | protected: | 
|  | 52 | DisplayTransactionTest(); | 
|  | 53 | ~DisplayTransactionTest() override; | 
|  | 54 |  | 
|  | 55 | void setupComposer(int virtualDisplayCount); | 
|  | 56 | void setupPrimaryDisplay(int width, int height); | 
|  | 57 |  | 
| Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 58 | void expectFramebufferQueuePairCreation(int width, int height); | 
|  | 59 |  | 
| Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 60 | TestableSurfaceFlinger mFlinger; | 
| Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 61 | mock::EventThread* mEventThread = new mock::EventThread(); | 
|  | 62 |  | 
|  | 63 | // These mocks are created by the test, but are destroyed by SurfaceFlinger | 
|  | 64 | // by virtue of being stored into a std::unique_ptr. However we still need | 
|  | 65 | // to keep a reference to them for use in setting up call expectations. | 
|  | 66 | RE::mock::RenderEngine* mRenderEngine = new RE::mock::RenderEngine(); | 
|  | 67 | Hwc2::mock::Composer* mComposer = new Hwc2::mock::Composer(); | 
| Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 68 |  | 
|  | 69 | // These mocks are created only when expected to be created via a factory. | 
|  | 70 | sp<mock::GraphicBufferConsumer> mConsumer; | 
|  | 71 | sp<mock::GraphicBufferProducer> mProducer; | 
| Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 72 | }; | 
|  | 73 |  | 
|  | 74 | DisplayTransactionTest::DisplayTransactionTest() { | 
|  | 75 | const ::testing::TestInfo* const test_info = | 
|  | 76 | ::testing::UnitTest::GetInstance()->current_test_info(); | 
|  | 77 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); | 
| Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 78 |  | 
| Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 79 | mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) { | 
|  | 80 | ADD_FAILURE() << "Unexpected request to create a buffer queue."; | 
|  | 81 | }); | 
|  | 82 |  | 
| Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 83 | mFlinger.mutableEventThread().reset(mEventThread); | 
|  | 84 | mFlinger.setupRenderEngine(std::unique_ptr<RE::RenderEngine>(mRenderEngine)); | 
|  | 85 |  | 
|  | 86 | setupComposer(0); | 
| Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
|  | 89 | DisplayTransactionTest::~DisplayTransactionTest() { | 
|  | 90 | const ::testing::TestInfo* const test_info = | 
|  | 91 | ::testing::UnitTest::GetInstance()->current_test_info(); | 
|  | 92 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); | 
|  | 93 | } | 
|  | 94 |  | 
| Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 95 | void DisplayTransactionTest::setupComposer(int virtualDisplayCount) { | 
|  | 96 | EXPECT_CALL(*mComposer, getCapabilities()) | 
|  | 97 | .WillOnce(Return(std::vector<IComposer::Capability>())); | 
|  | 98 | EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount)); | 
|  | 99 | mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer)); | 
| Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 100 |  | 
| Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 101 | Mock::VerifyAndClear(mComposer); | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | void DisplayTransactionTest::setupPrimaryDisplay(int width, int height) { | 
|  | 105 | EXPECT_CALL(*mComposer, getDisplayType(DisplayDevice::DISPLAY_PRIMARY, _)) | 
|  | 106 | .WillOnce(DoAll(SetArgPointee<1>(IComposerClient::DisplayType::PHYSICAL), | 
|  | 107 | Return(Error::NONE))); | 
|  | 108 | EXPECT_CALL(*mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE)); | 
|  | 109 | EXPECT_CALL(*mComposer, getDisplayConfigs(_, _)) | 
|  | 110 | .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{0}), Return(Error::NONE))); | 
|  | 111 | EXPECT_CALL(*mComposer, | 
|  | 112 | getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0, | 
|  | 113 | IComposerClient::Attribute::WIDTH, _)) | 
|  | 114 | .WillOnce(DoAll(SetArgPointee<3>(width), Return(Error::NONE))); | 
|  | 115 | EXPECT_CALL(*mComposer, | 
|  | 116 | getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0, | 
|  | 117 | IComposerClient::Attribute::HEIGHT, _)) | 
|  | 118 | .WillOnce(DoAll(SetArgPointee<3>(height), Return(Error::NONE))); | 
|  | 119 | EXPECT_CALL(*mComposer, | 
|  | 120 | getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0, | 
|  | 121 | IComposerClient::Attribute::VSYNC_PERIOD, _)) | 
|  | 122 | .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE))); | 
|  | 123 | EXPECT_CALL(*mComposer, | 
|  | 124 | getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0, | 
|  | 125 | IComposerClient::Attribute::DPI_X, _)) | 
|  | 126 | .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE))); | 
|  | 127 | EXPECT_CALL(*mComposer, | 
|  | 128 | getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0, | 
|  | 129 | IComposerClient::Attribute::DPI_Y, _)) | 
|  | 130 | .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE))); | 
|  | 131 |  | 
|  | 132 | mFlinger.setupPrimaryDisplay(); | 
|  | 133 |  | 
|  | 134 | Mock::VerifyAndClear(mComposer); | 
|  | 135 | } | 
|  | 136 |  | 
| Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 137 | void DisplayTransactionTest::expectFramebufferQueuePairCreation(int width, int height) { | 
|  | 138 | mConsumer = new mock::GraphicBufferConsumer(); | 
|  | 139 | mProducer = new mock::GraphicBufferProducer(); | 
|  | 140 |  | 
|  | 141 | mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) { | 
|  | 142 | *outProducer = mProducer; | 
|  | 143 | *outConsumer = mConsumer; | 
|  | 144 | }); | 
|  | 145 |  | 
|  | 146 | EXPECT_CALL(*mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR)); | 
|  | 147 | EXPECT_CALL(*mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR)); | 
|  | 148 | EXPECT_CALL(*mConsumer, | 
|  | 149 | setConsumerUsageBits(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | | 
|  | 150 | GRALLOC_USAGE_HW_FB)) | 
|  | 151 | .WillRepeatedly(Return(NO_ERROR)); | 
|  | 152 | EXPECT_CALL(*mConsumer, setDefaultBufferSize(width, height)).WillRepeatedly(Return(NO_ERROR)); | 
|  | 153 | EXPECT_CALL(*mConsumer, setMaxAcquiredBufferCount(_)).WillRepeatedly(Return(NO_ERROR)); | 
|  | 154 |  | 
|  | 155 | EXPECT_CALL(*mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return()); | 
|  | 156 | } | 
|  | 157 |  | 
| Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 158 | TEST_F(DisplayTransactionTest, processDisplayChangesLockedProcessesPrimaryDisplayConnected) { | 
|  | 159 | using android::hardware::graphics::common::V1_0::ColorMode; | 
|  | 160 |  | 
|  | 161 | setupPrimaryDisplay(1920, 1080); | 
|  | 162 |  | 
|  | 163 | sp<BBinder> token = new BBinder(); | 
|  | 164 | mFlinger.mutableCurrentState().displays.add(token, {DisplayDevice::DISPLAY_PRIMARY, true}); | 
|  | 165 |  | 
|  | 166 | EXPECT_CALL(*mComposer, getActiveConfig(DisplayDevice::DISPLAY_PRIMARY, _)) | 
|  | 167 | .WillOnce(DoAll(SetArgPointee<1>(0), Return(Error::NONE))); | 
|  | 168 | EXPECT_CALL(*mComposer, getColorModes(DisplayDevice::DISPLAY_PRIMARY, _)) | 
|  | 169 | .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::NATIVE})), | 
|  | 170 | Return(Error::NONE))); | 
| Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 171 | EXPECT_CALL(*mComposer, getHdrCapabilities(DisplayDevice::DISPLAY_PRIMARY, _, _, _, _)) | 
|  | 172 | .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE))); | 
|  | 173 |  | 
| Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 174 | expectFramebufferQueuePairCreation(1920, 1080); | 
|  | 175 |  | 
| Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 176 | auto reSurface = new RE::mock::Surface(); | 
|  | 177 | EXPECT_CALL(*mRenderEngine, createSurface()) | 
|  | 178 | .WillOnce(Return(ByMove(std::unique_ptr<RE::Surface>(reSurface)))); | 
|  | 179 | EXPECT_CALL(*reSurface, setAsync(false)).Times(1); | 
|  | 180 | EXPECT_CALL(*reSurface, setCritical(true)).Times(1); | 
|  | 181 | EXPECT_CALL(*reSurface, setNativeWindow(_)).Times(1); | 
|  | 182 | EXPECT_CALL(*reSurface, queryWidth()).WillOnce(Return(1920)); | 
|  | 183 | EXPECT_CALL(*reSurface, queryHeight()).WillOnce(Return(1080)); | 
|  | 184 |  | 
|  | 185 | EXPECT_CALL(*mEventThread, onHotplugReceived(DisplayDevice::DISPLAY_PRIMARY, true)).Times(1); | 
|  | 186 |  | 
|  | 187 | mFlinger.processDisplayChangesLocked(); | 
|  | 188 |  | 
|  | 189 | ASSERT_TRUE(mFlinger.mutableDisplays().indexOfKey(token) >= 0); | 
|  | 190 |  | 
|  | 191 | const auto& device = mFlinger.mutableDisplays().valueFor(token); | 
|  | 192 | ASSERT_TRUE(device.get()); | 
|  | 193 | EXPECT_TRUE(device->isSecure()); | 
|  | 194 | EXPECT_TRUE(device->isPrimary()); | 
|  | 195 |  | 
|  | 196 | ssize_t i = mFlinger.mutableDrawingState().displays.indexOfKey(token); | 
|  | 197 | ASSERT_GE(0, i); | 
|  | 198 | const auto& draw = mFlinger.mutableDrawingState().displays[i]; | 
|  | 199 | EXPECT_EQ(DisplayDevice::DISPLAY_PRIMARY, draw.type); | 
|  | 200 |  | 
|  | 201 | EXPECT_CALL(*mComposer, setVsyncEnabled(0, IComposerClient::Vsync::DISABLE)) | 
|  | 202 | .WillOnce(Return(Error::NONE)); | 
| Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 203 |  | 
|  | 204 | EXPECT_CALL(*mConsumer, consumerDisconnect()).Times(1); | 
| Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 205 | } | 
|  | 206 |  | 
|  | 207 | } // namespace | 
|  | 208 | } // namespace android |