• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 #undef LOG_TAG
18 #define LOG_TAG "LibSurfaceFlingerUnittests"
19 
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <gui/SurfaceComposerClient.h>
23 #include <log/log.h>
24 #include <renderengine/ExternalTexture.h>
25 #include <renderengine/mock/RenderEngine.h>
26 #include <utils/String8.h>
27 
28 #include "TestableSurfaceFlinger.h"
29 #include "mock/DisplayHardware/MockComposer.h"
30 #include "mock/MockEventThread.h"
31 #include "mock/MockVsyncController.h"
32 
33 namespace android {
34 
35 using testing::_;
36 using testing::Mock;
37 using testing::Return;
38 using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
39 using PresentState = frametimeline::SurfaceFrame::PresentState;
40 
41 class TransactionSurfaceFrameTest : public testing::Test {
42 public:
TransactionSurfaceFrameTest()43     TransactionSurfaceFrameTest() {
44         const ::testing::TestInfo* const test_info =
45                 ::testing::UnitTest::GetInstance()->current_test_info();
46         ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
47         setupScheduler();
48         mFlinger.setupComposer(std::make_unique<Hwc2::mock::Composer>());
49     }
50 
~TransactionSurfaceFrameTest()51     ~TransactionSurfaceFrameTest() {
52         const ::testing::TestInfo* const test_info =
53                 ::testing::UnitTest::GetInstance()->current_test_info();
54         ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
55     }
56 
createBufferStateLayer()57     sp<BufferStateLayer> createBufferStateLayer() {
58         sp<Client> client;
59         LayerCreationArgs args(mFlinger.flinger(), client, "buffer-state-layer", 100, 100, 0,
60                                LayerMetadata());
61         return new BufferStateLayer(args);
62     }
63 
commitTransaction(Layer * layer)64     void commitTransaction(Layer* layer) {
65         auto c = layer->getDrawingState();
66         layer->commitTransaction(c);
67     }
68 
setupScheduler()69     void setupScheduler() {
70         auto eventThread = std::make_unique<mock::EventThread>();
71         auto sfEventThread = std::make_unique<mock::EventThread>();
72 
73         EXPECT_CALL(*eventThread, registerDisplayEventConnection(_));
74         EXPECT_CALL(*eventThread, createEventConnection(_, _))
75                 .WillOnce(Return(new EventThreadConnection(eventThread.get(), /*callingUid=*/0,
76                                                            ResyncCallback())));
77 
78         EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
79         EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
80                 .WillOnce(Return(new EventThreadConnection(sfEventThread.get(), /*callingUid=*/0,
81                                                            ResyncCallback())));
82 
83         auto vsyncController = std::make_unique<mock::VsyncController>();
84         auto vsyncTracker = std::make_unique<mock::VSyncTracker>();
85 
86         EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
87         EXPECT_CALL(*vsyncTracker, currentPeriod())
88                 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD));
89         EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
90         mFlinger.setupScheduler(std::move(vsyncController), std::move(vsyncTracker),
91                                 std::move(eventThread), std::move(sfEventThread));
92     }
93 
94     TestableSurfaceFlinger mFlinger;
95     renderengine::mock::RenderEngine mRenderEngine;
96 
97     FenceToFenceTimeMap fenceFactory;
98     client_cache_t mClientCache;
99 
PresentedSurfaceFrameForBufferlessTransaction()100     void PresentedSurfaceFrameForBufferlessTransaction() {
101         sp<BufferStateLayer> layer = createBufferStateLayer();
102         layer->setFrameTimelineVsyncForBufferlessTransaction({/*vsyncId*/ 1, /*inputEventId*/ 0},
103                                                              10);
104         EXPECT_EQ(1u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
105         ASSERT_TRUE(layer->mDrawingState.bufferSurfaceFrameTX == nullptr);
106         const auto surfaceFrame = layer->mDrawingState.bufferlessSurfaceFramesTX.at(/*token*/ 1);
107         commitTransaction(layer.get());
108         EXPECT_EQ(1, surfaceFrame->getToken());
109         EXPECT_EQ(false, surfaceFrame->getIsBuffer());
110         EXPECT_EQ(PresentState::Presented, surfaceFrame->getPresentState());
111     }
112 
PresentedSurfaceFrameForBufferTransaction()113     void PresentedSurfaceFrameForBufferTransaction() {
114         sp<BufferStateLayer> layer = createBufferStateLayer();
115         sp<Fence> fence(new Fence());
116         auto acquireFence = fenceFactory.createFenceTimeForTest(fence);
117         const auto buffer = std::make_shared<
118                 renderengine::ExternalTexture>(new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888,
119                                                                  1, 0),
120                                                mRenderEngine, false);
121         layer->setBuffer(buffer, fence, 10, 20, false, mClientCache, 1, std::nullopt,
122                          {/*vsyncId*/ 1, /*inputEventId*/ 0}, nullptr /* releaseBufferCallback */);
123         acquireFence->signalForTest(12);
124 
125         commitTransaction(layer.get());
126         EXPECT_EQ(0u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
127         ASSERT_NE(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
128         const auto surfaceFrame = layer->mDrawingState.bufferSurfaceFrameTX;
129         // Buffers are presented only at latch time.
130         EXPECT_EQ(PresentState::Unknown, surfaceFrame->getPresentState());
131 
132         bool computeVisisbleRegions;
133         layer->updateTexImage(computeVisisbleRegions, 15, 0);
134 
135         EXPECT_EQ(1, surfaceFrame->getToken());
136         EXPECT_EQ(true, surfaceFrame->getIsBuffer());
137         EXPECT_EQ(PresentState::Presented, surfaceFrame->getPresentState());
138     }
139 
DroppedSurfaceFrameForBufferTransaction()140     void DroppedSurfaceFrameForBufferTransaction() {
141         sp<BufferStateLayer> layer = createBufferStateLayer();
142 
143         sp<Fence> fence1(new Fence());
144         auto acquireFence1 = fenceFactory.createFenceTimeForTest(fence1);
145         const auto buffer1 = std::make_shared<
146                 renderengine::ExternalTexture>(new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888,
147                                                                  1, 0),
148                                                mRenderEngine, false);
149         layer->setBuffer(buffer1, fence1, 10, 20, false, mClientCache, 1, std::nullopt,
150                          {/*vsyncId*/ 1, /*inputEventId*/ 0}, nullptr /* releaseBufferCallback */);
151         EXPECT_EQ(0u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
152         ASSERT_NE(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
153         const auto droppedSurfaceFrame = layer->mDrawingState.bufferSurfaceFrameTX;
154 
155         sp<Fence> fence2(new Fence());
156         auto acquireFence2 = fenceFactory.createFenceTimeForTest(fence2);
157         const auto buffer2 = std::make_shared<
158                 renderengine::ExternalTexture>(new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888,
159                                                                  1, 0),
160                                                mRenderEngine, false);
161         nsecs_t start = systemTime();
162         layer->setBuffer(buffer2, fence2, 10, 20, false, mClientCache, 1, std::nullopt,
163                          {/*vsyncId*/ 1, /*inputEventId*/ 0}, nullptr /* releaseBufferCallback */);
164         nsecs_t end = systemTime();
165         acquireFence2->signalForTest(12);
166 
167         EXPECT_EQ(0u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
168         ASSERT_NE(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
169         const auto presentedSurfaceFrame = layer->mDrawingState.bufferSurfaceFrameTX;
170 
171         commitTransaction(layer.get());
172         bool computeVisisbleRegions;
173         layer->updateTexImage(computeVisisbleRegions, 15, 0);
174 
175         EXPECT_EQ(1, droppedSurfaceFrame->getToken());
176         EXPECT_EQ(true, droppedSurfaceFrame->getIsBuffer());
177         EXPECT_EQ(PresentState::Dropped, droppedSurfaceFrame->getPresentState());
178         EXPECT_EQ(0u, droppedSurfaceFrame->getActuals().endTime);
179         auto dropTime = droppedSurfaceFrame->getDropTime();
180         EXPECT_TRUE(dropTime > start && dropTime < end);
181 
182         EXPECT_EQ(1, presentedSurfaceFrame->getToken());
183         EXPECT_EQ(true, presentedSurfaceFrame->getIsBuffer());
184         EXPECT_EQ(PresentState::Presented, presentedSurfaceFrame->getPresentState());
185     }
186 
BufferlessSurfaceFramePromotedToBufferSurfaceFrame()187     void BufferlessSurfaceFramePromotedToBufferSurfaceFrame() {
188         sp<BufferStateLayer> layer = createBufferStateLayer();
189 
190         layer->setFrameTimelineVsyncForBufferlessTransaction({/*vsyncId*/ 1, /*inputEventId*/ 0},
191                                                              10);
192 
193         EXPECT_EQ(1u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
194         ASSERT_EQ(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
195 
196         sp<Fence> fence(new Fence());
197         auto acquireFence = fenceFactory.createFenceTimeForTest(fence);
198         const auto buffer = std::make_shared<
199                 renderengine::ExternalTexture>(new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888,
200                                                                  1, 0),
201                                                mRenderEngine, false);
202         layer->setBuffer(buffer, fence, 10, 20, false, mClientCache, 1, std::nullopt,
203                          {/*vsyncId*/ 1, /*inputEventId*/ 0}, nullptr /* releaseBufferCallback */);
204         acquireFence->signalForTest(12);
205 
206         EXPECT_EQ(0u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
207         ASSERT_NE(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
208         const auto surfaceFrame = layer->mDrawingState.bufferSurfaceFrameTX;
209 
210         commitTransaction(layer.get());
211         EXPECT_EQ(1, surfaceFrame->getToken());
212         EXPECT_EQ(true, surfaceFrame->getIsBuffer());
213         // Buffers are presented only at latch time.
214         EXPECT_EQ(PresentState::Unknown, surfaceFrame->getPresentState());
215 
216         bool computeVisisbleRegions;
217         layer->updateTexImage(computeVisisbleRegions, 15, 0);
218 
219         EXPECT_EQ(PresentState::Presented, surfaceFrame->getPresentState());
220     }
221 
BufferlessSurfaceFrameNotCreatedIfBufferSufaceFrameExists()222     void BufferlessSurfaceFrameNotCreatedIfBufferSufaceFrameExists() {
223         sp<BufferStateLayer> layer = createBufferStateLayer();
224         sp<Fence> fence(new Fence());
225         auto acquireFence = fenceFactory.createFenceTimeForTest(fence);
226         const auto buffer = std::make_shared<
227                 renderengine::ExternalTexture>(new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888,
228                                                                  1, 0),
229                                                mRenderEngine, false);
230         layer->setBuffer(buffer, fence, 10, 20, false, mClientCache, 1, std::nullopt,
231                          {/*vsyncId*/ 1, /*inputEventId*/ 0}, nullptr /* releaseBufferCallback */);
232         EXPECT_EQ(0u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
233         ASSERT_NE(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
234 
235         layer->setFrameTimelineVsyncForBufferlessTransaction({/*vsyncId*/ 1, /*inputEventId*/ 0},
236                                                              10);
237         EXPECT_EQ(0u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
238         ASSERT_NE(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
239     }
240 
MultipleSurfaceFramesPresentedTogether()241     void MultipleSurfaceFramesPresentedTogether() {
242         sp<BufferStateLayer> layer = createBufferStateLayer();
243         layer->setFrameTimelineVsyncForBufferlessTransaction({/*vsyncId*/ 1, /*inputEventId*/ 0},
244                                                              10);
245         EXPECT_EQ(1u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
246         ASSERT_EQ(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
247         const auto bufferlessSurfaceFrame1 =
248                 layer->mDrawingState.bufferlessSurfaceFramesTX.at(/*token*/ 1);
249 
250         layer->setFrameTimelineVsyncForBufferlessTransaction({/*vsyncId*/ 4, /*inputEventId*/ 0},
251                                                              10);
252         EXPECT_EQ(2u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
253         ASSERT_EQ(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
254         const auto bufferlessSurfaceFrame2 = layer->mDrawingState.bufferlessSurfaceFramesTX[4];
255 
256         sp<Fence> fence(new Fence());
257         auto acquireFence = fenceFactory.createFenceTimeForTest(fence);
258         const auto buffer = std::make_shared<
259                 renderengine::ExternalTexture>(new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888,
260                                                                  1, 0),
261                                                mRenderEngine, false);
262         layer->setBuffer(buffer, fence, 10, 20, false, mClientCache, 1, std::nullopt,
263                          {/*vsyncId*/ 3, /*inputEventId*/ 0}, nullptr /* releaseBufferCallback */);
264         EXPECT_EQ(2u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
265         ASSERT_NE(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
266         const auto bufferSurfaceFrameTX = layer->mDrawingState.bufferSurfaceFrameTX;
267 
268         acquireFence->signalForTest(12);
269 
270         commitTransaction(layer.get());
271 
272         EXPECT_EQ(1, bufferlessSurfaceFrame1->getToken());
273         EXPECT_EQ(false, bufferlessSurfaceFrame1->getIsBuffer());
274         EXPECT_EQ(PresentState::Presented, bufferlessSurfaceFrame1->getPresentState());
275 
276         EXPECT_EQ(4, bufferlessSurfaceFrame2->getToken());
277         EXPECT_EQ(false, bufferlessSurfaceFrame2->getIsBuffer());
278         EXPECT_EQ(PresentState::Presented, bufferlessSurfaceFrame2->getPresentState());
279 
280         EXPECT_EQ(3, bufferSurfaceFrameTX->getToken());
281         EXPECT_EQ(true, bufferSurfaceFrameTX->getIsBuffer());
282         // Buffers are presented only at latch time.
283         EXPECT_EQ(PresentState::Unknown, bufferSurfaceFrameTX->getPresentState());
284 
285         bool computeVisisbleRegions;
286         layer->updateTexImage(computeVisisbleRegions, 15, 0);
287 
288         EXPECT_EQ(PresentState::Presented, bufferSurfaceFrameTX->getPresentState());
289     }
290 
PendingSurfaceFramesRemovedAfterClassification()291     void PendingSurfaceFramesRemovedAfterClassification() {
292         sp<BufferStateLayer> layer = createBufferStateLayer();
293 
294         sp<Fence> fence1(new Fence());
295         auto acquireFence1 = fenceFactory.createFenceTimeForTest(fence1);
296         const auto buffer1 = std::make_shared<
297                 renderengine::ExternalTexture>(new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888,
298                                                                  1, 0),
299                                                mRenderEngine, false);
300         layer->setBuffer(buffer1, fence1, 10, 20, false, mClientCache, 1, std::nullopt,
301                          {/*vsyncId*/ 1, /*inputEventId*/ 0}, nullptr /* releaseBufferCallback */);
302         ASSERT_NE(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
303         const auto droppedSurfaceFrame = layer->mDrawingState.bufferSurfaceFrameTX;
304 
305         sp<Fence> fence2(new Fence());
306         auto acquireFence2 = fenceFactory.createFenceTimeForTest(fence2);
307         const auto buffer2 = std::make_shared<
308                 renderengine::ExternalTexture>(new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888,
309                                                                  1, 0),
310                                                mRenderEngine, false);
311         layer->setBuffer(buffer2, fence2, 10, 20, false, mClientCache, 1, std::nullopt,
312                          {/*vsyncId*/ 1, /*inputEventId*/ 0}, nullptr /* releaseBufferCallback */);
313         acquireFence2->signalForTest(12);
314 
315         ASSERT_NE(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
316         auto presentedSurfaceFrame = layer->mDrawingState.bufferSurfaceFrameTX;
317 
318         commitTransaction(layer.get());
319         bool computeVisisbleRegions;
320         layer->updateTexImage(computeVisisbleRegions, 15, 0);
321 
322         // Both the droppedSurfaceFrame and presentedSurfaceFrame should be in
323         // pendingJankClassifications.
324         EXPECT_EQ(2u, layer->mPendingJankClassifications.size());
325         presentedSurfaceFrame->onPresent(20, JankType::None, Fps::fromPeriodNsecs(11),
326                                          /*displayDeadlineDelta*/ 0, /*displayPresentDelta*/ 0);
327         layer->releasePendingBuffer(25);
328 
329         EXPECT_EQ(0u, layer->mPendingJankClassifications.size());
330     }
331 
BufferSurfaceFrame_ReplaceValidTokenBufferWithInvalidTokenBuffer()332     void BufferSurfaceFrame_ReplaceValidTokenBufferWithInvalidTokenBuffer() {
333         sp<BufferStateLayer> layer = createBufferStateLayer();
334 
335         sp<Fence> fence1(new Fence());
336         auto acquireFence1 = fenceFactory.createFenceTimeForTest(fence1);
337         const auto buffer1 = std::make_shared<
338                 renderengine::ExternalTexture>(new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888,
339                                                                  1, 0),
340                                                mRenderEngine, false);
341         layer->setBuffer(buffer1, fence1, 10, 20, false, mClientCache, 1, std::nullopt,
342                          {/*vsyncId*/ 1, /*inputEventId*/ 0}, nullptr /* releaseBufferCallback */);
343         EXPECT_EQ(0u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
344         ASSERT_NE(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
345         const auto droppedSurfaceFrame1 = layer->mDrawingState.bufferSurfaceFrameTX;
346 
347         sp<Fence> fence2(new Fence());
348         auto acquireFence2 = fenceFactory.createFenceTimeForTest(fence2);
349         const auto buffer2 = std::make_shared<
350                 renderengine::ExternalTexture>(new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888,
351                                                                  1, 0),
352                                                mRenderEngine, false);
353         auto dropStartTime1 = systemTime();
354         layer->setBuffer(buffer2, fence2, 10, 20, false, mClientCache, 1, std::nullopt,
355                          {/*vsyncId*/ FrameTimelineInfo::INVALID_VSYNC_ID, /*inputEventId*/ 0},
356                          nullptr /* releaseBufferCallback */);
357         auto dropEndTime1 = systemTime();
358         EXPECT_EQ(0u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
359         ASSERT_NE(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
360         const auto droppedSurfaceFrame2 = layer->mDrawingState.bufferSurfaceFrameTX;
361 
362         sp<Fence> fence3(new Fence());
363         auto acquireFence3 = fenceFactory.createFenceTimeForTest(fence3);
364         const auto buffer3 = std::make_shared<
365                 renderengine::ExternalTexture>(new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888,
366                                                                  1, 0),
367                                                mRenderEngine, false);
368         auto dropStartTime2 = systemTime();
369         layer->setBuffer(buffer3, fence3, 10, 20, false, mClientCache, 1, std::nullopt,
370                          {/*vsyncId*/ 2, /*inputEventId*/ 0}, nullptr /* releaseBufferCallback */);
371         auto dropEndTime2 = systemTime();
372         acquireFence3->signalForTest(12);
373 
374         EXPECT_EQ(0u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
375         ASSERT_NE(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
376         const auto presentedSurfaceFrame = layer->mDrawingState.bufferSurfaceFrameTX;
377 
378         commitTransaction(layer.get());
379         bool computeVisisbleRegions;
380         layer->updateTexImage(computeVisisbleRegions, 15, 0);
381 
382         EXPECT_EQ(1, droppedSurfaceFrame1->getToken());
383         EXPECT_EQ(true, droppedSurfaceFrame1->getIsBuffer());
384         EXPECT_EQ(PresentState::Dropped, droppedSurfaceFrame1->getPresentState());
385         EXPECT_EQ(0u, droppedSurfaceFrame1->getActuals().endTime);
386         auto dropTime1 = droppedSurfaceFrame1->getDropTime();
387         EXPECT_TRUE(dropTime1 > dropStartTime1 && dropTime1 < dropEndTime1);
388 
389         EXPECT_EQ(FrameTimelineInfo::INVALID_VSYNC_ID, droppedSurfaceFrame2->getToken());
390         EXPECT_EQ(true, droppedSurfaceFrame2->getIsBuffer());
391         EXPECT_EQ(PresentState::Dropped, droppedSurfaceFrame2->getPresentState());
392         EXPECT_EQ(0u, droppedSurfaceFrame2->getActuals().endTime);
393         auto dropTime2 = droppedSurfaceFrame2->getDropTime();
394         EXPECT_TRUE(dropTime2 > dropStartTime2 && dropTime2 < dropEndTime2);
395 
396         EXPECT_EQ(2, presentedSurfaceFrame->getToken());
397         EXPECT_EQ(true, presentedSurfaceFrame->getIsBuffer());
398         EXPECT_EQ(PresentState::Presented, presentedSurfaceFrame->getPresentState());
399     }
400 
MultipleCommitsBeforeLatch()401     void MultipleCommitsBeforeLatch() {
402         sp<BufferStateLayer> layer = createBufferStateLayer();
403         uint32_t surfaceFramesPendingClassification = 0;
404         std::vector<std::shared_ptr<frametimeline::SurfaceFrame>> bufferlessSurfaceFrames;
405         for (int i = 0; i < 10; i += 2) {
406             sp<Fence> fence1(new Fence());
407             const auto buffer1 = std::make_shared<
408                     renderengine::ExternalTexture>(new GraphicBuffer(1, 1,
409                                                                      HAL_PIXEL_FORMAT_RGBA_8888, 1,
410                                                                      0),
411                                                    mRenderEngine, false);
412             layer->setBuffer(buffer1, fence1, 10, 20, false, mClientCache, 1, std::nullopt,
413                              {/*vsyncId*/ 1, /*inputEventId*/ 0},
414                              nullptr /* releaseBufferCallback */);
415             layer->setFrameTimelineVsyncForBufferlessTransaction({/*vsyncId*/ 2,
416                                                                   /*inputEventId*/ 0},
417                                                                  10);
418             ASSERT_NE(nullptr, layer->mDrawingState.bufferSurfaceFrameTX);
419             EXPECT_EQ(1u, layer->mDrawingState.bufferlessSurfaceFramesTX.size());
420             auto& bufferlessSurfaceFrame =
421                     layer->mDrawingState.bufferlessSurfaceFramesTX.at(/*vsyncId*/ 2);
422             bufferlessSurfaceFrames.push_back(bufferlessSurfaceFrame);
423 
424             commitTransaction(layer.get());
425             surfaceFramesPendingClassification += 2;
426             EXPECT_EQ(surfaceFramesPendingClassification,
427                       layer->mPendingJankClassifications.size());
428         }
429 
430         auto presentedBufferSurfaceFrame = layer->mDrawingState.bufferSurfaceFrameTX;
431         bool computeVisisbleRegions;
432         layer->updateTexImage(computeVisisbleRegions, 15, 0);
433         // BufferlessSurfaceFrames are immediately set to presented and added to the DisplayFrame.
434         // Since we don't have access to DisplayFrame here, trigger an onPresent directly.
435         for (auto& surfaceFrame : bufferlessSurfaceFrames) {
436             surfaceFrame->onPresent(20, JankType::None, Fps::fromPeriodNsecs(11),
437                                     /*displayDeadlineDelta*/ 0, /*displayPresentDelta*/ 0);
438         }
439         presentedBufferSurfaceFrame->onPresent(20, JankType::None, Fps::fromPeriodNsecs(11),
440                                                /*displayDeadlineDelta*/ 0,
441                                                /*displayPresentDelta*/ 0);
442 
443         // There should be 10 bufferlessSurfaceFrames and 1 bufferSurfaceFrame
444         ASSERT_EQ(10u, surfaceFramesPendingClassification);
445         ASSERT_EQ(surfaceFramesPendingClassification, layer->mPendingJankClassifications.size());
446 
447         // For the frames upto 8, the bufferSurfaceFrame should have been dropped while the
448         // bufferlessSurfaceFrame presented
449         for (uint32_t i = 0; i < 8; i += 2) {
450             auto& bufferSurfaceFrame = layer->mPendingJankClassifications[i];
451             auto& bufferlessSurfaceFrame = layer->mPendingJankClassifications[i + 1];
452             EXPECT_EQ(bufferSurfaceFrame->getPresentState(), PresentState::Dropped);
453             EXPECT_EQ(bufferlessSurfaceFrame->getPresentState(), PresentState::Presented);
454         }
455         {
456             auto& bufferSurfaceFrame = layer->mPendingJankClassifications[8u];
457             auto& bufferlessSurfaceFrame = layer->mPendingJankClassifications[9u];
458             EXPECT_EQ(bufferSurfaceFrame->getPresentState(), PresentState::Presented);
459             EXPECT_EQ(bufferlessSurfaceFrame->getPresentState(), PresentState::Presented);
460         }
461 
462         layer->releasePendingBuffer(25);
463 
464         // There shouldn't be any pending classifications. Everything should have been cleared.
465         EXPECT_EQ(0u, layer->mPendingJankClassifications.size());
466     }
467 };
468 
TEST_F(TransactionSurfaceFrameTest,PresentedBufferlessSurfaceFrame)469 TEST_F(TransactionSurfaceFrameTest, PresentedBufferlessSurfaceFrame) {
470     PresentedSurfaceFrameForBufferlessTransaction();
471 }
472 
TEST_F(TransactionSurfaceFrameTest,PresentedBufferSurfaceFrame)473 TEST_F(TransactionSurfaceFrameTest, PresentedBufferSurfaceFrame) {
474     PresentedSurfaceFrameForBufferTransaction();
475 }
476 
TEST_F(TransactionSurfaceFrameTest,DroppedBufferSurfaceFrame)477 TEST_F(TransactionSurfaceFrameTest, DroppedBufferSurfaceFrame) {
478     DroppedSurfaceFrameForBufferTransaction();
479 }
480 
TEST_F(TransactionSurfaceFrameTest,BufferlessSurfaceFramePromotedToBufferSurfaceFrame)481 TEST_F(TransactionSurfaceFrameTest, BufferlessSurfaceFramePromotedToBufferSurfaceFrame) {
482     BufferlessSurfaceFramePromotedToBufferSurfaceFrame();
483 }
484 
TEST_F(TransactionSurfaceFrameTest,BufferlessSurfaceFrameNotCreatedIfBufferSufaceFrameExists)485 TEST_F(TransactionSurfaceFrameTest, BufferlessSurfaceFrameNotCreatedIfBufferSufaceFrameExists) {
486     BufferlessSurfaceFrameNotCreatedIfBufferSufaceFrameExists();
487 }
488 
TEST_F(TransactionSurfaceFrameTest,MultipleSurfaceFramesPresentedTogether)489 TEST_F(TransactionSurfaceFrameTest, MultipleSurfaceFramesPresentedTogether) {
490     MultipleSurfaceFramesPresentedTogether();
491 }
492 
TEST_F(TransactionSurfaceFrameTest,PendingSurfaceFramesRemovedAfterClassification)493 TEST_F(TransactionSurfaceFrameTest, PendingSurfaceFramesRemovedAfterClassification) {
494     PendingSurfaceFramesRemovedAfterClassification();
495 }
496 
TEST_F(TransactionSurfaceFrameTest,BufferSurfaceFrame_ReplaceValidTokenBufferWithInvalidTokenBuffer)497 TEST_F(TransactionSurfaceFrameTest,
498        BufferSurfaceFrame_ReplaceValidTokenBufferWithInvalidTokenBuffer) {
499     BufferSurfaceFrame_ReplaceValidTokenBufferWithInvalidTokenBuffer();
500 }
501 
TEST_F(TransactionSurfaceFrameTest,MultipleCommitsBeforeLatch)502 TEST_F(TransactionSurfaceFrameTest, MultipleCommitsBeforeLatch) {
503     MultipleCommitsBeforeLatch();
504 }
505 
506 } // namespace android
507