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