blob: fa2e97f1a83d5f5518015b2387cfdab4a2b749d5 [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*/,
Marissa Wall78b72202019-03-15 14:58:34 -0700563 int64_t /*desiredPresentTime*/,
Marissa Wall3dad52d2019-03-22 14:03:19 -0700564 const cached_buffer_t& /*cachedBuffer*/,
565 const std::vector<ListenerCallbacks>& /*listenerCallbacks*/) override {
566 }
Marissa Wall17b4e452018-12-26 16:32:34 -0800567
Brian Anderson3da8d272016-07-28 16:20:47 -0700568 void bootFinished() override {}
569 bool authenticateSurfaceTexture(
570 const sp<IGraphicBufferProducer>& /*surface*/) const override {
571 return false;
572 }
Brian Anderson6b376712017-04-04 10:51:39 -0700573
574 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
575 const override {
576 *outSupported = {
577 FrameEvent::REQUESTED_PRESENT,
578 FrameEvent::ACQUIRE,
579 FrameEvent::LATCH,
580 FrameEvent::FIRST_REFRESH_START,
581 FrameEvent::LAST_REFRESH_START,
582 FrameEvent::GPU_COMPOSITION_DONE,
583 FrameEvent::DEQUEUE_READY,
584 FrameEvent::RELEASE
585 };
586 if (mSupportsPresent) {
587 outSupported->push_back(
588 FrameEvent::DISPLAY_PRESENT);
589 }
590 return NO_ERROR;
591 }
592
Brian Anderson3da8d272016-07-28 16:20:47 -0700593 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
594 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
595 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
596 status_t getDisplayStats(const sp<IBinder>& /*display*/,
597 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
598 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
599 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
600 override {
601 return NO_ERROR;
602 }
603 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700604 Vector<ColorMode>* /*outColorModes*/) override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700605 return NO_ERROR;
606 }
Daniel Solomon42d04562019-01-20 21:03:19 -0800607 status_t getDisplayNativePrimaries(const sp<IBinder>& /*display*/,
608 ui::DisplayPrimaries& /*primaries*/) override {
609 return NO_ERROR;
610 }
Peiyong Lina52f0292018-03-14 17:26:31 -0700611 ColorMode getActiveColorMode(const sp<IBinder>& /*display*/)
Brian Anderson3da8d272016-07-28 16:20:47 -0700612 override {
Peiyong Lina52f0292018-03-14 17:26:31 -0700613 return ColorMode::NATIVE;
Brian Anderson3da8d272016-07-28 16:20:47 -0700614 }
615 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700616 ColorMode /*colorMode*/) override { return NO_ERROR; }
Peiyong Lin0e003c92018-09-17 11:09:51 -0700617 status_t captureScreen(const sp<IBinder>& /*display*/, sp<GraphicBuffer>* /*outBuffer*/,
618 const ui::Dataspace /*reqDataspace*/,
619 const ui::PixelFormat /*reqPixelFormat*/, Rect /*sourceCrop*/,
620 uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrfa8855f2019-02-19 10:05:00 -0800621 bool /*useIdentityTransform*/, Rotation /*rotation*/,
622 bool /*captureSecureLayers*/) override {
Peiyong Lin0e003c92018-09-17 11:09:51 -0700623 return NO_ERROR;
624 }
Robert Carr866455f2019-04-02 16:28:26 -0700625 virtual status_t captureLayers(
626 const sp<IBinder>& /*parentHandle*/, sp<GraphicBuffer>* /*outBuffer*/,
627 const ui::Dataspace /*reqDataspace*/, const ui::PixelFormat /*reqPixelFormat*/,
628 const Rect& /*sourceCrop*/,
629 const std::unordered_set<sp<IBinder>,
630 ISurfaceComposer::SpHash<IBinder>>& /*excludeHandles*/,
631 float /*frameScale*/, bool /*childrenOnly*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700632 return NO_ERROR;
633 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700634 status_t clearAnimationFrameStats() override { return NO_ERROR; }
635 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
636 return NO_ERROR;
637 }
638 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
639 HdrCapabilities* /*outCapabilities*/) const override {
640 return NO_ERROR;
641 }
642 status_t enableVSyncInjections(bool /*enable*/) override {
643 return NO_ERROR;
644 }
645 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800646 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
647 return NO_ERROR;
648 }
Peiyong Linc6780972018-10-28 15:24:08 -0700649 status_t getCompositionPreference(
650 ui::Dataspace* /*outDefaultDataspace*/, ui::PixelFormat* /*outDefaultPixelFormat*/,
651 ui::Dataspace* /*outWideColorGamutDataspace*/,
652 ui::PixelFormat* /*outWideColorGamutPixelFormat*/) const override {
Peiyong Lin0256f722018-08-31 15:45:10 -0700653 return NO_ERROR;
654 }
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700655 status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& /*display*/,
656 ui::PixelFormat* /*outFormat*/,
657 ui::Dataspace* /*outDataspace*/,
658 uint8_t* /*outComponentMask*/) const override {
659 return NO_ERROR;
660 }
Kevin DuBois74e53772018-11-19 10:52:38 -0800661 status_t setDisplayContentSamplingEnabled(const sp<IBinder>& /*display*/, bool /*enable*/,
662 uint8_t /*componentMask*/,
663 uint64_t /*maxFrames*/) const override {
664 return NO_ERROR;
665 }
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700666 status_t getDisplayedContentSample(const sp<IBinder>& /*display*/, uint64_t /*maxFrames*/,
667 uint64_t /*timestamp*/,
668 DisplayedFrameStats* /*outStats*/) const override {
669 return NO_ERROR;
670 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700671
Peiyong Lin3c2791e2019-01-14 17:05:18 -0800672 status_t getColorManagement(bool* /*outGetColorManagement*/) const override { return NO_ERROR; }
673 status_t getProtectedContentSupport(bool* /*outSupported*/) const override { return NO_ERROR; }
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700674
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800675 status_t isWideColorDisplay(const sp<IBinder>&, bool*) const override { return NO_ERROR; }
Dan Gittik57e63c52019-01-18 16:37:54 +0000676 status_t getDisplayBrightnessSupport(const sp<IBinder>& /*displayToken*/,
677 bool* /*outSupport*/) const override {
678 return NO_ERROR;
679 }
680 status_t setDisplayBrightness(const sp<IBinder>& /*displayToken*/,
681 float /*brightness*/) const override {
682 return NO_ERROR;
683 }
Marissa Wallebc2c052019-01-16 19:16:55 -0800684
Dan Stoza84ab9372018-12-17 15:27:57 -0800685 status_t addRegionSamplingListener(const Rect& /*samplingArea*/,
686 const sp<IBinder>& /*stopLayerHandle*/,
687 const sp<IRegionSamplingListener>& /*listener*/) override {
688 return NO_ERROR;
689 }
690 status_t removeRegionSamplingListener(
691 const sp<IRegionSamplingListener>& /*listener*/) override {
692 return NO_ERROR;
693 }
Ady Abraham838de062019-02-04 10:24:03 -0800694 status_t setAllowedDisplayConfigs(const sp<IBinder>& /*displayToken*/,
695 const std::vector<int32_t>& /*allowedConfigs*/) override {
696 return NO_ERROR;
697 }
Ady Abrahamd9b3ea62019-02-26 14:08:03 -0800698 status_t getAllowedDisplayConfigs(const sp<IBinder>& /*displayToken*/,
699 std::vector<int32_t>* /*outAllowedConfigs*/) override {
700 return NO_ERROR;
701 }
Dan Stoza84ab9372018-12-17 15:27:57 -0800702
Brian Anderson3da8d272016-07-28 16:20:47 -0700703protected:
704 IBinder* onAsBinder() override { return nullptr; }
705
706private:
707 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700708};
709
710class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
711public:
Chih-Hung Hsiehaaf62162018-12-20 15:45:04 -0800712 explicit FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap) : mFenceMap(fenceMap) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700713
714 ~FakeProducerFrameEventHistory() {}
715
716 void updateAcquireFence(uint64_t frameNumber,
717 std::shared_ptr<FenceTime>&& acquire) override {
718 // Verify the acquire fence being added isn't the one from the consumer.
719 EXPECT_NE(mConsumerAcquireFence, acquire);
720 // Override the fence, so we can verify this was called by the
721 // producer after the frame is queued.
722 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
723 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
724 }
725
726 void setAcquireFenceOverride(
727 const std::shared_ptr<FenceTime>& acquireFenceOverride,
728 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
729 mAcquireFenceOverride = acquireFenceOverride;
730 mConsumerAcquireFence = consumerAcquireFence;
731 }
732
733protected:
734 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
735 const override {
736 return mFenceMap->createFenceTimeForTest(fence);
737 }
738
739 FenceToFenceTimeMap* mFenceMap{nullptr};
740
741 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
742 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
743};
744
745
746class TestSurface : public Surface {
747public:
748 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
749 FenceToFenceTimeMap* fenceMap)
750 : Surface(bufferProducer),
751 mFakeSurfaceComposer(new FakeSurfaceComposer) {
752 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
753 mFrameEventHistory.reset(mFakeFrameEventHistory);
754 }
755
756 ~TestSurface() override {}
757
758 sp<ISurfaceComposer> composerService() const override {
759 return mFakeSurfaceComposer;
760 }
761
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800762 nsecs_t now() const override {
763 return mNow;
764 }
765
766 void setNow(nsecs_t now) {
767 mNow = now;
768 }
769
Brian Anderson3da8d272016-07-28 16:20:47 -0700770public:
771 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800772 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700773
774 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
775 // but this raw pointer gives access to test functionality.
776 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
777};
778
779
Brian Andersond0010582017-03-07 13:20:31 -0800780class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700781protected:
782 struct FenceAndFenceTime {
783 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
784 : mFence(new Fence),
785 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
786 sp<Fence> mFence { nullptr };
787 std::shared_ptr<FenceTime> mFenceTime { nullptr };
788 };
789
790 struct RefreshEvents {
791 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800792 : mFenceMap(fenceMap),
793 kCompositorTiming(
794 {refreshStart, refreshStart + 1, refreshStart + 2 }),
795 kStartTime(refreshStart + 3),
796 kGpuCompositionDoneTime(refreshStart + 4),
797 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700798
799 void signalPostCompositeFences() {
800 mFenceMap.signalAllForTest(
801 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
802 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
803 }
804
805 FenceToFenceTimeMap& mFenceMap;
806
807 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
808 FenceAndFenceTime mPresent { mFenceMap };
809
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800810 const CompositorTiming kCompositorTiming;
811
Brian Anderson3da8d272016-07-28 16:20:47 -0700812 const nsecs_t kStartTime;
813 const nsecs_t kGpuCompositionDoneTime;
814 const nsecs_t kPresentTime;
815 };
816
817 struct FrameEvents {
818 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
819 : mFenceMap(fenceMap),
820 kPostedTime(frameStartTime + 100),
821 kRequestedPresentTime(frameStartTime + 200),
822 kProducerAcquireTime(frameStartTime + 300),
823 kConsumerAcquireTime(frameStartTime + 301),
824 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700825 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700826 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700827 mRefreshes {
828 { mFenceMap, frameStartTime + 410 },
829 { mFenceMap, frameStartTime + 420 },
830 { mFenceMap, frameStartTime + 430 } } {}
831
832 void signalQueueFences() {
833 mFenceMap.signalAllForTest(
834 mAcquireConsumer.mFence, kConsumerAcquireTime);
835 mFenceMap.signalAllForTest(
836 mAcquireProducer.mFence, kProducerAcquireTime);
837 }
838
839 void signalRefreshFences() {
840 for (auto& re : mRefreshes) {
841 re.signalPostCompositeFences();
842 }
843 }
844
845 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700846 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
847 }
848
849 FenceToFenceTimeMap& mFenceMap;
850
851 FenceAndFenceTime mAcquireConsumer { mFenceMap };
852 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700853 FenceAndFenceTime mRelease { mFenceMap };
854
855 const nsecs_t kPostedTime;
856 const nsecs_t kRequestedPresentTime;
857 const nsecs_t kProducerAcquireTime;
858 const nsecs_t kConsumerAcquireTime;
859 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700860 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700861 const nsecs_t kReleaseTime;
862
863 RefreshEvents mRefreshes[3];
864 };
865
Brian Andersond0010582017-03-07 13:20:31 -0800866 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700867
868 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700869 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
870 mFakeConsumer = new FakeConsumer;
871 mCfeh = &mFakeConsumer->mFrameEventHistory;
872 mConsumer->consumerConnect(mFakeConsumer, false);
873 mConsumer->setConsumerName(String8("TestConsumer"));
874 mSurface = new TestSurface(mProducer, &mFenceMap);
875 mWindow = mSurface;
876
877 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
878 NATIVE_WINDOW_API_CPU));
879 native_window_set_buffer_count(mWindow.get(), 4);
880 }
881
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800882 void disableFrameTimestamps() {
883 mFakeConsumer->mGetFrameTimestampsEnabled = false;
884 native_window_enable_frame_timestamps(mWindow.get(), 0);
885 mFrameTimestampsEnabled = false;
886 }
887
Brian Anderson3da8d272016-07-28 16:20:47 -0700888 void enableFrameTimestamps() {
889 mFakeConsumer->mGetFrameTimestampsEnabled = true;
890 native_window_enable_frame_timestamps(mWindow.get(), 1);
891 mFrameTimestampsEnabled = true;
892 }
893
Brian Anderson1049d1d2016-12-16 17:25:57 -0800894 int getAllFrameTimestamps(uint64_t frameId) {
895 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700896 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
897 &outFirstRefreshStartTime, &outLastRefreshStartTime,
898 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700899 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700900 }
901
Brian Anderson3da8d272016-07-28 16:20:47 -0700902 void resetTimestamps() {
903 outRequestedPresentTime = -1;
904 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700905 outLatchTime = -1;
906 outFirstRefreshStartTime = -1;
907 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700908 outGpuCompositionDoneTime = -1;
909 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700910 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700911 outReleaseTime = -1;
912 }
913
Brian Anderson1049d1d2016-12-16 17:25:57 -0800914 uint64_t getNextFrameId() {
915 uint64_t frameId = -1;
916 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
917 EXPECT_EQ(status, NO_ERROR);
918 return frameId;
919 }
920
Brian Anderson3da8d272016-07-28 16:20:47 -0700921 void dequeueAndQueue(uint64_t frameIndex) {
922 int fence = -1;
923 ANativeWindowBuffer* buffer = nullptr;
924 ASSERT_EQ(NO_ERROR,
925 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
926
927 int oldAddFrameTimestampsCount =
928 mFakeConsumer->mAddFrameTimestampsCount;
929
930 FrameEvents* frame = &mFrames[frameIndex];
931 uint64_t frameNumber = frameIndex + 1;
932
933 NewFrameEventsEntry fe;
934 fe.frameNumber = frameNumber;
935 fe.postedTime = frame->kPostedTime;
936 fe.requestedPresentTime = frame->kRequestedPresentTime;
937 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
938 mFakeConsumer->mNewFrameEntryOverride = fe;
939
940 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
941 frame->mAcquireProducer.mFenceTime,
942 frame->mAcquireConsumer.mFenceTime);
943
944 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
945
946 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
947
948 EXPECT_EQ(
949 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
950 mFakeConsumer->mAddFrameTimestampsCount);
951 }
952
953 void addFrameEvents(
954 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
955 FrameEvents* oldFrame =
956 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
957 FrameEvents* newFrame = &mFrames[iNewFrame];
958
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800959 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700960 uint64_t nNewFrame = iNewFrame + 1;
961
Brian Anderson4e606e32017-03-16 15:34:57 -0700962 // Latch, Composite, and Release the frames in a plausible order.
963 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700964 // that's okay for the purposes of this test.
965 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
966
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700967 // Composite the previous frame one more time, which helps verify
968 // LastRefresh is updated properly.
969 if (oldFrame != nullptr) {
970 mCfeh->addPreComposition(nOldFrame,
971 oldFrame->mRefreshes[2].kStartTime);
972 gpuDoneFenceTime = gpuComposited ?
973 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
974 FenceTime::NO_FENCE;
975 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800976 oldFrame->mRefreshes[2].mPresent.mFenceTime,
977 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700978 }
979
980 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700981 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
982
983 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
984 gpuDoneFenceTime = gpuComposited ?
985 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
986 FenceTime::NO_FENCE;
987 // HWC2 releases the previous buffer after a new latch just before
988 // calling postComposition.
989 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700990 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700991 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
992 }
993 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800994 newFrame->mRefreshes[0].mPresent.mFenceTime,
995 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700996
Brian Anderson3da8d272016-07-28 16:20:47 -0700997 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
998 gpuDoneFenceTime = gpuComposited ?
999 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
1000 FenceTime::NO_FENCE;
1001 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001002 newFrame->mRefreshes[1].mPresent.mFenceTime,
1003 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -07001004 }
1005
Brian Anderson3da8d272016-07-28 16:20:47 -07001006 sp<IGraphicBufferProducer> mProducer;
1007 sp<IGraphicBufferConsumer> mConsumer;
1008 sp<FakeConsumer> mFakeConsumer;
1009 ConsumerFrameEventHistory* mCfeh;
1010 sp<TestSurface> mSurface;
1011 sp<ANativeWindow> mWindow;
1012
1013 FenceToFenceTimeMap mFenceMap;
1014
1015 bool mFrameTimestampsEnabled = false;
1016
1017 int64_t outRequestedPresentTime = -1;
1018 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001019 int64_t outLatchTime = -1;
1020 int64_t outFirstRefreshStartTime = -1;
1021 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001022 int64_t outGpuCompositionDoneTime = -1;
1023 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001024 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001025 int64_t outReleaseTime = -1;
1026
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001027 FrameEvents mFrames[3] {
1028 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -07001029};
1030
1031
1032// This test verifies that the frame timestamps are not retrieved when not
1033// explicitly enabled via native_window_enable_frame_timestamps.
1034// We want to check this to make sure there's no overhead for users
1035// that don't need the timestamp information.
1036TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
1037 int fence;
1038 ANativeWindowBuffer* buffer;
1039
1040 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1041 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1042
Brian Anderson1049d1d2016-12-16 17:25:57 -08001043 const uint64_t fId = getNextFrameId();
1044
Brian Anderson3da8d272016-07-28 16:20:47 -07001045 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
1046 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1047 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1048 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1049
1050 // Verify the producer doesn't get frame timestamps piggybacked on queue.
1051 // It is okay that frame timestamps are added in the consumer since it is
1052 // still needed for SurfaceFlinger dumps.
1053 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1054 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1055 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1056
1057 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001058 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -07001059 EXPECT_EQ(INVALID_OPERATION, result);
1060 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001061
1062 // Verify compositor timing query fails.
1063 nsecs_t compositeDeadline = 0;
1064 nsecs_t compositeInterval = 0;
1065 nsecs_t compositeToPresentLatency = 0;
1066 result = native_window_get_compositor_timing(mWindow.get(),
1067 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1068 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001069}
1070
1071// This test verifies that the frame timestamps are retrieved if explicitly
1072// enabled via native_window_enable_frame_timestamps.
1073TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001074 CompositorTiming initialCompositorTiming {
1075 1000000000, // 1s deadline
1076 16666667, // 16ms interval
1077 50000000, // 50ms present latency
1078 };
1079 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1080
Brian Anderson3da8d272016-07-28 16:20:47 -07001081 enableFrameTimestamps();
1082
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001083 // Verify the compositor timing query gets the initial compositor values
1084 // after timststamps are enabled; even before the first frame is queued
1085 // or dequeued.
1086 nsecs_t compositeDeadline = 0;
1087 nsecs_t compositeInterval = 0;
1088 nsecs_t compositeToPresentLatency = 0;
1089 mSurface->setNow(initialCompositorTiming.deadline - 1);
1090 int result = native_window_get_compositor_timing(mWindow.get(),
1091 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1092 EXPECT_EQ(NO_ERROR, result);
1093 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1094 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1095 EXPECT_EQ(initialCompositorTiming.presentLatency,
1096 compositeToPresentLatency);
1097
Brian Anderson3da8d272016-07-28 16:20:47 -07001098 int fence;
1099 ANativeWindowBuffer* buffer;
1100
1101 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001102 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001103
Brian Anderson1049d1d2016-12-16 17:25:57 -08001104 const uint64_t fId1 = getNextFrameId();
1105
Brian Anderson3da8d272016-07-28 16:20:47 -07001106 // Verify getFrameTimestamps is piggybacked on dequeue.
1107 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1108 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001109 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001110
1111 NewFrameEventsEntry f1;
1112 f1.frameNumber = 1;
1113 f1.postedTime = mFrames[0].kPostedTime;
1114 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1115 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1116 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1117 mFrames[0].mAcquireProducer.mFenceTime,
1118 mFrames[0].mAcquireConsumer.mFenceTime);
1119 mFakeConsumer->mNewFrameEntryOverride = f1;
1120 mFrames[0].signalQueueFences();
1121
1122 // Verify getFrameTimestamps is piggybacked on queue.
1123 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1124 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1125 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001126 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001127
1128 // Verify queries for timestamps that the producer doesn't know about
1129 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001130 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001131 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001132 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001133}
1134
Brian Anderson6b376712017-04-04 10:51:39 -07001135TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1136 bool displayPresentSupported = true;
1137 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1138
1139 // Verify supported bits are forwarded.
1140 int supportsPresent = -1;
1141 mWindow.get()->query(mWindow.get(),
1142 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1143 EXPECT_EQ(displayPresentSupported, supportsPresent);
1144}
1145
1146TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1147 bool displayPresentSupported = false;
1148 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1149
1150 // Verify supported bits are forwarded.
1151 int supportsPresent = -1;
1152 mWindow.get()->query(mWindow.get(),
1153 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1154 EXPECT_EQ(displayPresentSupported, supportsPresent);
1155}
1156
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001157TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1158 nsecs_t phase = 4000;
1159 nsecs_t interval = 1000;
1160
1161 // Timestamp in previous interval.
1162 nsecs_t timestamp = 3500;
1163 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1164 timestamp, phase, interval));
1165
1166 // Timestamp in next interval.
1167 timestamp = 4500;
1168 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1169 timestamp, phase, interval));
1170
1171 // Timestamp multiple intervals before.
1172 timestamp = 2500;
1173 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1174 timestamp, phase, interval));
1175
1176 // Timestamp multiple intervals after.
1177 timestamp = 6500;
1178 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1179 timestamp, phase, interval));
1180
1181 // Timestamp on previous interval.
1182 timestamp = 3000;
1183 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1184 timestamp, phase, interval));
1185
1186 // Timestamp on next interval.
1187 timestamp = 5000;
1188 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1189 timestamp, phase, interval));
1190
1191 // Timestamp equal to phase.
1192 timestamp = 4000;
1193 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1194 timestamp, phase, interval));
1195}
1196
1197// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1198// if the number of intervals elapsed is internally stored in an int.
1199TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1200 nsecs_t phase = 0;
1201 nsecs_t interval = 4000;
1202 nsecs_t big_timestamp = 8635916564000;
1203 int32_t intervals = big_timestamp / interval;
1204
1205 EXPECT_LT(intervals, 0);
1206 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1207 big_timestamp, phase, interval));
1208 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1209 big_timestamp, big_timestamp, interval));
1210}
1211
1212// This verifies the compositor timing is updated by refresh events
1213// and piggy backed on a queue, dequeue, and enabling of timestamps..
1214TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1215 CompositorTiming initialCompositorTiming {
1216 1000000000, // 1s deadline
1217 16666667, // 16ms interval
1218 50000000, // 50ms present latency
1219 };
1220 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1221
1222 enableFrameTimestamps();
1223
1224 // We get the initial values before any frames are submitted.
1225 nsecs_t compositeDeadline = 0;
1226 nsecs_t compositeInterval = 0;
1227 nsecs_t compositeToPresentLatency = 0;
1228 mSurface->setNow(initialCompositorTiming.deadline - 1);
1229 int 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(0);
1238 addFrameEvents(true, NO_FRAME_INDEX, 0);
1239
1240 // Still get the initial values because the frame events for frame 0
1241 // didn't get a chance to piggyback on a queue or dequeue yet.
1242 result = native_window_get_compositor_timing(mWindow.get(),
1243 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1244 EXPECT_EQ(NO_ERROR, result);
1245 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1246 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1247 EXPECT_EQ(initialCompositorTiming.presentLatency,
1248 compositeToPresentLatency);
1249
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001250 dequeueAndQueue(1);
1251 addFrameEvents(true, 0, 1);
1252
1253 // Now expect the composite values associated with frame 1.
1254 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1255 result = native_window_get_compositor_timing(mWindow.get(),
1256 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1257 EXPECT_EQ(NO_ERROR, result);
1258 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1259 compositeDeadline);
1260 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1261 compositeInterval);
1262 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1263 compositeToPresentLatency);
1264
1265 dequeueAndQueue(2);
1266 addFrameEvents(true, 1, 2);
1267
1268 // Now expect the composite values associated with frame 2.
1269 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1270 result = native_window_get_compositor_timing(mWindow.get(),
1271 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1272 EXPECT_EQ(NO_ERROR, result);
1273 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1274 compositeDeadline);
1275 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1276 compositeInterval);
1277 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1278 compositeToPresentLatency);
1279
1280 // Re-enabling frame timestamps should get the latest values.
1281 disableFrameTimestamps();
1282 enableFrameTimestamps();
1283
1284 // Now expect the composite values associated with frame 3.
1285 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1286 result = native_window_get_compositor_timing(mWindow.get(),
1287 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1288 EXPECT_EQ(NO_ERROR, result);
1289 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1290 compositeDeadline);
1291 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1292 compositeInterval);
1293 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1294 compositeToPresentLatency);
1295}
1296
1297// This verifies the compositor deadline properly snaps to the the next
1298// deadline based on the current time.
1299TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1300 CompositorTiming initialCompositorTiming {
1301 1000000000, // 1s deadline
1302 16666667, // 16ms interval
1303 50000000, // 50ms present latency
1304 };
1305 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1306
1307 enableFrameTimestamps();
1308
1309 nsecs_t compositeDeadline = 0;
1310 nsecs_t compositeInterval = 0;
1311 nsecs_t compositeToPresentLatency = 0;
1312
1313 // A "now" just before the deadline snaps to the deadline.
1314 mSurface->setNow(initialCompositorTiming.deadline - 1);
1315 int result = native_window_get_compositor_timing(mWindow.get(),
1316 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1317 EXPECT_EQ(NO_ERROR, result);
1318 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1319 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1320 EXPECT_EQ(expectedDeadline, compositeDeadline);
1321
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001322 dequeueAndQueue(0);
1323 addFrameEvents(true, NO_FRAME_INDEX, 0);
1324
1325 // A "now" just after the deadline snaps properly.
1326 mSurface->setNow(initialCompositorTiming.deadline + 1);
1327 result = native_window_get_compositor_timing(mWindow.get(),
1328 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1329 EXPECT_EQ(NO_ERROR, result);
1330 expectedDeadline =
1331 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1332 EXPECT_EQ(expectedDeadline, compositeDeadline);
1333
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001334 dequeueAndQueue(1);
1335 addFrameEvents(true, 0, 1);
1336
1337 // A "now" just after the next interval snaps properly.
1338 mSurface->setNow(
1339 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1340 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1341 result = native_window_get_compositor_timing(mWindow.get(),
1342 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1343 EXPECT_EQ(NO_ERROR, result);
1344 expectedDeadline =
1345 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1346 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1347 EXPECT_EQ(expectedDeadline, compositeDeadline);
1348
1349 dequeueAndQueue(2);
1350 addFrameEvents(true, 1, 2);
1351
1352 // A "now" over 1 interval before the deadline snaps properly.
1353 mSurface->setNow(
1354 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1355 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1356 result = native_window_get_compositor_timing(mWindow.get(),
1357 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1358 EXPECT_EQ(NO_ERROR, result);
1359 expectedDeadline =
1360 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1361 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1362 EXPECT_EQ(expectedDeadline, compositeDeadline);
1363
1364 // Re-enabling frame timestamps should get the latest values.
1365 disableFrameTimestamps();
1366 enableFrameTimestamps();
1367
1368 // A "now" over 2 intervals before the deadline snaps properly.
1369 mSurface->setNow(
1370 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1371 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1372 result = native_window_get_compositor_timing(mWindow.get(),
1373 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1374 EXPECT_EQ(NO_ERROR, result);
1375 expectedDeadline =
1376 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1377 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1378 EXPECT_EQ(expectedDeadline, compositeDeadline);
1379}
1380
Brian Anderson1049d1d2016-12-16 17:25:57 -08001381// This verifies the timestamps recorded in the consumer's
1382// FrameTimestampsHistory are properly retrieved by the producer for the
1383// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001384TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1385 enableFrameTimestamps();
1386
Brian Anderson1049d1d2016-12-16 17:25:57 -08001387 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001388 dequeueAndQueue(0);
1389 mFrames[0].signalQueueFences();
1390
Brian Anderson1049d1d2016-12-16 17:25:57 -08001391 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001392 dequeueAndQueue(1);
1393 mFrames[1].signalQueueFences();
1394
1395 addFrameEvents(true, NO_FRAME_INDEX, 0);
1396 mFrames[0].signalRefreshFences();
1397 addFrameEvents(true, 0, 1);
1398 mFrames[0].signalReleaseFences();
1399 mFrames[1].signalRefreshFences();
1400
1401 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001402 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001403 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001404 EXPECT_EQ(NO_ERROR, result);
1405 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1406 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001407 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1408 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1409 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001410 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1411 outGpuCompositionDoneTime);
1412 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001413 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001414 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1415
1416 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001417 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001418 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001419 EXPECT_EQ(NO_ERROR, result);
1420 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1421 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001422 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1423 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1424 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001425 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1426 outGpuCompositionDoneTime);
1427 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001428 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1429 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001430}
1431
1432// This test verifies the acquire fence recorded by the consumer is not sent
1433// back to the producer and the producer saves its own fence.
1434TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1435 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001436
Brian Anderson3da8d272016-07-28 16:20:47 -07001437 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001438 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001439 dequeueAndQueue(0);
1440
1441 // Verify queue-related timestamps for f1 are available immediately in the
1442 // producer without asking the consumer again, even before signaling the
1443 // acquire fence.
1444 resetTimestamps();
1445 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001446 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001447 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001448 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001449 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1450 EXPECT_EQ(NO_ERROR, result);
1451 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001452 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001453
1454 // Signal acquire fences. Verify a sync call still isn't necessary.
1455 mFrames[0].signalQueueFences();
1456
1457 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001458 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001459 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001460 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001461 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1462 EXPECT_EQ(NO_ERROR, result);
1463 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1464 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1465
1466 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001467 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001468 dequeueAndQueue(1);
1469
1470 // Verify queue-related timestamps for f2 are available immediately in the
1471 // producer without asking the consumer again, even before signaling the
1472 // acquire fence.
1473 resetTimestamps();
1474 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001475 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001476 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001477 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001478 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1479 EXPECT_EQ(NO_ERROR, result);
1480 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001481 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001482
1483 // Signal acquire fences. Verify a sync call still isn't necessary.
1484 mFrames[1].signalQueueFences();
1485
1486 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001487 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001488 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001489 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001490 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1491 EXPECT_EQ(NO_ERROR, result);
1492 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1493 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1494}
1495
1496TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1497 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001498
1499 // Dequeue and queue frame 1.
1500 dequeueAndQueue(0);
1501 mFrames[0].signalQueueFences();
1502
1503 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001504 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001505 dequeueAndQueue(1);
1506 mFrames[1].signalQueueFences();
1507
1508 addFrameEvents(true, NO_FRAME_INDEX, 0);
1509 mFrames[0].signalRefreshFences();
1510 addFrameEvents(true, 0, 1);
1511 mFrames[0].signalReleaseFences();
1512 mFrames[1].signalRefreshFences();
1513
1514 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001515 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001516 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001517 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1518 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001519 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001520 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1521}
1522
1523// This test verifies that fences can signal and update timestamps producer
1524// side without an additional sync call to the consumer.
1525TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1526 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001527
1528 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001529 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001530 dequeueAndQueue(0);
1531 mFrames[0].signalQueueFences();
1532
1533 // Dequeue and queue frame 2.
1534 dequeueAndQueue(1);
1535 mFrames[1].signalQueueFences();
1536
1537 addFrameEvents(true, NO_FRAME_INDEX, 0);
1538 addFrameEvents(true, 0, 1);
1539
1540 // Verify available timestamps are correct for frame 1, before any
1541 // fence has been signaled.
1542 // Note: A sync call is necessary here since the events triggered by
1543 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001544 resetTimestamps();
1545 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001546 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001547 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1548 EXPECT_EQ(NO_ERROR, result);
1549 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1550 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001551 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1552 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1553 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001554 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1555 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001556 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001557 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001558
1559 // Verify available timestamps are correct for frame 1 again, before any
1560 // fence has been signaled.
1561 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001562 resetTimestamps();
1563 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001564 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001565 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1566 EXPECT_EQ(NO_ERROR, result);
1567 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1568 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001569 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1570 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1571 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001572 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1573 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001574 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001575 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001576
1577 // Signal the fences for frame 1.
1578 mFrames[0].signalRefreshFences();
1579 mFrames[0].signalReleaseFences();
1580
1581 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001582 resetTimestamps();
1583 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001584 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001585 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1586 EXPECT_EQ(NO_ERROR, result);
1587 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1588 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001589 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1590 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1591 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001592 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1593 outGpuCompositionDoneTime);
1594 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001595 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001596 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1597}
1598
1599// This test verifies that if the frame wasn't GPU composited but has a refresh
1600// event a sync call isn't made to get the GPU composite done time since it will
1601// never exist.
1602TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1603 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001604
Brian Anderson3da8d272016-07-28 16:20:47 -07001605 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001606 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001607 dequeueAndQueue(0);
1608 mFrames[0].signalQueueFences();
1609
1610 // Dequeue and queue frame 2.
1611 dequeueAndQueue(1);
1612 mFrames[1].signalQueueFences();
1613
1614 addFrameEvents(false, NO_FRAME_INDEX, 0);
1615 addFrameEvents(false, 0, 1);
1616
1617 // Verify available timestamps are correct for frame 1, before any
1618 // fence has been signaled.
1619 // Note: A sync call is necessary here since the events triggered by
1620 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1621 resetTimestamps();
1622 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001623 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001624 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1625 EXPECT_EQ(NO_ERROR, result);
1626 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1627 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001628 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1629 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1630 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001631 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1632 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001633 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001634 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001635
1636 // Signal the fences for frame 1.
1637 mFrames[0].signalRefreshFences();
1638 mFrames[0].signalReleaseFences();
1639
1640 // Verify all timestamps, except GPU composition, are available without a
1641 // sync call.
1642 resetTimestamps();
1643 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001644 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001645 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1646 EXPECT_EQ(NO_ERROR, result);
1647 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1648 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001649 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1650 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1651 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001652 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001653 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001654 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001655 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1656}
1657
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001658// This test verifies that if the certain timestamps can't possibly exist for
1659// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001660TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001661 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001662
1663 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001664 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001665 dequeueAndQueue(0);
1666 mFrames[0].signalQueueFences();
1667
1668 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001669 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001670 dequeueAndQueue(1);
1671 mFrames[1].signalQueueFences();
1672
1673 addFrameEvents(false, NO_FRAME_INDEX, 0);
1674 addFrameEvents(false, 0, 1);
1675
1676 // Verify available timestamps are correct for frame 1, before any
1677 // fence has been signaled.
1678 // Note: A sync call is necessary here since the events triggered by
1679 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001680 resetTimestamps();
1681 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001682 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001683 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1684 EXPECT_EQ(NO_ERROR, result);
1685 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1686 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001687 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1688 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1689 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001690 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1691 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001692 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001693 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001694
1695 mFrames[0].signalRefreshFences();
1696 mFrames[0].signalReleaseFences();
1697 mFrames[1].signalRefreshFences();
1698
Brian Anderson1049d1d2016-12-16 17:25:57 -08001699 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001700 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001701 // available, a sync call should not occur because it's not possible for f2
1702 // to encounter the final value for those events until another frame is
1703 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001704 resetTimestamps();
1705 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001706 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001707 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1708 EXPECT_EQ(NO_ERROR, result);
1709 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1710 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001711 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1712 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1713 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001714 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001715 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001716 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1717 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001718}
1719
Brian Anderson6b376712017-04-04 10:51:39 -07001720// This test verifies there are no sync calls for present times
1721// when they aren't supported and that an error is returned.
1722
1723TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1724 enableFrameTimestamps();
1725 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1726
1727 // Dequeue and queue frame 1.
1728 const uint64_t fId1 = getNextFrameId();
1729 dequeueAndQueue(0);
1730
1731 // Verify a query for the Present times do not trigger a sync call if they
1732 // are not supported.
1733 resetTimestamps();
1734 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1735 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1736 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1737 &outDisplayPresentTime, nullptr, nullptr);
1738 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1739 EXPECT_EQ(BAD_VALUE, result);
1740 EXPECT_EQ(-1, outDisplayPresentTime);
1741}
1742
Dan Stoza932f0082017-05-31 13:50:16 -07001743} // namespace android