blob: 08d6715a1c5f0cb43424c8d4535201b98d996fe9 [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 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700380 sp<IDisplayEventConnection> createDisplayEventConnection() override {
381 return nullptr;
382 }
383 sp<IBinder> createDisplay(const String8& /*displayName*/,
384 bool /*secure*/) override { return nullptr; }
385 void destroyDisplay(const sp<IBinder>& /*display */) override {}
386 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
387 void setTransactionState(const Vector<ComposerState>& /*state*/,
388 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
389 override {}
390 void bootFinished() override {}
391 bool authenticateSurfaceTexture(
392 const sp<IGraphicBufferProducer>& /*surface*/) const override {
393 return false;
394 }
Brian Anderson6b376712017-04-04 10:51:39 -0700395
396 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
397 const override {
398 *outSupported = {
399 FrameEvent::REQUESTED_PRESENT,
400 FrameEvent::ACQUIRE,
401 FrameEvent::LATCH,
402 FrameEvent::FIRST_REFRESH_START,
403 FrameEvent::LAST_REFRESH_START,
404 FrameEvent::GPU_COMPOSITION_DONE,
405 FrameEvent::DEQUEUE_READY,
406 FrameEvent::RELEASE
407 };
408 if (mSupportsPresent) {
409 outSupported->push_back(
410 FrameEvent::DISPLAY_PRESENT);
411 }
412 return NO_ERROR;
413 }
414
Brian Anderson3da8d272016-07-28 16:20:47 -0700415 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
416 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
417 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
418 status_t getDisplayStats(const sp<IBinder>& /*display*/,
419 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
420 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
421 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
422 override {
423 return NO_ERROR;
424 }
425 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
426 Vector<android_color_mode_t>* /*outColorModes*/) override {
427 return NO_ERROR;
428 }
429 android_color_mode_t getActiveColorMode(const sp<IBinder>& /*display*/)
430 override {
431 return HAL_COLOR_MODE_NATIVE;
432 }
433 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
434 android_color_mode_t /*colorMode*/) override { return NO_ERROR; }
435 status_t captureScreen(const sp<IBinder>& /*display*/,
436 const sp<IGraphicBufferProducer>& /*producer*/,
437 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800438 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700439 bool /*useIdentityTransform*/,
440 Rotation /*rotation*/) override { return NO_ERROR; }
441 status_t clearAnimationFrameStats() override { return NO_ERROR; }
442 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
443 return NO_ERROR;
444 }
445 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
446 HdrCapabilities* /*outCapabilities*/) const override {
447 return NO_ERROR;
448 }
449 status_t enableVSyncInjections(bool /*enable*/) override {
450 return NO_ERROR;
451 }
452 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
453
454protected:
455 IBinder* onAsBinder() override { return nullptr; }
456
457private:
458 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700459};
460
461class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
462public:
463 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
464 : mFenceMap(fenceMap) {}
465
466 ~FakeProducerFrameEventHistory() {}
467
468 void updateAcquireFence(uint64_t frameNumber,
469 std::shared_ptr<FenceTime>&& acquire) override {
470 // Verify the acquire fence being added isn't the one from the consumer.
471 EXPECT_NE(mConsumerAcquireFence, acquire);
472 // Override the fence, so we can verify this was called by the
473 // producer after the frame is queued.
474 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
475 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
476 }
477
478 void setAcquireFenceOverride(
479 const std::shared_ptr<FenceTime>& acquireFenceOverride,
480 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
481 mAcquireFenceOverride = acquireFenceOverride;
482 mConsumerAcquireFence = consumerAcquireFence;
483 }
484
485protected:
486 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
487 const override {
488 return mFenceMap->createFenceTimeForTest(fence);
489 }
490
491 FenceToFenceTimeMap* mFenceMap{nullptr};
492
493 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
494 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
495};
496
497
498class TestSurface : public Surface {
499public:
500 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
501 FenceToFenceTimeMap* fenceMap)
502 : Surface(bufferProducer),
503 mFakeSurfaceComposer(new FakeSurfaceComposer) {
504 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
505 mFrameEventHistory.reset(mFakeFrameEventHistory);
506 }
507
508 ~TestSurface() override {}
509
510 sp<ISurfaceComposer> composerService() const override {
511 return mFakeSurfaceComposer;
512 }
513
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800514 nsecs_t now() const override {
515 return mNow;
516 }
517
518 void setNow(nsecs_t now) {
519 mNow = now;
520 }
521
Brian Anderson3da8d272016-07-28 16:20:47 -0700522public:
523 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800524 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700525
526 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
527 // but this raw pointer gives access to test functionality.
528 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
529};
530
531
Brian Andersond0010582017-03-07 13:20:31 -0800532class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700533protected:
534 struct FenceAndFenceTime {
535 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
536 : mFence(new Fence),
537 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
538 sp<Fence> mFence { nullptr };
539 std::shared_ptr<FenceTime> mFenceTime { nullptr };
540 };
541
542 struct RefreshEvents {
543 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800544 : mFenceMap(fenceMap),
545 kCompositorTiming(
546 {refreshStart, refreshStart + 1, refreshStart + 2 }),
547 kStartTime(refreshStart + 3),
548 kGpuCompositionDoneTime(refreshStart + 4),
549 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700550
551 void signalPostCompositeFences() {
552 mFenceMap.signalAllForTest(
553 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
554 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
555 }
556
557 FenceToFenceTimeMap& mFenceMap;
558
559 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
560 FenceAndFenceTime mPresent { mFenceMap };
561
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800562 const CompositorTiming kCompositorTiming;
563
Brian Anderson3da8d272016-07-28 16:20:47 -0700564 const nsecs_t kStartTime;
565 const nsecs_t kGpuCompositionDoneTime;
566 const nsecs_t kPresentTime;
567 };
568
569 struct FrameEvents {
570 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
571 : mFenceMap(fenceMap),
572 kPostedTime(frameStartTime + 100),
573 kRequestedPresentTime(frameStartTime + 200),
574 kProducerAcquireTime(frameStartTime + 300),
575 kConsumerAcquireTime(frameStartTime + 301),
576 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700577 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700578 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700579 mRefreshes {
580 { mFenceMap, frameStartTime + 410 },
581 { mFenceMap, frameStartTime + 420 },
582 { mFenceMap, frameStartTime + 430 } } {}
583
584 void signalQueueFences() {
585 mFenceMap.signalAllForTest(
586 mAcquireConsumer.mFence, kConsumerAcquireTime);
587 mFenceMap.signalAllForTest(
588 mAcquireProducer.mFence, kProducerAcquireTime);
589 }
590
591 void signalRefreshFences() {
592 for (auto& re : mRefreshes) {
593 re.signalPostCompositeFences();
594 }
595 }
596
597 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700598 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
599 }
600
601 FenceToFenceTimeMap& mFenceMap;
602
603 FenceAndFenceTime mAcquireConsumer { mFenceMap };
604 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700605 FenceAndFenceTime mRelease { mFenceMap };
606
607 const nsecs_t kPostedTime;
608 const nsecs_t kRequestedPresentTime;
609 const nsecs_t kProducerAcquireTime;
610 const nsecs_t kConsumerAcquireTime;
611 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700612 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700613 const nsecs_t kReleaseTime;
614
615 RefreshEvents mRefreshes[3];
616 };
617
Brian Andersond0010582017-03-07 13:20:31 -0800618 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700619
620 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700621 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
622 mFakeConsumer = new FakeConsumer;
623 mCfeh = &mFakeConsumer->mFrameEventHistory;
624 mConsumer->consumerConnect(mFakeConsumer, false);
625 mConsumer->setConsumerName(String8("TestConsumer"));
626 mSurface = new TestSurface(mProducer, &mFenceMap);
627 mWindow = mSurface;
628
629 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
630 NATIVE_WINDOW_API_CPU));
631 native_window_set_buffer_count(mWindow.get(), 4);
632 }
633
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800634 void disableFrameTimestamps() {
635 mFakeConsumer->mGetFrameTimestampsEnabled = false;
636 native_window_enable_frame_timestamps(mWindow.get(), 0);
637 mFrameTimestampsEnabled = false;
638 }
639
Brian Anderson3da8d272016-07-28 16:20:47 -0700640 void enableFrameTimestamps() {
641 mFakeConsumer->mGetFrameTimestampsEnabled = true;
642 native_window_enable_frame_timestamps(mWindow.get(), 1);
643 mFrameTimestampsEnabled = true;
644 }
645
Brian Anderson1049d1d2016-12-16 17:25:57 -0800646 int getAllFrameTimestamps(uint64_t frameId) {
647 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700648 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
649 &outFirstRefreshStartTime, &outLastRefreshStartTime,
650 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700651 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700652 }
653
Brian Anderson3da8d272016-07-28 16:20:47 -0700654 void resetTimestamps() {
655 outRequestedPresentTime = -1;
656 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700657 outLatchTime = -1;
658 outFirstRefreshStartTime = -1;
659 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700660 outGpuCompositionDoneTime = -1;
661 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700662 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700663 outReleaseTime = -1;
664 }
665
Brian Anderson1049d1d2016-12-16 17:25:57 -0800666 uint64_t getNextFrameId() {
667 uint64_t frameId = -1;
668 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
669 EXPECT_EQ(status, NO_ERROR);
670 return frameId;
671 }
672
Brian Anderson3da8d272016-07-28 16:20:47 -0700673 void dequeueAndQueue(uint64_t frameIndex) {
674 int fence = -1;
675 ANativeWindowBuffer* buffer = nullptr;
676 ASSERT_EQ(NO_ERROR,
677 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
678
679 int oldAddFrameTimestampsCount =
680 mFakeConsumer->mAddFrameTimestampsCount;
681
682 FrameEvents* frame = &mFrames[frameIndex];
683 uint64_t frameNumber = frameIndex + 1;
684
685 NewFrameEventsEntry fe;
686 fe.frameNumber = frameNumber;
687 fe.postedTime = frame->kPostedTime;
688 fe.requestedPresentTime = frame->kRequestedPresentTime;
689 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
690 mFakeConsumer->mNewFrameEntryOverride = fe;
691
692 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
693 frame->mAcquireProducer.mFenceTime,
694 frame->mAcquireConsumer.mFenceTime);
695
696 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
697
698 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
699
700 EXPECT_EQ(
701 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
702 mFakeConsumer->mAddFrameTimestampsCount);
703 }
704
705 void addFrameEvents(
706 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
707 FrameEvents* oldFrame =
708 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
709 FrameEvents* newFrame = &mFrames[iNewFrame];
710
711 uint64_t nOldFrame = iOldFrame + 1;
712 uint64_t nNewFrame = iNewFrame + 1;
713
Brian Anderson4e606e32017-03-16 15:34:57 -0700714 // Latch, Composite, and Release the frames in a plausible order.
715 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700716 // that's okay for the purposes of this test.
717 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
718
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700719 // Composite the previous frame one more time, which helps verify
720 // LastRefresh is updated properly.
721 if (oldFrame != nullptr) {
722 mCfeh->addPreComposition(nOldFrame,
723 oldFrame->mRefreshes[2].kStartTime);
724 gpuDoneFenceTime = gpuComposited ?
725 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
726 FenceTime::NO_FENCE;
727 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800728 oldFrame->mRefreshes[2].mPresent.mFenceTime,
729 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700730 }
731
732 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700733 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
734
735 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
736 gpuDoneFenceTime = gpuComposited ?
737 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
738 FenceTime::NO_FENCE;
739 // HWC2 releases the previous buffer after a new latch just before
740 // calling postComposition.
741 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700742 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700743 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
744 }
745 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800746 newFrame->mRefreshes[0].mPresent.mFenceTime,
747 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700748
Brian Anderson3da8d272016-07-28 16:20:47 -0700749 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
750 gpuDoneFenceTime = gpuComposited ?
751 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
752 FenceTime::NO_FENCE;
753 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800754 newFrame->mRefreshes[1].mPresent.mFenceTime,
755 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700756 }
757
Brian Anderson3da8d272016-07-28 16:20:47 -0700758 sp<IGraphicBufferProducer> mProducer;
759 sp<IGraphicBufferConsumer> mConsumer;
760 sp<FakeConsumer> mFakeConsumer;
761 ConsumerFrameEventHistory* mCfeh;
762 sp<TestSurface> mSurface;
763 sp<ANativeWindow> mWindow;
764
765 FenceToFenceTimeMap mFenceMap;
766
767 bool mFrameTimestampsEnabled = false;
768
769 int64_t outRequestedPresentTime = -1;
770 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700771 int64_t outLatchTime = -1;
772 int64_t outFirstRefreshStartTime = -1;
773 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700774 int64_t outGpuCompositionDoneTime = -1;
775 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700776 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700777 int64_t outReleaseTime = -1;
778
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800779 FrameEvents mFrames[3] {
780 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700781};
782
783
784// This test verifies that the frame timestamps are not retrieved when not
785// explicitly enabled via native_window_enable_frame_timestamps.
786// We want to check this to make sure there's no overhead for users
787// that don't need the timestamp information.
788TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
789 int fence;
790 ANativeWindowBuffer* buffer;
791
792 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
793 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
794
Brian Anderson1049d1d2016-12-16 17:25:57 -0800795 const uint64_t fId = getNextFrameId();
796
Brian Anderson3da8d272016-07-28 16:20:47 -0700797 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
798 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
799 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
800 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
801
802 // Verify the producer doesn't get frame timestamps piggybacked on queue.
803 // It is okay that frame timestamps are added in the consumer since it is
804 // still needed for SurfaceFlinger dumps.
805 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
806 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
807 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
808
809 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800810 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700811 EXPECT_EQ(INVALID_OPERATION, result);
812 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800813
814 // Verify compositor timing query fails.
815 nsecs_t compositeDeadline = 0;
816 nsecs_t compositeInterval = 0;
817 nsecs_t compositeToPresentLatency = 0;
818 result = native_window_get_compositor_timing(mWindow.get(),
819 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
820 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700821}
822
823// This test verifies that the frame timestamps are retrieved if explicitly
824// enabled via native_window_enable_frame_timestamps.
825TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800826 CompositorTiming initialCompositorTiming {
827 1000000000, // 1s deadline
828 16666667, // 16ms interval
829 50000000, // 50ms present latency
830 };
831 mCfeh->initializeCompositorTiming(initialCompositorTiming);
832
Brian Anderson3da8d272016-07-28 16:20:47 -0700833 enableFrameTimestamps();
834
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800835 // Verify the compositor timing query gets the initial compositor values
836 // after timststamps are enabled; even before the first frame is queued
837 // or dequeued.
838 nsecs_t compositeDeadline = 0;
839 nsecs_t compositeInterval = 0;
840 nsecs_t compositeToPresentLatency = 0;
841 mSurface->setNow(initialCompositorTiming.deadline - 1);
842 int result = native_window_get_compositor_timing(mWindow.get(),
843 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
844 EXPECT_EQ(NO_ERROR, result);
845 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
846 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
847 EXPECT_EQ(initialCompositorTiming.presentLatency,
848 compositeToPresentLatency);
849
Brian Anderson3da8d272016-07-28 16:20:47 -0700850 int fence;
851 ANativeWindowBuffer* buffer;
852
853 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800854 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700855
Brian Anderson1049d1d2016-12-16 17:25:57 -0800856 const uint64_t fId1 = getNextFrameId();
857
Brian Anderson3da8d272016-07-28 16:20:47 -0700858 // Verify getFrameTimestamps is piggybacked on dequeue.
859 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
860 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800861 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700862
863 NewFrameEventsEntry f1;
864 f1.frameNumber = 1;
865 f1.postedTime = mFrames[0].kPostedTime;
866 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
867 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
868 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
869 mFrames[0].mAcquireProducer.mFenceTime,
870 mFrames[0].mAcquireConsumer.mFenceTime);
871 mFakeConsumer->mNewFrameEntryOverride = f1;
872 mFrames[0].signalQueueFences();
873
874 // Verify getFrameTimestamps is piggybacked on queue.
875 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
876 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
877 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800878 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700879
880 // Verify queries for timestamps that the producer doesn't know about
881 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800882 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -0700883 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800884 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700885}
886
Brian Anderson6b376712017-04-04 10:51:39 -0700887TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
888 bool displayPresentSupported = true;
889 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
890
891 // Verify supported bits are forwarded.
892 int supportsPresent = -1;
893 mWindow.get()->query(mWindow.get(),
894 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
895 EXPECT_EQ(displayPresentSupported, supportsPresent);
896}
897
898TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
899 bool displayPresentSupported = false;
900 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
901
902 // Verify supported bits are forwarded.
903 int supportsPresent = -1;
904 mWindow.get()->query(mWindow.get(),
905 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
906 EXPECT_EQ(displayPresentSupported, supportsPresent);
907}
908
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800909TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
910 nsecs_t phase = 4000;
911 nsecs_t interval = 1000;
912
913 // Timestamp in previous interval.
914 nsecs_t timestamp = 3500;
915 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
916 timestamp, phase, interval));
917
918 // Timestamp in next interval.
919 timestamp = 4500;
920 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
921 timestamp, phase, interval));
922
923 // Timestamp multiple intervals before.
924 timestamp = 2500;
925 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
926 timestamp, phase, interval));
927
928 // Timestamp multiple intervals after.
929 timestamp = 6500;
930 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
931 timestamp, phase, interval));
932
933 // Timestamp on previous interval.
934 timestamp = 3000;
935 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
936 timestamp, phase, interval));
937
938 // Timestamp on next interval.
939 timestamp = 5000;
940 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
941 timestamp, phase, interval));
942
943 // Timestamp equal to phase.
944 timestamp = 4000;
945 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
946 timestamp, phase, interval));
947}
948
949// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
950// if the number of intervals elapsed is internally stored in an int.
951TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
952 nsecs_t phase = 0;
953 nsecs_t interval = 4000;
954 nsecs_t big_timestamp = 8635916564000;
955 int32_t intervals = big_timestamp / interval;
956
957 EXPECT_LT(intervals, 0);
958 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
959 big_timestamp, phase, interval));
960 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
961 big_timestamp, big_timestamp, interval));
962}
963
964// This verifies the compositor timing is updated by refresh events
965// and piggy backed on a queue, dequeue, and enabling of timestamps..
966TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
967 CompositorTiming initialCompositorTiming {
968 1000000000, // 1s deadline
969 16666667, // 16ms interval
970 50000000, // 50ms present latency
971 };
972 mCfeh->initializeCompositorTiming(initialCompositorTiming);
973
974 enableFrameTimestamps();
975
976 // We get the initial values before any frames are submitted.
977 nsecs_t compositeDeadline = 0;
978 nsecs_t compositeInterval = 0;
979 nsecs_t compositeToPresentLatency = 0;
980 mSurface->setNow(initialCompositorTiming.deadline - 1);
981 int result = native_window_get_compositor_timing(mWindow.get(),
982 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
983 EXPECT_EQ(NO_ERROR, result);
984 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
985 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
986 EXPECT_EQ(initialCompositorTiming.presentLatency,
987 compositeToPresentLatency);
988
989 const uint64_t fId1 = getNextFrameId();
990 dequeueAndQueue(0);
991 addFrameEvents(true, NO_FRAME_INDEX, 0);
992
993 // Still get the initial values because the frame events for frame 0
994 // didn't get a chance to piggyback on a queue or dequeue yet.
995 result = native_window_get_compositor_timing(mWindow.get(),
996 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
997 EXPECT_EQ(NO_ERROR, result);
998 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
999 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1000 EXPECT_EQ(initialCompositorTiming.presentLatency,
1001 compositeToPresentLatency);
1002
1003 const uint64_t fId2 = getNextFrameId();
1004 dequeueAndQueue(1);
1005 addFrameEvents(true, 0, 1);
1006
1007 // Now expect the composite values associated with frame 1.
1008 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1009 result = native_window_get_compositor_timing(mWindow.get(),
1010 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1011 EXPECT_EQ(NO_ERROR, result);
1012 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1013 compositeDeadline);
1014 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1015 compositeInterval);
1016 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1017 compositeToPresentLatency);
1018
1019 dequeueAndQueue(2);
1020 addFrameEvents(true, 1, 2);
1021
1022 // Now expect the composite values associated with frame 2.
1023 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1024 result = native_window_get_compositor_timing(mWindow.get(),
1025 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1026 EXPECT_EQ(NO_ERROR, result);
1027 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1028 compositeDeadline);
1029 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1030 compositeInterval);
1031 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1032 compositeToPresentLatency);
1033
1034 // Re-enabling frame timestamps should get the latest values.
1035 disableFrameTimestamps();
1036 enableFrameTimestamps();
1037
1038 // Now expect the composite values associated with frame 3.
1039 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1040 result = native_window_get_compositor_timing(mWindow.get(),
1041 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1042 EXPECT_EQ(NO_ERROR, result);
1043 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1044 compositeDeadline);
1045 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1046 compositeInterval);
1047 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1048 compositeToPresentLatency);
1049}
1050
1051// This verifies the compositor deadline properly snaps to the the next
1052// deadline based on the current time.
1053TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1054 CompositorTiming initialCompositorTiming {
1055 1000000000, // 1s deadline
1056 16666667, // 16ms interval
1057 50000000, // 50ms present latency
1058 };
1059 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1060
1061 enableFrameTimestamps();
1062
1063 nsecs_t compositeDeadline = 0;
1064 nsecs_t compositeInterval = 0;
1065 nsecs_t compositeToPresentLatency = 0;
1066
1067 // A "now" just before the deadline snaps to the deadline.
1068 mSurface->setNow(initialCompositorTiming.deadline - 1);
1069 int result = native_window_get_compositor_timing(mWindow.get(),
1070 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1071 EXPECT_EQ(NO_ERROR, result);
1072 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1073 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1074 EXPECT_EQ(expectedDeadline, compositeDeadline);
1075
1076 const uint64_t fId1 = getNextFrameId();
1077 dequeueAndQueue(0);
1078 addFrameEvents(true, NO_FRAME_INDEX, 0);
1079
1080 // A "now" just after the deadline snaps properly.
1081 mSurface->setNow(initialCompositorTiming.deadline + 1);
1082 result = native_window_get_compositor_timing(mWindow.get(),
1083 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1084 EXPECT_EQ(NO_ERROR, result);
1085 expectedDeadline =
1086 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1087 EXPECT_EQ(expectedDeadline, compositeDeadline);
1088
1089 const uint64_t fId2 = getNextFrameId();
1090 dequeueAndQueue(1);
1091 addFrameEvents(true, 0, 1);
1092
1093 // A "now" just after the next interval snaps properly.
1094 mSurface->setNow(
1095 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1096 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1097 result = native_window_get_compositor_timing(mWindow.get(),
1098 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1099 EXPECT_EQ(NO_ERROR, result);
1100 expectedDeadline =
1101 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1102 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1103 EXPECT_EQ(expectedDeadline, compositeDeadline);
1104
1105 dequeueAndQueue(2);
1106 addFrameEvents(true, 1, 2);
1107
1108 // A "now" over 1 interval before the deadline snaps properly.
1109 mSurface->setNow(
1110 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1111 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1112 result = native_window_get_compositor_timing(mWindow.get(),
1113 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1114 EXPECT_EQ(NO_ERROR, result);
1115 expectedDeadline =
1116 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1117 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1118 EXPECT_EQ(expectedDeadline, compositeDeadline);
1119
1120 // Re-enabling frame timestamps should get the latest values.
1121 disableFrameTimestamps();
1122 enableFrameTimestamps();
1123
1124 // A "now" over 2 intervals before the deadline snaps properly.
1125 mSurface->setNow(
1126 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1127 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1128 result = native_window_get_compositor_timing(mWindow.get(),
1129 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1130 EXPECT_EQ(NO_ERROR, result);
1131 expectedDeadline =
1132 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1133 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1134 EXPECT_EQ(expectedDeadline, compositeDeadline);
1135}
1136
Brian Anderson1049d1d2016-12-16 17:25:57 -08001137// This verifies the timestamps recorded in the consumer's
1138// FrameTimestampsHistory are properly retrieved by the producer for the
1139// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001140TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1141 enableFrameTimestamps();
1142
Brian Anderson1049d1d2016-12-16 17:25:57 -08001143 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001144 dequeueAndQueue(0);
1145 mFrames[0].signalQueueFences();
1146
Brian Anderson1049d1d2016-12-16 17:25:57 -08001147 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001148 dequeueAndQueue(1);
1149 mFrames[1].signalQueueFences();
1150
1151 addFrameEvents(true, NO_FRAME_INDEX, 0);
1152 mFrames[0].signalRefreshFences();
1153 addFrameEvents(true, 0, 1);
1154 mFrames[0].signalReleaseFences();
1155 mFrames[1].signalRefreshFences();
1156
1157 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001158 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001159 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001160 EXPECT_EQ(NO_ERROR, result);
1161 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1162 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001163 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1164 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1165 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001166 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1167 outGpuCompositionDoneTime);
1168 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001169 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001170 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1171
1172 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001173 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001174 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001175 EXPECT_EQ(NO_ERROR, result);
1176 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1177 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001178 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1179 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1180 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001181 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1182 outGpuCompositionDoneTime);
1183 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001184 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1185 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001186}
1187
1188// This test verifies the acquire fence recorded by the consumer is not sent
1189// back to the producer and the producer saves its own fence.
1190TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1191 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001192
Brian Anderson3da8d272016-07-28 16:20:47 -07001193 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001194 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001195 dequeueAndQueue(0);
1196
1197 // Verify queue-related timestamps for f1 are available immediately in the
1198 // producer without asking the consumer again, even before signaling the
1199 // acquire fence.
1200 resetTimestamps();
1201 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001202 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001203 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001204 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001205 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1206 EXPECT_EQ(NO_ERROR, result);
1207 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001208 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001209
1210 // Signal acquire fences. Verify a sync call still isn't necessary.
1211 mFrames[0].signalQueueFences();
1212
1213 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001214 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001215 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001216 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001217 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1218 EXPECT_EQ(NO_ERROR, result);
1219 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1220 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1221
1222 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001223 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001224 dequeueAndQueue(1);
1225
1226 // Verify queue-related timestamps for f2 are available immediately in the
1227 // producer without asking the consumer again, even before signaling the
1228 // acquire fence.
1229 resetTimestamps();
1230 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001231 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001232 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001233 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001234 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1235 EXPECT_EQ(NO_ERROR, result);
1236 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001237 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001238
1239 // Signal acquire fences. Verify a sync call still isn't necessary.
1240 mFrames[1].signalQueueFences();
1241
1242 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001243 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001244 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001245 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001246 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1247 EXPECT_EQ(NO_ERROR, result);
1248 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1249 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1250}
1251
1252TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1253 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001254
1255 // Dequeue and queue frame 1.
1256 dequeueAndQueue(0);
1257 mFrames[0].signalQueueFences();
1258
1259 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001260 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001261 dequeueAndQueue(1);
1262 mFrames[1].signalQueueFences();
1263
1264 addFrameEvents(true, NO_FRAME_INDEX, 0);
1265 mFrames[0].signalRefreshFences();
1266 addFrameEvents(true, 0, 1);
1267 mFrames[0].signalReleaseFences();
1268 mFrames[1].signalRefreshFences();
1269
1270 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001271 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001272 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001273 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1274 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001275 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001276 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1277}
1278
1279// This test verifies that fences can signal and update timestamps producer
1280// side without an additional sync call to the consumer.
1281TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1282 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001283
1284 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001285 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001286 dequeueAndQueue(0);
1287 mFrames[0].signalQueueFences();
1288
1289 // Dequeue and queue frame 2.
1290 dequeueAndQueue(1);
1291 mFrames[1].signalQueueFences();
1292
1293 addFrameEvents(true, NO_FRAME_INDEX, 0);
1294 addFrameEvents(true, 0, 1);
1295
1296 // Verify available timestamps are correct for frame 1, before any
1297 // fence has been signaled.
1298 // Note: A sync call is necessary here since the events triggered by
1299 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001300 resetTimestamps();
1301 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001302 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001303 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1304 EXPECT_EQ(NO_ERROR, result);
1305 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1306 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001307 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1308 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1309 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001310 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1311 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001312 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001313 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001314
1315 // Verify available timestamps are correct for frame 1 again, before any
1316 // fence has been signaled.
1317 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001318 resetTimestamps();
1319 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001320 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001321 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1322 EXPECT_EQ(NO_ERROR, result);
1323 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1324 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001325 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1326 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1327 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001328 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1329 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001330 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001331 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001332
1333 // Signal the fences for frame 1.
1334 mFrames[0].signalRefreshFences();
1335 mFrames[0].signalReleaseFences();
1336
1337 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001338 resetTimestamps();
1339 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001340 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001341 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1342 EXPECT_EQ(NO_ERROR, result);
1343 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1344 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001345 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1346 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1347 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001348 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1349 outGpuCompositionDoneTime);
1350 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001351 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001352 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1353}
1354
1355// This test verifies that if the frame wasn't GPU composited but has a refresh
1356// event a sync call isn't made to get the GPU composite done time since it will
1357// never exist.
1358TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1359 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001360
Brian Anderson3da8d272016-07-28 16:20:47 -07001361 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001362 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001363 dequeueAndQueue(0);
1364 mFrames[0].signalQueueFences();
1365
1366 // Dequeue and queue frame 2.
1367 dequeueAndQueue(1);
1368 mFrames[1].signalQueueFences();
1369
1370 addFrameEvents(false, NO_FRAME_INDEX, 0);
1371 addFrameEvents(false, 0, 1);
1372
1373 // Verify available timestamps are correct for frame 1, before any
1374 // fence has been signaled.
1375 // Note: A sync call is necessary here since the events triggered by
1376 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1377 resetTimestamps();
1378 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001379 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001380 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1381 EXPECT_EQ(NO_ERROR, result);
1382 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1383 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001384 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1385 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1386 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001387 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1388 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001389 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001390 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001391
1392 // Signal the fences for frame 1.
1393 mFrames[0].signalRefreshFences();
1394 mFrames[0].signalReleaseFences();
1395
1396 // Verify all timestamps, except GPU composition, are available without a
1397 // sync call.
1398 resetTimestamps();
1399 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001400 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001401 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1402 EXPECT_EQ(NO_ERROR, result);
1403 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1404 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001405 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1406 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1407 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001408 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001409 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001410 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001411 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1412}
1413
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001414// This test verifies that if the certain timestamps can't possibly exist for
1415// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001416TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001417 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001418
1419 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001420 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001421 dequeueAndQueue(0);
1422 mFrames[0].signalQueueFences();
1423
1424 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001425 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001426 dequeueAndQueue(1);
1427 mFrames[1].signalQueueFences();
1428
1429 addFrameEvents(false, NO_FRAME_INDEX, 0);
1430 addFrameEvents(false, 0, 1);
1431
1432 // Verify available timestamps are correct for frame 1, before any
1433 // fence has been signaled.
1434 // Note: A sync call is necessary here since the events triggered by
1435 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001436 resetTimestamps();
1437 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001438 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001439 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1440 EXPECT_EQ(NO_ERROR, result);
1441 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1442 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001443 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1444 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1445 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001446 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1447 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001448 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001449 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001450
1451 mFrames[0].signalRefreshFences();
1452 mFrames[0].signalReleaseFences();
1453 mFrames[1].signalRefreshFences();
1454
Brian Anderson1049d1d2016-12-16 17:25:57 -08001455 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001456 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001457 // available, a sync call should not occur because it's not possible for f2
1458 // to encounter the final value for those events until another frame is
1459 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001460 resetTimestamps();
1461 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001462 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001463 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1464 EXPECT_EQ(NO_ERROR, result);
1465 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1466 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001467 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1468 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1469 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001470 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001471 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001472 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1473 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001474}
1475
Brian Anderson6b376712017-04-04 10:51:39 -07001476// This test verifies there are no sync calls for present times
1477// when they aren't supported and that an error is returned.
1478
1479TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1480 enableFrameTimestamps();
1481 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1482
1483 // Dequeue and queue frame 1.
1484 const uint64_t fId1 = getNextFrameId();
1485 dequeueAndQueue(0);
1486
1487 // Verify a query for the Present times do not trigger a sync call if they
1488 // are not supported.
1489 resetTimestamps();
1490 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1491 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1492 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1493 &outDisplayPresentTime, nullptr, nullptr);
1494 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1495 EXPECT_EQ(BAD_VALUE, result);
1496 EXPECT_EQ(-1, outDisplayPresentTime);
1497}
1498
Jamie Gennis134f0422011-03-08 12:18:54 -08001499}