SFFakeHwc_test.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /*
  2. * Copyright (C) 2017 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. // #define LOG_NDEBUG 0
  17. #undef LOG_TAG
  18. #define LOG_TAG "FakeHwcTest"
  19. #include "FakeComposerClient.h"
  20. #include "FakeComposerService.h"
  21. #include "FakeComposerUtils.h"
  22. #include <gui/DisplayEventReceiver.h>
  23. #include <gui/ISurfaceComposer.h>
  24. #include <gui/LayerDebugInfo.h>
  25. #include <gui/LayerState.h>
  26. #include <gui/Surface.h>
  27. #include <gui/SurfaceComposerClient.h>
  28. #include <android/hidl/manager/1.0/IServiceManager.h>
  29. #include <android/looper.h>
  30. #include <android/native_window.h>
  31. #include <binder/ProcessState.h>
  32. #include <hwbinder/ProcessState.h>
  33. #include <log/log.h>
  34. #include <private/gui/ComposerService.h>
  35. #include <ui/DisplayInfo.h>
  36. #include <utils/Looper.h>
  37. #include <gmock/gmock.h>
  38. #include <gtest/gtest.h>
  39. #include <limits>
  40. using namespace std::chrono_literals;
  41. using namespace android;
  42. using namespace android::hardware;
  43. using namespace sftest;
  44. namespace {
  45. // Mock test helpers
  46. using ::testing::Invoke;
  47. using ::testing::Return;
  48. using ::testing::SetArgPointee;
  49. using ::testing::_;
  50. using Transaction = SurfaceComposerClient::Transaction;
  51. ///////////////////////////////////////////////
  52. struct TestColor {
  53. public:
  54. uint8_t r;
  55. uint8_t g;
  56. uint8_t b;
  57. uint8_t a;
  58. };
  59. constexpr static TestColor RED = {195, 63, 63, 255};
  60. constexpr static TestColor LIGHT_RED = {255, 177, 177, 255};
  61. constexpr static TestColor GREEN = {63, 195, 63, 255};
  62. constexpr static TestColor BLUE = {63, 63, 195, 255};
  63. constexpr static TestColor DARK_GRAY = {63, 63, 63, 255};
  64. constexpr static TestColor LIGHT_GRAY = {200, 200, 200, 255};
  65. // Fill an RGBA_8888 formatted surface with a single color.
  66. static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, const TestColor& color,
  67. bool unlock = true) {
  68. ANativeWindow_Buffer outBuffer;
  69. sp<Surface> s = sc->getSurface();
  70. ASSERT_TRUE(s != nullptr);
  71. ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
  72. uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
  73. for (int y = 0; y < outBuffer.height; y++) {
  74. for (int x = 0; x < outBuffer.width; x++) {
  75. uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
  76. pixel[0] = color.r;
  77. pixel[1] = color.g;
  78. pixel[2] = color.b;
  79. pixel[3] = color.a;
  80. }
  81. }
  82. if (unlock) {
  83. ASSERT_EQ(NO_ERROR, s->unlockAndPost());
  84. }
  85. }
  86. inline RenderState makeSimpleRect(int left, int top, int right, int bottom) {
  87. RenderState res;
  88. res.mDisplayFrame = hwc_rect_t{left, top, right, bottom};
  89. res.mPlaneAlpha = 1.0f;
  90. res.mSwapCount = 0;
  91. res.mSourceCrop = hwc_frect_t{0.f, 0.f, static_cast<float>(right - left),
  92. static_cast<float>(bottom - top)};
  93. return res;
  94. }
  95. inline RenderState makeSimpleRect(unsigned int left, unsigned int top, unsigned int right,
  96. unsigned int bottom) {
  97. EXPECT_LE(left, static_cast<unsigned int>(INT_MAX));
  98. EXPECT_LE(top, static_cast<unsigned int>(INT_MAX));
  99. EXPECT_LE(right, static_cast<unsigned int>(INT_MAX));
  100. EXPECT_LE(bottom, static_cast<unsigned int>(INT_MAX));
  101. return makeSimpleRect(static_cast<int>(left), static_cast<int>(top), static_cast<int>(right),
  102. static_cast<int>(bottom));
  103. }
  104. ////////////////////////////////////////////////
  105. class DisplayTest : public ::testing::Test {
  106. public:
  107. class MockComposerClient : public FakeComposerClient {
  108. public:
  109. MOCK_METHOD2(getDisplayType, Error(Display display, ComposerClient::DisplayType* outType));
  110. MOCK_METHOD4(getDisplayAttribute,
  111. Error(Display display, Config config, IComposerClient::Attribute attribute,
  112. int32_t* outValue));
  113. // Re-routing to basic fake implementation
  114. Error getDisplayAttributeFake(Display display, Config config,
  115. IComposerClient::Attribute attribute, int32_t* outValue) {
  116. return FakeComposerClient::getDisplayAttribute(display, config, attribute, outValue);
  117. }
  118. };
  119. protected:
  120. static int processDisplayEvents(int fd, int events, void* data);
  121. void SetUp() override;
  122. void TearDown() override;
  123. void waitForDisplayTransaction();
  124. bool waitForHotplugEvent(PhysicalDisplayId displayId, bool connected);
  125. sp<IComposer> mFakeService;
  126. sp<SurfaceComposerClient> mComposerClient;
  127. MockComposerClient* mMockComposer;
  128. std::unique_ptr<DisplayEventReceiver> mReceiver;
  129. sp<Looper> mLooper;;
  130. std::deque<DisplayEventReceiver::Event> mReceivedDisplayEvents;
  131. };
  132. void DisplayTest::SetUp() {
  133. // TODO: The mMockComposer should be a unique_ptr, but it needs to
  134. // outlive the test class. Currently ComposerClient only dies
  135. // when the service is replaced. The Mock deletes itself when
  136. // removeClient is called on it, which is ugly. This can be
  137. // changed if HIDL ServiceManager allows removing services or
  138. // ComposerClient starts taking the ownership of the contained
  139. // implementation class. Moving the fake class to the HWC2
  140. // interface instead of the current Composer interface might also
  141. // change the situation.
  142. mMockComposer = new MockComposerClient;
  143. sp<ComposerClient> client = new ComposerClient(mMockComposer);
  144. mFakeService = new FakeComposerService(client);
  145. (void)mFakeService->registerAsService("mock");
  146. android::hardware::ProcessState::self()->startThreadPool();
  147. android::ProcessState::self()->startThreadPool();
  148. EXPECT_CALL(*mMockComposer, getDisplayType(PRIMARY_DISPLAY, _))
  149. .WillOnce(DoAll(SetArgPointee<1>(IComposerClient::DisplayType::PHYSICAL),
  150. Return(Error::NONE)));
  151. // Primary display will be queried twice for all 5 attributes. One
  152. // set of queries comes from the SurfaceFlinger proper an the
  153. // other set from the VR composer.
  154. // TODO: Is VR composer always present? Change to atLeast(5)?
  155. EXPECT_CALL(*mMockComposer, getDisplayAttribute(PRIMARY_DISPLAY, 1, _, _))
  156. .Times(2 * 5)
  157. .WillRepeatedly(Invoke(mMockComposer, &MockComposerClient::getDisplayAttributeFake));
  158. startSurfaceFlinger();
  159. // Fake composer wants to enable VSync injection
  160. mMockComposer->onSurfaceFlingerStart();
  161. mComposerClient = new SurfaceComposerClient;
  162. ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
  163. mReceiver.reset(new DisplayEventReceiver());
  164. mLooper = new Looper(false);
  165. mLooper->addFd(mReceiver->getFd(), 0, ALOOPER_EVENT_INPUT, processDisplayEvents, this);
  166. }
  167. void DisplayTest::TearDown() {
  168. mLooper = nullptr;
  169. mReceiver = nullptr;
  170. mComposerClient->dispose();
  171. mComposerClient = nullptr;
  172. // Fake composer needs to release SurfaceComposerClient before the stop.
  173. mMockComposer->onSurfaceFlingerStop();
  174. stopSurfaceFlinger();
  175. mFakeService = nullptr;
  176. // TODO: Currently deleted in FakeComposerClient::removeClient(). Devise better lifetime
  177. // management.
  178. mMockComposer = nullptr;
  179. }
  180. int DisplayTest::processDisplayEvents(int /*fd*/, int /*events*/, void* data) {
  181. auto self = static_cast<DisplayTest*>(data);
  182. ssize_t n;
  183. DisplayEventReceiver::Event buffer[1];
  184. while ((n = self->mReceiver->getEvents(buffer, 1)) > 0) {
  185. for (int i=0 ; i<n ; i++) {
  186. self->mReceivedDisplayEvents.push_back(buffer[i]);
  187. }
  188. }
  189. ALOGD_IF(n < 0, "Error reading events (%s)\n", strerror(-n));
  190. return 1;
  191. }
  192. void DisplayTest::waitForDisplayTransaction() {
  193. // Both a refresh and a vsync event are needed to apply pending display
  194. // transactions.
  195. mMockComposer->refreshDisplay(EXTERNAL_DISPLAY);
  196. mMockComposer->runVSyncAndWait();
  197. // Extra vsync and wait to avoid a 10% flake due to a race.
  198. mMockComposer->runVSyncAndWait();
  199. }
  200. bool DisplayTest::waitForHotplugEvent(PhysicalDisplayId displayId, bool connected) {
  201. int waitCount = 20;
  202. while (waitCount--) {
  203. while (!mReceivedDisplayEvents.empty()) {
  204. auto event = mReceivedDisplayEvents.front();
  205. mReceivedDisplayEvents.pop_front();
  206. ALOGV_IF(event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG,
  207. "event hotplug: displayId %" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT
  208. ", connected %d\t",
  209. event.header.displayId, event.hotplug.connected);
  210. if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG &&
  211. event.header.displayId == displayId && event.hotplug.connected == connected) {
  212. return true;
  213. }
  214. }
  215. mLooper->pollOnce(1);
  216. }
  217. return false;
  218. }
  219. TEST_F(DisplayTest, Hotplug) {
  220. ALOGD("DisplayTest::Hotplug");
  221. EXPECT_CALL(*mMockComposer, getDisplayType(EXTERNAL_DISPLAY, _))
  222. .Times(2)
  223. .WillRepeatedly(DoAll(SetArgPointee<1>(IComposerClient::DisplayType::PHYSICAL),
  224. Return(Error::NONE)));
  225. // The attribute queries will get done twice. This is for defaults
  226. EXPECT_CALL(*mMockComposer, getDisplayAttribute(EXTERNAL_DISPLAY, 1, _, _))
  227. .Times(2 * 3)
  228. .WillRepeatedly(Invoke(mMockComposer, &MockComposerClient::getDisplayAttributeFake));
  229. // ... and then special handling for dimensions. Specifying these
  230. // rules later means that gmock will try them first, i.e.,
  231. // ordering of width/height vs. the default implementation for
  232. // other queries is significant.
  233. EXPECT_CALL(*mMockComposer,
  234. getDisplayAttribute(EXTERNAL_DISPLAY, 1, IComposerClient::Attribute::WIDTH, _))
  235. .Times(2)
  236. .WillRepeatedly(DoAll(SetArgPointee<3>(400), Return(Error::NONE)));
  237. EXPECT_CALL(*mMockComposer,
  238. getDisplayAttribute(EXTERNAL_DISPLAY, 1, IComposerClient::Attribute::HEIGHT, _))
  239. .Times(2)
  240. .WillRepeatedly(DoAll(SetArgPointee<3>(200), Return(Error::NONE)));
  241. mMockComposer->hotplugDisplay(EXTERNAL_DISPLAY, IComposerCallback::Connection::CONNECTED);
  242. waitForDisplayTransaction();
  243. EXPECT_TRUE(waitForHotplugEvent(EXTERNAL_DISPLAY, true));
  244. {
  245. const auto display = SurfaceComposerClient::getPhysicalDisplayToken(EXTERNAL_DISPLAY);
  246. ASSERT_FALSE(display == nullptr);
  247. DisplayInfo info;
  248. ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(display, &info));
  249. ASSERT_EQ(400u, info.w);
  250. ASSERT_EQ(200u, info.h);
  251. auto surfaceControl =
  252. mComposerClient->createSurface(String8("Display Test Surface Foo"), info.w, info.h,
  253. PIXEL_FORMAT_RGBA_8888, 0);
  254. ASSERT_TRUE(surfaceControl != nullptr);
  255. ASSERT_TRUE(surfaceControl->isValid());
  256. fillSurfaceRGBA8(surfaceControl, BLUE);
  257. {
  258. TransactionScope ts(*mMockComposer);
  259. ts.setDisplayLayerStack(display, 0);
  260. ts.setLayer(surfaceControl, INT32_MAX - 2)
  261. .show(surfaceControl);
  262. }
  263. }
  264. mMockComposer->hotplugDisplay(EXTERNAL_DISPLAY, IComposerCallback::Connection::DISCONNECTED);
  265. mMockComposer->clearFrames();
  266. mMockComposer->hotplugDisplay(EXTERNAL_DISPLAY, IComposerCallback::Connection::CONNECTED);
  267. waitForDisplayTransaction();
  268. EXPECT_TRUE(waitForHotplugEvent(EXTERNAL_DISPLAY, false));
  269. EXPECT_TRUE(waitForHotplugEvent(EXTERNAL_DISPLAY, true));
  270. {
  271. const auto display = SurfaceComposerClient::getPhysicalDisplayToken(EXTERNAL_DISPLAY);
  272. ASSERT_FALSE(display == nullptr);
  273. DisplayInfo info;
  274. ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(display, &info));
  275. ASSERT_EQ(400u, info.w);
  276. ASSERT_EQ(200u, info.h);
  277. auto surfaceControl =
  278. mComposerClient->createSurface(String8("Display Test Surface Bar"), info.w, info.h,
  279. PIXEL_FORMAT_RGBA_8888, 0);
  280. ASSERT_TRUE(surfaceControl != nullptr);
  281. ASSERT_TRUE(surfaceControl->isValid());
  282. fillSurfaceRGBA8(surfaceControl, BLUE);
  283. {
  284. TransactionScope ts(*mMockComposer);
  285. ts.setDisplayLayerStack(display, 0);
  286. ts.setLayer(surfaceControl, INT32_MAX - 2)
  287. .show(surfaceControl);
  288. }
  289. }
  290. mMockComposer->hotplugDisplay(EXTERNAL_DISPLAY, IComposerCallback::Connection::DISCONNECTED);
  291. }
  292. TEST_F(DisplayTest, HotplugPrimaryDisplay) {
  293. ALOGD("DisplayTest::HotplugPrimaryDisplay");
  294. mMockComposer->hotplugDisplay(PRIMARY_DISPLAY, IComposerCallback::Connection::DISCONNECTED);
  295. waitForDisplayTransaction();
  296. EXPECT_TRUE(waitForHotplugEvent(PRIMARY_DISPLAY, false));
  297. {
  298. const auto display = SurfaceComposerClient::getPhysicalDisplayToken(PRIMARY_DISPLAY);
  299. EXPECT_FALSE(display == nullptr);
  300. DisplayInfo info;
  301. auto result = SurfaceComposerClient::getDisplayInfo(display, &info);
  302. EXPECT_NE(NO_ERROR, result);
  303. }
  304. mMockComposer->clearFrames();
  305. EXPECT_CALL(*mMockComposer, getDisplayType(PRIMARY_DISPLAY, _))
  306. .Times(2)
  307. .WillRepeatedly(DoAll(SetArgPointee<1>(IComposerClient::DisplayType::PHYSICAL),
  308. Return(Error::NONE)));
  309. // The attribute queries will get done twice. This is for defaults
  310. EXPECT_CALL(*mMockComposer, getDisplayAttribute(PRIMARY_DISPLAY, 1, _, _))
  311. .Times(2 * 3)
  312. .WillRepeatedly(Invoke(mMockComposer, &MockComposerClient::getDisplayAttributeFake));
  313. // ... and then special handling for dimensions. Specifying these
  314. // rules later means that gmock will try them first, i.e.,
  315. // ordering of width/height vs. the default implementation for
  316. // other queries is significant.
  317. EXPECT_CALL(*mMockComposer,
  318. getDisplayAttribute(PRIMARY_DISPLAY, 1, IComposerClient::Attribute::WIDTH, _))
  319. .Times(2)
  320. .WillRepeatedly(DoAll(SetArgPointee<3>(400), Return(Error::NONE)));
  321. EXPECT_CALL(*mMockComposer,
  322. getDisplayAttribute(PRIMARY_DISPLAY, 1, IComposerClient::Attribute::HEIGHT, _))
  323. .Times(2)
  324. .WillRepeatedly(DoAll(SetArgPointee<3>(200), Return(Error::NONE)));
  325. mMockComposer->hotplugDisplay(PRIMARY_DISPLAY, IComposerCallback::Connection::CONNECTED);
  326. waitForDisplayTransaction();
  327. EXPECT_TRUE(waitForHotplugEvent(PRIMARY_DISPLAY, true));
  328. {
  329. const auto display = SurfaceComposerClient::getPhysicalDisplayToken(PRIMARY_DISPLAY);
  330. EXPECT_FALSE(display == nullptr);
  331. DisplayInfo info;
  332. auto result = SurfaceComposerClient::getDisplayInfo(display, &info);
  333. EXPECT_EQ(NO_ERROR, result);
  334. ASSERT_EQ(400u, info.w);
  335. ASSERT_EQ(200u, info.h);
  336. }
  337. }
  338. ////////////////////////////////////////////////
  339. class TransactionTest : public ::testing::Test {
  340. protected:
  341. // Layer array indexing constants.
  342. constexpr static int BG_LAYER = 0;
  343. constexpr static int FG_LAYER = 1;
  344. static void SetUpTestCase();
  345. static void TearDownTestCase();
  346. void SetUp() override;
  347. void TearDown() override;
  348. sp<SurfaceComposerClient> mComposerClient;
  349. sp<SurfaceControl> mBGSurfaceControl;
  350. sp<SurfaceControl> mFGSurfaceControl;
  351. std::vector<RenderState> mBaseFrame;
  352. uint32_t mDisplayWidth;
  353. uint32_t mDisplayHeight;
  354. static FakeComposerClient* sFakeComposer;
  355. };
  356. FakeComposerClient* TransactionTest::sFakeComposer;
  357. void TransactionTest::SetUpTestCase() {
  358. // TODO: See TODO comment at DisplayTest::SetUp for background on
  359. // the lifetime of the FakeComposerClient.
  360. sFakeComposer = new FakeComposerClient;
  361. sp<ComposerClient> client = new ComposerClient(sFakeComposer);
  362. sp<IComposer> fakeService = new FakeComposerService(client);
  363. (void)fakeService->registerAsService("mock");
  364. android::hardware::ProcessState::self()->startThreadPool();
  365. android::ProcessState::self()->startThreadPool();
  366. startSurfaceFlinger();
  367. // Fake composer wants to enable VSync injection
  368. sFakeComposer->onSurfaceFlingerStart();
  369. }
  370. void TransactionTest::TearDownTestCase() {
  371. // Fake composer needs to release SurfaceComposerClient before the stop.
  372. sFakeComposer->onSurfaceFlingerStop();
  373. stopSurfaceFlinger();
  374. // TODO: This is deleted when the ComposerClient calls
  375. // removeClient. Devise better lifetime control.
  376. sFakeComposer = nullptr;
  377. }
  378. void TransactionTest::SetUp() {
  379. ALOGI("TransactionTest::SetUp");
  380. mComposerClient = new SurfaceComposerClient;
  381. ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
  382. ALOGI("TransactionTest::SetUp - display");
  383. const auto display = SurfaceComposerClient::getPhysicalDisplayToken(PRIMARY_DISPLAY);
  384. ASSERT_FALSE(display == nullptr);
  385. DisplayInfo info;
  386. ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(display, &info));
  387. mDisplayWidth = info.w;
  388. mDisplayHeight = info.h;
  389. // Background surface
  390. mBGSurfaceControl = mComposerClient->createSurface(String8("BG Test Surface"), mDisplayWidth,
  391. mDisplayHeight, PIXEL_FORMAT_RGBA_8888, 0);
  392. ASSERT_TRUE(mBGSurfaceControl != nullptr);
  393. ASSERT_TRUE(mBGSurfaceControl->isValid());
  394. fillSurfaceRGBA8(mBGSurfaceControl, BLUE);
  395. // Foreground surface
  396. mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
  397. PIXEL_FORMAT_RGBA_8888, 0);
  398. ASSERT_TRUE(mFGSurfaceControl != nullptr);
  399. ASSERT_TRUE(mFGSurfaceControl->isValid());
  400. fillSurfaceRGBA8(mFGSurfaceControl, RED);
  401. Transaction t;
  402. t.setDisplayLayerStack(display, 0);
  403. t.setLayer(mBGSurfaceControl, INT32_MAX - 2);
  404. t.show(mBGSurfaceControl);
  405. t.setLayer(mFGSurfaceControl, INT32_MAX - 1);
  406. t.setPosition(mFGSurfaceControl, 64, 64);
  407. t.show(mFGSurfaceControl);
  408. // Synchronous transaction will stop this thread, so we set up a
  409. // delayed, off-thread vsync request before closing the
  410. // transaction. In the test code this is usually done with
  411. // TransactionScope. Leaving here in the 'vanilla' form for
  412. // reference.
  413. ASSERT_EQ(0, sFakeComposer->getFrameCount());
  414. sFakeComposer->runVSyncAfter(1ms);
  415. t.apply();
  416. sFakeComposer->waitUntilFrame(1);
  417. // Reference data. This is what the HWC should see.
  418. static_assert(BG_LAYER == 0 && FG_LAYER == 1, "Unexpected enum values for array indexing");
  419. mBaseFrame.push_back(makeSimpleRect(0u, 0u, mDisplayWidth, mDisplayHeight));
  420. mBaseFrame[BG_LAYER].mSwapCount = 1;
  421. mBaseFrame.push_back(makeSimpleRect(64, 64, 64 + 64, 64 + 64));
  422. mBaseFrame[FG_LAYER].mSwapCount = 1;
  423. auto frame = sFakeComposer->getFrameRects(0);
  424. ASSERT_TRUE(framesAreSame(mBaseFrame, frame));
  425. }
  426. void TransactionTest::TearDown() {
  427. ALOGD("TransactionTest::TearDown");
  428. mComposerClient->dispose();
  429. mBGSurfaceControl = 0;
  430. mFGSurfaceControl = 0;
  431. mComposerClient = 0;
  432. sFakeComposer->runVSyncAndWait();
  433. mBaseFrame.clear();
  434. sFakeComposer->clearFrames();
  435. ASSERT_EQ(0, sFakeComposer->getFrameCount());
  436. sp<ISurfaceComposer> sf(ComposerService::getComposerService());
  437. std::vector<LayerDebugInfo> layers;
  438. status_t result = sf->getLayerDebugInfo(&layers);
  439. if (result != NO_ERROR) {
  440. ALOGE("Failed to get layers %s %d", strerror(-result), result);
  441. } else {
  442. // If this fails, the test being torn down leaked layers.
  443. EXPECT_EQ(0u, layers.size());
  444. if (layers.size() > 0) {
  445. for (auto layer = layers.begin(); layer != layers.end(); ++layer) {
  446. std::cout << to_string(*layer).c_str();
  447. }
  448. // To ensure the next test has clean slate, will run the class
  449. // tear down and setup here.
  450. TearDownTestCase();
  451. SetUpTestCase();
  452. }
  453. }
  454. ALOGD("TransactionTest::TearDown - complete");
  455. }
  456. TEST_F(TransactionTest, LayerMove) {
  457. ALOGD("TransactionTest::LayerMove");
  458. // The scope opens and closes a global transaction and, at the
  459. // same time, makes sure the SurfaceFlinger progresses one frame
  460. // after the transaction closes. The results of the transaction
  461. // should be available in the latest frame stored by the fake
  462. // composer.
  463. {
  464. TransactionScope ts(*sFakeComposer);
  465. ts.setPosition(mFGSurfaceControl, 128, 128);
  466. // NOTE: No changes yet, so vsync will do nothing, HWC does not get any calls.
  467. // (How to verify that? Throw in vsync and wait a 2x frame time? Separate test?)
  468. //
  469. // sFakeComposer->runVSyncAndWait();
  470. }
  471. fillSurfaceRGBA8(mFGSurfaceControl, GREEN);
  472. sFakeComposer->runVSyncAndWait();
  473. ASSERT_EQ(3, sFakeComposer->getFrameCount()); // Make sure the waits didn't time out and there's
  474. // no extra frames.
  475. // NOTE: Frame 0 is produced in the SetUp.
  476. auto frame1Ref = mBaseFrame;
  477. frame1Ref[FG_LAYER].mDisplayFrame =
  478. hwc_rect_t{128, 128, 128 + 64, 128 + 64}; // Top-most layer moves.
  479. EXPECT_TRUE(framesAreSame(frame1Ref, sFakeComposer->getFrameRects(1)));
  480. auto frame2Ref = frame1Ref;
  481. frame2Ref[FG_LAYER].mSwapCount++;
  482. EXPECT_TRUE(framesAreSame(frame2Ref, sFakeComposer->getFrameRects(2)));
  483. }
  484. TEST_F(TransactionTest, LayerResize) {
  485. ALOGD("TransactionTest::LayerResize");
  486. {
  487. TransactionScope ts(*sFakeComposer);
  488. ts.setSize(mFGSurfaceControl, 128, 128);
  489. }
  490. fillSurfaceRGBA8(mFGSurfaceControl, GREEN);
  491. sFakeComposer->runVSyncAndWait();
  492. ASSERT_EQ(3, sFakeComposer->getFrameCount()); // Make sure the waits didn't time out and there's
  493. // no extra frames.
  494. auto frame1Ref = mBaseFrame;
  495. // NOTE: The resize should not be visible for frame 1 as there's no buffer with new size posted.
  496. EXPECT_TRUE(framesAreSame(frame1Ref, sFakeComposer->getFrameRects(1)));
  497. auto frame2Ref = frame1Ref;
  498. frame2Ref[FG_LAYER].mSwapCount++;
  499. frame2Ref[FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 128, 64 + 128};
  500. frame2Ref[FG_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 128.f, 128.f};
  501. EXPECT_TRUE(framesAreSame(frame2Ref, sFakeComposer->getFrameRects(2)));
  502. }
  503. TEST_F(TransactionTest, LayerCrop) {
  504. // TODO: Add scaling to confirm that crop happens in buffer space?
  505. {
  506. TransactionScope ts(*sFakeComposer);
  507. Rect cropRect(16, 16, 32, 32);
  508. ts.setCrop_legacy(mFGSurfaceControl, cropRect);
  509. }
  510. ASSERT_EQ(2, sFakeComposer->getFrameCount());
  511. auto referenceFrame = mBaseFrame;
  512. referenceFrame[FG_LAYER].mSourceCrop = hwc_frect_t{16.f, 16.f, 32.f, 32.f};
  513. referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{64 + 16, 64 + 16, 64 + 32, 64 + 32};
  514. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  515. }
  516. TEST_F(TransactionTest, LayerSetLayer) {
  517. {
  518. TransactionScope ts(*sFakeComposer);
  519. ts.setLayer(mFGSurfaceControl, INT_MAX - 3);
  520. }
  521. ASSERT_EQ(2, sFakeComposer->getFrameCount());
  522. // The layers will switch order, but both are rendered because the background layer is
  523. // transparent (RGBA8888).
  524. std::vector<RenderState> referenceFrame(2);
  525. referenceFrame[0] = mBaseFrame[FG_LAYER];
  526. referenceFrame[1] = mBaseFrame[BG_LAYER];
  527. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  528. }
  529. TEST_F(TransactionTest, LayerSetLayerOpaque) {
  530. {
  531. TransactionScope ts(*sFakeComposer);
  532. ts.setLayer(mFGSurfaceControl, INT_MAX - 3);
  533. ts.setFlags(mBGSurfaceControl, layer_state_t::eLayerOpaque,
  534. layer_state_t::eLayerOpaque);
  535. }
  536. ASSERT_EQ(2, sFakeComposer->getFrameCount());
  537. // The former foreground layer is now covered with opaque layer - it should have disappeared
  538. std::vector<RenderState> referenceFrame(1);
  539. referenceFrame[BG_LAYER] = mBaseFrame[BG_LAYER];
  540. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  541. }
  542. TEST_F(TransactionTest, SetLayerStack) {
  543. ALOGD("TransactionTest::SetLayerStack");
  544. {
  545. TransactionScope ts(*sFakeComposer);
  546. ts.setLayerStack(mFGSurfaceControl, 1);
  547. }
  548. // Foreground layer should have disappeared.
  549. ASSERT_EQ(2, sFakeComposer->getFrameCount());
  550. std::vector<RenderState> refFrame(1);
  551. refFrame[BG_LAYER] = mBaseFrame[BG_LAYER];
  552. EXPECT_TRUE(framesAreSame(refFrame, sFakeComposer->getLatestFrame()));
  553. }
  554. TEST_F(TransactionTest, LayerShowHide) {
  555. ALOGD("TransactionTest::LayerShowHide");
  556. {
  557. TransactionScope ts(*sFakeComposer);
  558. ts.hide(mFGSurfaceControl);
  559. }
  560. // Foreground layer should have disappeared.
  561. ASSERT_EQ(2, sFakeComposer->getFrameCount());
  562. std::vector<RenderState> refFrame(1);
  563. refFrame[BG_LAYER] = mBaseFrame[BG_LAYER];
  564. EXPECT_TRUE(framesAreSame(refFrame, sFakeComposer->getLatestFrame()));
  565. {
  566. TransactionScope ts(*sFakeComposer);
  567. ts.show(mFGSurfaceControl);
  568. }
  569. // Foreground layer should be back
  570. ASSERT_EQ(3, sFakeComposer->getFrameCount());
  571. EXPECT_TRUE(framesAreSame(mBaseFrame, sFakeComposer->getLatestFrame()));
  572. }
  573. TEST_F(TransactionTest, LayerSetAlpha) {
  574. {
  575. TransactionScope ts(*sFakeComposer);
  576. ts.setAlpha(mFGSurfaceControl, 0.75f);
  577. }
  578. ASSERT_EQ(2, sFakeComposer->getFrameCount());
  579. auto referenceFrame = mBaseFrame;
  580. referenceFrame[FG_LAYER].mPlaneAlpha = 0.75f;
  581. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  582. }
  583. TEST_F(TransactionTest, LayerSetFlags) {
  584. {
  585. TransactionScope ts(*sFakeComposer);
  586. ts.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden,
  587. layer_state_t::eLayerHidden);
  588. }
  589. // Foreground layer should have disappeared.
  590. ASSERT_EQ(2, sFakeComposer->getFrameCount());
  591. std::vector<RenderState> refFrame(1);
  592. refFrame[BG_LAYER] = mBaseFrame[BG_LAYER];
  593. EXPECT_TRUE(framesAreSame(refFrame, sFakeComposer->getLatestFrame()));
  594. }
  595. TEST_F(TransactionTest, LayerSetMatrix) {
  596. struct matrixTestData {
  597. float matrix[4];
  598. hwc_transform_t expectedTransform;
  599. hwc_rect_t expectedDisplayFrame;
  600. };
  601. // The matrix operates on the display frame and is applied before
  602. // the position is added. So, the foreground layer rect is (0, 0,
  603. // 64, 64) is first transformed, potentially yielding negative
  604. // coordinates and then the position (64, 64) is added yielding
  605. // the final on-screen rectangles given.
  606. const matrixTestData MATRIX_TESTS[7] = // clang-format off
  607. {{{-1.f, 0.f, 0.f, 1.f}, HWC_TRANSFORM_FLIP_H, {0, 64, 64, 128}},
  608. {{1.f, 0.f, 0.f, -1.f}, HWC_TRANSFORM_FLIP_V, {64, 0, 128, 64}},
  609. {{0.f, 1.f, -1.f, 0.f}, HWC_TRANSFORM_ROT_90, {0, 64, 64, 128}},
  610. {{-1.f, 0.f, 0.f, -1.f}, HWC_TRANSFORM_ROT_180, {0, 0, 64, 64}},
  611. {{0.f, -1.f, 1.f, 0.f}, HWC_TRANSFORM_ROT_270, {64, 0, 128, 64}},
  612. {{0.f, 1.f, 1.f, 0.f}, HWC_TRANSFORM_FLIP_H_ROT_90, {64, 64, 128, 128}},
  613. {{0.f, 1.f, 1.f, 0.f}, HWC_TRANSFORM_FLIP_V_ROT_90, {64, 64, 128, 128}}};
  614. // clang-format on
  615. constexpr int TEST_COUNT = sizeof(MATRIX_TESTS) / sizeof(matrixTestData);
  616. for (int i = 0; i < TEST_COUNT; i++) {
  617. // TODO: How to leverage the HWC2 stringifiers?
  618. const matrixTestData& xform = MATRIX_TESTS[i];
  619. SCOPED_TRACE(i);
  620. {
  621. TransactionScope ts(*sFakeComposer);
  622. ts.setMatrix(mFGSurfaceControl, xform.matrix[0], xform.matrix[1],
  623. xform.matrix[2], xform.matrix[3]);
  624. }
  625. auto referenceFrame = mBaseFrame;
  626. referenceFrame[FG_LAYER].mTransform = xform.expectedTransform;
  627. referenceFrame[FG_LAYER].mDisplayFrame = xform.expectedDisplayFrame;
  628. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  629. }
  630. }
  631. #if 0
  632. TEST_F(TransactionTest, LayerSetMatrix2) {
  633. {
  634. TransactionScope ts(*sFakeComposer);
  635. // TODO: PLEASE SPEC THE FUNCTION!
  636. ts.setMatrix(mFGSurfaceControl, 0.11f, 0.123f,
  637. -2.33f, 0.22f);
  638. }
  639. auto referenceFrame = mBaseFrame;
  640. // TODO: Is this correct for sure?
  641. //referenceFrame[FG_LAYER].mTransform = HWC_TRANSFORM_FLIP_V & HWC_TRANSFORM_ROT_90;
  642. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  643. }
  644. #endif
  645. TEST_F(TransactionTest, DeferredTransaction) {
  646. // Synchronization surface
  647. constexpr static int SYNC_LAYER = 2;
  648. auto syncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
  649. PIXEL_FORMAT_RGBA_8888, 0);
  650. ASSERT_TRUE(syncSurfaceControl != nullptr);
  651. ASSERT_TRUE(syncSurfaceControl->isValid());
  652. fillSurfaceRGBA8(syncSurfaceControl, DARK_GRAY);
  653. {
  654. TransactionScope ts(*sFakeComposer);
  655. ts.setLayer(syncSurfaceControl, INT32_MAX - 1);
  656. ts.setPosition(syncSurfaceControl, mDisplayWidth - 2, mDisplayHeight - 2);
  657. ts.show(syncSurfaceControl);
  658. }
  659. auto referenceFrame = mBaseFrame;
  660. referenceFrame.push_back(makeSimpleRect(mDisplayWidth - 2, mDisplayHeight - 2,
  661. mDisplayWidth - 1, mDisplayHeight - 1));
  662. referenceFrame[SYNC_LAYER].mSwapCount = 1;
  663. EXPECT_EQ(2, sFakeComposer->getFrameCount());
  664. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  665. // set up two deferred transactions on different frames - these should not yield composited
  666. // frames
  667. {
  668. TransactionScope ts(*sFakeComposer);
  669. ts.setAlpha(mFGSurfaceControl, 0.75);
  670. ts.deferTransactionUntil_legacy(mFGSurfaceControl, syncSurfaceControl->getHandle(),
  671. syncSurfaceControl->getSurface()->getNextFrameNumber());
  672. }
  673. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  674. {
  675. TransactionScope ts(*sFakeComposer);
  676. ts.setPosition(mFGSurfaceControl, 128, 128);
  677. ts.deferTransactionUntil_legacy(mFGSurfaceControl, syncSurfaceControl->getHandle(),
  678. syncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
  679. }
  680. EXPECT_EQ(4, sFakeComposer->getFrameCount());
  681. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  682. // should trigger the first deferred transaction, but not the second one
  683. fillSurfaceRGBA8(syncSurfaceControl, DARK_GRAY);
  684. sFakeComposer->runVSyncAndWait();
  685. EXPECT_EQ(5, sFakeComposer->getFrameCount());
  686. referenceFrame[FG_LAYER].mPlaneAlpha = 0.75f;
  687. referenceFrame[SYNC_LAYER].mSwapCount++;
  688. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  689. // should show up immediately since it's not deferred
  690. {
  691. TransactionScope ts(*sFakeComposer);
  692. ts.setAlpha(mFGSurfaceControl, 1.0);
  693. }
  694. referenceFrame[FG_LAYER].mPlaneAlpha = 1.f;
  695. EXPECT_EQ(6, sFakeComposer->getFrameCount());
  696. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  697. // trigger the second deferred transaction
  698. fillSurfaceRGBA8(syncSurfaceControl, DARK_GRAY);
  699. sFakeComposer->runVSyncAndWait();
  700. // TODO: Compute from layer size?
  701. referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{128, 128, 128 + 64, 128 + 64};
  702. referenceFrame[SYNC_LAYER].mSwapCount++;
  703. EXPECT_EQ(7, sFakeComposer->getFrameCount());
  704. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  705. }
  706. TEST_F(TransactionTest, SetRelativeLayer) {
  707. constexpr int RELATIVE_LAYER = 2;
  708. auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
  709. PIXEL_FORMAT_RGBA_8888, 0);
  710. fillSurfaceRGBA8(relativeSurfaceControl, LIGHT_RED);
  711. // Now we stack the surface above the foreground surface and make sure it is visible.
  712. {
  713. TransactionScope ts(*sFakeComposer);
  714. ts.setPosition(relativeSurfaceControl, 64, 64);
  715. ts.show(relativeSurfaceControl);
  716. ts.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
  717. }
  718. auto referenceFrame = mBaseFrame;
  719. // NOTE: All three layers will be visible as the surfaces are
  720. // transparent because of the RGBA format.
  721. referenceFrame.push_back(makeSimpleRect(64, 64, 64 + 64, 64 + 64));
  722. referenceFrame[RELATIVE_LAYER].mSwapCount = 1;
  723. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  724. // A call to setLayer will override a call to setRelativeLayer
  725. {
  726. TransactionScope ts(*sFakeComposer);
  727. ts.setLayer(relativeSurfaceControl, 0);
  728. }
  729. // Previous top layer will now appear at the bottom.
  730. auto referenceFrame2 = mBaseFrame;
  731. referenceFrame2.insert(referenceFrame2.begin(), referenceFrame[RELATIVE_LAYER]);
  732. EXPECT_EQ(3, sFakeComposer->getFrameCount());
  733. EXPECT_TRUE(framesAreSame(referenceFrame2, sFakeComposer->getLatestFrame()));
  734. }
  735. class ChildLayerTest : public TransactionTest {
  736. protected:
  737. constexpr static int CHILD_LAYER = 2;
  738. void SetUp() override {
  739. TransactionTest::SetUp();
  740. mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
  741. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  742. fillSurfaceRGBA8(mChild, LIGHT_GRAY);
  743. sFakeComposer->runVSyncAndWait();
  744. mBaseFrame.push_back(makeSimpleRect(64, 64, 64 + 10, 64 + 10));
  745. mBaseFrame[CHILD_LAYER].mSwapCount = 1;
  746. ASSERT_EQ(2, sFakeComposer->getFrameCount());
  747. ASSERT_TRUE(framesAreSame(mBaseFrame, sFakeComposer->getLatestFrame()));
  748. }
  749. void TearDown() override {
  750. mChild = 0;
  751. TransactionTest::TearDown();
  752. }
  753. sp<SurfaceControl> mChild;
  754. };
  755. TEST_F(ChildLayerTest, Positioning) {
  756. {
  757. TransactionScope ts(*sFakeComposer);
  758. ts.show(mChild);
  759. ts.setPosition(mChild, 10, 10);
  760. // Move to the same position as in the original setup.
  761. ts.setPosition(mFGSurfaceControl, 64, 64);
  762. }
  763. auto referenceFrame = mBaseFrame;
  764. referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 64, 64 + 64};
  765. referenceFrame[CHILD_LAYER].mDisplayFrame =
  766. hwc_rect_t{64 + 10, 64 + 10, 64 + 10 + 10, 64 + 10 + 10};
  767. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  768. {
  769. TransactionScope ts(*sFakeComposer);
  770. ts.setPosition(mFGSurfaceControl, 0, 0);
  771. }
  772. auto referenceFrame2 = mBaseFrame;
  773. referenceFrame2[FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 0 + 64, 0 + 64};
  774. referenceFrame2[CHILD_LAYER].mDisplayFrame =
  775. hwc_rect_t{0 + 10, 0 + 10, 0 + 10 + 10, 0 + 10 + 10};
  776. EXPECT_TRUE(framesAreSame(referenceFrame2, sFakeComposer->getLatestFrame()));
  777. }
  778. TEST_F(ChildLayerTest, Cropping) {
  779. {
  780. TransactionScope ts(*sFakeComposer);
  781. ts.show(mChild);
  782. ts.setPosition(mChild, 0, 0);
  783. ts.setPosition(mFGSurfaceControl, 0, 0);
  784. ts.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
  785. }
  786. // NOTE: The foreground surface would be occluded by the child
  787. // now, but is included in the stack because the child is
  788. // transparent.
  789. auto referenceFrame = mBaseFrame;
  790. referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 0 + 5, 0 + 5};
  791. referenceFrame[FG_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 5.f, 5.f};
  792. referenceFrame[CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 0 + 5, 0 + 5};
  793. referenceFrame[CHILD_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 5.f, 5.f};
  794. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  795. }
  796. TEST_F(ChildLayerTest, Constraints) {
  797. {
  798. TransactionScope ts(*sFakeComposer);
  799. ts.show(mChild);
  800. ts.setPosition(mFGSurfaceControl, 0, 0);
  801. ts.setPosition(mChild, 63, 63);
  802. }
  803. auto referenceFrame = mBaseFrame;
  804. referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 64, 64};
  805. referenceFrame[CHILD_LAYER].mDisplayFrame = hwc_rect_t{63, 63, 64, 64};
  806. referenceFrame[CHILD_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 1.f, 1.f};
  807. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  808. }
  809. TEST_F(ChildLayerTest, Scaling) {
  810. {
  811. TransactionScope ts(*sFakeComposer);
  812. ts.setPosition(mFGSurfaceControl, 0, 0);
  813. }
  814. auto referenceFrame = mBaseFrame;
  815. referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 64, 64};
  816. referenceFrame[CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 10, 10};
  817. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  818. {
  819. TransactionScope ts(*sFakeComposer);
  820. ts.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0);
  821. }
  822. auto referenceFrame2 = mBaseFrame;
  823. referenceFrame2[FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 128, 128};
  824. referenceFrame2[CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 20, 20};
  825. EXPECT_TRUE(framesAreSame(referenceFrame2, sFakeComposer->getLatestFrame()));
  826. }
  827. TEST_F(ChildLayerTest, LayerAlpha) {
  828. {
  829. TransactionScope ts(*sFakeComposer);
  830. ts.show(mChild);
  831. ts.setPosition(mChild, 0, 0);
  832. ts.setPosition(mFGSurfaceControl, 0, 0);
  833. ts.setAlpha(mChild, 0.5);
  834. }
  835. auto referenceFrame = mBaseFrame;
  836. referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 64, 64};
  837. referenceFrame[CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 10, 10};
  838. referenceFrame[CHILD_LAYER].mPlaneAlpha = 0.5f;
  839. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  840. {
  841. TransactionScope ts(*sFakeComposer);
  842. ts.setAlpha(mFGSurfaceControl, 0.5);
  843. }
  844. auto referenceFrame2 = referenceFrame;
  845. referenceFrame2[FG_LAYER].mPlaneAlpha = 0.5f;
  846. referenceFrame2[CHILD_LAYER].mPlaneAlpha = 0.25f;
  847. EXPECT_TRUE(framesAreSame(referenceFrame2, sFakeComposer->getLatestFrame()));
  848. }
  849. TEST_F(ChildLayerTest, ReparentChildren) {
  850. {
  851. TransactionScope ts(*sFakeComposer);
  852. ts.show(mChild);
  853. ts.setPosition(mChild, 10, 10);
  854. ts.setPosition(mFGSurfaceControl, 64, 64);
  855. }
  856. auto referenceFrame = mBaseFrame;
  857. referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 64, 64 + 64};
  858. referenceFrame[CHILD_LAYER].mDisplayFrame =
  859. hwc_rect_t{64 + 10, 64 + 10, 64 + 10 + 10, 64 + 10 + 10};
  860. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  861. {
  862. TransactionScope ts(*sFakeComposer);
  863. ts.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
  864. }
  865. auto referenceFrame2 = referenceFrame;
  866. referenceFrame2[FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 64, 64 + 64};
  867. referenceFrame2[CHILD_LAYER].mDisplayFrame = hwc_rect_t{10, 10, 10 + 10, 10 + 10};
  868. EXPECT_TRUE(framesAreSame(referenceFrame2, sFakeComposer->getLatestFrame()));
  869. }
  870. TEST_F(ChildLayerTest, DetachChildrenSameClient) {
  871. {
  872. TransactionScope ts(*sFakeComposer);
  873. ts.show(mChild);
  874. ts.setPosition(mChild, 10, 10);
  875. ts.setPosition(mFGSurfaceControl, 64, 64);
  876. }
  877. auto referenceFrame = mBaseFrame;
  878. referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 64, 64 + 64};
  879. referenceFrame[CHILD_LAYER].mDisplayFrame =
  880. hwc_rect_t{64 + 10, 64 + 10, 64 + 10 + 10, 64 + 10 + 10};
  881. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  882. {
  883. TransactionScope ts(*sFakeComposer);
  884. ts.setPosition(mFGSurfaceControl, 0, 0);
  885. ts.detachChildren(mFGSurfaceControl);
  886. }
  887. {
  888. TransactionScope ts(*sFakeComposer);
  889. ts.setPosition(mFGSurfaceControl, 64, 64);
  890. ts.hide(mChild);
  891. }
  892. std::vector<RenderState> refFrame(2);
  893. refFrame[BG_LAYER] = mBaseFrame[BG_LAYER];
  894. refFrame[FG_LAYER] = mBaseFrame[FG_LAYER];
  895. EXPECT_TRUE(framesAreSame(refFrame, sFakeComposer->getLatestFrame()));
  896. }
  897. TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
  898. sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
  899. sp<SurfaceControl> childNewClient =
  900. newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
  901. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  902. ASSERT_TRUE(childNewClient != nullptr);
  903. ASSERT_TRUE(childNewClient->isValid());
  904. fillSurfaceRGBA8(childNewClient, LIGHT_GRAY);
  905. {
  906. TransactionScope ts(*sFakeComposer);
  907. ts.hide(mChild);
  908. ts.show(childNewClient);
  909. ts.setPosition(childNewClient, 10, 10);
  910. ts.setPosition(mFGSurfaceControl, 64, 64);
  911. }
  912. auto referenceFrame = mBaseFrame;
  913. referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 64, 64 + 64};
  914. referenceFrame[CHILD_LAYER].mDisplayFrame =
  915. hwc_rect_t{64 + 10, 64 + 10, 64 + 10 + 10, 64 + 10 + 10};
  916. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  917. {
  918. TransactionScope ts(*sFakeComposer);
  919. ts.detachChildren(mFGSurfaceControl);
  920. ts.setPosition(mFGSurfaceControl, 0, 0);
  921. }
  922. {
  923. TransactionScope ts(*sFakeComposer);
  924. ts.setPosition(mFGSurfaceControl, 64, 64);
  925. ts.setPosition(childNewClient, 0, 0);
  926. ts.hide(childNewClient);
  927. }
  928. // Nothing should have changed. The child control becomes a no-op
  929. // zombie on detach. See comments for detachChildren in the
  930. // SurfaceControl.h file.
  931. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  932. }
  933. TEST_F(ChildLayerTest, InheritNonTransformScalingFromParent) {
  934. {
  935. TransactionScope ts(*sFakeComposer);
  936. ts.show(mChild);
  937. ts.setPosition(mChild, 0, 0);
  938. ts.setPosition(mFGSurfaceControl, 0, 0);
  939. }
  940. {
  941. TransactionScope ts(*sFakeComposer);
  942. ts.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
  943. // We cause scaling by 2.
  944. ts.setSize(mFGSurfaceControl, 128, 128);
  945. }
  946. auto referenceFrame = mBaseFrame;
  947. referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 128, 128};
  948. referenceFrame[FG_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 64.f, 64.f};
  949. referenceFrame[CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 20, 20};
  950. referenceFrame[CHILD_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 10.f, 10.f};
  951. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  952. }
  953. // Regression test for b/37673612
  954. TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
  955. {
  956. TransactionScope ts(*sFakeComposer);
  957. ts.show(mChild);
  958. ts.setPosition(mChild, 0, 0);
  959. ts.setPosition(mFGSurfaceControl, 0, 0);
  960. }
  961. // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
  962. // the WM specified state size.
  963. {
  964. TransactionScope ts(*sFakeComposer);
  965. ts.setSize(mFGSurfaceControl, 128, 64);
  966. }
  967. sp<Surface> s = mFGSurfaceControl->getSurface();
  968. auto anw = static_cast<ANativeWindow*>(s.get());
  969. native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
  970. native_window_set_buffers_dimensions(anw, 64, 128);
  971. fillSurfaceRGBA8(mFGSurfaceControl, RED);
  972. sFakeComposer->runVSyncAndWait();
  973. // The child should still be in the same place and not have any strange scaling as in
  974. // b/37673612.
  975. auto referenceFrame = mBaseFrame;
  976. referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 128, 64};
  977. referenceFrame[FG_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 64.f, 128.f};
  978. referenceFrame[FG_LAYER].mSwapCount++;
  979. referenceFrame[CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 10, 10};
  980. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  981. }
  982. TEST_F(ChildLayerTest, Bug36858924) {
  983. // Destroy the child layer
  984. mChild.clear();
  985. // Now recreate it as hidden
  986. mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
  987. PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
  988. mFGSurfaceControl.get());
  989. // Show the child layer in a deferred transaction
  990. {
  991. TransactionScope ts(*sFakeComposer);
  992. ts.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
  993. mFGSurfaceControl->getSurface()->getNextFrameNumber());
  994. ts.show(mChild);
  995. }
  996. // Render the foreground surface a few times
  997. //
  998. // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
  999. // frame because SurfaceFlinger would never process the deferred transaction and would therefore
  1000. // never acquire/release the first buffer
  1001. ALOGI("Filling 1");
  1002. fillSurfaceRGBA8(mFGSurfaceControl, GREEN);
  1003. sFakeComposer->runVSyncAndWait();
  1004. ALOGI("Filling 2");
  1005. fillSurfaceRGBA8(mFGSurfaceControl, BLUE);
  1006. sFakeComposer->runVSyncAndWait();
  1007. ALOGI("Filling 3");
  1008. fillSurfaceRGBA8(mFGSurfaceControl, RED);
  1009. sFakeComposer->runVSyncAndWait();
  1010. ALOGI("Filling 4");
  1011. fillSurfaceRGBA8(mFGSurfaceControl, GREEN);
  1012. sFakeComposer->runVSyncAndWait();
  1013. }
  1014. class ChildColorLayerTest : public ChildLayerTest {
  1015. protected:
  1016. void SetUp() override {
  1017. TransactionTest::SetUp();
  1018. mChild = mComposerClient->createSurface(String8("Child surface"), 0, 0,
  1019. PIXEL_FORMAT_RGBA_8888,
  1020. ISurfaceComposerClient::eFXSurfaceColor,
  1021. mFGSurfaceControl.get());
  1022. {
  1023. TransactionScope ts(*sFakeComposer);
  1024. ts.setColor(mChild,
  1025. {LIGHT_GRAY.r / 255.0f, LIGHT_GRAY.g / 255.0f, LIGHT_GRAY.b / 255.0f});
  1026. ts.setCrop_legacy(mChild, Rect(0, 0, 10, 10));
  1027. }
  1028. sFakeComposer->runVSyncAndWait();
  1029. mBaseFrame.push_back(makeSimpleRect(64, 64, 64 + 10, 64 + 10));
  1030. mBaseFrame[CHILD_LAYER].mSourceCrop = hwc_frect_t{0.0f, 0.0f, 0.0f, 0.0f};
  1031. mBaseFrame[CHILD_LAYER].mSwapCount = 0;
  1032. ASSERT_EQ(2, sFakeComposer->getFrameCount());
  1033. ASSERT_TRUE(framesAreSame(mBaseFrame, sFakeComposer->getLatestFrame()));
  1034. }
  1035. };
  1036. TEST_F(ChildColorLayerTest, LayerAlpha) {
  1037. {
  1038. TransactionScope ts(*sFakeComposer);
  1039. ts.show(mChild);
  1040. ts.setPosition(mChild, 0, 0);
  1041. ts.setPosition(mFGSurfaceControl, 0, 0);
  1042. ts.setAlpha(mChild, 0.5);
  1043. }
  1044. auto referenceFrame = mBaseFrame;
  1045. referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 64, 64};
  1046. referenceFrame[CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 10, 10};
  1047. referenceFrame[CHILD_LAYER].mPlaneAlpha = 0.5f;
  1048. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  1049. {
  1050. TransactionScope ts(*sFakeComposer);
  1051. ts.setAlpha(mFGSurfaceControl, 0.5);
  1052. }
  1053. auto referenceFrame2 = referenceFrame;
  1054. referenceFrame2[FG_LAYER].mPlaneAlpha = 0.5f;
  1055. referenceFrame2[CHILD_LAYER].mPlaneAlpha = 0.25f;
  1056. EXPECT_TRUE(framesAreSame(referenceFrame2, sFakeComposer->getLatestFrame()));
  1057. }
  1058. TEST_F(ChildColorLayerTest, LayerZeroAlpha) {
  1059. {
  1060. TransactionScope ts(*sFakeComposer);
  1061. ts.show(mChild);
  1062. ts.setPosition(mChild, 0, 0);
  1063. ts.setPosition(mFGSurfaceControl, 0, 0);
  1064. ts.setAlpha(mChild, 0.5);
  1065. }
  1066. auto referenceFrame = mBaseFrame;
  1067. referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 64, 64};
  1068. referenceFrame[CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 10, 10};
  1069. referenceFrame[CHILD_LAYER].mPlaneAlpha = 0.5f;
  1070. EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
  1071. {
  1072. TransactionScope ts(*sFakeComposer);
  1073. ts.setAlpha(mFGSurfaceControl, 0.0f);
  1074. }
  1075. std::vector<RenderState> refFrame(1);
  1076. refFrame[BG_LAYER] = mBaseFrame[BG_LAYER];
  1077. EXPECT_TRUE(framesAreSame(refFrame, sFakeComposer->getLatestFrame()));
  1078. }
  1079. class LatchingTest : public TransactionTest {
  1080. protected:
  1081. void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, RED, false); }
  1082. void unlockFGBuffer() {
  1083. sp<Surface> s = mFGSurfaceControl->getSurface();
  1084. ASSERT_EQ(NO_ERROR, s->unlockAndPost());
  1085. sFakeComposer->runVSyncAndWait();
  1086. }
  1087. void completeFGResize() {
  1088. fillSurfaceRGBA8(mFGSurfaceControl, RED);
  1089. sFakeComposer->runVSyncAndWait();
  1090. }
  1091. void restoreInitialState() {
  1092. TransactionScope ts(*sFakeComposer);
  1093. ts.setSize(mFGSurfaceControl, 64, 64);
  1094. ts.setPosition(mFGSurfaceControl, 64, 64);
  1095. ts.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
  1096. }
  1097. };
  1098. TEST_F(LatchingTest, SurfacePositionLatching) {
  1099. // By default position can be updated even while
  1100. // a resize is pending.
  1101. {
  1102. TransactionScope ts(*sFakeComposer);
  1103. ts.setSize(mFGSurfaceControl, 32, 32);
  1104. ts.setPosition(mFGSurfaceControl, 100, 100);
  1105. }
  1106. // The size should not have updated as we have not provided a new buffer.
  1107. auto referenceFrame1 = mBaseFrame;
  1108. referenceFrame1[FG_LAYER].mDisplayFrame = hwc_rect_t{100, 100, 100 + 64, 100 + 64};
  1109. EXPECT_TRUE(framesAreSame(referenceFrame1, sFakeComposer->getLatestFrame()));
  1110. restoreInitialState();
  1111. // Now we repeat with setGeometryAppliesWithResize
  1112. // and verify the position DOESN'T latch.
  1113. {
  1114. TransactionScope ts(*sFakeComposer);
  1115. ts.setGeometryAppliesWithResize(mFGSurfaceControl);
  1116. ts.setSize(mFGSurfaceControl, 32, 32);
  1117. ts.setPosition(mFGSurfaceControl, 100, 100);
  1118. }
  1119. EXPECT_TRUE(framesAreSame(mBaseFrame, sFakeComposer->getLatestFrame()));
  1120. completeFGResize();
  1121. auto referenceFrame2 = mBaseFrame;
  1122. referenceFrame2[FG_LAYER].mDisplayFrame = hwc_rect_t{100, 100, 100 + 32, 100 + 32};
  1123. referenceFrame2[FG_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 32.f, 32.f};
  1124. referenceFrame2[FG_LAYER].mSwapCount++;
  1125. EXPECT_TRUE(framesAreSame(referenceFrame2, sFakeComposer->getLatestFrame()));
  1126. }
  1127. TEST_F(LatchingTest, CropLatching) {
  1128. // Normally the crop applies immediately even while a resize is pending.
  1129. {
  1130. TransactionScope ts(*sFakeComposer);
  1131. ts.setSize(mFGSurfaceControl, 128, 128);
  1132. ts.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 63, 63));
  1133. }
  1134. auto referenceFrame1 = mBaseFrame;
  1135. referenceFrame1[FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 63, 64 + 63};
  1136. referenceFrame1[FG_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 63.f, 63.f};
  1137. EXPECT_TRUE(framesAreSame(referenceFrame1, sFakeComposer->getLatestFrame()));
  1138. restoreInitialState();
  1139. {
  1140. TransactionScope ts(*sFakeComposer);
  1141. ts.setSize(mFGSurfaceControl, 128, 128);
  1142. ts.setGeometryAppliesWithResize(mFGSurfaceControl);
  1143. ts.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 63, 63));
  1144. }
  1145. EXPECT_TRUE(framesAreSame(mBaseFrame, sFakeComposer->getLatestFrame()));
  1146. completeFGResize();
  1147. auto referenceFrame2 = mBaseFrame;
  1148. referenceFrame2[FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 63, 64 + 63};
  1149. referenceFrame2[FG_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 63.f, 63.f};
  1150. referenceFrame2[FG_LAYER].mSwapCount++;
  1151. EXPECT_TRUE(framesAreSame(referenceFrame2, sFakeComposer->getLatestFrame()));
  1152. }
  1153. } // namespace
  1154. int main(int argc, char** argv) {
  1155. ::testing::InitGoogleTest(&argc, argv);
  1156. sftest::FakeHwcEnvironment* fakeEnvironment = new sftest::FakeHwcEnvironment;
  1157. ::testing::AddGlobalTestEnvironment(fakeEnvironment);
  1158. ::testing::InitGoogleMock(&argc, argv);
  1159. return RUN_ALL_TESTS();
  1160. }