blob: 1d2950af1989a8532289b26c75dcc7da42a29a17 [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) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800210 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_V0_SRGB;
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800211 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; }
Jorim Jaggib1e2f8d2017-06-08 15:43:59 -0700548 sp<IDisplayEventConnection> createDisplayEventConnection(ISurfaceComposer::VsyncSource)
549 override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700550 return nullptr;
551 }
552 sp<IBinder> createDisplay(const String8& /*displayName*/,
553 bool /*secure*/) override { return nullptr; }
554 void destroyDisplay(const sp<IBinder>& /*display */) override {}
555 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
556 void setTransactionState(const Vector<ComposerState>& /*state*/,
Marissa Wall713b63f2018-10-17 15:42:43 -0700557 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/,
chaviw273171b2018-12-26 11:46:30 -0800558 const sp<IBinder>& /*applyToken*/,
Marissa Wall17b4e452018-12-26 16:32:34 -0800559 const InputWindowCommands& /*inputWindowCommands*/,
560 int64_t /*desiredPresentTime*/) override {}
561
Brian Anderson3da8d272016-07-28 16:20:47 -0700562 void bootFinished() override {}
563 bool authenticateSurfaceTexture(
564 const sp<IGraphicBufferProducer>& /*surface*/) const override {
565 return false;
566 }
Brian Anderson6b376712017-04-04 10:51:39 -0700567
568 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
569 const override {
570 *outSupported = {
571 FrameEvent::REQUESTED_PRESENT,
572 FrameEvent::ACQUIRE,
573 FrameEvent::LATCH,
574 FrameEvent::FIRST_REFRESH_START,
575 FrameEvent::LAST_REFRESH_START,
576 FrameEvent::GPU_COMPOSITION_DONE,
577 FrameEvent::DEQUEUE_READY,
578 FrameEvent::RELEASE
579 };
580 if (mSupportsPresent) {
581 outSupported->push_back(
582 FrameEvent::DISPLAY_PRESENT);
583 }
584 return NO_ERROR;
585 }
586
Brian Anderson3da8d272016-07-28 16:20:47 -0700587 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
588 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
589 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
590 status_t getDisplayStats(const sp<IBinder>& /*display*/,
591 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
592 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
593 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
594 override {
595 return NO_ERROR;
596 }
597 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700598 Vector<ColorMode>* /*outColorModes*/) override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700599 return NO_ERROR;
600 }
Peiyong Lina52f0292018-03-14 17:26:31 -0700601 ColorMode getActiveColorMode(const sp<IBinder>& /*display*/)
Brian Anderson3da8d272016-07-28 16:20:47 -0700602 override {
Peiyong Lina52f0292018-03-14 17:26:31 -0700603 return ColorMode::NATIVE;
Brian Anderson3da8d272016-07-28 16:20:47 -0700604 }
605 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700606 ColorMode /*colorMode*/) override { return NO_ERROR; }
Peiyong Lin0e003c92018-09-17 11:09:51 -0700607 status_t captureScreen(const sp<IBinder>& /*display*/, sp<GraphicBuffer>* /*outBuffer*/,
608 const ui::Dataspace /*reqDataspace*/,
609 const ui::PixelFormat /*reqPixelFormat*/, Rect /*sourceCrop*/,
610 uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
611 bool /*useIdentityTransform*/, Rotation /*rotation*/) override {
612 return NO_ERROR;
613 }
chaviwa76b2712017-09-20 12:02:26 -0700614 virtual status_t captureLayers(const sp<IBinder>& /*parentHandle*/,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700615 sp<GraphicBuffer>* /*outBuffer*/,
616 const ui::Dataspace /*reqDataspace*/,
617 const ui::PixelFormat /*reqPixelFormat*/,
618 const Rect& /*sourceCrop*/, float /*frameScale*/,
619 bool /*childrenOnly*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700620 return NO_ERROR;
621 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700622 status_t clearAnimationFrameStats() override { return NO_ERROR; }
623 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
624 return NO_ERROR;
625 }
626 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
627 HdrCapabilities* /*outCapabilities*/) const override {
628 return NO_ERROR;
629 }
630 status_t enableVSyncInjections(bool /*enable*/) override {
631 return NO_ERROR;
632 }
633 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800634 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
635 return NO_ERROR;
636 }
Peiyong Linc6780972018-10-28 15:24:08 -0700637 status_t getCompositionPreference(
638 ui::Dataspace* /*outDefaultDataspace*/, ui::PixelFormat* /*outDefaultPixelFormat*/,
639 ui::Dataspace* /*outWideColorGamutDataspace*/,
640 ui::PixelFormat* /*outWideColorGamutPixelFormat*/) const override {
Peiyong Lin0256f722018-08-31 15:45:10 -0700641 return NO_ERROR;
642 }
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700643 status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& /*display*/,
644 ui::PixelFormat* /*outFormat*/,
645 ui::Dataspace* /*outDataspace*/,
646 uint8_t* /*outComponentMask*/) const override {
647 return NO_ERROR;
648 }
Kevin DuBois74e53772018-11-19 10:52:38 -0800649 status_t setDisplayContentSamplingEnabled(const sp<IBinder>& /*display*/, bool /*enable*/,
650 uint8_t /*componentMask*/,
651 uint64_t /*maxFrames*/) const override {
652 return NO_ERROR;
653 }
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700654 status_t getDisplayedContentSample(const sp<IBinder>& /*display*/, uint64_t /*maxFrames*/,
655 uint64_t /*timestamp*/,
656 DisplayedFrameStats* /*outStats*/) const override {
657 return NO_ERROR;
658 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700659
Peiyong Lin3c2791e2019-01-14 17:05:18 -0800660 status_t getColorManagement(bool* /*outGetColorManagement*/) const override { return NO_ERROR; }
661 status_t getProtectedContentSupport(bool* /*outSupported*/) const override { return NO_ERROR; }
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700662
Brian Anderson3da8d272016-07-28 16:20:47 -0700663protected:
664 IBinder* onAsBinder() override { return nullptr; }
665
666private:
667 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700668};
669
670class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
671public:
Chih-Hung Hsiehaaf62162018-12-20 15:45:04 -0800672 explicit FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap) : mFenceMap(fenceMap) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700673
674 ~FakeProducerFrameEventHistory() {}
675
676 void updateAcquireFence(uint64_t frameNumber,
677 std::shared_ptr<FenceTime>&& acquire) override {
678 // Verify the acquire fence being added isn't the one from the consumer.
679 EXPECT_NE(mConsumerAcquireFence, acquire);
680 // Override the fence, so we can verify this was called by the
681 // producer after the frame is queued.
682 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
683 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
684 }
685
686 void setAcquireFenceOverride(
687 const std::shared_ptr<FenceTime>& acquireFenceOverride,
688 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
689 mAcquireFenceOverride = acquireFenceOverride;
690 mConsumerAcquireFence = consumerAcquireFence;
691 }
692
693protected:
694 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
695 const override {
696 return mFenceMap->createFenceTimeForTest(fence);
697 }
698
699 FenceToFenceTimeMap* mFenceMap{nullptr};
700
701 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
702 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
703};
704
705
706class TestSurface : public Surface {
707public:
708 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
709 FenceToFenceTimeMap* fenceMap)
710 : Surface(bufferProducer),
711 mFakeSurfaceComposer(new FakeSurfaceComposer) {
712 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
713 mFrameEventHistory.reset(mFakeFrameEventHistory);
714 }
715
716 ~TestSurface() override {}
717
718 sp<ISurfaceComposer> composerService() const override {
719 return mFakeSurfaceComposer;
720 }
721
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800722 nsecs_t now() const override {
723 return mNow;
724 }
725
726 void setNow(nsecs_t now) {
727 mNow = now;
728 }
729
Brian Anderson3da8d272016-07-28 16:20:47 -0700730public:
731 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800732 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700733
734 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
735 // but this raw pointer gives access to test functionality.
736 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
737};
738
739
Brian Andersond0010582017-03-07 13:20:31 -0800740class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700741protected:
742 struct FenceAndFenceTime {
743 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
744 : mFence(new Fence),
745 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
746 sp<Fence> mFence { nullptr };
747 std::shared_ptr<FenceTime> mFenceTime { nullptr };
748 };
749
750 struct RefreshEvents {
751 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800752 : mFenceMap(fenceMap),
753 kCompositorTiming(
754 {refreshStart, refreshStart + 1, refreshStart + 2 }),
755 kStartTime(refreshStart + 3),
756 kGpuCompositionDoneTime(refreshStart + 4),
757 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700758
759 void signalPostCompositeFences() {
760 mFenceMap.signalAllForTest(
761 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
762 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
763 }
764
765 FenceToFenceTimeMap& mFenceMap;
766
767 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
768 FenceAndFenceTime mPresent { mFenceMap };
769
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800770 const CompositorTiming kCompositorTiming;
771
Brian Anderson3da8d272016-07-28 16:20:47 -0700772 const nsecs_t kStartTime;
773 const nsecs_t kGpuCompositionDoneTime;
774 const nsecs_t kPresentTime;
775 };
776
777 struct FrameEvents {
778 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
779 : mFenceMap(fenceMap),
780 kPostedTime(frameStartTime + 100),
781 kRequestedPresentTime(frameStartTime + 200),
782 kProducerAcquireTime(frameStartTime + 300),
783 kConsumerAcquireTime(frameStartTime + 301),
784 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700785 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700786 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700787 mRefreshes {
788 { mFenceMap, frameStartTime + 410 },
789 { mFenceMap, frameStartTime + 420 },
790 { mFenceMap, frameStartTime + 430 } } {}
791
792 void signalQueueFences() {
793 mFenceMap.signalAllForTest(
794 mAcquireConsumer.mFence, kConsumerAcquireTime);
795 mFenceMap.signalAllForTest(
796 mAcquireProducer.mFence, kProducerAcquireTime);
797 }
798
799 void signalRefreshFences() {
800 for (auto& re : mRefreshes) {
801 re.signalPostCompositeFences();
802 }
803 }
804
805 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700806 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
807 }
808
809 FenceToFenceTimeMap& mFenceMap;
810
811 FenceAndFenceTime mAcquireConsumer { mFenceMap };
812 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700813 FenceAndFenceTime mRelease { mFenceMap };
814
815 const nsecs_t kPostedTime;
816 const nsecs_t kRequestedPresentTime;
817 const nsecs_t kProducerAcquireTime;
818 const nsecs_t kConsumerAcquireTime;
819 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700820 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700821 const nsecs_t kReleaseTime;
822
823 RefreshEvents mRefreshes[3];
824 };
825
Brian Andersond0010582017-03-07 13:20:31 -0800826 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700827
828 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700829 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
830 mFakeConsumer = new FakeConsumer;
831 mCfeh = &mFakeConsumer->mFrameEventHistory;
832 mConsumer->consumerConnect(mFakeConsumer, false);
833 mConsumer->setConsumerName(String8("TestConsumer"));
834 mSurface = new TestSurface(mProducer, &mFenceMap);
835 mWindow = mSurface;
836
837 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
838 NATIVE_WINDOW_API_CPU));
839 native_window_set_buffer_count(mWindow.get(), 4);
840 }
841
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800842 void disableFrameTimestamps() {
843 mFakeConsumer->mGetFrameTimestampsEnabled = false;
844 native_window_enable_frame_timestamps(mWindow.get(), 0);
845 mFrameTimestampsEnabled = false;
846 }
847
Brian Anderson3da8d272016-07-28 16:20:47 -0700848 void enableFrameTimestamps() {
849 mFakeConsumer->mGetFrameTimestampsEnabled = true;
850 native_window_enable_frame_timestamps(mWindow.get(), 1);
851 mFrameTimestampsEnabled = true;
852 }
853
Brian Anderson1049d1d2016-12-16 17:25:57 -0800854 int getAllFrameTimestamps(uint64_t frameId) {
855 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700856 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
857 &outFirstRefreshStartTime, &outLastRefreshStartTime,
858 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700859 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700860 }
861
Brian Anderson3da8d272016-07-28 16:20:47 -0700862 void resetTimestamps() {
863 outRequestedPresentTime = -1;
864 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700865 outLatchTime = -1;
866 outFirstRefreshStartTime = -1;
867 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700868 outGpuCompositionDoneTime = -1;
869 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700870 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700871 outReleaseTime = -1;
872 }
873
Brian Anderson1049d1d2016-12-16 17:25:57 -0800874 uint64_t getNextFrameId() {
875 uint64_t frameId = -1;
876 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
877 EXPECT_EQ(status, NO_ERROR);
878 return frameId;
879 }
880
Brian Anderson3da8d272016-07-28 16:20:47 -0700881 void dequeueAndQueue(uint64_t frameIndex) {
882 int fence = -1;
883 ANativeWindowBuffer* buffer = nullptr;
884 ASSERT_EQ(NO_ERROR,
885 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
886
887 int oldAddFrameTimestampsCount =
888 mFakeConsumer->mAddFrameTimestampsCount;
889
890 FrameEvents* frame = &mFrames[frameIndex];
891 uint64_t frameNumber = frameIndex + 1;
892
893 NewFrameEventsEntry fe;
894 fe.frameNumber = frameNumber;
895 fe.postedTime = frame->kPostedTime;
896 fe.requestedPresentTime = frame->kRequestedPresentTime;
897 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
898 mFakeConsumer->mNewFrameEntryOverride = fe;
899
900 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
901 frame->mAcquireProducer.mFenceTime,
902 frame->mAcquireConsumer.mFenceTime);
903
904 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
905
906 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
907
908 EXPECT_EQ(
909 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
910 mFakeConsumer->mAddFrameTimestampsCount);
911 }
912
913 void addFrameEvents(
914 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
915 FrameEvents* oldFrame =
916 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
917 FrameEvents* newFrame = &mFrames[iNewFrame];
918
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800919 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700920 uint64_t nNewFrame = iNewFrame + 1;
921
Brian Anderson4e606e32017-03-16 15:34:57 -0700922 // Latch, Composite, and Release the frames in a plausible order.
923 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700924 // that's okay for the purposes of this test.
925 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
926
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700927 // Composite the previous frame one more time, which helps verify
928 // LastRefresh is updated properly.
929 if (oldFrame != nullptr) {
930 mCfeh->addPreComposition(nOldFrame,
931 oldFrame->mRefreshes[2].kStartTime);
932 gpuDoneFenceTime = gpuComposited ?
933 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
934 FenceTime::NO_FENCE;
935 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800936 oldFrame->mRefreshes[2].mPresent.mFenceTime,
937 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700938 }
939
940 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700941 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
942
943 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
944 gpuDoneFenceTime = gpuComposited ?
945 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
946 FenceTime::NO_FENCE;
947 // HWC2 releases the previous buffer after a new latch just before
948 // calling postComposition.
949 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700950 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700951 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
952 }
953 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800954 newFrame->mRefreshes[0].mPresent.mFenceTime,
955 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700956
Brian Anderson3da8d272016-07-28 16:20:47 -0700957 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
958 gpuDoneFenceTime = gpuComposited ?
959 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
960 FenceTime::NO_FENCE;
961 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800962 newFrame->mRefreshes[1].mPresent.mFenceTime,
963 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700964 }
965
Brian Anderson3da8d272016-07-28 16:20:47 -0700966 sp<IGraphicBufferProducer> mProducer;
967 sp<IGraphicBufferConsumer> mConsumer;
968 sp<FakeConsumer> mFakeConsumer;
969 ConsumerFrameEventHistory* mCfeh;
970 sp<TestSurface> mSurface;
971 sp<ANativeWindow> mWindow;
972
973 FenceToFenceTimeMap mFenceMap;
974
975 bool mFrameTimestampsEnabled = false;
976
977 int64_t outRequestedPresentTime = -1;
978 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700979 int64_t outLatchTime = -1;
980 int64_t outFirstRefreshStartTime = -1;
981 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700982 int64_t outGpuCompositionDoneTime = -1;
983 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700984 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700985 int64_t outReleaseTime = -1;
986
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800987 FrameEvents mFrames[3] {
988 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700989};
990
991
992// This test verifies that the frame timestamps are not retrieved when not
993// explicitly enabled via native_window_enable_frame_timestamps.
994// We want to check this to make sure there's no overhead for users
995// that don't need the timestamp information.
996TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
997 int fence;
998 ANativeWindowBuffer* buffer;
999
1000 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1001 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1002
Brian Anderson1049d1d2016-12-16 17:25:57 -08001003 const uint64_t fId = getNextFrameId();
1004
Brian Anderson3da8d272016-07-28 16:20:47 -07001005 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
1006 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1007 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1008 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1009
1010 // Verify the producer doesn't get frame timestamps piggybacked on queue.
1011 // It is okay that frame timestamps are added in the consumer since it is
1012 // still needed for SurfaceFlinger dumps.
1013 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1014 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1015 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1016
1017 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001018 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -07001019 EXPECT_EQ(INVALID_OPERATION, result);
1020 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001021
1022 // Verify compositor timing query fails.
1023 nsecs_t compositeDeadline = 0;
1024 nsecs_t compositeInterval = 0;
1025 nsecs_t compositeToPresentLatency = 0;
1026 result = native_window_get_compositor_timing(mWindow.get(),
1027 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1028 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001029}
1030
1031// This test verifies that the frame timestamps are retrieved if explicitly
1032// enabled via native_window_enable_frame_timestamps.
1033TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001034 CompositorTiming initialCompositorTiming {
1035 1000000000, // 1s deadline
1036 16666667, // 16ms interval
1037 50000000, // 50ms present latency
1038 };
1039 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1040
Brian Anderson3da8d272016-07-28 16:20:47 -07001041 enableFrameTimestamps();
1042
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001043 // Verify the compositor timing query gets the initial compositor values
1044 // after timststamps are enabled; even before the first frame is queued
1045 // or dequeued.
1046 nsecs_t compositeDeadline = 0;
1047 nsecs_t compositeInterval = 0;
1048 nsecs_t compositeToPresentLatency = 0;
1049 mSurface->setNow(initialCompositorTiming.deadline - 1);
1050 int result = native_window_get_compositor_timing(mWindow.get(),
1051 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1052 EXPECT_EQ(NO_ERROR, result);
1053 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1054 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1055 EXPECT_EQ(initialCompositorTiming.presentLatency,
1056 compositeToPresentLatency);
1057
Brian Anderson3da8d272016-07-28 16:20:47 -07001058 int fence;
1059 ANativeWindowBuffer* buffer;
1060
1061 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001062 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001063
Brian Anderson1049d1d2016-12-16 17:25:57 -08001064 const uint64_t fId1 = getNextFrameId();
1065
Brian Anderson3da8d272016-07-28 16:20:47 -07001066 // Verify getFrameTimestamps is piggybacked on dequeue.
1067 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1068 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001069 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001070
1071 NewFrameEventsEntry f1;
1072 f1.frameNumber = 1;
1073 f1.postedTime = mFrames[0].kPostedTime;
1074 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1075 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1076 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1077 mFrames[0].mAcquireProducer.mFenceTime,
1078 mFrames[0].mAcquireConsumer.mFenceTime);
1079 mFakeConsumer->mNewFrameEntryOverride = f1;
1080 mFrames[0].signalQueueFences();
1081
1082 // Verify getFrameTimestamps is piggybacked on queue.
1083 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1084 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1085 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001086 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001087
1088 // Verify queries for timestamps that the producer doesn't know about
1089 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001090 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001091 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001092 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001093}
1094
Brian Anderson6b376712017-04-04 10:51:39 -07001095TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1096 bool displayPresentSupported = true;
1097 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1098
1099 // Verify supported bits are forwarded.
1100 int supportsPresent = -1;
1101 mWindow.get()->query(mWindow.get(),
1102 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1103 EXPECT_EQ(displayPresentSupported, supportsPresent);
1104}
1105
1106TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1107 bool displayPresentSupported = false;
1108 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1109
1110 // Verify supported bits are forwarded.
1111 int supportsPresent = -1;
1112 mWindow.get()->query(mWindow.get(),
1113 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1114 EXPECT_EQ(displayPresentSupported, supportsPresent);
1115}
1116
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001117TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1118 nsecs_t phase = 4000;
1119 nsecs_t interval = 1000;
1120
1121 // Timestamp in previous interval.
1122 nsecs_t timestamp = 3500;
1123 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1124 timestamp, phase, interval));
1125
1126 // Timestamp in next interval.
1127 timestamp = 4500;
1128 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1129 timestamp, phase, interval));
1130
1131 // Timestamp multiple intervals before.
1132 timestamp = 2500;
1133 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1134 timestamp, phase, interval));
1135
1136 // Timestamp multiple intervals after.
1137 timestamp = 6500;
1138 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1139 timestamp, phase, interval));
1140
1141 // Timestamp on previous interval.
1142 timestamp = 3000;
1143 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1144 timestamp, phase, interval));
1145
1146 // Timestamp on next interval.
1147 timestamp = 5000;
1148 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1149 timestamp, phase, interval));
1150
1151 // Timestamp equal to phase.
1152 timestamp = 4000;
1153 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1154 timestamp, phase, interval));
1155}
1156
1157// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1158// if the number of intervals elapsed is internally stored in an int.
1159TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1160 nsecs_t phase = 0;
1161 nsecs_t interval = 4000;
1162 nsecs_t big_timestamp = 8635916564000;
1163 int32_t intervals = big_timestamp / interval;
1164
1165 EXPECT_LT(intervals, 0);
1166 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1167 big_timestamp, phase, interval));
1168 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1169 big_timestamp, big_timestamp, interval));
1170}
1171
1172// This verifies the compositor timing is updated by refresh events
1173// and piggy backed on a queue, dequeue, and enabling of timestamps..
1174TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1175 CompositorTiming initialCompositorTiming {
1176 1000000000, // 1s deadline
1177 16666667, // 16ms interval
1178 50000000, // 50ms present latency
1179 };
1180 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1181
1182 enableFrameTimestamps();
1183
1184 // We get the initial values before any frames are submitted.
1185 nsecs_t compositeDeadline = 0;
1186 nsecs_t compositeInterval = 0;
1187 nsecs_t compositeToPresentLatency = 0;
1188 mSurface->setNow(initialCompositorTiming.deadline - 1);
1189 int result = native_window_get_compositor_timing(mWindow.get(),
1190 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1191 EXPECT_EQ(NO_ERROR, result);
1192 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1193 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1194 EXPECT_EQ(initialCompositorTiming.presentLatency,
1195 compositeToPresentLatency);
1196
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001197 dequeueAndQueue(0);
1198 addFrameEvents(true, NO_FRAME_INDEX, 0);
1199
1200 // Still get the initial values because the frame events for frame 0
1201 // didn't get a chance to piggyback on a queue or dequeue yet.
1202 result = native_window_get_compositor_timing(mWindow.get(),
1203 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1204 EXPECT_EQ(NO_ERROR, result);
1205 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1206 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1207 EXPECT_EQ(initialCompositorTiming.presentLatency,
1208 compositeToPresentLatency);
1209
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001210 dequeueAndQueue(1);
1211 addFrameEvents(true, 0, 1);
1212
1213 // Now expect the composite values associated with frame 1.
1214 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1215 result = native_window_get_compositor_timing(mWindow.get(),
1216 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1217 EXPECT_EQ(NO_ERROR, result);
1218 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1219 compositeDeadline);
1220 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1221 compositeInterval);
1222 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1223 compositeToPresentLatency);
1224
1225 dequeueAndQueue(2);
1226 addFrameEvents(true, 1, 2);
1227
1228 // Now expect the composite values associated with frame 2.
1229 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1230 result = native_window_get_compositor_timing(mWindow.get(),
1231 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1232 EXPECT_EQ(NO_ERROR, result);
1233 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1234 compositeDeadline);
1235 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1236 compositeInterval);
1237 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1238 compositeToPresentLatency);
1239
1240 // Re-enabling frame timestamps should get the latest values.
1241 disableFrameTimestamps();
1242 enableFrameTimestamps();
1243
1244 // Now expect the composite values associated with frame 3.
1245 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1246 result = native_window_get_compositor_timing(mWindow.get(),
1247 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1248 EXPECT_EQ(NO_ERROR, result);
1249 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1250 compositeDeadline);
1251 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1252 compositeInterval);
1253 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1254 compositeToPresentLatency);
1255}
1256
1257// This verifies the compositor deadline properly snaps to the the next
1258// deadline based on the current time.
1259TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1260 CompositorTiming initialCompositorTiming {
1261 1000000000, // 1s deadline
1262 16666667, // 16ms interval
1263 50000000, // 50ms present latency
1264 };
1265 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1266
1267 enableFrameTimestamps();
1268
1269 nsecs_t compositeDeadline = 0;
1270 nsecs_t compositeInterval = 0;
1271 nsecs_t compositeToPresentLatency = 0;
1272
1273 // A "now" just before the deadline snaps to the deadline.
1274 mSurface->setNow(initialCompositorTiming.deadline - 1);
1275 int result = native_window_get_compositor_timing(mWindow.get(),
1276 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1277 EXPECT_EQ(NO_ERROR, result);
1278 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1279 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1280 EXPECT_EQ(expectedDeadline, compositeDeadline);
1281
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001282 dequeueAndQueue(0);
1283 addFrameEvents(true, NO_FRAME_INDEX, 0);
1284
1285 // A "now" just after the deadline snaps properly.
1286 mSurface->setNow(initialCompositorTiming.deadline + 1);
1287 result = native_window_get_compositor_timing(mWindow.get(),
1288 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1289 EXPECT_EQ(NO_ERROR, result);
1290 expectedDeadline =
1291 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1292 EXPECT_EQ(expectedDeadline, compositeDeadline);
1293
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001294 dequeueAndQueue(1);
1295 addFrameEvents(true, 0, 1);
1296
1297 // A "now" just after the next interval snaps properly.
1298 mSurface->setNow(
1299 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1300 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1301 result = native_window_get_compositor_timing(mWindow.get(),
1302 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1303 EXPECT_EQ(NO_ERROR, result);
1304 expectedDeadline =
1305 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1306 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1307 EXPECT_EQ(expectedDeadline, compositeDeadline);
1308
1309 dequeueAndQueue(2);
1310 addFrameEvents(true, 1, 2);
1311
1312 // A "now" over 1 interval before the deadline snaps properly.
1313 mSurface->setNow(
1314 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1315 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1316 result = native_window_get_compositor_timing(mWindow.get(),
1317 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1318 EXPECT_EQ(NO_ERROR, result);
1319 expectedDeadline =
1320 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1321 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1322 EXPECT_EQ(expectedDeadline, compositeDeadline);
1323
1324 // Re-enabling frame timestamps should get the latest values.
1325 disableFrameTimestamps();
1326 enableFrameTimestamps();
1327
1328 // A "now" over 2 intervals before the deadline snaps properly.
1329 mSurface->setNow(
1330 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1331 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1332 result = native_window_get_compositor_timing(mWindow.get(),
1333 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1334 EXPECT_EQ(NO_ERROR, result);
1335 expectedDeadline =
1336 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1337 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1338 EXPECT_EQ(expectedDeadline, compositeDeadline);
1339}
1340
Brian Anderson1049d1d2016-12-16 17:25:57 -08001341// This verifies the timestamps recorded in the consumer's
1342// FrameTimestampsHistory are properly retrieved by the producer for the
1343// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001344TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1345 enableFrameTimestamps();
1346
Brian Anderson1049d1d2016-12-16 17:25:57 -08001347 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001348 dequeueAndQueue(0);
1349 mFrames[0].signalQueueFences();
1350
Brian Anderson1049d1d2016-12-16 17:25:57 -08001351 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001352 dequeueAndQueue(1);
1353 mFrames[1].signalQueueFences();
1354
1355 addFrameEvents(true, NO_FRAME_INDEX, 0);
1356 mFrames[0].signalRefreshFences();
1357 addFrameEvents(true, 0, 1);
1358 mFrames[0].signalReleaseFences();
1359 mFrames[1].signalRefreshFences();
1360
1361 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001362 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001363 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001364 EXPECT_EQ(NO_ERROR, result);
1365 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1366 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001367 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1368 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1369 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001370 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1371 outGpuCompositionDoneTime);
1372 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001373 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001374 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1375
1376 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001377 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001378 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001379 EXPECT_EQ(NO_ERROR, result);
1380 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1381 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001382 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1383 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1384 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001385 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1386 outGpuCompositionDoneTime);
1387 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001388 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1389 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001390}
1391
1392// This test verifies the acquire fence recorded by the consumer is not sent
1393// back to the producer and the producer saves its own fence.
1394TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1395 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001396
Brian Anderson3da8d272016-07-28 16:20:47 -07001397 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001398 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001399 dequeueAndQueue(0);
1400
1401 // Verify queue-related timestamps for f1 are available immediately in the
1402 // producer without asking the consumer again, even before signaling the
1403 // acquire fence.
1404 resetTimestamps();
1405 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001406 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001407 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001408 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001409 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1410 EXPECT_EQ(NO_ERROR, result);
1411 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001412 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001413
1414 // Signal acquire fences. Verify a sync call still isn't necessary.
1415 mFrames[0].signalQueueFences();
1416
1417 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001418 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001419 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001420 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001421 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1422 EXPECT_EQ(NO_ERROR, result);
1423 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1424 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1425
1426 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001427 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001428 dequeueAndQueue(1);
1429
1430 // Verify queue-related timestamps for f2 are available immediately in the
1431 // producer without asking the consumer again, even before signaling the
1432 // acquire fence.
1433 resetTimestamps();
1434 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001435 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001436 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001437 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001438 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1439 EXPECT_EQ(NO_ERROR, result);
1440 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001441 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001442
1443 // Signal acquire fences. Verify a sync call still isn't necessary.
1444 mFrames[1].signalQueueFences();
1445
1446 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001447 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001448 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001449 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001450 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1451 EXPECT_EQ(NO_ERROR, result);
1452 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1453 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1454}
1455
1456TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1457 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001458
1459 // Dequeue and queue frame 1.
1460 dequeueAndQueue(0);
1461 mFrames[0].signalQueueFences();
1462
1463 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001464 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001465 dequeueAndQueue(1);
1466 mFrames[1].signalQueueFences();
1467
1468 addFrameEvents(true, NO_FRAME_INDEX, 0);
1469 mFrames[0].signalRefreshFences();
1470 addFrameEvents(true, 0, 1);
1471 mFrames[0].signalReleaseFences();
1472 mFrames[1].signalRefreshFences();
1473
1474 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001475 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001476 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001477 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1478 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001479 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001480 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1481}
1482
1483// This test verifies that fences can signal and update timestamps producer
1484// side without an additional sync call to the consumer.
1485TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1486 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001487
1488 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001489 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001490 dequeueAndQueue(0);
1491 mFrames[0].signalQueueFences();
1492
1493 // Dequeue and queue frame 2.
1494 dequeueAndQueue(1);
1495 mFrames[1].signalQueueFences();
1496
1497 addFrameEvents(true, NO_FRAME_INDEX, 0);
1498 addFrameEvents(true, 0, 1);
1499
1500 // Verify available timestamps are correct for frame 1, before any
1501 // fence has been signaled.
1502 // Note: A sync call is necessary here since the events triggered by
1503 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001504 resetTimestamps();
1505 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001506 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001507 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1508 EXPECT_EQ(NO_ERROR, result);
1509 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1510 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001511 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1512 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1513 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001514 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1515 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001516 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001517 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001518
1519 // Verify available timestamps are correct for frame 1 again, before any
1520 // fence has been signaled.
1521 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001522 resetTimestamps();
1523 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001524 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001525 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1526 EXPECT_EQ(NO_ERROR, result);
1527 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1528 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001529 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1530 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1531 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001532 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1533 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001534 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001535 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001536
1537 // Signal the fences for frame 1.
1538 mFrames[0].signalRefreshFences();
1539 mFrames[0].signalReleaseFences();
1540
1541 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001542 resetTimestamps();
1543 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001544 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001545 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1546 EXPECT_EQ(NO_ERROR, result);
1547 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1548 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001549 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1550 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1551 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001552 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1553 outGpuCompositionDoneTime);
1554 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001555 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001556 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1557}
1558
1559// This test verifies that if the frame wasn't GPU composited but has a refresh
1560// event a sync call isn't made to get the GPU composite done time since it will
1561// never exist.
1562TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1563 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001564
Brian Anderson3da8d272016-07-28 16:20:47 -07001565 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001566 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001567 dequeueAndQueue(0);
1568 mFrames[0].signalQueueFences();
1569
1570 // Dequeue and queue frame 2.
1571 dequeueAndQueue(1);
1572 mFrames[1].signalQueueFences();
1573
1574 addFrameEvents(false, NO_FRAME_INDEX, 0);
1575 addFrameEvents(false, 0, 1);
1576
1577 // Verify available timestamps are correct for frame 1, before any
1578 // fence has been signaled.
1579 // Note: A sync call is necessary here since the events triggered by
1580 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1581 resetTimestamps();
1582 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001583 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001584 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1585 EXPECT_EQ(NO_ERROR, result);
1586 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1587 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001588 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1589 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1590 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001591 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1592 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001593 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001594 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001595
1596 // Signal the fences for frame 1.
1597 mFrames[0].signalRefreshFences();
1598 mFrames[0].signalReleaseFences();
1599
1600 // Verify all timestamps, except GPU composition, are available without a
1601 // sync call.
1602 resetTimestamps();
1603 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001604 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001605 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1606 EXPECT_EQ(NO_ERROR, result);
1607 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1608 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001609 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1610 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1611 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001612 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001613 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001614 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001615 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1616}
1617
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001618// This test verifies that if the certain timestamps can't possibly exist for
1619// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001620TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001621 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001622
1623 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001624 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001625 dequeueAndQueue(0);
1626 mFrames[0].signalQueueFences();
1627
1628 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001629 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001630 dequeueAndQueue(1);
1631 mFrames[1].signalQueueFences();
1632
1633 addFrameEvents(false, NO_FRAME_INDEX, 0);
1634 addFrameEvents(false, 0, 1);
1635
1636 // Verify available timestamps are correct for frame 1, before any
1637 // fence has been signaled.
1638 // Note: A sync call is necessary here since the events triggered by
1639 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001640 resetTimestamps();
1641 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001642 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001643 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1644 EXPECT_EQ(NO_ERROR, result);
1645 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1646 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001647 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1648 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1649 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001650 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1651 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001652 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001653 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001654
1655 mFrames[0].signalRefreshFences();
1656 mFrames[0].signalReleaseFences();
1657 mFrames[1].signalRefreshFences();
1658
Brian Anderson1049d1d2016-12-16 17:25:57 -08001659 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001660 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001661 // available, a sync call should not occur because it's not possible for f2
1662 // to encounter the final value for those events until another frame is
1663 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001664 resetTimestamps();
1665 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001666 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001667 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1668 EXPECT_EQ(NO_ERROR, result);
1669 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1670 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001671 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1672 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1673 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001674 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001675 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001676 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1677 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001678}
1679
Brian Anderson6b376712017-04-04 10:51:39 -07001680// This test verifies there are no sync calls for present times
1681// when they aren't supported and that an error is returned.
1682
1683TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1684 enableFrameTimestamps();
1685 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1686
1687 // Dequeue and queue frame 1.
1688 const uint64_t fId1 = getNextFrameId();
1689 dequeueAndQueue(0);
1690
1691 // Verify a query for the Present times do not trigger a sync call if they
1692 // are not supported.
1693 resetTimestamps();
1694 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1695 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1696 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1697 &outDisplayPresentTime, nullptr, nullptr);
1698 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1699 EXPECT_EQ(BAD_VALUE, result);
1700 EXPECT_EQ(-1, outDisplayPresentTime);
1701}
1702
Dan Stoza932f0082017-05-31 13:50:16 -07001703} // namespace android