blob: 500df055c2efa36a21b06383acf77cbfc350207c [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;
Peiyong Lin0e003c92018-09-17 11:09:51 -0700137 ASSERT_EQ(NO_ERROR,
138 sf->captureScreen(display, &outBuffer, ui::Dataspace::V0_SRGB,
139 ui::PixelFormat::RGBA_8888, Rect(), 64, 64, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800140
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700141 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
142 NATIVE_WINDOW_API_CPU));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800143 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
144 // that we need to dequeue a buffer in order for it to actually get
145 // allocated in SurfaceFlinger.
146 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
147 GRALLOC_USAGE_PROTECTED));
148 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Yi Konga03e0442018-07-17 11:16:57 -0700149 ANativeWindowBuffer* buf = nullptr;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700150
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700151 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700152 if (err) {
153 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
154 // that's okay as long as this is the reason for the failure.
155 // try again without the GRALLOC_USAGE_PROTECTED bit.
156 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700157 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
158 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700159 return;
160 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700161 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700162
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800163 for (int i = 0; i < 4; i++) {
164 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700165 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
166 &buf));
167 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800168 }
Peiyong Lin0e003c92018-09-17 11:09:51 -0700169 ASSERT_EQ(NO_ERROR,
170 sf->captureScreen(display, &outBuffer, ui::Dataspace::V0_SRGB,
171 ui::PixelFormat::RGBA_8888, Rect(), 64, 64, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800172}
173
Jamie Gennis391bbe22011-03-14 15:00:06 -0700174TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
175 sp<ANativeWindow> anw(mSurface);
176 int result = -123;
177 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
178 EXPECT_EQ(NO_ERROR, err);
179 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
180}
181
Craig Donner6ebc46a2016-10-21 15:23:44 -0700182TEST_F(SurfaceTest, LayerCountIsOne) {
183 sp<ANativeWindow> anw(mSurface);
184 int result = -123;
185 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
186 EXPECT_EQ(NO_ERROR, err);
187 EXPECT_EQ(1, result);
188}
189
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700190TEST_F(SurfaceTest, QueryConsumerUsage) {
191 const int TEST_USAGE_FLAGS =
192 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700193 sp<IGraphicBufferProducer> producer;
194 sp<IGraphicBufferConsumer> consumer;
195 BufferQueue::createBufferQueue(&producer, &consumer);
196 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700197 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700198 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700199
200 sp<ANativeWindow> anw(s);
201
202 int flags = -1;
203 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
204
205 ASSERT_EQ(NO_ERROR, err);
206 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
207}
208
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800209TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
210 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
211 sp<IGraphicBufferProducer> producer;
212 sp<IGraphicBufferConsumer> consumer;
213 BufferQueue::createBufferQueue(&producer, &consumer);
214 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
215
216 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
217
218 sp<Surface> s = new Surface(producer);
219
220 sp<ANativeWindow> anw(s);
221
222 android_dataspace dataSpace;
223
224 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
225 reinterpret_cast<int*>(&dataSpace));
226
227 ASSERT_EQ(NO_ERROR, err);
228 ASSERT_EQ(TEST_DATASPACE, dataSpace);
229}
230
Dan Stoza812ed062015-06-02 15:45:22 -0700231TEST_F(SurfaceTest, SettingGenerationNumber) {
232 sp<IGraphicBufferProducer> producer;
233 sp<IGraphicBufferConsumer> consumer;
234 BufferQueue::createBufferQueue(&producer, &consumer);
235 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
236 sp<Surface> surface = new Surface(producer);
237 sp<ANativeWindow> window(surface);
238
239 // Allocate a buffer with a generation number of 0
240 ANativeWindowBuffer* buffer;
241 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700242 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
243 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700244 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
245 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
246
247 // Detach the buffer and check its generation number
248 sp<GraphicBuffer> graphicBuffer;
249 sp<Fence> fence;
250 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
251 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
252
253 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
254 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
255
256 // This should change the generation number of the GraphicBuffer
257 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
258
259 // Check that the new generation number sticks with the buffer
260 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
261 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
262 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
263 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
264}
265
Dan Stozac6f30bd2015-06-08 09:32:50 -0700266TEST_F(SurfaceTest, GetConsumerName) {
267 sp<IGraphicBufferProducer> producer;
268 sp<IGraphicBufferConsumer> consumer;
269 BufferQueue::createBufferQueue(&producer, &consumer);
270
271 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
272 consumer->consumerConnect(dummyConsumer, false);
273 consumer->setConsumerName(String8("TestConsumer"));
274
275 sp<Surface> surface = new Surface(producer);
276 sp<ANativeWindow> window(surface);
277 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
278
279 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
280}
281
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700282TEST_F(SurfaceTest, GetWideColorSupport) {
283 sp<IGraphicBufferProducer> producer;
284 sp<IGraphicBufferConsumer> consumer;
285 BufferQueue::createBufferQueue(&producer, &consumer);
286
287 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
288 consumer->consumerConnect(dummyConsumer, false);
289 consumer->setConsumerName(String8("TestConsumer"));
290
291 sp<Surface> surface = new Surface(producer);
292 sp<ANativeWindow> window(surface);
293 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
294
295 bool supported;
296 surface->getWideColorSupport(&supported);
297
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -0600298 // NOTE: This test assumes that device that supports
299 // wide-color (as indicated by BoardConfig) must also
300 // have a wide-color primary display.
301 // That assumption allows this test to cover devices
302 // that advertised a wide-color color mode without
303 // actually supporting wide-color to pass this test
304 // as well as the case of a device that does support
305 // wide-color (via BoardConfig) and has a wide-color
306 // primary display.
307 // NOT covered at this time is a device that supports
308 // wide color in the BoardConfig but does not support
309 // a wide-color color mode on the primary display.
310 ASSERT_EQ(hasWideColorDisplay, supported);
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700311}
312
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700313TEST_F(SurfaceTest, GetHdrSupport) {
314 sp<IGraphicBufferProducer> producer;
315 sp<IGraphicBufferConsumer> consumer;
316 BufferQueue::createBufferQueue(&producer, &consumer);
317
318 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
319 consumer->consumerConnect(dummyConsumer, false);
320 consumer->setConsumerName(String8("TestConsumer"));
321
322 sp<Surface> surface = new Surface(producer);
323 sp<ANativeWindow> window(surface);
324 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
325
326 bool supported;
327 status_t result = surface->getHdrSupport(&supported);
328 ASSERT_EQ(NO_ERROR, result);
329
330 // NOTE: This is not a CTS test.
331 // This test verifies that when the BoardConfig TARGET_HAS_HDR_DISPLAY
332 // is TRUE, getHdrSupport is also true.
333 // TODO: Add check for an HDR color mode on the primary display.
334 ASSERT_EQ(hasHdrDisplay, supported);
335}
336
337TEST_F(SurfaceTest, SetHdrMetadata) {
338 sp<IGraphicBufferProducer> producer;
339 sp<IGraphicBufferConsumer> consumer;
340 BufferQueue::createBufferQueue(&producer, &consumer);
341
342 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
343 consumer->consumerConnect(dummyConsumer, false);
344 consumer->setConsumerName(String8("TestConsumer"));
345
346 sp<Surface> surface = new Surface(producer);
347 sp<ANativeWindow> window(surface);
348 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
349
350 bool supported;
351 status_t result = surface->getHdrSupport(&supported);
352 ASSERT_EQ(NO_ERROR, result);
353
354 if (!hasHdrDisplay || !supported) {
355 return;
356 }
357 const android_smpte2086_metadata smpte2086 = {
358 {0.680, 0.320},
359 {0.265, 0.690},
360 {0.150, 0.060},
361 {0.3127, 0.3290},
362 100.0,
363 0.1,
364 };
365 const android_cta861_3_metadata cta861_3 = {
366 78.0,
367 62.0,
368 };
369 int error = native_window_set_buffers_smpte2086_metadata(window.get(), &smpte2086);
370 ASSERT_EQ(error, NO_ERROR);
371 error = native_window_set_buffers_cta861_3_metadata(window.get(), &cta861_3);
372 ASSERT_EQ(error, NO_ERROR);
373}
374
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800375TEST_F(SurfaceTest, DynamicSetBufferCount) {
376 sp<IGraphicBufferProducer> producer;
377 sp<IGraphicBufferConsumer> consumer;
378 BufferQueue::createBufferQueue(&producer, &consumer);
379
380 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
381 consumer->consumerConnect(dummyConsumer, false);
382 consumer->setConsumerName(String8("TestConsumer"));
383
384 sp<Surface> surface = new Surface(producer);
385 sp<ANativeWindow> window(surface);
386
387 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
388 NATIVE_WINDOW_API_CPU));
389 native_window_set_buffer_count(window.get(), 4);
390
391 int fence;
392 ANativeWindowBuffer* buffer;
393 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
394 native_window_set_buffer_count(window.get(), 3);
395 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
396 native_window_set_buffer_count(window.get(), 2);
397 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
398 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
399}
400
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700401TEST_F(SurfaceTest, GetAndFlushRemovedBuffers) {
402 sp<IGraphicBufferProducer> producer;
403 sp<IGraphicBufferConsumer> consumer;
404 BufferQueue::createBufferQueue(&producer, &consumer);
405
406 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
407 consumer->consumerConnect(dummyConsumer, false);
408 consumer->setConsumerName(String8("TestConsumer"));
409
410 sp<Surface> surface = new Surface(producer);
411 sp<ANativeWindow> window(surface);
412 sp<DummyProducerListener> listener = new DummyProducerListener();
413 ASSERT_EQ(OK, surface->connect(
414 NATIVE_WINDOW_API_CPU,
415 /*listener*/listener,
416 /*reportBufferRemoval*/true));
417 const int BUFFER_COUNT = 4;
418 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
419
420 sp<GraphicBuffer> detachedBuffer;
421 sp<Fence> outFence;
422 int fences[BUFFER_COUNT];
423 ANativeWindowBuffer* buffers[BUFFER_COUNT];
424 // Allocate buffers because detachNextBuffer requires allocated buffers
425 for (int i = 0; i < BUFFER_COUNT; i++) {
426 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
427 }
428 for (int i = 0; i < BUFFER_COUNT; i++) {
429 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
430 }
431
432 // Test detached buffer is correctly reported
433 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
434 std::vector<sp<GraphicBuffer>> removedBuffers;
435 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
436 ASSERT_EQ(1u, removedBuffers.size());
437 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
438 // Test the list is flushed one getAndFlushRemovedBuffers returns
439 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
440 ASSERT_EQ(0u, removedBuffers.size());
441
442
443 // Test removed buffer list is cleanup after next dequeueBuffer call
444 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
445 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[0], &fences[0]));
446 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
447 ASSERT_EQ(0u, removedBuffers.size());
448 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[0], fences[0]));
449
450 // Test removed buffer list is cleanup after next detachNextBuffer call
451 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
452 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
453 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
454 ASSERT_EQ(1u, removedBuffers.size());
455 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
456
457 // Re-allocate buffers since all buffers are detached up to now
458 for (int i = 0; i < BUFFER_COUNT; i++) {
459 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
460 }
461 for (int i = 0; i < BUFFER_COUNT; i++) {
462 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
463 }
464
465 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
466 ASSERT_EQ(NO_ERROR, surface->attachBuffer(detachedBuffer.get()));
467 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
468 // Depends on which slot GraphicBufferProducer impl pick, the attach call might
469 // get 0 or 1 buffer removed.
470 ASSERT_LE(removedBuffers.size(), 1u);
471}
Brian Anderson3da8d272016-07-28 16:20:47 -0700472
Dan Stoza932f0082017-05-31 13:50:16 -0700473TEST_F(SurfaceTest, TestGetLastDequeueStartTime) {
474 sp<ANativeWindow> anw(mSurface);
475 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU));
476
477 ANativeWindowBuffer* buffer = nullptr;
478 int32_t fenceFd = -1;
479
480 nsecs_t before = systemTime(CLOCK_MONOTONIC);
481 anw->dequeueBuffer(anw.get(), &buffer, &fenceFd);
482 nsecs_t after = systemTime(CLOCK_MONOTONIC);
483
484 nsecs_t lastDequeueTime = mSurface->getLastDequeueStartTime();
485 ASSERT_LE(before, lastDequeueTime);
486 ASSERT_GE(after, lastDequeueTime);
487}
488
Brian Anderson3da8d272016-07-28 16:20:47 -0700489class FakeConsumer : public BnConsumerListener {
490public:
491 void onFrameAvailable(const BufferItem& /*item*/) override {}
492 void onBuffersReleased() override {}
493 void onSidebandStreamChanged() override {}
494
495 void addAndGetFrameTimestamps(
496 const NewFrameEventsEntry* newTimestamps,
497 FrameEventHistoryDelta* outDelta) override {
498 if (newTimestamps) {
499 if (mGetFrameTimestampsEnabled) {
500 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
501 "Test should set mNewFrameEntryOverride before queuing "
502 "a frame.";
503 EXPECT_EQ(newTimestamps->frameNumber,
504 mNewFrameEntryOverride.frameNumber) <<
505 "Test attempting to add NewFrameEntryOverride with "
506 "incorrect frame number.";
507 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
508 mNewFrameEntryOverride.frameNumber = 0;
509 }
510 mAddFrameTimestampsCount++;
511 mLastAddedFrameNumber = newTimestamps->frameNumber;
512 }
513 if (outDelta) {
514 mFrameEventHistory.getAndResetDelta(outDelta);
515 mGetFrameTimestampsCount++;
516 }
517 mAddAndGetFrameTimestampsCallCount++;
518 }
519
520 bool mGetFrameTimestampsEnabled = false;
521
522 ConsumerFrameEventHistory mFrameEventHistory;
523 int mAddAndGetFrameTimestampsCallCount = 0;
524 int mAddFrameTimestampsCount = 0;
525 int mGetFrameTimestampsCount = 0;
526 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
527
528 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
529};
530
531
532class FakeSurfaceComposer : public ISurfaceComposer{
533public:
534 ~FakeSurfaceComposer() override {}
535
Brian Anderson6b376712017-04-04 10:51:39 -0700536 void setSupportsPresent(bool supportsPresent) {
537 mSupportsPresent = supportsPresent;
538 }
539
Brian Anderson3da8d272016-07-28 16:20:47 -0700540 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800541 sp<ISurfaceComposerClient> createScopedConnection(
542 const sp<IGraphicBufferProducer>& /* parent */) override {
543 return nullptr;
544 }
Jorim Jaggib1e2f8d2017-06-08 15:43:59 -0700545 sp<IDisplayEventConnection> createDisplayEventConnection(ISurfaceComposer::VsyncSource)
546 override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700547 return nullptr;
548 }
549 sp<IBinder> createDisplay(const String8& /*displayName*/,
550 bool /*secure*/) override { return nullptr; }
551 void destroyDisplay(const sp<IBinder>& /*display */) override {}
552 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
553 void setTransactionState(const Vector<ComposerState>& /*state*/,
554 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
555 override {}
556 void bootFinished() override {}
557 bool authenticateSurfaceTexture(
558 const sp<IGraphicBufferProducer>& /*surface*/) const override {
559 return false;
560 }
Brian Anderson6b376712017-04-04 10:51:39 -0700561
562 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
563 const override {
564 *outSupported = {
565 FrameEvent::REQUESTED_PRESENT,
566 FrameEvent::ACQUIRE,
567 FrameEvent::LATCH,
568 FrameEvent::FIRST_REFRESH_START,
569 FrameEvent::LAST_REFRESH_START,
570 FrameEvent::GPU_COMPOSITION_DONE,
571 FrameEvent::DEQUEUE_READY,
572 FrameEvent::RELEASE
573 };
574 if (mSupportsPresent) {
575 outSupported->push_back(
576 FrameEvent::DISPLAY_PRESENT);
577 }
578 return NO_ERROR;
579 }
580
Brian Anderson3da8d272016-07-28 16:20:47 -0700581 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
582 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
583 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
584 status_t getDisplayStats(const sp<IBinder>& /*display*/,
585 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
586 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
587 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
588 override {
589 return NO_ERROR;
590 }
591 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700592 Vector<ColorMode>* /*outColorModes*/) override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700593 return NO_ERROR;
594 }
Peiyong Lina52f0292018-03-14 17:26:31 -0700595 ColorMode getActiveColorMode(const sp<IBinder>& /*display*/)
Brian Anderson3da8d272016-07-28 16:20:47 -0700596 override {
Peiyong Lina52f0292018-03-14 17:26:31 -0700597 return ColorMode::NATIVE;
Brian Anderson3da8d272016-07-28 16:20:47 -0700598 }
599 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700600 ColorMode /*colorMode*/) override { return NO_ERROR; }
Peiyong Lin0e003c92018-09-17 11:09:51 -0700601 status_t captureScreen(const sp<IBinder>& /*display*/, sp<GraphicBuffer>* /*outBuffer*/,
602 const ui::Dataspace /*reqDataspace*/,
603 const ui::PixelFormat /*reqPixelFormat*/, Rect /*sourceCrop*/,
604 uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
605 bool /*useIdentityTransform*/, Rotation /*rotation*/) override {
606 return NO_ERROR;
607 }
chaviwa76b2712017-09-20 12:02:26 -0700608 virtual status_t captureLayers(const sp<IBinder>& /*parentHandle*/,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700609 sp<GraphicBuffer>* /*outBuffer*/,
610 const ui::Dataspace /*reqDataspace*/,
611 const ui::PixelFormat /*reqPixelFormat*/,
612 const Rect& /*sourceCrop*/, float /*frameScale*/,
613 bool /*childrenOnly*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700614 return NO_ERROR;
615 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700616 status_t clearAnimationFrameStats() override { return NO_ERROR; }
617 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
618 return NO_ERROR;
619 }
620 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
621 HdrCapabilities* /*outCapabilities*/) const override {
622 return NO_ERROR;
623 }
624 status_t enableVSyncInjections(bool /*enable*/) override {
625 return NO_ERROR;
626 }
627 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800628 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
629 return NO_ERROR;
630 }
Peiyong Lin0256f722018-08-31 15:45:10 -0700631 status_t getCompositionPreference(ui::Dataspace* /*outDataSpace*/,
632 ui::PixelFormat* /*outPixelFormat*/) const override {
633 return NO_ERROR;
634 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700635
636protected:
637 IBinder* onAsBinder() override { return nullptr; }
638
639private:
640 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700641};
642
643class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
644public:
645 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
646 : mFenceMap(fenceMap) {}
647
648 ~FakeProducerFrameEventHistory() {}
649
650 void updateAcquireFence(uint64_t frameNumber,
651 std::shared_ptr<FenceTime>&& acquire) override {
652 // Verify the acquire fence being added isn't the one from the consumer.
653 EXPECT_NE(mConsumerAcquireFence, acquire);
654 // Override the fence, so we can verify this was called by the
655 // producer after the frame is queued.
656 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
657 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
658 }
659
660 void setAcquireFenceOverride(
661 const std::shared_ptr<FenceTime>& acquireFenceOverride,
662 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
663 mAcquireFenceOverride = acquireFenceOverride;
664 mConsumerAcquireFence = consumerAcquireFence;
665 }
666
667protected:
668 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
669 const override {
670 return mFenceMap->createFenceTimeForTest(fence);
671 }
672
673 FenceToFenceTimeMap* mFenceMap{nullptr};
674
675 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
676 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
677};
678
679
680class TestSurface : public Surface {
681public:
682 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
683 FenceToFenceTimeMap* fenceMap)
684 : Surface(bufferProducer),
685 mFakeSurfaceComposer(new FakeSurfaceComposer) {
686 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
687 mFrameEventHistory.reset(mFakeFrameEventHistory);
688 }
689
690 ~TestSurface() override {}
691
692 sp<ISurfaceComposer> composerService() const override {
693 return mFakeSurfaceComposer;
694 }
695
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800696 nsecs_t now() const override {
697 return mNow;
698 }
699
700 void setNow(nsecs_t now) {
701 mNow = now;
702 }
703
Brian Anderson3da8d272016-07-28 16:20:47 -0700704public:
705 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800706 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700707
708 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
709 // but this raw pointer gives access to test functionality.
710 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
711};
712
713
Brian Andersond0010582017-03-07 13:20:31 -0800714class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700715protected:
716 struct FenceAndFenceTime {
717 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
718 : mFence(new Fence),
719 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
720 sp<Fence> mFence { nullptr };
721 std::shared_ptr<FenceTime> mFenceTime { nullptr };
722 };
723
724 struct RefreshEvents {
725 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800726 : mFenceMap(fenceMap),
727 kCompositorTiming(
728 {refreshStart, refreshStart + 1, refreshStart + 2 }),
729 kStartTime(refreshStart + 3),
730 kGpuCompositionDoneTime(refreshStart + 4),
731 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700732
733 void signalPostCompositeFences() {
734 mFenceMap.signalAllForTest(
735 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
736 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
737 }
738
739 FenceToFenceTimeMap& mFenceMap;
740
741 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
742 FenceAndFenceTime mPresent { mFenceMap };
743
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800744 const CompositorTiming kCompositorTiming;
745
Brian Anderson3da8d272016-07-28 16:20:47 -0700746 const nsecs_t kStartTime;
747 const nsecs_t kGpuCompositionDoneTime;
748 const nsecs_t kPresentTime;
749 };
750
751 struct FrameEvents {
752 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
753 : mFenceMap(fenceMap),
754 kPostedTime(frameStartTime + 100),
755 kRequestedPresentTime(frameStartTime + 200),
756 kProducerAcquireTime(frameStartTime + 300),
757 kConsumerAcquireTime(frameStartTime + 301),
758 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700759 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700760 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700761 mRefreshes {
762 { mFenceMap, frameStartTime + 410 },
763 { mFenceMap, frameStartTime + 420 },
764 { mFenceMap, frameStartTime + 430 } } {}
765
766 void signalQueueFences() {
767 mFenceMap.signalAllForTest(
768 mAcquireConsumer.mFence, kConsumerAcquireTime);
769 mFenceMap.signalAllForTest(
770 mAcquireProducer.mFence, kProducerAcquireTime);
771 }
772
773 void signalRefreshFences() {
774 for (auto& re : mRefreshes) {
775 re.signalPostCompositeFences();
776 }
777 }
778
779 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700780 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
781 }
782
783 FenceToFenceTimeMap& mFenceMap;
784
785 FenceAndFenceTime mAcquireConsumer { mFenceMap };
786 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700787 FenceAndFenceTime mRelease { mFenceMap };
788
789 const nsecs_t kPostedTime;
790 const nsecs_t kRequestedPresentTime;
791 const nsecs_t kProducerAcquireTime;
792 const nsecs_t kConsumerAcquireTime;
793 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700794 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700795 const nsecs_t kReleaseTime;
796
797 RefreshEvents mRefreshes[3];
798 };
799
Brian Andersond0010582017-03-07 13:20:31 -0800800 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700801
802 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700803 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
804 mFakeConsumer = new FakeConsumer;
805 mCfeh = &mFakeConsumer->mFrameEventHistory;
806 mConsumer->consumerConnect(mFakeConsumer, false);
807 mConsumer->setConsumerName(String8("TestConsumer"));
808 mSurface = new TestSurface(mProducer, &mFenceMap);
809 mWindow = mSurface;
810
811 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
812 NATIVE_WINDOW_API_CPU));
813 native_window_set_buffer_count(mWindow.get(), 4);
814 }
815
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800816 void disableFrameTimestamps() {
817 mFakeConsumer->mGetFrameTimestampsEnabled = false;
818 native_window_enable_frame_timestamps(mWindow.get(), 0);
819 mFrameTimestampsEnabled = false;
820 }
821
Brian Anderson3da8d272016-07-28 16:20:47 -0700822 void enableFrameTimestamps() {
823 mFakeConsumer->mGetFrameTimestampsEnabled = true;
824 native_window_enable_frame_timestamps(mWindow.get(), 1);
825 mFrameTimestampsEnabled = true;
826 }
827
Brian Anderson1049d1d2016-12-16 17:25:57 -0800828 int getAllFrameTimestamps(uint64_t frameId) {
829 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700830 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
831 &outFirstRefreshStartTime, &outLastRefreshStartTime,
832 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700833 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700834 }
835
Brian Anderson3da8d272016-07-28 16:20:47 -0700836 void resetTimestamps() {
837 outRequestedPresentTime = -1;
838 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700839 outLatchTime = -1;
840 outFirstRefreshStartTime = -1;
841 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700842 outGpuCompositionDoneTime = -1;
843 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700844 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700845 outReleaseTime = -1;
846 }
847
Brian Anderson1049d1d2016-12-16 17:25:57 -0800848 uint64_t getNextFrameId() {
849 uint64_t frameId = -1;
850 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
851 EXPECT_EQ(status, NO_ERROR);
852 return frameId;
853 }
854
Brian Anderson3da8d272016-07-28 16:20:47 -0700855 void dequeueAndQueue(uint64_t frameIndex) {
856 int fence = -1;
857 ANativeWindowBuffer* buffer = nullptr;
858 ASSERT_EQ(NO_ERROR,
859 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
860
861 int oldAddFrameTimestampsCount =
862 mFakeConsumer->mAddFrameTimestampsCount;
863
864 FrameEvents* frame = &mFrames[frameIndex];
865 uint64_t frameNumber = frameIndex + 1;
866
867 NewFrameEventsEntry fe;
868 fe.frameNumber = frameNumber;
869 fe.postedTime = frame->kPostedTime;
870 fe.requestedPresentTime = frame->kRequestedPresentTime;
871 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
872 mFakeConsumer->mNewFrameEntryOverride = fe;
873
874 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
875 frame->mAcquireProducer.mFenceTime,
876 frame->mAcquireConsumer.mFenceTime);
877
878 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
879
880 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
881
882 EXPECT_EQ(
883 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
884 mFakeConsumer->mAddFrameTimestampsCount);
885 }
886
887 void addFrameEvents(
888 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
889 FrameEvents* oldFrame =
890 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
891 FrameEvents* newFrame = &mFrames[iNewFrame];
892
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800893 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700894 uint64_t nNewFrame = iNewFrame + 1;
895
Brian Anderson4e606e32017-03-16 15:34:57 -0700896 // Latch, Composite, and Release the frames in a plausible order.
897 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700898 // that's okay for the purposes of this test.
899 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
900
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700901 // Composite the previous frame one more time, which helps verify
902 // LastRefresh is updated properly.
903 if (oldFrame != nullptr) {
904 mCfeh->addPreComposition(nOldFrame,
905 oldFrame->mRefreshes[2].kStartTime);
906 gpuDoneFenceTime = gpuComposited ?
907 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
908 FenceTime::NO_FENCE;
909 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800910 oldFrame->mRefreshes[2].mPresent.mFenceTime,
911 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700912 }
913
914 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700915 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
916
917 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
918 gpuDoneFenceTime = gpuComposited ?
919 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
920 FenceTime::NO_FENCE;
921 // HWC2 releases the previous buffer after a new latch just before
922 // calling postComposition.
923 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700924 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700925 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
926 }
927 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800928 newFrame->mRefreshes[0].mPresent.mFenceTime,
929 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700930
Brian Anderson3da8d272016-07-28 16:20:47 -0700931 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
932 gpuDoneFenceTime = gpuComposited ?
933 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
934 FenceTime::NO_FENCE;
935 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800936 newFrame->mRefreshes[1].mPresent.mFenceTime,
937 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700938 }
939
Brian Anderson3da8d272016-07-28 16:20:47 -0700940 sp<IGraphicBufferProducer> mProducer;
941 sp<IGraphicBufferConsumer> mConsumer;
942 sp<FakeConsumer> mFakeConsumer;
943 ConsumerFrameEventHistory* mCfeh;
944 sp<TestSurface> mSurface;
945 sp<ANativeWindow> mWindow;
946
947 FenceToFenceTimeMap mFenceMap;
948
949 bool mFrameTimestampsEnabled = false;
950
951 int64_t outRequestedPresentTime = -1;
952 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700953 int64_t outLatchTime = -1;
954 int64_t outFirstRefreshStartTime = -1;
955 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700956 int64_t outGpuCompositionDoneTime = -1;
957 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700958 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700959 int64_t outReleaseTime = -1;
960
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800961 FrameEvents mFrames[3] {
962 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700963};
964
965
966// This test verifies that the frame timestamps are not retrieved when not
967// explicitly enabled via native_window_enable_frame_timestamps.
968// We want to check this to make sure there's no overhead for users
969// that don't need the timestamp information.
970TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
971 int fence;
972 ANativeWindowBuffer* buffer;
973
974 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
975 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
976
Brian Anderson1049d1d2016-12-16 17:25:57 -0800977 const uint64_t fId = getNextFrameId();
978
Brian Anderson3da8d272016-07-28 16:20:47 -0700979 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
980 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
981 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
982 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
983
984 // Verify the producer doesn't get frame timestamps piggybacked on queue.
985 // It is okay that frame timestamps are added in the consumer since it is
986 // still needed for SurfaceFlinger dumps.
987 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
988 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
989 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
990
991 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800992 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700993 EXPECT_EQ(INVALID_OPERATION, result);
994 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800995
996 // Verify compositor timing query fails.
997 nsecs_t compositeDeadline = 0;
998 nsecs_t compositeInterval = 0;
999 nsecs_t compositeToPresentLatency = 0;
1000 result = native_window_get_compositor_timing(mWindow.get(),
1001 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1002 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001003}
1004
1005// This test verifies that the frame timestamps are retrieved if explicitly
1006// enabled via native_window_enable_frame_timestamps.
1007TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001008 CompositorTiming initialCompositorTiming {
1009 1000000000, // 1s deadline
1010 16666667, // 16ms interval
1011 50000000, // 50ms present latency
1012 };
1013 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1014
Brian Anderson3da8d272016-07-28 16:20:47 -07001015 enableFrameTimestamps();
1016
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001017 // Verify the compositor timing query gets the initial compositor values
1018 // after timststamps are enabled; even before the first frame is queued
1019 // or dequeued.
1020 nsecs_t compositeDeadline = 0;
1021 nsecs_t compositeInterval = 0;
1022 nsecs_t compositeToPresentLatency = 0;
1023 mSurface->setNow(initialCompositorTiming.deadline - 1);
1024 int result = native_window_get_compositor_timing(mWindow.get(),
1025 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1026 EXPECT_EQ(NO_ERROR, result);
1027 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1028 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1029 EXPECT_EQ(initialCompositorTiming.presentLatency,
1030 compositeToPresentLatency);
1031
Brian Anderson3da8d272016-07-28 16:20:47 -07001032 int fence;
1033 ANativeWindowBuffer* buffer;
1034
1035 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001036 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001037
Brian Anderson1049d1d2016-12-16 17:25:57 -08001038 const uint64_t fId1 = getNextFrameId();
1039
Brian Anderson3da8d272016-07-28 16:20:47 -07001040 // Verify getFrameTimestamps is piggybacked on dequeue.
1041 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1042 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001043 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001044
1045 NewFrameEventsEntry f1;
1046 f1.frameNumber = 1;
1047 f1.postedTime = mFrames[0].kPostedTime;
1048 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1049 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1050 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1051 mFrames[0].mAcquireProducer.mFenceTime,
1052 mFrames[0].mAcquireConsumer.mFenceTime);
1053 mFakeConsumer->mNewFrameEntryOverride = f1;
1054 mFrames[0].signalQueueFences();
1055
1056 // Verify getFrameTimestamps is piggybacked on queue.
1057 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1058 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1059 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001060 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001061
1062 // Verify queries for timestamps that the producer doesn't know about
1063 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001064 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001065 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001066 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001067}
1068
Brian Anderson6b376712017-04-04 10:51:39 -07001069TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1070 bool displayPresentSupported = true;
1071 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1072
1073 // Verify supported bits are forwarded.
1074 int supportsPresent = -1;
1075 mWindow.get()->query(mWindow.get(),
1076 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1077 EXPECT_EQ(displayPresentSupported, supportsPresent);
1078}
1079
1080TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1081 bool displayPresentSupported = false;
1082 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1083
1084 // Verify supported bits are forwarded.
1085 int supportsPresent = -1;
1086 mWindow.get()->query(mWindow.get(),
1087 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1088 EXPECT_EQ(displayPresentSupported, supportsPresent);
1089}
1090
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001091TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1092 nsecs_t phase = 4000;
1093 nsecs_t interval = 1000;
1094
1095 // Timestamp in previous interval.
1096 nsecs_t timestamp = 3500;
1097 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1098 timestamp, phase, interval));
1099
1100 // Timestamp in next interval.
1101 timestamp = 4500;
1102 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1103 timestamp, phase, interval));
1104
1105 // Timestamp multiple intervals before.
1106 timestamp = 2500;
1107 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1108 timestamp, phase, interval));
1109
1110 // Timestamp multiple intervals after.
1111 timestamp = 6500;
1112 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1113 timestamp, phase, interval));
1114
1115 // Timestamp on previous interval.
1116 timestamp = 3000;
1117 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1118 timestamp, phase, interval));
1119
1120 // Timestamp on next interval.
1121 timestamp = 5000;
1122 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1123 timestamp, phase, interval));
1124
1125 // Timestamp equal to phase.
1126 timestamp = 4000;
1127 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1128 timestamp, phase, interval));
1129}
1130
1131// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1132// if the number of intervals elapsed is internally stored in an int.
1133TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1134 nsecs_t phase = 0;
1135 nsecs_t interval = 4000;
1136 nsecs_t big_timestamp = 8635916564000;
1137 int32_t intervals = big_timestamp / interval;
1138
1139 EXPECT_LT(intervals, 0);
1140 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1141 big_timestamp, phase, interval));
1142 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1143 big_timestamp, big_timestamp, interval));
1144}
1145
1146// This verifies the compositor timing is updated by refresh events
1147// and piggy backed on a queue, dequeue, and enabling of timestamps..
1148TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1149 CompositorTiming initialCompositorTiming {
1150 1000000000, // 1s deadline
1151 16666667, // 16ms interval
1152 50000000, // 50ms present latency
1153 };
1154 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1155
1156 enableFrameTimestamps();
1157
1158 // We get the initial values before any frames are submitted.
1159 nsecs_t compositeDeadline = 0;
1160 nsecs_t compositeInterval = 0;
1161 nsecs_t compositeToPresentLatency = 0;
1162 mSurface->setNow(initialCompositorTiming.deadline - 1);
1163 int result = native_window_get_compositor_timing(mWindow.get(),
1164 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1165 EXPECT_EQ(NO_ERROR, result);
1166 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1167 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1168 EXPECT_EQ(initialCompositorTiming.presentLatency,
1169 compositeToPresentLatency);
1170
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001171 dequeueAndQueue(0);
1172 addFrameEvents(true, NO_FRAME_INDEX, 0);
1173
1174 // Still get the initial values because the frame events for frame 0
1175 // didn't get a chance to piggyback on a queue or dequeue yet.
1176 result = native_window_get_compositor_timing(mWindow.get(),
1177 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1178 EXPECT_EQ(NO_ERROR, result);
1179 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1180 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1181 EXPECT_EQ(initialCompositorTiming.presentLatency,
1182 compositeToPresentLatency);
1183
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001184 dequeueAndQueue(1);
1185 addFrameEvents(true, 0, 1);
1186
1187 // Now expect the composite values associated with frame 1.
1188 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1189 result = native_window_get_compositor_timing(mWindow.get(),
1190 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1191 EXPECT_EQ(NO_ERROR, result);
1192 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1193 compositeDeadline);
1194 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1195 compositeInterval);
1196 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1197 compositeToPresentLatency);
1198
1199 dequeueAndQueue(2);
1200 addFrameEvents(true, 1, 2);
1201
1202 // Now expect the composite values associated with frame 2.
1203 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1204 result = native_window_get_compositor_timing(mWindow.get(),
1205 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1206 EXPECT_EQ(NO_ERROR, result);
1207 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1208 compositeDeadline);
1209 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1210 compositeInterval);
1211 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1212 compositeToPresentLatency);
1213
1214 // Re-enabling frame timestamps should get the latest values.
1215 disableFrameTimestamps();
1216 enableFrameTimestamps();
1217
1218 // Now expect the composite values associated with frame 3.
1219 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1220 result = native_window_get_compositor_timing(mWindow.get(),
1221 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1222 EXPECT_EQ(NO_ERROR, result);
1223 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1224 compositeDeadline);
1225 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1226 compositeInterval);
1227 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1228 compositeToPresentLatency);
1229}
1230
1231// This verifies the compositor deadline properly snaps to the the next
1232// deadline based on the current time.
1233TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1234 CompositorTiming initialCompositorTiming {
1235 1000000000, // 1s deadline
1236 16666667, // 16ms interval
1237 50000000, // 50ms present latency
1238 };
1239 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1240
1241 enableFrameTimestamps();
1242
1243 nsecs_t compositeDeadline = 0;
1244 nsecs_t compositeInterval = 0;
1245 nsecs_t compositeToPresentLatency = 0;
1246
1247 // A "now" just before the deadline snaps to the deadline.
1248 mSurface->setNow(initialCompositorTiming.deadline - 1);
1249 int result = native_window_get_compositor_timing(mWindow.get(),
1250 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1251 EXPECT_EQ(NO_ERROR, result);
1252 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1253 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1254 EXPECT_EQ(expectedDeadline, compositeDeadline);
1255
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001256 dequeueAndQueue(0);
1257 addFrameEvents(true, NO_FRAME_INDEX, 0);
1258
1259 // A "now" just after the deadline snaps properly.
1260 mSurface->setNow(initialCompositorTiming.deadline + 1);
1261 result = native_window_get_compositor_timing(mWindow.get(),
1262 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1263 EXPECT_EQ(NO_ERROR, result);
1264 expectedDeadline =
1265 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1266 EXPECT_EQ(expectedDeadline, compositeDeadline);
1267
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001268 dequeueAndQueue(1);
1269 addFrameEvents(true, 0, 1);
1270
1271 // A "now" just after the next interval snaps properly.
1272 mSurface->setNow(
1273 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1274 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1275 result = native_window_get_compositor_timing(mWindow.get(),
1276 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1277 EXPECT_EQ(NO_ERROR, result);
1278 expectedDeadline =
1279 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1280 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1281 EXPECT_EQ(expectedDeadline, compositeDeadline);
1282
1283 dequeueAndQueue(2);
1284 addFrameEvents(true, 1, 2);
1285
1286 // A "now" over 1 interval before the deadline snaps properly.
1287 mSurface->setNow(
1288 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1289 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1290 result = native_window_get_compositor_timing(mWindow.get(),
1291 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1292 EXPECT_EQ(NO_ERROR, result);
1293 expectedDeadline =
1294 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1295 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1296 EXPECT_EQ(expectedDeadline, compositeDeadline);
1297
1298 // Re-enabling frame timestamps should get the latest values.
1299 disableFrameTimestamps();
1300 enableFrameTimestamps();
1301
1302 // A "now" over 2 intervals before the deadline snaps properly.
1303 mSurface->setNow(
1304 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1305 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1306 result = native_window_get_compositor_timing(mWindow.get(),
1307 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1308 EXPECT_EQ(NO_ERROR, result);
1309 expectedDeadline =
1310 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1311 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1312 EXPECT_EQ(expectedDeadline, compositeDeadline);
1313}
1314
Brian Anderson1049d1d2016-12-16 17:25:57 -08001315// This verifies the timestamps recorded in the consumer's
1316// FrameTimestampsHistory are properly retrieved by the producer for the
1317// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001318TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1319 enableFrameTimestamps();
1320
Brian Anderson1049d1d2016-12-16 17:25:57 -08001321 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001322 dequeueAndQueue(0);
1323 mFrames[0].signalQueueFences();
1324
Brian Anderson1049d1d2016-12-16 17:25:57 -08001325 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001326 dequeueAndQueue(1);
1327 mFrames[1].signalQueueFences();
1328
1329 addFrameEvents(true, NO_FRAME_INDEX, 0);
1330 mFrames[0].signalRefreshFences();
1331 addFrameEvents(true, 0, 1);
1332 mFrames[0].signalReleaseFences();
1333 mFrames[1].signalRefreshFences();
1334
1335 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001336 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001337 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001338 EXPECT_EQ(NO_ERROR, result);
1339 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1340 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001341 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1342 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1343 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001344 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1345 outGpuCompositionDoneTime);
1346 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001347 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001348 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1349
1350 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001351 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001352 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001353 EXPECT_EQ(NO_ERROR, result);
1354 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1355 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001356 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1357 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1358 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001359 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1360 outGpuCompositionDoneTime);
1361 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001362 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1363 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001364}
1365
1366// This test verifies the acquire fence recorded by the consumer is not sent
1367// back to the producer and the producer saves its own fence.
1368TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1369 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001370
Brian Anderson3da8d272016-07-28 16:20:47 -07001371 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001372 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001373 dequeueAndQueue(0);
1374
1375 // Verify queue-related timestamps for f1 are available immediately in the
1376 // producer without asking the consumer again, even before signaling the
1377 // acquire fence.
1378 resetTimestamps();
1379 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001380 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001381 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001382 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001383 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1384 EXPECT_EQ(NO_ERROR, result);
1385 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001386 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001387
1388 // Signal acquire fences. Verify a sync call still isn't necessary.
1389 mFrames[0].signalQueueFences();
1390
1391 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001392 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001393 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001394 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001395 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1396 EXPECT_EQ(NO_ERROR, result);
1397 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1398 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1399
1400 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001401 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001402 dequeueAndQueue(1);
1403
1404 // Verify queue-related timestamps for f2 are available immediately in the
1405 // producer without asking the consumer again, even before signaling the
1406 // acquire fence.
1407 resetTimestamps();
1408 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001409 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001410 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001411 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001412 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1413 EXPECT_EQ(NO_ERROR, result);
1414 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001415 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001416
1417 // Signal acquire fences. Verify a sync call still isn't necessary.
1418 mFrames[1].signalQueueFences();
1419
1420 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001421 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001422 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001423 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001424 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1425 EXPECT_EQ(NO_ERROR, result);
1426 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1427 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1428}
1429
1430TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1431 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001432
1433 // Dequeue and queue frame 1.
1434 dequeueAndQueue(0);
1435 mFrames[0].signalQueueFences();
1436
1437 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001438 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001439 dequeueAndQueue(1);
1440 mFrames[1].signalQueueFences();
1441
1442 addFrameEvents(true, NO_FRAME_INDEX, 0);
1443 mFrames[0].signalRefreshFences();
1444 addFrameEvents(true, 0, 1);
1445 mFrames[0].signalReleaseFences();
1446 mFrames[1].signalRefreshFences();
1447
1448 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001449 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001450 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001451 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1452 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001453 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001454 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1455}
1456
1457// This test verifies that fences can signal and update timestamps producer
1458// side without an additional sync call to the consumer.
1459TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1460 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001461
1462 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001463 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001464 dequeueAndQueue(0);
1465 mFrames[0].signalQueueFences();
1466
1467 // Dequeue and queue frame 2.
1468 dequeueAndQueue(1);
1469 mFrames[1].signalQueueFences();
1470
1471 addFrameEvents(true, NO_FRAME_INDEX, 0);
1472 addFrameEvents(true, 0, 1);
1473
1474 // Verify available timestamps are correct for frame 1, before any
1475 // fence has been signaled.
1476 // Note: A sync call is necessary here since the events triggered by
1477 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001478 resetTimestamps();
1479 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001480 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001481 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1482 EXPECT_EQ(NO_ERROR, result);
1483 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1484 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001485 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1486 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1487 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001488 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1489 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001490 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001491 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001492
1493 // Verify available timestamps are correct for frame 1 again, before any
1494 // fence has been signaled.
1495 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001496 resetTimestamps();
1497 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001498 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001499 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1500 EXPECT_EQ(NO_ERROR, result);
1501 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1502 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001503 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1504 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1505 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001506 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1507 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001508 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001509 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001510
1511 // Signal the fences for frame 1.
1512 mFrames[0].signalRefreshFences();
1513 mFrames[0].signalReleaseFences();
1514
1515 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001516 resetTimestamps();
1517 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001518 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001519 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1520 EXPECT_EQ(NO_ERROR, result);
1521 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1522 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001523 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1524 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1525 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001526 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1527 outGpuCompositionDoneTime);
1528 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001529 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001530 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1531}
1532
1533// This test verifies that if the frame wasn't GPU composited but has a refresh
1534// event a sync call isn't made to get the GPU composite done time since it will
1535// never exist.
1536TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1537 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001538
Brian Anderson3da8d272016-07-28 16:20:47 -07001539 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001540 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001541 dequeueAndQueue(0);
1542 mFrames[0].signalQueueFences();
1543
1544 // Dequeue and queue frame 2.
1545 dequeueAndQueue(1);
1546 mFrames[1].signalQueueFences();
1547
1548 addFrameEvents(false, NO_FRAME_INDEX, 0);
1549 addFrameEvents(false, 0, 1);
1550
1551 // Verify available timestamps are correct for frame 1, before any
1552 // fence has been signaled.
1553 // Note: A sync call is necessary here since the events triggered by
1554 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1555 resetTimestamps();
1556 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001557 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001558 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1559 EXPECT_EQ(NO_ERROR, result);
1560 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1561 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001562 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1563 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1564 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001565 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1566 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001567 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001568 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001569
1570 // Signal the fences for frame 1.
1571 mFrames[0].signalRefreshFences();
1572 mFrames[0].signalReleaseFences();
1573
1574 // Verify all timestamps, except GPU composition, are available without a
1575 // sync call.
1576 resetTimestamps();
1577 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001578 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001579 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1580 EXPECT_EQ(NO_ERROR, result);
1581 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1582 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001583 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1584 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1585 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001586 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001587 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001588 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001589 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1590}
1591
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001592// This test verifies that if the certain timestamps can't possibly exist for
1593// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001594TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001595 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001596
1597 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001598 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001599 dequeueAndQueue(0);
1600 mFrames[0].signalQueueFences();
1601
1602 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001603 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001604 dequeueAndQueue(1);
1605 mFrames[1].signalQueueFences();
1606
1607 addFrameEvents(false, NO_FRAME_INDEX, 0);
1608 addFrameEvents(false, 0, 1);
1609
1610 // Verify available timestamps are correct for frame 1, before any
1611 // fence has been signaled.
1612 // Note: A sync call is necessary here since the events triggered by
1613 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001614 resetTimestamps();
1615 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001616 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001617 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1618 EXPECT_EQ(NO_ERROR, result);
1619 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1620 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001621 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1622 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1623 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001624 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1625 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001626 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001627 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001628
1629 mFrames[0].signalRefreshFences();
1630 mFrames[0].signalReleaseFences();
1631 mFrames[1].signalRefreshFences();
1632
Brian Anderson1049d1d2016-12-16 17:25:57 -08001633 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001634 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001635 // available, a sync call should not occur because it's not possible for f2
1636 // to encounter the final value for those events until another frame is
1637 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001638 resetTimestamps();
1639 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001640 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001641 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1642 EXPECT_EQ(NO_ERROR, result);
1643 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1644 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001645 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1646 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1647 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001648 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001649 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001650 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1651 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001652}
1653
Brian Anderson6b376712017-04-04 10:51:39 -07001654// This test verifies there are no sync calls for present times
1655// when they aren't supported and that an error is returned.
1656
1657TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1658 enableFrameTimestamps();
1659 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1660
1661 // Dequeue and queue frame 1.
1662 const uint64_t fId1 = getNextFrameId();
1663 dequeueAndQueue(0);
1664
1665 // Verify a query for the Present times do not trigger a sync call if they
1666 // are not supported.
1667 resetTimestamps();
1668 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1669 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1670 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1671 &outDisplayPresentTime, nullptr, nullptr);
1672 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1673 EXPECT_EQ(BAD_VALUE, result);
1674 EXPECT_EQ(-1, outDisplayPresentTime);
1675}
1676
Dan Stoza932f0082017-05-31 13:50:16 -07001677} // namespace android