blob: 959aafc503a03cc9d99be4fbec050f957152a9fe [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;
Peiyong Linfd997e02018-03-28 15:29:00 -070045using ui::ColorMode;
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060046
Robert Carr4cdc58f2017-08-23 14:22:20 -070047using Transaction = SurfaceComposerClient::Transaction;
48
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060049static bool hasWideColorDisplay =
50 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
Kalle Raita643f0942016-12-07 09:20:14 -080051
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -070052static bool hasHdrDisplay =
53 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
54
Brian Anderson3da8d272016-07-28 16:20:47 -070055class FakeSurfaceComposer;
56class FakeProducerFrameEventHistory;
57
58static constexpr uint64_t NO_FRAME_INDEX = std::numeric_limits<uint64_t>::max();
59
Jamie Gennis134f0422011-03-08 12:18:54 -080060class SurfaceTest : public ::testing::Test {
61protected:
Mathias Agopian7c1a4872013-03-20 15:56:04 -070062 SurfaceTest() {
63 ProcessState::self()->startThreadPool();
64 }
65
Jamie Gennis134f0422011-03-08 12:18:54 -080066 virtual void SetUp() {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080067 mComposerClient = new SurfaceComposerClient;
Jamie Gennis134f0422011-03-08 12:18:54 -080068 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
69
Brian Andersond0010582017-03-07 13:20:31 -080070 // TODO(brianderson): The following sometimes fails and is a source of
71 // test flakiness.
Jamie Gennisfc850122011-04-25 16:40:05 -070072 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070073 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis134f0422011-03-08 12:18:54 -080074
75 ASSERT_TRUE(mSurfaceControl != NULL);
76 ASSERT_TRUE(mSurfaceControl->isValid());
77
Robert Carr4cdc58f2017-08-23 14:22:20 -070078 Transaction t;
79 ASSERT_EQ(NO_ERROR, t.setLayer(mSurfaceControl, 0x7fffffff)
80 .show(mSurfaceControl)
81 .apply());
Jamie Gennis134f0422011-03-08 12:18:54 -080082
83 mSurface = mSurfaceControl->getSurface();
84 ASSERT_TRUE(mSurface != NULL);
85 }
86
87 virtual void TearDown() {
88 mComposerClient->dispose();
89 }
90
91 sp<Surface> mSurface;
92 sp<SurfaceComposerClient> mComposerClient;
93 sp<SurfaceControl> mSurfaceControl;
94};
95
Robert Carr740eaf02018-03-27 12:59:18 -070096TEST_F(SurfaceTest, CreateSurfaceReturnsErrorBadClient) {
97 mComposerClient->dispose();
98 ASSERT_EQ(NO_INIT, mComposerClient->initCheck());
99
100 sp<SurfaceControl> sc;
101 status_t err = mComposerClient->createSurfaceChecked(
102 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, &sc, 0);
103 ASSERT_EQ(NO_INIT, err);
104}
105
Jamie Gennis134f0422011-03-08 12:18:54 -0800106TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
107 sp<ANativeWindow> anw(mSurface);
108 int result = -123;
109 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
110 &result);
111 EXPECT_EQ(NO_ERROR, err);
112 EXPECT_EQ(1, result);
113}
114
115TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
116 mSurfaceControl.clear();
Kalle Raita643f0942016-12-07 09:20:14 -0800117 // Wait for the async clean-up to complete.
118 std::this_thread::sleep_for(50ms);
Jamie Gennis134f0422011-03-08 12:18:54 -0800119
120 sp<ANativeWindow> anw(mSurface);
121 int result = -123;
122 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
123 &result);
124 EXPECT_EQ(NO_ERROR, err);
125 EXPECT_EQ(1, result);
126}
127
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800128// This test probably doesn't belong here.
Jamie Gennisc901ca02011-10-11 16:02:31 -0700129TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800130 sp<ANativeWindow> anw(mSurface);
131
132 // Verify the screenshot works with no protected buffers.
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800133 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Brian Anderson3da8d272016-07-28 16:20:47 -0700134 sp<IBinder> display(sf->getBuiltInDisplay(
135 ISurfaceComposer::eDisplayIdMain));
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000136 sp<GraphicBuffer> outBuffer;
137 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &outBuffer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800138 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800139
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700140 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
141 NATIVE_WINDOW_API_CPU));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800142 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
143 // that we need to dequeue a buffer in order for it to actually get
144 // allocated in SurfaceFlinger.
145 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
146 GRALLOC_USAGE_PROTECTED));
147 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Iliyan Malchev697526b2011-05-01 11:33:26 -0700148 ANativeWindowBuffer* buf = 0;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700149
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700150 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700151 if (err) {
152 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
153 // that's okay as long as this is the reason for the failure.
154 // try again without the GRALLOC_USAGE_PROTECTED bit.
155 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700156 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
157 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700158 return;
159 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700160 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700161
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800162 for (int i = 0; i < 4; i++) {
163 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700164 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
165 &buf));
166 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800167 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000168 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &outBuffer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800169 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800170}
171
Jamie Gennis391bbe22011-03-14 15:00:06 -0700172TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
173 sp<ANativeWindow> anw(mSurface);
174 int result = -123;
175 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
176 EXPECT_EQ(NO_ERROR, err);
177 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
178}
179
Craig Donner6ebc46a2016-10-21 15:23:44 -0700180TEST_F(SurfaceTest, LayerCountIsOne) {
181 sp<ANativeWindow> anw(mSurface);
182 int result = -123;
183 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
184 EXPECT_EQ(NO_ERROR, err);
185 EXPECT_EQ(1, result);
186}
187
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700188TEST_F(SurfaceTest, QueryConsumerUsage) {
189 const int TEST_USAGE_FLAGS =
190 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700191 sp<IGraphicBufferProducer> producer;
192 sp<IGraphicBufferConsumer> consumer;
193 BufferQueue::createBufferQueue(&producer, &consumer);
194 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700195 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700196 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700197
198 sp<ANativeWindow> anw(s);
199
200 int flags = -1;
201 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
202
203 ASSERT_EQ(NO_ERROR, err);
204 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
205}
206
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800207TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
208 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
209 sp<IGraphicBufferProducer> producer;
210 sp<IGraphicBufferConsumer> consumer;
211 BufferQueue::createBufferQueue(&producer, &consumer);
212 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
213
214 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
215
216 sp<Surface> s = new Surface(producer);
217
218 sp<ANativeWindow> anw(s);
219
220 android_dataspace dataSpace;
221
222 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
223 reinterpret_cast<int*>(&dataSpace));
224
225 ASSERT_EQ(NO_ERROR, err);
226 ASSERT_EQ(TEST_DATASPACE, dataSpace);
227}
228
Dan Stoza812ed062015-06-02 15:45:22 -0700229TEST_F(SurfaceTest, SettingGenerationNumber) {
230 sp<IGraphicBufferProducer> producer;
231 sp<IGraphicBufferConsumer> consumer;
232 BufferQueue::createBufferQueue(&producer, &consumer);
233 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
234 sp<Surface> surface = new Surface(producer);
235 sp<ANativeWindow> window(surface);
236
237 // Allocate a buffer with a generation number of 0
238 ANativeWindowBuffer* buffer;
239 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700240 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
241 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700242 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
243 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
244
245 // Detach the buffer and check its generation number
246 sp<GraphicBuffer> graphicBuffer;
247 sp<Fence> fence;
248 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
249 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
250
251 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
252 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
253
254 // This should change the generation number of the GraphicBuffer
255 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
256
257 // Check that the new generation number sticks with the buffer
258 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
259 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
260 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
261 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
262}
263
Dan Stozac6f30bd2015-06-08 09:32:50 -0700264TEST_F(SurfaceTest, GetConsumerName) {
265 sp<IGraphicBufferProducer> producer;
266 sp<IGraphicBufferConsumer> consumer;
267 BufferQueue::createBufferQueue(&producer, &consumer);
268
269 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
270 consumer->consumerConnect(dummyConsumer, false);
271 consumer->setConsumerName(String8("TestConsumer"));
272
273 sp<Surface> surface = new Surface(producer);
274 sp<ANativeWindow> window(surface);
275 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
276
277 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
278}
279
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700280TEST_F(SurfaceTest, GetWideColorSupport) {
281 sp<IGraphicBufferProducer> producer;
282 sp<IGraphicBufferConsumer> consumer;
283 BufferQueue::createBufferQueue(&producer, &consumer);
284
285 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
286 consumer->consumerConnect(dummyConsumer, false);
287 consumer->setConsumerName(String8("TestConsumer"));
288
289 sp<Surface> surface = new Surface(producer);
290 sp<ANativeWindow> window(surface);
291 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
292
293 bool supported;
294 surface->getWideColorSupport(&supported);
295
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -0600296 // NOTE: This test assumes that device that supports
297 // wide-color (as indicated by BoardConfig) must also
298 // have a wide-color primary display.
299 // That assumption allows this test to cover devices
300 // that advertised a wide-color color mode without
301 // actually supporting wide-color to pass this test
302 // as well as the case of a device that does support
303 // wide-color (via BoardConfig) and has a wide-color
304 // primary display.
305 // NOT covered at this time is a device that supports
306 // wide color in the BoardConfig but does not support
307 // a wide-color color mode on the primary display.
308 ASSERT_EQ(hasWideColorDisplay, supported);
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700309}
310
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700311TEST_F(SurfaceTest, GetHdrSupport) {
312 sp<IGraphicBufferProducer> producer;
313 sp<IGraphicBufferConsumer> consumer;
314 BufferQueue::createBufferQueue(&producer, &consumer);
315
316 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
317 consumer->consumerConnect(dummyConsumer, false);
318 consumer->setConsumerName(String8("TestConsumer"));
319
320 sp<Surface> surface = new Surface(producer);
321 sp<ANativeWindow> window(surface);
322 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
323
324 bool supported;
325 status_t result = surface->getHdrSupport(&supported);
326 ASSERT_EQ(NO_ERROR, result);
327
328 // NOTE: This is not a CTS test.
329 // This test verifies that when the BoardConfig TARGET_HAS_HDR_DISPLAY
330 // is TRUE, getHdrSupport is also true.
331 // TODO: Add check for an HDR color mode on the primary display.
332 ASSERT_EQ(hasHdrDisplay, supported);
333}
334
335TEST_F(SurfaceTest, SetHdrMetadata) {
336 sp<IGraphicBufferProducer> producer;
337 sp<IGraphicBufferConsumer> consumer;
338 BufferQueue::createBufferQueue(&producer, &consumer);
339
340 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
341 consumer->consumerConnect(dummyConsumer, false);
342 consumer->setConsumerName(String8("TestConsumer"));
343
344 sp<Surface> surface = new Surface(producer);
345 sp<ANativeWindow> window(surface);
346 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
347
348 bool supported;
349 status_t result = surface->getHdrSupport(&supported);
350 ASSERT_EQ(NO_ERROR, result);
351
352 if (!hasHdrDisplay || !supported) {
353 return;
354 }
355 const android_smpte2086_metadata smpte2086 = {
356 {0.680, 0.320},
357 {0.265, 0.690},
358 {0.150, 0.060},
359 {0.3127, 0.3290},
360 100.0,
361 0.1,
362 };
363 const android_cta861_3_metadata cta861_3 = {
364 78.0,
365 62.0,
366 };
367 int error = native_window_set_buffers_smpte2086_metadata(window.get(), &smpte2086);
368 ASSERT_EQ(error, NO_ERROR);
369 error = native_window_set_buffers_cta861_3_metadata(window.get(), &cta861_3);
370 ASSERT_EQ(error, NO_ERROR);
371}
372
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800373TEST_F(SurfaceTest, DynamicSetBufferCount) {
374 sp<IGraphicBufferProducer> producer;
375 sp<IGraphicBufferConsumer> consumer;
376 BufferQueue::createBufferQueue(&producer, &consumer);
377
378 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
379 consumer->consumerConnect(dummyConsumer, false);
380 consumer->setConsumerName(String8("TestConsumer"));
381
382 sp<Surface> surface = new Surface(producer);
383 sp<ANativeWindow> window(surface);
384
385 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
386 NATIVE_WINDOW_API_CPU));
387 native_window_set_buffer_count(window.get(), 4);
388
389 int fence;
390 ANativeWindowBuffer* buffer;
391 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
392 native_window_set_buffer_count(window.get(), 3);
393 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
394 native_window_set_buffer_count(window.get(), 2);
395 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
396 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
397}
398
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700399TEST_F(SurfaceTest, GetAndFlushRemovedBuffers) {
400 sp<IGraphicBufferProducer> producer;
401 sp<IGraphicBufferConsumer> consumer;
402 BufferQueue::createBufferQueue(&producer, &consumer);
403
404 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
405 consumer->consumerConnect(dummyConsumer, false);
406 consumer->setConsumerName(String8("TestConsumer"));
407
408 sp<Surface> surface = new Surface(producer);
409 sp<ANativeWindow> window(surface);
410 sp<DummyProducerListener> listener = new DummyProducerListener();
411 ASSERT_EQ(OK, surface->connect(
412 NATIVE_WINDOW_API_CPU,
413 /*listener*/listener,
414 /*reportBufferRemoval*/true));
415 const int BUFFER_COUNT = 4;
416 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
417
418 sp<GraphicBuffer> detachedBuffer;
419 sp<Fence> outFence;
420 int fences[BUFFER_COUNT];
421 ANativeWindowBuffer* buffers[BUFFER_COUNT];
422 // Allocate buffers because detachNextBuffer requires allocated buffers
423 for (int i = 0; i < BUFFER_COUNT; i++) {
424 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
425 }
426 for (int i = 0; i < BUFFER_COUNT; i++) {
427 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
428 }
429
430 // Test detached buffer is correctly reported
431 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
432 std::vector<sp<GraphicBuffer>> removedBuffers;
433 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
434 ASSERT_EQ(1u, removedBuffers.size());
435 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
436 // Test the list is flushed one getAndFlushRemovedBuffers returns
437 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
438 ASSERT_EQ(0u, removedBuffers.size());
439
440
441 // Test removed buffer list is cleanup after next dequeueBuffer call
442 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
443 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[0], &fences[0]));
444 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
445 ASSERT_EQ(0u, removedBuffers.size());
446 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[0], fences[0]));
447
448 // Test removed buffer list is cleanup after next detachNextBuffer call
449 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
450 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
451 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
452 ASSERT_EQ(1u, removedBuffers.size());
453 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
454
455 // Re-allocate buffers since all buffers are detached up to now
456 for (int i = 0; i < BUFFER_COUNT; i++) {
457 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
458 }
459 for (int i = 0; i < BUFFER_COUNT; i++) {
460 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
461 }
462
463 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
464 ASSERT_EQ(NO_ERROR, surface->attachBuffer(detachedBuffer.get()));
465 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
466 // Depends on which slot GraphicBufferProducer impl pick, the attach call might
467 // get 0 or 1 buffer removed.
468 ASSERT_LE(removedBuffers.size(), 1u);
469}
Brian Anderson3da8d272016-07-28 16:20:47 -0700470
Dan Stoza932f0082017-05-31 13:50:16 -0700471TEST_F(SurfaceTest, TestGetLastDequeueStartTime) {
472 sp<ANativeWindow> anw(mSurface);
473 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU));
474
475 ANativeWindowBuffer* buffer = nullptr;
476 int32_t fenceFd = -1;
477
478 nsecs_t before = systemTime(CLOCK_MONOTONIC);
479 anw->dequeueBuffer(anw.get(), &buffer, &fenceFd);
480 nsecs_t after = systemTime(CLOCK_MONOTONIC);
481
482 nsecs_t lastDequeueTime = mSurface->getLastDequeueStartTime();
483 ASSERT_LE(before, lastDequeueTime);
484 ASSERT_GE(after, lastDequeueTime);
485}
486
Brian Anderson3da8d272016-07-28 16:20:47 -0700487class FakeConsumer : public BnConsumerListener {
488public:
489 void onFrameAvailable(const BufferItem& /*item*/) override {}
490 void onBuffersReleased() override {}
491 void onSidebandStreamChanged() override {}
492
493 void addAndGetFrameTimestamps(
494 const NewFrameEventsEntry* newTimestamps,
495 FrameEventHistoryDelta* outDelta) override {
496 if (newTimestamps) {
497 if (mGetFrameTimestampsEnabled) {
498 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
499 "Test should set mNewFrameEntryOverride before queuing "
500 "a frame.";
501 EXPECT_EQ(newTimestamps->frameNumber,
502 mNewFrameEntryOverride.frameNumber) <<
503 "Test attempting to add NewFrameEntryOverride with "
504 "incorrect frame number.";
505 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
506 mNewFrameEntryOverride.frameNumber = 0;
507 }
508 mAddFrameTimestampsCount++;
509 mLastAddedFrameNumber = newTimestamps->frameNumber;
510 }
511 if (outDelta) {
512 mFrameEventHistory.getAndResetDelta(outDelta);
513 mGetFrameTimestampsCount++;
514 }
515 mAddAndGetFrameTimestampsCallCount++;
516 }
517
518 bool mGetFrameTimestampsEnabled = false;
519
520 ConsumerFrameEventHistory mFrameEventHistory;
521 int mAddAndGetFrameTimestampsCallCount = 0;
522 int mAddFrameTimestampsCount = 0;
523 int mGetFrameTimestampsCount = 0;
524 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
525
526 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
527};
528
529
530class FakeSurfaceComposer : public ISurfaceComposer{
531public:
532 ~FakeSurfaceComposer() override {}
533
Brian Anderson6b376712017-04-04 10:51:39 -0700534 void setSupportsPresent(bool supportsPresent) {
535 mSupportsPresent = supportsPresent;
536 }
537
Brian Anderson3da8d272016-07-28 16:20:47 -0700538 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800539 sp<ISurfaceComposerClient> createScopedConnection(
540 const sp<IGraphicBufferProducer>& /* parent */) override {
541 return nullptr;
542 }
Jorim Jaggib1e2f8d2017-06-08 15:43:59 -0700543 sp<IDisplayEventConnection> createDisplayEventConnection(ISurfaceComposer::VsyncSource)
544 override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700545 return nullptr;
546 }
547 sp<IBinder> createDisplay(const String8& /*displayName*/,
548 bool /*secure*/) override { return nullptr; }
549 void destroyDisplay(const sp<IBinder>& /*display */) override {}
550 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
551 void setTransactionState(const Vector<ComposerState>& /*state*/,
552 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
553 override {}
554 void bootFinished() override {}
555 bool authenticateSurfaceTexture(
556 const sp<IGraphicBufferProducer>& /*surface*/) const override {
557 return false;
558 }
Brian Anderson6b376712017-04-04 10:51:39 -0700559
560 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
561 const override {
562 *outSupported = {
563 FrameEvent::REQUESTED_PRESENT,
564 FrameEvent::ACQUIRE,
565 FrameEvent::LATCH,
566 FrameEvent::FIRST_REFRESH_START,
567 FrameEvent::LAST_REFRESH_START,
568 FrameEvent::GPU_COMPOSITION_DONE,
569 FrameEvent::DEQUEUE_READY,
570 FrameEvent::RELEASE
571 };
572 if (mSupportsPresent) {
573 outSupported->push_back(
574 FrameEvent::DISPLAY_PRESENT);
575 }
576 return NO_ERROR;
577 }
578
Brian Anderson3da8d272016-07-28 16:20:47 -0700579 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
580 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
581 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
582 status_t getDisplayStats(const sp<IBinder>& /*display*/,
583 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
Yiwei Zhang1d465af2018-08-21 15:15:42 -0700584 status_t getDisplayViewport(const sp<IBinder>& /*display*/, Rect* /*outViewport*/) override {
585 return NO_ERROR;
586 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700587 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
588 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
589 override {
590 return NO_ERROR;
591 }
592 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700593 Vector<ColorMode>* /*outColorModes*/) override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700594 return NO_ERROR;
595 }
Peiyong Lina52f0292018-03-14 17:26:31 -0700596 ColorMode getActiveColorMode(const sp<IBinder>& /*display*/)
Brian Anderson3da8d272016-07-28 16:20:47 -0700597 override {
Peiyong Lina52f0292018-03-14 17:26:31 -0700598 return ColorMode::NATIVE;
Brian Anderson3da8d272016-07-28 16:20:47 -0700599 }
600 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700601 ColorMode /*colorMode*/) override { return NO_ERROR; }
Brian Anderson3da8d272016-07-28 16:20:47 -0700602 status_t captureScreen(const sp<IBinder>& /*display*/,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000603 sp<GraphicBuffer>* /*outBuffer*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700604 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800605 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700606 bool /*useIdentityTransform*/,
607 Rotation /*rotation*/) override { return NO_ERROR; }
chaviwa76b2712017-09-20 12:02:26 -0700608 virtual status_t captureLayers(const sp<IBinder>& /*parentHandle*/,
Robert Carr578038f2018-03-09 12:25:24 -0800609 sp<GraphicBuffer>* /*outBuffer*/, const Rect& /*sourceCrop*/,
610 float /*frameScale*/, bool /*childrenOnly*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700611 return NO_ERROR;
612 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700613 status_t clearAnimationFrameStats() override { return NO_ERROR; }
614 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
615 return NO_ERROR;
616 }
617 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
618 HdrCapabilities* /*outCapabilities*/) const override {
619 return NO_ERROR;
620 }
621 status_t enableVSyncInjections(bool /*enable*/) override {
622 return NO_ERROR;
623 }
624 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800625 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
626 return NO_ERROR;
627 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700628
629protected:
630 IBinder* onAsBinder() override { return nullptr; }
631
632private:
633 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700634};
635
636class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
637public:
Chih-Hung Hsiehaaf62162018-12-20 15:45:04 -0800638 explicit FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap) : mFenceMap(fenceMap) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700639
640 ~FakeProducerFrameEventHistory() {}
641
642 void updateAcquireFence(uint64_t frameNumber,
643 std::shared_ptr<FenceTime>&& acquire) override {
644 // Verify the acquire fence being added isn't the one from the consumer.
645 EXPECT_NE(mConsumerAcquireFence, acquire);
646 // Override the fence, so we can verify this was called by the
647 // producer after the frame is queued.
648 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
649 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
650 }
651
652 void setAcquireFenceOverride(
653 const std::shared_ptr<FenceTime>& acquireFenceOverride,
654 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
655 mAcquireFenceOverride = acquireFenceOverride;
656 mConsumerAcquireFence = consumerAcquireFence;
657 }
658
659protected:
660 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
661 const override {
662 return mFenceMap->createFenceTimeForTest(fence);
663 }
664
665 FenceToFenceTimeMap* mFenceMap{nullptr};
666
667 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
668 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
669};
670
671
672class TestSurface : public Surface {
673public:
674 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
675 FenceToFenceTimeMap* fenceMap)
676 : Surface(bufferProducer),
677 mFakeSurfaceComposer(new FakeSurfaceComposer) {
678 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
679 mFrameEventHistory.reset(mFakeFrameEventHistory);
680 }
681
682 ~TestSurface() override {}
683
684 sp<ISurfaceComposer> composerService() const override {
685 return mFakeSurfaceComposer;
686 }
687
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800688 nsecs_t now() const override {
689 return mNow;
690 }
691
692 void setNow(nsecs_t now) {
693 mNow = now;
694 }
695
Brian Anderson3da8d272016-07-28 16:20:47 -0700696public:
697 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800698 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700699
700 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
701 // but this raw pointer gives access to test functionality.
702 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
703};
704
705
Brian Andersond0010582017-03-07 13:20:31 -0800706class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700707protected:
708 struct FenceAndFenceTime {
709 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
710 : mFence(new Fence),
711 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
712 sp<Fence> mFence { nullptr };
713 std::shared_ptr<FenceTime> mFenceTime { nullptr };
714 };
715
716 struct RefreshEvents {
717 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800718 : mFenceMap(fenceMap),
719 kCompositorTiming(
720 {refreshStart, refreshStart + 1, refreshStart + 2 }),
721 kStartTime(refreshStart + 3),
722 kGpuCompositionDoneTime(refreshStart + 4),
723 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700724
725 void signalPostCompositeFences() {
726 mFenceMap.signalAllForTest(
727 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
728 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
729 }
730
731 FenceToFenceTimeMap& mFenceMap;
732
733 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
734 FenceAndFenceTime mPresent { mFenceMap };
735
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800736 const CompositorTiming kCompositorTiming;
737
Brian Anderson3da8d272016-07-28 16:20:47 -0700738 const nsecs_t kStartTime;
739 const nsecs_t kGpuCompositionDoneTime;
740 const nsecs_t kPresentTime;
741 };
742
743 struct FrameEvents {
744 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
745 : mFenceMap(fenceMap),
746 kPostedTime(frameStartTime + 100),
747 kRequestedPresentTime(frameStartTime + 200),
748 kProducerAcquireTime(frameStartTime + 300),
749 kConsumerAcquireTime(frameStartTime + 301),
750 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700751 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700752 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700753 mRefreshes {
754 { mFenceMap, frameStartTime + 410 },
755 { mFenceMap, frameStartTime + 420 },
756 { mFenceMap, frameStartTime + 430 } } {}
757
758 void signalQueueFences() {
759 mFenceMap.signalAllForTest(
760 mAcquireConsumer.mFence, kConsumerAcquireTime);
761 mFenceMap.signalAllForTest(
762 mAcquireProducer.mFence, kProducerAcquireTime);
763 }
764
765 void signalRefreshFences() {
766 for (auto& re : mRefreshes) {
767 re.signalPostCompositeFences();
768 }
769 }
770
771 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700772 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
773 }
774
775 FenceToFenceTimeMap& mFenceMap;
776
777 FenceAndFenceTime mAcquireConsumer { mFenceMap };
778 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700779 FenceAndFenceTime mRelease { mFenceMap };
780
781 const nsecs_t kPostedTime;
782 const nsecs_t kRequestedPresentTime;
783 const nsecs_t kProducerAcquireTime;
784 const nsecs_t kConsumerAcquireTime;
785 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700786 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700787 const nsecs_t kReleaseTime;
788
789 RefreshEvents mRefreshes[3];
790 };
791
Brian Andersond0010582017-03-07 13:20:31 -0800792 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700793
794 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700795 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
796 mFakeConsumer = new FakeConsumer;
797 mCfeh = &mFakeConsumer->mFrameEventHistory;
798 mConsumer->consumerConnect(mFakeConsumer, false);
799 mConsumer->setConsumerName(String8("TestConsumer"));
800 mSurface = new TestSurface(mProducer, &mFenceMap);
801 mWindow = mSurface;
802
803 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
804 NATIVE_WINDOW_API_CPU));
805 native_window_set_buffer_count(mWindow.get(), 4);
806 }
807
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800808 void disableFrameTimestamps() {
809 mFakeConsumer->mGetFrameTimestampsEnabled = false;
810 native_window_enable_frame_timestamps(mWindow.get(), 0);
811 mFrameTimestampsEnabled = false;
812 }
813
Brian Anderson3da8d272016-07-28 16:20:47 -0700814 void enableFrameTimestamps() {
815 mFakeConsumer->mGetFrameTimestampsEnabled = true;
816 native_window_enable_frame_timestamps(mWindow.get(), 1);
817 mFrameTimestampsEnabled = true;
818 }
819
Brian Anderson1049d1d2016-12-16 17:25:57 -0800820 int getAllFrameTimestamps(uint64_t frameId) {
821 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700822 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
823 &outFirstRefreshStartTime, &outLastRefreshStartTime,
824 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700825 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700826 }
827
Brian Anderson3da8d272016-07-28 16:20:47 -0700828 void resetTimestamps() {
829 outRequestedPresentTime = -1;
830 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700831 outLatchTime = -1;
832 outFirstRefreshStartTime = -1;
833 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700834 outGpuCompositionDoneTime = -1;
835 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700836 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700837 outReleaseTime = -1;
838 }
839
Brian Anderson1049d1d2016-12-16 17:25:57 -0800840 uint64_t getNextFrameId() {
841 uint64_t frameId = -1;
842 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
843 EXPECT_EQ(status, NO_ERROR);
844 return frameId;
845 }
846
Brian Anderson3da8d272016-07-28 16:20:47 -0700847 void dequeueAndQueue(uint64_t frameIndex) {
848 int fence = -1;
849 ANativeWindowBuffer* buffer = nullptr;
850 ASSERT_EQ(NO_ERROR,
851 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
852
853 int oldAddFrameTimestampsCount =
854 mFakeConsumer->mAddFrameTimestampsCount;
855
856 FrameEvents* frame = &mFrames[frameIndex];
857 uint64_t frameNumber = frameIndex + 1;
858
859 NewFrameEventsEntry fe;
860 fe.frameNumber = frameNumber;
861 fe.postedTime = frame->kPostedTime;
862 fe.requestedPresentTime = frame->kRequestedPresentTime;
863 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
864 mFakeConsumer->mNewFrameEntryOverride = fe;
865
866 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
867 frame->mAcquireProducer.mFenceTime,
868 frame->mAcquireConsumer.mFenceTime);
869
870 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
871
872 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
873
874 EXPECT_EQ(
875 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
876 mFakeConsumer->mAddFrameTimestampsCount);
877 }
878
879 void addFrameEvents(
880 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
881 FrameEvents* oldFrame =
882 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
883 FrameEvents* newFrame = &mFrames[iNewFrame];
884
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800885 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700886 uint64_t nNewFrame = iNewFrame + 1;
887
Brian Anderson4e606e32017-03-16 15:34:57 -0700888 // Latch, Composite, and Release the frames in a plausible order.
889 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700890 // that's okay for the purposes of this test.
891 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
892
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700893 // Composite the previous frame one more time, which helps verify
894 // LastRefresh is updated properly.
895 if (oldFrame != nullptr) {
896 mCfeh->addPreComposition(nOldFrame,
897 oldFrame->mRefreshes[2].kStartTime);
898 gpuDoneFenceTime = gpuComposited ?
899 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
900 FenceTime::NO_FENCE;
901 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800902 oldFrame->mRefreshes[2].mPresent.mFenceTime,
903 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700904 }
905
906 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700907 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
908
909 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
910 gpuDoneFenceTime = gpuComposited ?
911 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
912 FenceTime::NO_FENCE;
913 // HWC2 releases the previous buffer after a new latch just before
914 // calling postComposition.
915 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700916 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700917 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
918 }
919 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800920 newFrame->mRefreshes[0].mPresent.mFenceTime,
921 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700922
Brian Anderson3da8d272016-07-28 16:20:47 -0700923 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
924 gpuDoneFenceTime = gpuComposited ?
925 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
926 FenceTime::NO_FENCE;
927 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800928 newFrame->mRefreshes[1].mPresent.mFenceTime,
929 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700930 }
931
Brian Anderson3da8d272016-07-28 16:20:47 -0700932 sp<IGraphicBufferProducer> mProducer;
933 sp<IGraphicBufferConsumer> mConsumer;
934 sp<FakeConsumer> mFakeConsumer;
935 ConsumerFrameEventHistory* mCfeh;
936 sp<TestSurface> mSurface;
937 sp<ANativeWindow> mWindow;
938
939 FenceToFenceTimeMap mFenceMap;
940
941 bool mFrameTimestampsEnabled = false;
942
943 int64_t outRequestedPresentTime = -1;
944 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700945 int64_t outLatchTime = -1;
946 int64_t outFirstRefreshStartTime = -1;
947 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700948 int64_t outGpuCompositionDoneTime = -1;
949 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700950 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700951 int64_t outReleaseTime = -1;
952
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800953 FrameEvents mFrames[3] {
954 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700955};
956
957
958// This test verifies that the frame timestamps are not retrieved when not
959// explicitly enabled via native_window_enable_frame_timestamps.
960// We want to check this to make sure there's no overhead for users
961// that don't need the timestamp information.
962TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
963 int fence;
964 ANativeWindowBuffer* buffer;
965
966 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
967 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
968
Brian Anderson1049d1d2016-12-16 17:25:57 -0800969 const uint64_t fId = getNextFrameId();
970
Brian Anderson3da8d272016-07-28 16:20:47 -0700971 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
972 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
973 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
974 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
975
976 // Verify the producer doesn't get frame timestamps piggybacked on queue.
977 // It is okay that frame timestamps are added in the consumer since it is
978 // still needed for SurfaceFlinger dumps.
979 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
980 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
981 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
982
983 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800984 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700985 EXPECT_EQ(INVALID_OPERATION, result);
986 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800987
988 // Verify compositor timing query fails.
989 nsecs_t compositeDeadline = 0;
990 nsecs_t compositeInterval = 0;
991 nsecs_t compositeToPresentLatency = 0;
992 result = native_window_get_compositor_timing(mWindow.get(),
993 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
994 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700995}
996
997// This test verifies that the frame timestamps are retrieved if explicitly
998// enabled via native_window_enable_frame_timestamps.
999TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001000 CompositorTiming initialCompositorTiming {
1001 1000000000, // 1s deadline
1002 16666667, // 16ms interval
1003 50000000, // 50ms present latency
1004 };
1005 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1006
Brian Anderson3da8d272016-07-28 16:20:47 -07001007 enableFrameTimestamps();
1008
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001009 // Verify the compositor timing query gets the initial compositor values
1010 // after timststamps are enabled; even before the first frame is queued
1011 // or dequeued.
1012 nsecs_t compositeDeadline = 0;
1013 nsecs_t compositeInterval = 0;
1014 nsecs_t compositeToPresentLatency = 0;
1015 mSurface->setNow(initialCompositorTiming.deadline - 1);
1016 int result = native_window_get_compositor_timing(mWindow.get(),
1017 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1018 EXPECT_EQ(NO_ERROR, result);
1019 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1020 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1021 EXPECT_EQ(initialCompositorTiming.presentLatency,
1022 compositeToPresentLatency);
1023
Brian Anderson3da8d272016-07-28 16:20:47 -07001024 int fence;
1025 ANativeWindowBuffer* buffer;
1026
1027 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001028 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001029
Brian Anderson1049d1d2016-12-16 17:25:57 -08001030 const uint64_t fId1 = getNextFrameId();
1031
Brian Anderson3da8d272016-07-28 16:20:47 -07001032 // Verify getFrameTimestamps is piggybacked on dequeue.
1033 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1034 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001035 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001036
1037 NewFrameEventsEntry f1;
1038 f1.frameNumber = 1;
1039 f1.postedTime = mFrames[0].kPostedTime;
1040 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1041 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1042 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1043 mFrames[0].mAcquireProducer.mFenceTime,
1044 mFrames[0].mAcquireConsumer.mFenceTime);
1045 mFakeConsumer->mNewFrameEntryOverride = f1;
1046 mFrames[0].signalQueueFences();
1047
1048 // Verify getFrameTimestamps is piggybacked on queue.
1049 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1050 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1051 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001052 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001053
1054 // Verify queries for timestamps that the producer doesn't know about
1055 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001056 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001057 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001058 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001059}
1060
Brian Anderson6b376712017-04-04 10:51:39 -07001061TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1062 bool displayPresentSupported = true;
1063 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1064
1065 // Verify supported bits are forwarded.
1066 int supportsPresent = -1;
1067 mWindow.get()->query(mWindow.get(),
1068 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1069 EXPECT_EQ(displayPresentSupported, supportsPresent);
1070}
1071
1072TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1073 bool displayPresentSupported = false;
1074 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1075
1076 // Verify supported bits are forwarded.
1077 int supportsPresent = -1;
1078 mWindow.get()->query(mWindow.get(),
1079 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1080 EXPECT_EQ(displayPresentSupported, supportsPresent);
1081}
1082
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001083TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1084 nsecs_t phase = 4000;
1085 nsecs_t interval = 1000;
1086
1087 // Timestamp in previous interval.
1088 nsecs_t timestamp = 3500;
1089 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1090 timestamp, phase, interval));
1091
1092 // Timestamp in next interval.
1093 timestamp = 4500;
1094 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1095 timestamp, phase, interval));
1096
1097 // Timestamp multiple intervals before.
1098 timestamp = 2500;
1099 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1100 timestamp, phase, interval));
1101
1102 // Timestamp multiple intervals after.
1103 timestamp = 6500;
1104 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1105 timestamp, phase, interval));
1106
1107 // Timestamp on previous interval.
1108 timestamp = 3000;
1109 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1110 timestamp, phase, interval));
1111
1112 // Timestamp on next interval.
1113 timestamp = 5000;
1114 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1115 timestamp, phase, interval));
1116
1117 // Timestamp equal to phase.
1118 timestamp = 4000;
1119 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1120 timestamp, phase, interval));
1121}
1122
1123// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1124// if the number of intervals elapsed is internally stored in an int.
1125TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1126 nsecs_t phase = 0;
1127 nsecs_t interval = 4000;
1128 nsecs_t big_timestamp = 8635916564000;
1129 int32_t intervals = big_timestamp / interval;
1130
1131 EXPECT_LT(intervals, 0);
1132 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1133 big_timestamp, phase, interval));
1134 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1135 big_timestamp, big_timestamp, interval));
1136}
1137
1138// This verifies the compositor timing is updated by refresh events
1139// and piggy backed on a queue, dequeue, and enabling of timestamps..
1140TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1141 CompositorTiming initialCompositorTiming {
1142 1000000000, // 1s deadline
1143 16666667, // 16ms interval
1144 50000000, // 50ms present latency
1145 };
1146 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1147
1148 enableFrameTimestamps();
1149
1150 // We get the initial values before any frames are submitted.
1151 nsecs_t compositeDeadline = 0;
1152 nsecs_t compositeInterval = 0;
1153 nsecs_t compositeToPresentLatency = 0;
1154 mSurface->setNow(initialCompositorTiming.deadline - 1);
1155 int 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(0);
1164 addFrameEvents(true, NO_FRAME_INDEX, 0);
1165
1166 // Still get the initial values because the frame events for frame 0
1167 // didn't get a chance to piggyback on a queue or dequeue yet.
1168 result = native_window_get_compositor_timing(mWindow.get(),
1169 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1170 EXPECT_EQ(NO_ERROR, result);
1171 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1172 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1173 EXPECT_EQ(initialCompositorTiming.presentLatency,
1174 compositeToPresentLatency);
1175
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001176 dequeueAndQueue(1);
1177 addFrameEvents(true, 0, 1);
1178
1179 // Now expect the composite values associated with frame 1.
1180 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1181 result = native_window_get_compositor_timing(mWindow.get(),
1182 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1183 EXPECT_EQ(NO_ERROR, result);
1184 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1185 compositeDeadline);
1186 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1187 compositeInterval);
1188 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1189 compositeToPresentLatency);
1190
1191 dequeueAndQueue(2);
1192 addFrameEvents(true, 1, 2);
1193
1194 // Now expect the composite values associated with frame 2.
1195 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1196 result = native_window_get_compositor_timing(mWindow.get(),
1197 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1198 EXPECT_EQ(NO_ERROR, result);
1199 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1200 compositeDeadline);
1201 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1202 compositeInterval);
1203 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1204 compositeToPresentLatency);
1205
1206 // Re-enabling frame timestamps should get the latest values.
1207 disableFrameTimestamps();
1208 enableFrameTimestamps();
1209
1210 // Now expect the composite values associated with frame 3.
1211 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1212 result = native_window_get_compositor_timing(mWindow.get(),
1213 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1214 EXPECT_EQ(NO_ERROR, result);
1215 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1216 compositeDeadline);
1217 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1218 compositeInterval);
1219 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1220 compositeToPresentLatency);
1221}
1222
1223// This verifies the compositor deadline properly snaps to the the next
1224// deadline based on the current time.
1225TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1226 CompositorTiming initialCompositorTiming {
1227 1000000000, // 1s deadline
1228 16666667, // 16ms interval
1229 50000000, // 50ms present latency
1230 };
1231 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1232
1233 enableFrameTimestamps();
1234
1235 nsecs_t compositeDeadline = 0;
1236 nsecs_t compositeInterval = 0;
1237 nsecs_t compositeToPresentLatency = 0;
1238
1239 // A "now" just before the deadline snaps to the deadline.
1240 mSurface->setNow(initialCompositorTiming.deadline - 1);
1241 int result = native_window_get_compositor_timing(mWindow.get(),
1242 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1243 EXPECT_EQ(NO_ERROR, result);
1244 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1245 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1246 EXPECT_EQ(expectedDeadline, compositeDeadline);
1247
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001248 dequeueAndQueue(0);
1249 addFrameEvents(true, NO_FRAME_INDEX, 0);
1250
1251 // A "now" just after the deadline snaps properly.
1252 mSurface->setNow(initialCompositorTiming.deadline + 1);
1253 result = native_window_get_compositor_timing(mWindow.get(),
1254 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1255 EXPECT_EQ(NO_ERROR, result);
1256 expectedDeadline =
1257 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1258 EXPECT_EQ(expectedDeadline, compositeDeadline);
1259
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001260 dequeueAndQueue(1);
1261 addFrameEvents(true, 0, 1);
1262
1263 // A "now" just after the next interval snaps properly.
1264 mSurface->setNow(
1265 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1266 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1267 result = native_window_get_compositor_timing(mWindow.get(),
1268 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1269 EXPECT_EQ(NO_ERROR, result);
1270 expectedDeadline =
1271 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1272 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1273 EXPECT_EQ(expectedDeadline, compositeDeadline);
1274
1275 dequeueAndQueue(2);
1276 addFrameEvents(true, 1, 2);
1277
1278 // A "now" over 1 interval before the deadline snaps properly.
1279 mSurface->setNow(
1280 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1281 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1282 result = native_window_get_compositor_timing(mWindow.get(),
1283 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1284 EXPECT_EQ(NO_ERROR, result);
1285 expectedDeadline =
1286 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1287 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1288 EXPECT_EQ(expectedDeadline, compositeDeadline);
1289
1290 // Re-enabling frame timestamps should get the latest values.
1291 disableFrameTimestamps();
1292 enableFrameTimestamps();
1293
1294 // A "now" over 2 intervals before the deadline snaps properly.
1295 mSurface->setNow(
1296 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1297 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1298 result = native_window_get_compositor_timing(mWindow.get(),
1299 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1300 EXPECT_EQ(NO_ERROR, result);
1301 expectedDeadline =
1302 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1303 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1304 EXPECT_EQ(expectedDeadline, compositeDeadline);
1305}
1306
Brian Anderson1049d1d2016-12-16 17:25:57 -08001307// This verifies the timestamps recorded in the consumer's
1308// FrameTimestampsHistory are properly retrieved by the producer for the
1309// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001310TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1311 enableFrameTimestamps();
1312
Brian Anderson1049d1d2016-12-16 17:25:57 -08001313 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001314 dequeueAndQueue(0);
1315 mFrames[0].signalQueueFences();
1316
Brian Anderson1049d1d2016-12-16 17:25:57 -08001317 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001318 dequeueAndQueue(1);
1319 mFrames[1].signalQueueFences();
1320
1321 addFrameEvents(true, NO_FRAME_INDEX, 0);
1322 mFrames[0].signalRefreshFences();
1323 addFrameEvents(true, 0, 1);
1324 mFrames[0].signalReleaseFences();
1325 mFrames[1].signalRefreshFences();
1326
1327 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001328 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001329 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001330 EXPECT_EQ(NO_ERROR, result);
1331 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1332 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001333 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1334 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1335 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001336 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1337 outGpuCompositionDoneTime);
1338 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001339 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001340 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1341
1342 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001343 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001344 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001345 EXPECT_EQ(NO_ERROR, result);
1346 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1347 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001348 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1349 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1350 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001351 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1352 outGpuCompositionDoneTime);
1353 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001354 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1355 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001356}
1357
1358// This test verifies the acquire fence recorded by the consumer is not sent
1359// back to the producer and the producer saves its own fence.
1360TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1361 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001362
Brian Anderson3da8d272016-07-28 16:20:47 -07001363 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001364 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001365 dequeueAndQueue(0);
1366
1367 // Verify queue-related timestamps for f1 are available immediately in the
1368 // producer without asking the consumer again, even before signaling the
1369 // acquire fence.
1370 resetTimestamps();
1371 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001372 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001373 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001374 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001375 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1376 EXPECT_EQ(NO_ERROR, result);
1377 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001378 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001379
1380 // Signal acquire fences. Verify a sync call still isn't necessary.
1381 mFrames[0].signalQueueFences();
1382
1383 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001384 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001385 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001386 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001387 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1388 EXPECT_EQ(NO_ERROR, result);
1389 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1390 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1391
1392 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001393 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001394 dequeueAndQueue(1);
1395
1396 // Verify queue-related timestamps for f2 are available immediately in the
1397 // producer without asking the consumer again, even before signaling the
1398 // acquire fence.
1399 resetTimestamps();
1400 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001401 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001402 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001403 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001404 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1405 EXPECT_EQ(NO_ERROR, result);
1406 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001407 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001408
1409 // Signal acquire fences. Verify a sync call still isn't necessary.
1410 mFrames[1].signalQueueFences();
1411
1412 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001413 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001414 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001415 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001416 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1417 EXPECT_EQ(NO_ERROR, result);
1418 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1419 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1420}
1421
1422TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1423 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001424
1425 // Dequeue and queue frame 1.
1426 dequeueAndQueue(0);
1427 mFrames[0].signalQueueFences();
1428
1429 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001430 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001431 dequeueAndQueue(1);
1432 mFrames[1].signalQueueFences();
1433
1434 addFrameEvents(true, NO_FRAME_INDEX, 0);
1435 mFrames[0].signalRefreshFences();
1436 addFrameEvents(true, 0, 1);
1437 mFrames[0].signalReleaseFences();
1438 mFrames[1].signalRefreshFences();
1439
1440 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001441 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001442 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001443 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1444 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001445 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001446 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1447}
1448
1449// This test verifies that fences can signal and update timestamps producer
1450// side without an additional sync call to the consumer.
1451TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1452 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001453
1454 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001455 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001456 dequeueAndQueue(0);
1457 mFrames[0].signalQueueFences();
1458
1459 // Dequeue and queue frame 2.
1460 dequeueAndQueue(1);
1461 mFrames[1].signalQueueFences();
1462
1463 addFrameEvents(true, NO_FRAME_INDEX, 0);
1464 addFrameEvents(true, 0, 1);
1465
1466 // Verify available timestamps are correct for frame 1, before any
1467 // fence has been signaled.
1468 // Note: A sync call is necessary here since the events triggered by
1469 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001470 resetTimestamps();
1471 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001472 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001473 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1474 EXPECT_EQ(NO_ERROR, result);
1475 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1476 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001477 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1478 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1479 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001480 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1481 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001482 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001483 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001484
1485 // Verify available timestamps are correct for frame 1 again, before any
1486 // fence has been signaled.
1487 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001488 resetTimestamps();
1489 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001490 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001491 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1492 EXPECT_EQ(NO_ERROR, result);
1493 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1494 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001495 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1496 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1497 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001498 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1499 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001500 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001501 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001502
1503 // Signal the fences for frame 1.
1504 mFrames[0].signalRefreshFences();
1505 mFrames[0].signalReleaseFences();
1506
1507 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001508 resetTimestamps();
1509 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001510 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001511 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1512 EXPECT_EQ(NO_ERROR, result);
1513 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1514 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001515 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1516 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1517 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001518 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1519 outGpuCompositionDoneTime);
1520 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001521 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001522 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1523}
1524
1525// This test verifies that if the frame wasn't GPU composited but has a refresh
1526// event a sync call isn't made to get the GPU composite done time since it will
1527// never exist.
1528TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1529 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001530
Brian Anderson3da8d272016-07-28 16:20:47 -07001531 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001532 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001533 dequeueAndQueue(0);
1534 mFrames[0].signalQueueFences();
1535
1536 // Dequeue and queue frame 2.
1537 dequeueAndQueue(1);
1538 mFrames[1].signalQueueFences();
1539
1540 addFrameEvents(false, NO_FRAME_INDEX, 0);
1541 addFrameEvents(false, 0, 1);
1542
1543 // Verify available timestamps are correct for frame 1, before any
1544 // fence has been signaled.
1545 // Note: A sync call is necessary here since the events triggered by
1546 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1547 resetTimestamps();
1548 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001549 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001550 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1551 EXPECT_EQ(NO_ERROR, result);
1552 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1553 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001554 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1555 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1556 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001557 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1558 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001559 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001560 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001561
1562 // Signal the fences for frame 1.
1563 mFrames[0].signalRefreshFences();
1564 mFrames[0].signalReleaseFences();
1565
1566 // Verify all timestamps, except GPU composition, are available without a
1567 // sync call.
1568 resetTimestamps();
1569 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001570 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001571 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1572 EXPECT_EQ(NO_ERROR, result);
1573 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1574 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001575 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1576 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1577 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001578 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001579 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001580 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001581 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1582}
1583
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001584// This test verifies that if the certain timestamps can't possibly exist for
1585// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001586TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001587 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001588
1589 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001590 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001591 dequeueAndQueue(0);
1592 mFrames[0].signalQueueFences();
1593
1594 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001595 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001596 dequeueAndQueue(1);
1597 mFrames[1].signalQueueFences();
1598
1599 addFrameEvents(false, NO_FRAME_INDEX, 0);
1600 addFrameEvents(false, 0, 1);
1601
1602 // Verify available timestamps are correct for frame 1, before any
1603 // fence has been signaled.
1604 // Note: A sync call is necessary here since the events triggered by
1605 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001606 resetTimestamps();
1607 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001608 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001609 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1610 EXPECT_EQ(NO_ERROR, result);
1611 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1612 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001613 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1614 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1615 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001616 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1617 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001618 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001619 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001620
1621 mFrames[0].signalRefreshFences();
1622 mFrames[0].signalReleaseFences();
1623 mFrames[1].signalRefreshFences();
1624
Brian Anderson1049d1d2016-12-16 17:25:57 -08001625 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001626 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001627 // available, a sync call should not occur because it's not possible for f2
1628 // to encounter the final value for those events until another frame is
1629 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001630 resetTimestamps();
1631 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001632 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001633 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1634 EXPECT_EQ(NO_ERROR, result);
1635 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1636 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001637 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1638 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1639 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001640 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001641 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001642 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1643 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001644}
1645
Brian Anderson6b376712017-04-04 10:51:39 -07001646// This test verifies there are no sync calls for present times
1647// when they aren't supported and that an error is returned.
1648
1649TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1650 enableFrameTimestamps();
1651 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1652
1653 // Dequeue and queue frame 1.
1654 const uint64_t fId1 = getNextFrameId();
1655 dequeueAndQueue(0);
1656
1657 // Verify a query for the Present times do not trigger a sync call if they
1658 // are not supported.
1659 resetTimestamps();
1660 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1661 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1662 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1663 &outDisplayPresentTime, nullptr, nullptr);
1664 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1665 EXPECT_EQ(BAD_VALUE, result);
1666 EXPECT_EQ(-1, outDisplayPresentTime);
1667}
1668
Dan Stoza932f0082017-05-31 13:50:16 -07001669} // namespace android