blob: 2c02ba657d6c9001acb9d22bb366cd1256eb728a [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 Lin9f034472018-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; }
584 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
585 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
586 override {
587 return NO_ERROR;
588 }
589 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700590 Vector<ColorMode>* /*outColorModes*/) override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700591 return NO_ERROR;
592 }
Peiyong Lina52f0292018-03-14 17:26:31 -0700593 ColorMode getActiveColorMode(const sp<IBinder>& /*display*/)
Brian Anderson3da8d272016-07-28 16:20:47 -0700594 override {
Peiyong Lina52f0292018-03-14 17:26:31 -0700595 return ColorMode::NATIVE;
Brian Anderson3da8d272016-07-28 16:20:47 -0700596 }
597 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700598 ColorMode /*colorMode*/) override { return NO_ERROR; }
Brian Anderson3da8d272016-07-28 16:20:47 -0700599 status_t captureScreen(const sp<IBinder>& /*display*/,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000600 sp<GraphicBuffer>* /*outBuffer*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700601 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800602 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700603 bool /*useIdentityTransform*/,
604 Rotation /*rotation*/) override { return NO_ERROR; }
chaviwa76b2712017-09-20 12:02:26 -0700605 virtual status_t captureLayers(const sp<IBinder>& /*parentHandle*/,
Robert Carr578038f2018-03-09 12:25:24 -0800606 sp<GraphicBuffer>* /*outBuffer*/, const Rect& /*sourceCrop*/,
607 float /*frameScale*/, bool /*childrenOnly*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700608 return NO_ERROR;
609 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700610 status_t clearAnimationFrameStats() override { return NO_ERROR; }
611 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
612 return NO_ERROR;
613 }
614 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
615 HdrCapabilities* /*outCapabilities*/) const override {
616 return NO_ERROR;
617 }
618 status_t enableVSyncInjections(bool /*enable*/) override {
619 return NO_ERROR;
620 }
621 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800622 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
623 return NO_ERROR;
624 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700625
626protected:
627 IBinder* onAsBinder() override { return nullptr; }
628
629private:
630 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700631};
632
633class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
634public:
635 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
636 : mFenceMap(fenceMap) {}
637
638 ~FakeProducerFrameEventHistory() {}
639
640 void updateAcquireFence(uint64_t frameNumber,
641 std::shared_ptr<FenceTime>&& acquire) override {
642 // Verify the acquire fence being added isn't the one from the consumer.
643 EXPECT_NE(mConsumerAcquireFence, acquire);
644 // Override the fence, so we can verify this was called by the
645 // producer after the frame is queued.
646 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
647 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
648 }
649
650 void setAcquireFenceOverride(
651 const std::shared_ptr<FenceTime>& acquireFenceOverride,
652 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
653 mAcquireFenceOverride = acquireFenceOverride;
654 mConsumerAcquireFence = consumerAcquireFence;
655 }
656
657protected:
658 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
659 const override {
660 return mFenceMap->createFenceTimeForTest(fence);
661 }
662
663 FenceToFenceTimeMap* mFenceMap{nullptr};
664
665 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
666 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
667};
668
669
670class TestSurface : public Surface {
671public:
672 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
673 FenceToFenceTimeMap* fenceMap)
674 : Surface(bufferProducer),
675 mFakeSurfaceComposer(new FakeSurfaceComposer) {
676 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
677 mFrameEventHistory.reset(mFakeFrameEventHistory);
678 }
679
680 ~TestSurface() override {}
681
682 sp<ISurfaceComposer> composerService() const override {
683 return mFakeSurfaceComposer;
684 }
685
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800686 nsecs_t now() const override {
687 return mNow;
688 }
689
690 void setNow(nsecs_t now) {
691 mNow = now;
692 }
693
Brian Anderson3da8d272016-07-28 16:20:47 -0700694public:
695 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800696 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700697
698 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
699 // but this raw pointer gives access to test functionality.
700 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
701};
702
703
Brian Andersond0010582017-03-07 13:20:31 -0800704class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700705protected:
706 struct FenceAndFenceTime {
707 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
708 : mFence(new Fence),
709 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
710 sp<Fence> mFence { nullptr };
711 std::shared_ptr<FenceTime> mFenceTime { nullptr };
712 };
713
714 struct RefreshEvents {
715 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800716 : mFenceMap(fenceMap),
717 kCompositorTiming(
718 {refreshStart, refreshStart + 1, refreshStart + 2 }),
719 kStartTime(refreshStart + 3),
720 kGpuCompositionDoneTime(refreshStart + 4),
721 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700722
723 void signalPostCompositeFences() {
724 mFenceMap.signalAllForTest(
725 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
726 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
727 }
728
729 FenceToFenceTimeMap& mFenceMap;
730
731 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
732 FenceAndFenceTime mPresent { mFenceMap };
733
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800734 const CompositorTiming kCompositorTiming;
735
Brian Anderson3da8d272016-07-28 16:20:47 -0700736 const nsecs_t kStartTime;
737 const nsecs_t kGpuCompositionDoneTime;
738 const nsecs_t kPresentTime;
739 };
740
741 struct FrameEvents {
742 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
743 : mFenceMap(fenceMap),
744 kPostedTime(frameStartTime + 100),
745 kRequestedPresentTime(frameStartTime + 200),
746 kProducerAcquireTime(frameStartTime + 300),
747 kConsumerAcquireTime(frameStartTime + 301),
748 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700749 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700750 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700751 mRefreshes {
752 { mFenceMap, frameStartTime + 410 },
753 { mFenceMap, frameStartTime + 420 },
754 { mFenceMap, frameStartTime + 430 } } {}
755
756 void signalQueueFences() {
757 mFenceMap.signalAllForTest(
758 mAcquireConsumer.mFence, kConsumerAcquireTime);
759 mFenceMap.signalAllForTest(
760 mAcquireProducer.mFence, kProducerAcquireTime);
761 }
762
763 void signalRefreshFences() {
764 for (auto& re : mRefreshes) {
765 re.signalPostCompositeFences();
766 }
767 }
768
769 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700770 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
771 }
772
773 FenceToFenceTimeMap& mFenceMap;
774
775 FenceAndFenceTime mAcquireConsumer { mFenceMap };
776 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700777 FenceAndFenceTime mRelease { mFenceMap };
778
779 const nsecs_t kPostedTime;
780 const nsecs_t kRequestedPresentTime;
781 const nsecs_t kProducerAcquireTime;
782 const nsecs_t kConsumerAcquireTime;
783 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700784 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700785 const nsecs_t kReleaseTime;
786
787 RefreshEvents mRefreshes[3];
788 };
789
Brian Andersond0010582017-03-07 13:20:31 -0800790 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700791
792 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700793 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
794 mFakeConsumer = new FakeConsumer;
795 mCfeh = &mFakeConsumer->mFrameEventHistory;
796 mConsumer->consumerConnect(mFakeConsumer, false);
797 mConsumer->setConsumerName(String8("TestConsumer"));
798 mSurface = new TestSurface(mProducer, &mFenceMap);
799 mWindow = mSurface;
800
801 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
802 NATIVE_WINDOW_API_CPU));
803 native_window_set_buffer_count(mWindow.get(), 4);
804 }
805
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800806 void disableFrameTimestamps() {
807 mFakeConsumer->mGetFrameTimestampsEnabled = false;
808 native_window_enable_frame_timestamps(mWindow.get(), 0);
809 mFrameTimestampsEnabled = false;
810 }
811
Brian Anderson3da8d272016-07-28 16:20:47 -0700812 void enableFrameTimestamps() {
813 mFakeConsumer->mGetFrameTimestampsEnabled = true;
814 native_window_enable_frame_timestamps(mWindow.get(), 1);
815 mFrameTimestampsEnabled = true;
816 }
817
Brian Anderson1049d1d2016-12-16 17:25:57 -0800818 int getAllFrameTimestamps(uint64_t frameId) {
819 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700820 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
821 &outFirstRefreshStartTime, &outLastRefreshStartTime,
822 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700823 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700824 }
825
Brian Anderson3da8d272016-07-28 16:20:47 -0700826 void resetTimestamps() {
827 outRequestedPresentTime = -1;
828 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700829 outLatchTime = -1;
830 outFirstRefreshStartTime = -1;
831 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700832 outGpuCompositionDoneTime = -1;
833 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700834 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700835 outReleaseTime = -1;
836 }
837
Brian Anderson1049d1d2016-12-16 17:25:57 -0800838 uint64_t getNextFrameId() {
839 uint64_t frameId = -1;
840 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
841 EXPECT_EQ(status, NO_ERROR);
842 return frameId;
843 }
844
Brian Anderson3da8d272016-07-28 16:20:47 -0700845 void dequeueAndQueue(uint64_t frameIndex) {
846 int fence = -1;
847 ANativeWindowBuffer* buffer = nullptr;
848 ASSERT_EQ(NO_ERROR,
849 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
850
851 int oldAddFrameTimestampsCount =
852 mFakeConsumer->mAddFrameTimestampsCount;
853
854 FrameEvents* frame = &mFrames[frameIndex];
855 uint64_t frameNumber = frameIndex + 1;
856
857 NewFrameEventsEntry fe;
858 fe.frameNumber = frameNumber;
859 fe.postedTime = frame->kPostedTime;
860 fe.requestedPresentTime = frame->kRequestedPresentTime;
861 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
862 mFakeConsumer->mNewFrameEntryOverride = fe;
863
864 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
865 frame->mAcquireProducer.mFenceTime,
866 frame->mAcquireConsumer.mFenceTime);
867
868 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
869
870 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
871
872 EXPECT_EQ(
873 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
874 mFakeConsumer->mAddFrameTimestampsCount);
875 }
876
877 void addFrameEvents(
878 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
879 FrameEvents* oldFrame =
880 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
881 FrameEvents* newFrame = &mFrames[iNewFrame];
882
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800883 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700884 uint64_t nNewFrame = iNewFrame + 1;
885
Brian Anderson4e606e32017-03-16 15:34:57 -0700886 // Latch, Composite, and Release the frames in a plausible order.
887 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700888 // that's okay for the purposes of this test.
889 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
890
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700891 // Composite the previous frame one more time, which helps verify
892 // LastRefresh is updated properly.
893 if (oldFrame != nullptr) {
894 mCfeh->addPreComposition(nOldFrame,
895 oldFrame->mRefreshes[2].kStartTime);
896 gpuDoneFenceTime = gpuComposited ?
897 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
898 FenceTime::NO_FENCE;
899 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800900 oldFrame->mRefreshes[2].mPresent.mFenceTime,
901 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700902 }
903
904 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700905 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
906
907 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
908 gpuDoneFenceTime = gpuComposited ?
909 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
910 FenceTime::NO_FENCE;
911 // HWC2 releases the previous buffer after a new latch just before
912 // calling postComposition.
913 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700914 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700915 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
916 }
917 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800918 newFrame->mRefreshes[0].mPresent.mFenceTime,
919 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700920
Brian Anderson3da8d272016-07-28 16:20:47 -0700921 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
922 gpuDoneFenceTime = gpuComposited ?
923 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
924 FenceTime::NO_FENCE;
925 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800926 newFrame->mRefreshes[1].mPresent.mFenceTime,
927 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700928 }
929
Brian Anderson3da8d272016-07-28 16:20:47 -0700930 sp<IGraphicBufferProducer> mProducer;
931 sp<IGraphicBufferConsumer> mConsumer;
932 sp<FakeConsumer> mFakeConsumer;
933 ConsumerFrameEventHistory* mCfeh;
934 sp<TestSurface> mSurface;
935 sp<ANativeWindow> mWindow;
936
937 FenceToFenceTimeMap mFenceMap;
938
939 bool mFrameTimestampsEnabled = false;
940
941 int64_t outRequestedPresentTime = -1;
942 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700943 int64_t outLatchTime = -1;
944 int64_t outFirstRefreshStartTime = -1;
945 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700946 int64_t outGpuCompositionDoneTime = -1;
947 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700948 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700949 int64_t outReleaseTime = -1;
950
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800951 FrameEvents mFrames[3] {
952 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700953};
954
955
956// This test verifies that the frame timestamps are not retrieved when not
957// explicitly enabled via native_window_enable_frame_timestamps.
958// We want to check this to make sure there's no overhead for users
959// that don't need the timestamp information.
960TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
961 int fence;
962 ANativeWindowBuffer* buffer;
963
964 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
965 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
966
Brian Anderson1049d1d2016-12-16 17:25:57 -0800967 const uint64_t fId = getNextFrameId();
968
Brian Anderson3da8d272016-07-28 16:20:47 -0700969 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
970 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
971 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
972 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
973
974 // Verify the producer doesn't get frame timestamps piggybacked on queue.
975 // It is okay that frame timestamps are added in the consumer since it is
976 // still needed for SurfaceFlinger dumps.
977 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
978 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
979 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
980
981 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800982 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700983 EXPECT_EQ(INVALID_OPERATION, result);
984 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800985
986 // Verify compositor timing query fails.
987 nsecs_t compositeDeadline = 0;
988 nsecs_t compositeInterval = 0;
989 nsecs_t compositeToPresentLatency = 0;
990 result = native_window_get_compositor_timing(mWindow.get(),
991 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
992 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700993}
994
995// This test verifies that the frame timestamps are retrieved if explicitly
996// enabled via native_window_enable_frame_timestamps.
997TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800998 CompositorTiming initialCompositorTiming {
999 1000000000, // 1s deadline
1000 16666667, // 16ms interval
1001 50000000, // 50ms present latency
1002 };
1003 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1004
Brian Anderson3da8d272016-07-28 16:20:47 -07001005 enableFrameTimestamps();
1006
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001007 // Verify the compositor timing query gets the initial compositor values
1008 // after timststamps are enabled; even before the first frame is queued
1009 // or dequeued.
1010 nsecs_t compositeDeadline = 0;
1011 nsecs_t compositeInterval = 0;
1012 nsecs_t compositeToPresentLatency = 0;
1013 mSurface->setNow(initialCompositorTiming.deadline - 1);
1014 int result = native_window_get_compositor_timing(mWindow.get(),
1015 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1016 EXPECT_EQ(NO_ERROR, result);
1017 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1018 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1019 EXPECT_EQ(initialCompositorTiming.presentLatency,
1020 compositeToPresentLatency);
1021
Brian Anderson3da8d272016-07-28 16:20:47 -07001022 int fence;
1023 ANativeWindowBuffer* buffer;
1024
1025 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001026 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001027
Brian Anderson1049d1d2016-12-16 17:25:57 -08001028 const uint64_t fId1 = getNextFrameId();
1029
Brian Anderson3da8d272016-07-28 16:20:47 -07001030 // Verify getFrameTimestamps is piggybacked on dequeue.
1031 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1032 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001033 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001034
1035 NewFrameEventsEntry f1;
1036 f1.frameNumber = 1;
1037 f1.postedTime = mFrames[0].kPostedTime;
1038 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1039 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1040 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1041 mFrames[0].mAcquireProducer.mFenceTime,
1042 mFrames[0].mAcquireConsumer.mFenceTime);
1043 mFakeConsumer->mNewFrameEntryOverride = f1;
1044 mFrames[0].signalQueueFences();
1045
1046 // Verify getFrameTimestamps is piggybacked on queue.
1047 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1048 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1049 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001050 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001051
1052 // Verify queries for timestamps that the producer doesn't know about
1053 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001054 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001055 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001056 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001057}
1058
Brian Anderson6b376712017-04-04 10:51:39 -07001059TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1060 bool displayPresentSupported = true;
1061 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1062
1063 // Verify supported bits are forwarded.
1064 int supportsPresent = -1;
1065 mWindow.get()->query(mWindow.get(),
1066 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1067 EXPECT_EQ(displayPresentSupported, supportsPresent);
1068}
1069
1070TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1071 bool displayPresentSupported = false;
1072 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1073
1074 // Verify supported bits are forwarded.
1075 int supportsPresent = -1;
1076 mWindow.get()->query(mWindow.get(),
1077 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1078 EXPECT_EQ(displayPresentSupported, supportsPresent);
1079}
1080
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001081TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1082 nsecs_t phase = 4000;
1083 nsecs_t interval = 1000;
1084
1085 // Timestamp in previous interval.
1086 nsecs_t timestamp = 3500;
1087 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1088 timestamp, phase, interval));
1089
1090 // Timestamp in next interval.
1091 timestamp = 4500;
1092 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1093 timestamp, phase, interval));
1094
1095 // Timestamp multiple intervals before.
1096 timestamp = 2500;
1097 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1098 timestamp, phase, interval));
1099
1100 // Timestamp multiple intervals after.
1101 timestamp = 6500;
1102 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1103 timestamp, phase, interval));
1104
1105 // Timestamp on previous interval.
1106 timestamp = 3000;
1107 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1108 timestamp, phase, interval));
1109
1110 // Timestamp on next interval.
1111 timestamp = 5000;
1112 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1113 timestamp, phase, interval));
1114
1115 // Timestamp equal to phase.
1116 timestamp = 4000;
1117 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1118 timestamp, phase, interval));
1119}
1120
1121// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1122// if the number of intervals elapsed is internally stored in an int.
1123TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1124 nsecs_t phase = 0;
1125 nsecs_t interval = 4000;
1126 nsecs_t big_timestamp = 8635916564000;
1127 int32_t intervals = big_timestamp / interval;
1128
1129 EXPECT_LT(intervals, 0);
1130 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1131 big_timestamp, phase, interval));
1132 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1133 big_timestamp, big_timestamp, interval));
1134}
1135
1136// This verifies the compositor timing is updated by refresh events
1137// and piggy backed on a queue, dequeue, and enabling of timestamps..
1138TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1139 CompositorTiming initialCompositorTiming {
1140 1000000000, // 1s deadline
1141 16666667, // 16ms interval
1142 50000000, // 50ms present latency
1143 };
1144 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1145
1146 enableFrameTimestamps();
1147
1148 // We get the initial values before any frames are submitted.
1149 nsecs_t compositeDeadline = 0;
1150 nsecs_t compositeInterval = 0;
1151 nsecs_t compositeToPresentLatency = 0;
1152 mSurface->setNow(initialCompositorTiming.deadline - 1);
1153 int result = native_window_get_compositor_timing(mWindow.get(),
1154 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1155 EXPECT_EQ(NO_ERROR, result);
1156 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1157 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1158 EXPECT_EQ(initialCompositorTiming.presentLatency,
1159 compositeToPresentLatency);
1160
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001161 dequeueAndQueue(0);
1162 addFrameEvents(true, NO_FRAME_INDEX, 0);
1163
1164 // Still get the initial values because the frame events for frame 0
1165 // didn't get a chance to piggyback on a queue or dequeue yet.
1166 result = native_window_get_compositor_timing(mWindow.get(),
1167 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1168 EXPECT_EQ(NO_ERROR, result);
1169 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1170 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1171 EXPECT_EQ(initialCompositorTiming.presentLatency,
1172 compositeToPresentLatency);
1173
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001174 dequeueAndQueue(1);
1175 addFrameEvents(true, 0, 1);
1176
1177 // Now expect the composite values associated with frame 1.
1178 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1179 result = native_window_get_compositor_timing(mWindow.get(),
1180 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1181 EXPECT_EQ(NO_ERROR, result);
1182 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1183 compositeDeadline);
1184 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1185 compositeInterval);
1186 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1187 compositeToPresentLatency);
1188
1189 dequeueAndQueue(2);
1190 addFrameEvents(true, 1, 2);
1191
1192 // Now expect the composite values associated with frame 2.
1193 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1194 result = native_window_get_compositor_timing(mWindow.get(),
1195 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1196 EXPECT_EQ(NO_ERROR, result);
1197 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1198 compositeDeadline);
1199 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1200 compositeInterval);
1201 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1202 compositeToPresentLatency);
1203
1204 // Re-enabling frame timestamps should get the latest values.
1205 disableFrameTimestamps();
1206 enableFrameTimestamps();
1207
1208 // Now expect the composite values associated with frame 3.
1209 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1210 result = native_window_get_compositor_timing(mWindow.get(),
1211 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1212 EXPECT_EQ(NO_ERROR, result);
1213 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1214 compositeDeadline);
1215 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1216 compositeInterval);
1217 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1218 compositeToPresentLatency);
1219}
1220
1221// This verifies the compositor deadline properly snaps to the the next
1222// deadline based on the current time.
1223TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1224 CompositorTiming initialCompositorTiming {
1225 1000000000, // 1s deadline
1226 16666667, // 16ms interval
1227 50000000, // 50ms present latency
1228 };
1229 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1230
1231 enableFrameTimestamps();
1232
1233 nsecs_t compositeDeadline = 0;
1234 nsecs_t compositeInterval = 0;
1235 nsecs_t compositeToPresentLatency = 0;
1236
1237 // A "now" just before the deadline snaps to the deadline.
1238 mSurface->setNow(initialCompositorTiming.deadline - 1);
1239 int result = native_window_get_compositor_timing(mWindow.get(),
1240 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1241 EXPECT_EQ(NO_ERROR, result);
1242 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1243 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1244 EXPECT_EQ(expectedDeadline, compositeDeadline);
1245
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001246 dequeueAndQueue(0);
1247 addFrameEvents(true, NO_FRAME_INDEX, 0);
1248
1249 // A "now" just after the deadline snaps properly.
1250 mSurface->setNow(initialCompositorTiming.deadline + 1);
1251 result = native_window_get_compositor_timing(mWindow.get(),
1252 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1253 EXPECT_EQ(NO_ERROR, result);
1254 expectedDeadline =
1255 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1256 EXPECT_EQ(expectedDeadline, compositeDeadline);
1257
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001258 dequeueAndQueue(1);
1259 addFrameEvents(true, 0, 1);
1260
1261 // A "now" just after the next interval snaps properly.
1262 mSurface->setNow(
1263 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1264 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1265 result = native_window_get_compositor_timing(mWindow.get(),
1266 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1267 EXPECT_EQ(NO_ERROR, result);
1268 expectedDeadline =
1269 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1270 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1271 EXPECT_EQ(expectedDeadline, compositeDeadline);
1272
1273 dequeueAndQueue(2);
1274 addFrameEvents(true, 1, 2);
1275
1276 // A "now" over 1 interval before the deadline snaps properly.
1277 mSurface->setNow(
1278 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1279 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1280 result = native_window_get_compositor_timing(mWindow.get(),
1281 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1282 EXPECT_EQ(NO_ERROR, result);
1283 expectedDeadline =
1284 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1285 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1286 EXPECT_EQ(expectedDeadline, compositeDeadline);
1287
1288 // Re-enabling frame timestamps should get the latest values.
1289 disableFrameTimestamps();
1290 enableFrameTimestamps();
1291
1292 // A "now" over 2 intervals before the deadline snaps properly.
1293 mSurface->setNow(
1294 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1295 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1296 result = native_window_get_compositor_timing(mWindow.get(),
1297 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1298 EXPECT_EQ(NO_ERROR, result);
1299 expectedDeadline =
1300 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1301 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1302 EXPECT_EQ(expectedDeadline, compositeDeadline);
1303}
1304
Brian Anderson1049d1d2016-12-16 17:25:57 -08001305// This verifies the timestamps recorded in the consumer's
1306// FrameTimestampsHistory are properly retrieved by the producer for the
1307// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001308TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1309 enableFrameTimestamps();
1310
Brian Anderson1049d1d2016-12-16 17:25:57 -08001311 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001312 dequeueAndQueue(0);
1313 mFrames[0].signalQueueFences();
1314
Brian Anderson1049d1d2016-12-16 17:25:57 -08001315 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001316 dequeueAndQueue(1);
1317 mFrames[1].signalQueueFences();
1318
1319 addFrameEvents(true, NO_FRAME_INDEX, 0);
1320 mFrames[0].signalRefreshFences();
1321 addFrameEvents(true, 0, 1);
1322 mFrames[0].signalReleaseFences();
1323 mFrames[1].signalRefreshFences();
1324
1325 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001326 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001327 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001328 EXPECT_EQ(NO_ERROR, result);
1329 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1330 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001331 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1332 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1333 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001334 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1335 outGpuCompositionDoneTime);
1336 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001337 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001338 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1339
1340 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001341 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001342 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001343 EXPECT_EQ(NO_ERROR, result);
1344 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1345 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001346 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1347 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1348 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001349 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1350 outGpuCompositionDoneTime);
1351 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001352 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1353 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001354}
1355
1356// This test verifies the acquire fence recorded by the consumer is not sent
1357// back to the producer and the producer saves its own fence.
1358TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1359 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001360
Brian Anderson3da8d272016-07-28 16:20:47 -07001361 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001362 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001363 dequeueAndQueue(0);
1364
1365 // Verify queue-related timestamps for f1 are available immediately in the
1366 // producer without asking the consumer again, even before signaling the
1367 // acquire fence.
1368 resetTimestamps();
1369 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001370 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001371 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001372 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001373 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1374 EXPECT_EQ(NO_ERROR, result);
1375 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001376 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001377
1378 // Signal acquire fences. Verify a sync call still isn't necessary.
1379 mFrames[0].signalQueueFences();
1380
1381 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001382 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001383 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001384 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001385 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1386 EXPECT_EQ(NO_ERROR, result);
1387 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1388 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1389
1390 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001391 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001392 dequeueAndQueue(1);
1393
1394 // Verify queue-related timestamps for f2 are available immediately in the
1395 // producer without asking the consumer again, even before signaling the
1396 // acquire fence.
1397 resetTimestamps();
1398 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001399 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001400 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001401 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001402 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1403 EXPECT_EQ(NO_ERROR, result);
1404 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001405 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001406
1407 // Signal acquire fences. Verify a sync call still isn't necessary.
1408 mFrames[1].signalQueueFences();
1409
1410 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001411 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001412 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001413 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001414 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1415 EXPECT_EQ(NO_ERROR, result);
1416 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1417 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1418}
1419
1420TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1421 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001422
1423 // Dequeue and queue frame 1.
1424 dequeueAndQueue(0);
1425 mFrames[0].signalQueueFences();
1426
1427 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001428 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001429 dequeueAndQueue(1);
1430 mFrames[1].signalQueueFences();
1431
1432 addFrameEvents(true, NO_FRAME_INDEX, 0);
1433 mFrames[0].signalRefreshFences();
1434 addFrameEvents(true, 0, 1);
1435 mFrames[0].signalReleaseFences();
1436 mFrames[1].signalRefreshFences();
1437
1438 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001439 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001440 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001441 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1442 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001443 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001444 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1445}
1446
1447// This test verifies that fences can signal and update timestamps producer
1448// side without an additional sync call to the consumer.
1449TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1450 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001451
1452 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001453 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001454 dequeueAndQueue(0);
1455 mFrames[0].signalQueueFences();
1456
1457 // Dequeue and queue frame 2.
1458 dequeueAndQueue(1);
1459 mFrames[1].signalQueueFences();
1460
1461 addFrameEvents(true, NO_FRAME_INDEX, 0);
1462 addFrameEvents(true, 0, 1);
1463
1464 // Verify available timestamps are correct for frame 1, before any
1465 // fence has been signaled.
1466 // Note: A sync call is necessary here since the events triggered by
1467 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001468 resetTimestamps();
1469 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001470 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001471 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1472 EXPECT_EQ(NO_ERROR, result);
1473 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1474 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001475 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1476 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1477 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001478 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1479 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001480 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001481 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001482
1483 // Verify available timestamps are correct for frame 1 again, before any
1484 // fence has been signaled.
1485 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001486 resetTimestamps();
1487 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001488 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001489 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1490 EXPECT_EQ(NO_ERROR, result);
1491 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1492 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001493 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1494 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1495 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001496 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1497 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001498 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001499 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001500
1501 // Signal the fences for frame 1.
1502 mFrames[0].signalRefreshFences();
1503 mFrames[0].signalReleaseFences();
1504
1505 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001506 resetTimestamps();
1507 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001508 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001509 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1510 EXPECT_EQ(NO_ERROR, result);
1511 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1512 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001513 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1514 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1515 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001516 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1517 outGpuCompositionDoneTime);
1518 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001519 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001520 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1521}
1522
1523// This test verifies that if the frame wasn't GPU composited but has a refresh
1524// event a sync call isn't made to get the GPU composite done time since it will
1525// never exist.
1526TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1527 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001528
Brian Anderson3da8d272016-07-28 16:20:47 -07001529 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001530 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001531 dequeueAndQueue(0);
1532 mFrames[0].signalQueueFences();
1533
1534 // Dequeue and queue frame 2.
1535 dequeueAndQueue(1);
1536 mFrames[1].signalQueueFences();
1537
1538 addFrameEvents(false, NO_FRAME_INDEX, 0);
1539 addFrameEvents(false, 0, 1);
1540
1541 // Verify available timestamps are correct for frame 1, before any
1542 // fence has been signaled.
1543 // Note: A sync call is necessary here since the events triggered by
1544 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1545 resetTimestamps();
1546 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001547 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001548 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1549 EXPECT_EQ(NO_ERROR, result);
1550 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1551 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001552 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1553 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1554 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001555 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1556 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001557 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001558 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001559
1560 // Signal the fences for frame 1.
1561 mFrames[0].signalRefreshFences();
1562 mFrames[0].signalReleaseFences();
1563
1564 // Verify all timestamps, except GPU composition, are available without a
1565 // sync call.
1566 resetTimestamps();
1567 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001568 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001569 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1570 EXPECT_EQ(NO_ERROR, result);
1571 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1572 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001573 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1574 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1575 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001576 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001577 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001578 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001579 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1580}
1581
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001582// This test verifies that if the certain timestamps can't possibly exist for
1583// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001584TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001585 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001586
1587 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001588 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001589 dequeueAndQueue(0);
1590 mFrames[0].signalQueueFences();
1591
1592 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001593 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001594 dequeueAndQueue(1);
1595 mFrames[1].signalQueueFences();
1596
1597 addFrameEvents(false, NO_FRAME_INDEX, 0);
1598 addFrameEvents(false, 0, 1);
1599
1600 // Verify available timestamps are correct for frame 1, before any
1601 // fence has been signaled.
1602 // Note: A sync call is necessary here since the events triggered by
1603 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001604 resetTimestamps();
1605 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001606 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001607 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1608 EXPECT_EQ(NO_ERROR, result);
1609 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1610 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001611 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1612 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1613 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001614 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1615 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001616 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001617 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001618
1619 mFrames[0].signalRefreshFences();
1620 mFrames[0].signalReleaseFences();
1621 mFrames[1].signalRefreshFences();
1622
Brian Anderson1049d1d2016-12-16 17:25:57 -08001623 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001624 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001625 // available, a sync call should not occur because it's not possible for f2
1626 // to encounter the final value for those events until another frame is
1627 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001628 resetTimestamps();
1629 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001630 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001631 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1632 EXPECT_EQ(NO_ERROR, result);
1633 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1634 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001635 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1636 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1637 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001638 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001639 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001640 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1641 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001642}
1643
Brian Anderson6b376712017-04-04 10:51:39 -07001644// This test verifies there are no sync calls for present times
1645// when they aren't supported and that an error is returned.
1646
1647TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1648 enableFrameTimestamps();
1649 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1650
1651 // Dequeue and queue frame 1.
1652 const uint64_t fId1 = getNextFrameId();
1653 dequeueAndQueue(0);
1654
1655 // Verify a query for the Present times do not trigger a sync call if they
1656 // are not supported.
1657 resetTimestamps();
1658 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1659 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1660 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1661 &outDisplayPresentTime, nullptr, nullptr);
1662 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1663 EXPECT_EQ(BAD_VALUE, result);
1664 EXPECT_EQ(-1, outDisplayPresentTime);
1665}
1666
Dan Stoza932f0082017-05-31 13:50:16 -07001667} // namespace android