blob: d3708586f55eacf90ea626869666384424e7eb6e [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; }
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700551 sp<IDisplayEventConnection> createDisplayEventConnection(
552 ISurfaceComposer::VsyncSource, ISurfaceComposer::ConfigChanged) 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 }
Ady Abraham8532d012019-05-08 14:50:56 -0700707 status_t notifyPowerHint(int32_t /*hintId*/) override { return NO_ERROR; }
Dan Stoza84ab9372018-12-17 15:27:57 -0800708
Brian Anderson3da8d272016-07-28 16:20:47 -0700709protected:
710 IBinder* onAsBinder() override { return nullptr; }
711
712private:
713 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700714};
715
716class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
717public:
Chih-Hung Hsiehaaf62162018-12-20 15:45:04 -0800718 explicit FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap) : mFenceMap(fenceMap) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700719
720 ~FakeProducerFrameEventHistory() {}
721
722 void updateAcquireFence(uint64_t frameNumber,
723 std::shared_ptr<FenceTime>&& acquire) override {
724 // Verify the acquire fence being added isn't the one from the consumer.
725 EXPECT_NE(mConsumerAcquireFence, acquire);
726 // Override the fence, so we can verify this was called by the
727 // producer after the frame is queued.
728 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
729 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
730 }
731
732 void setAcquireFenceOverride(
733 const std::shared_ptr<FenceTime>& acquireFenceOverride,
734 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
735 mAcquireFenceOverride = acquireFenceOverride;
736 mConsumerAcquireFence = consumerAcquireFence;
737 }
738
739protected:
740 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
741 const override {
742 return mFenceMap->createFenceTimeForTest(fence);
743 }
744
745 FenceToFenceTimeMap* mFenceMap{nullptr};
746
747 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
748 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
749};
750
751
752class TestSurface : public Surface {
753public:
754 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
755 FenceToFenceTimeMap* fenceMap)
756 : Surface(bufferProducer),
757 mFakeSurfaceComposer(new FakeSurfaceComposer) {
758 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
759 mFrameEventHistory.reset(mFakeFrameEventHistory);
760 }
761
762 ~TestSurface() override {}
763
764 sp<ISurfaceComposer> composerService() const override {
765 return mFakeSurfaceComposer;
766 }
767
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800768 nsecs_t now() const override {
769 return mNow;
770 }
771
772 void setNow(nsecs_t now) {
773 mNow = now;
774 }
775
Brian Anderson3da8d272016-07-28 16:20:47 -0700776public:
777 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800778 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700779
780 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
781 // but this raw pointer gives access to test functionality.
782 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
783};
784
785
Brian Andersond0010582017-03-07 13:20:31 -0800786class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700787protected:
788 struct FenceAndFenceTime {
789 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
790 : mFence(new Fence),
791 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
792 sp<Fence> mFence { nullptr };
793 std::shared_ptr<FenceTime> mFenceTime { nullptr };
794 };
795
796 struct RefreshEvents {
797 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800798 : mFenceMap(fenceMap),
799 kCompositorTiming(
800 {refreshStart, refreshStart + 1, refreshStart + 2 }),
801 kStartTime(refreshStart + 3),
802 kGpuCompositionDoneTime(refreshStart + 4),
803 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700804
805 void signalPostCompositeFences() {
806 mFenceMap.signalAllForTest(
807 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
808 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
809 }
810
811 FenceToFenceTimeMap& mFenceMap;
812
813 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
814 FenceAndFenceTime mPresent { mFenceMap };
815
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800816 const CompositorTiming kCompositorTiming;
817
Brian Anderson3da8d272016-07-28 16:20:47 -0700818 const nsecs_t kStartTime;
819 const nsecs_t kGpuCompositionDoneTime;
820 const nsecs_t kPresentTime;
821 };
822
823 struct FrameEvents {
824 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
825 : mFenceMap(fenceMap),
826 kPostedTime(frameStartTime + 100),
827 kRequestedPresentTime(frameStartTime + 200),
828 kProducerAcquireTime(frameStartTime + 300),
829 kConsumerAcquireTime(frameStartTime + 301),
830 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700831 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700832 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700833 mRefreshes {
834 { mFenceMap, frameStartTime + 410 },
835 { mFenceMap, frameStartTime + 420 },
836 { mFenceMap, frameStartTime + 430 } } {}
837
838 void signalQueueFences() {
839 mFenceMap.signalAllForTest(
840 mAcquireConsumer.mFence, kConsumerAcquireTime);
841 mFenceMap.signalAllForTest(
842 mAcquireProducer.mFence, kProducerAcquireTime);
843 }
844
845 void signalRefreshFences() {
846 for (auto& re : mRefreshes) {
847 re.signalPostCompositeFences();
848 }
849 }
850
851 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700852 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
853 }
854
855 FenceToFenceTimeMap& mFenceMap;
856
857 FenceAndFenceTime mAcquireConsumer { mFenceMap };
858 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700859 FenceAndFenceTime mRelease { mFenceMap };
860
861 const nsecs_t kPostedTime;
862 const nsecs_t kRequestedPresentTime;
863 const nsecs_t kProducerAcquireTime;
864 const nsecs_t kConsumerAcquireTime;
865 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700866 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700867 const nsecs_t kReleaseTime;
868
869 RefreshEvents mRefreshes[3];
870 };
871
Brian Andersond0010582017-03-07 13:20:31 -0800872 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700873
874 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700875 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
876 mFakeConsumer = new FakeConsumer;
877 mCfeh = &mFakeConsumer->mFrameEventHistory;
878 mConsumer->consumerConnect(mFakeConsumer, false);
879 mConsumer->setConsumerName(String8("TestConsumer"));
880 mSurface = new TestSurface(mProducer, &mFenceMap);
881 mWindow = mSurface;
882
883 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
884 NATIVE_WINDOW_API_CPU));
885 native_window_set_buffer_count(mWindow.get(), 4);
886 }
887
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800888 void disableFrameTimestamps() {
889 mFakeConsumer->mGetFrameTimestampsEnabled = false;
890 native_window_enable_frame_timestamps(mWindow.get(), 0);
891 mFrameTimestampsEnabled = false;
892 }
893
Brian Anderson3da8d272016-07-28 16:20:47 -0700894 void enableFrameTimestamps() {
895 mFakeConsumer->mGetFrameTimestampsEnabled = true;
896 native_window_enable_frame_timestamps(mWindow.get(), 1);
897 mFrameTimestampsEnabled = true;
898 }
899
Brian Anderson1049d1d2016-12-16 17:25:57 -0800900 int getAllFrameTimestamps(uint64_t frameId) {
901 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700902 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
903 &outFirstRefreshStartTime, &outLastRefreshStartTime,
904 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700905 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700906 }
907
Brian Anderson3da8d272016-07-28 16:20:47 -0700908 void resetTimestamps() {
909 outRequestedPresentTime = -1;
910 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700911 outLatchTime = -1;
912 outFirstRefreshStartTime = -1;
913 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700914 outGpuCompositionDoneTime = -1;
915 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700916 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700917 outReleaseTime = -1;
918 }
919
Brian Anderson1049d1d2016-12-16 17:25:57 -0800920 uint64_t getNextFrameId() {
921 uint64_t frameId = -1;
922 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
923 EXPECT_EQ(status, NO_ERROR);
924 return frameId;
925 }
926
Brian Anderson3da8d272016-07-28 16:20:47 -0700927 void dequeueAndQueue(uint64_t frameIndex) {
928 int fence = -1;
929 ANativeWindowBuffer* buffer = nullptr;
930 ASSERT_EQ(NO_ERROR,
931 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
932
933 int oldAddFrameTimestampsCount =
934 mFakeConsumer->mAddFrameTimestampsCount;
935
936 FrameEvents* frame = &mFrames[frameIndex];
937 uint64_t frameNumber = frameIndex + 1;
938
939 NewFrameEventsEntry fe;
940 fe.frameNumber = frameNumber;
941 fe.postedTime = frame->kPostedTime;
942 fe.requestedPresentTime = frame->kRequestedPresentTime;
943 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
944 mFakeConsumer->mNewFrameEntryOverride = fe;
945
946 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
947 frame->mAcquireProducer.mFenceTime,
948 frame->mAcquireConsumer.mFenceTime);
949
950 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
951
952 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
953
954 EXPECT_EQ(
955 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
956 mFakeConsumer->mAddFrameTimestampsCount);
957 }
958
959 void addFrameEvents(
960 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
961 FrameEvents* oldFrame =
962 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
963 FrameEvents* newFrame = &mFrames[iNewFrame];
964
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800965 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700966 uint64_t nNewFrame = iNewFrame + 1;
967
Brian Anderson4e606e32017-03-16 15:34:57 -0700968 // Latch, Composite, and Release the frames in a plausible order.
969 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700970 // that's okay for the purposes of this test.
971 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
972
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700973 // Composite the previous frame one more time, which helps verify
974 // LastRefresh is updated properly.
975 if (oldFrame != nullptr) {
976 mCfeh->addPreComposition(nOldFrame,
977 oldFrame->mRefreshes[2].kStartTime);
978 gpuDoneFenceTime = gpuComposited ?
979 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
980 FenceTime::NO_FENCE;
981 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800982 oldFrame->mRefreshes[2].mPresent.mFenceTime,
983 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700984 }
985
986 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700987 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
988
989 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
990 gpuDoneFenceTime = gpuComposited ?
991 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
992 FenceTime::NO_FENCE;
993 // HWC2 releases the previous buffer after a new latch just before
994 // calling postComposition.
995 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700996 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700997 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
998 }
999 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001000 newFrame->mRefreshes[0].mPresent.mFenceTime,
1001 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -07001002
Brian Anderson3da8d272016-07-28 16:20:47 -07001003 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
1004 gpuDoneFenceTime = gpuComposited ?
1005 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
1006 FenceTime::NO_FENCE;
1007 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001008 newFrame->mRefreshes[1].mPresent.mFenceTime,
1009 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -07001010 }
1011
Brian Anderson3da8d272016-07-28 16:20:47 -07001012 sp<IGraphicBufferProducer> mProducer;
1013 sp<IGraphicBufferConsumer> mConsumer;
1014 sp<FakeConsumer> mFakeConsumer;
1015 ConsumerFrameEventHistory* mCfeh;
1016 sp<TestSurface> mSurface;
1017 sp<ANativeWindow> mWindow;
1018
1019 FenceToFenceTimeMap mFenceMap;
1020
1021 bool mFrameTimestampsEnabled = false;
1022
1023 int64_t outRequestedPresentTime = -1;
1024 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001025 int64_t outLatchTime = -1;
1026 int64_t outFirstRefreshStartTime = -1;
1027 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001028 int64_t outGpuCompositionDoneTime = -1;
1029 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001030 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001031 int64_t outReleaseTime = -1;
1032
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001033 FrameEvents mFrames[3] {
1034 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -07001035};
1036
1037
1038// This test verifies that the frame timestamps are not retrieved when not
1039// explicitly enabled via native_window_enable_frame_timestamps.
1040// We want to check this to make sure there's no overhead for users
1041// that don't need the timestamp information.
1042TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
1043 int fence;
1044 ANativeWindowBuffer* buffer;
1045
1046 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1047 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1048
Brian Anderson1049d1d2016-12-16 17:25:57 -08001049 const uint64_t fId = getNextFrameId();
1050
Brian Anderson3da8d272016-07-28 16:20:47 -07001051 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
1052 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1053 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1054 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1055
1056 // Verify the producer doesn't get frame timestamps piggybacked on queue.
1057 // It is okay that frame timestamps are added in the consumer since it is
1058 // still needed for SurfaceFlinger dumps.
1059 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1060 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1061 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1062
1063 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001064 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -07001065 EXPECT_EQ(INVALID_OPERATION, result);
1066 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001067
1068 // Verify compositor timing query fails.
1069 nsecs_t compositeDeadline = 0;
1070 nsecs_t compositeInterval = 0;
1071 nsecs_t compositeToPresentLatency = 0;
1072 result = native_window_get_compositor_timing(mWindow.get(),
1073 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1074 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001075}
1076
1077// This test verifies that the frame timestamps are retrieved if explicitly
1078// enabled via native_window_enable_frame_timestamps.
1079TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001080 CompositorTiming initialCompositorTiming {
1081 1000000000, // 1s deadline
1082 16666667, // 16ms interval
1083 50000000, // 50ms present latency
1084 };
1085 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1086
Brian Anderson3da8d272016-07-28 16:20:47 -07001087 enableFrameTimestamps();
1088
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001089 // Verify the compositor timing query gets the initial compositor values
1090 // after timststamps are enabled; even before the first frame is queued
1091 // or dequeued.
1092 nsecs_t compositeDeadline = 0;
1093 nsecs_t compositeInterval = 0;
1094 nsecs_t compositeToPresentLatency = 0;
1095 mSurface->setNow(initialCompositorTiming.deadline - 1);
1096 int result = native_window_get_compositor_timing(mWindow.get(),
1097 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1098 EXPECT_EQ(NO_ERROR, result);
1099 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1100 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1101 EXPECT_EQ(initialCompositorTiming.presentLatency,
1102 compositeToPresentLatency);
1103
Brian Anderson3da8d272016-07-28 16:20:47 -07001104 int fence;
1105 ANativeWindowBuffer* buffer;
1106
1107 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001108 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001109
Brian Anderson1049d1d2016-12-16 17:25:57 -08001110 const uint64_t fId1 = getNextFrameId();
1111
Brian Anderson3da8d272016-07-28 16:20:47 -07001112 // Verify getFrameTimestamps is piggybacked on dequeue.
1113 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1114 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001115 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001116
1117 NewFrameEventsEntry f1;
1118 f1.frameNumber = 1;
1119 f1.postedTime = mFrames[0].kPostedTime;
1120 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1121 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1122 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1123 mFrames[0].mAcquireProducer.mFenceTime,
1124 mFrames[0].mAcquireConsumer.mFenceTime);
1125 mFakeConsumer->mNewFrameEntryOverride = f1;
1126 mFrames[0].signalQueueFences();
1127
1128 // Verify getFrameTimestamps is piggybacked on queue.
1129 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1130 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1131 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001132 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001133
1134 // Verify queries for timestamps that the producer doesn't know about
1135 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001136 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001137 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001138 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001139}
1140
Brian Anderson6b376712017-04-04 10:51:39 -07001141TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1142 bool displayPresentSupported = true;
1143 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1144
1145 // Verify supported bits are forwarded.
1146 int supportsPresent = -1;
1147 mWindow.get()->query(mWindow.get(),
1148 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1149 EXPECT_EQ(displayPresentSupported, supportsPresent);
1150}
1151
1152TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1153 bool displayPresentSupported = false;
1154 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1155
1156 // Verify supported bits are forwarded.
1157 int supportsPresent = -1;
1158 mWindow.get()->query(mWindow.get(),
1159 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1160 EXPECT_EQ(displayPresentSupported, supportsPresent);
1161}
1162
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001163TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1164 nsecs_t phase = 4000;
1165 nsecs_t interval = 1000;
1166
1167 // Timestamp in previous interval.
1168 nsecs_t timestamp = 3500;
1169 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1170 timestamp, phase, interval));
1171
1172 // Timestamp in next interval.
1173 timestamp = 4500;
1174 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1175 timestamp, phase, interval));
1176
1177 // Timestamp multiple intervals before.
1178 timestamp = 2500;
1179 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1180 timestamp, phase, interval));
1181
1182 // Timestamp multiple intervals after.
1183 timestamp = 6500;
1184 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1185 timestamp, phase, interval));
1186
1187 // Timestamp on previous interval.
1188 timestamp = 3000;
1189 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1190 timestamp, phase, interval));
1191
1192 // Timestamp on next interval.
1193 timestamp = 5000;
1194 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1195 timestamp, phase, interval));
1196
1197 // Timestamp equal to phase.
1198 timestamp = 4000;
1199 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1200 timestamp, phase, interval));
1201}
1202
1203// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1204// if the number of intervals elapsed is internally stored in an int.
1205TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1206 nsecs_t phase = 0;
1207 nsecs_t interval = 4000;
1208 nsecs_t big_timestamp = 8635916564000;
1209 int32_t intervals = big_timestamp / interval;
1210
1211 EXPECT_LT(intervals, 0);
1212 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1213 big_timestamp, phase, interval));
1214 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1215 big_timestamp, big_timestamp, interval));
1216}
1217
1218// This verifies the compositor timing is updated by refresh events
1219// and piggy backed on a queue, dequeue, and enabling of timestamps..
1220TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1221 CompositorTiming initialCompositorTiming {
1222 1000000000, // 1s deadline
1223 16666667, // 16ms interval
1224 50000000, // 50ms present latency
1225 };
1226 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1227
1228 enableFrameTimestamps();
1229
1230 // We get the initial values before any frames are submitted.
1231 nsecs_t compositeDeadline = 0;
1232 nsecs_t compositeInterval = 0;
1233 nsecs_t compositeToPresentLatency = 0;
1234 mSurface->setNow(initialCompositorTiming.deadline - 1);
1235 int result = native_window_get_compositor_timing(mWindow.get(),
1236 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1237 EXPECT_EQ(NO_ERROR, result);
1238 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1239 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1240 EXPECT_EQ(initialCompositorTiming.presentLatency,
1241 compositeToPresentLatency);
1242
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001243 dequeueAndQueue(0);
1244 addFrameEvents(true, NO_FRAME_INDEX, 0);
1245
1246 // Still get the initial values because the frame events for frame 0
1247 // didn't get a chance to piggyback on a queue or dequeue yet.
1248 result = native_window_get_compositor_timing(mWindow.get(),
1249 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1250 EXPECT_EQ(NO_ERROR, result);
1251 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1252 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1253 EXPECT_EQ(initialCompositorTiming.presentLatency,
1254 compositeToPresentLatency);
1255
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001256 dequeueAndQueue(1);
1257 addFrameEvents(true, 0, 1);
1258
1259 // Now expect the composite values associated with frame 1.
1260 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1261 result = native_window_get_compositor_timing(mWindow.get(),
1262 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1263 EXPECT_EQ(NO_ERROR, result);
1264 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1265 compositeDeadline);
1266 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1267 compositeInterval);
1268 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1269 compositeToPresentLatency);
1270
1271 dequeueAndQueue(2);
1272 addFrameEvents(true, 1, 2);
1273
1274 // Now expect the composite values associated with frame 2.
1275 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1276 result = native_window_get_compositor_timing(mWindow.get(),
1277 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1278 EXPECT_EQ(NO_ERROR, result);
1279 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1280 compositeDeadline);
1281 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1282 compositeInterval);
1283 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1284 compositeToPresentLatency);
1285
1286 // Re-enabling frame timestamps should get the latest values.
1287 disableFrameTimestamps();
1288 enableFrameTimestamps();
1289
1290 // Now expect the composite values associated with frame 3.
1291 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1292 result = native_window_get_compositor_timing(mWindow.get(),
1293 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1294 EXPECT_EQ(NO_ERROR, result);
1295 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1296 compositeDeadline);
1297 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1298 compositeInterval);
1299 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1300 compositeToPresentLatency);
1301}
1302
1303// This verifies the compositor deadline properly snaps to the the next
1304// deadline based on the current time.
1305TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1306 CompositorTiming initialCompositorTiming {
1307 1000000000, // 1s deadline
1308 16666667, // 16ms interval
1309 50000000, // 50ms present latency
1310 };
1311 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1312
1313 enableFrameTimestamps();
1314
1315 nsecs_t compositeDeadline = 0;
1316 nsecs_t compositeInterval = 0;
1317 nsecs_t compositeToPresentLatency = 0;
1318
1319 // A "now" just before the deadline snaps to the deadline.
1320 mSurface->setNow(initialCompositorTiming.deadline - 1);
1321 int result = native_window_get_compositor_timing(mWindow.get(),
1322 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1323 EXPECT_EQ(NO_ERROR, result);
1324 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1325 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1326 EXPECT_EQ(expectedDeadline, compositeDeadline);
1327
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001328 dequeueAndQueue(0);
1329 addFrameEvents(true, NO_FRAME_INDEX, 0);
1330
1331 // A "now" just after the deadline snaps properly.
1332 mSurface->setNow(initialCompositorTiming.deadline + 1);
1333 result = native_window_get_compositor_timing(mWindow.get(),
1334 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1335 EXPECT_EQ(NO_ERROR, result);
1336 expectedDeadline =
1337 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1338 EXPECT_EQ(expectedDeadline, compositeDeadline);
1339
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001340 dequeueAndQueue(1);
1341 addFrameEvents(true, 0, 1);
1342
1343 // A "now" just after the next interval snaps properly.
1344 mSurface->setNow(
1345 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1346 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1347 result = native_window_get_compositor_timing(mWindow.get(),
1348 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1349 EXPECT_EQ(NO_ERROR, result);
1350 expectedDeadline =
1351 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1352 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1353 EXPECT_EQ(expectedDeadline, compositeDeadline);
1354
1355 dequeueAndQueue(2);
1356 addFrameEvents(true, 1, 2);
1357
1358 // A "now" over 1 interval before the deadline snaps properly.
1359 mSurface->setNow(
1360 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1361 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1362 result = native_window_get_compositor_timing(mWindow.get(),
1363 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1364 EXPECT_EQ(NO_ERROR, result);
1365 expectedDeadline =
1366 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1367 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1368 EXPECT_EQ(expectedDeadline, compositeDeadline);
1369
1370 // Re-enabling frame timestamps should get the latest values.
1371 disableFrameTimestamps();
1372 enableFrameTimestamps();
1373
1374 // A "now" over 2 intervals before the deadline snaps properly.
1375 mSurface->setNow(
1376 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1377 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1378 result = native_window_get_compositor_timing(mWindow.get(),
1379 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1380 EXPECT_EQ(NO_ERROR, result);
1381 expectedDeadline =
1382 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1383 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1384 EXPECT_EQ(expectedDeadline, compositeDeadline);
1385}
1386
Brian Anderson1049d1d2016-12-16 17:25:57 -08001387// This verifies the timestamps recorded in the consumer's
1388// FrameTimestampsHistory are properly retrieved by the producer for the
1389// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001390TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1391 enableFrameTimestamps();
1392
Brian Anderson1049d1d2016-12-16 17:25:57 -08001393 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001394 dequeueAndQueue(0);
1395 mFrames[0].signalQueueFences();
1396
Brian Anderson1049d1d2016-12-16 17:25:57 -08001397 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001398 dequeueAndQueue(1);
1399 mFrames[1].signalQueueFences();
1400
1401 addFrameEvents(true, NO_FRAME_INDEX, 0);
1402 mFrames[0].signalRefreshFences();
1403 addFrameEvents(true, 0, 1);
1404 mFrames[0].signalReleaseFences();
1405 mFrames[1].signalRefreshFences();
1406
1407 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001408 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001409 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001410 EXPECT_EQ(NO_ERROR, result);
1411 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1412 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001413 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1414 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1415 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001416 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1417 outGpuCompositionDoneTime);
1418 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001419 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001420 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1421
1422 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001423 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001424 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001425 EXPECT_EQ(NO_ERROR, result);
1426 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1427 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001428 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1429 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1430 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001431 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1432 outGpuCompositionDoneTime);
1433 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001434 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1435 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001436}
1437
1438// This test verifies the acquire fence recorded by the consumer is not sent
1439// back to the producer and the producer saves its own fence.
1440TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1441 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001442
Brian Anderson3da8d272016-07-28 16:20:47 -07001443 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001444 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001445 dequeueAndQueue(0);
1446
1447 // Verify queue-related timestamps for f1 are available immediately in the
1448 // producer without asking the consumer again, even before signaling the
1449 // acquire fence.
1450 resetTimestamps();
1451 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001452 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001453 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001454 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001455 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1456 EXPECT_EQ(NO_ERROR, result);
1457 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001458 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001459
1460 // Signal acquire fences. Verify a sync call still isn't necessary.
1461 mFrames[0].signalQueueFences();
1462
1463 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001464 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001465 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001466 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001467 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1468 EXPECT_EQ(NO_ERROR, result);
1469 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1470 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1471
1472 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001473 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001474 dequeueAndQueue(1);
1475
1476 // Verify queue-related timestamps for f2 are available immediately in the
1477 // producer without asking the consumer again, even before signaling the
1478 // acquire fence.
1479 resetTimestamps();
1480 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001481 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001482 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001483 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001484 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1485 EXPECT_EQ(NO_ERROR, result);
1486 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001487 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001488
1489 // Signal acquire fences. Verify a sync call still isn't necessary.
1490 mFrames[1].signalQueueFences();
1491
1492 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001493 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001494 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001495 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001496 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1497 EXPECT_EQ(NO_ERROR, result);
1498 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1499 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1500}
1501
1502TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1503 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001504
1505 // Dequeue and queue frame 1.
1506 dequeueAndQueue(0);
1507 mFrames[0].signalQueueFences();
1508
1509 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001510 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001511 dequeueAndQueue(1);
1512 mFrames[1].signalQueueFences();
1513
1514 addFrameEvents(true, NO_FRAME_INDEX, 0);
1515 mFrames[0].signalRefreshFences();
1516 addFrameEvents(true, 0, 1);
1517 mFrames[0].signalReleaseFences();
1518 mFrames[1].signalRefreshFences();
1519
1520 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001521 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001522 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001523 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1524 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001525 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001526 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1527}
1528
1529// This test verifies that fences can signal and update timestamps producer
1530// side without an additional sync call to the consumer.
1531TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1532 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001533
1534 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001535 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001536 dequeueAndQueue(0);
1537 mFrames[0].signalQueueFences();
1538
1539 // Dequeue and queue frame 2.
1540 dequeueAndQueue(1);
1541 mFrames[1].signalQueueFences();
1542
1543 addFrameEvents(true, NO_FRAME_INDEX, 0);
1544 addFrameEvents(true, 0, 1);
1545
1546 // Verify available timestamps are correct for frame 1, before any
1547 // fence has been signaled.
1548 // Note: A sync call is necessary here since the events triggered by
1549 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001550 resetTimestamps();
1551 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001552 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001553 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1554 EXPECT_EQ(NO_ERROR, result);
1555 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1556 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001557 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1558 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1559 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001560 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1561 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001562 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001563 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001564
1565 // Verify available timestamps are correct for frame 1 again, before any
1566 // fence has been signaled.
1567 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001568 resetTimestamps();
1569 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001570 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001571 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1572 EXPECT_EQ(NO_ERROR, result);
1573 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1574 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001575 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1576 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1577 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001578 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1579 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001580 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001581 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001582
1583 // Signal the fences for frame 1.
1584 mFrames[0].signalRefreshFences();
1585 mFrames[0].signalReleaseFences();
1586
1587 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001588 resetTimestamps();
1589 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001590 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001591 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1592 EXPECT_EQ(NO_ERROR, result);
1593 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1594 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001595 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1596 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1597 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001598 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1599 outGpuCompositionDoneTime);
1600 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001601 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001602 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1603}
1604
1605// This test verifies that if the frame wasn't GPU composited but has a refresh
1606// event a sync call isn't made to get the GPU composite done time since it will
1607// never exist.
1608TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1609 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001610
Brian Anderson3da8d272016-07-28 16:20:47 -07001611 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001612 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001613 dequeueAndQueue(0);
1614 mFrames[0].signalQueueFences();
1615
1616 // Dequeue and queue frame 2.
1617 dequeueAndQueue(1);
1618 mFrames[1].signalQueueFences();
1619
1620 addFrameEvents(false, NO_FRAME_INDEX, 0);
1621 addFrameEvents(false, 0, 1);
1622
1623 // Verify available timestamps are correct for frame 1, before any
1624 // fence has been signaled.
1625 // Note: A sync call is necessary here since the events triggered by
1626 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1627 resetTimestamps();
1628 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001629 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001630 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1631 EXPECT_EQ(NO_ERROR, result);
1632 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1633 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001634 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1635 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1636 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001637 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1638 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001639 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001640 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001641
1642 // Signal the fences for frame 1.
1643 mFrames[0].signalRefreshFences();
1644 mFrames[0].signalReleaseFences();
1645
1646 // Verify all timestamps, except GPU composition, are available without a
1647 // sync call.
1648 resetTimestamps();
1649 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001650 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001651 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1652 EXPECT_EQ(NO_ERROR, result);
1653 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1654 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001655 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1656 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1657 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001658 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001659 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001660 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001661 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1662}
1663
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001664// This test verifies that if the certain timestamps can't possibly exist for
1665// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001666TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001667 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001668
1669 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001670 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001671 dequeueAndQueue(0);
1672 mFrames[0].signalQueueFences();
1673
1674 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001675 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001676 dequeueAndQueue(1);
1677 mFrames[1].signalQueueFences();
1678
1679 addFrameEvents(false, NO_FRAME_INDEX, 0);
1680 addFrameEvents(false, 0, 1);
1681
1682 // Verify available timestamps are correct for frame 1, before any
1683 // fence has been signaled.
1684 // Note: A sync call is necessary here since the events triggered by
1685 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001686 resetTimestamps();
1687 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001688 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001689 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1690 EXPECT_EQ(NO_ERROR, result);
1691 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1692 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001693 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1694 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1695 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001696 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1697 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001698 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001699 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001700
1701 mFrames[0].signalRefreshFences();
1702 mFrames[0].signalReleaseFences();
1703 mFrames[1].signalRefreshFences();
1704
Brian Anderson1049d1d2016-12-16 17:25:57 -08001705 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001706 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001707 // available, a sync call should not occur because it's not possible for f2
1708 // to encounter the final value for those events until another frame is
1709 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001710 resetTimestamps();
1711 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001712 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001713 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1714 EXPECT_EQ(NO_ERROR, result);
1715 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1716 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001717 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1718 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1719 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001720 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001721 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001722 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1723 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001724}
1725
Brian Anderson6b376712017-04-04 10:51:39 -07001726// This test verifies there are no sync calls for present times
1727// when they aren't supported and that an error is returned.
1728
1729TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1730 enableFrameTimestamps();
1731 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1732
1733 // Dequeue and queue frame 1.
1734 const uint64_t fId1 = getNextFrameId();
1735 dequeueAndQueue(0);
1736
1737 // Verify a query for the Present times do not trigger a sync call if they
1738 // are not supported.
1739 resetTimestamps();
1740 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1741 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1742 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1743 &outDisplayPresentTime, nullptr, nullptr);
1744 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1745 EXPECT_EQ(BAD_VALUE, result);
1746 EXPECT_EQ(-1, outDisplayPresentTime);
1747}
1748
Dan Stoza932f0082017-05-31 13:50:16 -07001749} // namespace android