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> |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 24 | #include "system/window.h" |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 25 | |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 26 | #include "MockComposer.h" |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 27 | #include "MockDisplaySurface.h" |
| 28 | #include "MockEventControlThread.h" |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 29 | #include "MockEventThread.h" |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 30 | #include "MockGraphicBufferConsumer.h" |
| 31 | #include "MockGraphicBufferProducer.h" |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 32 | #include "MockMessageQueue.h" |
| 33 | #include "MockNativeWindow.h" |
| 34 | #include "MockNativeWindowSurface.h" |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 35 | #include "MockRenderEngine.h" |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 36 | #include "MockSurfaceInterceptor.h" |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 37 | #include "TestableSurfaceFlinger.h" |
| 38 | |
| 39 | namespace android { |
| 40 | namespace { |
| 41 | |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 42 | using testing::_; |
| 43 | using testing::ByMove; |
| 44 | using testing::DoAll; |
| 45 | using testing::Mock; |
| 46 | using testing::Return; |
| 47 | using testing::SetArgPointee; |
| 48 | |
| 49 | using android::hardware::graphics::common::V1_0::Hdr; |
Peiyong Lin | 0e7a791 | 2018-04-05 14:36:36 -0700 | [diff] [blame] | 50 | using android::hardware::graphics::common::V1_1::ColorMode; |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 51 | using android::Hwc2::Error; |
| 52 | using android::Hwc2::IComposer; |
| 53 | using android::Hwc2::IComposerClient; |
| 54 | |
Lloyd Pique | bc79209 | 2018-01-17 11:52:30 -0800 | [diff] [blame] | 55 | using HWC2Display = TestableSurfaceFlinger::HWC2Display; |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 56 | using HotplugEvent = TestableSurfaceFlinger::HotplugEvent; |
Lloyd Pique | bc79209 | 2018-01-17 11:52:30 -0800 | [diff] [blame] | 57 | |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 58 | constexpr int32_t DEFAULT_REFRESH_RATE = 1666666666; |
| 59 | constexpr int32_t DEFAULT_DPI = 320; |
| 60 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 61 | constexpr int DEFAULT_CONFIG_ID = 0; |
| 62 | |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 63 | class DisplayTransactionTest : public testing::Test { |
| 64 | protected: |
| 65 | DisplayTransactionTest(); |
| 66 | ~DisplayTransactionTest() override; |
| 67 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 68 | // -------------------------------------------------------------------- |
| 69 | // Precondition helpers |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 70 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 71 | void setupComposer(int virtualDisplayCount); |
| 72 | void setupFakeHwcDisplay(hwc2_display_t displayId, DisplayDevice::DisplayType type, int width, |
| 73 | int height); |
| 74 | |
| 75 | struct FakeDisplayDeviceFactory { |
| 76 | public: |
| 77 | FakeDisplayDeviceFactory(TestableSurfaceFlinger& flinger, sp<BBinder>& displayToken, |
| 78 | DisplayDevice::DisplayType type, int hwcId) |
| 79 | : mFlinger(flinger), mDisplayToken(displayToken), mType(type), mHwcId(hwcId) {} |
| 80 | |
| 81 | sp<DisplayDevice> build() { |
| 82 | return new DisplayDevice(mFlinger.mFlinger.get(), mType, mHwcId, false, mDisplayToken, |
| 83 | mNativeWindow, mDisplaySurface, std::move(mRenderSurface), 0, |
| 84 | 0, false, false, HWC_POWER_MODE_NORMAL); |
| 85 | } |
| 86 | |
| 87 | FakeDisplayDeviceFactory& setNativeWindow(const sp<ANativeWindow>& nativeWindow) { |
| 88 | mNativeWindow = nativeWindow; |
| 89 | return *this; |
| 90 | } |
| 91 | |
| 92 | FakeDisplayDeviceFactory& setDisplaySurface(const sp<DisplaySurface>& displaySurface) { |
| 93 | mDisplaySurface = displaySurface; |
| 94 | return *this; |
| 95 | } |
| 96 | |
| 97 | FakeDisplayDeviceFactory& setRenderSurface(std::unique_ptr<RE::Surface> renderSurface) { |
| 98 | mRenderSurface = std::move(renderSurface); |
| 99 | return *this; |
| 100 | } |
| 101 | |
| 102 | TestableSurfaceFlinger& mFlinger; |
| 103 | sp<BBinder>& mDisplayToken; |
| 104 | DisplayDevice::DisplayType mType; |
| 105 | int mHwcId; |
| 106 | sp<ANativeWindow> mNativeWindow; |
| 107 | sp<DisplaySurface> mDisplaySurface; |
| 108 | std::unique_ptr<RE::Surface> mRenderSurface; |
| 109 | }; |
| 110 | |
| 111 | sp<BBinder> setupFakeExistingPhysicalDisplay(hwc2_display_t displayId, |
| 112 | DisplayDevice::DisplayType type); |
| 113 | |
| 114 | void setupFakeBufferQueueFactory(); |
| 115 | void setupFakeNativeWindowSurfaceFactory(int displayWidth, int displayHeight, bool critical, |
| 116 | bool async); |
| 117 | void expectFramebufferUsageSet(int width, int height, int grallocUsage); |
| 118 | void expectHwcHotplugCalls(hwc2_display_t displayId, int displayWidth, int displayHeight); |
| 119 | |
| 120 | // -------------------------------------------------------------------- |
| 121 | // Call expectation helpers |
| 122 | |
| 123 | void expectRESurfaceCreationCalls(); |
| 124 | void expectPhysicalDisplayDeviceCreationCalls(hwc2_display_t displayId, int displayWidth, |
| 125 | int displayHeight, bool critical, bool async); |
| 126 | |
| 127 | // -------------------------------------------------------------------- |
| 128 | // Postcondition helpers |
| 129 | |
| 130 | bool hasTransactionFlagSet(int flag); |
| 131 | bool hasDisplayDevice(sp<IBinder> displayToken); |
| 132 | sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken); |
| 133 | bool hasCurrentDisplayState(sp<IBinder> displayToken); |
| 134 | const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken); |
| 135 | bool hasDrawingDisplayState(sp<IBinder> displayToken); |
| 136 | const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken); |
| 137 | |
| 138 | // -------------------------------------------------------------------- |
| 139 | // Test instances |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 140 | |
Lloyd Pique | bc79209 | 2018-01-17 11:52:30 -0800 | [diff] [blame] | 141 | std::unordered_set<HWC2::Capability> mCapabilities; |
| 142 | |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 143 | TestableSurfaceFlinger mFlinger; |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 144 | mock::EventThread* mEventThread = new mock::EventThread(); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 145 | mock::EventControlThread* mEventControlThread = new mock::EventControlThread(); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 146 | |
| 147 | // These mocks are created by the test, but are destroyed by SurfaceFlinger |
| 148 | // by virtue of being stored into a std::unique_ptr. However we still need |
| 149 | // to keep a reference to them for use in setting up call expectations. |
| 150 | RE::mock::RenderEngine* mRenderEngine = new RE::mock::RenderEngine(); |
| 151 | Hwc2::mock::Composer* mComposer = new Hwc2::mock::Composer(); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 152 | mock::MessageQueue* mMessageQueue = new mock::MessageQueue(); |
| 153 | mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor(); |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 154 | |
| 155 | // These mocks are created only when expected to be created via a factory. |
| 156 | sp<mock::GraphicBufferConsumer> mConsumer; |
| 157 | sp<mock::GraphicBufferProducer> mProducer; |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 158 | mock::NativeWindowSurface* mNativeWindowSurface = nullptr; |
| 159 | sp<mock::NativeWindow> mNativeWindow; |
| 160 | RE::mock::Surface* mRenderSurface = nullptr; |
| 161 | std::vector<std::unique_ptr<HWC2Display>> mFakeHwcDisplays; |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | DisplayTransactionTest::DisplayTransactionTest() { |
| 165 | const ::testing::TestInfo* const test_info = |
| 166 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 167 | 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] | 168 | |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 169 | mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) { |
| 170 | ADD_FAILURE() << "Unexpected request to create a buffer queue."; |
| 171 | }); |
| 172 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 173 | mFlinger.setCreateNativeWindowSurface([](auto) { |
| 174 | ADD_FAILURE() << "Unexpected request to create a native window surface."; |
| 175 | return nullptr; |
| 176 | }); |
| 177 | |
| 178 | mFlinger.mutableEventControlThread().reset(mEventControlThread); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 179 | mFlinger.mutableEventThread().reset(mEventThread); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 180 | mFlinger.mutableEventQueue().reset(mMessageQueue); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 181 | mFlinger.setupRenderEngine(std::unique_ptr<RE::RenderEngine>(mRenderEngine)); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 182 | mFlinger.mutableInterceptor().reset(mSurfaceInterceptor); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 183 | |
| 184 | setupComposer(0); |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | DisplayTransactionTest::~DisplayTransactionTest() { |
| 188 | const ::testing::TestInfo* const test_info = |
| 189 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 190 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 191 | } |
| 192 | |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 193 | void DisplayTransactionTest::setupComposer(int virtualDisplayCount) { |
| 194 | EXPECT_CALL(*mComposer, getCapabilities()) |
| 195 | .WillOnce(Return(std::vector<IComposer::Capability>())); |
| 196 | EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount)); |
| 197 | mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer)); |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 198 | |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 199 | Mock::VerifyAndClear(mComposer); |
| 200 | } |
| 201 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 202 | void DisplayTransactionTest::setupFakeHwcDisplay(hwc2_display_t displayId, |
| 203 | DisplayDevice::DisplayType type, int width, |
| 204 | int height) { |
| 205 | auto display = std::make_unique<HWC2Display>(*mComposer, mCapabilities, displayId, |
| 206 | HWC2::DisplayType::Physical); |
| 207 | display->mutableIsConnected() = true; |
| 208 | display->mutableConfigs().emplace(DEFAULT_CONFIG_ID, |
| 209 | HWC2::Display::Config::Builder(*display, DEFAULT_CONFIG_ID) |
| 210 | .setWidth(width) |
| 211 | .setHeight(height) |
| 212 | .setVsyncPeriod(DEFAULT_REFRESH_RATE) |
| 213 | .setDpiX(DEFAULT_DPI) |
| 214 | .setDpiY(DEFAULT_DPI) |
| 215 | .build()); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 216 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 217 | mFlinger.mutableHwcDisplayData()[type].reset(); |
| 218 | mFlinger.mutableHwcDisplayData()[type].hwcDisplay = display.get(); |
| 219 | mFlinger.mutableHwcDisplaySlots().emplace(displayId, type); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 220 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 221 | mFakeHwcDisplays.push_back(std::move(display)); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 224 | sp<BBinder> DisplayTransactionTest::setupFakeExistingPhysicalDisplay( |
| 225 | hwc2_display_t displayId, DisplayDevice::DisplayType type) { |
| 226 | setupFakeHwcDisplay(displayId, type, 0, 0); |
| 227 | |
| 228 | sp<BBinder> displayToken = new BBinder(); |
| 229 | mFlinger.mutableBuiltinDisplays()[type] = displayToken; |
| 230 | mFlinger.mutableDisplays() |
| 231 | .add(displayToken, |
| 232 | FakeDisplayDeviceFactory(mFlinger, displayToken, type, type).build()); |
| 233 | |
| 234 | DisplayDeviceState state(type, true); |
| 235 | mFlinger.mutableCurrentState().displays.add(displayToken, state); |
| 236 | mFlinger.mutableDrawingState().displays.add(displayToken, state); |
| 237 | |
| 238 | return displayToken; |
| 239 | } |
| 240 | |
| 241 | void DisplayTransactionTest::setupFakeBufferQueueFactory() { |
| 242 | // This setup is only expected once per test. |
| 243 | ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr); |
| 244 | |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 245 | mConsumer = new mock::GraphicBufferConsumer(); |
| 246 | mProducer = new mock::GraphicBufferProducer(); |
| 247 | |
| 248 | mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) { |
| 249 | *outProducer = mProducer; |
| 250 | *outConsumer = mConsumer; |
| 251 | }); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 252 | } |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 253 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 254 | void DisplayTransactionTest::setupFakeNativeWindowSurfaceFactory(int displayWidth, |
| 255 | int displayHeight, bool critical, |
| 256 | bool async) { |
| 257 | // This setup is only expected once per test. |
| 258 | ASSERT_TRUE(mNativeWindowSurface == nullptr); |
| 259 | |
| 260 | mNativeWindowSurface = new mock::NativeWindowSurface(); |
| 261 | mNativeWindow = new mock::NativeWindow(); |
| 262 | |
| 263 | mFlinger.setCreateNativeWindowSurface( |
| 264 | [this](auto) { return std::unique_ptr<NativeWindowSurface>(mNativeWindowSurface); }); |
| 265 | |
| 266 | EXPECT_CALL(*mNativeWindowSurface, getNativeWindow()).WillOnce(Return(mNativeWindow)); |
| 267 | |
| 268 | EXPECT_CALL(*mNativeWindow, perform(19)).Times(1); |
| 269 | |
| 270 | EXPECT_CALL(*mRenderSurface, setAsync(async)).Times(1); |
| 271 | EXPECT_CALL(*mRenderSurface, setCritical(critical)).Times(1); |
| 272 | EXPECT_CALL(*mRenderSurface, setNativeWindow(mNativeWindow.get())).Times(1); |
| 273 | EXPECT_CALL(*mRenderSurface, queryWidth()).WillOnce(Return(displayWidth)); |
| 274 | EXPECT_CALL(*mRenderSurface, queryHeight()).WillOnce(Return(displayHeight)); |
| 275 | } |
| 276 | |
| 277 | void DisplayTransactionTest::expectFramebufferUsageSet(int width, int height, int grallocUsage) { |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 278 | EXPECT_CALL(*mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR)); |
| 279 | EXPECT_CALL(*mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR)); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 280 | EXPECT_CALL(*mConsumer, setConsumerUsageBits(grallocUsage)).WillRepeatedly(Return(NO_ERROR)); |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 281 | EXPECT_CALL(*mConsumer, setDefaultBufferSize(width, height)).WillRepeatedly(Return(NO_ERROR)); |
| 282 | EXPECT_CALL(*mConsumer, setMaxAcquiredBufferCount(_)).WillRepeatedly(Return(NO_ERROR)); |
| 283 | |
| 284 | EXPECT_CALL(*mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return()); |
| 285 | } |
| 286 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 287 | void DisplayTransactionTest::expectHwcHotplugCalls(hwc2_display_t displayId, int displayWidth, |
| 288 | int displayHeight) { |
| 289 | EXPECT_CALL(*mComposer, getDisplayType(displayId, _)) |
| 290 | .WillOnce(DoAll(SetArgPointee<1>(IComposerClient::DisplayType::PHYSICAL), |
| 291 | Return(Error::NONE))); |
| 292 | EXPECT_CALL(*mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE)); |
| 293 | EXPECT_CALL(*mComposer, getDisplayConfigs(_, _)) |
| 294 | .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{0}), Return(Error::NONE))); |
| 295 | EXPECT_CALL(*mComposer, getDisplayAttribute(displayId, 0, IComposerClient::Attribute::WIDTH, _)) |
| 296 | .WillOnce(DoAll(SetArgPointee<3>(displayWidth), Return(Error::NONE))); |
| 297 | EXPECT_CALL(*mComposer, |
| 298 | getDisplayAttribute(displayId, 0, IComposerClient::Attribute::HEIGHT, _)) |
| 299 | .WillOnce(DoAll(SetArgPointee<3>(displayHeight), Return(Error::NONE))); |
| 300 | EXPECT_CALL(*mComposer, |
| 301 | getDisplayAttribute(displayId, 0, IComposerClient::Attribute::VSYNC_PERIOD, _)) |
| 302 | .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE))); |
| 303 | EXPECT_CALL(*mComposer, getDisplayAttribute(displayId, 0, IComposerClient::Attribute::DPI_X, _)) |
| 304 | .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE))); |
| 305 | EXPECT_CALL(*mComposer, getDisplayAttribute(displayId, 0, IComposerClient::Attribute::DPI_Y, _)) |
| 306 | .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE))); |
| 307 | } |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 308 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 309 | void DisplayTransactionTest::expectRESurfaceCreationCalls() { |
| 310 | // This setup is only expected once per test. |
| 311 | ASSERT_TRUE(mRenderSurface == nullptr); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 312 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 313 | mRenderSurface = new RE::mock::Surface(); |
| 314 | EXPECT_CALL(*mRenderEngine, createSurface()) |
| 315 | .WillOnce(Return(ByMove(std::unique_ptr<RE::Surface>(mRenderSurface)))); |
| 316 | } |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 317 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 318 | void DisplayTransactionTest::expectPhysicalDisplayDeviceCreationCalls(hwc2_display_t displayId, |
| 319 | int displayWidth, |
| 320 | int displayHeight, |
| 321 | bool critical, bool async) { |
| 322 | EXPECT_CALL(*mComposer, getActiveConfig(displayId, _)) |
| 323 | .WillOnce(DoAll(SetArgPointee<1>(DEFAULT_CONFIG_ID), Return(Error::NONE))); |
| 324 | EXPECT_CALL(*mComposer, getColorModes(displayId, _)).Times(0); |
| 325 | EXPECT_CALL(*mComposer, getHdrCapabilities(displayId, _, _, _, _)) |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 326 | .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE))); |
| 327 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 328 | setupFakeBufferQueueFactory(); |
| 329 | expectFramebufferUsageSet(displayWidth, displayHeight, |
| 330 | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | |
| 331 | GRALLOC_USAGE_HW_FB); |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 332 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 333 | setupFakeNativeWindowSurfaceFactory(displayWidth, displayHeight, critical, async); |
| 334 | } |
| 335 | |
| 336 | bool DisplayTransactionTest::hasTransactionFlagSet(int flag) { |
| 337 | return mFlinger.mutableTransactionFlags() & flag; |
| 338 | } |
| 339 | |
| 340 | bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) { |
| 341 | return mFlinger.mutableDisplays().indexOfKey(displayToken) >= 0; |
| 342 | } |
| 343 | |
| 344 | sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) { |
| 345 | return mFlinger.mutableDisplays().valueFor(displayToken); |
| 346 | } |
| 347 | |
| 348 | bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) { |
| 349 | return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0; |
| 350 | } |
| 351 | |
| 352 | const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) { |
| 353 | return mFlinger.mutableCurrentState().displays.valueFor(displayToken); |
| 354 | } |
| 355 | |
| 356 | bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) { |
| 357 | return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0; |
| 358 | } |
| 359 | |
| 360 | const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) { |
| 361 | return mFlinger.mutableDrawingState().displays.valueFor(displayToken); |
| 362 | } |
| 363 | |
| 364 | /* ------------------------------------------------------------------------ |
| 365 | * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded) |
| 366 | */ |
| 367 | |
| 368 | TEST_F(DisplayTransactionTest, handleTransactionLockedProcessesHotplugConnectPrimary) { |
| 369 | constexpr hwc2_display_t externalDisplayId = 102; |
| 370 | constexpr hwc2_display_t displayId = 123; |
| 371 | constexpr int displayWidth = 1920; |
| 372 | constexpr int displayHeight = 1080; |
| 373 | |
| 374 | // -------------------------------------------------------------------- |
| 375 | // Preconditions |
| 376 | |
| 377 | // An external display may already be set up |
| 378 | setupFakeHwcDisplay(externalDisplayId, DisplayDevice::DISPLAY_EXTERNAL, 3840, 2160); |
| 379 | |
| 380 | // A hotplug connect comes in for a new display |
| 381 | mFlinger.mutablePendingHotplugEvents().emplace_back( |
| 382 | HotplugEvent{displayId, HWC2::Connection::Connected}); |
| 383 | |
| 384 | // -------------------------------------------------------------------- |
| 385 | // Call Expectations |
| 386 | |
| 387 | EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false)); |
| 388 | expectHwcHotplugCalls(displayId, displayWidth, displayHeight); |
| 389 | expectRESurfaceCreationCalls(); |
| 390 | expectPhysicalDisplayDeviceCreationCalls(displayId, displayWidth, displayHeight, true, false); |
| 391 | |
| 392 | EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 393 | |
| 394 | EXPECT_CALL(*mEventThread, onHotplugReceived(DisplayDevice::DISPLAY_PRIMARY, true)).Times(1); |
| 395 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 396 | // -------------------------------------------------------------------- |
| 397 | // Invocation |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 398 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 399 | mFlinger.handleTransactionLocked(eDisplayTransactionNeeded); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 400 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 401 | // -------------------------------------------------------------------- |
| 402 | // Postconditions |
| 403 | |
| 404 | // HWComposer should have an entry for the display |
| 405 | EXPECT_TRUE(mFlinger.mutableHwcDisplaySlots().count(displayId) == 1); |
| 406 | |
| 407 | // The display should have set up as a primary built-in display. |
| 408 | auto displayToken = mFlinger.mutableBuiltinDisplays()[DisplayDevice::DISPLAY_PRIMARY]; |
| 409 | ASSERT_TRUE(displayToken != nullptr); |
| 410 | |
| 411 | // The display device should have been set up in the list of displays. |
| 412 | ASSERT_TRUE(hasDisplayDevice(displayToken)); |
| 413 | const auto& device = getDisplayDevice(displayToken); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 414 | EXPECT_TRUE(device->isSecure()); |
| 415 | EXPECT_TRUE(device->isPrimary()); |
| 416 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 417 | // The display should have been set up in the current display state |
| 418 | ASSERT_TRUE(hasCurrentDisplayState(displayToken)); |
| 419 | const auto& current = getCurrentDisplayState(displayToken); |
| 420 | EXPECT_EQ(DisplayDevice::DISPLAY_PRIMARY, current.type); |
| 421 | |
| 422 | // The display should have been set up in the drawing display state |
| 423 | ASSERT_TRUE(hasDrawingDisplayState(displayToken)); |
| 424 | const auto& draw = getDrawingDisplayState(displayToken); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 425 | EXPECT_EQ(DisplayDevice::DISPLAY_PRIMARY, draw.type); |
| 426 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 427 | // -------------------------------------------------------------------- |
| 428 | // Cleanup conditions |
| 429 | |
| 430 | EXPECT_CALL(*mComposer, setVsyncEnabled(displayId, IComposerClient::Vsync::DISABLE)) |
| 431 | .WillOnce(Return(Error::NONE)); |
| 432 | EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR)); |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | } // namespace |
| 436 | } // namespace android |