blob: 6db3c9c179a08c39a87a440e34d2a906330118b4 [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*/,
618 bool /*useIdentityTransform*/, Rotation /*rotation*/) override {
619 return NO_ERROR;
620 }
chaviwa76b2712017-09-20 12:02:26 -0700621 virtual status_t captureLayers(const sp<IBinder>& /*parentHandle*/,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700622 sp<GraphicBuffer>* /*outBuffer*/,
623 const ui::Dataspace /*reqDataspace*/,
624 const ui::PixelFormat /*reqPixelFormat*/,
625 const Rect& /*sourceCrop*/, float /*frameScale*/,
626 bool /*childrenOnly*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700627 return NO_ERROR;
628 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700629 status_t clearAnimationFrameStats() override { return NO_ERROR; }
630 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
631 return NO_ERROR;
632 }
633 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
634 HdrCapabilities* /*outCapabilities*/) const override {
635 return NO_ERROR;
636 }
637 status_t enableVSyncInjections(bool /*enable*/) override {
638 return NO_ERROR;
639 }
640 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800641 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
642 return NO_ERROR;
643 }
Peiyong Linc6780972018-10-28 15:24:08 -0700644 status_t getCompositionPreference(
645 ui::Dataspace* /*outDefaultDataspace*/, ui::PixelFormat* /*outDefaultPixelFormat*/,
646 ui::Dataspace* /*outWideColorGamutDataspace*/,
647 ui::PixelFormat* /*outWideColorGamutPixelFormat*/) const override {
Peiyong Lin0256f722018-08-31 15:45:10 -0700648 return NO_ERROR;
649 }
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700650 status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& /*display*/,
651 ui::PixelFormat* /*outFormat*/,
652 ui::Dataspace* /*outDataspace*/,
653 uint8_t* /*outComponentMask*/) const override {
654 return NO_ERROR;
655 }
Kevin DuBois74e53772018-11-19 10:52:38 -0800656 status_t setDisplayContentSamplingEnabled(const sp<IBinder>& /*display*/, bool /*enable*/,
657 uint8_t /*componentMask*/,
658 uint64_t /*maxFrames*/) const override {
659 return NO_ERROR;
660 }
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700661 status_t getDisplayedContentSample(const sp<IBinder>& /*display*/, uint64_t /*maxFrames*/,
662 uint64_t /*timestamp*/,
663 DisplayedFrameStats* /*outStats*/) const override {
664 return NO_ERROR;
665 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700666
Peiyong Lin3c2791e2019-01-14 17:05:18 -0800667 status_t getColorManagement(bool* /*outGetColorManagement*/) const override { return NO_ERROR; }
668 status_t getProtectedContentSupport(bool* /*outSupported*/) const override { return NO_ERROR; }
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700669
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800670 status_t isWideColorDisplay(const sp<IBinder>&, bool*) const override { return NO_ERROR; }
Marissa Wallebc2c052019-01-16 19:16:55 -0800671
Dan Stoza84ab9372018-12-17 15:27:57 -0800672 status_t addRegionSamplingListener(const Rect& /*samplingArea*/,
673 const sp<IBinder>& /*stopLayerHandle*/,
674 const sp<IRegionSamplingListener>& /*listener*/) override {
675 return NO_ERROR;
676 }
677 status_t removeRegionSamplingListener(
678 const sp<IRegionSamplingListener>& /*listener*/) override {
679 return NO_ERROR;
680 }
Ady Abraham838de062019-02-04 10:24:03 -0800681 status_t setAllowedDisplayConfigs(const sp<IBinder>& /*displayToken*/,
682 const std::vector<int32_t>& /*allowedConfigs*/) override {
683 return NO_ERROR;
684 }
Ady Abrahamd9b3ea62019-02-26 14:08:03 -0800685 status_t getAllowedDisplayConfigs(const sp<IBinder>& /*displayToken*/,
686 std::vector<int32_t>* /*outAllowedConfigs*/) override {
687 return NO_ERROR;
688 }
Dan Stoza84ab9372018-12-17 15:27:57 -0800689
Brian Anderson3da8d272016-07-28 16:20:47 -0700690protected:
691 IBinder* onAsBinder() override { return nullptr; }
692
693private:
694 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700695};
696
697class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
698public:
Chih-Hung Hsiehaaf62162018-12-20 15:45:04 -0800699 explicit FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap) : mFenceMap(fenceMap) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700700
701 ~FakeProducerFrameEventHistory() {}
702
703 void updateAcquireFence(uint64_t frameNumber,
704 std::shared_ptr<FenceTime>&& acquire) override {
705 // Verify the acquire fence being added isn't the one from the consumer.
706 EXPECT_NE(mConsumerAcquireFence, acquire);
707 // Override the fence, so we can verify this was called by the
708 // producer after the frame is queued.
709 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
710 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
711 }
712
713 void setAcquireFenceOverride(
714 const std::shared_ptr<FenceTime>& acquireFenceOverride,
715 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
716 mAcquireFenceOverride = acquireFenceOverride;
717 mConsumerAcquireFence = consumerAcquireFence;
718 }
719
720protected:
721 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
722 const override {
723 return mFenceMap->createFenceTimeForTest(fence);
724 }
725
726 FenceToFenceTimeMap* mFenceMap{nullptr};
727
728 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
729 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
730};
731
732
733class TestSurface : public Surface {
734public:
735 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
736 FenceToFenceTimeMap* fenceMap)
737 : Surface(bufferProducer),
738 mFakeSurfaceComposer(new FakeSurfaceComposer) {
739 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
740 mFrameEventHistory.reset(mFakeFrameEventHistory);
741 }
742
743 ~TestSurface() override {}
744
745 sp<ISurfaceComposer> composerService() const override {
746 return mFakeSurfaceComposer;
747 }
748
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800749 nsecs_t now() const override {
750 return mNow;
751 }
752
753 void setNow(nsecs_t now) {
754 mNow = now;
755 }
756
Brian Anderson3da8d272016-07-28 16:20:47 -0700757public:
758 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800759 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700760
761 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
762 // but this raw pointer gives access to test functionality.
763 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
764};
765
766
Brian Andersond0010582017-03-07 13:20:31 -0800767class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700768protected:
769 struct FenceAndFenceTime {
770 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
771 : mFence(new Fence),
772 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
773 sp<Fence> mFence { nullptr };
774 std::shared_ptr<FenceTime> mFenceTime { nullptr };
775 };
776
777 struct RefreshEvents {
778 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800779 : mFenceMap(fenceMap),
780 kCompositorTiming(
781 {refreshStart, refreshStart + 1, refreshStart + 2 }),
782 kStartTime(refreshStart + 3),
783 kGpuCompositionDoneTime(refreshStart + 4),
784 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700785
786 void signalPostCompositeFences() {
787 mFenceMap.signalAllForTest(
788 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
789 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
790 }
791
792 FenceToFenceTimeMap& mFenceMap;
793
794 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
795 FenceAndFenceTime mPresent { mFenceMap };
796
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800797 const CompositorTiming kCompositorTiming;
798
Brian Anderson3da8d272016-07-28 16:20:47 -0700799 const nsecs_t kStartTime;
800 const nsecs_t kGpuCompositionDoneTime;
801 const nsecs_t kPresentTime;
802 };
803
804 struct FrameEvents {
805 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
806 : mFenceMap(fenceMap),
807 kPostedTime(frameStartTime + 100),
808 kRequestedPresentTime(frameStartTime + 200),
809 kProducerAcquireTime(frameStartTime + 300),
810 kConsumerAcquireTime(frameStartTime + 301),
811 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700812 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700813 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700814 mRefreshes {
815 { mFenceMap, frameStartTime + 410 },
816 { mFenceMap, frameStartTime + 420 },
817 { mFenceMap, frameStartTime + 430 } } {}
818
819 void signalQueueFences() {
820 mFenceMap.signalAllForTest(
821 mAcquireConsumer.mFence, kConsumerAcquireTime);
822 mFenceMap.signalAllForTest(
823 mAcquireProducer.mFence, kProducerAcquireTime);
824 }
825
826 void signalRefreshFences() {
827 for (auto& re : mRefreshes) {
828 re.signalPostCompositeFences();
829 }
830 }
831
832 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700833 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
834 }
835
836 FenceToFenceTimeMap& mFenceMap;
837
838 FenceAndFenceTime mAcquireConsumer { mFenceMap };
839 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700840 FenceAndFenceTime mRelease { mFenceMap };
841
842 const nsecs_t kPostedTime;
843 const nsecs_t kRequestedPresentTime;
844 const nsecs_t kProducerAcquireTime;
845 const nsecs_t kConsumerAcquireTime;
846 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700847 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700848 const nsecs_t kReleaseTime;
849
850 RefreshEvents mRefreshes[3];
851 };
852
Brian Andersond0010582017-03-07 13:20:31 -0800853 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700854
855 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700856 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
857 mFakeConsumer = new FakeConsumer;
858 mCfeh = &mFakeConsumer->mFrameEventHistory;
859 mConsumer->consumerConnect(mFakeConsumer, false);
860 mConsumer->setConsumerName(String8("TestConsumer"));
861 mSurface = new TestSurface(mProducer, &mFenceMap);
862 mWindow = mSurface;
863
864 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
865 NATIVE_WINDOW_API_CPU));
866 native_window_set_buffer_count(mWindow.get(), 4);
867 }
868
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800869 void disableFrameTimestamps() {
870 mFakeConsumer->mGetFrameTimestampsEnabled = false;
871 native_window_enable_frame_timestamps(mWindow.get(), 0);
872 mFrameTimestampsEnabled = false;
873 }
874
Brian Anderson3da8d272016-07-28 16:20:47 -0700875 void enableFrameTimestamps() {
876 mFakeConsumer->mGetFrameTimestampsEnabled = true;
877 native_window_enable_frame_timestamps(mWindow.get(), 1);
878 mFrameTimestampsEnabled = true;
879 }
880
Brian Anderson1049d1d2016-12-16 17:25:57 -0800881 int getAllFrameTimestamps(uint64_t frameId) {
882 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700883 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
884 &outFirstRefreshStartTime, &outLastRefreshStartTime,
885 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700886 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700887 }
888
Brian Anderson3da8d272016-07-28 16:20:47 -0700889 void resetTimestamps() {
890 outRequestedPresentTime = -1;
891 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700892 outLatchTime = -1;
893 outFirstRefreshStartTime = -1;
894 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700895 outGpuCompositionDoneTime = -1;
896 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700897 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700898 outReleaseTime = -1;
899 }
900
Brian Anderson1049d1d2016-12-16 17:25:57 -0800901 uint64_t getNextFrameId() {
902 uint64_t frameId = -1;
903 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
904 EXPECT_EQ(status, NO_ERROR);
905 return frameId;
906 }
907
Brian Anderson3da8d272016-07-28 16:20:47 -0700908 void dequeueAndQueue(uint64_t frameIndex) {
909 int fence = -1;
910 ANativeWindowBuffer* buffer = nullptr;
911 ASSERT_EQ(NO_ERROR,
912 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
913
914 int oldAddFrameTimestampsCount =
915 mFakeConsumer->mAddFrameTimestampsCount;
916
917 FrameEvents* frame = &mFrames[frameIndex];
918 uint64_t frameNumber = frameIndex + 1;
919
920 NewFrameEventsEntry fe;
921 fe.frameNumber = frameNumber;
922 fe.postedTime = frame->kPostedTime;
923 fe.requestedPresentTime = frame->kRequestedPresentTime;
924 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
925 mFakeConsumer->mNewFrameEntryOverride = fe;
926
927 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
928 frame->mAcquireProducer.mFenceTime,
929 frame->mAcquireConsumer.mFenceTime);
930
931 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
932
933 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
934
935 EXPECT_EQ(
936 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
937 mFakeConsumer->mAddFrameTimestampsCount);
938 }
939
940 void addFrameEvents(
941 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
942 FrameEvents* oldFrame =
943 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
944 FrameEvents* newFrame = &mFrames[iNewFrame];
945
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800946 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700947 uint64_t nNewFrame = iNewFrame + 1;
948
Brian Anderson4e606e32017-03-16 15:34:57 -0700949 // Latch, Composite, and Release the frames in a plausible order.
950 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700951 // that's okay for the purposes of this test.
952 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
953
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700954 // Composite the previous frame one more time, which helps verify
955 // LastRefresh is updated properly.
956 if (oldFrame != nullptr) {
957 mCfeh->addPreComposition(nOldFrame,
958 oldFrame->mRefreshes[2].kStartTime);
959 gpuDoneFenceTime = gpuComposited ?
960 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
961 FenceTime::NO_FENCE;
962 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800963 oldFrame->mRefreshes[2].mPresent.mFenceTime,
964 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700965 }
966
967 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700968 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
969
970 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
971 gpuDoneFenceTime = gpuComposited ?
972 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
973 FenceTime::NO_FENCE;
974 // HWC2 releases the previous buffer after a new latch just before
975 // calling postComposition.
976 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700977 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700978 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
979 }
980 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800981 newFrame->mRefreshes[0].mPresent.mFenceTime,
982 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700983
Brian Anderson3da8d272016-07-28 16:20:47 -0700984 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
985 gpuDoneFenceTime = gpuComposited ?
986 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
987 FenceTime::NO_FENCE;
988 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800989 newFrame->mRefreshes[1].mPresent.mFenceTime,
990 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700991 }
992
Brian Anderson3da8d272016-07-28 16:20:47 -0700993 sp<IGraphicBufferProducer> mProducer;
994 sp<IGraphicBufferConsumer> mConsumer;
995 sp<FakeConsumer> mFakeConsumer;
996 ConsumerFrameEventHistory* mCfeh;
997 sp<TestSurface> mSurface;
998 sp<ANativeWindow> mWindow;
999
1000 FenceToFenceTimeMap mFenceMap;
1001
1002 bool mFrameTimestampsEnabled = false;
1003
1004 int64_t outRequestedPresentTime = -1;
1005 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001006 int64_t outLatchTime = -1;
1007 int64_t outFirstRefreshStartTime = -1;
1008 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001009 int64_t outGpuCompositionDoneTime = -1;
1010 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001011 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001012 int64_t outReleaseTime = -1;
1013
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001014 FrameEvents mFrames[3] {
1015 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -07001016};
1017
1018
1019// This test verifies that the frame timestamps are not retrieved when not
1020// explicitly enabled via native_window_enable_frame_timestamps.
1021// We want to check this to make sure there's no overhead for users
1022// that don't need the timestamp information.
1023TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
1024 int fence;
1025 ANativeWindowBuffer* buffer;
1026
1027 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1028 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1029
Brian Anderson1049d1d2016-12-16 17:25:57 -08001030 const uint64_t fId = getNextFrameId();
1031
Brian Anderson3da8d272016-07-28 16:20:47 -07001032 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
1033 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1034 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1035 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1036
1037 // Verify the producer doesn't get frame timestamps piggybacked on queue.
1038 // It is okay that frame timestamps are added in the consumer since it is
1039 // still needed for SurfaceFlinger dumps.
1040 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1041 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1042 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1043
1044 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001045 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -07001046 EXPECT_EQ(INVALID_OPERATION, result);
1047 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001048
1049 // Verify compositor timing query fails.
1050 nsecs_t compositeDeadline = 0;
1051 nsecs_t compositeInterval = 0;
1052 nsecs_t compositeToPresentLatency = 0;
1053 result = native_window_get_compositor_timing(mWindow.get(),
1054 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1055 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001056}
1057
1058// This test verifies that the frame timestamps are retrieved if explicitly
1059// enabled via native_window_enable_frame_timestamps.
1060TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001061 CompositorTiming initialCompositorTiming {
1062 1000000000, // 1s deadline
1063 16666667, // 16ms interval
1064 50000000, // 50ms present latency
1065 };
1066 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1067
Brian Anderson3da8d272016-07-28 16:20:47 -07001068 enableFrameTimestamps();
1069
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001070 // Verify the compositor timing query gets the initial compositor values
1071 // after timststamps are enabled; even before the first frame is queued
1072 // or dequeued.
1073 nsecs_t compositeDeadline = 0;
1074 nsecs_t compositeInterval = 0;
1075 nsecs_t compositeToPresentLatency = 0;
1076 mSurface->setNow(initialCompositorTiming.deadline - 1);
1077 int result = native_window_get_compositor_timing(mWindow.get(),
1078 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1079 EXPECT_EQ(NO_ERROR, result);
1080 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1081 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1082 EXPECT_EQ(initialCompositorTiming.presentLatency,
1083 compositeToPresentLatency);
1084
Brian Anderson3da8d272016-07-28 16:20:47 -07001085 int fence;
1086 ANativeWindowBuffer* buffer;
1087
1088 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001089 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001090
Brian Anderson1049d1d2016-12-16 17:25:57 -08001091 const uint64_t fId1 = getNextFrameId();
1092
Brian Anderson3da8d272016-07-28 16:20:47 -07001093 // Verify getFrameTimestamps is piggybacked on dequeue.
1094 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1095 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001096 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001097
1098 NewFrameEventsEntry f1;
1099 f1.frameNumber = 1;
1100 f1.postedTime = mFrames[0].kPostedTime;
1101 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1102 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1103 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1104 mFrames[0].mAcquireProducer.mFenceTime,
1105 mFrames[0].mAcquireConsumer.mFenceTime);
1106 mFakeConsumer->mNewFrameEntryOverride = f1;
1107 mFrames[0].signalQueueFences();
1108
1109 // Verify getFrameTimestamps is piggybacked on queue.
1110 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1111 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1112 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001113 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001114
1115 // Verify queries for timestamps that the producer doesn't know about
1116 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001117 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001118 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001119 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001120}
1121
Brian Anderson6b376712017-04-04 10:51:39 -07001122TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1123 bool displayPresentSupported = true;
1124 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1125
1126 // Verify supported bits are forwarded.
1127 int supportsPresent = -1;
1128 mWindow.get()->query(mWindow.get(),
1129 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1130 EXPECT_EQ(displayPresentSupported, supportsPresent);
1131}
1132
1133TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1134 bool displayPresentSupported = false;
1135 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1136
1137 // Verify supported bits are forwarded.
1138 int supportsPresent = -1;
1139 mWindow.get()->query(mWindow.get(),
1140 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1141 EXPECT_EQ(displayPresentSupported, supportsPresent);
1142}
1143
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001144TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1145 nsecs_t phase = 4000;
1146 nsecs_t interval = 1000;
1147
1148 // Timestamp in previous interval.
1149 nsecs_t timestamp = 3500;
1150 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1151 timestamp, phase, interval));
1152
1153 // Timestamp in next interval.
1154 timestamp = 4500;
1155 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1156 timestamp, phase, interval));
1157
1158 // Timestamp multiple intervals before.
1159 timestamp = 2500;
1160 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1161 timestamp, phase, interval));
1162
1163 // Timestamp multiple intervals after.
1164 timestamp = 6500;
1165 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1166 timestamp, phase, interval));
1167
1168 // Timestamp on previous interval.
1169 timestamp = 3000;
1170 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1171 timestamp, phase, interval));
1172
1173 // Timestamp on next interval.
1174 timestamp = 5000;
1175 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1176 timestamp, phase, interval));
1177
1178 // Timestamp equal to phase.
1179 timestamp = 4000;
1180 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1181 timestamp, phase, interval));
1182}
1183
1184// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1185// if the number of intervals elapsed is internally stored in an int.
1186TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1187 nsecs_t phase = 0;
1188 nsecs_t interval = 4000;
1189 nsecs_t big_timestamp = 8635916564000;
1190 int32_t intervals = big_timestamp / interval;
1191
1192 EXPECT_LT(intervals, 0);
1193 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1194 big_timestamp, phase, interval));
1195 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1196 big_timestamp, big_timestamp, interval));
1197}
1198
1199// This verifies the compositor timing is updated by refresh events
1200// and piggy backed on a queue, dequeue, and enabling of timestamps..
1201TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1202 CompositorTiming initialCompositorTiming {
1203 1000000000, // 1s deadline
1204 16666667, // 16ms interval
1205 50000000, // 50ms present latency
1206 };
1207 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1208
1209 enableFrameTimestamps();
1210
1211 // We get the initial values before any frames are submitted.
1212 nsecs_t compositeDeadline = 0;
1213 nsecs_t compositeInterval = 0;
1214 nsecs_t compositeToPresentLatency = 0;
1215 mSurface->setNow(initialCompositorTiming.deadline - 1);
1216 int result = native_window_get_compositor_timing(mWindow.get(),
1217 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1218 EXPECT_EQ(NO_ERROR, result);
1219 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1220 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1221 EXPECT_EQ(initialCompositorTiming.presentLatency,
1222 compositeToPresentLatency);
1223
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001224 dequeueAndQueue(0);
1225 addFrameEvents(true, NO_FRAME_INDEX, 0);
1226
1227 // Still get the initial values because the frame events for frame 0
1228 // didn't get a chance to piggyback on a queue or dequeue yet.
1229 result = native_window_get_compositor_timing(mWindow.get(),
1230 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1231 EXPECT_EQ(NO_ERROR, result);
1232 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1233 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1234 EXPECT_EQ(initialCompositorTiming.presentLatency,
1235 compositeToPresentLatency);
1236
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001237 dequeueAndQueue(1);
1238 addFrameEvents(true, 0, 1);
1239
1240 // Now expect the composite values associated with frame 1.
1241 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1242 result = native_window_get_compositor_timing(mWindow.get(),
1243 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1244 EXPECT_EQ(NO_ERROR, result);
1245 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1246 compositeDeadline);
1247 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1248 compositeInterval);
1249 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1250 compositeToPresentLatency);
1251
1252 dequeueAndQueue(2);
1253 addFrameEvents(true, 1, 2);
1254
1255 // Now expect the composite values associated with frame 2.
1256 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1257 result = native_window_get_compositor_timing(mWindow.get(),
1258 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1259 EXPECT_EQ(NO_ERROR, result);
1260 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1261 compositeDeadline);
1262 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1263 compositeInterval);
1264 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1265 compositeToPresentLatency);
1266
1267 // Re-enabling frame timestamps should get the latest values.
1268 disableFrameTimestamps();
1269 enableFrameTimestamps();
1270
1271 // Now expect the composite values associated with frame 3.
1272 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1273 result = native_window_get_compositor_timing(mWindow.get(),
1274 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1275 EXPECT_EQ(NO_ERROR, result);
1276 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1277 compositeDeadline);
1278 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1279 compositeInterval);
1280 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1281 compositeToPresentLatency);
1282}
1283
1284// This verifies the compositor deadline properly snaps to the the next
1285// deadline based on the current time.
1286TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1287 CompositorTiming initialCompositorTiming {
1288 1000000000, // 1s deadline
1289 16666667, // 16ms interval
1290 50000000, // 50ms present latency
1291 };
1292 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1293
1294 enableFrameTimestamps();
1295
1296 nsecs_t compositeDeadline = 0;
1297 nsecs_t compositeInterval = 0;
1298 nsecs_t compositeToPresentLatency = 0;
1299
1300 // A "now" just before the deadline snaps to the deadline.
1301 mSurface->setNow(initialCompositorTiming.deadline - 1);
1302 int result = native_window_get_compositor_timing(mWindow.get(),
1303 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1304 EXPECT_EQ(NO_ERROR, result);
1305 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1306 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1307 EXPECT_EQ(expectedDeadline, compositeDeadline);
1308
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001309 dequeueAndQueue(0);
1310 addFrameEvents(true, NO_FRAME_INDEX, 0);
1311
1312 // A "now" just after the deadline snaps properly.
1313 mSurface->setNow(initialCompositorTiming.deadline + 1);
1314 result = native_window_get_compositor_timing(mWindow.get(),
1315 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1316 EXPECT_EQ(NO_ERROR, result);
1317 expectedDeadline =
1318 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1319 EXPECT_EQ(expectedDeadline, compositeDeadline);
1320
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001321 dequeueAndQueue(1);
1322 addFrameEvents(true, 0, 1);
1323
1324 // A "now" just after the next interval snaps properly.
1325 mSurface->setNow(
1326 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1327 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1328 result = native_window_get_compositor_timing(mWindow.get(),
1329 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1330 EXPECT_EQ(NO_ERROR, result);
1331 expectedDeadline =
1332 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1333 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1334 EXPECT_EQ(expectedDeadline, compositeDeadline);
1335
1336 dequeueAndQueue(2);
1337 addFrameEvents(true, 1, 2);
1338
1339 // A "now" over 1 interval before the deadline snaps properly.
1340 mSurface->setNow(
1341 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1342 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1343 result = native_window_get_compositor_timing(mWindow.get(),
1344 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1345 EXPECT_EQ(NO_ERROR, result);
1346 expectedDeadline =
1347 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1348 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1349 EXPECT_EQ(expectedDeadline, compositeDeadline);
1350
1351 // Re-enabling frame timestamps should get the latest values.
1352 disableFrameTimestamps();
1353 enableFrameTimestamps();
1354
1355 // A "now" over 2 intervals before the deadline snaps properly.
1356 mSurface->setNow(
1357 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1358 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1359 result = native_window_get_compositor_timing(mWindow.get(),
1360 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1361 EXPECT_EQ(NO_ERROR, result);
1362 expectedDeadline =
1363 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1364 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1365 EXPECT_EQ(expectedDeadline, compositeDeadline);
1366}
1367
Brian Anderson1049d1d2016-12-16 17:25:57 -08001368// This verifies the timestamps recorded in the consumer's
1369// FrameTimestampsHistory are properly retrieved by the producer for the
1370// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001371TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1372 enableFrameTimestamps();
1373
Brian Anderson1049d1d2016-12-16 17:25:57 -08001374 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001375 dequeueAndQueue(0);
1376 mFrames[0].signalQueueFences();
1377
Brian Anderson1049d1d2016-12-16 17:25:57 -08001378 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001379 dequeueAndQueue(1);
1380 mFrames[1].signalQueueFences();
1381
1382 addFrameEvents(true, NO_FRAME_INDEX, 0);
1383 mFrames[0].signalRefreshFences();
1384 addFrameEvents(true, 0, 1);
1385 mFrames[0].signalReleaseFences();
1386 mFrames[1].signalRefreshFences();
1387
1388 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001389 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001390 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001391 EXPECT_EQ(NO_ERROR, result);
1392 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1393 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001394 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1395 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1396 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001397 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1398 outGpuCompositionDoneTime);
1399 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001400 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001401 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1402
1403 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001404 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001405 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001406 EXPECT_EQ(NO_ERROR, result);
1407 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1408 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001409 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1410 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1411 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001412 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1413 outGpuCompositionDoneTime);
1414 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001415 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1416 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001417}
1418
1419// This test verifies the acquire fence recorded by the consumer is not sent
1420// back to the producer and the producer saves its own fence.
1421TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1422 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001423
Brian Anderson3da8d272016-07-28 16:20:47 -07001424 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001425 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001426 dequeueAndQueue(0);
1427
1428 // Verify queue-related timestamps for f1 are available immediately in the
1429 // producer without asking the consumer again, even before signaling the
1430 // acquire fence.
1431 resetTimestamps();
1432 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001433 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001434 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001435 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001436 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1437 EXPECT_EQ(NO_ERROR, result);
1438 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001439 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001440
1441 // Signal acquire fences. Verify a sync call still isn't necessary.
1442 mFrames[0].signalQueueFences();
1443
1444 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001445 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001446 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001447 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001448 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1449 EXPECT_EQ(NO_ERROR, result);
1450 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1451 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1452
1453 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001454 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001455 dequeueAndQueue(1);
1456
1457 // Verify queue-related timestamps for f2 are available immediately in the
1458 // producer without asking the consumer again, even before signaling the
1459 // acquire fence.
1460 resetTimestamps();
1461 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001462 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001463 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001464 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001465 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1466 EXPECT_EQ(NO_ERROR, result);
1467 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001468 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001469
1470 // Signal acquire fences. Verify a sync call still isn't necessary.
1471 mFrames[1].signalQueueFences();
1472
1473 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001474 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001475 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001476 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001477 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1478 EXPECT_EQ(NO_ERROR, result);
1479 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1480 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1481}
1482
1483TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1484 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001485
1486 // Dequeue and queue frame 1.
1487 dequeueAndQueue(0);
1488 mFrames[0].signalQueueFences();
1489
1490 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001491 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001492 dequeueAndQueue(1);
1493 mFrames[1].signalQueueFences();
1494
1495 addFrameEvents(true, NO_FRAME_INDEX, 0);
1496 mFrames[0].signalRefreshFences();
1497 addFrameEvents(true, 0, 1);
1498 mFrames[0].signalReleaseFences();
1499 mFrames[1].signalRefreshFences();
1500
1501 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001502 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001503 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001504 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1505 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001506 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001507 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1508}
1509
1510// This test verifies that fences can signal and update timestamps producer
1511// side without an additional sync call to the consumer.
1512TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1513 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001514
1515 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001516 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001517 dequeueAndQueue(0);
1518 mFrames[0].signalQueueFences();
1519
1520 // Dequeue and queue frame 2.
1521 dequeueAndQueue(1);
1522 mFrames[1].signalQueueFences();
1523
1524 addFrameEvents(true, NO_FRAME_INDEX, 0);
1525 addFrameEvents(true, 0, 1);
1526
1527 // Verify available timestamps are correct for frame 1, before any
1528 // fence has been signaled.
1529 // Note: A sync call is necessary here since the events triggered by
1530 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001531 resetTimestamps();
1532 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001533 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001534 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1535 EXPECT_EQ(NO_ERROR, result);
1536 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1537 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001538 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1539 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1540 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001541 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1542 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001543 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001544 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001545
1546 // Verify available timestamps are correct for frame 1 again, before any
1547 // fence has been signaled.
1548 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001549 resetTimestamps();
1550 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001551 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001552 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1553 EXPECT_EQ(NO_ERROR, result);
1554 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1555 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001556 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1557 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1558 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001559 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1560 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001561 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001562 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001563
1564 // Signal the fences for frame 1.
1565 mFrames[0].signalRefreshFences();
1566 mFrames[0].signalReleaseFences();
1567
1568 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001569 resetTimestamps();
1570 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001571 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001572 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1573 EXPECT_EQ(NO_ERROR, result);
1574 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1575 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001576 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1577 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1578 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001579 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1580 outGpuCompositionDoneTime);
1581 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001582 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001583 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1584}
1585
1586// This test verifies that if the frame wasn't GPU composited but has a refresh
1587// event a sync call isn't made to get the GPU composite done time since it will
1588// never exist.
1589TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1590 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001591
Brian Anderson3da8d272016-07-28 16:20:47 -07001592 // 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.
1598 dequeueAndQueue(1);
1599 mFrames[1].signalQueueFences();
1600
1601 addFrameEvents(false, NO_FRAME_INDEX, 0);
1602 addFrameEvents(false, 0, 1);
1603
1604 // Verify available timestamps are correct for frame 1, before any
1605 // fence has been signaled.
1606 // Note: A sync call is necessary here since the events triggered by
1607 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1608 resetTimestamps();
1609 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001610 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001611 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1612 EXPECT_EQ(NO_ERROR, result);
1613 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1614 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001615 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1616 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1617 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001618 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1619 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001620 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001621 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001622
1623 // Signal the fences for frame 1.
1624 mFrames[0].signalRefreshFences();
1625 mFrames[0].signalReleaseFences();
1626
1627 // Verify all timestamps, except GPU composition, are available without a
1628 // sync call.
1629 resetTimestamps();
1630 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001631 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001632 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1633 EXPECT_EQ(NO_ERROR, result);
1634 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1635 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001636 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1637 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1638 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001639 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001640 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001641 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001642 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1643}
1644
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001645// This test verifies that if the certain timestamps can't possibly exist for
1646// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001647TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001648 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001649
1650 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001651 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001652 dequeueAndQueue(0);
1653 mFrames[0].signalQueueFences();
1654
1655 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001656 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001657 dequeueAndQueue(1);
1658 mFrames[1].signalQueueFences();
1659
1660 addFrameEvents(false, NO_FRAME_INDEX, 0);
1661 addFrameEvents(false, 0, 1);
1662
1663 // Verify available timestamps are correct for frame 1, before any
1664 // fence has been signaled.
1665 // Note: A sync call is necessary here since the events triggered by
1666 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001667 resetTimestamps();
1668 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001669 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001670 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1671 EXPECT_EQ(NO_ERROR, result);
1672 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1673 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001674 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1675 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1676 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001677 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1678 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001679 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001680 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001681
1682 mFrames[0].signalRefreshFences();
1683 mFrames[0].signalReleaseFences();
1684 mFrames[1].signalRefreshFences();
1685
Brian Anderson1049d1d2016-12-16 17:25:57 -08001686 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001687 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001688 // available, a sync call should not occur because it's not possible for f2
1689 // to encounter the final value for those events until another frame is
1690 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001691 resetTimestamps();
1692 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001693 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001694 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1695 EXPECT_EQ(NO_ERROR, result);
1696 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1697 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001698 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1699 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1700 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001701 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001702 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001703 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1704 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001705}
1706
Brian Anderson6b376712017-04-04 10:51:39 -07001707// This test verifies there are no sync calls for present times
1708// when they aren't supported and that an error is returned.
1709
1710TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1711 enableFrameTimestamps();
1712 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1713
1714 // Dequeue and queue frame 1.
1715 const uint64_t fId1 = getNextFrameId();
1716 dequeueAndQueue(0);
1717
1718 // Verify a query for the Present times do not trigger a sync call if they
1719 // are not supported.
1720 resetTimestamps();
1721 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1722 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1723 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1724 &outDisplayPresentTime, nullptr, nullptr);
1725 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1726 EXPECT_EQ(BAD_VALUE, result);
1727 EXPECT_EQ(-1, outDisplayPresentTime);
1728}
1729
Dan Stoza932f0082017-05-31 13:50:16 -07001730} // namespace android