blob: db97faed0201ab819ccc73dc277cafa23c8bbf82 [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.
Robert Carr108b2c72019-04-02 16:32:58 -0700129TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersDontSucceed) {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800130 sp<ANativeWindow> anw(mSurface);
131
132 // Verify the screenshot works with no protected buffers.
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800133 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800134
135 const sp<IBinder> display = sf->getInternalDisplayToken();
136 ASSERT_FALSE(display == nullptr);
137
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000138 sp<GraphicBuffer> outBuffer;
Robert Carr108b2c72019-04-02 16:32:58 -0700139 bool ignored;
Peiyong Lin0e003c92018-09-17 11:09:51 -0700140 ASSERT_EQ(NO_ERROR,
Robert Carr108b2c72019-04-02 16:32:58 -0700141 sf->captureScreen(display, &outBuffer, ignored, ui::Dataspace::V0_SRGB,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700142 ui::PixelFormat::RGBA_8888, Rect(), 64, 64, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800143
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700144 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
145 NATIVE_WINDOW_API_CPU));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800146 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
147 // that we need to dequeue a buffer in order for it to actually get
148 // allocated in SurfaceFlinger.
149 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
150 GRALLOC_USAGE_PROTECTED));
151 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Yi Konga03e0442018-07-17 11:16:57 -0700152 ANativeWindowBuffer* buf = nullptr;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700153
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700154 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700155 if (err) {
156 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
157 // that's okay as long as this is the reason for the failure.
158 // try again without the GRALLOC_USAGE_PROTECTED bit.
159 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700160 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
161 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700162 return;
163 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700164 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700165
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800166 for (int i = 0; i < 4; i++) {
167 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700168 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
169 &buf));
170 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800171 }
Peiyong Lin0e003c92018-09-17 11:09:51 -0700172 ASSERT_EQ(NO_ERROR,
Robert Carr108b2c72019-04-02 16:32:58 -0700173 sf->captureScreen(display, &outBuffer, ignored, ui::Dataspace::V0_SRGB,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700174 ui::PixelFormat::RGBA_8888, Rect(), 64, 64, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800175}
176
Jamie Gennis391bbe22011-03-14 15:00:06 -0700177TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
178 sp<ANativeWindow> anw(mSurface);
179 int result = -123;
180 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
181 EXPECT_EQ(NO_ERROR, err);
182 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
183}
184
Craig Donner6ebc46a2016-10-21 15:23:44 -0700185TEST_F(SurfaceTest, LayerCountIsOne) {
186 sp<ANativeWindow> anw(mSurface);
187 int result = -123;
188 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
189 EXPECT_EQ(NO_ERROR, err);
190 EXPECT_EQ(1, result);
191}
192
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700193TEST_F(SurfaceTest, QueryConsumerUsage) {
194 const int TEST_USAGE_FLAGS =
195 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700196 sp<IGraphicBufferProducer> producer;
197 sp<IGraphicBufferConsumer> consumer;
198 BufferQueue::createBufferQueue(&producer, &consumer);
199 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700200 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700201 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700202
203 sp<ANativeWindow> anw(s);
204
205 int flags = -1;
206 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
207
208 ASSERT_EQ(NO_ERROR, err);
209 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
210}
211
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800212TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800213 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_V0_SRGB;
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800214 sp<IGraphicBufferProducer> producer;
215 sp<IGraphicBufferConsumer> consumer;
216 BufferQueue::createBufferQueue(&producer, &consumer);
217 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
218
219 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
220
221 sp<Surface> s = new Surface(producer);
222
223 sp<ANativeWindow> anw(s);
224
225 android_dataspace dataSpace;
226
227 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
228 reinterpret_cast<int*>(&dataSpace));
229
230 ASSERT_EQ(NO_ERROR, err);
231 ASSERT_EQ(TEST_DATASPACE, dataSpace);
232}
233
Dan Stoza812ed062015-06-02 15:45:22 -0700234TEST_F(SurfaceTest, SettingGenerationNumber) {
235 sp<IGraphicBufferProducer> producer;
236 sp<IGraphicBufferConsumer> consumer;
237 BufferQueue::createBufferQueue(&producer, &consumer);
238 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
239 sp<Surface> surface = new Surface(producer);
240 sp<ANativeWindow> window(surface);
241
242 // Allocate a buffer with a generation number of 0
243 ANativeWindowBuffer* buffer;
244 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700245 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
246 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700247 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
248 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
249
250 // Detach the buffer and check its generation number
251 sp<GraphicBuffer> graphicBuffer;
252 sp<Fence> fence;
253 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
254 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
255
256 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
257 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
258
259 // This should change the generation number of the GraphicBuffer
260 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
261
262 // Check that the new generation number sticks with the buffer
263 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
264 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
265 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
266 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
267}
268
Dan Stozac6f30bd2015-06-08 09:32:50 -0700269TEST_F(SurfaceTest, GetConsumerName) {
270 sp<IGraphicBufferProducer> producer;
271 sp<IGraphicBufferConsumer> consumer;
272 BufferQueue::createBufferQueue(&producer, &consumer);
273
274 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
275 consumer->consumerConnect(dummyConsumer, false);
276 consumer->setConsumerName(String8("TestConsumer"));
277
278 sp<Surface> surface = new Surface(producer);
279 sp<ANativeWindow> window(surface);
280 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
281
282 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
283}
284
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700285TEST_F(SurfaceTest, GetWideColorSupport) {
286 sp<IGraphicBufferProducer> producer;
287 sp<IGraphicBufferConsumer> consumer;
288 BufferQueue::createBufferQueue(&producer, &consumer);
289
290 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
291 consumer->consumerConnect(dummyConsumer, false);
292 consumer->setConsumerName(String8("TestConsumer"));
293
294 sp<Surface> surface = new Surface(producer);
295 sp<ANativeWindow> window(surface);
296 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
297
298 bool supported;
299 surface->getWideColorSupport(&supported);
300
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -0600301 // NOTE: This test assumes that device that supports
302 // wide-color (as indicated by BoardConfig) must also
303 // have a wide-color primary display.
304 // That assumption allows this test to cover devices
305 // that advertised a wide-color color mode without
306 // actually supporting wide-color to pass this test
307 // as well as the case of a device that does support
308 // wide-color (via BoardConfig) and has a wide-color
309 // primary display.
310 // NOT covered at this time is a device that supports
311 // wide color in the BoardConfig but does not support
312 // a wide-color color mode on the primary display.
313 ASSERT_EQ(hasWideColorDisplay, supported);
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700314}
315
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700316TEST_F(SurfaceTest, GetHdrSupport) {
317 sp<IGraphicBufferProducer> producer;
318 sp<IGraphicBufferConsumer> consumer;
319 BufferQueue::createBufferQueue(&producer, &consumer);
320
321 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
322 consumer->consumerConnect(dummyConsumer, false);
323 consumer->setConsumerName(String8("TestConsumer"));
324
325 sp<Surface> surface = new Surface(producer);
326 sp<ANativeWindow> window(surface);
327 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
328
329 bool supported;
330 status_t result = surface->getHdrSupport(&supported);
331 ASSERT_EQ(NO_ERROR, result);
332
333 // NOTE: This is not a CTS test.
334 // This test verifies that when the BoardConfig TARGET_HAS_HDR_DISPLAY
335 // is TRUE, getHdrSupport is also true.
336 // TODO: Add check for an HDR color mode on the primary display.
337 ASSERT_EQ(hasHdrDisplay, supported);
338}
339
340TEST_F(SurfaceTest, SetHdrMetadata) {
341 sp<IGraphicBufferProducer> producer;
342 sp<IGraphicBufferConsumer> consumer;
343 BufferQueue::createBufferQueue(&producer, &consumer);
344
345 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
346 consumer->consumerConnect(dummyConsumer, false);
347 consumer->setConsumerName(String8("TestConsumer"));
348
349 sp<Surface> surface = new Surface(producer);
350 sp<ANativeWindow> window(surface);
351 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
352
353 bool supported;
354 status_t result = surface->getHdrSupport(&supported);
355 ASSERT_EQ(NO_ERROR, result);
356
357 if (!hasHdrDisplay || !supported) {
358 return;
359 }
360 const android_smpte2086_metadata smpte2086 = {
361 {0.680, 0.320},
362 {0.265, 0.690},
363 {0.150, 0.060},
364 {0.3127, 0.3290},
365 100.0,
366 0.1,
367 };
368 const android_cta861_3_metadata cta861_3 = {
369 78.0,
370 62.0,
371 };
Valerie Haua82679d2018-11-21 09:31:43 -0800372
373 std::vector<uint8_t> hdr10plus;
374 hdr10plus.push_back(0xff);
375
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700376 int error = native_window_set_buffers_smpte2086_metadata(window.get(), &smpte2086);
377 ASSERT_EQ(error, NO_ERROR);
378 error = native_window_set_buffers_cta861_3_metadata(window.get(), &cta861_3);
379 ASSERT_EQ(error, NO_ERROR);
Valerie Haua82679d2018-11-21 09:31:43 -0800380 error = native_window_set_buffers_hdr10_plus_metadata(window.get(), hdr10plus.size(),
381 hdr10plus.data());
382 ASSERT_EQ(error, NO_ERROR);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700383}
384
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800385TEST_F(SurfaceTest, DynamicSetBufferCount) {
386 sp<IGraphicBufferProducer> producer;
387 sp<IGraphicBufferConsumer> consumer;
388 BufferQueue::createBufferQueue(&producer, &consumer);
389
390 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
391 consumer->consumerConnect(dummyConsumer, false);
392 consumer->setConsumerName(String8("TestConsumer"));
393
394 sp<Surface> surface = new Surface(producer);
395 sp<ANativeWindow> window(surface);
396
397 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
398 NATIVE_WINDOW_API_CPU));
399 native_window_set_buffer_count(window.get(), 4);
400
401 int fence;
402 ANativeWindowBuffer* buffer;
403 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
404 native_window_set_buffer_count(window.get(), 3);
405 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
406 native_window_set_buffer_count(window.get(), 2);
407 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
408 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
409}
410
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700411TEST_F(SurfaceTest, GetAndFlushRemovedBuffers) {
412 sp<IGraphicBufferProducer> producer;
413 sp<IGraphicBufferConsumer> consumer;
414 BufferQueue::createBufferQueue(&producer, &consumer);
415
416 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
417 consumer->consumerConnect(dummyConsumer, false);
418 consumer->setConsumerName(String8("TestConsumer"));
419
420 sp<Surface> surface = new Surface(producer);
421 sp<ANativeWindow> window(surface);
422 sp<DummyProducerListener> listener = new DummyProducerListener();
423 ASSERT_EQ(OK, surface->connect(
424 NATIVE_WINDOW_API_CPU,
425 /*listener*/listener,
426 /*reportBufferRemoval*/true));
427 const int BUFFER_COUNT = 4;
428 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
429
430 sp<GraphicBuffer> detachedBuffer;
431 sp<Fence> outFence;
432 int fences[BUFFER_COUNT];
433 ANativeWindowBuffer* buffers[BUFFER_COUNT];
434 // Allocate buffers because detachNextBuffer requires allocated buffers
435 for (int i = 0; i < BUFFER_COUNT; i++) {
436 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
437 }
438 for (int i = 0; i < BUFFER_COUNT; i++) {
439 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
440 }
441
442 // Test detached buffer is correctly reported
443 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
444 std::vector<sp<GraphicBuffer>> removedBuffers;
445 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
446 ASSERT_EQ(1u, removedBuffers.size());
447 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
448 // Test the list is flushed one getAndFlushRemovedBuffers returns
449 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
450 ASSERT_EQ(0u, removedBuffers.size());
451
452
453 // Test removed buffer list is cleanup after next dequeueBuffer call
454 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
455 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[0], &fences[0]));
456 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
457 ASSERT_EQ(0u, removedBuffers.size());
458 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[0], fences[0]));
459
460 // Test removed buffer list is cleanup after next detachNextBuffer call
461 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
462 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
463 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
464 ASSERT_EQ(1u, removedBuffers.size());
465 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
466
467 // Re-allocate buffers since all buffers are detached up to now
468 for (int i = 0; i < BUFFER_COUNT; i++) {
469 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
470 }
471 for (int i = 0; i < BUFFER_COUNT; i++) {
472 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
473 }
474
475 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
476 ASSERT_EQ(NO_ERROR, surface->attachBuffer(detachedBuffer.get()));
477 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
478 // Depends on which slot GraphicBufferProducer impl pick, the attach call might
479 // get 0 or 1 buffer removed.
480 ASSERT_LE(removedBuffers.size(), 1u);
481}
Brian Anderson3da8d272016-07-28 16:20:47 -0700482
Dan Stoza932f0082017-05-31 13:50:16 -0700483TEST_F(SurfaceTest, TestGetLastDequeueStartTime) {
484 sp<ANativeWindow> anw(mSurface);
485 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU));
486
487 ANativeWindowBuffer* buffer = nullptr;
488 int32_t fenceFd = -1;
489
490 nsecs_t before = systemTime(CLOCK_MONOTONIC);
491 anw->dequeueBuffer(anw.get(), &buffer, &fenceFd);
492 nsecs_t after = systemTime(CLOCK_MONOTONIC);
493
494 nsecs_t lastDequeueTime = mSurface->getLastDequeueStartTime();
495 ASSERT_LE(before, lastDequeueTime);
496 ASSERT_GE(after, lastDequeueTime);
497}
498
Brian Anderson3da8d272016-07-28 16:20:47 -0700499class FakeConsumer : public BnConsumerListener {
500public:
501 void onFrameAvailable(const BufferItem& /*item*/) override {}
502 void onBuffersReleased() override {}
503 void onSidebandStreamChanged() override {}
504
505 void addAndGetFrameTimestamps(
506 const NewFrameEventsEntry* newTimestamps,
507 FrameEventHistoryDelta* outDelta) override {
508 if (newTimestamps) {
509 if (mGetFrameTimestampsEnabled) {
510 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
511 "Test should set mNewFrameEntryOverride before queuing "
512 "a frame.";
513 EXPECT_EQ(newTimestamps->frameNumber,
514 mNewFrameEntryOverride.frameNumber) <<
515 "Test attempting to add NewFrameEntryOverride with "
516 "incorrect frame number.";
517 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
518 mNewFrameEntryOverride.frameNumber = 0;
519 }
520 mAddFrameTimestampsCount++;
521 mLastAddedFrameNumber = newTimestamps->frameNumber;
522 }
523 if (outDelta) {
524 mFrameEventHistory.getAndResetDelta(outDelta);
525 mGetFrameTimestampsCount++;
526 }
527 mAddAndGetFrameTimestampsCallCount++;
528 }
529
530 bool mGetFrameTimestampsEnabled = false;
531
532 ConsumerFrameEventHistory mFrameEventHistory;
533 int mAddAndGetFrameTimestampsCallCount = 0;
534 int mAddFrameTimestampsCount = 0;
535 int mGetFrameTimestampsCount = 0;
536 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
537
538 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
539};
540
541
542class FakeSurfaceComposer : public ISurfaceComposer{
543public:
544 ~FakeSurfaceComposer() override {}
545
Brian Anderson6b376712017-04-04 10:51:39 -0700546 void setSupportsPresent(bool supportsPresent) {
547 mSupportsPresent = supportsPresent;
548 }
549
Brian Anderson3da8d272016-07-28 16:20:47 -0700550 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Jorim Jaggib1e2f8d2017-06-08 15:43:59 -0700551 sp<IDisplayEventConnection> createDisplayEventConnection(ISurfaceComposer::VsyncSource)
552 override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700553 return nullptr;
554 }
555 sp<IBinder> createDisplay(const String8& /*displayName*/,
556 bool /*secure*/) override { return nullptr; }
557 void destroyDisplay(const sp<IBinder>& /*display */) override {}
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800558 std::vector<PhysicalDisplayId> getPhysicalDisplayIds() const override { return {}; }
559 sp<IBinder> getPhysicalDisplayToken(PhysicalDisplayId) const override { return nullptr; }
Brian Anderson3da8d272016-07-28 16:20:47 -0700560 void setTransactionState(const Vector<ComposerState>& /*state*/,
Marissa Wall713b63f2018-10-17 15:42:43 -0700561 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/,
chaviw273171b2018-12-26 11:46:30 -0800562 const sp<IBinder>& /*applyToken*/,
Marissa Wall17b4e452018-12-26 16:32:34 -0800563 const InputWindowCommands& /*inputWindowCommands*/,
Marissa Wall947d34e2019-03-29 14:03:53 -0700564 int64_t /*desiredPresentTime*/, const client_cache_t& /*cachedBuffer*/,
Marissa Wall3dad52d2019-03-22 14:03:19 -0700565 const std::vector<ListenerCallbacks>& /*listenerCallbacks*/) override {
566 }
Marissa Wall17b4e452018-12-26 16:32:34 -0800567
Brian Anderson3da8d272016-07-28 16:20:47 -0700568 void bootFinished() override {}
569 bool authenticateSurfaceTexture(
570 const sp<IGraphicBufferProducer>& /*surface*/) const override {
571 return false;
572 }
Brian Anderson6b376712017-04-04 10:51:39 -0700573
574 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
575 const override {
576 *outSupported = {
577 FrameEvent::REQUESTED_PRESENT,
578 FrameEvent::ACQUIRE,
579 FrameEvent::LATCH,
580 FrameEvent::FIRST_REFRESH_START,
581 FrameEvent::LAST_REFRESH_START,
582 FrameEvent::GPU_COMPOSITION_DONE,
583 FrameEvent::DEQUEUE_READY,
584 FrameEvent::RELEASE
585 };
586 if (mSupportsPresent) {
587 outSupported->push_back(
588 FrameEvent::DISPLAY_PRESENT);
589 }
590 return NO_ERROR;
591 }
592
Brian Anderson3da8d272016-07-28 16:20:47 -0700593 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
594 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
595 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
596 status_t getDisplayStats(const sp<IBinder>& /*display*/,
597 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
598 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
599 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
600 override {
601 return NO_ERROR;
602 }
603 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700604 Vector<ColorMode>* /*outColorModes*/) override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700605 return NO_ERROR;
606 }
Daniel Solomon42d04562019-01-20 21:03:19 -0800607 status_t getDisplayNativePrimaries(const sp<IBinder>& /*display*/,
608 ui::DisplayPrimaries& /*primaries*/) override {
609 return NO_ERROR;
610 }
Peiyong Lina52f0292018-03-14 17:26:31 -0700611 ColorMode getActiveColorMode(const sp<IBinder>& /*display*/)
Brian Anderson3da8d272016-07-28 16:20:47 -0700612 override {
Peiyong Lina52f0292018-03-14 17:26:31 -0700613 return ColorMode::NATIVE;
Brian Anderson3da8d272016-07-28 16:20:47 -0700614 }
615 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700616 ColorMode /*colorMode*/) override { return NO_ERROR; }
Peiyong Lin0e003c92018-09-17 11:09:51 -0700617 status_t captureScreen(const sp<IBinder>& /*display*/, sp<GraphicBuffer>* /*outBuffer*/,
Robert Carr108b2c72019-04-02 16:32:58 -0700618 bool& /* outCapturedSecureLayers */,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700619 const ui::Dataspace /*reqDataspace*/,
620 const ui::PixelFormat /*reqPixelFormat*/, Rect /*sourceCrop*/,
621 uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrfa8855f2019-02-19 10:05:00 -0800622 bool /*useIdentityTransform*/, Rotation /*rotation*/,
623 bool /*captureSecureLayers*/) override {
Peiyong Lin0e003c92018-09-17 11:09:51 -0700624 return NO_ERROR;
625 }
chaviw93df2ea2019-04-30 16:45:12 -0700626 status_t captureScreen(uint64_t /*displayOrLayerStack*/, ui::Dataspace* /*outDataspace*/,
627 sp<GraphicBuffer>* /*outBuffer*/) override {
628 return NO_ERROR;
629 }
Robert Carr866455f2019-04-02 16:28:26 -0700630 virtual status_t captureLayers(
631 const sp<IBinder>& /*parentHandle*/, sp<GraphicBuffer>* /*outBuffer*/,
632 const ui::Dataspace /*reqDataspace*/, const ui::PixelFormat /*reqPixelFormat*/,
633 const Rect& /*sourceCrop*/,
634 const std::unordered_set<sp<IBinder>,
635 ISurfaceComposer::SpHash<IBinder>>& /*excludeHandles*/,
636 float /*frameScale*/, bool /*childrenOnly*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700637 return NO_ERROR;
638 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700639 status_t clearAnimationFrameStats() override { return NO_ERROR; }
640 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
641 return NO_ERROR;
642 }
643 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
644 HdrCapabilities* /*outCapabilities*/) const override {
645 return NO_ERROR;
646 }
647 status_t enableVSyncInjections(bool /*enable*/) override {
648 return NO_ERROR;
649 }
650 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800651 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
652 return NO_ERROR;
653 }
Peiyong Linc6780972018-10-28 15:24:08 -0700654 status_t getCompositionPreference(
655 ui::Dataspace* /*outDefaultDataspace*/, ui::PixelFormat* /*outDefaultPixelFormat*/,
656 ui::Dataspace* /*outWideColorGamutDataspace*/,
657 ui::PixelFormat* /*outWideColorGamutPixelFormat*/) const override {
Peiyong Lin0256f722018-08-31 15:45:10 -0700658 return NO_ERROR;
659 }
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700660 status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& /*display*/,
661 ui::PixelFormat* /*outFormat*/,
662 ui::Dataspace* /*outDataspace*/,
663 uint8_t* /*outComponentMask*/) const override {
664 return NO_ERROR;
665 }
Kevin DuBois74e53772018-11-19 10:52:38 -0800666 status_t setDisplayContentSamplingEnabled(const sp<IBinder>& /*display*/, bool /*enable*/,
667 uint8_t /*componentMask*/,
668 uint64_t /*maxFrames*/) const override {
669 return NO_ERROR;
670 }
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700671 status_t getDisplayedContentSample(const sp<IBinder>& /*display*/, uint64_t /*maxFrames*/,
672 uint64_t /*timestamp*/,
673 DisplayedFrameStats* /*outStats*/) const override {
674 return NO_ERROR;
675 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700676
Peiyong Lin3c2791e2019-01-14 17:05:18 -0800677 status_t getColorManagement(bool* /*outGetColorManagement*/) const override { return NO_ERROR; }
678 status_t getProtectedContentSupport(bool* /*outSupported*/) const override { return NO_ERROR; }
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700679
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800680 status_t isWideColorDisplay(const sp<IBinder>&, bool*) const override { return NO_ERROR; }
Dan Gittik57e63c52019-01-18 16:37:54 +0000681 status_t getDisplayBrightnessSupport(const sp<IBinder>& /*displayToken*/,
682 bool* /*outSupport*/) const override {
683 return NO_ERROR;
684 }
685 status_t setDisplayBrightness(const sp<IBinder>& /*displayToken*/,
686 float /*brightness*/) const override {
687 return NO_ERROR;
688 }
Marissa Wallebc2c052019-01-16 19:16:55 -0800689
Dan Stoza84ab9372018-12-17 15:27:57 -0800690 status_t addRegionSamplingListener(const Rect& /*samplingArea*/,
691 const sp<IBinder>& /*stopLayerHandle*/,
692 const sp<IRegionSamplingListener>& /*listener*/) override {
693 return NO_ERROR;
694 }
695 status_t removeRegionSamplingListener(
696 const sp<IRegionSamplingListener>& /*listener*/) override {
697 return NO_ERROR;
698 }
Ady Abraham838de062019-02-04 10:24:03 -0800699 status_t setAllowedDisplayConfigs(const sp<IBinder>& /*displayToken*/,
700 const std::vector<int32_t>& /*allowedConfigs*/) override {
701 return NO_ERROR;
702 }
Ady Abrahamd9b3ea62019-02-26 14:08:03 -0800703 status_t getAllowedDisplayConfigs(const sp<IBinder>& /*displayToken*/,
704 std::vector<int32_t>* /*outAllowedConfigs*/) override {
705 return NO_ERROR;
706 }
Dan Stoza84ab9372018-12-17 15:27:57 -0800707
Brian Anderson3da8d272016-07-28 16:20:47 -0700708protected:
709 IBinder* onAsBinder() override { return nullptr; }
710
711private:
712 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700713};
714
715class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
716public:
Chih-Hung Hsiehaaf62162018-12-20 15:45:04 -0800717 explicit FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap) : mFenceMap(fenceMap) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700718
719 ~FakeProducerFrameEventHistory() {}
720
721 void updateAcquireFence(uint64_t frameNumber,
722 std::shared_ptr<FenceTime>&& acquire) override {
723 // Verify the acquire fence being added isn't the one from the consumer.
724 EXPECT_NE(mConsumerAcquireFence, acquire);
725 // Override the fence, so we can verify this was called by the
726 // producer after the frame is queued.
727 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
728 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
729 }
730
731 void setAcquireFenceOverride(
732 const std::shared_ptr<FenceTime>& acquireFenceOverride,
733 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
734 mAcquireFenceOverride = acquireFenceOverride;
735 mConsumerAcquireFence = consumerAcquireFence;
736 }
737
738protected:
739 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
740 const override {
741 return mFenceMap->createFenceTimeForTest(fence);
742 }
743
744 FenceToFenceTimeMap* mFenceMap{nullptr};
745
746 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
747 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
748};
749
750
751class TestSurface : public Surface {
752public:
753 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
754 FenceToFenceTimeMap* fenceMap)
755 : Surface(bufferProducer),
756 mFakeSurfaceComposer(new FakeSurfaceComposer) {
757 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
758 mFrameEventHistory.reset(mFakeFrameEventHistory);
759 }
760
761 ~TestSurface() override {}
762
763 sp<ISurfaceComposer> composerService() const override {
764 return mFakeSurfaceComposer;
765 }
766
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800767 nsecs_t now() const override {
768 return mNow;
769 }
770
771 void setNow(nsecs_t now) {
772 mNow = now;
773 }
774
Brian Anderson3da8d272016-07-28 16:20:47 -0700775public:
776 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800777 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700778
779 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
780 // but this raw pointer gives access to test functionality.
781 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
782};
783
784
Brian Andersond0010582017-03-07 13:20:31 -0800785class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700786protected:
787 struct FenceAndFenceTime {
788 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
789 : mFence(new Fence),
790 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
791 sp<Fence> mFence { nullptr };
792 std::shared_ptr<FenceTime> mFenceTime { nullptr };
793 };
794
795 struct RefreshEvents {
796 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800797 : mFenceMap(fenceMap),
798 kCompositorTiming(
799 {refreshStart, refreshStart + 1, refreshStart + 2 }),
800 kStartTime(refreshStart + 3),
801 kGpuCompositionDoneTime(refreshStart + 4),
802 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700803
804 void signalPostCompositeFences() {
805 mFenceMap.signalAllForTest(
806 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
807 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
808 }
809
810 FenceToFenceTimeMap& mFenceMap;
811
812 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
813 FenceAndFenceTime mPresent { mFenceMap };
814
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800815 const CompositorTiming kCompositorTiming;
816
Brian Anderson3da8d272016-07-28 16:20:47 -0700817 const nsecs_t kStartTime;
818 const nsecs_t kGpuCompositionDoneTime;
819 const nsecs_t kPresentTime;
820 };
821
822 struct FrameEvents {
823 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
824 : mFenceMap(fenceMap),
825 kPostedTime(frameStartTime + 100),
826 kRequestedPresentTime(frameStartTime + 200),
827 kProducerAcquireTime(frameStartTime + 300),
828 kConsumerAcquireTime(frameStartTime + 301),
829 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700830 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700831 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700832 mRefreshes {
833 { mFenceMap, frameStartTime + 410 },
834 { mFenceMap, frameStartTime + 420 },
835 { mFenceMap, frameStartTime + 430 } } {}
836
837 void signalQueueFences() {
838 mFenceMap.signalAllForTest(
839 mAcquireConsumer.mFence, kConsumerAcquireTime);
840 mFenceMap.signalAllForTest(
841 mAcquireProducer.mFence, kProducerAcquireTime);
842 }
843
844 void signalRefreshFences() {
845 for (auto& re : mRefreshes) {
846 re.signalPostCompositeFences();
847 }
848 }
849
850 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700851 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
852 }
853
854 FenceToFenceTimeMap& mFenceMap;
855
856 FenceAndFenceTime mAcquireConsumer { mFenceMap };
857 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700858 FenceAndFenceTime mRelease { mFenceMap };
859
860 const nsecs_t kPostedTime;
861 const nsecs_t kRequestedPresentTime;
862 const nsecs_t kProducerAcquireTime;
863 const nsecs_t kConsumerAcquireTime;
864 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700865 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700866 const nsecs_t kReleaseTime;
867
868 RefreshEvents mRefreshes[3];
869 };
870
Brian Andersond0010582017-03-07 13:20:31 -0800871 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700872
873 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700874 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
875 mFakeConsumer = new FakeConsumer;
876 mCfeh = &mFakeConsumer->mFrameEventHistory;
877 mConsumer->consumerConnect(mFakeConsumer, false);
878 mConsumer->setConsumerName(String8("TestConsumer"));
879 mSurface = new TestSurface(mProducer, &mFenceMap);
880 mWindow = mSurface;
881
882 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
883 NATIVE_WINDOW_API_CPU));
884 native_window_set_buffer_count(mWindow.get(), 4);
885 }
886
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800887 void disableFrameTimestamps() {
888 mFakeConsumer->mGetFrameTimestampsEnabled = false;
889 native_window_enable_frame_timestamps(mWindow.get(), 0);
890 mFrameTimestampsEnabled = false;
891 }
892
Brian Anderson3da8d272016-07-28 16:20:47 -0700893 void enableFrameTimestamps() {
894 mFakeConsumer->mGetFrameTimestampsEnabled = true;
895 native_window_enable_frame_timestamps(mWindow.get(), 1);
896 mFrameTimestampsEnabled = true;
897 }
898
Brian Anderson1049d1d2016-12-16 17:25:57 -0800899 int getAllFrameTimestamps(uint64_t frameId) {
900 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700901 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
902 &outFirstRefreshStartTime, &outLastRefreshStartTime,
903 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700904 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700905 }
906
Brian Anderson3da8d272016-07-28 16:20:47 -0700907 void resetTimestamps() {
908 outRequestedPresentTime = -1;
909 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700910 outLatchTime = -1;
911 outFirstRefreshStartTime = -1;
912 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700913 outGpuCompositionDoneTime = -1;
914 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700915 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700916 outReleaseTime = -1;
917 }
918
Brian Anderson1049d1d2016-12-16 17:25:57 -0800919 uint64_t getNextFrameId() {
920 uint64_t frameId = -1;
921 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
922 EXPECT_EQ(status, NO_ERROR);
923 return frameId;
924 }
925
Brian Anderson3da8d272016-07-28 16:20:47 -0700926 void dequeueAndQueue(uint64_t frameIndex) {
927 int fence = -1;
928 ANativeWindowBuffer* buffer = nullptr;
929 ASSERT_EQ(NO_ERROR,
930 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
931
932 int oldAddFrameTimestampsCount =
933 mFakeConsumer->mAddFrameTimestampsCount;
934
935 FrameEvents* frame = &mFrames[frameIndex];
936 uint64_t frameNumber = frameIndex + 1;
937
938 NewFrameEventsEntry fe;
939 fe.frameNumber = frameNumber;
940 fe.postedTime = frame->kPostedTime;
941 fe.requestedPresentTime = frame->kRequestedPresentTime;
942 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
943 mFakeConsumer->mNewFrameEntryOverride = fe;
944
945 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
946 frame->mAcquireProducer.mFenceTime,
947 frame->mAcquireConsumer.mFenceTime);
948
949 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
950
951 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
952
953 EXPECT_EQ(
954 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
955 mFakeConsumer->mAddFrameTimestampsCount);
956 }
957
958 void addFrameEvents(
959 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
960 FrameEvents* oldFrame =
961 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
962 FrameEvents* newFrame = &mFrames[iNewFrame];
963
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800964 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700965 uint64_t nNewFrame = iNewFrame + 1;
966
Brian Anderson4e606e32017-03-16 15:34:57 -0700967 // Latch, Composite, and Release the frames in a plausible order.
968 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700969 // that's okay for the purposes of this test.
970 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
971
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700972 // Composite the previous frame one more time, which helps verify
973 // LastRefresh is updated properly.
974 if (oldFrame != nullptr) {
975 mCfeh->addPreComposition(nOldFrame,
976 oldFrame->mRefreshes[2].kStartTime);
977 gpuDoneFenceTime = gpuComposited ?
978 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
979 FenceTime::NO_FENCE;
980 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800981 oldFrame->mRefreshes[2].mPresent.mFenceTime,
982 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700983 }
984
985 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700986 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
987
988 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
989 gpuDoneFenceTime = gpuComposited ?
990 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
991 FenceTime::NO_FENCE;
992 // HWC2 releases the previous buffer after a new latch just before
993 // calling postComposition.
994 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700995 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700996 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
997 }
998 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800999 newFrame->mRefreshes[0].mPresent.mFenceTime,
1000 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -07001001
Brian Anderson3da8d272016-07-28 16:20:47 -07001002 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
1003 gpuDoneFenceTime = gpuComposited ?
1004 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
1005 FenceTime::NO_FENCE;
1006 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001007 newFrame->mRefreshes[1].mPresent.mFenceTime,
1008 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -07001009 }
1010
Brian Anderson3da8d272016-07-28 16:20:47 -07001011 sp<IGraphicBufferProducer> mProducer;
1012 sp<IGraphicBufferConsumer> mConsumer;
1013 sp<FakeConsumer> mFakeConsumer;
1014 ConsumerFrameEventHistory* mCfeh;
1015 sp<TestSurface> mSurface;
1016 sp<ANativeWindow> mWindow;
1017
1018 FenceToFenceTimeMap mFenceMap;
1019
1020 bool mFrameTimestampsEnabled = false;
1021
1022 int64_t outRequestedPresentTime = -1;
1023 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001024 int64_t outLatchTime = -1;
1025 int64_t outFirstRefreshStartTime = -1;
1026 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001027 int64_t outGpuCompositionDoneTime = -1;
1028 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001029 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001030 int64_t outReleaseTime = -1;
1031
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001032 FrameEvents mFrames[3] {
1033 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -07001034};
1035
1036
1037// This test verifies that the frame timestamps are not retrieved when not
1038// explicitly enabled via native_window_enable_frame_timestamps.
1039// We want to check this to make sure there's no overhead for users
1040// that don't need the timestamp information.
1041TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
1042 int fence;
1043 ANativeWindowBuffer* buffer;
1044
1045 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1046 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1047
Brian Anderson1049d1d2016-12-16 17:25:57 -08001048 const uint64_t fId = getNextFrameId();
1049
Brian Anderson3da8d272016-07-28 16:20:47 -07001050 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
1051 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1052 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1053 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1054
1055 // Verify the producer doesn't get frame timestamps piggybacked on queue.
1056 // It is okay that frame timestamps are added in the consumer since it is
1057 // still needed for SurfaceFlinger dumps.
1058 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1059 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1060 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1061
1062 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001063 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -07001064 EXPECT_EQ(INVALID_OPERATION, result);
1065 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001066
1067 // Verify compositor timing query fails.
1068 nsecs_t compositeDeadline = 0;
1069 nsecs_t compositeInterval = 0;
1070 nsecs_t compositeToPresentLatency = 0;
1071 result = native_window_get_compositor_timing(mWindow.get(),
1072 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1073 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001074}
1075
1076// This test verifies that the frame timestamps are retrieved if explicitly
1077// enabled via native_window_enable_frame_timestamps.
1078TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001079 CompositorTiming initialCompositorTiming {
1080 1000000000, // 1s deadline
1081 16666667, // 16ms interval
1082 50000000, // 50ms present latency
1083 };
1084 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1085
Brian Anderson3da8d272016-07-28 16:20:47 -07001086 enableFrameTimestamps();
1087
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001088 // Verify the compositor timing query gets the initial compositor values
1089 // after timststamps are enabled; even before the first frame is queued
1090 // or dequeued.
1091 nsecs_t compositeDeadline = 0;
1092 nsecs_t compositeInterval = 0;
1093 nsecs_t compositeToPresentLatency = 0;
1094 mSurface->setNow(initialCompositorTiming.deadline - 1);
1095 int result = native_window_get_compositor_timing(mWindow.get(),
1096 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1097 EXPECT_EQ(NO_ERROR, result);
1098 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1099 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1100 EXPECT_EQ(initialCompositorTiming.presentLatency,
1101 compositeToPresentLatency);
1102
Brian Anderson3da8d272016-07-28 16:20:47 -07001103 int fence;
1104 ANativeWindowBuffer* buffer;
1105
1106 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001107 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001108
Brian Anderson1049d1d2016-12-16 17:25:57 -08001109 const uint64_t fId1 = getNextFrameId();
1110
Brian Anderson3da8d272016-07-28 16:20:47 -07001111 // Verify getFrameTimestamps is piggybacked on dequeue.
1112 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1113 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001114 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001115
1116 NewFrameEventsEntry f1;
1117 f1.frameNumber = 1;
1118 f1.postedTime = mFrames[0].kPostedTime;
1119 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1120 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1121 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1122 mFrames[0].mAcquireProducer.mFenceTime,
1123 mFrames[0].mAcquireConsumer.mFenceTime);
1124 mFakeConsumer->mNewFrameEntryOverride = f1;
1125 mFrames[0].signalQueueFences();
1126
1127 // Verify getFrameTimestamps is piggybacked on queue.
1128 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1129 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1130 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001131 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001132
1133 // Verify queries for timestamps that the producer doesn't know about
1134 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001135 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001136 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001137 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001138}
1139
Brian Anderson6b376712017-04-04 10:51:39 -07001140TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1141 bool displayPresentSupported = true;
1142 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1143
1144 // Verify supported bits are forwarded.
1145 int supportsPresent = -1;
1146 mWindow.get()->query(mWindow.get(),
1147 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1148 EXPECT_EQ(displayPresentSupported, supportsPresent);
1149}
1150
1151TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1152 bool displayPresentSupported = false;
1153 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1154
1155 // Verify supported bits are forwarded.
1156 int supportsPresent = -1;
1157 mWindow.get()->query(mWindow.get(),
1158 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1159 EXPECT_EQ(displayPresentSupported, supportsPresent);
1160}
1161
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001162TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1163 nsecs_t phase = 4000;
1164 nsecs_t interval = 1000;
1165
1166 // Timestamp in previous interval.
1167 nsecs_t timestamp = 3500;
1168 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1169 timestamp, phase, interval));
1170
1171 // Timestamp in next interval.
1172 timestamp = 4500;
1173 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1174 timestamp, phase, interval));
1175
1176 // Timestamp multiple intervals before.
1177 timestamp = 2500;
1178 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1179 timestamp, phase, interval));
1180
1181 // Timestamp multiple intervals after.
1182 timestamp = 6500;
1183 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1184 timestamp, phase, interval));
1185
1186 // Timestamp on previous interval.
1187 timestamp = 3000;
1188 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1189 timestamp, phase, interval));
1190
1191 // Timestamp on next interval.
1192 timestamp = 5000;
1193 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1194 timestamp, phase, interval));
1195
1196 // Timestamp equal to phase.
1197 timestamp = 4000;
1198 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1199 timestamp, phase, interval));
1200}
1201
1202// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1203// if the number of intervals elapsed is internally stored in an int.
1204TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1205 nsecs_t phase = 0;
1206 nsecs_t interval = 4000;
1207 nsecs_t big_timestamp = 8635916564000;
1208 int32_t intervals = big_timestamp / interval;
1209
1210 EXPECT_LT(intervals, 0);
1211 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1212 big_timestamp, phase, interval));
1213 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1214 big_timestamp, big_timestamp, interval));
1215}
1216
1217// This verifies the compositor timing is updated by refresh events
1218// and piggy backed on a queue, dequeue, and enabling of timestamps..
1219TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1220 CompositorTiming initialCompositorTiming {
1221 1000000000, // 1s deadline
1222 16666667, // 16ms interval
1223 50000000, // 50ms present latency
1224 };
1225 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1226
1227 enableFrameTimestamps();
1228
1229 // We get the initial values before any frames are submitted.
1230 nsecs_t compositeDeadline = 0;
1231 nsecs_t compositeInterval = 0;
1232 nsecs_t compositeToPresentLatency = 0;
1233 mSurface->setNow(initialCompositorTiming.deadline - 1);
1234 int result = native_window_get_compositor_timing(mWindow.get(),
1235 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1236 EXPECT_EQ(NO_ERROR, result);
1237 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1238 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1239 EXPECT_EQ(initialCompositorTiming.presentLatency,
1240 compositeToPresentLatency);
1241
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001242 dequeueAndQueue(0);
1243 addFrameEvents(true, NO_FRAME_INDEX, 0);
1244
1245 // Still get the initial values because the frame events for frame 0
1246 // didn't get a chance to piggyback on a queue or dequeue yet.
1247 result = native_window_get_compositor_timing(mWindow.get(),
1248 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1249 EXPECT_EQ(NO_ERROR, result);
1250 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1251 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1252 EXPECT_EQ(initialCompositorTiming.presentLatency,
1253 compositeToPresentLatency);
1254
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001255 dequeueAndQueue(1);
1256 addFrameEvents(true, 0, 1);
1257
1258 // Now expect the composite values associated with frame 1.
1259 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1260 result = native_window_get_compositor_timing(mWindow.get(),
1261 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1262 EXPECT_EQ(NO_ERROR, result);
1263 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1264 compositeDeadline);
1265 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1266 compositeInterval);
1267 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1268 compositeToPresentLatency);
1269
1270 dequeueAndQueue(2);
1271 addFrameEvents(true, 1, 2);
1272
1273 // Now expect the composite values associated with frame 2.
1274 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1275 result = native_window_get_compositor_timing(mWindow.get(),
1276 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1277 EXPECT_EQ(NO_ERROR, result);
1278 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1279 compositeDeadline);
1280 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1281 compositeInterval);
1282 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1283 compositeToPresentLatency);
1284
1285 // Re-enabling frame timestamps should get the latest values.
1286 disableFrameTimestamps();
1287 enableFrameTimestamps();
1288
1289 // Now expect the composite values associated with frame 3.
1290 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1291 result = native_window_get_compositor_timing(mWindow.get(),
1292 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1293 EXPECT_EQ(NO_ERROR, result);
1294 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1295 compositeDeadline);
1296 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1297 compositeInterval);
1298 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1299 compositeToPresentLatency);
1300}
1301
1302// This verifies the compositor deadline properly snaps to the the next
1303// deadline based on the current time.
1304TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1305 CompositorTiming initialCompositorTiming {
1306 1000000000, // 1s deadline
1307 16666667, // 16ms interval
1308 50000000, // 50ms present latency
1309 };
1310 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1311
1312 enableFrameTimestamps();
1313
1314 nsecs_t compositeDeadline = 0;
1315 nsecs_t compositeInterval = 0;
1316 nsecs_t compositeToPresentLatency = 0;
1317
1318 // A "now" just before the deadline snaps to the deadline.
1319 mSurface->setNow(initialCompositorTiming.deadline - 1);
1320 int result = native_window_get_compositor_timing(mWindow.get(),
1321 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1322 EXPECT_EQ(NO_ERROR, result);
1323 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1324 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1325 EXPECT_EQ(expectedDeadline, compositeDeadline);
1326
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001327 dequeueAndQueue(0);
1328 addFrameEvents(true, NO_FRAME_INDEX, 0);
1329
1330 // A "now" just after the deadline snaps properly.
1331 mSurface->setNow(initialCompositorTiming.deadline + 1);
1332 result = native_window_get_compositor_timing(mWindow.get(),
1333 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1334 EXPECT_EQ(NO_ERROR, result);
1335 expectedDeadline =
1336 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1337 EXPECT_EQ(expectedDeadline, compositeDeadline);
1338
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001339 dequeueAndQueue(1);
1340 addFrameEvents(true, 0, 1);
1341
1342 // A "now" just after the next interval snaps properly.
1343 mSurface->setNow(
1344 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1345 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1346 result = native_window_get_compositor_timing(mWindow.get(),
1347 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1348 EXPECT_EQ(NO_ERROR, result);
1349 expectedDeadline =
1350 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1351 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1352 EXPECT_EQ(expectedDeadline, compositeDeadline);
1353
1354 dequeueAndQueue(2);
1355 addFrameEvents(true, 1, 2);
1356
1357 // A "now" over 1 interval before the deadline snaps properly.
1358 mSurface->setNow(
1359 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1360 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1361 result = native_window_get_compositor_timing(mWindow.get(),
1362 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1363 EXPECT_EQ(NO_ERROR, result);
1364 expectedDeadline =
1365 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1366 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1367 EXPECT_EQ(expectedDeadline, compositeDeadline);
1368
1369 // Re-enabling frame timestamps should get the latest values.
1370 disableFrameTimestamps();
1371 enableFrameTimestamps();
1372
1373 // A "now" over 2 intervals before the deadline snaps properly.
1374 mSurface->setNow(
1375 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1376 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1377 result = native_window_get_compositor_timing(mWindow.get(),
1378 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1379 EXPECT_EQ(NO_ERROR, result);
1380 expectedDeadline =
1381 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1382 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1383 EXPECT_EQ(expectedDeadline, compositeDeadline);
1384}
1385
Brian Anderson1049d1d2016-12-16 17:25:57 -08001386// This verifies the timestamps recorded in the consumer's
1387// FrameTimestampsHistory are properly retrieved by the producer for the
1388// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001389TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1390 enableFrameTimestamps();
1391
Brian Anderson1049d1d2016-12-16 17:25:57 -08001392 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001393 dequeueAndQueue(0);
1394 mFrames[0].signalQueueFences();
1395
Brian Anderson1049d1d2016-12-16 17:25:57 -08001396 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001397 dequeueAndQueue(1);
1398 mFrames[1].signalQueueFences();
1399
1400 addFrameEvents(true, NO_FRAME_INDEX, 0);
1401 mFrames[0].signalRefreshFences();
1402 addFrameEvents(true, 0, 1);
1403 mFrames[0].signalReleaseFences();
1404 mFrames[1].signalRefreshFences();
1405
1406 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001407 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001408 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001409 EXPECT_EQ(NO_ERROR, result);
1410 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1411 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001412 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1413 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1414 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001415 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1416 outGpuCompositionDoneTime);
1417 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001418 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001419 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1420
1421 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001422 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001423 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001424 EXPECT_EQ(NO_ERROR, result);
1425 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1426 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001427 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1428 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1429 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001430 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1431 outGpuCompositionDoneTime);
1432 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001433 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1434 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001435}
1436
1437// This test verifies the acquire fence recorded by the consumer is not sent
1438// back to the producer and the producer saves its own fence.
1439TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1440 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001441
Brian Anderson3da8d272016-07-28 16:20:47 -07001442 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001443 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001444 dequeueAndQueue(0);
1445
1446 // Verify queue-related timestamps for f1 are available immediately in the
1447 // producer without asking the consumer again, even before signaling the
1448 // acquire fence.
1449 resetTimestamps();
1450 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001451 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001452 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001453 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001454 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1455 EXPECT_EQ(NO_ERROR, result);
1456 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001457 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001458
1459 // Signal acquire fences. Verify a sync call still isn't necessary.
1460 mFrames[0].signalQueueFences();
1461
1462 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001463 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001464 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001465 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001466 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1467 EXPECT_EQ(NO_ERROR, result);
1468 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1469 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1470
1471 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001472 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001473 dequeueAndQueue(1);
1474
1475 // Verify queue-related timestamps for f2 are available immediately in the
1476 // producer without asking the consumer again, even before signaling the
1477 // acquire fence.
1478 resetTimestamps();
1479 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001480 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001481 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001482 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001483 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1484 EXPECT_EQ(NO_ERROR, result);
1485 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001486 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001487
1488 // Signal acquire fences. Verify a sync call still isn't necessary.
1489 mFrames[1].signalQueueFences();
1490
1491 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001492 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001493 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001494 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001495 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1496 EXPECT_EQ(NO_ERROR, result);
1497 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1498 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1499}
1500
1501TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1502 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001503
1504 // Dequeue and queue frame 1.
1505 dequeueAndQueue(0);
1506 mFrames[0].signalQueueFences();
1507
1508 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001509 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001510 dequeueAndQueue(1);
1511 mFrames[1].signalQueueFences();
1512
1513 addFrameEvents(true, NO_FRAME_INDEX, 0);
1514 mFrames[0].signalRefreshFences();
1515 addFrameEvents(true, 0, 1);
1516 mFrames[0].signalReleaseFences();
1517 mFrames[1].signalRefreshFences();
1518
1519 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001520 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001521 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001522 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1523 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001524 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001525 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1526}
1527
1528// This test verifies that fences can signal and update timestamps producer
1529// side without an additional sync call to the consumer.
1530TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1531 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001532
1533 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001534 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001535 dequeueAndQueue(0);
1536 mFrames[0].signalQueueFences();
1537
1538 // Dequeue and queue frame 2.
1539 dequeueAndQueue(1);
1540 mFrames[1].signalQueueFences();
1541
1542 addFrameEvents(true, NO_FRAME_INDEX, 0);
1543 addFrameEvents(true, 0, 1);
1544
1545 // Verify available timestamps are correct for frame 1, before any
1546 // fence has been signaled.
1547 // Note: A sync call is necessary here since the events triggered by
1548 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001549 resetTimestamps();
1550 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001551 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001552 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1553 EXPECT_EQ(NO_ERROR, result);
1554 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1555 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001556 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1557 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1558 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001559 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1560 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001561 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001562 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001563
1564 // Verify available timestamps are correct for frame 1 again, before any
1565 // fence has been signaled.
1566 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001567 resetTimestamps();
1568 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001569 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001570 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1571 EXPECT_EQ(NO_ERROR, result);
1572 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1573 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001574 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1575 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1576 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001577 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1578 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001579 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001580 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001581
1582 // Signal the fences for frame 1.
1583 mFrames[0].signalRefreshFences();
1584 mFrames[0].signalReleaseFences();
1585
1586 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001587 resetTimestamps();
1588 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001589 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001590 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1591 EXPECT_EQ(NO_ERROR, result);
1592 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1593 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001594 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1595 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1596 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001597 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1598 outGpuCompositionDoneTime);
1599 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001600 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001601 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1602}
1603
1604// This test verifies that if the frame wasn't GPU composited but has a refresh
1605// event a sync call isn't made to get the GPU composite done time since it will
1606// never exist.
1607TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1608 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001609
Brian Anderson3da8d272016-07-28 16:20:47 -07001610 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001611 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001612 dequeueAndQueue(0);
1613 mFrames[0].signalQueueFences();
1614
1615 // Dequeue and queue frame 2.
1616 dequeueAndQueue(1);
1617 mFrames[1].signalQueueFences();
1618
1619 addFrameEvents(false, NO_FRAME_INDEX, 0);
1620 addFrameEvents(false, 0, 1);
1621
1622 // Verify available timestamps are correct for frame 1, before any
1623 // fence has been signaled.
1624 // Note: A sync call is necessary here since the events triggered by
1625 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1626 resetTimestamps();
1627 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001628 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001629 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1630 EXPECT_EQ(NO_ERROR, result);
1631 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1632 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001633 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1634 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1635 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001636 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1637 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001638 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001639 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001640
1641 // Signal the fences for frame 1.
1642 mFrames[0].signalRefreshFences();
1643 mFrames[0].signalReleaseFences();
1644
1645 // Verify all timestamps, except GPU composition, are available without a
1646 // sync call.
1647 resetTimestamps();
1648 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001649 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001650 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1651 EXPECT_EQ(NO_ERROR, result);
1652 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1653 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001654 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1655 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1656 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001657 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001658 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001659 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001660 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1661}
1662
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001663// This test verifies that if the certain timestamps can't possibly exist for
1664// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001665TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001666 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001667
1668 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001669 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001670 dequeueAndQueue(0);
1671 mFrames[0].signalQueueFences();
1672
1673 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001674 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001675 dequeueAndQueue(1);
1676 mFrames[1].signalQueueFences();
1677
1678 addFrameEvents(false, NO_FRAME_INDEX, 0);
1679 addFrameEvents(false, 0, 1);
1680
1681 // Verify available timestamps are correct for frame 1, before any
1682 // fence has been signaled.
1683 // Note: A sync call is necessary here since the events triggered by
1684 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001685 resetTimestamps();
1686 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001687 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001688 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1689 EXPECT_EQ(NO_ERROR, result);
1690 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1691 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001692 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1693 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1694 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001695 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1696 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001697 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001698 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001699
1700 mFrames[0].signalRefreshFences();
1701 mFrames[0].signalReleaseFences();
1702 mFrames[1].signalRefreshFences();
1703
Brian Anderson1049d1d2016-12-16 17:25:57 -08001704 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001705 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001706 // available, a sync call should not occur because it's not possible for f2
1707 // to encounter the final value for those events until another frame is
1708 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001709 resetTimestamps();
1710 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001711 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001712 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1713 EXPECT_EQ(NO_ERROR, result);
1714 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1715 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001716 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1717 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1718 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001719 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001720 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001721 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1722 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001723}
1724
Brian Anderson6b376712017-04-04 10:51:39 -07001725// This test verifies there are no sync calls for present times
1726// when they aren't supported and that an error is returned.
1727
1728TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1729 enableFrameTimestamps();
1730 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1731
1732 // Dequeue and queue frame 1.
1733 const uint64_t fId1 = getNextFrameId();
1734 dequeueAndQueue(0);
1735
1736 // Verify a query for the Present times do not trigger a sync call if they
1737 // are not supported.
1738 resetTimestamps();
1739 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1740 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1741 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1742 &outDisplayPresentTime, nullptr, nullptr);
1743 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1744 EXPECT_EQ(BAD_VALUE, result);
1745 EXPECT_EQ(-1, outDisplayPresentTime);
1746}
1747
Dan Stoza932f0082017-05-31 13:50:16 -07001748} // namespace android