blob: d5bdd74a8632b9a67a3d8c0251306954ce3bba87 [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 Wall78b72202019-03-15 14:58:34 -0700564 int64_t /*desiredPresentTime*/,
Marissa Wall3dad52d2019-03-22 14:03:19 -0700565 const cached_buffer_t& /*cachedBuffer*/,
566 const std::vector<ListenerCallbacks>& /*listenerCallbacks*/) override {
567 }
Marissa Wall17b4e452018-12-26 16:32:34 -0800568
Brian Anderson3da8d272016-07-28 16:20:47 -0700569 void bootFinished() override {}
570 bool authenticateSurfaceTexture(
571 const sp<IGraphicBufferProducer>& /*surface*/) const override {
572 return false;
573 }
Brian Anderson6b376712017-04-04 10:51:39 -0700574
575 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
576 const override {
577 *outSupported = {
578 FrameEvent::REQUESTED_PRESENT,
579 FrameEvent::ACQUIRE,
580 FrameEvent::LATCH,
581 FrameEvent::FIRST_REFRESH_START,
582 FrameEvent::LAST_REFRESH_START,
583 FrameEvent::GPU_COMPOSITION_DONE,
584 FrameEvent::DEQUEUE_READY,
585 FrameEvent::RELEASE
586 };
587 if (mSupportsPresent) {
588 outSupported->push_back(
589 FrameEvent::DISPLAY_PRESENT);
590 }
591 return NO_ERROR;
592 }
593
Brian Anderson3da8d272016-07-28 16:20:47 -0700594 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
595 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
596 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
597 status_t getDisplayStats(const sp<IBinder>& /*display*/,
598 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
599 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
600 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
601 override {
602 return NO_ERROR;
603 }
604 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700605 Vector<ColorMode>* /*outColorModes*/) override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700606 return NO_ERROR;
607 }
Daniel Solomon42d04562019-01-20 21:03:19 -0800608 status_t getDisplayNativePrimaries(const sp<IBinder>& /*display*/,
609 ui::DisplayPrimaries& /*primaries*/) override {
610 return NO_ERROR;
611 }
Peiyong Lina52f0292018-03-14 17:26:31 -0700612 ColorMode getActiveColorMode(const sp<IBinder>& /*display*/)
Brian Anderson3da8d272016-07-28 16:20:47 -0700613 override {
Peiyong Lina52f0292018-03-14 17:26:31 -0700614 return ColorMode::NATIVE;
Brian Anderson3da8d272016-07-28 16:20:47 -0700615 }
616 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700617 ColorMode /*colorMode*/) override { return NO_ERROR; }
Peiyong Lin0e003c92018-09-17 11:09:51 -0700618 status_t captureScreen(const sp<IBinder>& /*display*/, sp<GraphicBuffer>* /*outBuffer*/,
Robert Carr108b2c72019-04-02 16:32:58 -0700619 bool& /* outCapturedSecureLayers */,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700620 const ui::Dataspace /*reqDataspace*/,
621 const ui::PixelFormat /*reqPixelFormat*/, Rect /*sourceCrop*/,
622 uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrfa8855f2019-02-19 10:05:00 -0800623 bool /*useIdentityTransform*/, Rotation /*rotation*/,
624 bool /*captureSecureLayers*/) override {
Peiyong Lin0e003c92018-09-17 11:09:51 -0700625 return NO_ERROR;
626 }
Robert Carr866455f2019-04-02 16:28:26 -0700627 virtual status_t captureLayers(
628 const sp<IBinder>& /*parentHandle*/, sp<GraphicBuffer>* /*outBuffer*/,
629 const ui::Dataspace /*reqDataspace*/, const ui::PixelFormat /*reqPixelFormat*/,
630 const Rect& /*sourceCrop*/,
631 const std::unordered_set<sp<IBinder>,
632 ISurfaceComposer::SpHash<IBinder>>& /*excludeHandles*/,
633 float /*frameScale*/, bool /*childrenOnly*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700634 return NO_ERROR;
635 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700636 status_t clearAnimationFrameStats() override { return NO_ERROR; }
637 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
638 return NO_ERROR;
639 }
640 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
641 HdrCapabilities* /*outCapabilities*/) const override {
642 return NO_ERROR;
643 }
644 status_t enableVSyncInjections(bool /*enable*/) override {
645 return NO_ERROR;
646 }
647 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800648 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
649 return NO_ERROR;
650 }
Peiyong Linc6780972018-10-28 15:24:08 -0700651 status_t getCompositionPreference(
652 ui::Dataspace* /*outDefaultDataspace*/, ui::PixelFormat* /*outDefaultPixelFormat*/,
653 ui::Dataspace* /*outWideColorGamutDataspace*/,
654 ui::PixelFormat* /*outWideColorGamutPixelFormat*/) const override {
Peiyong Lin0256f722018-08-31 15:45:10 -0700655 return NO_ERROR;
656 }
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700657 status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& /*display*/,
658 ui::PixelFormat* /*outFormat*/,
659 ui::Dataspace* /*outDataspace*/,
660 uint8_t* /*outComponentMask*/) const override {
661 return NO_ERROR;
662 }
Kevin DuBois74e53772018-11-19 10:52:38 -0800663 status_t setDisplayContentSamplingEnabled(const sp<IBinder>& /*display*/, bool /*enable*/,
664 uint8_t /*componentMask*/,
665 uint64_t /*maxFrames*/) const override {
666 return NO_ERROR;
667 }
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700668 status_t getDisplayedContentSample(const sp<IBinder>& /*display*/, uint64_t /*maxFrames*/,
669 uint64_t /*timestamp*/,
670 DisplayedFrameStats* /*outStats*/) const override {
671 return NO_ERROR;
672 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700673
Peiyong Lin3c2791e2019-01-14 17:05:18 -0800674 status_t getColorManagement(bool* /*outGetColorManagement*/) const override { return NO_ERROR; }
675 status_t getProtectedContentSupport(bool* /*outSupported*/) const override { return NO_ERROR; }
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700676
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800677 status_t isWideColorDisplay(const sp<IBinder>&, bool*) const override { return NO_ERROR; }
Dan Gittik57e63c52019-01-18 16:37:54 +0000678 status_t getDisplayBrightnessSupport(const sp<IBinder>& /*displayToken*/,
679 bool* /*outSupport*/) const override {
680 return NO_ERROR;
681 }
682 status_t setDisplayBrightness(const sp<IBinder>& /*displayToken*/,
683 float /*brightness*/) const override {
684 return NO_ERROR;
685 }
Marissa Wallebc2c052019-01-16 19:16:55 -0800686
Dan Stoza84ab9372018-12-17 15:27:57 -0800687 status_t addRegionSamplingListener(const Rect& /*samplingArea*/,
688 const sp<IBinder>& /*stopLayerHandle*/,
689 const sp<IRegionSamplingListener>& /*listener*/) override {
690 return NO_ERROR;
691 }
692 status_t removeRegionSamplingListener(
693 const sp<IRegionSamplingListener>& /*listener*/) override {
694 return NO_ERROR;
695 }
Ady Abraham838de062019-02-04 10:24:03 -0800696 status_t setAllowedDisplayConfigs(const sp<IBinder>& /*displayToken*/,
697 const std::vector<int32_t>& /*allowedConfigs*/) override {
698 return NO_ERROR;
699 }
Ady Abrahamd9b3ea62019-02-26 14:08:03 -0800700 status_t getAllowedDisplayConfigs(const sp<IBinder>& /*displayToken*/,
701 std::vector<int32_t>* /*outAllowedConfigs*/) override {
702 return NO_ERROR;
703 }
Dan Stoza84ab9372018-12-17 15:27:57 -0800704
Brian Anderson3da8d272016-07-28 16:20:47 -0700705protected:
706 IBinder* onAsBinder() override { return nullptr; }
707
708private:
709 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700710};
711
712class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
713public:
Chih-Hung Hsiehaaf62162018-12-20 15:45:04 -0800714 explicit FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap) : mFenceMap(fenceMap) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700715
716 ~FakeProducerFrameEventHistory() {}
717
718 void updateAcquireFence(uint64_t frameNumber,
719 std::shared_ptr<FenceTime>&& acquire) override {
720 // Verify the acquire fence being added isn't the one from the consumer.
721 EXPECT_NE(mConsumerAcquireFence, acquire);
722 // Override the fence, so we can verify this was called by the
723 // producer after the frame is queued.
724 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
725 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
726 }
727
728 void setAcquireFenceOverride(
729 const std::shared_ptr<FenceTime>& acquireFenceOverride,
730 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
731 mAcquireFenceOverride = acquireFenceOverride;
732 mConsumerAcquireFence = consumerAcquireFence;
733 }
734
735protected:
736 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
737 const override {
738 return mFenceMap->createFenceTimeForTest(fence);
739 }
740
741 FenceToFenceTimeMap* mFenceMap{nullptr};
742
743 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
744 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
745};
746
747
748class TestSurface : public Surface {
749public:
750 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
751 FenceToFenceTimeMap* fenceMap)
752 : Surface(bufferProducer),
753 mFakeSurfaceComposer(new FakeSurfaceComposer) {
754 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
755 mFrameEventHistory.reset(mFakeFrameEventHistory);
756 }
757
758 ~TestSurface() override {}
759
760 sp<ISurfaceComposer> composerService() const override {
761 return mFakeSurfaceComposer;
762 }
763
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800764 nsecs_t now() const override {
765 return mNow;
766 }
767
768 void setNow(nsecs_t now) {
769 mNow = now;
770 }
771
Brian Anderson3da8d272016-07-28 16:20:47 -0700772public:
773 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800774 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700775
776 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
777 // but this raw pointer gives access to test functionality.
778 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
779};
780
781
Brian Andersond0010582017-03-07 13:20:31 -0800782class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700783protected:
784 struct FenceAndFenceTime {
785 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
786 : mFence(new Fence),
787 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
788 sp<Fence> mFence { nullptr };
789 std::shared_ptr<FenceTime> mFenceTime { nullptr };
790 };
791
792 struct RefreshEvents {
793 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800794 : mFenceMap(fenceMap),
795 kCompositorTiming(
796 {refreshStart, refreshStart + 1, refreshStart + 2 }),
797 kStartTime(refreshStart + 3),
798 kGpuCompositionDoneTime(refreshStart + 4),
799 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700800
801 void signalPostCompositeFences() {
802 mFenceMap.signalAllForTest(
803 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
804 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
805 }
806
807 FenceToFenceTimeMap& mFenceMap;
808
809 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
810 FenceAndFenceTime mPresent { mFenceMap };
811
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800812 const CompositorTiming kCompositorTiming;
813
Brian Anderson3da8d272016-07-28 16:20:47 -0700814 const nsecs_t kStartTime;
815 const nsecs_t kGpuCompositionDoneTime;
816 const nsecs_t kPresentTime;
817 };
818
819 struct FrameEvents {
820 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
821 : mFenceMap(fenceMap),
822 kPostedTime(frameStartTime + 100),
823 kRequestedPresentTime(frameStartTime + 200),
824 kProducerAcquireTime(frameStartTime + 300),
825 kConsumerAcquireTime(frameStartTime + 301),
826 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700827 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700828 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700829 mRefreshes {
830 { mFenceMap, frameStartTime + 410 },
831 { mFenceMap, frameStartTime + 420 },
832 { mFenceMap, frameStartTime + 430 } } {}
833
834 void signalQueueFences() {
835 mFenceMap.signalAllForTest(
836 mAcquireConsumer.mFence, kConsumerAcquireTime);
837 mFenceMap.signalAllForTest(
838 mAcquireProducer.mFence, kProducerAcquireTime);
839 }
840
841 void signalRefreshFences() {
842 for (auto& re : mRefreshes) {
843 re.signalPostCompositeFences();
844 }
845 }
846
847 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700848 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
849 }
850
851 FenceToFenceTimeMap& mFenceMap;
852
853 FenceAndFenceTime mAcquireConsumer { mFenceMap };
854 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700855 FenceAndFenceTime mRelease { mFenceMap };
856
857 const nsecs_t kPostedTime;
858 const nsecs_t kRequestedPresentTime;
859 const nsecs_t kProducerAcquireTime;
860 const nsecs_t kConsumerAcquireTime;
861 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700862 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700863 const nsecs_t kReleaseTime;
864
865 RefreshEvents mRefreshes[3];
866 };
867
Brian Andersond0010582017-03-07 13:20:31 -0800868 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700869
870 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700871 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
872 mFakeConsumer = new FakeConsumer;
873 mCfeh = &mFakeConsumer->mFrameEventHistory;
874 mConsumer->consumerConnect(mFakeConsumer, false);
875 mConsumer->setConsumerName(String8("TestConsumer"));
876 mSurface = new TestSurface(mProducer, &mFenceMap);
877 mWindow = mSurface;
878
879 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
880 NATIVE_WINDOW_API_CPU));
881 native_window_set_buffer_count(mWindow.get(), 4);
882 }
883
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800884 void disableFrameTimestamps() {
885 mFakeConsumer->mGetFrameTimestampsEnabled = false;
886 native_window_enable_frame_timestamps(mWindow.get(), 0);
887 mFrameTimestampsEnabled = false;
888 }
889
Brian Anderson3da8d272016-07-28 16:20:47 -0700890 void enableFrameTimestamps() {
891 mFakeConsumer->mGetFrameTimestampsEnabled = true;
892 native_window_enable_frame_timestamps(mWindow.get(), 1);
893 mFrameTimestampsEnabled = true;
894 }
895
Brian Anderson1049d1d2016-12-16 17:25:57 -0800896 int getAllFrameTimestamps(uint64_t frameId) {
897 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700898 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
899 &outFirstRefreshStartTime, &outLastRefreshStartTime,
900 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700901 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700902 }
903
Brian Anderson3da8d272016-07-28 16:20:47 -0700904 void resetTimestamps() {
905 outRequestedPresentTime = -1;
906 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700907 outLatchTime = -1;
908 outFirstRefreshStartTime = -1;
909 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700910 outGpuCompositionDoneTime = -1;
911 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700912 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700913 outReleaseTime = -1;
914 }
915
Brian Anderson1049d1d2016-12-16 17:25:57 -0800916 uint64_t getNextFrameId() {
917 uint64_t frameId = -1;
918 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
919 EXPECT_EQ(status, NO_ERROR);
920 return frameId;
921 }
922
Brian Anderson3da8d272016-07-28 16:20:47 -0700923 void dequeueAndQueue(uint64_t frameIndex) {
924 int fence = -1;
925 ANativeWindowBuffer* buffer = nullptr;
926 ASSERT_EQ(NO_ERROR,
927 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
928
929 int oldAddFrameTimestampsCount =
930 mFakeConsumer->mAddFrameTimestampsCount;
931
932 FrameEvents* frame = &mFrames[frameIndex];
933 uint64_t frameNumber = frameIndex + 1;
934
935 NewFrameEventsEntry fe;
936 fe.frameNumber = frameNumber;
937 fe.postedTime = frame->kPostedTime;
938 fe.requestedPresentTime = frame->kRequestedPresentTime;
939 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
940 mFakeConsumer->mNewFrameEntryOverride = fe;
941
942 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
943 frame->mAcquireProducer.mFenceTime,
944 frame->mAcquireConsumer.mFenceTime);
945
946 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
947
948 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
949
950 EXPECT_EQ(
951 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
952 mFakeConsumer->mAddFrameTimestampsCount);
953 }
954
955 void addFrameEvents(
956 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
957 FrameEvents* oldFrame =
958 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
959 FrameEvents* newFrame = &mFrames[iNewFrame];
960
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800961 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700962 uint64_t nNewFrame = iNewFrame + 1;
963
Brian Anderson4e606e32017-03-16 15:34:57 -0700964 // Latch, Composite, and Release the frames in a plausible order.
965 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700966 // that's okay for the purposes of this test.
967 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
968
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700969 // Composite the previous frame one more time, which helps verify
970 // LastRefresh is updated properly.
971 if (oldFrame != nullptr) {
972 mCfeh->addPreComposition(nOldFrame,
973 oldFrame->mRefreshes[2].kStartTime);
974 gpuDoneFenceTime = gpuComposited ?
975 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
976 FenceTime::NO_FENCE;
977 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800978 oldFrame->mRefreshes[2].mPresent.mFenceTime,
979 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700980 }
981
982 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700983 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
984
985 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
986 gpuDoneFenceTime = gpuComposited ?
987 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
988 FenceTime::NO_FENCE;
989 // HWC2 releases the previous buffer after a new latch just before
990 // calling postComposition.
991 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700992 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700993 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
994 }
995 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800996 newFrame->mRefreshes[0].mPresent.mFenceTime,
997 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700998
Brian Anderson3da8d272016-07-28 16:20:47 -0700999 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
1000 gpuDoneFenceTime = gpuComposited ?
1001 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
1002 FenceTime::NO_FENCE;
1003 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001004 newFrame->mRefreshes[1].mPresent.mFenceTime,
1005 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -07001006 }
1007
Brian Anderson3da8d272016-07-28 16:20:47 -07001008 sp<IGraphicBufferProducer> mProducer;
1009 sp<IGraphicBufferConsumer> mConsumer;
1010 sp<FakeConsumer> mFakeConsumer;
1011 ConsumerFrameEventHistory* mCfeh;
1012 sp<TestSurface> mSurface;
1013 sp<ANativeWindow> mWindow;
1014
1015 FenceToFenceTimeMap mFenceMap;
1016
1017 bool mFrameTimestampsEnabled = false;
1018
1019 int64_t outRequestedPresentTime = -1;
1020 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001021 int64_t outLatchTime = -1;
1022 int64_t outFirstRefreshStartTime = -1;
1023 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001024 int64_t outGpuCompositionDoneTime = -1;
1025 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001026 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001027 int64_t outReleaseTime = -1;
1028
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001029 FrameEvents mFrames[3] {
1030 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -07001031};
1032
1033
1034// This test verifies that the frame timestamps are not retrieved when not
1035// explicitly enabled via native_window_enable_frame_timestamps.
1036// We want to check this to make sure there's no overhead for users
1037// that don't need the timestamp information.
1038TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
1039 int fence;
1040 ANativeWindowBuffer* buffer;
1041
1042 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1043 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1044
Brian Anderson1049d1d2016-12-16 17:25:57 -08001045 const uint64_t fId = getNextFrameId();
1046
Brian Anderson3da8d272016-07-28 16:20:47 -07001047 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
1048 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1049 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1050 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1051
1052 // Verify the producer doesn't get frame timestamps piggybacked on queue.
1053 // It is okay that frame timestamps are added in the consumer since it is
1054 // still needed for SurfaceFlinger dumps.
1055 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1056 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1057 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1058
1059 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001060 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -07001061 EXPECT_EQ(INVALID_OPERATION, result);
1062 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001063
1064 // Verify compositor timing query fails.
1065 nsecs_t compositeDeadline = 0;
1066 nsecs_t compositeInterval = 0;
1067 nsecs_t compositeToPresentLatency = 0;
1068 result = native_window_get_compositor_timing(mWindow.get(),
1069 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1070 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001071}
1072
1073// This test verifies that the frame timestamps are retrieved if explicitly
1074// enabled via native_window_enable_frame_timestamps.
1075TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001076 CompositorTiming initialCompositorTiming {
1077 1000000000, // 1s deadline
1078 16666667, // 16ms interval
1079 50000000, // 50ms present latency
1080 };
1081 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1082
Brian Anderson3da8d272016-07-28 16:20:47 -07001083 enableFrameTimestamps();
1084
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001085 // Verify the compositor timing query gets the initial compositor values
1086 // after timststamps are enabled; even before the first frame is queued
1087 // or dequeued.
1088 nsecs_t compositeDeadline = 0;
1089 nsecs_t compositeInterval = 0;
1090 nsecs_t compositeToPresentLatency = 0;
1091 mSurface->setNow(initialCompositorTiming.deadline - 1);
1092 int result = native_window_get_compositor_timing(mWindow.get(),
1093 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1094 EXPECT_EQ(NO_ERROR, result);
1095 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1096 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1097 EXPECT_EQ(initialCompositorTiming.presentLatency,
1098 compositeToPresentLatency);
1099
Brian Anderson3da8d272016-07-28 16:20:47 -07001100 int fence;
1101 ANativeWindowBuffer* buffer;
1102
1103 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001104 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001105
Brian Anderson1049d1d2016-12-16 17:25:57 -08001106 const uint64_t fId1 = getNextFrameId();
1107
Brian Anderson3da8d272016-07-28 16:20:47 -07001108 // Verify getFrameTimestamps is piggybacked on dequeue.
1109 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1110 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001111 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001112
1113 NewFrameEventsEntry f1;
1114 f1.frameNumber = 1;
1115 f1.postedTime = mFrames[0].kPostedTime;
1116 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1117 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1118 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1119 mFrames[0].mAcquireProducer.mFenceTime,
1120 mFrames[0].mAcquireConsumer.mFenceTime);
1121 mFakeConsumer->mNewFrameEntryOverride = f1;
1122 mFrames[0].signalQueueFences();
1123
1124 // Verify getFrameTimestamps is piggybacked on queue.
1125 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1126 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1127 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001128 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001129
1130 // Verify queries for timestamps that the producer doesn't know about
1131 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001132 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001133 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001134 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001135}
1136
Brian Anderson6b376712017-04-04 10:51:39 -07001137TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1138 bool displayPresentSupported = true;
1139 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1140
1141 // Verify supported bits are forwarded.
1142 int supportsPresent = -1;
1143 mWindow.get()->query(mWindow.get(),
1144 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1145 EXPECT_EQ(displayPresentSupported, supportsPresent);
1146}
1147
1148TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1149 bool displayPresentSupported = false;
1150 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1151
1152 // Verify supported bits are forwarded.
1153 int supportsPresent = -1;
1154 mWindow.get()->query(mWindow.get(),
1155 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1156 EXPECT_EQ(displayPresentSupported, supportsPresent);
1157}
1158
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001159TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1160 nsecs_t phase = 4000;
1161 nsecs_t interval = 1000;
1162
1163 // Timestamp in previous interval.
1164 nsecs_t timestamp = 3500;
1165 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1166 timestamp, phase, interval));
1167
1168 // Timestamp in next interval.
1169 timestamp = 4500;
1170 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1171 timestamp, phase, interval));
1172
1173 // Timestamp multiple intervals before.
1174 timestamp = 2500;
1175 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1176 timestamp, phase, interval));
1177
1178 // Timestamp multiple intervals after.
1179 timestamp = 6500;
1180 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1181 timestamp, phase, interval));
1182
1183 // Timestamp on previous interval.
1184 timestamp = 3000;
1185 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1186 timestamp, phase, interval));
1187
1188 // Timestamp on next interval.
1189 timestamp = 5000;
1190 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1191 timestamp, phase, interval));
1192
1193 // Timestamp equal to phase.
1194 timestamp = 4000;
1195 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1196 timestamp, phase, interval));
1197}
1198
1199// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1200// if the number of intervals elapsed is internally stored in an int.
1201TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1202 nsecs_t phase = 0;
1203 nsecs_t interval = 4000;
1204 nsecs_t big_timestamp = 8635916564000;
1205 int32_t intervals = big_timestamp / interval;
1206
1207 EXPECT_LT(intervals, 0);
1208 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1209 big_timestamp, phase, interval));
1210 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1211 big_timestamp, big_timestamp, interval));
1212}
1213
1214// This verifies the compositor timing is updated by refresh events
1215// and piggy backed on a queue, dequeue, and enabling of timestamps..
1216TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1217 CompositorTiming initialCompositorTiming {
1218 1000000000, // 1s deadline
1219 16666667, // 16ms interval
1220 50000000, // 50ms present latency
1221 };
1222 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1223
1224 enableFrameTimestamps();
1225
1226 // We get the initial values before any frames are submitted.
1227 nsecs_t compositeDeadline = 0;
1228 nsecs_t compositeInterval = 0;
1229 nsecs_t compositeToPresentLatency = 0;
1230 mSurface->setNow(initialCompositorTiming.deadline - 1);
1231 int result = native_window_get_compositor_timing(mWindow.get(),
1232 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1233 EXPECT_EQ(NO_ERROR, result);
1234 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1235 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1236 EXPECT_EQ(initialCompositorTiming.presentLatency,
1237 compositeToPresentLatency);
1238
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001239 dequeueAndQueue(0);
1240 addFrameEvents(true, NO_FRAME_INDEX, 0);
1241
1242 // Still get the initial values because the frame events for frame 0
1243 // didn't get a chance to piggyback on a queue or dequeue yet.
1244 result = native_window_get_compositor_timing(mWindow.get(),
1245 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1246 EXPECT_EQ(NO_ERROR, result);
1247 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1248 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1249 EXPECT_EQ(initialCompositorTiming.presentLatency,
1250 compositeToPresentLatency);
1251
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001252 dequeueAndQueue(1);
1253 addFrameEvents(true, 0, 1);
1254
1255 // Now expect the composite values associated with frame 1.
1256 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1257 result = native_window_get_compositor_timing(mWindow.get(),
1258 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1259 EXPECT_EQ(NO_ERROR, result);
1260 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1261 compositeDeadline);
1262 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1263 compositeInterval);
1264 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1265 compositeToPresentLatency);
1266
1267 dequeueAndQueue(2);
1268 addFrameEvents(true, 1, 2);
1269
1270 // Now expect the composite values associated with frame 2.
1271 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1272 result = native_window_get_compositor_timing(mWindow.get(),
1273 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1274 EXPECT_EQ(NO_ERROR, result);
1275 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1276 compositeDeadline);
1277 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1278 compositeInterval);
1279 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1280 compositeToPresentLatency);
1281
1282 // Re-enabling frame timestamps should get the latest values.
1283 disableFrameTimestamps();
1284 enableFrameTimestamps();
1285
1286 // Now expect the composite values associated with frame 3.
1287 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1288 result = native_window_get_compositor_timing(mWindow.get(),
1289 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1290 EXPECT_EQ(NO_ERROR, result);
1291 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1292 compositeDeadline);
1293 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1294 compositeInterval);
1295 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1296 compositeToPresentLatency);
1297}
1298
1299// This verifies the compositor deadline properly snaps to the the next
1300// deadline based on the current time.
1301TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1302 CompositorTiming initialCompositorTiming {
1303 1000000000, // 1s deadline
1304 16666667, // 16ms interval
1305 50000000, // 50ms present latency
1306 };
1307 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1308
1309 enableFrameTimestamps();
1310
1311 nsecs_t compositeDeadline = 0;
1312 nsecs_t compositeInterval = 0;
1313 nsecs_t compositeToPresentLatency = 0;
1314
1315 // A "now" just before the deadline snaps to the deadline.
1316 mSurface->setNow(initialCompositorTiming.deadline - 1);
1317 int result = native_window_get_compositor_timing(mWindow.get(),
1318 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1319 EXPECT_EQ(NO_ERROR, result);
1320 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1321 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1322 EXPECT_EQ(expectedDeadline, compositeDeadline);
1323
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001324 dequeueAndQueue(0);
1325 addFrameEvents(true, NO_FRAME_INDEX, 0);
1326
1327 // A "now" just after the deadline snaps properly.
1328 mSurface->setNow(initialCompositorTiming.deadline + 1);
1329 result = native_window_get_compositor_timing(mWindow.get(),
1330 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1331 EXPECT_EQ(NO_ERROR, result);
1332 expectedDeadline =
1333 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1334 EXPECT_EQ(expectedDeadline, compositeDeadline);
1335
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001336 dequeueAndQueue(1);
1337 addFrameEvents(true, 0, 1);
1338
1339 // A "now" just after the next interval snaps properly.
1340 mSurface->setNow(
1341 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1342 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1343 result = native_window_get_compositor_timing(mWindow.get(),
1344 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1345 EXPECT_EQ(NO_ERROR, result);
1346 expectedDeadline =
1347 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1348 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1349 EXPECT_EQ(expectedDeadline, compositeDeadline);
1350
1351 dequeueAndQueue(2);
1352 addFrameEvents(true, 1, 2);
1353
1354 // A "now" over 1 interval before the deadline snaps properly.
1355 mSurface->setNow(
1356 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1357 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1358 result = native_window_get_compositor_timing(mWindow.get(),
1359 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1360 EXPECT_EQ(NO_ERROR, result);
1361 expectedDeadline =
1362 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1363 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1364 EXPECT_EQ(expectedDeadline, compositeDeadline);
1365
1366 // Re-enabling frame timestamps should get the latest values.
1367 disableFrameTimestamps();
1368 enableFrameTimestamps();
1369
1370 // A "now" over 2 intervals before the deadline snaps properly.
1371 mSurface->setNow(
1372 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1373 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1374 result = native_window_get_compositor_timing(mWindow.get(),
1375 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1376 EXPECT_EQ(NO_ERROR, result);
1377 expectedDeadline =
1378 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1379 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1380 EXPECT_EQ(expectedDeadline, compositeDeadline);
1381}
1382
Brian Anderson1049d1d2016-12-16 17:25:57 -08001383// This verifies the timestamps recorded in the consumer's
1384// FrameTimestampsHistory are properly retrieved by the producer for the
1385// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001386TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1387 enableFrameTimestamps();
1388
Brian Anderson1049d1d2016-12-16 17:25:57 -08001389 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001390 dequeueAndQueue(0);
1391 mFrames[0].signalQueueFences();
1392
Brian Anderson1049d1d2016-12-16 17:25:57 -08001393 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001394 dequeueAndQueue(1);
1395 mFrames[1].signalQueueFences();
1396
1397 addFrameEvents(true, NO_FRAME_INDEX, 0);
1398 mFrames[0].signalRefreshFences();
1399 addFrameEvents(true, 0, 1);
1400 mFrames[0].signalReleaseFences();
1401 mFrames[1].signalRefreshFences();
1402
1403 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001404 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001405 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001406 EXPECT_EQ(NO_ERROR, result);
1407 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1408 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001409 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1410 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1411 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001412 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1413 outGpuCompositionDoneTime);
1414 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001415 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001416 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1417
1418 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001419 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001420 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001421 EXPECT_EQ(NO_ERROR, result);
1422 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1423 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001424 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1425 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1426 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001427 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1428 outGpuCompositionDoneTime);
1429 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001430 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1431 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001432}
1433
1434// This test verifies the acquire fence recorded by the consumer is not sent
1435// back to the producer and the producer saves its own fence.
1436TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1437 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001438
Brian Anderson3da8d272016-07-28 16:20:47 -07001439 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001440 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001441 dequeueAndQueue(0);
1442
1443 // Verify queue-related timestamps for f1 are available immediately in the
1444 // producer without asking the consumer again, even before signaling the
1445 // acquire fence.
1446 resetTimestamps();
1447 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001448 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001449 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001450 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001451 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1452 EXPECT_EQ(NO_ERROR, result);
1453 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001454 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001455
1456 // Signal acquire fences. Verify a sync call still isn't necessary.
1457 mFrames[0].signalQueueFences();
1458
1459 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001460 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001461 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001462 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001463 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1464 EXPECT_EQ(NO_ERROR, result);
1465 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1466 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1467
1468 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001469 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001470 dequeueAndQueue(1);
1471
1472 // Verify queue-related timestamps for f2 are available immediately in the
1473 // producer without asking the consumer again, even before signaling the
1474 // acquire fence.
1475 resetTimestamps();
1476 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001477 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001478 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001479 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001480 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1481 EXPECT_EQ(NO_ERROR, result);
1482 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001483 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001484
1485 // Signal acquire fences. Verify a sync call still isn't necessary.
1486 mFrames[1].signalQueueFences();
1487
1488 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001489 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001490 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001491 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001492 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1493 EXPECT_EQ(NO_ERROR, result);
1494 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1495 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1496}
1497
1498TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1499 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001500
1501 // Dequeue and queue frame 1.
1502 dequeueAndQueue(0);
1503 mFrames[0].signalQueueFences();
1504
1505 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001506 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001507 dequeueAndQueue(1);
1508 mFrames[1].signalQueueFences();
1509
1510 addFrameEvents(true, NO_FRAME_INDEX, 0);
1511 mFrames[0].signalRefreshFences();
1512 addFrameEvents(true, 0, 1);
1513 mFrames[0].signalReleaseFences();
1514 mFrames[1].signalRefreshFences();
1515
1516 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001517 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001518 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001519 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1520 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001521 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001522 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1523}
1524
1525// This test verifies that fences can signal and update timestamps producer
1526// side without an additional sync call to the consumer.
1527TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1528 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001529
1530 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001531 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001532 dequeueAndQueue(0);
1533 mFrames[0].signalQueueFences();
1534
1535 // Dequeue and queue frame 2.
1536 dequeueAndQueue(1);
1537 mFrames[1].signalQueueFences();
1538
1539 addFrameEvents(true, NO_FRAME_INDEX, 0);
1540 addFrameEvents(true, 0, 1);
1541
1542 // Verify available timestamps are correct for frame 1, before any
1543 // fence has been signaled.
1544 // Note: A sync call is necessary here since the events triggered by
1545 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001546 resetTimestamps();
1547 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001548 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001549 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1550 EXPECT_EQ(NO_ERROR, result);
1551 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1552 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001553 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1554 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1555 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001556 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1557 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001558 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001559 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001560
1561 // Verify available timestamps are correct for frame 1 again, before any
1562 // fence has been signaled.
1563 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001564 resetTimestamps();
1565 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001566 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001567 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1568 EXPECT_EQ(NO_ERROR, result);
1569 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1570 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001571 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1572 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1573 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001574 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1575 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001576 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001577 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001578
1579 // Signal the fences for frame 1.
1580 mFrames[0].signalRefreshFences();
1581 mFrames[0].signalReleaseFences();
1582
1583 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001584 resetTimestamps();
1585 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001586 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001587 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1588 EXPECT_EQ(NO_ERROR, result);
1589 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1590 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001591 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1592 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1593 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001594 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1595 outGpuCompositionDoneTime);
1596 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001597 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001598 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1599}
1600
1601// This test verifies that if the frame wasn't GPU composited but has a refresh
1602// event a sync call isn't made to get the GPU composite done time since it will
1603// never exist.
1604TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1605 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001606
Brian Anderson3da8d272016-07-28 16:20:47 -07001607 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001608 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001609 dequeueAndQueue(0);
1610 mFrames[0].signalQueueFences();
1611
1612 // Dequeue and queue frame 2.
1613 dequeueAndQueue(1);
1614 mFrames[1].signalQueueFences();
1615
1616 addFrameEvents(false, NO_FRAME_INDEX, 0);
1617 addFrameEvents(false, 0, 1);
1618
1619 // Verify available timestamps are correct for frame 1, before any
1620 // fence has been signaled.
1621 // Note: A sync call is necessary here since the events triggered by
1622 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1623 resetTimestamps();
1624 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001625 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001626 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1627 EXPECT_EQ(NO_ERROR, result);
1628 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1629 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001630 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1631 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1632 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001633 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1634 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001635 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001636 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001637
1638 // Signal the fences for frame 1.
1639 mFrames[0].signalRefreshFences();
1640 mFrames[0].signalReleaseFences();
1641
1642 // Verify all timestamps, except GPU composition, are available without a
1643 // sync call.
1644 resetTimestamps();
1645 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001646 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001647 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1648 EXPECT_EQ(NO_ERROR, result);
1649 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1650 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001651 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1652 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1653 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001654 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001655 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001656 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001657 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1658}
1659
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001660// This test verifies that if the certain timestamps can't possibly exist for
1661// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001662TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001663 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001664
1665 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001666 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001667 dequeueAndQueue(0);
1668 mFrames[0].signalQueueFences();
1669
1670 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001671 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001672 dequeueAndQueue(1);
1673 mFrames[1].signalQueueFences();
1674
1675 addFrameEvents(false, NO_FRAME_INDEX, 0);
1676 addFrameEvents(false, 0, 1);
1677
1678 // Verify available timestamps are correct for frame 1, before any
1679 // fence has been signaled.
1680 // Note: A sync call is necessary here since the events triggered by
1681 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001682 resetTimestamps();
1683 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001684 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001685 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1686 EXPECT_EQ(NO_ERROR, result);
1687 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1688 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001689 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1690 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1691 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001692 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1693 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001694 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001695 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001696
1697 mFrames[0].signalRefreshFences();
1698 mFrames[0].signalReleaseFences();
1699 mFrames[1].signalRefreshFences();
1700
Brian Anderson1049d1d2016-12-16 17:25:57 -08001701 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001702 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001703 // available, a sync call should not occur because it's not possible for f2
1704 // to encounter the final value for those events until another frame is
1705 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001706 resetTimestamps();
1707 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001708 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001709 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1710 EXPECT_EQ(NO_ERROR, result);
1711 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1712 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001713 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1714 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1715 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001716 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001717 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001718 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1719 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001720}
1721
Brian Anderson6b376712017-04-04 10:51:39 -07001722// This test verifies there are no sync calls for present times
1723// when they aren't supported and that an error is returned.
1724
1725TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1726 enableFrameTimestamps();
1727 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1728
1729 // Dequeue and queue frame 1.
1730 const uint64_t fId1 = getNextFrameId();
1731 dequeueAndQueue(0);
1732
1733 // Verify a query for the Present times do not trigger a sync call if they
1734 // are not supported.
1735 resetTimestamps();
1736 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1737 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1738 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1739 &outDisplayPresentTime, nullptr, nullptr);
1740 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1741 EXPECT_EQ(BAD_VALUE, result);
1742 EXPECT_EQ(-1, outDisplayPresentTime);
1743}
1744
Dan Stoza932f0082017-05-31 13:50:16 -07001745} // namespace android