blob: 52980270f2db6e849baf2fe884b127102c082790 [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
21#include <binder/IMemory.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070022#include <binder/ProcessState.h>
23#include <gui/IDisplayEventConnection.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080024#include <gui/ISurfaceComposer.h>
25#include <gui/Surface.h>
26#include <gui/SurfaceComposerClient.h>
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -070027#include <gui/BufferItemConsumer.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
Jamie Gennisfc850122011-04-25 16:40:05 -070055 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070056 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis134f0422011-03-08 12:18:54 -080057
58 ASSERT_TRUE(mSurfaceControl != NULL);
59 ASSERT_TRUE(mSurfaceControl->isValid());
60
Mathias Agopian698c0872011-06-28 19:09:31 -070061 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian9303eee2011-07-01 15:27:27 -070062 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff));
Jamie Gennis134f0422011-03-08 12:18:54 -080063 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070064 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennis134f0422011-03-08 12:18:54 -080065
66 mSurface = mSurfaceControl->getSurface();
67 ASSERT_TRUE(mSurface != NULL);
68 }
69
70 virtual void TearDown() {
71 mComposerClient->dispose();
72 }
73
74 sp<Surface> mSurface;
75 sp<SurfaceComposerClient> mComposerClient;
76 sp<SurfaceControl> mSurfaceControl;
77};
78
79TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
80 sp<ANativeWindow> anw(mSurface);
81 int result = -123;
82 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
83 &result);
84 EXPECT_EQ(NO_ERROR, err);
85 EXPECT_EQ(1, result);
86}
87
88TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
89 mSurfaceControl.clear();
Kalle Raita643f0942016-12-07 09:20:14 -080090 // Wait for the async clean-up to complete.
91 std::this_thread::sleep_for(50ms);
Jamie Gennis134f0422011-03-08 12:18:54 -080092
93 sp<ANativeWindow> anw(mSurface);
94 int result = -123;
95 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
96 &result);
97 EXPECT_EQ(NO_ERROR, err);
98 EXPECT_EQ(1, result);
99}
100
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800101// This test probably doesn't belong here.
Jamie Gennisc901ca02011-10-11 16:02:31 -0700102TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800103 sp<ANativeWindow> anw(mSurface);
104
105 // Verify the screenshot works with no protected buffers.
Dan Stoza5603a2f2014-04-07 13:41:37 -0700106 sp<IGraphicBufferProducer> producer;
107 sp<IGraphicBufferConsumer> consumer;
108 BufferQueue::createBufferQueue(&producer, &consumer);
109 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800110 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Brian Anderson3da8d272016-07-28 16:20:47 -0700111 sp<IBinder> display(sf->getBuiltInDisplay(
112 ISurfaceComposer::eDisplayIdMain));
Dan Stozac1879002014-05-22 15:59:05 -0700113 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800114 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800115
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700116 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
117 NATIVE_WINDOW_API_CPU));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800118 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
119 // that we need to dequeue a buffer in order for it to actually get
120 // allocated in SurfaceFlinger.
121 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
122 GRALLOC_USAGE_PROTECTED));
123 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Iliyan Malchev697526b2011-05-01 11:33:26 -0700124 ANativeWindowBuffer* buf = 0;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700125
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700126 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700127 if (err) {
128 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
129 // that's okay as long as this is the reason for the failure.
130 // try again without the GRALLOC_USAGE_PROTECTED bit.
131 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700132 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
133 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700134 return;
135 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700136 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700137
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800138 for (int i = 0; i < 4; i++) {
139 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700140 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
141 &buf));
142 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800143 }
Dan Stozac1879002014-05-22 15:59:05 -0700144 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800145 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800146}
147
Jamie Gennis391bbe22011-03-14 15:00:06 -0700148TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
149 sp<ANativeWindow> anw(mSurface);
150 int result = -123;
151 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
152 EXPECT_EQ(NO_ERROR, err);
153 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
154}
155
Craig Donner6ebc46a2016-10-21 15:23:44 -0700156TEST_F(SurfaceTest, LayerCountIsOne) {
157 sp<ANativeWindow> anw(mSurface);
158 int result = -123;
159 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
160 EXPECT_EQ(NO_ERROR, err);
161 EXPECT_EQ(1, result);
162}
163
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700164TEST_F(SurfaceTest, QueryConsumerUsage) {
165 const int TEST_USAGE_FLAGS =
166 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700167 sp<IGraphicBufferProducer> producer;
168 sp<IGraphicBufferConsumer> consumer;
169 BufferQueue::createBufferQueue(&producer, &consumer);
170 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700171 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700172 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700173
174 sp<ANativeWindow> anw(s);
175
176 int flags = -1;
177 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
178
179 ASSERT_EQ(NO_ERROR, err);
180 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
181}
182
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800183TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
184 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
185 sp<IGraphicBufferProducer> producer;
186 sp<IGraphicBufferConsumer> consumer;
187 BufferQueue::createBufferQueue(&producer, &consumer);
188 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
189
190 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
191
192 sp<Surface> s = new Surface(producer);
193
194 sp<ANativeWindow> anw(s);
195
196 android_dataspace dataSpace;
197
198 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
199 reinterpret_cast<int*>(&dataSpace));
200
201 ASSERT_EQ(NO_ERROR, err);
202 ASSERT_EQ(TEST_DATASPACE, dataSpace);
203}
204
Dan Stoza812ed062015-06-02 15:45:22 -0700205TEST_F(SurfaceTest, SettingGenerationNumber) {
206 sp<IGraphicBufferProducer> producer;
207 sp<IGraphicBufferConsumer> consumer;
208 BufferQueue::createBufferQueue(&producer, &consumer);
209 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
210 sp<Surface> surface = new Surface(producer);
211 sp<ANativeWindow> window(surface);
212
213 // Allocate a buffer with a generation number of 0
214 ANativeWindowBuffer* buffer;
215 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700216 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
217 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700218 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
219 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
220
221 // Detach the buffer and check its generation number
222 sp<GraphicBuffer> graphicBuffer;
223 sp<Fence> fence;
224 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
225 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
226
227 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
228 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
229
230 // This should change the generation number of the GraphicBuffer
231 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
232
233 // Check that the new generation number sticks with the buffer
234 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
235 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
236 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
237 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
238}
239
Dan Stozac6f30bd2015-06-08 09:32:50 -0700240TEST_F(SurfaceTest, GetConsumerName) {
241 sp<IGraphicBufferProducer> producer;
242 sp<IGraphicBufferConsumer> consumer;
243 BufferQueue::createBufferQueue(&producer, &consumer);
244
245 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
246 consumer->consumerConnect(dummyConsumer, false);
247 consumer->setConsumerName(String8("TestConsumer"));
248
249 sp<Surface> surface = new Surface(producer);
250 sp<ANativeWindow> window(surface);
251 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
252
253 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
254}
255
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800256TEST_F(SurfaceTest, DynamicSetBufferCount) {
257 sp<IGraphicBufferProducer> producer;
258 sp<IGraphicBufferConsumer> consumer;
259 BufferQueue::createBufferQueue(&producer, &consumer);
260
261 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
262 consumer->consumerConnect(dummyConsumer, false);
263 consumer->setConsumerName(String8("TestConsumer"));
264
265 sp<Surface> surface = new Surface(producer);
266 sp<ANativeWindow> window(surface);
267
268 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
269 NATIVE_WINDOW_API_CPU));
270 native_window_set_buffer_count(window.get(), 4);
271
272 int fence;
273 ANativeWindowBuffer* buffer;
274 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
275 native_window_set_buffer_count(window.get(), 3);
276 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
277 native_window_set_buffer_count(window.get(), 2);
278 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
279 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
280}
281
Brian Anderson3da8d272016-07-28 16:20:47 -0700282
283class FakeConsumer : public BnConsumerListener {
284public:
285 void onFrameAvailable(const BufferItem& /*item*/) override {}
286 void onBuffersReleased() override {}
287 void onSidebandStreamChanged() override {}
288
289 void addAndGetFrameTimestamps(
290 const NewFrameEventsEntry* newTimestamps,
291 FrameEventHistoryDelta* outDelta) override {
292 if (newTimestamps) {
293 if (mGetFrameTimestampsEnabled) {
294 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
295 "Test should set mNewFrameEntryOverride before queuing "
296 "a frame.";
297 EXPECT_EQ(newTimestamps->frameNumber,
298 mNewFrameEntryOverride.frameNumber) <<
299 "Test attempting to add NewFrameEntryOverride with "
300 "incorrect frame number.";
301 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
302 mNewFrameEntryOverride.frameNumber = 0;
303 }
304 mAddFrameTimestampsCount++;
305 mLastAddedFrameNumber = newTimestamps->frameNumber;
306 }
307 if (outDelta) {
308 mFrameEventHistory.getAndResetDelta(outDelta);
309 mGetFrameTimestampsCount++;
310 }
311 mAddAndGetFrameTimestampsCallCount++;
312 }
313
314 bool mGetFrameTimestampsEnabled = false;
315
316 ConsumerFrameEventHistory mFrameEventHistory;
317 int mAddAndGetFrameTimestampsCallCount = 0;
318 int mAddFrameTimestampsCount = 0;
319 int mGetFrameTimestampsCount = 0;
320 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
321
322 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
323};
324
325
326class FakeSurfaceComposer : public ISurfaceComposer{
327public:
328 ~FakeSurfaceComposer() override {}
329
330 void setSupportedTimestamps(bool supportsPresent, bool supportsRetire) {
331 mSupportsPresent = supportsPresent;
332 mSupportsRetire = supportsRetire;
333 }
334
335 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800336 sp<ISurfaceComposerClient> createScopedConnection(
337 const sp<IGraphicBufferProducer>& /* parent */) override {
338 return nullptr;
339 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700340 sp<IGraphicBufferAlloc> createGraphicBufferAlloc() override {
341 return nullptr;
342 }
343 sp<IDisplayEventConnection> createDisplayEventConnection() override {
344 return nullptr;
345 }
346 sp<IBinder> createDisplay(const String8& /*displayName*/,
347 bool /*secure*/) override { return nullptr; }
348 void destroyDisplay(const sp<IBinder>& /*display */) override {}
349 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
350 void setTransactionState(const Vector<ComposerState>& /*state*/,
351 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
352 override {}
353 void bootFinished() override {}
354 bool authenticateSurfaceTexture(
355 const sp<IGraphicBufferProducer>& /*surface*/) const override {
356 return false;
357 }
358
359 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
360 const override {
361 *outSupported = {
362 FrameEvent::REQUESTED_PRESENT,
363 FrameEvent::ACQUIRE,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700364 FrameEvent::LATCH,
Brian Anderson3da8d272016-07-28 16:20:47 -0700365 FrameEvent::FIRST_REFRESH_START,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700366 FrameEvent::LAST_REFRESH_START,
Brian Anderson3da8d272016-07-28 16:20:47 -0700367 FrameEvent::GL_COMPOSITION_DONE,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700368 FrameEvent::DEQUEUE_READY,
Brian Anderson3da8d272016-07-28 16:20:47 -0700369 FrameEvent::RELEASE
370 };
371 if (mSupportsPresent) {
372 outSupported->push_back(
373 FrameEvent::DISPLAY_PRESENT);
374 }
375 if (mSupportsRetire) {
376 outSupported->push_back(
377 FrameEvent::DISPLAY_RETIRE);
378 }
379 return NO_ERROR;
380 }
381
382 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
383 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
384 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
385 status_t getDisplayStats(const sp<IBinder>& /*display*/,
386 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
387 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
388 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
389 override {
390 return NO_ERROR;
391 }
392 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
393 Vector<android_color_mode_t>* /*outColorModes*/) override {
394 return NO_ERROR;
395 }
396 android_color_mode_t getActiveColorMode(const sp<IBinder>& /*display*/)
397 override {
398 return HAL_COLOR_MODE_NATIVE;
399 }
400 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
401 android_color_mode_t /*colorMode*/) override { return NO_ERROR; }
402 status_t captureScreen(const sp<IBinder>& /*display*/,
403 const sp<IGraphicBufferProducer>& /*producer*/,
404 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800405 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700406 bool /*useIdentityTransform*/,
407 Rotation /*rotation*/) override { return NO_ERROR; }
408 status_t clearAnimationFrameStats() override { return NO_ERROR; }
409 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
410 return NO_ERROR;
411 }
412 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
413 HdrCapabilities* /*outCapabilities*/) const override {
414 return NO_ERROR;
415 }
416 status_t enableVSyncInjections(bool /*enable*/) override {
417 return NO_ERROR;
418 }
419 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
420
421protected:
422 IBinder* onAsBinder() override { return nullptr; }
423
424private:
425 bool mSupportsPresent{true};
426 bool mSupportsRetire{true};
427};
428
429class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
430public:
431 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
432 : mFenceMap(fenceMap) {}
433
434 ~FakeProducerFrameEventHistory() {}
435
436 void updateAcquireFence(uint64_t frameNumber,
437 std::shared_ptr<FenceTime>&& acquire) override {
438 // Verify the acquire fence being added isn't the one from the consumer.
439 EXPECT_NE(mConsumerAcquireFence, acquire);
440 // Override the fence, so we can verify this was called by the
441 // producer after the frame is queued.
442 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
443 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
444 }
445
446 void setAcquireFenceOverride(
447 const std::shared_ptr<FenceTime>& acquireFenceOverride,
448 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
449 mAcquireFenceOverride = acquireFenceOverride;
450 mConsumerAcquireFence = consumerAcquireFence;
451 }
452
453protected:
454 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
455 const override {
456 return mFenceMap->createFenceTimeForTest(fence);
457 }
458
459 FenceToFenceTimeMap* mFenceMap{nullptr};
460
461 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
462 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
463};
464
465
466class TestSurface : public Surface {
467public:
468 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
469 FenceToFenceTimeMap* fenceMap)
470 : Surface(bufferProducer),
471 mFakeSurfaceComposer(new FakeSurfaceComposer) {
472 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
473 mFrameEventHistory.reset(mFakeFrameEventHistory);
474 }
475
476 ~TestSurface() override {}
477
478 sp<ISurfaceComposer> composerService() const override {
479 return mFakeSurfaceComposer;
480 }
481
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800482 nsecs_t now() const override {
483 return mNow;
484 }
485
486 void setNow(nsecs_t now) {
487 mNow = now;
488 }
489
Brian Anderson3da8d272016-07-28 16:20:47 -0700490public:
491 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800492 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700493
494 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
495 // but this raw pointer gives access to test functionality.
496 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
497};
498
499
500class GetFrameTimestampsTest : public SurfaceTest {
501protected:
502 struct FenceAndFenceTime {
503 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
504 : mFence(new Fence),
505 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
506 sp<Fence> mFence { nullptr };
507 std::shared_ptr<FenceTime> mFenceTime { nullptr };
508 };
509
510 struct RefreshEvents {
511 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800512 : mFenceMap(fenceMap),
513 kCompositorTiming(
514 {refreshStart, refreshStart + 1, refreshStart + 2 }),
515 kStartTime(refreshStart + 3),
516 kGpuCompositionDoneTime(refreshStart + 4),
517 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700518
519 void signalPostCompositeFences() {
520 mFenceMap.signalAllForTest(
521 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
522 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
523 }
524
525 FenceToFenceTimeMap& mFenceMap;
526
527 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
528 FenceAndFenceTime mPresent { mFenceMap };
529
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800530 const CompositorTiming kCompositorTiming;
531
Brian Anderson3da8d272016-07-28 16:20:47 -0700532 const nsecs_t kStartTime;
533 const nsecs_t kGpuCompositionDoneTime;
534 const nsecs_t kPresentTime;
535 };
536
537 struct FrameEvents {
538 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
539 : mFenceMap(fenceMap),
540 kPostedTime(frameStartTime + 100),
541 kRequestedPresentTime(frameStartTime + 200),
542 kProducerAcquireTime(frameStartTime + 300),
543 kConsumerAcquireTime(frameStartTime + 301),
544 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700545 kDequeueReadyTime(frameStartTime + 600),
546 kRetireTime(frameStartTime + 700),
547 kReleaseTime(frameStartTime + 800),
Brian Anderson3da8d272016-07-28 16:20:47 -0700548 mRefreshes {
549 { mFenceMap, frameStartTime + 410 },
550 { mFenceMap, frameStartTime + 420 },
551 { mFenceMap, frameStartTime + 430 } } {}
552
553 void signalQueueFences() {
554 mFenceMap.signalAllForTest(
555 mAcquireConsumer.mFence, kConsumerAcquireTime);
556 mFenceMap.signalAllForTest(
557 mAcquireProducer.mFence, kProducerAcquireTime);
558 }
559
560 void signalRefreshFences() {
561 for (auto& re : mRefreshes) {
562 re.signalPostCompositeFences();
563 }
564 }
565
566 void signalReleaseFences() {
567 mFenceMap.signalAllForTest(mRetire.mFence, kRetireTime);
568 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
569 }
570
571 FenceToFenceTimeMap& mFenceMap;
572
573 FenceAndFenceTime mAcquireConsumer { mFenceMap };
574 FenceAndFenceTime mAcquireProducer { mFenceMap };
575 FenceAndFenceTime mRetire { mFenceMap };
576 FenceAndFenceTime mRelease { mFenceMap };
577
578 const nsecs_t kPostedTime;
579 const nsecs_t kRequestedPresentTime;
580 const nsecs_t kProducerAcquireTime;
581 const nsecs_t kConsumerAcquireTime;
582 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700583 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700584 const nsecs_t kRetireTime;
585 const nsecs_t kReleaseTime;
586
587 RefreshEvents mRefreshes[3];
588 };
589
590 GetFrameTimestampsTest() : SurfaceTest() {}
591
592 virtual void SetUp() {
593 SurfaceTest::SetUp();
594
595 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
596 mFakeConsumer = new FakeConsumer;
597 mCfeh = &mFakeConsumer->mFrameEventHistory;
598 mConsumer->consumerConnect(mFakeConsumer, false);
599 mConsumer->setConsumerName(String8("TestConsumer"));
600 mSurface = new TestSurface(mProducer, &mFenceMap);
601 mWindow = mSurface;
602
603 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
604 NATIVE_WINDOW_API_CPU));
605 native_window_set_buffer_count(mWindow.get(), 4);
606 }
607
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800608 void disableFrameTimestamps() {
609 mFakeConsumer->mGetFrameTimestampsEnabled = false;
610 native_window_enable_frame_timestamps(mWindow.get(), 0);
611 mFrameTimestampsEnabled = false;
612 }
613
Brian Anderson3da8d272016-07-28 16:20:47 -0700614 void enableFrameTimestamps() {
615 mFakeConsumer->mGetFrameTimestampsEnabled = true;
616 native_window_enable_frame_timestamps(mWindow.get(), 1);
617 mFrameTimestampsEnabled = true;
618 }
619
Brian Anderson1049d1d2016-12-16 17:25:57 -0800620 int getAllFrameTimestamps(uint64_t frameId) {
621 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700622 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
623 &outFirstRefreshStartTime, &outLastRefreshStartTime,
624 &outGpuCompositionDoneTime, &outDisplayPresentTime,
625 &outDisplayRetireTime, &outDequeueReadyTime, &outReleaseTime);
626 }
627
Brian Anderson3da8d272016-07-28 16:20:47 -0700628 void resetTimestamps() {
629 outRequestedPresentTime = -1;
630 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700631 outLatchTime = -1;
632 outFirstRefreshStartTime = -1;
633 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700634 outGpuCompositionDoneTime = -1;
635 outDisplayPresentTime = -1;
636 outDisplayRetireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700637 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700638 outReleaseTime = -1;
639 }
640
Brian Anderson1049d1d2016-12-16 17:25:57 -0800641 uint64_t getNextFrameId() {
642 uint64_t frameId = -1;
643 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
644 EXPECT_EQ(status, NO_ERROR);
645 return frameId;
646 }
647
Brian Anderson3da8d272016-07-28 16:20:47 -0700648 void dequeueAndQueue(uint64_t frameIndex) {
649 int fence = -1;
650 ANativeWindowBuffer* buffer = nullptr;
651 ASSERT_EQ(NO_ERROR,
652 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
653
654 int oldAddFrameTimestampsCount =
655 mFakeConsumer->mAddFrameTimestampsCount;
656
657 FrameEvents* frame = &mFrames[frameIndex];
658 uint64_t frameNumber = frameIndex + 1;
659
660 NewFrameEventsEntry fe;
661 fe.frameNumber = frameNumber;
662 fe.postedTime = frame->kPostedTime;
663 fe.requestedPresentTime = frame->kRequestedPresentTime;
664 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
665 mFakeConsumer->mNewFrameEntryOverride = fe;
666
667 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
668 frame->mAcquireProducer.mFenceTime,
669 frame->mAcquireConsumer.mFenceTime);
670
671 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
672
673 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
674
675 EXPECT_EQ(
676 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
677 mFakeConsumer->mAddFrameTimestampsCount);
678 }
679
680 void addFrameEvents(
681 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
682 FrameEvents* oldFrame =
683 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
684 FrameEvents* newFrame = &mFrames[iNewFrame];
685
686 uint64_t nOldFrame = iOldFrame + 1;
687 uint64_t nNewFrame = iNewFrame + 1;
688
689 // Latch, Composite, Retire, and Release the frames in a plausible
690 // order. Note: The timestamps won't necessarily match the order, but
691 // that's okay for the purposes of this test.
692 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
693
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700694 // Composite the previous frame one more time, which helps verify
695 // LastRefresh is updated properly.
696 if (oldFrame != nullptr) {
697 mCfeh->addPreComposition(nOldFrame,
698 oldFrame->mRefreshes[2].kStartTime);
699 gpuDoneFenceTime = gpuComposited ?
700 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
701 FenceTime::NO_FENCE;
702 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800703 oldFrame->mRefreshes[2].mPresent.mFenceTime,
704 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700705 }
706
707 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700708 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
709
710 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
711 gpuDoneFenceTime = gpuComposited ?
712 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
713 FenceTime::NO_FENCE;
714 // HWC2 releases the previous buffer after a new latch just before
715 // calling postComposition.
716 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700717 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700718 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
719 }
720 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800721 newFrame->mRefreshes[0].mPresent.mFenceTime,
722 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700723
724 // Retire the previous buffer just after compositing the new buffer.
725 if (oldFrame != nullptr) {
726 mCfeh->addRetire(nOldFrame, oldFrame->mRetire.mFenceTime);
727 }
728
729 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
730 gpuDoneFenceTime = gpuComposited ?
731 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
732 FenceTime::NO_FENCE;
733 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800734 newFrame->mRefreshes[1].mPresent.mFenceTime,
735 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700736 }
737
738 void QueryPresentRetireSupported(
739 bool displayPresentSupported, bool displayRetireSupported);
740 void PresentOrRetireUnsupportedNoSyncTest(
741 bool displayPresentSupported, bool displayRetireSupported);
742
743 sp<IGraphicBufferProducer> mProducer;
744 sp<IGraphicBufferConsumer> mConsumer;
745 sp<FakeConsumer> mFakeConsumer;
746 ConsumerFrameEventHistory* mCfeh;
747 sp<TestSurface> mSurface;
748 sp<ANativeWindow> mWindow;
749
750 FenceToFenceTimeMap mFenceMap;
751
752 bool mFrameTimestampsEnabled = false;
753
754 int64_t outRequestedPresentTime = -1;
755 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700756 int64_t outLatchTime = -1;
757 int64_t outFirstRefreshStartTime = -1;
758 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700759 int64_t outGpuCompositionDoneTime = -1;
760 int64_t outDisplayPresentTime = -1;
761 int64_t outDisplayRetireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700762 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700763 int64_t outReleaseTime = -1;
764
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800765 FrameEvents mFrames[3] {
766 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700767};
768
769
770// This test verifies that the frame timestamps are not retrieved when not
771// explicitly enabled via native_window_enable_frame_timestamps.
772// We want to check this to make sure there's no overhead for users
773// that don't need the timestamp information.
774TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
775 int fence;
776 ANativeWindowBuffer* buffer;
777
778 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
779 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
780
Brian Anderson1049d1d2016-12-16 17:25:57 -0800781 const uint64_t fId = getNextFrameId();
782
Brian Anderson3da8d272016-07-28 16:20:47 -0700783 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
784 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
785 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
786 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
787
788 // Verify the producer doesn't get frame timestamps piggybacked on queue.
789 // It is okay that frame timestamps are added in the consumer since it is
790 // still needed for SurfaceFlinger dumps.
791 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
792 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
793 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
794
795 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800796 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700797 EXPECT_EQ(INVALID_OPERATION, result);
798 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800799
800 // Verify compositor timing query fails.
801 nsecs_t compositeDeadline = 0;
802 nsecs_t compositeInterval = 0;
803 nsecs_t compositeToPresentLatency = 0;
804 result = native_window_get_compositor_timing(mWindow.get(),
805 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
806 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700807}
808
809// This test verifies that the frame timestamps are retrieved if explicitly
810// enabled via native_window_enable_frame_timestamps.
811TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800812 CompositorTiming initialCompositorTiming {
813 1000000000, // 1s deadline
814 16666667, // 16ms interval
815 50000000, // 50ms present latency
816 };
817 mCfeh->initializeCompositorTiming(initialCompositorTiming);
818
Brian Anderson3da8d272016-07-28 16:20:47 -0700819 enableFrameTimestamps();
820
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800821 // Verify the compositor timing query gets the initial compositor values
822 // after timststamps are enabled; even before the first frame is queued
823 // or dequeued.
824 nsecs_t compositeDeadline = 0;
825 nsecs_t compositeInterval = 0;
826 nsecs_t compositeToPresentLatency = 0;
827 mSurface->setNow(initialCompositorTiming.deadline - 1);
828 int result = native_window_get_compositor_timing(mWindow.get(),
829 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
830 EXPECT_EQ(NO_ERROR, result);
831 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
832 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
833 EXPECT_EQ(initialCompositorTiming.presentLatency,
834 compositeToPresentLatency);
835
Brian Anderson3da8d272016-07-28 16:20:47 -0700836 int fence;
837 ANativeWindowBuffer* buffer;
838
839 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800840 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700841
Brian Anderson1049d1d2016-12-16 17:25:57 -0800842 const uint64_t fId1 = getNextFrameId();
843
Brian Anderson3da8d272016-07-28 16:20:47 -0700844 // Verify getFrameTimestamps is piggybacked on dequeue.
845 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
846 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800847 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700848
849 NewFrameEventsEntry f1;
850 f1.frameNumber = 1;
851 f1.postedTime = mFrames[0].kPostedTime;
852 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
853 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
854 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
855 mFrames[0].mAcquireProducer.mFenceTime,
856 mFrames[0].mAcquireConsumer.mFenceTime);
857 mFakeConsumer->mNewFrameEntryOverride = f1;
858 mFrames[0].signalQueueFences();
859
860 // Verify getFrameTimestamps is piggybacked on queue.
861 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
862 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
863 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800864 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700865
866 // Verify queries for timestamps that the producer doesn't know about
867 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800868 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -0700869 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800870 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700871}
872
873void GetFrameTimestampsTest::QueryPresentRetireSupported(
874 bool displayPresentSupported, bool displayRetireSupported) {
875 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(
876 displayPresentSupported, displayRetireSupported);
877
878 // Verify supported bits are forwarded.
879 int supportsPresent = -1;
880 mWindow.get()->query(mWindow.get(),
881 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
882 EXPECT_EQ(displayPresentSupported, supportsPresent);
883
884 int supportsRetire = -1;
885 mWindow.get()->query(mWindow.get(),
886 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &supportsRetire);
887 EXPECT_EQ(displayRetireSupported, supportsRetire);
888}
889
890TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
891 QueryPresentRetireSupported(true, false);
892}
893
894TEST_F(GetFrameTimestampsTest, QueryRetireSupported) {
895 QueryPresentRetireSupported(false, true);
896}
897
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800898TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
899 nsecs_t phase = 4000;
900 nsecs_t interval = 1000;
901
902 // Timestamp in previous interval.
903 nsecs_t timestamp = 3500;
904 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
905 timestamp, phase, interval));
906
907 // Timestamp in next interval.
908 timestamp = 4500;
909 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
910 timestamp, phase, interval));
911
912 // Timestamp multiple intervals before.
913 timestamp = 2500;
914 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
915 timestamp, phase, interval));
916
917 // Timestamp multiple intervals after.
918 timestamp = 6500;
919 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
920 timestamp, phase, interval));
921
922 // Timestamp on previous interval.
923 timestamp = 3000;
924 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
925 timestamp, phase, interval));
926
927 // Timestamp on next interval.
928 timestamp = 5000;
929 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
930 timestamp, phase, interval));
931
932 // Timestamp equal to phase.
933 timestamp = 4000;
934 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
935 timestamp, phase, interval));
936}
937
938// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
939// if the number of intervals elapsed is internally stored in an int.
940TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
941 nsecs_t phase = 0;
942 nsecs_t interval = 4000;
943 nsecs_t big_timestamp = 8635916564000;
944 int32_t intervals = big_timestamp / interval;
945
946 EXPECT_LT(intervals, 0);
947 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
948 big_timestamp, phase, interval));
949 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
950 big_timestamp, big_timestamp, interval));
951}
952
953// This verifies the compositor timing is updated by refresh events
954// and piggy backed on a queue, dequeue, and enabling of timestamps..
955TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
956 CompositorTiming initialCompositorTiming {
957 1000000000, // 1s deadline
958 16666667, // 16ms interval
959 50000000, // 50ms present latency
960 };
961 mCfeh->initializeCompositorTiming(initialCompositorTiming);
962
963 enableFrameTimestamps();
964
965 // We get the initial values before any frames are submitted.
966 nsecs_t compositeDeadline = 0;
967 nsecs_t compositeInterval = 0;
968 nsecs_t compositeToPresentLatency = 0;
969 mSurface->setNow(initialCompositorTiming.deadline - 1);
970 int result = native_window_get_compositor_timing(mWindow.get(),
971 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
972 EXPECT_EQ(NO_ERROR, result);
973 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
974 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
975 EXPECT_EQ(initialCompositorTiming.presentLatency,
976 compositeToPresentLatency);
977
978 const uint64_t fId1 = getNextFrameId();
979 dequeueAndQueue(0);
980 addFrameEvents(true, NO_FRAME_INDEX, 0);
981
982 // Still get the initial values because the frame events for frame 0
983 // didn't get a chance to piggyback on a queue or dequeue yet.
984 result = native_window_get_compositor_timing(mWindow.get(),
985 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
986 EXPECT_EQ(NO_ERROR, result);
987 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
988 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
989 EXPECT_EQ(initialCompositorTiming.presentLatency,
990 compositeToPresentLatency);
991
992 const uint64_t fId2 = getNextFrameId();
993 dequeueAndQueue(1);
994 addFrameEvents(true, 0, 1);
995
996 // Now expect the composite values associated with frame 1.
997 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
998 result = native_window_get_compositor_timing(mWindow.get(),
999 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1000 EXPECT_EQ(NO_ERROR, result);
1001 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1002 compositeDeadline);
1003 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1004 compositeInterval);
1005 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1006 compositeToPresentLatency);
1007
1008 dequeueAndQueue(2);
1009 addFrameEvents(true, 1, 2);
1010
1011 // Now expect the composite values associated with frame 2.
1012 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1013 result = native_window_get_compositor_timing(mWindow.get(),
1014 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1015 EXPECT_EQ(NO_ERROR, result);
1016 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1017 compositeDeadline);
1018 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1019 compositeInterval);
1020 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1021 compositeToPresentLatency);
1022
1023 // Re-enabling frame timestamps should get the latest values.
1024 disableFrameTimestamps();
1025 enableFrameTimestamps();
1026
1027 // Now expect the composite values associated with frame 3.
1028 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1029 result = native_window_get_compositor_timing(mWindow.get(),
1030 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1031 EXPECT_EQ(NO_ERROR, result);
1032 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1033 compositeDeadline);
1034 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1035 compositeInterval);
1036 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1037 compositeToPresentLatency);
1038}
1039
1040// This verifies the compositor deadline properly snaps to the the next
1041// deadline based on the current time.
1042TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1043 CompositorTiming initialCompositorTiming {
1044 1000000000, // 1s deadline
1045 16666667, // 16ms interval
1046 50000000, // 50ms present latency
1047 };
1048 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1049
1050 enableFrameTimestamps();
1051
1052 nsecs_t compositeDeadline = 0;
1053 nsecs_t compositeInterval = 0;
1054 nsecs_t compositeToPresentLatency = 0;
1055
1056 // A "now" just before the deadline snaps to the deadline.
1057 mSurface->setNow(initialCompositorTiming.deadline - 1);
1058 int result = native_window_get_compositor_timing(mWindow.get(),
1059 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1060 EXPECT_EQ(NO_ERROR, result);
1061 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1062 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1063 EXPECT_EQ(expectedDeadline, compositeDeadline);
1064
1065 const uint64_t fId1 = getNextFrameId();
1066 dequeueAndQueue(0);
1067 addFrameEvents(true, NO_FRAME_INDEX, 0);
1068
1069 // A "now" just after the deadline snaps properly.
1070 mSurface->setNow(initialCompositorTiming.deadline + 1);
1071 result = native_window_get_compositor_timing(mWindow.get(),
1072 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1073 EXPECT_EQ(NO_ERROR, result);
1074 expectedDeadline =
1075 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1076 EXPECT_EQ(expectedDeadline, compositeDeadline);
1077
1078 const uint64_t fId2 = getNextFrameId();
1079 dequeueAndQueue(1);
1080 addFrameEvents(true, 0, 1);
1081
1082 // A "now" just after the next interval snaps properly.
1083 mSurface->setNow(
1084 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1085 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1086 result = native_window_get_compositor_timing(mWindow.get(),
1087 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1088 EXPECT_EQ(NO_ERROR, result);
1089 expectedDeadline =
1090 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1091 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1092 EXPECT_EQ(expectedDeadline, compositeDeadline);
1093
1094 dequeueAndQueue(2);
1095 addFrameEvents(true, 1, 2);
1096
1097 // A "now" over 1 interval before the deadline snaps properly.
1098 mSurface->setNow(
1099 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1100 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1101 result = native_window_get_compositor_timing(mWindow.get(),
1102 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1103 EXPECT_EQ(NO_ERROR, result);
1104 expectedDeadline =
1105 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1106 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1107 EXPECT_EQ(expectedDeadline, compositeDeadline);
1108
1109 // Re-enabling frame timestamps should get the latest values.
1110 disableFrameTimestamps();
1111 enableFrameTimestamps();
1112
1113 // A "now" over 2 intervals before the deadline snaps properly.
1114 mSurface->setNow(
1115 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1116 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1117 result = native_window_get_compositor_timing(mWindow.get(),
1118 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1119 EXPECT_EQ(NO_ERROR, result);
1120 expectedDeadline =
1121 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1122 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1123 EXPECT_EQ(expectedDeadline, compositeDeadline);
1124}
1125
Brian Anderson1049d1d2016-12-16 17:25:57 -08001126// This verifies the timestamps recorded in the consumer's
1127// FrameTimestampsHistory are properly retrieved by the producer for the
1128// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001129TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1130 enableFrameTimestamps();
1131
Brian Anderson1049d1d2016-12-16 17:25:57 -08001132 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001133 dequeueAndQueue(0);
1134 mFrames[0].signalQueueFences();
1135
Brian Anderson1049d1d2016-12-16 17:25:57 -08001136 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001137 dequeueAndQueue(1);
1138 mFrames[1].signalQueueFences();
1139
1140 addFrameEvents(true, NO_FRAME_INDEX, 0);
1141 mFrames[0].signalRefreshFences();
1142 addFrameEvents(true, 0, 1);
1143 mFrames[0].signalReleaseFences();
1144 mFrames[1].signalRefreshFences();
1145
1146 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001147 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001148 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001149 EXPECT_EQ(NO_ERROR, result);
1150 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1151 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001152 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1153 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1154 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001155 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1156 outGpuCompositionDoneTime);
1157 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1158 EXPECT_EQ(mFrames[0].kRetireTime, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001159 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001160 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1161
1162 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001163 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001164 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001165 EXPECT_EQ(NO_ERROR, result);
1166 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1167 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001168 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1169 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1170 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001171 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1172 outGpuCompositionDoneTime);
1173 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1174 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001175 EXPECT_EQ(0, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001176 EXPECT_EQ(0, outReleaseTime);
1177}
1178
1179// This test verifies the acquire fence recorded by the consumer is not sent
1180// back to the producer and the producer saves its own fence.
1181TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1182 enableFrameTimestamps();
1183 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1184
Brian Anderson3da8d272016-07-28 16:20:47 -07001185 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001186 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001187 dequeueAndQueue(0);
1188
1189 // Verify queue-related timestamps for f1 are available immediately in the
1190 // producer without asking the consumer again, even before signaling the
1191 // acquire fence.
1192 resetTimestamps();
1193 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001194 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001195 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001196 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001197 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1198 EXPECT_EQ(NO_ERROR, result);
1199 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1200 EXPECT_EQ(0, outAcquireTime);
1201
1202 // Signal acquire fences. Verify a sync call still isn't necessary.
1203 mFrames[0].signalQueueFences();
1204
1205 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001206 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001207 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001208 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001209 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1210 EXPECT_EQ(NO_ERROR, result);
1211 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1212 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1213
1214 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001215 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001216 dequeueAndQueue(1);
1217
1218 // Verify queue-related timestamps for f2 are available immediately in the
1219 // producer without asking the consumer again, even before signaling the
1220 // acquire fence.
1221 resetTimestamps();
1222 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001223 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
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[1].kRequestedPresentTime, outRequestedPresentTime);
1229 EXPECT_EQ(0, outAcquireTime);
1230
1231 // Signal acquire fences. Verify a sync call still isn't necessary.
1232 mFrames[1].signalQueueFences();
1233
1234 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001235 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
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[1].kRequestedPresentTime, outRequestedPresentTime);
1241 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1242}
1243
1244TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1245 enableFrameTimestamps();
1246 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1247
1248 // Dequeue and queue frame 1.
1249 dequeueAndQueue(0);
1250 mFrames[0].signalQueueFences();
1251
1252 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001253 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001254 dequeueAndQueue(1);
1255 mFrames[1].signalQueueFences();
1256
1257 addFrameEvents(true, NO_FRAME_INDEX, 0);
1258 mFrames[0].signalRefreshFences();
1259 addFrameEvents(true, 0, 1);
1260 mFrames[0].signalReleaseFences();
1261 mFrames[1].signalRefreshFences();
1262
1263 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001264 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001265 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001266 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1267 nullptr, nullptr, nullptr);
1268 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001269 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1270}
1271
1272// This test verifies that fences can signal and update timestamps producer
1273// side without an additional sync call to the consumer.
1274TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1275 enableFrameTimestamps();
1276 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1277
1278 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001279 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001280 dequeueAndQueue(0);
1281 mFrames[0].signalQueueFences();
1282
1283 // Dequeue and queue frame 2.
1284 dequeueAndQueue(1);
1285 mFrames[1].signalQueueFences();
1286
1287 addFrameEvents(true, NO_FRAME_INDEX, 0);
1288 addFrameEvents(true, 0, 1);
1289
1290 // Verify available timestamps are correct for frame 1, before any
1291 // fence has been signaled.
1292 // Note: A sync call is necessary here since the events triggered by
1293 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001294 resetTimestamps();
1295 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001296 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001297 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1298 EXPECT_EQ(NO_ERROR, result);
1299 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1300 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001301 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1302 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1303 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001304 EXPECT_EQ(0, outGpuCompositionDoneTime);
1305 EXPECT_EQ(0, outDisplayPresentTime);
1306 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001307 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001308 EXPECT_EQ(0, outReleaseTime);
1309
1310 // Verify available timestamps are correct for frame 1 again, before any
1311 // fence has been signaled.
1312 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001313 resetTimestamps();
1314 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001315 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001316 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1317 EXPECT_EQ(NO_ERROR, result);
1318 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1319 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001320 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1321 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1322 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001323 EXPECT_EQ(0, outGpuCompositionDoneTime);
1324 EXPECT_EQ(0, outDisplayPresentTime);
1325 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001326 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001327 EXPECT_EQ(0, outReleaseTime);
1328
1329 // Signal the fences for frame 1.
1330 mFrames[0].signalRefreshFences();
1331 mFrames[0].signalReleaseFences();
1332
1333 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001334 resetTimestamps();
1335 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001336 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001337 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1338 EXPECT_EQ(NO_ERROR, result);
1339 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1340 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001341 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1342 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1343 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001344 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1345 outGpuCompositionDoneTime);
1346 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1347 EXPECT_EQ(mFrames[0].kRetireTime, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001348 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001349 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1350}
1351
1352// This test verifies that if the frame wasn't GPU composited but has a refresh
1353// event a sync call isn't made to get the GPU composite done time since it will
1354// never exist.
1355TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1356 enableFrameTimestamps();
1357 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1358
Brian Anderson3da8d272016-07-28 16:20:47 -07001359 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001360 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001361 dequeueAndQueue(0);
1362 mFrames[0].signalQueueFences();
1363
1364 // Dequeue and queue frame 2.
1365 dequeueAndQueue(1);
1366 mFrames[1].signalQueueFences();
1367
1368 addFrameEvents(false, NO_FRAME_INDEX, 0);
1369 addFrameEvents(false, 0, 1);
1370
1371 // Verify available timestamps are correct for frame 1, before any
1372 // fence has been signaled.
1373 // Note: A sync call is necessary here since the events triggered by
1374 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1375 resetTimestamps();
1376 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001377 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001378 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1379 EXPECT_EQ(NO_ERROR, result);
1380 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1381 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001382 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1383 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1384 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001385 EXPECT_EQ(0, outGpuCompositionDoneTime);
1386 EXPECT_EQ(0, outDisplayPresentTime);
1387 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001388 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001389 EXPECT_EQ(0, outReleaseTime);
1390
1391 // Signal the fences for frame 1.
1392 mFrames[0].signalRefreshFences();
1393 mFrames[0].signalReleaseFences();
1394
1395 // Verify all timestamps, except GPU composition, are available without a
1396 // sync call.
1397 resetTimestamps();
1398 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001399 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001400 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1401 EXPECT_EQ(NO_ERROR, result);
1402 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1403 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001404 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1405 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1406 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001407 EXPECT_EQ(0, outGpuCompositionDoneTime);
1408 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1409 EXPECT_EQ(mFrames[0].kRetireTime, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001410 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001411 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1412}
1413
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001414// This test verifies that if the certain timestamps can't possibly exist for
1415// the most recent frame, then a sync call is not done.
Brian Anderson3da8d272016-07-28 16:20:47 -07001416TEST_F(GetFrameTimestampsTest, NoRetireOrReleaseNoSync) {
1417 enableFrameTimestamps();
1418 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1419
1420 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001421 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001422 dequeueAndQueue(0);
1423 mFrames[0].signalQueueFences();
1424
1425 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001426 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001427 dequeueAndQueue(1);
1428 mFrames[1].signalQueueFences();
1429
1430 addFrameEvents(false, NO_FRAME_INDEX, 0);
1431 addFrameEvents(false, 0, 1);
1432
1433 // Verify available timestamps are correct for frame 1, before any
1434 // fence has been signaled.
1435 // Note: A sync call is necessary here since the events triggered by
1436 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001437 resetTimestamps();
1438 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001439 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001440 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1441 EXPECT_EQ(NO_ERROR, result);
1442 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1443 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001444 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1445 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1446 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001447 EXPECT_EQ(0, outGpuCompositionDoneTime);
1448 EXPECT_EQ(0, outDisplayPresentTime);
1449 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001450 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001451 EXPECT_EQ(0, outReleaseTime);
1452
1453 mFrames[0].signalRefreshFences();
1454 mFrames[0].signalReleaseFences();
1455 mFrames[1].signalRefreshFences();
1456
Brian Anderson1049d1d2016-12-16 17:25:57 -08001457 // Verify querying for all timestmaps of f2 does not do a sync call. Even
1458 // though the lastRefresh, retire, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001459 // available, a sync call should not occur because it's not possible for f2
1460 // to encounter the final value for those events until another frame is
1461 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001462 resetTimestamps();
1463 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001464 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001465 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1466 EXPECT_EQ(NO_ERROR, result);
1467 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1468 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001469 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1470 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1471 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001472 EXPECT_EQ(0, outGpuCompositionDoneTime);
1473 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1474 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001475 EXPECT_EQ(0, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001476 EXPECT_EQ(0, outReleaseTime);
1477}
1478
1479// This test verifies there are no sync calls for present or retire times
1480// when they aren't supported and that an error is returned.
1481void GetFrameTimestampsTest::PresentOrRetireUnsupportedNoSyncTest(
1482 bool displayPresentSupported, bool displayRetireSupported) {
1483
1484 enableFrameTimestamps();
1485 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(
1486 displayPresentSupported, displayRetireSupported);
1487
1488 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001489 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001490 dequeueAndQueue(0);
1491
1492 // Verify a query for the Present and Retire times do not trigger
1493 // a sync call if they are not supported.
Brian Anderson3da8d272016-07-28 16:20:47 -07001494 resetTimestamps();
1495 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001496 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001497 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
Brian Anderson3da8d272016-07-28 16:20:47 -07001498 displayPresentSupported ? nullptr : &outDisplayPresentTime,
1499 displayRetireSupported ? nullptr : &outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001500 nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001501 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1502 EXPECT_EQ(BAD_VALUE, result);
1503 EXPECT_EQ(-1, outDisplayRetireTime);
1504 EXPECT_EQ(-1, outDisplayPresentTime);
1505}
1506
1507TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1508 PresentOrRetireUnsupportedNoSyncTest(false, true);
1509}
1510
1511TEST_F(GetFrameTimestampsTest, RetireUnsupportedNoSync) {
1512 PresentOrRetireUnsupportedNoSyncTest(true, false);
1513}
1514
Jamie Gennis134f0422011-03-08 12:18:54 -08001515}