blob: cb1756f15bacd9ac0909192b591f9d6c267d6c19 [file] [log] [blame]
Jamie Gennis134f0422011-03-08 12:18:54 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stozac6f30bd2015-06-08 09:32:50 -070017#include "DummyConsumer.h"
18
Jamie Gennis134f0422011-03-08 12:18:54 -080019#include <gtest/gtest.h>
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080020
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060021#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070022#include <binder/ProcessState.h>
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060023#include <configstore/Utils.h>
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -070024#include <cutils/properties.h>
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -080025#include <inttypes.h>
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -070026#include <gui/BufferItemConsumer.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070027#include <gui/IDisplayEventConnection.h>
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -070028#include <gui/IProducerListener.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080029#include <gui/ISurfaceComposer.h>
30#include <gui/Surface.h>
31#include <gui/SurfaceComposerClient.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070032#include <private/gui/ComposerService.h>
Dan Stozac1879002014-05-22 15:59:05 -070033#include <ui/Rect.h>
Jamie Gennis134f0422011-03-08 12:18:54 -080034#include <utils/String8.h>
35
Brian Anderson3da8d272016-07-28 16:20:47 -070036#include <limits>
Kalle Raita643f0942016-12-07 09:20:14 -080037#include <thread>
38
Jamie Gennis134f0422011-03-08 12:18:54 -080039namespace android {
40
Kalle Raita643f0942016-12-07 09:20:14 -080041using namespace std::chrono_literals;
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060042// retrieve wide-color and hdr settings from configstore
43using namespace android::hardware::configstore;
44using namespace android::hardware::configstore::V1_0;
Peiyong Lin9f034472018-03-28 15:29:00 -070045using ui::ColorMode;
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060046
Robert Carr4cdc58f2017-08-23 14:22:20 -070047using Transaction = SurfaceComposerClient::Transaction;
48
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060049static bool hasWideColorDisplay =
50 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
Kalle Raita643f0942016-12-07 09:20:14 -080051
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -070052static bool hasHdrDisplay =
53 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
54
Brian Anderson3da8d272016-07-28 16:20:47 -070055class FakeSurfaceComposer;
56class FakeProducerFrameEventHistory;
57
58static constexpr uint64_t NO_FRAME_INDEX = std::numeric_limits<uint64_t>::max();
59
Jamie Gennis134f0422011-03-08 12:18:54 -080060class SurfaceTest : public ::testing::Test {
61protected:
Mathias Agopian7c1a4872013-03-20 15:56:04 -070062 SurfaceTest() {
63 ProcessState::self()->startThreadPool();
64 }
65
Jamie Gennis134f0422011-03-08 12:18:54 -080066 virtual void SetUp() {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080067 mComposerClient = new SurfaceComposerClient;
Jamie Gennis134f0422011-03-08 12:18:54 -080068 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
69
Brian Andersond0010582017-03-07 13:20:31 -080070 // TODO(brianderson): The following sometimes fails and is a source of
71 // test flakiness.
Jamie Gennisfc850122011-04-25 16:40:05 -070072 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070073 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis134f0422011-03-08 12:18:54 -080074
Yi Konga03e0442018-07-17 11:16:57 -070075 ASSERT_TRUE(mSurfaceControl != nullptr);
Jamie Gennis134f0422011-03-08 12:18:54 -080076 ASSERT_TRUE(mSurfaceControl->isValid());
77
Robert Carr4cdc58f2017-08-23 14:22:20 -070078 Transaction t;
79 ASSERT_EQ(NO_ERROR, t.setLayer(mSurfaceControl, 0x7fffffff)
80 .show(mSurfaceControl)
81 .apply());
Jamie Gennis134f0422011-03-08 12:18:54 -080082
83 mSurface = mSurfaceControl->getSurface();
Yi Konga03e0442018-07-17 11:16:57 -070084 ASSERT_TRUE(mSurface != nullptr);
Jamie Gennis134f0422011-03-08 12:18:54 -080085 }
86
87 virtual void TearDown() {
88 mComposerClient->dispose();
89 }
90
91 sp<Surface> mSurface;
92 sp<SurfaceComposerClient> mComposerClient;
93 sp<SurfaceControl> mSurfaceControl;
94};
95
Robert Carr740eaf02018-03-27 12:59:18 -070096TEST_F(SurfaceTest, CreateSurfaceReturnsErrorBadClient) {
97 mComposerClient->dispose();
98 ASSERT_EQ(NO_INIT, mComposerClient->initCheck());
99
100 sp<SurfaceControl> sc;
101 status_t err = mComposerClient->createSurfaceChecked(
102 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, &sc, 0);
103 ASSERT_EQ(NO_INIT, err);
104}
105
Jamie Gennis134f0422011-03-08 12:18:54 -0800106TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
107 sp<ANativeWindow> anw(mSurface);
108 int result = -123;
109 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
110 &result);
111 EXPECT_EQ(NO_ERROR, err);
112 EXPECT_EQ(1, result);
113}
114
115TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
116 mSurfaceControl.clear();
Kalle Raita643f0942016-12-07 09:20:14 -0800117 // Wait for the async clean-up to complete.
118 std::this_thread::sleep_for(50ms);
Jamie Gennis134f0422011-03-08 12:18:54 -0800119
120 sp<ANativeWindow> anw(mSurface);
121 int result = -123;
122 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
123 &result);
124 EXPECT_EQ(NO_ERROR, err);
125 EXPECT_EQ(1, result);
126}
127
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800128// This test probably doesn't belong here.
Jamie Gennisc901ca02011-10-11 16:02:31 -0700129TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800130 sp<ANativeWindow> anw(mSurface);
131
132 // Verify the screenshot works with no protected buffers.
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800133 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Brian Anderson3da8d272016-07-28 16:20:47 -0700134 sp<IBinder> display(sf->getBuiltInDisplay(
135 ISurfaceComposer::eDisplayIdMain));
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000136 sp<GraphicBuffer> outBuffer;
Peiyong Lin0e003c92018-09-17 11:09:51 -0700137 ASSERT_EQ(NO_ERROR,
138 sf->captureScreen(display, &outBuffer, ui::Dataspace::V0_SRGB,
139 ui::PixelFormat::RGBA_8888, Rect(), 64, 64, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800140
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700141 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
142 NATIVE_WINDOW_API_CPU));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800143 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
144 // that we need to dequeue a buffer in order for it to actually get
145 // allocated in SurfaceFlinger.
146 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
147 GRALLOC_USAGE_PROTECTED));
148 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Yi Konga03e0442018-07-17 11:16:57 -0700149 ANativeWindowBuffer* buf = nullptr;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700150
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700151 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700152 if (err) {
153 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
154 // that's okay as long as this is the reason for the failure.
155 // try again without the GRALLOC_USAGE_PROTECTED bit.
156 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700157 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
158 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700159 return;
160 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700161 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700162
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800163 for (int i = 0; i < 4; i++) {
164 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700165 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
166 &buf));
167 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800168 }
Peiyong Lin0e003c92018-09-17 11:09:51 -0700169 ASSERT_EQ(NO_ERROR,
170 sf->captureScreen(display, &outBuffer, ui::Dataspace::V0_SRGB,
171 ui::PixelFormat::RGBA_8888, Rect(), 64, 64, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800172}
173
Jamie Gennis391bbe22011-03-14 15:00:06 -0700174TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
175 sp<ANativeWindow> anw(mSurface);
176 int result = -123;
177 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
178 EXPECT_EQ(NO_ERROR, err);
179 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
180}
181
Craig Donner6ebc46a2016-10-21 15:23:44 -0700182TEST_F(SurfaceTest, LayerCountIsOne) {
183 sp<ANativeWindow> anw(mSurface);
184 int result = -123;
185 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
186 EXPECT_EQ(NO_ERROR, err);
187 EXPECT_EQ(1, result);
188}
189
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700190TEST_F(SurfaceTest, QueryConsumerUsage) {
191 const int TEST_USAGE_FLAGS =
192 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700193 sp<IGraphicBufferProducer> producer;
194 sp<IGraphicBufferConsumer> consumer;
195 BufferQueue::createBufferQueue(&producer, &consumer);
196 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700197 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700198 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700199
200 sp<ANativeWindow> anw(s);
201
202 int flags = -1;
203 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
204
205 ASSERT_EQ(NO_ERROR, err);
206 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
207}
208
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800209TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
210 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
211 sp<IGraphicBufferProducer> producer;
212 sp<IGraphicBufferConsumer> consumer;
213 BufferQueue::createBufferQueue(&producer, &consumer);
214 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
215
216 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
217
218 sp<Surface> s = new Surface(producer);
219
220 sp<ANativeWindow> anw(s);
221
222 android_dataspace dataSpace;
223
224 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
225 reinterpret_cast<int*>(&dataSpace));
226
227 ASSERT_EQ(NO_ERROR, err);
228 ASSERT_EQ(TEST_DATASPACE, dataSpace);
229}
230
Dan Stoza812ed062015-06-02 15:45:22 -0700231TEST_F(SurfaceTest, SettingGenerationNumber) {
232 sp<IGraphicBufferProducer> producer;
233 sp<IGraphicBufferConsumer> consumer;
234 BufferQueue::createBufferQueue(&producer, &consumer);
235 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
236 sp<Surface> surface = new Surface(producer);
237 sp<ANativeWindow> window(surface);
238
239 // Allocate a buffer with a generation number of 0
240 ANativeWindowBuffer* buffer;
241 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700242 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
243 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700244 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
245 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
246
247 // Detach the buffer and check its generation number
248 sp<GraphicBuffer> graphicBuffer;
249 sp<Fence> fence;
250 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
251 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
252
253 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
254 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
255
256 // This should change the generation number of the GraphicBuffer
257 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
258
259 // Check that the new generation number sticks with the buffer
260 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
261 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
262 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
263 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
264}
265
Dan Stozac6f30bd2015-06-08 09:32:50 -0700266TEST_F(SurfaceTest, GetConsumerName) {
267 sp<IGraphicBufferProducer> producer;
268 sp<IGraphicBufferConsumer> consumer;
269 BufferQueue::createBufferQueue(&producer, &consumer);
270
271 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
272 consumer->consumerConnect(dummyConsumer, false);
273 consumer->setConsumerName(String8("TestConsumer"));
274
275 sp<Surface> surface = new Surface(producer);
276 sp<ANativeWindow> window(surface);
277 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
278
279 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
280}
281
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700282TEST_F(SurfaceTest, GetWideColorSupport) {
283 sp<IGraphicBufferProducer> producer;
284 sp<IGraphicBufferConsumer> consumer;
285 BufferQueue::createBufferQueue(&producer, &consumer);
286
287 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
288 consumer->consumerConnect(dummyConsumer, false);
289 consumer->setConsumerName(String8("TestConsumer"));
290
291 sp<Surface> surface = new Surface(producer);
292 sp<ANativeWindow> window(surface);
293 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
294
295 bool supported;
296 surface->getWideColorSupport(&supported);
297
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -0600298 // NOTE: This test assumes that device that supports
299 // wide-color (as indicated by BoardConfig) must also
300 // have a wide-color primary display.
301 // That assumption allows this test to cover devices
302 // that advertised a wide-color color mode without
303 // actually supporting wide-color to pass this test
304 // as well as the case of a device that does support
305 // wide-color (via BoardConfig) and has a wide-color
306 // primary display.
307 // NOT covered at this time is a device that supports
308 // wide color in the BoardConfig but does not support
309 // a wide-color color mode on the primary display.
310 ASSERT_EQ(hasWideColorDisplay, supported);
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700311}
312
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700313TEST_F(SurfaceTest, GetHdrSupport) {
314 sp<IGraphicBufferProducer> producer;
315 sp<IGraphicBufferConsumer> consumer;
316 BufferQueue::createBufferQueue(&producer, &consumer);
317
318 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
319 consumer->consumerConnect(dummyConsumer, false);
320 consumer->setConsumerName(String8("TestConsumer"));
321
322 sp<Surface> surface = new Surface(producer);
323 sp<ANativeWindow> window(surface);
324 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
325
326 bool supported;
327 status_t result = surface->getHdrSupport(&supported);
328 ASSERT_EQ(NO_ERROR, result);
329
330 // NOTE: This is not a CTS test.
331 // This test verifies that when the BoardConfig TARGET_HAS_HDR_DISPLAY
332 // is TRUE, getHdrSupport is also true.
333 // TODO: Add check for an HDR color mode on the primary display.
334 ASSERT_EQ(hasHdrDisplay, supported);
335}
336
337TEST_F(SurfaceTest, SetHdrMetadata) {
338 sp<IGraphicBufferProducer> producer;
339 sp<IGraphicBufferConsumer> consumer;
340 BufferQueue::createBufferQueue(&producer, &consumer);
341
342 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
343 consumer->consumerConnect(dummyConsumer, false);
344 consumer->setConsumerName(String8("TestConsumer"));
345
346 sp<Surface> surface = new Surface(producer);
347 sp<ANativeWindow> window(surface);
348 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
349
350 bool supported;
351 status_t result = surface->getHdrSupport(&supported);
352 ASSERT_EQ(NO_ERROR, result);
353
354 if (!hasHdrDisplay || !supported) {
355 return;
356 }
357 const android_smpte2086_metadata smpte2086 = {
358 {0.680, 0.320},
359 {0.265, 0.690},
360 {0.150, 0.060},
361 {0.3127, 0.3290},
362 100.0,
363 0.1,
364 };
365 const android_cta861_3_metadata cta861_3 = {
366 78.0,
367 62.0,
368 };
Valerie Haua82679d2018-11-21 09:31:43 -0800369
370 std::vector<uint8_t> hdr10plus;
371 hdr10plus.push_back(0xff);
372
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700373 int error = native_window_set_buffers_smpte2086_metadata(window.get(), &smpte2086);
374 ASSERT_EQ(error, NO_ERROR);
375 error = native_window_set_buffers_cta861_3_metadata(window.get(), &cta861_3);
376 ASSERT_EQ(error, NO_ERROR);
Valerie Haua82679d2018-11-21 09:31:43 -0800377 error = native_window_set_buffers_hdr10_plus_metadata(window.get(), hdr10plus.size(),
378 hdr10plus.data());
379 ASSERT_EQ(error, NO_ERROR);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700380}
381
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800382TEST_F(SurfaceTest, DynamicSetBufferCount) {
383 sp<IGraphicBufferProducer> producer;
384 sp<IGraphicBufferConsumer> consumer;
385 BufferQueue::createBufferQueue(&producer, &consumer);
386
387 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
388 consumer->consumerConnect(dummyConsumer, false);
389 consumer->setConsumerName(String8("TestConsumer"));
390
391 sp<Surface> surface = new Surface(producer);
392 sp<ANativeWindow> window(surface);
393
394 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
395 NATIVE_WINDOW_API_CPU));
396 native_window_set_buffer_count(window.get(), 4);
397
398 int fence;
399 ANativeWindowBuffer* buffer;
400 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
401 native_window_set_buffer_count(window.get(), 3);
402 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
403 native_window_set_buffer_count(window.get(), 2);
404 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
405 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
406}
407
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700408TEST_F(SurfaceTest, GetAndFlushRemovedBuffers) {
409 sp<IGraphicBufferProducer> producer;
410 sp<IGraphicBufferConsumer> consumer;
411 BufferQueue::createBufferQueue(&producer, &consumer);
412
413 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
414 consumer->consumerConnect(dummyConsumer, false);
415 consumer->setConsumerName(String8("TestConsumer"));
416
417 sp<Surface> surface = new Surface(producer);
418 sp<ANativeWindow> window(surface);
419 sp<DummyProducerListener> listener = new DummyProducerListener();
420 ASSERT_EQ(OK, surface->connect(
421 NATIVE_WINDOW_API_CPU,
422 /*listener*/listener,
423 /*reportBufferRemoval*/true));
424 const int BUFFER_COUNT = 4;
425 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
426
427 sp<GraphicBuffer> detachedBuffer;
428 sp<Fence> outFence;
429 int fences[BUFFER_COUNT];
430 ANativeWindowBuffer* buffers[BUFFER_COUNT];
431 // Allocate buffers because detachNextBuffer requires allocated buffers
432 for (int i = 0; i < BUFFER_COUNT; i++) {
433 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
434 }
435 for (int i = 0; i < BUFFER_COUNT; i++) {
436 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
437 }
438
439 // Test detached buffer is correctly reported
440 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
441 std::vector<sp<GraphicBuffer>> removedBuffers;
442 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
443 ASSERT_EQ(1u, removedBuffers.size());
444 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
445 // Test the list is flushed one getAndFlushRemovedBuffers returns
446 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
447 ASSERT_EQ(0u, removedBuffers.size());
448
449
450 // Test removed buffer list is cleanup after next dequeueBuffer call
451 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
452 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[0], &fences[0]));
453 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
454 ASSERT_EQ(0u, removedBuffers.size());
455 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[0], fences[0]));
456
457 // Test removed buffer list is cleanup after next detachNextBuffer call
458 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
459 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
460 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
461 ASSERT_EQ(1u, removedBuffers.size());
462 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
463
464 // Re-allocate buffers since all buffers are detached up to now
465 for (int i = 0; i < BUFFER_COUNT; i++) {
466 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
467 }
468 for (int i = 0; i < BUFFER_COUNT; i++) {
469 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
470 }
471
472 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
473 ASSERT_EQ(NO_ERROR, surface->attachBuffer(detachedBuffer.get()));
474 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
475 // Depends on which slot GraphicBufferProducer impl pick, the attach call might
476 // get 0 or 1 buffer removed.
477 ASSERT_LE(removedBuffers.size(), 1u);
478}
Brian Anderson3da8d272016-07-28 16:20:47 -0700479
Dan Stoza932f0082017-05-31 13:50:16 -0700480TEST_F(SurfaceTest, TestGetLastDequeueStartTime) {
481 sp<ANativeWindow> anw(mSurface);
482 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU));
483
484 ANativeWindowBuffer* buffer = nullptr;
485 int32_t fenceFd = -1;
486
487 nsecs_t before = systemTime(CLOCK_MONOTONIC);
488 anw->dequeueBuffer(anw.get(), &buffer, &fenceFd);
489 nsecs_t after = systemTime(CLOCK_MONOTONIC);
490
491 nsecs_t lastDequeueTime = mSurface->getLastDequeueStartTime();
492 ASSERT_LE(before, lastDequeueTime);
493 ASSERT_GE(after, lastDequeueTime);
494}
495
Brian Anderson3da8d272016-07-28 16:20:47 -0700496class FakeConsumer : public BnConsumerListener {
497public:
498 void onFrameAvailable(const BufferItem& /*item*/) override {}
499 void onBuffersReleased() override {}
500 void onSidebandStreamChanged() override {}
501
502 void addAndGetFrameTimestamps(
503 const NewFrameEventsEntry* newTimestamps,
504 FrameEventHistoryDelta* outDelta) override {
505 if (newTimestamps) {
506 if (mGetFrameTimestampsEnabled) {
507 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
508 "Test should set mNewFrameEntryOverride before queuing "
509 "a frame.";
510 EXPECT_EQ(newTimestamps->frameNumber,
511 mNewFrameEntryOverride.frameNumber) <<
512 "Test attempting to add NewFrameEntryOverride with "
513 "incorrect frame number.";
514 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
515 mNewFrameEntryOverride.frameNumber = 0;
516 }
517 mAddFrameTimestampsCount++;
518 mLastAddedFrameNumber = newTimestamps->frameNumber;
519 }
520 if (outDelta) {
521 mFrameEventHistory.getAndResetDelta(outDelta);
522 mGetFrameTimestampsCount++;
523 }
524 mAddAndGetFrameTimestampsCallCount++;
525 }
526
527 bool mGetFrameTimestampsEnabled = false;
528
529 ConsumerFrameEventHistory mFrameEventHistory;
530 int mAddAndGetFrameTimestampsCallCount = 0;
531 int mAddFrameTimestampsCount = 0;
532 int mGetFrameTimestampsCount = 0;
533 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
534
535 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
536};
537
538
539class FakeSurfaceComposer : public ISurfaceComposer{
540public:
541 ~FakeSurfaceComposer() override {}
542
Brian Anderson6b376712017-04-04 10:51:39 -0700543 void setSupportsPresent(bool supportsPresent) {
544 mSupportsPresent = supportsPresent;
545 }
546
Brian Anderson3da8d272016-07-28 16:20:47 -0700547 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800548 sp<ISurfaceComposerClient> createScopedConnection(
549 const sp<IGraphicBufferProducer>& /* parent */) override {
550 return nullptr;
551 }
Jorim Jaggib1e2f8d2017-06-08 15:43:59 -0700552 sp<IDisplayEventConnection> createDisplayEventConnection(ISurfaceComposer::VsyncSource)
553 override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700554 return nullptr;
555 }
556 sp<IBinder> createDisplay(const String8& /*displayName*/,
557 bool /*secure*/) override { return nullptr; }
558 void destroyDisplay(const sp<IBinder>& /*display */) override {}
559 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
560 void setTransactionState(const Vector<ComposerState>& /*state*/,
561 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
562 override {}
563 void bootFinished() override {}
564 bool authenticateSurfaceTexture(
565 const sp<IGraphicBufferProducer>& /*surface*/) const override {
566 return false;
567 }
Brian Anderson6b376712017-04-04 10:51:39 -0700568
569 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
570 const override {
571 *outSupported = {
572 FrameEvent::REQUESTED_PRESENT,
573 FrameEvent::ACQUIRE,
574 FrameEvent::LATCH,
575 FrameEvent::FIRST_REFRESH_START,
576 FrameEvent::LAST_REFRESH_START,
577 FrameEvent::GPU_COMPOSITION_DONE,
578 FrameEvent::DEQUEUE_READY,
579 FrameEvent::RELEASE
580 };
581 if (mSupportsPresent) {
582 outSupported->push_back(
583 FrameEvent::DISPLAY_PRESENT);
584 }
585 return NO_ERROR;
586 }
587
Brian Anderson3da8d272016-07-28 16:20:47 -0700588 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
589 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
590 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
591 status_t getDisplayStats(const sp<IBinder>& /*display*/,
592 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
593 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
594 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
595 override {
596 return NO_ERROR;
597 }
598 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700599 Vector<ColorMode>* /*outColorModes*/) override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700600 return NO_ERROR;
601 }
Peiyong Lina52f0292018-03-14 17:26:31 -0700602 ColorMode getActiveColorMode(const sp<IBinder>& /*display*/)
Brian Anderson3da8d272016-07-28 16:20:47 -0700603 override {
Peiyong Lina52f0292018-03-14 17:26:31 -0700604 return ColorMode::NATIVE;
Brian Anderson3da8d272016-07-28 16:20:47 -0700605 }
606 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700607 ColorMode /*colorMode*/) override { return NO_ERROR; }
Peiyong Lin0e003c92018-09-17 11:09:51 -0700608 status_t captureScreen(const sp<IBinder>& /*display*/, sp<GraphicBuffer>* /*outBuffer*/,
609 const ui::Dataspace /*reqDataspace*/,
610 const ui::PixelFormat /*reqPixelFormat*/, Rect /*sourceCrop*/,
611 uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
612 bool /*useIdentityTransform*/, Rotation /*rotation*/) override {
613 return NO_ERROR;
614 }
chaviwa76b2712017-09-20 12:02:26 -0700615 virtual status_t captureLayers(const sp<IBinder>& /*parentHandle*/,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700616 sp<GraphicBuffer>* /*outBuffer*/,
617 const ui::Dataspace /*reqDataspace*/,
618 const ui::PixelFormat /*reqPixelFormat*/,
619 const Rect& /*sourceCrop*/, float /*frameScale*/,
620 bool /*childrenOnly*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700621 return NO_ERROR;
622 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700623 status_t clearAnimationFrameStats() override { return NO_ERROR; }
624 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
625 return NO_ERROR;
626 }
627 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
628 HdrCapabilities* /*outCapabilities*/) const override {
629 return NO_ERROR;
630 }
631 status_t enableVSyncInjections(bool /*enable*/) override {
632 return NO_ERROR;
633 }
634 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800635 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
636 return NO_ERROR;
637 }
Peiyong Linc6780972018-10-28 15:24:08 -0700638 status_t getCompositionPreference(
639 ui::Dataspace* /*outDefaultDataspace*/, ui::PixelFormat* /*outDefaultPixelFormat*/,
640 ui::Dataspace* /*outWideColorGamutDataspace*/,
641 ui::PixelFormat* /*outWideColorGamutPixelFormat*/) const override {
Peiyong Lin0256f722018-08-31 15:45:10 -0700642 return NO_ERROR;
643 }
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700644 status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& /*display*/,
645 ui::PixelFormat* /*outFormat*/,
646 ui::Dataspace* /*outDataspace*/,
647 uint8_t* /*outComponentMask*/) const override {
648 return NO_ERROR;
649 }
Kevin DuBois74e53772018-11-19 10:52:38 -0800650 status_t setDisplayContentSamplingEnabled(const sp<IBinder>& /*display*/, bool /*enable*/,
651 uint8_t /*componentMask*/,
652 uint64_t /*maxFrames*/) const override {
653 return NO_ERROR;
654 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700655
Ady Abraham37965d42018-11-01 13:43:32 -0700656 virtual status_t getColorManagement(bool* /*outGetColorManagement*/) const { return NO_ERROR; }
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700657
Brian Anderson3da8d272016-07-28 16:20:47 -0700658protected:
659 IBinder* onAsBinder() override { return nullptr; }
660
661private:
662 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700663};
664
665class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
666public:
667 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
668 : mFenceMap(fenceMap) {}
669
670 ~FakeProducerFrameEventHistory() {}
671
672 void updateAcquireFence(uint64_t frameNumber,
673 std::shared_ptr<FenceTime>&& acquire) override {
674 // Verify the acquire fence being added isn't the one from the consumer.
675 EXPECT_NE(mConsumerAcquireFence, acquire);
676 // Override the fence, so we can verify this was called by the
677 // producer after the frame is queued.
678 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
679 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
680 }
681
682 void setAcquireFenceOverride(
683 const std::shared_ptr<FenceTime>& acquireFenceOverride,
684 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
685 mAcquireFenceOverride = acquireFenceOverride;
686 mConsumerAcquireFence = consumerAcquireFence;
687 }
688
689protected:
690 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
691 const override {
692 return mFenceMap->createFenceTimeForTest(fence);
693 }
694
695 FenceToFenceTimeMap* mFenceMap{nullptr};
696
697 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
698 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
699};
700
701
702class TestSurface : public Surface {
703public:
704 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
705 FenceToFenceTimeMap* fenceMap)
706 : Surface(bufferProducer),
707 mFakeSurfaceComposer(new FakeSurfaceComposer) {
708 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
709 mFrameEventHistory.reset(mFakeFrameEventHistory);
710 }
711
712 ~TestSurface() override {}
713
714 sp<ISurfaceComposer> composerService() const override {
715 return mFakeSurfaceComposer;
716 }
717
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800718 nsecs_t now() const override {
719 return mNow;
720 }
721
722 void setNow(nsecs_t now) {
723 mNow = now;
724 }
725
Brian Anderson3da8d272016-07-28 16:20:47 -0700726public:
727 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800728 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700729
730 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
731 // but this raw pointer gives access to test functionality.
732 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
733};
734
735
Brian Andersond0010582017-03-07 13:20:31 -0800736class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700737protected:
738 struct FenceAndFenceTime {
739 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
740 : mFence(new Fence),
741 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
742 sp<Fence> mFence { nullptr };
743 std::shared_ptr<FenceTime> mFenceTime { nullptr };
744 };
745
746 struct RefreshEvents {
747 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800748 : mFenceMap(fenceMap),
749 kCompositorTiming(
750 {refreshStart, refreshStart + 1, refreshStart + 2 }),
751 kStartTime(refreshStart + 3),
752 kGpuCompositionDoneTime(refreshStart + 4),
753 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700754
755 void signalPostCompositeFences() {
756 mFenceMap.signalAllForTest(
757 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
758 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
759 }
760
761 FenceToFenceTimeMap& mFenceMap;
762
763 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
764 FenceAndFenceTime mPresent { mFenceMap };
765
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800766 const CompositorTiming kCompositorTiming;
767
Brian Anderson3da8d272016-07-28 16:20:47 -0700768 const nsecs_t kStartTime;
769 const nsecs_t kGpuCompositionDoneTime;
770 const nsecs_t kPresentTime;
771 };
772
773 struct FrameEvents {
774 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
775 : mFenceMap(fenceMap),
776 kPostedTime(frameStartTime + 100),
777 kRequestedPresentTime(frameStartTime + 200),
778 kProducerAcquireTime(frameStartTime + 300),
779 kConsumerAcquireTime(frameStartTime + 301),
780 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700781 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700782 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700783 mRefreshes {
784 { mFenceMap, frameStartTime + 410 },
785 { mFenceMap, frameStartTime + 420 },
786 { mFenceMap, frameStartTime + 430 } } {}
787
788 void signalQueueFences() {
789 mFenceMap.signalAllForTest(
790 mAcquireConsumer.mFence, kConsumerAcquireTime);
791 mFenceMap.signalAllForTest(
792 mAcquireProducer.mFence, kProducerAcquireTime);
793 }
794
795 void signalRefreshFences() {
796 for (auto& re : mRefreshes) {
797 re.signalPostCompositeFences();
798 }
799 }
800
801 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700802 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
803 }
804
805 FenceToFenceTimeMap& mFenceMap;
806
807 FenceAndFenceTime mAcquireConsumer { mFenceMap };
808 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700809 FenceAndFenceTime mRelease { mFenceMap };
810
811 const nsecs_t kPostedTime;
812 const nsecs_t kRequestedPresentTime;
813 const nsecs_t kProducerAcquireTime;
814 const nsecs_t kConsumerAcquireTime;
815 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700816 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700817 const nsecs_t kReleaseTime;
818
819 RefreshEvents mRefreshes[3];
820 };
821
Brian Andersond0010582017-03-07 13:20:31 -0800822 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700823
824 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700825 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
826 mFakeConsumer = new FakeConsumer;
827 mCfeh = &mFakeConsumer->mFrameEventHistory;
828 mConsumer->consumerConnect(mFakeConsumer, false);
829 mConsumer->setConsumerName(String8("TestConsumer"));
830 mSurface = new TestSurface(mProducer, &mFenceMap);
831 mWindow = mSurface;
832
833 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
834 NATIVE_WINDOW_API_CPU));
835 native_window_set_buffer_count(mWindow.get(), 4);
836 }
837
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800838 void disableFrameTimestamps() {
839 mFakeConsumer->mGetFrameTimestampsEnabled = false;
840 native_window_enable_frame_timestamps(mWindow.get(), 0);
841 mFrameTimestampsEnabled = false;
842 }
843
Brian Anderson3da8d272016-07-28 16:20:47 -0700844 void enableFrameTimestamps() {
845 mFakeConsumer->mGetFrameTimestampsEnabled = true;
846 native_window_enable_frame_timestamps(mWindow.get(), 1);
847 mFrameTimestampsEnabled = true;
848 }
849
Brian Anderson1049d1d2016-12-16 17:25:57 -0800850 int getAllFrameTimestamps(uint64_t frameId) {
851 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700852 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
853 &outFirstRefreshStartTime, &outLastRefreshStartTime,
854 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700855 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700856 }
857
Brian Anderson3da8d272016-07-28 16:20:47 -0700858 void resetTimestamps() {
859 outRequestedPresentTime = -1;
860 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700861 outLatchTime = -1;
862 outFirstRefreshStartTime = -1;
863 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700864 outGpuCompositionDoneTime = -1;
865 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700866 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700867 outReleaseTime = -1;
868 }
869
Brian Anderson1049d1d2016-12-16 17:25:57 -0800870 uint64_t getNextFrameId() {
871 uint64_t frameId = -1;
872 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
873 EXPECT_EQ(status, NO_ERROR);
874 return frameId;
875 }
876
Brian Anderson3da8d272016-07-28 16:20:47 -0700877 void dequeueAndQueue(uint64_t frameIndex) {
878 int fence = -1;
879 ANativeWindowBuffer* buffer = nullptr;
880 ASSERT_EQ(NO_ERROR,
881 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
882
883 int oldAddFrameTimestampsCount =
884 mFakeConsumer->mAddFrameTimestampsCount;
885
886 FrameEvents* frame = &mFrames[frameIndex];
887 uint64_t frameNumber = frameIndex + 1;
888
889 NewFrameEventsEntry fe;
890 fe.frameNumber = frameNumber;
891 fe.postedTime = frame->kPostedTime;
892 fe.requestedPresentTime = frame->kRequestedPresentTime;
893 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
894 mFakeConsumer->mNewFrameEntryOverride = fe;
895
896 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
897 frame->mAcquireProducer.mFenceTime,
898 frame->mAcquireConsumer.mFenceTime);
899
900 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
901
902 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
903
904 EXPECT_EQ(
905 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
906 mFakeConsumer->mAddFrameTimestampsCount);
907 }
908
909 void addFrameEvents(
910 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
911 FrameEvents* oldFrame =
912 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
913 FrameEvents* newFrame = &mFrames[iNewFrame];
914
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800915 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700916 uint64_t nNewFrame = iNewFrame + 1;
917
Brian Anderson4e606e32017-03-16 15:34:57 -0700918 // Latch, Composite, and Release the frames in a plausible order.
919 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700920 // that's okay for the purposes of this test.
921 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
922
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700923 // Composite the previous frame one more time, which helps verify
924 // LastRefresh is updated properly.
925 if (oldFrame != nullptr) {
926 mCfeh->addPreComposition(nOldFrame,
927 oldFrame->mRefreshes[2].kStartTime);
928 gpuDoneFenceTime = gpuComposited ?
929 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
930 FenceTime::NO_FENCE;
931 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800932 oldFrame->mRefreshes[2].mPresent.mFenceTime,
933 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700934 }
935
936 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700937 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
938
939 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
940 gpuDoneFenceTime = gpuComposited ?
941 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
942 FenceTime::NO_FENCE;
943 // HWC2 releases the previous buffer after a new latch just before
944 // calling postComposition.
945 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700946 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700947 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
948 }
949 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800950 newFrame->mRefreshes[0].mPresent.mFenceTime,
951 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700952
Brian Anderson3da8d272016-07-28 16:20:47 -0700953 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
954 gpuDoneFenceTime = gpuComposited ?
955 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
956 FenceTime::NO_FENCE;
957 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800958 newFrame->mRefreshes[1].mPresent.mFenceTime,
959 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700960 }
961
Brian Anderson3da8d272016-07-28 16:20:47 -0700962 sp<IGraphicBufferProducer> mProducer;
963 sp<IGraphicBufferConsumer> mConsumer;
964 sp<FakeConsumer> mFakeConsumer;
965 ConsumerFrameEventHistory* mCfeh;
966 sp<TestSurface> mSurface;
967 sp<ANativeWindow> mWindow;
968
969 FenceToFenceTimeMap mFenceMap;
970
971 bool mFrameTimestampsEnabled = false;
972
973 int64_t outRequestedPresentTime = -1;
974 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700975 int64_t outLatchTime = -1;
976 int64_t outFirstRefreshStartTime = -1;
977 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700978 int64_t outGpuCompositionDoneTime = -1;
979 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700980 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700981 int64_t outReleaseTime = -1;
982
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800983 FrameEvents mFrames[3] {
984 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700985};
986
987
988// This test verifies that the frame timestamps are not retrieved when not
989// explicitly enabled via native_window_enable_frame_timestamps.
990// We want to check this to make sure there's no overhead for users
991// that don't need the timestamp information.
992TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
993 int fence;
994 ANativeWindowBuffer* buffer;
995
996 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
997 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
998
Brian Anderson1049d1d2016-12-16 17:25:57 -0800999 const uint64_t fId = getNextFrameId();
1000
Brian Anderson3da8d272016-07-28 16:20:47 -07001001 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
1002 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1003 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1004 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1005
1006 // Verify the producer doesn't get frame timestamps piggybacked on queue.
1007 // It is okay that frame timestamps are added in the consumer since it is
1008 // still needed for SurfaceFlinger dumps.
1009 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1010 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1011 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1012
1013 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001014 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -07001015 EXPECT_EQ(INVALID_OPERATION, result);
1016 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001017
1018 // Verify compositor timing query fails.
1019 nsecs_t compositeDeadline = 0;
1020 nsecs_t compositeInterval = 0;
1021 nsecs_t compositeToPresentLatency = 0;
1022 result = native_window_get_compositor_timing(mWindow.get(),
1023 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1024 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001025}
1026
1027// This test verifies that the frame timestamps are retrieved if explicitly
1028// enabled via native_window_enable_frame_timestamps.
1029TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001030 CompositorTiming initialCompositorTiming {
1031 1000000000, // 1s deadline
1032 16666667, // 16ms interval
1033 50000000, // 50ms present latency
1034 };
1035 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1036
Brian Anderson3da8d272016-07-28 16:20:47 -07001037 enableFrameTimestamps();
1038
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001039 // Verify the compositor timing query gets the initial compositor values
1040 // after timststamps are enabled; even before the first frame is queued
1041 // or dequeued.
1042 nsecs_t compositeDeadline = 0;
1043 nsecs_t compositeInterval = 0;
1044 nsecs_t compositeToPresentLatency = 0;
1045 mSurface->setNow(initialCompositorTiming.deadline - 1);
1046 int result = native_window_get_compositor_timing(mWindow.get(),
1047 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1048 EXPECT_EQ(NO_ERROR, result);
1049 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1050 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1051 EXPECT_EQ(initialCompositorTiming.presentLatency,
1052 compositeToPresentLatency);
1053
Brian Anderson3da8d272016-07-28 16:20:47 -07001054 int fence;
1055 ANativeWindowBuffer* buffer;
1056
1057 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001058 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001059
Brian Anderson1049d1d2016-12-16 17:25:57 -08001060 const uint64_t fId1 = getNextFrameId();
1061
Brian Anderson3da8d272016-07-28 16:20:47 -07001062 // Verify getFrameTimestamps is piggybacked on dequeue.
1063 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1064 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001065 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001066
1067 NewFrameEventsEntry f1;
1068 f1.frameNumber = 1;
1069 f1.postedTime = mFrames[0].kPostedTime;
1070 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1071 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1072 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1073 mFrames[0].mAcquireProducer.mFenceTime,
1074 mFrames[0].mAcquireConsumer.mFenceTime);
1075 mFakeConsumer->mNewFrameEntryOverride = f1;
1076 mFrames[0].signalQueueFences();
1077
1078 // Verify getFrameTimestamps is piggybacked on queue.
1079 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1080 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1081 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001082 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001083
1084 // Verify queries for timestamps that the producer doesn't know about
1085 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001086 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001087 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001088 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001089}
1090
Brian Anderson6b376712017-04-04 10:51:39 -07001091TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1092 bool displayPresentSupported = true;
1093 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1094
1095 // Verify supported bits are forwarded.
1096 int supportsPresent = -1;
1097 mWindow.get()->query(mWindow.get(),
1098 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1099 EXPECT_EQ(displayPresentSupported, supportsPresent);
1100}
1101
1102TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1103 bool displayPresentSupported = false;
1104 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1105
1106 // Verify supported bits are forwarded.
1107 int supportsPresent = -1;
1108 mWindow.get()->query(mWindow.get(),
1109 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1110 EXPECT_EQ(displayPresentSupported, supportsPresent);
1111}
1112
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001113TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1114 nsecs_t phase = 4000;
1115 nsecs_t interval = 1000;
1116
1117 // Timestamp in previous interval.
1118 nsecs_t timestamp = 3500;
1119 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1120 timestamp, phase, interval));
1121
1122 // Timestamp in next interval.
1123 timestamp = 4500;
1124 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1125 timestamp, phase, interval));
1126
1127 // Timestamp multiple intervals before.
1128 timestamp = 2500;
1129 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1130 timestamp, phase, interval));
1131
1132 // Timestamp multiple intervals after.
1133 timestamp = 6500;
1134 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1135 timestamp, phase, interval));
1136
1137 // Timestamp on previous interval.
1138 timestamp = 3000;
1139 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1140 timestamp, phase, interval));
1141
1142 // Timestamp on next interval.
1143 timestamp = 5000;
1144 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1145 timestamp, phase, interval));
1146
1147 // Timestamp equal to phase.
1148 timestamp = 4000;
1149 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1150 timestamp, phase, interval));
1151}
1152
1153// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1154// if the number of intervals elapsed is internally stored in an int.
1155TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1156 nsecs_t phase = 0;
1157 nsecs_t interval = 4000;
1158 nsecs_t big_timestamp = 8635916564000;
1159 int32_t intervals = big_timestamp / interval;
1160
1161 EXPECT_LT(intervals, 0);
1162 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1163 big_timestamp, phase, interval));
1164 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1165 big_timestamp, big_timestamp, interval));
1166}
1167
1168// This verifies the compositor timing is updated by refresh events
1169// and piggy backed on a queue, dequeue, and enabling of timestamps..
1170TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1171 CompositorTiming initialCompositorTiming {
1172 1000000000, // 1s deadline
1173 16666667, // 16ms interval
1174 50000000, // 50ms present latency
1175 };
1176 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1177
1178 enableFrameTimestamps();
1179
1180 // We get the initial values before any frames are submitted.
1181 nsecs_t compositeDeadline = 0;
1182 nsecs_t compositeInterval = 0;
1183 nsecs_t compositeToPresentLatency = 0;
1184 mSurface->setNow(initialCompositorTiming.deadline - 1);
1185 int result = native_window_get_compositor_timing(mWindow.get(),
1186 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1187 EXPECT_EQ(NO_ERROR, result);
1188 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1189 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1190 EXPECT_EQ(initialCompositorTiming.presentLatency,
1191 compositeToPresentLatency);
1192
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001193 dequeueAndQueue(0);
1194 addFrameEvents(true, NO_FRAME_INDEX, 0);
1195
1196 // Still get the initial values because the frame events for frame 0
1197 // didn't get a chance to piggyback on a queue or dequeue yet.
1198 result = native_window_get_compositor_timing(mWindow.get(),
1199 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1200 EXPECT_EQ(NO_ERROR, result);
1201 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1202 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1203 EXPECT_EQ(initialCompositorTiming.presentLatency,
1204 compositeToPresentLatency);
1205
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001206 dequeueAndQueue(1);
1207 addFrameEvents(true, 0, 1);
1208
1209 // Now expect the composite values associated with frame 1.
1210 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1211 result = native_window_get_compositor_timing(mWindow.get(),
1212 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1213 EXPECT_EQ(NO_ERROR, result);
1214 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1215 compositeDeadline);
1216 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1217 compositeInterval);
1218 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1219 compositeToPresentLatency);
1220
1221 dequeueAndQueue(2);
1222 addFrameEvents(true, 1, 2);
1223
1224 // Now expect the composite values associated with frame 2.
1225 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1226 result = native_window_get_compositor_timing(mWindow.get(),
1227 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1228 EXPECT_EQ(NO_ERROR, result);
1229 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1230 compositeDeadline);
1231 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1232 compositeInterval);
1233 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1234 compositeToPresentLatency);
1235
1236 // Re-enabling frame timestamps should get the latest values.
1237 disableFrameTimestamps();
1238 enableFrameTimestamps();
1239
1240 // Now expect the composite values associated with frame 3.
1241 mSurface->setNow(mFrames[2].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[2].mRefreshes[1].kCompositorTiming.deadline,
1246 compositeDeadline);
1247 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1248 compositeInterval);
1249 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1250 compositeToPresentLatency);
1251}
1252
1253// This verifies the compositor deadline properly snaps to the the next
1254// deadline based on the current time.
1255TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1256 CompositorTiming initialCompositorTiming {
1257 1000000000, // 1s deadline
1258 16666667, // 16ms interval
1259 50000000, // 50ms present latency
1260 };
1261 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1262
1263 enableFrameTimestamps();
1264
1265 nsecs_t compositeDeadline = 0;
1266 nsecs_t compositeInterval = 0;
1267 nsecs_t compositeToPresentLatency = 0;
1268
1269 // A "now" just before the deadline snaps to the deadline.
1270 mSurface->setNow(initialCompositorTiming.deadline - 1);
1271 int result = native_window_get_compositor_timing(mWindow.get(),
1272 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1273 EXPECT_EQ(NO_ERROR, result);
1274 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1275 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1276 EXPECT_EQ(expectedDeadline, compositeDeadline);
1277
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001278 dequeueAndQueue(0);
1279 addFrameEvents(true, NO_FRAME_INDEX, 0);
1280
1281 // A "now" just after the deadline snaps properly.
1282 mSurface->setNow(initialCompositorTiming.deadline + 1);
1283 result = native_window_get_compositor_timing(mWindow.get(),
1284 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1285 EXPECT_EQ(NO_ERROR, result);
1286 expectedDeadline =
1287 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1288 EXPECT_EQ(expectedDeadline, compositeDeadline);
1289
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001290 dequeueAndQueue(1);
1291 addFrameEvents(true, 0, 1);
1292
1293 // A "now" just after the next interval snaps properly.
1294 mSurface->setNow(
1295 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1296 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1297 result = native_window_get_compositor_timing(mWindow.get(),
1298 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1299 EXPECT_EQ(NO_ERROR, result);
1300 expectedDeadline =
1301 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1302 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1303 EXPECT_EQ(expectedDeadline, compositeDeadline);
1304
1305 dequeueAndQueue(2);
1306 addFrameEvents(true, 1, 2);
1307
1308 // A "now" over 1 interval before the deadline snaps properly.
1309 mSurface->setNow(
1310 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1311 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1312 result = native_window_get_compositor_timing(mWindow.get(),
1313 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1314 EXPECT_EQ(NO_ERROR, result);
1315 expectedDeadline =
1316 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1317 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1318 EXPECT_EQ(expectedDeadline, compositeDeadline);
1319
1320 // Re-enabling frame timestamps should get the latest values.
1321 disableFrameTimestamps();
1322 enableFrameTimestamps();
1323
1324 // A "now" over 2 intervals before the deadline snaps properly.
1325 mSurface->setNow(
1326 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1327 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1328 result = native_window_get_compositor_timing(mWindow.get(),
1329 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1330 EXPECT_EQ(NO_ERROR, result);
1331 expectedDeadline =
1332 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1333 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1334 EXPECT_EQ(expectedDeadline, compositeDeadline);
1335}
1336
Brian Anderson1049d1d2016-12-16 17:25:57 -08001337// This verifies the timestamps recorded in the consumer's
1338// FrameTimestampsHistory are properly retrieved by the producer for the
1339// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001340TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1341 enableFrameTimestamps();
1342
Brian Anderson1049d1d2016-12-16 17:25:57 -08001343 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001344 dequeueAndQueue(0);
1345 mFrames[0].signalQueueFences();
1346
Brian Anderson1049d1d2016-12-16 17:25:57 -08001347 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001348 dequeueAndQueue(1);
1349 mFrames[1].signalQueueFences();
1350
1351 addFrameEvents(true, NO_FRAME_INDEX, 0);
1352 mFrames[0].signalRefreshFences();
1353 addFrameEvents(true, 0, 1);
1354 mFrames[0].signalReleaseFences();
1355 mFrames[1].signalRefreshFences();
1356
1357 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001358 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001359 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001360 EXPECT_EQ(NO_ERROR, result);
1361 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1362 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001363 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1364 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1365 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001366 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1367 outGpuCompositionDoneTime);
1368 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001369 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001370 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1371
1372 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001373 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001374 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001375 EXPECT_EQ(NO_ERROR, result);
1376 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1377 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001378 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1379 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1380 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001381 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1382 outGpuCompositionDoneTime);
1383 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001384 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1385 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001386}
1387
1388// This test verifies the acquire fence recorded by the consumer is not sent
1389// back to the producer and the producer saves its own fence.
1390TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1391 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001392
Brian Anderson3da8d272016-07-28 16:20:47 -07001393 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001394 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001395 dequeueAndQueue(0);
1396
1397 // Verify queue-related timestamps for f1 are available immediately in the
1398 // producer without asking the consumer again, even before signaling the
1399 // acquire fence.
1400 resetTimestamps();
1401 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001402 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001403 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001404 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001405 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1406 EXPECT_EQ(NO_ERROR, result);
1407 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001408 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001409
1410 // Signal acquire fences. Verify a sync call still isn't necessary.
1411 mFrames[0].signalQueueFences();
1412
1413 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001414 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001415 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001416 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001417 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1418 EXPECT_EQ(NO_ERROR, result);
1419 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1420 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1421
1422 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001423 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001424 dequeueAndQueue(1);
1425
1426 // Verify queue-related timestamps for f2 are available immediately in the
1427 // producer without asking the consumer again, even before signaling the
1428 // acquire fence.
1429 resetTimestamps();
1430 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001431 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001432 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001433 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001434 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1435 EXPECT_EQ(NO_ERROR, result);
1436 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001437 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001438
1439 // Signal acquire fences. Verify a sync call still isn't necessary.
1440 mFrames[1].signalQueueFences();
1441
1442 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001443 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001444 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001445 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001446 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1447 EXPECT_EQ(NO_ERROR, result);
1448 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1449 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1450}
1451
1452TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1453 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001454
1455 // Dequeue and queue frame 1.
1456 dequeueAndQueue(0);
1457 mFrames[0].signalQueueFences();
1458
1459 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001460 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001461 dequeueAndQueue(1);
1462 mFrames[1].signalQueueFences();
1463
1464 addFrameEvents(true, NO_FRAME_INDEX, 0);
1465 mFrames[0].signalRefreshFences();
1466 addFrameEvents(true, 0, 1);
1467 mFrames[0].signalReleaseFences();
1468 mFrames[1].signalRefreshFences();
1469
1470 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001471 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001472 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001473 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1474 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001475 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001476 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1477}
1478
1479// This test verifies that fences can signal and update timestamps producer
1480// side without an additional sync call to the consumer.
1481TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1482 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001483
1484 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001485 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001486 dequeueAndQueue(0);
1487 mFrames[0].signalQueueFences();
1488
1489 // Dequeue and queue frame 2.
1490 dequeueAndQueue(1);
1491 mFrames[1].signalQueueFences();
1492
1493 addFrameEvents(true, NO_FRAME_INDEX, 0);
1494 addFrameEvents(true, 0, 1);
1495
1496 // Verify available timestamps are correct for frame 1, before any
1497 // fence has been signaled.
1498 // Note: A sync call is necessary here since the events triggered by
1499 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001500 resetTimestamps();
1501 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001502 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001503 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1504 EXPECT_EQ(NO_ERROR, result);
1505 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1506 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001507 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1508 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1509 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001510 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1511 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001512 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001513 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001514
1515 // Verify available timestamps are correct for frame 1 again, before any
1516 // fence has been signaled.
1517 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001518 resetTimestamps();
1519 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001520 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001521 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1522 EXPECT_EQ(NO_ERROR, result);
1523 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1524 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001525 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1526 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1527 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001528 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1529 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001530 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001531 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001532
1533 // Signal the fences for frame 1.
1534 mFrames[0].signalRefreshFences();
1535 mFrames[0].signalReleaseFences();
1536
1537 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001538 resetTimestamps();
1539 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001540 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001541 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1542 EXPECT_EQ(NO_ERROR, result);
1543 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1544 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001545 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1546 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1547 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001548 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1549 outGpuCompositionDoneTime);
1550 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001551 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001552 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1553}
1554
1555// This test verifies that if the frame wasn't GPU composited but has a refresh
1556// event a sync call isn't made to get the GPU composite done time since it will
1557// never exist.
1558TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1559 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001560
Brian Anderson3da8d272016-07-28 16:20:47 -07001561 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001562 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001563 dequeueAndQueue(0);
1564 mFrames[0].signalQueueFences();
1565
1566 // Dequeue and queue frame 2.
1567 dequeueAndQueue(1);
1568 mFrames[1].signalQueueFences();
1569
1570 addFrameEvents(false, NO_FRAME_INDEX, 0);
1571 addFrameEvents(false, 0, 1);
1572
1573 // Verify available timestamps are correct for frame 1, before any
1574 // fence has been signaled.
1575 // Note: A sync call is necessary here since the events triggered by
1576 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1577 resetTimestamps();
1578 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001579 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001580 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1581 EXPECT_EQ(NO_ERROR, result);
1582 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1583 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001584 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1585 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1586 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001587 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1588 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001589 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001590 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001591
1592 // Signal the fences for frame 1.
1593 mFrames[0].signalRefreshFences();
1594 mFrames[0].signalReleaseFences();
1595
1596 // Verify all timestamps, except GPU composition, are available without a
1597 // sync call.
1598 resetTimestamps();
1599 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001600 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001601 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1602 EXPECT_EQ(NO_ERROR, result);
1603 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1604 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001605 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1606 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1607 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001608 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001609 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001610 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001611 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1612}
1613
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001614// This test verifies that if the certain timestamps can't possibly exist for
1615// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001616TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001617 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001618
1619 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001620 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001621 dequeueAndQueue(0);
1622 mFrames[0].signalQueueFences();
1623
1624 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001625 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001626 dequeueAndQueue(1);
1627 mFrames[1].signalQueueFences();
1628
1629 addFrameEvents(false, NO_FRAME_INDEX, 0);
1630 addFrameEvents(false, 0, 1);
1631
1632 // Verify available timestamps are correct for frame 1, before any
1633 // fence has been signaled.
1634 // Note: A sync call is necessary here since the events triggered by
1635 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001636 resetTimestamps();
1637 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001638 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001639 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1640 EXPECT_EQ(NO_ERROR, result);
1641 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1642 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001643 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1644 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1645 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001646 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1647 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001648 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001649 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001650
1651 mFrames[0].signalRefreshFences();
1652 mFrames[0].signalReleaseFences();
1653 mFrames[1].signalRefreshFences();
1654
Brian Anderson1049d1d2016-12-16 17:25:57 -08001655 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001656 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001657 // available, a sync call should not occur because it's not possible for f2
1658 // to encounter the final value for those events until another frame is
1659 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001660 resetTimestamps();
1661 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001662 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001663 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1664 EXPECT_EQ(NO_ERROR, result);
1665 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1666 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001667 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1668 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1669 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001670 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001671 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001672 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1673 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001674}
1675
Brian Anderson6b376712017-04-04 10:51:39 -07001676// This test verifies there are no sync calls for present times
1677// when they aren't supported and that an error is returned.
1678
1679TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1680 enableFrameTimestamps();
1681 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1682
1683 // Dequeue and queue frame 1.
1684 const uint64_t fId1 = getNextFrameId();
1685 dequeueAndQueue(0);
1686
1687 // Verify a query for the Present times do not trigger a sync call if they
1688 // are not supported.
1689 resetTimestamps();
1690 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1691 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1692 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1693 &outDisplayPresentTime, nullptr, nullptr);
1694 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1695 EXPECT_EQ(BAD_VALUE, result);
1696 EXPECT_EQ(-1, outDisplayPresentTime);
1697}
1698
Dan Stoza932f0082017-05-31 13:50:16 -07001699} // namespace android