blob: 470a338639b862aceb53e45582f7b5e3f86f86e2 [file] [log] [blame]
Jamie Gennis134f0422011-03-08 12:18:54 -08001/*
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
Dan Stozac6f30bd2015-06-08 09:32:50 -070017#include "DummyConsumer.h"
18
Jamie Gennis134f0422011-03-08 12:18:54 -080019#include <gtest/gtest.h>
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080020
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060021#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070022#include <binder/ProcessState.h>
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060023#include <configstore/Utils.h>
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -070024#include <cutils/properties.h>
25#include <gui/BufferItemConsumer.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070026#include <gui/IDisplayEventConnection.h>
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -070027#include <gui/IProducerListener.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080028#include <gui/ISurfaceComposer.h>
29#include <gui/Surface.h>
30#include <gui/SurfaceComposerClient.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070031#include <private/gui/ComposerService.h>
Dan Stozac1879002014-05-22 15:59:05 -070032#include <ui/Rect.h>
Jamie Gennis134f0422011-03-08 12:18:54 -080033#include <utils/String8.h>
34
Brian Anderson3da8d272016-07-28 16:20:47 -070035#include <limits>
Kalle Raita643f0942016-12-07 09:20:14 -080036#include <thread>
37
Jamie Gennis134f0422011-03-08 12:18:54 -080038namespace android {
39
Kalle Raita643f0942016-12-07 09:20:14 -080040using namespace std::chrono_literals;
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060041// retrieve wide-color and hdr settings from configstore
42using namespace android::hardware::configstore;
43using namespace android::hardware::configstore::V1_0;
44
Robert Carr4cdc58f2017-08-23 14:22:20 -070045using Transaction = SurfaceComposerClient::Transaction;
46
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060047static bool hasWideColorDisplay =
48 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
Kalle Raita643f0942016-12-07 09:20:14 -080049
Brian Anderson3da8d272016-07-28 16:20:47 -070050class FakeSurfaceComposer;
51class FakeProducerFrameEventHistory;
52
53static constexpr uint64_t NO_FRAME_INDEX = std::numeric_limits<uint64_t>::max();
54
Jamie Gennis134f0422011-03-08 12:18:54 -080055class SurfaceTest : public ::testing::Test {
56protected:
Mathias Agopian7c1a4872013-03-20 15:56:04 -070057
58 SurfaceTest() {
59 ProcessState::self()->startThreadPool();
60 }
61
Jamie Gennis134f0422011-03-08 12:18:54 -080062 virtual void SetUp() {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080063 mComposerClient = new SurfaceComposerClient;
Jamie Gennis134f0422011-03-08 12:18:54 -080064 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
65
Brian Andersond0010582017-03-07 13:20:31 -080066 // TODO(brianderson): The following sometimes fails and is a source of
67 // test flakiness.
Jamie Gennisfc850122011-04-25 16:40:05 -070068 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070069 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis134f0422011-03-08 12:18:54 -080070
71 ASSERT_TRUE(mSurfaceControl != NULL);
72 ASSERT_TRUE(mSurfaceControl->isValid());
73
Robert Carr4cdc58f2017-08-23 14:22:20 -070074 Transaction t;
75 ASSERT_EQ(NO_ERROR, t.setLayer(mSurfaceControl, 0x7fffffff)
76 .show(mSurfaceControl)
77 .apply());
Jamie Gennis134f0422011-03-08 12:18:54 -080078
79 mSurface = mSurfaceControl->getSurface();
80 ASSERT_TRUE(mSurface != NULL);
81 }
82
83 virtual void TearDown() {
84 mComposerClient->dispose();
85 }
86
87 sp<Surface> mSurface;
88 sp<SurfaceComposerClient> mComposerClient;
89 sp<SurfaceControl> mSurfaceControl;
90};
91
92TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
93 sp<ANativeWindow> anw(mSurface);
94 int result = -123;
95 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
96 &result);
97 EXPECT_EQ(NO_ERROR, err);
98 EXPECT_EQ(1, result);
99}
100
101TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
102 mSurfaceControl.clear();
Kalle Raita643f0942016-12-07 09:20:14 -0800103 // Wait for the async clean-up to complete.
104 std::this_thread::sleep_for(50ms);
Jamie Gennis134f0422011-03-08 12:18:54 -0800105
106 sp<ANativeWindow> anw(mSurface);
107 int result = -123;
108 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
109 &result);
110 EXPECT_EQ(NO_ERROR, err);
111 EXPECT_EQ(1, result);
112}
113
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800114// This test probably doesn't belong here.
Jamie Gennisc901ca02011-10-11 16:02:31 -0700115TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800116 sp<ANativeWindow> anw(mSurface);
117
118 // Verify the screenshot works with no protected buffers.
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800119 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Brian Anderson3da8d272016-07-28 16:20:47 -0700120 sp<IBinder> display(sf->getBuiltInDisplay(
121 ISurfaceComposer::eDisplayIdMain));
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000122 sp<GraphicBuffer> outBuffer;
123 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &outBuffer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800124 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800125
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700126 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
127 NATIVE_WINDOW_API_CPU));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800128 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
129 // that we need to dequeue a buffer in order for it to actually get
130 // allocated in SurfaceFlinger.
131 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
132 GRALLOC_USAGE_PROTECTED));
133 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Iliyan Malchev697526b2011-05-01 11:33:26 -0700134 ANativeWindowBuffer* buf = 0;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700135
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700136 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700137 if (err) {
138 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
139 // that's okay as long as this is the reason for the failure.
140 // try again without the GRALLOC_USAGE_PROTECTED bit.
141 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700142 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
143 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700144 return;
145 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700146 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700147
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800148 for (int i = 0; i < 4; i++) {
149 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700150 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
151 &buf));
152 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800153 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000154 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &outBuffer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800155 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800156}
157
Jamie Gennis391bbe22011-03-14 15:00:06 -0700158TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
159 sp<ANativeWindow> anw(mSurface);
160 int result = -123;
161 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
162 EXPECT_EQ(NO_ERROR, err);
163 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
164}
165
Craig Donner6ebc46a2016-10-21 15:23:44 -0700166TEST_F(SurfaceTest, LayerCountIsOne) {
167 sp<ANativeWindow> anw(mSurface);
168 int result = -123;
169 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
170 EXPECT_EQ(NO_ERROR, err);
171 EXPECT_EQ(1, result);
172}
173
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700174TEST_F(SurfaceTest, QueryConsumerUsage) {
175 const int TEST_USAGE_FLAGS =
176 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700177 sp<IGraphicBufferProducer> producer;
178 sp<IGraphicBufferConsumer> consumer;
179 BufferQueue::createBufferQueue(&producer, &consumer);
180 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700181 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700182 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700183
184 sp<ANativeWindow> anw(s);
185
186 int flags = -1;
187 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
188
189 ASSERT_EQ(NO_ERROR, err);
190 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
191}
192
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800193TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
194 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
195 sp<IGraphicBufferProducer> producer;
196 sp<IGraphicBufferConsumer> consumer;
197 BufferQueue::createBufferQueue(&producer, &consumer);
198 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
199
200 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
201
202 sp<Surface> s = new Surface(producer);
203
204 sp<ANativeWindow> anw(s);
205
206 android_dataspace dataSpace;
207
208 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
209 reinterpret_cast<int*>(&dataSpace));
210
211 ASSERT_EQ(NO_ERROR, err);
212 ASSERT_EQ(TEST_DATASPACE, dataSpace);
213}
214
Dan Stoza812ed062015-06-02 15:45:22 -0700215TEST_F(SurfaceTest, SettingGenerationNumber) {
216 sp<IGraphicBufferProducer> producer;
217 sp<IGraphicBufferConsumer> consumer;
218 BufferQueue::createBufferQueue(&producer, &consumer);
219 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
220 sp<Surface> surface = new Surface(producer);
221 sp<ANativeWindow> window(surface);
222
223 // Allocate a buffer with a generation number of 0
224 ANativeWindowBuffer* buffer;
225 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700226 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
227 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700228 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
229 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
230
231 // Detach the buffer and check its generation number
232 sp<GraphicBuffer> graphicBuffer;
233 sp<Fence> fence;
234 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
235 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
236
237 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
238 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
239
240 // This should change the generation number of the GraphicBuffer
241 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
242
243 // Check that the new generation number sticks with the buffer
244 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
245 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
246 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
247 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
248}
249
Dan Stozac6f30bd2015-06-08 09:32:50 -0700250TEST_F(SurfaceTest, GetConsumerName) {
251 sp<IGraphicBufferProducer> producer;
252 sp<IGraphicBufferConsumer> consumer;
253 BufferQueue::createBufferQueue(&producer, &consumer);
254
255 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
256 consumer->consumerConnect(dummyConsumer, false);
257 consumer->setConsumerName(String8("TestConsumer"));
258
259 sp<Surface> surface = new Surface(producer);
260 sp<ANativeWindow> window(surface);
261 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
262
263 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
264}
265
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700266TEST_F(SurfaceTest, GetWideColorSupport) {
267 sp<IGraphicBufferProducer> producer;
268 sp<IGraphicBufferConsumer> consumer;
269 BufferQueue::createBufferQueue(&producer, &consumer);
270
271 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
272 consumer->consumerConnect(dummyConsumer, false);
273 consumer->setConsumerName(String8("TestConsumer"));
274
275 sp<Surface> surface = new Surface(producer);
276 sp<ANativeWindow> window(surface);
277 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
278
279 bool supported;
280 surface->getWideColorSupport(&supported);
281
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -0600282 // NOTE: This test assumes that device that supports
283 // wide-color (as indicated by BoardConfig) must also
284 // have a wide-color primary display.
285 // That assumption allows this test to cover devices
286 // that advertised a wide-color color mode without
287 // actually supporting wide-color to pass this test
288 // as well as the case of a device that does support
289 // wide-color (via BoardConfig) and has a wide-color
290 // primary display.
291 // NOT covered at this time is a device that supports
292 // wide color in the BoardConfig but does not support
293 // a wide-color color mode on the primary display.
294 ASSERT_EQ(hasWideColorDisplay, supported);
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700295}
296
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800297TEST_F(SurfaceTest, DynamicSetBufferCount) {
298 sp<IGraphicBufferProducer> producer;
299 sp<IGraphicBufferConsumer> consumer;
300 BufferQueue::createBufferQueue(&producer, &consumer);
301
302 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
303 consumer->consumerConnect(dummyConsumer, false);
304 consumer->setConsumerName(String8("TestConsumer"));
305
306 sp<Surface> surface = new Surface(producer);
307 sp<ANativeWindow> window(surface);
308
309 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
310 NATIVE_WINDOW_API_CPU));
311 native_window_set_buffer_count(window.get(), 4);
312
313 int fence;
314 ANativeWindowBuffer* buffer;
315 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
316 native_window_set_buffer_count(window.get(), 3);
317 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
318 native_window_set_buffer_count(window.get(), 2);
319 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
320 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
321}
322
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700323TEST_F(SurfaceTest, GetAndFlushRemovedBuffers) {
324 sp<IGraphicBufferProducer> producer;
325 sp<IGraphicBufferConsumer> consumer;
326 BufferQueue::createBufferQueue(&producer, &consumer);
327
328 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
329 consumer->consumerConnect(dummyConsumer, false);
330 consumer->setConsumerName(String8("TestConsumer"));
331
332 sp<Surface> surface = new Surface(producer);
333 sp<ANativeWindow> window(surface);
334 sp<DummyProducerListener> listener = new DummyProducerListener();
335 ASSERT_EQ(OK, surface->connect(
336 NATIVE_WINDOW_API_CPU,
337 /*listener*/listener,
338 /*reportBufferRemoval*/true));
339 const int BUFFER_COUNT = 4;
340 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
341
342 sp<GraphicBuffer> detachedBuffer;
343 sp<Fence> outFence;
344 int fences[BUFFER_COUNT];
345 ANativeWindowBuffer* buffers[BUFFER_COUNT];
346 // Allocate buffers because detachNextBuffer requires allocated buffers
347 for (int i = 0; i < BUFFER_COUNT; i++) {
348 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
349 }
350 for (int i = 0; i < BUFFER_COUNT; i++) {
351 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
352 }
353
354 // Test detached buffer is correctly reported
355 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
356 std::vector<sp<GraphicBuffer>> removedBuffers;
357 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
358 ASSERT_EQ(1u, removedBuffers.size());
359 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
360 // Test the list is flushed one getAndFlushRemovedBuffers returns
361 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
362 ASSERT_EQ(0u, removedBuffers.size());
363
364
365 // Test removed buffer list is cleanup after next dequeueBuffer call
366 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
367 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[0], &fences[0]));
368 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
369 ASSERT_EQ(0u, removedBuffers.size());
370 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[0], fences[0]));
371
372 // Test removed buffer list is cleanup after next detachNextBuffer call
373 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
374 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
375 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
376 ASSERT_EQ(1u, removedBuffers.size());
377 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
378
379 // Re-allocate buffers since all buffers are detached up to now
380 for (int i = 0; i < BUFFER_COUNT; i++) {
381 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
382 }
383 for (int i = 0; i < BUFFER_COUNT; i++) {
384 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
385 }
386
387 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
388 ASSERT_EQ(NO_ERROR, surface->attachBuffer(detachedBuffer.get()));
389 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
390 // Depends on which slot GraphicBufferProducer impl pick, the attach call might
391 // get 0 or 1 buffer removed.
392 ASSERT_LE(removedBuffers.size(), 1u);
393}
Brian Anderson3da8d272016-07-28 16:20:47 -0700394
Dan Stoza932f0082017-05-31 13:50:16 -0700395TEST_F(SurfaceTest, TestGetLastDequeueStartTime) {
396 sp<ANativeWindow> anw(mSurface);
397 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU));
398
399 ANativeWindowBuffer* buffer = nullptr;
400 int32_t fenceFd = -1;
401
402 nsecs_t before = systemTime(CLOCK_MONOTONIC);
403 anw->dequeueBuffer(anw.get(), &buffer, &fenceFd);
404 nsecs_t after = systemTime(CLOCK_MONOTONIC);
405
406 nsecs_t lastDequeueTime = mSurface->getLastDequeueStartTime();
407 ASSERT_LE(before, lastDequeueTime);
408 ASSERT_GE(after, lastDequeueTime);
409}
410
Brian Anderson3da8d272016-07-28 16:20:47 -0700411class FakeConsumer : public BnConsumerListener {
412public:
413 void onFrameAvailable(const BufferItem& /*item*/) override {}
414 void onBuffersReleased() override {}
415 void onSidebandStreamChanged() override {}
416
417 void addAndGetFrameTimestamps(
418 const NewFrameEventsEntry* newTimestamps,
419 FrameEventHistoryDelta* outDelta) override {
420 if (newTimestamps) {
421 if (mGetFrameTimestampsEnabled) {
422 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
423 "Test should set mNewFrameEntryOverride before queuing "
424 "a frame.";
425 EXPECT_EQ(newTimestamps->frameNumber,
426 mNewFrameEntryOverride.frameNumber) <<
427 "Test attempting to add NewFrameEntryOverride with "
428 "incorrect frame number.";
429 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
430 mNewFrameEntryOverride.frameNumber = 0;
431 }
432 mAddFrameTimestampsCount++;
433 mLastAddedFrameNumber = newTimestamps->frameNumber;
434 }
435 if (outDelta) {
436 mFrameEventHistory.getAndResetDelta(outDelta);
437 mGetFrameTimestampsCount++;
438 }
439 mAddAndGetFrameTimestampsCallCount++;
440 }
441
442 bool mGetFrameTimestampsEnabled = false;
443
444 ConsumerFrameEventHistory mFrameEventHistory;
445 int mAddAndGetFrameTimestampsCallCount = 0;
446 int mAddFrameTimestampsCount = 0;
447 int mGetFrameTimestampsCount = 0;
448 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
449
450 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
451};
452
453
454class FakeSurfaceComposer : public ISurfaceComposer{
455public:
456 ~FakeSurfaceComposer() override {}
457
Brian Anderson6b376712017-04-04 10:51:39 -0700458 void setSupportsPresent(bool supportsPresent) {
459 mSupportsPresent = supportsPresent;
460 }
461
Brian Anderson3da8d272016-07-28 16:20:47 -0700462 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800463 sp<ISurfaceComposerClient> createScopedConnection(
464 const sp<IGraphicBufferProducer>& /* parent */) override {
465 return nullptr;
466 }
Jorim Jaggib1e2f8d2017-06-08 15:43:59 -0700467 sp<IDisplayEventConnection> createDisplayEventConnection(ISurfaceComposer::VsyncSource)
468 override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700469 return nullptr;
470 }
471 sp<IBinder> createDisplay(const String8& /*displayName*/,
472 bool /*secure*/) override { return nullptr; }
473 void destroyDisplay(const sp<IBinder>& /*display */) override {}
474 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
475 void setTransactionState(const Vector<ComposerState>& /*state*/,
476 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
477 override {}
478 void bootFinished() override {}
479 bool authenticateSurfaceTexture(
480 const sp<IGraphicBufferProducer>& /*surface*/) const override {
481 return false;
482 }
Brian Anderson6b376712017-04-04 10:51:39 -0700483
484 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
485 const override {
486 *outSupported = {
487 FrameEvent::REQUESTED_PRESENT,
488 FrameEvent::ACQUIRE,
489 FrameEvent::LATCH,
490 FrameEvent::FIRST_REFRESH_START,
491 FrameEvent::LAST_REFRESH_START,
492 FrameEvent::GPU_COMPOSITION_DONE,
493 FrameEvent::DEQUEUE_READY,
494 FrameEvent::RELEASE
495 };
496 if (mSupportsPresent) {
497 outSupported->push_back(
498 FrameEvent::DISPLAY_PRESENT);
499 }
500 return NO_ERROR;
501 }
502
Brian Anderson3da8d272016-07-28 16:20:47 -0700503 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
504 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
505 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
506 status_t getDisplayStats(const sp<IBinder>& /*display*/,
507 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
508 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
509 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
510 override {
511 return NO_ERROR;
512 }
513 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
514 Vector<android_color_mode_t>* /*outColorModes*/) override {
515 return NO_ERROR;
516 }
517 android_color_mode_t getActiveColorMode(const sp<IBinder>& /*display*/)
518 override {
519 return HAL_COLOR_MODE_NATIVE;
520 }
521 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
522 android_color_mode_t /*colorMode*/) override { return NO_ERROR; }
523 status_t captureScreen(const sp<IBinder>& /*display*/,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000524 sp<GraphicBuffer>* /*outBuffer*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700525 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800526 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700527 bool /*useIdentityTransform*/,
528 Rotation /*rotation*/) override { return NO_ERROR; }
chaviwa76b2712017-09-20 12:02:26 -0700529 virtual status_t captureLayers(const sp<IBinder>& /*parentHandle*/,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000530 sp<GraphicBuffer>* /*outBuffer*/,
chaviw7206d492017-11-10 16:16:12 -0800531 const Rect& /*sourceCrop*/, float /*frameScale*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700532 return NO_ERROR;
533 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700534 status_t clearAnimationFrameStats() override { return NO_ERROR; }
535 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
536 return NO_ERROR;
537 }
538 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
539 HdrCapabilities* /*outCapabilities*/) const override {
540 return NO_ERROR;
541 }
542 status_t enableVSyncInjections(bool /*enable*/) override {
543 return NO_ERROR;
544 }
545 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800546 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
547 return NO_ERROR;
548 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700549
550protected:
551 IBinder* onAsBinder() override { return nullptr; }
552
553private:
554 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700555};
556
557class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
558public:
559 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
560 : mFenceMap(fenceMap) {}
561
562 ~FakeProducerFrameEventHistory() {}
563
564 void updateAcquireFence(uint64_t frameNumber,
565 std::shared_ptr<FenceTime>&& acquire) override {
566 // Verify the acquire fence being added isn't the one from the consumer.
567 EXPECT_NE(mConsumerAcquireFence, acquire);
568 // Override the fence, so we can verify this was called by the
569 // producer after the frame is queued.
570 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
571 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
572 }
573
574 void setAcquireFenceOverride(
575 const std::shared_ptr<FenceTime>& acquireFenceOverride,
576 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
577 mAcquireFenceOverride = acquireFenceOverride;
578 mConsumerAcquireFence = consumerAcquireFence;
579 }
580
581protected:
582 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
583 const override {
584 return mFenceMap->createFenceTimeForTest(fence);
585 }
586
587 FenceToFenceTimeMap* mFenceMap{nullptr};
588
589 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
590 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
591};
592
593
594class TestSurface : public Surface {
595public:
596 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
597 FenceToFenceTimeMap* fenceMap)
598 : Surface(bufferProducer),
599 mFakeSurfaceComposer(new FakeSurfaceComposer) {
600 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
601 mFrameEventHistory.reset(mFakeFrameEventHistory);
602 }
603
604 ~TestSurface() override {}
605
606 sp<ISurfaceComposer> composerService() const override {
607 return mFakeSurfaceComposer;
608 }
609
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800610 nsecs_t now() const override {
611 return mNow;
612 }
613
614 void setNow(nsecs_t now) {
615 mNow = now;
616 }
617
Brian Anderson3da8d272016-07-28 16:20:47 -0700618public:
619 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800620 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700621
622 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
623 // but this raw pointer gives access to test functionality.
624 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
625};
626
627
Brian Andersond0010582017-03-07 13:20:31 -0800628class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700629protected:
630 struct FenceAndFenceTime {
631 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
632 : mFence(new Fence),
633 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
634 sp<Fence> mFence { nullptr };
635 std::shared_ptr<FenceTime> mFenceTime { nullptr };
636 };
637
638 struct RefreshEvents {
639 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800640 : mFenceMap(fenceMap),
641 kCompositorTiming(
642 {refreshStart, refreshStart + 1, refreshStart + 2 }),
643 kStartTime(refreshStart + 3),
644 kGpuCompositionDoneTime(refreshStart + 4),
645 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700646
647 void signalPostCompositeFences() {
648 mFenceMap.signalAllForTest(
649 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
650 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
651 }
652
653 FenceToFenceTimeMap& mFenceMap;
654
655 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
656 FenceAndFenceTime mPresent { mFenceMap };
657
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800658 const CompositorTiming kCompositorTiming;
659
Brian Anderson3da8d272016-07-28 16:20:47 -0700660 const nsecs_t kStartTime;
661 const nsecs_t kGpuCompositionDoneTime;
662 const nsecs_t kPresentTime;
663 };
664
665 struct FrameEvents {
666 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
667 : mFenceMap(fenceMap),
668 kPostedTime(frameStartTime + 100),
669 kRequestedPresentTime(frameStartTime + 200),
670 kProducerAcquireTime(frameStartTime + 300),
671 kConsumerAcquireTime(frameStartTime + 301),
672 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700673 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700674 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700675 mRefreshes {
676 { mFenceMap, frameStartTime + 410 },
677 { mFenceMap, frameStartTime + 420 },
678 { mFenceMap, frameStartTime + 430 } } {}
679
680 void signalQueueFences() {
681 mFenceMap.signalAllForTest(
682 mAcquireConsumer.mFence, kConsumerAcquireTime);
683 mFenceMap.signalAllForTest(
684 mAcquireProducer.mFence, kProducerAcquireTime);
685 }
686
687 void signalRefreshFences() {
688 for (auto& re : mRefreshes) {
689 re.signalPostCompositeFences();
690 }
691 }
692
693 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700694 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
695 }
696
697 FenceToFenceTimeMap& mFenceMap;
698
699 FenceAndFenceTime mAcquireConsumer { mFenceMap };
700 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700701 FenceAndFenceTime mRelease { mFenceMap };
702
703 const nsecs_t kPostedTime;
704 const nsecs_t kRequestedPresentTime;
705 const nsecs_t kProducerAcquireTime;
706 const nsecs_t kConsumerAcquireTime;
707 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700708 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700709 const nsecs_t kReleaseTime;
710
711 RefreshEvents mRefreshes[3];
712 };
713
Brian Andersond0010582017-03-07 13:20:31 -0800714 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700715
716 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700717 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
718 mFakeConsumer = new FakeConsumer;
719 mCfeh = &mFakeConsumer->mFrameEventHistory;
720 mConsumer->consumerConnect(mFakeConsumer, false);
721 mConsumer->setConsumerName(String8("TestConsumer"));
722 mSurface = new TestSurface(mProducer, &mFenceMap);
723 mWindow = mSurface;
724
725 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
726 NATIVE_WINDOW_API_CPU));
727 native_window_set_buffer_count(mWindow.get(), 4);
728 }
729
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800730 void disableFrameTimestamps() {
731 mFakeConsumer->mGetFrameTimestampsEnabled = false;
732 native_window_enable_frame_timestamps(mWindow.get(), 0);
733 mFrameTimestampsEnabled = false;
734 }
735
Brian Anderson3da8d272016-07-28 16:20:47 -0700736 void enableFrameTimestamps() {
737 mFakeConsumer->mGetFrameTimestampsEnabled = true;
738 native_window_enable_frame_timestamps(mWindow.get(), 1);
739 mFrameTimestampsEnabled = true;
740 }
741
Brian Anderson1049d1d2016-12-16 17:25:57 -0800742 int getAllFrameTimestamps(uint64_t frameId) {
743 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700744 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
745 &outFirstRefreshStartTime, &outLastRefreshStartTime,
746 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700747 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700748 }
749
Brian Anderson3da8d272016-07-28 16:20:47 -0700750 void resetTimestamps() {
751 outRequestedPresentTime = -1;
752 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700753 outLatchTime = -1;
754 outFirstRefreshStartTime = -1;
755 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700756 outGpuCompositionDoneTime = -1;
757 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700758 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700759 outReleaseTime = -1;
760 }
761
Brian Anderson1049d1d2016-12-16 17:25:57 -0800762 uint64_t getNextFrameId() {
763 uint64_t frameId = -1;
764 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
765 EXPECT_EQ(status, NO_ERROR);
766 return frameId;
767 }
768
Brian Anderson3da8d272016-07-28 16:20:47 -0700769 void dequeueAndQueue(uint64_t frameIndex) {
770 int fence = -1;
771 ANativeWindowBuffer* buffer = nullptr;
772 ASSERT_EQ(NO_ERROR,
773 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
774
775 int oldAddFrameTimestampsCount =
776 mFakeConsumer->mAddFrameTimestampsCount;
777
778 FrameEvents* frame = &mFrames[frameIndex];
779 uint64_t frameNumber = frameIndex + 1;
780
781 NewFrameEventsEntry fe;
782 fe.frameNumber = frameNumber;
783 fe.postedTime = frame->kPostedTime;
784 fe.requestedPresentTime = frame->kRequestedPresentTime;
785 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
786 mFakeConsumer->mNewFrameEntryOverride = fe;
787
788 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
789 frame->mAcquireProducer.mFenceTime,
790 frame->mAcquireConsumer.mFenceTime);
791
792 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
793
794 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
795
796 EXPECT_EQ(
797 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
798 mFakeConsumer->mAddFrameTimestampsCount);
799 }
800
801 void addFrameEvents(
802 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
803 FrameEvents* oldFrame =
804 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
805 FrameEvents* newFrame = &mFrames[iNewFrame];
806
807 uint64_t nOldFrame = iOldFrame + 1;
808 uint64_t nNewFrame = iNewFrame + 1;
809
Brian Anderson4e606e32017-03-16 15:34:57 -0700810 // Latch, Composite, and Release the frames in a plausible order.
811 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700812 // that's okay for the purposes of this test.
813 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
814
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700815 // Composite the previous frame one more time, which helps verify
816 // LastRefresh is updated properly.
817 if (oldFrame != nullptr) {
818 mCfeh->addPreComposition(nOldFrame,
819 oldFrame->mRefreshes[2].kStartTime);
820 gpuDoneFenceTime = gpuComposited ?
821 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
822 FenceTime::NO_FENCE;
823 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800824 oldFrame->mRefreshes[2].mPresent.mFenceTime,
825 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700826 }
827
828 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700829 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
830
831 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
832 gpuDoneFenceTime = gpuComposited ?
833 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
834 FenceTime::NO_FENCE;
835 // HWC2 releases the previous buffer after a new latch just before
836 // calling postComposition.
837 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700838 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700839 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
840 }
841 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800842 newFrame->mRefreshes[0].mPresent.mFenceTime,
843 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700844
Brian Anderson3da8d272016-07-28 16:20:47 -0700845 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
846 gpuDoneFenceTime = gpuComposited ?
847 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
848 FenceTime::NO_FENCE;
849 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800850 newFrame->mRefreshes[1].mPresent.mFenceTime,
851 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700852 }
853
Brian Anderson3da8d272016-07-28 16:20:47 -0700854 sp<IGraphicBufferProducer> mProducer;
855 sp<IGraphicBufferConsumer> mConsumer;
856 sp<FakeConsumer> mFakeConsumer;
857 ConsumerFrameEventHistory* mCfeh;
858 sp<TestSurface> mSurface;
859 sp<ANativeWindow> mWindow;
860
861 FenceToFenceTimeMap mFenceMap;
862
863 bool mFrameTimestampsEnabled = false;
864
865 int64_t outRequestedPresentTime = -1;
866 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700867 int64_t outLatchTime = -1;
868 int64_t outFirstRefreshStartTime = -1;
869 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700870 int64_t outGpuCompositionDoneTime = -1;
871 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700872 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700873 int64_t outReleaseTime = -1;
874
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800875 FrameEvents mFrames[3] {
876 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700877};
878
879
880// This test verifies that the frame timestamps are not retrieved when not
881// explicitly enabled via native_window_enable_frame_timestamps.
882// We want to check this to make sure there's no overhead for users
883// that don't need the timestamp information.
884TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
885 int fence;
886 ANativeWindowBuffer* buffer;
887
888 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
889 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
890
Brian Anderson1049d1d2016-12-16 17:25:57 -0800891 const uint64_t fId = getNextFrameId();
892
Brian Anderson3da8d272016-07-28 16:20:47 -0700893 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
894 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
895 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
896 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
897
898 // Verify the producer doesn't get frame timestamps piggybacked on queue.
899 // It is okay that frame timestamps are added in the consumer since it is
900 // still needed for SurfaceFlinger dumps.
901 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
902 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
903 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
904
905 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800906 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700907 EXPECT_EQ(INVALID_OPERATION, result);
908 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800909
910 // Verify compositor timing query fails.
911 nsecs_t compositeDeadline = 0;
912 nsecs_t compositeInterval = 0;
913 nsecs_t compositeToPresentLatency = 0;
914 result = native_window_get_compositor_timing(mWindow.get(),
915 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
916 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700917}
918
919// This test verifies that the frame timestamps are retrieved if explicitly
920// enabled via native_window_enable_frame_timestamps.
921TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800922 CompositorTiming initialCompositorTiming {
923 1000000000, // 1s deadline
924 16666667, // 16ms interval
925 50000000, // 50ms present latency
926 };
927 mCfeh->initializeCompositorTiming(initialCompositorTiming);
928
Brian Anderson3da8d272016-07-28 16:20:47 -0700929 enableFrameTimestamps();
930
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800931 // Verify the compositor timing query gets the initial compositor values
932 // after timststamps are enabled; even before the first frame is queued
933 // or dequeued.
934 nsecs_t compositeDeadline = 0;
935 nsecs_t compositeInterval = 0;
936 nsecs_t compositeToPresentLatency = 0;
937 mSurface->setNow(initialCompositorTiming.deadline - 1);
938 int result = native_window_get_compositor_timing(mWindow.get(),
939 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
940 EXPECT_EQ(NO_ERROR, result);
941 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
942 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
943 EXPECT_EQ(initialCompositorTiming.presentLatency,
944 compositeToPresentLatency);
945
Brian Anderson3da8d272016-07-28 16:20:47 -0700946 int fence;
947 ANativeWindowBuffer* buffer;
948
949 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800950 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700951
Brian Anderson1049d1d2016-12-16 17:25:57 -0800952 const uint64_t fId1 = getNextFrameId();
953
Brian Anderson3da8d272016-07-28 16:20:47 -0700954 // Verify getFrameTimestamps is piggybacked on dequeue.
955 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
956 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800957 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700958
959 NewFrameEventsEntry f1;
960 f1.frameNumber = 1;
961 f1.postedTime = mFrames[0].kPostedTime;
962 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
963 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
964 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
965 mFrames[0].mAcquireProducer.mFenceTime,
966 mFrames[0].mAcquireConsumer.mFenceTime);
967 mFakeConsumer->mNewFrameEntryOverride = f1;
968 mFrames[0].signalQueueFences();
969
970 // Verify getFrameTimestamps is piggybacked on queue.
971 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
972 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
973 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800974 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700975
976 // Verify queries for timestamps that the producer doesn't know about
977 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800978 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -0700979 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800980 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700981}
982
Brian Anderson6b376712017-04-04 10:51:39 -0700983TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
984 bool displayPresentSupported = true;
985 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
986
987 // Verify supported bits are forwarded.
988 int supportsPresent = -1;
989 mWindow.get()->query(mWindow.get(),
990 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
991 EXPECT_EQ(displayPresentSupported, supportsPresent);
992}
993
994TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
995 bool displayPresentSupported = false;
996 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
997
998 // Verify supported bits are forwarded.
999 int supportsPresent = -1;
1000 mWindow.get()->query(mWindow.get(),
1001 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1002 EXPECT_EQ(displayPresentSupported, supportsPresent);
1003}
1004
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001005TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1006 nsecs_t phase = 4000;
1007 nsecs_t interval = 1000;
1008
1009 // Timestamp in previous interval.
1010 nsecs_t timestamp = 3500;
1011 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1012 timestamp, phase, interval));
1013
1014 // Timestamp in next interval.
1015 timestamp = 4500;
1016 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1017 timestamp, phase, interval));
1018
1019 // Timestamp multiple intervals before.
1020 timestamp = 2500;
1021 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1022 timestamp, phase, interval));
1023
1024 // Timestamp multiple intervals after.
1025 timestamp = 6500;
1026 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1027 timestamp, phase, interval));
1028
1029 // Timestamp on previous interval.
1030 timestamp = 3000;
1031 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1032 timestamp, phase, interval));
1033
1034 // Timestamp on next interval.
1035 timestamp = 5000;
1036 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1037 timestamp, phase, interval));
1038
1039 // Timestamp equal to phase.
1040 timestamp = 4000;
1041 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1042 timestamp, phase, interval));
1043}
1044
1045// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1046// if the number of intervals elapsed is internally stored in an int.
1047TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1048 nsecs_t phase = 0;
1049 nsecs_t interval = 4000;
1050 nsecs_t big_timestamp = 8635916564000;
1051 int32_t intervals = big_timestamp / interval;
1052
1053 EXPECT_LT(intervals, 0);
1054 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1055 big_timestamp, phase, interval));
1056 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1057 big_timestamp, big_timestamp, interval));
1058}
1059
1060// This verifies the compositor timing is updated by refresh events
1061// and piggy backed on a queue, dequeue, and enabling of timestamps..
1062TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1063 CompositorTiming initialCompositorTiming {
1064 1000000000, // 1s deadline
1065 16666667, // 16ms interval
1066 50000000, // 50ms present latency
1067 };
1068 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1069
1070 enableFrameTimestamps();
1071
1072 // We get the initial values before any frames are submitted.
1073 nsecs_t compositeDeadline = 0;
1074 nsecs_t compositeInterval = 0;
1075 nsecs_t compositeToPresentLatency = 0;
1076 mSurface->setNow(initialCompositorTiming.deadline - 1);
1077 int result = native_window_get_compositor_timing(mWindow.get(),
1078 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1079 EXPECT_EQ(NO_ERROR, result);
1080 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1081 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1082 EXPECT_EQ(initialCompositorTiming.presentLatency,
1083 compositeToPresentLatency);
1084
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001085 dequeueAndQueue(0);
1086 addFrameEvents(true, NO_FRAME_INDEX, 0);
1087
1088 // Still get the initial values because the frame events for frame 0
1089 // didn't get a chance to piggyback on a queue or dequeue yet.
1090 result = native_window_get_compositor_timing(mWindow.get(),
1091 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1092 EXPECT_EQ(NO_ERROR, result);
1093 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1094 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1095 EXPECT_EQ(initialCompositorTiming.presentLatency,
1096 compositeToPresentLatency);
1097
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001098 dequeueAndQueue(1);
1099 addFrameEvents(true, 0, 1);
1100
1101 // Now expect the composite values associated with frame 1.
1102 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1103 result = native_window_get_compositor_timing(mWindow.get(),
1104 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1105 EXPECT_EQ(NO_ERROR, result);
1106 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1107 compositeDeadline);
1108 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1109 compositeInterval);
1110 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1111 compositeToPresentLatency);
1112
1113 dequeueAndQueue(2);
1114 addFrameEvents(true, 1, 2);
1115
1116 // Now expect the composite values associated with frame 2.
1117 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1118 result = native_window_get_compositor_timing(mWindow.get(),
1119 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1120 EXPECT_EQ(NO_ERROR, result);
1121 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1122 compositeDeadline);
1123 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1124 compositeInterval);
1125 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1126 compositeToPresentLatency);
1127
1128 // Re-enabling frame timestamps should get the latest values.
1129 disableFrameTimestamps();
1130 enableFrameTimestamps();
1131
1132 // Now expect the composite values associated with frame 3.
1133 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1134 result = native_window_get_compositor_timing(mWindow.get(),
1135 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1136 EXPECT_EQ(NO_ERROR, result);
1137 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1138 compositeDeadline);
1139 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1140 compositeInterval);
1141 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1142 compositeToPresentLatency);
1143}
1144
1145// This verifies the compositor deadline properly snaps to the the next
1146// deadline based on the current time.
1147TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1148 CompositorTiming initialCompositorTiming {
1149 1000000000, // 1s deadline
1150 16666667, // 16ms interval
1151 50000000, // 50ms present latency
1152 };
1153 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1154
1155 enableFrameTimestamps();
1156
1157 nsecs_t compositeDeadline = 0;
1158 nsecs_t compositeInterval = 0;
1159 nsecs_t compositeToPresentLatency = 0;
1160
1161 // A "now" just before the deadline snaps to the deadline.
1162 mSurface->setNow(initialCompositorTiming.deadline - 1);
1163 int result = native_window_get_compositor_timing(mWindow.get(),
1164 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1165 EXPECT_EQ(NO_ERROR, result);
1166 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1167 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1168 EXPECT_EQ(expectedDeadline, compositeDeadline);
1169
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001170 dequeueAndQueue(0);
1171 addFrameEvents(true, NO_FRAME_INDEX, 0);
1172
1173 // A "now" just after the deadline snaps properly.
1174 mSurface->setNow(initialCompositorTiming.deadline + 1);
1175 result = native_window_get_compositor_timing(mWindow.get(),
1176 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1177 EXPECT_EQ(NO_ERROR, result);
1178 expectedDeadline =
1179 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1180 EXPECT_EQ(expectedDeadline, compositeDeadline);
1181
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001182 dequeueAndQueue(1);
1183 addFrameEvents(true, 0, 1);
1184
1185 // A "now" just after the next interval snaps properly.
1186 mSurface->setNow(
1187 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1188 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1189 result = native_window_get_compositor_timing(mWindow.get(),
1190 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1191 EXPECT_EQ(NO_ERROR, result);
1192 expectedDeadline =
1193 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1194 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1195 EXPECT_EQ(expectedDeadline, compositeDeadline);
1196
1197 dequeueAndQueue(2);
1198 addFrameEvents(true, 1, 2);
1199
1200 // A "now" over 1 interval before the deadline snaps properly.
1201 mSurface->setNow(
1202 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1203 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1204 result = native_window_get_compositor_timing(mWindow.get(),
1205 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1206 EXPECT_EQ(NO_ERROR, result);
1207 expectedDeadline =
1208 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1209 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1210 EXPECT_EQ(expectedDeadline, compositeDeadline);
1211
1212 // Re-enabling frame timestamps should get the latest values.
1213 disableFrameTimestamps();
1214 enableFrameTimestamps();
1215
1216 // A "now" over 2 intervals before the deadline snaps properly.
1217 mSurface->setNow(
1218 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1219 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1220 result = native_window_get_compositor_timing(mWindow.get(),
1221 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1222 EXPECT_EQ(NO_ERROR, result);
1223 expectedDeadline =
1224 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1225 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1226 EXPECT_EQ(expectedDeadline, compositeDeadline);
1227}
1228
Brian Anderson1049d1d2016-12-16 17:25:57 -08001229// This verifies the timestamps recorded in the consumer's
1230// FrameTimestampsHistory are properly retrieved by the producer for the
1231// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001232TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1233 enableFrameTimestamps();
1234
Brian Anderson1049d1d2016-12-16 17:25:57 -08001235 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001236 dequeueAndQueue(0);
1237 mFrames[0].signalQueueFences();
1238
Brian Anderson1049d1d2016-12-16 17:25:57 -08001239 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001240 dequeueAndQueue(1);
1241 mFrames[1].signalQueueFences();
1242
1243 addFrameEvents(true, NO_FRAME_INDEX, 0);
1244 mFrames[0].signalRefreshFences();
1245 addFrameEvents(true, 0, 1);
1246 mFrames[0].signalReleaseFences();
1247 mFrames[1].signalRefreshFences();
1248
1249 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001250 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001251 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001252 EXPECT_EQ(NO_ERROR, result);
1253 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1254 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001255 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1256 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1257 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001258 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1259 outGpuCompositionDoneTime);
1260 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001261 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001262 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1263
1264 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001265 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001266 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001267 EXPECT_EQ(NO_ERROR, result);
1268 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1269 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001270 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1271 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1272 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001273 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1274 outGpuCompositionDoneTime);
1275 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001276 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1277 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001278}
1279
1280// This test verifies the acquire fence recorded by the consumer is not sent
1281// back to the producer and the producer saves its own fence.
1282TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1283 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001284
Brian Anderson3da8d272016-07-28 16:20:47 -07001285 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001286 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001287 dequeueAndQueue(0);
1288
1289 // Verify queue-related timestamps for f1 are available immediately in the
1290 // producer without asking the consumer again, even before signaling the
1291 // acquire fence.
1292 resetTimestamps();
1293 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001294 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001295 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001296 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001297 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1298 EXPECT_EQ(NO_ERROR, result);
1299 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001300 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001301
1302 // Signal acquire fences. Verify a sync call still isn't necessary.
1303 mFrames[0].signalQueueFences();
1304
1305 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001306 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001307 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001308 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001309 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1310 EXPECT_EQ(NO_ERROR, result);
1311 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1312 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1313
1314 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001315 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001316 dequeueAndQueue(1);
1317
1318 // Verify queue-related timestamps for f2 are available immediately in the
1319 // producer without asking the consumer again, even before signaling the
1320 // acquire fence.
1321 resetTimestamps();
1322 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001323 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001324 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001325 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001326 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1327 EXPECT_EQ(NO_ERROR, result);
1328 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001329 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001330
1331 // Signal acquire fences. Verify a sync call still isn't necessary.
1332 mFrames[1].signalQueueFences();
1333
1334 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001335 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001336 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001337 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001338 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1339 EXPECT_EQ(NO_ERROR, result);
1340 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1341 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1342}
1343
1344TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1345 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001346
1347 // Dequeue and queue frame 1.
1348 dequeueAndQueue(0);
1349 mFrames[0].signalQueueFences();
1350
1351 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001352 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001353 dequeueAndQueue(1);
1354 mFrames[1].signalQueueFences();
1355
1356 addFrameEvents(true, NO_FRAME_INDEX, 0);
1357 mFrames[0].signalRefreshFences();
1358 addFrameEvents(true, 0, 1);
1359 mFrames[0].signalReleaseFences();
1360 mFrames[1].signalRefreshFences();
1361
1362 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001363 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001364 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001365 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1366 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001367 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001368 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1369}
1370
1371// This test verifies that fences can signal and update timestamps producer
1372// side without an additional sync call to the consumer.
1373TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1374 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001375
1376 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001377 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001378 dequeueAndQueue(0);
1379 mFrames[0].signalQueueFences();
1380
1381 // Dequeue and queue frame 2.
1382 dequeueAndQueue(1);
1383 mFrames[1].signalQueueFences();
1384
1385 addFrameEvents(true, NO_FRAME_INDEX, 0);
1386 addFrameEvents(true, 0, 1);
1387
1388 // Verify available timestamps are correct for frame 1, before any
1389 // fence has been signaled.
1390 // Note: A sync call is necessary here since the events triggered by
1391 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001392 resetTimestamps();
1393 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001394 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001395 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1396 EXPECT_EQ(NO_ERROR, result);
1397 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1398 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001399 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1400 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1401 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001402 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1403 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001404 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001405 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001406
1407 // Verify available timestamps are correct for frame 1 again, before any
1408 // fence has been signaled.
1409 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001410 resetTimestamps();
1411 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001412 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001413 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1414 EXPECT_EQ(NO_ERROR, result);
1415 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1416 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001417 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1418 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1419 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001420 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1421 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001422 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001423 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001424
1425 // Signal the fences for frame 1.
1426 mFrames[0].signalRefreshFences();
1427 mFrames[0].signalReleaseFences();
1428
1429 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001430 resetTimestamps();
1431 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001432 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001433 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1434 EXPECT_EQ(NO_ERROR, result);
1435 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1436 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001437 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1438 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1439 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001440 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1441 outGpuCompositionDoneTime);
1442 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001443 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001444 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1445}
1446
1447// This test verifies that if the frame wasn't GPU composited but has a refresh
1448// event a sync call isn't made to get the GPU composite done time since it will
1449// never exist.
1450TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1451 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001452
Brian Anderson3da8d272016-07-28 16:20:47 -07001453 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001454 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001455 dequeueAndQueue(0);
1456 mFrames[0].signalQueueFences();
1457
1458 // Dequeue and queue frame 2.
1459 dequeueAndQueue(1);
1460 mFrames[1].signalQueueFences();
1461
1462 addFrameEvents(false, NO_FRAME_INDEX, 0);
1463 addFrameEvents(false, 0, 1);
1464
1465 // Verify available timestamps are correct for frame 1, before any
1466 // fence has been signaled.
1467 // Note: A sync call is necessary here since the events triggered by
1468 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1469 resetTimestamps();
1470 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001471 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001472 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1473 EXPECT_EQ(NO_ERROR, result);
1474 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1475 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001476 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1477 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1478 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001479 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1480 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001481 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001482 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001483
1484 // Signal the fences for frame 1.
1485 mFrames[0].signalRefreshFences();
1486 mFrames[0].signalReleaseFences();
1487
1488 // Verify all timestamps, except GPU composition, are available without a
1489 // sync call.
1490 resetTimestamps();
1491 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001492 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001493 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1494 EXPECT_EQ(NO_ERROR, result);
1495 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1496 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001497 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1498 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1499 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001500 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001501 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001502 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001503 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1504}
1505
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001506// This test verifies that if the certain timestamps can't possibly exist for
1507// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001508TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001509 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001510
1511 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001512 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001513 dequeueAndQueue(0);
1514 mFrames[0].signalQueueFences();
1515
1516 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001517 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001518 dequeueAndQueue(1);
1519 mFrames[1].signalQueueFences();
1520
1521 addFrameEvents(false, NO_FRAME_INDEX, 0);
1522 addFrameEvents(false, 0, 1);
1523
1524 // Verify available timestamps are correct for frame 1, before any
1525 // fence has been signaled.
1526 // Note: A sync call is necessary here since the events triggered by
1527 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001528 resetTimestamps();
1529 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001530 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001531 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1532 EXPECT_EQ(NO_ERROR, result);
1533 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1534 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001535 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1536 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1537 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001538 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1539 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001540 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001541 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001542
1543 mFrames[0].signalRefreshFences();
1544 mFrames[0].signalReleaseFences();
1545 mFrames[1].signalRefreshFences();
1546
Brian Anderson1049d1d2016-12-16 17:25:57 -08001547 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001548 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001549 // available, a sync call should not occur because it's not possible for f2
1550 // to encounter the final value for those events until another frame is
1551 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001552 resetTimestamps();
1553 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001554 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001555 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1556 EXPECT_EQ(NO_ERROR, result);
1557 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1558 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001559 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1560 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1561 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001562 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001563 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001564 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1565 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001566}
1567
Brian Anderson6b376712017-04-04 10:51:39 -07001568// This test verifies there are no sync calls for present times
1569// when they aren't supported and that an error is returned.
1570
1571TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1572 enableFrameTimestamps();
1573 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1574
1575 // Dequeue and queue frame 1.
1576 const uint64_t fId1 = getNextFrameId();
1577 dequeueAndQueue(0);
1578
1579 // Verify a query for the Present times do not trigger a sync call if they
1580 // are not supported.
1581 resetTimestamps();
1582 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1583 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1584 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1585 &outDisplayPresentTime, nullptr, nullptr);
1586 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1587 EXPECT_EQ(BAD_VALUE, result);
1588 EXPECT_EQ(-1, outDisplayPresentTime);
1589}
1590
Dan Stoza932f0082017-05-31 13:50:16 -07001591} // namespace android