blob: ce114864aaa27ecb2242fe5724b63a199ea9d7e7 [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 Anderson3da8d272016-07-28 16:20:47 -0700371 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800372 sp<ISurfaceComposerClient> createScopedConnection(
373 const sp<IGraphicBufferProducer>& /* parent */) override {
374 return nullptr;
375 }
Chia-I Wu527747d2017-03-13 20:38:48 +0000376 sp<IGraphicBufferAlloc> createGraphicBufferAlloc() override {
377 return nullptr;
378 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700379 sp<IDisplayEventConnection> createDisplayEventConnection() override {
380 return nullptr;
381 }
382 sp<IBinder> createDisplay(const String8& /*displayName*/,
383 bool /*secure*/) override { return nullptr; }
384 void destroyDisplay(const sp<IBinder>& /*display */) override {}
385 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
386 void setTransactionState(const Vector<ComposerState>& /*state*/,
387 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
388 override {}
389 void bootFinished() override {}
390 bool authenticateSurfaceTexture(
391 const sp<IGraphicBufferProducer>& /*surface*/) const override {
392 return false;
393 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700394 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
395 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
396 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
397 status_t getDisplayStats(const sp<IBinder>& /*display*/,
398 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
399 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
400 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
401 override {
402 return NO_ERROR;
403 }
404 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
405 Vector<android_color_mode_t>* /*outColorModes*/) override {
406 return NO_ERROR;
407 }
408 android_color_mode_t getActiveColorMode(const sp<IBinder>& /*display*/)
409 override {
410 return HAL_COLOR_MODE_NATIVE;
411 }
412 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
413 android_color_mode_t /*colorMode*/) override { return NO_ERROR; }
414 status_t captureScreen(const sp<IBinder>& /*display*/,
415 const sp<IGraphicBufferProducer>& /*producer*/,
416 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800417 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700418 bool /*useIdentityTransform*/,
419 Rotation /*rotation*/) override { return NO_ERROR; }
420 status_t clearAnimationFrameStats() override { return NO_ERROR; }
421 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
422 return NO_ERROR;
423 }
424 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
425 HdrCapabilities* /*outCapabilities*/) const override {
426 return NO_ERROR;
427 }
428 status_t enableVSyncInjections(bool /*enable*/) override {
429 return NO_ERROR;
430 }
431 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
432
433protected:
434 IBinder* onAsBinder() override { return nullptr; }
435
436private:
437 bool mSupportsPresent{true};
438 bool mSupportsRetire{true};
439};
440
441class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
442public:
443 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
444 : mFenceMap(fenceMap) {}
445
446 ~FakeProducerFrameEventHistory() {}
447
448 void updateAcquireFence(uint64_t frameNumber,
449 std::shared_ptr<FenceTime>&& acquire) override {
450 // Verify the acquire fence being added isn't the one from the consumer.
451 EXPECT_NE(mConsumerAcquireFence, acquire);
452 // Override the fence, so we can verify this was called by the
453 // producer after the frame is queued.
454 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
455 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
456 }
457
458 void setAcquireFenceOverride(
459 const std::shared_ptr<FenceTime>& acquireFenceOverride,
460 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
461 mAcquireFenceOverride = acquireFenceOverride;
462 mConsumerAcquireFence = consumerAcquireFence;
463 }
464
465protected:
466 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
467 const override {
468 return mFenceMap->createFenceTimeForTest(fence);
469 }
470
471 FenceToFenceTimeMap* mFenceMap{nullptr};
472
473 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
474 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
475};
476
477
478class TestSurface : public Surface {
479public:
480 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
481 FenceToFenceTimeMap* fenceMap)
482 : Surface(bufferProducer),
483 mFakeSurfaceComposer(new FakeSurfaceComposer) {
484 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
485 mFrameEventHistory.reset(mFakeFrameEventHistory);
486 }
487
488 ~TestSurface() override {}
489
490 sp<ISurfaceComposer> composerService() const override {
491 return mFakeSurfaceComposer;
492 }
493
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800494 nsecs_t now() const override {
495 return mNow;
496 }
497
498 void setNow(nsecs_t now) {
499 mNow = now;
500 }
501
Brian Anderson3da8d272016-07-28 16:20:47 -0700502public:
503 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800504 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700505
506 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
507 // but this raw pointer gives access to test functionality.
508 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
509};
510
511
Brian Andersond0010582017-03-07 13:20:31 -0800512class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700513protected:
514 struct FenceAndFenceTime {
515 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
516 : mFence(new Fence),
517 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
518 sp<Fence> mFence { nullptr };
519 std::shared_ptr<FenceTime> mFenceTime { nullptr };
520 };
521
522 struct RefreshEvents {
523 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800524 : mFenceMap(fenceMap),
525 kCompositorTiming(
526 {refreshStart, refreshStart + 1, refreshStart + 2 }),
527 kStartTime(refreshStart + 3),
528 kGpuCompositionDoneTime(refreshStart + 4),
529 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700530
531 void signalPostCompositeFences() {
532 mFenceMap.signalAllForTest(
533 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
534 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
535 }
536
537 FenceToFenceTimeMap& mFenceMap;
538
539 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
540 FenceAndFenceTime mPresent { mFenceMap };
541
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800542 const CompositorTiming kCompositorTiming;
543
Brian Anderson3da8d272016-07-28 16:20:47 -0700544 const nsecs_t kStartTime;
545 const nsecs_t kGpuCompositionDoneTime;
546 const nsecs_t kPresentTime;
547 };
548
549 struct FrameEvents {
550 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
551 : mFenceMap(fenceMap),
552 kPostedTime(frameStartTime + 100),
553 kRequestedPresentTime(frameStartTime + 200),
554 kProducerAcquireTime(frameStartTime + 300),
555 kConsumerAcquireTime(frameStartTime + 301),
556 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700557 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700558 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700559 mRefreshes {
560 { mFenceMap, frameStartTime + 410 },
561 { mFenceMap, frameStartTime + 420 },
562 { mFenceMap, frameStartTime + 430 } } {}
563
564 void signalQueueFences() {
565 mFenceMap.signalAllForTest(
566 mAcquireConsumer.mFence, kConsumerAcquireTime);
567 mFenceMap.signalAllForTest(
568 mAcquireProducer.mFence, kProducerAcquireTime);
569 }
570
571 void signalRefreshFences() {
572 for (auto& re : mRefreshes) {
573 re.signalPostCompositeFences();
574 }
575 }
576
577 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700578 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
579 }
580
581 FenceToFenceTimeMap& mFenceMap;
582
583 FenceAndFenceTime mAcquireConsumer { mFenceMap };
584 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700585 FenceAndFenceTime mRelease { mFenceMap };
586
587 const nsecs_t kPostedTime;
588 const nsecs_t kRequestedPresentTime;
589 const nsecs_t kProducerAcquireTime;
590 const nsecs_t kConsumerAcquireTime;
591 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700592 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700593 const nsecs_t kReleaseTime;
594
595 RefreshEvents mRefreshes[3];
596 };
597
Brian Andersond0010582017-03-07 13:20:31 -0800598 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700599
600 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700601 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
602 mFakeConsumer = new FakeConsumer;
603 mCfeh = &mFakeConsumer->mFrameEventHistory;
604 mConsumer->consumerConnect(mFakeConsumer, false);
605 mConsumer->setConsumerName(String8("TestConsumer"));
606 mSurface = new TestSurface(mProducer, &mFenceMap);
607 mWindow = mSurface;
608
609 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
610 NATIVE_WINDOW_API_CPU));
611 native_window_set_buffer_count(mWindow.get(), 4);
612 }
613
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800614 void disableFrameTimestamps() {
615 mFakeConsumer->mGetFrameTimestampsEnabled = false;
616 native_window_enable_frame_timestamps(mWindow.get(), 0);
617 mFrameTimestampsEnabled = false;
618 }
619
Brian Anderson3da8d272016-07-28 16:20:47 -0700620 void enableFrameTimestamps() {
621 mFakeConsumer->mGetFrameTimestampsEnabled = true;
622 native_window_enable_frame_timestamps(mWindow.get(), 1);
623 mFrameTimestampsEnabled = true;
624 }
625
Brian Anderson1049d1d2016-12-16 17:25:57 -0800626 int getAllFrameTimestamps(uint64_t frameId) {
627 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700628 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
629 &outFirstRefreshStartTime, &outLastRefreshStartTime,
630 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700631 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700632 }
633
Brian Anderson3da8d272016-07-28 16:20:47 -0700634 void resetTimestamps() {
635 outRequestedPresentTime = -1;
636 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700637 outLatchTime = -1;
638 outFirstRefreshStartTime = -1;
639 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700640 outGpuCompositionDoneTime = -1;
641 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700642 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700643 outReleaseTime = -1;
644 }
645
Brian Anderson1049d1d2016-12-16 17:25:57 -0800646 uint64_t getNextFrameId() {
647 uint64_t frameId = -1;
648 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
649 EXPECT_EQ(status, NO_ERROR);
650 return frameId;
651 }
652
Brian Anderson3da8d272016-07-28 16:20:47 -0700653 void dequeueAndQueue(uint64_t frameIndex) {
654 int fence = -1;
655 ANativeWindowBuffer* buffer = nullptr;
656 ASSERT_EQ(NO_ERROR,
657 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
658
659 int oldAddFrameTimestampsCount =
660 mFakeConsumer->mAddFrameTimestampsCount;
661
662 FrameEvents* frame = &mFrames[frameIndex];
663 uint64_t frameNumber = frameIndex + 1;
664
665 NewFrameEventsEntry fe;
666 fe.frameNumber = frameNumber;
667 fe.postedTime = frame->kPostedTime;
668 fe.requestedPresentTime = frame->kRequestedPresentTime;
669 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
670 mFakeConsumer->mNewFrameEntryOverride = fe;
671
672 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
673 frame->mAcquireProducer.mFenceTime,
674 frame->mAcquireConsumer.mFenceTime);
675
676 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
677
678 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
679
680 EXPECT_EQ(
681 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
682 mFakeConsumer->mAddFrameTimestampsCount);
683 }
684
685 void addFrameEvents(
686 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
687 FrameEvents* oldFrame =
688 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
689 FrameEvents* newFrame = &mFrames[iNewFrame];
690
691 uint64_t nOldFrame = iOldFrame + 1;
692 uint64_t nNewFrame = iNewFrame + 1;
693
Brian Anderson4e606e32017-03-16 15:34:57 -0700694 // Latch, Composite, and Release the frames in a plausible order.
695 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700696 // that's okay for the purposes of this test.
697 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
698
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700699 // Composite the previous frame one more time, which helps verify
700 // LastRefresh is updated properly.
701 if (oldFrame != nullptr) {
702 mCfeh->addPreComposition(nOldFrame,
703 oldFrame->mRefreshes[2].kStartTime);
704 gpuDoneFenceTime = gpuComposited ?
705 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
706 FenceTime::NO_FENCE;
707 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800708 oldFrame->mRefreshes[2].mPresent.mFenceTime,
709 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700710 }
711
712 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700713 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
714
715 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
716 gpuDoneFenceTime = gpuComposited ?
717 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
718 FenceTime::NO_FENCE;
719 // HWC2 releases the previous buffer after a new latch just before
720 // calling postComposition.
721 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700722 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700723 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
724 }
725 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800726 newFrame->mRefreshes[0].mPresent.mFenceTime,
727 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700728
Brian Anderson3da8d272016-07-28 16:20:47 -0700729 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
730 gpuDoneFenceTime = gpuComposited ?
731 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
732 FenceTime::NO_FENCE;
733 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800734 newFrame->mRefreshes[1].mPresent.mFenceTime,
735 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700736 }
737
Brian Anderson3da8d272016-07-28 16:20:47 -0700738 sp<IGraphicBufferProducer> mProducer;
739 sp<IGraphicBufferConsumer> mConsumer;
740 sp<FakeConsumer> mFakeConsumer;
741 ConsumerFrameEventHistory* mCfeh;
742 sp<TestSurface> mSurface;
743 sp<ANativeWindow> mWindow;
744
745 FenceToFenceTimeMap mFenceMap;
746
747 bool mFrameTimestampsEnabled = false;
748
749 int64_t outRequestedPresentTime = -1;
750 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700751 int64_t outLatchTime = -1;
752 int64_t outFirstRefreshStartTime = -1;
753 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700754 int64_t outGpuCompositionDoneTime = -1;
755 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700756 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700757 int64_t outReleaseTime = -1;
758
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800759 FrameEvents mFrames[3] {
760 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700761};
762
763
764// This test verifies that the frame timestamps are not retrieved when not
765// explicitly enabled via native_window_enable_frame_timestamps.
766// We want to check this to make sure there's no overhead for users
767// that don't need the timestamp information.
768TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
769 int fence;
770 ANativeWindowBuffer* buffer;
771
772 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
773 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
774
Brian Anderson1049d1d2016-12-16 17:25:57 -0800775 const uint64_t fId = getNextFrameId();
776
Brian Anderson3da8d272016-07-28 16:20:47 -0700777 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
778 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
779 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
780 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
781
782 // Verify the producer doesn't get frame timestamps piggybacked on queue.
783 // It is okay that frame timestamps are added in the consumer since it is
784 // still needed for SurfaceFlinger dumps.
785 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
786 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
787 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
788
789 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800790 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700791 EXPECT_EQ(INVALID_OPERATION, result);
792 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800793
794 // Verify compositor timing query fails.
795 nsecs_t compositeDeadline = 0;
796 nsecs_t compositeInterval = 0;
797 nsecs_t compositeToPresentLatency = 0;
798 result = native_window_get_compositor_timing(mWindow.get(),
799 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
800 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700801}
802
803// This test verifies that the frame timestamps are retrieved if explicitly
804// enabled via native_window_enable_frame_timestamps.
805TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800806 CompositorTiming initialCompositorTiming {
807 1000000000, // 1s deadline
808 16666667, // 16ms interval
809 50000000, // 50ms present latency
810 };
811 mCfeh->initializeCompositorTiming(initialCompositorTiming);
812
Brian Anderson3da8d272016-07-28 16:20:47 -0700813 enableFrameTimestamps();
814
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800815 // Verify the compositor timing query gets the initial compositor values
816 // after timststamps are enabled; even before the first frame is queued
817 // or dequeued.
818 nsecs_t compositeDeadline = 0;
819 nsecs_t compositeInterval = 0;
820 nsecs_t compositeToPresentLatency = 0;
821 mSurface->setNow(initialCompositorTiming.deadline - 1);
822 int result = native_window_get_compositor_timing(mWindow.get(),
823 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
824 EXPECT_EQ(NO_ERROR, result);
825 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
826 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
827 EXPECT_EQ(initialCompositorTiming.presentLatency,
828 compositeToPresentLatency);
829
Brian Anderson3da8d272016-07-28 16:20:47 -0700830 int fence;
831 ANativeWindowBuffer* buffer;
832
833 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800834 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700835
Brian Anderson1049d1d2016-12-16 17:25:57 -0800836 const uint64_t fId1 = getNextFrameId();
837
Brian Anderson3da8d272016-07-28 16:20:47 -0700838 // Verify getFrameTimestamps is piggybacked on dequeue.
839 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
840 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800841 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700842
843 NewFrameEventsEntry f1;
844 f1.frameNumber = 1;
845 f1.postedTime = mFrames[0].kPostedTime;
846 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
847 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
848 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
849 mFrames[0].mAcquireProducer.mFenceTime,
850 mFrames[0].mAcquireConsumer.mFenceTime);
851 mFakeConsumer->mNewFrameEntryOverride = f1;
852 mFrames[0].signalQueueFences();
853
854 // Verify getFrameTimestamps is piggybacked on queue.
855 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
856 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
857 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800858 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700859
860 // Verify queries for timestamps that the producer doesn't know about
861 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800862 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -0700863 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800864 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700865}
866
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800867TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
868 nsecs_t phase = 4000;
869 nsecs_t interval = 1000;
870
871 // Timestamp in previous interval.
872 nsecs_t timestamp = 3500;
873 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
874 timestamp, phase, interval));
875
876 // Timestamp in next interval.
877 timestamp = 4500;
878 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
879 timestamp, phase, interval));
880
881 // Timestamp multiple intervals before.
882 timestamp = 2500;
883 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
884 timestamp, phase, interval));
885
886 // Timestamp multiple intervals after.
887 timestamp = 6500;
888 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
889 timestamp, phase, interval));
890
891 // Timestamp on previous interval.
892 timestamp = 3000;
893 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
894 timestamp, phase, interval));
895
896 // Timestamp on next interval.
897 timestamp = 5000;
898 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
899 timestamp, phase, interval));
900
901 // Timestamp equal to phase.
902 timestamp = 4000;
903 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
904 timestamp, phase, interval));
905}
906
907// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
908// if the number of intervals elapsed is internally stored in an int.
909TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
910 nsecs_t phase = 0;
911 nsecs_t interval = 4000;
912 nsecs_t big_timestamp = 8635916564000;
913 int32_t intervals = big_timestamp / interval;
914
915 EXPECT_LT(intervals, 0);
916 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
917 big_timestamp, phase, interval));
918 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
919 big_timestamp, big_timestamp, interval));
920}
921
922// This verifies the compositor timing is updated by refresh events
923// and piggy backed on a queue, dequeue, and enabling of timestamps..
924TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
925 CompositorTiming initialCompositorTiming {
926 1000000000, // 1s deadline
927 16666667, // 16ms interval
928 50000000, // 50ms present latency
929 };
930 mCfeh->initializeCompositorTiming(initialCompositorTiming);
931
932 enableFrameTimestamps();
933
934 // We get the initial values before any frames are submitted.
935 nsecs_t compositeDeadline = 0;
936 nsecs_t compositeInterval = 0;
937 nsecs_t compositeToPresentLatency = 0;
938 mSurface->setNow(initialCompositorTiming.deadline - 1);
939 int result = native_window_get_compositor_timing(mWindow.get(),
940 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
941 EXPECT_EQ(NO_ERROR, result);
942 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
943 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
944 EXPECT_EQ(initialCompositorTiming.presentLatency,
945 compositeToPresentLatency);
946
947 const uint64_t fId1 = getNextFrameId();
948 dequeueAndQueue(0);
949 addFrameEvents(true, NO_FRAME_INDEX, 0);
950
951 // Still get the initial values because the frame events for frame 0
952 // didn't get a chance to piggyback on a queue or dequeue yet.
953 result = native_window_get_compositor_timing(mWindow.get(),
954 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
955 EXPECT_EQ(NO_ERROR, result);
956 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
957 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
958 EXPECT_EQ(initialCompositorTiming.presentLatency,
959 compositeToPresentLatency);
960
961 const uint64_t fId2 = getNextFrameId();
962 dequeueAndQueue(1);
963 addFrameEvents(true, 0, 1);
964
965 // Now expect the composite values associated with frame 1.
966 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
967 result = native_window_get_compositor_timing(mWindow.get(),
968 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
969 EXPECT_EQ(NO_ERROR, result);
970 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
971 compositeDeadline);
972 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
973 compositeInterval);
974 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
975 compositeToPresentLatency);
976
977 dequeueAndQueue(2);
978 addFrameEvents(true, 1, 2);
979
980 // Now expect the composite values associated with frame 2.
981 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
982 result = native_window_get_compositor_timing(mWindow.get(),
983 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
984 EXPECT_EQ(NO_ERROR, result);
985 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
986 compositeDeadline);
987 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
988 compositeInterval);
989 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
990 compositeToPresentLatency);
991
992 // Re-enabling frame timestamps should get the latest values.
993 disableFrameTimestamps();
994 enableFrameTimestamps();
995
996 // Now expect the composite values associated with frame 3.
997 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
998 result = native_window_get_compositor_timing(mWindow.get(),
999 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1000 EXPECT_EQ(NO_ERROR, result);
1001 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1002 compositeDeadline);
1003 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1004 compositeInterval);
1005 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1006 compositeToPresentLatency);
1007}
1008
1009// This verifies the compositor deadline properly snaps to the the next
1010// deadline based on the current time.
1011TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1012 CompositorTiming initialCompositorTiming {
1013 1000000000, // 1s deadline
1014 16666667, // 16ms interval
1015 50000000, // 50ms present latency
1016 };
1017 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1018
1019 enableFrameTimestamps();
1020
1021 nsecs_t compositeDeadline = 0;
1022 nsecs_t compositeInterval = 0;
1023 nsecs_t compositeToPresentLatency = 0;
1024
1025 // A "now" just before the deadline snaps to the deadline.
1026 mSurface->setNow(initialCompositorTiming.deadline - 1);
1027 int result = native_window_get_compositor_timing(mWindow.get(),
1028 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1029 EXPECT_EQ(NO_ERROR, result);
1030 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1031 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1032 EXPECT_EQ(expectedDeadline, compositeDeadline);
1033
1034 const uint64_t fId1 = getNextFrameId();
1035 dequeueAndQueue(0);
1036 addFrameEvents(true, NO_FRAME_INDEX, 0);
1037
1038 // A "now" just after the deadline snaps properly.
1039 mSurface->setNow(initialCompositorTiming.deadline + 1);
1040 result = native_window_get_compositor_timing(mWindow.get(),
1041 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1042 EXPECT_EQ(NO_ERROR, result);
1043 expectedDeadline =
1044 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1045 EXPECT_EQ(expectedDeadline, compositeDeadline);
1046
1047 const uint64_t fId2 = getNextFrameId();
1048 dequeueAndQueue(1);
1049 addFrameEvents(true, 0, 1);
1050
1051 // A "now" just after the next interval snaps properly.
1052 mSurface->setNow(
1053 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1054 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1055 result = native_window_get_compositor_timing(mWindow.get(),
1056 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1057 EXPECT_EQ(NO_ERROR, result);
1058 expectedDeadline =
1059 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1060 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1061 EXPECT_EQ(expectedDeadline, compositeDeadline);
1062
1063 dequeueAndQueue(2);
1064 addFrameEvents(true, 1, 2);
1065
1066 // A "now" over 1 interval before the deadline snaps properly.
1067 mSurface->setNow(
1068 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1069 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1070 result = native_window_get_compositor_timing(mWindow.get(),
1071 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1072 EXPECT_EQ(NO_ERROR, result);
1073 expectedDeadline =
1074 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1075 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1076 EXPECT_EQ(expectedDeadline, compositeDeadline);
1077
1078 // Re-enabling frame timestamps should get the latest values.
1079 disableFrameTimestamps();
1080 enableFrameTimestamps();
1081
1082 // A "now" over 2 intervals before the deadline snaps properly.
1083 mSurface->setNow(
1084 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1085 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1086 result = native_window_get_compositor_timing(mWindow.get(),
1087 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1088 EXPECT_EQ(NO_ERROR, result);
1089 expectedDeadline =
1090 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1091 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1092 EXPECT_EQ(expectedDeadline, compositeDeadline);
1093}
1094
Brian Anderson1049d1d2016-12-16 17:25:57 -08001095// This verifies the timestamps recorded in the consumer's
1096// FrameTimestampsHistory are properly retrieved by the producer for the
1097// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001098TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1099 enableFrameTimestamps();
1100
Brian Anderson1049d1d2016-12-16 17:25:57 -08001101 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001102 dequeueAndQueue(0);
1103 mFrames[0].signalQueueFences();
1104
Brian Anderson1049d1d2016-12-16 17:25:57 -08001105 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001106 dequeueAndQueue(1);
1107 mFrames[1].signalQueueFences();
1108
1109 addFrameEvents(true, NO_FRAME_INDEX, 0);
1110 mFrames[0].signalRefreshFences();
1111 addFrameEvents(true, 0, 1);
1112 mFrames[0].signalReleaseFences();
1113 mFrames[1].signalRefreshFences();
1114
1115 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001116 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001117 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001118 EXPECT_EQ(NO_ERROR, result);
1119 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1120 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001121 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1122 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1123 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001124 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1125 outGpuCompositionDoneTime);
1126 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001127 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001128 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1129
1130 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001131 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001132 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001133 EXPECT_EQ(NO_ERROR, result);
1134 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1135 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001136 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1137 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1138 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001139 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1140 outGpuCompositionDoneTime);
1141 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001142 EXPECT_EQ(0, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001143 EXPECT_EQ(0, outReleaseTime);
1144}
1145
1146// This test verifies the acquire fence recorded by the consumer is not sent
1147// back to the producer and the producer saves its own fence.
1148TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1149 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001150
Brian Anderson3da8d272016-07-28 16:20:47 -07001151 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001152 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001153 dequeueAndQueue(0);
1154
1155 // Verify queue-related timestamps for f1 are available immediately in the
1156 // producer without asking the consumer again, even before signaling the
1157 // acquire fence.
1158 resetTimestamps();
1159 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001160 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001161 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001162 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001163 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1164 EXPECT_EQ(NO_ERROR, result);
1165 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1166 EXPECT_EQ(0, outAcquireTime);
1167
1168 // Signal acquire fences. Verify a sync call still isn't necessary.
1169 mFrames[0].signalQueueFences();
1170
1171 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001172 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001173 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001174 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001175 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1176 EXPECT_EQ(NO_ERROR, result);
1177 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1178 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1179
1180 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001181 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001182 dequeueAndQueue(1);
1183
1184 // Verify queue-related timestamps for f2 are available immediately in the
1185 // producer without asking the consumer again, even before signaling the
1186 // acquire fence.
1187 resetTimestamps();
1188 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001189 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001190 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001191 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001192 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1193 EXPECT_EQ(NO_ERROR, result);
1194 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1195 EXPECT_EQ(0, outAcquireTime);
1196
1197 // Signal acquire fences. Verify a sync call still isn't necessary.
1198 mFrames[1].signalQueueFences();
1199
1200 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001201 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001202 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001203 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001204 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1205 EXPECT_EQ(NO_ERROR, result);
1206 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1207 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1208}
1209
1210TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1211 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001212
1213 // Dequeue and queue frame 1.
1214 dequeueAndQueue(0);
1215 mFrames[0].signalQueueFences();
1216
1217 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001218 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001219 dequeueAndQueue(1);
1220 mFrames[1].signalQueueFences();
1221
1222 addFrameEvents(true, NO_FRAME_INDEX, 0);
1223 mFrames[0].signalRefreshFences();
1224 addFrameEvents(true, 0, 1);
1225 mFrames[0].signalReleaseFences();
1226 mFrames[1].signalRefreshFences();
1227
1228 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001229 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001230 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson4e606e32017-03-16 15:34:57 -07001231 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001232 nullptr, nullptr, nullptr);
1233 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001234 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1235}
1236
1237// This test verifies that fences can signal and update timestamps producer
1238// side without an additional sync call to the consumer.
1239TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1240 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001241
1242 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001243 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001244 dequeueAndQueue(0);
1245 mFrames[0].signalQueueFences();
1246
1247 // Dequeue and queue frame 2.
1248 dequeueAndQueue(1);
1249 mFrames[1].signalQueueFences();
1250
1251 addFrameEvents(true, NO_FRAME_INDEX, 0);
1252 addFrameEvents(true, 0, 1);
1253
1254 // Verify available timestamps are correct for frame 1, before any
1255 // fence has been signaled.
1256 // Note: A sync call is necessary here since the events triggered by
1257 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001258 resetTimestamps();
1259 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001260 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001261 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1262 EXPECT_EQ(NO_ERROR, result);
1263 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1264 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001265 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1266 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1267 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001268 EXPECT_EQ(0, outGpuCompositionDoneTime);
1269 EXPECT_EQ(0, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001270 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001271 EXPECT_EQ(0, outReleaseTime);
1272
1273 // Verify available timestamps are correct for frame 1 again, before any
1274 // fence has been signaled.
1275 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001276 resetTimestamps();
1277 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001278 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001279 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1280 EXPECT_EQ(NO_ERROR, result);
1281 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1282 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001283 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1284 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1285 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001286 EXPECT_EQ(0, outGpuCompositionDoneTime);
1287 EXPECT_EQ(0, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001288 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001289 EXPECT_EQ(0, outReleaseTime);
1290
1291 // Signal the fences for frame 1.
1292 mFrames[0].signalRefreshFences();
1293 mFrames[0].signalReleaseFences();
1294
1295 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001296 resetTimestamps();
1297 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001298 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001299 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1300 EXPECT_EQ(NO_ERROR, result);
1301 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1302 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001303 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1304 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1305 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001306 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1307 outGpuCompositionDoneTime);
1308 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001309 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001310 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1311}
1312
1313// This test verifies that if the frame wasn't GPU composited but has a refresh
1314// event a sync call isn't made to get the GPU composite done time since it will
1315// never exist.
1316TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1317 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001318
Brian Anderson3da8d272016-07-28 16:20:47 -07001319 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001320 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001321 dequeueAndQueue(0);
1322 mFrames[0].signalQueueFences();
1323
1324 // Dequeue and queue frame 2.
1325 dequeueAndQueue(1);
1326 mFrames[1].signalQueueFences();
1327
1328 addFrameEvents(false, NO_FRAME_INDEX, 0);
1329 addFrameEvents(false, 0, 1);
1330
1331 // Verify available timestamps are correct for frame 1, before any
1332 // fence has been signaled.
1333 // Note: A sync call is necessary here since the events triggered by
1334 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1335 resetTimestamps();
1336 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001337 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001338 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1339 EXPECT_EQ(NO_ERROR, result);
1340 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1341 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001342 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1343 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1344 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001345 EXPECT_EQ(0, outGpuCompositionDoneTime);
1346 EXPECT_EQ(0, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001347 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001348 EXPECT_EQ(0, outReleaseTime);
1349
1350 // Signal the fences for frame 1.
1351 mFrames[0].signalRefreshFences();
1352 mFrames[0].signalReleaseFences();
1353
1354 // Verify all timestamps, except GPU composition, are available without a
1355 // sync call.
1356 resetTimestamps();
1357 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001358 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001359 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1360 EXPECT_EQ(NO_ERROR, result);
1361 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1362 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001363 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1364 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1365 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001366 EXPECT_EQ(0, outGpuCompositionDoneTime);
1367 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001368 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001369 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1370}
1371
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001372// This test verifies that if the certain timestamps can't possibly exist for
1373// the most recent frame, then a sync call is not done.
Brian Anderson3da8d272016-07-28 16:20:47 -07001374TEST_F(GetFrameTimestampsTest, NoRetireOrReleaseNoSync) {
1375 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001376
1377 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001378 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001379 dequeueAndQueue(0);
1380 mFrames[0].signalQueueFences();
1381
1382 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001383 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001384 dequeueAndQueue(1);
1385 mFrames[1].signalQueueFences();
1386
1387 addFrameEvents(false, NO_FRAME_INDEX, 0);
1388 addFrameEvents(false, 0, 1);
1389
1390 // Verify available timestamps are correct for frame 1, before any
1391 // fence has been signaled.
1392 // Note: A sync call is necessary here since the events triggered by
1393 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001394 resetTimestamps();
1395 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001396 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001397 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1398 EXPECT_EQ(NO_ERROR, result);
1399 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1400 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001401 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1402 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1403 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001404 EXPECT_EQ(0, outGpuCompositionDoneTime);
1405 EXPECT_EQ(0, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001406 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001407 EXPECT_EQ(0, outReleaseTime);
1408
1409 mFrames[0].signalRefreshFences();
1410 mFrames[0].signalReleaseFences();
1411 mFrames[1].signalRefreshFences();
1412
Brian Anderson1049d1d2016-12-16 17:25:57 -08001413 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001414 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001415 // available, a sync call should not occur because it's not possible for f2
1416 // to encounter the final value for those events until another frame is
1417 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001418 resetTimestamps();
1419 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001420 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001421 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1422 EXPECT_EQ(NO_ERROR, result);
1423 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1424 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001425 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1426 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1427 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001428 EXPECT_EQ(0, outGpuCompositionDoneTime);
1429 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001430 EXPECT_EQ(0, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001431 EXPECT_EQ(0, outReleaseTime);
1432}
1433
Jamie Gennis134f0422011-03-08 12:18:54 -08001434}