blob: 243f27f8133a43dbb88b40088a93c6b4e1c0e524 [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; }
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 }
Peiyong Lin0256f722018-08-31 15:45:10 -0700625 status_t getCompositionPreference(ui::Dataspace* /*outDataSpace*/,
626 ui::PixelFormat* /*outPixelFormat*/) const override {
627 return NO_ERROR;
628 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700629
630protected:
631 IBinder* onAsBinder() override { return nullptr; }
632
633private:
634 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700635};
636
637class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
638public:
639 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
640 : mFenceMap(fenceMap) {}
641
642 ~FakeProducerFrameEventHistory() {}
643
644 void updateAcquireFence(uint64_t frameNumber,
645 std::shared_ptr<FenceTime>&& acquire) override {
646 // Verify the acquire fence being added isn't the one from the consumer.
647 EXPECT_NE(mConsumerAcquireFence, acquire);
648 // Override the fence, so we can verify this was called by the
649 // producer after the frame is queued.
650 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
651 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
652 }
653
654 void setAcquireFenceOverride(
655 const std::shared_ptr<FenceTime>& acquireFenceOverride,
656 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
657 mAcquireFenceOverride = acquireFenceOverride;
658 mConsumerAcquireFence = consumerAcquireFence;
659 }
660
661protected:
662 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
663 const override {
664 return mFenceMap->createFenceTimeForTest(fence);
665 }
666
667 FenceToFenceTimeMap* mFenceMap{nullptr};
668
669 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
670 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
671};
672
673
674class TestSurface : public Surface {
675public:
676 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
677 FenceToFenceTimeMap* fenceMap)
678 : Surface(bufferProducer),
679 mFakeSurfaceComposer(new FakeSurfaceComposer) {
680 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
681 mFrameEventHistory.reset(mFakeFrameEventHistory);
682 }
683
684 ~TestSurface() override {}
685
686 sp<ISurfaceComposer> composerService() const override {
687 return mFakeSurfaceComposer;
688 }
689
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800690 nsecs_t now() const override {
691 return mNow;
692 }
693
694 void setNow(nsecs_t now) {
695 mNow = now;
696 }
697
Brian Anderson3da8d272016-07-28 16:20:47 -0700698public:
699 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800700 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700701
702 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
703 // but this raw pointer gives access to test functionality.
704 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
705};
706
707
Brian Andersond0010582017-03-07 13:20:31 -0800708class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700709protected:
710 struct FenceAndFenceTime {
711 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
712 : mFence(new Fence),
713 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
714 sp<Fence> mFence { nullptr };
715 std::shared_ptr<FenceTime> mFenceTime { nullptr };
716 };
717
718 struct RefreshEvents {
719 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800720 : mFenceMap(fenceMap),
721 kCompositorTiming(
722 {refreshStart, refreshStart + 1, refreshStart + 2 }),
723 kStartTime(refreshStart + 3),
724 kGpuCompositionDoneTime(refreshStart + 4),
725 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700726
727 void signalPostCompositeFences() {
728 mFenceMap.signalAllForTest(
729 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
730 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
731 }
732
733 FenceToFenceTimeMap& mFenceMap;
734
735 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
736 FenceAndFenceTime mPresent { mFenceMap };
737
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800738 const CompositorTiming kCompositorTiming;
739
Brian Anderson3da8d272016-07-28 16:20:47 -0700740 const nsecs_t kStartTime;
741 const nsecs_t kGpuCompositionDoneTime;
742 const nsecs_t kPresentTime;
743 };
744
745 struct FrameEvents {
746 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
747 : mFenceMap(fenceMap),
748 kPostedTime(frameStartTime + 100),
749 kRequestedPresentTime(frameStartTime + 200),
750 kProducerAcquireTime(frameStartTime + 300),
751 kConsumerAcquireTime(frameStartTime + 301),
752 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700753 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700754 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700755 mRefreshes {
756 { mFenceMap, frameStartTime + 410 },
757 { mFenceMap, frameStartTime + 420 },
758 { mFenceMap, frameStartTime + 430 } } {}
759
760 void signalQueueFences() {
761 mFenceMap.signalAllForTest(
762 mAcquireConsumer.mFence, kConsumerAcquireTime);
763 mFenceMap.signalAllForTest(
764 mAcquireProducer.mFence, kProducerAcquireTime);
765 }
766
767 void signalRefreshFences() {
768 for (auto& re : mRefreshes) {
769 re.signalPostCompositeFences();
770 }
771 }
772
773 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700774 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
775 }
776
777 FenceToFenceTimeMap& mFenceMap;
778
779 FenceAndFenceTime mAcquireConsumer { mFenceMap };
780 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700781 FenceAndFenceTime mRelease { mFenceMap };
782
783 const nsecs_t kPostedTime;
784 const nsecs_t kRequestedPresentTime;
785 const nsecs_t kProducerAcquireTime;
786 const nsecs_t kConsumerAcquireTime;
787 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700788 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700789 const nsecs_t kReleaseTime;
790
791 RefreshEvents mRefreshes[3];
792 };
793
Brian Andersond0010582017-03-07 13:20:31 -0800794 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700795
796 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700797 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
798 mFakeConsumer = new FakeConsumer;
799 mCfeh = &mFakeConsumer->mFrameEventHistory;
800 mConsumer->consumerConnect(mFakeConsumer, false);
801 mConsumer->setConsumerName(String8("TestConsumer"));
802 mSurface = new TestSurface(mProducer, &mFenceMap);
803 mWindow = mSurface;
804
805 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
806 NATIVE_WINDOW_API_CPU));
807 native_window_set_buffer_count(mWindow.get(), 4);
808 }
809
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800810 void disableFrameTimestamps() {
811 mFakeConsumer->mGetFrameTimestampsEnabled = false;
812 native_window_enable_frame_timestamps(mWindow.get(), 0);
813 mFrameTimestampsEnabled = false;
814 }
815
Brian Anderson3da8d272016-07-28 16:20:47 -0700816 void enableFrameTimestamps() {
817 mFakeConsumer->mGetFrameTimestampsEnabled = true;
818 native_window_enable_frame_timestamps(mWindow.get(), 1);
819 mFrameTimestampsEnabled = true;
820 }
821
Brian Anderson1049d1d2016-12-16 17:25:57 -0800822 int getAllFrameTimestamps(uint64_t frameId) {
823 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700824 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
825 &outFirstRefreshStartTime, &outLastRefreshStartTime,
826 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700827 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700828 }
829
Brian Anderson3da8d272016-07-28 16:20:47 -0700830 void resetTimestamps() {
831 outRequestedPresentTime = -1;
832 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700833 outLatchTime = -1;
834 outFirstRefreshStartTime = -1;
835 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700836 outGpuCompositionDoneTime = -1;
837 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700838 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700839 outReleaseTime = -1;
840 }
841
Brian Anderson1049d1d2016-12-16 17:25:57 -0800842 uint64_t getNextFrameId() {
843 uint64_t frameId = -1;
844 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
845 EXPECT_EQ(status, NO_ERROR);
846 return frameId;
847 }
848
Brian Anderson3da8d272016-07-28 16:20:47 -0700849 void dequeueAndQueue(uint64_t frameIndex) {
850 int fence = -1;
851 ANativeWindowBuffer* buffer = nullptr;
852 ASSERT_EQ(NO_ERROR,
853 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
854
855 int oldAddFrameTimestampsCount =
856 mFakeConsumer->mAddFrameTimestampsCount;
857
858 FrameEvents* frame = &mFrames[frameIndex];
859 uint64_t frameNumber = frameIndex + 1;
860
861 NewFrameEventsEntry fe;
862 fe.frameNumber = frameNumber;
863 fe.postedTime = frame->kPostedTime;
864 fe.requestedPresentTime = frame->kRequestedPresentTime;
865 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
866 mFakeConsumer->mNewFrameEntryOverride = fe;
867
868 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
869 frame->mAcquireProducer.mFenceTime,
870 frame->mAcquireConsumer.mFenceTime);
871
872 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
873
874 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
875
876 EXPECT_EQ(
877 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
878 mFakeConsumer->mAddFrameTimestampsCount);
879 }
880
881 void addFrameEvents(
882 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
883 FrameEvents* oldFrame =
884 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
885 FrameEvents* newFrame = &mFrames[iNewFrame];
886
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800887 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700888 uint64_t nNewFrame = iNewFrame + 1;
889
Brian Anderson4e606e32017-03-16 15:34:57 -0700890 // Latch, Composite, and Release the frames in a plausible order.
891 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700892 // that's okay for the purposes of this test.
893 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
894
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700895 // Composite the previous frame one more time, which helps verify
896 // LastRefresh is updated properly.
897 if (oldFrame != nullptr) {
898 mCfeh->addPreComposition(nOldFrame,
899 oldFrame->mRefreshes[2].kStartTime);
900 gpuDoneFenceTime = gpuComposited ?
901 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
902 FenceTime::NO_FENCE;
903 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800904 oldFrame->mRefreshes[2].mPresent.mFenceTime,
905 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700906 }
907
908 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700909 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
910
911 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
912 gpuDoneFenceTime = gpuComposited ?
913 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
914 FenceTime::NO_FENCE;
915 // HWC2 releases the previous buffer after a new latch just before
916 // calling postComposition.
917 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700918 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700919 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
920 }
921 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800922 newFrame->mRefreshes[0].mPresent.mFenceTime,
923 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700924
Brian Anderson3da8d272016-07-28 16:20:47 -0700925 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
926 gpuDoneFenceTime = gpuComposited ?
927 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
928 FenceTime::NO_FENCE;
929 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800930 newFrame->mRefreshes[1].mPresent.mFenceTime,
931 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700932 }
933
Brian Anderson3da8d272016-07-28 16:20:47 -0700934 sp<IGraphicBufferProducer> mProducer;
935 sp<IGraphicBufferConsumer> mConsumer;
936 sp<FakeConsumer> mFakeConsumer;
937 ConsumerFrameEventHistory* mCfeh;
938 sp<TestSurface> mSurface;
939 sp<ANativeWindow> mWindow;
940
941 FenceToFenceTimeMap mFenceMap;
942
943 bool mFrameTimestampsEnabled = false;
944
945 int64_t outRequestedPresentTime = -1;
946 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700947 int64_t outLatchTime = -1;
948 int64_t outFirstRefreshStartTime = -1;
949 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700950 int64_t outGpuCompositionDoneTime = -1;
951 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700952 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700953 int64_t outReleaseTime = -1;
954
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800955 FrameEvents mFrames[3] {
956 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700957};
958
959
960// This test verifies that the frame timestamps are not retrieved when not
961// explicitly enabled via native_window_enable_frame_timestamps.
962// We want to check this to make sure there's no overhead for users
963// that don't need the timestamp information.
964TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
965 int fence;
966 ANativeWindowBuffer* buffer;
967
968 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
969 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
970
Brian Anderson1049d1d2016-12-16 17:25:57 -0800971 const uint64_t fId = getNextFrameId();
972
Brian Anderson3da8d272016-07-28 16:20:47 -0700973 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
974 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
975 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
976 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
977
978 // Verify the producer doesn't get frame timestamps piggybacked on queue.
979 // It is okay that frame timestamps are added in the consumer since it is
980 // still needed for SurfaceFlinger dumps.
981 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
982 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
983 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
984
985 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800986 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700987 EXPECT_EQ(INVALID_OPERATION, result);
988 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800989
990 // Verify compositor timing query fails.
991 nsecs_t compositeDeadline = 0;
992 nsecs_t compositeInterval = 0;
993 nsecs_t compositeToPresentLatency = 0;
994 result = native_window_get_compositor_timing(mWindow.get(),
995 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
996 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700997}
998
999// This test verifies that the frame timestamps are retrieved if explicitly
1000// enabled via native_window_enable_frame_timestamps.
1001TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001002 CompositorTiming initialCompositorTiming {
1003 1000000000, // 1s deadline
1004 16666667, // 16ms interval
1005 50000000, // 50ms present latency
1006 };
1007 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1008
Brian Anderson3da8d272016-07-28 16:20:47 -07001009 enableFrameTimestamps();
1010
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001011 // Verify the compositor timing query gets the initial compositor values
1012 // after timststamps are enabled; even before the first frame is queued
1013 // or dequeued.
1014 nsecs_t compositeDeadline = 0;
1015 nsecs_t compositeInterval = 0;
1016 nsecs_t compositeToPresentLatency = 0;
1017 mSurface->setNow(initialCompositorTiming.deadline - 1);
1018 int result = native_window_get_compositor_timing(mWindow.get(),
1019 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1020 EXPECT_EQ(NO_ERROR, result);
1021 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1022 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1023 EXPECT_EQ(initialCompositorTiming.presentLatency,
1024 compositeToPresentLatency);
1025
Brian Anderson3da8d272016-07-28 16:20:47 -07001026 int fence;
1027 ANativeWindowBuffer* buffer;
1028
1029 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001030 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001031
Brian Anderson1049d1d2016-12-16 17:25:57 -08001032 const uint64_t fId1 = getNextFrameId();
1033
Brian Anderson3da8d272016-07-28 16:20:47 -07001034 // Verify getFrameTimestamps is piggybacked on dequeue.
1035 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1036 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001037 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001038
1039 NewFrameEventsEntry f1;
1040 f1.frameNumber = 1;
1041 f1.postedTime = mFrames[0].kPostedTime;
1042 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1043 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1044 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1045 mFrames[0].mAcquireProducer.mFenceTime,
1046 mFrames[0].mAcquireConsumer.mFenceTime);
1047 mFakeConsumer->mNewFrameEntryOverride = f1;
1048 mFrames[0].signalQueueFences();
1049
1050 // Verify getFrameTimestamps is piggybacked on queue.
1051 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1052 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1053 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001054 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001055
1056 // Verify queries for timestamps that the producer doesn't know about
1057 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001058 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001059 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001060 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001061}
1062
Brian Anderson6b376712017-04-04 10:51:39 -07001063TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1064 bool displayPresentSupported = true;
1065 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1066
1067 // Verify supported bits are forwarded.
1068 int supportsPresent = -1;
1069 mWindow.get()->query(mWindow.get(),
1070 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1071 EXPECT_EQ(displayPresentSupported, supportsPresent);
1072}
1073
1074TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1075 bool displayPresentSupported = false;
1076 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1077
1078 // Verify supported bits are forwarded.
1079 int supportsPresent = -1;
1080 mWindow.get()->query(mWindow.get(),
1081 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1082 EXPECT_EQ(displayPresentSupported, supportsPresent);
1083}
1084
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001085TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1086 nsecs_t phase = 4000;
1087 nsecs_t interval = 1000;
1088
1089 // Timestamp in previous interval.
1090 nsecs_t timestamp = 3500;
1091 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1092 timestamp, phase, interval));
1093
1094 // Timestamp in next interval.
1095 timestamp = 4500;
1096 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1097 timestamp, phase, interval));
1098
1099 // Timestamp multiple intervals before.
1100 timestamp = 2500;
1101 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1102 timestamp, phase, interval));
1103
1104 // Timestamp multiple intervals after.
1105 timestamp = 6500;
1106 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1107 timestamp, phase, interval));
1108
1109 // Timestamp on previous interval.
1110 timestamp = 3000;
1111 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1112 timestamp, phase, interval));
1113
1114 // Timestamp on next interval.
1115 timestamp = 5000;
1116 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1117 timestamp, phase, interval));
1118
1119 // Timestamp equal to phase.
1120 timestamp = 4000;
1121 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1122 timestamp, phase, interval));
1123}
1124
1125// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1126// if the number of intervals elapsed is internally stored in an int.
1127TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1128 nsecs_t phase = 0;
1129 nsecs_t interval = 4000;
1130 nsecs_t big_timestamp = 8635916564000;
1131 int32_t intervals = big_timestamp / interval;
1132
1133 EXPECT_LT(intervals, 0);
1134 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1135 big_timestamp, phase, interval));
1136 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1137 big_timestamp, big_timestamp, interval));
1138}
1139
1140// This verifies the compositor timing is updated by refresh events
1141// and piggy backed on a queue, dequeue, and enabling of timestamps..
1142TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1143 CompositorTiming initialCompositorTiming {
1144 1000000000, // 1s deadline
1145 16666667, // 16ms interval
1146 50000000, // 50ms present latency
1147 };
1148 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1149
1150 enableFrameTimestamps();
1151
1152 // We get the initial values before any frames are submitted.
1153 nsecs_t compositeDeadline = 0;
1154 nsecs_t compositeInterval = 0;
1155 nsecs_t compositeToPresentLatency = 0;
1156 mSurface->setNow(initialCompositorTiming.deadline - 1);
1157 int result = native_window_get_compositor_timing(mWindow.get(),
1158 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1159 EXPECT_EQ(NO_ERROR, result);
1160 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1161 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1162 EXPECT_EQ(initialCompositorTiming.presentLatency,
1163 compositeToPresentLatency);
1164
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001165 dequeueAndQueue(0);
1166 addFrameEvents(true, NO_FRAME_INDEX, 0);
1167
1168 // Still get the initial values because the frame events for frame 0
1169 // didn't get a chance to piggyback on a queue or dequeue yet.
1170 result = native_window_get_compositor_timing(mWindow.get(),
1171 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1172 EXPECT_EQ(NO_ERROR, result);
1173 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1174 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1175 EXPECT_EQ(initialCompositorTiming.presentLatency,
1176 compositeToPresentLatency);
1177
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001178 dequeueAndQueue(1);
1179 addFrameEvents(true, 0, 1);
1180
1181 // Now expect the composite values associated with frame 1.
1182 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1183 result = native_window_get_compositor_timing(mWindow.get(),
1184 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1185 EXPECT_EQ(NO_ERROR, result);
1186 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1187 compositeDeadline);
1188 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1189 compositeInterval);
1190 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1191 compositeToPresentLatency);
1192
1193 dequeueAndQueue(2);
1194 addFrameEvents(true, 1, 2);
1195
1196 // Now expect the composite values associated with frame 2.
1197 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1198 result = native_window_get_compositor_timing(mWindow.get(),
1199 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1200 EXPECT_EQ(NO_ERROR, result);
1201 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1202 compositeDeadline);
1203 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1204 compositeInterval);
1205 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1206 compositeToPresentLatency);
1207
1208 // Re-enabling frame timestamps should get the latest values.
1209 disableFrameTimestamps();
1210 enableFrameTimestamps();
1211
1212 // Now expect the composite values associated with frame 3.
1213 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1214 result = native_window_get_compositor_timing(mWindow.get(),
1215 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1216 EXPECT_EQ(NO_ERROR, result);
1217 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1218 compositeDeadline);
1219 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1220 compositeInterval);
1221 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1222 compositeToPresentLatency);
1223}
1224
1225// This verifies the compositor deadline properly snaps to the the next
1226// deadline based on the current time.
1227TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1228 CompositorTiming initialCompositorTiming {
1229 1000000000, // 1s deadline
1230 16666667, // 16ms interval
1231 50000000, // 50ms present latency
1232 };
1233 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1234
1235 enableFrameTimestamps();
1236
1237 nsecs_t compositeDeadline = 0;
1238 nsecs_t compositeInterval = 0;
1239 nsecs_t compositeToPresentLatency = 0;
1240
1241 // A "now" just before the deadline snaps to the deadline.
1242 mSurface->setNow(initialCompositorTiming.deadline - 1);
1243 int result = native_window_get_compositor_timing(mWindow.get(),
1244 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1245 EXPECT_EQ(NO_ERROR, result);
1246 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1247 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1248 EXPECT_EQ(expectedDeadline, compositeDeadline);
1249
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001250 dequeueAndQueue(0);
1251 addFrameEvents(true, NO_FRAME_INDEX, 0);
1252
1253 // A "now" just after the deadline snaps properly.
1254 mSurface->setNow(initialCompositorTiming.deadline + 1);
1255 result = native_window_get_compositor_timing(mWindow.get(),
1256 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1257 EXPECT_EQ(NO_ERROR, result);
1258 expectedDeadline =
1259 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1260 EXPECT_EQ(expectedDeadline, compositeDeadline);
1261
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001262 dequeueAndQueue(1);
1263 addFrameEvents(true, 0, 1);
1264
1265 // A "now" just after the next interval snaps properly.
1266 mSurface->setNow(
1267 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1268 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1269 result = native_window_get_compositor_timing(mWindow.get(),
1270 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1271 EXPECT_EQ(NO_ERROR, result);
1272 expectedDeadline =
1273 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1274 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1275 EXPECT_EQ(expectedDeadline, compositeDeadline);
1276
1277 dequeueAndQueue(2);
1278 addFrameEvents(true, 1, 2);
1279
1280 // A "now" over 1 interval before the deadline snaps properly.
1281 mSurface->setNow(
1282 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1283 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1284 result = native_window_get_compositor_timing(mWindow.get(),
1285 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1286 EXPECT_EQ(NO_ERROR, result);
1287 expectedDeadline =
1288 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1289 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1290 EXPECT_EQ(expectedDeadline, compositeDeadline);
1291
1292 // Re-enabling frame timestamps should get the latest values.
1293 disableFrameTimestamps();
1294 enableFrameTimestamps();
1295
1296 // A "now" over 2 intervals before the deadline snaps properly.
1297 mSurface->setNow(
1298 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1299 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1300 result = native_window_get_compositor_timing(mWindow.get(),
1301 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1302 EXPECT_EQ(NO_ERROR, result);
1303 expectedDeadline =
1304 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1305 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1306 EXPECT_EQ(expectedDeadline, compositeDeadline);
1307}
1308
Brian Anderson1049d1d2016-12-16 17:25:57 -08001309// This verifies the timestamps recorded in the consumer's
1310// FrameTimestampsHistory are properly retrieved by the producer for the
1311// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001312TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1313 enableFrameTimestamps();
1314
Brian Anderson1049d1d2016-12-16 17:25:57 -08001315 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001316 dequeueAndQueue(0);
1317 mFrames[0].signalQueueFences();
1318
Brian Anderson1049d1d2016-12-16 17:25:57 -08001319 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001320 dequeueAndQueue(1);
1321 mFrames[1].signalQueueFences();
1322
1323 addFrameEvents(true, NO_FRAME_INDEX, 0);
1324 mFrames[0].signalRefreshFences();
1325 addFrameEvents(true, 0, 1);
1326 mFrames[0].signalReleaseFences();
1327 mFrames[1].signalRefreshFences();
1328
1329 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001330 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001331 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001332 EXPECT_EQ(NO_ERROR, result);
1333 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1334 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001335 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1336 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1337 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001338 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1339 outGpuCompositionDoneTime);
1340 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001341 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001342 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1343
1344 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001345 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001346 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001347 EXPECT_EQ(NO_ERROR, result);
1348 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1349 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001350 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1351 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1352 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001353 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1354 outGpuCompositionDoneTime);
1355 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001356 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1357 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001358}
1359
1360// This test verifies the acquire fence recorded by the consumer is not sent
1361// back to the producer and the producer saves its own fence.
1362TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1363 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001364
Brian Anderson3da8d272016-07-28 16:20:47 -07001365 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001366 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001367 dequeueAndQueue(0);
1368
1369 // Verify queue-related timestamps for f1 are available immediately in the
1370 // producer without asking the consumer again, even before signaling the
1371 // acquire fence.
1372 resetTimestamps();
1373 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001374 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001375 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001376 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001377 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1378 EXPECT_EQ(NO_ERROR, result);
1379 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001380 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001381
1382 // Signal acquire fences. Verify a sync call still isn't necessary.
1383 mFrames[0].signalQueueFences();
1384
1385 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001386 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001387 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001388 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001389 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1390 EXPECT_EQ(NO_ERROR, result);
1391 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1392 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1393
1394 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001395 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001396 dequeueAndQueue(1);
1397
1398 // Verify queue-related timestamps for f2 are available immediately in the
1399 // producer without asking the consumer again, even before signaling the
1400 // acquire fence.
1401 resetTimestamps();
1402 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001403 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001404 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001405 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001406 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1407 EXPECT_EQ(NO_ERROR, result);
1408 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001409 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001410
1411 // Signal acquire fences. Verify a sync call still isn't necessary.
1412 mFrames[1].signalQueueFences();
1413
1414 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001415 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001416 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001417 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001418 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1419 EXPECT_EQ(NO_ERROR, result);
1420 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1421 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1422}
1423
1424TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1425 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001426
1427 // Dequeue and queue frame 1.
1428 dequeueAndQueue(0);
1429 mFrames[0].signalQueueFences();
1430
1431 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001432 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001433 dequeueAndQueue(1);
1434 mFrames[1].signalQueueFences();
1435
1436 addFrameEvents(true, NO_FRAME_INDEX, 0);
1437 mFrames[0].signalRefreshFences();
1438 addFrameEvents(true, 0, 1);
1439 mFrames[0].signalReleaseFences();
1440 mFrames[1].signalRefreshFences();
1441
1442 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001443 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001444 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001445 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1446 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001447 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001448 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1449}
1450
1451// This test verifies that fences can signal and update timestamps producer
1452// side without an additional sync call to the consumer.
1453TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1454 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001455
1456 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001457 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001458 dequeueAndQueue(0);
1459 mFrames[0].signalQueueFences();
1460
1461 // Dequeue and queue frame 2.
1462 dequeueAndQueue(1);
1463 mFrames[1].signalQueueFences();
1464
1465 addFrameEvents(true, NO_FRAME_INDEX, 0);
1466 addFrameEvents(true, 0, 1);
1467
1468 // Verify available timestamps are correct for frame 1, before any
1469 // fence has been signaled.
1470 // Note: A sync call is necessary here since the events triggered by
1471 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001472 resetTimestamps();
1473 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001474 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001475 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1476 EXPECT_EQ(NO_ERROR, result);
1477 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1478 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001479 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1480 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1481 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001482 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1483 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001484 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001485 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001486
1487 // Verify available timestamps are correct for frame 1 again, before any
1488 // fence has been signaled.
1489 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001490 resetTimestamps();
1491 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001492 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001493 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1494 EXPECT_EQ(NO_ERROR, result);
1495 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1496 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001497 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1498 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1499 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001500 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1501 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001502 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001503 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001504
1505 // Signal the fences for frame 1.
1506 mFrames[0].signalRefreshFences();
1507 mFrames[0].signalReleaseFences();
1508
1509 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001510 resetTimestamps();
1511 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001512 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001513 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1514 EXPECT_EQ(NO_ERROR, result);
1515 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1516 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001517 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1518 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1519 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001520 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1521 outGpuCompositionDoneTime);
1522 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001523 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001524 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1525}
1526
1527// This test verifies that if the frame wasn't GPU composited but has a refresh
1528// event a sync call isn't made to get the GPU composite done time since it will
1529// never exist.
1530TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1531 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001532
Brian Anderson3da8d272016-07-28 16:20:47 -07001533 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001534 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001535 dequeueAndQueue(0);
1536 mFrames[0].signalQueueFences();
1537
1538 // Dequeue and queue frame 2.
1539 dequeueAndQueue(1);
1540 mFrames[1].signalQueueFences();
1541
1542 addFrameEvents(false, NO_FRAME_INDEX, 0);
1543 addFrameEvents(false, 0, 1);
1544
1545 // Verify available timestamps are correct for frame 1, before any
1546 // fence has been signaled.
1547 // Note: A sync call is necessary here since the events triggered by
1548 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1549 resetTimestamps();
1550 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001551 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001552 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1553 EXPECT_EQ(NO_ERROR, result);
1554 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1555 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001556 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1557 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1558 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001559 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1560 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001561 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001562 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001563
1564 // Signal the fences for frame 1.
1565 mFrames[0].signalRefreshFences();
1566 mFrames[0].signalReleaseFences();
1567
1568 // Verify all timestamps, except GPU composition, are available without a
1569 // sync call.
1570 resetTimestamps();
1571 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001572 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001573 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1574 EXPECT_EQ(NO_ERROR, result);
1575 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1576 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001577 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1578 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1579 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001580 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001581 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001582 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001583 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1584}
1585
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001586// This test verifies that if the certain timestamps can't possibly exist for
1587// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001588TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001589 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001590
1591 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001592 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001593 dequeueAndQueue(0);
1594 mFrames[0].signalQueueFences();
1595
1596 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001597 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001598 dequeueAndQueue(1);
1599 mFrames[1].signalQueueFences();
1600
1601 addFrameEvents(false, NO_FRAME_INDEX, 0);
1602 addFrameEvents(false, 0, 1);
1603
1604 // Verify available timestamps are correct for frame 1, before any
1605 // fence has been signaled.
1606 // Note: A sync call is necessary here since the events triggered by
1607 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001608 resetTimestamps();
1609 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001610 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001611 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1612 EXPECT_EQ(NO_ERROR, result);
1613 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1614 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001615 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1616 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1617 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001618 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1619 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001620 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001621 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001622
1623 mFrames[0].signalRefreshFences();
1624 mFrames[0].signalReleaseFences();
1625 mFrames[1].signalRefreshFences();
1626
Brian Anderson1049d1d2016-12-16 17:25:57 -08001627 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001628 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001629 // available, a sync call should not occur because it's not possible for f2
1630 // to encounter the final value for those events until another frame is
1631 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001632 resetTimestamps();
1633 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001634 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001635 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1636 EXPECT_EQ(NO_ERROR, result);
1637 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1638 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001639 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1640 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1641 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001642 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001643 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001644 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1645 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001646}
1647
Brian Anderson6b376712017-04-04 10:51:39 -07001648// This test verifies there are no sync calls for present times
1649// when they aren't supported and that an error is returned.
1650
1651TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1652 enableFrameTimestamps();
1653 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1654
1655 // Dequeue and queue frame 1.
1656 const uint64_t fId1 = getNextFrameId();
1657 dequeueAndQueue(0);
1658
1659 // Verify a query for the Present times do not trigger a sync call if they
1660 // are not supported.
1661 resetTimestamps();
1662 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1663 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1664 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1665 &outDisplayPresentTime, nullptr, nullptr);
1666 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1667 EXPECT_EQ(BAD_VALUE, result);
1668 EXPECT_EQ(-1, outDisplayPresentTime);
1669}
1670
Dan Stoza932f0082017-05-31 13:50:16 -07001671} // namespace android