blob: 0fd42317a99c4a3540a3f866ab232adaac7cefa5 [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 Linfd997e02018-03-28 15:29:00 -070045using ui::ColorMode;
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060046
Robert Carr4cdc58f2017-08-23 14:22:20 -070047using Transaction = SurfaceComposerClient::Transaction;
48
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060049static bool hasWideColorDisplay =
50 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
Kalle Raita643f0942016-12-07 09:20:14 -080051
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -070052static bool hasHdrDisplay =
53 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
54
Brian Anderson3da8d272016-07-28 16:20:47 -070055class FakeSurfaceComposer;
56class FakeProducerFrameEventHistory;
57
58static constexpr uint64_t NO_FRAME_INDEX = std::numeric_limits<uint64_t>::max();
59
Jamie Gennis134f0422011-03-08 12:18:54 -080060class SurfaceTest : public ::testing::Test {
61protected:
Mathias Agopian7c1a4872013-03-20 15:56:04 -070062 SurfaceTest() {
63 ProcessState::self()->startThreadPool();
64 }
65
Jamie Gennis134f0422011-03-08 12:18:54 -080066 virtual void SetUp() {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080067 mComposerClient = new SurfaceComposerClient;
Jamie Gennis134f0422011-03-08 12:18:54 -080068 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
69
Brian Andersond0010582017-03-07 13:20:31 -080070 // TODO(brianderson): The following sometimes fails and is a source of
71 // test flakiness.
Jamie Gennisfc850122011-04-25 16:40:05 -070072 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070073 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis134f0422011-03-08 12:18:54 -080074
75 ASSERT_TRUE(mSurfaceControl != NULL);
76 ASSERT_TRUE(mSurfaceControl->isValid());
77
Robert Carr4cdc58f2017-08-23 14:22:20 -070078 Transaction t;
79 ASSERT_EQ(NO_ERROR, t.setLayer(mSurfaceControl, 0x7fffffff)
80 .show(mSurfaceControl)
81 .apply());
Jamie Gennis134f0422011-03-08 12:18:54 -080082
83 mSurface = mSurfaceControl->getSurface();
84 ASSERT_TRUE(mSurface != NULL);
85 }
86
87 virtual void TearDown() {
88 mComposerClient->dispose();
89 }
90
91 sp<Surface> mSurface;
92 sp<SurfaceComposerClient> mComposerClient;
93 sp<SurfaceControl> mSurfaceControl;
94};
95
Robert Carr740eaf02018-03-27 12:59:18 -070096TEST_F(SurfaceTest, CreateSurfaceReturnsErrorBadClient) {
97 mComposerClient->dispose();
98 ASSERT_EQ(NO_INIT, mComposerClient->initCheck());
99
100 sp<SurfaceControl> sc;
101 status_t err = mComposerClient->createSurfaceChecked(
102 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, &sc, 0);
103 ASSERT_EQ(NO_INIT, err);
104}
105
Jamie Gennis134f0422011-03-08 12:18:54 -0800106TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
107 sp<ANativeWindow> anw(mSurface);
108 int result = -123;
109 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
110 &result);
111 EXPECT_EQ(NO_ERROR, err);
112 EXPECT_EQ(1, result);
113}
114
115TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
116 mSurfaceControl.clear();
Kalle Raita643f0942016-12-07 09:20:14 -0800117 // Wait for the async clean-up to complete.
118 std::this_thread::sleep_for(50ms);
Jamie Gennis134f0422011-03-08 12:18:54 -0800119
120 sp<ANativeWindow> anw(mSurface);
121 int result = -123;
122 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
123 &result);
124 EXPECT_EQ(NO_ERROR, err);
125 EXPECT_EQ(1, result);
126}
127
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800128// This test probably doesn't belong here.
Robert Carr0283c732019-04-02 16:32:58 -0700129TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersDontSucceed) {
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;
Robert Carr0283c732019-04-02 16:32:58 -0700137 bool ignored;
138 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &outBuffer, ignored, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800139 64, 64, 0, 0x7fffffff, 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));
Iliyan Malchev697526b2011-05-01 11:33:26 -0700149 ANativeWindowBuffer* buf = 0;
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 }
Robert Carr0283c732019-04-02 16:32:58 -0700169 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &outBuffer, ignored, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800170 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800171}
172
Jamie Gennis391bbe22011-03-14 15:00:06 -0700173TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
174 sp<ANativeWindow> anw(mSurface);
175 int result = -123;
176 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
177 EXPECT_EQ(NO_ERROR, err);
178 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
179}
180
Craig Donner6ebc46a2016-10-21 15:23:44 -0700181TEST_F(SurfaceTest, LayerCountIsOne) {
182 sp<ANativeWindow> anw(mSurface);
183 int result = -123;
184 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
185 EXPECT_EQ(NO_ERROR, err);
186 EXPECT_EQ(1, result);
187}
188
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700189TEST_F(SurfaceTest, QueryConsumerUsage) {
190 const int TEST_USAGE_FLAGS =
191 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700192 sp<IGraphicBufferProducer> producer;
193 sp<IGraphicBufferConsumer> consumer;
194 BufferQueue::createBufferQueue(&producer, &consumer);
195 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700196 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700197 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700198
199 sp<ANativeWindow> anw(s);
200
201 int flags = -1;
202 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
203
204 ASSERT_EQ(NO_ERROR, err);
205 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
206}
207
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800208TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
209 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
210 sp<IGraphicBufferProducer> producer;
211 sp<IGraphicBufferConsumer> consumer;
212 BufferQueue::createBufferQueue(&producer, &consumer);
213 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
214
215 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
216
217 sp<Surface> s = new Surface(producer);
218
219 sp<ANativeWindow> anw(s);
220
221 android_dataspace dataSpace;
222
223 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
224 reinterpret_cast<int*>(&dataSpace));
225
226 ASSERT_EQ(NO_ERROR, err);
227 ASSERT_EQ(TEST_DATASPACE, dataSpace);
228}
229
Dan Stoza812ed062015-06-02 15:45:22 -0700230TEST_F(SurfaceTest, SettingGenerationNumber) {
231 sp<IGraphicBufferProducer> producer;
232 sp<IGraphicBufferConsumer> consumer;
233 BufferQueue::createBufferQueue(&producer, &consumer);
234 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
235 sp<Surface> surface = new Surface(producer);
236 sp<ANativeWindow> window(surface);
237
238 // Allocate a buffer with a generation number of 0
239 ANativeWindowBuffer* buffer;
240 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700241 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
242 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700243 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
244 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
245
246 // Detach the buffer and check its generation number
247 sp<GraphicBuffer> graphicBuffer;
248 sp<Fence> fence;
249 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
250 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
251
252 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
253 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
254
255 // This should change the generation number of the GraphicBuffer
256 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
257
258 // Check that the new generation number sticks with the buffer
259 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
260 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
261 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
262 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
263}
264
Dan Stozac6f30bd2015-06-08 09:32:50 -0700265TEST_F(SurfaceTest, GetConsumerName) {
266 sp<IGraphicBufferProducer> producer;
267 sp<IGraphicBufferConsumer> consumer;
268 BufferQueue::createBufferQueue(&producer, &consumer);
269
270 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
271 consumer->consumerConnect(dummyConsumer, false);
272 consumer->setConsumerName(String8("TestConsumer"));
273
274 sp<Surface> surface = new Surface(producer);
275 sp<ANativeWindow> window(surface);
276 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
277
278 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
279}
280
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700281TEST_F(SurfaceTest, GetWideColorSupport) {
282 sp<IGraphicBufferProducer> producer;
283 sp<IGraphicBufferConsumer> consumer;
284 BufferQueue::createBufferQueue(&producer, &consumer);
285
286 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
287 consumer->consumerConnect(dummyConsumer, false);
288 consumer->setConsumerName(String8("TestConsumer"));
289
290 sp<Surface> surface = new Surface(producer);
291 sp<ANativeWindow> window(surface);
292 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
293
294 bool supported;
295 surface->getWideColorSupport(&supported);
296
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -0600297 // NOTE: This test assumes that device that supports
298 // wide-color (as indicated by BoardConfig) must also
299 // have a wide-color primary display.
300 // That assumption allows this test to cover devices
301 // that advertised a wide-color color mode without
302 // actually supporting wide-color to pass this test
303 // as well as the case of a device that does support
304 // wide-color (via BoardConfig) and has a wide-color
305 // primary display.
306 // NOT covered at this time is a device that supports
307 // wide color in the BoardConfig but does not support
308 // a wide-color color mode on the primary display.
309 ASSERT_EQ(hasWideColorDisplay, supported);
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700310}
311
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700312TEST_F(SurfaceTest, GetHdrSupport) {
313 sp<IGraphicBufferProducer> producer;
314 sp<IGraphicBufferConsumer> consumer;
315 BufferQueue::createBufferQueue(&producer, &consumer);
316
317 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
318 consumer->consumerConnect(dummyConsumer, false);
319 consumer->setConsumerName(String8("TestConsumer"));
320
321 sp<Surface> surface = new Surface(producer);
322 sp<ANativeWindow> window(surface);
323 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
324
325 bool supported;
326 status_t result = surface->getHdrSupport(&supported);
327 ASSERT_EQ(NO_ERROR, result);
328
329 // NOTE: This is not a CTS test.
330 // This test verifies that when the BoardConfig TARGET_HAS_HDR_DISPLAY
331 // is TRUE, getHdrSupport is also true.
332 // TODO: Add check for an HDR color mode on the primary display.
333 ASSERT_EQ(hasHdrDisplay, supported);
334}
335
336TEST_F(SurfaceTest, SetHdrMetadata) {
337 sp<IGraphicBufferProducer> producer;
338 sp<IGraphicBufferConsumer> consumer;
339 BufferQueue::createBufferQueue(&producer, &consumer);
340
341 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
342 consumer->consumerConnect(dummyConsumer, false);
343 consumer->setConsumerName(String8("TestConsumer"));
344
345 sp<Surface> surface = new Surface(producer);
346 sp<ANativeWindow> window(surface);
347 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
348
349 bool supported;
350 status_t result = surface->getHdrSupport(&supported);
351 ASSERT_EQ(NO_ERROR, result);
352
353 if (!hasHdrDisplay || !supported) {
354 return;
355 }
356 const android_smpte2086_metadata smpte2086 = {
357 {0.680, 0.320},
358 {0.265, 0.690},
359 {0.150, 0.060},
360 {0.3127, 0.3290},
361 100.0,
362 0.1,
363 };
364 const android_cta861_3_metadata cta861_3 = {
365 78.0,
366 62.0,
367 };
368 int error = native_window_set_buffers_smpte2086_metadata(window.get(), &smpte2086);
369 ASSERT_EQ(error, NO_ERROR);
370 error = native_window_set_buffers_cta861_3_metadata(window.get(), &cta861_3);
371 ASSERT_EQ(error, NO_ERROR);
372}
373
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800374TEST_F(SurfaceTest, DynamicSetBufferCount) {
375 sp<IGraphicBufferProducer> producer;
376 sp<IGraphicBufferConsumer> consumer;
377 BufferQueue::createBufferQueue(&producer, &consumer);
378
379 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
380 consumer->consumerConnect(dummyConsumer, false);
381 consumer->setConsumerName(String8("TestConsumer"));
382
383 sp<Surface> surface = new Surface(producer);
384 sp<ANativeWindow> window(surface);
385
386 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
387 NATIVE_WINDOW_API_CPU));
388 native_window_set_buffer_count(window.get(), 4);
389
390 int fence;
391 ANativeWindowBuffer* buffer;
392 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
393 native_window_set_buffer_count(window.get(), 3);
394 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
395 native_window_set_buffer_count(window.get(), 2);
396 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
397 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
398}
399
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700400TEST_F(SurfaceTest, GetAndFlushRemovedBuffers) {
401 sp<IGraphicBufferProducer> producer;
402 sp<IGraphicBufferConsumer> consumer;
403 BufferQueue::createBufferQueue(&producer, &consumer);
404
405 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
406 consumer->consumerConnect(dummyConsumer, false);
407 consumer->setConsumerName(String8("TestConsumer"));
408
409 sp<Surface> surface = new Surface(producer);
410 sp<ANativeWindow> window(surface);
411 sp<DummyProducerListener> listener = new DummyProducerListener();
412 ASSERT_EQ(OK, surface->connect(
413 NATIVE_WINDOW_API_CPU,
414 /*listener*/listener,
415 /*reportBufferRemoval*/true));
416 const int BUFFER_COUNT = 4;
417 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
418
419 sp<GraphicBuffer> detachedBuffer;
420 sp<Fence> outFence;
421 int fences[BUFFER_COUNT];
422 ANativeWindowBuffer* buffers[BUFFER_COUNT];
423 // Allocate buffers because detachNextBuffer requires allocated buffers
424 for (int i = 0; i < BUFFER_COUNT; i++) {
425 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
426 }
427 for (int i = 0; i < BUFFER_COUNT; i++) {
428 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
429 }
430
431 // Test detached buffer is correctly reported
432 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
433 std::vector<sp<GraphicBuffer>> removedBuffers;
434 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
435 ASSERT_EQ(1u, removedBuffers.size());
436 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
437 // Test the list is flushed one getAndFlushRemovedBuffers returns
438 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
439 ASSERT_EQ(0u, removedBuffers.size());
440
441
442 // Test removed buffer list is cleanup after next dequeueBuffer call
443 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
444 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[0], &fences[0]));
445 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
446 ASSERT_EQ(0u, removedBuffers.size());
447 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[0], fences[0]));
448
449 // Test removed buffer list is cleanup after next detachNextBuffer call
450 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
451 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
452 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
453 ASSERT_EQ(1u, removedBuffers.size());
454 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
455
456 // Re-allocate buffers since all buffers are detached up to now
457 for (int i = 0; i < BUFFER_COUNT; i++) {
458 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
459 }
460 for (int i = 0; i < BUFFER_COUNT; i++) {
461 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
462 }
463
464 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
465 ASSERT_EQ(NO_ERROR, surface->attachBuffer(detachedBuffer.get()));
466 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
467 // Depends on which slot GraphicBufferProducer impl pick, the attach call might
468 // get 0 or 1 buffer removed.
469 ASSERT_LE(removedBuffers.size(), 1u);
470}
Brian Anderson3da8d272016-07-28 16:20:47 -0700471
Dan Stoza932f0082017-05-31 13:50:16 -0700472TEST_F(SurfaceTest, TestGetLastDequeueStartTime) {
473 sp<ANativeWindow> anw(mSurface);
474 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU));
475
476 ANativeWindowBuffer* buffer = nullptr;
477 int32_t fenceFd = -1;
478
479 nsecs_t before = systemTime(CLOCK_MONOTONIC);
480 anw->dequeueBuffer(anw.get(), &buffer, &fenceFd);
481 nsecs_t after = systemTime(CLOCK_MONOTONIC);
482
483 nsecs_t lastDequeueTime = mSurface->getLastDequeueStartTime();
484 ASSERT_LE(before, lastDequeueTime);
485 ASSERT_GE(after, lastDequeueTime);
486}
487
Brian Anderson3da8d272016-07-28 16:20:47 -0700488class FakeConsumer : public BnConsumerListener {
489public:
490 void onFrameAvailable(const BufferItem& /*item*/) override {}
491 void onBuffersReleased() override {}
492 void onSidebandStreamChanged() override {}
493
494 void addAndGetFrameTimestamps(
495 const NewFrameEventsEntry* newTimestamps,
496 FrameEventHistoryDelta* outDelta) override {
497 if (newTimestamps) {
498 if (mGetFrameTimestampsEnabled) {
499 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
500 "Test should set mNewFrameEntryOverride before queuing "
501 "a frame.";
502 EXPECT_EQ(newTimestamps->frameNumber,
503 mNewFrameEntryOverride.frameNumber) <<
504 "Test attempting to add NewFrameEntryOverride with "
505 "incorrect frame number.";
506 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
507 mNewFrameEntryOverride.frameNumber = 0;
508 }
509 mAddFrameTimestampsCount++;
510 mLastAddedFrameNumber = newTimestamps->frameNumber;
511 }
512 if (outDelta) {
513 mFrameEventHistory.getAndResetDelta(outDelta);
514 mGetFrameTimestampsCount++;
515 }
516 mAddAndGetFrameTimestampsCallCount++;
517 }
518
519 bool mGetFrameTimestampsEnabled = false;
520
521 ConsumerFrameEventHistory mFrameEventHistory;
522 int mAddAndGetFrameTimestampsCallCount = 0;
523 int mAddFrameTimestampsCount = 0;
524 int mGetFrameTimestampsCount = 0;
525 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
526
527 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
528};
529
530
531class FakeSurfaceComposer : public ISurfaceComposer{
532public:
533 ~FakeSurfaceComposer() override {}
534
Brian Anderson6b376712017-04-04 10:51:39 -0700535 void setSupportsPresent(bool supportsPresent) {
536 mSupportsPresent = supportsPresent;
537 }
538
Brian Anderson3da8d272016-07-28 16:20:47 -0700539 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800540 sp<ISurfaceComposerClient> createScopedConnection(
541 const sp<IGraphicBufferProducer>& /* parent */) override {
542 return nullptr;
543 }
Jorim Jaggib1e2f8d2017-06-08 15:43:59 -0700544 sp<IDisplayEventConnection> createDisplayEventConnection(ISurfaceComposer::VsyncSource)
545 override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700546 return nullptr;
547 }
548 sp<IBinder> createDisplay(const String8& /*displayName*/,
549 bool /*secure*/) override { return nullptr; }
550 void destroyDisplay(const sp<IBinder>& /*display */) override {}
551 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
552 void setTransactionState(const Vector<ComposerState>& /*state*/,
553 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
554 override {}
555 void bootFinished() override {}
556 bool authenticateSurfaceTexture(
557 const sp<IGraphicBufferProducer>& /*surface*/) const override {
558 return false;
559 }
Brian Anderson6b376712017-04-04 10:51:39 -0700560
561 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
562 const override {
563 *outSupported = {
564 FrameEvent::REQUESTED_PRESENT,
565 FrameEvent::ACQUIRE,
566 FrameEvent::LATCH,
567 FrameEvent::FIRST_REFRESH_START,
568 FrameEvent::LAST_REFRESH_START,
569 FrameEvent::GPU_COMPOSITION_DONE,
570 FrameEvent::DEQUEUE_READY,
571 FrameEvent::RELEASE
572 };
573 if (mSupportsPresent) {
574 outSupported->push_back(
575 FrameEvent::DISPLAY_PRESENT);
576 }
577 return NO_ERROR;
578 }
579
Brian Anderson3da8d272016-07-28 16:20:47 -0700580 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
581 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
582 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
583 status_t getDisplayStats(const sp<IBinder>& /*display*/,
584 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
Yiwei Zhang1d465af2018-08-21 15:15:42 -0700585 status_t getDisplayViewport(const sp<IBinder>& /*display*/, Rect* /*outViewport*/) override {
586 return NO_ERROR;
587 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700588 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
589 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
590 override {
591 return NO_ERROR;
592 }
593 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700594 Vector<ColorMode>* /*outColorModes*/) override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700595 return NO_ERROR;
596 }
Peiyong Lina52f0292018-03-14 17:26:31 -0700597 ColorMode getActiveColorMode(const sp<IBinder>& /*display*/)
Brian Anderson3da8d272016-07-28 16:20:47 -0700598 override {
Peiyong Lina52f0292018-03-14 17:26:31 -0700599 return ColorMode::NATIVE;
Brian Anderson3da8d272016-07-28 16:20:47 -0700600 }
601 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700602 ColorMode /*colorMode*/) override { return NO_ERROR; }
Brian Anderson3da8d272016-07-28 16:20:47 -0700603 status_t captureScreen(const sp<IBinder>& /*display*/,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000604 sp<GraphicBuffer>* /*outBuffer*/,
Robert Carr0283c732019-04-02 16:32:58 -0700605 bool& /* outCapturedSecureLayers */,
Brian Anderson3da8d272016-07-28 16:20:47 -0700606 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800607 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700608 bool /*useIdentityTransform*/,
Robert Carr9085ced2019-02-19 10:05:00 -0800609 Rotation /*rotation*/,
610 bool /*captureSecureLayers*/) override { return NO_ERROR; }
chaviwa76b2712017-09-20 12:02:26 -0700611 virtual status_t captureLayers(const sp<IBinder>& /*parentHandle*/,
Robert Carr578038f2018-03-09 12:25:24 -0800612 sp<GraphicBuffer>* /*outBuffer*/, const Rect& /*sourceCrop*/,
613 float /*frameScale*/, 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 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700631
632protected:
633 IBinder* onAsBinder() override { return nullptr; }
634
635private:
636 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700637};
638
639class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
640public:
Chih-Hung Hsiehaaf62162018-12-20 15:45:04 -0800641 explicit FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap) : mFenceMap(fenceMap) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700642
643 ~FakeProducerFrameEventHistory() {}
644
645 void updateAcquireFence(uint64_t frameNumber,
646 std::shared_ptr<FenceTime>&& acquire) override {
647 // Verify the acquire fence being added isn't the one from the consumer.
648 EXPECT_NE(mConsumerAcquireFence, acquire);
649 // Override the fence, so we can verify this was called by the
650 // producer after the frame is queued.
651 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
652 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
653 }
654
655 void setAcquireFenceOverride(
656 const std::shared_ptr<FenceTime>& acquireFenceOverride,
657 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
658 mAcquireFenceOverride = acquireFenceOverride;
659 mConsumerAcquireFence = consumerAcquireFence;
660 }
661
662protected:
663 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
664 const override {
665 return mFenceMap->createFenceTimeForTest(fence);
666 }
667
668 FenceToFenceTimeMap* mFenceMap{nullptr};
669
670 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
671 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
672};
673
674
675class TestSurface : public Surface {
676public:
677 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
678 FenceToFenceTimeMap* fenceMap)
679 : Surface(bufferProducer),
680 mFakeSurfaceComposer(new FakeSurfaceComposer) {
681 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
682 mFrameEventHistory.reset(mFakeFrameEventHistory);
683 }
684
685 ~TestSurface() override {}
686
687 sp<ISurfaceComposer> composerService() const override {
688 return mFakeSurfaceComposer;
689 }
690
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800691 nsecs_t now() const override {
692 return mNow;
693 }
694
695 void setNow(nsecs_t now) {
696 mNow = now;
697 }
698
Brian Anderson3da8d272016-07-28 16:20:47 -0700699public:
700 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800701 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700702
703 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
704 // but this raw pointer gives access to test functionality.
705 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
706};
707
708
Brian Andersond0010582017-03-07 13:20:31 -0800709class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700710protected:
711 struct FenceAndFenceTime {
712 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
713 : mFence(new Fence),
714 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
715 sp<Fence> mFence { nullptr };
716 std::shared_ptr<FenceTime> mFenceTime { nullptr };
717 };
718
719 struct RefreshEvents {
720 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800721 : mFenceMap(fenceMap),
722 kCompositorTiming(
723 {refreshStart, refreshStart + 1, refreshStart + 2 }),
724 kStartTime(refreshStart + 3),
725 kGpuCompositionDoneTime(refreshStart + 4),
726 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700727
728 void signalPostCompositeFences() {
729 mFenceMap.signalAllForTest(
730 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
731 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
732 }
733
734 FenceToFenceTimeMap& mFenceMap;
735
736 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
737 FenceAndFenceTime mPresent { mFenceMap };
738
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800739 const CompositorTiming kCompositorTiming;
740
Brian Anderson3da8d272016-07-28 16:20:47 -0700741 const nsecs_t kStartTime;
742 const nsecs_t kGpuCompositionDoneTime;
743 const nsecs_t kPresentTime;
744 };
745
746 struct FrameEvents {
747 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
748 : mFenceMap(fenceMap),
749 kPostedTime(frameStartTime + 100),
750 kRequestedPresentTime(frameStartTime + 200),
751 kProducerAcquireTime(frameStartTime + 300),
752 kConsumerAcquireTime(frameStartTime + 301),
753 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700754 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700755 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700756 mRefreshes {
757 { mFenceMap, frameStartTime + 410 },
758 { mFenceMap, frameStartTime + 420 },
759 { mFenceMap, frameStartTime + 430 } } {}
760
761 void signalQueueFences() {
762 mFenceMap.signalAllForTest(
763 mAcquireConsumer.mFence, kConsumerAcquireTime);
764 mFenceMap.signalAllForTest(
765 mAcquireProducer.mFence, kProducerAcquireTime);
766 }
767
768 void signalRefreshFences() {
769 for (auto& re : mRefreshes) {
770 re.signalPostCompositeFences();
771 }
772 }
773
774 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700775 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
776 }
777
778 FenceToFenceTimeMap& mFenceMap;
779
780 FenceAndFenceTime mAcquireConsumer { mFenceMap };
781 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700782 FenceAndFenceTime mRelease { mFenceMap };
783
784 const nsecs_t kPostedTime;
785 const nsecs_t kRequestedPresentTime;
786 const nsecs_t kProducerAcquireTime;
787 const nsecs_t kConsumerAcquireTime;
788 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700789 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700790 const nsecs_t kReleaseTime;
791
792 RefreshEvents mRefreshes[3];
793 };
794
Brian Andersond0010582017-03-07 13:20:31 -0800795 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700796
797 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700798 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
799 mFakeConsumer = new FakeConsumer;
800 mCfeh = &mFakeConsumer->mFrameEventHistory;
801 mConsumer->consumerConnect(mFakeConsumer, false);
802 mConsumer->setConsumerName(String8("TestConsumer"));
803 mSurface = new TestSurface(mProducer, &mFenceMap);
804 mWindow = mSurface;
805
806 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
807 NATIVE_WINDOW_API_CPU));
808 native_window_set_buffer_count(mWindow.get(), 4);
809 }
810
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800811 void disableFrameTimestamps() {
812 mFakeConsumer->mGetFrameTimestampsEnabled = false;
813 native_window_enable_frame_timestamps(mWindow.get(), 0);
814 mFrameTimestampsEnabled = false;
815 }
816
Brian Anderson3da8d272016-07-28 16:20:47 -0700817 void enableFrameTimestamps() {
818 mFakeConsumer->mGetFrameTimestampsEnabled = true;
819 native_window_enable_frame_timestamps(mWindow.get(), 1);
820 mFrameTimestampsEnabled = true;
821 }
822
Brian Anderson1049d1d2016-12-16 17:25:57 -0800823 int getAllFrameTimestamps(uint64_t frameId) {
824 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700825 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
826 &outFirstRefreshStartTime, &outLastRefreshStartTime,
827 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700828 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700829 }
830
Brian Anderson3da8d272016-07-28 16:20:47 -0700831 void resetTimestamps() {
832 outRequestedPresentTime = -1;
833 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700834 outLatchTime = -1;
835 outFirstRefreshStartTime = -1;
836 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700837 outGpuCompositionDoneTime = -1;
838 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700839 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700840 outReleaseTime = -1;
841 }
842
Brian Anderson1049d1d2016-12-16 17:25:57 -0800843 uint64_t getNextFrameId() {
844 uint64_t frameId = -1;
845 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
846 EXPECT_EQ(status, NO_ERROR);
847 return frameId;
848 }
849
Brian Anderson3da8d272016-07-28 16:20:47 -0700850 void dequeueAndQueue(uint64_t frameIndex) {
851 int fence = -1;
852 ANativeWindowBuffer* buffer = nullptr;
853 ASSERT_EQ(NO_ERROR,
854 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
855
856 int oldAddFrameTimestampsCount =
857 mFakeConsumer->mAddFrameTimestampsCount;
858
859 FrameEvents* frame = &mFrames[frameIndex];
860 uint64_t frameNumber = frameIndex + 1;
861
862 NewFrameEventsEntry fe;
863 fe.frameNumber = frameNumber;
864 fe.postedTime = frame->kPostedTime;
865 fe.requestedPresentTime = frame->kRequestedPresentTime;
866 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
867 mFakeConsumer->mNewFrameEntryOverride = fe;
868
869 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
870 frame->mAcquireProducer.mFenceTime,
871 frame->mAcquireConsumer.mFenceTime);
872
873 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
874
875 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
876
877 EXPECT_EQ(
878 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
879 mFakeConsumer->mAddFrameTimestampsCount);
880 }
881
882 void addFrameEvents(
883 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
884 FrameEvents* oldFrame =
885 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
886 FrameEvents* newFrame = &mFrames[iNewFrame];
887
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800888 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700889 uint64_t nNewFrame = iNewFrame + 1;
890
Brian Anderson4e606e32017-03-16 15:34:57 -0700891 // Latch, Composite, and Release the frames in a plausible order.
892 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700893 // that's okay for the purposes of this test.
894 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
895
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700896 // Composite the previous frame one more time, which helps verify
897 // LastRefresh is updated properly.
898 if (oldFrame != nullptr) {
899 mCfeh->addPreComposition(nOldFrame,
900 oldFrame->mRefreshes[2].kStartTime);
901 gpuDoneFenceTime = gpuComposited ?
902 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
903 FenceTime::NO_FENCE;
904 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800905 oldFrame->mRefreshes[2].mPresent.mFenceTime,
906 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700907 }
908
909 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700910 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
911
912 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
913 gpuDoneFenceTime = gpuComposited ?
914 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
915 FenceTime::NO_FENCE;
916 // HWC2 releases the previous buffer after a new latch just before
917 // calling postComposition.
918 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700919 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700920 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
921 }
922 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800923 newFrame->mRefreshes[0].mPresent.mFenceTime,
924 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700925
Brian Anderson3da8d272016-07-28 16:20:47 -0700926 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
927 gpuDoneFenceTime = gpuComposited ?
928 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
929 FenceTime::NO_FENCE;
930 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800931 newFrame->mRefreshes[1].mPresent.mFenceTime,
932 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700933 }
934
Brian Anderson3da8d272016-07-28 16:20:47 -0700935 sp<IGraphicBufferProducer> mProducer;
936 sp<IGraphicBufferConsumer> mConsumer;
937 sp<FakeConsumer> mFakeConsumer;
938 ConsumerFrameEventHistory* mCfeh;
939 sp<TestSurface> mSurface;
940 sp<ANativeWindow> mWindow;
941
942 FenceToFenceTimeMap mFenceMap;
943
944 bool mFrameTimestampsEnabled = false;
945
946 int64_t outRequestedPresentTime = -1;
947 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700948 int64_t outLatchTime = -1;
949 int64_t outFirstRefreshStartTime = -1;
950 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700951 int64_t outGpuCompositionDoneTime = -1;
952 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700953 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700954 int64_t outReleaseTime = -1;
955
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800956 FrameEvents mFrames[3] {
957 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700958};
959
960
961// This test verifies that the frame timestamps are not retrieved when not
962// explicitly enabled via native_window_enable_frame_timestamps.
963// We want to check this to make sure there's no overhead for users
964// that don't need the timestamp information.
965TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
966 int fence;
967 ANativeWindowBuffer* buffer;
968
969 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
970 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
971
Brian Anderson1049d1d2016-12-16 17:25:57 -0800972 const uint64_t fId = getNextFrameId();
973
Brian Anderson3da8d272016-07-28 16:20:47 -0700974 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
975 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
976 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
977 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
978
979 // Verify the producer doesn't get frame timestamps piggybacked on queue.
980 // It is okay that frame timestamps are added in the consumer since it is
981 // still needed for SurfaceFlinger dumps.
982 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
983 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
984 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
985
986 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800987 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700988 EXPECT_EQ(INVALID_OPERATION, result);
989 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800990
991 // Verify compositor timing query fails.
992 nsecs_t compositeDeadline = 0;
993 nsecs_t compositeInterval = 0;
994 nsecs_t compositeToPresentLatency = 0;
995 result = native_window_get_compositor_timing(mWindow.get(),
996 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
997 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700998}
999
1000// This test verifies that the frame timestamps are retrieved if explicitly
1001// enabled via native_window_enable_frame_timestamps.
1002TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001003 CompositorTiming initialCompositorTiming {
1004 1000000000, // 1s deadline
1005 16666667, // 16ms interval
1006 50000000, // 50ms present latency
1007 };
1008 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1009
Brian Anderson3da8d272016-07-28 16:20:47 -07001010 enableFrameTimestamps();
1011
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001012 // Verify the compositor timing query gets the initial compositor values
1013 // after timststamps are enabled; even before the first frame is queued
1014 // or dequeued.
1015 nsecs_t compositeDeadline = 0;
1016 nsecs_t compositeInterval = 0;
1017 nsecs_t compositeToPresentLatency = 0;
1018 mSurface->setNow(initialCompositorTiming.deadline - 1);
1019 int result = native_window_get_compositor_timing(mWindow.get(),
1020 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1021 EXPECT_EQ(NO_ERROR, result);
1022 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1023 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1024 EXPECT_EQ(initialCompositorTiming.presentLatency,
1025 compositeToPresentLatency);
1026
Brian Anderson3da8d272016-07-28 16:20:47 -07001027 int fence;
1028 ANativeWindowBuffer* buffer;
1029
1030 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001031 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001032
Brian Anderson1049d1d2016-12-16 17:25:57 -08001033 const uint64_t fId1 = getNextFrameId();
1034
Brian Anderson3da8d272016-07-28 16:20:47 -07001035 // Verify getFrameTimestamps is piggybacked on dequeue.
1036 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1037 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001038 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001039
1040 NewFrameEventsEntry f1;
1041 f1.frameNumber = 1;
1042 f1.postedTime = mFrames[0].kPostedTime;
1043 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1044 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1045 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1046 mFrames[0].mAcquireProducer.mFenceTime,
1047 mFrames[0].mAcquireConsumer.mFenceTime);
1048 mFakeConsumer->mNewFrameEntryOverride = f1;
1049 mFrames[0].signalQueueFences();
1050
1051 // Verify getFrameTimestamps is piggybacked on queue.
1052 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1053 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1054 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001055 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001056
1057 // Verify queries for timestamps that the producer doesn't know about
1058 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001059 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001060 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001061 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001062}
1063
Brian Anderson6b376712017-04-04 10:51:39 -07001064TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1065 bool displayPresentSupported = true;
1066 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1067
1068 // Verify supported bits are forwarded.
1069 int supportsPresent = -1;
1070 mWindow.get()->query(mWindow.get(),
1071 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1072 EXPECT_EQ(displayPresentSupported, supportsPresent);
1073}
1074
1075TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1076 bool displayPresentSupported = false;
1077 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1078
1079 // Verify supported bits are forwarded.
1080 int supportsPresent = -1;
1081 mWindow.get()->query(mWindow.get(),
1082 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1083 EXPECT_EQ(displayPresentSupported, supportsPresent);
1084}
1085
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001086TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1087 nsecs_t phase = 4000;
1088 nsecs_t interval = 1000;
1089
1090 // Timestamp in previous interval.
1091 nsecs_t timestamp = 3500;
1092 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1093 timestamp, phase, interval));
1094
1095 // Timestamp in next interval.
1096 timestamp = 4500;
1097 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1098 timestamp, phase, interval));
1099
1100 // Timestamp multiple intervals before.
1101 timestamp = 2500;
1102 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1103 timestamp, phase, interval));
1104
1105 // Timestamp multiple intervals after.
1106 timestamp = 6500;
1107 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1108 timestamp, phase, interval));
1109
1110 // Timestamp on previous interval.
1111 timestamp = 3000;
1112 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1113 timestamp, phase, interval));
1114
1115 // Timestamp on next interval.
1116 timestamp = 5000;
1117 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1118 timestamp, phase, interval));
1119
1120 // Timestamp equal to phase.
1121 timestamp = 4000;
1122 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1123 timestamp, phase, interval));
1124}
1125
1126// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1127// if the number of intervals elapsed is internally stored in an int.
1128TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1129 nsecs_t phase = 0;
1130 nsecs_t interval = 4000;
1131 nsecs_t big_timestamp = 8635916564000;
1132 int32_t intervals = big_timestamp / interval;
1133
1134 EXPECT_LT(intervals, 0);
1135 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1136 big_timestamp, phase, interval));
1137 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1138 big_timestamp, big_timestamp, interval));
1139}
1140
1141// This verifies the compositor timing is updated by refresh events
1142// and piggy backed on a queue, dequeue, and enabling of timestamps..
1143TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1144 CompositorTiming initialCompositorTiming {
1145 1000000000, // 1s deadline
1146 16666667, // 16ms interval
1147 50000000, // 50ms present latency
1148 };
1149 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1150
1151 enableFrameTimestamps();
1152
1153 // We get the initial values before any frames are submitted.
1154 nsecs_t compositeDeadline = 0;
1155 nsecs_t compositeInterval = 0;
1156 nsecs_t compositeToPresentLatency = 0;
1157 mSurface->setNow(initialCompositorTiming.deadline - 1);
1158 int result = native_window_get_compositor_timing(mWindow.get(),
1159 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1160 EXPECT_EQ(NO_ERROR, result);
1161 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1162 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1163 EXPECT_EQ(initialCompositorTiming.presentLatency,
1164 compositeToPresentLatency);
1165
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001166 dequeueAndQueue(0);
1167 addFrameEvents(true, NO_FRAME_INDEX, 0);
1168
1169 // Still get the initial values because the frame events for frame 0
1170 // didn't get a chance to piggyback on a queue or dequeue yet.
1171 result = native_window_get_compositor_timing(mWindow.get(),
1172 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1173 EXPECT_EQ(NO_ERROR, result);
1174 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1175 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1176 EXPECT_EQ(initialCompositorTiming.presentLatency,
1177 compositeToPresentLatency);
1178
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001179 dequeueAndQueue(1);
1180 addFrameEvents(true, 0, 1);
1181
1182 // Now expect the composite values associated with frame 1.
1183 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1184 result = native_window_get_compositor_timing(mWindow.get(),
1185 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1186 EXPECT_EQ(NO_ERROR, result);
1187 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1188 compositeDeadline);
1189 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1190 compositeInterval);
1191 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1192 compositeToPresentLatency);
1193
1194 dequeueAndQueue(2);
1195 addFrameEvents(true, 1, 2);
1196
1197 // Now expect the composite values associated with frame 2.
1198 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1199 result = native_window_get_compositor_timing(mWindow.get(),
1200 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1201 EXPECT_EQ(NO_ERROR, result);
1202 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1203 compositeDeadline);
1204 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1205 compositeInterval);
1206 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1207 compositeToPresentLatency);
1208
1209 // Re-enabling frame timestamps should get the latest values.
1210 disableFrameTimestamps();
1211 enableFrameTimestamps();
1212
1213 // Now expect the composite values associated with frame 3.
1214 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1215 result = native_window_get_compositor_timing(mWindow.get(),
1216 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1217 EXPECT_EQ(NO_ERROR, result);
1218 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1219 compositeDeadline);
1220 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1221 compositeInterval);
1222 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1223 compositeToPresentLatency);
1224}
1225
1226// This verifies the compositor deadline properly snaps to the the next
1227// deadline based on the current time.
1228TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1229 CompositorTiming initialCompositorTiming {
1230 1000000000, // 1s deadline
1231 16666667, // 16ms interval
1232 50000000, // 50ms present latency
1233 };
1234 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1235
1236 enableFrameTimestamps();
1237
1238 nsecs_t compositeDeadline = 0;
1239 nsecs_t compositeInterval = 0;
1240 nsecs_t compositeToPresentLatency = 0;
1241
1242 // A "now" just before the deadline snaps to the deadline.
1243 mSurface->setNow(initialCompositorTiming.deadline - 1);
1244 int result = native_window_get_compositor_timing(mWindow.get(),
1245 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1246 EXPECT_EQ(NO_ERROR, result);
1247 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1248 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1249 EXPECT_EQ(expectedDeadline, compositeDeadline);
1250
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001251 dequeueAndQueue(0);
1252 addFrameEvents(true, NO_FRAME_INDEX, 0);
1253
1254 // A "now" just after the deadline snaps properly.
1255 mSurface->setNow(initialCompositorTiming.deadline + 1);
1256 result = native_window_get_compositor_timing(mWindow.get(),
1257 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1258 EXPECT_EQ(NO_ERROR, result);
1259 expectedDeadline =
1260 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1261 EXPECT_EQ(expectedDeadline, compositeDeadline);
1262
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001263 dequeueAndQueue(1);
1264 addFrameEvents(true, 0, 1);
1265
1266 // A "now" just after the next interval snaps properly.
1267 mSurface->setNow(
1268 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1269 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1270 result = native_window_get_compositor_timing(mWindow.get(),
1271 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1272 EXPECT_EQ(NO_ERROR, result);
1273 expectedDeadline =
1274 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1275 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1276 EXPECT_EQ(expectedDeadline, compositeDeadline);
1277
1278 dequeueAndQueue(2);
1279 addFrameEvents(true, 1, 2);
1280
1281 // A "now" over 1 interval before the deadline snaps properly.
1282 mSurface->setNow(
1283 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1284 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1285 result = native_window_get_compositor_timing(mWindow.get(),
1286 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1287 EXPECT_EQ(NO_ERROR, result);
1288 expectedDeadline =
1289 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1290 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1291 EXPECT_EQ(expectedDeadline, compositeDeadline);
1292
1293 // Re-enabling frame timestamps should get the latest values.
1294 disableFrameTimestamps();
1295 enableFrameTimestamps();
1296
1297 // A "now" over 2 intervals before the deadline snaps properly.
1298 mSurface->setNow(
1299 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1300 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1301 result = native_window_get_compositor_timing(mWindow.get(),
1302 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1303 EXPECT_EQ(NO_ERROR, result);
1304 expectedDeadline =
1305 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1306 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1307 EXPECT_EQ(expectedDeadline, compositeDeadline);
1308}
1309
Brian Anderson1049d1d2016-12-16 17:25:57 -08001310// This verifies the timestamps recorded in the consumer's
1311// FrameTimestampsHistory are properly retrieved by the producer for the
1312// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001313TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1314 enableFrameTimestamps();
1315
Brian Anderson1049d1d2016-12-16 17:25:57 -08001316 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001317 dequeueAndQueue(0);
1318 mFrames[0].signalQueueFences();
1319
Brian Anderson1049d1d2016-12-16 17:25:57 -08001320 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001321 dequeueAndQueue(1);
1322 mFrames[1].signalQueueFences();
1323
1324 addFrameEvents(true, NO_FRAME_INDEX, 0);
1325 mFrames[0].signalRefreshFences();
1326 addFrameEvents(true, 0, 1);
1327 mFrames[0].signalReleaseFences();
1328 mFrames[1].signalRefreshFences();
1329
1330 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001331 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001332 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001333 EXPECT_EQ(NO_ERROR, result);
1334 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1335 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001336 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1337 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1338 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001339 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1340 outGpuCompositionDoneTime);
1341 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001342 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001343 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1344
1345 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001346 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001347 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001348 EXPECT_EQ(NO_ERROR, result);
1349 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1350 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001351 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1352 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1353 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001354 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1355 outGpuCompositionDoneTime);
1356 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001357 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1358 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001359}
1360
1361// This test verifies the acquire fence recorded by the consumer is not sent
1362// back to the producer and the producer saves its own fence.
1363TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1364 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001365
Brian Anderson3da8d272016-07-28 16:20:47 -07001366 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001367 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001368 dequeueAndQueue(0);
1369
1370 // Verify queue-related timestamps for f1 are available immediately in the
1371 // producer without asking the consumer again, even before signaling the
1372 // acquire fence.
1373 resetTimestamps();
1374 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001375 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001376 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001377 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001378 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1379 EXPECT_EQ(NO_ERROR, result);
1380 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001381 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001382
1383 // Signal acquire fences. Verify a sync call still isn't necessary.
1384 mFrames[0].signalQueueFences();
1385
1386 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001387 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001388 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001389 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001390 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1391 EXPECT_EQ(NO_ERROR, result);
1392 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1393 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1394
1395 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001396 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001397 dequeueAndQueue(1);
1398
1399 // Verify queue-related timestamps for f2 are available immediately in the
1400 // producer without asking the consumer again, even before signaling the
1401 // acquire fence.
1402 resetTimestamps();
1403 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001404 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001405 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001406 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001407 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1408 EXPECT_EQ(NO_ERROR, result);
1409 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001410 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001411
1412 // Signal acquire fences. Verify a sync call still isn't necessary.
1413 mFrames[1].signalQueueFences();
1414
1415 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001416 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001417 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001418 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001419 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1420 EXPECT_EQ(NO_ERROR, result);
1421 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1422 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1423}
1424
1425TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1426 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001427
1428 // Dequeue and queue frame 1.
1429 dequeueAndQueue(0);
1430 mFrames[0].signalQueueFences();
1431
1432 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001433 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001434 dequeueAndQueue(1);
1435 mFrames[1].signalQueueFences();
1436
1437 addFrameEvents(true, NO_FRAME_INDEX, 0);
1438 mFrames[0].signalRefreshFences();
1439 addFrameEvents(true, 0, 1);
1440 mFrames[0].signalReleaseFences();
1441 mFrames[1].signalRefreshFences();
1442
1443 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001444 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001445 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001446 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1447 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001448 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001449 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1450}
1451
1452// This test verifies that fences can signal and update timestamps producer
1453// side without an additional sync call to the consumer.
1454TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1455 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001456
1457 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001458 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001459 dequeueAndQueue(0);
1460 mFrames[0].signalQueueFences();
1461
1462 // Dequeue and queue frame 2.
1463 dequeueAndQueue(1);
1464 mFrames[1].signalQueueFences();
1465
1466 addFrameEvents(true, NO_FRAME_INDEX, 0);
1467 addFrameEvents(true, 0, 1);
1468
1469 // Verify available timestamps are correct for frame 1, before any
1470 // fence has been signaled.
1471 // Note: A sync call is necessary here since the events triggered by
1472 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001473 resetTimestamps();
1474 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001475 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001476 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1477 EXPECT_EQ(NO_ERROR, result);
1478 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1479 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001480 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1481 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1482 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001483 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1484 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001485 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001486 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001487
1488 // Verify available timestamps are correct for frame 1 again, before any
1489 // fence has been signaled.
1490 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001491 resetTimestamps();
1492 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001493 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001494 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1495 EXPECT_EQ(NO_ERROR, result);
1496 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1497 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001498 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1499 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1500 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001501 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1502 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001503 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001504 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001505
1506 // Signal the fences for frame 1.
1507 mFrames[0].signalRefreshFences();
1508 mFrames[0].signalReleaseFences();
1509
1510 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001511 resetTimestamps();
1512 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001513 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001514 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1515 EXPECT_EQ(NO_ERROR, result);
1516 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1517 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001518 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1519 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1520 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001521 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1522 outGpuCompositionDoneTime);
1523 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001524 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001525 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1526}
1527
1528// This test verifies that if the frame wasn't GPU composited but has a refresh
1529// event a sync call isn't made to get the GPU composite done time since it will
1530// never exist.
1531TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1532 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001533
Brian Anderson3da8d272016-07-28 16:20:47 -07001534 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001535 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001536 dequeueAndQueue(0);
1537 mFrames[0].signalQueueFences();
1538
1539 // Dequeue and queue frame 2.
1540 dequeueAndQueue(1);
1541 mFrames[1].signalQueueFences();
1542
1543 addFrameEvents(false, NO_FRAME_INDEX, 0);
1544 addFrameEvents(false, 0, 1);
1545
1546 // Verify available timestamps are correct for frame 1, before any
1547 // fence has been signaled.
1548 // Note: A sync call is necessary here since the events triggered by
1549 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1550 resetTimestamps();
1551 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001552 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001553 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1554 EXPECT_EQ(NO_ERROR, result);
1555 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1556 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001557 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1558 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1559 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001560 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1561 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001562 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001563 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001564
1565 // Signal the fences for frame 1.
1566 mFrames[0].signalRefreshFences();
1567 mFrames[0].signalReleaseFences();
1568
1569 // Verify all timestamps, except GPU composition, are available without a
1570 // sync call.
1571 resetTimestamps();
1572 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001573 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001574 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1575 EXPECT_EQ(NO_ERROR, result);
1576 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1577 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001578 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1579 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1580 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001581 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001582 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001583 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001584 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1585}
1586
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001587// This test verifies that if the certain timestamps can't possibly exist for
1588// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001589TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001590 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001591
1592 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001593 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001594 dequeueAndQueue(0);
1595 mFrames[0].signalQueueFences();
1596
1597 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001598 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001599 dequeueAndQueue(1);
1600 mFrames[1].signalQueueFences();
1601
1602 addFrameEvents(false, NO_FRAME_INDEX, 0);
1603 addFrameEvents(false, 0, 1);
1604
1605 // Verify available timestamps are correct for frame 1, before any
1606 // fence has been signaled.
1607 // Note: A sync call is necessary here since the events triggered by
1608 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001609 resetTimestamps();
1610 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001611 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001612 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1613 EXPECT_EQ(NO_ERROR, result);
1614 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1615 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001616 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1617 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1618 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001619 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1620 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001621 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001622 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001623
1624 mFrames[0].signalRefreshFences();
1625 mFrames[0].signalReleaseFences();
1626 mFrames[1].signalRefreshFences();
1627
Brian Anderson1049d1d2016-12-16 17:25:57 -08001628 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001629 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001630 // available, a sync call should not occur because it's not possible for f2
1631 // to encounter the final value for those events until another frame is
1632 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001633 resetTimestamps();
1634 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001635 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001636 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1637 EXPECT_EQ(NO_ERROR, result);
1638 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1639 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001640 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1641 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1642 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001643 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001644 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001645 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1646 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001647}
1648
Brian Anderson6b376712017-04-04 10:51:39 -07001649// This test verifies there are no sync calls for present times
1650// when they aren't supported and that an error is returned.
1651
1652TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1653 enableFrameTimestamps();
1654 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1655
1656 // Dequeue and queue frame 1.
1657 const uint64_t fId1 = getNextFrameId();
1658 dequeueAndQueue(0);
1659
1660 // Verify a query for the Present times do not trigger a sync call if they
1661 // are not supported.
1662 resetTimestamps();
1663 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1664 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1665 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1666 &outDisplayPresentTime, nullptr, nullptr);
1667 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1668 EXPECT_EQ(BAD_VALUE, result);
1669 EXPECT_EQ(-1, outDisplayPresentTime);
1670}
1671
Dan Stoza932f0082017-05-31 13:50:16 -07001672} // namespace android