1 /*
2  * Copyright (C) 2019 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 <sys/epoll.h>
18 
19 #include <gui/DisplayEventReceiver.h>
20 
21 #include <gui/test/CallbackUtils.h>
22 #include "LayerTransactionTest.h"
23 
24 using namespace std::chrono_literals;
25 
26 namespace android {
27 
28 using android::hardware::graphics::common::V1_1::BufferUsage;
29 using SCHash = SurfaceComposerClient::SCHash;
30 
31 ::testing::Environment* const binderEnv =
32         ::testing::AddGlobalTestEnvironment(new BinderEnvironment());
33 
34 class LayerCallbackTest : public LayerTransactionTest {
35 public:
SetUp()36     void SetUp() override {
37         LayerTransactionTest::SetUp();
38 
39         EXPECT_EQ(NO_ERROR, mDisplayEventReceiver.initCheck());
40 
41         mEpollFd = epoll_create1(EPOLL_CLOEXEC);
42         EXPECT_GT(mEpollFd, 1);
43 
44         epoll_event event;
45         event.events = EPOLLIN;
46         EXPECT_EQ(0, epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mDisplayEventReceiver.getFd(), &event));
47     }
48 
TearDown()49     void TearDown() override {
50         close(mEpollFd);
51         LayerTransactionTest::TearDown();
52     }
53 
createLayerWithBuffer()54     virtual sp<SurfaceControl> createLayerWithBuffer() {
55         return createLayer(mClient, "test", 0, 0, ISurfaceComposerClient::eFXSurfaceBufferState);
56     }
57 
fillBuffer(Transaction & transaction,const sp<SurfaceControl> & layer,bool setBuffer=true,bool setBackgroundColor=false)58     static int fillBuffer(Transaction& transaction, const sp<SurfaceControl>& layer,
59                           bool setBuffer = true, bool setBackgroundColor = false) {
60         sp<GraphicBuffer> buffer;
61         sp<Fence> fence;
62         if (setBuffer) {
63             int err = getBuffer(&buffer, &fence);
64             if (err != NO_ERROR) {
65                 return err;
66             }
67 
68             transaction.setBuffer(layer, buffer, fence);
69         }
70 
71         if (setBackgroundColor) {
72             transaction.setBackgroundColor(layer, /*color*/ half3(1.0f, 0, 0), /*alpha*/ 1.0f,
73                                            ui::Dataspace::UNKNOWN);
74         }
75 
76         return NO_ERROR;
77     }
78 
fillTransaction(Transaction & transaction,CallbackHelper * callbackHelper,const sp<SurfaceControl> & layer=nullptr,bool setBuffer=true,bool setBackgroundColor=false)79     static int fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
80                                const sp<SurfaceControl>& layer = nullptr, bool setBuffer = true,
81                                bool setBackgroundColor = false) {
82         if (layer) {
83             int err = fillBuffer(transaction, layer, setBuffer, setBackgroundColor);
84             if (err != NO_ERROR) {
85                 return err;
86             }
87         }
88 
89         transaction.addTransactionCompletedCallback(callbackHelper->function,
90                                                     callbackHelper->getContext());
91         return NO_ERROR;
92     }
93 
waitForCallback(CallbackHelper & helper,const ExpectedResult & expectedResult,bool finalState=false)94     static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
95                                 bool finalState = false) {
96         CallbackData callbackData;
97         ASSERT_NO_FATAL_FAILURE(helper.getCallbackData(&callbackData));
98         EXPECT_NO_FATAL_FAILURE(expectedResult.verifyCallbackData(callbackData));
99 
100         if (finalState) {
101             ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
102         }
103     }
104 
waitForCallbacks(CallbackHelper & helper,const std::vector<ExpectedResult> & expectedResults,bool finalState=false)105     static void waitForCallbacks(CallbackHelper& helper,
106                                  const std::vector<ExpectedResult>& expectedResults,
107                                  bool finalState = false) {
108         for (const auto& expectedResult : expectedResults) {
109             waitForCallback(helper, expectedResult);
110         }
111         if (finalState) {
112             ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
113         }
114     }
115 
waitForCommitCallback(CallbackHelper & helper,const std::unordered_set<sp<SurfaceControl>,SCHash> & committedSc)116     static void waitForCommitCallback(
117             CallbackHelper& helper,
118             const std::unordered_set<sp<SurfaceControl>, SCHash>& committedSc) {
119         CallbackData callbackData;
120         ASSERT_NO_FATAL_FAILURE(helper.getCallbackData(&callbackData));
121 
122         const auto& surfaceControlStats = callbackData.surfaceControlStats;
123 
124         ASSERT_EQ(surfaceControlStats.size(), committedSc.size()) << "wrong number of surfaces";
125 
126         for (const auto& stats : surfaceControlStats) {
127             ASSERT_NE(stats.surfaceControl, nullptr) << "returned null surface control";
128 
129             const auto& expectedSc = committedSc.find(stats.surfaceControl);
130             ASSERT_NE(expectedSc, committedSc.end()) << "unexpected surface control";
131         }
132     }
133 
134     DisplayEventReceiver mDisplayEventReceiver;
135     int mEpollFd;
136 
137     struct Vsync {
138         int64_t vsyncId = FrameTimelineInfo::INVALID_VSYNC_ID;
139         nsecs_t expectedPresentTime = std::numeric_limits<nsecs_t>::max();
140     };
141 
waitForNextVsync()142     Vsync waitForNextVsync() {
143         mDisplayEventReceiver.requestNextVsync();
144         epoll_event epollEvent;
145         Vsync vsync;
146         EXPECT_EQ(1, epoll_wait(mEpollFd, &epollEvent, 1, 1000))
147                 << "Timeout waiting for vsync event";
148         DisplayEventReceiver::Event event;
149         while (mDisplayEventReceiver.getEvents(&event, 1) > 0) {
150             if (event.header.type != DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
151                 continue;
152             }
153 
154             vsync = {event.vsync.vsyncData.preferredVsyncId(),
155                      event.vsync.vsyncData.preferredExpectedPresentationTime()};
156         }
157 
158         EXPECT_GE(vsync.vsyncId, 1);
159         EXPECT_GT(vsync.expectedPresentTime, systemTime());
160 
161         return vsync;
162     }
163 };
164 
TEST_F(LayerCallbackTest,BufferColor)165 TEST_F(LayerCallbackTest, BufferColor) {
166     sp<SurfaceControl> layer;
167     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
168 
169     Transaction transaction;
170     CallbackHelper callback;
171     int err = fillTransaction(transaction, &callback, layer, true, true);
172     if (err) {
173         GTEST_SUCCEED() << "test not supported";
174         return;
175     }
176 
177     transaction.apply();
178 
179     ExpectedResult expected;
180     expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
181     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
182 }
183 
TEST_F(LayerCallbackTest,NoBufferNoColor)184 TEST_F(LayerCallbackTest, NoBufferNoColor) {
185     sp<SurfaceControl> layer;
186     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
187 
188     Transaction transaction;
189     CallbackHelper callback;
190     int err = fillTransaction(transaction, &callback, layer, false, false);
191     if (err) {
192         GTEST_SUCCEED() << "test not supported";
193         return;
194     }
195 
196     ui::Size bufferSize = getBufferSize();
197     TransactionUtils::setFrame(transaction, layer, Rect(0, 0, bufferSize.width, bufferSize.height),
198                                Rect(0, 0, 32, 32));
199     transaction.apply();
200 
201     ExpectedResult expected;
202     expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
203                         ExpectedResult::Buffer::NOT_ACQUIRED);
204     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
205 }
206 
TEST_F(LayerCallbackTest,BufferNoColor)207 TEST_F(LayerCallbackTest, BufferNoColor) {
208     sp<SurfaceControl> layer;
209     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
210 
211     Transaction transaction;
212     CallbackHelper callback;
213     int err = fillTransaction(transaction, &callback, layer, true, false);
214     if (err) {
215         GTEST_SUCCEED() << "test not supported";
216         return;
217     }
218 
219     ui::Size bufferSize = getBufferSize();
220     TransactionUtils::setFrame(transaction, layer, Rect(0, 0, bufferSize.width, bufferSize.height),
221                                Rect(0, 0, 32, 32));
222     transaction.apply();
223 
224     ExpectedResult expected;
225     expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
226     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
227 }
228 
TEST_F(LayerCallbackTest,NoBufferColor)229 TEST_F(LayerCallbackTest, NoBufferColor) {
230     sp<SurfaceControl> layer;
231     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
232 
233     Transaction transaction;
234     CallbackHelper callback;
235     int err = fillTransaction(transaction, &callback, layer, false, true);
236     if (err) {
237         GTEST_SUCCEED() << "test not supported";
238         return;
239     }
240 
241     ui::Size bufferSize = getBufferSize();
242     TransactionUtils::setFrame(transaction, layer, Rect(0, 0, bufferSize.width, bufferSize.height),
243                                Rect(0, 0, 32, 32));
244     transaction.apply();
245 
246     ExpectedResult expected;
247     expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
248                         ExpectedResult::Buffer::NOT_ACQUIRED);
249     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
250 }
251 
TEST_F(LayerCallbackTest,NoStateChange)252 TEST_F(LayerCallbackTest, NoStateChange) {
253     Transaction transaction;
254     CallbackHelper callback;
255     int err = fillTransaction(transaction, &callback);
256     if (err) {
257         GTEST_SUCCEED() << "test not supported";
258         return;
259     }
260 
261     transaction.apply();
262 
263     ExpectedResult expected;
264     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
265 }
266 
TEST_F(LayerCallbackTest,OffScreen)267 TEST_F(LayerCallbackTest, OffScreen) {
268     sp<SurfaceControl> layer;
269     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
270 
271     Transaction transaction;
272     CallbackHelper callback;
273     int err = fillTransaction(transaction, &callback, layer);
274     if (err) {
275         GTEST_SUCCEED() << "test not supported";
276         return;
277     }
278 
279     ui::Size bufferSize = getBufferSize();
280     TransactionUtils::setFrame(transaction, layer, Rect(0, 0, bufferSize.width, bufferSize.height),
281                                Rect(-100, -100, 100, 100));
282     transaction.apply();
283 
284     ExpectedResult expected;
285     expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
286     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
287 }
288 
TEST_F(LayerCallbackTest,MergeBufferNoColor)289 TEST_F(LayerCallbackTest, MergeBufferNoColor) {
290     sp<SurfaceControl> layer1, layer2;
291     ASSERT_NO_FATAL_FAILURE(layer1 = createLayerWithBuffer());
292     ASSERT_NO_FATAL_FAILURE(layer2 = createLayerWithBuffer());
293 
294     Transaction transaction1, transaction2;
295     CallbackHelper callback1, callback2;
296     int err = fillTransaction(transaction1, &callback1, layer1);
297     if (err) {
298         GTEST_SUCCEED() << "test not supported";
299         return;
300     }
301     err = fillTransaction(transaction2, &callback2, layer2);
302     if (err) {
303         GTEST_SUCCEED() << "test not supported";
304         return;
305     }
306 
307     ui::Size bufferSize = getBufferSize();
308 
309     TransactionUtils::setFrame(transaction1, layer1,
310                                Rect(0, 0, bufferSize.width, bufferSize.height), Rect(0, 0, 32, 32));
311     TransactionUtils::setFrame(transaction2, layer2,
312                                Rect(0, 0, bufferSize.width, bufferSize.height),
313                                Rect(32, 32, 64, 64));
314 
315     transaction2.merge(std::move(transaction1)).apply();
316 
317     ExpectedResult expected;
318     expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
319     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
320     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
321 }
322 
TEST_F(LayerCallbackTest,MergeNoBufferColor)323 TEST_F(LayerCallbackTest, MergeNoBufferColor) {
324     sp<SurfaceControl> layer1, layer2;
325     ASSERT_NO_FATAL_FAILURE(layer1 = createLayerWithBuffer());
326     ASSERT_NO_FATAL_FAILURE(layer2 = createLayerWithBuffer());
327 
328     Transaction transaction1, transaction2;
329     CallbackHelper callback1, callback2;
330     int err = fillTransaction(transaction1, &callback1, layer1, false, true);
331     if (err) {
332         GTEST_SUCCEED() << "test not supported";
333         return;
334     }
335     err = fillTransaction(transaction2, &callback2, layer2, false, true);
336     if (err) {
337         GTEST_SUCCEED() << "test not supported";
338         return;
339     }
340 
341     ui::Size bufferSize = getBufferSize();
342 
343     TransactionUtils::setFrame(transaction1, layer1,
344                                Rect(0, 0, bufferSize.width, bufferSize.height), Rect(0, 0, 32, 32));
345     TransactionUtils::setFrame(transaction2, layer2,
346                                Rect(0, 0, bufferSize.width, bufferSize.height),
347                                Rect(32, 32, 64, 64));
348 
349     transaction2.merge(std::move(transaction1)).apply();
350 
351     ExpectedResult expected;
352     expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
353                          ExpectedResult::Buffer::NOT_ACQUIRED);
354     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
355     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
356 }
357 
TEST_F(LayerCallbackTest,MergeOneBufferOneColor)358 TEST_F(LayerCallbackTest, MergeOneBufferOneColor) {
359     sp<SurfaceControl> layer1, layer2;
360     ASSERT_NO_FATAL_FAILURE(layer1 = createLayerWithBuffer());
361     ASSERT_NO_FATAL_FAILURE(layer2 = createLayerWithBuffer());
362 
363     Transaction transaction1, transaction2;
364     CallbackHelper callback1, callback2;
365     int err = fillTransaction(transaction1, &callback1, layer1);
366     if (err) {
367         GTEST_SUCCEED() << "test not supported";
368         return;
369     }
370     err = fillTransaction(transaction2, &callback2, layer2, false, true);
371     if (err) {
372         GTEST_SUCCEED() << "test not supported";
373         return;
374     }
375 
376     ui::Size bufferSize = getBufferSize();
377 
378     TransactionUtils::setFrame(transaction1, layer1,
379                                Rect(0, 0, bufferSize.width, bufferSize.height), Rect(0, 0, 32, 32));
380     TransactionUtils::setFrame(transaction2, layer2,
381                                Rect(0, 0, bufferSize.width, bufferSize.height),
382                                Rect(32, 32, 64, 64));
383 
384     transaction2.merge(std::move(transaction1)).apply();
385 
386     ExpectedResult expected;
387     expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer1);
388     expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer2,
389                         ExpectedResult::Buffer::NOT_ACQUIRED);
390     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
391     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
392 }
TEST_F(LayerCallbackTest,Merge_SameCallback)393 TEST_F(LayerCallbackTest, Merge_SameCallback) {
394     sp<SurfaceControl> layer1, layer2;
395     ASSERT_NO_FATAL_FAILURE(layer1 = createLayerWithBuffer());
396     ASSERT_NO_FATAL_FAILURE(layer2 = createLayerWithBuffer());
397 
398     Transaction transaction1, transaction2;
399     CallbackHelper callback;
400     int err = fillTransaction(transaction1, &callback, layer1);
401     if (err) {
402         GTEST_SUCCEED() << "test not supported";
403         return;
404     }
405     err = fillTransaction(transaction2, &callback, layer2);
406     if (err) {
407         GTEST_SUCCEED() << "test not supported";
408         return;
409     }
410 
411     transaction2.merge(std::move(transaction1)).apply();
412 
413     ExpectedResult expected;
414     expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
415     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
416     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
417 }
418 
TEST_F(LayerCallbackTest,Merge_SameLayer)419 TEST_F(LayerCallbackTest, Merge_SameLayer) {
420     sp<SurfaceControl> layer;
421     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
422 
423     Transaction transaction1, transaction2;
424     CallbackHelper callback1, callback2;
425     int err = fillTransaction(transaction1, &callback1, layer);
426     if (err) {
427         GTEST_SUCCEED() << "test not supported";
428         return;
429     }
430     err = fillTransaction(transaction2, &callback2, layer);
431     if (err) {
432         GTEST_SUCCEED() << "test not supported";
433         return;
434     }
435 
436     transaction2.merge(std::move(transaction1)).apply();
437 
438     ExpectedResult expected;
439     expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
440     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
441     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
442 }
443 
TEST_F(LayerCallbackTest,Merge_DifferentClients)444 TEST_F(LayerCallbackTest, Merge_DifferentClients) {
445     sp<SurfaceComposerClient> client1(sp<SurfaceComposerClient>::make()),
446             client2(sp<SurfaceComposerClient>::make());
447 
448     ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
449     ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
450 
451     sp<SurfaceControl> layer1, layer2;
452     ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
453                                                  ISurfaceComposerClient::eFXSurfaceBufferState));
454     ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
455                                                  ISurfaceComposerClient::eFXSurfaceBufferState));
456 
457     Transaction transaction1, transaction2;
458     CallbackHelper callback1, callback2;
459     int err = fillTransaction(transaction1, &callback1, layer1);
460     if (err) {
461         GTEST_SUCCEED() << "test not supported";
462         return;
463     }
464     err = fillTransaction(transaction2, &callback2, layer2);
465     if (err) {
466         GTEST_SUCCEED() << "test not supported";
467         return;
468     }
469 
470     ui::Size bufferSize = getBufferSize();
471 
472     TransactionUtils::setFrame(transaction1, layer1,
473                                Rect(0, 0, bufferSize.width, bufferSize.height), Rect(0, 0, 32, 32));
474     TransactionUtils::setFrame(transaction2, layer2,
475                                Rect(0, 0, bufferSize.width, bufferSize.height),
476                                Rect(32, 32, 64, 64));
477 
478     transaction2.merge(std::move(transaction1)).apply();
479 
480     ExpectedResult expected;
481     expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
482     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
483     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
484 }
485 
TEST_F(LayerCallbackTest,MultipleTransactions)486 TEST_F(LayerCallbackTest, MultipleTransactions) {
487     sp<SurfaceControl> layer;
488     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
489 
490     Transaction transaction;
491     CallbackHelper callback;
492     for (size_t i = 0; i < 10; i++) {
493         int err = fillTransaction(transaction, &callback, layer);
494         if (err) {
495             GTEST_SUCCEED() << "test not supported";
496             return;
497         }
498 
499         transaction.apply();
500 
501         ExpectedResult expected;
502         expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
503                             ExpectedResult::Buffer::ACQUIRED,
504                             (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
505                                      : ExpectedResult::PreviousBuffer::RELEASED);
506         EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
507     }
508     ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
509 }
510 
TEST_F(LayerCallbackTest,MultipleTransactions_NoStateChange)511 TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
512     sp<SurfaceControl> layer;
513     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
514 
515     Transaction transaction;
516     CallbackHelper callback;
517     for (size_t i = 0; i < 10; i++) {
518         ExpectedResult expected;
519 
520         if (i == 0) {
521             int err = fillTransaction(transaction, &callback, layer);
522             if (err) {
523                 GTEST_SUCCEED() << "test not supported";
524                 return;
525             }
526             expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
527         } else {
528             int err = fillTransaction(transaction, &callback);
529             if (err) {
530                 GTEST_SUCCEED() << "test not supported";
531                 return;
532             }
533         }
534 
535         transaction.apply();
536 
537         EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
538     }
539     ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
540 }
541 
TEST_F(LayerCallbackTest,MultipleTransactions_SameStateChange)542 TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
543     sp<SurfaceControl> layer;
544     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
545 
546     Transaction transaction;
547     CallbackHelper callback;
548     for (size_t i = 0; i < 10; i++) {
549         if (i == 0) {
550             int err = fillTransaction(transaction, &callback, layer);
551             if (err) {
552                 GTEST_SUCCEED() << "test not supported";
553                 return;
554             }
555         } else {
556             int err = fillTransaction(transaction, &callback);
557             if (err) {
558                 GTEST_SUCCEED() << "test not supported";
559                 return;
560             }
561         }
562 
563         ui::Size bufferSize = getBufferSize();
564         TransactionUtils::setFrame(transaction, layer,
565                                    Rect(0, 0, bufferSize.width, bufferSize.height),
566                                    Rect(0, 0, 32, 32));
567         transaction.apply();
568 
569         ExpectedResult expected;
570         expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
571                                      : ExpectedResult::Transaction::NOT_PRESENTED,
572                             layer,
573                             (i == 0) ? ExpectedResult::Buffer::ACQUIRED
574                                      : ExpectedResult::Buffer::NOT_ACQUIRED);
575         EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
576     }
577     ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
578 }
579 
TEST_F(LayerCallbackTest,MultipleTransactions_Merge)580 TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
581     sp<SurfaceControl> layer1, layer2;
582     ASSERT_NO_FATAL_FAILURE(layer1 = createLayerWithBuffer());
583     ASSERT_NO_FATAL_FAILURE(layer2 = createLayerWithBuffer());
584 
585     Transaction transaction1, transaction2;
586     CallbackHelper callback1, callback2;
587     for (size_t i = 0; i < 10; i++) {
588         int err = fillTransaction(transaction1, &callback1, layer1);
589         if (err) {
590             GTEST_SUCCEED() << "test not supported";
591             return;
592         }
593         err = fillTransaction(transaction2, &callback2, layer2);
594         if (err) {
595             GTEST_SUCCEED() << "test not supported";
596             return;
597         }
598 
599         ui::Size bufferSize = getBufferSize();
600 
601         TransactionUtils::setFrame(transaction1, layer1,
602                                    Rect(0, 0, bufferSize.width, bufferSize.height),
603                                    Rect(0, 0, 32, 32));
604         TransactionUtils::setFrame(transaction2, layer2,
605                                    Rect(0, 0, bufferSize.width, bufferSize.height),
606                                    Rect(32, 32, 64, 64));
607 
608         transaction2.merge(std::move(transaction1)).apply();
609 
610         ExpectedResult expected;
611         expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
612                              ExpectedResult::Buffer::ACQUIRED,
613                              (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
614                                       : ExpectedResult::PreviousBuffer::RELEASED);
615         EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
616         EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
617     }
618     ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
619     ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
620 }
621 
TEST_F(LayerCallbackTest,MultipleTransactions_Merge_DifferentClients)622 TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
623     sp<SurfaceComposerClient> client1(sp<SurfaceComposerClient>::make()),
624             client2(sp<SurfaceComposerClient>::make());
625     ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
626     ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
627 
628     sp<SurfaceControl> layer1, layer2;
629     ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
630                                                  ISurfaceComposerClient::eFXSurfaceBufferState));
631     ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
632                                                  ISurfaceComposerClient::eFXSurfaceBufferState));
633 
634     Transaction transaction1, transaction2;
635     CallbackHelper callback1, callback2;
636     for (size_t i = 0; i < 10; i++) {
637         int err = fillTransaction(transaction1, &callback1, layer1);
638         if (err) {
639             GTEST_SUCCEED() << "test not supported";
640             return;
641         }
642         err = fillTransaction(transaction2, &callback2, layer2);
643         if (err) {
644             GTEST_SUCCEED() << "test not supported";
645             return;
646         }
647 
648         ui::Size bufferSize = getBufferSize();
649 
650         TransactionUtils::setFrame(transaction1, layer1,
651                                    Rect(0, 0, bufferSize.width, bufferSize.height),
652                                    Rect(0, 0, 32, 32));
653         TransactionUtils::setFrame(transaction2, layer2,
654                                    Rect(0, 0, bufferSize.width, bufferSize.height),
655                                    Rect(32, 32, 64, 64));
656 
657         transaction2.merge(std::move(transaction1)).apply();
658 
659         ExpectedResult expected;
660         expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
661                              ExpectedResult::Buffer::ACQUIRED,
662                              (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
663                                       : ExpectedResult::PreviousBuffer::RELEASED);
664         EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
665         EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
666     }
667     ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
668     ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
669 }
670 
TEST_F(LayerCallbackTest,MultipleTransactions_Merge_DifferentClients_NoStateChange)671 TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
672     sp<SurfaceComposerClient> client1(sp<SurfaceComposerClient>::make()),
673             client2(sp<SurfaceComposerClient>::make());
674     ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
675     ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
676 
677     sp<SurfaceControl> layer1, layer2;
678     ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
679                                                  ISurfaceComposerClient::eFXSurfaceBufferState));
680     ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
681                                                  ISurfaceComposerClient::eFXSurfaceBufferState));
682 
683     Transaction transaction1, transaction2;
684     CallbackHelper callback1, callback2;
685 
686     // Normal call to set up test
687     int err = fillTransaction(transaction1, &callback1, layer1);
688     if (err) {
689         GTEST_SUCCEED() << "test not supported";
690         return;
691     }
692     err = fillTransaction(transaction2, &callback2, layer2);
693     if (err) {
694         GTEST_SUCCEED() << "test not supported";
695         return;
696     }
697 
698     ui::Size bufferSize = getBufferSize();
699 
700     TransactionUtils::setFrame(transaction1, layer1,
701                                Rect(0, 0, bufferSize.width, bufferSize.height), Rect(0, 0, 32, 32));
702     TransactionUtils::setFrame(transaction2, layer2,
703                                Rect(0, 0, bufferSize.width, bufferSize.height),
704                                Rect(32, 32, 64, 64));
705 
706     transaction2.merge(std::move(transaction1)).apply();
707 
708     ExpectedResult expected;
709     expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
710     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
711     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
712     expected.reset();
713 
714     // Test
715     err = fillTransaction(transaction1, &callback1);
716     if (err) {
717         GTEST_SUCCEED() << "test not supported";
718         return;
719     }
720     err = fillTransaction(transaction2, &callback2);
721     if (err) {
722         GTEST_SUCCEED() << "test not supported";
723         return;
724     }
725 
726     transaction2.merge(std::move(transaction1)).apply();
727 
728     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
729     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
730 }
731 
TEST_F(LayerCallbackTest,MultipleTransactions_Merge_DifferentClients_SameStateChange)732 TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
733     sp<SurfaceComposerClient> client1(sp<SurfaceComposerClient>::make()),
734             client2(sp<SurfaceComposerClient>::make());
735 
736     ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
737     ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
738 
739     sp<SurfaceControl> layer1, layer2;
740     ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
741                                                  ISurfaceComposerClient::eFXSurfaceBufferState));
742     ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
743                                                  ISurfaceComposerClient::eFXSurfaceBufferState));
744 
745     Transaction transaction1, transaction2;
746     CallbackHelper callback1, callback2;
747 
748     // Normal call to set up test
749     int err = fillTransaction(transaction1, &callback1, layer1);
750     if (err) {
751         GTEST_SUCCEED() << "test not supported";
752         return;
753     }
754     err = fillTransaction(transaction2, &callback2, layer2);
755     if (err) {
756         GTEST_SUCCEED() << "test not supported";
757         return;
758     }
759 
760     ui::Size bufferSize = getBufferSize();
761 
762     TransactionUtils::setFrame(transaction1, layer1,
763                                Rect(0, 0, bufferSize.width, bufferSize.height), Rect(0, 0, 32, 32));
764     TransactionUtils::setFrame(transaction2, layer2,
765                                Rect(0, 0, bufferSize.width, bufferSize.height),
766                                Rect(32, 32, 64, 64));
767 
768     transaction2.merge(std::move(transaction1)).apply();
769 
770     ExpectedResult expected;
771     expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
772     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
773     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
774     expected.reset();
775 
776     // Test
777     err = fillTransaction(transaction1, &callback1);
778     if (err) {
779         GTEST_SUCCEED() << "test not supported";
780         return;
781     }
782     err = fillTransaction(transaction2, &callback2);
783     if (err) {
784         GTEST_SUCCEED() << "test not supported";
785         return;
786     }
787 
788     TransactionUtils::setFrame(transaction2, layer2,
789                                Rect(0, 0, bufferSize.width, bufferSize.height),
790                                Rect(32, 32, 64, 64));
791     transaction2.merge(std::move(transaction1)).apply();
792 
793     expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2,
794                         ExpectedResult::Buffer::NOT_ACQUIRED);
795     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
796     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
797 }
798 
799 // TODO (b/183181768): Fix & re-enable
TEST_F(LayerCallbackTest,DISABLED_MultipleTransactions_SingleFrame)800 TEST_F(LayerCallbackTest, DISABLED_MultipleTransactions_SingleFrame) {
801     sp<SurfaceControl> layer;
802     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
803 
804     Transaction transaction;
805     CallbackHelper callback;
806     std::vector<ExpectedResult> expectedResults(50);
807     for (auto& expected : expectedResults) {
808         expected.reset();
809         expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
810                             ExpectedResult::Buffer::ACQUIRED,
811                             ExpectedResult::PreviousBuffer::UNKNOWN);
812 
813         int err = fillTransaction(transaction, &callback, layer);
814         if (err) {
815             GTEST_SUCCEED() << "test not supported";
816             return;
817         }
818 
819         transaction.apply();
820     }
821     EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
822 }
823 
TEST_F(LayerCallbackTest,MultipleTransactions_SingleFrame_NoStateChange)824 TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
825     sp<SurfaceControl> layer;
826     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
827 
828     // Normal call to set up test
829     Transaction transaction;
830     CallbackHelper callback;
831     int err = fillTransaction(transaction, &callback, layer);
832     if (err) {
833         GTEST_SUCCEED() << "test not supported";
834         return;
835     }
836 
837     transaction.apply();
838 
839     ExpectedResult expected;
840     expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
841     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
842 
843     // Test
844     std::vector<ExpectedResult> expectedResults(50);
845     for (auto& expected : expectedResults) {
846         expected.reset();
847 
848         err = fillTransaction(transaction, &callback);
849         if (err) {
850             GTEST_SUCCEED() << "test not supported";
851             return;
852         }
853 
854         transaction.apply();
855     }
856     EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
857 }
858 
TEST_F(LayerCallbackTest,MultipleTransactions_SingleFrame_SameStateChange)859 TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
860     sp<SurfaceControl> layer;
861     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
862 
863     // Normal call to set up test
864     Transaction transaction;
865     CallbackHelper callback;
866     int err = fillTransaction(transaction, &callback, layer);
867     if (err) {
868         GTEST_SUCCEED() << "test not supported";
869         return;
870     }
871 
872     ui::Size bufferSize = getBufferSize();
873     TransactionUtils::setFrame(transaction, layer, Rect(0, 0, bufferSize.width, bufferSize.height),
874                                Rect(0, 0, 32, 32));
875     transaction.apply();
876 
877     ExpectedResult expectedResult;
878     expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
879     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
880 
881     // Test
882     std::vector<ExpectedResult> expectedResults(50);
883     for (auto& expected : expectedResults) {
884         expected.reset();
885         expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
886                             ExpectedResult::Buffer::NOT_ACQUIRED);
887 
888         err = fillTransaction(transaction, &callback);
889         if (err) {
890             GTEST_SUCCEED() << "test not supported";
891             return;
892         }
893 
894         TransactionUtils::setFrame(transaction, layer,
895                                    Rect(0, 0, bufferSize.width, bufferSize.height),
896                                    Rect(0, 0, 32, 32));
897         transaction.apply();
898     }
899     EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
900 }
901 
TEST_F(LayerCallbackTest,DesiredPresentTime)902 TEST_F(LayerCallbackTest, DesiredPresentTime) {
903     sp<SurfaceControl> layer;
904     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
905 
906     Transaction transaction;
907     CallbackHelper callback;
908     int err = fillTransaction(transaction, &callback, layer);
909     if (err) {
910         GTEST_SUCCEED() << "test not supported";
911         return;
912     }
913 
914     // Try to present 100ms in the future
915     nsecs_t time = systemTime() + std::chrono::nanoseconds(100ms).count();
916 
917     transaction.setDesiredPresentTime(time);
918     transaction.apply();
919 
920     ExpectedResult expected;
921     expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
922     expected.addExpectedPresentTime(time);
923     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
924 }
925 
TEST_F(LayerCallbackTest,DesiredPresentTime_Multiple)926 TEST_F(LayerCallbackTest, DesiredPresentTime_Multiple) {
927     sp<SurfaceControl> layer;
928     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
929 
930     Transaction transaction;
931     CallbackHelper callback1;
932     int err = fillTransaction(transaction, &callback1, layer);
933     if (err) {
934         GTEST_SUCCEED() << "test not supported";
935         return;
936     }
937 
938     // Try to present 100ms in the future
939     nsecs_t time = systemTime() + std::chrono::nanoseconds(100ms).count();
940 
941     transaction.setDesiredPresentTime(time);
942     transaction.apply();
943 
944     ExpectedResult expected1;
945     expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
946     expected1.addExpectedPresentTime(time);
947 
948     CallbackHelper callback2;
949     err = fillTransaction(transaction, &callback2, layer);
950     if (err) {
951         GTEST_SUCCEED() << "test not supported";
952         return;
953     }
954 
955     // Try to present 33ms after the first frame
956     time += std::chrono::nanoseconds(33ms).count();
957 
958     transaction.setDesiredPresentTime(time);
959     transaction.apply();
960 
961     ExpectedResult expected2;
962     expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
963                          ExpectedResult::Buffer::ACQUIRED,
964                          ExpectedResult::PreviousBuffer::RELEASED);
965     expected2.addExpectedPresentTime(time);
966 
967     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
968     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
969 }
970 
971 // TODO (b/183181768): Fix & re-enable
TEST_F(LayerCallbackTest,DISABLED_DesiredPresentTime_OutOfOrder)972 TEST_F(LayerCallbackTest, DISABLED_DesiredPresentTime_OutOfOrder) {
973     sp<SurfaceControl> layer;
974     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
975 
976     Transaction transaction;
977     CallbackHelper callback1;
978     int err = fillTransaction(transaction, &callback1, layer);
979     if (err) {
980         GTEST_SUCCEED() << "test not supported";
981         return;
982     }
983 
984     // Try to present 100ms in the future
985     nsecs_t time = systemTime() + std::chrono::nanoseconds(100ms).count();
986 
987     transaction.setDesiredPresentTime(time);
988     transaction.apply();
989 
990     ExpectedResult expected1;
991     expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
992     expected1.addExpectedPresentTime(time);
993 
994     CallbackHelper callback2;
995     err = fillTransaction(transaction, &callback2, layer);
996     if (err) {
997         GTEST_SUCCEED() << "test not supported";
998         return;
999     }
1000 
1001     // Try to present 33ms before the previous frame
1002     time -= std::chrono::nanoseconds(33ms).count();
1003 
1004     transaction.setDesiredPresentTime(time);
1005     transaction.apply();
1006 
1007     ExpectedResult expected2;
1008     expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
1009                          ExpectedResult::Buffer::ACQUIRED,
1010                          ExpectedResult::PreviousBuffer::RELEASED);
1011 
1012     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
1013     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
1014 }
1015 
TEST_F(LayerCallbackTest,DesiredPresentTime_Past)1016 TEST_F(LayerCallbackTest, DesiredPresentTime_Past) {
1017     sp<SurfaceControl> layer;
1018     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
1019 
1020     Transaction transaction;
1021     CallbackHelper callback;
1022     int err = fillTransaction(transaction, &callback, layer);
1023     if (err) {
1024         GTEST_SUCCEED() << "test not supported";
1025         return;
1026     }
1027 
1028     // Try to present 100ms in the past
1029     nsecs_t time = systemTime() - std::chrono::nanoseconds(100ms).count();
1030 
1031     transaction.setDesiredPresentTime(time);
1032     transaction.apply();
1033 
1034     ExpectedResult expected;
1035     expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
1036     expected.addExpectedPresentTime(systemTime());
1037     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
1038 }
1039 
TEST_F(LayerCallbackTest,ExpectedPresentTime)1040 TEST_F(LayerCallbackTest, ExpectedPresentTime) {
1041     sp<SurfaceControl> layer;
1042     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
1043 
1044     Transaction transaction;
1045     CallbackHelper callback;
1046     int err = fillTransaction(transaction, &callback, layer);
1047     if (err) {
1048         GTEST_SUCCEED() << "test not supported";
1049         return;
1050     }
1051 
1052     const Vsync vsync = waitForNextVsync();
1053     FrameTimelineInfo ftInfo;
1054     ftInfo.vsyncId = vsync.vsyncId;
1055     ftInfo.inputEventId = 0;
1056     transaction.setFrameTimelineInfo(ftInfo);
1057     transaction.apply();
1058 
1059     ExpectedResult expected;
1060     expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
1061     expected.addExpectedPresentTimeForVsyncId(vsync.expectedPresentTime);
1062     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
1063 }
1064 
1065 // b202394221
TEST_F(LayerCallbackTest,EmptyBufferStateChanges)1066 TEST_F(LayerCallbackTest, EmptyBufferStateChanges) {
1067     sp<SurfaceControl> bufferLayer, emptyBufferLayer;
1068     ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayerWithBuffer());
1069     ASSERT_NO_FATAL_FAILURE(emptyBufferLayer = createLayerWithBuffer());
1070 
1071     Transaction transaction;
1072     CallbackHelper callback;
1073     for (size_t i = 0; i < 10; i++) {
1074         int err = fillTransaction(transaction, &callback, bufferLayer);
1075         if (err) {
1076             GTEST_SUCCEED() << "test not supported";
1077             return;
1078         }
1079 
1080         ui::Size bufferSize = getBufferSize();
1081 
1082         TransactionUtils::setFrame(transaction, bufferLayer,
1083                                    Rect(0, 0, bufferSize.width, bufferSize.height),
1084                                    Rect(0, 0, 32, 32));
1085         transaction.setPosition(emptyBufferLayer, 1 + i, 2 + i);
1086         transaction.apply();
1087 
1088         ExpectedResult expected;
1089         expected.addSurface(ExpectedResult::Transaction::PRESENTED, bufferLayer,
1090                             ExpectedResult::Buffer::ACQUIRED,
1091                             (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
1092                                      : ExpectedResult::PreviousBuffer::RELEASED);
1093         expected.addSurface(ExpectedResult::Transaction::PRESENTED, emptyBufferLayer,
1094                             ExpectedResult::Buffer::NOT_ACQUIRED,
1095                             ExpectedResult::PreviousBuffer::NOT_RELEASED);
1096 
1097         EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
1098     }
1099     ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
1100 }
1101 
1102 // b202394221
TEST_F(LayerCallbackTest,DISABLED_NonBufferLayerStateChanges)1103 TEST_F(LayerCallbackTest, DISABLED_NonBufferLayerStateChanges) {
1104     sp<SurfaceControl> layer;
1105     ASSERT_NO_FATAL_FAILURE(layer = createColorLayer("ColorLayer", Color::RED));
1106 
1107     Transaction transaction;
1108     CallbackHelper callback;
1109     int err = fillTransaction(transaction, &callback);
1110     if (err) {
1111         GTEST_SUCCEED() << "test not supported";
1112         return;
1113     }
1114     transaction.setPosition(layer, 1, 2);
1115     transaction.apply();
1116 
1117     ExpectedResult expected;
1118     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
1119 }
1120 
TEST_F(LayerCallbackTest,CommitCallbackOffscreenLayer)1121 TEST_F(LayerCallbackTest, CommitCallbackOffscreenLayer) {
1122     sp<SurfaceControl> layer;
1123     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
1124     sp<SurfaceControl> offscreenLayer =
1125             createSurface(mClient, "Offscreen Layer", 0, 0, PIXEL_FORMAT_RGBA_8888,
1126                           ISurfaceComposerClient::eFXSurfaceBufferState, layer.get());
1127 
1128     Transaction transaction;
1129     CallbackHelper callback;
1130     int err = fillTransaction(transaction, &callback, layer, true);
1131     err |= fillBuffer(transaction, offscreenLayer);
1132     if (err) {
1133         GTEST_SUCCEED() << "test not supported";
1134         return;
1135     }
1136 
1137     transaction.reparent(offscreenLayer, nullptr)
1138             .addTransactionCommittedCallback(callback.function, callback.getContext());
1139     transaction.apply();
1140 
1141     std::unordered_set<sp<SurfaceControl>, SCHash> committedSc;
1142     committedSc.insert(layer);
1143     committedSc.insert(offscreenLayer);
1144     EXPECT_NO_FATAL_FAILURE(waitForCommitCallback(callback, committedSc));
1145 
1146     ExpectedResult expected;
1147     expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
1148     expected.addSurface(ExpectedResult::Transaction::PRESENTED, offscreenLayer);
1149     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
1150 }
1151 
TEST_F(LayerCallbackTest,TransactionCommittedCallback_BSL)1152 TEST_F(LayerCallbackTest, TransactionCommittedCallback_BSL) {
1153     sp<SurfaceControl> layer;
1154     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
1155 
1156     Transaction transaction;
1157     CallbackHelper callback;
1158     int err = fillTransaction(transaction, &callback, layer, true);
1159     if (err) {
1160         GTEST_SUCCEED() << "test not supported";
1161         return;
1162     }
1163     transaction.addTransactionCommittedCallback(callback.function, callback.getContext()).apply();
1164     std::unordered_set<sp<SurfaceControl>, SCHash> committedSc;
1165     committedSc.insert(layer);
1166     EXPECT_NO_FATAL_FAILURE(waitForCommitCallback(callback, committedSc));
1167     ExpectedResult expected;
1168     expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
1169     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
1170 }
1171 
TEST_F(LayerCallbackTest,TransactionCommittedCallback_EffectLayer)1172 TEST_F(LayerCallbackTest, TransactionCommittedCallback_EffectLayer) {
1173     sp<SurfaceControl> layer;
1174     ASSERT_NO_FATAL_FAILURE(layer = createColorLayer("ColorLayer", Color::RED));
1175 
1176     Transaction transaction;
1177     CallbackHelper callback;
1178     int err = fillTransaction(transaction, &callback);
1179     if (err) {
1180         GTEST_SUCCEED() << "test not supported";
1181         return;
1182     }
1183     transaction.addTransactionCommittedCallback(callback.function, callback.getContext()).apply();
1184     std::unordered_set<sp<SurfaceControl>, SCHash> committedSc;
1185     EXPECT_NO_FATAL_FAILURE(waitForCommitCallback(callback, committedSc));
1186 
1187     ExpectedResult expected;
1188     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
1189 }
1190 
TEST_F(LayerCallbackTest,TransactionCommittedCallback_ContainerLayer)1191 TEST_F(LayerCallbackTest, TransactionCommittedCallback_ContainerLayer) {
1192     sp<SurfaceControl> layer;
1193     ASSERT_NO_FATAL_FAILURE(layer = createLayer(mClient, "Container Layer", 0, 0,
1194                                                 ISurfaceComposerClient::eFXSurfaceContainer));
1195 
1196     Transaction transaction;
1197     CallbackHelper callback;
1198     int err = fillTransaction(transaction, &callback);
1199     if (err) {
1200         GTEST_SUCCEED() << "test not supported";
1201         return;
1202     }
1203     transaction.addTransactionCommittedCallback(callback.function, callback.getContext()).apply();
1204     std::unordered_set<sp<SurfaceControl>, SCHash> committedSc;
1205     EXPECT_NO_FATAL_FAILURE(waitForCommitCallback(callback, committedSc));
1206 
1207     ExpectedResult expected;
1208     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
1209 }
1210 
TEST_F(LayerCallbackTest,TransactionCommittedCallback_NoLayer)1211 TEST_F(LayerCallbackTest, TransactionCommittedCallback_NoLayer) {
1212     Transaction transaction;
1213     CallbackHelper callback;
1214     int err = fillTransaction(transaction, &callback);
1215     if (err) {
1216         GTEST_SUCCEED() << "test not supported";
1217         return;
1218     }
1219     transaction.addTransactionCommittedCallback(callback.function, callback.getContext()).apply();
1220     std::unordered_set<sp<SurfaceControl>, SCHash> committedSc;
1221     EXPECT_NO_FATAL_FAILURE(waitForCommitCallback(callback, committedSc));
1222 
1223     ExpectedResult expected;
1224     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
1225 }
1226 
TEST_F(LayerCallbackTest,SetNullBuffer)1227 TEST_F(LayerCallbackTest, SetNullBuffer) {
1228     sp<SurfaceControl> layer;
1229     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
1230 
1231     Transaction transaction;
1232     CallbackHelper callback;
1233     int err = fillTransaction(transaction, &callback, layer, /*setBuffer=*/true,
1234                               /*setBackgroundColor=*/false);
1235     if (err) {
1236         GTEST_SUCCEED() << "test not supported";
1237         return;
1238     }
1239     transaction.apply();
1240 
1241     {
1242         ExpectedResult expected;
1243         expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
1244                             ExpectedResult::Buffer::ACQUIRED,
1245                             ExpectedResult::PreviousBuffer::NOT_RELEASED);
1246         EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
1247     }
1248 
1249     transaction.setBuffer(layer, nullptr);
1250     transaction.addTransactionCompletedCallback(callback.function, callback.getContext());
1251     transaction.apply();
1252 
1253     {
1254         ExpectedResult expected;
1255         expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
1256                             ExpectedResult::Buffer::ACQUIRED_NULL,
1257                             ExpectedResult::PreviousBuffer::RELEASED);
1258         EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
1259     }
1260 
1261     err = fillTransaction(transaction, &callback, layer, /*setBuffer=*/true,
1262                           /*setBackgroundColor=*/false);
1263     if (err) {
1264         GTEST_SUCCEED() << "test not supported";
1265         return;
1266     }
1267 
1268     transaction.apply();
1269 
1270     {
1271         ExpectedResult expected;
1272         expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
1273                             ExpectedResult::Buffer::ACQUIRED,
1274                             ExpectedResult::PreviousBuffer::NOT_RELEASED);
1275         EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
1276     }
1277 }
1278 
TEST_F(LayerCallbackTest,SetNullBufferOnLayerWithoutBuffer)1279 TEST_F(LayerCallbackTest, SetNullBufferOnLayerWithoutBuffer) {
1280     sp<SurfaceControl> layer;
1281     ASSERT_NO_FATAL_FAILURE(layer = createLayerWithBuffer());
1282 
1283     Transaction transaction;
1284     transaction.setBuffer(layer, nullptr);
1285     CallbackHelper callback;
1286     transaction.addTransactionCompletedCallback(callback.function, callback.getContext());
1287     transaction.apply();
1288 
1289     {
1290         ExpectedResult expected;
1291         expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
1292                             ExpectedResult::Buffer::NOT_ACQUIRED,
1293                             ExpectedResult::PreviousBuffer::NOT_RELEASED);
1294         EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
1295     }
1296 }
1297 
TEST_F(LayerCallbackTest,OccludedLayerHasReleaseCallback)1298 TEST_F(LayerCallbackTest, OccludedLayerHasReleaseCallback) {
1299     sp<SurfaceControl> layer1, layer2;
1300     ASSERT_NO_FATAL_FAILURE(layer1 = createLayerWithBuffer());
1301     ASSERT_NO_FATAL_FAILURE(layer2 = createLayerWithBuffer());
1302 
1303     Transaction transaction1, transaction2;
1304     CallbackHelper callback1a, callback1b, callback2a, callback2b;
1305     int err = fillTransaction(transaction1, &callback1a, layer1);
1306     if (err) {
1307         GTEST_SUCCEED() << "test not supported";
1308         return;
1309     }
1310     err = fillTransaction(transaction2, &callback2a, layer2);
1311     if (err) {
1312         GTEST_SUCCEED() << "test not supported";
1313         return;
1314     }
1315 
1316     ui::Size bufferSize = getBufferSize();
1317 
1318     // Occlude layer1 with layer2
1319     TransactionUtils::setFrame(transaction1, layer1,
1320                                Rect(0, 0, bufferSize.width, bufferSize.height), Rect(0, 0, 32, 32));
1321     TransactionUtils::setFrame(transaction2, layer2,
1322                                Rect(0, 0, bufferSize.width, bufferSize.height), Rect(0, 0, 32, 32));
1323     transaction1.apply();
1324     transaction2.apply();
1325 
1326     ExpectedResult expected1a, expected1b, expected2a, expected2b;
1327     expected1a.addSurface(ExpectedResult::Transaction::PRESENTED, {layer1},
1328                           ExpectedResult::Buffer::ACQUIRED,
1329                           ExpectedResult::PreviousBuffer::NOT_RELEASED);
1330 
1331     expected2a.addSurface(ExpectedResult::Transaction::PRESENTED, {layer2},
1332                           ExpectedResult::Buffer::ACQUIRED,
1333                           ExpectedResult::PreviousBuffer::NOT_RELEASED);
1334 
1335     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1a, expected1a, true));
1336     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2a, expected2a, true));
1337 
1338     // Submit new buffers so previous buffers can be released
1339     err = fillTransaction(transaction1, &callback1b, layer1);
1340     if (err) {
1341         GTEST_SUCCEED() << "test not supported";
1342         return;
1343     }
1344     err = fillTransaction(transaction2, &callback2b, layer2);
1345     if (err) {
1346         GTEST_SUCCEED() << "test not supported";
1347         return;
1348     }
1349 
1350     TransactionUtils::setFrame(transaction1, layer1,
1351                                Rect(0, 0, bufferSize.width, bufferSize.height), Rect(0, 0, 32, 32));
1352     TransactionUtils::setFrame(transaction2, layer2,
1353                                Rect(0, 0, bufferSize.width, bufferSize.height), Rect(0, 0, 32, 32));
1354     transaction1.apply();
1355     transaction2.apply();
1356 
1357     expected1b.addSurface(ExpectedResult::Transaction::PRESENTED, {layer1},
1358                           ExpectedResult::Buffer::ACQUIRED,
1359                           ExpectedResult::PreviousBuffer::RELEASED);
1360 
1361     expected2b.addSurface(ExpectedResult::Transaction::PRESENTED, {layer2},
1362                           ExpectedResult::Buffer::ACQUIRED,
1363                           ExpectedResult::PreviousBuffer::RELEASED);
1364 
1365     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1b, expected1b, true));
1366     EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2b, expected2b, true));
1367 }
1368 } // namespace android
1369