blob: 014b1fac7d19e945fc00ab84fe3c10a07e71b3d3 [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 }
Robert Carr866455f2019-04-02 16:28:26 -0700626 virtual status_t captureLayers(
627 const sp<IBinder>& /*parentHandle*/, sp<GraphicBuffer>* /*outBuffer*/,
628 const ui::Dataspace /*reqDataspace*/, const ui::PixelFormat /*reqPixelFormat*/,
629 const Rect& /*sourceCrop*/,
630 const std::unordered_set<sp<IBinder>,
631 ISurfaceComposer::SpHash<IBinder>>& /*excludeHandles*/,
632 float /*frameScale*/, bool /*childrenOnly*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700633 return NO_ERROR;
634 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700635 status_t clearAnimationFrameStats() override { return NO_ERROR; }
636 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
637 return NO_ERROR;
638 }
639 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
640 HdrCapabilities* /*outCapabilities*/) const override {
641 return NO_ERROR;
642 }
643 status_t enableVSyncInjections(bool /*enable*/) override {
644 return NO_ERROR;
645 }
646 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800647 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
648 return NO_ERROR;
649 }
Peiyong Linc6780972018-10-28 15:24:08 -0700650 status_t getCompositionPreference(
651 ui::Dataspace* /*outDefaultDataspace*/, ui::PixelFormat* /*outDefaultPixelFormat*/,
652 ui::Dataspace* /*outWideColorGamutDataspace*/,
653 ui::PixelFormat* /*outWideColorGamutPixelFormat*/) const override {
Peiyong Lin0256f722018-08-31 15:45:10 -0700654 return NO_ERROR;
655 }
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700656 status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& /*display*/,
657 ui::PixelFormat* /*outFormat*/,
658 ui::Dataspace* /*outDataspace*/,
659 uint8_t* /*outComponentMask*/) const override {
660 return NO_ERROR;
661 }
Kevin DuBois74e53772018-11-19 10:52:38 -0800662 status_t setDisplayContentSamplingEnabled(const sp<IBinder>& /*display*/, bool /*enable*/,
663 uint8_t /*componentMask*/,
664 uint64_t /*maxFrames*/) const override {
665 return NO_ERROR;
666 }
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700667 status_t getDisplayedContentSample(const sp<IBinder>& /*display*/, uint64_t /*maxFrames*/,
668 uint64_t /*timestamp*/,
669 DisplayedFrameStats* /*outStats*/) const override {
670 return NO_ERROR;
671 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700672
Peiyong Lin3c2791e2019-01-14 17:05:18 -0800673 status_t getColorManagement(bool* /*outGetColorManagement*/) const override { return NO_ERROR; }
674 status_t getProtectedContentSupport(bool* /*outSupported*/) const override { return NO_ERROR; }
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700675
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800676 status_t isWideColorDisplay(const sp<IBinder>&, bool*) const override { return NO_ERROR; }
Dan Gittik57e63c52019-01-18 16:37:54 +0000677 status_t getDisplayBrightnessSupport(const sp<IBinder>& /*displayToken*/,
678 bool* /*outSupport*/) const override {
679 return NO_ERROR;
680 }
681 status_t setDisplayBrightness(const sp<IBinder>& /*displayToken*/,
682 float /*brightness*/) const override {
683 return NO_ERROR;
684 }
Marissa Wallebc2c052019-01-16 19:16:55 -0800685
Dan Stoza84ab9372018-12-17 15:27:57 -0800686 status_t addRegionSamplingListener(const Rect& /*samplingArea*/,
687 const sp<IBinder>& /*stopLayerHandle*/,
688 const sp<IRegionSamplingListener>& /*listener*/) override {
689 return NO_ERROR;
690 }
691 status_t removeRegionSamplingListener(
692 const sp<IRegionSamplingListener>& /*listener*/) override {
693 return NO_ERROR;
694 }
Ady Abraham838de062019-02-04 10:24:03 -0800695 status_t setAllowedDisplayConfigs(const sp<IBinder>& /*displayToken*/,
696 const std::vector<int32_t>& /*allowedConfigs*/) override {
697 return NO_ERROR;
698 }
Ady Abrahamd9b3ea62019-02-26 14:08:03 -0800699 status_t getAllowedDisplayConfigs(const sp<IBinder>& /*displayToken*/,
700 std::vector<int32_t>* /*outAllowedConfigs*/) override {
701 return NO_ERROR;
702 }
Dan Stoza84ab9372018-12-17 15:27:57 -0800703
Brian Anderson3da8d272016-07-28 16:20:47 -0700704protected:
705 IBinder* onAsBinder() override { return nullptr; }
706
707private:
708 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700709};
710
711class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
712public:
Chih-Hung Hsiehaaf62162018-12-20 15:45:04 -0800713 explicit FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap) : mFenceMap(fenceMap) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700714
715 ~FakeProducerFrameEventHistory() {}
716
717 void updateAcquireFence(uint64_t frameNumber,
718 std::shared_ptr<FenceTime>&& acquire) override {
719 // Verify the acquire fence being added isn't the one from the consumer.
720 EXPECT_NE(mConsumerAcquireFence, acquire);
721 // Override the fence, so we can verify this was called by the
722 // producer after the frame is queued.
723 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
724 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
725 }
726
727 void setAcquireFenceOverride(
728 const std::shared_ptr<FenceTime>& acquireFenceOverride,
729 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
730 mAcquireFenceOverride = acquireFenceOverride;
731 mConsumerAcquireFence = consumerAcquireFence;
732 }
733
734protected:
735 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
736 const override {
737 return mFenceMap->createFenceTimeForTest(fence);
738 }
739
740 FenceToFenceTimeMap* mFenceMap{nullptr};
741
742 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
743 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
744};
745
746
747class TestSurface : public Surface {
748public:
749 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
750 FenceToFenceTimeMap* fenceMap)
751 : Surface(bufferProducer),
752 mFakeSurfaceComposer(new FakeSurfaceComposer) {
753 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
754 mFrameEventHistory.reset(mFakeFrameEventHistory);
755 }
756
757 ~TestSurface() override {}
758
759 sp<ISurfaceComposer> composerService() const override {
760 return mFakeSurfaceComposer;
761 }
762
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800763 nsecs_t now() const override {
764 return mNow;
765 }
766
767 void setNow(nsecs_t now) {
768 mNow = now;
769 }
770
Brian Anderson3da8d272016-07-28 16:20:47 -0700771public:
772 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800773 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700774
775 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
776 // but this raw pointer gives access to test functionality.
777 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
778};
779
780
Brian Andersond0010582017-03-07 13:20:31 -0800781class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700782protected:
783 struct FenceAndFenceTime {
784 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
785 : mFence(new Fence),
786 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
787 sp<Fence> mFence { nullptr };
788 std::shared_ptr<FenceTime> mFenceTime { nullptr };
789 };
790
791 struct RefreshEvents {
792 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800793 : mFenceMap(fenceMap),
794 kCompositorTiming(
795 {refreshStart, refreshStart + 1, refreshStart + 2 }),
796 kStartTime(refreshStart + 3),
797 kGpuCompositionDoneTime(refreshStart + 4),
798 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700799
800 void signalPostCompositeFences() {
801 mFenceMap.signalAllForTest(
802 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
803 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
804 }
805
806 FenceToFenceTimeMap& mFenceMap;
807
808 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
809 FenceAndFenceTime mPresent { mFenceMap };
810
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800811 const CompositorTiming kCompositorTiming;
812
Brian Anderson3da8d272016-07-28 16:20:47 -0700813 const nsecs_t kStartTime;
814 const nsecs_t kGpuCompositionDoneTime;
815 const nsecs_t kPresentTime;
816 };
817
818 struct FrameEvents {
819 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
820 : mFenceMap(fenceMap),
821 kPostedTime(frameStartTime + 100),
822 kRequestedPresentTime(frameStartTime + 200),
823 kProducerAcquireTime(frameStartTime + 300),
824 kConsumerAcquireTime(frameStartTime + 301),
825 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700826 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700827 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700828 mRefreshes {
829 { mFenceMap, frameStartTime + 410 },
830 { mFenceMap, frameStartTime + 420 },
831 { mFenceMap, frameStartTime + 430 } } {}
832
833 void signalQueueFences() {
834 mFenceMap.signalAllForTest(
835 mAcquireConsumer.mFence, kConsumerAcquireTime);
836 mFenceMap.signalAllForTest(
837 mAcquireProducer.mFence, kProducerAcquireTime);
838 }
839
840 void signalRefreshFences() {
841 for (auto& re : mRefreshes) {
842 re.signalPostCompositeFences();
843 }
844 }
845
846 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700847 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
848 }
849
850 FenceToFenceTimeMap& mFenceMap;
851
852 FenceAndFenceTime mAcquireConsumer { mFenceMap };
853 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700854 FenceAndFenceTime mRelease { mFenceMap };
855
856 const nsecs_t kPostedTime;
857 const nsecs_t kRequestedPresentTime;
858 const nsecs_t kProducerAcquireTime;
859 const nsecs_t kConsumerAcquireTime;
860 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700861 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700862 const nsecs_t kReleaseTime;
863
864 RefreshEvents mRefreshes[3];
865 };
866
Brian Andersond0010582017-03-07 13:20:31 -0800867 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700868
869 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700870 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
871 mFakeConsumer = new FakeConsumer;
872 mCfeh = &mFakeConsumer->mFrameEventHistory;
873 mConsumer->consumerConnect(mFakeConsumer, false);
874 mConsumer->setConsumerName(String8("TestConsumer"));
875 mSurface = new TestSurface(mProducer, &mFenceMap);
876 mWindow = mSurface;
877
878 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
879 NATIVE_WINDOW_API_CPU));
880 native_window_set_buffer_count(mWindow.get(), 4);
881 }
882
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800883 void disableFrameTimestamps() {
884 mFakeConsumer->mGetFrameTimestampsEnabled = false;
885 native_window_enable_frame_timestamps(mWindow.get(), 0);
886 mFrameTimestampsEnabled = false;
887 }
888
Brian Anderson3da8d272016-07-28 16:20:47 -0700889 void enableFrameTimestamps() {
890 mFakeConsumer->mGetFrameTimestampsEnabled = true;
891 native_window_enable_frame_timestamps(mWindow.get(), 1);
892 mFrameTimestampsEnabled = true;
893 }
894
Brian Anderson1049d1d2016-12-16 17:25:57 -0800895 int getAllFrameTimestamps(uint64_t frameId) {
896 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700897 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
898 &outFirstRefreshStartTime, &outLastRefreshStartTime,
899 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700900 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700901 }
902
Brian Anderson3da8d272016-07-28 16:20:47 -0700903 void resetTimestamps() {
904 outRequestedPresentTime = -1;
905 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700906 outLatchTime = -1;
907 outFirstRefreshStartTime = -1;
908 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700909 outGpuCompositionDoneTime = -1;
910 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700911 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700912 outReleaseTime = -1;
913 }
914
Brian Anderson1049d1d2016-12-16 17:25:57 -0800915 uint64_t getNextFrameId() {
916 uint64_t frameId = -1;
917 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
918 EXPECT_EQ(status, NO_ERROR);
919 return frameId;
920 }
921
Brian Anderson3da8d272016-07-28 16:20:47 -0700922 void dequeueAndQueue(uint64_t frameIndex) {
923 int fence = -1;
924 ANativeWindowBuffer* buffer = nullptr;
925 ASSERT_EQ(NO_ERROR,
926 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
927
928 int oldAddFrameTimestampsCount =
929 mFakeConsumer->mAddFrameTimestampsCount;
930
931 FrameEvents* frame = &mFrames[frameIndex];
932 uint64_t frameNumber = frameIndex + 1;
933
934 NewFrameEventsEntry fe;
935 fe.frameNumber = frameNumber;
936 fe.postedTime = frame->kPostedTime;
937 fe.requestedPresentTime = frame->kRequestedPresentTime;
938 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
939 mFakeConsumer->mNewFrameEntryOverride = fe;
940
941 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
942 frame->mAcquireProducer.mFenceTime,
943 frame->mAcquireConsumer.mFenceTime);
944
945 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
946
947 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
948
949 EXPECT_EQ(
950 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
951 mFakeConsumer->mAddFrameTimestampsCount);
952 }
953
954 void addFrameEvents(
955 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
956 FrameEvents* oldFrame =
957 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
958 FrameEvents* newFrame = &mFrames[iNewFrame];
959
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800960 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700961 uint64_t nNewFrame = iNewFrame + 1;
962
Brian Anderson4e606e32017-03-16 15:34:57 -0700963 // Latch, Composite, and Release the frames in a plausible order.
964 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700965 // that's okay for the purposes of this test.
966 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
967
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700968 // Composite the previous frame one more time, which helps verify
969 // LastRefresh is updated properly.
970 if (oldFrame != nullptr) {
971 mCfeh->addPreComposition(nOldFrame,
972 oldFrame->mRefreshes[2].kStartTime);
973 gpuDoneFenceTime = gpuComposited ?
974 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
975 FenceTime::NO_FENCE;
976 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800977 oldFrame->mRefreshes[2].mPresent.mFenceTime,
978 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700979 }
980
981 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700982 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
983
984 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
985 gpuDoneFenceTime = gpuComposited ?
986 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
987 FenceTime::NO_FENCE;
988 // HWC2 releases the previous buffer after a new latch just before
989 // calling postComposition.
990 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700991 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700992 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
993 }
994 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800995 newFrame->mRefreshes[0].mPresent.mFenceTime,
996 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700997
Brian Anderson3da8d272016-07-28 16:20:47 -0700998 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
999 gpuDoneFenceTime = gpuComposited ?
1000 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
1001 FenceTime::NO_FENCE;
1002 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001003 newFrame->mRefreshes[1].mPresent.mFenceTime,
1004 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -07001005 }
1006
Brian Anderson3da8d272016-07-28 16:20:47 -07001007 sp<IGraphicBufferProducer> mProducer;
1008 sp<IGraphicBufferConsumer> mConsumer;
1009 sp<FakeConsumer> mFakeConsumer;
1010 ConsumerFrameEventHistory* mCfeh;
1011 sp<TestSurface> mSurface;
1012 sp<ANativeWindow> mWindow;
1013
1014 FenceToFenceTimeMap mFenceMap;
1015
1016 bool mFrameTimestampsEnabled = false;
1017
1018 int64_t outRequestedPresentTime = -1;
1019 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001020 int64_t outLatchTime = -1;
1021 int64_t outFirstRefreshStartTime = -1;
1022 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001023 int64_t outGpuCompositionDoneTime = -1;
1024 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001025 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001026 int64_t outReleaseTime = -1;
1027
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001028 FrameEvents mFrames[3] {
1029 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -07001030};
1031
1032
1033// This test verifies that the frame timestamps are not retrieved when not
1034// explicitly enabled via native_window_enable_frame_timestamps.
1035// We want to check this to make sure there's no overhead for users
1036// that don't need the timestamp information.
1037TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
1038 int fence;
1039 ANativeWindowBuffer* buffer;
1040
1041 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1042 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1043
Brian Anderson1049d1d2016-12-16 17:25:57 -08001044 const uint64_t fId = getNextFrameId();
1045
Brian Anderson3da8d272016-07-28 16:20:47 -07001046 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
1047 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1048 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1049 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1050
1051 // Verify the producer doesn't get frame timestamps piggybacked on queue.
1052 // It is okay that frame timestamps are added in the consumer since it is
1053 // still needed for SurfaceFlinger dumps.
1054 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1055 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1056 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1057
1058 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001059 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -07001060 EXPECT_EQ(INVALID_OPERATION, result);
1061 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001062
1063 // Verify compositor timing query fails.
1064 nsecs_t compositeDeadline = 0;
1065 nsecs_t compositeInterval = 0;
1066 nsecs_t compositeToPresentLatency = 0;
1067 result = native_window_get_compositor_timing(mWindow.get(),
1068 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1069 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001070}
1071
1072// This test verifies that the frame timestamps are retrieved if explicitly
1073// enabled via native_window_enable_frame_timestamps.
1074TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001075 CompositorTiming initialCompositorTiming {
1076 1000000000, // 1s deadline
1077 16666667, // 16ms interval
1078 50000000, // 50ms present latency
1079 };
1080 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1081
Brian Anderson3da8d272016-07-28 16:20:47 -07001082 enableFrameTimestamps();
1083
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001084 // Verify the compositor timing query gets the initial compositor values
1085 // after timststamps are enabled; even before the first frame is queued
1086 // or dequeued.
1087 nsecs_t compositeDeadline = 0;
1088 nsecs_t compositeInterval = 0;
1089 nsecs_t compositeToPresentLatency = 0;
1090 mSurface->setNow(initialCompositorTiming.deadline - 1);
1091 int result = native_window_get_compositor_timing(mWindow.get(),
1092 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1093 EXPECT_EQ(NO_ERROR, result);
1094 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1095 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1096 EXPECT_EQ(initialCompositorTiming.presentLatency,
1097 compositeToPresentLatency);
1098
Brian Anderson3da8d272016-07-28 16:20:47 -07001099 int fence;
1100 ANativeWindowBuffer* buffer;
1101
1102 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001103 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001104
Brian Anderson1049d1d2016-12-16 17:25:57 -08001105 const uint64_t fId1 = getNextFrameId();
1106
Brian Anderson3da8d272016-07-28 16:20:47 -07001107 // Verify getFrameTimestamps is piggybacked on dequeue.
1108 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1109 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001110 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001111
1112 NewFrameEventsEntry f1;
1113 f1.frameNumber = 1;
1114 f1.postedTime = mFrames[0].kPostedTime;
1115 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1116 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1117 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1118 mFrames[0].mAcquireProducer.mFenceTime,
1119 mFrames[0].mAcquireConsumer.mFenceTime);
1120 mFakeConsumer->mNewFrameEntryOverride = f1;
1121 mFrames[0].signalQueueFences();
1122
1123 // Verify getFrameTimestamps is piggybacked on queue.
1124 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1125 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1126 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001127 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001128
1129 // Verify queries for timestamps that the producer doesn't know about
1130 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001131 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001132 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001133 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001134}
1135
Brian Anderson6b376712017-04-04 10:51:39 -07001136TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1137 bool displayPresentSupported = true;
1138 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1139
1140 // Verify supported bits are forwarded.
1141 int supportsPresent = -1;
1142 mWindow.get()->query(mWindow.get(),
1143 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1144 EXPECT_EQ(displayPresentSupported, supportsPresent);
1145}
1146
1147TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1148 bool displayPresentSupported = false;
1149 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1150
1151 // Verify supported bits are forwarded.
1152 int supportsPresent = -1;
1153 mWindow.get()->query(mWindow.get(),
1154 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1155 EXPECT_EQ(displayPresentSupported, supportsPresent);
1156}
1157
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001158TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1159 nsecs_t phase = 4000;
1160 nsecs_t interval = 1000;
1161
1162 // Timestamp in previous interval.
1163 nsecs_t timestamp = 3500;
1164 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1165 timestamp, phase, interval));
1166
1167 // Timestamp in next interval.
1168 timestamp = 4500;
1169 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1170 timestamp, phase, interval));
1171
1172 // Timestamp multiple intervals before.
1173 timestamp = 2500;
1174 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1175 timestamp, phase, interval));
1176
1177 // Timestamp multiple intervals after.
1178 timestamp = 6500;
1179 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1180 timestamp, phase, interval));
1181
1182 // Timestamp on previous interval.
1183 timestamp = 3000;
1184 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1185 timestamp, phase, interval));
1186
1187 // Timestamp on next interval.
1188 timestamp = 5000;
1189 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1190 timestamp, phase, interval));
1191
1192 // Timestamp equal to phase.
1193 timestamp = 4000;
1194 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1195 timestamp, phase, interval));
1196}
1197
1198// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1199// if the number of intervals elapsed is internally stored in an int.
1200TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1201 nsecs_t phase = 0;
1202 nsecs_t interval = 4000;
1203 nsecs_t big_timestamp = 8635916564000;
1204 int32_t intervals = big_timestamp / interval;
1205
1206 EXPECT_LT(intervals, 0);
1207 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1208 big_timestamp, phase, interval));
1209 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1210 big_timestamp, big_timestamp, interval));
1211}
1212
1213// This verifies the compositor timing is updated by refresh events
1214// and piggy backed on a queue, dequeue, and enabling of timestamps..
1215TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1216 CompositorTiming initialCompositorTiming {
1217 1000000000, // 1s deadline
1218 16666667, // 16ms interval
1219 50000000, // 50ms present latency
1220 };
1221 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1222
1223 enableFrameTimestamps();
1224
1225 // We get the initial values before any frames are submitted.
1226 nsecs_t compositeDeadline = 0;
1227 nsecs_t compositeInterval = 0;
1228 nsecs_t compositeToPresentLatency = 0;
1229 mSurface->setNow(initialCompositorTiming.deadline - 1);
1230 int result = native_window_get_compositor_timing(mWindow.get(),
1231 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1232 EXPECT_EQ(NO_ERROR, result);
1233 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1234 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1235 EXPECT_EQ(initialCompositorTiming.presentLatency,
1236 compositeToPresentLatency);
1237
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001238 dequeueAndQueue(0);
1239 addFrameEvents(true, NO_FRAME_INDEX, 0);
1240
1241 // Still get the initial values because the frame events for frame 0
1242 // didn't get a chance to piggyback on a queue or dequeue yet.
1243 result = native_window_get_compositor_timing(mWindow.get(),
1244 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1245 EXPECT_EQ(NO_ERROR, result);
1246 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1247 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1248 EXPECT_EQ(initialCompositorTiming.presentLatency,
1249 compositeToPresentLatency);
1250
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001251 dequeueAndQueue(1);
1252 addFrameEvents(true, 0, 1);
1253
1254 // Now expect the composite values associated with frame 1.
1255 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1256 result = native_window_get_compositor_timing(mWindow.get(),
1257 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1258 EXPECT_EQ(NO_ERROR, result);
1259 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1260 compositeDeadline);
1261 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1262 compositeInterval);
1263 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1264 compositeToPresentLatency);
1265
1266 dequeueAndQueue(2);
1267 addFrameEvents(true, 1, 2);
1268
1269 // Now expect the composite values associated with frame 2.
1270 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1271 result = native_window_get_compositor_timing(mWindow.get(),
1272 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1273 EXPECT_EQ(NO_ERROR, result);
1274 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1275 compositeDeadline);
1276 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1277 compositeInterval);
1278 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1279 compositeToPresentLatency);
1280
1281 // Re-enabling frame timestamps should get the latest values.
1282 disableFrameTimestamps();
1283 enableFrameTimestamps();
1284
1285 // Now expect the composite values associated with frame 3.
1286 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1287 result = native_window_get_compositor_timing(mWindow.get(),
1288 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1289 EXPECT_EQ(NO_ERROR, result);
1290 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1291 compositeDeadline);
1292 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1293 compositeInterval);
1294 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1295 compositeToPresentLatency);
1296}
1297
1298// This verifies the compositor deadline properly snaps to the the next
1299// deadline based on the current time.
1300TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1301 CompositorTiming initialCompositorTiming {
1302 1000000000, // 1s deadline
1303 16666667, // 16ms interval
1304 50000000, // 50ms present latency
1305 };
1306 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1307
1308 enableFrameTimestamps();
1309
1310 nsecs_t compositeDeadline = 0;
1311 nsecs_t compositeInterval = 0;
1312 nsecs_t compositeToPresentLatency = 0;
1313
1314 // A "now" just before the deadline snaps to the deadline.
1315 mSurface->setNow(initialCompositorTiming.deadline - 1);
1316 int result = native_window_get_compositor_timing(mWindow.get(),
1317 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1318 EXPECT_EQ(NO_ERROR, result);
1319 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1320 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1321 EXPECT_EQ(expectedDeadline, compositeDeadline);
1322
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001323 dequeueAndQueue(0);
1324 addFrameEvents(true, NO_FRAME_INDEX, 0);
1325
1326 // A "now" just after the deadline snaps properly.
1327 mSurface->setNow(initialCompositorTiming.deadline + 1);
1328 result = native_window_get_compositor_timing(mWindow.get(),
1329 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1330 EXPECT_EQ(NO_ERROR, result);
1331 expectedDeadline =
1332 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1333 EXPECT_EQ(expectedDeadline, compositeDeadline);
1334
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001335 dequeueAndQueue(1);
1336 addFrameEvents(true, 0, 1);
1337
1338 // A "now" just after the next interval snaps properly.
1339 mSurface->setNow(
1340 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1341 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1342 result = native_window_get_compositor_timing(mWindow.get(),
1343 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1344 EXPECT_EQ(NO_ERROR, result);
1345 expectedDeadline =
1346 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1347 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1348 EXPECT_EQ(expectedDeadline, compositeDeadline);
1349
1350 dequeueAndQueue(2);
1351 addFrameEvents(true, 1, 2);
1352
1353 // A "now" over 1 interval before the deadline snaps properly.
1354 mSurface->setNow(
1355 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1356 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1357 result = native_window_get_compositor_timing(mWindow.get(),
1358 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1359 EXPECT_EQ(NO_ERROR, result);
1360 expectedDeadline =
1361 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1362 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1363 EXPECT_EQ(expectedDeadline, compositeDeadline);
1364
1365 // Re-enabling frame timestamps should get the latest values.
1366 disableFrameTimestamps();
1367 enableFrameTimestamps();
1368
1369 // A "now" over 2 intervals before the deadline snaps properly.
1370 mSurface->setNow(
1371 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1372 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1373 result = native_window_get_compositor_timing(mWindow.get(),
1374 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1375 EXPECT_EQ(NO_ERROR, result);
1376 expectedDeadline =
1377 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1378 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1379 EXPECT_EQ(expectedDeadline, compositeDeadline);
1380}
1381
Brian Anderson1049d1d2016-12-16 17:25:57 -08001382// This verifies the timestamps recorded in the consumer's
1383// FrameTimestampsHistory are properly retrieved by the producer for the
1384// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001385TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1386 enableFrameTimestamps();
1387
Brian Anderson1049d1d2016-12-16 17:25:57 -08001388 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001389 dequeueAndQueue(0);
1390 mFrames[0].signalQueueFences();
1391
Brian Anderson1049d1d2016-12-16 17:25:57 -08001392 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001393 dequeueAndQueue(1);
1394 mFrames[1].signalQueueFences();
1395
1396 addFrameEvents(true, NO_FRAME_INDEX, 0);
1397 mFrames[0].signalRefreshFences();
1398 addFrameEvents(true, 0, 1);
1399 mFrames[0].signalReleaseFences();
1400 mFrames[1].signalRefreshFences();
1401
1402 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001403 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001404 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001405 EXPECT_EQ(NO_ERROR, result);
1406 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1407 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001408 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1409 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1410 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001411 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1412 outGpuCompositionDoneTime);
1413 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001414 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001415 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1416
1417 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001418 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001419 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001420 EXPECT_EQ(NO_ERROR, result);
1421 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1422 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001423 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1424 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1425 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001426 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1427 outGpuCompositionDoneTime);
1428 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001429 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1430 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001431}
1432
1433// This test verifies the acquire fence recorded by the consumer is not sent
1434// back to the producer and the producer saves its own fence.
1435TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1436 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001437
Brian Anderson3da8d272016-07-28 16:20:47 -07001438 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001439 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001440 dequeueAndQueue(0);
1441
1442 // Verify queue-related timestamps for f1 are available immediately in the
1443 // producer without asking the consumer again, even before signaling the
1444 // acquire fence.
1445 resetTimestamps();
1446 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001447 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001448 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001449 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001450 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1451 EXPECT_EQ(NO_ERROR, result);
1452 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001453 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001454
1455 // Signal acquire fences. Verify a sync call still isn't necessary.
1456 mFrames[0].signalQueueFences();
1457
1458 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001459 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001460 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001461 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001462 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1463 EXPECT_EQ(NO_ERROR, result);
1464 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1465 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1466
1467 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001468 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001469 dequeueAndQueue(1);
1470
1471 // Verify queue-related timestamps for f2 are available immediately in the
1472 // producer without asking the consumer again, even before signaling the
1473 // acquire fence.
1474 resetTimestamps();
1475 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001476 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001477 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001478 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001479 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1480 EXPECT_EQ(NO_ERROR, result);
1481 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001482 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001483
1484 // Signal acquire fences. Verify a sync call still isn't necessary.
1485 mFrames[1].signalQueueFences();
1486
1487 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001488 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001489 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001490 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001491 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1492 EXPECT_EQ(NO_ERROR, result);
1493 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1494 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1495}
1496
1497TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1498 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001499
1500 // Dequeue and queue frame 1.
1501 dequeueAndQueue(0);
1502 mFrames[0].signalQueueFences();
1503
1504 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001505 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001506 dequeueAndQueue(1);
1507 mFrames[1].signalQueueFences();
1508
1509 addFrameEvents(true, NO_FRAME_INDEX, 0);
1510 mFrames[0].signalRefreshFences();
1511 addFrameEvents(true, 0, 1);
1512 mFrames[0].signalReleaseFences();
1513 mFrames[1].signalRefreshFences();
1514
1515 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001516 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001517 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001518 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1519 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001520 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001521 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1522}
1523
1524// This test verifies that fences can signal and update timestamps producer
1525// side without an additional sync call to the consumer.
1526TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1527 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001528
1529 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001530 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001531 dequeueAndQueue(0);
1532 mFrames[0].signalQueueFences();
1533
1534 // Dequeue and queue frame 2.
1535 dequeueAndQueue(1);
1536 mFrames[1].signalQueueFences();
1537
1538 addFrameEvents(true, NO_FRAME_INDEX, 0);
1539 addFrameEvents(true, 0, 1);
1540
1541 // Verify available timestamps are correct for frame 1, before any
1542 // fence has been signaled.
1543 // Note: A sync call is necessary here since the events triggered by
1544 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001545 resetTimestamps();
1546 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001547 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001548 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1549 EXPECT_EQ(NO_ERROR, result);
1550 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1551 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001552 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1553 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1554 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001555 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1556 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001557 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001558 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001559
1560 // Verify available timestamps are correct for frame 1 again, before any
1561 // fence has been signaled.
1562 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001563 resetTimestamps();
1564 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001565 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001566 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1567 EXPECT_EQ(NO_ERROR, result);
1568 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1569 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001570 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1571 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1572 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001573 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1574 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001575 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001576 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001577
1578 // Signal the fences for frame 1.
1579 mFrames[0].signalRefreshFences();
1580 mFrames[0].signalReleaseFences();
1581
1582 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001583 resetTimestamps();
1584 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001585 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001586 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1587 EXPECT_EQ(NO_ERROR, result);
1588 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1589 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001590 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1591 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1592 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001593 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1594 outGpuCompositionDoneTime);
1595 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001596 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001597 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1598}
1599
1600// This test verifies that if the frame wasn't GPU composited but has a refresh
1601// event a sync call isn't made to get the GPU composite done time since it will
1602// never exist.
1603TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1604 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001605
Brian Anderson3da8d272016-07-28 16:20:47 -07001606 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001607 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001608 dequeueAndQueue(0);
1609 mFrames[0].signalQueueFences();
1610
1611 // Dequeue and queue frame 2.
1612 dequeueAndQueue(1);
1613 mFrames[1].signalQueueFences();
1614
1615 addFrameEvents(false, NO_FRAME_INDEX, 0);
1616 addFrameEvents(false, 0, 1);
1617
1618 // Verify available timestamps are correct for frame 1, before any
1619 // fence has been signaled.
1620 // Note: A sync call is necessary here since the events triggered by
1621 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1622 resetTimestamps();
1623 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001624 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001625 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1626 EXPECT_EQ(NO_ERROR, result);
1627 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1628 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001629 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1630 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1631 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001632 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1633 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001634 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001635 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001636
1637 // Signal the fences for frame 1.
1638 mFrames[0].signalRefreshFences();
1639 mFrames[0].signalReleaseFences();
1640
1641 // Verify all timestamps, except GPU composition, are available without a
1642 // sync call.
1643 resetTimestamps();
1644 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001645 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001646 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1647 EXPECT_EQ(NO_ERROR, result);
1648 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1649 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001650 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1651 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1652 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001653 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001654 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001655 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001656 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1657}
1658
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001659// This test verifies that if the certain timestamps can't possibly exist for
1660// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001661TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001662 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001663
1664 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001665 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001666 dequeueAndQueue(0);
1667 mFrames[0].signalQueueFences();
1668
1669 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001670 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001671 dequeueAndQueue(1);
1672 mFrames[1].signalQueueFences();
1673
1674 addFrameEvents(false, NO_FRAME_INDEX, 0);
1675 addFrameEvents(false, 0, 1);
1676
1677 // Verify available timestamps are correct for frame 1, before any
1678 // fence has been signaled.
1679 // Note: A sync call is necessary here since the events triggered by
1680 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001681 resetTimestamps();
1682 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001683 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001684 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1685 EXPECT_EQ(NO_ERROR, result);
1686 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1687 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001688 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1689 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1690 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001691 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1692 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001693 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001694 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001695
1696 mFrames[0].signalRefreshFences();
1697 mFrames[0].signalReleaseFences();
1698 mFrames[1].signalRefreshFences();
1699
Brian Anderson1049d1d2016-12-16 17:25:57 -08001700 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001701 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001702 // available, a sync call should not occur because it's not possible for f2
1703 // to encounter the final value for those events until another frame is
1704 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001705 resetTimestamps();
1706 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001707 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001708 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1709 EXPECT_EQ(NO_ERROR, result);
1710 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1711 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001712 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1713 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1714 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001715 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001716 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001717 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1718 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001719}
1720
Brian Anderson6b376712017-04-04 10:51:39 -07001721// This test verifies there are no sync calls for present times
1722// when they aren't supported and that an error is returned.
1723
1724TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1725 enableFrameTimestamps();
1726 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1727
1728 // Dequeue and queue frame 1.
1729 const uint64_t fId1 = getNextFrameId();
1730 dequeueAndQueue(0);
1731
1732 // Verify a query for the Present times do not trigger a sync call if they
1733 // are not supported.
1734 resetTimestamps();
1735 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1736 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1737 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1738 &outDisplayPresentTime, nullptr, nullptr);
1739 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1740 EXPECT_EQ(BAD_VALUE, result);
1741 EXPECT_EQ(-1, outDisplayPresentTime);
1742}
1743
Dan Stoza932f0082017-05-31 13:50:16 -07001744} // namespace android