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 | |
Lloyd Pique | bc79209 | 2018-01-17 11:52:30 -0800 | [diff] [blame^] | 47 | using HWC2Display = TestableSurfaceFlinger::HWC2Display; |
| 48 | |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 49 | constexpr int32_t DEFAULT_REFRESH_RATE = 1666666666; |
| 50 | constexpr int32_t DEFAULT_DPI = 320; |
| 51 | |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 52 | class DisplayTransactionTest : public testing::Test { |
| 53 | protected: |
| 54 | DisplayTransactionTest(); |
| 55 | ~DisplayTransactionTest() override; |
| 56 | |
| 57 | void setupComposer(int virtualDisplayCount); |
| 58 | void setupPrimaryDisplay(int width, int height); |
| 59 | |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 60 | void expectFramebufferQueuePairCreation(int width, int height); |
| 61 | |
Lloyd Pique | bc79209 | 2018-01-17 11:52:30 -0800 | [diff] [blame^] | 62 | std::unordered_set<HWC2::Capability> mCapabilities; |
| 63 | |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 64 | TestableSurfaceFlinger mFlinger; |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 65 | mock::EventThread* mEventThread = new mock::EventThread(); |
| 66 | |
| 67 | // These mocks are created by the test, but are destroyed by SurfaceFlinger |
| 68 | // by virtue of being stored into a std::unique_ptr. However we still need |
| 69 | // to keep a reference to them for use in setting up call expectations. |
| 70 | RE::mock::RenderEngine* mRenderEngine = new RE::mock::RenderEngine(); |
| 71 | Hwc2::mock::Composer* mComposer = new Hwc2::mock::Composer(); |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 72 | |
| 73 | // These mocks are created only when expected to be created via a factory. |
| 74 | sp<mock::GraphicBufferConsumer> mConsumer; |
| 75 | sp<mock::GraphicBufferProducer> mProducer; |
Lloyd Pique | bc79209 | 2018-01-17 11:52:30 -0800 | [diff] [blame^] | 76 | std::unique_ptr<HWC2Display> mDisplay; |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | DisplayTransactionTest::DisplayTransactionTest() { |
| 80 | const ::testing::TestInfo* const test_info = |
| 81 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 82 | 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] | 83 | |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 84 | mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) { |
| 85 | ADD_FAILURE() << "Unexpected request to create a buffer queue."; |
| 86 | }); |
| 87 | |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 88 | mFlinger.mutableEventThread().reset(mEventThread); |
| 89 | mFlinger.setupRenderEngine(std::unique_ptr<RE::RenderEngine>(mRenderEngine)); |
| 90 | |
| 91 | setupComposer(0); |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | DisplayTransactionTest::~DisplayTransactionTest() { |
| 95 | const ::testing::TestInfo* const test_info = |
| 96 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 97 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 98 | } |
| 99 | |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 100 | void DisplayTransactionTest::setupComposer(int virtualDisplayCount) { |
| 101 | EXPECT_CALL(*mComposer, getCapabilities()) |
| 102 | .WillOnce(Return(std::vector<IComposer::Capability>())); |
| 103 | EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount)); |
| 104 | mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer)); |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 105 | |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 106 | Mock::VerifyAndClear(mComposer); |
| 107 | } |
| 108 | |
| 109 | void DisplayTransactionTest::setupPrimaryDisplay(int width, int height) { |
Lloyd Pique | bc79209 | 2018-01-17 11:52:30 -0800 | [diff] [blame^] | 110 | mDisplay = std::make_unique<HWC2Display>(*mComposer, mCapabilities, 0, |
| 111 | HWC2::DisplayType::Physical); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 112 | |
Lloyd Pique | bc79209 | 2018-01-17 11:52:30 -0800 | [diff] [blame^] | 113 | mDisplay->mutableIsConnected() = true; |
| 114 | mDisplay->mutableConfigs().emplace(0, |
| 115 | HWC2::Display::Config::Builder(*mDisplay, 0) |
| 116 | .setWidth(width) |
| 117 | .setHeight(height) |
| 118 | .setVsyncPeriod(DEFAULT_REFRESH_RATE) |
| 119 | .setDpiX(DEFAULT_DPI) |
| 120 | .setDpiY(DEFAULT_DPI) |
| 121 | .build()); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 122 | |
Lloyd Pique | bc79209 | 2018-01-17 11:52:30 -0800 | [diff] [blame^] | 123 | mFlinger.mutableHwcDisplayData()[DisplayDevice::DISPLAY_PRIMARY].reset(); |
| 124 | mFlinger.mutableHwcDisplayData()[DisplayDevice::DISPLAY_PRIMARY].hwcDisplay = mDisplay.get(); |
| 125 | mFlinger.mutableHwcDisplaySlots().emplace(0, DisplayDevice::DISPLAY_PRIMARY); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 128 | void DisplayTransactionTest::expectFramebufferQueuePairCreation(int width, int height) { |
| 129 | mConsumer = new mock::GraphicBufferConsumer(); |
| 130 | mProducer = new mock::GraphicBufferProducer(); |
| 131 | |
| 132 | mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) { |
| 133 | *outProducer = mProducer; |
| 134 | *outConsumer = mConsumer; |
| 135 | }); |
| 136 | |
| 137 | EXPECT_CALL(*mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR)); |
| 138 | EXPECT_CALL(*mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR)); |
| 139 | EXPECT_CALL(*mConsumer, |
| 140 | setConsumerUsageBits(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | |
| 141 | GRALLOC_USAGE_HW_FB)) |
| 142 | .WillRepeatedly(Return(NO_ERROR)); |
| 143 | EXPECT_CALL(*mConsumer, setDefaultBufferSize(width, height)).WillRepeatedly(Return(NO_ERROR)); |
| 144 | EXPECT_CALL(*mConsumer, setMaxAcquiredBufferCount(_)).WillRepeatedly(Return(NO_ERROR)); |
| 145 | |
| 146 | EXPECT_CALL(*mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return()); |
| 147 | } |
| 148 | |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 149 | TEST_F(DisplayTransactionTest, processDisplayChangesLockedProcessesPrimaryDisplayConnected) { |
| 150 | using android::hardware::graphics::common::V1_0::ColorMode; |
| 151 | |
| 152 | setupPrimaryDisplay(1920, 1080); |
| 153 | |
| 154 | sp<BBinder> token = new BBinder(); |
| 155 | mFlinger.mutableCurrentState().displays.add(token, {DisplayDevice::DISPLAY_PRIMARY, true}); |
| 156 | |
| 157 | EXPECT_CALL(*mComposer, getActiveConfig(DisplayDevice::DISPLAY_PRIMARY, _)) |
| 158 | .WillOnce(DoAll(SetArgPointee<1>(0), Return(Error::NONE))); |
Lloyd Pique | 926a5fc | 2018-03-01 13:52:37 -0800 | [diff] [blame] | 159 | EXPECT_CALL(*mComposer, getColorModes(DisplayDevice::DISPLAY_PRIMARY, _)).Times(0); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 160 | EXPECT_CALL(*mComposer, getHdrCapabilities(DisplayDevice::DISPLAY_PRIMARY, _, _, _, _)) |
| 161 | .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE))); |
| 162 | |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 163 | expectFramebufferQueuePairCreation(1920, 1080); |
| 164 | |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 165 | auto reSurface = new RE::mock::Surface(); |
| 166 | EXPECT_CALL(*mRenderEngine, createSurface()) |
| 167 | .WillOnce(Return(ByMove(std::unique_ptr<RE::Surface>(reSurface)))); |
| 168 | EXPECT_CALL(*reSurface, setAsync(false)).Times(1); |
| 169 | EXPECT_CALL(*reSurface, setCritical(true)).Times(1); |
| 170 | EXPECT_CALL(*reSurface, setNativeWindow(_)).Times(1); |
| 171 | EXPECT_CALL(*reSurface, queryWidth()).WillOnce(Return(1920)); |
| 172 | EXPECT_CALL(*reSurface, queryHeight()).WillOnce(Return(1080)); |
| 173 | |
| 174 | EXPECT_CALL(*mEventThread, onHotplugReceived(DisplayDevice::DISPLAY_PRIMARY, true)).Times(1); |
| 175 | |
| 176 | mFlinger.processDisplayChangesLocked(); |
| 177 | |
| 178 | ASSERT_TRUE(mFlinger.mutableDisplays().indexOfKey(token) >= 0); |
| 179 | |
| 180 | const auto& device = mFlinger.mutableDisplays().valueFor(token); |
| 181 | ASSERT_TRUE(device.get()); |
| 182 | EXPECT_TRUE(device->isSecure()); |
| 183 | EXPECT_TRUE(device->isPrimary()); |
| 184 | |
| 185 | ssize_t i = mFlinger.mutableDrawingState().displays.indexOfKey(token); |
| 186 | ASSERT_GE(0, i); |
| 187 | const auto& draw = mFlinger.mutableDrawingState().displays[i]; |
| 188 | EXPECT_EQ(DisplayDevice::DISPLAY_PRIMARY, draw.type); |
| 189 | |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 190 | EXPECT_CALL(*mConsumer, consumerDisconnect()).Times(1); |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | } // namespace |
| 194 | } // namespace android |