blob: f22e7026e960904584cfbf6b03cc004c71b471dd [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
Yi Konga03e0442018-07-17 11:16:57 -070075 ASSERT_TRUE(mSurfaceControl != nullptr);
Jamie Gennis134f0422011-03-08 12:18:54 -080076 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();
Yi Konga03e0442018-07-17 11:16:57 -070084 ASSERT_TRUE(mSurface != nullptr);
Jamie Gennis134f0422011-03-08 12:18:54 -080085 }
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));
Yi Konga03e0442018-07-17 11:16:57 -0700148 ANativeWindowBuffer* buf = nullptr;
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 Zhangcd3f9e92018-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:
638 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
639 : mFenceMap(fenceMap) {}
640
641 ~FakeProducerFrameEventHistory() {}
642
643 void updateAcquireFence(uint64_t frameNumber,
644 std::shared_ptr<FenceTime>&& acquire) override {
645 // Verify the acquire fence being added isn't the one from the consumer.
646 EXPECT_NE(mConsumerAcquireFence, acquire);
647 // Override the fence, so we can verify this was called by the
648 // producer after the frame is queued.
649 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
650 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
651 }
652
653 void setAcquireFenceOverride(
654 const std::shared_ptr<FenceTime>& acquireFenceOverride,
655 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
656 mAcquireFenceOverride = acquireFenceOverride;
657 mConsumerAcquireFence = consumerAcquireFence;
658 }
659
660protected:
661 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
662 const override {
663 return mFenceMap->createFenceTimeForTest(fence);
664 }
665
666 FenceToFenceTimeMap* mFenceMap{nullptr};
667
668 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
669 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
670};
671
672
673class TestSurface : public Surface {
674public:
675 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
676 FenceToFenceTimeMap* fenceMap)
677 : Surface(bufferProducer),
678 mFakeSurfaceComposer(new FakeSurfaceComposer) {
679 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
680 mFrameEventHistory.reset(mFakeFrameEventHistory);
681 }
682
683 ~TestSurface() override {}
684
685 sp<ISurfaceComposer> composerService() const override {
686 return mFakeSurfaceComposer;
687 }
688
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800689 nsecs_t now() const override {
690 return mNow;
691 }
692
693 void setNow(nsecs_t now) {
694 mNow = now;
695 }
696
Brian Anderson3da8d272016-07-28 16:20:47 -0700697public:
698 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800699 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700700
701 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
702 // but this raw pointer gives access to test functionality.
703 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
704};
705
706
Brian Andersond0010582017-03-07 13:20:31 -0800707class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700708protected:
709 struct FenceAndFenceTime {
710 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
711 : mFence(new Fence),
712 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
713 sp<Fence> mFence { nullptr };
714 std::shared_ptr<FenceTime> mFenceTime { nullptr };
715 };
716
717 struct RefreshEvents {
718 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800719 : mFenceMap(fenceMap),
720 kCompositorTiming(
721 {refreshStart, refreshStart + 1, refreshStart + 2 }),
722 kStartTime(refreshStart + 3),
723 kGpuCompositionDoneTime(refreshStart + 4),
724 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700725
726 void signalPostCompositeFences() {
727 mFenceMap.signalAllForTest(
728 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
729 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
730 }
731
732 FenceToFenceTimeMap& mFenceMap;
733
734 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
735 FenceAndFenceTime mPresent { mFenceMap };
736
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800737 const CompositorTiming kCompositorTiming;
738
Brian Anderson3da8d272016-07-28 16:20:47 -0700739 const nsecs_t kStartTime;
740 const nsecs_t kGpuCompositionDoneTime;
741 const nsecs_t kPresentTime;
742 };
743
744 struct FrameEvents {
745 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
746 : mFenceMap(fenceMap),
747 kPostedTime(frameStartTime + 100),
748 kRequestedPresentTime(frameStartTime + 200),
749 kProducerAcquireTime(frameStartTime + 300),
750 kConsumerAcquireTime(frameStartTime + 301),
751 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700752 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700753 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700754 mRefreshes {
755 { mFenceMap, frameStartTime + 410 },
756 { mFenceMap, frameStartTime + 420 },
757 { mFenceMap, frameStartTime + 430 } } {}
758
759 void signalQueueFences() {
760 mFenceMap.signalAllForTest(
761 mAcquireConsumer.mFence, kConsumerAcquireTime);
762 mFenceMap.signalAllForTest(
763 mAcquireProducer.mFence, kProducerAcquireTime);
764 }
765
766 void signalRefreshFences() {
767 for (auto& re : mRefreshes) {
768 re.signalPostCompositeFences();
769 }
770 }
771
772 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700773 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
774 }
775
776 FenceToFenceTimeMap& mFenceMap;
777
778 FenceAndFenceTime mAcquireConsumer { mFenceMap };
779 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700780 FenceAndFenceTime mRelease { mFenceMap };
781
782 const nsecs_t kPostedTime;
783 const nsecs_t kRequestedPresentTime;
784 const nsecs_t kProducerAcquireTime;
785 const nsecs_t kConsumerAcquireTime;
786 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700787 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700788 const nsecs_t kReleaseTime;
789
790 RefreshEvents mRefreshes[3];
791 };
792
Brian Andersond0010582017-03-07 13:20:31 -0800793 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700794
795 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700796 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
797 mFakeConsumer = new FakeConsumer;
798 mCfeh = &mFakeConsumer->mFrameEventHistory;
799 mConsumer->consumerConnect(mFakeConsumer, false);
800 mConsumer->setConsumerName(String8("TestConsumer"));
801 mSurface = new TestSurface(mProducer, &mFenceMap);
802 mWindow = mSurface;
803
804 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
805 NATIVE_WINDOW_API_CPU));
806 native_window_set_buffer_count(mWindow.get(), 4);
807 }
808
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800809 void disableFrameTimestamps() {
810 mFakeConsumer->mGetFrameTimestampsEnabled = false;
811 native_window_enable_frame_timestamps(mWindow.get(), 0);
812 mFrameTimestampsEnabled = false;
813 }
814
Brian Anderson3da8d272016-07-28 16:20:47 -0700815 void enableFrameTimestamps() {
816 mFakeConsumer->mGetFrameTimestampsEnabled = true;
817 native_window_enable_frame_timestamps(mWindow.get(), 1);
818 mFrameTimestampsEnabled = true;
819 }
820
Brian Anderson1049d1d2016-12-16 17:25:57 -0800821 int getAllFrameTimestamps(uint64_t frameId) {
822 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700823 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
824 &outFirstRefreshStartTime, &outLastRefreshStartTime,
825 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700826 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700827 }
828
Brian Anderson3da8d272016-07-28 16:20:47 -0700829 void resetTimestamps() {
830 outRequestedPresentTime = -1;
831 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700832 outLatchTime = -1;
833 outFirstRefreshStartTime = -1;
834 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700835 outGpuCompositionDoneTime = -1;
836 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700837 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700838 outReleaseTime = -1;
839 }
840
Brian Anderson1049d1d2016-12-16 17:25:57 -0800841 uint64_t getNextFrameId() {
842 uint64_t frameId = -1;
843 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
844 EXPECT_EQ(status, NO_ERROR);
845 return frameId;
846 }
847
Brian Anderson3da8d272016-07-28 16:20:47 -0700848 void dequeueAndQueue(uint64_t frameIndex) {
849 int fence = -1;
850 ANativeWindowBuffer* buffer = nullptr;
851 ASSERT_EQ(NO_ERROR,
852 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
853
854 int oldAddFrameTimestampsCount =
855 mFakeConsumer->mAddFrameTimestampsCount;
856
857 FrameEvents* frame = &mFrames[frameIndex];
858 uint64_t frameNumber = frameIndex + 1;
859
860 NewFrameEventsEntry fe;
861 fe.frameNumber = frameNumber;
862 fe.postedTime = frame->kPostedTime;
863 fe.requestedPresentTime = frame->kRequestedPresentTime;
864 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
865 mFakeConsumer->mNewFrameEntryOverride = fe;
866
867 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
868 frame->mAcquireProducer.mFenceTime,
869 frame->mAcquireConsumer.mFenceTime);
870
871 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
872
873 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
874
875 EXPECT_EQ(
876 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
877 mFakeConsumer->mAddFrameTimestampsCount);
878 }
879
880 void addFrameEvents(
881 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
882 FrameEvents* oldFrame =
883 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
884 FrameEvents* newFrame = &mFrames[iNewFrame];
885
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800886 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700887 uint64_t nNewFrame = iNewFrame + 1;
888
Brian Anderson4e606e32017-03-16 15:34:57 -0700889 // Latch, Composite, and Release the frames in a plausible order.
890 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700891 // that's okay for the purposes of this test.
892 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
893
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700894 // Composite the previous frame one more time, which helps verify
895 // LastRefresh is updated properly.
896 if (oldFrame != nullptr) {
897 mCfeh->addPreComposition(nOldFrame,
898 oldFrame->mRefreshes[2].kStartTime);
899 gpuDoneFenceTime = gpuComposited ?
900 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
901 FenceTime::NO_FENCE;
902 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800903 oldFrame->mRefreshes[2].mPresent.mFenceTime,
904 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700905 }
906
907 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700908 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
909
910 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
911 gpuDoneFenceTime = gpuComposited ?
912 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
913 FenceTime::NO_FENCE;
914 // HWC2 releases the previous buffer after a new latch just before
915 // calling postComposition.
916 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700917 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700918 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
919 }
920 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800921 newFrame->mRefreshes[0].mPresent.mFenceTime,
922 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700923
Brian Anderson3da8d272016-07-28 16:20:47 -0700924 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
925 gpuDoneFenceTime = gpuComposited ?
926 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
927 FenceTime::NO_FENCE;
928 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800929 newFrame->mRefreshes[1].mPresent.mFenceTime,
930 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700931 }
932
Brian Anderson3da8d272016-07-28 16:20:47 -0700933 sp<IGraphicBufferProducer> mProducer;
934 sp<IGraphicBufferConsumer> mConsumer;
935 sp<FakeConsumer> mFakeConsumer;
936 ConsumerFrameEventHistory* mCfeh;
937 sp<TestSurface> mSurface;
938 sp<ANativeWindow> mWindow;
939
940 FenceToFenceTimeMap mFenceMap;
941
942 bool mFrameTimestampsEnabled = false;
943
944 int64_t outRequestedPresentTime = -1;
945 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700946 int64_t outLatchTime = -1;
947 int64_t outFirstRefreshStartTime = -1;
948 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700949 int64_t outGpuCompositionDoneTime = -1;
950 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700951 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700952 int64_t outReleaseTime = -1;
953
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800954 FrameEvents mFrames[3] {
955 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700956};
957
958
959// This test verifies that the frame timestamps are not retrieved when not
960// explicitly enabled via native_window_enable_frame_timestamps.
961// We want to check this to make sure there's no overhead for users
962// that don't need the timestamp information.
963TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
964 int fence;
965 ANativeWindowBuffer* buffer;
966
967 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
968 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
969
Brian Anderson1049d1d2016-12-16 17:25:57 -0800970 const uint64_t fId = getNextFrameId();
971
Brian Anderson3da8d272016-07-28 16:20:47 -0700972 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
973 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
974 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
975 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
976
977 // Verify the producer doesn't get frame timestamps piggybacked on queue.
978 // It is okay that frame timestamps are added in the consumer since it is
979 // still needed for SurfaceFlinger dumps.
980 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
981 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
982 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
983
984 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800985 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700986 EXPECT_EQ(INVALID_OPERATION, result);
987 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800988
989 // Verify compositor timing query fails.
990 nsecs_t compositeDeadline = 0;
991 nsecs_t compositeInterval = 0;
992 nsecs_t compositeToPresentLatency = 0;
993 result = native_window_get_compositor_timing(mWindow.get(),
994 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
995 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700996}
997
998// This test verifies that the frame timestamps are retrieved if explicitly
999// enabled via native_window_enable_frame_timestamps.
1000TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001001 CompositorTiming initialCompositorTiming {
1002 1000000000, // 1s deadline
1003 16666667, // 16ms interval
1004 50000000, // 50ms present latency
1005 };
1006 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1007
Brian Anderson3da8d272016-07-28 16:20:47 -07001008 enableFrameTimestamps();
1009
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001010 // Verify the compositor timing query gets the initial compositor values
1011 // after timststamps are enabled; even before the first frame is queued
1012 // or dequeued.
1013 nsecs_t compositeDeadline = 0;
1014 nsecs_t compositeInterval = 0;
1015 nsecs_t compositeToPresentLatency = 0;
1016 mSurface->setNow(initialCompositorTiming.deadline - 1);
1017 int result = native_window_get_compositor_timing(mWindow.get(),
1018 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1019 EXPECT_EQ(NO_ERROR, result);
1020 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1021 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1022 EXPECT_EQ(initialCompositorTiming.presentLatency,
1023 compositeToPresentLatency);
1024
Brian Anderson3da8d272016-07-28 16:20:47 -07001025 int fence;
1026 ANativeWindowBuffer* buffer;
1027
1028 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001029 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001030
Brian Anderson1049d1d2016-12-16 17:25:57 -08001031 const uint64_t fId1 = getNextFrameId();
1032
Brian Anderson3da8d272016-07-28 16:20:47 -07001033 // Verify getFrameTimestamps is piggybacked on dequeue.
1034 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1035 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001036 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001037
1038 NewFrameEventsEntry f1;
1039 f1.frameNumber = 1;
1040 f1.postedTime = mFrames[0].kPostedTime;
1041 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1042 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1043 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1044 mFrames[0].mAcquireProducer.mFenceTime,
1045 mFrames[0].mAcquireConsumer.mFenceTime);
1046 mFakeConsumer->mNewFrameEntryOverride = f1;
1047 mFrames[0].signalQueueFences();
1048
1049 // Verify getFrameTimestamps is piggybacked on queue.
1050 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1051 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1052 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001053 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001054
1055 // Verify queries for timestamps that the producer doesn't know about
1056 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001057 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001058 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001059 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001060}
1061
Brian Anderson6b376712017-04-04 10:51:39 -07001062TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1063 bool displayPresentSupported = true;
1064 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1065
1066 // Verify supported bits are forwarded.
1067 int supportsPresent = -1;
1068 mWindow.get()->query(mWindow.get(),
1069 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1070 EXPECT_EQ(displayPresentSupported, supportsPresent);
1071}
1072
1073TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1074 bool displayPresentSupported = false;
1075 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1076
1077 // Verify supported bits are forwarded.
1078 int supportsPresent = -1;
1079 mWindow.get()->query(mWindow.get(),
1080 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1081 EXPECT_EQ(displayPresentSupported, supportsPresent);
1082}
1083
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001084TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1085 nsecs_t phase = 4000;
1086 nsecs_t interval = 1000;
1087
1088 // Timestamp in previous interval.
1089 nsecs_t timestamp = 3500;
1090 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1091 timestamp, phase, interval));
1092
1093 // Timestamp in next interval.
1094 timestamp = 4500;
1095 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1096 timestamp, phase, interval));
1097
1098 // Timestamp multiple intervals before.
1099 timestamp = 2500;
1100 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1101 timestamp, phase, interval));
1102
1103 // Timestamp multiple intervals after.
1104 timestamp = 6500;
1105 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1106 timestamp, phase, interval));
1107
1108 // Timestamp on previous interval.
1109 timestamp = 3000;
1110 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1111 timestamp, phase, interval));
1112
1113 // Timestamp on next interval.
1114 timestamp = 5000;
1115 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1116 timestamp, phase, interval));
1117
1118 // Timestamp equal to phase.
1119 timestamp = 4000;
1120 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1121 timestamp, phase, interval));
1122}
1123
1124// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1125// if the number of intervals elapsed is internally stored in an int.
1126TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1127 nsecs_t phase = 0;
1128 nsecs_t interval = 4000;
1129 nsecs_t big_timestamp = 8635916564000;
1130 int32_t intervals = big_timestamp / interval;
1131
1132 EXPECT_LT(intervals, 0);
1133 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1134 big_timestamp, phase, interval));
1135 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1136 big_timestamp, big_timestamp, interval));
1137}
1138
1139// This verifies the compositor timing is updated by refresh events
1140// and piggy backed on a queue, dequeue, and enabling of timestamps..
1141TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1142 CompositorTiming initialCompositorTiming {
1143 1000000000, // 1s deadline
1144 16666667, // 16ms interval
1145 50000000, // 50ms present latency
1146 };
1147 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1148
1149 enableFrameTimestamps();
1150
1151 // We get the initial values before any frames are submitted.
1152 nsecs_t compositeDeadline = 0;
1153 nsecs_t compositeInterval = 0;
1154 nsecs_t compositeToPresentLatency = 0;
1155 mSurface->setNow(initialCompositorTiming.deadline - 1);
1156 int result = native_window_get_compositor_timing(mWindow.get(),
1157 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1158 EXPECT_EQ(NO_ERROR, result);
1159 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1160 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1161 EXPECT_EQ(initialCompositorTiming.presentLatency,
1162 compositeToPresentLatency);
1163
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001164 dequeueAndQueue(0);
1165 addFrameEvents(true, NO_FRAME_INDEX, 0);
1166
1167 // Still get the initial values because the frame events for frame 0
1168 // didn't get a chance to piggyback on a queue or dequeue yet.
1169 result = native_window_get_compositor_timing(mWindow.get(),
1170 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1171 EXPECT_EQ(NO_ERROR, result);
1172 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1173 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1174 EXPECT_EQ(initialCompositorTiming.presentLatency,
1175 compositeToPresentLatency);
1176
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001177 dequeueAndQueue(1);
1178 addFrameEvents(true, 0, 1);
1179
1180 // Now expect the composite values associated with frame 1.
1181 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1182 result = native_window_get_compositor_timing(mWindow.get(),
1183 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1184 EXPECT_EQ(NO_ERROR, result);
1185 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1186 compositeDeadline);
1187 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1188 compositeInterval);
1189 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1190 compositeToPresentLatency);
1191
1192 dequeueAndQueue(2);
1193 addFrameEvents(true, 1, 2);
1194
1195 // Now expect the composite values associated with frame 2.
1196 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1197 result = native_window_get_compositor_timing(mWindow.get(),
1198 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1199 EXPECT_EQ(NO_ERROR, result);
1200 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1201 compositeDeadline);
1202 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1203 compositeInterval);
1204 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1205 compositeToPresentLatency);
1206
1207 // Re-enabling frame timestamps should get the latest values.
1208 disableFrameTimestamps();
1209 enableFrameTimestamps();
1210
1211 // Now expect the composite values associated with frame 3.
1212 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1213 result = native_window_get_compositor_timing(mWindow.get(),
1214 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1215 EXPECT_EQ(NO_ERROR, result);
1216 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1217 compositeDeadline);
1218 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1219 compositeInterval);
1220 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1221 compositeToPresentLatency);
1222}
1223
1224// This verifies the compositor deadline properly snaps to the the next
1225// deadline based on the current time.
1226TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1227 CompositorTiming initialCompositorTiming {
1228 1000000000, // 1s deadline
1229 16666667, // 16ms interval
1230 50000000, // 50ms present latency
1231 };
1232 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1233
1234 enableFrameTimestamps();
1235
1236 nsecs_t compositeDeadline = 0;
1237 nsecs_t compositeInterval = 0;
1238 nsecs_t compositeToPresentLatency = 0;
1239
1240 // A "now" just before the deadline snaps to the deadline.
1241 mSurface->setNow(initialCompositorTiming.deadline - 1);
1242 int result = native_window_get_compositor_timing(mWindow.get(),
1243 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1244 EXPECT_EQ(NO_ERROR, result);
1245 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1246 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1247 EXPECT_EQ(expectedDeadline, compositeDeadline);
1248
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001249 dequeueAndQueue(0);
1250 addFrameEvents(true, NO_FRAME_INDEX, 0);
1251
1252 // A "now" just after the deadline snaps properly.
1253 mSurface->setNow(initialCompositorTiming.deadline + 1);
1254 result = native_window_get_compositor_timing(mWindow.get(),
1255 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1256 EXPECT_EQ(NO_ERROR, result);
1257 expectedDeadline =
1258 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1259 EXPECT_EQ(expectedDeadline, compositeDeadline);
1260
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001261 dequeueAndQueue(1);
1262 addFrameEvents(true, 0, 1);
1263
1264 // A "now" just after the next interval snaps properly.
1265 mSurface->setNow(
1266 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1267 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1268 result = native_window_get_compositor_timing(mWindow.get(),
1269 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1270 EXPECT_EQ(NO_ERROR, result);
1271 expectedDeadline =
1272 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1273 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1274 EXPECT_EQ(expectedDeadline, compositeDeadline);
1275
1276 dequeueAndQueue(2);
1277 addFrameEvents(true, 1, 2);
1278
1279 // A "now" over 1 interval before the deadline snaps properly.
1280 mSurface->setNow(
1281 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1282 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1283 result = native_window_get_compositor_timing(mWindow.get(),
1284 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1285 EXPECT_EQ(NO_ERROR, result);
1286 expectedDeadline =
1287 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1288 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1289 EXPECT_EQ(expectedDeadline, compositeDeadline);
1290
1291 // Re-enabling frame timestamps should get the latest values.
1292 disableFrameTimestamps();
1293 enableFrameTimestamps();
1294
1295 // A "now" over 2 intervals before the deadline snaps properly.
1296 mSurface->setNow(
1297 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1298 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1299 result = native_window_get_compositor_timing(mWindow.get(),
1300 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1301 EXPECT_EQ(NO_ERROR, result);
1302 expectedDeadline =
1303 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1304 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1305 EXPECT_EQ(expectedDeadline, compositeDeadline);
1306}
1307
Brian Anderson1049d1d2016-12-16 17:25:57 -08001308// This verifies the timestamps recorded in the consumer's
1309// FrameTimestampsHistory are properly retrieved by the producer for the
1310// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001311TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1312 enableFrameTimestamps();
1313
Brian Anderson1049d1d2016-12-16 17:25:57 -08001314 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001315 dequeueAndQueue(0);
1316 mFrames[0].signalQueueFences();
1317
Brian Anderson1049d1d2016-12-16 17:25:57 -08001318 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001319 dequeueAndQueue(1);
1320 mFrames[1].signalQueueFences();
1321
1322 addFrameEvents(true, NO_FRAME_INDEX, 0);
1323 mFrames[0].signalRefreshFences();
1324 addFrameEvents(true, 0, 1);
1325 mFrames[0].signalReleaseFences();
1326 mFrames[1].signalRefreshFences();
1327
1328 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001329 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001330 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001331 EXPECT_EQ(NO_ERROR, result);
1332 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1333 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001334 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1335 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1336 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001337 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1338 outGpuCompositionDoneTime);
1339 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001340 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001341 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1342
1343 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001344 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001345 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001346 EXPECT_EQ(NO_ERROR, result);
1347 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1348 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001349 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1350 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1351 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001352 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1353 outGpuCompositionDoneTime);
1354 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001355 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1356 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001357}
1358
1359// This test verifies the acquire fence recorded by the consumer is not sent
1360// back to the producer and the producer saves its own fence.
1361TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1362 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001363
Brian Anderson3da8d272016-07-28 16:20:47 -07001364 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001365 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001366 dequeueAndQueue(0);
1367
1368 // Verify queue-related timestamps for f1 are available immediately in the
1369 // producer without asking the consumer again, even before signaling the
1370 // acquire fence.
1371 resetTimestamps();
1372 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001373 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001374 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001375 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001376 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1377 EXPECT_EQ(NO_ERROR, result);
1378 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001379 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001380
1381 // Signal acquire fences. Verify a sync call still isn't necessary.
1382 mFrames[0].signalQueueFences();
1383
1384 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001385 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001386 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001387 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001388 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1389 EXPECT_EQ(NO_ERROR, result);
1390 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1391 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1392
1393 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001394 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001395 dequeueAndQueue(1);
1396
1397 // Verify queue-related timestamps for f2 are available immediately in the
1398 // producer without asking the consumer again, even before signaling the
1399 // acquire fence.
1400 resetTimestamps();
1401 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001402 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001403 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001404 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001405 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1406 EXPECT_EQ(NO_ERROR, result);
1407 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001408 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001409
1410 // Signal acquire fences. Verify a sync call still isn't necessary.
1411 mFrames[1].signalQueueFences();
1412
1413 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001414 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001415 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001416 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001417 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1418 EXPECT_EQ(NO_ERROR, result);
1419 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1420 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1421}
1422
1423TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1424 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001425
1426 // Dequeue and queue frame 1.
1427 dequeueAndQueue(0);
1428 mFrames[0].signalQueueFences();
1429
1430 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001431 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001432 dequeueAndQueue(1);
1433 mFrames[1].signalQueueFences();
1434
1435 addFrameEvents(true, NO_FRAME_INDEX, 0);
1436 mFrames[0].signalRefreshFences();
1437 addFrameEvents(true, 0, 1);
1438 mFrames[0].signalReleaseFences();
1439 mFrames[1].signalRefreshFences();
1440
1441 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001442 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001443 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001444 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1445 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001446 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001447 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1448}
1449
1450// This test verifies that fences can signal and update timestamps producer
1451// side without an additional sync call to the consumer.
1452TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1453 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001454
1455 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001456 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001457 dequeueAndQueue(0);
1458 mFrames[0].signalQueueFences();
1459
1460 // Dequeue and queue frame 2.
1461 dequeueAndQueue(1);
1462 mFrames[1].signalQueueFences();
1463
1464 addFrameEvents(true, NO_FRAME_INDEX, 0);
1465 addFrameEvents(true, 0, 1);
1466
1467 // Verify available timestamps are correct for frame 1, before any
1468 // fence has been signaled.
1469 // Note: A sync call is necessary here since the events triggered by
1470 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001471 resetTimestamps();
1472 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001473 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001474 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1475 EXPECT_EQ(NO_ERROR, result);
1476 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1477 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001478 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1479 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1480 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001481 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1482 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001483 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001484 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001485
1486 // Verify available timestamps are correct for frame 1 again, before any
1487 // fence has been signaled.
1488 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001489 resetTimestamps();
1490 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001491 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001492 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1493 EXPECT_EQ(NO_ERROR, result);
1494 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1495 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001496 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1497 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1498 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001499 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1500 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001501 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001502 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001503
1504 // Signal the fences for frame 1.
1505 mFrames[0].signalRefreshFences();
1506 mFrames[0].signalReleaseFences();
1507
1508 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001509 resetTimestamps();
1510 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001511 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001512 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1513 EXPECT_EQ(NO_ERROR, result);
1514 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1515 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001516 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1517 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1518 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001519 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1520 outGpuCompositionDoneTime);
1521 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001522 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001523 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1524}
1525
1526// This test verifies that if the frame wasn't GPU composited but has a refresh
1527// event a sync call isn't made to get the GPU composite done time since it will
1528// never exist.
1529TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1530 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001531
Brian Anderson3da8d272016-07-28 16:20:47 -07001532 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001533 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001534 dequeueAndQueue(0);
1535 mFrames[0].signalQueueFences();
1536
1537 // Dequeue and queue frame 2.
1538 dequeueAndQueue(1);
1539 mFrames[1].signalQueueFences();
1540
1541 addFrameEvents(false, NO_FRAME_INDEX, 0);
1542 addFrameEvents(false, 0, 1);
1543
1544 // Verify available timestamps are correct for frame 1, before any
1545 // fence has been signaled.
1546 // Note: A sync call is necessary here since the events triggered by
1547 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1548 resetTimestamps();
1549 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001550 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001551 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1552 EXPECT_EQ(NO_ERROR, result);
1553 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1554 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001555 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1556 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1557 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001558 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1559 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001560 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001561 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001562
1563 // Signal the fences for frame 1.
1564 mFrames[0].signalRefreshFences();
1565 mFrames[0].signalReleaseFences();
1566
1567 // Verify all timestamps, except GPU composition, are available without a
1568 // sync call.
1569 resetTimestamps();
1570 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001571 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001572 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1573 EXPECT_EQ(NO_ERROR, result);
1574 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1575 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001576 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1577 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1578 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001579 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001580 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001581 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001582 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1583}
1584
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001585// This test verifies that if the certain timestamps can't possibly exist for
1586// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001587TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001588 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001589
1590 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001591 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001592 dequeueAndQueue(0);
1593 mFrames[0].signalQueueFences();
1594
1595 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001596 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001597 dequeueAndQueue(1);
1598 mFrames[1].signalQueueFences();
1599
1600 addFrameEvents(false, NO_FRAME_INDEX, 0);
1601 addFrameEvents(false, 0, 1);
1602
1603 // Verify available timestamps are correct for frame 1, before any
1604 // fence has been signaled.
1605 // Note: A sync call is necessary here since the events triggered by
1606 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001607 resetTimestamps();
1608 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001609 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001610 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1611 EXPECT_EQ(NO_ERROR, result);
1612 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1613 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001614 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1615 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1616 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001617 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1618 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001619 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001620 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001621
1622 mFrames[0].signalRefreshFences();
1623 mFrames[0].signalReleaseFences();
1624 mFrames[1].signalRefreshFences();
1625
Brian Anderson1049d1d2016-12-16 17:25:57 -08001626 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001627 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001628 // available, a sync call should not occur because it's not possible for f2
1629 // to encounter the final value for those events until another frame is
1630 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001631 resetTimestamps();
1632 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001633 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001634 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1635 EXPECT_EQ(NO_ERROR, result);
1636 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1637 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001638 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1639 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1640 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001641 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001642 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001643 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1644 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001645}
1646
Brian Anderson6b376712017-04-04 10:51:39 -07001647// This test verifies there are no sync calls for present times
1648// when they aren't supported and that an error is returned.
1649
1650TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1651 enableFrameTimestamps();
1652 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1653
1654 // Dequeue and queue frame 1.
1655 const uint64_t fId1 = getNextFrameId();
1656 dequeueAndQueue(0);
1657
1658 // Verify a query for the Present times do not trigger a sync call if they
1659 // are not supported.
1660 resetTimestamps();
1661 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1662 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1663 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1664 &outDisplayPresentTime, nullptr, nullptr);
1665 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1666 EXPECT_EQ(BAD_VALUE, result);
1667 EXPECT_EQ(-1, outDisplayPresentTime);
1668}
1669
Dan Stoza932f0082017-05-31 13:50:16 -07001670} // namespace android