blob: 4fb51e1b96631985bf028e4b2fa6f86e6aab19ee [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;
45
Robert Carr4cdc58f2017-08-23 14:22:20 -070046using Transaction = SurfaceComposerClient::Transaction;
47
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060048static bool hasWideColorDisplay =
49 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
Kalle Raita643f0942016-12-07 09:20:14 -080050
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -070051static bool hasHdrDisplay =
52 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
53
Brian Anderson3da8d272016-07-28 16:20:47 -070054class FakeSurfaceComposer;
55class FakeProducerFrameEventHistory;
56
57static constexpr uint64_t NO_FRAME_INDEX = std::numeric_limits<uint64_t>::max();
58
Jamie Gennis134f0422011-03-08 12:18:54 -080059class SurfaceTest : public ::testing::Test {
60protected:
Mathias Agopian7c1a4872013-03-20 15:56:04 -070061
62 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
75 ASSERT_TRUE(mSurfaceControl != NULL);
76 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();
84 ASSERT_TRUE(mSurface != NULL);
85 }
86
87 virtual void TearDown() {
88 mComposerClient->dispose();
89 }
90
91 sp<Surface> mSurface;
92 sp<SurfaceComposerClient> mComposerClient;
93 sp<SurfaceControl> mSurfaceControl;
94};
95
96TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
97 sp<ANativeWindow> anw(mSurface);
98 int result = -123;
99 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
100 &result);
101 EXPECT_EQ(NO_ERROR, err);
102 EXPECT_EQ(1, result);
103}
104
105TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
106 mSurfaceControl.clear();
Kalle Raita643f0942016-12-07 09:20:14 -0800107 // Wait for the async clean-up to complete.
108 std::this_thread::sleep_for(50ms);
Jamie Gennis134f0422011-03-08 12:18:54 -0800109
110 sp<ANativeWindow> anw(mSurface);
111 int result = -123;
112 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
113 &result);
114 EXPECT_EQ(NO_ERROR, err);
115 EXPECT_EQ(1, result);
116}
117
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800118// This test probably doesn't belong here.
Jamie Gennisc901ca02011-10-11 16:02:31 -0700119TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800120 sp<ANativeWindow> anw(mSurface);
121
122 // Verify the screenshot works with no protected buffers.
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800123 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Brian Anderson3da8d272016-07-28 16:20:47 -0700124 sp<IBinder> display(sf->getBuiltInDisplay(
125 ISurfaceComposer::eDisplayIdMain));
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000126 sp<GraphicBuffer> outBuffer;
127 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &outBuffer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800128 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800129
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700130 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
131 NATIVE_WINDOW_API_CPU));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800132 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
133 // that we need to dequeue a buffer in order for it to actually get
134 // allocated in SurfaceFlinger.
135 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
136 GRALLOC_USAGE_PROTECTED));
137 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Iliyan Malchev697526b2011-05-01 11:33:26 -0700138 ANativeWindowBuffer* buf = 0;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700139
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700140 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700141 if (err) {
142 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
143 // that's okay as long as this is the reason for the failure.
144 // try again without the GRALLOC_USAGE_PROTECTED bit.
145 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700146 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
147 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700148 return;
149 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700150 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700151
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800152 for (int i = 0; i < 4; i++) {
153 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700154 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
155 &buf));
156 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800157 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000158 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &outBuffer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800159 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800160}
161
Jamie Gennis391bbe22011-03-14 15:00:06 -0700162TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
163 sp<ANativeWindow> anw(mSurface);
164 int result = -123;
165 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
166 EXPECT_EQ(NO_ERROR, err);
167 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
168}
169
Craig Donner6ebc46a2016-10-21 15:23:44 -0700170TEST_F(SurfaceTest, LayerCountIsOne) {
171 sp<ANativeWindow> anw(mSurface);
172 int result = -123;
173 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
174 EXPECT_EQ(NO_ERROR, err);
175 EXPECT_EQ(1, result);
176}
177
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700178TEST_F(SurfaceTest, QueryConsumerUsage) {
179 const int TEST_USAGE_FLAGS =
180 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700181 sp<IGraphicBufferProducer> producer;
182 sp<IGraphicBufferConsumer> consumer;
183 BufferQueue::createBufferQueue(&producer, &consumer);
184 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700185 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700186 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700187
188 sp<ANativeWindow> anw(s);
189
190 int flags = -1;
191 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
192
193 ASSERT_EQ(NO_ERROR, err);
194 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
195}
196
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800197TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
198 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
199 sp<IGraphicBufferProducer> producer;
200 sp<IGraphicBufferConsumer> consumer;
201 BufferQueue::createBufferQueue(&producer, &consumer);
202 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
203
204 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
205
206 sp<Surface> s = new Surface(producer);
207
208 sp<ANativeWindow> anw(s);
209
210 android_dataspace dataSpace;
211
212 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
213 reinterpret_cast<int*>(&dataSpace));
214
215 ASSERT_EQ(NO_ERROR, err);
216 ASSERT_EQ(TEST_DATASPACE, dataSpace);
217}
218
Dan Stoza812ed062015-06-02 15:45:22 -0700219TEST_F(SurfaceTest, SettingGenerationNumber) {
220 sp<IGraphicBufferProducer> producer;
221 sp<IGraphicBufferConsumer> consumer;
222 BufferQueue::createBufferQueue(&producer, &consumer);
223 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
224 sp<Surface> surface = new Surface(producer);
225 sp<ANativeWindow> window(surface);
226
227 // Allocate a buffer with a generation number of 0
228 ANativeWindowBuffer* buffer;
229 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700230 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
231 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700232 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
233 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
234
235 // Detach the buffer and check its generation number
236 sp<GraphicBuffer> graphicBuffer;
237 sp<Fence> fence;
238 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
239 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
240
241 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
242 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
243
244 // This should change the generation number of the GraphicBuffer
245 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
246
247 // Check that the new generation number sticks with the buffer
248 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
249 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
250 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
251 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
252}
253
Dan Stozac6f30bd2015-06-08 09:32:50 -0700254TEST_F(SurfaceTest, GetConsumerName) {
255 sp<IGraphicBufferProducer> producer;
256 sp<IGraphicBufferConsumer> consumer;
257 BufferQueue::createBufferQueue(&producer, &consumer);
258
259 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
260 consumer->consumerConnect(dummyConsumer, false);
261 consumer->setConsumerName(String8("TestConsumer"));
262
263 sp<Surface> surface = new Surface(producer);
264 sp<ANativeWindow> window(surface);
265 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
266
267 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
268}
269
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700270TEST_F(SurfaceTest, GetWideColorSupport) {
271 sp<IGraphicBufferProducer> producer;
272 sp<IGraphicBufferConsumer> consumer;
273 BufferQueue::createBufferQueue(&producer, &consumer);
274
275 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
276 consumer->consumerConnect(dummyConsumer, false);
277 consumer->setConsumerName(String8("TestConsumer"));
278
279 sp<Surface> surface = new Surface(producer);
280 sp<ANativeWindow> window(surface);
281 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
282
283 bool supported;
284 surface->getWideColorSupport(&supported);
285
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -0600286 // NOTE: This test assumes that device that supports
287 // wide-color (as indicated by BoardConfig) must also
288 // have a wide-color primary display.
289 // That assumption allows this test to cover devices
290 // that advertised a wide-color color mode without
291 // actually supporting wide-color to pass this test
292 // as well as the case of a device that does support
293 // wide-color (via BoardConfig) and has a wide-color
294 // primary display.
295 // NOT covered at this time is a device that supports
296 // wide color in the BoardConfig but does not support
297 // a wide-color color mode on the primary display.
298 ASSERT_EQ(hasWideColorDisplay, supported);
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700299}
300
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700301TEST_F(SurfaceTest, GetHdrSupport) {
302 sp<IGraphicBufferProducer> producer;
303 sp<IGraphicBufferConsumer> consumer;
304 BufferQueue::createBufferQueue(&producer, &consumer);
305
306 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
307 consumer->consumerConnect(dummyConsumer, false);
308 consumer->setConsumerName(String8("TestConsumer"));
309
310 sp<Surface> surface = new Surface(producer);
311 sp<ANativeWindow> window(surface);
312 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
313
314 bool supported;
315 status_t result = surface->getHdrSupport(&supported);
316 ASSERT_EQ(NO_ERROR, result);
317
318 // NOTE: This is not a CTS test.
319 // This test verifies that when the BoardConfig TARGET_HAS_HDR_DISPLAY
320 // is TRUE, getHdrSupport is also true.
321 // TODO: Add check for an HDR color mode on the primary display.
322 ASSERT_EQ(hasHdrDisplay, supported);
323}
324
325TEST_F(SurfaceTest, SetHdrMetadata) {
326 sp<IGraphicBufferProducer> producer;
327 sp<IGraphicBufferConsumer> consumer;
328 BufferQueue::createBufferQueue(&producer, &consumer);
329
330 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
331 consumer->consumerConnect(dummyConsumer, false);
332 consumer->setConsumerName(String8("TestConsumer"));
333
334 sp<Surface> surface = new Surface(producer);
335 sp<ANativeWindow> window(surface);
336 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
337
338 bool supported;
339 status_t result = surface->getHdrSupport(&supported);
340 ASSERT_EQ(NO_ERROR, result);
341
342 if (!hasHdrDisplay || !supported) {
343 return;
344 }
345 const android_smpte2086_metadata smpte2086 = {
346 {0.680, 0.320},
347 {0.265, 0.690},
348 {0.150, 0.060},
349 {0.3127, 0.3290},
350 100.0,
351 0.1,
352 };
353 const android_cta861_3_metadata cta861_3 = {
354 78.0,
355 62.0,
356 };
357 int error = native_window_set_buffers_smpte2086_metadata(window.get(), &smpte2086);
358 ASSERT_EQ(error, NO_ERROR);
359 error = native_window_set_buffers_cta861_3_metadata(window.get(), &cta861_3);
360 ASSERT_EQ(error, NO_ERROR);
361}
362
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800363TEST_F(SurfaceTest, DynamicSetBufferCount) {
364 sp<IGraphicBufferProducer> producer;
365 sp<IGraphicBufferConsumer> consumer;
366 BufferQueue::createBufferQueue(&producer, &consumer);
367
368 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
369 consumer->consumerConnect(dummyConsumer, false);
370 consumer->setConsumerName(String8("TestConsumer"));
371
372 sp<Surface> surface = new Surface(producer);
373 sp<ANativeWindow> window(surface);
374
375 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
376 NATIVE_WINDOW_API_CPU));
377 native_window_set_buffer_count(window.get(), 4);
378
379 int fence;
380 ANativeWindowBuffer* buffer;
381 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
382 native_window_set_buffer_count(window.get(), 3);
383 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
384 native_window_set_buffer_count(window.get(), 2);
385 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
386 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
387}
388
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700389TEST_F(SurfaceTest, GetAndFlushRemovedBuffers) {
390 sp<IGraphicBufferProducer> producer;
391 sp<IGraphicBufferConsumer> consumer;
392 BufferQueue::createBufferQueue(&producer, &consumer);
393
394 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
395 consumer->consumerConnect(dummyConsumer, false);
396 consumer->setConsumerName(String8("TestConsumer"));
397
398 sp<Surface> surface = new Surface(producer);
399 sp<ANativeWindow> window(surface);
400 sp<DummyProducerListener> listener = new DummyProducerListener();
401 ASSERT_EQ(OK, surface->connect(
402 NATIVE_WINDOW_API_CPU,
403 /*listener*/listener,
404 /*reportBufferRemoval*/true));
405 const int BUFFER_COUNT = 4;
406 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
407
408 sp<GraphicBuffer> detachedBuffer;
409 sp<Fence> outFence;
410 int fences[BUFFER_COUNT];
411 ANativeWindowBuffer* buffers[BUFFER_COUNT];
412 // Allocate buffers because detachNextBuffer requires allocated buffers
413 for (int i = 0; i < BUFFER_COUNT; i++) {
414 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
415 }
416 for (int i = 0; i < BUFFER_COUNT; i++) {
417 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
418 }
419
420 // Test detached buffer is correctly reported
421 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
422 std::vector<sp<GraphicBuffer>> removedBuffers;
423 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
424 ASSERT_EQ(1u, removedBuffers.size());
425 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
426 // Test the list is flushed one getAndFlushRemovedBuffers returns
427 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
428 ASSERT_EQ(0u, removedBuffers.size());
429
430
431 // Test removed buffer list is cleanup after next dequeueBuffer call
432 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
433 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[0], &fences[0]));
434 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
435 ASSERT_EQ(0u, removedBuffers.size());
436 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[0], fences[0]));
437
438 // Test removed buffer list is cleanup after next detachNextBuffer call
439 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
440 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
441 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
442 ASSERT_EQ(1u, removedBuffers.size());
443 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
444
445 // Re-allocate buffers since all buffers are detached up to now
446 for (int i = 0; i < BUFFER_COUNT; i++) {
447 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
448 }
449 for (int i = 0; i < BUFFER_COUNT; i++) {
450 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
451 }
452
453 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
454 ASSERT_EQ(NO_ERROR, surface->attachBuffer(detachedBuffer.get()));
455 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
456 // Depends on which slot GraphicBufferProducer impl pick, the attach call might
457 // get 0 or 1 buffer removed.
458 ASSERT_LE(removedBuffers.size(), 1u);
459}
Brian Anderson3da8d272016-07-28 16:20:47 -0700460
Dan Stoza932f0082017-05-31 13:50:16 -0700461TEST_F(SurfaceTest, TestGetLastDequeueStartTime) {
462 sp<ANativeWindow> anw(mSurface);
463 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU));
464
465 ANativeWindowBuffer* buffer = nullptr;
466 int32_t fenceFd = -1;
467
468 nsecs_t before = systemTime(CLOCK_MONOTONIC);
469 anw->dequeueBuffer(anw.get(), &buffer, &fenceFd);
470 nsecs_t after = systemTime(CLOCK_MONOTONIC);
471
472 nsecs_t lastDequeueTime = mSurface->getLastDequeueStartTime();
473 ASSERT_LE(before, lastDequeueTime);
474 ASSERT_GE(after, lastDequeueTime);
475}
476
Brian Anderson3da8d272016-07-28 16:20:47 -0700477class FakeConsumer : public BnConsumerListener {
478public:
479 void onFrameAvailable(const BufferItem& /*item*/) override {}
480 void onBuffersReleased() override {}
481 void onSidebandStreamChanged() override {}
482
483 void addAndGetFrameTimestamps(
484 const NewFrameEventsEntry* newTimestamps,
485 FrameEventHistoryDelta* outDelta) override {
486 if (newTimestamps) {
487 if (mGetFrameTimestampsEnabled) {
488 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
489 "Test should set mNewFrameEntryOverride before queuing "
490 "a frame.";
491 EXPECT_EQ(newTimestamps->frameNumber,
492 mNewFrameEntryOverride.frameNumber) <<
493 "Test attempting to add NewFrameEntryOverride with "
494 "incorrect frame number.";
495 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
496 mNewFrameEntryOverride.frameNumber = 0;
497 }
498 mAddFrameTimestampsCount++;
499 mLastAddedFrameNumber = newTimestamps->frameNumber;
500 }
501 if (outDelta) {
502 mFrameEventHistory.getAndResetDelta(outDelta);
503 mGetFrameTimestampsCount++;
504 }
505 mAddAndGetFrameTimestampsCallCount++;
506 }
507
508 bool mGetFrameTimestampsEnabled = false;
509
510 ConsumerFrameEventHistory mFrameEventHistory;
511 int mAddAndGetFrameTimestampsCallCount = 0;
512 int mAddFrameTimestampsCount = 0;
513 int mGetFrameTimestampsCount = 0;
514 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
515
516 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
517};
518
519
520class FakeSurfaceComposer : public ISurfaceComposer{
521public:
522 ~FakeSurfaceComposer() override {}
523
Brian Anderson6b376712017-04-04 10:51:39 -0700524 void setSupportsPresent(bool supportsPresent) {
525 mSupportsPresent = supportsPresent;
526 }
527
Brian Anderson3da8d272016-07-28 16:20:47 -0700528 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800529 sp<ISurfaceComposerClient> createScopedConnection(
530 const sp<IGraphicBufferProducer>& /* parent */) override {
531 return nullptr;
532 }
Jorim Jaggib1e2f8d2017-06-08 15:43:59 -0700533 sp<IDisplayEventConnection> createDisplayEventConnection(ISurfaceComposer::VsyncSource)
534 override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700535 return nullptr;
536 }
537 sp<IBinder> createDisplay(const String8& /*displayName*/,
538 bool /*secure*/) override { return nullptr; }
539 void destroyDisplay(const sp<IBinder>& /*display */) override {}
540 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
541 void setTransactionState(const Vector<ComposerState>& /*state*/,
542 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
543 override {}
544 void bootFinished() override {}
545 bool authenticateSurfaceTexture(
546 const sp<IGraphicBufferProducer>& /*surface*/) const override {
547 return false;
548 }
Brian Anderson6b376712017-04-04 10:51:39 -0700549
550 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
551 const override {
552 *outSupported = {
553 FrameEvent::REQUESTED_PRESENT,
554 FrameEvent::ACQUIRE,
555 FrameEvent::LATCH,
556 FrameEvent::FIRST_REFRESH_START,
557 FrameEvent::LAST_REFRESH_START,
558 FrameEvent::GPU_COMPOSITION_DONE,
559 FrameEvent::DEQUEUE_READY,
560 FrameEvent::RELEASE
561 };
562 if (mSupportsPresent) {
563 outSupported->push_back(
564 FrameEvent::DISPLAY_PRESENT);
565 }
566 return NO_ERROR;
567 }
568
Brian Anderson3da8d272016-07-28 16:20:47 -0700569 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
570 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
571 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
572 status_t getDisplayStats(const sp<IBinder>& /*display*/,
573 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
574 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
575 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
576 override {
577 return NO_ERROR;
578 }
579 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
580 Vector<android_color_mode_t>* /*outColorModes*/) override {
581 return NO_ERROR;
582 }
583 android_color_mode_t getActiveColorMode(const sp<IBinder>& /*display*/)
584 override {
585 return HAL_COLOR_MODE_NATIVE;
586 }
587 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
588 android_color_mode_t /*colorMode*/) override { return NO_ERROR; }
589 status_t captureScreen(const sp<IBinder>& /*display*/,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000590 sp<GraphicBuffer>* /*outBuffer*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700591 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800592 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700593 bool /*useIdentityTransform*/,
594 Rotation /*rotation*/) override { return NO_ERROR; }
chaviwa76b2712017-09-20 12:02:26 -0700595 virtual status_t captureLayers(const sp<IBinder>& /*parentHandle*/,
Robert Carr578038f2018-03-09 12:25:24 -0800596 sp<GraphicBuffer>* /*outBuffer*/, const Rect& /*sourceCrop*/,
597 float /*frameScale*/, bool /*childrenOnly*/) override {
chaviwa76b2712017-09-20 12:02:26 -0700598 return NO_ERROR;
599 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700600 status_t clearAnimationFrameStats() override { return NO_ERROR; }
601 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
602 return NO_ERROR;
603 }
604 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
605 HdrCapabilities* /*outCapabilities*/) const override {
606 return NO_ERROR;
607 }
608 status_t enableVSyncInjections(bool /*enable*/) override {
609 return NO_ERROR;
610 }
611 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Kalle Raitaa099a242017-01-11 11:17:29 -0800612 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) const override {
613 return NO_ERROR;
614 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700615
616protected:
617 IBinder* onAsBinder() override { return nullptr; }
618
619private:
620 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700621};
622
623class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
624public:
625 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
626 : mFenceMap(fenceMap) {}
627
628 ~FakeProducerFrameEventHistory() {}
629
630 void updateAcquireFence(uint64_t frameNumber,
631 std::shared_ptr<FenceTime>&& acquire) override {
632 // Verify the acquire fence being added isn't the one from the consumer.
633 EXPECT_NE(mConsumerAcquireFence, acquire);
634 // Override the fence, so we can verify this was called by the
635 // producer after the frame is queued.
636 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
637 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
638 }
639
640 void setAcquireFenceOverride(
641 const std::shared_ptr<FenceTime>& acquireFenceOverride,
642 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
643 mAcquireFenceOverride = acquireFenceOverride;
644 mConsumerAcquireFence = consumerAcquireFence;
645 }
646
647protected:
648 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
649 const override {
650 return mFenceMap->createFenceTimeForTest(fence);
651 }
652
653 FenceToFenceTimeMap* mFenceMap{nullptr};
654
655 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
656 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
657};
658
659
660class TestSurface : public Surface {
661public:
662 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
663 FenceToFenceTimeMap* fenceMap)
664 : Surface(bufferProducer),
665 mFakeSurfaceComposer(new FakeSurfaceComposer) {
666 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
667 mFrameEventHistory.reset(mFakeFrameEventHistory);
668 }
669
670 ~TestSurface() override {}
671
672 sp<ISurfaceComposer> composerService() const override {
673 return mFakeSurfaceComposer;
674 }
675
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800676 nsecs_t now() const override {
677 return mNow;
678 }
679
680 void setNow(nsecs_t now) {
681 mNow = now;
682 }
683
Brian Anderson3da8d272016-07-28 16:20:47 -0700684public:
685 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800686 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700687
688 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
689 // but this raw pointer gives access to test functionality.
690 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
691};
692
693
Brian Andersond0010582017-03-07 13:20:31 -0800694class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700695protected:
696 struct FenceAndFenceTime {
697 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
698 : mFence(new Fence),
699 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
700 sp<Fence> mFence { nullptr };
701 std::shared_ptr<FenceTime> mFenceTime { nullptr };
702 };
703
704 struct RefreshEvents {
705 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800706 : mFenceMap(fenceMap),
707 kCompositorTiming(
708 {refreshStart, refreshStart + 1, refreshStart + 2 }),
709 kStartTime(refreshStart + 3),
710 kGpuCompositionDoneTime(refreshStart + 4),
711 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700712
713 void signalPostCompositeFences() {
714 mFenceMap.signalAllForTest(
715 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
716 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
717 }
718
719 FenceToFenceTimeMap& mFenceMap;
720
721 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
722 FenceAndFenceTime mPresent { mFenceMap };
723
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800724 const CompositorTiming kCompositorTiming;
725
Brian Anderson3da8d272016-07-28 16:20:47 -0700726 const nsecs_t kStartTime;
727 const nsecs_t kGpuCompositionDoneTime;
728 const nsecs_t kPresentTime;
729 };
730
731 struct FrameEvents {
732 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
733 : mFenceMap(fenceMap),
734 kPostedTime(frameStartTime + 100),
735 kRequestedPresentTime(frameStartTime + 200),
736 kProducerAcquireTime(frameStartTime + 300),
737 kConsumerAcquireTime(frameStartTime + 301),
738 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700739 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700740 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700741 mRefreshes {
742 { mFenceMap, frameStartTime + 410 },
743 { mFenceMap, frameStartTime + 420 },
744 { mFenceMap, frameStartTime + 430 } } {}
745
746 void signalQueueFences() {
747 mFenceMap.signalAllForTest(
748 mAcquireConsumer.mFence, kConsumerAcquireTime);
749 mFenceMap.signalAllForTest(
750 mAcquireProducer.mFence, kProducerAcquireTime);
751 }
752
753 void signalRefreshFences() {
754 for (auto& re : mRefreshes) {
755 re.signalPostCompositeFences();
756 }
757 }
758
759 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700760 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
761 }
762
763 FenceToFenceTimeMap& mFenceMap;
764
765 FenceAndFenceTime mAcquireConsumer { mFenceMap };
766 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700767 FenceAndFenceTime mRelease { mFenceMap };
768
769 const nsecs_t kPostedTime;
770 const nsecs_t kRequestedPresentTime;
771 const nsecs_t kProducerAcquireTime;
772 const nsecs_t kConsumerAcquireTime;
773 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700774 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700775 const nsecs_t kReleaseTime;
776
777 RefreshEvents mRefreshes[3];
778 };
779
Brian Andersond0010582017-03-07 13:20:31 -0800780 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700781
782 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700783 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
784 mFakeConsumer = new FakeConsumer;
785 mCfeh = &mFakeConsumer->mFrameEventHistory;
786 mConsumer->consumerConnect(mFakeConsumer, false);
787 mConsumer->setConsumerName(String8("TestConsumer"));
788 mSurface = new TestSurface(mProducer, &mFenceMap);
789 mWindow = mSurface;
790
791 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
792 NATIVE_WINDOW_API_CPU));
793 native_window_set_buffer_count(mWindow.get(), 4);
794 }
795
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800796 void disableFrameTimestamps() {
797 mFakeConsumer->mGetFrameTimestampsEnabled = false;
798 native_window_enable_frame_timestamps(mWindow.get(), 0);
799 mFrameTimestampsEnabled = false;
800 }
801
Brian Anderson3da8d272016-07-28 16:20:47 -0700802 void enableFrameTimestamps() {
803 mFakeConsumer->mGetFrameTimestampsEnabled = true;
804 native_window_enable_frame_timestamps(mWindow.get(), 1);
805 mFrameTimestampsEnabled = true;
806 }
807
Brian Anderson1049d1d2016-12-16 17:25:57 -0800808 int getAllFrameTimestamps(uint64_t frameId) {
809 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700810 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
811 &outFirstRefreshStartTime, &outLastRefreshStartTime,
812 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700813 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700814 }
815
Brian Anderson3da8d272016-07-28 16:20:47 -0700816 void resetTimestamps() {
817 outRequestedPresentTime = -1;
818 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700819 outLatchTime = -1;
820 outFirstRefreshStartTime = -1;
821 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700822 outGpuCompositionDoneTime = -1;
823 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700824 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700825 outReleaseTime = -1;
826 }
827
Brian Anderson1049d1d2016-12-16 17:25:57 -0800828 uint64_t getNextFrameId() {
829 uint64_t frameId = -1;
830 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
831 EXPECT_EQ(status, NO_ERROR);
832 return frameId;
833 }
834
Brian Anderson3da8d272016-07-28 16:20:47 -0700835 void dequeueAndQueue(uint64_t frameIndex) {
836 int fence = -1;
837 ANativeWindowBuffer* buffer = nullptr;
838 ASSERT_EQ(NO_ERROR,
839 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
840
841 int oldAddFrameTimestampsCount =
842 mFakeConsumer->mAddFrameTimestampsCount;
843
844 FrameEvents* frame = &mFrames[frameIndex];
845 uint64_t frameNumber = frameIndex + 1;
846
847 NewFrameEventsEntry fe;
848 fe.frameNumber = frameNumber;
849 fe.postedTime = frame->kPostedTime;
850 fe.requestedPresentTime = frame->kRequestedPresentTime;
851 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
852 mFakeConsumer->mNewFrameEntryOverride = fe;
853
854 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
855 frame->mAcquireProducer.mFenceTime,
856 frame->mAcquireConsumer.mFenceTime);
857
858 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
859
860 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
861
862 EXPECT_EQ(
863 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
864 mFakeConsumer->mAddFrameTimestampsCount);
865 }
866
867 void addFrameEvents(
868 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
869 FrameEvents* oldFrame =
870 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
871 FrameEvents* newFrame = &mFrames[iNewFrame];
872
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -0800873 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700874 uint64_t nNewFrame = iNewFrame + 1;
875
Brian Anderson4e606e32017-03-16 15:34:57 -0700876 // Latch, Composite, and Release the frames in a plausible order.
877 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700878 // that's okay for the purposes of this test.
879 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
880
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700881 // Composite the previous frame one more time, which helps verify
882 // LastRefresh is updated properly.
883 if (oldFrame != nullptr) {
884 mCfeh->addPreComposition(nOldFrame,
885 oldFrame->mRefreshes[2].kStartTime);
886 gpuDoneFenceTime = gpuComposited ?
887 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
888 FenceTime::NO_FENCE;
889 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800890 oldFrame->mRefreshes[2].mPresent.mFenceTime,
891 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700892 }
893
894 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700895 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
896
897 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
898 gpuDoneFenceTime = gpuComposited ?
899 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
900 FenceTime::NO_FENCE;
901 // HWC2 releases the previous buffer after a new latch just before
902 // calling postComposition.
903 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700904 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700905 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
906 }
907 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800908 newFrame->mRefreshes[0].mPresent.mFenceTime,
909 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700910
Brian Anderson3da8d272016-07-28 16:20:47 -0700911 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
912 gpuDoneFenceTime = gpuComposited ?
913 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
914 FenceTime::NO_FENCE;
915 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800916 newFrame->mRefreshes[1].mPresent.mFenceTime,
917 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700918 }
919
Brian Anderson3da8d272016-07-28 16:20:47 -0700920 sp<IGraphicBufferProducer> mProducer;
921 sp<IGraphicBufferConsumer> mConsumer;
922 sp<FakeConsumer> mFakeConsumer;
923 ConsumerFrameEventHistory* mCfeh;
924 sp<TestSurface> mSurface;
925 sp<ANativeWindow> mWindow;
926
927 FenceToFenceTimeMap mFenceMap;
928
929 bool mFrameTimestampsEnabled = false;
930
931 int64_t outRequestedPresentTime = -1;
932 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700933 int64_t outLatchTime = -1;
934 int64_t outFirstRefreshStartTime = -1;
935 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700936 int64_t outGpuCompositionDoneTime = -1;
937 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700938 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700939 int64_t outReleaseTime = -1;
940
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800941 FrameEvents mFrames[3] {
942 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700943};
944
945
946// This test verifies that the frame timestamps are not retrieved when not
947// explicitly enabled via native_window_enable_frame_timestamps.
948// We want to check this to make sure there's no overhead for users
949// that don't need the timestamp information.
950TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
951 int fence;
952 ANativeWindowBuffer* buffer;
953
954 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
955 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
956
Brian Anderson1049d1d2016-12-16 17:25:57 -0800957 const uint64_t fId = getNextFrameId();
958
Brian Anderson3da8d272016-07-28 16:20:47 -0700959 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
960 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
961 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
962 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
963
964 // Verify the producer doesn't get frame timestamps piggybacked on queue.
965 // It is okay that frame timestamps are added in the consumer since it is
966 // still needed for SurfaceFlinger dumps.
967 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
968 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
969 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
970
971 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800972 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700973 EXPECT_EQ(INVALID_OPERATION, result);
974 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800975
976 // Verify compositor timing query fails.
977 nsecs_t compositeDeadline = 0;
978 nsecs_t compositeInterval = 0;
979 nsecs_t compositeToPresentLatency = 0;
980 result = native_window_get_compositor_timing(mWindow.get(),
981 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
982 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700983}
984
985// This test verifies that the frame timestamps are retrieved if explicitly
986// enabled via native_window_enable_frame_timestamps.
987TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800988 CompositorTiming initialCompositorTiming {
989 1000000000, // 1s deadline
990 16666667, // 16ms interval
991 50000000, // 50ms present latency
992 };
993 mCfeh->initializeCompositorTiming(initialCompositorTiming);
994
Brian Anderson3da8d272016-07-28 16:20:47 -0700995 enableFrameTimestamps();
996
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800997 // Verify the compositor timing query gets the initial compositor values
998 // after timststamps are enabled; even before the first frame is queued
999 // or dequeued.
1000 nsecs_t compositeDeadline = 0;
1001 nsecs_t compositeInterval = 0;
1002 nsecs_t compositeToPresentLatency = 0;
1003 mSurface->setNow(initialCompositorTiming.deadline - 1);
1004 int result = native_window_get_compositor_timing(mWindow.get(),
1005 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1006 EXPECT_EQ(NO_ERROR, result);
1007 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1008 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1009 EXPECT_EQ(initialCompositorTiming.presentLatency,
1010 compositeToPresentLatency);
1011
Brian Anderson3da8d272016-07-28 16:20:47 -07001012 int fence;
1013 ANativeWindowBuffer* buffer;
1014
1015 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001016 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001017
Brian Anderson1049d1d2016-12-16 17:25:57 -08001018 const uint64_t fId1 = getNextFrameId();
1019
Brian Anderson3da8d272016-07-28 16:20:47 -07001020 // Verify getFrameTimestamps is piggybacked on dequeue.
1021 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1022 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001023 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001024
1025 NewFrameEventsEntry f1;
1026 f1.frameNumber = 1;
1027 f1.postedTime = mFrames[0].kPostedTime;
1028 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1029 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1030 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1031 mFrames[0].mAcquireProducer.mFenceTime,
1032 mFrames[0].mAcquireConsumer.mFenceTime);
1033 mFakeConsumer->mNewFrameEntryOverride = f1;
1034 mFrames[0].signalQueueFences();
1035
1036 // Verify getFrameTimestamps is piggybacked on queue.
1037 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1038 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1039 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001040 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001041
1042 // Verify queries for timestamps that the producer doesn't know about
1043 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001044 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001045 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001046 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001047}
1048
Brian Anderson6b376712017-04-04 10:51:39 -07001049TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1050 bool displayPresentSupported = true;
1051 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1052
1053 // Verify supported bits are forwarded.
1054 int supportsPresent = -1;
1055 mWindow.get()->query(mWindow.get(),
1056 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1057 EXPECT_EQ(displayPresentSupported, supportsPresent);
1058}
1059
1060TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1061 bool displayPresentSupported = false;
1062 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1063
1064 // Verify supported bits are forwarded.
1065 int supportsPresent = -1;
1066 mWindow.get()->query(mWindow.get(),
1067 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1068 EXPECT_EQ(displayPresentSupported, supportsPresent);
1069}
1070
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001071TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1072 nsecs_t phase = 4000;
1073 nsecs_t interval = 1000;
1074
1075 // Timestamp in previous interval.
1076 nsecs_t timestamp = 3500;
1077 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1078 timestamp, phase, interval));
1079
1080 // Timestamp in next interval.
1081 timestamp = 4500;
1082 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1083 timestamp, phase, interval));
1084
1085 // Timestamp multiple intervals before.
1086 timestamp = 2500;
1087 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1088 timestamp, phase, interval));
1089
1090 // Timestamp multiple intervals after.
1091 timestamp = 6500;
1092 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1093 timestamp, phase, interval));
1094
1095 // Timestamp on previous interval.
1096 timestamp = 3000;
1097 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1098 timestamp, phase, interval));
1099
1100 // Timestamp on next interval.
1101 timestamp = 5000;
1102 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1103 timestamp, phase, interval));
1104
1105 // Timestamp equal to phase.
1106 timestamp = 4000;
1107 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1108 timestamp, phase, interval));
1109}
1110
1111// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1112// if the number of intervals elapsed is internally stored in an int.
1113TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1114 nsecs_t phase = 0;
1115 nsecs_t interval = 4000;
1116 nsecs_t big_timestamp = 8635916564000;
1117 int32_t intervals = big_timestamp / interval;
1118
1119 EXPECT_LT(intervals, 0);
1120 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1121 big_timestamp, phase, interval));
1122 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1123 big_timestamp, big_timestamp, interval));
1124}
1125
1126// This verifies the compositor timing is updated by refresh events
1127// and piggy backed on a queue, dequeue, and enabling of timestamps..
1128TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1129 CompositorTiming initialCompositorTiming {
1130 1000000000, // 1s deadline
1131 16666667, // 16ms interval
1132 50000000, // 50ms present latency
1133 };
1134 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1135
1136 enableFrameTimestamps();
1137
1138 // We get the initial values before any frames are submitted.
1139 nsecs_t compositeDeadline = 0;
1140 nsecs_t compositeInterval = 0;
1141 nsecs_t compositeToPresentLatency = 0;
1142 mSurface->setNow(initialCompositorTiming.deadline - 1);
1143 int result = native_window_get_compositor_timing(mWindow.get(),
1144 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1145 EXPECT_EQ(NO_ERROR, result);
1146 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1147 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1148 EXPECT_EQ(initialCompositorTiming.presentLatency,
1149 compositeToPresentLatency);
1150
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001151 dequeueAndQueue(0);
1152 addFrameEvents(true, NO_FRAME_INDEX, 0);
1153
1154 // Still get the initial values because the frame events for frame 0
1155 // didn't get a chance to piggyback on a queue or dequeue yet.
1156 result = native_window_get_compositor_timing(mWindow.get(),
1157 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1158 EXPECT_EQ(NO_ERROR, result);
1159 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1160 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1161 EXPECT_EQ(initialCompositorTiming.presentLatency,
1162 compositeToPresentLatency);
1163
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001164 dequeueAndQueue(1);
1165 addFrameEvents(true, 0, 1);
1166
1167 // Now expect the composite values associated with frame 1.
1168 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1169 result = native_window_get_compositor_timing(mWindow.get(),
1170 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1171 EXPECT_EQ(NO_ERROR, result);
1172 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1173 compositeDeadline);
1174 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1175 compositeInterval);
1176 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1177 compositeToPresentLatency);
1178
1179 dequeueAndQueue(2);
1180 addFrameEvents(true, 1, 2);
1181
1182 // Now expect the composite values associated with frame 2.
1183 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1184 result = native_window_get_compositor_timing(mWindow.get(),
1185 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1186 EXPECT_EQ(NO_ERROR, result);
1187 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1188 compositeDeadline);
1189 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1190 compositeInterval);
1191 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1192 compositeToPresentLatency);
1193
1194 // Re-enabling frame timestamps should get the latest values.
1195 disableFrameTimestamps();
1196 enableFrameTimestamps();
1197
1198 // Now expect the composite values associated with frame 3.
1199 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1200 result = native_window_get_compositor_timing(mWindow.get(),
1201 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1202 EXPECT_EQ(NO_ERROR, result);
1203 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1204 compositeDeadline);
1205 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1206 compositeInterval);
1207 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1208 compositeToPresentLatency);
1209}
1210
1211// This verifies the compositor deadline properly snaps to the the next
1212// deadline based on the current time.
1213TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1214 CompositorTiming initialCompositorTiming {
1215 1000000000, // 1s deadline
1216 16666667, // 16ms interval
1217 50000000, // 50ms present latency
1218 };
1219 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1220
1221 enableFrameTimestamps();
1222
1223 nsecs_t compositeDeadline = 0;
1224 nsecs_t compositeInterval = 0;
1225 nsecs_t compositeToPresentLatency = 0;
1226
1227 // A "now" just before the deadline snaps to the deadline.
1228 mSurface->setNow(initialCompositorTiming.deadline - 1);
1229 int result = native_window_get_compositor_timing(mWindow.get(),
1230 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1231 EXPECT_EQ(NO_ERROR, result);
1232 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1233 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1234 EXPECT_EQ(expectedDeadline, compositeDeadline);
1235
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001236 dequeueAndQueue(0);
1237 addFrameEvents(true, NO_FRAME_INDEX, 0);
1238
1239 // A "now" just after the deadline snaps properly.
1240 mSurface->setNow(initialCompositorTiming.deadline + 1);
1241 result = native_window_get_compositor_timing(mWindow.get(),
1242 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1243 EXPECT_EQ(NO_ERROR, result);
1244 expectedDeadline =
1245 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1246 EXPECT_EQ(expectedDeadline, compositeDeadline);
1247
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001248 dequeueAndQueue(1);
1249 addFrameEvents(true, 0, 1);
1250
1251 // A "now" just after the next interval snaps properly.
1252 mSurface->setNow(
1253 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1254 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1255 result = native_window_get_compositor_timing(mWindow.get(),
1256 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1257 EXPECT_EQ(NO_ERROR, result);
1258 expectedDeadline =
1259 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1260 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1261 EXPECT_EQ(expectedDeadline, compositeDeadline);
1262
1263 dequeueAndQueue(2);
1264 addFrameEvents(true, 1, 2);
1265
1266 // A "now" over 1 interval before the deadline snaps properly.
1267 mSurface->setNow(
1268 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1269 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1270 result = native_window_get_compositor_timing(mWindow.get(),
1271 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1272 EXPECT_EQ(NO_ERROR, result);
1273 expectedDeadline =
1274 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1275 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1276 EXPECT_EQ(expectedDeadline, compositeDeadline);
1277
1278 // Re-enabling frame timestamps should get the latest values.
1279 disableFrameTimestamps();
1280 enableFrameTimestamps();
1281
1282 // A "now" over 2 intervals before the deadline snaps properly.
1283 mSurface->setNow(
1284 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1285 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1286 result = native_window_get_compositor_timing(mWindow.get(),
1287 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1288 EXPECT_EQ(NO_ERROR, result);
1289 expectedDeadline =
1290 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1291 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1292 EXPECT_EQ(expectedDeadline, compositeDeadline);
1293}
1294
Brian Anderson1049d1d2016-12-16 17:25:57 -08001295// This verifies the timestamps recorded in the consumer's
1296// FrameTimestampsHistory are properly retrieved by the producer for the
1297// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001298TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1299 enableFrameTimestamps();
1300
Brian Anderson1049d1d2016-12-16 17:25:57 -08001301 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001302 dequeueAndQueue(0);
1303 mFrames[0].signalQueueFences();
1304
Brian Anderson1049d1d2016-12-16 17:25:57 -08001305 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001306 dequeueAndQueue(1);
1307 mFrames[1].signalQueueFences();
1308
1309 addFrameEvents(true, NO_FRAME_INDEX, 0);
1310 mFrames[0].signalRefreshFences();
1311 addFrameEvents(true, 0, 1);
1312 mFrames[0].signalReleaseFences();
1313 mFrames[1].signalRefreshFences();
1314
1315 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001316 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001317 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001318 EXPECT_EQ(NO_ERROR, result);
1319 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1320 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001321 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1322 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1323 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001324 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1325 outGpuCompositionDoneTime);
1326 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001327 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001328 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1329
1330 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001331 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001332 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001333 EXPECT_EQ(NO_ERROR, result);
1334 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1335 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001336 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1337 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1338 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001339 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1340 outGpuCompositionDoneTime);
1341 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001342 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1343 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001344}
1345
1346// This test verifies the acquire fence recorded by the consumer is not sent
1347// back to the producer and the producer saves its own fence.
1348TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1349 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001350
Brian Anderson3da8d272016-07-28 16:20:47 -07001351 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001352 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001353 dequeueAndQueue(0);
1354
1355 // Verify queue-related timestamps for f1 are available immediately in the
1356 // producer without asking the consumer again, even before signaling the
1357 // acquire fence.
1358 resetTimestamps();
1359 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001360 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001361 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001362 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001363 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1364 EXPECT_EQ(NO_ERROR, result);
1365 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001366 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001367
1368 // Signal acquire fences. Verify a sync call still isn't necessary.
1369 mFrames[0].signalQueueFences();
1370
1371 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001372 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001373 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001374 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001375 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1376 EXPECT_EQ(NO_ERROR, result);
1377 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1378 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1379
1380 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001381 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001382 dequeueAndQueue(1);
1383
1384 // Verify queue-related timestamps for f2 are available immediately in the
1385 // producer without asking the consumer again, even before signaling the
1386 // acquire fence.
1387 resetTimestamps();
1388 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001389 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001390 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001391 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001392 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1393 EXPECT_EQ(NO_ERROR, result);
1394 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001395 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001396
1397 // Signal acquire fences. Verify a sync call still isn't necessary.
1398 mFrames[1].signalQueueFences();
1399
1400 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001401 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001402 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001403 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001404 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1405 EXPECT_EQ(NO_ERROR, result);
1406 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1407 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1408}
1409
1410TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1411 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001412
1413 // Dequeue and queue frame 1.
1414 dequeueAndQueue(0);
1415 mFrames[0].signalQueueFences();
1416
1417 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001418 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001419 dequeueAndQueue(1);
1420 mFrames[1].signalQueueFences();
1421
1422 addFrameEvents(true, NO_FRAME_INDEX, 0);
1423 mFrames[0].signalRefreshFences();
1424 addFrameEvents(true, 0, 1);
1425 mFrames[0].signalReleaseFences();
1426 mFrames[1].signalRefreshFences();
1427
1428 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001429 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001430 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001431 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1432 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001433 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001434 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1435}
1436
1437// This test verifies that fences can signal and update timestamps producer
1438// side without an additional sync call to the consumer.
1439TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1440 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001441
1442 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001443 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001444 dequeueAndQueue(0);
1445 mFrames[0].signalQueueFences();
1446
1447 // Dequeue and queue frame 2.
1448 dequeueAndQueue(1);
1449 mFrames[1].signalQueueFences();
1450
1451 addFrameEvents(true, NO_FRAME_INDEX, 0);
1452 addFrameEvents(true, 0, 1);
1453
1454 // Verify available timestamps are correct for frame 1, before any
1455 // fence has been signaled.
1456 // Note: A sync call is necessary here since the events triggered by
1457 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001458 resetTimestamps();
1459 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001460 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001461 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1462 EXPECT_EQ(NO_ERROR, result);
1463 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1464 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001465 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1466 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1467 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001468 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1469 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001470 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001471 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001472
1473 // Verify available timestamps are correct for frame 1 again, before any
1474 // fence has been signaled.
1475 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001476 resetTimestamps();
1477 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001478 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001479 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1480 EXPECT_EQ(NO_ERROR, result);
1481 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1482 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001483 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1484 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1485 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001486 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1487 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001488 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001489 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001490
1491 // Signal the fences for frame 1.
1492 mFrames[0].signalRefreshFences();
1493 mFrames[0].signalReleaseFences();
1494
1495 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001496 resetTimestamps();
1497 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001498 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001499 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1500 EXPECT_EQ(NO_ERROR, result);
1501 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1502 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001503 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1504 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1505 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001506 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1507 outGpuCompositionDoneTime);
1508 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001509 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001510 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1511}
1512
1513// This test verifies that if the frame wasn't GPU composited but has a refresh
1514// event a sync call isn't made to get the GPU composite done time since it will
1515// never exist.
1516TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1517 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001518
Brian Anderson3da8d272016-07-28 16:20:47 -07001519 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001520 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001521 dequeueAndQueue(0);
1522 mFrames[0].signalQueueFences();
1523
1524 // Dequeue and queue frame 2.
1525 dequeueAndQueue(1);
1526 mFrames[1].signalQueueFences();
1527
1528 addFrameEvents(false, NO_FRAME_INDEX, 0);
1529 addFrameEvents(false, 0, 1);
1530
1531 // Verify available timestamps are correct for frame 1, before any
1532 // fence has been signaled.
1533 // Note: A sync call is necessary here since the events triggered by
1534 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1535 resetTimestamps();
1536 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001537 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001538 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1539 EXPECT_EQ(NO_ERROR, result);
1540 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1541 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001542 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1543 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1544 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001545 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1546 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001547 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001548 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001549
1550 // Signal the fences for frame 1.
1551 mFrames[0].signalRefreshFences();
1552 mFrames[0].signalReleaseFences();
1553
1554 // Verify all timestamps, except GPU composition, are available without a
1555 // sync call.
1556 resetTimestamps();
1557 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001558 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001559 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1560 EXPECT_EQ(NO_ERROR, result);
1561 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1562 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001563 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1564 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1565 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001566 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001567 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001568 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001569 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1570}
1571
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001572// This test verifies that if the certain timestamps can't possibly exist for
1573// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001574TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001575 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001576
1577 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001578 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001579 dequeueAndQueue(0);
1580 mFrames[0].signalQueueFences();
1581
1582 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001583 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001584 dequeueAndQueue(1);
1585 mFrames[1].signalQueueFences();
1586
1587 addFrameEvents(false, NO_FRAME_INDEX, 0);
1588 addFrameEvents(false, 0, 1);
1589
1590 // Verify available timestamps are correct for frame 1, before any
1591 // fence has been signaled.
1592 // Note: A sync call is necessary here since the events triggered by
1593 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001594 resetTimestamps();
1595 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001596 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001597 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1598 EXPECT_EQ(NO_ERROR, result);
1599 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1600 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001601 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1602 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1603 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001604 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1605 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001606 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001607 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001608
1609 mFrames[0].signalRefreshFences();
1610 mFrames[0].signalReleaseFences();
1611 mFrames[1].signalRefreshFences();
1612
Brian Anderson1049d1d2016-12-16 17:25:57 -08001613 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001614 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001615 // available, a sync call should not occur because it's not possible for f2
1616 // to encounter the final value for those events until another frame is
1617 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001618 resetTimestamps();
1619 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001620 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001621 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1622 EXPECT_EQ(NO_ERROR, result);
1623 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1624 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001625 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1626 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1627 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001628 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001629 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001630 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1631 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001632}
1633
Brian Anderson6b376712017-04-04 10:51:39 -07001634// This test verifies there are no sync calls for present times
1635// when they aren't supported and that an error is returned.
1636
1637TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1638 enableFrameTimestamps();
1639 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1640
1641 // Dequeue and queue frame 1.
1642 const uint64_t fId1 = getNextFrameId();
1643 dequeueAndQueue(0);
1644
1645 // Verify a query for the Present times do not trigger a sync call if they
1646 // are not supported.
1647 resetTimestamps();
1648 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1649 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1650 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1651 &outDisplayPresentTime, nullptr, nullptr);
1652 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1653 EXPECT_EQ(BAD_VALUE, result);
1654 EXPECT_EQ(-1, outDisplayPresentTime);
1655}
1656
Dan Stoza932f0082017-05-31 13:50:16 -07001657} // namespace android