blob: 25f762bbe3b3dac725a37acec2bc145bf182f05e [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
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700636 virtual bool isColorManagementUsed() const { return false; }
637
Brian Anderson3da8d272016-07-28 16:20:47 -0700638protected:
639 IBinder* onAsBinder() override { return nullptr; }
640
641private:
642 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700643};
644
645class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
646public:
647 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
648 : mFenceMap(fenceMap) {}
649
650 ~FakeProducerFrameEventHistory() {}
651
652 void updateAcquireFence(uint64_t frameNumber,
653 std::shared_ptr<FenceTime>&& acquire) override {
654 // Verify the acquire fence being added isn't the one from the consumer.
655 EXPECT_NE(mConsumerAcquireFence, acquire);
656 // Override the fence, so we can verify this was called by the
657 // producer after the frame is queued.
658 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
659 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
660 }
661
662 void setAcquireFenceOverride(
663 const std::shared_ptr<FenceTime>& acquireFenceOverride,
664 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
665 mAcquireFenceOverride = acquireFenceOverride;
666 mConsumerAcquireFence = consumerAcquireFence;
667 }
668
669protected:
670 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
671 const override {
672 return mFenceMap->createFenceTimeForTest(fence);
673 }
674
675 FenceToFenceTimeMap* mFenceMap{nullptr};
676
677 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
678 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
679};
680
681
682class TestSurface : public Surface {
683public:
684 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
685 FenceToFenceTimeMap* fenceMap)
686 : Surface(bufferProducer),
687 mFakeSurfaceComposer(new FakeSurfaceComposer) {
688 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
689 mFrameEventHistory.reset(mFakeFrameEventHistory);
690 }
691
692 ~TestSurface() override {}
693
694 sp<ISurfaceComposer> composerService() const override {
695 return mFakeSurfaceComposer;
696 }
697
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800698 nsecs_t now() const override {
699 return mNow;
700 }
701
702 void setNow(nsecs_t now) {
703 mNow = now;
704 }
705
Brian Anderson3da8d272016-07-28 16:20:47 -0700706public:
707 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800708 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700709
710 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
711 // but this raw pointer gives access to test functionality.
712 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
713};
714
715
Brian Andersond0010582017-03-07 13:20:31 -0800716class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700717protected:
718 struct FenceAndFenceTime {
719 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
720 : mFence(new Fence),
721 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
722 sp<Fence> mFence { nullptr };
723 std::shared_ptr<FenceTime> mFenceTime { nullptr };
724 };
725
726 struct RefreshEvents {
727 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800728 : mFenceMap(fenceMap),
729 kCompositorTiming(
730 {refreshStart, refreshStart + 1, refreshStart + 2 }),
731 kStartTime(refreshStart + 3),
732 kGpuCompositionDoneTime(refreshStart + 4),
733 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700734
735 void signalPostCompositeFences() {
736 mFenceMap.signalAllForTest(
737 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
738 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
739 }
740
741 FenceToFenceTimeMap& mFenceMap;
742
743 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
744 FenceAndFenceTime mPresent { mFenceMap };
745
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800746 const CompositorTiming kCompositorTiming;
747
Brian Anderson3da8d272016-07-28 16:20:47 -0700748 const nsecs_t kStartTime;
749 const nsecs_t kGpuCompositionDoneTime;
750 const nsecs_t kPresentTime;
751 };
752
753 struct FrameEvents {
754 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
755 : mFenceMap(fenceMap),
756 kPostedTime(frameStartTime + 100),
757 kRequestedPresentTime(frameStartTime + 200),
758 kProducerAcquireTime(frameStartTime + 300),
759 kConsumerAcquireTime(frameStartTime + 301),
760 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700761 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700762 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700763 mRefreshes {
764 { mFenceMap, frameStartTime + 410 },
765 { mFenceMap, frameStartTime + 420 },
766 { mFenceMap, frameStartTime + 430 } } {}
767
768 void signalQueueFences() {
769 mFenceMap.signalAllForTest(
770 mAcquireConsumer.mFence, kConsumerAcquireTime);
771 mFenceMap.signalAllForTest(
772 mAcquireProducer.mFence, kProducerAcquireTime);
773 }
774
775 void signalRefreshFences() {
776 for (auto& re : mRefreshes) {
777 re.signalPostCompositeFences();
778 }
779 }
780
781 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700782 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
783 }
784
785 FenceToFenceTimeMap& mFenceMap;
786
787 FenceAndFenceTime mAcquireConsumer { mFenceMap };
788 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700789 FenceAndFenceTime mRelease { mFenceMap };
790
791 const nsecs_t kPostedTime;
792 const nsecs_t kRequestedPresentTime;
793 const nsecs_t kProducerAcquireTime;
794 const nsecs_t kConsumerAcquireTime;
795 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700796 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700797 const nsecs_t kReleaseTime;
798
799 RefreshEvents mRefreshes[3];
800 };
801
Brian Andersond0010582017-03-07 13:20:31 -0800802 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700803
804 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700805 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
806 mFakeConsumer = new FakeConsumer;
807 mCfeh = &mFakeConsumer->mFrameEventHistory;
808 mConsumer->consumerConnect(mFakeConsumer, false);
809 mConsumer->setConsumerName(String8("TestConsumer"));
810 mSurface = new TestSurface(mProducer, &mFenceMap);
811 mWindow = mSurface;
812
813 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
814 NATIVE_WINDOW_API_CPU));
815 native_window_set_buffer_count(mWindow.get(), 4);
816 }
817
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800818 void disableFrameTimestamps() {
819 mFakeConsumer->mGetFrameTimestampsEnabled = false;
820 native_window_enable_frame_timestamps(mWindow.get(), 0);
821 mFrameTimestampsEnabled = false;
822 }
823
Brian Anderson3da8d272016-07-28 16:20:47 -0700824 void enableFrameTimestamps() {
825 mFakeConsumer->mGetFrameTimestampsEnabled = true;
826 native_window_enable_frame_timestamps(mWindow.get(), 1);
827 mFrameTimestampsEnabled = true;
828 }
829
Brian Anderson1049d1d2016-12-16 17:25:57 -0800830 int getAllFrameTimestamps(uint64_t frameId) {
831 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700832 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
833 &outFirstRefreshStartTime, &outLastRefreshStartTime,
834 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700835 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700836 }
837
Brian Anderson3da8d272016-07-28 16:20:47 -0700838 void resetTimestamps() {
839 outRequestedPresentTime = -1;
840 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700841 outLatchTime = -1;
842 outFirstRefreshStartTime = -1;
843 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700844 outGpuCompositionDoneTime = -1;
845 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700846 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700847 outReleaseTime = -1;
848 }
849
Brian Anderson1049d1d2016-12-16 17:25:57 -0800850 uint64_t getNextFrameId() {
851 uint64_t frameId = -1;
852 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
853 EXPECT_EQ(status, NO_ERROR);
854 return frameId;
855 }
856
Brian Anderson3da8d272016-07-28 16:20:47 -0700857 void dequeueAndQueue(uint64_t frameIndex) {
858 int fence = -1;
859 ANativeWindowBuffer* buffer = nullptr;
860 ASSERT_EQ(NO_ERROR,
861 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
862
863 int oldAddFrameTimestampsCount =
864 mFakeConsumer->mAddFrameTimestampsCount;
865
866 FrameEvents* frame = &mFrames[frameIndex];
867 uint64_t frameNumber = frameIndex + 1;
868
869 NewFrameEventsEntry fe;
870 fe.frameNumber = frameNumber;
871 fe.postedTime = frame->kPostedTime;
872 fe.requestedPresentTime = frame->kRequestedPresentTime;
873 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
874 mFakeConsumer->mNewFrameEntryOverride = fe;
875
876 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
877 frame->mAcquireProducer.mFenceTime,
878 frame->mAcquireConsumer.mFenceTime);
879
880 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
881
882 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
883
884 EXPECT_EQ(
885 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
886 mFakeConsumer->mAddFrameTimestampsCount);
887 }
888
889 void addFrameEvents(
890 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
891 FrameEvents* oldFrame =
892 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
893 FrameEvents* newFrame = &mFrames[iNewFrame];
894
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800895 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700896 uint64_t nNewFrame = iNewFrame + 1;
897
Brian Anderson4e606e32017-03-16 15:34:57 -0700898 // Latch, Composite, and Release the frames in a plausible order.
899 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700900 // that's okay for the purposes of this test.
901 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
902
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700903 // Composite the previous frame one more time, which helps verify
904 // LastRefresh is updated properly.
905 if (oldFrame != nullptr) {
906 mCfeh->addPreComposition(nOldFrame,
907 oldFrame->mRefreshes[2].kStartTime);
908 gpuDoneFenceTime = gpuComposited ?
909 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
910 FenceTime::NO_FENCE;
911 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800912 oldFrame->mRefreshes[2].mPresent.mFenceTime,
913 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700914 }
915
916 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700917 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
918
919 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
920 gpuDoneFenceTime = gpuComposited ?
921 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
922 FenceTime::NO_FENCE;
923 // HWC2 releases the previous buffer after a new latch just before
924 // calling postComposition.
925 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700926 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700927 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
928 }
929 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800930 newFrame->mRefreshes[0].mPresent.mFenceTime,
931 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700932
Brian Anderson3da8d272016-07-28 16:20:47 -0700933 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
934 gpuDoneFenceTime = gpuComposited ?
935 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
936 FenceTime::NO_FENCE;
937 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800938 newFrame->mRefreshes[1].mPresent.mFenceTime,
939 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700940 }
941
Brian Anderson3da8d272016-07-28 16:20:47 -0700942 sp<IGraphicBufferProducer> mProducer;
943 sp<IGraphicBufferConsumer> mConsumer;
944 sp<FakeConsumer> mFakeConsumer;
945 ConsumerFrameEventHistory* mCfeh;
946 sp<TestSurface> mSurface;
947 sp<ANativeWindow> mWindow;
948
949 FenceToFenceTimeMap mFenceMap;
950
951 bool mFrameTimestampsEnabled = false;
952
953 int64_t outRequestedPresentTime = -1;
954 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700955 int64_t outLatchTime = -1;
956 int64_t outFirstRefreshStartTime = -1;
957 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700958 int64_t outGpuCompositionDoneTime = -1;
959 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700960 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700961 int64_t outReleaseTime = -1;
962
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800963 FrameEvents mFrames[3] {
964 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700965};
966
967
968// This test verifies that the frame timestamps are not retrieved when not
969// explicitly enabled via native_window_enable_frame_timestamps.
970// We want to check this to make sure there's no overhead for users
971// that don't need the timestamp information.
972TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
973 int fence;
974 ANativeWindowBuffer* buffer;
975
976 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
977 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
978
Brian Anderson1049d1d2016-12-16 17:25:57 -0800979 const uint64_t fId = getNextFrameId();
980
Brian Anderson3da8d272016-07-28 16:20:47 -0700981 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
982 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
983 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
984 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
985
986 // Verify the producer doesn't get frame timestamps piggybacked on queue.
987 // It is okay that frame timestamps are added in the consumer since it is
988 // still needed for SurfaceFlinger dumps.
989 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
990 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
991 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
992
993 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800994 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700995 EXPECT_EQ(INVALID_OPERATION, result);
996 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800997
998 // Verify compositor timing query fails.
999 nsecs_t compositeDeadline = 0;
1000 nsecs_t compositeInterval = 0;
1001 nsecs_t compositeToPresentLatency = 0;
1002 result = native_window_get_compositor_timing(mWindow.get(),
1003 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1004 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001005}
1006
1007// This test verifies that the frame timestamps are retrieved if explicitly
1008// enabled via native_window_enable_frame_timestamps.
1009TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001010 CompositorTiming initialCompositorTiming {
1011 1000000000, // 1s deadline
1012 16666667, // 16ms interval
1013 50000000, // 50ms present latency
1014 };
1015 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1016
Brian Anderson3da8d272016-07-28 16:20:47 -07001017 enableFrameTimestamps();
1018
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001019 // Verify the compositor timing query gets the initial compositor values
1020 // after timststamps are enabled; even before the first frame is queued
1021 // or dequeued.
1022 nsecs_t compositeDeadline = 0;
1023 nsecs_t compositeInterval = 0;
1024 nsecs_t compositeToPresentLatency = 0;
1025 mSurface->setNow(initialCompositorTiming.deadline - 1);
1026 int result = native_window_get_compositor_timing(mWindow.get(),
1027 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1028 EXPECT_EQ(NO_ERROR, result);
1029 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1030 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1031 EXPECT_EQ(initialCompositorTiming.presentLatency,
1032 compositeToPresentLatency);
1033
Brian Anderson3da8d272016-07-28 16:20:47 -07001034 int fence;
1035 ANativeWindowBuffer* buffer;
1036
1037 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001038 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001039
Brian Anderson1049d1d2016-12-16 17:25:57 -08001040 const uint64_t fId1 = getNextFrameId();
1041
Brian Anderson3da8d272016-07-28 16:20:47 -07001042 // Verify getFrameTimestamps is piggybacked on dequeue.
1043 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1044 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001045 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001046
1047 NewFrameEventsEntry f1;
1048 f1.frameNumber = 1;
1049 f1.postedTime = mFrames[0].kPostedTime;
1050 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1051 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1052 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1053 mFrames[0].mAcquireProducer.mFenceTime,
1054 mFrames[0].mAcquireConsumer.mFenceTime);
1055 mFakeConsumer->mNewFrameEntryOverride = f1;
1056 mFrames[0].signalQueueFences();
1057
1058 // Verify getFrameTimestamps is piggybacked on queue.
1059 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1060 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1061 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001062 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001063
1064 // Verify queries for timestamps that the producer doesn't know about
1065 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001066 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001067 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001068 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001069}
1070
Brian Anderson6b376712017-04-04 10:51:39 -07001071TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1072 bool displayPresentSupported = true;
1073 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1074
1075 // Verify supported bits are forwarded.
1076 int supportsPresent = -1;
1077 mWindow.get()->query(mWindow.get(),
1078 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1079 EXPECT_EQ(displayPresentSupported, supportsPresent);
1080}
1081
1082TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1083 bool displayPresentSupported = false;
1084 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1085
1086 // Verify supported bits are forwarded.
1087 int supportsPresent = -1;
1088 mWindow.get()->query(mWindow.get(),
1089 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1090 EXPECT_EQ(displayPresentSupported, supportsPresent);
1091}
1092
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001093TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1094 nsecs_t phase = 4000;
1095 nsecs_t interval = 1000;
1096
1097 // Timestamp in previous interval.
1098 nsecs_t timestamp = 3500;
1099 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1100 timestamp, phase, interval));
1101
1102 // Timestamp in next interval.
1103 timestamp = 4500;
1104 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1105 timestamp, phase, interval));
1106
1107 // Timestamp multiple intervals before.
1108 timestamp = 2500;
1109 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1110 timestamp, phase, interval));
1111
1112 // Timestamp multiple intervals after.
1113 timestamp = 6500;
1114 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1115 timestamp, phase, interval));
1116
1117 // Timestamp on previous interval.
1118 timestamp = 3000;
1119 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1120 timestamp, phase, interval));
1121
1122 // Timestamp on next interval.
1123 timestamp = 5000;
1124 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1125 timestamp, phase, interval));
1126
1127 // Timestamp equal to phase.
1128 timestamp = 4000;
1129 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1130 timestamp, phase, interval));
1131}
1132
1133// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1134// if the number of intervals elapsed is internally stored in an int.
1135TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1136 nsecs_t phase = 0;
1137 nsecs_t interval = 4000;
1138 nsecs_t big_timestamp = 8635916564000;
1139 int32_t intervals = big_timestamp / interval;
1140
1141 EXPECT_LT(intervals, 0);
1142 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1143 big_timestamp, phase, interval));
1144 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1145 big_timestamp, big_timestamp, interval));
1146}
1147
1148// This verifies the compositor timing is updated by refresh events
1149// and piggy backed on a queue, dequeue, and enabling of timestamps..
1150TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1151 CompositorTiming initialCompositorTiming {
1152 1000000000, // 1s deadline
1153 16666667, // 16ms interval
1154 50000000, // 50ms present latency
1155 };
1156 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1157
1158 enableFrameTimestamps();
1159
1160 // We get the initial values before any frames are submitted.
1161 nsecs_t compositeDeadline = 0;
1162 nsecs_t compositeInterval = 0;
1163 nsecs_t compositeToPresentLatency = 0;
1164 mSurface->setNow(initialCompositorTiming.deadline - 1);
1165 int result = native_window_get_compositor_timing(mWindow.get(),
1166 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1167 EXPECT_EQ(NO_ERROR, result);
1168 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1169 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1170 EXPECT_EQ(initialCompositorTiming.presentLatency,
1171 compositeToPresentLatency);
1172
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001173 dequeueAndQueue(0);
1174 addFrameEvents(true, NO_FRAME_INDEX, 0);
1175
1176 // Still get the initial values because the frame events for frame 0
1177 // didn't get a chance to piggyback on a queue or dequeue yet.
1178 result = native_window_get_compositor_timing(mWindow.get(),
1179 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1180 EXPECT_EQ(NO_ERROR, result);
1181 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1182 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1183 EXPECT_EQ(initialCompositorTiming.presentLatency,
1184 compositeToPresentLatency);
1185
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001186 dequeueAndQueue(1);
1187 addFrameEvents(true, 0, 1);
1188
1189 // Now expect the composite values associated with frame 1.
1190 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1191 result = native_window_get_compositor_timing(mWindow.get(),
1192 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1193 EXPECT_EQ(NO_ERROR, result);
1194 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1195 compositeDeadline);
1196 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1197 compositeInterval);
1198 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1199 compositeToPresentLatency);
1200
1201 dequeueAndQueue(2);
1202 addFrameEvents(true, 1, 2);
1203
1204 // Now expect the composite values associated with frame 2.
1205 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1206 result = native_window_get_compositor_timing(mWindow.get(),
1207 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1208 EXPECT_EQ(NO_ERROR, result);
1209 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1210 compositeDeadline);
1211 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1212 compositeInterval);
1213 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1214 compositeToPresentLatency);
1215
1216 // Re-enabling frame timestamps should get the latest values.
1217 disableFrameTimestamps();
1218 enableFrameTimestamps();
1219
1220 // Now expect the composite values associated with frame 3.
1221 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1222 result = native_window_get_compositor_timing(mWindow.get(),
1223 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1224 EXPECT_EQ(NO_ERROR, result);
1225 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1226 compositeDeadline);
1227 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1228 compositeInterval);
1229 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1230 compositeToPresentLatency);
1231}
1232
1233// This verifies the compositor deadline properly snaps to the the next
1234// deadline based on the current time.
1235TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1236 CompositorTiming initialCompositorTiming {
1237 1000000000, // 1s deadline
1238 16666667, // 16ms interval
1239 50000000, // 50ms present latency
1240 };
1241 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1242
1243 enableFrameTimestamps();
1244
1245 nsecs_t compositeDeadline = 0;
1246 nsecs_t compositeInterval = 0;
1247 nsecs_t compositeToPresentLatency = 0;
1248
1249 // A "now" just before the deadline snaps to the deadline.
1250 mSurface->setNow(initialCompositorTiming.deadline - 1);
1251 int result = native_window_get_compositor_timing(mWindow.get(),
1252 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1253 EXPECT_EQ(NO_ERROR, result);
1254 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1255 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1256 EXPECT_EQ(expectedDeadline, compositeDeadline);
1257
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001258 dequeueAndQueue(0);
1259 addFrameEvents(true, NO_FRAME_INDEX, 0);
1260
1261 // A "now" just after the deadline snaps properly.
1262 mSurface->setNow(initialCompositorTiming.deadline + 1);
1263 result = native_window_get_compositor_timing(mWindow.get(),
1264 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1265 EXPECT_EQ(NO_ERROR, result);
1266 expectedDeadline =
1267 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1268 EXPECT_EQ(expectedDeadline, compositeDeadline);
1269
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001270 dequeueAndQueue(1);
1271 addFrameEvents(true, 0, 1);
1272
1273 // A "now" just after the next interval snaps properly.
1274 mSurface->setNow(
1275 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1276 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1277 result = native_window_get_compositor_timing(mWindow.get(),
1278 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1279 EXPECT_EQ(NO_ERROR, result);
1280 expectedDeadline =
1281 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1282 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1283 EXPECT_EQ(expectedDeadline, compositeDeadline);
1284
1285 dequeueAndQueue(2);
1286 addFrameEvents(true, 1, 2);
1287
1288 // A "now" over 1 interval before the deadline snaps properly.
1289 mSurface->setNow(
1290 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1291 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1292 result = native_window_get_compositor_timing(mWindow.get(),
1293 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1294 EXPECT_EQ(NO_ERROR, result);
1295 expectedDeadline =
1296 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1297 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1298 EXPECT_EQ(expectedDeadline, compositeDeadline);
1299
1300 // Re-enabling frame timestamps should get the latest values.
1301 disableFrameTimestamps();
1302 enableFrameTimestamps();
1303
1304 // A "now" over 2 intervals before the deadline snaps properly.
1305 mSurface->setNow(
1306 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1307 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1308 result = native_window_get_compositor_timing(mWindow.get(),
1309 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1310 EXPECT_EQ(NO_ERROR, result);
1311 expectedDeadline =
1312 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1313 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1314 EXPECT_EQ(expectedDeadline, compositeDeadline);
1315}
1316
Brian Anderson1049d1d2016-12-16 17:25:57 -08001317// This verifies the timestamps recorded in the consumer's
1318// FrameTimestampsHistory are properly retrieved by the producer for the
1319// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001320TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1321 enableFrameTimestamps();
1322
Brian Anderson1049d1d2016-12-16 17:25:57 -08001323 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001324 dequeueAndQueue(0);
1325 mFrames[0].signalQueueFences();
1326
Brian Anderson1049d1d2016-12-16 17:25:57 -08001327 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001328 dequeueAndQueue(1);
1329 mFrames[1].signalQueueFences();
1330
1331 addFrameEvents(true, NO_FRAME_INDEX, 0);
1332 mFrames[0].signalRefreshFences();
1333 addFrameEvents(true, 0, 1);
1334 mFrames[0].signalReleaseFences();
1335 mFrames[1].signalRefreshFences();
1336
1337 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001338 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001339 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001340 EXPECT_EQ(NO_ERROR, result);
1341 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1342 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001343 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1344 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1345 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001346 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1347 outGpuCompositionDoneTime);
1348 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001349 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001350 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1351
1352 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001353 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001354 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001355 EXPECT_EQ(NO_ERROR, result);
1356 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1357 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001358 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1359 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1360 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001361 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1362 outGpuCompositionDoneTime);
1363 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001364 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1365 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001366}
1367
1368// This test verifies the acquire fence recorded by the consumer is not sent
1369// back to the producer and the producer saves its own fence.
1370TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1371 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001372
Brian Anderson3da8d272016-07-28 16:20:47 -07001373 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001374 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001375 dequeueAndQueue(0);
1376
1377 // Verify queue-related timestamps for f1 are available immediately in the
1378 // producer without asking the consumer again, even before signaling the
1379 // acquire fence.
1380 resetTimestamps();
1381 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001382 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001383 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001384 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001385 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1386 EXPECT_EQ(NO_ERROR, result);
1387 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001388 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001389
1390 // Signal acquire fences. Verify a sync call still isn't necessary.
1391 mFrames[0].signalQueueFences();
1392
1393 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001394 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001395 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001396 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001397 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1398 EXPECT_EQ(NO_ERROR, result);
1399 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1400 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1401
1402 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001403 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001404 dequeueAndQueue(1);
1405
1406 // Verify queue-related timestamps for f2 are available immediately in the
1407 // producer without asking the consumer again, even before signaling the
1408 // acquire fence.
1409 resetTimestamps();
1410 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001411 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001412 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001413 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001414 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1415 EXPECT_EQ(NO_ERROR, result);
1416 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001417 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001418
1419 // Signal acquire fences. Verify a sync call still isn't necessary.
1420 mFrames[1].signalQueueFences();
1421
1422 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001423 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001424 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001425 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001426 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1427 EXPECT_EQ(NO_ERROR, result);
1428 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1429 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1430}
1431
1432TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1433 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001434
1435 // Dequeue and queue frame 1.
1436 dequeueAndQueue(0);
1437 mFrames[0].signalQueueFences();
1438
1439 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001440 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001441 dequeueAndQueue(1);
1442 mFrames[1].signalQueueFences();
1443
1444 addFrameEvents(true, NO_FRAME_INDEX, 0);
1445 mFrames[0].signalRefreshFences();
1446 addFrameEvents(true, 0, 1);
1447 mFrames[0].signalReleaseFences();
1448 mFrames[1].signalRefreshFences();
1449
1450 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001451 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001452 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001453 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1454 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001455 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001456 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1457}
1458
1459// This test verifies that fences can signal and update timestamps producer
1460// side without an additional sync call to the consumer.
1461TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1462 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001463
1464 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001465 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001466 dequeueAndQueue(0);
1467 mFrames[0].signalQueueFences();
1468
1469 // Dequeue and queue frame 2.
1470 dequeueAndQueue(1);
1471 mFrames[1].signalQueueFences();
1472
1473 addFrameEvents(true, NO_FRAME_INDEX, 0);
1474 addFrameEvents(true, 0, 1);
1475
1476 // Verify available timestamps are correct for frame 1, before any
1477 // fence has been signaled.
1478 // Note: A sync call is necessary here since the events triggered by
1479 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001480 resetTimestamps();
1481 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001482 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001483 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1484 EXPECT_EQ(NO_ERROR, result);
1485 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1486 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001487 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1488 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1489 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001490 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1491 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001492 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001493 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001494
1495 // Verify available timestamps are correct for frame 1 again, before any
1496 // fence has been signaled.
1497 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001498 resetTimestamps();
1499 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001500 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001501 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1502 EXPECT_EQ(NO_ERROR, result);
1503 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1504 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001505 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1506 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1507 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001508 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1509 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001510 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001511 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001512
1513 // Signal the fences for frame 1.
1514 mFrames[0].signalRefreshFences();
1515 mFrames[0].signalReleaseFences();
1516
1517 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001518 resetTimestamps();
1519 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001520 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001521 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1522 EXPECT_EQ(NO_ERROR, result);
1523 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1524 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001525 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1526 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1527 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001528 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1529 outGpuCompositionDoneTime);
1530 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001531 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001532 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1533}
1534
1535// This test verifies that if the frame wasn't GPU composited but has a refresh
1536// event a sync call isn't made to get the GPU composite done time since it will
1537// never exist.
1538TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1539 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001540
Brian Anderson3da8d272016-07-28 16:20:47 -07001541 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001542 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001543 dequeueAndQueue(0);
1544 mFrames[0].signalQueueFences();
1545
1546 // Dequeue and queue frame 2.
1547 dequeueAndQueue(1);
1548 mFrames[1].signalQueueFences();
1549
1550 addFrameEvents(false, NO_FRAME_INDEX, 0);
1551 addFrameEvents(false, 0, 1);
1552
1553 // Verify available timestamps are correct for frame 1, before any
1554 // fence has been signaled.
1555 // Note: A sync call is necessary here since the events triggered by
1556 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1557 resetTimestamps();
1558 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001559 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001560 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1561 EXPECT_EQ(NO_ERROR, result);
1562 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1563 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001564 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1565 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1566 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001567 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1568 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001569 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001570 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001571
1572 // Signal the fences for frame 1.
1573 mFrames[0].signalRefreshFences();
1574 mFrames[0].signalReleaseFences();
1575
1576 // Verify all timestamps, except GPU composition, are available without a
1577 // sync call.
1578 resetTimestamps();
1579 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001580 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001581 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1582 EXPECT_EQ(NO_ERROR, result);
1583 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1584 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001585 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1586 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1587 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001588 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001589 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001590 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001591 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1592}
1593
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001594// This test verifies that if the certain timestamps can't possibly exist for
1595// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001596TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001597 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001598
1599 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001600 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001601 dequeueAndQueue(0);
1602 mFrames[0].signalQueueFences();
1603
1604 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001605 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001606 dequeueAndQueue(1);
1607 mFrames[1].signalQueueFences();
1608
1609 addFrameEvents(false, NO_FRAME_INDEX, 0);
1610 addFrameEvents(false, 0, 1);
1611
1612 // Verify available timestamps are correct for frame 1, before any
1613 // fence has been signaled.
1614 // Note: A sync call is necessary here since the events triggered by
1615 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001616 resetTimestamps();
1617 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001618 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001619 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1620 EXPECT_EQ(NO_ERROR, result);
1621 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1622 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001623 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1624 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1625 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001626 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1627 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001628 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001629 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001630
1631 mFrames[0].signalRefreshFences();
1632 mFrames[0].signalReleaseFences();
1633 mFrames[1].signalRefreshFences();
1634
Brian Anderson1049d1d2016-12-16 17:25:57 -08001635 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001636 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001637 // available, a sync call should not occur because it's not possible for f2
1638 // to encounter the final value for those events until another frame is
1639 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001640 resetTimestamps();
1641 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001642 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001643 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1644 EXPECT_EQ(NO_ERROR, result);
1645 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1646 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001647 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1648 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1649 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001650 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001651 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001652 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1653 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001654}
1655
Brian Anderson6b376712017-04-04 10:51:39 -07001656// This test verifies there are no sync calls for present times
1657// when they aren't supported and that an error is returned.
1658
1659TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1660 enableFrameTimestamps();
1661 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1662
1663 // Dequeue and queue frame 1.
1664 const uint64_t fId1 = getNextFrameId();
1665 dequeueAndQueue(0);
1666
1667 // Verify a query for the Present times do not trigger a sync call if they
1668 // are not supported.
1669 resetTimestamps();
1670 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1671 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1672 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1673 &outDisplayPresentTime, nullptr, nullptr);
1674 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1675 EXPECT_EQ(BAD_VALUE, result);
1676 EXPECT_EQ(-1, outDisplayPresentTime);
1677}
1678
Dan Stoza932f0082017-05-31 13:50:16 -07001679} // namespace android