blob: f1556d8bc7d537ab187ee0c7c5ac10cfaf392b3b [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
214 auto& addCapability(HWC2::Capability cap) {
215 mCapabilities.emplace(cap);
216 return *this;
217 }
218
219 void inject(TestableSurfaceFlinger* flinger, Hwc2::Composer* composer) {
220 auto display = std::make_unique<HWC2Display>(*composer, mCapabilities, mHwcDisplayId,
221 mHwcDisplayType);
222
223 auto config = HWC2::Display::Config::Builder(*display, mActiveConfig);
224 config.setWidth(mWidth);
225 config.setHeight(mHeight);
226 config.setVsyncPeriod(mRefreshRate);
227 config.setDpiX(mDpiX);
228 config.setDpiY(mDpiY);
229 display->mutableConfigs().emplace(mActiveConfig, config.build());
230 display->mutableIsConnected() = true;
231
232 ASSERT_TRUE(flinger->mutableHwcDisplayData().size() > static_cast<size_t>(mType));
233 flinger->mutableHwcDisplayData()[mType].reset();
234 flinger->mutableHwcDisplayData()[mType].hwcDisplay = display.get();
235 flinger->mutableHwcDisplaySlots().emplace(mHwcDisplayId, mType);
236
237 flinger->mFakeHwcDisplays.push_back(std::move(display));
238 }
239
240 private:
241 DisplayDevice::DisplayType mType;
242 HWC2::DisplayType mHwcDisplayType;
243 hwc2_display_t mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
244 int32_t mWidth = DEFAULT_WIDTH;
245 int32_t mHeight = DEFAULT_HEIGHT;
246 int32_t mRefreshRate = DEFAULT_REFRESH_RATE;
247 int32_t mDpiX = DEFAULT_DPI;
248 int32_t mDpiY = DEFAULT_DPI;
249 int32_t mActiveConfig = DEFAULT_ACTIVE_CONFIG;
250 std::unordered_set<HWC2::Capability> mCapabilities;
251 };
252
253 class FakeDisplayDeviceInjector {
254 public:
255 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger, DisplayDevice::DisplayType type,
256 int hwcId)
257 : mFlinger(flinger), mType(type), mHwcId(hwcId) {}
258
259 sp<IBinder> token() const { return mDisplayToken; }
260
261 DisplayDeviceState& mutableDrawingDisplayState() {
262 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
263 }
264
265 DisplayDeviceState& mutableCurrentDisplayState() {
266 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
267 }
268
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800269 const auto& getDrawingDisplayState() {
270 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
271 }
272
273 const auto& getCurrentDisplayState() {
274 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
275 }
276
277 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays().valueFor(mDisplayToken); }
278
Lloyd Piquefc12f562018-01-22 18:44:59 -0800279 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
280 mNativeWindow = nativeWindow;
281 return *this;
282 }
283
284 auto& setDisplaySurface(const sp<DisplaySurface>& displaySurface) {
285 mDisplaySurface = displaySurface;
286 return *this;
287 }
288
289 auto& setRenderSurface(std::unique_ptr<RE::Surface> renderSurface) {
290 mRenderSurface = std::move(renderSurface);
291 return *this;
292 }
293
294 auto& setSecure(bool secure) {
295 mSecure = secure;
296 return *this;
297 }
298
299 sp<DisplayDevice> inject() {
Peiyong Lind6fa4a72018-04-17 15:09:44 -0700300 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> hdrAndRenderIntents;
Lloyd Piquefc12f562018-01-22 18:44:59 -0800301 sp<DisplayDevice> device =
302 new DisplayDevice(mFlinger.mFlinger.get(), mType, mHwcId, mSecure, mDisplayToken,
303 mNativeWindow, mDisplaySurface, std::move(mRenderSurface), 0,
Peiyong Lind6fa4a72018-04-17 15:09:44 -0700304 0, false, HdrCapabilities(), 0, hdrAndRenderIntents,
305 HWC_POWER_MODE_NORMAL);
Lloyd Piquefc12f562018-01-22 18:44:59 -0800306 mFlinger.mutableDisplays().add(mDisplayToken, device);
307
308 DisplayDeviceState state(mType, mSecure);
309 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
310 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
311
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800312 if (mType >= DisplayDevice::DISPLAY_PRIMARY && mType < DisplayDevice::DISPLAY_VIRTUAL) {
Lloyd Piquefc12f562018-01-22 18:44:59 -0800313 mFlinger.mutableBuiltinDisplays()[mType] = mDisplayToken;
314 }
315
316 return device;
317 }
318
319 private:
320 TestableSurfaceFlinger& mFlinger;
321 sp<BBinder> mDisplayToken = new BBinder();
322 DisplayDevice::DisplayType mType;
323 int mHwcId;
324 sp<ANativeWindow> mNativeWindow;
325 sp<DisplaySurface> mDisplaySurface;
326 std::unique_ptr<RE::Surface> mRenderSurface;
327 bool mSecure = false;
328 };
329
Lloyd Pique2d3ee6d2018-01-17 13:42:24 -0800330 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(SurfaceFlinger::SkipInitialization);
Lloyd Piquefc12f562018-01-22 18:44:59 -0800331
332 // We need to keep a reference to these so they are properly destroyed.
333 std::vector<std::unique_ptr<HWC2Display>> mFakeHwcDisplays;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800334};
335
336} // namespace android