• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 "DummyConsumer.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
22 #include <binder/ProcessState.h>
23 #include <configstore/Utils.h>
24 #include <cutils/properties.h>
25 #include <inttypes.h>
26 #include <gui/BufferItemConsumer.h>
27 #include <gui/IDisplayEventConnection.h>
28 #include <gui/IProducerListener.h>
29 #include <gui/ISurfaceComposer.h>
30 #include <gui/Surface.h>
31 #include <gui/SurfaceComposerClient.h>
32 #include <private/gui/ComposerService.h>
33 #include <ui/Rect.h>
34 #include <utils/String8.h>
35 
36 #include <limits>
37 #include <thread>
38 
39 namespace android {
40 
41 using namespace std::chrono_literals;
42 // retrieve wide-color and hdr settings from configstore
43 using namespace android::hardware::configstore;
44 using namespace android::hardware::configstore::V1_0;
45 using ui::ColorMode;
46 
47 using Transaction = SurfaceComposerClient::Transaction;
48 
49 static bool hasWideColorDisplay =
50         getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
51 
52 static bool hasHdrDisplay =
53         getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
54 
55 class FakeSurfaceComposer;
56 class FakeProducerFrameEventHistory;
57 
58 static constexpr uint64_t NO_FRAME_INDEX = std::numeric_limits<uint64_t>::max();
59 
60 class SurfaceTest : public ::testing::Test {
61 protected:
SurfaceTest()62     SurfaceTest() {
63         ProcessState::self()->startThreadPool();
64     }
65 
SetUp()66     virtual void SetUp() {
67         mComposerClient = new SurfaceComposerClient;
68         ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
69 
70         // TODO(brianderson): The following sometimes fails and is a source of
71         //   test flakiness.
72         mSurfaceControl = mComposerClient->createSurface(
73                 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
74 
75         ASSERT_TRUE(mSurfaceControl != nullptr);
76         ASSERT_TRUE(mSurfaceControl->isValid());
77 
78         Transaction t;
79         ASSERT_EQ(NO_ERROR, t.setLayer(mSurfaceControl, 0x7fffffff)
80                 .show(mSurfaceControl)
81                 .apply());
82 
83         mSurface = mSurfaceControl->getSurface();
84         ASSERT_TRUE(mSurface != nullptr);
85     }
86 
TearDown()87     virtual void TearDown() {
88         mComposerClient->dispose();
89     }
90 
91     sp<Surface> mSurface;
92     sp<SurfaceComposerClient> mComposerClient;
93     sp<SurfaceControl> mSurfaceControl;
94 };
95 
TEST_F(SurfaceTest,CreateSurfaceReturnsErrorBadClient)96 TEST_F(SurfaceTest, CreateSurfaceReturnsErrorBadClient) {
97     mComposerClient->dispose();
98     ASSERT_EQ(NO_INIT, mComposerClient->initCheck());
99 
100     sp<SurfaceControl> sc;
101     status_t err = mComposerClient->createSurfaceChecked(
102             String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, &sc, 0);
103     ASSERT_EQ(NO_INIT, err);
104 }
105 
TEST_F(SurfaceTest,QueuesToWindowComposerIsTrueWhenVisible)106 TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
107     sp<ANativeWindow> anw(mSurface);
108     int result = -123;
109     int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
110             &result);
111     EXPECT_EQ(NO_ERROR, err);
112     EXPECT_EQ(1, result);
113 }
114 
TEST_F(SurfaceTest,QueuesToWindowComposerIsTrueWhenPurgatorized)115 TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
116     mSurfaceControl.clear();
117     // Wait for the async clean-up to complete.
118     std::this_thread::sleep_for(50ms);
119 
120     sp<ANativeWindow> anw(mSurface);
121     int result = -123;
122     int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
123             &result);
124     EXPECT_EQ(NO_ERROR, err);
125     EXPECT_EQ(1, result);
126 }
127 
128 // This test probably doesn't belong here.
TEST_F(SurfaceTest,ScreenshotsOfProtectedBuffersDontSucceed)129 TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersDontSucceed) {
130     sp<ANativeWindow> anw(mSurface);
131 
132     // Verify the screenshot works with no protected buffers.
133     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
134 
135     const sp<IBinder> display = sf->getInternalDisplayToken();
136     ASSERT_FALSE(display == nullptr);
137 
138     sp<GraphicBuffer> outBuffer;
139     bool ignored;
140     ASSERT_EQ(NO_ERROR,
141               sf->captureScreen(display, &outBuffer, ignored, ui::Dataspace::V0_SRGB,
142                                 ui::PixelFormat::RGBA_8888, Rect(), 64, 64, false));
143 
144     ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
145             NATIVE_WINDOW_API_CPU));
146     // Set the PROTECTED usage bit and verify that the screenshot fails.  Note
147     // that we need to dequeue a buffer in order for it to actually get
148     // allocated in SurfaceFlinger.
149     ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
150             GRALLOC_USAGE_PROTECTED));
151     ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
152     ANativeWindowBuffer* buf = nullptr;
153 
154     status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
155     if (err) {
156         // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
157         // that's okay as long as this is the reason for the failure.
158         // try again without the GRALLOC_USAGE_PROTECTED bit.
159         ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
160         ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
161                 &buf));
162         return;
163     }
164     ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
165 
166     for (int i = 0; i < 4; i++) {
167         // Loop to make sure SurfaceFlinger has retired a protected buffer.
168         ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
169                 &buf));
170         ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
171     }
172     ASSERT_EQ(NO_ERROR,
173               sf->captureScreen(display, &outBuffer, ignored, ui::Dataspace::V0_SRGB,
174                                 ui::PixelFormat::RGBA_8888, Rect(), 64, 64, false));
175 }
176 
TEST_F(SurfaceTest,ConcreteTypeIsSurface)177 TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
178     sp<ANativeWindow> anw(mSurface);
179     int result = -123;
180     int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
181     EXPECT_EQ(NO_ERROR, err);
182     EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
183 }
184 
TEST_F(SurfaceTest,LayerCountIsOne)185 TEST_F(SurfaceTest, LayerCountIsOne) {
186     sp<ANativeWindow> anw(mSurface);
187     int result = -123;
188     int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
189     EXPECT_EQ(NO_ERROR, err);
190     EXPECT_EQ(1, result);
191 }
192 
TEST_F(SurfaceTest,QueryConsumerUsage)193 TEST_F(SurfaceTest, QueryConsumerUsage) {
194     const int TEST_USAGE_FLAGS =
195             GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
196     sp<IGraphicBufferProducer> producer;
197     sp<IGraphicBufferConsumer> consumer;
198     BufferQueue::createBufferQueue(&producer, &consumer);
199     sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
200             TEST_USAGE_FLAGS);
201     sp<Surface> s = new Surface(producer);
202 
203     sp<ANativeWindow> anw(s);
204 
205     int flags = -1;
206     int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
207 
208     ASSERT_EQ(NO_ERROR, err);
209     ASSERT_EQ(TEST_USAGE_FLAGS, flags);
210 }
211 
TEST_F(SurfaceTest,QueryDefaultBuffersDataSpace)212 TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
213     const android_dataspace TEST_DATASPACE = HAL_DATASPACE_V0_SRGB;
214     sp<IGraphicBufferProducer> producer;
215     sp<IGraphicBufferConsumer> consumer;
216     BufferQueue::createBufferQueue(&producer, &consumer);
217     sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
218 
219     cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
220 
221     sp<Surface> s = new Surface(producer);
222 
223     sp<ANativeWindow> anw(s);
224 
225     android_dataspace dataSpace;
226 
227     int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
228             reinterpret_cast<int*>(&dataSpace));
229 
230     ASSERT_EQ(NO_ERROR, err);
231     ASSERT_EQ(TEST_DATASPACE, dataSpace);
232 }
233 
TEST_F(SurfaceTest,SettingGenerationNumber)234 TEST_F(SurfaceTest, SettingGenerationNumber) {
235     sp<IGraphicBufferProducer> producer;
236     sp<IGraphicBufferConsumer> consumer;
237     BufferQueue::createBufferQueue(&producer, &consumer);
238     sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
239     sp<Surface> surface = new Surface(producer);
240     sp<ANativeWindow> window(surface);
241 
242     // Allocate a buffer with a generation number of 0
243     ANativeWindowBuffer* buffer;
244     int fenceFd;
245     ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
246             NATIVE_WINDOW_API_CPU));
247     ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
248     ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
249 
250     // Detach the buffer and check its generation number
251     sp<GraphicBuffer> graphicBuffer;
252     sp<Fence> fence;
253     ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
254     ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
255 
256     ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
257     buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
258 
259     // This should change the generation number of the GraphicBuffer
260     ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
261 
262     // Check that the new generation number sticks with the buffer
263     ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
264     ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
265     graphicBuffer = static_cast<GraphicBuffer*>(buffer);
266     ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
267 }
268 
TEST_F(SurfaceTest,GetConsumerName)269 TEST_F(SurfaceTest, GetConsumerName) {
270     sp<IGraphicBufferProducer> producer;
271     sp<IGraphicBufferConsumer> consumer;
272     BufferQueue::createBufferQueue(&producer, &consumer);
273 
274     sp<DummyConsumer> dummyConsumer(new DummyConsumer);
275     consumer->consumerConnect(dummyConsumer, false);
276     consumer->setConsumerName(String8("TestConsumer"));
277 
278     sp<Surface> surface = new Surface(producer);
279     sp<ANativeWindow> window(surface);
280     native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
281 
282     EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
283 }
284 
TEST_F(SurfaceTest,GetWideColorSupport)285 TEST_F(SurfaceTest, GetWideColorSupport) {
286     sp<IGraphicBufferProducer> producer;
287     sp<IGraphicBufferConsumer> consumer;
288     BufferQueue::createBufferQueue(&producer, &consumer);
289 
290     sp<DummyConsumer> dummyConsumer(new DummyConsumer);
291     consumer->consumerConnect(dummyConsumer, false);
292     consumer->setConsumerName(String8("TestConsumer"));
293 
294     sp<Surface> surface = new Surface(producer);
295     sp<ANativeWindow> window(surface);
296     native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
297 
298     bool supported;
299     surface->getWideColorSupport(&supported);
300 
301     // NOTE: This test assumes that device that supports
302     // wide-color (as indicated by BoardConfig) must also
303     // have a wide-color primary display.
304     // That assumption allows this test to cover devices
305     // that advertised a wide-color color mode without
306     // actually supporting wide-color to pass this test
307     // as well as the case of a device that does support
308     // wide-color (via BoardConfig) and has a wide-color
309     // primary display.
310     // NOT covered at this time is a device that supports
311     // wide color in the BoardConfig but does not support
312     // a wide-color color mode on the primary display.
313     ASSERT_EQ(hasWideColorDisplay, supported);
314 }
315 
TEST_F(SurfaceTest,GetHdrSupport)316 TEST_F(SurfaceTest, GetHdrSupport) {
317     sp<IGraphicBufferProducer> producer;
318     sp<IGraphicBufferConsumer> consumer;
319     BufferQueue::createBufferQueue(&producer, &consumer);
320 
321     sp<DummyConsumer> dummyConsumer(new DummyConsumer);
322     consumer->consumerConnect(dummyConsumer, false);
323     consumer->setConsumerName(String8("TestConsumer"));
324 
325     sp<Surface> surface = new Surface(producer);
326     sp<ANativeWindow> window(surface);
327     native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
328 
329     bool supported;
330     status_t result = surface->getHdrSupport(&supported);
331     ASSERT_EQ(NO_ERROR, result);
332 
333     // NOTE: This is not a CTS test.
334     // This test verifies that when the BoardConfig TARGET_HAS_HDR_DISPLAY
335     // is TRUE, getHdrSupport is also true.
336     // TODO: Add check for an HDR color mode on the primary display.
337     ASSERT_EQ(hasHdrDisplay, supported);
338 }
339 
TEST_F(SurfaceTest,SetHdrMetadata)340 TEST_F(SurfaceTest, SetHdrMetadata) {
341     sp<IGraphicBufferProducer> producer;
342     sp<IGraphicBufferConsumer> consumer;
343     BufferQueue::createBufferQueue(&producer, &consumer);
344 
345     sp<DummyConsumer> dummyConsumer(new DummyConsumer);
346     consumer->consumerConnect(dummyConsumer, false);
347     consumer->setConsumerName(String8("TestConsumer"));
348 
349     sp<Surface> surface = new Surface(producer);
350     sp<ANativeWindow> window(surface);
351     native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
352 
353     bool supported;
354     status_t result = surface->getHdrSupport(&supported);
355     ASSERT_EQ(NO_ERROR, result);
356 
357     if (!hasHdrDisplay || !supported) {
358         return;
359     }
360     const android_smpte2086_metadata smpte2086 = {
361         {0.680, 0.320},
362         {0.265, 0.690},
363         {0.150, 0.060},
364         {0.3127, 0.3290},
365         100.0,
366         0.1,
367     };
368     const android_cta861_3_metadata cta861_3 = {
369         78.0,
370         62.0,
371     };
372 
373     std::vector<uint8_t> hdr10plus;
374     hdr10plus.push_back(0xff);
375 
376     int error = native_window_set_buffers_smpte2086_metadata(window.get(), &smpte2086);
377     ASSERT_EQ(error, NO_ERROR);
378     error = native_window_set_buffers_cta861_3_metadata(window.get(), &cta861_3);
379     ASSERT_EQ(error, NO_ERROR);
380     error = native_window_set_buffers_hdr10_plus_metadata(window.get(), hdr10plus.size(),
381                                                           hdr10plus.data());
382     ASSERT_EQ(error, NO_ERROR);
383 }
384 
TEST_F(SurfaceTest,DynamicSetBufferCount)385 TEST_F(SurfaceTest, DynamicSetBufferCount) {
386     sp<IGraphicBufferProducer> producer;
387     sp<IGraphicBufferConsumer> consumer;
388     BufferQueue::createBufferQueue(&producer, &consumer);
389 
390     sp<DummyConsumer> dummyConsumer(new DummyConsumer);
391     consumer->consumerConnect(dummyConsumer, false);
392     consumer->setConsumerName(String8("TestConsumer"));
393 
394     sp<Surface> surface = new Surface(producer);
395     sp<ANativeWindow> window(surface);
396 
397     ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
398             NATIVE_WINDOW_API_CPU));
399     native_window_set_buffer_count(window.get(), 4);
400 
401     int fence;
402     ANativeWindowBuffer* buffer;
403     ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
404     native_window_set_buffer_count(window.get(), 3);
405     ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
406     native_window_set_buffer_count(window.get(), 2);
407     ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
408     ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
409 }
410 
TEST_F(SurfaceTest,GetAndFlushRemovedBuffers)411 TEST_F(SurfaceTest, GetAndFlushRemovedBuffers) {
412     sp<IGraphicBufferProducer> producer;
413     sp<IGraphicBufferConsumer> consumer;
414     BufferQueue::createBufferQueue(&producer, &consumer);
415 
416     sp<DummyConsumer> dummyConsumer(new DummyConsumer);
417     consumer->consumerConnect(dummyConsumer, false);
418     consumer->setConsumerName(String8("TestConsumer"));
419 
420     sp<Surface> surface = new Surface(producer);
421     sp<ANativeWindow> window(surface);
422     sp<DummyProducerListener> listener = new DummyProducerListener();
423     ASSERT_EQ(OK, surface->connect(
424             NATIVE_WINDOW_API_CPU,
425             /*listener*/listener,
426             /*reportBufferRemoval*/true));
427     const int BUFFER_COUNT = 4;
428     ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
429 
430     sp<GraphicBuffer> detachedBuffer;
431     sp<Fence> outFence;
432     int fences[BUFFER_COUNT];
433     ANativeWindowBuffer* buffers[BUFFER_COUNT];
434     // Allocate buffers because detachNextBuffer requires allocated buffers
435     for (int i = 0; i < BUFFER_COUNT; i++) {
436         ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
437     }
438     for (int i = 0; i < BUFFER_COUNT; i++) {
439         ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
440     }
441 
442     // Test detached buffer is correctly reported
443     ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
444     std::vector<sp<GraphicBuffer>> removedBuffers;
445     ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
446     ASSERT_EQ(1u, removedBuffers.size());
447     ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
448     // Test the list is flushed one getAndFlushRemovedBuffers returns
449     ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
450     ASSERT_EQ(0u, removedBuffers.size());
451 
452 
453     // Test removed buffer list is cleanup after next dequeueBuffer call
454     ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
455     ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[0], &fences[0]));
456     ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
457     ASSERT_EQ(0u, removedBuffers.size());
458     ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[0], fences[0]));
459 
460     // Test removed buffer list is cleanup after next detachNextBuffer call
461     ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
462     ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
463     ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
464     ASSERT_EQ(1u, removedBuffers.size());
465     ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
466 
467     // Re-allocate buffers since all buffers are detached up to now
468     for (int i = 0; i < BUFFER_COUNT; i++) {
469         ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
470     }
471     for (int i = 0; i < BUFFER_COUNT; i++) {
472         ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
473     }
474 
475     ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
476     ASSERT_EQ(NO_ERROR, surface->attachBuffer(detachedBuffer.get()));
477     ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
478     // Depends on which slot GraphicBufferProducer impl pick, the attach call might
479     // get 0 or 1 buffer removed.
480     ASSERT_LE(removedBuffers.size(), 1u);
481 }
482 
TEST_F(SurfaceTest,TestGetLastDequeueStartTime)483 TEST_F(SurfaceTest, TestGetLastDequeueStartTime) {
484     sp<ANativeWindow> anw(mSurface);
485     ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU));
486 
487     ANativeWindowBuffer* buffer = nullptr;
488     int32_t fenceFd = -1;
489 
490     nsecs_t before = systemTime(CLOCK_MONOTONIC);
491     anw->dequeueBuffer(anw.get(), &buffer, &fenceFd);
492     nsecs_t after = systemTime(CLOCK_MONOTONIC);
493 
494     nsecs_t lastDequeueTime = mSurface->getLastDequeueStartTime();
495     ASSERT_LE(before, lastDequeueTime);
496     ASSERT_GE(after, lastDequeueTime);
497 }
498 
499 class FakeConsumer : public BnConsumerListener {
500 public:
onFrameAvailable(const BufferItem &)501     void onFrameAvailable(const BufferItem& /*item*/) override {}
onBuffersReleased()502     void onBuffersReleased() override {}
onSidebandStreamChanged()503     void onSidebandStreamChanged() override {}
504 
addAndGetFrameTimestamps(const NewFrameEventsEntry * newTimestamps,FrameEventHistoryDelta * outDelta)505     void addAndGetFrameTimestamps(
506             const NewFrameEventsEntry* newTimestamps,
507             FrameEventHistoryDelta* outDelta) override {
508         if (newTimestamps) {
509             if (mGetFrameTimestampsEnabled) {
510                 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
511                         "Test should set mNewFrameEntryOverride before queuing "
512                         "a frame.";
513                 EXPECT_EQ(newTimestamps->frameNumber,
514                         mNewFrameEntryOverride.frameNumber) <<
515                         "Test attempting to add NewFrameEntryOverride with "
516                         "incorrect frame number.";
517                 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
518                 mNewFrameEntryOverride.frameNumber = 0;
519             }
520             mAddFrameTimestampsCount++;
521             mLastAddedFrameNumber = newTimestamps->frameNumber;
522         }
523         if (outDelta) {
524             mFrameEventHistory.getAndResetDelta(outDelta);
525             mGetFrameTimestampsCount++;
526         }
527         mAddAndGetFrameTimestampsCallCount++;
528     }
529 
530     bool mGetFrameTimestampsEnabled = false;
531 
532     ConsumerFrameEventHistory mFrameEventHistory;
533     int mAddAndGetFrameTimestampsCallCount = 0;
534     int mAddFrameTimestampsCount = 0;
535     int mGetFrameTimestampsCount = 0;
536     uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
537 
538     NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
539 };
540 
541 
542 class FakeSurfaceComposer : public ISurfaceComposer{
543 public:
~FakeSurfaceComposer()544     ~FakeSurfaceComposer() override {}
545 
setSupportsPresent(bool supportsPresent)546     void setSupportsPresent(bool supportsPresent) {
547         mSupportsPresent = supportsPresent;
548     }
549 
createConnection()550     sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
createDisplayEventConnection(ISurfaceComposer::VsyncSource)551     sp<IDisplayEventConnection> createDisplayEventConnection(ISurfaceComposer::VsyncSource)
552             override {
553         return nullptr;
554     }
createDisplay(const String8 &,bool)555     sp<IBinder> createDisplay(const String8& /*displayName*/,
556             bool /*secure*/) override { return nullptr; }
destroyDisplay(const sp<IBinder> &)557     void destroyDisplay(const sp<IBinder>& /*display */) override {}
getPhysicalDisplayIds() const558     std::vector<PhysicalDisplayId> getPhysicalDisplayIds() const override { return {}; }
getPhysicalDisplayToken(PhysicalDisplayId) const559     sp<IBinder> getPhysicalDisplayToken(PhysicalDisplayId) const override { return nullptr; }
setTransactionState(const Vector<ComposerState> &,const Vector<DisplayState> &,uint32_t,const sp<IBinder> &,const InputWindowCommands &,int64_t,const client_cache_t &,const std::vector<ListenerCallbacks> &)560     void setTransactionState(const Vector<ComposerState>& /*state*/,
561                              const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/,
562                              const sp<IBinder>& /*applyToken*/,
563                              const InputWindowCommands& /*inputWindowCommands*/,
564                              int64_t /*desiredPresentTime*/, const client_cache_t& /*cachedBuffer*/,
565                              const std::vector<ListenerCallbacks>& /*listenerCallbacks*/) override {
566     }
567 
bootFinished()568     void bootFinished() override {}
authenticateSurfaceTexture(const sp<IGraphicBufferProducer> &) const569     bool authenticateSurfaceTexture(
570             const sp<IGraphicBufferProducer>& /*surface*/) const override {
571         return false;
572     }
573 
getSupportedFrameTimestamps(std::vector<FrameEvent> * outSupported) const574     status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
575             const override {
576         *outSupported = {
577                 FrameEvent::REQUESTED_PRESENT,
578                 FrameEvent::ACQUIRE,
579                 FrameEvent::LATCH,
580                 FrameEvent::FIRST_REFRESH_START,
581                 FrameEvent::LAST_REFRESH_START,
582                 FrameEvent::GPU_COMPOSITION_DONE,
583                 FrameEvent::DEQUEUE_READY,
584                 FrameEvent::RELEASE
585         };
586         if (mSupportsPresent) {
587             outSupported->push_back(
588                         FrameEvent::DISPLAY_PRESENT);
589         }
590         return NO_ERROR;
591     }
592 
setPowerMode(const sp<IBinder> &,int)593     void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
getDisplayConfigs(const sp<IBinder> &,Vector<DisplayInfo> *)594     status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
595             Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
getDisplayStats(const sp<IBinder> &,DisplayStatInfo *)596     status_t getDisplayStats(const sp<IBinder>& /*display*/,
597             DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
getActiveConfig(const sp<IBinder> &)598     int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
setActiveConfig(const sp<IBinder> &,int)599     status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
600             override {
601         return NO_ERROR;
602     }
getDisplayColorModes(const sp<IBinder> &,Vector<ColorMode> *)603     status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
604             Vector<ColorMode>* /*outColorModes*/) override {
605         return NO_ERROR;
606     }
getDisplayNativePrimaries(const sp<IBinder> &,ui::DisplayPrimaries &)607     status_t getDisplayNativePrimaries(const sp<IBinder>& /*display*/,
608             ui::DisplayPrimaries& /*primaries*/) override {
609         return NO_ERROR;
610     }
getActiveColorMode(const sp<IBinder> &)611     ColorMode getActiveColorMode(const sp<IBinder>& /*display*/)
612             override {
613         return ColorMode::NATIVE;
614     }
setActiveColorMode(const sp<IBinder> &,ColorMode)615     status_t setActiveColorMode(const sp<IBinder>& /*display*/,
616         ColorMode /*colorMode*/) override { return NO_ERROR; }
captureScreen(const sp<IBinder> &,sp<GraphicBuffer> *,bool &,const ui::Dataspace,const ui::PixelFormat,Rect,uint32_t,uint32_t,bool,Rotation,bool)617     status_t captureScreen(const sp<IBinder>& /*display*/, sp<GraphicBuffer>* /*outBuffer*/,
618                            bool& /* outCapturedSecureLayers */,
619                            const ui::Dataspace /*reqDataspace*/,
620                            const ui::PixelFormat /*reqPixelFormat*/, Rect /*sourceCrop*/,
621                            uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
622                            bool /*useIdentityTransform*/, Rotation /*rotation*/,
623                            bool /*captureSecureLayers*/) override {
624         return NO_ERROR;
625     }
captureScreen(uint64_t,ui::Dataspace *,sp<GraphicBuffer> *)626     status_t captureScreen(uint64_t /*displayOrLayerStack*/, ui::Dataspace* /*outDataspace*/,
627                            sp<GraphicBuffer>* /*outBuffer*/) override {
628         return NO_ERROR;
629     }
captureLayers(const sp<IBinder> &,sp<GraphicBuffer> *,const ui::Dataspace,const ui::PixelFormat,const Rect &,const std::unordered_set<sp<IBinder>,ISurfaceComposer::SpHash<IBinder>> &,float,bool)630     virtual status_t captureLayers(
631             const sp<IBinder>& /*parentHandle*/, sp<GraphicBuffer>* /*outBuffer*/,
632             const ui::Dataspace /*reqDataspace*/, const ui::PixelFormat /*reqPixelFormat*/,
633             const Rect& /*sourceCrop*/,
634             const std::unordered_set<sp<IBinder>,
635                                      ISurfaceComposer::SpHash<IBinder>>& /*excludeHandles*/,
636             float /*frameScale*/, bool /*childrenOnly*/) override {
637         return NO_ERROR;
638     }
clearAnimationFrameStats()639     status_t clearAnimationFrameStats() override { return NO_ERROR; }
getAnimationFrameStats(FrameStats *) const640     status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
641         return NO_ERROR;
642     }
getHdrCapabilities(const sp<IBinder> &,HdrCapabilities *) const643     status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
644             HdrCapabilities* /*outCapabilities*/) const override {
645         return NO_ERROR;
646     }
enableVSyncInjections(bool)647     status_t enableVSyncInjections(bool /*enable*/) override {
648         return NO_ERROR;
649     }
injectVSync(nsecs_t)650     status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
getLayerDebugInfo(std::vector<LayerDebugInfo> *)651     status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) override {
652         return NO_ERROR;
653     }
getCompositionPreference(ui::Dataspace *,ui::PixelFormat *,ui::Dataspace *,ui::PixelFormat *) const654     status_t getCompositionPreference(
655             ui::Dataspace* /*outDefaultDataspace*/, ui::PixelFormat* /*outDefaultPixelFormat*/,
656             ui::Dataspace* /*outWideColorGamutDataspace*/,
657             ui::PixelFormat* /*outWideColorGamutPixelFormat*/) const override {
658         return NO_ERROR;
659     }
getDisplayedContentSamplingAttributes(const sp<IBinder> &,ui::PixelFormat *,ui::Dataspace *,uint8_t *) const660     status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& /*display*/,
661                                                    ui::PixelFormat* /*outFormat*/,
662                                                    ui::Dataspace* /*outDataspace*/,
663                                                    uint8_t* /*outComponentMask*/) const override {
664         return NO_ERROR;
665     }
setDisplayContentSamplingEnabled(const sp<IBinder> &,bool,uint8_t,uint64_t) const666     status_t setDisplayContentSamplingEnabled(const sp<IBinder>& /*display*/, bool /*enable*/,
667                                               uint8_t /*componentMask*/,
668                                               uint64_t /*maxFrames*/) const override {
669         return NO_ERROR;
670     }
getDisplayedContentSample(const sp<IBinder> &,uint64_t,uint64_t,DisplayedFrameStats *) const671     status_t getDisplayedContentSample(const sp<IBinder>& /*display*/, uint64_t /*maxFrames*/,
672                                        uint64_t /*timestamp*/,
673                                        DisplayedFrameStats* /*outStats*/) const override {
674         return NO_ERROR;
675     }
676 
getColorManagement(bool *) const677     status_t getColorManagement(bool* /*outGetColorManagement*/) const override { return NO_ERROR; }
getProtectedContentSupport(bool *) const678     status_t getProtectedContentSupport(bool* /*outSupported*/) const override { return NO_ERROR; }
679 
isWideColorDisplay(const sp<IBinder> &,bool *) const680     status_t isWideColorDisplay(const sp<IBinder>&, bool*) const override { return NO_ERROR; }
getDisplayBrightnessSupport(const sp<IBinder> &,bool *) const681     status_t getDisplayBrightnessSupport(const sp<IBinder>& /*displayToken*/,
682                                          bool* /*outSupport*/) const override {
683         return NO_ERROR;
684     }
setDisplayBrightness(const sp<IBinder> &,float) const685     status_t setDisplayBrightness(const sp<IBinder>& /*displayToken*/,
686                                   float /*brightness*/) const override {
687         return NO_ERROR;
688     }
689 
addRegionSamplingListener(const Rect &,const sp<IBinder> &,const sp<IRegionSamplingListener> &)690     status_t addRegionSamplingListener(const Rect& /*samplingArea*/,
691                                        const sp<IBinder>& /*stopLayerHandle*/,
692                                        const sp<IRegionSamplingListener>& /*listener*/) override {
693         return NO_ERROR;
694     }
removeRegionSamplingListener(const sp<IRegionSamplingListener> &)695     status_t removeRegionSamplingListener(
696             const sp<IRegionSamplingListener>& /*listener*/) override {
697         return NO_ERROR;
698     }
setAllowedDisplayConfigs(const sp<IBinder> &,const std::vector<int32_t> &)699     status_t setAllowedDisplayConfigs(const sp<IBinder>& /*displayToken*/,
700                                       const std::vector<int32_t>& /*allowedConfigs*/) override {
701         return NO_ERROR;
702     }
getAllowedDisplayConfigs(const sp<IBinder> &,std::vector<int32_t> *)703     status_t getAllowedDisplayConfigs(const sp<IBinder>& /*displayToken*/,
704                                       std::vector<int32_t>* /*outAllowedConfigs*/) override {
705         return NO_ERROR;
706     }
notifyPowerHint(int32_t)707     status_t notifyPowerHint(int32_t /*hintId*/) override { return NO_ERROR; }
708 
709 protected:
onAsBinder()710     IBinder* onAsBinder() override { return nullptr; }
711 
712 private:
713     bool mSupportsPresent{true};
714 };
715 
716 class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
717 public:
FakeProducerFrameEventHistory(FenceToFenceTimeMap * fenceMap)718     explicit FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap) : mFenceMap(fenceMap) {}
719 
~FakeProducerFrameEventHistory()720     ~FakeProducerFrameEventHistory() {}
721 
updateAcquireFence(uint64_t frameNumber,std::shared_ptr<FenceTime> && acquire)722     void updateAcquireFence(uint64_t frameNumber,
723             std::shared_ptr<FenceTime>&& acquire) override {
724         // Verify the acquire fence being added isn't the one from the consumer.
725         EXPECT_NE(mConsumerAcquireFence, acquire);
726         // Override the fence, so we can verify this was called by the
727         // producer after the frame is queued.
728         ProducerFrameEventHistory::updateAcquireFence(frameNumber,
729                 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
730     }
731 
setAcquireFenceOverride(const std::shared_ptr<FenceTime> & acquireFenceOverride,const std::shared_ptr<FenceTime> & consumerAcquireFence)732     void setAcquireFenceOverride(
733             const std::shared_ptr<FenceTime>& acquireFenceOverride,
734             const std::shared_ptr<FenceTime>& consumerAcquireFence) {
735         mAcquireFenceOverride = acquireFenceOverride;
736         mConsumerAcquireFence = consumerAcquireFence;
737     }
738 
739 protected:
createFenceTime(const sp<Fence> & fence) const740     std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
741             const override {
742         return mFenceMap->createFenceTimeForTest(fence);
743     }
744 
745     FenceToFenceTimeMap* mFenceMap{nullptr};
746 
747     std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
748     std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
749 };
750 
751 
752 class TestSurface : public Surface {
753 public:
TestSurface(const sp<IGraphicBufferProducer> & bufferProducer,FenceToFenceTimeMap * fenceMap)754     TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
755             FenceToFenceTimeMap* fenceMap)
756         : Surface(bufferProducer),
757           mFakeSurfaceComposer(new FakeSurfaceComposer) {
758         mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
759         mFrameEventHistory.reset(mFakeFrameEventHistory);
760     }
761 
~TestSurface()762     ~TestSurface() override {}
763 
composerService() const764     sp<ISurfaceComposer> composerService() const override {
765         return mFakeSurfaceComposer;
766     }
767 
now() const768     nsecs_t now() const override {
769         return mNow;
770     }
771 
setNow(nsecs_t now)772     void setNow(nsecs_t now) {
773         mNow = now;
774     }
775 
776 public:
777     sp<FakeSurfaceComposer> mFakeSurfaceComposer;
778     nsecs_t mNow = 0;
779 
780     // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
781     // but this raw pointer gives access to test functionality.
782     FakeProducerFrameEventHistory* mFakeFrameEventHistory;
783 };
784 
785 
786 class GetFrameTimestampsTest : public ::testing::Test {
787 protected:
788     struct FenceAndFenceTime {
FenceAndFenceTimeandroid::GetFrameTimestampsTest::FenceAndFenceTime789         explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
790            : mFence(new Fence),
791              mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
792         sp<Fence> mFence { nullptr };
793         std::shared_ptr<FenceTime> mFenceTime { nullptr };
794     };
795 
796     struct RefreshEvents {
RefreshEventsandroid::GetFrameTimestampsTest::RefreshEvents797         RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
798           : mFenceMap(fenceMap),
799             kCompositorTiming(
800                 {refreshStart, refreshStart + 1, refreshStart + 2 }),
801             kStartTime(refreshStart + 3),
802             kGpuCompositionDoneTime(refreshStart + 4),
803             kPresentTime(refreshStart + 5) {}
804 
signalPostCompositeFencesandroid::GetFrameTimestampsTest::RefreshEvents805         void signalPostCompositeFences() {
806             mFenceMap.signalAllForTest(
807                         mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
808             mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
809         }
810 
811         FenceToFenceTimeMap& mFenceMap;
812 
813         FenceAndFenceTime mGpuCompositionDone { mFenceMap };
814         FenceAndFenceTime mPresent { mFenceMap };
815 
816         const CompositorTiming kCompositorTiming;
817 
818         const nsecs_t kStartTime;
819         const nsecs_t kGpuCompositionDoneTime;
820         const nsecs_t kPresentTime;
821     };
822 
823     struct FrameEvents {
FrameEventsandroid::GetFrameTimestampsTest::FrameEvents824         FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
825             : mFenceMap(fenceMap),
826               kPostedTime(frameStartTime + 100),
827               kRequestedPresentTime(frameStartTime + 200),
828               kProducerAcquireTime(frameStartTime + 300),
829               kConsumerAcquireTime(frameStartTime + 301),
830               kLatchTime(frameStartTime + 500),
831               kDequeueReadyTime(frameStartTime + 600),
832               kReleaseTime(frameStartTime + 700),
833               mRefreshes {
834                     { mFenceMap, frameStartTime + 410 },
835                     { mFenceMap, frameStartTime + 420 },
836                     { mFenceMap, frameStartTime + 430 } } {}
837 
signalQueueFencesandroid::GetFrameTimestampsTest::FrameEvents838         void signalQueueFences() {
839             mFenceMap.signalAllForTest(
840                         mAcquireConsumer.mFence, kConsumerAcquireTime);
841             mFenceMap.signalAllForTest(
842                         mAcquireProducer.mFence, kProducerAcquireTime);
843         }
844 
signalRefreshFencesandroid::GetFrameTimestampsTest::FrameEvents845         void signalRefreshFences() {
846             for (auto& re : mRefreshes) {
847                 re.signalPostCompositeFences();
848             }
849         }
850 
signalReleaseFencesandroid::GetFrameTimestampsTest::FrameEvents851         void signalReleaseFences() {
852             mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
853         }
854 
855         FenceToFenceTimeMap& mFenceMap;
856 
857         FenceAndFenceTime mAcquireConsumer { mFenceMap };
858         FenceAndFenceTime mAcquireProducer { mFenceMap };
859         FenceAndFenceTime mRelease { mFenceMap };
860 
861         const nsecs_t kPostedTime;
862         const nsecs_t kRequestedPresentTime;
863         const nsecs_t kProducerAcquireTime;
864         const nsecs_t kConsumerAcquireTime;
865         const nsecs_t kLatchTime;
866         const nsecs_t kDequeueReadyTime;
867         const nsecs_t kReleaseTime;
868 
869         RefreshEvents mRefreshes[3];
870     };
871 
GetFrameTimestampsTest()872     GetFrameTimestampsTest() {}
873 
SetUp()874     virtual void SetUp() {
875         BufferQueue::createBufferQueue(&mProducer, &mConsumer);
876         mFakeConsumer = new FakeConsumer;
877         mCfeh = &mFakeConsumer->mFrameEventHistory;
878         mConsumer->consumerConnect(mFakeConsumer, false);
879         mConsumer->setConsumerName(String8("TestConsumer"));
880         mSurface = new TestSurface(mProducer, &mFenceMap);
881         mWindow = mSurface;
882 
883         ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
884                 NATIVE_WINDOW_API_CPU));
885         native_window_set_buffer_count(mWindow.get(), 4);
886     }
887 
disableFrameTimestamps()888     void disableFrameTimestamps() {
889         mFakeConsumer->mGetFrameTimestampsEnabled = false;
890         native_window_enable_frame_timestamps(mWindow.get(), 0);
891         mFrameTimestampsEnabled = false;
892     }
893 
enableFrameTimestamps()894     void enableFrameTimestamps() {
895         mFakeConsumer->mGetFrameTimestampsEnabled = true;
896         native_window_enable_frame_timestamps(mWindow.get(), 1);
897         mFrameTimestampsEnabled = true;
898     }
899 
getAllFrameTimestamps(uint64_t frameId)900     int getAllFrameTimestamps(uint64_t frameId) {
901         return native_window_get_frame_timestamps(mWindow.get(), frameId,
902                 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
903                 &outFirstRefreshStartTime, &outLastRefreshStartTime,
904                 &outGpuCompositionDoneTime, &outDisplayPresentTime,
905                 &outDequeueReadyTime, &outReleaseTime);
906     }
907 
resetTimestamps()908     void resetTimestamps() {
909         outRequestedPresentTime = -1;
910         outAcquireTime = -1;
911         outLatchTime = -1;
912         outFirstRefreshStartTime = -1;
913         outLastRefreshStartTime = -1;
914         outGpuCompositionDoneTime = -1;
915         outDisplayPresentTime = -1;
916         outDequeueReadyTime = -1;
917         outReleaseTime = -1;
918     }
919 
getNextFrameId()920     uint64_t getNextFrameId() {
921         uint64_t frameId = -1;
922         int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
923         EXPECT_EQ(status, NO_ERROR);
924         return frameId;
925     }
926 
dequeueAndQueue(uint64_t frameIndex)927     void dequeueAndQueue(uint64_t frameIndex) {
928         int fence = -1;
929         ANativeWindowBuffer* buffer = nullptr;
930         ASSERT_EQ(NO_ERROR,
931                 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
932 
933         int oldAddFrameTimestampsCount =
934                 mFakeConsumer->mAddFrameTimestampsCount;
935 
936         FrameEvents* frame = &mFrames[frameIndex];
937         uint64_t frameNumber = frameIndex + 1;
938 
939         NewFrameEventsEntry fe;
940         fe.frameNumber = frameNumber;
941         fe.postedTime = frame->kPostedTime;
942         fe.requestedPresentTime = frame->kRequestedPresentTime;
943         fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
944         mFakeConsumer->mNewFrameEntryOverride = fe;
945 
946         mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
947                     frame->mAcquireProducer.mFenceTime,
948                     frame->mAcquireConsumer.mFenceTime);
949 
950         ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
951 
952         EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
953 
954         EXPECT_EQ(
955                 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
956                 mFakeConsumer->mAddFrameTimestampsCount);
957     }
958 
addFrameEvents(bool gpuComposited,uint64_t iOldFrame,int64_t iNewFrame)959     void addFrameEvents(
960             bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
961         FrameEvents* oldFrame =
962                 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
963         FrameEvents* newFrame = &mFrames[iNewFrame];
964 
965         uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
966         uint64_t nNewFrame = iNewFrame + 1;
967 
968         // Latch, Composite, and Release the frames in a plausible order.
969         // Note: The timestamps won't necessarily match the order, but
970         // that's okay for the purposes of this test.
971         std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
972 
973         // Composite the previous frame one more time, which helps verify
974         // LastRefresh is updated properly.
975         if (oldFrame != nullptr) {
976             mCfeh->addPreComposition(nOldFrame,
977                                      oldFrame->mRefreshes[2].kStartTime);
978             gpuDoneFenceTime = gpuComposited ?
979                     oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
980                     FenceTime::NO_FENCE;
981             mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
982                     oldFrame->mRefreshes[2].mPresent.mFenceTime,
983                     oldFrame->mRefreshes[2].kCompositorTiming);
984         }
985 
986         // Latch the new frame.
987         mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
988 
989         mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
990         gpuDoneFenceTime = gpuComposited ?
991                 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
992                 FenceTime::NO_FENCE;
993         // HWC2 releases the previous buffer after a new latch just before
994         // calling postComposition.
995         if (oldFrame != nullptr) {
996             mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
997                     std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
998         }
999         mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
1000                 newFrame->mRefreshes[0].mPresent.mFenceTime,
1001                 newFrame->mRefreshes[0].kCompositorTiming);
1002 
1003         mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
1004         gpuDoneFenceTime = gpuComposited ?
1005                 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
1006                 FenceTime::NO_FENCE;
1007         mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
1008                 newFrame->mRefreshes[1].mPresent.mFenceTime,
1009                 newFrame->mRefreshes[1].kCompositorTiming);
1010     }
1011 
1012     sp<IGraphicBufferProducer> mProducer;
1013     sp<IGraphicBufferConsumer> mConsumer;
1014     sp<FakeConsumer> mFakeConsumer;
1015     ConsumerFrameEventHistory* mCfeh;
1016     sp<TestSurface> mSurface;
1017     sp<ANativeWindow> mWindow;
1018 
1019     FenceToFenceTimeMap mFenceMap;
1020 
1021     bool mFrameTimestampsEnabled = false;
1022 
1023     int64_t outRequestedPresentTime = -1;
1024     int64_t outAcquireTime = -1;
1025     int64_t outLatchTime = -1;
1026     int64_t outFirstRefreshStartTime = -1;
1027     int64_t outLastRefreshStartTime = -1;
1028     int64_t outGpuCompositionDoneTime = -1;
1029     int64_t outDisplayPresentTime = -1;
1030     int64_t outDequeueReadyTime = -1;
1031     int64_t outReleaseTime = -1;
1032 
1033     FrameEvents mFrames[3] {
1034         { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
1035 };
1036 
1037 
1038 // This test verifies that the frame timestamps are not retrieved when not
1039 // explicitly enabled via native_window_enable_frame_timestamps.
1040 // We want to check this to make sure there's no overhead for users
1041 // that don't need the timestamp information.
TEST_F(GetFrameTimestampsTest,DefaultDisabled)1042 TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
1043     int fence;
1044     ANativeWindowBuffer* buffer;
1045 
1046     EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1047     EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1048 
1049     const uint64_t fId = getNextFrameId();
1050 
1051     // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
1052     ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1053     EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1054     EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1055 
1056     // Verify the producer doesn't get frame timestamps piggybacked on queue.
1057     // It is okay that frame timestamps are added in the consumer since it is
1058     // still needed for SurfaceFlinger dumps.
1059     ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1060     EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1061     EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1062 
1063     // Verify attempts to get frame timestamps fail.
1064     int result = getAllFrameTimestamps(fId);
1065     EXPECT_EQ(INVALID_OPERATION, result);
1066     EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1067 
1068     // Verify compositor timing query fails.
1069     nsecs_t compositeDeadline = 0;
1070     nsecs_t compositeInterval = 0;
1071     nsecs_t compositeToPresentLatency = 0;
1072     result = native_window_get_compositor_timing(mWindow.get(),
1073         &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1074     EXPECT_EQ(INVALID_OPERATION, result);
1075 }
1076 
1077 // This test verifies that the frame timestamps are retrieved if explicitly
1078 // enabled via native_window_enable_frame_timestamps.
TEST_F(GetFrameTimestampsTest,EnabledSimple)1079 TEST_F(GetFrameTimestampsTest, EnabledSimple) {
1080     CompositorTiming initialCompositorTiming {
1081         1000000000, // 1s deadline
1082         16666667, // 16ms interval
1083         50000000, // 50ms present latency
1084     };
1085     mCfeh->initializeCompositorTiming(initialCompositorTiming);
1086 
1087     enableFrameTimestamps();
1088 
1089     // Verify the compositor timing query gets the initial compositor values
1090     // after timststamps are enabled; even before the first frame is queued
1091     // or dequeued.
1092     nsecs_t compositeDeadline = 0;
1093     nsecs_t compositeInterval = 0;
1094     nsecs_t compositeToPresentLatency = 0;
1095     mSurface->setNow(initialCompositorTiming.deadline - 1);
1096     int result = native_window_get_compositor_timing(mWindow.get(),
1097         &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1098     EXPECT_EQ(NO_ERROR, result);
1099     EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1100     EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1101     EXPECT_EQ(initialCompositorTiming.presentLatency,
1102               compositeToPresentLatency);
1103 
1104     int fence;
1105     ANativeWindowBuffer* buffer;
1106 
1107     EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1108     EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
1109 
1110     const uint64_t fId1 = getNextFrameId();
1111 
1112     // Verify getFrameTimestamps is piggybacked on dequeue.
1113     ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1114     EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1115     EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
1116 
1117     NewFrameEventsEntry f1;
1118     f1.frameNumber = 1;
1119     f1.postedTime = mFrames[0].kPostedTime;
1120     f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1121     f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1122     mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1123             mFrames[0].mAcquireProducer.mFenceTime,
1124             mFrames[0].mAcquireConsumer.mFenceTime);
1125     mFakeConsumer->mNewFrameEntryOverride = f1;
1126     mFrames[0].signalQueueFences();
1127 
1128     // Verify getFrameTimestamps is piggybacked on queue.
1129     ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1130     EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1131     EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
1132     EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
1133 
1134     // Verify queries for timestamps that the producer doesn't know about
1135     // triggers a call to see if the consumer has any new timestamps.
1136     result = getAllFrameTimestamps(fId1);
1137     EXPECT_EQ(NO_ERROR, result);
1138     EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
1139 }
1140 
TEST_F(GetFrameTimestampsTest,QueryPresentSupported)1141 TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1142     bool displayPresentSupported = true;
1143     mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1144 
1145     // Verify supported bits are forwarded.
1146     int supportsPresent = -1;
1147     mWindow.get()->query(mWindow.get(),
1148             NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1149     EXPECT_EQ(displayPresentSupported, supportsPresent);
1150 }
1151 
TEST_F(GetFrameTimestampsTest,QueryPresentNotSupported)1152 TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1153     bool displayPresentSupported = false;
1154     mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1155 
1156     // Verify supported bits are forwarded.
1157     int supportsPresent = -1;
1158     mWindow.get()->query(mWindow.get(),
1159             NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1160     EXPECT_EQ(displayPresentSupported, supportsPresent);
1161 }
1162 
TEST_F(GetFrameTimestampsTest,SnapToNextTickBasic)1163 TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1164     nsecs_t phase = 4000;
1165     nsecs_t interval = 1000;
1166 
1167     // Timestamp in previous interval.
1168     nsecs_t timestamp = 3500;
1169     EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1170             timestamp, phase, interval));
1171 
1172     // Timestamp in next interval.
1173     timestamp = 4500;
1174     EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1175             timestamp, phase, interval));
1176 
1177     // Timestamp multiple intervals before.
1178     timestamp = 2500;
1179     EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1180             timestamp, phase, interval));
1181 
1182     // Timestamp multiple intervals after.
1183     timestamp = 6500;
1184     EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1185             timestamp, phase, interval));
1186 
1187     // Timestamp on previous interval.
1188     timestamp = 3000;
1189     EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1190             timestamp, phase, interval));
1191 
1192     // Timestamp on next interval.
1193     timestamp = 5000;
1194     EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1195             timestamp, phase, interval));
1196 
1197     // Timestamp equal to phase.
1198     timestamp = 4000;
1199     EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1200             timestamp, phase, interval));
1201 }
1202 
1203 // int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1204 // if the number of intervals elapsed is internally stored in an int.
TEST_F(GetFrameTimestampsTest,SnapToNextTickOverflow)1205 TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1206       nsecs_t phase = 0;
1207       nsecs_t interval = 4000;
1208       nsecs_t big_timestamp = 8635916564000;
1209       int32_t intervals = big_timestamp / interval;
1210 
1211       EXPECT_LT(intervals, 0);
1212       EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1213             big_timestamp, phase, interval));
1214       EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1215             big_timestamp, big_timestamp, interval));
1216 }
1217 
1218 // This verifies the compositor timing is updated by refresh events
1219 // and piggy backed on a queue, dequeue, and enabling of timestamps..
TEST_F(GetFrameTimestampsTest,CompositorTimingUpdatesBasic)1220 TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1221     CompositorTiming initialCompositorTiming {
1222         1000000000, // 1s deadline
1223         16666667, // 16ms interval
1224         50000000, // 50ms present latency
1225     };
1226     mCfeh->initializeCompositorTiming(initialCompositorTiming);
1227 
1228     enableFrameTimestamps();
1229 
1230     // We get the initial values before any frames are submitted.
1231     nsecs_t compositeDeadline = 0;
1232     nsecs_t compositeInterval = 0;
1233     nsecs_t compositeToPresentLatency = 0;
1234     mSurface->setNow(initialCompositorTiming.deadline - 1);
1235     int result = native_window_get_compositor_timing(mWindow.get(),
1236         &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1237     EXPECT_EQ(NO_ERROR, result);
1238     EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1239     EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1240     EXPECT_EQ(initialCompositorTiming.presentLatency,
1241               compositeToPresentLatency);
1242 
1243     dequeueAndQueue(0);
1244     addFrameEvents(true, NO_FRAME_INDEX, 0);
1245 
1246     // Still get the initial values because the frame events for frame 0
1247     // didn't get a chance to piggyback on a queue or dequeue yet.
1248     result = native_window_get_compositor_timing(mWindow.get(),
1249         &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1250     EXPECT_EQ(NO_ERROR, result);
1251     EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1252     EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1253     EXPECT_EQ(initialCompositorTiming.presentLatency,
1254               compositeToPresentLatency);
1255 
1256     dequeueAndQueue(1);
1257     addFrameEvents(true, 0, 1);
1258 
1259     // Now expect the composite values associated with frame 1.
1260     mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1261     result = native_window_get_compositor_timing(mWindow.get(),
1262         &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1263     EXPECT_EQ(NO_ERROR, result);
1264     EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1265             compositeDeadline);
1266     EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1267             compositeInterval);
1268     EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1269             compositeToPresentLatency);
1270 
1271     dequeueAndQueue(2);
1272     addFrameEvents(true, 1, 2);
1273 
1274     // Now expect the composite values associated with frame 2.
1275     mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1276     result = native_window_get_compositor_timing(mWindow.get(),
1277         &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1278     EXPECT_EQ(NO_ERROR, result);
1279     EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1280             compositeDeadline);
1281     EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1282             compositeInterval);
1283     EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1284             compositeToPresentLatency);
1285 
1286     // Re-enabling frame timestamps should get the latest values.
1287     disableFrameTimestamps();
1288     enableFrameTimestamps();
1289 
1290     // Now expect the composite values associated with frame 3.
1291     mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1292     result = native_window_get_compositor_timing(mWindow.get(),
1293         &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1294     EXPECT_EQ(NO_ERROR, result);
1295     EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1296             compositeDeadline);
1297     EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1298             compositeInterval);
1299     EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1300             compositeToPresentLatency);
1301 }
1302 
1303 // This verifies the compositor deadline properly snaps to the the next
1304 // deadline based on the current time.
TEST_F(GetFrameTimestampsTest,CompositorTimingDeadlineSnaps)1305 TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1306     CompositorTiming initialCompositorTiming {
1307         1000000000, // 1s deadline
1308         16666667, // 16ms interval
1309         50000000, // 50ms present latency
1310     };
1311     mCfeh->initializeCompositorTiming(initialCompositorTiming);
1312 
1313     enableFrameTimestamps();
1314 
1315     nsecs_t compositeDeadline = 0;
1316     nsecs_t compositeInterval = 0;
1317     nsecs_t compositeToPresentLatency = 0;
1318 
1319     // A "now" just before the deadline snaps to the deadline.
1320     mSurface->setNow(initialCompositorTiming.deadline - 1);
1321     int result = native_window_get_compositor_timing(mWindow.get(),
1322         &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1323     EXPECT_EQ(NO_ERROR, result);
1324     EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1325     nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1326     EXPECT_EQ(expectedDeadline, compositeDeadline);
1327 
1328     dequeueAndQueue(0);
1329     addFrameEvents(true, NO_FRAME_INDEX, 0);
1330 
1331     // A "now" just after the deadline snaps properly.
1332     mSurface->setNow(initialCompositorTiming.deadline + 1);
1333     result = native_window_get_compositor_timing(mWindow.get(),
1334         &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1335     EXPECT_EQ(NO_ERROR, result);
1336     expectedDeadline =
1337             initialCompositorTiming.deadline +initialCompositorTiming.interval;
1338     EXPECT_EQ(expectedDeadline, compositeDeadline);
1339 
1340     dequeueAndQueue(1);
1341     addFrameEvents(true, 0, 1);
1342 
1343     // A "now" just after the next interval snaps properly.
1344     mSurface->setNow(
1345             mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1346             mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1347     result = native_window_get_compositor_timing(mWindow.get(),
1348         &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1349     EXPECT_EQ(NO_ERROR, result);
1350     expectedDeadline =
1351             mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1352             mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1353     EXPECT_EQ(expectedDeadline, compositeDeadline);
1354 
1355     dequeueAndQueue(2);
1356     addFrameEvents(true, 1, 2);
1357 
1358     // A "now" over 1 interval before the deadline snaps properly.
1359     mSurface->setNow(
1360             mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1361             mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1362     result = native_window_get_compositor_timing(mWindow.get(),
1363         &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1364     EXPECT_EQ(NO_ERROR, result);
1365     expectedDeadline =
1366             mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1367             mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1368     EXPECT_EQ(expectedDeadline, compositeDeadline);
1369 
1370     // Re-enabling frame timestamps should get the latest values.
1371     disableFrameTimestamps();
1372     enableFrameTimestamps();
1373 
1374     // A "now" over 2 intervals before the deadline snaps properly.
1375     mSurface->setNow(
1376             mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1377             mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1378     result = native_window_get_compositor_timing(mWindow.get(),
1379         &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1380     EXPECT_EQ(NO_ERROR, result);
1381     expectedDeadline =
1382             mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1383             mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1384     EXPECT_EQ(expectedDeadline, compositeDeadline);
1385 }
1386 
1387 // This verifies the timestamps recorded in the consumer's
1388 // FrameTimestampsHistory are properly retrieved by the producer for the
1389 // correct frames.
TEST_F(GetFrameTimestampsTest,TimestampsAssociatedWithCorrectFrame)1390 TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1391     enableFrameTimestamps();
1392 
1393     const uint64_t fId1 = getNextFrameId();
1394     dequeueAndQueue(0);
1395     mFrames[0].signalQueueFences();
1396 
1397     const uint64_t fId2 = getNextFrameId();
1398     dequeueAndQueue(1);
1399     mFrames[1].signalQueueFences();
1400 
1401     addFrameEvents(true, NO_FRAME_INDEX, 0);
1402     mFrames[0].signalRefreshFences();
1403     addFrameEvents(true, 0, 1);
1404     mFrames[0].signalReleaseFences();
1405     mFrames[1].signalRefreshFences();
1406 
1407     // Verify timestamps are correct for frame 1.
1408     resetTimestamps();
1409     int result = getAllFrameTimestamps(fId1);
1410     EXPECT_EQ(NO_ERROR, result);
1411     EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1412     EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1413     EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1414     EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1415     EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
1416     EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1417             outGpuCompositionDoneTime);
1418     EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1419     EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
1420     EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1421 
1422     // Verify timestamps are correct for frame 2.
1423     resetTimestamps();
1424     result = getAllFrameTimestamps(fId2);
1425     EXPECT_EQ(NO_ERROR, result);
1426     EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1427     EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1428     EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1429     EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1430     EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
1431     EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1432             outGpuCompositionDoneTime);
1433     EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1434     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1435     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
1436 }
1437 
1438 // This test verifies the acquire fence recorded by the consumer is not sent
1439 // back to the producer and the producer saves its own fence.
TEST_F(GetFrameTimestampsTest,QueueTimestampsNoSync)1440 TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1441     enableFrameTimestamps();
1442 
1443     // Dequeue and queue frame 1.
1444     const uint64_t fId1 = getNextFrameId();
1445     dequeueAndQueue(0);
1446 
1447     // Verify queue-related timestamps for f1 are available immediately in the
1448     // producer without asking the consumer again, even before signaling the
1449     // acquire fence.
1450     resetTimestamps();
1451     int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1452     int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1453             &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
1454             nullptr, nullptr, nullptr, nullptr, nullptr);
1455     EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1456     EXPECT_EQ(NO_ERROR, result);
1457     EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1458     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
1459 
1460     // Signal acquire fences. Verify a sync call still isn't necessary.
1461     mFrames[0].signalQueueFences();
1462 
1463     oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1464     result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1465             &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
1466             nullptr, nullptr, nullptr, nullptr, nullptr);
1467     EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1468     EXPECT_EQ(NO_ERROR, result);
1469     EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1470     EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1471 
1472     // Dequeue and queue frame 2.
1473     const uint64_t fId2 = getNextFrameId();
1474     dequeueAndQueue(1);
1475 
1476     // Verify queue-related timestamps for f2 are available immediately in the
1477     // producer without asking the consumer again, even before signaling the
1478     // acquire fence.
1479     resetTimestamps();
1480     oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1481     result = native_window_get_frame_timestamps(mWindow.get(), fId2,
1482             &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
1483             nullptr, nullptr, nullptr, nullptr, nullptr);
1484     EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1485     EXPECT_EQ(NO_ERROR, result);
1486     EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1487     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
1488 
1489     // Signal acquire fences. Verify a sync call still isn't necessary.
1490     mFrames[1].signalQueueFences();
1491 
1492     oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1493     result = native_window_get_frame_timestamps(mWindow.get(), fId2,
1494             &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
1495             nullptr, nullptr, nullptr, nullptr, nullptr);
1496     EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1497     EXPECT_EQ(NO_ERROR, result);
1498     EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1499     EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1500 }
1501 
TEST_F(GetFrameTimestampsTest,ZeroRequestedTimestampsNoSync)1502 TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1503     enableFrameTimestamps();
1504 
1505     // Dequeue and queue frame 1.
1506     dequeueAndQueue(0);
1507     mFrames[0].signalQueueFences();
1508 
1509     // Dequeue and queue frame 2.
1510     const uint64_t fId2 = getNextFrameId();
1511     dequeueAndQueue(1);
1512     mFrames[1].signalQueueFences();
1513 
1514     addFrameEvents(true, NO_FRAME_INDEX, 0);
1515     mFrames[0].signalRefreshFences();
1516     addFrameEvents(true, 0, 1);
1517     mFrames[0].signalReleaseFences();
1518     mFrames[1].signalRefreshFences();
1519 
1520     // Verify a request for no timestamps doesn't result in a sync call.
1521     int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1522     int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
1523             nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1524             nullptr, nullptr);
1525     EXPECT_EQ(NO_ERROR, result);
1526     EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1527 }
1528 
1529 // This test verifies that fences can signal and update timestamps producer
1530 // side without an additional sync call to the consumer.
TEST_F(GetFrameTimestampsTest,FencesInProducerNoSync)1531 TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1532     enableFrameTimestamps();
1533 
1534     // Dequeue and queue frame 1.
1535     const uint64_t fId1 = getNextFrameId();
1536     dequeueAndQueue(0);
1537     mFrames[0].signalQueueFences();
1538 
1539     // Dequeue and queue frame 2.
1540     dequeueAndQueue(1);
1541     mFrames[1].signalQueueFences();
1542 
1543     addFrameEvents(true, NO_FRAME_INDEX, 0);
1544     addFrameEvents(true, 0, 1);
1545 
1546     // Verify available timestamps are correct for frame 1, before any
1547     // fence has been signaled.
1548     // Note: A sync call is necessary here since the events triggered by
1549     // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1550     resetTimestamps();
1551     int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1552     int result = getAllFrameTimestamps(fId1);
1553     EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1554     EXPECT_EQ(NO_ERROR, result);
1555     EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1556     EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1557     EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1558     EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1559     EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
1560     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1561     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
1562     EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
1563     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
1564 
1565     // Verify available timestamps are correct for frame 1 again, before any
1566     // fence has been signaled.
1567     // This time a sync call should not be necessary.
1568     resetTimestamps();
1569     oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1570     result = getAllFrameTimestamps(fId1);
1571     EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1572     EXPECT_EQ(NO_ERROR, result);
1573     EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1574     EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1575     EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1576     EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1577     EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
1578     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1579     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
1580     EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
1581     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
1582 
1583     // Signal the fences for frame 1.
1584     mFrames[0].signalRefreshFences();
1585     mFrames[0].signalReleaseFences();
1586 
1587     // Verify all timestamps are available without a sync call.
1588     resetTimestamps();
1589     oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1590     result = getAllFrameTimestamps(fId1);
1591     EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1592     EXPECT_EQ(NO_ERROR, result);
1593     EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1594     EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1595     EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1596     EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1597     EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
1598     EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1599             outGpuCompositionDoneTime);
1600     EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1601     EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
1602     EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1603 }
1604 
1605 // This test verifies that if the frame wasn't GPU composited but has a refresh
1606 // event a sync call isn't made to get the GPU composite done time since it will
1607 // never exist.
TEST_F(GetFrameTimestampsTest,NoGpuNoSync)1608 TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1609     enableFrameTimestamps();
1610 
1611     // Dequeue and queue frame 1.
1612     const uint64_t fId1 = getNextFrameId();
1613     dequeueAndQueue(0);
1614     mFrames[0].signalQueueFences();
1615 
1616     // Dequeue and queue frame 2.
1617     dequeueAndQueue(1);
1618     mFrames[1].signalQueueFences();
1619 
1620     addFrameEvents(false, NO_FRAME_INDEX, 0);
1621     addFrameEvents(false, 0, 1);
1622 
1623     // Verify available timestamps are correct for frame 1, before any
1624     // fence has been signaled.
1625     // Note: A sync call is necessary here since the events triggered by
1626     // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1627     resetTimestamps();
1628     int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1629     int result = getAllFrameTimestamps(fId1);
1630     EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1631     EXPECT_EQ(NO_ERROR, result);
1632     EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1633     EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1634     EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1635     EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1636     EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
1637     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1638     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
1639     EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
1640     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
1641 
1642     // Signal the fences for frame 1.
1643     mFrames[0].signalRefreshFences();
1644     mFrames[0].signalReleaseFences();
1645 
1646     // Verify all timestamps, except GPU composition, are available without a
1647     // sync call.
1648     resetTimestamps();
1649     oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1650     result = getAllFrameTimestamps(fId1);
1651     EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1652     EXPECT_EQ(NO_ERROR, result);
1653     EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1654     EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1655     EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1656     EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1657     EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
1658     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1659     EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1660     EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
1661     EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1662 }
1663 
1664 // This test verifies that if the certain timestamps can't possibly exist for
1665 // the most recent frame, then a sync call is not done.
TEST_F(GetFrameTimestampsTest,NoReleaseNoSync)1666 TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
1667     enableFrameTimestamps();
1668 
1669     // Dequeue and queue frame 1.
1670     const uint64_t fId1 = getNextFrameId();
1671     dequeueAndQueue(0);
1672     mFrames[0].signalQueueFences();
1673 
1674     // Dequeue and queue frame 2.
1675     const uint64_t fId2 = getNextFrameId();
1676     dequeueAndQueue(1);
1677     mFrames[1].signalQueueFences();
1678 
1679     addFrameEvents(false, NO_FRAME_INDEX, 0);
1680     addFrameEvents(false, 0, 1);
1681 
1682     // Verify available timestamps are correct for frame 1, before any
1683     // fence has been signaled.
1684     // Note: A sync call is necessary here since the events triggered by
1685     // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1686     resetTimestamps();
1687     int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1688     int result = getAllFrameTimestamps(fId1);
1689     EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1690     EXPECT_EQ(NO_ERROR, result);
1691     EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1692     EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1693     EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1694     EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1695     EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
1696     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1697     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
1698     EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
1699     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
1700 
1701     mFrames[0].signalRefreshFences();
1702     mFrames[0].signalReleaseFences();
1703     mFrames[1].signalRefreshFences();
1704 
1705     // Verify querying for all timestmaps of f2 does not do a sync call. Even
1706     // though the lastRefresh, dequeueReady, and release times aren't
1707     // available, a sync call should not occur because it's not possible for f2
1708     // to encounter the final value for those events until another frame is
1709     // queued.
1710     resetTimestamps();
1711     oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1712     result = getAllFrameTimestamps(fId2);
1713     EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1714     EXPECT_EQ(NO_ERROR, result);
1715     EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1716     EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1717     EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1718     EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1719     EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
1720     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1721     EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1722     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1723     EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
1724 }
1725 
1726 // This test verifies there are no sync calls for present times
1727 // when they aren't supported and that an error is returned.
1728 
TEST_F(GetFrameTimestampsTest,PresentUnsupportedNoSync)1729 TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1730     enableFrameTimestamps();
1731     mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1732 
1733     // Dequeue and queue frame 1.
1734     const uint64_t fId1 = getNextFrameId();
1735     dequeueAndQueue(0);
1736 
1737     // Verify a query for the Present times do not trigger a sync call if they
1738     // are not supported.
1739     resetTimestamps();
1740     int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1741     int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1742             nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1743             &outDisplayPresentTime, nullptr, nullptr);
1744     EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1745     EXPECT_EQ(BAD_VALUE, result);
1746     EXPECT_EQ(-1, outDisplayPresentTime);
1747 }
1748 
1749 } // namespace android
1750