blob: d3b637bad95b9c55fd26dd75a3c1d8a21e5271d1 [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 Piquec11e0d32018-01-22 18:44:59 -080063 auto setupNewDisplayDeviceInternal(const wp<IBinder>& display, int hwcId,
64 const DisplayDeviceState& state,
65 const sp<DisplaySurface>& dispSurface,
66 const sp<IGraphicBufferProducer>& producer) {
67 return mFlinger->setupNewDisplayDeviceInternal(display, hwcId, state, dispSurface,
68 producer);
69 }
70
Lloyd Pique0b1fe702018-01-22 18:03:16 -080071 auto handleTransactionLocked(uint32_t transactionFlags) {
72 return mFlinger->handleTransactionLocked(transactionFlags);
73 }
Lloyd Piquef58625d2017-12-19 13:22:33 -080074
75 /* ------------------------------------------------------------------------
76 * Read-write access to private data to set up preconditions and assert
77 * post-conditions.
78 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -080079
Lloyd Piquec11e0d32018-01-22 18:44:59 -080080 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
81
Lloyd Piquef58625d2017-12-19 13:22:33 -080082 auto& mutableBuiltinDisplays() { return mFlinger->mBuiltinDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -080083 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -080084 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -080085 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piquee39cad22017-12-20 17:01:29 -080086 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -080087 auto& mutableEventControlThread() { return mFlinger->mEventControlThread; }
Lloyd Piquee39cad22017-12-20 17:01:29 -080088 auto& mutableEventQueue() { return mFlinger->mEventQueue; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -080089 auto& mutableEventThread() { return mFlinger->mEventThread; }
90 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
91 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
92 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -080093 auto& mutableUseHwcVirtualDisplays() { return mFlinger->mUseHwcVirtualDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -080094
Lloyd Piquea618d852018-01-17 11:52:30 -080095 auto& mutableHwcDisplayData() { return mFlinger->getBE().mHwc->mDisplayData; }
96 auto& mutableHwcDisplaySlots() { return mFlinger->getBE().mHwc->mHwcDisplaySlots; }
97
Lloyd Piquee39cad22017-12-20 17:01:29 -080098 ~TestableSurfaceFlinger() {
99 // All these pointer and container clears help ensure that GMock does
100 // not report a leaked object, since the SurfaceFlinger instance may
101 // still be referenced by something despite our best efforts to destroy
102 // it after each test is done.
103 mutableDisplays().clear();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800104 mutableEventControlThread().reset();
105 mutableEventQueue().reset();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800106 mutableEventThread().reset();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800107 mutableInterceptor().reset();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800108 mFlinger->getBE().mHwc.reset();
109 mFlinger->getBE().mRenderEngine.reset();
110 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800111
Lloyd Piquea618d852018-01-17 11:52:30 -0800112 /* ------------------------------------------------------------------------
113 * Wrapper classes for Read-write access to private data to set up
114 * preconditions and assert post-conditions.
115 */
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800116
Lloyd Piquea618d852018-01-17 11:52:30 -0800117 struct HWC2Display : public HWC2::Display {
118 HWC2Display(Hwc2::Composer& composer,
119 const std::unordered_set<HWC2::Capability>& capabilities, hwc2_display_t id,
120 HWC2::DisplayType type)
121 : HWC2::Display(composer, capabilities, id, type) {}
122 ~HWC2Display() {
123 // Prevents a call to disable vsyncs.
124 mType = HWC2::DisplayType::Invalid;
125 }
126
127 auto& mutableIsConnected() { return this->mIsConnected; }
128 auto& mutableConfigs() { return this->mConfigs; }
129 };
130
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800131 class FakeHwcDisplayInjector {
132 public:
133 static constexpr hwc2_display_t DEFAULT_HWC_DISPLAY_ID = 1000;
134 static constexpr int32_t DEFAULT_WIDTH = 1920;
135 static constexpr int32_t DEFAULT_HEIGHT = 1280;
136 static constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
137 static constexpr int32_t DEFAULT_DPI = 320;
138 static constexpr int32_t DEFAULT_ACTIVE_CONFIG = 0;
139
140 FakeHwcDisplayInjector(DisplayDevice::DisplayType type, HWC2::DisplayType hwcDisplayType)
141 : mType(type), mHwcDisplayType(hwcDisplayType) {}
142
143 auto& setHwcDisplayId(hwc2_display_t displayId) {
144 mHwcDisplayId = displayId;
145 return *this;
146 }
147
148 auto& setWidth(int32_t width) {
149 mWidth = width;
150 return *this;
151 }
152
153 auto& setHeight(int32_t height) {
154 mHeight = height;
155 return *this;
156 }
157
158 auto& setRefreshRate(int32_t refreshRate) {
159 mRefreshRate = refreshRate;
160 return *this;
161 }
162
163 auto& setDpiX(int32_t dpi) {
164 mDpiX = dpi;
165 return *this;
166 }
167
168 auto& setDpiY(int32_t dpi) {
169 mDpiY = dpi;
170 return *this;
171 }
172
173 auto& setActiveConfig(int32_t config) {
174 mActiveConfig = config;
175 return *this;
176 }
177
178 auto& addCapability(HWC2::Capability cap) {
179 mCapabilities.emplace(cap);
180 return *this;
181 }
182
183 void inject(TestableSurfaceFlinger* flinger, Hwc2::Composer* composer) {
184 auto display = std::make_unique<HWC2Display>(*composer, mCapabilities, mHwcDisplayId,
185 mHwcDisplayType);
186
187 auto config = HWC2::Display::Config::Builder(*display, mActiveConfig);
188 config.setWidth(mWidth);
189 config.setHeight(mHeight);
190 config.setVsyncPeriod(mRefreshRate);
191 config.setDpiX(mDpiX);
192 config.setDpiY(mDpiY);
193 display->mutableConfigs().emplace(mActiveConfig, config.build());
194 display->mutableIsConnected() = true;
195
196 ASSERT_TRUE(flinger->mutableHwcDisplayData().size() > static_cast<size_t>(mType));
197 flinger->mutableHwcDisplayData()[mType].reset();
198 flinger->mutableHwcDisplayData()[mType].hwcDisplay = display.get();
199 flinger->mutableHwcDisplaySlots().emplace(mHwcDisplayId, mType);
200
201 flinger->mFakeHwcDisplays.push_back(std::move(display));
202 }
203
204 private:
205 DisplayDevice::DisplayType mType;
206 HWC2::DisplayType mHwcDisplayType;
207 hwc2_display_t mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
208 int32_t mWidth = DEFAULT_WIDTH;
209 int32_t mHeight = DEFAULT_HEIGHT;
210 int32_t mRefreshRate = DEFAULT_REFRESH_RATE;
211 int32_t mDpiX = DEFAULT_DPI;
212 int32_t mDpiY = DEFAULT_DPI;
213 int32_t mActiveConfig = DEFAULT_ACTIVE_CONFIG;
214 std::unordered_set<HWC2::Capability> mCapabilities;
215 };
216
217 class FakeDisplayDeviceInjector {
218 public:
219 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger, DisplayDevice::DisplayType type,
220 int hwcId)
221 : mFlinger(flinger), mType(type), mHwcId(hwcId) {}
222
223 sp<IBinder> token() const { return mDisplayToken; }
224
225 DisplayDeviceState& mutableDrawingDisplayState() {
226 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
227 }
228
229 DisplayDeviceState& mutableCurrentDisplayState() {
230 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
231 }
232
233 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
234 mNativeWindow = nativeWindow;
235 return *this;
236 }
237
238 auto& setDisplaySurface(const sp<DisplaySurface>& displaySurface) {
239 mDisplaySurface = displaySurface;
240 return *this;
241 }
242
243 auto& setRenderSurface(std::unique_ptr<RE::Surface> renderSurface) {
244 mRenderSurface = std::move(renderSurface);
245 return *this;
246 }
247
248 auto& setSecure(bool secure) {
249 mSecure = secure;
250 return *this;
251 }
252
253 sp<DisplayDevice> inject() {
254 sp<DisplayDevice> device =
255 new DisplayDevice(mFlinger.mFlinger.get(), mType, mHwcId, mSecure, mDisplayToken,
256 mNativeWindow, mDisplaySurface, std::move(mRenderSurface), 0,
257 0, false, HdrCapabilities(), 0, HWC_POWER_MODE_NORMAL);
258 mFlinger.mutableDisplays().add(mDisplayToken, device);
259
260 DisplayDeviceState state;
261 state.type = mType;
262 state.isSecure = mSecure;
263 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
264 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
265
266 if (mType < DisplayDevice::DISPLAY_VIRTUAL) {
267 mFlinger.mutableBuiltinDisplays()[mType] = mDisplayToken;
268 }
269
270 return device;
271 }
272
273 private:
274 TestableSurfaceFlinger& mFlinger;
275 sp<BBinder> mDisplayToken = new BBinder();
276 DisplayDevice::DisplayType mType;
277 int mHwcId;
278 sp<ANativeWindow> mNativeWindow;
279 sp<DisplaySurface> mDisplaySurface;
280 std::unique_ptr<RE::Surface> mRenderSurface;
281 bool mSecure = false;
282 };
283
Lloyd Piqueac648ee2018-01-17 13:42:24 -0800284 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(SurfaceFlinger::SkipInitialization);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800285
286 // We need to keep a reference to these so they are properly destroyed.
287 std::vector<std::unique_ptr<HWC2Display>> mFakeHwcDisplays;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800288};
289
290} // namespace android