blob: eec505ec5898aab589d6330d21fb45fa338848c1 [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 Pique1fa4d462018-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 Pique1fa4d462018-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
67 auto destroyDisplay(const sp<IBinder>& display) { return mFlinger->destroyDisplay(display); }
68
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -080069 auto resetDisplayState() { return mFlinger->resetDisplayState(); }
70
Lloyd Piquefc12f562018-01-22 18:44:59 -080071 auto setupNewDisplayDeviceInternal(const wp<IBinder>& display, int hwcId,
72 const DisplayDeviceState& state,
73 const sp<DisplaySurface>& dispSurface,
74 const sp<IGraphicBufferProducer>& producer) {
75 return mFlinger->setupNewDisplayDeviceInternal(display, hwcId, state, dispSurface,
76 producer);
77 }
78
Lloyd Pique1fa4d462018-01-22 18:03:16 -080079 auto handleTransactionLocked(uint32_t transactionFlags) {
80 return mFlinger->handleTransactionLocked(transactionFlags);
81 }
Lloyd Piquef58625d2017-12-19 13:22:33 -080082
Lloyd Pique6cf11032018-01-22 18:57:44 -080083 auto onHotplugReceived(int32_t sequenceId, hwc2_display_t display,
84 HWC2::Connection connection) {
85 return mFlinger->onHotplugReceived(sequenceId, display, connection);
86 }
87
Lloyd Pique9d9cf402018-02-16 17:47:13 -080088 auto setDisplayStateLocked(const DisplayState& s) { return mFlinger->setDisplayStateLocked(s); }
89
Lloyd Pique86016da2018-03-01 16:09:38 -080090 auto onInitializeDisplays() { return mFlinger->onInitializeDisplays(); }
91
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -080092 auto setPowerModeInternal(const sp<DisplayDevice>& hw, int mode, bool stateLockHeld = false) {
93 return mFlinger->setPowerModeInternal(hw, mode, stateLockHeld);
94 }
95
Lloyd Pique86016da2018-03-01 16:09:38 -080096 /* ------------------------------------------------------------------------
97 * Read-only access to private data to assert post-conditions.
98 */
99
100 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800101 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
102 const auto& getHWVsyncAvailable() const { return mFlinger->mHWVsyncAvailable; }
103 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
104
Lloyd Pique86016da2018-03-01 16:09:38 -0800105 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
106
Lloyd Piquef58625d2017-12-19 13:22:33 -0800107 /* ------------------------------------------------------------------------
108 * Read-write access to private data to set up preconditions and assert
109 * post-conditions.
110 */
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800111
Lloyd Piquefc12f562018-01-22 18:44:59 -0800112 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
113
Lloyd Piquef58625d2017-12-19 13:22:33 -0800114 auto& mutableBuiltinDisplays() { return mFlinger->mBuiltinDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800115 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800116 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquefc12f562018-01-22 18:44:59 -0800117 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800118 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800119 auto& mutableEventControlThread() { return mFlinger->mEventControlThread; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800120 auto& mutableEventQueue() { return mFlinger->mEventQueue; }
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800121 auto& mutableEventThread() { return mFlinger->mEventThread; }
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800122 auto& mutableHWVsyncAvailable() { return mFlinger->mHWVsyncAvailable; }
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800123 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800124 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800125 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800126 auto& mutablePrimaryHWVsyncEnabled() { return mFlinger->mPrimaryHWVsyncEnabled; }
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800127 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Lloyd Piquefc12f562018-01-22 18:44:59 -0800128 auto& mutableUseHwcVirtualDisplays() { return mFlinger->mUseHwcVirtualDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800129
Lloyd Pique6cf11032018-01-22 18:57:44 -0800130 auto& mutableComposerSequenceId() { return mFlinger->getBE().mComposerSequenceId; }
Lloyd Piquebc792092018-01-17 11:52:30 -0800131 auto& mutableHwcDisplayData() { return mFlinger->getBE().mHwc->mDisplayData; }
132 auto& mutableHwcDisplaySlots() { return mFlinger->getBE().mHwc->mHwcDisplaySlots; }
133
Lloyd Piquee39cad22017-12-20 17:01:29 -0800134 ~TestableSurfaceFlinger() {
135 // All these pointer and container clears help ensure that GMock does
136 // not report a leaked object, since the SurfaceFlinger instance may
137 // still be referenced by something despite our best efforts to destroy
138 // it after each test is done.
139 mutableDisplays().clear();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800140 mutableEventControlThread().reset();
141 mutableEventQueue().reset();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800142 mutableEventThread().reset();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800143 mutableInterceptor().reset();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800144 mFlinger->getBE().mHwc.reset();
145 mFlinger->getBE().mRenderEngine.reset();
146 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800147
Lloyd Piquebc792092018-01-17 11:52:30 -0800148 /* ------------------------------------------------------------------------
149 * Wrapper classes for Read-write access to private data to set up
150 * preconditions and assert post-conditions.
151 */
Lloyd Piquefc12f562018-01-22 18:44:59 -0800152
Lloyd Piquebc792092018-01-17 11:52:30 -0800153 struct HWC2Display : public HWC2::Display {
154 HWC2Display(Hwc2::Composer& composer,
155 const std::unordered_set<HWC2::Capability>& capabilities, hwc2_display_t id,
156 HWC2::DisplayType type)
157 : HWC2::Display(composer, capabilities, id, type) {}
158 ~HWC2Display() {
159 // Prevents a call to disable vsyncs.
160 mType = HWC2::DisplayType::Invalid;
161 }
162
163 auto& mutableIsConnected() { return this->mIsConnected; }
164 auto& mutableConfigs() { return this->mConfigs; }
165 };
166
Lloyd Piquefc12f562018-01-22 18:44:59 -0800167 class FakeHwcDisplayInjector {
168 public:
169 static constexpr hwc2_display_t DEFAULT_HWC_DISPLAY_ID = 1000;
170 static constexpr int32_t DEFAULT_WIDTH = 1920;
171 static constexpr int32_t DEFAULT_HEIGHT = 1280;
172 static constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
173 static constexpr int32_t DEFAULT_DPI = 320;
174 static constexpr int32_t DEFAULT_ACTIVE_CONFIG = 0;
175
176 FakeHwcDisplayInjector(DisplayDevice::DisplayType type, HWC2::DisplayType hwcDisplayType)
177 : mType(type), mHwcDisplayType(hwcDisplayType) {}
178
179 auto& setHwcDisplayId(hwc2_display_t displayId) {
180 mHwcDisplayId = displayId;
181 return *this;
182 }
183
184 auto& setWidth(int32_t width) {
185 mWidth = width;
186 return *this;
187 }
188
189 auto& setHeight(int32_t height) {
190 mHeight = height;
191 return *this;
192 }
193
194 auto& setRefreshRate(int32_t refreshRate) {
195 mRefreshRate = refreshRate;
196 return *this;
197 }
198
199 auto& setDpiX(int32_t dpi) {
200 mDpiX = dpi;
201 return *this;
202 }
203
204 auto& setDpiY(int32_t dpi) {
205 mDpiY = dpi;
206 return *this;
207 }
208
209 auto& setActiveConfig(int32_t config) {
210 mActiveConfig = config;
211 return *this;
212 }
213
Lloyd Pique08c66222018-10-09 11:53:39 -0700214 auto& setCapabilities(const std::unordered_set<HWC2::Capability>* capabilities) {
215 mCapabilities = capabilities;
Lloyd Piquefc12f562018-01-22 18:44:59 -0800216 return *this;
217 }
218
219 void inject(TestableSurfaceFlinger* flinger, Hwc2::Composer* composer) {
Lloyd Pique08c66222018-10-09 11:53:39 -0700220 static const std::unordered_set<HWC2::Capability> defaultCapabilities;
221 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities;
222
223 // Caution - Make sure that any values passed by reference here do
224 // not refer to an instance owned by FakeHwcDisplayInjector. This
225 // class has temporary lifetime, while the constructed HWC2::Display
226 // is much longer lived.
227 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId,
Lloyd Piquefc12f562018-01-22 18:44:59 -0800228 mHwcDisplayType);
229
230 auto config = HWC2::Display::Config::Builder(*display, mActiveConfig);
231 config.setWidth(mWidth);
232 config.setHeight(mHeight);
233 config.setVsyncPeriod(mRefreshRate);
234 config.setDpiX(mDpiX);
235 config.setDpiY(mDpiY);
236 display->mutableConfigs().emplace(mActiveConfig, config.build());
237 display->mutableIsConnected() = true;
238
239 ASSERT_TRUE(flinger->mutableHwcDisplayData().size() > static_cast<size_t>(mType));
240 flinger->mutableHwcDisplayData()[mType].reset();
241 flinger->mutableHwcDisplayData()[mType].hwcDisplay = display.get();
242 flinger->mutableHwcDisplaySlots().emplace(mHwcDisplayId, mType);
243
244 flinger->mFakeHwcDisplays.push_back(std::move(display));
245 }
246
247 private:
248 DisplayDevice::DisplayType mType;
249 HWC2::DisplayType mHwcDisplayType;
250 hwc2_display_t mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
251 int32_t mWidth = DEFAULT_WIDTH;
252 int32_t mHeight = DEFAULT_HEIGHT;
253 int32_t mRefreshRate = DEFAULT_REFRESH_RATE;
254 int32_t mDpiX = DEFAULT_DPI;
255 int32_t mDpiY = DEFAULT_DPI;
256 int32_t mActiveConfig = DEFAULT_ACTIVE_CONFIG;
Lloyd Pique08c66222018-10-09 11:53:39 -0700257 const std::unordered_set<HWC2::Capability>* mCapabilities = nullptr;
Lloyd Piquefc12f562018-01-22 18:44:59 -0800258 };
259
260 class FakeDisplayDeviceInjector {
261 public:
262 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger, DisplayDevice::DisplayType type,
263 int hwcId)
264 : mFlinger(flinger), mType(type), mHwcId(hwcId) {}
265
266 sp<IBinder> token() const { return mDisplayToken; }
267
268 DisplayDeviceState& mutableDrawingDisplayState() {
269 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
270 }
271
272 DisplayDeviceState& mutableCurrentDisplayState() {
273 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
274 }
275
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800276 const auto& getDrawingDisplayState() {
277 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
278 }
279
280 const auto& getCurrentDisplayState() {
281 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
282 }
283
284 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays().valueFor(mDisplayToken); }
285
Lloyd Piquefc12f562018-01-22 18:44:59 -0800286 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
287 mNativeWindow = nativeWindow;
288 return *this;
289 }
290
291 auto& setDisplaySurface(const sp<DisplaySurface>& displaySurface) {
292 mDisplaySurface = displaySurface;
293 return *this;
294 }
295
296 auto& setRenderSurface(std::unique_ptr<RE::Surface> renderSurface) {
297 mRenderSurface = std::move(renderSurface);
298 return *this;
299 }
300
301 auto& setSecure(bool secure) {
302 mSecure = secure;
303 return *this;
304 }
305
306 sp<DisplayDevice> inject() {
Peiyong Lind6fa4a72018-04-17 15:09:44 -0700307 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> hdrAndRenderIntents;
Lloyd Piquefc12f562018-01-22 18:44:59 -0800308 sp<DisplayDevice> device =
309 new DisplayDevice(mFlinger.mFlinger.get(), mType, mHwcId, mSecure, mDisplayToken,
310 mNativeWindow, mDisplaySurface, std::move(mRenderSurface), 0,
Peiyong Lind6fa4a72018-04-17 15:09:44 -0700311 0, false, HdrCapabilities(), 0, hdrAndRenderIntents,
312 HWC_POWER_MODE_NORMAL);
Lloyd Piquefc12f562018-01-22 18:44:59 -0800313 mFlinger.mutableDisplays().add(mDisplayToken, device);
314
315 DisplayDeviceState state(mType, mSecure);
316 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
317 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
318
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800319 if (mType >= DisplayDevice::DISPLAY_PRIMARY && mType < DisplayDevice::DISPLAY_VIRTUAL) {
Lloyd Piquefc12f562018-01-22 18:44:59 -0800320 mFlinger.mutableBuiltinDisplays()[mType] = mDisplayToken;
321 }
322
323 return device;
324 }
325
326 private:
327 TestableSurfaceFlinger& mFlinger;
328 sp<BBinder> mDisplayToken = new BBinder();
329 DisplayDevice::DisplayType mType;
330 int mHwcId;
331 sp<ANativeWindow> mNativeWindow;
332 sp<DisplaySurface> mDisplaySurface;
333 std::unique_ptr<RE::Surface> mRenderSurface;
334 bool mSecure = false;
335 };
336
Lloyd Pique2d3ee6d2018-01-17 13:42:24 -0800337 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(SurfaceFlinger::SkipInitialization);
Lloyd Piquefc12f562018-01-22 18:44:59 -0800338
339 // We need to keep a reference to these so they are properly destroyed.
340 std::vector<std::unique_ptr<HWC2Display>> mFakeHwcDisplays;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800341};
342
343} // namespace android