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