blob: 5497d032bf764da430d73db0de8b4797b6bac7eb [file] [log] [blame]
Lloyd Piquef58625d2017-12-19 13:22:33 -08001/*
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#pragma once
18
19#include "DisplayDevice.h"
20#include "SurfaceFlinger.h"
21
22namespace android {
23
Lloyd Piquee39cad22017-12-20 17:01:29 -080024class EventThread;
25
26namespace RE {
27class RenderEngine;
28}
29
30namespace Hwc2 {
31class Composer;
32}
33
Lloyd Piquef58625d2017-12-19 13:22:33 -080034class TestableSurfaceFlinger {
35public:
36 // Extend this as needed for accessing SurfaceFlinger private (and public)
37 // functions.
38
Lloyd Piquee39cad22017-12-20 17:01:29 -080039 void setupRenderEngine(std::unique_ptr<RE::RenderEngine> renderEngine) {
40 mFlinger->getBE().mRenderEngine = std::move(renderEngine);
41 }
42
43 void setupComposer(std::unique_ptr<Hwc2::Composer> composer) {
44 mFlinger->getBE().mHwc.reset(new HWComposer(std::move(composer)));
45 }
46
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080047 using CreateBufferQueueFunction = SurfaceFlinger::CreateBufferQueueFunction;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080048 void setCreateBufferQueueFunction(CreateBufferQueueFunction f) {
49 mFlinger->mCreateBufferQueue = f;
50 }
51
Lloyd Pique0b1fe702018-01-22 18:03:16 -080052 using CreateNativeWindowSurfaceFunction = SurfaceFlinger::CreateNativeWindowSurfaceFunction;
53 void setCreateNativeWindowSurface(CreateNativeWindowSurfaceFunction f) {
54 mFlinger->mCreateNativeWindowSurface = f;
55 }
56
57 using HotplugEvent = SurfaceFlinger::HotplugEvent;
58
Lloyd Piquef58625d2017-12-19 13:22:33 -080059 /* ------------------------------------------------------------------------
60 * Forwarding for functions being tested
61 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -080062
Lloyd Piquea482f992018-01-22 19:00:34 -080063 auto createDisplay(const String8& displayName, bool secure) {
64 return mFlinger->createDisplay(displayName, secure);
65 }
66
Dominik Laskowskieecd6592018-05-29 10:25:41 -070067 auto destroyDisplay(const sp<IBinder>& displayToken) {
68 return mFlinger->destroyDisplay(displayToken);
69 }
Lloyd Piquea482f992018-01-22 19:00:34 -080070
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -080071 auto resetDisplayState() { return mFlinger->resetDisplayState(); }
72
Dominik Laskowskieecd6592018-05-29 10:25:41 -070073 auto setupNewDisplayDeviceInternal(const wp<IBinder>& displayToken, int hwcId,
Lloyd Piquec11e0d32018-01-22 18:44:59 -080074 const DisplayDeviceState& state,
75 const sp<DisplaySurface>& dispSurface,
76 const sp<IGraphicBufferProducer>& producer) {
Dominik Laskowskieecd6592018-05-29 10:25:41 -070077 return mFlinger->setupNewDisplayDeviceInternal(displayToken, hwcId, state, dispSurface,
Lloyd Piquec11e0d32018-01-22 18:44:59 -080078 producer);
79 }
80
Lloyd Pique0b1fe702018-01-22 18:03:16 -080081 auto handleTransactionLocked(uint32_t transactionFlags) {
82 return mFlinger->handleTransactionLocked(transactionFlags);
83 }
Lloyd Piquef58625d2017-12-19 13:22:33 -080084
Lloyd Pique6cf11032018-01-22 18:57:44 -080085 auto onHotplugReceived(int32_t sequenceId, hwc2_display_t display,
86 HWC2::Connection connection) {
87 return mFlinger->onHotplugReceived(sequenceId, display, connection);
88 }
89
Lloyd Pique9d9cf402018-02-16 17:47:13 -080090 auto setDisplayStateLocked(const DisplayState& s) { return mFlinger->setDisplayStateLocked(s); }
91
Lloyd Pique86016da2018-03-01 16:09:38 -080092 auto onInitializeDisplays() { return mFlinger->onInitializeDisplays(); }
93
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -080094 auto setPowerModeInternal(const sp<DisplayDevice>& hw, int mode, bool stateLockHeld = false) {
95 return mFlinger->setPowerModeInternal(hw, mode, stateLockHeld);
96 }
97
Lloyd Pique86016da2018-03-01 16:09:38 -080098 /* ------------------------------------------------------------------------
99 * Read-only access to private data to assert post-conditions.
100 */
101
102 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800103 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
104 const auto& getHWVsyncAvailable() const { return mFlinger->mHWVsyncAvailable; }
105 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
106
Lloyd Pique86016da2018-03-01 16:09:38 -0800107 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
108
Lloyd Piquef58625d2017-12-19 13:22:33 -0800109 /* ------------------------------------------------------------------------
110 * Read-write access to private data to set up preconditions and assert
111 * post-conditions.
112 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800113
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800114 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
115
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700116 auto& mutableDisplayTokens() { return mFlinger->mDisplayTokens; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800117 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800118 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800119 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800120 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800121 auto& mutableEventControlThread() { return mFlinger->mEventControlThread; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800122 auto& mutableEventQueue() { return mFlinger->mEventQueue; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800123 auto& mutableEventThread() { return mFlinger->mEventThread; }
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800124 auto& mutableHWVsyncAvailable() { return mFlinger->mHWVsyncAvailable; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800125 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800126 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800127 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800128 auto& mutablePrimaryHWVsyncEnabled() { return mFlinger->mPrimaryHWVsyncEnabled; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800129 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800130 auto& mutableUseHwcVirtualDisplays() { return mFlinger->mUseHwcVirtualDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800131
Lloyd Pique6cf11032018-01-22 18:57:44 -0800132 auto& mutableComposerSequenceId() { return mFlinger->getBE().mComposerSequenceId; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800133 auto& mutableHwcDisplayData() { return mFlinger->getBE().mHwc->mDisplayData; }
134 auto& mutableHwcDisplaySlots() { return mFlinger->getBE().mHwc->mHwcDisplaySlots; }
135
Lloyd Piquee39cad22017-12-20 17:01:29 -0800136 ~TestableSurfaceFlinger() {
137 // All these pointer and container clears help ensure that GMock does
138 // not report a leaked object, since the SurfaceFlinger instance may
139 // still be referenced by something despite our best efforts to destroy
140 // it after each test is done.
141 mutableDisplays().clear();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800142 mutableEventControlThread().reset();
143 mutableEventQueue().reset();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800144 mutableEventThread().reset();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800145 mutableInterceptor().reset();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800146 mFlinger->getBE().mHwc.reset();
147 mFlinger->getBE().mRenderEngine.reset();
148 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800149
Lloyd Piquea618d852018-01-17 11:52:30 -0800150 /* ------------------------------------------------------------------------
151 * Wrapper classes for Read-write access to private data to set up
152 * preconditions and assert post-conditions.
153 */
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800154
Lloyd Piquea618d852018-01-17 11:52:30 -0800155 struct HWC2Display : public HWC2::Display {
156 HWC2Display(Hwc2::Composer& composer,
157 const std::unordered_set<HWC2::Capability>& capabilities, hwc2_display_t id,
158 HWC2::DisplayType type)
159 : HWC2::Display(composer, capabilities, id, type) {}
160 ~HWC2Display() {
161 // Prevents a call to disable vsyncs.
162 mType = HWC2::DisplayType::Invalid;
163 }
164
165 auto& mutableIsConnected() { return this->mIsConnected; }
166 auto& mutableConfigs() { return this->mConfigs; }
167 };
168
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800169 class FakeHwcDisplayInjector {
170 public:
171 static constexpr hwc2_display_t DEFAULT_HWC_DISPLAY_ID = 1000;
172 static constexpr int32_t DEFAULT_WIDTH = 1920;
173 static constexpr int32_t DEFAULT_HEIGHT = 1280;
174 static constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
175 static constexpr int32_t DEFAULT_DPI = 320;
176 static constexpr int32_t DEFAULT_ACTIVE_CONFIG = 0;
177
178 FakeHwcDisplayInjector(DisplayDevice::DisplayType type, HWC2::DisplayType hwcDisplayType)
179 : mType(type), mHwcDisplayType(hwcDisplayType) {}
180
181 auto& setHwcDisplayId(hwc2_display_t displayId) {
182 mHwcDisplayId = displayId;
183 return *this;
184 }
185
186 auto& setWidth(int32_t width) {
187 mWidth = width;
188 return *this;
189 }
190
191 auto& setHeight(int32_t height) {
192 mHeight = height;
193 return *this;
194 }
195
196 auto& setRefreshRate(int32_t refreshRate) {
197 mRefreshRate = refreshRate;
198 return *this;
199 }
200
201 auto& setDpiX(int32_t dpi) {
202 mDpiX = dpi;
203 return *this;
204 }
205
206 auto& setDpiY(int32_t dpi) {
207 mDpiY = dpi;
208 return *this;
209 }
210
211 auto& setActiveConfig(int32_t config) {
212 mActiveConfig = config;
213 return *this;
214 }
215
216 auto& addCapability(HWC2::Capability cap) {
217 mCapabilities.emplace(cap);
218 return *this;
219 }
220
221 void inject(TestableSurfaceFlinger* flinger, Hwc2::Composer* composer) {
222 auto display = std::make_unique<HWC2Display>(*composer, mCapabilities, mHwcDisplayId,
223 mHwcDisplayType);
224
225 auto config = HWC2::Display::Config::Builder(*display, mActiveConfig);
226 config.setWidth(mWidth);
227 config.setHeight(mHeight);
228 config.setVsyncPeriod(mRefreshRate);
229 config.setDpiX(mDpiX);
230 config.setDpiY(mDpiY);
231 display->mutableConfigs().emplace(mActiveConfig, config.build());
232 display->mutableIsConnected() = true;
233
234 ASSERT_TRUE(flinger->mutableHwcDisplayData().size() > static_cast<size_t>(mType));
235 flinger->mutableHwcDisplayData()[mType].reset();
236 flinger->mutableHwcDisplayData()[mType].hwcDisplay = display.get();
237 flinger->mutableHwcDisplaySlots().emplace(mHwcDisplayId, mType);
238
239 flinger->mFakeHwcDisplays.push_back(std::move(display));
240 }
241
242 private:
243 DisplayDevice::DisplayType mType;
244 HWC2::DisplayType mHwcDisplayType;
245 hwc2_display_t mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
246 int32_t mWidth = DEFAULT_WIDTH;
247 int32_t mHeight = DEFAULT_HEIGHT;
248 int32_t mRefreshRate = DEFAULT_REFRESH_RATE;
249 int32_t mDpiX = DEFAULT_DPI;
250 int32_t mDpiY = DEFAULT_DPI;
251 int32_t mActiveConfig = DEFAULT_ACTIVE_CONFIG;
252 std::unordered_set<HWC2::Capability> mCapabilities;
253 };
254
255 class FakeDisplayDeviceInjector {
256 public:
257 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger, DisplayDevice::DisplayType type,
258 int hwcId)
259 : mFlinger(flinger), mType(type), mHwcId(hwcId) {}
260
261 sp<IBinder> token() const { return mDisplayToken; }
262
263 DisplayDeviceState& mutableDrawingDisplayState() {
264 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
265 }
266
267 DisplayDeviceState& mutableCurrentDisplayState() {
268 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
269 }
270
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800271 const auto& getDrawingDisplayState() {
272 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
273 }
274
275 const auto& getCurrentDisplayState() {
276 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
277 }
278
279 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays().valueFor(mDisplayToken); }
280
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800281 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
282 mNativeWindow = nativeWindow;
283 return *this;
284 }
285
286 auto& setDisplaySurface(const sp<DisplaySurface>& displaySurface) {
287 mDisplaySurface = displaySurface;
288 return *this;
289 }
290
291 auto& setRenderSurface(std::unique_ptr<RE::Surface> renderSurface) {
292 mRenderSurface = std::move(renderSurface);
293 return *this;
294 }
295
296 auto& setSecure(bool secure) {
297 mSecure = secure;
298 return *this;
299 }
300
301 sp<DisplayDevice> inject() {
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700302 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> hdrAndRenderIntents;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800303 sp<DisplayDevice> device =
304 new DisplayDevice(mFlinger.mFlinger.get(), mType, mHwcId, mSecure, mDisplayToken,
305 mNativeWindow, mDisplaySurface, std::move(mRenderSurface), 0,
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700306 0, false, HdrCapabilities(), 0, hdrAndRenderIntents,
307 HWC_POWER_MODE_NORMAL);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800308 mFlinger.mutableDisplays().add(mDisplayToken, device);
309
310 DisplayDeviceState state;
311 state.type = mType;
312 state.isSecure = mSecure;
313 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
314 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
315
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800316 if (mType >= DisplayDevice::DISPLAY_PRIMARY && mType < DisplayDevice::DISPLAY_VIRTUAL) {
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700317 mFlinger.mutableDisplayTokens()[mType] = mDisplayToken;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800318 }
319
320 return device;
321 }
322
323 private:
324 TestableSurfaceFlinger& mFlinger;
325 sp<BBinder> mDisplayToken = new BBinder();
326 DisplayDevice::DisplayType mType;
327 int mHwcId;
328 sp<ANativeWindow> mNativeWindow;
329 sp<DisplaySurface> mDisplaySurface;
330 std::unique_ptr<RE::Surface> mRenderSurface;
331 bool mSecure = false;
332 };
333
Lloyd Piqueac648ee2018-01-17 13:42:24 -0800334 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(SurfaceFlinger::SkipInitialization);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800335
336 // We need to keep a reference to these so they are properly destroyed.
337 std::vector<std::unique_ptr<HWC2Display>> mFakeHwcDisplays;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800338};
339
340} // namespace android