blob: cf3d1b27c707e4761b3d94e783ccc6017664c4b5 [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>
25#include <gui/BufferItemConsumer.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070026#include <gui/IDisplayEventConnection.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080027#include <gui/ISurfaceComposer.h>
28#include <gui/Surface.h>
29#include <gui/SurfaceComposerClient.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070030#include <private/gui/ComposerService.h>
Dan Stozac1879002014-05-22 15:59:05 -070031#include <ui/Rect.h>
Jamie Gennis134f0422011-03-08 12:18:54 -080032#include <utils/String8.h>
33
Brian Anderson3da8d272016-07-28 16:20:47 -070034#include <limits>
Kalle Raita643f0942016-12-07 09:20:14 -080035#include <thread>
36
Jamie Gennis134f0422011-03-08 12:18:54 -080037namespace android {
38
Kalle Raita643f0942016-12-07 09:20:14 -080039using namespace std::chrono_literals;
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060040// retrieve wide-color and hdr settings from configstore
41using namespace android::hardware::configstore;
42using namespace android::hardware::configstore::V1_0;
43
44static bool hasWideColorDisplay =
45 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
Kalle Raita643f0942016-12-07 09:20:14 -080046
Brian Anderson3da8d272016-07-28 16:20:47 -070047class FakeSurfaceComposer;
48class FakeProducerFrameEventHistory;
49
50static constexpr uint64_t NO_FRAME_INDEX = std::numeric_limits<uint64_t>::max();
51
Jamie Gennis134f0422011-03-08 12:18:54 -080052class SurfaceTest : public ::testing::Test {
53protected:
Mathias Agopian7c1a4872013-03-20 15:56:04 -070054
55 SurfaceTest() {
56 ProcessState::self()->startThreadPool();
57 }
58
Jamie Gennis134f0422011-03-08 12:18:54 -080059 virtual void SetUp() {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080060 mComposerClient = new SurfaceComposerClient;
Jamie Gennis134f0422011-03-08 12:18:54 -080061 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
62
Brian Andersond0010582017-03-07 13:20:31 -080063 // TODO(brianderson): The following sometimes fails and is a source of
64 // test flakiness.
Jamie Gennisfc850122011-04-25 16:40:05 -070065 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070066 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis134f0422011-03-08 12:18:54 -080067
68 ASSERT_TRUE(mSurfaceControl != NULL);
69 ASSERT_TRUE(mSurfaceControl->isValid());
70
Mathias Agopian698c0872011-06-28 19:09:31 -070071 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian9303eee2011-07-01 15:27:27 -070072 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff));
Jamie Gennis134f0422011-03-08 12:18:54 -080073 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070074 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennis134f0422011-03-08 12:18:54 -080075
76 mSurface = mSurfaceControl->getSurface();
77 ASSERT_TRUE(mSurface != NULL);
78 }
79
80 virtual void TearDown() {
81 mComposerClient->dispose();
82 }
83
84 sp<Surface> mSurface;
85 sp<SurfaceComposerClient> mComposerClient;
86 sp<SurfaceControl> mSurfaceControl;
87};
88
89TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
90 sp<ANativeWindow> anw(mSurface);
91 int result = -123;
92 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
93 &result);
94 EXPECT_EQ(NO_ERROR, err);
95 EXPECT_EQ(1, result);
96}
97
98TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
99 mSurfaceControl.clear();
Kalle Raita643f0942016-12-07 09:20:14 -0800100 // Wait for the async clean-up to complete.
101 std::this_thread::sleep_for(50ms);
Jamie Gennis134f0422011-03-08 12:18:54 -0800102
103 sp<ANativeWindow> anw(mSurface);
104 int result = -123;
105 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
106 &result);
107 EXPECT_EQ(NO_ERROR, err);
108 EXPECT_EQ(1, result);
109}
110
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800111// This test probably doesn't belong here.
Jamie Gennisc901ca02011-10-11 16:02:31 -0700112TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800113 sp<ANativeWindow> anw(mSurface);
114
115 // Verify the screenshot works with no protected buffers.
Dan Stoza5603a2f2014-04-07 13:41:37 -0700116 sp<IGraphicBufferProducer> producer;
117 sp<IGraphicBufferConsumer> consumer;
118 BufferQueue::createBufferQueue(&producer, &consumer);
119 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800120 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Brian Anderson3da8d272016-07-28 16:20:47 -0700121 sp<IBinder> display(sf->getBuiltInDisplay(
122 ISurfaceComposer::eDisplayIdMain));
Dan Stozac1879002014-05-22 15:59:05 -0700123 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800124 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800125
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700126 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
127 NATIVE_WINDOW_API_CPU));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800128 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
129 // that we need to dequeue a buffer in order for it to actually get
130 // allocated in SurfaceFlinger.
131 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
132 GRALLOC_USAGE_PROTECTED));
133 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Iliyan Malchev697526b2011-05-01 11:33:26 -0700134 ANativeWindowBuffer* buf = 0;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700135
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700136 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700137 if (err) {
138 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
139 // that's okay as long as this is the reason for the failure.
140 // try again without the GRALLOC_USAGE_PROTECTED bit.
141 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700142 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
143 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700144 return;
145 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700146 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700147
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800148 for (int i = 0; i < 4; i++) {
149 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700150 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
151 &buf));
152 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800153 }
Dan Stozac1879002014-05-22 15:59:05 -0700154 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800155 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800156}
157
Jamie Gennis391bbe22011-03-14 15:00:06 -0700158TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
159 sp<ANativeWindow> anw(mSurface);
160 int result = -123;
161 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
162 EXPECT_EQ(NO_ERROR, err);
163 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
164}
165
Craig Donner6ebc46a2016-10-21 15:23:44 -0700166TEST_F(SurfaceTest, LayerCountIsOne) {
167 sp<ANativeWindow> anw(mSurface);
168 int result = -123;
169 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
170 EXPECT_EQ(NO_ERROR, err);
171 EXPECT_EQ(1, result);
172}
173
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700174TEST_F(SurfaceTest, QueryConsumerUsage) {
175 const int TEST_USAGE_FLAGS =
176 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700177 sp<IGraphicBufferProducer> producer;
178 sp<IGraphicBufferConsumer> consumer;
179 BufferQueue::createBufferQueue(&producer, &consumer);
180 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700181 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700182 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700183
184 sp<ANativeWindow> anw(s);
185
186 int flags = -1;
187 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
188
189 ASSERT_EQ(NO_ERROR, err);
190 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
191}
192
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800193TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
194 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
195 sp<IGraphicBufferProducer> producer;
196 sp<IGraphicBufferConsumer> consumer;
197 BufferQueue::createBufferQueue(&producer, &consumer);
198 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
199
200 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
201
202 sp<Surface> s = new Surface(producer);
203
204 sp<ANativeWindow> anw(s);
205
206 android_dataspace dataSpace;
207
208 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
209 reinterpret_cast<int*>(&dataSpace));
210
211 ASSERT_EQ(NO_ERROR, err);
212 ASSERT_EQ(TEST_DATASPACE, dataSpace);
213}
214
Dan Stoza812ed062015-06-02 15:45:22 -0700215TEST_F(SurfaceTest, SettingGenerationNumber) {
216 sp<IGraphicBufferProducer> producer;
217 sp<IGraphicBufferConsumer> consumer;
218 BufferQueue::createBufferQueue(&producer, &consumer);
219 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
220 sp<Surface> surface = new Surface(producer);
221 sp<ANativeWindow> window(surface);
222
223 // Allocate a buffer with a generation number of 0
224 ANativeWindowBuffer* buffer;
225 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700226 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
227 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700228 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
229 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
230
231 // Detach the buffer and check its generation number
232 sp<GraphicBuffer> graphicBuffer;
233 sp<Fence> fence;
234 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
235 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
236
237 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
238 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
239
240 // This should change the generation number of the GraphicBuffer
241 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
242
243 // Check that the new generation number sticks with the buffer
244 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
245 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
246 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
247 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
248}
249
Dan Stozac6f30bd2015-06-08 09:32:50 -0700250TEST_F(SurfaceTest, GetConsumerName) {
251 sp<IGraphicBufferProducer> producer;
252 sp<IGraphicBufferConsumer> consumer;
253 BufferQueue::createBufferQueue(&producer, &consumer);
254
255 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
256 consumer->consumerConnect(dummyConsumer, false);
257 consumer->setConsumerName(String8("TestConsumer"));
258
259 sp<Surface> surface = new Surface(producer);
260 sp<ANativeWindow> window(surface);
261 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
262
263 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
264}
265
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700266TEST_F(SurfaceTest, GetWideColorSupport) {
267 sp<IGraphicBufferProducer> producer;
268 sp<IGraphicBufferConsumer> consumer;
269 BufferQueue::createBufferQueue(&producer, &consumer);
270
271 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
272 consumer->consumerConnect(dummyConsumer, false);
273 consumer->setConsumerName(String8("TestConsumer"));
274
275 sp<Surface> surface = new Surface(producer);
276 sp<ANativeWindow> window(surface);
277 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
278
279 bool supported;
280 surface->getWideColorSupport(&supported);
281
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -0600282 // NOTE: This test assumes that device that supports
283 // wide-color (as indicated by BoardConfig) must also
284 // have a wide-color primary display.
285 // That assumption allows this test to cover devices
286 // that advertised a wide-color color mode without
287 // actually supporting wide-color to pass this test
288 // as well as the case of a device that does support
289 // wide-color (via BoardConfig) and has a wide-color
290 // primary display.
291 // NOT covered at this time is a device that supports
292 // wide color in the BoardConfig but does not support
293 // a wide-color color mode on the primary display.
294 ASSERT_EQ(hasWideColorDisplay, supported);
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700295}
296
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800297TEST_F(SurfaceTest, DynamicSetBufferCount) {
298 sp<IGraphicBufferProducer> producer;
299 sp<IGraphicBufferConsumer> consumer;
300 BufferQueue::createBufferQueue(&producer, &consumer);
301
302 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
303 consumer->consumerConnect(dummyConsumer, false);
304 consumer->setConsumerName(String8("TestConsumer"));
305
306 sp<Surface> surface = new Surface(producer);
307 sp<ANativeWindow> window(surface);
308
309 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
310 NATIVE_WINDOW_API_CPU));
311 native_window_set_buffer_count(window.get(), 4);
312
313 int fence;
314 ANativeWindowBuffer* buffer;
315 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
316 native_window_set_buffer_count(window.get(), 3);
317 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
318 native_window_set_buffer_count(window.get(), 2);
319 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
320 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
321}
322
Brian Anderson3da8d272016-07-28 16:20:47 -0700323
324class FakeConsumer : public BnConsumerListener {
325public:
326 void onFrameAvailable(const BufferItem& /*item*/) override {}
327 void onBuffersReleased() override {}
328 void onSidebandStreamChanged() override {}
329
330 void addAndGetFrameTimestamps(
331 const NewFrameEventsEntry* newTimestamps,
332 FrameEventHistoryDelta* outDelta) override {
333 if (newTimestamps) {
334 if (mGetFrameTimestampsEnabled) {
335 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
336 "Test should set mNewFrameEntryOverride before queuing "
337 "a frame.";
338 EXPECT_EQ(newTimestamps->frameNumber,
339 mNewFrameEntryOverride.frameNumber) <<
340 "Test attempting to add NewFrameEntryOverride with "
341 "incorrect frame number.";
342 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
343 mNewFrameEntryOverride.frameNumber = 0;
344 }
345 mAddFrameTimestampsCount++;
346 mLastAddedFrameNumber = newTimestamps->frameNumber;
347 }
348 if (outDelta) {
349 mFrameEventHistory.getAndResetDelta(outDelta);
350 mGetFrameTimestampsCount++;
351 }
352 mAddAndGetFrameTimestampsCallCount++;
353 }
354
355 bool mGetFrameTimestampsEnabled = false;
356
357 ConsumerFrameEventHistory mFrameEventHistory;
358 int mAddAndGetFrameTimestampsCallCount = 0;
359 int mAddFrameTimestampsCount = 0;
360 int mGetFrameTimestampsCount = 0;
361 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
362
363 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
364};
365
366
367class FakeSurfaceComposer : public ISurfaceComposer{
368public:
369 ~FakeSurfaceComposer() override {}
370
Brian Anderson6b376712017-04-04 10:51:39 -0700371 void setSupportsPresent(bool supportsPresent) {
372 mSupportsPresent = supportsPresent;
373 }
374
Brian Anderson3da8d272016-07-28 16:20:47 -0700375 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800376 sp<ISurfaceComposerClient> createScopedConnection(
377 const sp<IGraphicBufferProducer>& /* parent */) override {
378 return nullptr;
379 }
Chia-I Wu527747d2017-03-13 20:38:48 +0000380 sp<IGraphicBufferAlloc> createGraphicBufferAlloc() override {
381 return nullptr;
382 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700383 sp<IDisplayEventConnection> createDisplayEventConnection() override {
384 return nullptr;
385 }
386 sp<IBinder> createDisplay(const String8& /*displayName*/,
387 bool /*secure*/) override { return nullptr; }
388 void destroyDisplay(const sp<IBinder>& /*display */) override {}
389 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
390 void setTransactionState(const Vector<ComposerState>& /*state*/,
391 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
392 override {}
393 void bootFinished() override {}
394 bool authenticateSurfaceTexture(
395 const sp<IGraphicBufferProducer>& /*surface*/) const override {
396 return false;
397 }
Brian Anderson6b376712017-04-04 10:51:39 -0700398
399 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
400 const override {
401 *outSupported = {
402 FrameEvent::REQUESTED_PRESENT,
403 FrameEvent::ACQUIRE,
404 FrameEvent::LATCH,
405 FrameEvent::FIRST_REFRESH_START,
406 FrameEvent::LAST_REFRESH_START,
407 FrameEvent::GPU_COMPOSITION_DONE,
408 FrameEvent::DEQUEUE_READY,
409 FrameEvent::RELEASE
410 };
411 if (mSupportsPresent) {
412 outSupported->push_back(
413 FrameEvent::DISPLAY_PRESENT);
414 }
415 return NO_ERROR;
416 }
417
Brian Anderson3da8d272016-07-28 16:20:47 -0700418 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
419 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
420 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
421 status_t getDisplayStats(const sp<IBinder>& /*display*/,
422 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
423 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
424 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
425 override {
426 return NO_ERROR;
427 }
428 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
429 Vector<android_color_mode_t>* /*outColorModes*/) override {
430 return NO_ERROR;
431 }
432 android_color_mode_t getActiveColorMode(const sp<IBinder>& /*display*/)
433 override {
434 return HAL_COLOR_MODE_NATIVE;
435 }
436 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
437 android_color_mode_t /*colorMode*/) override { return NO_ERROR; }
438 status_t captureScreen(const sp<IBinder>& /*display*/,
439 const sp<IGraphicBufferProducer>& /*producer*/,
440 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800441 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700442 bool /*useIdentityTransform*/,
443 Rotation /*rotation*/) override { return NO_ERROR; }
444 status_t clearAnimationFrameStats() override { return NO_ERROR; }
445 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
446 return NO_ERROR;
447 }
448 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
449 HdrCapabilities* /*outCapabilities*/) const override {
450 return NO_ERROR;
451 }
452 status_t enableVSyncInjections(bool /*enable*/) override {
453 return NO_ERROR;
454 }
455 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
456
457protected:
458 IBinder* onAsBinder() override { return nullptr; }
459
460private:
461 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700462};
463
464class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
465public:
466 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
467 : mFenceMap(fenceMap) {}
468
469 ~FakeProducerFrameEventHistory() {}
470
471 void updateAcquireFence(uint64_t frameNumber,
472 std::shared_ptr<FenceTime>&& acquire) override {
473 // Verify the acquire fence being added isn't the one from the consumer.
474 EXPECT_NE(mConsumerAcquireFence, acquire);
475 // Override the fence, so we can verify this was called by the
476 // producer after the frame is queued.
477 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
478 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
479 }
480
481 void setAcquireFenceOverride(
482 const std::shared_ptr<FenceTime>& acquireFenceOverride,
483 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
484 mAcquireFenceOverride = acquireFenceOverride;
485 mConsumerAcquireFence = consumerAcquireFence;
486 }
487
488protected:
489 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
490 const override {
491 return mFenceMap->createFenceTimeForTest(fence);
492 }
493
494 FenceToFenceTimeMap* mFenceMap{nullptr};
495
496 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
497 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
498};
499
500
501class TestSurface : public Surface {
502public:
503 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
504 FenceToFenceTimeMap* fenceMap)
505 : Surface(bufferProducer),
506 mFakeSurfaceComposer(new FakeSurfaceComposer) {
507 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
508 mFrameEventHistory.reset(mFakeFrameEventHistory);
509 }
510
511 ~TestSurface() override {}
512
513 sp<ISurfaceComposer> composerService() const override {
514 return mFakeSurfaceComposer;
515 }
516
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800517 nsecs_t now() const override {
518 return mNow;
519 }
520
521 void setNow(nsecs_t now) {
522 mNow = now;
523 }
524
Brian Anderson3da8d272016-07-28 16:20:47 -0700525public:
526 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800527 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700528
529 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
530 // but this raw pointer gives access to test functionality.
531 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
532};
533
534
Brian Andersond0010582017-03-07 13:20:31 -0800535class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700536protected:
537 struct FenceAndFenceTime {
538 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
539 : mFence(new Fence),
540 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
541 sp<Fence> mFence { nullptr };
542 std::shared_ptr<FenceTime> mFenceTime { nullptr };
543 };
544
545 struct RefreshEvents {
546 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800547 : mFenceMap(fenceMap),
548 kCompositorTiming(
549 {refreshStart, refreshStart + 1, refreshStart + 2 }),
550 kStartTime(refreshStart + 3),
551 kGpuCompositionDoneTime(refreshStart + 4),
552 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700553
554 void signalPostCompositeFences() {
555 mFenceMap.signalAllForTest(
556 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
557 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
558 }
559
560 FenceToFenceTimeMap& mFenceMap;
561
562 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
563 FenceAndFenceTime mPresent { mFenceMap };
564
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800565 const CompositorTiming kCompositorTiming;
566
Brian Anderson3da8d272016-07-28 16:20:47 -0700567 const nsecs_t kStartTime;
568 const nsecs_t kGpuCompositionDoneTime;
569 const nsecs_t kPresentTime;
570 };
571
572 struct FrameEvents {
573 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
574 : mFenceMap(fenceMap),
575 kPostedTime(frameStartTime + 100),
576 kRequestedPresentTime(frameStartTime + 200),
577 kProducerAcquireTime(frameStartTime + 300),
578 kConsumerAcquireTime(frameStartTime + 301),
579 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700580 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700581 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700582 mRefreshes {
583 { mFenceMap, frameStartTime + 410 },
584 { mFenceMap, frameStartTime + 420 },
585 { mFenceMap, frameStartTime + 430 } } {}
586
587 void signalQueueFences() {
588 mFenceMap.signalAllForTest(
589 mAcquireConsumer.mFence, kConsumerAcquireTime);
590 mFenceMap.signalAllForTest(
591 mAcquireProducer.mFence, kProducerAcquireTime);
592 }
593
594 void signalRefreshFences() {
595 for (auto& re : mRefreshes) {
596 re.signalPostCompositeFences();
597 }
598 }
599
600 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700601 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
602 }
603
604 FenceToFenceTimeMap& mFenceMap;
605
606 FenceAndFenceTime mAcquireConsumer { mFenceMap };
607 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700608 FenceAndFenceTime mRelease { mFenceMap };
609
610 const nsecs_t kPostedTime;
611 const nsecs_t kRequestedPresentTime;
612 const nsecs_t kProducerAcquireTime;
613 const nsecs_t kConsumerAcquireTime;
614 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700615 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700616 const nsecs_t kReleaseTime;
617
618 RefreshEvents mRefreshes[3];
619 };
620
Brian Andersond0010582017-03-07 13:20:31 -0800621 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700622
623 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700624 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
625 mFakeConsumer = new FakeConsumer;
626 mCfeh = &mFakeConsumer->mFrameEventHistory;
627 mConsumer->consumerConnect(mFakeConsumer, false);
628 mConsumer->setConsumerName(String8("TestConsumer"));
629 mSurface = new TestSurface(mProducer, &mFenceMap);
630 mWindow = mSurface;
631
632 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
633 NATIVE_WINDOW_API_CPU));
634 native_window_set_buffer_count(mWindow.get(), 4);
635 }
636
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800637 void disableFrameTimestamps() {
638 mFakeConsumer->mGetFrameTimestampsEnabled = false;
639 native_window_enable_frame_timestamps(mWindow.get(), 0);
640 mFrameTimestampsEnabled = false;
641 }
642
Brian Anderson3da8d272016-07-28 16:20:47 -0700643 void enableFrameTimestamps() {
644 mFakeConsumer->mGetFrameTimestampsEnabled = true;
645 native_window_enable_frame_timestamps(mWindow.get(), 1);
646 mFrameTimestampsEnabled = true;
647 }
648
Brian Anderson1049d1d2016-12-16 17:25:57 -0800649 int getAllFrameTimestamps(uint64_t frameId) {
650 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700651 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
652 &outFirstRefreshStartTime, &outLastRefreshStartTime,
653 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700654 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700655 }
656
Brian Anderson3da8d272016-07-28 16:20:47 -0700657 void resetTimestamps() {
658 outRequestedPresentTime = -1;
659 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700660 outLatchTime = -1;
661 outFirstRefreshStartTime = -1;
662 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700663 outGpuCompositionDoneTime = -1;
664 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700665 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700666 outReleaseTime = -1;
667 }
668
Brian Anderson1049d1d2016-12-16 17:25:57 -0800669 uint64_t getNextFrameId() {
670 uint64_t frameId = -1;
671 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
672 EXPECT_EQ(status, NO_ERROR);
673 return frameId;
674 }
675
Brian Anderson3da8d272016-07-28 16:20:47 -0700676 void dequeueAndQueue(uint64_t frameIndex) {
677 int fence = -1;
678 ANativeWindowBuffer* buffer = nullptr;
679 ASSERT_EQ(NO_ERROR,
680 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
681
682 int oldAddFrameTimestampsCount =
683 mFakeConsumer->mAddFrameTimestampsCount;
684
685 FrameEvents* frame = &mFrames[frameIndex];
686 uint64_t frameNumber = frameIndex + 1;
687
688 NewFrameEventsEntry fe;
689 fe.frameNumber = frameNumber;
690 fe.postedTime = frame->kPostedTime;
691 fe.requestedPresentTime = frame->kRequestedPresentTime;
692 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
693 mFakeConsumer->mNewFrameEntryOverride = fe;
694
695 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
696 frame->mAcquireProducer.mFenceTime,
697 frame->mAcquireConsumer.mFenceTime);
698
699 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
700
701 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
702
703 EXPECT_EQ(
704 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
705 mFakeConsumer->mAddFrameTimestampsCount);
706 }
707
708 void addFrameEvents(
709 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
710 FrameEvents* oldFrame =
711 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
712 FrameEvents* newFrame = &mFrames[iNewFrame];
713
714 uint64_t nOldFrame = iOldFrame + 1;
715 uint64_t nNewFrame = iNewFrame + 1;
716
Brian Anderson4e606e32017-03-16 15:34:57 -0700717 // Latch, Composite, and Release the frames in a plausible order.
718 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700719 // that's okay for the purposes of this test.
720 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
721
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700722 // Composite the previous frame one more time, which helps verify
723 // LastRefresh is updated properly.
724 if (oldFrame != nullptr) {
725 mCfeh->addPreComposition(nOldFrame,
726 oldFrame->mRefreshes[2].kStartTime);
727 gpuDoneFenceTime = gpuComposited ?
728 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
729 FenceTime::NO_FENCE;
730 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800731 oldFrame->mRefreshes[2].mPresent.mFenceTime,
732 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700733 }
734
735 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700736 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
737
738 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
739 gpuDoneFenceTime = gpuComposited ?
740 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
741 FenceTime::NO_FENCE;
742 // HWC2 releases the previous buffer after a new latch just before
743 // calling postComposition.
744 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700745 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700746 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
747 }
748 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800749 newFrame->mRefreshes[0].mPresent.mFenceTime,
750 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700751
Brian Anderson3da8d272016-07-28 16:20:47 -0700752 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
753 gpuDoneFenceTime = gpuComposited ?
754 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
755 FenceTime::NO_FENCE;
756 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800757 newFrame->mRefreshes[1].mPresent.mFenceTime,
758 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700759 }
760
Brian Anderson3da8d272016-07-28 16:20:47 -0700761 sp<IGraphicBufferProducer> mProducer;
762 sp<IGraphicBufferConsumer> mConsumer;
763 sp<FakeConsumer> mFakeConsumer;
764 ConsumerFrameEventHistory* mCfeh;
765 sp<TestSurface> mSurface;
766 sp<ANativeWindow> mWindow;
767
768 FenceToFenceTimeMap mFenceMap;
769
770 bool mFrameTimestampsEnabled = false;
771
772 int64_t outRequestedPresentTime = -1;
773 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700774 int64_t outLatchTime = -1;
775 int64_t outFirstRefreshStartTime = -1;
776 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700777 int64_t outGpuCompositionDoneTime = -1;
778 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700779 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700780 int64_t outReleaseTime = -1;
781
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800782 FrameEvents mFrames[3] {
783 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700784};
785
786
787// This test verifies that the frame timestamps are not retrieved when not
788// explicitly enabled via native_window_enable_frame_timestamps.
789// We want to check this to make sure there's no overhead for users
790// that don't need the timestamp information.
791TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
792 int fence;
793 ANativeWindowBuffer* buffer;
794
795 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
796 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
797
Brian Anderson1049d1d2016-12-16 17:25:57 -0800798 const uint64_t fId = getNextFrameId();
799
Brian Anderson3da8d272016-07-28 16:20:47 -0700800 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
801 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
802 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
803 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
804
805 // Verify the producer doesn't get frame timestamps piggybacked on queue.
806 // It is okay that frame timestamps are added in the consumer since it is
807 // still needed for SurfaceFlinger dumps.
808 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
809 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
810 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
811
812 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800813 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700814 EXPECT_EQ(INVALID_OPERATION, result);
815 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800816
817 // Verify compositor timing query fails.
818 nsecs_t compositeDeadline = 0;
819 nsecs_t compositeInterval = 0;
820 nsecs_t compositeToPresentLatency = 0;
821 result = native_window_get_compositor_timing(mWindow.get(),
822 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
823 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700824}
825
826// This test verifies that the frame timestamps are retrieved if explicitly
827// enabled via native_window_enable_frame_timestamps.
828TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800829 CompositorTiming initialCompositorTiming {
830 1000000000, // 1s deadline
831 16666667, // 16ms interval
832 50000000, // 50ms present latency
833 };
834 mCfeh->initializeCompositorTiming(initialCompositorTiming);
835
Brian Anderson3da8d272016-07-28 16:20:47 -0700836 enableFrameTimestamps();
837
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800838 // Verify the compositor timing query gets the initial compositor values
839 // after timststamps are enabled; even before the first frame is queued
840 // or dequeued.
841 nsecs_t compositeDeadline = 0;
842 nsecs_t compositeInterval = 0;
843 nsecs_t compositeToPresentLatency = 0;
844 mSurface->setNow(initialCompositorTiming.deadline - 1);
845 int result = native_window_get_compositor_timing(mWindow.get(),
846 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
847 EXPECT_EQ(NO_ERROR, result);
848 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
849 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
850 EXPECT_EQ(initialCompositorTiming.presentLatency,
851 compositeToPresentLatency);
852
Brian Anderson3da8d272016-07-28 16:20:47 -0700853 int fence;
854 ANativeWindowBuffer* buffer;
855
856 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800857 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700858
Brian Anderson1049d1d2016-12-16 17:25:57 -0800859 const uint64_t fId1 = getNextFrameId();
860
Brian Anderson3da8d272016-07-28 16:20:47 -0700861 // Verify getFrameTimestamps is piggybacked on dequeue.
862 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
863 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800864 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700865
866 NewFrameEventsEntry f1;
867 f1.frameNumber = 1;
868 f1.postedTime = mFrames[0].kPostedTime;
869 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
870 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
871 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
872 mFrames[0].mAcquireProducer.mFenceTime,
873 mFrames[0].mAcquireConsumer.mFenceTime);
874 mFakeConsumer->mNewFrameEntryOverride = f1;
875 mFrames[0].signalQueueFences();
876
877 // Verify getFrameTimestamps is piggybacked on queue.
878 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
879 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
880 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800881 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700882
883 // Verify queries for timestamps that the producer doesn't know about
884 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800885 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -0700886 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800887 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700888}
889
Brian Anderson6b376712017-04-04 10:51:39 -0700890TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
891 bool displayPresentSupported = true;
892 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
893
894 // Verify supported bits are forwarded.
895 int supportsPresent = -1;
896 mWindow.get()->query(mWindow.get(),
897 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
898 EXPECT_EQ(displayPresentSupported, supportsPresent);
899}
900
901TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
902 bool displayPresentSupported = false;
903 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
904
905 // Verify supported bits are forwarded.
906 int supportsPresent = -1;
907 mWindow.get()->query(mWindow.get(),
908 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
909 EXPECT_EQ(displayPresentSupported, supportsPresent);
910}
911
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800912TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
913 nsecs_t phase = 4000;
914 nsecs_t interval = 1000;
915
916 // Timestamp in previous interval.
917 nsecs_t timestamp = 3500;
918 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
919 timestamp, phase, interval));
920
921 // Timestamp in next interval.
922 timestamp = 4500;
923 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
924 timestamp, phase, interval));
925
926 // Timestamp multiple intervals before.
927 timestamp = 2500;
928 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
929 timestamp, phase, interval));
930
931 // Timestamp multiple intervals after.
932 timestamp = 6500;
933 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
934 timestamp, phase, interval));
935
936 // Timestamp on previous interval.
937 timestamp = 3000;
938 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
939 timestamp, phase, interval));
940
941 // Timestamp on next interval.
942 timestamp = 5000;
943 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
944 timestamp, phase, interval));
945
946 // Timestamp equal to phase.
947 timestamp = 4000;
948 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
949 timestamp, phase, interval));
950}
951
952// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
953// if the number of intervals elapsed is internally stored in an int.
954TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
955 nsecs_t phase = 0;
956 nsecs_t interval = 4000;
957 nsecs_t big_timestamp = 8635916564000;
958 int32_t intervals = big_timestamp / interval;
959
960 EXPECT_LT(intervals, 0);
961 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
962 big_timestamp, phase, interval));
963 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
964 big_timestamp, big_timestamp, interval));
965}
966
967// This verifies the compositor timing is updated by refresh events
968// and piggy backed on a queue, dequeue, and enabling of timestamps..
969TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
970 CompositorTiming initialCompositorTiming {
971 1000000000, // 1s deadline
972 16666667, // 16ms interval
973 50000000, // 50ms present latency
974 };
975 mCfeh->initializeCompositorTiming(initialCompositorTiming);
976
977 enableFrameTimestamps();
978
979 // We get the initial values before any frames are submitted.
980 nsecs_t compositeDeadline = 0;
981 nsecs_t compositeInterval = 0;
982 nsecs_t compositeToPresentLatency = 0;
983 mSurface->setNow(initialCompositorTiming.deadline - 1);
984 int result = native_window_get_compositor_timing(mWindow.get(),
985 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
986 EXPECT_EQ(NO_ERROR, result);
987 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
988 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
989 EXPECT_EQ(initialCompositorTiming.presentLatency,
990 compositeToPresentLatency);
991
992 const uint64_t fId1 = getNextFrameId();
993 dequeueAndQueue(0);
994 addFrameEvents(true, NO_FRAME_INDEX, 0);
995
996 // Still get the initial values because the frame events for frame 0
997 // didn't get a chance to piggyback on a queue or dequeue yet.
998 result = native_window_get_compositor_timing(mWindow.get(),
999 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1000 EXPECT_EQ(NO_ERROR, result);
1001 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1002 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1003 EXPECT_EQ(initialCompositorTiming.presentLatency,
1004 compositeToPresentLatency);
1005
1006 const uint64_t fId2 = getNextFrameId();
1007 dequeueAndQueue(1);
1008 addFrameEvents(true, 0, 1);
1009
1010 // Now expect the composite values associated with frame 1.
1011 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1012 result = native_window_get_compositor_timing(mWindow.get(),
1013 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1014 EXPECT_EQ(NO_ERROR, result);
1015 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1016 compositeDeadline);
1017 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1018 compositeInterval);
1019 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1020 compositeToPresentLatency);
1021
1022 dequeueAndQueue(2);
1023 addFrameEvents(true, 1, 2);
1024
1025 // Now expect the composite values associated with frame 2.
1026 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1027 result = native_window_get_compositor_timing(mWindow.get(),
1028 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1029 EXPECT_EQ(NO_ERROR, result);
1030 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1031 compositeDeadline);
1032 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1033 compositeInterval);
1034 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1035 compositeToPresentLatency);
1036
1037 // Re-enabling frame timestamps should get the latest values.
1038 disableFrameTimestamps();
1039 enableFrameTimestamps();
1040
1041 // Now expect the composite values associated with frame 3.
1042 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1043 result = native_window_get_compositor_timing(mWindow.get(),
1044 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1045 EXPECT_EQ(NO_ERROR, result);
1046 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1047 compositeDeadline);
1048 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1049 compositeInterval);
1050 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1051 compositeToPresentLatency);
1052}
1053
1054// This verifies the compositor deadline properly snaps to the the next
1055// deadline based on the current time.
1056TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1057 CompositorTiming initialCompositorTiming {
1058 1000000000, // 1s deadline
1059 16666667, // 16ms interval
1060 50000000, // 50ms present latency
1061 };
1062 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1063
1064 enableFrameTimestamps();
1065
1066 nsecs_t compositeDeadline = 0;
1067 nsecs_t compositeInterval = 0;
1068 nsecs_t compositeToPresentLatency = 0;
1069
1070 // A "now" just before the deadline snaps to the deadline.
1071 mSurface->setNow(initialCompositorTiming.deadline - 1);
1072 int result = native_window_get_compositor_timing(mWindow.get(),
1073 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1074 EXPECT_EQ(NO_ERROR, result);
1075 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1076 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1077 EXPECT_EQ(expectedDeadline, compositeDeadline);
1078
1079 const uint64_t fId1 = getNextFrameId();
1080 dequeueAndQueue(0);
1081 addFrameEvents(true, NO_FRAME_INDEX, 0);
1082
1083 // A "now" just after the deadline snaps properly.
1084 mSurface->setNow(initialCompositorTiming.deadline + 1);
1085 result = native_window_get_compositor_timing(mWindow.get(),
1086 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1087 EXPECT_EQ(NO_ERROR, result);
1088 expectedDeadline =
1089 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1090 EXPECT_EQ(expectedDeadline, compositeDeadline);
1091
1092 const uint64_t fId2 = getNextFrameId();
1093 dequeueAndQueue(1);
1094 addFrameEvents(true, 0, 1);
1095
1096 // A "now" just after the next interval snaps properly.
1097 mSurface->setNow(
1098 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1099 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1100 result = native_window_get_compositor_timing(mWindow.get(),
1101 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1102 EXPECT_EQ(NO_ERROR, result);
1103 expectedDeadline =
1104 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1105 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1106 EXPECT_EQ(expectedDeadline, compositeDeadline);
1107
1108 dequeueAndQueue(2);
1109 addFrameEvents(true, 1, 2);
1110
1111 // A "now" over 1 interval before the deadline snaps properly.
1112 mSurface->setNow(
1113 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1114 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1115 result = native_window_get_compositor_timing(mWindow.get(),
1116 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1117 EXPECT_EQ(NO_ERROR, result);
1118 expectedDeadline =
1119 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1120 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1121 EXPECT_EQ(expectedDeadline, compositeDeadline);
1122
1123 // Re-enabling frame timestamps should get the latest values.
1124 disableFrameTimestamps();
1125 enableFrameTimestamps();
1126
1127 // A "now" over 2 intervals before the deadline snaps properly.
1128 mSurface->setNow(
1129 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1130 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1131 result = native_window_get_compositor_timing(mWindow.get(),
1132 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1133 EXPECT_EQ(NO_ERROR, result);
1134 expectedDeadline =
1135 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1136 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1137 EXPECT_EQ(expectedDeadline, compositeDeadline);
1138}
1139
Brian Anderson1049d1d2016-12-16 17:25:57 -08001140// This verifies the timestamps recorded in the consumer's
1141// FrameTimestampsHistory are properly retrieved by the producer for the
1142// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001143TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1144 enableFrameTimestamps();
1145
Brian Anderson1049d1d2016-12-16 17:25:57 -08001146 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001147 dequeueAndQueue(0);
1148 mFrames[0].signalQueueFences();
1149
Brian Anderson1049d1d2016-12-16 17:25:57 -08001150 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001151 dequeueAndQueue(1);
1152 mFrames[1].signalQueueFences();
1153
1154 addFrameEvents(true, NO_FRAME_INDEX, 0);
1155 mFrames[0].signalRefreshFences();
1156 addFrameEvents(true, 0, 1);
1157 mFrames[0].signalReleaseFences();
1158 mFrames[1].signalRefreshFences();
1159
1160 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001161 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001162 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001163 EXPECT_EQ(NO_ERROR, result);
1164 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1165 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001166 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1167 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1168 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001169 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1170 outGpuCompositionDoneTime);
1171 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001172 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001173 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1174
1175 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001176 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001177 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001178 EXPECT_EQ(NO_ERROR, result);
1179 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1180 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001181 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1182 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1183 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001184 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1185 outGpuCompositionDoneTime);
1186 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001187 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1188 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001189}
1190
1191// This test verifies the acquire fence recorded by the consumer is not sent
1192// back to the producer and the producer saves its own fence.
1193TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1194 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001195
Brian Anderson3da8d272016-07-28 16:20:47 -07001196 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001197 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001198 dequeueAndQueue(0);
1199
1200 // Verify queue-related timestamps for f1 are available immediately in the
1201 // producer without asking the consumer again, even before signaling the
1202 // acquire fence.
1203 resetTimestamps();
1204 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001205 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001206 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001207 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001208 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1209 EXPECT_EQ(NO_ERROR, result);
1210 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001211 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001212
1213 // Signal acquire fences. Verify a sync call still isn't necessary.
1214 mFrames[0].signalQueueFences();
1215
1216 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001217 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001218 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001219 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001220 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1221 EXPECT_EQ(NO_ERROR, result);
1222 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1223 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1224
1225 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001226 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001227 dequeueAndQueue(1);
1228
1229 // Verify queue-related timestamps for f2 are available immediately in the
1230 // producer without asking the consumer again, even before signaling the
1231 // acquire fence.
1232 resetTimestamps();
1233 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001234 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001235 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001236 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001237 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1238 EXPECT_EQ(NO_ERROR, result);
1239 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001240 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001241
1242 // Signal acquire fences. Verify a sync call still isn't necessary.
1243 mFrames[1].signalQueueFences();
1244
1245 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001246 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001247 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001248 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001249 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1250 EXPECT_EQ(NO_ERROR, result);
1251 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1252 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1253}
1254
1255TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1256 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001257
1258 // Dequeue and queue frame 1.
1259 dequeueAndQueue(0);
1260 mFrames[0].signalQueueFences();
1261
1262 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001263 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001264 dequeueAndQueue(1);
1265 mFrames[1].signalQueueFences();
1266
1267 addFrameEvents(true, NO_FRAME_INDEX, 0);
1268 mFrames[0].signalRefreshFences();
1269 addFrameEvents(true, 0, 1);
1270 mFrames[0].signalReleaseFences();
1271 mFrames[1].signalRefreshFences();
1272
1273 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001274 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001275 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001276 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1277 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001278 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001279 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1280}
1281
1282// This test verifies that fences can signal and update timestamps producer
1283// side without an additional sync call to the consumer.
1284TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1285 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001286
1287 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001288 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001289 dequeueAndQueue(0);
1290 mFrames[0].signalQueueFences();
1291
1292 // Dequeue and queue frame 2.
1293 dequeueAndQueue(1);
1294 mFrames[1].signalQueueFences();
1295
1296 addFrameEvents(true, NO_FRAME_INDEX, 0);
1297 addFrameEvents(true, 0, 1);
1298
1299 // Verify available timestamps are correct for frame 1, before any
1300 // fence has been signaled.
1301 // Note: A sync call is necessary here since the events triggered by
1302 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001303 resetTimestamps();
1304 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001305 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001306 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1307 EXPECT_EQ(NO_ERROR, result);
1308 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1309 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001310 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1311 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1312 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001313 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1314 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001315 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001316 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001317
1318 // Verify available timestamps are correct for frame 1 again, before any
1319 // fence has been signaled.
1320 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001321 resetTimestamps();
1322 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001323 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001324 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1325 EXPECT_EQ(NO_ERROR, result);
1326 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1327 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001328 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1329 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1330 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001331 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1332 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001333 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001334 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001335
1336 // Signal the fences for frame 1.
1337 mFrames[0].signalRefreshFences();
1338 mFrames[0].signalReleaseFences();
1339
1340 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001341 resetTimestamps();
1342 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001343 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001344 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1345 EXPECT_EQ(NO_ERROR, result);
1346 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1347 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001348 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1349 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1350 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001351 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1352 outGpuCompositionDoneTime);
1353 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001354 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001355 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1356}
1357
1358// This test verifies that if the frame wasn't GPU composited but has a refresh
1359// event a sync call isn't made to get the GPU composite done time since it will
1360// never exist.
1361TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1362 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001363
Brian Anderson3da8d272016-07-28 16:20:47 -07001364 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001365 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001366 dequeueAndQueue(0);
1367 mFrames[0].signalQueueFences();
1368
1369 // Dequeue and queue frame 2.
1370 dequeueAndQueue(1);
1371 mFrames[1].signalQueueFences();
1372
1373 addFrameEvents(false, NO_FRAME_INDEX, 0);
1374 addFrameEvents(false, 0, 1);
1375
1376 // Verify available timestamps are correct for frame 1, before any
1377 // fence has been signaled.
1378 // Note: A sync call is necessary here since the events triggered by
1379 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1380 resetTimestamps();
1381 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001382 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001383 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1384 EXPECT_EQ(NO_ERROR, result);
1385 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1386 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001387 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1388 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1389 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001390 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1391 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001392 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001393 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001394
1395 // Signal the fences for frame 1.
1396 mFrames[0].signalRefreshFences();
1397 mFrames[0].signalReleaseFences();
1398
1399 // Verify all timestamps, except GPU composition, are available without a
1400 // sync call.
1401 resetTimestamps();
1402 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001403 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001404 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1405 EXPECT_EQ(NO_ERROR, result);
1406 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1407 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001408 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1409 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1410 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001411 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001412 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001413 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001414 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1415}
1416
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001417// This test verifies that if the certain timestamps can't possibly exist for
1418// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001419TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001420 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001421
1422 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001423 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001424 dequeueAndQueue(0);
1425 mFrames[0].signalQueueFences();
1426
1427 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001428 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001429 dequeueAndQueue(1);
1430 mFrames[1].signalQueueFences();
1431
1432 addFrameEvents(false, NO_FRAME_INDEX, 0);
1433 addFrameEvents(false, 0, 1);
1434
1435 // Verify available timestamps are correct for frame 1, before any
1436 // fence has been signaled.
1437 // Note: A sync call is necessary here since the events triggered by
1438 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001439 resetTimestamps();
1440 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001441 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001442 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1443 EXPECT_EQ(NO_ERROR, result);
1444 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1445 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001446 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1447 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1448 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001449 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1450 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001451 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001452 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001453
1454 mFrames[0].signalRefreshFences();
1455 mFrames[0].signalReleaseFences();
1456 mFrames[1].signalRefreshFences();
1457
Brian Anderson1049d1d2016-12-16 17:25:57 -08001458 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001459 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001460 // available, a sync call should not occur because it's not possible for f2
1461 // to encounter the final value for those events until another frame is
1462 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001463 resetTimestamps();
1464 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001465 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001466 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1467 EXPECT_EQ(NO_ERROR, result);
1468 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1469 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001470 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1471 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1472 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001473 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001474 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001475 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1476 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001477}
1478
Brian Anderson6b376712017-04-04 10:51:39 -07001479// This test verifies there are no sync calls for present times
1480// when they aren't supported and that an error is returned.
1481
1482TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1483 enableFrameTimestamps();
1484 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1485
1486 // Dequeue and queue frame 1.
1487 const uint64_t fId1 = getNextFrameId();
1488 dequeueAndQueue(0);
1489
1490 // Verify a query for the Present times do not trigger a sync call if they
1491 // are not supported.
1492 resetTimestamps();
1493 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1494 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1495 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1496 &outDisplayPresentTime, nullptr, nullptr);
1497 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1498 EXPECT_EQ(BAD_VALUE, result);
1499 EXPECT_EQ(-1, outDisplayPresentTime);
1500}
1501
Jamie Gennis134f0422011-03-08 12:18:54 -08001502}