blob: 3932b9267430c6ea2c8e7ad4f0998ac5cfb6e357 [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
Brian Anderson3da8d272016-07-28 16:20:47 -070021#include <binder/ProcessState.h>
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -070022#include <cutils/properties.h>
23#include <gui/BufferItemConsumer.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070024#include <gui/IDisplayEventConnection.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080025#include <gui/ISurfaceComposer.h>
26#include <gui/Surface.h>
27#include <gui/SurfaceComposerClient.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070028#include <private/gui/ComposerService.h>
Dan Stozac1879002014-05-22 15:59:05 -070029#include <ui/Rect.h>
Jamie Gennis134f0422011-03-08 12:18:54 -080030#include <utils/String8.h>
31
Brian Anderson3da8d272016-07-28 16:20:47 -070032#include <limits>
Kalle Raita643f0942016-12-07 09:20:14 -080033#include <thread>
34
Jamie Gennis134f0422011-03-08 12:18:54 -080035namespace android {
36
Kalle Raita643f0942016-12-07 09:20:14 -080037using namespace std::chrono_literals;
38
Brian Anderson3da8d272016-07-28 16:20:47 -070039class FakeSurfaceComposer;
40class FakeProducerFrameEventHistory;
41
42static constexpr uint64_t NO_FRAME_INDEX = std::numeric_limits<uint64_t>::max();
43
Jamie Gennis134f0422011-03-08 12:18:54 -080044class SurfaceTest : public ::testing::Test {
45protected:
Mathias Agopian7c1a4872013-03-20 15:56:04 -070046
47 SurfaceTest() {
48 ProcessState::self()->startThreadPool();
49 }
50
Jamie Gennis134f0422011-03-08 12:18:54 -080051 virtual void SetUp() {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080052 mComposerClient = new SurfaceComposerClient;
Jamie Gennis134f0422011-03-08 12:18:54 -080053 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
54
Brian Andersond0010582017-03-07 13:20:31 -080055 // TODO(brianderson): The following sometimes fails and is a source of
56 // test flakiness.
Jamie Gennisfc850122011-04-25 16:40:05 -070057 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070058 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis134f0422011-03-08 12:18:54 -080059
60 ASSERT_TRUE(mSurfaceControl != NULL);
61 ASSERT_TRUE(mSurfaceControl->isValid());
62
Mathias Agopian698c0872011-06-28 19:09:31 -070063 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian9303eee2011-07-01 15:27:27 -070064 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff));
Jamie Gennis134f0422011-03-08 12:18:54 -080065 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070066 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennis134f0422011-03-08 12:18:54 -080067
68 mSurface = mSurfaceControl->getSurface();
69 ASSERT_TRUE(mSurface != NULL);
70 }
71
72 virtual void TearDown() {
73 mComposerClient->dispose();
74 }
75
76 sp<Surface> mSurface;
77 sp<SurfaceComposerClient> mComposerClient;
78 sp<SurfaceControl> mSurfaceControl;
79};
80
81TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
82 sp<ANativeWindow> anw(mSurface);
83 int result = -123;
84 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
85 &result);
86 EXPECT_EQ(NO_ERROR, err);
87 EXPECT_EQ(1, result);
88}
89
90TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
91 mSurfaceControl.clear();
Kalle Raita643f0942016-12-07 09:20:14 -080092 // Wait for the async clean-up to complete.
93 std::this_thread::sleep_for(50ms);
Jamie Gennis134f0422011-03-08 12:18:54 -080094
95 sp<ANativeWindow> anw(mSurface);
96 int result = -123;
97 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
98 &result);
99 EXPECT_EQ(NO_ERROR, err);
100 EXPECT_EQ(1, result);
101}
102
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800103// This test probably doesn't belong here.
Jamie Gennisc901ca02011-10-11 16:02:31 -0700104TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800105 sp<ANativeWindow> anw(mSurface);
106
107 // Verify the screenshot works with no protected buffers.
Dan Stoza5603a2f2014-04-07 13:41:37 -0700108 sp<IGraphicBufferProducer> producer;
109 sp<IGraphicBufferConsumer> consumer;
110 BufferQueue::createBufferQueue(&producer, &consumer);
111 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800112 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Brian Anderson3da8d272016-07-28 16:20:47 -0700113 sp<IBinder> display(sf->getBuiltInDisplay(
114 ISurfaceComposer::eDisplayIdMain));
Dan Stozac1879002014-05-22 15:59:05 -0700115 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800116 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800117
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700118 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
119 NATIVE_WINDOW_API_CPU));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800120 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
121 // that we need to dequeue a buffer in order for it to actually get
122 // allocated in SurfaceFlinger.
123 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
124 GRALLOC_USAGE_PROTECTED));
125 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Iliyan Malchev697526b2011-05-01 11:33:26 -0700126 ANativeWindowBuffer* buf = 0;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700127
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700128 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700129 if (err) {
130 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
131 // that's okay as long as this is the reason for the failure.
132 // try again without the GRALLOC_USAGE_PROTECTED bit.
133 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700134 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
135 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700136 return;
137 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700138 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700139
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800140 for (int i = 0; i < 4; i++) {
141 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700142 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
143 &buf));
144 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800145 }
Dan Stozac1879002014-05-22 15:59:05 -0700146 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800147 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800148}
149
Jamie Gennis391bbe22011-03-14 15:00:06 -0700150TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
151 sp<ANativeWindow> anw(mSurface);
152 int result = -123;
153 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
154 EXPECT_EQ(NO_ERROR, err);
155 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
156}
157
Craig Donner6ebc46a2016-10-21 15:23:44 -0700158TEST_F(SurfaceTest, LayerCountIsOne) {
159 sp<ANativeWindow> anw(mSurface);
160 int result = -123;
161 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
162 EXPECT_EQ(NO_ERROR, err);
163 EXPECT_EQ(1, result);
164}
165
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700166TEST_F(SurfaceTest, QueryConsumerUsage) {
167 const int TEST_USAGE_FLAGS =
168 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700169 sp<IGraphicBufferProducer> producer;
170 sp<IGraphicBufferConsumer> consumer;
171 BufferQueue::createBufferQueue(&producer, &consumer);
172 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700173 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700174 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700175
176 sp<ANativeWindow> anw(s);
177
178 int flags = -1;
179 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
180
181 ASSERT_EQ(NO_ERROR, err);
182 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
183}
184
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800185TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
186 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
187 sp<IGraphicBufferProducer> producer;
188 sp<IGraphicBufferConsumer> consumer;
189 BufferQueue::createBufferQueue(&producer, &consumer);
190 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
191
192 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
193
194 sp<Surface> s = new Surface(producer);
195
196 sp<ANativeWindow> anw(s);
197
198 android_dataspace dataSpace;
199
200 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
201 reinterpret_cast<int*>(&dataSpace));
202
203 ASSERT_EQ(NO_ERROR, err);
204 ASSERT_EQ(TEST_DATASPACE, dataSpace);
205}
206
Dan Stoza812ed062015-06-02 15:45:22 -0700207TEST_F(SurfaceTest, SettingGenerationNumber) {
208 sp<IGraphicBufferProducer> producer;
209 sp<IGraphicBufferConsumer> consumer;
210 BufferQueue::createBufferQueue(&producer, &consumer);
211 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
212 sp<Surface> surface = new Surface(producer);
213 sp<ANativeWindow> window(surface);
214
215 // Allocate a buffer with a generation number of 0
216 ANativeWindowBuffer* buffer;
217 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700218 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
219 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700220 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
221 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
222
223 // Detach the buffer and check its generation number
224 sp<GraphicBuffer> graphicBuffer;
225 sp<Fence> fence;
226 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
227 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
228
229 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
230 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
231
232 // This should change the generation number of the GraphicBuffer
233 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
234
235 // Check that the new generation number sticks with the buffer
236 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
237 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
238 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
239 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
240}
241
Dan Stozac6f30bd2015-06-08 09:32:50 -0700242TEST_F(SurfaceTest, GetConsumerName) {
243 sp<IGraphicBufferProducer> producer;
244 sp<IGraphicBufferConsumer> consumer;
245 BufferQueue::createBufferQueue(&producer, &consumer);
246
247 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
248 consumer->consumerConnect(dummyConsumer, false);
249 consumer->setConsumerName(String8("TestConsumer"));
250
251 sp<Surface> surface = new Surface(producer);
252 sp<ANativeWindow> window(surface);
253 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
254
255 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
256}
257
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700258TEST_F(SurfaceTest, GetWideColorSupport) {
259 sp<IGraphicBufferProducer> producer;
260 sp<IGraphicBufferConsumer> consumer;
261 BufferQueue::createBufferQueue(&producer, &consumer);
262
263 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
264 consumer->consumerConnect(dummyConsumer, false);
265 consumer->setConsumerName(String8("TestConsumer"));
266
267 sp<Surface> surface = new Surface(producer);
268 sp<ANativeWindow> window(surface);
269 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
270
271 bool supported;
272 surface->getWideColorSupport(&supported);
273
274 // TODO(courtneygo): How can we know what device we are on to
275 // verify that this is correct?
276 char product[PROPERTY_VALUE_MAX] = "0";
277 property_get("ro.build.product", product, "0");
278 std::cerr << "[ ] product = " << product << std::endl;
279
280 if (strcmp("marlin", product) == 0 || strcmp("sailfish", product) == 0) {
281 ASSERT_EQ(true, supported);
282 } else {
283 ASSERT_EQ(false, supported);
284 }
285}
286
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800287TEST_F(SurfaceTest, DynamicSetBufferCount) {
288 sp<IGraphicBufferProducer> producer;
289 sp<IGraphicBufferConsumer> consumer;
290 BufferQueue::createBufferQueue(&producer, &consumer);
291
292 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
293 consumer->consumerConnect(dummyConsumer, false);
294 consumer->setConsumerName(String8("TestConsumer"));
295
296 sp<Surface> surface = new Surface(producer);
297 sp<ANativeWindow> window(surface);
298
299 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
300 NATIVE_WINDOW_API_CPU));
301 native_window_set_buffer_count(window.get(), 4);
302
303 int fence;
304 ANativeWindowBuffer* buffer;
305 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
306 native_window_set_buffer_count(window.get(), 3);
307 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
308 native_window_set_buffer_count(window.get(), 2);
309 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
310 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
311}
312
Brian Anderson3da8d272016-07-28 16:20:47 -0700313
314class FakeConsumer : public BnConsumerListener {
315public:
316 void onFrameAvailable(const BufferItem& /*item*/) override {}
317 void onBuffersReleased() override {}
318 void onSidebandStreamChanged() override {}
319
320 void addAndGetFrameTimestamps(
321 const NewFrameEventsEntry* newTimestamps,
322 FrameEventHistoryDelta* outDelta) override {
323 if (newTimestamps) {
324 if (mGetFrameTimestampsEnabled) {
325 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
326 "Test should set mNewFrameEntryOverride before queuing "
327 "a frame.";
328 EXPECT_EQ(newTimestamps->frameNumber,
329 mNewFrameEntryOverride.frameNumber) <<
330 "Test attempting to add NewFrameEntryOverride with "
331 "incorrect frame number.";
332 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
333 mNewFrameEntryOverride.frameNumber = 0;
334 }
335 mAddFrameTimestampsCount++;
336 mLastAddedFrameNumber = newTimestamps->frameNumber;
337 }
338 if (outDelta) {
339 mFrameEventHistory.getAndResetDelta(outDelta);
340 mGetFrameTimestampsCount++;
341 }
342 mAddAndGetFrameTimestampsCallCount++;
343 }
344
345 bool mGetFrameTimestampsEnabled = false;
346
347 ConsumerFrameEventHistory mFrameEventHistory;
348 int mAddAndGetFrameTimestampsCallCount = 0;
349 int mAddFrameTimestampsCount = 0;
350 int mGetFrameTimestampsCount = 0;
351 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
352
353 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
354};
355
356
357class FakeSurfaceComposer : public ISurfaceComposer{
358public:
359 ~FakeSurfaceComposer() override {}
360
Brian Anderson3da8d272016-07-28 16:20:47 -0700361 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800362 sp<ISurfaceComposerClient> createScopedConnection(
363 const sp<IGraphicBufferProducer>& /* parent */) override {
364 return nullptr;
365 }
Chia-I Wu527747d2017-03-13 20:38:48 +0000366 sp<IGraphicBufferAlloc> createGraphicBufferAlloc() override {
367 return nullptr;
368 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700369 sp<IDisplayEventConnection> createDisplayEventConnection() override {
370 return nullptr;
371 }
372 sp<IBinder> createDisplay(const String8& /*displayName*/,
373 bool /*secure*/) override { return nullptr; }
374 void destroyDisplay(const sp<IBinder>& /*display */) override {}
375 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
376 void setTransactionState(const Vector<ComposerState>& /*state*/,
377 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
378 override {}
379 void bootFinished() override {}
380 bool authenticateSurfaceTexture(
381 const sp<IGraphicBufferProducer>& /*surface*/) const override {
382 return false;
383 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700384 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
385 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
386 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
387 status_t getDisplayStats(const sp<IBinder>& /*display*/,
388 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
389 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
390 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
391 override {
392 return NO_ERROR;
393 }
394 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
395 Vector<android_color_mode_t>* /*outColorModes*/) override {
396 return NO_ERROR;
397 }
398 android_color_mode_t getActiveColorMode(const sp<IBinder>& /*display*/)
399 override {
400 return HAL_COLOR_MODE_NATIVE;
401 }
402 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
403 android_color_mode_t /*colorMode*/) override { return NO_ERROR; }
404 status_t captureScreen(const sp<IBinder>& /*display*/,
405 const sp<IGraphicBufferProducer>& /*producer*/,
406 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800407 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700408 bool /*useIdentityTransform*/,
409 Rotation /*rotation*/) override { return NO_ERROR; }
410 status_t clearAnimationFrameStats() override { return NO_ERROR; }
411 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
412 return NO_ERROR;
413 }
414 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
415 HdrCapabilities* /*outCapabilities*/) const override {
416 return NO_ERROR;
417 }
418 status_t enableVSyncInjections(bool /*enable*/) override {
419 return NO_ERROR;
420 }
421 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
422
423protected:
424 IBinder* onAsBinder() override { return nullptr; }
425
426private:
427 bool mSupportsPresent{true};
428 bool mSupportsRetire{true};
429};
430
431class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
432public:
433 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
434 : mFenceMap(fenceMap) {}
435
436 ~FakeProducerFrameEventHistory() {}
437
438 void updateAcquireFence(uint64_t frameNumber,
439 std::shared_ptr<FenceTime>&& acquire) override {
440 // Verify the acquire fence being added isn't the one from the consumer.
441 EXPECT_NE(mConsumerAcquireFence, acquire);
442 // Override the fence, so we can verify this was called by the
443 // producer after the frame is queued.
444 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
445 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
446 }
447
448 void setAcquireFenceOverride(
449 const std::shared_ptr<FenceTime>& acquireFenceOverride,
450 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
451 mAcquireFenceOverride = acquireFenceOverride;
452 mConsumerAcquireFence = consumerAcquireFence;
453 }
454
455protected:
456 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
457 const override {
458 return mFenceMap->createFenceTimeForTest(fence);
459 }
460
461 FenceToFenceTimeMap* mFenceMap{nullptr};
462
463 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
464 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
465};
466
467
468class TestSurface : public Surface {
469public:
470 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
471 FenceToFenceTimeMap* fenceMap)
472 : Surface(bufferProducer),
473 mFakeSurfaceComposer(new FakeSurfaceComposer) {
474 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
475 mFrameEventHistory.reset(mFakeFrameEventHistory);
476 }
477
478 ~TestSurface() override {}
479
480 sp<ISurfaceComposer> composerService() const override {
481 return mFakeSurfaceComposer;
482 }
483
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800484 nsecs_t now() const override {
485 return mNow;
486 }
487
488 void setNow(nsecs_t now) {
489 mNow = now;
490 }
491
Brian Anderson3da8d272016-07-28 16:20:47 -0700492public:
493 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800494 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700495
496 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
497 // but this raw pointer gives access to test functionality.
498 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
499};
500
501
Brian Andersond0010582017-03-07 13:20:31 -0800502class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700503protected:
504 struct FenceAndFenceTime {
505 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
506 : mFence(new Fence),
507 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
508 sp<Fence> mFence { nullptr };
509 std::shared_ptr<FenceTime> mFenceTime { nullptr };
510 };
511
512 struct RefreshEvents {
513 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800514 : mFenceMap(fenceMap),
515 kCompositorTiming(
516 {refreshStart, refreshStart + 1, refreshStart + 2 }),
517 kStartTime(refreshStart + 3),
518 kGpuCompositionDoneTime(refreshStart + 4),
519 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700520
521 void signalPostCompositeFences() {
522 mFenceMap.signalAllForTest(
523 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
524 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
525 }
526
527 FenceToFenceTimeMap& mFenceMap;
528
529 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
530 FenceAndFenceTime mPresent { mFenceMap };
531
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800532 const CompositorTiming kCompositorTiming;
533
Brian Anderson3da8d272016-07-28 16:20:47 -0700534 const nsecs_t kStartTime;
535 const nsecs_t kGpuCompositionDoneTime;
536 const nsecs_t kPresentTime;
537 };
538
539 struct FrameEvents {
540 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
541 : mFenceMap(fenceMap),
542 kPostedTime(frameStartTime + 100),
543 kRequestedPresentTime(frameStartTime + 200),
544 kProducerAcquireTime(frameStartTime + 300),
545 kConsumerAcquireTime(frameStartTime + 301),
546 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700547 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -0700548 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -0700549 mRefreshes {
550 { mFenceMap, frameStartTime + 410 },
551 { mFenceMap, frameStartTime + 420 },
552 { mFenceMap, frameStartTime + 430 } } {}
553
554 void signalQueueFences() {
555 mFenceMap.signalAllForTest(
556 mAcquireConsumer.mFence, kConsumerAcquireTime);
557 mFenceMap.signalAllForTest(
558 mAcquireProducer.mFence, kProducerAcquireTime);
559 }
560
561 void signalRefreshFences() {
562 for (auto& re : mRefreshes) {
563 re.signalPostCompositeFences();
564 }
565 }
566
567 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700568 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
569 }
570
571 FenceToFenceTimeMap& mFenceMap;
572
573 FenceAndFenceTime mAcquireConsumer { mFenceMap };
574 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -0700575 FenceAndFenceTime mRelease { mFenceMap };
576
577 const nsecs_t kPostedTime;
578 const nsecs_t kRequestedPresentTime;
579 const nsecs_t kProducerAcquireTime;
580 const nsecs_t kConsumerAcquireTime;
581 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700582 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700583 const nsecs_t kReleaseTime;
584
585 RefreshEvents mRefreshes[3];
586 };
587
Brian Andersond0010582017-03-07 13:20:31 -0800588 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700589
590 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700591 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
592 mFakeConsumer = new FakeConsumer;
593 mCfeh = &mFakeConsumer->mFrameEventHistory;
594 mConsumer->consumerConnect(mFakeConsumer, false);
595 mConsumer->setConsumerName(String8("TestConsumer"));
596 mSurface = new TestSurface(mProducer, &mFenceMap);
597 mWindow = mSurface;
598
599 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
600 NATIVE_WINDOW_API_CPU));
601 native_window_set_buffer_count(mWindow.get(), 4);
602 }
603
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800604 void disableFrameTimestamps() {
605 mFakeConsumer->mGetFrameTimestampsEnabled = false;
606 native_window_enable_frame_timestamps(mWindow.get(), 0);
607 mFrameTimestampsEnabled = false;
608 }
609
Brian Anderson3da8d272016-07-28 16:20:47 -0700610 void enableFrameTimestamps() {
611 mFakeConsumer->mGetFrameTimestampsEnabled = true;
612 native_window_enable_frame_timestamps(mWindow.get(), 1);
613 mFrameTimestampsEnabled = true;
614 }
615
Brian Anderson1049d1d2016-12-16 17:25:57 -0800616 int getAllFrameTimestamps(uint64_t frameId) {
617 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700618 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
619 &outFirstRefreshStartTime, &outLastRefreshStartTime,
620 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700621 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700622 }
623
Brian Anderson3da8d272016-07-28 16:20:47 -0700624 void resetTimestamps() {
625 outRequestedPresentTime = -1;
626 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700627 outLatchTime = -1;
628 outFirstRefreshStartTime = -1;
629 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700630 outGpuCompositionDoneTime = -1;
631 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700632 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700633 outReleaseTime = -1;
634 }
635
Brian Anderson1049d1d2016-12-16 17:25:57 -0800636 uint64_t getNextFrameId() {
637 uint64_t frameId = -1;
638 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
639 EXPECT_EQ(status, NO_ERROR);
640 return frameId;
641 }
642
Brian Anderson3da8d272016-07-28 16:20:47 -0700643 void dequeueAndQueue(uint64_t frameIndex) {
644 int fence = -1;
645 ANativeWindowBuffer* buffer = nullptr;
646 ASSERT_EQ(NO_ERROR,
647 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
648
649 int oldAddFrameTimestampsCount =
650 mFakeConsumer->mAddFrameTimestampsCount;
651
652 FrameEvents* frame = &mFrames[frameIndex];
653 uint64_t frameNumber = frameIndex + 1;
654
655 NewFrameEventsEntry fe;
656 fe.frameNumber = frameNumber;
657 fe.postedTime = frame->kPostedTime;
658 fe.requestedPresentTime = frame->kRequestedPresentTime;
659 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
660 mFakeConsumer->mNewFrameEntryOverride = fe;
661
662 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
663 frame->mAcquireProducer.mFenceTime,
664 frame->mAcquireConsumer.mFenceTime);
665
666 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
667
668 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
669
670 EXPECT_EQ(
671 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
672 mFakeConsumer->mAddFrameTimestampsCount);
673 }
674
675 void addFrameEvents(
676 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
677 FrameEvents* oldFrame =
678 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
679 FrameEvents* newFrame = &mFrames[iNewFrame];
680
681 uint64_t nOldFrame = iOldFrame + 1;
682 uint64_t nNewFrame = iNewFrame + 1;
683
Brian Anderson4e606e32017-03-16 15:34:57 -0700684 // Latch, Composite, and Release the frames in a plausible order.
685 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -0700686 // that's okay for the purposes of this test.
687 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
688
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700689 // Composite the previous frame one more time, which helps verify
690 // LastRefresh is updated properly.
691 if (oldFrame != nullptr) {
692 mCfeh->addPreComposition(nOldFrame,
693 oldFrame->mRefreshes[2].kStartTime);
694 gpuDoneFenceTime = gpuComposited ?
695 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
696 FenceTime::NO_FENCE;
697 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800698 oldFrame->mRefreshes[2].mPresent.mFenceTime,
699 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700700 }
701
702 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700703 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
704
705 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
706 gpuDoneFenceTime = gpuComposited ?
707 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
708 FenceTime::NO_FENCE;
709 // HWC2 releases the previous buffer after a new latch just before
710 // calling postComposition.
711 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700712 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700713 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
714 }
715 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800716 newFrame->mRefreshes[0].mPresent.mFenceTime,
717 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700718
Brian Anderson3da8d272016-07-28 16:20:47 -0700719 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
720 gpuDoneFenceTime = gpuComposited ?
721 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
722 FenceTime::NO_FENCE;
723 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800724 newFrame->mRefreshes[1].mPresent.mFenceTime,
725 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700726 }
727
Brian Anderson3da8d272016-07-28 16:20:47 -0700728 sp<IGraphicBufferProducer> mProducer;
729 sp<IGraphicBufferConsumer> mConsumer;
730 sp<FakeConsumer> mFakeConsumer;
731 ConsumerFrameEventHistory* mCfeh;
732 sp<TestSurface> mSurface;
733 sp<ANativeWindow> mWindow;
734
735 FenceToFenceTimeMap mFenceMap;
736
737 bool mFrameTimestampsEnabled = false;
738
739 int64_t outRequestedPresentTime = -1;
740 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700741 int64_t outLatchTime = -1;
742 int64_t outFirstRefreshStartTime = -1;
743 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700744 int64_t outGpuCompositionDoneTime = -1;
745 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700746 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700747 int64_t outReleaseTime = -1;
748
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800749 FrameEvents mFrames[3] {
750 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700751};
752
753
754// This test verifies that the frame timestamps are not retrieved when not
755// explicitly enabled via native_window_enable_frame_timestamps.
756// We want to check this to make sure there's no overhead for users
757// that don't need the timestamp information.
758TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
759 int fence;
760 ANativeWindowBuffer* buffer;
761
762 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
763 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
764
Brian Anderson1049d1d2016-12-16 17:25:57 -0800765 const uint64_t fId = getNextFrameId();
766
Brian Anderson3da8d272016-07-28 16:20:47 -0700767 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
768 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
769 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
770 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
771
772 // Verify the producer doesn't get frame timestamps piggybacked on queue.
773 // It is okay that frame timestamps are added in the consumer since it is
774 // still needed for SurfaceFlinger dumps.
775 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
776 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
777 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
778
779 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800780 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700781 EXPECT_EQ(INVALID_OPERATION, result);
782 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800783
784 // Verify compositor timing query fails.
785 nsecs_t compositeDeadline = 0;
786 nsecs_t compositeInterval = 0;
787 nsecs_t compositeToPresentLatency = 0;
788 result = native_window_get_compositor_timing(mWindow.get(),
789 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
790 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700791}
792
793// This test verifies that the frame timestamps are retrieved if explicitly
794// enabled via native_window_enable_frame_timestamps.
795TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800796 CompositorTiming initialCompositorTiming {
797 1000000000, // 1s deadline
798 16666667, // 16ms interval
799 50000000, // 50ms present latency
800 };
801 mCfeh->initializeCompositorTiming(initialCompositorTiming);
802
Brian Anderson3da8d272016-07-28 16:20:47 -0700803 enableFrameTimestamps();
804
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800805 // Verify the compositor timing query gets the initial compositor values
806 // after timststamps are enabled; even before the first frame is queued
807 // or dequeued.
808 nsecs_t compositeDeadline = 0;
809 nsecs_t compositeInterval = 0;
810 nsecs_t compositeToPresentLatency = 0;
811 mSurface->setNow(initialCompositorTiming.deadline - 1);
812 int result = native_window_get_compositor_timing(mWindow.get(),
813 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
814 EXPECT_EQ(NO_ERROR, result);
815 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
816 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
817 EXPECT_EQ(initialCompositorTiming.presentLatency,
818 compositeToPresentLatency);
819
Brian Anderson3da8d272016-07-28 16:20:47 -0700820 int fence;
821 ANativeWindowBuffer* buffer;
822
823 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800824 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700825
Brian Anderson1049d1d2016-12-16 17:25:57 -0800826 const uint64_t fId1 = getNextFrameId();
827
Brian Anderson3da8d272016-07-28 16:20:47 -0700828 // Verify getFrameTimestamps is piggybacked on dequeue.
829 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
830 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800831 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700832
833 NewFrameEventsEntry f1;
834 f1.frameNumber = 1;
835 f1.postedTime = mFrames[0].kPostedTime;
836 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
837 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
838 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
839 mFrames[0].mAcquireProducer.mFenceTime,
840 mFrames[0].mAcquireConsumer.mFenceTime);
841 mFakeConsumer->mNewFrameEntryOverride = f1;
842 mFrames[0].signalQueueFences();
843
844 // Verify getFrameTimestamps is piggybacked on queue.
845 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
846 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
847 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800848 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700849
850 // Verify queries for timestamps that the producer doesn't know about
851 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800852 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -0700853 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800854 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700855}
856
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800857TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
858 nsecs_t phase = 4000;
859 nsecs_t interval = 1000;
860
861 // Timestamp in previous interval.
862 nsecs_t timestamp = 3500;
863 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
864 timestamp, phase, interval));
865
866 // Timestamp in next interval.
867 timestamp = 4500;
868 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
869 timestamp, phase, interval));
870
871 // Timestamp multiple intervals before.
872 timestamp = 2500;
873 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
874 timestamp, phase, interval));
875
876 // Timestamp multiple intervals after.
877 timestamp = 6500;
878 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
879 timestamp, phase, interval));
880
881 // Timestamp on previous interval.
882 timestamp = 3000;
883 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
884 timestamp, phase, interval));
885
886 // Timestamp on next interval.
887 timestamp = 5000;
888 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
889 timestamp, phase, interval));
890
891 // Timestamp equal to phase.
892 timestamp = 4000;
893 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
894 timestamp, phase, interval));
895}
896
897// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
898// if the number of intervals elapsed is internally stored in an int.
899TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
900 nsecs_t phase = 0;
901 nsecs_t interval = 4000;
902 nsecs_t big_timestamp = 8635916564000;
903 int32_t intervals = big_timestamp / interval;
904
905 EXPECT_LT(intervals, 0);
906 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
907 big_timestamp, phase, interval));
908 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
909 big_timestamp, big_timestamp, interval));
910}
911
912// This verifies the compositor timing is updated by refresh events
913// and piggy backed on a queue, dequeue, and enabling of timestamps..
914TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
915 CompositorTiming initialCompositorTiming {
916 1000000000, // 1s deadline
917 16666667, // 16ms interval
918 50000000, // 50ms present latency
919 };
920 mCfeh->initializeCompositorTiming(initialCompositorTiming);
921
922 enableFrameTimestamps();
923
924 // We get the initial values before any frames are submitted.
925 nsecs_t compositeDeadline = 0;
926 nsecs_t compositeInterval = 0;
927 nsecs_t compositeToPresentLatency = 0;
928 mSurface->setNow(initialCompositorTiming.deadline - 1);
929 int result = native_window_get_compositor_timing(mWindow.get(),
930 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
931 EXPECT_EQ(NO_ERROR, result);
932 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
933 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
934 EXPECT_EQ(initialCompositorTiming.presentLatency,
935 compositeToPresentLatency);
936
937 const uint64_t fId1 = getNextFrameId();
938 dequeueAndQueue(0);
939 addFrameEvents(true, NO_FRAME_INDEX, 0);
940
941 // Still get the initial values because the frame events for frame 0
942 // didn't get a chance to piggyback on a queue or dequeue yet.
943 result = native_window_get_compositor_timing(mWindow.get(),
944 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
945 EXPECT_EQ(NO_ERROR, result);
946 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
947 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
948 EXPECT_EQ(initialCompositorTiming.presentLatency,
949 compositeToPresentLatency);
950
951 const uint64_t fId2 = getNextFrameId();
952 dequeueAndQueue(1);
953 addFrameEvents(true, 0, 1);
954
955 // Now expect the composite values associated with frame 1.
956 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
957 result = native_window_get_compositor_timing(mWindow.get(),
958 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
959 EXPECT_EQ(NO_ERROR, result);
960 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
961 compositeDeadline);
962 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
963 compositeInterval);
964 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
965 compositeToPresentLatency);
966
967 dequeueAndQueue(2);
968 addFrameEvents(true, 1, 2);
969
970 // Now expect the composite values associated with frame 2.
971 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
972 result = native_window_get_compositor_timing(mWindow.get(),
973 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
974 EXPECT_EQ(NO_ERROR, result);
975 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
976 compositeDeadline);
977 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
978 compositeInterval);
979 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
980 compositeToPresentLatency);
981
982 // Re-enabling frame timestamps should get the latest values.
983 disableFrameTimestamps();
984 enableFrameTimestamps();
985
986 // Now expect the composite values associated with frame 3.
987 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
988 result = native_window_get_compositor_timing(mWindow.get(),
989 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
990 EXPECT_EQ(NO_ERROR, result);
991 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
992 compositeDeadline);
993 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
994 compositeInterval);
995 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
996 compositeToPresentLatency);
997}
998
999// This verifies the compositor deadline properly snaps to the the next
1000// deadline based on the current time.
1001TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1002 CompositorTiming initialCompositorTiming {
1003 1000000000, // 1s deadline
1004 16666667, // 16ms interval
1005 50000000, // 50ms present latency
1006 };
1007 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1008
1009 enableFrameTimestamps();
1010
1011 nsecs_t compositeDeadline = 0;
1012 nsecs_t compositeInterval = 0;
1013 nsecs_t compositeToPresentLatency = 0;
1014
1015 // A "now" just before the deadline snaps to the deadline.
1016 mSurface->setNow(initialCompositorTiming.deadline - 1);
1017 int result = native_window_get_compositor_timing(mWindow.get(),
1018 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1019 EXPECT_EQ(NO_ERROR, result);
1020 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1021 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1022 EXPECT_EQ(expectedDeadline, compositeDeadline);
1023
1024 const uint64_t fId1 = getNextFrameId();
1025 dequeueAndQueue(0);
1026 addFrameEvents(true, NO_FRAME_INDEX, 0);
1027
1028 // A "now" just after the deadline snaps properly.
1029 mSurface->setNow(initialCompositorTiming.deadline + 1);
1030 result = native_window_get_compositor_timing(mWindow.get(),
1031 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1032 EXPECT_EQ(NO_ERROR, result);
1033 expectedDeadline =
1034 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1035 EXPECT_EQ(expectedDeadline, compositeDeadline);
1036
1037 const uint64_t fId2 = getNextFrameId();
1038 dequeueAndQueue(1);
1039 addFrameEvents(true, 0, 1);
1040
1041 // A "now" just after the next interval snaps properly.
1042 mSurface->setNow(
1043 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1044 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1045 result = native_window_get_compositor_timing(mWindow.get(),
1046 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1047 EXPECT_EQ(NO_ERROR, result);
1048 expectedDeadline =
1049 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1050 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1051 EXPECT_EQ(expectedDeadline, compositeDeadline);
1052
1053 dequeueAndQueue(2);
1054 addFrameEvents(true, 1, 2);
1055
1056 // A "now" over 1 interval before the deadline snaps properly.
1057 mSurface->setNow(
1058 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1059 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1060 result = native_window_get_compositor_timing(mWindow.get(),
1061 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1062 EXPECT_EQ(NO_ERROR, result);
1063 expectedDeadline =
1064 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1065 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1066 EXPECT_EQ(expectedDeadline, compositeDeadline);
1067
1068 // Re-enabling frame timestamps should get the latest values.
1069 disableFrameTimestamps();
1070 enableFrameTimestamps();
1071
1072 // A "now" over 2 intervals before the deadline snaps properly.
1073 mSurface->setNow(
1074 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1075 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1076 result = native_window_get_compositor_timing(mWindow.get(),
1077 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1078 EXPECT_EQ(NO_ERROR, result);
1079 expectedDeadline =
1080 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1081 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1082 EXPECT_EQ(expectedDeadline, compositeDeadline);
1083}
1084
Brian Anderson1049d1d2016-12-16 17:25:57 -08001085// This verifies the timestamps recorded in the consumer's
1086// FrameTimestampsHistory are properly retrieved by the producer for the
1087// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001088TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1089 enableFrameTimestamps();
1090
Brian Anderson1049d1d2016-12-16 17:25:57 -08001091 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001092 dequeueAndQueue(0);
1093 mFrames[0].signalQueueFences();
1094
Brian Anderson1049d1d2016-12-16 17:25:57 -08001095 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001096 dequeueAndQueue(1);
1097 mFrames[1].signalQueueFences();
1098
1099 addFrameEvents(true, NO_FRAME_INDEX, 0);
1100 mFrames[0].signalRefreshFences();
1101 addFrameEvents(true, 0, 1);
1102 mFrames[0].signalReleaseFences();
1103 mFrames[1].signalRefreshFences();
1104
1105 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001106 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001107 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001108 EXPECT_EQ(NO_ERROR, result);
1109 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1110 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001111 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1112 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1113 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001114 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1115 outGpuCompositionDoneTime);
1116 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001117 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001118 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1119
1120 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001121 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001122 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001123 EXPECT_EQ(NO_ERROR, result);
1124 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1125 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001126 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1127 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1128 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001129 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1130 outGpuCompositionDoneTime);
1131 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001132 EXPECT_EQ(0, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001133 EXPECT_EQ(0, outReleaseTime);
1134}
1135
1136// This test verifies the acquire fence recorded by the consumer is not sent
1137// back to the producer and the producer saves its own fence.
1138TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1139 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001140
Brian Anderson3da8d272016-07-28 16:20:47 -07001141 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001142 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001143 dequeueAndQueue(0);
1144
1145 // Verify queue-related timestamps for f1 are available immediately in the
1146 // producer without asking the consumer again, even before signaling the
1147 // acquire fence.
1148 resetTimestamps();
1149 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001150 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001151 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001152 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001153 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1154 EXPECT_EQ(NO_ERROR, result);
1155 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1156 EXPECT_EQ(0, outAcquireTime);
1157
1158 // Signal acquire fences. Verify a sync call still isn't necessary.
1159 mFrames[0].signalQueueFences();
1160
1161 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001162 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001163 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001164 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001165 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1166 EXPECT_EQ(NO_ERROR, result);
1167 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1168 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1169
1170 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001171 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001172 dequeueAndQueue(1);
1173
1174 // Verify queue-related timestamps for f2 are available immediately in the
1175 // producer without asking the consumer again, even before signaling the
1176 // acquire fence.
1177 resetTimestamps();
1178 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001179 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001180 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001181 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001182 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1183 EXPECT_EQ(NO_ERROR, result);
1184 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1185 EXPECT_EQ(0, outAcquireTime);
1186
1187 // Signal acquire fences. Verify a sync call still isn't necessary.
1188 mFrames[1].signalQueueFences();
1189
1190 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001191 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001192 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001193 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001194 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1195 EXPECT_EQ(NO_ERROR, result);
1196 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1197 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1198}
1199
1200TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1201 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001202
1203 // Dequeue and queue frame 1.
1204 dequeueAndQueue(0);
1205 mFrames[0].signalQueueFences();
1206
1207 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001208 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001209 dequeueAndQueue(1);
1210 mFrames[1].signalQueueFences();
1211
1212 addFrameEvents(true, NO_FRAME_INDEX, 0);
1213 mFrames[0].signalRefreshFences();
1214 addFrameEvents(true, 0, 1);
1215 mFrames[0].signalReleaseFences();
1216 mFrames[1].signalRefreshFences();
1217
1218 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001219 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001220 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson4e606e32017-03-16 15:34:57 -07001221 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001222 nullptr, nullptr, nullptr);
1223 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001224 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1225}
1226
1227// This test verifies that fences can signal and update timestamps producer
1228// side without an additional sync call to the consumer.
1229TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1230 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001231
1232 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001233 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001234 dequeueAndQueue(0);
1235 mFrames[0].signalQueueFences();
1236
1237 // Dequeue and queue frame 2.
1238 dequeueAndQueue(1);
1239 mFrames[1].signalQueueFences();
1240
1241 addFrameEvents(true, NO_FRAME_INDEX, 0);
1242 addFrameEvents(true, 0, 1);
1243
1244 // Verify available timestamps are correct for frame 1, before any
1245 // fence has been signaled.
1246 // Note: A sync call is necessary here since the events triggered by
1247 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001248 resetTimestamps();
1249 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001250 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001251 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1252 EXPECT_EQ(NO_ERROR, result);
1253 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1254 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001255 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1256 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1257 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001258 EXPECT_EQ(0, outGpuCompositionDoneTime);
1259 EXPECT_EQ(0, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001260 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001261 EXPECT_EQ(0, outReleaseTime);
1262
1263 // Verify available timestamps are correct for frame 1 again, before any
1264 // fence has been signaled.
1265 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001266 resetTimestamps();
1267 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001268 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001269 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1270 EXPECT_EQ(NO_ERROR, result);
1271 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1272 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001273 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1274 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1275 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001276 EXPECT_EQ(0, outGpuCompositionDoneTime);
1277 EXPECT_EQ(0, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001278 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001279 EXPECT_EQ(0, outReleaseTime);
1280
1281 // Signal the fences for frame 1.
1282 mFrames[0].signalRefreshFences();
1283 mFrames[0].signalReleaseFences();
1284
1285 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001286 resetTimestamps();
1287 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001288 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001289 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1290 EXPECT_EQ(NO_ERROR, result);
1291 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1292 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001293 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1294 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1295 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001296 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1297 outGpuCompositionDoneTime);
1298 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001299 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001300 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1301}
1302
1303// This test verifies that if the frame wasn't GPU composited but has a refresh
1304// event a sync call isn't made to get the GPU composite done time since it will
1305// never exist.
1306TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1307 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001308
Brian Anderson3da8d272016-07-28 16:20:47 -07001309 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001310 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001311 dequeueAndQueue(0);
1312 mFrames[0].signalQueueFences();
1313
1314 // Dequeue and queue frame 2.
1315 dequeueAndQueue(1);
1316 mFrames[1].signalQueueFences();
1317
1318 addFrameEvents(false, NO_FRAME_INDEX, 0);
1319 addFrameEvents(false, 0, 1);
1320
1321 // Verify available timestamps are correct for frame 1, before any
1322 // fence has been signaled.
1323 // Note: A sync call is necessary here since the events triggered by
1324 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1325 resetTimestamps();
1326 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001327 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001328 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1329 EXPECT_EQ(NO_ERROR, result);
1330 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1331 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001332 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1333 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1334 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001335 EXPECT_EQ(0, outGpuCompositionDoneTime);
1336 EXPECT_EQ(0, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001337 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001338 EXPECT_EQ(0, outReleaseTime);
1339
1340 // Signal the fences for frame 1.
1341 mFrames[0].signalRefreshFences();
1342 mFrames[0].signalReleaseFences();
1343
1344 // Verify all timestamps, except GPU composition, are available without a
1345 // sync call.
1346 resetTimestamps();
1347 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001348 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001349 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1350 EXPECT_EQ(NO_ERROR, result);
1351 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1352 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001353 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1354 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1355 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001356 EXPECT_EQ(0, outGpuCompositionDoneTime);
1357 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001358 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001359 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1360}
1361
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001362// This test verifies that if the certain timestamps can't possibly exist for
1363// the most recent frame, then a sync call is not done.
Brian Anderson3da8d272016-07-28 16:20:47 -07001364TEST_F(GetFrameTimestampsTest, NoRetireOrReleaseNoSync) {
1365 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001366
1367 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001368 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001369 dequeueAndQueue(0);
1370 mFrames[0].signalQueueFences();
1371
1372 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001373 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001374 dequeueAndQueue(1);
1375 mFrames[1].signalQueueFences();
1376
1377 addFrameEvents(false, NO_FRAME_INDEX, 0);
1378 addFrameEvents(false, 0, 1);
1379
1380 // Verify available timestamps are correct for frame 1, before any
1381 // fence has been signaled.
1382 // Note: A sync call is necessary here since the events triggered by
1383 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001384 resetTimestamps();
1385 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001386 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001387 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1388 EXPECT_EQ(NO_ERROR, result);
1389 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1390 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001391 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1392 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1393 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001394 EXPECT_EQ(0, outGpuCompositionDoneTime);
1395 EXPECT_EQ(0, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001396 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001397 EXPECT_EQ(0, outReleaseTime);
1398
1399 mFrames[0].signalRefreshFences();
1400 mFrames[0].signalReleaseFences();
1401 mFrames[1].signalRefreshFences();
1402
Brian Anderson1049d1d2016-12-16 17:25:57 -08001403 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001404 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001405 // available, a sync call should not occur because it's not possible for f2
1406 // to encounter the final value for those events until another frame is
1407 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001408 resetTimestamps();
1409 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001410 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001411 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1412 EXPECT_EQ(NO_ERROR, result);
1413 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1414 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001415 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1416 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1417 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001418 EXPECT_EQ(0, outGpuCompositionDoneTime);
1419 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001420 EXPECT_EQ(0, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001421 EXPECT_EQ(0, outReleaseTime);
1422}
1423
Jamie Gennis134f0422011-03-08 12:18:54 -08001424}