blob: cd29d4a6671de73254c0efd6a90f28bf42f6e29e [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
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -070050static bool hasHdrDisplay =
51 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
52
Brian Anderson3da8d272016-07-28 16:20:47 -070053class FakeSurfaceComposer;
54class FakeProducerFrameEventHistory;
55
56static constexpr uint64_t NO_FRAME_INDEX = std::numeric_limits<uint64_t>::max();
57
Jamie Gennis134f0422011-03-08 12:18:54 -080058class SurfaceTest : public ::testing::Test {
59protected:
Mathias Agopian7c1a4872013-03-20 15:56:04 -070060
61 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
95TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
96 sp<ANativeWindow> anw(mSurface);
97 int result = -123;
98 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
99 &result);
100 EXPECT_EQ(NO_ERROR, err);
101 EXPECT_EQ(1, result);
102}
103
104TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
105 mSurfaceControl.clear();
Kalle Raita643f0942016-12-07 09:20:14 -0800106 // Wait for the async clean-up to complete.
107 std::this_thread::sleep_for(50ms);
Jamie Gennis134f0422011-03-08 12:18:54 -0800108
109 sp<ANativeWindow> anw(mSurface);
110 int result = -123;
111 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
112 &result);
113 EXPECT_EQ(NO_ERROR, err);
114 EXPECT_EQ(1, result);
115}
116
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800117// This test probably doesn't belong here.
Jamie Gennisc901ca02011-10-11 16:02:31 -0700118TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800119 sp<ANativeWindow> anw(mSurface);
120
121 // Verify the screenshot works with no protected buffers.
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800122 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Brian Anderson3da8d272016-07-28 16:20:47 -0700123 sp<IBinder> display(sf->getBuiltInDisplay(
124 ISurfaceComposer::eDisplayIdMain));
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000125 sp<GraphicBuffer> outBuffer;
126 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &outBuffer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800127 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800128
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700129 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
130 NATIVE_WINDOW_API_CPU));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800131 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
132 // that we need to dequeue a buffer in order for it to actually get
133 // allocated in SurfaceFlinger.
134 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
135 GRALLOC_USAGE_PROTECTED));
136 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Iliyan Malchev697526b2011-05-01 11:33:26 -0700137 ANativeWindowBuffer* buf = 0;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700138
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700139 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700140 if (err) {
141 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
142 // that's okay as long as this is the reason for the failure.
143 // try again without the GRALLOC_USAGE_PROTECTED bit.
144 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700145 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
146 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700147 return;
148 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700149 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700150
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800151 for (int i = 0; i < 4; i++) {
152 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700153 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
154 &buf));
155 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800156 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000157 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &outBuffer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800158 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800159}
160
Jamie Gennis391bbe22011-03-14 15:00:06 -0700161TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
162 sp<ANativeWindow> anw(mSurface);
163 int result = -123;
164 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
165 EXPECT_EQ(NO_ERROR, err);
166 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
167}
168
Craig Donner6ebc46a2016-10-21 15:23:44 -0700169TEST_F(SurfaceTest, LayerCountIsOne) {
170 sp<ANativeWindow> anw(mSurface);
171 int result = -123;
172 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
173 EXPECT_EQ(NO_ERROR, err);
174 EXPECT_EQ(1, result);
175}
176
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700177TEST_F(SurfaceTest, QueryConsumerUsage) {
178 const int TEST_USAGE_FLAGS =
179 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700180 sp<IGraphicBufferProducer> producer;
181 sp<IGraphicBufferConsumer> consumer;
182 BufferQueue::createBufferQueue(&producer, &consumer);
183 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700184 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700185 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700186
187 sp<ANativeWindow> anw(s);
188
189 int flags = -1;
190 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
191
192 ASSERT_EQ(NO_ERROR, err);
193 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
194}
195
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800196TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
197 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
198 sp<IGraphicBufferProducer> producer;
199 sp<IGraphicBufferConsumer> consumer;
200 BufferQueue::createBufferQueue(&producer, &consumer);
201 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
202
203 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
204
205 sp<Surface> s = new Surface(producer);
206
207 sp<ANativeWindow> anw(s);
208
209 android_dataspace dataSpace;
210
211 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
212 reinterpret_cast<int*>(&dataSpace));
213
214 ASSERT_EQ(NO_ERROR, err);
215 ASSERT_EQ(TEST_DATASPACE, dataSpace);
216}
217
Dan Stoza812ed062015-06-02 15:45:22 -0700218TEST_F(SurfaceTest, SettingGenerationNumber) {
219 sp<IGraphicBufferProducer> producer;
220 sp<IGraphicBufferConsumer> consumer;
221 BufferQueue::createBufferQueue(&producer, &consumer);
222 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
223 sp<Surface> surface = new Surface(producer);
224 sp<ANativeWindow> window(surface);
225
226 // Allocate a buffer with a generation number of 0
227 ANativeWindowBuffer* buffer;
228 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700229 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
230 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700231 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
232 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
233
234 // Detach the buffer and check its generation number
235 sp<GraphicBuffer> graphicBuffer;
236 sp<Fence> fence;
237 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
238 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
239
240 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
241 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
242
243 // This should change the generation number of the GraphicBuffer
244 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
245
246 // Check that the new generation number sticks with the buffer
247 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
248 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
249 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
250 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
251}
252
Dan Stozac6f30bd2015-06-08 09:32:50 -0700253TEST_F(SurfaceTest, GetConsumerName) {
254 sp<IGraphicBufferProducer> producer;
255 sp<IGraphicBufferConsumer> consumer;
256 BufferQueue::createBufferQueue(&producer, &consumer);
257
258 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
259 consumer->consumerConnect(dummyConsumer, false);
260 consumer->setConsumerName(String8("TestConsumer"));
261
262 sp<Surface> surface = new Surface(producer);
263 sp<ANativeWindow> window(surface);
264 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
265
266 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
267}
268
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700269TEST_F(SurfaceTest, GetWideColorSupport) {
270 sp<IGraphicBufferProducer> producer;
271 sp<IGraphicBufferConsumer> consumer;
272 BufferQueue::createBufferQueue(&producer, &consumer);
273
274 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
275 consumer->consumerConnect(dummyConsumer, false);
276 consumer->setConsumerName(String8("TestConsumer"));
277
278 sp<Surface> surface = new Surface(producer);
279 sp<ANativeWindow> window(surface);
280 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
281
282 bool supported;
283 surface->getWideColorSupport(&supported);
284
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -0600285 // NOTE: This test assumes that device that supports
286 // wide-color (as indicated by BoardConfig) must also
287 // have a wide-color primary display.
288 // That assumption allows this test to cover devices
289 // that advertised a wide-color color mode without
290 // actually supporting wide-color to pass this test
291 // as well as the case of a device that does support
292 // wide-color (via BoardConfig) and has a wide-color
293 // primary display.
294 // NOT covered at this time is a device that supports
295 // wide color in the BoardConfig but does not support
296 // a wide-color color mode on the primary display.
297 ASSERT_EQ(hasWideColorDisplay, supported);
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700298}
299
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700300TEST_F(SurfaceTest, GetHdrSupport) {
301 sp<IGraphicBufferProducer> producer;
302 sp<IGraphicBufferConsumer> consumer;
303 BufferQueue::createBufferQueue(&producer, &consumer);
304
305 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
306 consumer->consumerConnect(dummyConsumer, false);
307 consumer->setConsumerName(String8("TestConsumer"));
308
309 sp<Surface> surface = new Surface(producer);
310 sp<ANativeWindow> window(surface);
311 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
312
313 bool supported;
314 status_t result = surface->getHdrSupport(&supported);
315 ASSERT_EQ(NO_ERROR, result);
316
317 // NOTE: This is not a CTS test.
318 // This test verifies that when the BoardConfig TARGET_HAS_HDR_DISPLAY
319 // is TRUE, getHdrSupport is also true.
320 // TODO: Add check for an HDR color mode on the primary display.
321 ASSERT_EQ(hasHdrDisplay, supported);
322}
323
324TEST_F(SurfaceTest, SetHdrMetadata) {
325 sp<IGraphicBufferProducer> producer;
326 sp<IGraphicBufferConsumer> consumer;
327 BufferQueue::createBufferQueue(&producer, &consumer);
328
329 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
330 consumer->consumerConnect(dummyConsumer, false);
331 consumer->setConsumerName(String8("TestConsumer"));
332
333 sp<Surface> surface = new Surface(producer);
334 sp<ANativeWindow> window(surface);
335 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
336
337 bool supported;
338 status_t result = surface->getHdrSupport(&supported);
339 ASSERT_EQ(NO_ERROR, result);
340
341 if (!hasHdrDisplay || !supported) {
342 return;
343 }
344 const android_smpte2086_metadata smpte2086 = {
345 {0.680, 0.320},
346 {0.265, 0.690},
347 {0.150, 0.060},
348 {0.3127, 0.3290},
349 100.0,
350 0.1,
351 };
352 const android_cta861_3_metadata cta861_3 = {
353 78.0,
354 62.0,
355 };
356 int error = native_window_set_buffers_smpte2086_metadata(window.get(), &smpte2086);
357 ASSERT_EQ(error, NO_ERROR);
358 error = native_window_set_buffers_cta861_3_metadata(window.get(), &cta861_3);
359 ASSERT_EQ(error, NO_ERROR);
360}
361
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800362TEST_F(SurfaceTest, DynamicSetBufferCount) {
363 sp<IGraphicBufferProducer> producer;
364 sp<IGraphicBufferConsumer> consumer;
365 BufferQueue::createBufferQueue(&producer, &consumer);
366
367 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
368 consumer->consumerConnect(dummyConsumer, false);
369 consumer->setConsumerName(String8("TestConsumer"));
370
371 sp<Surface> surface = new Surface(producer);
372 sp<ANativeWindow> window(surface);
373
374 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
375 NATIVE_WINDOW_API_CPU));
376 native_window_set_buffer_count(window.get(), 4);
377
378 int fence;
379 ANativeWindowBuffer* buffer;
380 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
381 native_window_set_buffer_count(window.get(), 3);
382 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
383 native_window_set_buffer_count(window.get(), 2);
384 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
385 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
386}
387
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700388TEST_F(SurfaceTest, GetAndFlushRemovedBuffers) {
389 sp<IGraphicBufferProducer> producer;
390 sp<IGraphicBufferConsumer> consumer;
391 BufferQueue::createBufferQueue(&producer, &consumer);
392
393 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
394 consumer->consumerConnect(dummyConsumer, false);
395 consumer->setConsumerName(String8("TestConsumer"));
396
397 sp<Surface> surface = new Surface(producer);
398 sp<ANativeWindow> window(surface);
399 sp<DummyProducerListener> listener = new DummyProducerListener();
400 ASSERT_EQ(OK, surface->connect(
401 NATIVE_WINDOW_API_CPU,
402 /*listener*/listener,
403 /*reportBufferRemoval*/true));
404 const int BUFFER_COUNT = 4;
405 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
406
407 sp<GraphicBuffer> detachedBuffer;
408 sp<Fence> outFence;
409 int fences[BUFFER_COUNT];
410 ANativeWindowBuffer* buffers[BUFFER_COUNT];
411 // Allocate buffers because detachNextBuffer requires allocated buffers
412 for (int i = 0; i < BUFFER_COUNT; i++) {
413 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
414 }
415 for (int i = 0; i < BUFFER_COUNT; i++) {
416 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
417 }
418
419 // Test detached buffer is correctly reported
420 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
421 std::vector<sp<GraphicBuffer>> removedBuffers;
422 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
423 ASSERT_EQ(1u, removedBuffers.size());
424 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
425 // Test the list is flushed one getAndFlushRemovedBuffers returns
426 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
427 ASSERT_EQ(0u, removedBuffers.size());
428
429
430 // Test removed buffer list is cleanup after next dequeueBuffer call
431 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
432 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[0], &fences[0]));
433 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
434 ASSERT_EQ(0u, removedBuffers.size());
435 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[0], fences[0]));
436
437 // Test removed buffer list is cleanup after next detachNextBuffer call
438 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
439 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
440 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
441 ASSERT_EQ(1u, removedBuffers.size());
442 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
443
444 // Re-allocate buffers since all buffers are detached up to now
445 for (int i = 0; i < BUFFER_COUNT; i++) {
446 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
447 }
448 for (int i = 0; i < BUFFER_COUNT; i++) {
449 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
450 }
451
452 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
453 ASSERT_EQ(NO_ERROR, surface->attachBuffer(detachedBuffer.get()));
454 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
455 // Depends on which slot GraphicBufferProducer impl pick, the attach call might
456 // get 0 or 1 buffer removed.
457 ASSERT_LE(removedBuffers.size(), 1u);
458}
Brian Anderson3da8d272016-07-28 16:20:47 -0700459
Dan Stoza932f0082017-05-31 13:50:16 -0700460TEST_F(SurfaceTest, TestGetLastDequeueStartTime) {
461 sp<ANativeWindow> anw(mSurface);
462 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU));
463
464 ANativeWindowBuffer* buffer = nullptr;
465 int32_t fenceFd = -1;
466
467 nsecs_t before = systemTime(CLOCK_MONOTONIC);
468 anw->dequeueBuffer(anw.get(), &buffer, &fenceFd);
469 nsecs_t after = systemTime(CLOCK_MONOTONIC);
470
471 nsecs_t lastDequeueTime = mSurface->getLastDequeueStartTime();
472 ASSERT_LE(before, lastDequeueTime);
473 ASSERT_GE(after, lastDequeueTime);
474}
475
Brian Anderson3da8d272016-07-28 16:20:47 -0700476class FakeConsumer : public BnConsumerListener {
477public:
478 void onFrameAvailable(const BufferItem& /*item*/) override {}
479 void onBuffersReleased() override {}
480 void onSidebandStreamChanged() override {}
481
482 void addAndGetFrameTimestamps(
483 const NewFrameEventsEntry* newTimestamps,
484 FrameEventHistoryDelta* outDelta) override {
485 if (newTimestamps) {
486 if (mGetFrameTimestampsEnabled) {
487 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
488 "Test should set mNewFrameEntryOverride before queuing "
489 "a frame.";
490 EXPECT_EQ(newTimestamps->frameNumber,
491 mNewFrameEntryOverride.frameNumber) <<
492 "Test attempting to add NewFrameEntryOverride with "
493 "incorrect frame number.";
494 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
495 mNewFrameEntryOverride.frameNumber = 0;
496 }
497 mAddFrameTimestampsCount++;
498 mLastAddedFrameNumber = newTimestamps->frameNumber;
499 }
500 if (outDelta) {
501 mFrameEventHistory.getAndResetDelta(outDelta);
502 mGetFrameTimestampsCount++;
503 }
504 mAddAndGetFrameTimestampsCallCount++;
505 }
506
507 bool mGetFrameTimestampsEnabled = false;
508
509 ConsumerFrameEventHistory mFrameEventHistory;
510 int mAddAndGetFrameTimestampsCallCount = 0;
511 int mAddFrameTimestampsCount = 0;
512 int mGetFrameTimestampsCount = 0;
513 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
514
515 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
516};
517
518
519class FakeSurfaceComposer : public ISurfaceComposer{
520public:
521 ~FakeSurfaceComposer() override {}
522
Brian Anderson6b376712017-04-04 10:51:39 -0700523 void setSupportsPresent(bool supportsPresent) {
524 mSupportsPresent = supportsPresent;
525 }
526
Brian Anderson3da8d272016-07-28 16:20:47 -0700527 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800528 sp<ISurfaceComposerClient> createScopedConnection(
529 const sp<IGraphicBufferProducer>& /* parent */) override {
530 return nullptr;
531 }
Jorim Jaggib1e2f8d2017-06-08 15:43:59 -0700532 sp<IDisplayEventConnection> createDisplayEventConnection(ISurfaceComposer::VsyncSource)
533 override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700534 return nullptr;
535 }
536 sp<IBinder> createDisplay(const String8& /*displayName*/,
537 bool /*secure*/) override { return nullptr; }
538 void destroyDisplay(const sp<IBinder>& /*display */) override {}
539 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
540 void setTransactionState(const Vector<ComposerState>& /*state*/,
541 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
542 override {}
543 void bootFinished() override {}
544 bool authenticateSurfaceTexture(
545 const sp<IGraphicBufferProducer>& /*surface*/) const override {
546 return false;
547 }
Brian Anderson6b376712017-04-04 10:51:39 -0700548
549 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
550 const override {
551 *outSupported = {
552 FrameEvent::REQUESTED_PRESENT,
553 FrameEvent::ACQUIRE,
554 FrameEvent::LATCH,
555 FrameEvent::FIRST_REFRESH_START,
556 FrameEvent::LAST_REFRESH_START,
557 FrameEvent::GPU_COMPOSITION_DONE,
558 FrameEvent::DEQUEUE_READY,
559 FrameEvent::RELEASE
560 };
561 if (mSupportsPresent) {
562 outSupported->push_back(
563 FrameEvent::DISPLAY_PRESENT);
564 }
565 return NO_ERROR;
566 }
567
Brian Anderson3da8d272016-07-28 16:20:47 -0700568 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
569 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
570 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
571 status_t getDisplayStats(const sp<IBinder>& /*display*/,
572 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
573 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
574 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
575 override {
576 return NO_ERROR;
577 }
578 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
579 Vector<android_color_mode_t>* /*outColorModes*/) override {
580 return NO_ERROR;
581 }
582 android_color_mode_t getActiveColorMode(const sp<IBinder>& /*display*/)
583 override {
584 return HAL_COLOR_MODE_NATIVE;
585 }
586 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
587 android_color_mode_t /*colorMode*/) override { return NO_ERROR; }
588 status_t captureScreen(const sp<IBinder>& /*display*/,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000589 sp<GraphicBuffer>* /*outBuffer*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700590 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800591 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700592 bool /*useIdentityTransform*/,
593 Rotation /*rotation*/) override { return NO_ERROR; }
chaviwa76b2712017-09-20 12:02:26 -0700594 virtual status_t captureLayers(const sp<IBinder>& /*parentHandle*/,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000595 sp<GraphicBuffer>* /*outBuffer*/,
chaviw7206d492017-11-10 16:16:12 -0800596 const Rect& /*sourceCrop*/, float /*frameScale*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700597 return NO_ERROR;
598 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700599 status_t clearAnimationFrameStats() override { return NO_ERROR; }
600 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
601 return NO_ERROR;
602 }
603 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
604 HdrCapabilities* /*outCapabilities*/) const override {
605 return NO_ERROR;
606 }
607 status_t enableVSyncInjections(bool /*enable*/) override {
608 return NO_ERROR;
609 }
610 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800611 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
612 return NO_ERROR;
613 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700614
615protected:
616 IBinder* onAsBinder() override { return nullptr; }
617
618private:
619 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700620};
621
622class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
623public:
624 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
625 : mFenceMap(fenceMap) {}
626
627 ~FakeProducerFrameEventHistory() {}
628
629 void updateAcquireFence(uint64_t frameNumber,
630 std::shared_ptr<FenceTime>&& acquire) override {
631 // Verify the acquire fence being added isn't the one from the consumer.
632 EXPECT_NE(mConsumerAcquireFence, acquire);
633 // Override the fence, so we can verify this was called by the
634 // producer after the frame is queued.
635 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
636 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
637 }
638
639 void setAcquireFenceOverride(
640 const std::shared_ptr<FenceTime>& acquireFenceOverride,
641 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
642 mAcquireFenceOverride = acquireFenceOverride;
643 mConsumerAcquireFence = consumerAcquireFence;
644 }
645
646protected:
647 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
648 const override {
649 return mFenceMap->createFenceTimeForTest(fence);
650 }
651
652 FenceToFenceTimeMap* mFenceMap{nullptr};
653
654 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
655 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
656};
657
658
659class TestSurface : public Surface {
660public:
661 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
662 FenceToFenceTimeMap* fenceMap)
663 : Surface(bufferProducer),
664 mFakeSurfaceComposer(new FakeSurfaceComposer) {
665 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
666 mFrameEventHistory.reset(mFakeFrameEventHistory);
667 }
668
669 ~TestSurface() override {}
670
671 sp<ISurfaceComposer> composerService() const override {
672 return mFakeSurfaceComposer;
673 }
674
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800675 nsecs_t now() const override {
676 return mNow;
677 }
678
679 void setNow(nsecs_t now) {
680 mNow = now;
681 }
682
Brian Anderson3da8d272016-07-28 16:20:47 -0700683public:
684 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800685 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700686
687 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
688 // but this raw pointer gives access to test functionality.
689 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
690};
691
692
Brian Andersond0010582017-03-07 13:20:31 -0800693class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700694protected:
695 struct FenceAndFenceTime {
696 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
697 : mFence(new Fence),
698 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
699 sp<Fence> mFence { nullptr };
700 std::shared_ptr<FenceTime> mFenceTime { nullptr };
701 };
702
703 struct RefreshEvents {
704 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800705 : mFenceMap(fenceMap),
706 kCompositorTiming(
707 {refreshStart, refreshStart + 1, refreshStart + 2 }),
708 kStartTime(refreshStart + 3),
709 kGpuCompositionDoneTime(refreshStart + 4),
710 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700711
712 void signalPostCompositeFences() {
713 mFenceMap.signalAllForTest(
714 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
715 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
716 }
717
718 FenceToFenceTimeMap& mFenceMap;
719
720 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
721 FenceAndFenceTime mPresent { mFenceMap };
722
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800723 const CompositorTiming kCompositorTiming;
724
Brian Anderson3da8d272016-07-28 16:20:47 -0700725 const nsecs_t kStartTime;
726 const nsecs_t kGpuCompositionDoneTime;
727 const nsecs_t kPresentTime;
728 };
729
730 struct FrameEvents {
731 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
732 : mFenceMap(fenceMap),
733 kPostedTime(frameStartTime + 100),
734 kRequestedPresentTime(frameStartTime + 200),
735 kProducerAcquireTime(frameStartTime + 300),
736 kConsumerAcquireTime(frameStartTime + 301),
737 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700738 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700739 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700740 mRefreshes {
741 { mFenceMap, frameStartTime + 410 },
742 { mFenceMap, frameStartTime + 420 },
743 { mFenceMap, frameStartTime + 430 } } {}
744
745 void signalQueueFences() {
746 mFenceMap.signalAllForTest(
747 mAcquireConsumer.mFence, kConsumerAcquireTime);
748 mFenceMap.signalAllForTest(
749 mAcquireProducer.mFence, kProducerAcquireTime);
750 }
751
752 void signalRefreshFences() {
753 for (auto& re : mRefreshes) {
754 re.signalPostCompositeFences();
755 }
756 }
757
758 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700759 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
760 }
761
762 FenceToFenceTimeMap& mFenceMap;
763
764 FenceAndFenceTime mAcquireConsumer { mFenceMap };
765 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700766 FenceAndFenceTime mRelease { mFenceMap };
767
768 const nsecs_t kPostedTime;
769 const nsecs_t kRequestedPresentTime;
770 const nsecs_t kProducerAcquireTime;
771 const nsecs_t kConsumerAcquireTime;
772 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700773 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700774 const nsecs_t kReleaseTime;
775
776 RefreshEvents mRefreshes[3];
777 };
778
Brian Andersond0010582017-03-07 13:20:31 -0800779 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700780
781 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700782 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
783 mFakeConsumer = new FakeConsumer;
784 mCfeh = &mFakeConsumer->mFrameEventHistory;
785 mConsumer->consumerConnect(mFakeConsumer, false);
786 mConsumer->setConsumerName(String8("TestConsumer"));
787 mSurface = new TestSurface(mProducer, &mFenceMap);
788 mWindow = mSurface;
789
790 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
791 NATIVE_WINDOW_API_CPU));
792 native_window_set_buffer_count(mWindow.get(), 4);
793 }
794
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800795 void disableFrameTimestamps() {
796 mFakeConsumer->mGetFrameTimestampsEnabled = false;
797 native_window_enable_frame_timestamps(mWindow.get(), 0);
798 mFrameTimestampsEnabled = false;
799 }
800
Brian Anderson3da8d272016-07-28 16:20:47 -0700801 void enableFrameTimestamps() {
802 mFakeConsumer->mGetFrameTimestampsEnabled = true;
803 native_window_enable_frame_timestamps(mWindow.get(), 1);
804 mFrameTimestampsEnabled = true;
805 }
806
Brian Anderson1049d1d2016-12-16 17:25:57 -0800807 int getAllFrameTimestamps(uint64_t frameId) {
808 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700809 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
810 &outFirstRefreshStartTime, &outLastRefreshStartTime,
811 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700812 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700813 }
814
Brian Anderson3da8d272016-07-28 16:20:47 -0700815 void resetTimestamps() {
816 outRequestedPresentTime = -1;
817 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700818 outLatchTime = -1;
819 outFirstRefreshStartTime = -1;
820 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700821 outGpuCompositionDoneTime = -1;
822 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700823 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700824 outReleaseTime = -1;
825 }
826
Brian Anderson1049d1d2016-12-16 17:25:57 -0800827 uint64_t getNextFrameId() {
828 uint64_t frameId = -1;
829 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
830 EXPECT_EQ(status, NO_ERROR);
831 return frameId;
832 }
833
Brian Anderson3da8d272016-07-28 16:20:47 -0700834 void dequeueAndQueue(uint64_t frameIndex) {
835 int fence = -1;
836 ANativeWindowBuffer* buffer = nullptr;
837 ASSERT_EQ(NO_ERROR,
838 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
839
840 int oldAddFrameTimestampsCount =
841 mFakeConsumer->mAddFrameTimestampsCount;
842
843 FrameEvents* frame = &mFrames[frameIndex];
844 uint64_t frameNumber = frameIndex + 1;
845
846 NewFrameEventsEntry fe;
847 fe.frameNumber = frameNumber;
848 fe.postedTime = frame->kPostedTime;
849 fe.requestedPresentTime = frame->kRequestedPresentTime;
850 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
851 mFakeConsumer->mNewFrameEntryOverride = fe;
852
853 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
854 frame->mAcquireProducer.mFenceTime,
855 frame->mAcquireConsumer.mFenceTime);
856
857 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
858
859 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
860
861 EXPECT_EQ(
862 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
863 mFakeConsumer->mAddFrameTimestampsCount);
864 }
865
866 void addFrameEvents(
867 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
868 FrameEvents* oldFrame =
869 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
870 FrameEvents* newFrame = &mFrames[iNewFrame];
871
872 uint64_t nOldFrame = iOldFrame + 1;
873 uint64_t nNewFrame = iNewFrame + 1;
874
Brian Anderson4e606e32017-03-16 15:34:57 -0700875 // Latch, Composite, and Release the frames in a plausible order.
876 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700877 // that's okay for the purposes of this test.
878 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
879
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700880 // Composite the previous frame one more time, which helps verify
881 // LastRefresh is updated properly.
882 if (oldFrame != nullptr) {
883 mCfeh->addPreComposition(nOldFrame,
884 oldFrame->mRefreshes[2].kStartTime);
885 gpuDoneFenceTime = gpuComposited ?
886 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
887 FenceTime::NO_FENCE;
888 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800889 oldFrame->mRefreshes[2].mPresent.mFenceTime,
890 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700891 }
892
893 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700894 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
895
896 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
897 gpuDoneFenceTime = gpuComposited ?
898 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
899 FenceTime::NO_FENCE;
900 // HWC2 releases the previous buffer after a new latch just before
901 // calling postComposition.
902 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700903 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700904 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
905 }
906 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800907 newFrame->mRefreshes[0].mPresent.mFenceTime,
908 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700909
Brian Anderson3da8d272016-07-28 16:20:47 -0700910 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
911 gpuDoneFenceTime = gpuComposited ?
912 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
913 FenceTime::NO_FENCE;
914 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800915 newFrame->mRefreshes[1].mPresent.mFenceTime,
916 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700917 }
918
Brian Anderson3da8d272016-07-28 16:20:47 -0700919 sp<IGraphicBufferProducer> mProducer;
920 sp<IGraphicBufferConsumer> mConsumer;
921 sp<FakeConsumer> mFakeConsumer;
922 ConsumerFrameEventHistory* mCfeh;
923 sp<TestSurface> mSurface;
924 sp<ANativeWindow> mWindow;
925
926 FenceToFenceTimeMap mFenceMap;
927
928 bool mFrameTimestampsEnabled = false;
929
930 int64_t outRequestedPresentTime = -1;
931 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700932 int64_t outLatchTime = -1;
933 int64_t outFirstRefreshStartTime = -1;
934 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700935 int64_t outGpuCompositionDoneTime = -1;
936 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700937 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700938 int64_t outReleaseTime = -1;
939
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800940 FrameEvents mFrames[3] {
941 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700942};
943
944
945// This test verifies that the frame timestamps are not retrieved when not
946// explicitly enabled via native_window_enable_frame_timestamps.
947// We want to check this to make sure there's no overhead for users
948// that don't need the timestamp information.
949TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
950 int fence;
951 ANativeWindowBuffer* buffer;
952
953 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
954 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
955
Brian Anderson1049d1d2016-12-16 17:25:57 -0800956 const uint64_t fId = getNextFrameId();
957
Brian Anderson3da8d272016-07-28 16:20:47 -0700958 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
959 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
960 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
961 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
962
963 // Verify the producer doesn't get frame timestamps piggybacked on queue.
964 // It is okay that frame timestamps are added in the consumer since it is
965 // still needed for SurfaceFlinger dumps.
966 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
967 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
968 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
969
970 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800971 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700972 EXPECT_EQ(INVALID_OPERATION, result);
973 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800974
975 // Verify compositor timing query fails.
976 nsecs_t compositeDeadline = 0;
977 nsecs_t compositeInterval = 0;
978 nsecs_t compositeToPresentLatency = 0;
979 result = native_window_get_compositor_timing(mWindow.get(),
980 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
981 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700982}
983
984// This test verifies that the frame timestamps are retrieved if explicitly
985// enabled via native_window_enable_frame_timestamps.
986TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800987 CompositorTiming initialCompositorTiming {
988 1000000000, // 1s deadline
989 16666667, // 16ms interval
990 50000000, // 50ms present latency
991 };
992 mCfeh->initializeCompositorTiming(initialCompositorTiming);
993
Brian Anderson3da8d272016-07-28 16:20:47 -0700994 enableFrameTimestamps();
995
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800996 // Verify the compositor timing query gets the initial compositor values
997 // after timststamps are enabled; even before the first frame is queued
998 // or dequeued.
999 nsecs_t compositeDeadline = 0;
1000 nsecs_t compositeInterval = 0;
1001 nsecs_t compositeToPresentLatency = 0;
1002 mSurface->setNow(initialCompositorTiming.deadline - 1);
1003 int result = native_window_get_compositor_timing(mWindow.get(),
1004 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1005 EXPECT_EQ(NO_ERROR, result);
1006 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1007 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1008 EXPECT_EQ(initialCompositorTiming.presentLatency,
1009 compositeToPresentLatency);
1010
Brian Anderson3da8d272016-07-28 16:20:47 -07001011 int fence;
1012 ANativeWindowBuffer* buffer;
1013
1014 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001015 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001016
Brian Anderson1049d1d2016-12-16 17:25:57 -08001017 const uint64_t fId1 = getNextFrameId();
1018
Brian Anderson3da8d272016-07-28 16:20:47 -07001019 // Verify getFrameTimestamps is piggybacked on dequeue.
1020 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1021 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001022 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001023
1024 NewFrameEventsEntry f1;
1025 f1.frameNumber = 1;
1026 f1.postedTime = mFrames[0].kPostedTime;
1027 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1028 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1029 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1030 mFrames[0].mAcquireProducer.mFenceTime,
1031 mFrames[0].mAcquireConsumer.mFenceTime);
1032 mFakeConsumer->mNewFrameEntryOverride = f1;
1033 mFrames[0].signalQueueFences();
1034
1035 // Verify getFrameTimestamps is piggybacked on queue.
1036 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1037 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1038 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001039 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001040
1041 // Verify queries for timestamps that the producer doesn't know about
1042 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001043 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001044 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001045 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001046}
1047
Brian Anderson6b376712017-04-04 10:51:39 -07001048TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1049 bool displayPresentSupported = true;
1050 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1051
1052 // Verify supported bits are forwarded.
1053 int supportsPresent = -1;
1054 mWindow.get()->query(mWindow.get(),
1055 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1056 EXPECT_EQ(displayPresentSupported, supportsPresent);
1057}
1058
1059TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1060 bool displayPresentSupported = false;
1061 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1062
1063 // Verify supported bits are forwarded.
1064 int supportsPresent = -1;
1065 mWindow.get()->query(mWindow.get(),
1066 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1067 EXPECT_EQ(displayPresentSupported, supportsPresent);
1068}
1069
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001070TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1071 nsecs_t phase = 4000;
1072 nsecs_t interval = 1000;
1073
1074 // Timestamp in previous interval.
1075 nsecs_t timestamp = 3500;
1076 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1077 timestamp, phase, interval));
1078
1079 // Timestamp in next interval.
1080 timestamp = 4500;
1081 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1082 timestamp, phase, interval));
1083
1084 // Timestamp multiple intervals before.
1085 timestamp = 2500;
1086 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1087 timestamp, phase, interval));
1088
1089 // Timestamp multiple intervals after.
1090 timestamp = 6500;
1091 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1092 timestamp, phase, interval));
1093
1094 // Timestamp on previous interval.
1095 timestamp = 3000;
1096 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1097 timestamp, phase, interval));
1098
1099 // Timestamp on next interval.
1100 timestamp = 5000;
1101 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1102 timestamp, phase, interval));
1103
1104 // Timestamp equal to phase.
1105 timestamp = 4000;
1106 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1107 timestamp, phase, interval));
1108}
1109
1110// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1111// if the number of intervals elapsed is internally stored in an int.
1112TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1113 nsecs_t phase = 0;
1114 nsecs_t interval = 4000;
1115 nsecs_t big_timestamp = 8635916564000;
1116 int32_t intervals = big_timestamp / interval;
1117
1118 EXPECT_LT(intervals, 0);
1119 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1120 big_timestamp, phase, interval));
1121 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1122 big_timestamp, big_timestamp, interval));
1123}
1124
1125// This verifies the compositor timing is updated by refresh events
1126// and piggy backed on a queue, dequeue, and enabling of timestamps..
1127TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1128 CompositorTiming initialCompositorTiming {
1129 1000000000, // 1s deadline
1130 16666667, // 16ms interval
1131 50000000, // 50ms present latency
1132 };
1133 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1134
1135 enableFrameTimestamps();
1136
1137 // We get the initial values before any frames are submitted.
1138 nsecs_t compositeDeadline = 0;
1139 nsecs_t compositeInterval = 0;
1140 nsecs_t compositeToPresentLatency = 0;
1141 mSurface->setNow(initialCompositorTiming.deadline - 1);
1142 int result = native_window_get_compositor_timing(mWindow.get(),
1143 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1144 EXPECT_EQ(NO_ERROR, result);
1145 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1146 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1147 EXPECT_EQ(initialCompositorTiming.presentLatency,
1148 compositeToPresentLatency);
1149
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001150 dequeueAndQueue(0);
1151 addFrameEvents(true, NO_FRAME_INDEX, 0);
1152
1153 // Still get the initial values because the frame events for frame 0
1154 // didn't get a chance to piggyback on a queue or dequeue yet.
1155 result = native_window_get_compositor_timing(mWindow.get(),
1156 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1157 EXPECT_EQ(NO_ERROR, result);
1158 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1159 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1160 EXPECT_EQ(initialCompositorTiming.presentLatency,
1161 compositeToPresentLatency);
1162
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001163 dequeueAndQueue(1);
1164 addFrameEvents(true, 0, 1);
1165
1166 // Now expect the composite values associated with frame 1.
1167 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1168 result = native_window_get_compositor_timing(mWindow.get(),
1169 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1170 EXPECT_EQ(NO_ERROR, result);
1171 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1172 compositeDeadline);
1173 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1174 compositeInterval);
1175 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1176 compositeToPresentLatency);
1177
1178 dequeueAndQueue(2);
1179 addFrameEvents(true, 1, 2);
1180
1181 // Now expect the composite values associated with frame 2.
1182 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1183 result = native_window_get_compositor_timing(mWindow.get(),
1184 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1185 EXPECT_EQ(NO_ERROR, result);
1186 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1187 compositeDeadline);
1188 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1189 compositeInterval);
1190 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1191 compositeToPresentLatency);
1192
1193 // Re-enabling frame timestamps should get the latest values.
1194 disableFrameTimestamps();
1195 enableFrameTimestamps();
1196
1197 // Now expect the composite values associated with frame 3.
1198 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1199 result = native_window_get_compositor_timing(mWindow.get(),
1200 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1201 EXPECT_EQ(NO_ERROR, result);
1202 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1203 compositeDeadline);
1204 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1205 compositeInterval);
1206 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1207 compositeToPresentLatency);
1208}
1209
1210// This verifies the compositor deadline properly snaps to the the next
1211// deadline based on the current time.
1212TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1213 CompositorTiming initialCompositorTiming {
1214 1000000000, // 1s deadline
1215 16666667, // 16ms interval
1216 50000000, // 50ms present latency
1217 };
1218 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1219
1220 enableFrameTimestamps();
1221
1222 nsecs_t compositeDeadline = 0;
1223 nsecs_t compositeInterval = 0;
1224 nsecs_t compositeToPresentLatency = 0;
1225
1226 // A "now" just before the deadline snaps to the deadline.
1227 mSurface->setNow(initialCompositorTiming.deadline - 1);
1228 int result = native_window_get_compositor_timing(mWindow.get(),
1229 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1230 EXPECT_EQ(NO_ERROR, result);
1231 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1232 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1233 EXPECT_EQ(expectedDeadline, compositeDeadline);
1234
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001235 dequeueAndQueue(0);
1236 addFrameEvents(true, NO_FRAME_INDEX, 0);
1237
1238 // A "now" just after the deadline snaps properly.
1239 mSurface->setNow(initialCompositorTiming.deadline + 1);
1240 result = native_window_get_compositor_timing(mWindow.get(),
1241 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1242 EXPECT_EQ(NO_ERROR, result);
1243 expectedDeadline =
1244 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1245 EXPECT_EQ(expectedDeadline, compositeDeadline);
1246
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001247 dequeueAndQueue(1);
1248 addFrameEvents(true, 0, 1);
1249
1250 // A "now" just after the next interval snaps properly.
1251 mSurface->setNow(
1252 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1253 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1254 result = native_window_get_compositor_timing(mWindow.get(),
1255 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1256 EXPECT_EQ(NO_ERROR, result);
1257 expectedDeadline =
1258 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1259 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1260 EXPECT_EQ(expectedDeadline, compositeDeadline);
1261
1262 dequeueAndQueue(2);
1263 addFrameEvents(true, 1, 2);
1264
1265 // A "now" over 1 interval before the deadline snaps properly.
1266 mSurface->setNow(
1267 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1268 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1269 result = native_window_get_compositor_timing(mWindow.get(),
1270 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1271 EXPECT_EQ(NO_ERROR, result);
1272 expectedDeadline =
1273 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1274 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1275 EXPECT_EQ(expectedDeadline, compositeDeadline);
1276
1277 // Re-enabling frame timestamps should get the latest values.
1278 disableFrameTimestamps();
1279 enableFrameTimestamps();
1280
1281 // A "now" over 2 intervals before the deadline snaps properly.
1282 mSurface->setNow(
1283 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1284 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1285 result = native_window_get_compositor_timing(mWindow.get(),
1286 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1287 EXPECT_EQ(NO_ERROR, result);
1288 expectedDeadline =
1289 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1290 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1291 EXPECT_EQ(expectedDeadline, compositeDeadline);
1292}
1293
Brian Anderson1049d1d2016-12-16 17:25:57 -08001294// This verifies the timestamps recorded in the consumer's
1295// FrameTimestampsHistory are properly retrieved by the producer for the
1296// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001297TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1298 enableFrameTimestamps();
1299
Brian Anderson1049d1d2016-12-16 17:25:57 -08001300 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001301 dequeueAndQueue(0);
1302 mFrames[0].signalQueueFences();
1303
Brian Anderson1049d1d2016-12-16 17:25:57 -08001304 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001305 dequeueAndQueue(1);
1306 mFrames[1].signalQueueFences();
1307
1308 addFrameEvents(true, NO_FRAME_INDEX, 0);
1309 mFrames[0].signalRefreshFences();
1310 addFrameEvents(true, 0, 1);
1311 mFrames[0].signalReleaseFences();
1312 mFrames[1].signalRefreshFences();
1313
1314 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001315 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001316 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001317 EXPECT_EQ(NO_ERROR, result);
1318 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1319 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001320 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1321 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1322 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001323 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1324 outGpuCompositionDoneTime);
1325 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001326 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001327 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1328
1329 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001330 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001331 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001332 EXPECT_EQ(NO_ERROR, result);
1333 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1334 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001335 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1336 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1337 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001338 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1339 outGpuCompositionDoneTime);
1340 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001341 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1342 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001343}
1344
1345// This test verifies the acquire fence recorded by the consumer is not sent
1346// back to the producer and the producer saves its own fence.
1347TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1348 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001349
Brian Anderson3da8d272016-07-28 16:20:47 -07001350 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001351 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001352 dequeueAndQueue(0);
1353
1354 // Verify queue-related timestamps for f1 are available immediately in the
1355 // producer without asking the consumer again, even before signaling the
1356 // acquire fence.
1357 resetTimestamps();
1358 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001359 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001360 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001361 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001362 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1363 EXPECT_EQ(NO_ERROR, result);
1364 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001365 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001366
1367 // Signal acquire fences. Verify a sync call still isn't necessary.
1368 mFrames[0].signalQueueFences();
1369
1370 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001371 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001372 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001373 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001374 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1375 EXPECT_EQ(NO_ERROR, result);
1376 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1377 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1378
1379 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001380 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001381 dequeueAndQueue(1);
1382
1383 // Verify queue-related timestamps for f2 are available immediately in the
1384 // producer without asking the consumer again, even before signaling the
1385 // acquire fence.
1386 resetTimestamps();
1387 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001388 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001389 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001390 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001391 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1392 EXPECT_EQ(NO_ERROR, result);
1393 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001394 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001395
1396 // Signal acquire fences. Verify a sync call still isn't necessary.
1397 mFrames[1].signalQueueFences();
1398
1399 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001400 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001401 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001402 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001403 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1404 EXPECT_EQ(NO_ERROR, result);
1405 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1406 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1407}
1408
1409TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1410 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001411
1412 // Dequeue and queue frame 1.
1413 dequeueAndQueue(0);
1414 mFrames[0].signalQueueFences();
1415
1416 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001417 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001418 dequeueAndQueue(1);
1419 mFrames[1].signalQueueFences();
1420
1421 addFrameEvents(true, NO_FRAME_INDEX, 0);
1422 mFrames[0].signalRefreshFences();
1423 addFrameEvents(true, 0, 1);
1424 mFrames[0].signalReleaseFences();
1425 mFrames[1].signalRefreshFences();
1426
1427 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001428 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001429 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001430 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1431 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001432 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001433 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1434}
1435
1436// This test verifies that fences can signal and update timestamps producer
1437// side without an additional sync call to the consumer.
1438TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1439 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001440
1441 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001442 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001443 dequeueAndQueue(0);
1444 mFrames[0].signalQueueFences();
1445
1446 // Dequeue and queue frame 2.
1447 dequeueAndQueue(1);
1448 mFrames[1].signalQueueFences();
1449
1450 addFrameEvents(true, NO_FRAME_INDEX, 0);
1451 addFrameEvents(true, 0, 1);
1452
1453 // Verify available timestamps are correct for frame 1, before any
1454 // fence has been signaled.
1455 // Note: A sync call is necessary here since the events triggered by
1456 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001457 resetTimestamps();
1458 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001459 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001460 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1461 EXPECT_EQ(NO_ERROR, result);
1462 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1463 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001464 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1465 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1466 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001467 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1468 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001469 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001470 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001471
1472 // Verify available timestamps are correct for frame 1 again, before any
1473 // fence has been signaled.
1474 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001475 resetTimestamps();
1476 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001477 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001478 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1479 EXPECT_EQ(NO_ERROR, result);
1480 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1481 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001482 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1483 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1484 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001485 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1486 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001487 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001488 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001489
1490 // Signal the fences for frame 1.
1491 mFrames[0].signalRefreshFences();
1492 mFrames[0].signalReleaseFences();
1493
1494 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001495 resetTimestamps();
1496 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001497 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001498 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1499 EXPECT_EQ(NO_ERROR, result);
1500 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1501 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001502 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1503 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1504 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001505 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1506 outGpuCompositionDoneTime);
1507 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001508 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001509 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1510}
1511
1512// This test verifies that if the frame wasn't GPU composited but has a refresh
1513// event a sync call isn't made to get the GPU composite done time since it will
1514// never exist.
1515TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1516 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001517
Brian Anderson3da8d272016-07-28 16:20:47 -07001518 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001519 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001520 dequeueAndQueue(0);
1521 mFrames[0].signalQueueFences();
1522
1523 // Dequeue and queue frame 2.
1524 dequeueAndQueue(1);
1525 mFrames[1].signalQueueFences();
1526
1527 addFrameEvents(false, NO_FRAME_INDEX, 0);
1528 addFrameEvents(false, 0, 1);
1529
1530 // Verify available timestamps are correct for frame 1, before any
1531 // fence has been signaled.
1532 // Note: A sync call is necessary here since the events triggered by
1533 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1534 resetTimestamps();
1535 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001536 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001537 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1538 EXPECT_EQ(NO_ERROR, result);
1539 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1540 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001541 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1542 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1543 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001544 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1545 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001546 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001547 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001548
1549 // Signal the fences for frame 1.
1550 mFrames[0].signalRefreshFences();
1551 mFrames[0].signalReleaseFences();
1552
1553 // Verify all timestamps, except GPU composition, are available without a
1554 // sync call.
1555 resetTimestamps();
1556 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001557 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001558 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1559 EXPECT_EQ(NO_ERROR, result);
1560 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1561 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001562 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1563 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1564 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001565 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001566 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001567 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001568 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1569}
1570
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001571// This test verifies that if the certain timestamps can't possibly exist for
1572// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001573TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001574 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001575
1576 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001577 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001578 dequeueAndQueue(0);
1579 mFrames[0].signalQueueFences();
1580
1581 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001582 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001583 dequeueAndQueue(1);
1584 mFrames[1].signalQueueFences();
1585
1586 addFrameEvents(false, NO_FRAME_INDEX, 0);
1587 addFrameEvents(false, 0, 1);
1588
1589 // Verify available timestamps are correct for frame 1, before any
1590 // fence has been signaled.
1591 // Note: A sync call is necessary here since the events triggered by
1592 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001593 resetTimestamps();
1594 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001595 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001596 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1597 EXPECT_EQ(NO_ERROR, result);
1598 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1599 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001600 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1601 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1602 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001603 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1604 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001605 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001606 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001607
1608 mFrames[0].signalRefreshFences();
1609 mFrames[0].signalReleaseFences();
1610 mFrames[1].signalRefreshFences();
1611
Brian Anderson1049d1d2016-12-16 17:25:57 -08001612 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001613 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001614 // available, a sync call should not occur because it's not possible for f2
1615 // to encounter the final value for those events until another frame is
1616 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001617 resetTimestamps();
1618 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001619 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001620 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1621 EXPECT_EQ(NO_ERROR, result);
1622 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1623 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001624 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1625 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1626 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001627 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001628 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001629 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1630 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001631}
1632
Brian Anderson6b376712017-04-04 10:51:39 -07001633// This test verifies there are no sync calls for present times
1634// when they aren't supported and that an error is returned.
1635
1636TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1637 enableFrameTimestamps();
1638 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1639
1640 // Dequeue and queue frame 1.
1641 const uint64_t fId1 = getNextFrameId();
1642 dequeueAndQueue(0);
1643
1644 // Verify a query for the Present times do not trigger a sync call if they
1645 // are not supported.
1646 resetTimestamps();
1647 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1648 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1649 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1650 &outDisplayPresentTime, nullptr, nullptr);
1651 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1652 EXPECT_EQ(BAD_VALUE, result);
1653 EXPECT_EQ(-1, outDisplayPresentTime);
1654}
1655
Dan Stoza932f0082017-05-31 13:50:16 -07001656} // namespace android