blob: df391ed4bb6eb2b182e5dcf4d5709125936aafbd [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>
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -080025#include <inttypes.h>
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -070026#include <gui/BufferItemConsumer.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070027#include <gui/IDisplayEventConnection.h>
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -070028#include <gui/IProducerListener.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080029#include <gui/ISurfaceComposer.h>
30#include <gui/Surface.h>
31#include <gui/SurfaceComposerClient.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070032#include <private/gui/ComposerService.h>
Dan Stozac1879002014-05-22 15:59:05 -070033#include <ui/Rect.h>
Jamie Gennis134f0422011-03-08 12:18:54 -080034#include <utils/String8.h>
35
Brian Anderson3da8d272016-07-28 16:20:47 -070036#include <limits>
Kalle Raita643f0942016-12-07 09:20:14 -080037#include <thread>
38
Jamie Gennis134f0422011-03-08 12:18:54 -080039namespace android {
40
Kalle Raita643f0942016-12-07 09:20:14 -080041using namespace std::chrono_literals;
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060042// retrieve wide-color and hdr settings from configstore
43using namespace android::hardware::configstore;
44using namespace android::hardware::configstore::V1_0;
45
Robert Carr4cdc58f2017-08-23 14:22:20 -070046using Transaction = SurfaceComposerClient::Transaction;
47
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060048static bool hasWideColorDisplay =
49 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
Kalle Raita643f0942016-12-07 09:20:14 -080050
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -070051static bool hasHdrDisplay =
52 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
53
Brian Anderson3da8d272016-07-28 16:20:47 -070054class FakeSurfaceComposer;
55class FakeProducerFrameEventHistory;
56
57static constexpr uint64_t NO_FRAME_INDEX = std::numeric_limits<uint64_t>::max();
58
Jamie Gennis134f0422011-03-08 12:18:54 -080059class SurfaceTest : public ::testing::Test {
60protected:
Mathias Agopian7c1a4872013-03-20 15:56:04 -070061 SurfaceTest() {
62 ProcessState::self()->startThreadPool();
63 }
64
Jamie Gennis134f0422011-03-08 12:18:54 -080065 virtual void SetUp() {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080066 mComposerClient = new SurfaceComposerClient;
Jamie Gennis134f0422011-03-08 12:18:54 -080067 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
68
Brian Andersond0010582017-03-07 13:20:31 -080069 // TODO(brianderson): The following sometimes fails and is a source of
70 // test flakiness.
Jamie Gennisfc850122011-04-25 16:40:05 -070071 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070072 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis134f0422011-03-08 12:18:54 -080073
74 ASSERT_TRUE(mSurfaceControl != NULL);
75 ASSERT_TRUE(mSurfaceControl->isValid());
76
Robert Carr4cdc58f2017-08-23 14:22:20 -070077 Transaction t;
78 ASSERT_EQ(NO_ERROR, t.setLayer(mSurfaceControl, 0x7fffffff)
79 .show(mSurfaceControl)
80 .apply());
Jamie Gennis134f0422011-03-08 12:18:54 -080081
82 mSurface = mSurfaceControl->getSurface();
83 ASSERT_TRUE(mSurface != NULL);
84 }
85
86 virtual void TearDown() {
87 mComposerClient->dispose();
88 }
89
90 sp<Surface> mSurface;
91 sp<SurfaceComposerClient> mComposerClient;
92 sp<SurfaceControl> mSurfaceControl;
93};
94
Robert Carr740eaf02018-03-27 12:59:18 -070095TEST_F(SurfaceTest, CreateSurfaceReturnsErrorBadClient) {
96 mComposerClient->dispose();
97 ASSERT_EQ(NO_INIT, mComposerClient->initCheck());
98
99 sp<SurfaceControl> sc;
100 status_t err = mComposerClient->createSurfaceChecked(
101 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, &sc, 0);
102 ASSERT_EQ(NO_INIT, err);
103}
104
Jamie Gennis134f0422011-03-08 12:18:54 -0800105TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
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
114TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
115 mSurfaceControl.clear();
Kalle Raita643f0942016-12-07 09:20:14 -0800116 // Wait for the async clean-up to complete.
117 std::this_thread::sleep_for(50ms);
Jamie Gennis134f0422011-03-08 12:18:54 -0800118
119 sp<ANativeWindow> anw(mSurface);
120 int result = -123;
121 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
122 &result);
123 EXPECT_EQ(NO_ERROR, err);
124 EXPECT_EQ(1, result);
125}
126
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800127// This test probably doesn't belong here.
Jamie Gennisc901ca02011-10-11 16:02:31 -0700128TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800129 sp<ANativeWindow> anw(mSurface);
130
131 // Verify the screenshot works with no protected buffers.
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800132 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Brian Anderson3da8d272016-07-28 16:20:47 -0700133 sp<IBinder> display(sf->getBuiltInDisplay(
134 ISurfaceComposer::eDisplayIdMain));
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000135 sp<GraphicBuffer> outBuffer;
136 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &outBuffer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800137 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800138
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700139 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
140 NATIVE_WINDOW_API_CPU));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800141 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
142 // that we need to dequeue a buffer in order for it to actually get
143 // allocated in SurfaceFlinger.
144 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
145 GRALLOC_USAGE_PROTECTED));
146 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Iliyan Malchev697526b2011-05-01 11:33:26 -0700147 ANativeWindowBuffer* buf = 0;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700148
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700149 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700150 if (err) {
151 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
152 // that's okay as long as this is the reason for the failure.
153 // try again without the GRALLOC_USAGE_PROTECTED bit.
154 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700155 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
156 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700157 return;
158 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700159 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700160
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800161 for (int i = 0; i < 4; i++) {
162 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700163 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
164 &buf));
165 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800166 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000167 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &outBuffer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800168 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800169}
170
Jamie Gennis391bbe22011-03-14 15:00:06 -0700171TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
172 sp<ANativeWindow> anw(mSurface);
173 int result = -123;
174 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
175 EXPECT_EQ(NO_ERROR, err);
176 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
177}
178
Craig Donner6ebc46a2016-10-21 15:23:44 -0700179TEST_F(SurfaceTest, LayerCountIsOne) {
180 sp<ANativeWindow> anw(mSurface);
181 int result = -123;
182 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
183 EXPECT_EQ(NO_ERROR, err);
184 EXPECT_EQ(1, result);
185}
186
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700187TEST_F(SurfaceTest, QueryConsumerUsage) {
188 const int TEST_USAGE_FLAGS =
189 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700190 sp<IGraphicBufferProducer> producer;
191 sp<IGraphicBufferConsumer> consumer;
192 BufferQueue::createBufferQueue(&producer, &consumer);
193 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700194 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700195 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700196
197 sp<ANativeWindow> anw(s);
198
199 int flags = -1;
200 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
201
202 ASSERT_EQ(NO_ERROR, err);
203 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
204}
205
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800206TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
207 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
208 sp<IGraphicBufferProducer> producer;
209 sp<IGraphicBufferConsumer> consumer;
210 BufferQueue::createBufferQueue(&producer, &consumer);
211 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
212
213 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
214
215 sp<Surface> s = new Surface(producer);
216
217 sp<ANativeWindow> anw(s);
218
219 android_dataspace dataSpace;
220
221 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
222 reinterpret_cast<int*>(&dataSpace));
223
224 ASSERT_EQ(NO_ERROR, err);
225 ASSERT_EQ(TEST_DATASPACE, dataSpace);
226}
227
Dan Stoza812ed062015-06-02 15:45:22 -0700228TEST_F(SurfaceTest, SettingGenerationNumber) {
229 sp<IGraphicBufferProducer> producer;
230 sp<IGraphicBufferConsumer> consumer;
231 BufferQueue::createBufferQueue(&producer, &consumer);
232 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
233 sp<Surface> surface = new Surface(producer);
234 sp<ANativeWindow> window(surface);
235
236 // Allocate a buffer with a generation number of 0
237 ANativeWindowBuffer* buffer;
238 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700239 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
240 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700241 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
242 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
243
244 // Detach the buffer and check its generation number
245 sp<GraphicBuffer> graphicBuffer;
246 sp<Fence> fence;
247 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
248 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
249
250 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
251 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
252
253 // This should change the generation number of the GraphicBuffer
254 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
255
256 // Check that the new generation number sticks with the buffer
257 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
258 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
259 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
260 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
261}
262
Dan Stozac6f30bd2015-06-08 09:32:50 -0700263TEST_F(SurfaceTest, GetConsumerName) {
264 sp<IGraphicBufferProducer> producer;
265 sp<IGraphicBufferConsumer> consumer;
266 BufferQueue::createBufferQueue(&producer, &consumer);
267
268 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
269 consumer->consumerConnect(dummyConsumer, false);
270 consumer->setConsumerName(String8("TestConsumer"));
271
272 sp<Surface> surface = new Surface(producer);
273 sp<ANativeWindow> window(surface);
274 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
275
276 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
277}
278
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700279TEST_F(SurfaceTest, GetWideColorSupport) {
280 sp<IGraphicBufferProducer> producer;
281 sp<IGraphicBufferConsumer> consumer;
282 BufferQueue::createBufferQueue(&producer, &consumer);
283
284 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
285 consumer->consumerConnect(dummyConsumer, false);
286 consumer->setConsumerName(String8("TestConsumer"));
287
288 sp<Surface> surface = new Surface(producer);
289 sp<ANativeWindow> window(surface);
290 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
291
292 bool supported;
293 surface->getWideColorSupport(&supported);
294
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -0600295 // NOTE: This test assumes that device that supports
296 // wide-color (as indicated by BoardConfig) must also
297 // have a wide-color primary display.
298 // That assumption allows this test to cover devices
299 // that advertised a wide-color color mode without
300 // actually supporting wide-color to pass this test
301 // as well as the case of a device that does support
302 // wide-color (via BoardConfig) and has a wide-color
303 // primary display.
304 // NOT covered at this time is a device that supports
305 // wide color in the BoardConfig but does not support
306 // a wide-color color mode on the primary display.
307 ASSERT_EQ(hasWideColorDisplay, supported);
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700308}
309
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700310TEST_F(SurfaceTest, GetHdrSupport) {
311 sp<IGraphicBufferProducer> producer;
312 sp<IGraphicBufferConsumer> consumer;
313 BufferQueue::createBufferQueue(&producer, &consumer);
314
315 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
316 consumer->consumerConnect(dummyConsumer, false);
317 consumer->setConsumerName(String8("TestConsumer"));
318
319 sp<Surface> surface = new Surface(producer);
320 sp<ANativeWindow> window(surface);
321 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
322
323 bool supported;
324 status_t result = surface->getHdrSupport(&supported);
325 ASSERT_EQ(NO_ERROR, result);
326
327 // NOTE: This is not a CTS test.
328 // This test verifies that when the BoardConfig TARGET_HAS_HDR_DISPLAY
329 // is TRUE, getHdrSupport is also true.
330 // TODO: Add check for an HDR color mode on the primary display.
331 ASSERT_EQ(hasHdrDisplay, supported);
332}
333
334TEST_F(SurfaceTest, SetHdrMetadata) {
335 sp<IGraphicBufferProducer> producer;
336 sp<IGraphicBufferConsumer> consumer;
337 BufferQueue::createBufferQueue(&producer, &consumer);
338
339 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
340 consumer->consumerConnect(dummyConsumer, false);
341 consumer->setConsumerName(String8("TestConsumer"));
342
343 sp<Surface> surface = new Surface(producer);
344 sp<ANativeWindow> window(surface);
345 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
346
347 bool supported;
348 status_t result = surface->getHdrSupport(&supported);
349 ASSERT_EQ(NO_ERROR, result);
350
351 if (!hasHdrDisplay || !supported) {
352 return;
353 }
354 const android_smpte2086_metadata smpte2086 = {
355 {0.680, 0.320},
356 {0.265, 0.690},
357 {0.150, 0.060},
358 {0.3127, 0.3290},
359 100.0,
360 0.1,
361 };
362 const android_cta861_3_metadata cta861_3 = {
363 78.0,
364 62.0,
365 };
366 int error = native_window_set_buffers_smpte2086_metadata(window.get(), &smpte2086);
367 ASSERT_EQ(error, NO_ERROR);
368 error = native_window_set_buffers_cta861_3_metadata(window.get(), &cta861_3);
369 ASSERT_EQ(error, NO_ERROR);
370}
371
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800372TEST_F(SurfaceTest, DynamicSetBufferCount) {
373 sp<IGraphicBufferProducer> producer;
374 sp<IGraphicBufferConsumer> consumer;
375 BufferQueue::createBufferQueue(&producer, &consumer);
376
377 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
378 consumer->consumerConnect(dummyConsumer, false);
379 consumer->setConsumerName(String8("TestConsumer"));
380
381 sp<Surface> surface = new Surface(producer);
382 sp<ANativeWindow> window(surface);
383
384 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
385 NATIVE_WINDOW_API_CPU));
386 native_window_set_buffer_count(window.get(), 4);
387
388 int fence;
389 ANativeWindowBuffer* buffer;
390 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
391 native_window_set_buffer_count(window.get(), 3);
392 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
393 native_window_set_buffer_count(window.get(), 2);
394 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
395 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
396}
397
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700398TEST_F(SurfaceTest, GetAndFlushRemovedBuffers) {
399 sp<IGraphicBufferProducer> producer;
400 sp<IGraphicBufferConsumer> consumer;
401 BufferQueue::createBufferQueue(&producer, &consumer);
402
403 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
404 consumer->consumerConnect(dummyConsumer, false);
405 consumer->setConsumerName(String8("TestConsumer"));
406
407 sp<Surface> surface = new Surface(producer);
408 sp<ANativeWindow> window(surface);
409 sp<DummyProducerListener> listener = new DummyProducerListener();
410 ASSERT_EQ(OK, surface->connect(
411 NATIVE_WINDOW_API_CPU,
412 /*listener*/listener,
413 /*reportBufferRemoval*/true));
414 const int BUFFER_COUNT = 4;
415 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
416
417 sp<GraphicBuffer> detachedBuffer;
418 sp<Fence> outFence;
419 int fences[BUFFER_COUNT];
420 ANativeWindowBuffer* buffers[BUFFER_COUNT];
421 // Allocate buffers because detachNextBuffer requires allocated buffers
422 for (int i = 0; i < BUFFER_COUNT; i++) {
423 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
424 }
425 for (int i = 0; i < BUFFER_COUNT; i++) {
426 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
427 }
428
429 // Test detached buffer is correctly reported
430 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
431 std::vector<sp<GraphicBuffer>> removedBuffers;
432 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
433 ASSERT_EQ(1u, removedBuffers.size());
434 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
435 // Test the list is flushed one getAndFlushRemovedBuffers returns
436 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
437 ASSERT_EQ(0u, removedBuffers.size());
438
439
440 // Test removed buffer list is cleanup after next dequeueBuffer call
441 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
442 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[0], &fences[0]));
443 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
444 ASSERT_EQ(0u, removedBuffers.size());
445 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[0], fences[0]));
446
447 // Test removed buffer list is cleanup after next detachNextBuffer call
448 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
449 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
450 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
451 ASSERT_EQ(1u, removedBuffers.size());
452 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
453
454 // Re-allocate buffers since all buffers are detached up to now
455 for (int i = 0; i < BUFFER_COUNT; i++) {
456 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
457 }
458 for (int i = 0; i < BUFFER_COUNT; i++) {
459 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
460 }
461
462 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
463 ASSERT_EQ(NO_ERROR, surface->attachBuffer(detachedBuffer.get()));
464 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
465 // Depends on which slot GraphicBufferProducer impl pick, the attach call might
466 // get 0 or 1 buffer removed.
467 ASSERT_LE(removedBuffers.size(), 1u);
468}
Brian Anderson3da8d272016-07-28 16:20:47 -0700469
Dan Stoza932f0082017-05-31 13:50:16 -0700470TEST_F(SurfaceTest, TestGetLastDequeueStartTime) {
471 sp<ANativeWindow> anw(mSurface);
472 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU));
473
474 ANativeWindowBuffer* buffer = nullptr;
475 int32_t fenceFd = -1;
476
477 nsecs_t before = systemTime(CLOCK_MONOTONIC);
478 anw->dequeueBuffer(anw.get(), &buffer, &fenceFd);
479 nsecs_t after = systemTime(CLOCK_MONOTONIC);
480
481 nsecs_t lastDequeueTime = mSurface->getLastDequeueStartTime();
482 ASSERT_LE(before, lastDequeueTime);
483 ASSERT_GE(after, lastDequeueTime);
484}
485
Brian Anderson3da8d272016-07-28 16:20:47 -0700486class FakeConsumer : public BnConsumerListener {
487public:
488 void onFrameAvailable(const BufferItem& /*item*/) override {}
489 void onBuffersReleased() override {}
490 void onSidebandStreamChanged() override {}
491
492 void addAndGetFrameTimestamps(
493 const NewFrameEventsEntry* newTimestamps,
494 FrameEventHistoryDelta* outDelta) override {
495 if (newTimestamps) {
496 if (mGetFrameTimestampsEnabled) {
497 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
498 "Test should set mNewFrameEntryOverride before queuing "
499 "a frame.";
500 EXPECT_EQ(newTimestamps->frameNumber,
501 mNewFrameEntryOverride.frameNumber) <<
502 "Test attempting to add NewFrameEntryOverride with "
503 "incorrect frame number.";
504 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
505 mNewFrameEntryOverride.frameNumber = 0;
506 }
507 mAddFrameTimestampsCount++;
508 mLastAddedFrameNumber = newTimestamps->frameNumber;
509 }
510 if (outDelta) {
511 mFrameEventHistory.getAndResetDelta(outDelta);
512 mGetFrameTimestampsCount++;
513 }
514 mAddAndGetFrameTimestampsCallCount++;
515 }
516
517 bool mGetFrameTimestampsEnabled = false;
518
519 ConsumerFrameEventHistory mFrameEventHistory;
520 int mAddAndGetFrameTimestampsCallCount = 0;
521 int mAddFrameTimestampsCount = 0;
522 int mGetFrameTimestampsCount = 0;
523 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
524
525 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
526};
527
528
529class FakeSurfaceComposer : public ISurfaceComposer{
530public:
531 ~FakeSurfaceComposer() override {}
532
Brian Anderson6b376712017-04-04 10:51:39 -0700533 void setSupportsPresent(bool supportsPresent) {
534 mSupportsPresent = supportsPresent;
535 }
536
Brian Anderson3da8d272016-07-28 16:20:47 -0700537 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800538 sp<ISurfaceComposerClient> createScopedConnection(
539 const sp<IGraphicBufferProducer>& /* parent */) override {
540 return nullptr;
541 }
Jorim Jaggib1e2f8d2017-06-08 15:43:59 -0700542 sp<IDisplayEventConnection> createDisplayEventConnection(ISurfaceComposer::VsyncSource)
543 override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700544 return nullptr;
545 }
546 sp<IBinder> createDisplay(const String8& /*displayName*/,
547 bool /*secure*/) override { return nullptr; }
548 void destroyDisplay(const sp<IBinder>& /*display */) override {}
549 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
550 void setTransactionState(const Vector<ComposerState>& /*state*/,
551 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
552 override {}
553 void bootFinished() override {}
554 bool authenticateSurfaceTexture(
555 const sp<IGraphicBufferProducer>& /*surface*/) const override {
556 return false;
557 }
Brian Anderson6b376712017-04-04 10:51:39 -0700558
559 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
560 const override {
561 *outSupported = {
562 FrameEvent::REQUESTED_PRESENT,
563 FrameEvent::ACQUIRE,
564 FrameEvent::LATCH,
565 FrameEvent::FIRST_REFRESH_START,
566 FrameEvent::LAST_REFRESH_START,
567 FrameEvent::GPU_COMPOSITION_DONE,
568 FrameEvent::DEQUEUE_READY,
569 FrameEvent::RELEASE
570 };
571 if (mSupportsPresent) {
572 outSupported->push_back(
573 FrameEvent::DISPLAY_PRESENT);
574 }
575 return NO_ERROR;
576 }
577
Brian Anderson3da8d272016-07-28 16:20:47 -0700578 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
579 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
580 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
581 status_t getDisplayStats(const sp<IBinder>& /*display*/,
582 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
583 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
584 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
585 override {
586 return NO_ERROR;
587 }
588 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700589 Vector<ColorMode>* /*outColorModes*/) override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700590 return NO_ERROR;
591 }
Peiyong Lina52f0292018-03-14 17:26:31 -0700592 ColorMode getActiveColorMode(const sp<IBinder>& /*display*/)
Brian Anderson3da8d272016-07-28 16:20:47 -0700593 override {
Peiyong Lina52f0292018-03-14 17:26:31 -0700594 return ColorMode::NATIVE;
Brian Anderson3da8d272016-07-28 16:20:47 -0700595 }
596 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700597 ColorMode /*colorMode*/) override { return NO_ERROR; }
Brian Anderson3da8d272016-07-28 16:20:47 -0700598 status_t captureScreen(const sp<IBinder>& /*display*/,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000599 sp<GraphicBuffer>* /*outBuffer*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700600 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800601 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700602 bool /*useIdentityTransform*/,
603 Rotation /*rotation*/) override { return NO_ERROR; }
chaviwa76b2712017-09-20 12:02:26 -0700604 virtual status_t captureLayers(const sp<IBinder>& /*parentHandle*/,
Robert Carr578038f2018-03-09 12:25:24 -0800605 sp<GraphicBuffer>* /*outBuffer*/, const Rect& /*sourceCrop*/,
606 float /*frameScale*/, bool /*childrenOnly*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700607 return NO_ERROR;
608 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700609 status_t clearAnimationFrameStats() override { return NO_ERROR; }
610 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
611 return NO_ERROR;
612 }
613 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
614 HdrCapabilities* /*outCapabilities*/) const override {
615 return NO_ERROR;
616 }
617 status_t enableVSyncInjections(bool /*enable*/) override {
618 return NO_ERROR;
619 }
620 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800621 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
622 return NO_ERROR;
623 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700624
625protected:
626 IBinder* onAsBinder() override { return nullptr; }
627
628private:
629 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700630};
631
632class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
633public:
634 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
635 : mFenceMap(fenceMap) {}
636
637 ~FakeProducerFrameEventHistory() {}
638
639 void updateAcquireFence(uint64_t frameNumber,
640 std::shared_ptr<FenceTime>&& acquire) override {
641 // Verify the acquire fence being added isn't the one from the consumer.
642 EXPECT_NE(mConsumerAcquireFence, acquire);
643 // Override the fence, so we can verify this was called by the
644 // producer after the frame is queued.
645 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
646 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
647 }
648
649 void setAcquireFenceOverride(
650 const std::shared_ptr<FenceTime>& acquireFenceOverride,
651 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
652 mAcquireFenceOverride = acquireFenceOverride;
653 mConsumerAcquireFence = consumerAcquireFence;
654 }
655
656protected:
657 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
658 const override {
659 return mFenceMap->createFenceTimeForTest(fence);
660 }
661
662 FenceToFenceTimeMap* mFenceMap{nullptr};
663
664 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
665 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
666};
667
668
669class TestSurface : public Surface {
670public:
671 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
672 FenceToFenceTimeMap* fenceMap)
673 : Surface(bufferProducer),
674 mFakeSurfaceComposer(new FakeSurfaceComposer) {
675 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
676 mFrameEventHistory.reset(mFakeFrameEventHistory);
677 }
678
679 ~TestSurface() override {}
680
681 sp<ISurfaceComposer> composerService() const override {
682 return mFakeSurfaceComposer;
683 }
684
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800685 nsecs_t now() const override {
686 return mNow;
687 }
688
689 void setNow(nsecs_t now) {
690 mNow = now;
691 }
692
Brian Anderson3da8d272016-07-28 16:20:47 -0700693public:
694 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800695 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700696
697 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
698 // but this raw pointer gives access to test functionality.
699 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
700};
701
702
Brian Andersond0010582017-03-07 13:20:31 -0800703class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700704protected:
705 struct FenceAndFenceTime {
706 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
707 : mFence(new Fence),
708 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
709 sp<Fence> mFence { nullptr };
710 std::shared_ptr<FenceTime> mFenceTime { nullptr };
711 };
712
713 struct RefreshEvents {
714 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800715 : mFenceMap(fenceMap),
716 kCompositorTiming(
717 {refreshStart, refreshStart + 1, refreshStart + 2 }),
718 kStartTime(refreshStart + 3),
719 kGpuCompositionDoneTime(refreshStart + 4),
720 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700721
722 void signalPostCompositeFences() {
723 mFenceMap.signalAllForTest(
724 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
725 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
726 }
727
728 FenceToFenceTimeMap& mFenceMap;
729
730 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
731 FenceAndFenceTime mPresent { mFenceMap };
732
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800733 const CompositorTiming kCompositorTiming;
734
Brian Anderson3da8d272016-07-28 16:20:47 -0700735 const nsecs_t kStartTime;
736 const nsecs_t kGpuCompositionDoneTime;
737 const nsecs_t kPresentTime;
738 };
739
740 struct FrameEvents {
741 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
742 : mFenceMap(fenceMap),
743 kPostedTime(frameStartTime + 100),
744 kRequestedPresentTime(frameStartTime + 200),
745 kProducerAcquireTime(frameStartTime + 300),
746 kConsumerAcquireTime(frameStartTime + 301),
747 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700748 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700749 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700750 mRefreshes {
751 { mFenceMap, frameStartTime + 410 },
752 { mFenceMap, frameStartTime + 420 },
753 { mFenceMap, frameStartTime + 430 } } {}
754
755 void signalQueueFences() {
756 mFenceMap.signalAllForTest(
757 mAcquireConsumer.mFence, kConsumerAcquireTime);
758 mFenceMap.signalAllForTest(
759 mAcquireProducer.mFence, kProducerAcquireTime);
760 }
761
762 void signalRefreshFences() {
763 for (auto& re : mRefreshes) {
764 re.signalPostCompositeFences();
765 }
766 }
767
768 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700769 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
770 }
771
772 FenceToFenceTimeMap& mFenceMap;
773
774 FenceAndFenceTime mAcquireConsumer { mFenceMap };
775 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700776 FenceAndFenceTime mRelease { mFenceMap };
777
778 const nsecs_t kPostedTime;
779 const nsecs_t kRequestedPresentTime;
780 const nsecs_t kProducerAcquireTime;
781 const nsecs_t kConsumerAcquireTime;
782 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700783 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700784 const nsecs_t kReleaseTime;
785
786 RefreshEvents mRefreshes[3];
787 };
788
Brian Andersond0010582017-03-07 13:20:31 -0800789 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700790
791 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700792 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
793 mFakeConsumer = new FakeConsumer;
794 mCfeh = &mFakeConsumer->mFrameEventHistory;
795 mConsumer->consumerConnect(mFakeConsumer, false);
796 mConsumer->setConsumerName(String8("TestConsumer"));
797 mSurface = new TestSurface(mProducer, &mFenceMap);
798 mWindow = mSurface;
799
800 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
801 NATIVE_WINDOW_API_CPU));
802 native_window_set_buffer_count(mWindow.get(), 4);
803 }
804
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800805 void disableFrameTimestamps() {
806 mFakeConsumer->mGetFrameTimestampsEnabled = false;
807 native_window_enable_frame_timestamps(mWindow.get(), 0);
808 mFrameTimestampsEnabled = false;
809 }
810
Brian Anderson3da8d272016-07-28 16:20:47 -0700811 void enableFrameTimestamps() {
812 mFakeConsumer->mGetFrameTimestampsEnabled = true;
813 native_window_enable_frame_timestamps(mWindow.get(), 1);
814 mFrameTimestampsEnabled = true;
815 }
816
Brian Anderson1049d1d2016-12-16 17:25:57 -0800817 int getAllFrameTimestamps(uint64_t frameId) {
818 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700819 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
820 &outFirstRefreshStartTime, &outLastRefreshStartTime,
821 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700822 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700823 }
824
Brian Anderson3da8d272016-07-28 16:20:47 -0700825 void resetTimestamps() {
826 outRequestedPresentTime = -1;
827 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700828 outLatchTime = -1;
829 outFirstRefreshStartTime = -1;
830 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700831 outGpuCompositionDoneTime = -1;
832 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700833 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700834 outReleaseTime = -1;
835 }
836
Brian Anderson1049d1d2016-12-16 17:25:57 -0800837 uint64_t getNextFrameId() {
838 uint64_t frameId = -1;
839 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
840 EXPECT_EQ(status, NO_ERROR);
841 return frameId;
842 }
843
Brian Anderson3da8d272016-07-28 16:20:47 -0700844 void dequeueAndQueue(uint64_t frameIndex) {
845 int fence = -1;
846 ANativeWindowBuffer* buffer = nullptr;
847 ASSERT_EQ(NO_ERROR,
848 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
849
850 int oldAddFrameTimestampsCount =
851 mFakeConsumer->mAddFrameTimestampsCount;
852
853 FrameEvents* frame = &mFrames[frameIndex];
854 uint64_t frameNumber = frameIndex + 1;
855
856 NewFrameEventsEntry fe;
857 fe.frameNumber = frameNumber;
858 fe.postedTime = frame->kPostedTime;
859 fe.requestedPresentTime = frame->kRequestedPresentTime;
860 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
861 mFakeConsumer->mNewFrameEntryOverride = fe;
862
863 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
864 frame->mAcquireProducer.mFenceTime,
865 frame->mAcquireConsumer.mFenceTime);
866
867 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
868
869 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
870
871 EXPECT_EQ(
872 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
873 mFakeConsumer->mAddFrameTimestampsCount);
874 }
875
876 void addFrameEvents(
877 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
878 FrameEvents* oldFrame =
879 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
880 FrameEvents* newFrame = &mFrames[iNewFrame];
881
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800882 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700883 uint64_t nNewFrame = iNewFrame + 1;
884
Brian Anderson4e606e32017-03-16 15:34:57 -0700885 // Latch, Composite, and Release the frames in a plausible order.
886 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700887 // that's okay for the purposes of this test.
888 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
889
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700890 // Composite the previous frame one more time, which helps verify
891 // LastRefresh is updated properly.
892 if (oldFrame != nullptr) {
893 mCfeh->addPreComposition(nOldFrame,
894 oldFrame->mRefreshes[2].kStartTime);
895 gpuDoneFenceTime = gpuComposited ?
896 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
897 FenceTime::NO_FENCE;
898 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800899 oldFrame->mRefreshes[2].mPresent.mFenceTime,
900 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700901 }
902
903 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700904 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
905
906 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
907 gpuDoneFenceTime = gpuComposited ?
908 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
909 FenceTime::NO_FENCE;
910 // HWC2 releases the previous buffer after a new latch just before
911 // calling postComposition.
912 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700913 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700914 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
915 }
916 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800917 newFrame->mRefreshes[0].mPresent.mFenceTime,
918 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700919
Brian Anderson3da8d272016-07-28 16:20:47 -0700920 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
921 gpuDoneFenceTime = gpuComposited ?
922 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
923 FenceTime::NO_FENCE;
924 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800925 newFrame->mRefreshes[1].mPresent.mFenceTime,
926 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700927 }
928
Brian Anderson3da8d272016-07-28 16:20:47 -0700929 sp<IGraphicBufferProducer> mProducer;
930 sp<IGraphicBufferConsumer> mConsumer;
931 sp<FakeConsumer> mFakeConsumer;
932 ConsumerFrameEventHistory* mCfeh;
933 sp<TestSurface> mSurface;
934 sp<ANativeWindow> mWindow;
935
936 FenceToFenceTimeMap mFenceMap;
937
938 bool mFrameTimestampsEnabled = false;
939
940 int64_t outRequestedPresentTime = -1;
941 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700942 int64_t outLatchTime = -1;
943 int64_t outFirstRefreshStartTime = -1;
944 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700945 int64_t outGpuCompositionDoneTime = -1;
946 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700947 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700948 int64_t outReleaseTime = -1;
949
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800950 FrameEvents mFrames[3] {
951 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700952};
953
954
955// This test verifies that the frame timestamps are not retrieved when not
956// explicitly enabled via native_window_enable_frame_timestamps.
957// We want to check this to make sure there's no overhead for users
958// that don't need the timestamp information.
959TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
960 int fence;
961 ANativeWindowBuffer* buffer;
962
963 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
964 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
965
Brian Anderson1049d1d2016-12-16 17:25:57 -0800966 const uint64_t fId = getNextFrameId();
967
Brian Anderson3da8d272016-07-28 16:20:47 -0700968 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
969 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
970 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
971 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
972
973 // Verify the producer doesn't get frame timestamps piggybacked on queue.
974 // It is okay that frame timestamps are added in the consumer since it is
975 // still needed for SurfaceFlinger dumps.
976 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
977 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
978 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
979
980 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800981 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700982 EXPECT_EQ(INVALID_OPERATION, result);
983 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800984
985 // Verify compositor timing query fails.
986 nsecs_t compositeDeadline = 0;
987 nsecs_t compositeInterval = 0;
988 nsecs_t compositeToPresentLatency = 0;
989 result = native_window_get_compositor_timing(mWindow.get(),
990 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
991 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700992}
993
994// This test verifies that the frame timestamps are retrieved if explicitly
995// enabled via native_window_enable_frame_timestamps.
996TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800997 CompositorTiming initialCompositorTiming {
998 1000000000, // 1s deadline
999 16666667, // 16ms interval
1000 50000000, // 50ms present latency
1001 };
1002 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1003
Brian Anderson3da8d272016-07-28 16:20:47 -07001004 enableFrameTimestamps();
1005
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001006 // Verify the compositor timing query gets the initial compositor values
1007 // after timststamps are enabled; even before the first frame is queued
1008 // or dequeued.
1009 nsecs_t compositeDeadline = 0;
1010 nsecs_t compositeInterval = 0;
1011 nsecs_t compositeToPresentLatency = 0;
1012 mSurface->setNow(initialCompositorTiming.deadline - 1);
1013 int result = native_window_get_compositor_timing(mWindow.get(),
1014 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1015 EXPECT_EQ(NO_ERROR, result);
1016 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1017 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1018 EXPECT_EQ(initialCompositorTiming.presentLatency,
1019 compositeToPresentLatency);
1020
Brian Anderson3da8d272016-07-28 16:20:47 -07001021 int fence;
1022 ANativeWindowBuffer* buffer;
1023
1024 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001025 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001026
Brian Anderson1049d1d2016-12-16 17:25:57 -08001027 const uint64_t fId1 = getNextFrameId();
1028
Brian Anderson3da8d272016-07-28 16:20:47 -07001029 // Verify getFrameTimestamps is piggybacked on dequeue.
1030 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1031 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001032 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001033
1034 NewFrameEventsEntry f1;
1035 f1.frameNumber = 1;
1036 f1.postedTime = mFrames[0].kPostedTime;
1037 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1038 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1039 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1040 mFrames[0].mAcquireProducer.mFenceTime,
1041 mFrames[0].mAcquireConsumer.mFenceTime);
1042 mFakeConsumer->mNewFrameEntryOverride = f1;
1043 mFrames[0].signalQueueFences();
1044
1045 // Verify getFrameTimestamps is piggybacked on queue.
1046 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1047 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1048 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001049 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001050
1051 // Verify queries for timestamps that the producer doesn't know about
1052 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001053 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001054 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001055 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001056}
1057
Brian Anderson6b376712017-04-04 10:51:39 -07001058TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1059 bool displayPresentSupported = true;
1060 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1061
1062 // Verify supported bits are forwarded.
1063 int supportsPresent = -1;
1064 mWindow.get()->query(mWindow.get(),
1065 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1066 EXPECT_EQ(displayPresentSupported, supportsPresent);
1067}
1068
1069TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1070 bool displayPresentSupported = false;
1071 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1072
1073 // Verify supported bits are forwarded.
1074 int supportsPresent = -1;
1075 mWindow.get()->query(mWindow.get(),
1076 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1077 EXPECT_EQ(displayPresentSupported, supportsPresent);
1078}
1079
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001080TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1081 nsecs_t phase = 4000;
1082 nsecs_t interval = 1000;
1083
1084 // Timestamp in previous interval.
1085 nsecs_t timestamp = 3500;
1086 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1087 timestamp, phase, interval));
1088
1089 // Timestamp in next interval.
1090 timestamp = 4500;
1091 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1092 timestamp, phase, interval));
1093
1094 // Timestamp multiple intervals before.
1095 timestamp = 2500;
1096 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1097 timestamp, phase, interval));
1098
1099 // Timestamp multiple intervals after.
1100 timestamp = 6500;
1101 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1102 timestamp, phase, interval));
1103
1104 // Timestamp on previous interval.
1105 timestamp = 3000;
1106 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1107 timestamp, phase, interval));
1108
1109 // Timestamp on next interval.
1110 timestamp = 5000;
1111 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1112 timestamp, phase, interval));
1113
1114 // Timestamp equal to phase.
1115 timestamp = 4000;
1116 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1117 timestamp, phase, interval));
1118}
1119
1120// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1121// if the number of intervals elapsed is internally stored in an int.
1122TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1123 nsecs_t phase = 0;
1124 nsecs_t interval = 4000;
1125 nsecs_t big_timestamp = 8635916564000;
1126 int32_t intervals = big_timestamp / interval;
1127
1128 EXPECT_LT(intervals, 0);
1129 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1130 big_timestamp, phase, interval));
1131 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1132 big_timestamp, big_timestamp, interval));
1133}
1134
1135// This verifies the compositor timing is updated by refresh events
1136// and piggy backed on a queue, dequeue, and enabling of timestamps..
1137TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1138 CompositorTiming initialCompositorTiming {
1139 1000000000, // 1s deadline
1140 16666667, // 16ms interval
1141 50000000, // 50ms present latency
1142 };
1143 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1144
1145 enableFrameTimestamps();
1146
1147 // We get the initial values before any frames are submitted.
1148 nsecs_t compositeDeadline = 0;
1149 nsecs_t compositeInterval = 0;
1150 nsecs_t compositeToPresentLatency = 0;
1151 mSurface->setNow(initialCompositorTiming.deadline - 1);
1152 int result = native_window_get_compositor_timing(mWindow.get(),
1153 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1154 EXPECT_EQ(NO_ERROR, result);
1155 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1156 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1157 EXPECT_EQ(initialCompositorTiming.presentLatency,
1158 compositeToPresentLatency);
1159
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001160 dequeueAndQueue(0);
1161 addFrameEvents(true, NO_FRAME_INDEX, 0);
1162
1163 // Still get the initial values because the frame events for frame 0
1164 // didn't get a chance to piggyback on a queue or dequeue yet.
1165 result = native_window_get_compositor_timing(mWindow.get(),
1166 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1167 EXPECT_EQ(NO_ERROR, result);
1168 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1169 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1170 EXPECT_EQ(initialCompositorTiming.presentLatency,
1171 compositeToPresentLatency);
1172
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001173 dequeueAndQueue(1);
1174 addFrameEvents(true, 0, 1);
1175
1176 // Now expect the composite values associated with frame 1.
1177 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1178 result = native_window_get_compositor_timing(mWindow.get(),
1179 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1180 EXPECT_EQ(NO_ERROR, result);
1181 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1182 compositeDeadline);
1183 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1184 compositeInterval);
1185 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1186 compositeToPresentLatency);
1187
1188 dequeueAndQueue(2);
1189 addFrameEvents(true, 1, 2);
1190
1191 // Now expect the composite values associated with frame 2.
1192 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1193 result = native_window_get_compositor_timing(mWindow.get(),
1194 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1195 EXPECT_EQ(NO_ERROR, result);
1196 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1197 compositeDeadline);
1198 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1199 compositeInterval);
1200 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1201 compositeToPresentLatency);
1202
1203 // Re-enabling frame timestamps should get the latest values.
1204 disableFrameTimestamps();
1205 enableFrameTimestamps();
1206
1207 // Now expect the composite values associated with frame 3.
1208 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1209 result = native_window_get_compositor_timing(mWindow.get(),
1210 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1211 EXPECT_EQ(NO_ERROR, result);
1212 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1213 compositeDeadline);
1214 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1215 compositeInterval);
1216 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1217 compositeToPresentLatency);
1218}
1219
1220// This verifies the compositor deadline properly snaps to the the next
1221// deadline based on the current time.
1222TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1223 CompositorTiming initialCompositorTiming {
1224 1000000000, // 1s deadline
1225 16666667, // 16ms interval
1226 50000000, // 50ms present latency
1227 };
1228 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1229
1230 enableFrameTimestamps();
1231
1232 nsecs_t compositeDeadline = 0;
1233 nsecs_t compositeInterval = 0;
1234 nsecs_t compositeToPresentLatency = 0;
1235
1236 // A "now" just before the deadline snaps to the deadline.
1237 mSurface->setNow(initialCompositorTiming.deadline - 1);
1238 int result = native_window_get_compositor_timing(mWindow.get(),
1239 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1240 EXPECT_EQ(NO_ERROR, result);
1241 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1242 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1243 EXPECT_EQ(expectedDeadline, compositeDeadline);
1244
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001245 dequeueAndQueue(0);
1246 addFrameEvents(true, NO_FRAME_INDEX, 0);
1247
1248 // A "now" just after the deadline snaps properly.
1249 mSurface->setNow(initialCompositorTiming.deadline + 1);
1250 result = native_window_get_compositor_timing(mWindow.get(),
1251 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1252 EXPECT_EQ(NO_ERROR, result);
1253 expectedDeadline =
1254 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1255 EXPECT_EQ(expectedDeadline, compositeDeadline);
1256
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001257 dequeueAndQueue(1);
1258 addFrameEvents(true, 0, 1);
1259
1260 // A "now" just after the next interval snaps properly.
1261 mSurface->setNow(
1262 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1263 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1264 result = native_window_get_compositor_timing(mWindow.get(),
1265 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1266 EXPECT_EQ(NO_ERROR, result);
1267 expectedDeadline =
1268 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1269 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1270 EXPECT_EQ(expectedDeadline, compositeDeadline);
1271
1272 dequeueAndQueue(2);
1273 addFrameEvents(true, 1, 2);
1274
1275 // A "now" over 1 interval before the deadline snaps properly.
1276 mSurface->setNow(
1277 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1278 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1279 result = native_window_get_compositor_timing(mWindow.get(),
1280 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1281 EXPECT_EQ(NO_ERROR, result);
1282 expectedDeadline =
1283 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1284 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1285 EXPECT_EQ(expectedDeadline, compositeDeadline);
1286
1287 // Re-enabling frame timestamps should get the latest values.
1288 disableFrameTimestamps();
1289 enableFrameTimestamps();
1290
1291 // A "now" over 2 intervals before the deadline snaps properly.
1292 mSurface->setNow(
1293 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1294 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1295 result = native_window_get_compositor_timing(mWindow.get(),
1296 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1297 EXPECT_EQ(NO_ERROR, result);
1298 expectedDeadline =
1299 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1300 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1301 EXPECT_EQ(expectedDeadline, compositeDeadline);
1302}
1303
Brian Anderson1049d1d2016-12-16 17:25:57 -08001304// This verifies the timestamps recorded in the consumer's
1305// FrameTimestampsHistory are properly retrieved by the producer for the
1306// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001307TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1308 enableFrameTimestamps();
1309
Brian Anderson1049d1d2016-12-16 17:25:57 -08001310 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001311 dequeueAndQueue(0);
1312 mFrames[0].signalQueueFences();
1313
Brian Anderson1049d1d2016-12-16 17:25:57 -08001314 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001315 dequeueAndQueue(1);
1316 mFrames[1].signalQueueFences();
1317
1318 addFrameEvents(true, NO_FRAME_INDEX, 0);
1319 mFrames[0].signalRefreshFences();
1320 addFrameEvents(true, 0, 1);
1321 mFrames[0].signalReleaseFences();
1322 mFrames[1].signalRefreshFences();
1323
1324 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001325 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001326 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001327 EXPECT_EQ(NO_ERROR, result);
1328 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1329 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001330 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1331 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1332 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001333 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1334 outGpuCompositionDoneTime);
1335 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001336 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001337 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1338
1339 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001340 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001341 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001342 EXPECT_EQ(NO_ERROR, result);
1343 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1344 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001345 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1346 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1347 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001348 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1349 outGpuCompositionDoneTime);
1350 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001351 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1352 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001353}
1354
1355// This test verifies the acquire fence recorded by the consumer is not sent
1356// back to the producer and the producer saves its own fence.
1357TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1358 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001359
Brian Anderson3da8d272016-07-28 16:20:47 -07001360 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001361 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001362 dequeueAndQueue(0);
1363
1364 // Verify queue-related timestamps for f1 are available immediately in the
1365 // producer without asking the consumer again, even before signaling the
1366 // acquire fence.
1367 resetTimestamps();
1368 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001369 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001370 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001371 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001372 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1373 EXPECT_EQ(NO_ERROR, result);
1374 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001375 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001376
1377 // Signal acquire fences. Verify a sync call still isn't necessary.
1378 mFrames[0].signalQueueFences();
1379
1380 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001381 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001382 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001383 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001384 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1385 EXPECT_EQ(NO_ERROR, result);
1386 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1387 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1388
1389 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001390 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001391 dequeueAndQueue(1);
1392
1393 // Verify queue-related timestamps for f2 are available immediately in the
1394 // producer without asking the consumer again, even before signaling the
1395 // acquire fence.
1396 resetTimestamps();
1397 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001398 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001399 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001400 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001401 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1402 EXPECT_EQ(NO_ERROR, result);
1403 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001404 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001405
1406 // Signal acquire fences. Verify a sync call still isn't necessary.
1407 mFrames[1].signalQueueFences();
1408
1409 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001410 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001411 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001412 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001413 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1414 EXPECT_EQ(NO_ERROR, result);
1415 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1416 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1417}
1418
1419TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1420 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001421
1422 // Dequeue and queue frame 1.
1423 dequeueAndQueue(0);
1424 mFrames[0].signalQueueFences();
1425
1426 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001427 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001428 dequeueAndQueue(1);
1429 mFrames[1].signalQueueFences();
1430
1431 addFrameEvents(true, NO_FRAME_INDEX, 0);
1432 mFrames[0].signalRefreshFences();
1433 addFrameEvents(true, 0, 1);
1434 mFrames[0].signalReleaseFences();
1435 mFrames[1].signalRefreshFences();
1436
1437 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001438 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001439 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001440 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1441 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001442 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001443 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1444}
1445
1446// This test verifies that fences can signal and update timestamps producer
1447// side without an additional sync call to the consumer.
1448TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1449 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001450
1451 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001452 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001453 dequeueAndQueue(0);
1454 mFrames[0].signalQueueFences();
1455
1456 // Dequeue and queue frame 2.
1457 dequeueAndQueue(1);
1458 mFrames[1].signalQueueFences();
1459
1460 addFrameEvents(true, NO_FRAME_INDEX, 0);
1461 addFrameEvents(true, 0, 1);
1462
1463 // Verify available timestamps are correct for frame 1, before any
1464 // fence has been signaled.
1465 // Note: A sync call is necessary here since the events triggered by
1466 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001467 resetTimestamps();
1468 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001469 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001470 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1471 EXPECT_EQ(NO_ERROR, result);
1472 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1473 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001474 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1475 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1476 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001477 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1478 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001479 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001480 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001481
1482 // Verify available timestamps are correct for frame 1 again, before any
1483 // fence has been signaled.
1484 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001485 resetTimestamps();
1486 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001487 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001488 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1489 EXPECT_EQ(NO_ERROR, result);
1490 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1491 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001492 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1493 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1494 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001495 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1496 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001497 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001498 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001499
1500 // Signal the fences for frame 1.
1501 mFrames[0].signalRefreshFences();
1502 mFrames[0].signalReleaseFences();
1503
1504 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001505 resetTimestamps();
1506 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001507 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001508 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1509 EXPECT_EQ(NO_ERROR, result);
1510 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1511 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001512 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1513 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1514 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001515 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1516 outGpuCompositionDoneTime);
1517 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001518 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001519 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1520}
1521
1522// This test verifies that if the frame wasn't GPU composited but has a refresh
1523// event a sync call isn't made to get the GPU composite done time since it will
1524// never exist.
1525TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1526 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001527
Brian Anderson3da8d272016-07-28 16:20:47 -07001528 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001529 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001530 dequeueAndQueue(0);
1531 mFrames[0].signalQueueFences();
1532
1533 // Dequeue and queue frame 2.
1534 dequeueAndQueue(1);
1535 mFrames[1].signalQueueFences();
1536
1537 addFrameEvents(false, NO_FRAME_INDEX, 0);
1538 addFrameEvents(false, 0, 1);
1539
1540 // Verify available timestamps are correct for frame 1, before any
1541 // fence has been signaled.
1542 // Note: A sync call is necessary here since the events triggered by
1543 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1544 resetTimestamps();
1545 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001546 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001547 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1548 EXPECT_EQ(NO_ERROR, result);
1549 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1550 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001551 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1552 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1553 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001554 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1555 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001556 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001557 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001558
1559 // Signal the fences for frame 1.
1560 mFrames[0].signalRefreshFences();
1561 mFrames[0].signalReleaseFences();
1562
1563 // Verify all timestamps, except GPU composition, are available without a
1564 // sync call.
1565 resetTimestamps();
1566 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001567 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001568 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1569 EXPECT_EQ(NO_ERROR, result);
1570 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1571 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001572 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1573 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1574 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001575 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001576 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001577 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001578 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1579}
1580
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001581// This test verifies that if the certain timestamps can't possibly exist for
1582// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001583TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001584 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001585
1586 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001587 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001588 dequeueAndQueue(0);
1589 mFrames[0].signalQueueFences();
1590
1591 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001592 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001593 dequeueAndQueue(1);
1594 mFrames[1].signalQueueFences();
1595
1596 addFrameEvents(false, NO_FRAME_INDEX, 0);
1597 addFrameEvents(false, 0, 1);
1598
1599 // Verify available timestamps are correct for frame 1, before any
1600 // fence has been signaled.
1601 // Note: A sync call is necessary here since the events triggered by
1602 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001603 resetTimestamps();
1604 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001605 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001606 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1607 EXPECT_EQ(NO_ERROR, result);
1608 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1609 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001610 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1611 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1612 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001613 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1614 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001615 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001616 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001617
1618 mFrames[0].signalRefreshFences();
1619 mFrames[0].signalReleaseFences();
1620 mFrames[1].signalRefreshFences();
1621
Brian Anderson1049d1d2016-12-16 17:25:57 -08001622 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001623 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001624 // available, a sync call should not occur because it's not possible for f2
1625 // to encounter the final value for those events until another frame is
1626 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001627 resetTimestamps();
1628 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001629 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001630 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1631 EXPECT_EQ(NO_ERROR, result);
1632 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1633 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001634 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1635 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1636 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001637 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001638 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001639 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1640 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001641}
1642
Brian Anderson6b376712017-04-04 10:51:39 -07001643// This test verifies there are no sync calls for present times
1644// when they aren't supported and that an error is returned.
1645
1646TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1647 enableFrameTimestamps();
1648 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1649
1650 // Dequeue and queue frame 1.
1651 const uint64_t fId1 = getNextFrameId();
1652 dequeueAndQueue(0);
1653
1654 // Verify a query for the Present times do not trigger a sync call if they
1655 // are not supported.
1656 resetTimestamps();
1657 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1658 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1659 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1660 &outDisplayPresentTime, nullptr, nullptr);
1661 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1662 EXPECT_EQ(BAD_VALUE, result);
1663 EXPECT_EQ(-1, outDisplayPresentTime);
1664}
1665
Dan Stoza932f0082017-05-31 13:50:16 -07001666} // namespace android