SurfaceInterceptor_test.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * Copyright (C) 2016 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. #include <frameworks/native/cmds/surfacereplayer/proto/src/trace.pb.h>
  17. #include <google/protobuf/io/zero_copy_stream_impl.h>
  18. #include <gtest/gtest.h>
  19. #include <android/native_window.h>
  20. #include <gui/ISurfaceComposer.h>
  21. #include <gui/LayerState.h>
  22. #include <gui/Surface.h>
  23. #include <gui/SurfaceComposerClient.h>
  24. #include <private/gui/ComposerService.h>
  25. #include <ui/DisplayInfo.h>
  26. #include <fstream>
  27. #include <random>
  28. #include <thread>
  29. namespace android {
  30. using Transaction = SurfaceComposerClient::Transaction;
  31. constexpr int32_t SCALING_UPDATE = 1;
  32. constexpr uint32_t BUFFER_UPDATES = 18;
  33. constexpr uint32_t LAYER_UPDATE = INT_MAX - 2;
  34. constexpr uint32_t SIZE_UPDATE = 134;
  35. constexpr uint32_t STACK_UPDATE = 1;
  36. constexpr uint64_t DEFERRED_UPDATE = 0;
  37. constexpr float ALPHA_UPDATE = 0.29f;
  38. constexpr float CORNER_RADIUS_UPDATE = 0.2f;
  39. constexpr float POSITION_UPDATE = 121;
  40. const Rect CROP_UPDATE(16, 16, 32, 32);
  41. const String8 DISPLAY_NAME("SurfaceInterceptor Display Test");
  42. constexpr auto TEST_SURFACE_NAME = "BG Interceptor Test Surface";
  43. constexpr auto UNIQUE_TEST_SURFACE_NAME = "BG Interceptor Test Surface#0";
  44. constexpr auto LAYER_NAME = "Layer Create and Delete Test";
  45. constexpr auto UNIQUE_LAYER_NAME = "Layer Create and Delete Test#0";
  46. constexpr auto DEFAULT_FILENAME = "/data/SurfaceTrace.dat";
  47. // Fill an RGBA_8888 formatted surface with a single color.
  48. static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b) {
  49. ANativeWindow_Buffer outBuffer;
  50. sp<Surface> s = sc->getSurface();
  51. ASSERT_TRUE(s != nullptr);
  52. ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
  53. uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
  54. for (int y = 0; y < outBuffer.height; y++) {
  55. for (int x = 0; x < outBuffer.width; x++) {
  56. uint8_t* pixel = img + (4 * (y*outBuffer.stride + x));
  57. pixel[0] = r;
  58. pixel[1] = g;
  59. pixel[2] = b;
  60. pixel[3] = 255;
  61. }
  62. }
  63. ASSERT_EQ(NO_ERROR, s->unlockAndPost());
  64. }
  65. static status_t readProtoFile(Trace* trace) {
  66. status_t err = NO_ERROR;
  67. int fd = open(DEFAULT_FILENAME, O_RDONLY);
  68. {
  69. google::protobuf::io::FileInputStream f(fd);
  70. if (fd && !trace->ParseFromZeroCopyStream(&f)) {
  71. err = PERMISSION_DENIED;
  72. }
  73. }
  74. close(fd);
  75. return err;
  76. }
  77. static void enableInterceptor() {
  78. system("service call SurfaceFlinger 1020 i32 1 > /dev/null");
  79. }
  80. static void disableInterceptor() {
  81. system("service call SurfaceFlinger 1020 i32 0 > /dev/null");
  82. }
  83. int32_t getSurfaceId(const Trace& capturedTrace, const std::string& surfaceName) {
  84. int32_t layerId = 0;
  85. for (const auto& increment : capturedTrace.increment()) {
  86. if (increment.increment_case() == increment.kSurfaceCreation) {
  87. if (increment.surface_creation().name() == surfaceName) {
  88. layerId = increment.surface_creation().id();
  89. }
  90. }
  91. }
  92. return layerId;
  93. }
  94. int32_t getDisplayId(const Trace& capturedTrace, const std::string& displayName) {
  95. int32_t displayId = 0;
  96. for (const auto& increment : capturedTrace.increment()) {
  97. if (increment.increment_case() == increment.kDisplayCreation) {
  98. if (increment.display_creation().name() == displayName) {
  99. displayId = increment.display_creation().id();
  100. break;
  101. }
  102. }
  103. }
  104. return displayId;
  105. }
  106. class SurfaceInterceptorTest : public ::testing::Test {
  107. protected:
  108. void SetUp() override {
  109. // Allow SurfaceInterceptor write to /data
  110. system("setenforce 0");
  111. mComposerClient = new SurfaceComposerClient;
  112. ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
  113. }
  114. void TearDown() override {
  115. mComposerClient->dispose();
  116. mBGSurfaceControl.clear();
  117. mComposerClient.clear();
  118. }
  119. sp<SurfaceComposerClient> mComposerClient;
  120. sp<SurfaceControl> mBGSurfaceControl;
  121. int32_t mBGLayerId;
  122. public:
  123. using TestTransactionAction = void (SurfaceInterceptorTest::*)(Transaction&);
  124. using TestAction = void (SurfaceInterceptorTest::*)();
  125. using TestBooleanVerification = bool (SurfaceInterceptorTest::*)(const Trace&);
  126. using TestVerification = void (SurfaceInterceptorTest::*)(const Trace&);
  127. void setupBackgroundSurface();
  128. void preProcessTrace(const Trace& trace);
  129. // captureTest will enable SurfaceInterceptor, setup background surface,
  130. // disable SurfaceInterceptor, collect the trace and process the trace for
  131. // id of background surface before further verification.
  132. void captureTest(TestTransactionAction action, TestBooleanVerification verification);
  133. void captureTest(TestTransactionAction action, SurfaceChange::SurfaceChangeCase changeCase);
  134. void captureTest(TestTransactionAction action, Increment::IncrementCase incrementCase);
  135. void captureTest(TestAction action, TestBooleanVerification verification);
  136. void captureTest(TestAction action, TestVerification verification);
  137. void runInTransaction(TestTransactionAction action);
  138. // Verification of changes to a surface
  139. bool positionUpdateFound(const SurfaceChange& change, bool foundPosition);
  140. bool sizeUpdateFound(const SurfaceChange& change, bool foundSize);
  141. bool alphaUpdateFound(const SurfaceChange& change, bool foundAlpha);
  142. bool layerUpdateFound(const SurfaceChange& change, bool foundLayer);
  143. bool cropUpdateFound(const SurfaceChange& change, bool foundCrop);
  144. bool cornerRadiusUpdateFound(const SurfaceChange& change, bool foundCornerRadius);
  145. bool matrixUpdateFound(const SurfaceChange& change, bool foundMatrix);
  146. bool scalingModeUpdateFound(const SurfaceChange& change, bool foundScalingMode);
  147. bool transparentRegionHintUpdateFound(const SurfaceChange& change, bool foundTransparentRegion);
  148. bool layerStackUpdateFound(const SurfaceChange& change, bool foundLayerStack);
  149. bool hiddenFlagUpdateFound(const SurfaceChange& change, bool foundHiddenFlag);
  150. bool opaqueFlagUpdateFound(const SurfaceChange& change, bool foundOpaqueFlag);
  151. bool secureFlagUpdateFound(const SurfaceChange& change, bool foundSecureFlag);
  152. bool deferredTransactionUpdateFound(const SurfaceChange& change, bool foundDeferred);
  153. bool surfaceUpdateFound(const Trace& trace, SurfaceChange::SurfaceChangeCase changeCase);
  154. // Find all of the updates in the single trace
  155. void assertAllUpdatesFound(const Trace& trace);
  156. // Verification of creation and deletion of a surface
  157. bool surfaceCreationFound(const Increment& increment, bool foundSurface);
  158. bool surfaceDeletionFound(const Increment& increment, const int32_t targetId,
  159. bool foundSurface);
  160. bool displayCreationFound(const Increment& increment, bool foundDisplay);
  161. bool displayDeletionFound(const Increment& increment, const int32_t targetId,
  162. bool foundDisplay);
  163. bool singleIncrementFound(const Trace& trace, Increment::IncrementCase incrementCase);
  164. // Verification of buffer updates
  165. bool bufferUpdatesFound(const Trace& trace);
  166. // Perform each of the possible changes to a surface
  167. void positionUpdate(Transaction&);
  168. void sizeUpdate(Transaction&);
  169. void alphaUpdate(Transaction&);
  170. void layerUpdate(Transaction&);
  171. void cropUpdate(Transaction&);
  172. void cornerRadiusUpdate(Transaction&);
  173. void matrixUpdate(Transaction&);
  174. void overrideScalingModeUpdate(Transaction&);
  175. void transparentRegionHintUpdate(Transaction&);
  176. void layerStackUpdate(Transaction&);
  177. void hiddenFlagUpdate(Transaction&);
  178. void opaqueFlagUpdate(Transaction&);
  179. void secureFlagUpdate(Transaction&);
  180. void deferredTransactionUpdate(Transaction&);
  181. void surfaceCreation(Transaction&);
  182. void displayCreation(Transaction&);
  183. void displayDeletion(Transaction&);
  184. void nBufferUpdates();
  185. void runAllUpdates();
  186. private:
  187. void captureInTransaction(TestTransactionAction action, Trace*);
  188. void capture(TestAction action, Trace*);
  189. };
  190. void SurfaceInterceptorTest::captureInTransaction(TestTransactionAction action, Trace* outTrace) {
  191. enableInterceptor();
  192. setupBackgroundSurface();
  193. runInTransaction(action);
  194. disableInterceptor();
  195. ASSERT_EQ(NO_ERROR, readProtoFile(outTrace));
  196. preProcessTrace(*outTrace);
  197. }
  198. void SurfaceInterceptorTest::capture(TestAction action, Trace* outTrace) {
  199. enableInterceptor();
  200. setupBackgroundSurface();
  201. (this->*action)();
  202. disableInterceptor();
  203. ASSERT_EQ(NO_ERROR, readProtoFile(outTrace));
  204. preProcessTrace(*outTrace);
  205. }
  206. void SurfaceInterceptorTest::setupBackgroundSurface() {
  207. const auto display = SurfaceComposerClient::getInternalDisplayToken();
  208. ASSERT_FALSE(display == nullptr);
  209. DisplayInfo info;
  210. ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(display, &info));
  211. ssize_t displayWidth = info.w;
  212. ssize_t displayHeight = info.h;
  213. // Background surface
  214. mBGSurfaceControl = mComposerClient->createSurface(
  215. String8(TEST_SURFACE_NAME), displayWidth, displayHeight,
  216. PIXEL_FORMAT_RGBA_8888, 0);
  217. ASSERT_TRUE(mBGSurfaceControl != nullptr);
  218. ASSERT_TRUE(mBGSurfaceControl->isValid());
  219. Transaction t;
  220. t.setDisplayLayerStack(display, 0);
  221. ASSERT_EQ(NO_ERROR, t.setLayer(mBGSurfaceControl, INT_MAX-3)
  222. .show(mBGSurfaceControl)
  223. .apply());
  224. }
  225. void SurfaceInterceptorTest::preProcessTrace(const Trace& trace) {
  226. mBGLayerId = getSurfaceId(trace, UNIQUE_TEST_SURFACE_NAME);
  227. }
  228. void SurfaceInterceptorTest::captureTest(TestTransactionAction action,
  229. TestBooleanVerification verification) {
  230. Trace capturedTrace;
  231. captureInTransaction(action, &capturedTrace);
  232. ASSERT_TRUE((this->*verification)(capturedTrace));
  233. }
  234. void SurfaceInterceptorTest::captureTest(TestTransactionAction action,
  235. Increment::IncrementCase incrementCase) {
  236. Trace capturedTrace;
  237. captureInTransaction(action, &capturedTrace);
  238. ASSERT_TRUE(singleIncrementFound(capturedTrace, incrementCase));
  239. }
  240. void SurfaceInterceptorTest::captureTest(TestTransactionAction action,
  241. SurfaceChange::SurfaceChangeCase changeCase) {
  242. Trace capturedTrace;
  243. captureInTransaction(action, &capturedTrace);
  244. ASSERT_TRUE(surfaceUpdateFound(capturedTrace, changeCase));
  245. }
  246. void SurfaceInterceptorTest::captureTest(TestAction action, TestBooleanVerification verification) {
  247. Trace capturedTrace;
  248. capture(action, &capturedTrace);
  249. ASSERT_TRUE((this->*verification)(capturedTrace));
  250. }
  251. void SurfaceInterceptorTest::captureTest(TestAction action, TestVerification verification) {
  252. Trace capturedTrace;
  253. capture(action, &capturedTrace);
  254. (this->*verification)(capturedTrace);
  255. }
  256. void SurfaceInterceptorTest::runInTransaction(TestTransactionAction action) {
  257. Transaction t;
  258. (this->*action)(t);
  259. t.apply(true);
  260. }
  261. void SurfaceInterceptorTest::positionUpdate(Transaction& t) {
  262. t.setPosition(mBGSurfaceControl, POSITION_UPDATE, POSITION_UPDATE);
  263. }
  264. void SurfaceInterceptorTest::sizeUpdate(Transaction& t) {
  265. t.setSize(mBGSurfaceControl, SIZE_UPDATE, SIZE_UPDATE);
  266. }
  267. void SurfaceInterceptorTest::alphaUpdate(Transaction& t) {
  268. t.setAlpha(mBGSurfaceControl, ALPHA_UPDATE);
  269. }
  270. void SurfaceInterceptorTest::cornerRadiusUpdate(Transaction& t) {
  271. t.setCornerRadius(mBGSurfaceControl, CORNER_RADIUS_UPDATE);
  272. }
  273. void SurfaceInterceptorTest::layerUpdate(Transaction& t) {
  274. t.setLayer(mBGSurfaceControl, LAYER_UPDATE);
  275. }
  276. void SurfaceInterceptorTest::cropUpdate(Transaction& t) {
  277. t.setCrop_legacy(mBGSurfaceControl, CROP_UPDATE);
  278. }
  279. void SurfaceInterceptorTest::matrixUpdate(Transaction& t) {
  280. t.setMatrix(mBGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
  281. }
  282. void SurfaceInterceptorTest::overrideScalingModeUpdate(Transaction& t) {
  283. t.setOverrideScalingMode(mBGSurfaceControl, SCALING_UPDATE);
  284. }
  285. void SurfaceInterceptorTest::transparentRegionHintUpdate(Transaction& t) {
  286. Region region(CROP_UPDATE);
  287. t.setTransparentRegionHint(mBGSurfaceControl, region);
  288. }
  289. void SurfaceInterceptorTest::layerStackUpdate(Transaction& t) {
  290. t.setLayerStack(mBGSurfaceControl, STACK_UPDATE);
  291. }
  292. void SurfaceInterceptorTest::hiddenFlagUpdate(Transaction& t) {
  293. t.setFlags(mBGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
  294. }
  295. void SurfaceInterceptorTest::opaqueFlagUpdate(Transaction& t) {
  296. t.setFlags(mBGSurfaceControl, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque);
  297. }
  298. void SurfaceInterceptorTest::secureFlagUpdate(Transaction& t) {
  299. t.setFlags(mBGSurfaceControl, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
  300. }
  301. void SurfaceInterceptorTest::deferredTransactionUpdate(Transaction& t) {
  302. t.deferTransactionUntil_legacy(mBGSurfaceControl, mBGSurfaceControl->getHandle(),
  303. DEFERRED_UPDATE);
  304. }
  305. void SurfaceInterceptorTest::displayCreation(Transaction&) {
  306. sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, true);
  307. SurfaceComposerClient::destroyDisplay(testDisplay);
  308. }
  309. void SurfaceInterceptorTest::displayDeletion(Transaction&) {
  310. sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, false);
  311. SurfaceComposerClient::destroyDisplay(testDisplay);
  312. }
  313. void SurfaceInterceptorTest::runAllUpdates() {
  314. runInTransaction(&SurfaceInterceptorTest::positionUpdate);
  315. runInTransaction(&SurfaceInterceptorTest::sizeUpdate);
  316. runInTransaction(&SurfaceInterceptorTest::alphaUpdate);
  317. runInTransaction(&SurfaceInterceptorTest::cornerRadiusUpdate);
  318. runInTransaction(&SurfaceInterceptorTest::layerUpdate);
  319. runInTransaction(&SurfaceInterceptorTest::cropUpdate);
  320. runInTransaction(&SurfaceInterceptorTest::matrixUpdate);
  321. runInTransaction(&SurfaceInterceptorTest::overrideScalingModeUpdate);
  322. runInTransaction(&SurfaceInterceptorTest::transparentRegionHintUpdate);
  323. runInTransaction(&SurfaceInterceptorTest::layerStackUpdate);
  324. runInTransaction(&SurfaceInterceptorTest::hiddenFlagUpdate);
  325. runInTransaction(&SurfaceInterceptorTest::opaqueFlagUpdate);
  326. runInTransaction(&SurfaceInterceptorTest::secureFlagUpdate);
  327. runInTransaction(&SurfaceInterceptorTest::deferredTransactionUpdate);
  328. }
  329. void SurfaceInterceptorTest::surfaceCreation(Transaction&) {
  330. mComposerClient->createSurface(String8(LAYER_NAME), SIZE_UPDATE, SIZE_UPDATE,
  331. PIXEL_FORMAT_RGBA_8888, 0);
  332. }
  333. void SurfaceInterceptorTest::nBufferUpdates() {
  334. std::random_device rd;
  335. std::mt19937_64 gen(rd());
  336. // This makes testing fun
  337. std::uniform_int_distribution<uint8_t> dis;
  338. for (uint32_t i = 0; i < BUFFER_UPDATES; ++i) {
  339. fillSurfaceRGBA8(mBGSurfaceControl, dis(gen), dis(gen), dis(gen));
  340. }
  341. }
  342. bool SurfaceInterceptorTest::positionUpdateFound(const SurfaceChange& change, bool foundPosition) {
  343. // There should only be one position transaction with x and y = POSITION_UPDATE
  344. bool hasX(change.position().x() == POSITION_UPDATE);
  345. bool hasY(change.position().y() == POSITION_UPDATE);
  346. if (hasX && hasY && !foundPosition) {
  347. foundPosition = true;
  348. } else if (hasX && hasY && foundPosition) {
  349. // Failed because the position update was found a second time
  350. [] () { FAIL(); }();
  351. }
  352. return foundPosition;
  353. }
  354. bool SurfaceInterceptorTest::sizeUpdateFound(const SurfaceChange& change, bool foundSize) {
  355. bool hasWidth(change.size().h() == SIZE_UPDATE);
  356. bool hasHeight(change.size().w() == SIZE_UPDATE);
  357. if (hasWidth && hasHeight && !foundSize) {
  358. foundSize = true;
  359. } else if (hasWidth && hasHeight && foundSize) {
  360. [] () { FAIL(); }();
  361. }
  362. return foundSize;
  363. }
  364. bool SurfaceInterceptorTest::alphaUpdateFound(const SurfaceChange& change, bool foundAlpha) {
  365. bool hasAlpha(change.alpha().alpha() == ALPHA_UPDATE);
  366. if (hasAlpha && !foundAlpha) {
  367. foundAlpha = true;
  368. } else if (hasAlpha && foundAlpha) {
  369. [] () { FAIL(); }();
  370. }
  371. return foundAlpha;
  372. }
  373. bool SurfaceInterceptorTest::cornerRadiusUpdateFound(const SurfaceChange &change,
  374. bool foundCornerRadius) {
  375. bool hasCornerRadius(change.corner_radius().corner_radius() == CORNER_RADIUS_UPDATE);
  376. if (hasCornerRadius && !foundCornerRadius) {
  377. foundCornerRadius = true;
  378. } else if (hasCornerRadius && foundCornerRadius) {
  379. [] () { FAIL(); }();
  380. }
  381. return foundCornerRadius;
  382. }
  383. bool SurfaceInterceptorTest::layerUpdateFound(const SurfaceChange& change, bool foundLayer) {
  384. bool hasLayer(change.layer().layer() == LAYER_UPDATE);
  385. if (hasLayer && !foundLayer) {
  386. foundLayer = true;
  387. } else if (hasLayer && foundLayer) {
  388. [] () { FAIL(); }();
  389. }
  390. return foundLayer;
  391. }
  392. bool SurfaceInterceptorTest::cropUpdateFound(const SurfaceChange& change, bool foundCrop) {
  393. bool hasLeft(change.crop().rectangle().left() == CROP_UPDATE.left);
  394. bool hasTop(change.crop().rectangle().top() == CROP_UPDATE.top);
  395. bool hasRight(change.crop().rectangle().right() == CROP_UPDATE.right);
  396. bool hasBottom(change.crop().rectangle().bottom() == CROP_UPDATE.bottom);
  397. if (hasLeft && hasRight && hasTop && hasBottom && !foundCrop) {
  398. foundCrop = true;
  399. } else if (hasLeft && hasRight && hasTop && hasBottom && foundCrop) {
  400. [] () { FAIL(); }();
  401. }
  402. return foundCrop;
  403. }
  404. bool SurfaceInterceptorTest::matrixUpdateFound(const SurfaceChange& change, bool foundMatrix) {
  405. bool hasSx((float)change.matrix().dsdx() == (float)M_SQRT1_2);
  406. bool hasTx((float)change.matrix().dtdx() == (float)M_SQRT1_2);
  407. bool hasSy((float)change.matrix().dsdy() == (float)M_SQRT1_2);
  408. bool hasTy((float)change.matrix().dtdy() == (float)-M_SQRT1_2);
  409. if (hasSx && hasTx && hasSy && hasTy && !foundMatrix) {
  410. foundMatrix = true;
  411. } else if (hasSx && hasTx && hasSy && hasTy && foundMatrix) {
  412. [] () { FAIL(); }();
  413. }
  414. return foundMatrix;
  415. }
  416. bool SurfaceInterceptorTest::scalingModeUpdateFound(const SurfaceChange& change,
  417. bool foundScalingMode) {
  418. bool hasScalingUpdate(change.override_scaling_mode().override_scaling_mode() == SCALING_UPDATE);
  419. if (hasScalingUpdate && !foundScalingMode) {
  420. foundScalingMode = true;
  421. } else if (hasScalingUpdate && foundScalingMode) {
  422. [] () { FAIL(); }();
  423. }
  424. return foundScalingMode;
  425. }
  426. bool SurfaceInterceptorTest::transparentRegionHintUpdateFound(const SurfaceChange& change,
  427. bool foundTransparentRegion) {
  428. auto traceRegion = change.transparent_region_hint().region(0);
  429. bool hasLeft(traceRegion.left() == CROP_UPDATE.left);
  430. bool hasTop(traceRegion.top() == CROP_UPDATE.top);
  431. bool hasRight(traceRegion.right() == CROP_UPDATE.right);
  432. bool hasBottom(traceRegion.bottom() == CROP_UPDATE.bottom);
  433. if (hasLeft && hasRight && hasTop && hasBottom && !foundTransparentRegion) {
  434. foundTransparentRegion = true;
  435. } else if (hasLeft && hasRight && hasTop && hasBottom && foundTransparentRegion) {
  436. [] () { FAIL(); }();
  437. }
  438. return foundTransparentRegion;
  439. }
  440. bool SurfaceInterceptorTest::layerStackUpdateFound(const SurfaceChange& change,
  441. bool foundLayerStack) {
  442. bool hasLayerStackUpdate(change.layer_stack().layer_stack() == STACK_UPDATE);
  443. if (hasLayerStackUpdate && !foundLayerStack) {
  444. foundLayerStack = true;
  445. } else if (hasLayerStackUpdate && foundLayerStack) {
  446. [] () { FAIL(); }();
  447. }
  448. return foundLayerStack;
  449. }
  450. bool SurfaceInterceptorTest::hiddenFlagUpdateFound(const SurfaceChange& change,
  451. bool foundHiddenFlag) {
  452. bool hasHiddenFlag(change.hidden_flag().hidden_flag());
  453. if (hasHiddenFlag && !foundHiddenFlag) {
  454. foundHiddenFlag = true;
  455. } else if (hasHiddenFlag && foundHiddenFlag) {
  456. [] () { FAIL(); }();
  457. }
  458. return foundHiddenFlag;
  459. }
  460. bool SurfaceInterceptorTest::opaqueFlagUpdateFound(const SurfaceChange& change,
  461. bool foundOpaqueFlag) {
  462. bool hasOpaqueFlag(change.opaque_flag().opaque_flag());
  463. if (hasOpaqueFlag && !foundOpaqueFlag) {
  464. foundOpaqueFlag = true;
  465. } else if (hasOpaqueFlag && foundOpaqueFlag) {
  466. [] () { FAIL(); }();
  467. }
  468. return foundOpaqueFlag;
  469. }
  470. bool SurfaceInterceptorTest::secureFlagUpdateFound(const SurfaceChange& change,
  471. bool foundSecureFlag) {
  472. bool hasSecureFlag(change.secure_flag().secure_flag());
  473. if (hasSecureFlag && !foundSecureFlag) {
  474. foundSecureFlag = true;
  475. } else if (hasSecureFlag && foundSecureFlag) {
  476. [] () { FAIL(); }();
  477. }
  478. return foundSecureFlag;
  479. }
  480. bool SurfaceInterceptorTest::deferredTransactionUpdateFound(const SurfaceChange& change,
  481. bool foundDeferred) {
  482. bool hasId(change.deferred_transaction().layer_id() == mBGLayerId);
  483. bool hasFrameNumber(change.deferred_transaction().frame_number() == DEFERRED_UPDATE);
  484. if (hasId && hasFrameNumber && !foundDeferred) {
  485. foundDeferred = true;
  486. } else if (hasId && hasFrameNumber && foundDeferred) {
  487. [] () { FAIL(); }();
  488. }
  489. return foundDeferred;
  490. }
  491. bool SurfaceInterceptorTest::surfaceUpdateFound(const Trace& trace,
  492. SurfaceChange::SurfaceChangeCase changeCase) {
  493. bool foundUpdate = false;
  494. for (const auto& increment : trace.increment()) {
  495. if (increment.increment_case() == increment.kTransaction) {
  496. for (const auto& change : increment.transaction().surface_change()) {
  497. if (change.id() == mBGLayerId && change.SurfaceChange_case() == changeCase) {
  498. switch (changeCase) {
  499. case SurfaceChange::SurfaceChangeCase::kPosition:
  500. // foundUpdate is sent for the tests to fail on duplicated increments
  501. foundUpdate = positionUpdateFound(change, foundUpdate);
  502. break;
  503. case SurfaceChange::SurfaceChangeCase::kSize:
  504. foundUpdate = sizeUpdateFound(change, foundUpdate);
  505. break;
  506. case SurfaceChange::SurfaceChangeCase::kAlpha:
  507. foundUpdate = alphaUpdateFound(change, foundUpdate);
  508. break;
  509. case SurfaceChange::SurfaceChangeCase::kLayer:
  510. foundUpdate = layerUpdateFound(change, foundUpdate);
  511. break;
  512. case SurfaceChange::SurfaceChangeCase::kCrop:
  513. foundUpdate = cropUpdateFound(change, foundUpdate);
  514. break;
  515. case SurfaceChange::SurfaceChangeCase::kCornerRadius:
  516. foundUpdate = cornerRadiusUpdateFound(change, foundUpdate);
  517. break;
  518. case SurfaceChange::SurfaceChangeCase::kMatrix:
  519. foundUpdate = matrixUpdateFound(change, foundUpdate);
  520. break;
  521. case SurfaceChange::SurfaceChangeCase::kOverrideScalingMode:
  522. foundUpdate = scalingModeUpdateFound(change, foundUpdate);
  523. break;
  524. case SurfaceChange::SurfaceChangeCase::kTransparentRegionHint:
  525. foundUpdate = transparentRegionHintUpdateFound(change, foundUpdate);
  526. break;
  527. case SurfaceChange::SurfaceChangeCase::kLayerStack:
  528. foundUpdate = layerStackUpdateFound(change, foundUpdate);
  529. break;
  530. case SurfaceChange::SurfaceChangeCase::kHiddenFlag:
  531. foundUpdate = hiddenFlagUpdateFound(change, foundUpdate);
  532. break;
  533. case SurfaceChange::SurfaceChangeCase::kOpaqueFlag:
  534. foundUpdate = opaqueFlagUpdateFound(change, foundUpdate);
  535. break;
  536. case SurfaceChange::SurfaceChangeCase::kSecureFlag:
  537. foundUpdate = secureFlagUpdateFound(change, foundUpdate);
  538. break;
  539. case SurfaceChange::SurfaceChangeCase::kDeferredTransaction:
  540. foundUpdate = deferredTransactionUpdateFound(change, foundUpdate);
  541. break;
  542. case SurfaceChange::SurfaceChangeCase::SURFACECHANGE_NOT_SET:
  543. break;
  544. }
  545. }
  546. }
  547. }
  548. }
  549. return foundUpdate;
  550. }
  551. void SurfaceInterceptorTest::assertAllUpdatesFound(const Trace& trace) {
  552. ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kPosition));
  553. ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kSize));
  554. ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kAlpha));
  555. ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kLayer));
  556. ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kCrop));
  557. ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kMatrix));
  558. ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kOverrideScalingMode));
  559. ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kTransparentRegionHint));
  560. ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kLayerStack));
  561. ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kHiddenFlag));
  562. ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kOpaqueFlag));
  563. ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kSecureFlag));
  564. ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kDeferredTransaction));
  565. }
  566. bool SurfaceInterceptorTest::surfaceCreationFound(const Increment& increment, bool foundSurface) {
  567. bool isMatch(increment.surface_creation().name() == UNIQUE_LAYER_NAME &&
  568. increment.surface_creation().w() == SIZE_UPDATE &&
  569. increment.surface_creation().h() == SIZE_UPDATE);
  570. if (isMatch && !foundSurface) {
  571. foundSurface = true;
  572. } else if (isMatch && foundSurface) {
  573. [] () { FAIL(); }();
  574. }
  575. return foundSurface;
  576. }
  577. bool SurfaceInterceptorTest::surfaceDeletionFound(const Increment& increment,
  578. const int32_t targetId, bool foundSurface) {
  579. bool isMatch(increment.surface_deletion().id() == targetId);
  580. if (isMatch && !foundSurface) {
  581. foundSurface = true;
  582. } else if (isMatch && foundSurface) {
  583. [] () { FAIL(); }();
  584. }
  585. return foundSurface;
  586. }
  587. bool SurfaceInterceptorTest::displayCreationFound(const Increment& increment, bool foundDisplay) {
  588. bool isMatch(increment.display_creation().name() == DISPLAY_NAME.string() &&
  589. increment.display_creation().is_secure());
  590. if (isMatch && !foundDisplay) {
  591. foundDisplay = true;
  592. } else if (isMatch && foundDisplay) {
  593. [] () { FAIL(); }();
  594. }
  595. return foundDisplay;
  596. }
  597. bool SurfaceInterceptorTest::displayDeletionFound(const Increment& increment,
  598. const int32_t targetId, bool foundDisplay) {
  599. bool isMatch(increment.display_deletion().id() == targetId);
  600. if (isMatch && !foundDisplay) {
  601. foundDisplay = true;
  602. } else if (isMatch && foundDisplay) {
  603. [] () { FAIL(); }();
  604. }
  605. return foundDisplay;
  606. }
  607. bool SurfaceInterceptorTest::singleIncrementFound(const Trace& trace,
  608. Increment::IncrementCase incrementCase) {
  609. bool foundIncrement = false;
  610. for (const auto& increment : trace.increment()) {
  611. if (increment.increment_case() == incrementCase) {
  612. int32_t targetId = 0;
  613. switch (incrementCase) {
  614. case Increment::IncrementCase::kSurfaceCreation:
  615. foundIncrement = surfaceCreationFound(increment, foundIncrement);
  616. break;
  617. case Increment::IncrementCase::kSurfaceDeletion:
  618. // Find the id of created surface.
  619. targetId = getSurfaceId(trace, UNIQUE_LAYER_NAME);
  620. foundIncrement = surfaceDeletionFound(increment, targetId, foundIncrement);
  621. break;
  622. case Increment::IncrementCase::kDisplayCreation:
  623. foundIncrement = displayCreationFound(increment, foundIncrement);
  624. break;
  625. case Increment::IncrementCase::kDisplayDeletion:
  626. // Find the id of created display.
  627. targetId = getDisplayId(trace, DISPLAY_NAME.string());
  628. foundIncrement = displayDeletionFound(increment, targetId, foundIncrement);
  629. break;
  630. default:
  631. /* code */
  632. break;
  633. }
  634. }
  635. }
  636. return foundIncrement;
  637. }
  638. bool SurfaceInterceptorTest::bufferUpdatesFound(const Trace& trace) {
  639. uint32_t updates = 0;
  640. for (const auto& inc : trace.increment()) {
  641. if (inc.increment_case() == inc.kBufferUpdate && inc.buffer_update().id() == mBGLayerId) {
  642. updates++;
  643. }
  644. }
  645. return updates == BUFFER_UPDATES;
  646. }
  647. TEST_F(SurfaceInterceptorTest, InterceptPositionUpdateWorks) {
  648. captureTest(&SurfaceInterceptorTest::positionUpdate,
  649. SurfaceChange::SurfaceChangeCase::kPosition);
  650. }
  651. TEST_F(SurfaceInterceptorTest, InterceptSizeUpdateWorks) {
  652. captureTest(&SurfaceInterceptorTest::sizeUpdate, SurfaceChange::SurfaceChangeCase::kSize);
  653. }
  654. TEST_F(SurfaceInterceptorTest, InterceptAlphaUpdateWorks) {
  655. captureTest(&SurfaceInterceptorTest::alphaUpdate, SurfaceChange::SurfaceChangeCase::kAlpha);
  656. }
  657. TEST_F(SurfaceInterceptorTest, InterceptLayerUpdateWorks) {
  658. captureTest(&SurfaceInterceptorTest::layerUpdate, SurfaceChange::SurfaceChangeCase::kLayer);
  659. }
  660. TEST_F(SurfaceInterceptorTest, InterceptCropUpdateWorks) {
  661. captureTest(&SurfaceInterceptorTest::cropUpdate, SurfaceChange::SurfaceChangeCase::kCrop);
  662. }
  663. TEST_F(SurfaceInterceptorTest, InterceptCornerRadiusUpdateWorks) {
  664. captureTest(&SurfaceInterceptorTest::cornerRadiusUpdate,
  665. SurfaceChange::SurfaceChangeCase::kCornerRadius);
  666. }
  667. TEST_F(SurfaceInterceptorTest, InterceptMatrixUpdateWorks) {
  668. captureTest(&SurfaceInterceptorTest::matrixUpdate, SurfaceChange::SurfaceChangeCase::kMatrix);
  669. }
  670. TEST_F(SurfaceInterceptorTest, InterceptOverrideScalingModeUpdateWorks) {
  671. captureTest(&SurfaceInterceptorTest::overrideScalingModeUpdate,
  672. SurfaceChange::SurfaceChangeCase::kOverrideScalingMode);
  673. }
  674. TEST_F(SurfaceInterceptorTest, InterceptTransparentRegionHintUpdateWorks) {
  675. captureTest(&SurfaceInterceptorTest::transparentRegionHintUpdate,
  676. SurfaceChange::SurfaceChangeCase::kTransparentRegionHint);
  677. }
  678. TEST_F(SurfaceInterceptorTest, InterceptLayerStackUpdateWorks) {
  679. captureTest(&SurfaceInterceptorTest::layerStackUpdate,
  680. SurfaceChange::SurfaceChangeCase::kLayerStack);
  681. }
  682. TEST_F(SurfaceInterceptorTest, InterceptHiddenFlagUpdateWorks) {
  683. captureTest(&SurfaceInterceptorTest::hiddenFlagUpdate,
  684. SurfaceChange::SurfaceChangeCase::kHiddenFlag);
  685. }
  686. TEST_F(SurfaceInterceptorTest, InterceptOpaqueFlagUpdateWorks) {
  687. captureTest(&SurfaceInterceptorTest::opaqueFlagUpdate,
  688. SurfaceChange::SurfaceChangeCase::kOpaqueFlag);
  689. }
  690. TEST_F(SurfaceInterceptorTest, InterceptSecureFlagUpdateWorks) {
  691. captureTest(&SurfaceInterceptorTest::secureFlagUpdate,
  692. SurfaceChange::SurfaceChangeCase::kSecureFlag);
  693. }
  694. TEST_F(SurfaceInterceptorTest, InterceptDeferredTransactionUpdateWorks) {
  695. captureTest(&SurfaceInterceptorTest::deferredTransactionUpdate,
  696. SurfaceChange::SurfaceChangeCase::kDeferredTransaction);
  697. }
  698. TEST_F(SurfaceInterceptorTest, InterceptAllUpdatesWorks) {
  699. captureTest(&SurfaceInterceptorTest::runAllUpdates,
  700. &SurfaceInterceptorTest::assertAllUpdatesFound);
  701. }
  702. TEST_F(SurfaceInterceptorTest, InterceptSurfaceCreationWorks) {
  703. captureTest(&SurfaceInterceptorTest::surfaceCreation,
  704. Increment::IncrementCase::kSurfaceCreation);
  705. }
  706. TEST_F(SurfaceInterceptorTest, InterceptDisplayCreationWorks) {
  707. captureTest(&SurfaceInterceptorTest::displayCreation,
  708. Increment::IncrementCase::kDisplayCreation);
  709. }
  710. TEST_F(SurfaceInterceptorTest, InterceptDisplayDeletionWorks) {
  711. enableInterceptor();
  712. runInTransaction(&SurfaceInterceptorTest::displayDeletion);
  713. disableInterceptor();
  714. Trace capturedTrace;
  715. ASSERT_EQ(NO_ERROR, readProtoFile(&capturedTrace));
  716. ASSERT_TRUE(singleIncrementFound(capturedTrace, Increment::IncrementCase::kDisplayDeletion));
  717. }
  718. TEST_F(SurfaceInterceptorTest, InterceptBufferUpdateWorks) {
  719. captureTest(&SurfaceInterceptorTest::nBufferUpdates,
  720. &SurfaceInterceptorTest::bufferUpdatesFound);
  721. }
  722. // If the interceptor is enabled while buffer updates are being pushed, the interceptor should
  723. // first create a snapshot of the existing displays and surfaces and then start capturing
  724. // the buffer updates
  725. TEST_F(SurfaceInterceptorTest, InterceptWhileBufferUpdatesWorks) {
  726. setupBackgroundSurface();
  727. std::thread bufferUpdates(&SurfaceInterceptorTest::nBufferUpdates, this);
  728. enableInterceptor();
  729. disableInterceptor();
  730. bufferUpdates.join();
  731. Trace capturedTrace;
  732. ASSERT_EQ(NO_ERROR, readProtoFile(&capturedTrace));
  733. const auto& firstIncrement = capturedTrace.mutable_increment(0);
  734. ASSERT_EQ(firstIncrement->increment_case(), Increment::IncrementCase::kDisplayCreation);
  735. }
  736. TEST_F(SurfaceInterceptorTest, InterceptSimultaneousUpdatesWorks) {
  737. enableInterceptor();
  738. setupBackgroundSurface();
  739. std::thread bufferUpdates(&SurfaceInterceptorTest::nBufferUpdates, this);
  740. std::thread surfaceUpdates(&SurfaceInterceptorTest::runAllUpdates, this);
  741. runInTransaction(&SurfaceInterceptorTest::surfaceCreation);
  742. bufferUpdates.join();
  743. surfaceUpdates.join();
  744. disableInterceptor();
  745. Trace capturedTrace;
  746. ASSERT_EQ(NO_ERROR, readProtoFile(&capturedTrace));
  747. preProcessTrace(capturedTrace);
  748. assertAllUpdatesFound(capturedTrace);
  749. ASSERT_TRUE(bufferUpdatesFound(capturedTrace));
  750. ASSERT_TRUE(singleIncrementFound(capturedTrace, Increment::IncrementCase::kSurfaceCreation));
  751. }
  752. }