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