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