blob: 06fe86c0cfe2bc75a4b321e918f65e1116bae40f [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());
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800134
135 const sp<IBinder> display = sf->getInternalDisplayToken();
136 ASSERT_FALSE(display == nullptr);
137
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000138 sp<GraphicBuffer> outBuffer;
Peiyong Lin0e003c92018-09-17 11:09:51 -0700139 ASSERT_EQ(NO_ERROR,
140 sf->captureScreen(display, &outBuffer, ui::Dataspace::V0_SRGB,
141 ui::PixelFormat::RGBA_8888, Rect(), 64, 64, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800142
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700143 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
144 NATIVE_WINDOW_API_CPU));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800145 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
146 // that we need to dequeue a buffer in order for it to actually get
147 // allocated in SurfaceFlinger.
148 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
149 GRALLOC_USAGE_PROTECTED));
150 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Yi Konga03e0442018-07-17 11:16:57 -0700151 ANativeWindowBuffer* buf = nullptr;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700152
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700153 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700154 if (err) {
155 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
156 // that's okay as long as this is the reason for the failure.
157 // try again without the GRALLOC_USAGE_PROTECTED bit.
158 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700159 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
160 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700161 return;
162 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700163 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700164
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800165 for (int i = 0; i < 4; i++) {
166 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700167 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
168 &buf));
169 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800170 }
Peiyong Lin0e003c92018-09-17 11:09:51 -0700171 ASSERT_EQ(NO_ERROR,
172 sf->captureScreen(display, &outBuffer, ui::Dataspace::V0_SRGB,
173 ui::PixelFormat::RGBA_8888, Rect(), 64, 64, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800174}
175
Jamie Gennis391bbe22011-03-14 15:00:06 -0700176TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
177 sp<ANativeWindow> anw(mSurface);
178 int result = -123;
179 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
180 EXPECT_EQ(NO_ERROR, err);
181 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
182}
183
Craig Donner6ebc46a2016-10-21 15:23:44 -0700184TEST_F(SurfaceTest, LayerCountIsOne) {
185 sp<ANativeWindow> anw(mSurface);
186 int result = -123;
187 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
188 EXPECT_EQ(NO_ERROR, err);
189 EXPECT_EQ(1, result);
190}
191
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700192TEST_F(SurfaceTest, QueryConsumerUsage) {
193 const int TEST_USAGE_FLAGS =
194 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700195 sp<IGraphicBufferProducer> producer;
196 sp<IGraphicBufferConsumer> consumer;
197 BufferQueue::createBufferQueue(&producer, &consumer);
198 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700199 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700200 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700201
202 sp<ANativeWindow> anw(s);
203
204 int flags = -1;
205 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
206
207 ASSERT_EQ(NO_ERROR, err);
208 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
209}
210
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800211TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800212 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_V0_SRGB;
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800213 sp<IGraphicBufferProducer> producer;
214 sp<IGraphicBufferConsumer> consumer;
215 BufferQueue::createBufferQueue(&producer, &consumer);
216 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
217
218 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
219
220 sp<Surface> s = new Surface(producer);
221
222 sp<ANativeWindow> anw(s);
223
224 android_dataspace dataSpace;
225
226 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
227 reinterpret_cast<int*>(&dataSpace));
228
229 ASSERT_EQ(NO_ERROR, err);
230 ASSERT_EQ(TEST_DATASPACE, dataSpace);
231}
232
Dan Stoza812ed062015-06-02 15:45:22 -0700233TEST_F(SurfaceTest, SettingGenerationNumber) {
234 sp<IGraphicBufferProducer> producer;
235 sp<IGraphicBufferConsumer> consumer;
236 BufferQueue::createBufferQueue(&producer, &consumer);
237 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
238 sp<Surface> surface = new Surface(producer);
239 sp<ANativeWindow> window(surface);
240
241 // Allocate a buffer with a generation number of 0
242 ANativeWindowBuffer* buffer;
243 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700244 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
245 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700246 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
247 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
248
249 // Detach the buffer and check its generation number
250 sp<GraphicBuffer> graphicBuffer;
251 sp<Fence> fence;
252 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
253 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
254
255 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
256 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
257
258 // This should change the generation number of the GraphicBuffer
259 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
260
261 // Check that the new generation number sticks with the buffer
262 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
263 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
264 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
265 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
266}
267
Dan Stozac6f30bd2015-06-08 09:32:50 -0700268TEST_F(SurfaceTest, GetConsumerName) {
269 sp<IGraphicBufferProducer> producer;
270 sp<IGraphicBufferConsumer> consumer;
271 BufferQueue::createBufferQueue(&producer, &consumer);
272
273 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
274 consumer->consumerConnect(dummyConsumer, false);
275 consumer->setConsumerName(String8("TestConsumer"));
276
277 sp<Surface> surface = new Surface(producer);
278 sp<ANativeWindow> window(surface);
279 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
280
281 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
282}
283
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700284TEST_F(SurfaceTest, GetWideColorSupport) {
285 sp<IGraphicBufferProducer> producer;
286 sp<IGraphicBufferConsumer> consumer;
287 BufferQueue::createBufferQueue(&producer, &consumer);
288
289 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
290 consumer->consumerConnect(dummyConsumer, false);
291 consumer->setConsumerName(String8("TestConsumer"));
292
293 sp<Surface> surface = new Surface(producer);
294 sp<ANativeWindow> window(surface);
295 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
296
297 bool supported;
298 surface->getWideColorSupport(&supported);
299
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -0600300 // NOTE: This test assumes that device that supports
301 // wide-color (as indicated by BoardConfig) must also
302 // have a wide-color primary display.
303 // That assumption allows this test to cover devices
304 // that advertised a wide-color color mode without
305 // actually supporting wide-color to pass this test
306 // as well as the case of a device that does support
307 // wide-color (via BoardConfig) and has a wide-color
308 // primary display.
309 // NOT covered at this time is a device that supports
310 // wide color in the BoardConfig but does not support
311 // a wide-color color mode on the primary display.
312 ASSERT_EQ(hasWideColorDisplay, supported);
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700313}
314
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700315TEST_F(SurfaceTest, GetHdrSupport) {
316 sp<IGraphicBufferProducer> producer;
317 sp<IGraphicBufferConsumer> consumer;
318 BufferQueue::createBufferQueue(&producer, &consumer);
319
320 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
321 consumer->consumerConnect(dummyConsumer, false);
322 consumer->setConsumerName(String8("TestConsumer"));
323
324 sp<Surface> surface = new Surface(producer);
325 sp<ANativeWindow> window(surface);
326 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
327
328 bool supported;
329 status_t result = surface->getHdrSupport(&supported);
330 ASSERT_EQ(NO_ERROR, result);
331
332 // NOTE: This is not a CTS test.
333 // This test verifies that when the BoardConfig TARGET_HAS_HDR_DISPLAY
334 // is TRUE, getHdrSupport is also true.
335 // TODO: Add check for an HDR color mode on the primary display.
336 ASSERT_EQ(hasHdrDisplay, supported);
337}
338
339TEST_F(SurfaceTest, SetHdrMetadata) {
340 sp<IGraphicBufferProducer> producer;
341 sp<IGraphicBufferConsumer> consumer;
342 BufferQueue::createBufferQueue(&producer, &consumer);
343
344 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
345 consumer->consumerConnect(dummyConsumer, false);
346 consumer->setConsumerName(String8("TestConsumer"));
347
348 sp<Surface> surface = new Surface(producer);
349 sp<ANativeWindow> window(surface);
350 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
351
352 bool supported;
353 status_t result = surface->getHdrSupport(&supported);
354 ASSERT_EQ(NO_ERROR, result);
355
356 if (!hasHdrDisplay || !supported) {
357 return;
358 }
359 const android_smpte2086_metadata smpte2086 = {
360 {0.680, 0.320},
361 {0.265, 0.690},
362 {0.150, 0.060},
363 {0.3127, 0.3290},
364 100.0,
365 0.1,
366 };
367 const android_cta861_3_metadata cta861_3 = {
368 78.0,
369 62.0,
370 };
Valerie Haua82679d2018-11-21 09:31:43 -0800371
372 std::vector<uint8_t> hdr10plus;
373 hdr10plus.push_back(0xff);
374
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700375 int error = native_window_set_buffers_smpte2086_metadata(window.get(), &smpte2086);
376 ASSERT_EQ(error, NO_ERROR);
377 error = native_window_set_buffers_cta861_3_metadata(window.get(), &cta861_3);
378 ASSERT_EQ(error, NO_ERROR);
Valerie Haua82679d2018-11-21 09:31:43 -0800379 error = native_window_set_buffers_hdr10_plus_metadata(window.get(), hdr10plus.size(),
380 hdr10plus.data());
381 ASSERT_EQ(error, NO_ERROR);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700382}
383
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800384TEST_F(SurfaceTest, DynamicSetBufferCount) {
385 sp<IGraphicBufferProducer> producer;
386 sp<IGraphicBufferConsumer> consumer;
387 BufferQueue::createBufferQueue(&producer, &consumer);
388
389 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
390 consumer->consumerConnect(dummyConsumer, false);
391 consumer->setConsumerName(String8("TestConsumer"));
392
393 sp<Surface> surface = new Surface(producer);
394 sp<ANativeWindow> window(surface);
395
396 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
397 NATIVE_WINDOW_API_CPU));
398 native_window_set_buffer_count(window.get(), 4);
399
400 int fence;
401 ANativeWindowBuffer* buffer;
402 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
403 native_window_set_buffer_count(window.get(), 3);
404 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
405 native_window_set_buffer_count(window.get(), 2);
406 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
407 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
408}
409
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700410TEST_F(SurfaceTest, GetAndFlushRemovedBuffers) {
411 sp<IGraphicBufferProducer> producer;
412 sp<IGraphicBufferConsumer> consumer;
413 BufferQueue::createBufferQueue(&producer, &consumer);
414
415 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
416 consumer->consumerConnect(dummyConsumer, false);
417 consumer->setConsumerName(String8("TestConsumer"));
418
419 sp<Surface> surface = new Surface(producer);
420 sp<ANativeWindow> window(surface);
421 sp<DummyProducerListener> listener = new DummyProducerListener();
422 ASSERT_EQ(OK, surface->connect(
423 NATIVE_WINDOW_API_CPU,
424 /*listener*/listener,
425 /*reportBufferRemoval*/true));
426 const int BUFFER_COUNT = 4;
427 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
428
429 sp<GraphicBuffer> detachedBuffer;
430 sp<Fence> outFence;
431 int fences[BUFFER_COUNT];
432 ANativeWindowBuffer* buffers[BUFFER_COUNT];
433 // Allocate buffers because detachNextBuffer requires allocated buffers
434 for (int i = 0; i < BUFFER_COUNT; i++) {
435 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
436 }
437 for (int i = 0; i < BUFFER_COUNT; i++) {
438 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
439 }
440
441 // Test detached buffer is correctly reported
442 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
443 std::vector<sp<GraphicBuffer>> removedBuffers;
444 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
445 ASSERT_EQ(1u, removedBuffers.size());
446 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
447 // Test the list is flushed one getAndFlushRemovedBuffers returns
448 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
449 ASSERT_EQ(0u, removedBuffers.size());
450
451
452 // Test removed buffer list is cleanup after next dequeueBuffer call
453 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
454 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[0], &fences[0]));
455 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
456 ASSERT_EQ(0u, removedBuffers.size());
457 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[0], fences[0]));
458
459 // Test removed buffer list is cleanup after next detachNextBuffer call
460 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
461 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
462 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
463 ASSERT_EQ(1u, removedBuffers.size());
464 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
465
466 // Re-allocate buffers since all buffers are detached up to now
467 for (int i = 0; i < BUFFER_COUNT; i++) {
468 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
469 }
470 for (int i = 0; i < BUFFER_COUNT; i++) {
471 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
472 }
473
474 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
475 ASSERT_EQ(NO_ERROR, surface->attachBuffer(detachedBuffer.get()));
476 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
477 // Depends on which slot GraphicBufferProducer impl pick, the attach call might
478 // get 0 or 1 buffer removed.
479 ASSERT_LE(removedBuffers.size(), 1u);
480}
Brian Anderson3da8d272016-07-28 16:20:47 -0700481
Dan Stoza932f0082017-05-31 13:50:16 -0700482TEST_F(SurfaceTest, TestGetLastDequeueStartTime) {
483 sp<ANativeWindow> anw(mSurface);
484 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU));
485
486 ANativeWindowBuffer* buffer = nullptr;
487 int32_t fenceFd = -1;
488
489 nsecs_t before = systemTime(CLOCK_MONOTONIC);
490 anw->dequeueBuffer(anw.get(), &buffer, &fenceFd);
491 nsecs_t after = systemTime(CLOCK_MONOTONIC);
492
493 nsecs_t lastDequeueTime = mSurface->getLastDequeueStartTime();
494 ASSERT_LE(before, lastDequeueTime);
495 ASSERT_GE(after, lastDequeueTime);
496}
497
Brian Anderson3da8d272016-07-28 16:20:47 -0700498class FakeConsumer : public BnConsumerListener {
499public:
500 void onFrameAvailable(const BufferItem& /*item*/) override {}
501 void onBuffersReleased() override {}
502 void onSidebandStreamChanged() override {}
503
504 void addAndGetFrameTimestamps(
505 const NewFrameEventsEntry* newTimestamps,
506 FrameEventHistoryDelta* outDelta) override {
507 if (newTimestamps) {
508 if (mGetFrameTimestampsEnabled) {
509 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
510 "Test should set mNewFrameEntryOverride before queuing "
511 "a frame.";
512 EXPECT_EQ(newTimestamps->frameNumber,
513 mNewFrameEntryOverride.frameNumber) <<
514 "Test attempting to add NewFrameEntryOverride with "
515 "incorrect frame number.";
516 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
517 mNewFrameEntryOverride.frameNumber = 0;
518 }
519 mAddFrameTimestampsCount++;
520 mLastAddedFrameNumber = newTimestamps->frameNumber;
521 }
522 if (outDelta) {
523 mFrameEventHistory.getAndResetDelta(outDelta);
524 mGetFrameTimestampsCount++;
525 }
526 mAddAndGetFrameTimestampsCallCount++;
527 }
528
529 bool mGetFrameTimestampsEnabled = false;
530
531 ConsumerFrameEventHistory mFrameEventHistory;
532 int mAddAndGetFrameTimestampsCallCount = 0;
533 int mAddFrameTimestampsCount = 0;
534 int mGetFrameTimestampsCount = 0;
535 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
536
537 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
538};
539
540
541class FakeSurfaceComposer : public ISurfaceComposer{
542public:
543 ~FakeSurfaceComposer() override {}
544
Brian Anderson6b376712017-04-04 10:51:39 -0700545 void setSupportsPresent(bool supportsPresent) {
546 mSupportsPresent = supportsPresent;
547 }
548
Brian Anderson3da8d272016-07-28 16:20:47 -0700549 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Jorim Jaggib1e2f8d2017-06-08 15:43:59 -0700550 sp<IDisplayEventConnection> createDisplayEventConnection(ISurfaceComposer::VsyncSource)
551 override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700552 return nullptr;
553 }
554 sp<IBinder> createDisplay(const String8& /*displayName*/,
555 bool /*secure*/) override { return nullptr; }
556 void destroyDisplay(const sp<IBinder>& /*display */) override {}
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800557 std::vector<PhysicalDisplayId> getPhysicalDisplayIds() const override { return {}; }
558 sp<IBinder> getPhysicalDisplayToken(PhysicalDisplayId) const override { return nullptr; }
Brian Anderson3da8d272016-07-28 16:20:47 -0700559 void setTransactionState(const Vector<ComposerState>& /*state*/,
Marissa Wall713b63f2018-10-17 15:42:43 -0700560 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/,
chaviw273171b2018-12-26 11:46:30 -0800561 const sp<IBinder>& /*applyToken*/,
Marissa Wall17b4e452018-12-26 16:32:34 -0800562 const InputWindowCommands& /*inputWindowCommands*/,
563 int64_t /*desiredPresentTime*/) override {}
564
Brian Anderson3da8d272016-07-28 16:20:47 -0700565 void bootFinished() override {}
566 bool authenticateSurfaceTexture(
567 const sp<IGraphicBufferProducer>& /*surface*/) const override {
568 return false;
569 }
Brian Anderson6b376712017-04-04 10:51:39 -0700570
571 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
572 const override {
573 *outSupported = {
574 FrameEvent::REQUESTED_PRESENT,
575 FrameEvent::ACQUIRE,
576 FrameEvent::LATCH,
577 FrameEvent::FIRST_REFRESH_START,
578 FrameEvent::LAST_REFRESH_START,
579 FrameEvent::GPU_COMPOSITION_DONE,
580 FrameEvent::DEQUEUE_READY,
581 FrameEvent::RELEASE
582 };
583 if (mSupportsPresent) {
584 outSupported->push_back(
585 FrameEvent::DISPLAY_PRESENT);
586 }
587 return NO_ERROR;
588 }
589
Brian Anderson3da8d272016-07-28 16:20:47 -0700590 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
591 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
592 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
593 status_t getDisplayStats(const sp<IBinder>& /*display*/,
594 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
595 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
596 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
597 override {
598 return NO_ERROR;
599 }
600 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700601 Vector<ColorMode>* /*outColorModes*/) override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700602 return NO_ERROR;
603 }
Daniel Solomon42d04562019-01-20 21:03:19 -0800604 status_t getDisplayNativePrimaries(const sp<IBinder>& /*display*/,
605 ui::DisplayPrimaries& /*primaries*/) override {
606 return NO_ERROR;
607 }
Peiyong Lina52f0292018-03-14 17:26:31 -0700608 ColorMode getActiveColorMode(const sp<IBinder>& /*display*/)
Brian Anderson3da8d272016-07-28 16:20:47 -0700609 override {
Peiyong Lina52f0292018-03-14 17:26:31 -0700610 return ColorMode::NATIVE;
Brian Anderson3da8d272016-07-28 16:20:47 -0700611 }
612 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700613 ColorMode /*colorMode*/) override { return NO_ERROR; }
Peiyong Lin0e003c92018-09-17 11:09:51 -0700614 status_t captureScreen(const sp<IBinder>& /*display*/, sp<GraphicBuffer>* /*outBuffer*/,
615 const ui::Dataspace /*reqDataspace*/,
616 const ui::PixelFormat /*reqPixelFormat*/, Rect /*sourceCrop*/,
617 uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrfa8855f2019-02-19 10:05:00 -0800618 bool /*useIdentityTransform*/, Rotation /*rotation*/,
619 bool /*captureSecureLayers*/) override {
Peiyong Lin0e003c92018-09-17 11:09:51 -0700620 return NO_ERROR;
621 }
chaviwa76b2712017-09-20 12:02:26 -0700622 virtual status_t captureLayers(const sp<IBinder>& /*parentHandle*/,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700623 sp<GraphicBuffer>* /*outBuffer*/,
624 const ui::Dataspace /*reqDataspace*/,
625 const ui::PixelFormat /*reqPixelFormat*/,
626 const Rect& /*sourceCrop*/, float /*frameScale*/,
627 bool /*childrenOnly*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700628 return NO_ERROR;
629 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700630 status_t clearAnimationFrameStats() override { return NO_ERROR; }
631 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
632 return NO_ERROR;
633 }
634 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
635 HdrCapabilities* /*outCapabilities*/) const override {
636 return NO_ERROR;
637 }
638 status_t enableVSyncInjections(bool /*enable*/) override {
639 return NO_ERROR;
640 }
641 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800642 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
643 return NO_ERROR;
644 }
Peiyong Linc6780972018-10-28 15:24:08 -0700645 status_t getCompositionPreference(
646 ui::Dataspace* /*outDefaultDataspace*/, ui::PixelFormat* /*outDefaultPixelFormat*/,
647 ui::Dataspace* /*outWideColorGamutDataspace*/,
648 ui::PixelFormat* /*outWideColorGamutPixelFormat*/) const override {
Peiyong Lin0256f722018-08-31 15:45:10 -0700649 return NO_ERROR;
650 }
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700651 status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& /*display*/,
652 ui::PixelFormat* /*outFormat*/,
653 ui::Dataspace* /*outDataspace*/,
654 uint8_t* /*outComponentMask*/) const override {
655 return NO_ERROR;
656 }
Kevin DuBois74e53772018-11-19 10:52:38 -0800657 status_t setDisplayContentSamplingEnabled(const sp<IBinder>& /*display*/, bool /*enable*/,
658 uint8_t /*componentMask*/,
659 uint64_t /*maxFrames*/) const override {
660 return NO_ERROR;
661 }
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700662 status_t getDisplayedContentSample(const sp<IBinder>& /*display*/, uint64_t /*maxFrames*/,
663 uint64_t /*timestamp*/,
664 DisplayedFrameStats* /*outStats*/) const override {
665 return NO_ERROR;
666 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700667
Peiyong Lin3c2791e2019-01-14 17:05:18 -0800668 status_t getColorManagement(bool* /*outGetColorManagement*/) const override { return NO_ERROR; }
669 status_t getProtectedContentSupport(bool* /*outSupported*/) const override { return NO_ERROR; }
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700670
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800671 status_t isWideColorDisplay(const sp<IBinder>&, bool*) const override { return NO_ERROR; }
Dan Gittik57e63c52019-01-18 16:37:54 +0000672 status_t getDisplayBrightnessSupport(const sp<IBinder>& /*displayToken*/,
673 bool* /*outSupport*/) const override {
674 return NO_ERROR;
675 }
676 status_t setDisplayBrightness(const sp<IBinder>& /*displayToken*/,
677 float /*brightness*/) const override {
678 return NO_ERROR;
679 }
Marissa Wallebc2c052019-01-16 19:16:55 -0800680
Dan Stoza84ab9372018-12-17 15:27:57 -0800681 status_t addRegionSamplingListener(const Rect& /*samplingArea*/,
682 const sp<IBinder>& /*stopLayerHandle*/,
683 const sp<IRegionSamplingListener>& /*listener*/) override {
684 return NO_ERROR;
685 }
686 status_t removeRegionSamplingListener(
687 const sp<IRegionSamplingListener>& /*listener*/) override {
688 return NO_ERROR;
689 }
Ady Abraham838de062019-02-04 10:24:03 -0800690 status_t setAllowedDisplayConfigs(const sp<IBinder>& /*displayToken*/,
691 const std::vector<int32_t>& /*allowedConfigs*/) override {
692 return NO_ERROR;
693 }
Ady Abrahamd9b3ea62019-02-26 14:08:03 -0800694 status_t getAllowedDisplayConfigs(const sp<IBinder>& /*displayToken*/,
695 std::vector<int32_t>* /*outAllowedConfigs*/) override {
696 return NO_ERROR;
697 }
Dan Stoza84ab9372018-12-17 15:27:57 -0800698
Brian Anderson3da8d272016-07-28 16:20:47 -0700699protected:
700 IBinder* onAsBinder() override { return nullptr; }
701
702private:
703 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700704};
705
706class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
707public:
Chih-Hung Hsiehaaf62162018-12-20 15:45:04 -0800708 explicit FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap) : mFenceMap(fenceMap) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700709
710 ~FakeProducerFrameEventHistory() {}
711
712 void updateAcquireFence(uint64_t frameNumber,
713 std::shared_ptr<FenceTime>&& acquire) override {
714 // Verify the acquire fence being added isn't the one from the consumer.
715 EXPECT_NE(mConsumerAcquireFence, acquire);
716 // Override the fence, so we can verify this was called by the
717 // producer after the frame is queued.
718 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
719 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
720 }
721
722 void setAcquireFenceOverride(
723 const std::shared_ptr<FenceTime>& acquireFenceOverride,
724 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
725 mAcquireFenceOverride = acquireFenceOverride;
726 mConsumerAcquireFence = consumerAcquireFence;
727 }
728
729protected:
730 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
731 const override {
732 return mFenceMap->createFenceTimeForTest(fence);
733 }
734
735 FenceToFenceTimeMap* mFenceMap{nullptr};
736
737 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
738 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
739};
740
741
742class TestSurface : public Surface {
743public:
744 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
745 FenceToFenceTimeMap* fenceMap)
746 : Surface(bufferProducer),
747 mFakeSurfaceComposer(new FakeSurfaceComposer) {
748 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
749 mFrameEventHistory.reset(mFakeFrameEventHistory);
750 }
751
752 ~TestSurface() override {}
753
754 sp<ISurfaceComposer> composerService() const override {
755 return mFakeSurfaceComposer;
756 }
757
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800758 nsecs_t now() const override {
759 return mNow;
760 }
761
762 void setNow(nsecs_t now) {
763 mNow = now;
764 }
765
Brian Anderson3da8d272016-07-28 16:20:47 -0700766public:
767 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800768 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700769
770 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
771 // but this raw pointer gives access to test functionality.
772 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
773};
774
775
Brian Andersond0010582017-03-07 13:20:31 -0800776class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700777protected:
778 struct FenceAndFenceTime {
779 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
780 : mFence(new Fence),
781 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
782 sp<Fence> mFence { nullptr };
783 std::shared_ptr<FenceTime> mFenceTime { nullptr };
784 };
785
786 struct RefreshEvents {
787 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800788 : mFenceMap(fenceMap),
789 kCompositorTiming(
790 {refreshStart, refreshStart + 1, refreshStart + 2 }),
791 kStartTime(refreshStart + 3),
792 kGpuCompositionDoneTime(refreshStart + 4),
793 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700794
795 void signalPostCompositeFences() {
796 mFenceMap.signalAllForTest(
797 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
798 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
799 }
800
801 FenceToFenceTimeMap& mFenceMap;
802
803 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
804 FenceAndFenceTime mPresent { mFenceMap };
805
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800806 const CompositorTiming kCompositorTiming;
807
Brian Anderson3da8d272016-07-28 16:20:47 -0700808 const nsecs_t kStartTime;
809 const nsecs_t kGpuCompositionDoneTime;
810 const nsecs_t kPresentTime;
811 };
812
813 struct FrameEvents {
814 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
815 : mFenceMap(fenceMap),
816 kPostedTime(frameStartTime + 100),
817 kRequestedPresentTime(frameStartTime + 200),
818 kProducerAcquireTime(frameStartTime + 300),
819 kConsumerAcquireTime(frameStartTime + 301),
820 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700821 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700822 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700823 mRefreshes {
824 { mFenceMap, frameStartTime + 410 },
825 { mFenceMap, frameStartTime + 420 },
826 { mFenceMap, frameStartTime + 430 } } {}
827
828 void signalQueueFences() {
829 mFenceMap.signalAllForTest(
830 mAcquireConsumer.mFence, kConsumerAcquireTime);
831 mFenceMap.signalAllForTest(
832 mAcquireProducer.mFence, kProducerAcquireTime);
833 }
834
835 void signalRefreshFences() {
836 for (auto& re : mRefreshes) {
837 re.signalPostCompositeFences();
838 }
839 }
840
841 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700842 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
843 }
844
845 FenceToFenceTimeMap& mFenceMap;
846
847 FenceAndFenceTime mAcquireConsumer { mFenceMap };
848 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700849 FenceAndFenceTime mRelease { mFenceMap };
850
851 const nsecs_t kPostedTime;
852 const nsecs_t kRequestedPresentTime;
853 const nsecs_t kProducerAcquireTime;
854 const nsecs_t kConsumerAcquireTime;
855 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700856 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700857 const nsecs_t kReleaseTime;
858
859 RefreshEvents mRefreshes[3];
860 };
861
Brian Andersond0010582017-03-07 13:20:31 -0800862 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700863
864 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700865 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
866 mFakeConsumer = new FakeConsumer;
867 mCfeh = &mFakeConsumer->mFrameEventHistory;
868 mConsumer->consumerConnect(mFakeConsumer, false);
869 mConsumer->setConsumerName(String8("TestConsumer"));
870 mSurface = new TestSurface(mProducer, &mFenceMap);
871 mWindow = mSurface;
872
873 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
874 NATIVE_WINDOW_API_CPU));
875 native_window_set_buffer_count(mWindow.get(), 4);
876 }
877
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800878 void disableFrameTimestamps() {
879 mFakeConsumer->mGetFrameTimestampsEnabled = false;
880 native_window_enable_frame_timestamps(mWindow.get(), 0);
881 mFrameTimestampsEnabled = false;
882 }
883
Brian Anderson3da8d272016-07-28 16:20:47 -0700884 void enableFrameTimestamps() {
885 mFakeConsumer->mGetFrameTimestampsEnabled = true;
886 native_window_enable_frame_timestamps(mWindow.get(), 1);
887 mFrameTimestampsEnabled = true;
888 }
889
Brian Anderson1049d1d2016-12-16 17:25:57 -0800890 int getAllFrameTimestamps(uint64_t frameId) {
891 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700892 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
893 &outFirstRefreshStartTime, &outLastRefreshStartTime,
894 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700895 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700896 }
897
Brian Anderson3da8d272016-07-28 16:20:47 -0700898 void resetTimestamps() {
899 outRequestedPresentTime = -1;
900 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700901 outLatchTime = -1;
902 outFirstRefreshStartTime = -1;
903 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700904 outGpuCompositionDoneTime = -1;
905 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700906 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700907 outReleaseTime = -1;
908 }
909
Brian Anderson1049d1d2016-12-16 17:25:57 -0800910 uint64_t getNextFrameId() {
911 uint64_t frameId = -1;
912 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
913 EXPECT_EQ(status, NO_ERROR);
914 return frameId;
915 }
916
Brian Anderson3da8d272016-07-28 16:20:47 -0700917 void dequeueAndQueue(uint64_t frameIndex) {
918 int fence = -1;
919 ANativeWindowBuffer* buffer = nullptr;
920 ASSERT_EQ(NO_ERROR,
921 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
922
923 int oldAddFrameTimestampsCount =
924 mFakeConsumer->mAddFrameTimestampsCount;
925
926 FrameEvents* frame = &mFrames[frameIndex];
927 uint64_t frameNumber = frameIndex + 1;
928
929 NewFrameEventsEntry fe;
930 fe.frameNumber = frameNumber;
931 fe.postedTime = frame->kPostedTime;
932 fe.requestedPresentTime = frame->kRequestedPresentTime;
933 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
934 mFakeConsumer->mNewFrameEntryOverride = fe;
935
936 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
937 frame->mAcquireProducer.mFenceTime,
938 frame->mAcquireConsumer.mFenceTime);
939
940 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
941
942 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
943
944 EXPECT_EQ(
945 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
946 mFakeConsumer->mAddFrameTimestampsCount);
947 }
948
949 void addFrameEvents(
950 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
951 FrameEvents* oldFrame =
952 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
953 FrameEvents* newFrame = &mFrames[iNewFrame];
954
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800955 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700956 uint64_t nNewFrame = iNewFrame + 1;
957
Brian Anderson4e606e32017-03-16 15:34:57 -0700958 // Latch, Composite, and Release the frames in a plausible order.
959 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700960 // that's okay for the purposes of this test.
961 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
962
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700963 // Composite the previous frame one more time, which helps verify
964 // LastRefresh is updated properly.
965 if (oldFrame != nullptr) {
966 mCfeh->addPreComposition(nOldFrame,
967 oldFrame->mRefreshes[2].kStartTime);
968 gpuDoneFenceTime = gpuComposited ?
969 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
970 FenceTime::NO_FENCE;
971 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800972 oldFrame->mRefreshes[2].mPresent.mFenceTime,
973 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700974 }
975
976 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700977 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
978
979 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
980 gpuDoneFenceTime = gpuComposited ?
981 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
982 FenceTime::NO_FENCE;
983 // HWC2 releases the previous buffer after a new latch just before
984 // calling postComposition.
985 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700986 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700987 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
988 }
989 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800990 newFrame->mRefreshes[0].mPresent.mFenceTime,
991 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700992
Brian Anderson3da8d272016-07-28 16:20:47 -0700993 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
994 gpuDoneFenceTime = gpuComposited ?
995 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
996 FenceTime::NO_FENCE;
997 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800998 newFrame->mRefreshes[1].mPresent.mFenceTime,
999 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -07001000 }
1001
Brian Anderson3da8d272016-07-28 16:20:47 -07001002 sp<IGraphicBufferProducer> mProducer;
1003 sp<IGraphicBufferConsumer> mConsumer;
1004 sp<FakeConsumer> mFakeConsumer;
1005 ConsumerFrameEventHistory* mCfeh;
1006 sp<TestSurface> mSurface;
1007 sp<ANativeWindow> mWindow;
1008
1009 FenceToFenceTimeMap mFenceMap;
1010
1011 bool mFrameTimestampsEnabled = false;
1012
1013 int64_t outRequestedPresentTime = -1;
1014 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001015 int64_t outLatchTime = -1;
1016 int64_t outFirstRefreshStartTime = -1;
1017 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001018 int64_t outGpuCompositionDoneTime = -1;
1019 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001020 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001021 int64_t outReleaseTime = -1;
1022
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001023 FrameEvents mFrames[3] {
1024 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -07001025};
1026
1027
1028// This test verifies that the frame timestamps are not retrieved when not
1029// explicitly enabled via native_window_enable_frame_timestamps.
1030// We want to check this to make sure there's no overhead for users
1031// that don't need the timestamp information.
1032TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
1033 int fence;
1034 ANativeWindowBuffer* buffer;
1035
1036 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1037 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1038
Brian Anderson1049d1d2016-12-16 17:25:57 -08001039 const uint64_t fId = getNextFrameId();
1040
Brian Anderson3da8d272016-07-28 16:20:47 -07001041 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
1042 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1043 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1044 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1045
1046 // Verify the producer doesn't get frame timestamps piggybacked on queue.
1047 // It is okay that frame timestamps are added in the consumer since it is
1048 // still needed for SurfaceFlinger dumps.
1049 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1050 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1051 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1052
1053 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001054 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -07001055 EXPECT_EQ(INVALID_OPERATION, result);
1056 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001057
1058 // Verify compositor timing query fails.
1059 nsecs_t compositeDeadline = 0;
1060 nsecs_t compositeInterval = 0;
1061 nsecs_t compositeToPresentLatency = 0;
1062 result = native_window_get_compositor_timing(mWindow.get(),
1063 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1064 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001065}
1066
1067// This test verifies that the frame timestamps are retrieved if explicitly
1068// enabled via native_window_enable_frame_timestamps.
1069TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001070 CompositorTiming initialCompositorTiming {
1071 1000000000, // 1s deadline
1072 16666667, // 16ms interval
1073 50000000, // 50ms present latency
1074 };
1075 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1076
Brian Anderson3da8d272016-07-28 16:20:47 -07001077 enableFrameTimestamps();
1078
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001079 // Verify the compositor timing query gets the initial compositor values
1080 // after timststamps are enabled; even before the first frame is queued
1081 // or dequeued.
1082 nsecs_t compositeDeadline = 0;
1083 nsecs_t compositeInterval = 0;
1084 nsecs_t compositeToPresentLatency = 0;
1085 mSurface->setNow(initialCompositorTiming.deadline - 1);
1086 int result = native_window_get_compositor_timing(mWindow.get(),
1087 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1088 EXPECT_EQ(NO_ERROR, result);
1089 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1090 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1091 EXPECT_EQ(initialCompositorTiming.presentLatency,
1092 compositeToPresentLatency);
1093
Brian Anderson3da8d272016-07-28 16:20:47 -07001094 int fence;
1095 ANativeWindowBuffer* buffer;
1096
1097 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001098 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001099
Brian Anderson1049d1d2016-12-16 17:25:57 -08001100 const uint64_t fId1 = getNextFrameId();
1101
Brian Anderson3da8d272016-07-28 16:20:47 -07001102 // Verify getFrameTimestamps is piggybacked on dequeue.
1103 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1104 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001105 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001106
1107 NewFrameEventsEntry f1;
1108 f1.frameNumber = 1;
1109 f1.postedTime = mFrames[0].kPostedTime;
1110 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1111 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1112 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1113 mFrames[0].mAcquireProducer.mFenceTime,
1114 mFrames[0].mAcquireConsumer.mFenceTime);
1115 mFakeConsumer->mNewFrameEntryOverride = f1;
1116 mFrames[0].signalQueueFences();
1117
1118 // Verify getFrameTimestamps is piggybacked on queue.
1119 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1120 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1121 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001122 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001123
1124 // Verify queries for timestamps that the producer doesn't know about
1125 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001126 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001127 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001128 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001129}
1130
Brian Anderson6b376712017-04-04 10:51:39 -07001131TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1132 bool displayPresentSupported = true;
1133 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1134
1135 // Verify supported bits are forwarded.
1136 int supportsPresent = -1;
1137 mWindow.get()->query(mWindow.get(),
1138 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1139 EXPECT_EQ(displayPresentSupported, supportsPresent);
1140}
1141
1142TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1143 bool displayPresentSupported = false;
1144 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1145
1146 // Verify supported bits are forwarded.
1147 int supportsPresent = -1;
1148 mWindow.get()->query(mWindow.get(),
1149 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1150 EXPECT_EQ(displayPresentSupported, supportsPresent);
1151}
1152
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001153TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1154 nsecs_t phase = 4000;
1155 nsecs_t interval = 1000;
1156
1157 // Timestamp in previous interval.
1158 nsecs_t timestamp = 3500;
1159 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1160 timestamp, phase, interval));
1161
1162 // Timestamp in next interval.
1163 timestamp = 4500;
1164 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1165 timestamp, phase, interval));
1166
1167 // Timestamp multiple intervals before.
1168 timestamp = 2500;
1169 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1170 timestamp, phase, interval));
1171
1172 // Timestamp multiple intervals after.
1173 timestamp = 6500;
1174 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1175 timestamp, phase, interval));
1176
1177 // Timestamp on previous interval.
1178 timestamp = 3000;
1179 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1180 timestamp, phase, interval));
1181
1182 // Timestamp on next interval.
1183 timestamp = 5000;
1184 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1185 timestamp, phase, interval));
1186
1187 // Timestamp equal to phase.
1188 timestamp = 4000;
1189 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1190 timestamp, phase, interval));
1191}
1192
1193// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1194// if the number of intervals elapsed is internally stored in an int.
1195TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1196 nsecs_t phase = 0;
1197 nsecs_t interval = 4000;
1198 nsecs_t big_timestamp = 8635916564000;
1199 int32_t intervals = big_timestamp / interval;
1200
1201 EXPECT_LT(intervals, 0);
1202 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1203 big_timestamp, phase, interval));
1204 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1205 big_timestamp, big_timestamp, interval));
1206}
1207
1208// This verifies the compositor timing is updated by refresh events
1209// and piggy backed on a queue, dequeue, and enabling of timestamps..
1210TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1211 CompositorTiming initialCompositorTiming {
1212 1000000000, // 1s deadline
1213 16666667, // 16ms interval
1214 50000000, // 50ms present latency
1215 };
1216 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1217
1218 enableFrameTimestamps();
1219
1220 // We get the initial values before any frames are submitted.
1221 nsecs_t compositeDeadline = 0;
1222 nsecs_t compositeInterval = 0;
1223 nsecs_t compositeToPresentLatency = 0;
1224 mSurface->setNow(initialCompositorTiming.deadline - 1);
1225 int result = native_window_get_compositor_timing(mWindow.get(),
1226 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1227 EXPECT_EQ(NO_ERROR, result);
1228 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1229 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1230 EXPECT_EQ(initialCompositorTiming.presentLatency,
1231 compositeToPresentLatency);
1232
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001233 dequeueAndQueue(0);
1234 addFrameEvents(true, NO_FRAME_INDEX, 0);
1235
1236 // Still get the initial values because the frame events for frame 0
1237 // didn't get a chance to piggyback on a queue or dequeue yet.
1238 result = native_window_get_compositor_timing(mWindow.get(),
1239 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1240 EXPECT_EQ(NO_ERROR, result);
1241 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1242 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1243 EXPECT_EQ(initialCompositorTiming.presentLatency,
1244 compositeToPresentLatency);
1245
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001246 dequeueAndQueue(1);
1247 addFrameEvents(true, 0, 1);
1248
1249 // Now expect the composite values associated with frame 1.
1250 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1251 result = native_window_get_compositor_timing(mWindow.get(),
1252 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1253 EXPECT_EQ(NO_ERROR, result);
1254 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1255 compositeDeadline);
1256 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1257 compositeInterval);
1258 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1259 compositeToPresentLatency);
1260
1261 dequeueAndQueue(2);
1262 addFrameEvents(true, 1, 2);
1263
1264 // Now expect the composite values associated with frame 2.
1265 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1266 result = native_window_get_compositor_timing(mWindow.get(),
1267 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1268 EXPECT_EQ(NO_ERROR, result);
1269 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1270 compositeDeadline);
1271 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1272 compositeInterval);
1273 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1274 compositeToPresentLatency);
1275
1276 // Re-enabling frame timestamps should get the latest values.
1277 disableFrameTimestamps();
1278 enableFrameTimestamps();
1279
1280 // Now expect the composite values associated with frame 3.
1281 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1282 result = native_window_get_compositor_timing(mWindow.get(),
1283 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1284 EXPECT_EQ(NO_ERROR, result);
1285 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1286 compositeDeadline);
1287 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1288 compositeInterval);
1289 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1290 compositeToPresentLatency);
1291}
1292
1293// This verifies the compositor deadline properly snaps to the the next
1294// deadline based on the current time.
1295TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1296 CompositorTiming initialCompositorTiming {
1297 1000000000, // 1s deadline
1298 16666667, // 16ms interval
1299 50000000, // 50ms present latency
1300 };
1301 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1302
1303 enableFrameTimestamps();
1304
1305 nsecs_t compositeDeadline = 0;
1306 nsecs_t compositeInterval = 0;
1307 nsecs_t compositeToPresentLatency = 0;
1308
1309 // A "now" just before the deadline snaps to the deadline.
1310 mSurface->setNow(initialCompositorTiming.deadline - 1);
1311 int result = native_window_get_compositor_timing(mWindow.get(),
1312 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1313 EXPECT_EQ(NO_ERROR, result);
1314 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1315 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1316 EXPECT_EQ(expectedDeadline, compositeDeadline);
1317
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001318 dequeueAndQueue(0);
1319 addFrameEvents(true, NO_FRAME_INDEX, 0);
1320
1321 // A "now" just after the deadline snaps properly.
1322 mSurface->setNow(initialCompositorTiming.deadline + 1);
1323 result = native_window_get_compositor_timing(mWindow.get(),
1324 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1325 EXPECT_EQ(NO_ERROR, result);
1326 expectedDeadline =
1327 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1328 EXPECT_EQ(expectedDeadline, compositeDeadline);
1329
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001330 dequeueAndQueue(1);
1331 addFrameEvents(true, 0, 1);
1332
1333 // A "now" just after the next interval snaps properly.
1334 mSurface->setNow(
1335 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1336 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1337 result = native_window_get_compositor_timing(mWindow.get(),
1338 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1339 EXPECT_EQ(NO_ERROR, result);
1340 expectedDeadline =
1341 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1342 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1343 EXPECT_EQ(expectedDeadline, compositeDeadline);
1344
1345 dequeueAndQueue(2);
1346 addFrameEvents(true, 1, 2);
1347
1348 // A "now" over 1 interval before the deadline snaps properly.
1349 mSurface->setNow(
1350 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1351 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1352 result = native_window_get_compositor_timing(mWindow.get(),
1353 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1354 EXPECT_EQ(NO_ERROR, result);
1355 expectedDeadline =
1356 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1357 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1358 EXPECT_EQ(expectedDeadline, compositeDeadline);
1359
1360 // Re-enabling frame timestamps should get the latest values.
1361 disableFrameTimestamps();
1362 enableFrameTimestamps();
1363
1364 // A "now" over 2 intervals before the deadline snaps properly.
1365 mSurface->setNow(
1366 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1367 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1368 result = native_window_get_compositor_timing(mWindow.get(),
1369 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1370 EXPECT_EQ(NO_ERROR, result);
1371 expectedDeadline =
1372 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1373 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1374 EXPECT_EQ(expectedDeadline, compositeDeadline);
1375}
1376
Brian Anderson1049d1d2016-12-16 17:25:57 -08001377// This verifies the timestamps recorded in the consumer's
1378// FrameTimestampsHistory are properly retrieved by the producer for the
1379// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001380TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1381 enableFrameTimestamps();
1382
Brian Anderson1049d1d2016-12-16 17:25:57 -08001383 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001384 dequeueAndQueue(0);
1385 mFrames[0].signalQueueFences();
1386
Brian Anderson1049d1d2016-12-16 17:25:57 -08001387 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001388 dequeueAndQueue(1);
1389 mFrames[1].signalQueueFences();
1390
1391 addFrameEvents(true, NO_FRAME_INDEX, 0);
1392 mFrames[0].signalRefreshFences();
1393 addFrameEvents(true, 0, 1);
1394 mFrames[0].signalReleaseFences();
1395 mFrames[1].signalRefreshFences();
1396
1397 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001398 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001399 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001400 EXPECT_EQ(NO_ERROR, result);
1401 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1402 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001403 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1404 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1405 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001406 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1407 outGpuCompositionDoneTime);
1408 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001409 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001410 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1411
1412 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001413 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001414 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001415 EXPECT_EQ(NO_ERROR, result);
1416 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1417 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001418 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1419 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1420 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001421 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1422 outGpuCompositionDoneTime);
1423 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001424 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1425 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001426}
1427
1428// This test verifies the acquire fence recorded by the consumer is not sent
1429// back to the producer and the producer saves its own fence.
1430TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1431 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001432
Brian Anderson3da8d272016-07-28 16:20:47 -07001433 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001434 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001435 dequeueAndQueue(0);
1436
1437 // Verify queue-related timestamps for f1 are available immediately in the
1438 // producer without asking the consumer again, even before signaling the
1439 // acquire fence.
1440 resetTimestamps();
1441 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001442 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001443 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001444 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001445 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1446 EXPECT_EQ(NO_ERROR, result);
1447 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001448 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001449
1450 // Signal acquire fences. Verify a sync call still isn't necessary.
1451 mFrames[0].signalQueueFences();
1452
1453 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001454 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001455 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001456 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001457 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1458 EXPECT_EQ(NO_ERROR, result);
1459 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1460 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1461
1462 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001463 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001464 dequeueAndQueue(1);
1465
1466 // Verify queue-related timestamps for f2 are available immediately in the
1467 // producer without asking the consumer again, even before signaling the
1468 // acquire fence.
1469 resetTimestamps();
1470 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001471 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001472 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001473 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001474 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1475 EXPECT_EQ(NO_ERROR, result);
1476 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001477 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001478
1479 // Signal acquire fences. Verify a sync call still isn't necessary.
1480 mFrames[1].signalQueueFences();
1481
1482 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001483 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001484 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001485 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001486 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1487 EXPECT_EQ(NO_ERROR, result);
1488 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1489 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1490}
1491
1492TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1493 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001494
1495 // Dequeue and queue frame 1.
1496 dequeueAndQueue(0);
1497 mFrames[0].signalQueueFences();
1498
1499 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001500 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001501 dequeueAndQueue(1);
1502 mFrames[1].signalQueueFences();
1503
1504 addFrameEvents(true, NO_FRAME_INDEX, 0);
1505 mFrames[0].signalRefreshFences();
1506 addFrameEvents(true, 0, 1);
1507 mFrames[0].signalReleaseFences();
1508 mFrames[1].signalRefreshFences();
1509
1510 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001511 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001512 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001513 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1514 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001515 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001516 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1517}
1518
1519// This test verifies that fences can signal and update timestamps producer
1520// side without an additional sync call to the consumer.
1521TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1522 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001523
1524 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001525 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001526 dequeueAndQueue(0);
1527 mFrames[0].signalQueueFences();
1528
1529 // Dequeue and queue frame 2.
1530 dequeueAndQueue(1);
1531 mFrames[1].signalQueueFences();
1532
1533 addFrameEvents(true, NO_FRAME_INDEX, 0);
1534 addFrameEvents(true, 0, 1);
1535
1536 // Verify available timestamps are correct for frame 1, before any
1537 // fence has been signaled.
1538 // Note: A sync call is necessary here since the events triggered by
1539 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001540 resetTimestamps();
1541 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001542 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001543 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1544 EXPECT_EQ(NO_ERROR, result);
1545 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1546 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001547 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1548 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1549 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001550 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1551 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001552 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001553 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001554
1555 // Verify available timestamps are correct for frame 1 again, before any
1556 // fence has been signaled.
1557 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001558 resetTimestamps();
1559 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001560 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001561 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1562 EXPECT_EQ(NO_ERROR, result);
1563 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1564 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001565 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1566 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1567 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001568 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1569 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001570 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001571 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001572
1573 // Signal the fences for frame 1.
1574 mFrames[0].signalRefreshFences();
1575 mFrames[0].signalReleaseFences();
1576
1577 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001578 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 Anderson3da8d272016-07-28 16:20:47 -07001588 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1589 outGpuCompositionDoneTime);
1590 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001591 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001592 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1593}
1594
1595// This test verifies that if the frame wasn't GPU composited but has a refresh
1596// event a sync call isn't made to get the GPU composite done time since it will
1597// never exist.
1598TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1599 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001600
Brian Anderson3da8d272016-07-28 16:20:47 -07001601 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001602 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001603 dequeueAndQueue(0);
1604 mFrames[0].signalQueueFences();
1605
1606 // Dequeue and queue frame 2.
1607 dequeueAndQueue(1);
1608 mFrames[1].signalQueueFences();
1609
1610 addFrameEvents(false, NO_FRAME_INDEX, 0);
1611 addFrameEvents(false, 0, 1);
1612
1613 // Verify available timestamps are correct for frame 1, before any
1614 // fence has been signaled.
1615 // Note: A sync call is necessary here since the events triggered by
1616 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1617 resetTimestamps();
1618 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001619 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001620 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1621 EXPECT_EQ(NO_ERROR, result);
1622 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1623 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001624 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1625 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1626 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001627 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1628 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001629 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001630 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001631
1632 // Signal the fences for frame 1.
1633 mFrames[0].signalRefreshFences();
1634 mFrames[0].signalReleaseFences();
1635
1636 // Verify all timestamps, except GPU composition, are available without a
1637 // sync call.
1638 resetTimestamps();
1639 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001640 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001641 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1642 EXPECT_EQ(NO_ERROR, result);
1643 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1644 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001645 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1646 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1647 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001648 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001649 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001650 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001651 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1652}
1653
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001654// This test verifies that if the certain timestamps can't possibly exist for
1655// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001656TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001657 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001658
1659 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001660 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001661 dequeueAndQueue(0);
1662 mFrames[0].signalQueueFences();
1663
1664 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001665 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001666 dequeueAndQueue(1);
1667 mFrames[1].signalQueueFences();
1668
1669 addFrameEvents(false, NO_FRAME_INDEX, 0);
1670 addFrameEvents(false, 0, 1);
1671
1672 // Verify available timestamps are correct for frame 1, before any
1673 // fence has been signaled.
1674 // Note: A sync call is necessary here since the events triggered by
1675 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001676 resetTimestamps();
1677 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001678 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001679 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1680 EXPECT_EQ(NO_ERROR, result);
1681 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1682 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001683 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1684 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1685 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001686 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1687 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001688 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001689 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001690
1691 mFrames[0].signalRefreshFences();
1692 mFrames[0].signalReleaseFences();
1693 mFrames[1].signalRefreshFences();
1694
Brian Anderson1049d1d2016-12-16 17:25:57 -08001695 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001696 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001697 // available, a sync call should not occur because it's not possible for f2
1698 // to encounter the final value for those events until another frame is
1699 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001700 resetTimestamps();
1701 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001702 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001703 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1704 EXPECT_EQ(NO_ERROR, result);
1705 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1706 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001707 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1708 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1709 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001710 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001711 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001712 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1713 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001714}
1715
Brian Anderson6b376712017-04-04 10:51:39 -07001716// This test verifies there are no sync calls for present times
1717// when they aren't supported and that an error is returned.
1718
1719TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1720 enableFrameTimestamps();
1721 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1722
1723 // Dequeue and queue frame 1.
1724 const uint64_t fId1 = getNextFrameId();
1725 dequeueAndQueue(0);
1726
1727 // Verify a query for the Present times do not trigger a sync call if they
1728 // are not supported.
1729 resetTimestamps();
1730 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1731 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1732 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1733 &outDisplayPresentTime, nullptr, nullptr);
1734 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1735 EXPECT_EQ(BAD_VALUE, result);
1736 EXPECT_EQ(-1, outDisplayPresentTime);
1737}
1738
Dan Stoza932f0082017-05-31 13:50:16 -07001739} // namespace android