blob: 58227e6a314871bca7388147d9820f06a0315244 [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
361 void setSupportedTimestamps(bool supportsPresent, bool supportsRetire) {
362 mSupportsPresent = supportsPresent;
363 mSupportsRetire = supportsRetire;
364 }
365
366 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800367 sp<ISurfaceComposerClient> createScopedConnection(
368 const sp<IGraphicBufferProducer>& /* parent */) override {
369 return nullptr;
370 }
Chia-I Wu527747d2017-03-13 20:38:48 +0000371 sp<IGraphicBufferAlloc> createGraphicBufferAlloc() override {
372 return nullptr;
373 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700374 sp<IDisplayEventConnection> createDisplayEventConnection() override {
375 return nullptr;
376 }
377 sp<IBinder> createDisplay(const String8& /*displayName*/,
378 bool /*secure*/) override { return nullptr; }
379 void destroyDisplay(const sp<IBinder>& /*display */) override {}
380 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
381 void setTransactionState(const Vector<ComposerState>& /*state*/,
382 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
383 override {}
384 void bootFinished() override {}
385 bool authenticateSurfaceTexture(
386 const sp<IGraphicBufferProducer>& /*surface*/) const override {
387 return false;
388 }
389
390 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
391 const override {
392 *outSupported = {
393 FrameEvent::REQUESTED_PRESENT,
394 FrameEvent::ACQUIRE,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700395 FrameEvent::LATCH,
Brian Anderson3da8d272016-07-28 16:20:47 -0700396 FrameEvent::FIRST_REFRESH_START,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700397 FrameEvent::LAST_REFRESH_START,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700398 FrameEvent::GPU_COMPOSITION_DONE,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700399 FrameEvent::DEQUEUE_READY,
Brian Anderson3da8d272016-07-28 16:20:47 -0700400 FrameEvent::RELEASE
401 };
402 if (mSupportsPresent) {
403 outSupported->push_back(
404 FrameEvent::DISPLAY_PRESENT);
405 }
406 if (mSupportsRetire) {
407 outSupported->push_back(
408 FrameEvent::DISPLAY_RETIRE);
409 }
410 return NO_ERROR;
411 }
412
413 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
414 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
415 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
416 status_t getDisplayStats(const sp<IBinder>& /*display*/,
417 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
418 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
419 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
420 override {
421 return NO_ERROR;
422 }
423 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
424 Vector<android_color_mode_t>* /*outColorModes*/) override {
425 return NO_ERROR;
426 }
427 android_color_mode_t getActiveColorMode(const sp<IBinder>& /*display*/)
428 override {
429 return HAL_COLOR_MODE_NATIVE;
430 }
431 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
432 android_color_mode_t /*colorMode*/) override { return NO_ERROR; }
433 status_t captureScreen(const sp<IBinder>& /*display*/,
434 const sp<IGraphicBufferProducer>& /*producer*/,
435 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800436 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700437 bool /*useIdentityTransform*/,
438 Rotation /*rotation*/) override { return NO_ERROR; }
439 status_t clearAnimationFrameStats() override { return NO_ERROR; }
440 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
441 return NO_ERROR;
442 }
443 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
444 HdrCapabilities* /*outCapabilities*/) const override {
445 return NO_ERROR;
446 }
447 status_t enableVSyncInjections(bool /*enable*/) override {
448 return NO_ERROR;
449 }
450 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
451
452protected:
453 IBinder* onAsBinder() override { return nullptr; }
454
455private:
456 bool mSupportsPresent{true};
457 bool mSupportsRetire{true};
458};
459
460class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
461public:
462 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
463 : mFenceMap(fenceMap) {}
464
465 ~FakeProducerFrameEventHistory() {}
466
467 void updateAcquireFence(uint64_t frameNumber,
468 std::shared_ptr<FenceTime>&& acquire) override {
469 // Verify the acquire fence being added isn't the one from the consumer.
470 EXPECT_NE(mConsumerAcquireFence, acquire);
471 // Override the fence, so we can verify this was called by the
472 // producer after the frame is queued.
473 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
474 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
475 }
476
477 void setAcquireFenceOverride(
478 const std::shared_ptr<FenceTime>& acquireFenceOverride,
479 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
480 mAcquireFenceOverride = acquireFenceOverride;
481 mConsumerAcquireFence = consumerAcquireFence;
482 }
483
484protected:
485 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
486 const override {
487 return mFenceMap->createFenceTimeForTest(fence);
488 }
489
490 FenceToFenceTimeMap* mFenceMap{nullptr};
491
492 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
493 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
494};
495
496
497class TestSurface : public Surface {
498public:
499 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
500 FenceToFenceTimeMap* fenceMap)
501 : Surface(bufferProducer),
502 mFakeSurfaceComposer(new FakeSurfaceComposer) {
503 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
504 mFrameEventHistory.reset(mFakeFrameEventHistory);
505 }
506
507 ~TestSurface() override {}
508
509 sp<ISurfaceComposer> composerService() const override {
510 return mFakeSurfaceComposer;
511 }
512
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800513 nsecs_t now() const override {
514 return mNow;
515 }
516
517 void setNow(nsecs_t now) {
518 mNow = now;
519 }
520
Brian Anderson3da8d272016-07-28 16:20:47 -0700521public:
522 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800523 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700524
525 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
526 // but this raw pointer gives access to test functionality.
527 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
528};
529
530
Brian Andersond0010582017-03-07 13:20:31 -0800531class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700532protected:
533 struct FenceAndFenceTime {
534 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
535 : mFence(new Fence),
536 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
537 sp<Fence> mFence { nullptr };
538 std::shared_ptr<FenceTime> mFenceTime { nullptr };
539 };
540
541 struct RefreshEvents {
542 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800543 : mFenceMap(fenceMap),
544 kCompositorTiming(
545 {refreshStart, refreshStart + 1, refreshStart + 2 }),
546 kStartTime(refreshStart + 3),
547 kGpuCompositionDoneTime(refreshStart + 4),
548 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700549
550 void signalPostCompositeFences() {
551 mFenceMap.signalAllForTest(
552 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
553 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
554 }
555
556 FenceToFenceTimeMap& mFenceMap;
557
558 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
559 FenceAndFenceTime mPresent { mFenceMap };
560
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800561 const CompositorTiming kCompositorTiming;
562
Brian Anderson3da8d272016-07-28 16:20:47 -0700563 const nsecs_t kStartTime;
564 const nsecs_t kGpuCompositionDoneTime;
565 const nsecs_t kPresentTime;
566 };
567
568 struct FrameEvents {
569 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
570 : mFenceMap(fenceMap),
571 kPostedTime(frameStartTime + 100),
572 kRequestedPresentTime(frameStartTime + 200),
573 kProducerAcquireTime(frameStartTime + 300),
574 kConsumerAcquireTime(frameStartTime + 301),
575 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700576 kDequeueReadyTime(frameStartTime + 600),
577 kRetireTime(frameStartTime + 700),
578 kReleaseTime(frameStartTime + 800),
Brian Anderson3da8d272016-07-28 16:20:47 -0700579 mRefreshes {
580 { mFenceMap, frameStartTime + 410 },
581 { mFenceMap, frameStartTime + 420 },
582 { mFenceMap, frameStartTime + 430 } } {}
583
584 void signalQueueFences() {
585 mFenceMap.signalAllForTest(
586 mAcquireConsumer.mFence, kConsumerAcquireTime);
587 mFenceMap.signalAllForTest(
588 mAcquireProducer.mFence, kProducerAcquireTime);
589 }
590
591 void signalRefreshFences() {
592 for (auto& re : mRefreshes) {
593 re.signalPostCompositeFences();
594 }
595 }
596
597 void signalReleaseFences() {
598 mFenceMap.signalAllForTest(mRetire.mFence, kRetireTime);
599 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
600 }
601
602 FenceToFenceTimeMap& mFenceMap;
603
604 FenceAndFenceTime mAcquireConsumer { mFenceMap };
605 FenceAndFenceTime mAcquireProducer { mFenceMap };
606 FenceAndFenceTime mRetire { mFenceMap };
607 FenceAndFenceTime mRelease { mFenceMap };
608
609 const nsecs_t kPostedTime;
610 const nsecs_t kRequestedPresentTime;
611 const nsecs_t kProducerAcquireTime;
612 const nsecs_t kConsumerAcquireTime;
613 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700614 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700615 const nsecs_t kRetireTime;
616 const nsecs_t kReleaseTime;
617
618 RefreshEvents mRefreshes[3];
619 };
620
Brian Andersond0010582017-03-07 13:20:31 -0800621 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700622
623 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -0700624 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
625 mFakeConsumer = new FakeConsumer;
626 mCfeh = &mFakeConsumer->mFrameEventHistory;
627 mConsumer->consumerConnect(mFakeConsumer, false);
628 mConsumer->setConsumerName(String8("TestConsumer"));
629 mSurface = new TestSurface(mProducer, &mFenceMap);
630 mWindow = mSurface;
631
632 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
633 NATIVE_WINDOW_API_CPU));
634 native_window_set_buffer_count(mWindow.get(), 4);
635 }
636
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800637 void disableFrameTimestamps() {
638 mFakeConsumer->mGetFrameTimestampsEnabled = false;
639 native_window_enable_frame_timestamps(mWindow.get(), 0);
640 mFrameTimestampsEnabled = false;
641 }
642
Brian Anderson3da8d272016-07-28 16:20:47 -0700643 void enableFrameTimestamps() {
644 mFakeConsumer->mGetFrameTimestampsEnabled = true;
645 native_window_enable_frame_timestamps(mWindow.get(), 1);
646 mFrameTimestampsEnabled = true;
647 }
648
Brian Anderson1049d1d2016-12-16 17:25:57 -0800649 int getAllFrameTimestamps(uint64_t frameId) {
650 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700651 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
652 &outFirstRefreshStartTime, &outLastRefreshStartTime,
653 &outGpuCompositionDoneTime, &outDisplayPresentTime,
654 &outDisplayRetireTime, &outDequeueReadyTime, &outReleaseTime);
655 }
656
Brian Anderson3da8d272016-07-28 16:20:47 -0700657 void resetTimestamps() {
658 outRequestedPresentTime = -1;
659 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700660 outLatchTime = -1;
661 outFirstRefreshStartTime = -1;
662 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700663 outGpuCompositionDoneTime = -1;
664 outDisplayPresentTime = -1;
665 outDisplayRetireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700666 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700667 outReleaseTime = -1;
668 }
669
Brian Anderson1049d1d2016-12-16 17:25:57 -0800670 uint64_t getNextFrameId() {
671 uint64_t frameId = -1;
672 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
673 EXPECT_EQ(status, NO_ERROR);
674 return frameId;
675 }
676
Brian Anderson3da8d272016-07-28 16:20:47 -0700677 void dequeueAndQueue(uint64_t frameIndex) {
678 int fence = -1;
679 ANativeWindowBuffer* buffer = nullptr;
680 ASSERT_EQ(NO_ERROR,
681 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
682
683 int oldAddFrameTimestampsCount =
684 mFakeConsumer->mAddFrameTimestampsCount;
685
686 FrameEvents* frame = &mFrames[frameIndex];
687 uint64_t frameNumber = frameIndex + 1;
688
689 NewFrameEventsEntry fe;
690 fe.frameNumber = frameNumber;
691 fe.postedTime = frame->kPostedTime;
692 fe.requestedPresentTime = frame->kRequestedPresentTime;
693 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
694 mFakeConsumer->mNewFrameEntryOverride = fe;
695
696 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
697 frame->mAcquireProducer.mFenceTime,
698 frame->mAcquireConsumer.mFenceTime);
699
700 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
701
702 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
703
704 EXPECT_EQ(
705 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
706 mFakeConsumer->mAddFrameTimestampsCount);
707 }
708
709 void addFrameEvents(
710 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
711 FrameEvents* oldFrame =
712 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
713 FrameEvents* newFrame = &mFrames[iNewFrame];
714
715 uint64_t nOldFrame = iOldFrame + 1;
716 uint64_t nNewFrame = iNewFrame + 1;
717
718 // Latch, Composite, Retire, and Release the frames in a plausible
719 // order. Note: The timestamps won't necessarily match the order, but
720 // that's okay for the purposes of this test.
721 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
722
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700723 // Composite the previous frame one more time, which helps verify
724 // LastRefresh is updated properly.
725 if (oldFrame != nullptr) {
726 mCfeh->addPreComposition(nOldFrame,
727 oldFrame->mRefreshes[2].kStartTime);
728 gpuDoneFenceTime = gpuComposited ?
729 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
730 FenceTime::NO_FENCE;
731 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800732 oldFrame->mRefreshes[2].mPresent.mFenceTime,
733 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700734 }
735
736 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700737 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
738
739 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
740 gpuDoneFenceTime = gpuComposited ?
741 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
742 FenceTime::NO_FENCE;
743 // HWC2 releases the previous buffer after a new latch just before
744 // calling postComposition.
745 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700746 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700747 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
748 }
749 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800750 newFrame->mRefreshes[0].mPresent.mFenceTime,
751 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700752
753 // Retire the previous buffer just after compositing the new buffer.
754 if (oldFrame != nullptr) {
755 mCfeh->addRetire(nOldFrame, oldFrame->mRetire.mFenceTime);
756 }
757
758 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
759 gpuDoneFenceTime = gpuComposited ?
760 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
761 FenceTime::NO_FENCE;
762 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800763 newFrame->mRefreshes[1].mPresent.mFenceTime,
764 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700765 }
766
767 void QueryPresentRetireSupported(
768 bool displayPresentSupported, bool displayRetireSupported);
769 void PresentOrRetireUnsupportedNoSyncTest(
770 bool displayPresentSupported, bool displayRetireSupported);
771
772 sp<IGraphicBufferProducer> mProducer;
773 sp<IGraphicBufferConsumer> mConsumer;
774 sp<FakeConsumer> mFakeConsumer;
775 ConsumerFrameEventHistory* mCfeh;
776 sp<TestSurface> mSurface;
777 sp<ANativeWindow> mWindow;
778
779 FenceToFenceTimeMap mFenceMap;
780
781 bool mFrameTimestampsEnabled = false;
782
783 int64_t outRequestedPresentTime = -1;
784 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700785 int64_t outLatchTime = -1;
786 int64_t outFirstRefreshStartTime = -1;
787 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700788 int64_t outGpuCompositionDoneTime = -1;
789 int64_t outDisplayPresentTime = -1;
790 int64_t outDisplayRetireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700791 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700792 int64_t outReleaseTime = -1;
793
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800794 FrameEvents mFrames[3] {
795 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700796};
797
798
799// This test verifies that the frame timestamps are not retrieved when not
800// explicitly enabled via native_window_enable_frame_timestamps.
801// We want to check this to make sure there's no overhead for users
802// that don't need the timestamp information.
803TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
804 int fence;
805 ANativeWindowBuffer* buffer;
806
807 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
808 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
809
Brian Anderson1049d1d2016-12-16 17:25:57 -0800810 const uint64_t fId = getNextFrameId();
811
Brian Anderson3da8d272016-07-28 16:20:47 -0700812 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
813 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
814 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
815 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
816
817 // Verify the producer doesn't get frame timestamps piggybacked on queue.
818 // It is okay that frame timestamps are added in the consumer since it is
819 // still needed for SurfaceFlinger dumps.
820 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
821 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
822 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
823
824 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800825 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700826 EXPECT_EQ(INVALID_OPERATION, result);
827 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800828
829 // Verify compositor timing query fails.
830 nsecs_t compositeDeadline = 0;
831 nsecs_t compositeInterval = 0;
832 nsecs_t compositeToPresentLatency = 0;
833 result = native_window_get_compositor_timing(mWindow.get(),
834 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
835 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700836}
837
838// This test verifies that the frame timestamps are retrieved if explicitly
839// enabled via native_window_enable_frame_timestamps.
840TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800841 CompositorTiming initialCompositorTiming {
842 1000000000, // 1s deadline
843 16666667, // 16ms interval
844 50000000, // 50ms present latency
845 };
846 mCfeh->initializeCompositorTiming(initialCompositorTiming);
847
Brian Anderson3da8d272016-07-28 16:20:47 -0700848 enableFrameTimestamps();
849
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800850 // Verify the compositor timing query gets the initial compositor values
851 // after timststamps are enabled; even before the first frame is queued
852 // or dequeued.
853 nsecs_t compositeDeadline = 0;
854 nsecs_t compositeInterval = 0;
855 nsecs_t compositeToPresentLatency = 0;
856 mSurface->setNow(initialCompositorTiming.deadline - 1);
857 int result = native_window_get_compositor_timing(mWindow.get(),
858 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
859 EXPECT_EQ(NO_ERROR, result);
860 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
861 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
862 EXPECT_EQ(initialCompositorTiming.presentLatency,
863 compositeToPresentLatency);
864
Brian Anderson3da8d272016-07-28 16:20:47 -0700865 int fence;
866 ANativeWindowBuffer* buffer;
867
868 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800869 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700870
Brian Anderson1049d1d2016-12-16 17:25:57 -0800871 const uint64_t fId1 = getNextFrameId();
872
Brian Anderson3da8d272016-07-28 16:20:47 -0700873 // Verify getFrameTimestamps is piggybacked on dequeue.
874 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
875 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800876 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700877
878 NewFrameEventsEntry f1;
879 f1.frameNumber = 1;
880 f1.postedTime = mFrames[0].kPostedTime;
881 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
882 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
883 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
884 mFrames[0].mAcquireProducer.mFenceTime,
885 mFrames[0].mAcquireConsumer.mFenceTime);
886 mFakeConsumer->mNewFrameEntryOverride = f1;
887 mFrames[0].signalQueueFences();
888
889 // Verify getFrameTimestamps is piggybacked on queue.
890 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
891 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
892 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800893 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700894
895 // Verify queries for timestamps that the producer doesn't know about
896 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800897 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -0700898 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800899 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700900}
901
902void GetFrameTimestampsTest::QueryPresentRetireSupported(
903 bool displayPresentSupported, bool displayRetireSupported) {
904 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(
905 displayPresentSupported, displayRetireSupported);
906
907 // Verify supported bits are forwarded.
908 int supportsPresent = -1;
909 mWindow.get()->query(mWindow.get(),
910 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
911 EXPECT_EQ(displayPresentSupported, supportsPresent);
912
913 int supportsRetire = -1;
914 mWindow.get()->query(mWindow.get(),
915 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &supportsRetire);
916 EXPECT_EQ(displayRetireSupported, supportsRetire);
917}
918
919TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
920 QueryPresentRetireSupported(true, false);
921}
922
923TEST_F(GetFrameTimestampsTest, QueryRetireSupported) {
924 QueryPresentRetireSupported(false, true);
925}
926
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800927TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
928 nsecs_t phase = 4000;
929 nsecs_t interval = 1000;
930
931 // Timestamp in previous interval.
932 nsecs_t timestamp = 3500;
933 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
934 timestamp, phase, interval));
935
936 // Timestamp in next interval.
937 timestamp = 4500;
938 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
939 timestamp, phase, interval));
940
941 // Timestamp multiple intervals before.
942 timestamp = 2500;
943 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
944 timestamp, phase, interval));
945
946 // Timestamp multiple intervals after.
947 timestamp = 6500;
948 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
949 timestamp, phase, interval));
950
951 // Timestamp on previous interval.
952 timestamp = 3000;
953 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
954 timestamp, phase, interval));
955
956 // Timestamp on next interval.
957 timestamp = 5000;
958 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
959 timestamp, phase, interval));
960
961 // Timestamp equal to phase.
962 timestamp = 4000;
963 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
964 timestamp, phase, interval));
965}
966
967// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
968// if the number of intervals elapsed is internally stored in an int.
969TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
970 nsecs_t phase = 0;
971 nsecs_t interval = 4000;
972 nsecs_t big_timestamp = 8635916564000;
973 int32_t intervals = big_timestamp / interval;
974
975 EXPECT_LT(intervals, 0);
976 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
977 big_timestamp, phase, interval));
978 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
979 big_timestamp, big_timestamp, interval));
980}
981
982// This verifies the compositor timing is updated by refresh events
983// and piggy backed on a queue, dequeue, and enabling of timestamps..
984TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
985 CompositorTiming initialCompositorTiming {
986 1000000000, // 1s deadline
987 16666667, // 16ms interval
988 50000000, // 50ms present latency
989 };
990 mCfeh->initializeCompositorTiming(initialCompositorTiming);
991
992 enableFrameTimestamps();
993
994 // We get the initial values before any frames are submitted.
995 nsecs_t compositeDeadline = 0;
996 nsecs_t compositeInterval = 0;
997 nsecs_t compositeToPresentLatency = 0;
998 mSurface->setNow(initialCompositorTiming.deadline - 1);
999 int result = native_window_get_compositor_timing(mWindow.get(),
1000 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1001 EXPECT_EQ(NO_ERROR, result);
1002 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1003 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1004 EXPECT_EQ(initialCompositorTiming.presentLatency,
1005 compositeToPresentLatency);
1006
1007 const uint64_t fId1 = getNextFrameId();
1008 dequeueAndQueue(0);
1009 addFrameEvents(true, NO_FRAME_INDEX, 0);
1010
1011 // Still get the initial values because the frame events for frame 0
1012 // didn't get a chance to piggyback on a queue or dequeue yet.
1013 result = native_window_get_compositor_timing(mWindow.get(),
1014 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1015 EXPECT_EQ(NO_ERROR, result);
1016 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1017 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1018 EXPECT_EQ(initialCompositorTiming.presentLatency,
1019 compositeToPresentLatency);
1020
1021 const uint64_t fId2 = getNextFrameId();
1022 dequeueAndQueue(1);
1023 addFrameEvents(true, 0, 1);
1024
1025 // Now expect the composite values associated with frame 1.
1026 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1027 result = native_window_get_compositor_timing(mWindow.get(),
1028 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1029 EXPECT_EQ(NO_ERROR, result);
1030 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1031 compositeDeadline);
1032 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1033 compositeInterval);
1034 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1035 compositeToPresentLatency);
1036
1037 dequeueAndQueue(2);
1038 addFrameEvents(true, 1, 2);
1039
1040 // Now expect the composite values associated with frame 2.
1041 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1042 result = native_window_get_compositor_timing(mWindow.get(),
1043 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1044 EXPECT_EQ(NO_ERROR, result);
1045 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1046 compositeDeadline);
1047 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1048 compositeInterval);
1049 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1050 compositeToPresentLatency);
1051
1052 // Re-enabling frame timestamps should get the latest values.
1053 disableFrameTimestamps();
1054 enableFrameTimestamps();
1055
1056 // Now expect the composite values associated with frame 3.
1057 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1058 result = native_window_get_compositor_timing(mWindow.get(),
1059 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1060 EXPECT_EQ(NO_ERROR, result);
1061 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1062 compositeDeadline);
1063 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1064 compositeInterval);
1065 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1066 compositeToPresentLatency);
1067}
1068
1069// This verifies the compositor deadline properly snaps to the the next
1070// deadline based on the current time.
1071TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1072 CompositorTiming initialCompositorTiming {
1073 1000000000, // 1s deadline
1074 16666667, // 16ms interval
1075 50000000, // 50ms present latency
1076 };
1077 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1078
1079 enableFrameTimestamps();
1080
1081 nsecs_t compositeDeadline = 0;
1082 nsecs_t compositeInterval = 0;
1083 nsecs_t compositeToPresentLatency = 0;
1084
1085 // A "now" just before the deadline snaps to the deadline.
1086 mSurface->setNow(initialCompositorTiming.deadline - 1);
1087 int result = native_window_get_compositor_timing(mWindow.get(),
1088 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1089 EXPECT_EQ(NO_ERROR, result);
1090 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1091 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1092 EXPECT_EQ(expectedDeadline, compositeDeadline);
1093
1094 const uint64_t fId1 = getNextFrameId();
1095 dequeueAndQueue(0);
1096 addFrameEvents(true, NO_FRAME_INDEX, 0);
1097
1098 // A "now" just after the deadline snaps properly.
1099 mSurface->setNow(initialCompositorTiming.deadline + 1);
1100 result = native_window_get_compositor_timing(mWindow.get(),
1101 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1102 EXPECT_EQ(NO_ERROR, result);
1103 expectedDeadline =
1104 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1105 EXPECT_EQ(expectedDeadline, compositeDeadline);
1106
1107 const uint64_t fId2 = getNextFrameId();
1108 dequeueAndQueue(1);
1109 addFrameEvents(true, 0, 1);
1110
1111 // A "now" just after the next interval snaps properly.
1112 mSurface->setNow(
1113 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1114 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1115 result = native_window_get_compositor_timing(mWindow.get(),
1116 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1117 EXPECT_EQ(NO_ERROR, result);
1118 expectedDeadline =
1119 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1120 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1121 EXPECT_EQ(expectedDeadline, compositeDeadline);
1122
1123 dequeueAndQueue(2);
1124 addFrameEvents(true, 1, 2);
1125
1126 // A "now" over 1 interval before the deadline snaps properly.
1127 mSurface->setNow(
1128 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1129 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1130 result = native_window_get_compositor_timing(mWindow.get(),
1131 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1132 EXPECT_EQ(NO_ERROR, result);
1133 expectedDeadline =
1134 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1135 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1136 EXPECT_EQ(expectedDeadline, compositeDeadline);
1137
1138 // Re-enabling frame timestamps should get the latest values.
1139 disableFrameTimestamps();
1140 enableFrameTimestamps();
1141
1142 // A "now" over 2 intervals before the deadline snaps properly.
1143 mSurface->setNow(
1144 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1145 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1146 result = native_window_get_compositor_timing(mWindow.get(),
1147 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1148 EXPECT_EQ(NO_ERROR, result);
1149 expectedDeadline =
1150 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1151 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1152 EXPECT_EQ(expectedDeadline, compositeDeadline);
1153}
1154
Brian Anderson1049d1d2016-12-16 17:25:57 -08001155// This verifies the timestamps recorded in the consumer's
1156// FrameTimestampsHistory are properly retrieved by the producer for the
1157// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001158TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1159 enableFrameTimestamps();
1160
Brian Anderson1049d1d2016-12-16 17:25:57 -08001161 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001162 dequeueAndQueue(0);
1163 mFrames[0].signalQueueFences();
1164
Brian Anderson1049d1d2016-12-16 17:25:57 -08001165 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001166 dequeueAndQueue(1);
1167 mFrames[1].signalQueueFences();
1168
1169 addFrameEvents(true, NO_FRAME_INDEX, 0);
1170 mFrames[0].signalRefreshFences();
1171 addFrameEvents(true, 0, 1);
1172 mFrames[0].signalReleaseFences();
1173 mFrames[1].signalRefreshFences();
1174
1175 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001176 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001177 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001178 EXPECT_EQ(NO_ERROR, result);
1179 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1180 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001181 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1182 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1183 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001184 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1185 outGpuCompositionDoneTime);
1186 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1187 EXPECT_EQ(mFrames[0].kRetireTime, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001188 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001189 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1190
1191 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001192 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001193 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001194 EXPECT_EQ(NO_ERROR, result);
1195 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1196 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001197 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1198 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1199 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001200 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1201 outGpuCompositionDoneTime);
1202 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1203 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001204 EXPECT_EQ(0, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001205 EXPECT_EQ(0, outReleaseTime);
1206}
1207
1208// This test verifies the acquire fence recorded by the consumer is not sent
1209// back to the producer and the producer saves its own fence.
1210TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1211 enableFrameTimestamps();
1212 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1213
Brian Anderson3da8d272016-07-28 16:20:47 -07001214 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001215 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001216 dequeueAndQueue(0);
1217
1218 // Verify queue-related timestamps for f1 are available immediately in the
1219 // producer without asking the consumer again, even before signaling the
1220 // acquire fence.
1221 resetTimestamps();
1222 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001223 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001224 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001225 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001226 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1227 EXPECT_EQ(NO_ERROR, result);
1228 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1229 EXPECT_EQ(0, outAcquireTime);
1230
1231 // Signal acquire fences. Verify a sync call still isn't necessary.
1232 mFrames[0].signalQueueFences();
1233
1234 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001235 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001236 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001237 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001238 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1239 EXPECT_EQ(NO_ERROR, result);
1240 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1241 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1242
1243 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001244 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001245 dequeueAndQueue(1);
1246
1247 // Verify queue-related timestamps for f2 are available immediately in the
1248 // producer without asking the consumer again, even before signaling the
1249 // acquire fence.
1250 resetTimestamps();
1251 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001252 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001253 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001254 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001255 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1256 EXPECT_EQ(NO_ERROR, result);
1257 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1258 EXPECT_EQ(0, outAcquireTime);
1259
1260 // Signal acquire fences. Verify a sync call still isn't necessary.
1261 mFrames[1].signalQueueFences();
1262
1263 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001264 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001265 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001266 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001267 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1268 EXPECT_EQ(NO_ERROR, result);
1269 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1270 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1271}
1272
1273TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1274 enableFrameTimestamps();
1275 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1276
1277 // Dequeue and queue frame 1.
1278 dequeueAndQueue(0);
1279 mFrames[0].signalQueueFences();
1280
1281 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001282 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001283 dequeueAndQueue(1);
1284 mFrames[1].signalQueueFences();
1285
1286 addFrameEvents(true, NO_FRAME_INDEX, 0);
1287 mFrames[0].signalRefreshFences();
1288 addFrameEvents(true, 0, 1);
1289 mFrames[0].signalReleaseFences();
1290 mFrames[1].signalRefreshFences();
1291
1292 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001293 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001294 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001295 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1296 nullptr, nullptr, nullptr);
1297 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001298 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1299}
1300
1301// This test verifies that fences can signal and update timestamps producer
1302// side without an additional sync call to the consumer.
1303TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1304 enableFrameTimestamps();
1305 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1306
1307 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001308 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001309 dequeueAndQueue(0);
1310 mFrames[0].signalQueueFences();
1311
1312 // Dequeue and queue frame 2.
1313 dequeueAndQueue(1);
1314 mFrames[1].signalQueueFences();
1315
1316 addFrameEvents(true, NO_FRAME_INDEX, 0);
1317 addFrameEvents(true, 0, 1);
1318
1319 // Verify available timestamps are correct for frame 1, before any
1320 // fence has been signaled.
1321 // Note: A sync call is necessary here since the events triggered by
1322 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001323 resetTimestamps();
1324 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001325 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001326 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1327 EXPECT_EQ(NO_ERROR, result);
1328 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1329 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001330 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1331 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1332 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001333 EXPECT_EQ(0, outGpuCompositionDoneTime);
1334 EXPECT_EQ(0, outDisplayPresentTime);
1335 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001336 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001337 EXPECT_EQ(0, outReleaseTime);
1338
1339 // Verify available timestamps are correct for frame 1 again, before any
1340 // fence has been signaled.
1341 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001342 resetTimestamps();
1343 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001344 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001345 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1346 EXPECT_EQ(NO_ERROR, result);
1347 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1348 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001349 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1350 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1351 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001352 EXPECT_EQ(0, outGpuCompositionDoneTime);
1353 EXPECT_EQ(0, outDisplayPresentTime);
1354 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001355 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001356 EXPECT_EQ(0, outReleaseTime);
1357
1358 // Signal the fences for frame 1.
1359 mFrames[0].signalRefreshFences();
1360 mFrames[0].signalReleaseFences();
1361
1362 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001363 resetTimestamps();
1364 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001365 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001366 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1367 EXPECT_EQ(NO_ERROR, result);
1368 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1369 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001370 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1371 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1372 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001373 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1374 outGpuCompositionDoneTime);
1375 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1376 EXPECT_EQ(mFrames[0].kRetireTime, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001377 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001378 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1379}
1380
1381// This test verifies that if the frame wasn't GPU composited but has a refresh
1382// event a sync call isn't made to get the GPU composite done time since it will
1383// never exist.
1384TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1385 enableFrameTimestamps();
1386 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1387
Brian Anderson3da8d272016-07-28 16:20:47 -07001388 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001389 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001390 dequeueAndQueue(0);
1391 mFrames[0].signalQueueFences();
1392
1393 // Dequeue and queue frame 2.
1394 dequeueAndQueue(1);
1395 mFrames[1].signalQueueFences();
1396
1397 addFrameEvents(false, NO_FRAME_INDEX, 0);
1398 addFrameEvents(false, 0, 1);
1399
1400 // Verify available timestamps are correct for frame 1, before any
1401 // fence has been signaled.
1402 // Note: A sync call is necessary here since the events triggered by
1403 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1404 resetTimestamps();
1405 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001406 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001407 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1408 EXPECT_EQ(NO_ERROR, result);
1409 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1410 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001411 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1412 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1413 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001414 EXPECT_EQ(0, outGpuCompositionDoneTime);
1415 EXPECT_EQ(0, outDisplayPresentTime);
1416 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001417 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001418 EXPECT_EQ(0, outReleaseTime);
1419
1420 // Signal the fences for frame 1.
1421 mFrames[0].signalRefreshFences();
1422 mFrames[0].signalReleaseFences();
1423
1424 // Verify all timestamps, except GPU composition, are available without a
1425 // sync call.
1426 resetTimestamps();
1427 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001428 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001429 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1430 EXPECT_EQ(NO_ERROR, result);
1431 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1432 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001433 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1434 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1435 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001436 EXPECT_EQ(0, outGpuCompositionDoneTime);
1437 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1438 EXPECT_EQ(mFrames[0].kRetireTime, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001439 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001440 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1441}
1442
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001443// This test verifies that if the certain timestamps can't possibly exist for
1444// the most recent frame, then a sync call is not done.
Brian Anderson3da8d272016-07-28 16:20:47 -07001445TEST_F(GetFrameTimestampsTest, NoRetireOrReleaseNoSync) {
1446 enableFrameTimestamps();
1447 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1448
1449 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001450 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001451 dequeueAndQueue(0);
1452 mFrames[0].signalQueueFences();
1453
1454 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001455 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001456 dequeueAndQueue(1);
1457 mFrames[1].signalQueueFences();
1458
1459 addFrameEvents(false, NO_FRAME_INDEX, 0);
1460 addFrameEvents(false, 0, 1);
1461
1462 // Verify available timestamps are correct for frame 1, before any
1463 // fence has been signaled.
1464 // Note: A sync call is necessary here since the events triggered by
1465 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001466 resetTimestamps();
1467 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001468 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001469 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1470 EXPECT_EQ(NO_ERROR, result);
1471 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1472 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001473 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1474 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1475 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001476 EXPECT_EQ(0, outGpuCompositionDoneTime);
1477 EXPECT_EQ(0, outDisplayPresentTime);
1478 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001479 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001480 EXPECT_EQ(0, outReleaseTime);
1481
1482 mFrames[0].signalRefreshFences();
1483 mFrames[0].signalReleaseFences();
1484 mFrames[1].signalRefreshFences();
1485
Brian Anderson1049d1d2016-12-16 17:25:57 -08001486 // Verify querying for all timestmaps of f2 does not do a sync call. Even
1487 // though the lastRefresh, retire, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001488 // available, a sync call should not occur because it's not possible for f2
1489 // to encounter the final value for those events until another frame is
1490 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001491 resetTimestamps();
1492 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001493 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001494 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1495 EXPECT_EQ(NO_ERROR, result);
1496 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1497 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001498 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1499 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1500 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001501 EXPECT_EQ(0, outGpuCompositionDoneTime);
1502 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1503 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001504 EXPECT_EQ(0, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001505 EXPECT_EQ(0, outReleaseTime);
1506}
1507
1508// This test verifies there are no sync calls for present or retire times
1509// when they aren't supported and that an error is returned.
1510void GetFrameTimestampsTest::PresentOrRetireUnsupportedNoSyncTest(
1511 bool displayPresentSupported, bool displayRetireSupported) {
1512
1513 enableFrameTimestamps();
1514 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(
1515 displayPresentSupported, displayRetireSupported);
1516
1517 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001518 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001519 dequeueAndQueue(0);
1520
1521 // Verify a query for the Present and Retire times do not trigger
1522 // a sync call if they are not supported.
Brian Anderson3da8d272016-07-28 16:20:47 -07001523 resetTimestamps();
1524 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001525 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001526 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
Brian Anderson3da8d272016-07-28 16:20:47 -07001527 displayPresentSupported ? nullptr : &outDisplayPresentTime,
1528 displayRetireSupported ? nullptr : &outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001529 nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001530 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1531 EXPECT_EQ(BAD_VALUE, result);
1532 EXPECT_EQ(-1, outDisplayRetireTime);
1533 EXPECT_EQ(-1, outDisplayPresentTime);
1534}
1535
1536TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1537 PresentOrRetireUnsupportedNoSyncTest(false, true);
1538}
1539
1540TEST_F(GetFrameTimestampsTest, RetireUnsupportedNoSync) {
1541 PresentOrRetireUnsupportedNoSyncTest(true, false);
1542}
1543
Jamie Gennis134f0422011-03-08 12:18:54 -08001544}