blob: 012dbe7c9552e219bf79613a52c06602685f4987 [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>
22#include <gui/IDisplayEventConnection.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080023#include <gui/ISurfaceComposer.h>
24#include <gui/Surface.h>
25#include <gui/SurfaceComposerClient.h>
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -070026#include <gui/BufferItemConsumer.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070027#include <private/gui/ComposerService.h>
Dan Stozac1879002014-05-22 15:59:05 -070028#include <ui/Rect.h>
Jamie Gennis134f0422011-03-08 12:18:54 -080029#include <utils/String8.h>
30
Brian Anderson3da8d272016-07-28 16:20:47 -070031#include <limits>
Kalle Raita643f0942016-12-07 09:20:14 -080032#include <thread>
33
Jamie Gennis134f0422011-03-08 12:18:54 -080034namespace android {
35
Kalle Raita643f0942016-12-07 09:20:14 -080036using namespace std::chrono_literals;
37
Brian Anderson3da8d272016-07-28 16:20:47 -070038class FakeSurfaceComposer;
39class FakeProducerFrameEventHistory;
40
41static constexpr uint64_t NO_FRAME_INDEX = std::numeric_limits<uint64_t>::max();
42
Jamie Gennis134f0422011-03-08 12:18:54 -080043class SurfaceTest : public ::testing::Test {
44protected:
Mathias Agopian7c1a4872013-03-20 15:56:04 -070045
46 SurfaceTest() {
47 ProcessState::self()->startThreadPool();
48 }
49
Jamie Gennis134f0422011-03-08 12:18:54 -080050 virtual void SetUp() {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080051 mComposerClient = new SurfaceComposerClient;
Jamie Gennis134f0422011-03-08 12:18:54 -080052 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
53
Jamie Gennisfc850122011-04-25 16:40:05 -070054 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070055 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis134f0422011-03-08 12:18:54 -080056
57 ASSERT_TRUE(mSurfaceControl != NULL);
58 ASSERT_TRUE(mSurfaceControl->isValid());
59
Mathias Agopian698c0872011-06-28 19:09:31 -070060 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian9303eee2011-07-01 15:27:27 -070061 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff));
Jamie Gennis134f0422011-03-08 12:18:54 -080062 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070063 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennis134f0422011-03-08 12:18:54 -080064
65 mSurface = mSurfaceControl->getSurface();
66 ASSERT_TRUE(mSurface != NULL);
67 }
68
69 virtual void TearDown() {
70 mComposerClient->dispose();
71 }
72
73 sp<Surface> mSurface;
74 sp<SurfaceComposerClient> mComposerClient;
75 sp<SurfaceControl> mSurfaceControl;
76};
77
78TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
79 sp<ANativeWindow> anw(mSurface);
80 int result = -123;
81 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
82 &result);
83 EXPECT_EQ(NO_ERROR, err);
84 EXPECT_EQ(1, result);
85}
86
87TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
88 mSurfaceControl.clear();
Kalle Raita643f0942016-12-07 09:20:14 -080089 // Wait for the async clean-up to complete.
90 std::this_thread::sleep_for(50ms);
Jamie Gennis134f0422011-03-08 12:18:54 -080091
92 sp<ANativeWindow> anw(mSurface);
93 int result = -123;
94 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
95 &result);
96 EXPECT_EQ(NO_ERROR, err);
97 EXPECT_EQ(1, result);
98}
99
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800100// This test probably doesn't belong here.
Jamie Gennisc901ca02011-10-11 16:02:31 -0700101TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800102 sp<ANativeWindow> anw(mSurface);
103
104 // Verify the screenshot works with no protected buffers.
Dan Stoza5603a2f2014-04-07 13:41:37 -0700105 sp<IGraphicBufferProducer> producer;
106 sp<IGraphicBufferConsumer> consumer;
107 BufferQueue::createBufferQueue(&producer, &consumer);
108 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800109 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Brian Anderson3da8d272016-07-28 16:20:47 -0700110 sp<IBinder> display(sf->getBuiltInDisplay(
111 ISurfaceComposer::eDisplayIdMain));
Dan Stozac1879002014-05-22 15:59:05 -0700112 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800113 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800114
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700115 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
116 NATIVE_WINDOW_API_CPU));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800117 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
118 // that we need to dequeue a buffer in order for it to actually get
119 // allocated in SurfaceFlinger.
120 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
121 GRALLOC_USAGE_PROTECTED));
122 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Iliyan Malchev697526b2011-05-01 11:33:26 -0700123 ANativeWindowBuffer* buf = 0;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700124
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700125 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700126 if (err) {
127 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
128 // that's okay as long as this is the reason for the failure.
129 // try again without the GRALLOC_USAGE_PROTECTED bit.
130 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700131 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
132 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700133 return;
134 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700135 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700136
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800137 for (int i = 0; i < 4; i++) {
138 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700139 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
140 &buf));
141 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800142 }
Dan Stozac1879002014-05-22 15:59:05 -0700143 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
Dan Stoza8d759962014-02-19 18:35:30 -0800144 64, 64, 0, 0x7fffffff, false));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800145}
146
Jamie Gennis391bbe22011-03-14 15:00:06 -0700147TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
148 sp<ANativeWindow> anw(mSurface);
149 int result = -123;
150 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
151 EXPECT_EQ(NO_ERROR, err);
152 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
153}
154
Craig Donner6ebc46a2016-10-21 15:23:44 -0700155TEST_F(SurfaceTest, LayerCountIsOne) {
156 sp<ANativeWindow> anw(mSurface);
157 int result = -123;
158 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
159 EXPECT_EQ(NO_ERROR, err);
160 EXPECT_EQ(1, result);
161}
162
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700163TEST_F(SurfaceTest, QueryConsumerUsage) {
164 const int TEST_USAGE_FLAGS =
165 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700166 sp<IGraphicBufferProducer> producer;
167 sp<IGraphicBufferConsumer> consumer;
168 BufferQueue::createBufferQueue(&producer, &consumer);
169 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700170 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700171 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700172
173 sp<ANativeWindow> anw(s);
174
175 int flags = -1;
176 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
177
178 ASSERT_EQ(NO_ERROR, err);
179 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
180}
181
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800182TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
183 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
184 sp<IGraphicBufferProducer> producer;
185 sp<IGraphicBufferConsumer> consumer;
186 BufferQueue::createBufferQueue(&producer, &consumer);
187 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
188
189 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
190
191 sp<Surface> s = new Surface(producer);
192
193 sp<ANativeWindow> anw(s);
194
195 android_dataspace dataSpace;
196
197 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
198 reinterpret_cast<int*>(&dataSpace));
199
200 ASSERT_EQ(NO_ERROR, err);
201 ASSERT_EQ(TEST_DATASPACE, dataSpace);
202}
203
Dan Stoza812ed062015-06-02 15:45:22 -0700204TEST_F(SurfaceTest, SettingGenerationNumber) {
205 sp<IGraphicBufferProducer> producer;
206 sp<IGraphicBufferConsumer> consumer;
207 BufferQueue::createBufferQueue(&producer, &consumer);
208 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
209 sp<Surface> surface = new Surface(producer);
210 sp<ANativeWindow> window(surface);
211
212 // Allocate a buffer with a generation number of 0
213 ANativeWindowBuffer* buffer;
214 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700215 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
216 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700217 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
218 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
219
220 // Detach the buffer and check its generation number
221 sp<GraphicBuffer> graphicBuffer;
222 sp<Fence> fence;
223 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
224 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
225
226 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
227 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
228
229 // This should change the generation number of the GraphicBuffer
230 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
231
232 // Check that the new generation number sticks with the buffer
233 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
234 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
235 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
236 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
237}
238
Dan Stozac6f30bd2015-06-08 09:32:50 -0700239TEST_F(SurfaceTest, GetConsumerName) {
240 sp<IGraphicBufferProducer> producer;
241 sp<IGraphicBufferConsumer> consumer;
242 BufferQueue::createBufferQueue(&producer, &consumer);
243
244 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
245 consumer->consumerConnect(dummyConsumer, false);
246 consumer->setConsumerName(String8("TestConsumer"));
247
248 sp<Surface> surface = new Surface(producer);
249 sp<ANativeWindow> window(surface);
250 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
251
252 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
253}
254
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800255TEST_F(SurfaceTest, DynamicSetBufferCount) {
256 sp<IGraphicBufferProducer> producer;
257 sp<IGraphicBufferConsumer> consumer;
258 BufferQueue::createBufferQueue(&producer, &consumer);
259
260 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
261 consumer->consumerConnect(dummyConsumer, false);
262 consumer->setConsumerName(String8("TestConsumer"));
263
264 sp<Surface> surface = new Surface(producer);
265 sp<ANativeWindow> window(surface);
266
267 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
268 NATIVE_WINDOW_API_CPU));
269 native_window_set_buffer_count(window.get(), 4);
270
271 int fence;
272 ANativeWindowBuffer* buffer;
273 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
274 native_window_set_buffer_count(window.get(), 3);
275 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
276 native_window_set_buffer_count(window.get(), 2);
277 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
278 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
279}
280
Brian Anderson3da8d272016-07-28 16:20:47 -0700281
282class FakeConsumer : public BnConsumerListener {
283public:
284 void onFrameAvailable(const BufferItem& /*item*/) override {}
285 void onBuffersReleased() override {}
286 void onSidebandStreamChanged() override {}
287
288 void addAndGetFrameTimestamps(
289 const NewFrameEventsEntry* newTimestamps,
290 FrameEventHistoryDelta* outDelta) override {
291 if (newTimestamps) {
292 if (mGetFrameTimestampsEnabled) {
293 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
294 "Test should set mNewFrameEntryOverride before queuing "
295 "a frame.";
296 EXPECT_EQ(newTimestamps->frameNumber,
297 mNewFrameEntryOverride.frameNumber) <<
298 "Test attempting to add NewFrameEntryOverride with "
299 "incorrect frame number.";
300 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
301 mNewFrameEntryOverride.frameNumber = 0;
302 }
303 mAddFrameTimestampsCount++;
304 mLastAddedFrameNumber = newTimestamps->frameNumber;
305 }
306 if (outDelta) {
307 mFrameEventHistory.getAndResetDelta(outDelta);
308 mGetFrameTimestampsCount++;
309 }
310 mAddAndGetFrameTimestampsCallCount++;
311 }
312
313 bool mGetFrameTimestampsEnabled = false;
314
315 ConsumerFrameEventHistory mFrameEventHistory;
316 int mAddAndGetFrameTimestampsCallCount = 0;
317 int mAddFrameTimestampsCount = 0;
318 int mGetFrameTimestampsCount = 0;
319 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
320
321 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
322};
323
324
325class FakeSurfaceComposer : public ISurfaceComposer{
326public:
327 ~FakeSurfaceComposer() override {}
328
329 void setSupportedTimestamps(bool supportsPresent, bool supportsRetire) {
330 mSupportsPresent = supportsPresent;
331 mSupportsRetire = supportsRetire;
332 }
333
334 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800335 sp<ISurfaceComposerClient> createScopedConnection(
336 const sp<IGraphicBufferProducer>& /* parent */) override {
337 return nullptr;
338 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700339 sp<IGraphicBufferAlloc> createGraphicBufferAlloc() override {
340 return nullptr;
341 }
342 sp<IDisplayEventConnection> createDisplayEventConnection() override {
343 return nullptr;
344 }
345 sp<IBinder> createDisplay(const String8& /*displayName*/,
346 bool /*secure*/) override { return nullptr; }
347 void destroyDisplay(const sp<IBinder>& /*display */) override {}
348 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
349 void setTransactionState(const Vector<ComposerState>& /*state*/,
350 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
351 override {}
352 void bootFinished() override {}
353 bool authenticateSurfaceTexture(
354 const sp<IGraphicBufferProducer>& /*surface*/) const override {
355 return false;
356 }
357
358 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
359 const override {
360 *outSupported = {
361 FrameEvent::REQUESTED_PRESENT,
362 FrameEvent::ACQUIRE,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700363 FrameEvent::LATCH,
Brian Anderson3da8d272016-07-28 16:20:47 -0700364 FrameEvent::FIRST_REFRESH_START,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700365 FrameEvent::LAST_REFRESH_START,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700366 FrameEvent::GPU_COMPOSITION_DONE,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700367 FrameEvent::DEQUEUE_READY,
Brian Anderson3da8d272016-07-28 16:20:47 -0700368 FrameEvent::RELEASE
369 };
370 if (mSupportsPresent) {
371 outSupported->push_back(
372 FrameEvent::DISPLAY_PRESENT);
373 }
374 if (mSupportsRetire) {
375 outSupported->push_back(
376 FrameEvent::DISPLAY_RETIRE);
377 }
378 return NO_ERROR;
379 }
380
381 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
382 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
383 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
384 status_t getDisplayStats(const sp<IBinder>& /*display*/,
385 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
386 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
387 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
388 override {
389 return NO_ERROR;
390 }
391 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
392 Vector<android_color_mode_t>* /*outColorModes*/) override {
393 return NO_ERROR;
394 }
395 android_color_mode_t getActiveColorMode(const sp<IBinder>& /*display*/)
396 override {
397 return HAL_COLOR_MODE_NATIVE;
398 }
399 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
400 android_color_mode_t /*colorMode*/) override { return NO_ERROR; }
401 status_t captureScreen(const sp<IBinder>& /*display*/,
402 const sp<IGraphicBufferProducer>& /*producer*/,
403 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800404 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700405 bool /*useIdentityTransform*/,
406 Rotation /*rotation*/) override { return NO_ERROR; }
407 status_t clearAnimationFrameStats() override { return NO_ERROR; }
408 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
409 return NO_ERROR;
410 }
411 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
412 HdrCapabilities* /*outCapabilities*/) const override {
413 return NO_ERROR;
414 }
415 status_t enableVSyncInjections(bool /*enable*/) override {
416 return NO_ERROR;
417 }
418 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
419
420protected:
421 IBinder* onAsBinder() override { return nullptr; }
422
423private:
424 bool mSupportsPresent{true};
425 bool mSupportsRetire{true};
426};
427
428class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
429public:
430 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
431 : mFenceMap(fenceMap) {}
432
433 ~FakeProducerFrameEventHistory() {}
434
435 void updateAcquireFence(uint64_t frameNumber,
436 std::shared_ptr<FenceTime>&& acquire) override {
437 // Verify the acquire fence being added isn't the one from the consumer.
438 EXPECT_NE(mConsumerAcquireFence, acquire);
439 // Override the fence, so we can verify this was called by the
440 // producer after the frame is queued.
441 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
442 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
443 }
444
445 void setAcquireFenceOverride(
446 const std::shared_ptr<FenceTime>& acquireFenceOverride,
447 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
448 mAcquireFenceOverride = acquireFenceOverride;
449 mConsumerAcquireFence = consumerAcquireFence;
450 }
451
452protected:
453 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
454 const override {
455 return mFenceMap->createFenceTimeForTest(fence);
456 }
457
458 FenceToFenceTimeMap* mFenceMap{nullptr};
459
460 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
461 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
462};
463
464
465class TestSurface : public Surface {
466public:
467 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
468 FenceToFenceTimeMap* fenceMap)
469 : Surface(bufferProducer),
470 mFakeSurfaceComposer(new FakeSurfaceComposer) {
471 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
472 mFrameEventHistory.reset(mFakeFrameEventHistory);
473 }
474
475 ~TestSurface() override {}
476
477 sp<ISurfaceComposer> composerService() const override {
478 return mFakeSurfaceComposer;
479 }
480
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800481 nsecs_t now() const override {
482 return mNow;
483 }
484
485 void setNow(nsecs_t now) {
486 mNow = now;
487 }
488
Brian Anderson3da8d272016-07-28 16:20:47 -0700489public:
490 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800491 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700492
493 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
494 // but this raw pointer gives access to test functionality.
495 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
496};
497
498
499class GetFrameTimestampsTest : public SurfaceTest {
500protected:
501 struct FenceAndFenceTime {
502 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
503 : mFence(new Fence),
504 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
505 sp<Fence> mFence { nullptr };
506 std::shared_ptr<FenceTime> mFenceTime { nullptr };
507 };
508
509 struct RefreshEvents {
510 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800511 : mFenceMap(fenceMap),
512 kCompositorTiming(
513 {refreshStart, refreshStart + 1, refreshStart + 2 }),
514 kStartTime(refreshStart + 3),
515 kGpuCompositionDoneTime(refreshStart + 4),
516 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700517
518 void signalPostCompositeFences() {
519 mFenceMap.signalAllForTest(
520 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
521 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
522 }
523
524 FenceToFenceTimeMap& mFenceMap;
525
526 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
527 FenceAndFenceTime mPresent { mFenceMap };
528
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800529 const CompositorTiming kCompositorTiming;
530
Brian Anderson3da8d272016-07-28 16:20:47 -0700531 const nsecs_t kStartTime;
532 const nsecs_t kGpuCompositionDoneTime;
533 const nsecs_t kPresentTime;
534 };
535
536 struct FrameEvents {
537 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
538 : mFenceMap(fenceMap),
539 kPostedTime(frameStartTime + 100),
540 kRequestedPresentTime(frameStartTime + 200),
541 kProducerAcquireTime(frameStartTime + 300),
542 kConsumerAcquireTime(frameStartTime + 301),
543 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700544 kDequeueReadyTime(frameStartTime + 600),
545 kRetireTime(frameStartTime + 700),
546 kReleaseTime(frameStartTime + 800),
Brian Anderson3da8d272016-07-28 16:20:47 -0700547 mRefreshes {
548 { mFenceMap, frameStartTime + 410 },
549 { mFenceMap, frameStartTime + 420 },
550 { mFenceMap, frameStartTime + 430 } } {}
551
552 void signalQueueFences() {
553 mFenceMap.signalAllForTest(
554 mAcquireConsumer.mFence, kConsumerAcquireTime);
555 mFenceMap.signalAllForTest(
556 mAcquireProducer.mFence, kProducerAcquireTime);
557 }
558
559 void signalRefreshFences() {
560 for (auto& re : mRefreshes) {
561 re.signalPostCompositeFences();
562 }
563 }
564
565 void signalReleaseFences() {
566 mFenceMap.signalAllForTest(mRetire.mFence, kRetireTime);
567 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
568 }
569
570 FenceToFenceTimeMap& mFenceMap;
571
572 FenceAndFenceTime mAcquireConsumer { mFenceMap };
573 FenceAndFenceTime mAcquireProducer { mFenceMap };
574 FenceAndFenceTime mRetire { mFenceMap };
575 FenceAndFenceTime mRelease { mFenceMap };
576
577 const nsecs_t kPostedTime;
578 const nsecs_t kRequestedPresentTime;
579 const nsecs_t kProducerAcquireTime;
580 const nsecs_t kConsumerAcquireTime;
581 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700582 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700583 const nsecs_t kRetireTime;
584 const nsecs_t kReleaseTime;
585
586 RefreshEvents mRefreshes[3];
587 };
588
589 GetFrameTimestampsTest() : SurfaceTest() {}
590
591 virtual void SetUp() {
592 SurfaceTest::SetUp();
593
594 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
595 mFakeConsumer = new FakeConsumer;
596 mCfeh = &mFakeConsumer->mFrameEventHistory;
597 mConsumer->consumerConnect(mFakeConsumer, false);
598 mConsumer->setConsumerName(String8("TestConsumer"));
599 mSurface = new TestSurface(mProducer, &mFenceMap);
600 mWindow = mSurface;
601
602 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
603 NATIVE_WINDOW_API_CPU));
604 native_window_set_buffer_count(mWindow.get(), 4);
605 }
606
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800607 void disableFrameTimestamps() {
608 mFakeConsumer->mGetFrameTimestampsEnabled = false;
609 native_window_enable_frame_timestamps(mWindow.get(), 0);
610 mFrameTimestampsEnabled = false;
611 }
612
Brian Anderson3da8d272016-07-28 16:20:47 -0700613 void enableFrameTimestamps() {
614 mFakeConsumer->mGetFrameTimestampsEnabled = true;
615 native_window_enable_frame_timestamps(mWindow.get(), 1);
616 mFrameTimestampsEnabled = true;
617 }
618
Brian Anderson1049d1d2016-12-16 17:25:57 -0800619 int getAllFrameTimestamps(uint64_t frameId) {
620 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700621 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
622 &outFirstRefreshStartTime, &outLastRefreshStartTime,
623 &outGpuCompositionDoneTime, &outDisplayPresentTime,
624 &outDisplayRetireTime, &outDequeueReadyTime, &outReleaseTime);
625 }
626
Brian Anderson3da8d272016-07-28 16:20:47 -0700627 void resetTimestamps() {
628 outRequestedPresentTime = -1;
629 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700630 outLatchTime = -1;
631 outFirstRefreshStartTime = -1;
632 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700633 outGpuCompositionDoneTime = -1;
634 outDisplayPresentTime = -1;
635 outDisplayRetireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700636 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700637 outReleaseTime = -1;
638 }
639
Brian Anderson1049d1d2016-12-16 17:25:57 -0800640 uint64_t getNextFrameId() {
641 uint64_t frameId = -1;
642 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
643 EXPECT_EQ(status, NO_ERROR);
644 return frameId;
645 }
646
Brian Anderson3da8d272016-07-28 16:20:47 -0700647 void dequeueAndQueue(uint64_t frameIndex) {
648 int fence = -1;
649 ANativeWindowBuffer* buffer = nullptr;
650 ASSERT_EQ(NO_ERROR,
651 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
652
653 int oldAddFrameTimestampsCount =
654 mFakeConsumer->mAddFrameTimestampsCount;
655
656 FrameEvents* frame = &mFrames[frameIndex];
657 uint64_t frameNumber = frameIndex + 1;
658
659 NewFrameEventsEntry fe;
660 fe.frameNumber = frameNumber;
661 fe.postedTime = frame->kPostedTime;
662 fe.requestedPresentTime = frame->kRequestedPresentTime;
663 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
664 mFakeConsumer->mNewFrameEntryOverride = fe;
665
666 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
667 frame->mAcquireProducer.mFenceTime,
668 frame->mAcquireConsumer.mFenceTime);
669
670 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
671
672 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
673
674 EXPECT_EQ(
675 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
676 mFakeConsumer->mAddFrameTimestampsCount);
677 }
678
679 void addFrameEvents(
680 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
681 FrameEvents* oldFrame =
682 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
683 FrameEvents* newFrame = &mFrames[iNewFrame];
684
685 uint64_t nOldFrame = iOldFrame + 1;
686 uint64_t nNewFrame = iNewFrame + 1;
687
688 // Latch, Composite, Retire, and Release the frames in a plausible
689 // order. Note: The timestamps won't necessarily match the order, but
690 // that's okay for the purposes of this test.
691 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
692
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700693 // Composite the previous frame one more time, which helps verify
694 // LastRefresh is updated properly.
695 if (oldFrame != nullptr) {
696 mCfeh->addPreComposition(nOldFrame,
697 oldFrame->mRefreshes[2].kStartTime);
698 gpuDoneFenceTime = gpuComposited ?
699 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
700 FenceTime::NO_FENCE;
701 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800702 oldFrame->mRefreshes[2].mPresent.mFenceTime,
703 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700704 }
705
706 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700707 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
708
709 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
710 gpuDoneFenceTime = gpuComposited ?
711 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
712 FenceTime::NO_FENCE;
713 // HWC2 releases the previous buffer after a new latch just before
714 // calling postComposition.
715 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700716 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700717 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
718 }
719 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800720 newFrame->mRefreshes[0].mPresent.mFenceTime,
721 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700722
723 // Retire the previous buffer just after compositing the new buffer.
724 if (oldFrame != nullptr) {
725 mCfeh->addRetire(nOldFrame, oldFrame->mRetire.mFenceTime);
726 }
727
728 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
729 gpuDoneFenceTime = gpuComposited ?
730 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
731 FenceTime::NO_FENCE;
732 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800733 newFrame->mRefreshes[1].mPresent.mFenceTime,
734 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700735 }
736
737 void QueryPresentRetireSupported(
738 bool displayPresentSupported, bool displayRetireSupported);
739 void PresentOrRetireUnsupportedNoSyncTest(
740 bool displayPresentSupported, bool displayRetireSupported);
741
742 sp<IGraphicBufferProducer> mProducer;
743 sp<IGraphicBufferConsumer> mConsumer;
744 sp<FakeConsumer> mFakeConsumer;
745 ConsumerFrameEventHistory* mCfeh;
746 sp<TestSurface> mSurface;
747 sp<ANativeWindow> mWindow;
748
749 FenceToFenceTimeMap mFenceMap;
750
751 bool mFrameTimestampsEnabled = false;
752
753 int64_t outRequestedPresentTime = -1;
754 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700755 int64_t outLatchTime = -1;
756 int64_t outFirstRefreshStartTime = -1;
757 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700758 int64_t outGpuCompositionDoneTime = -1;
759 int64_t outDisplayPresentTime = -1;
760 int64_t outDisplayRetireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700761 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700762 int64_t outReleaseTime = -1;
763
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800764 FrameEvents mFrames[3] {
765 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700766};
767
768
769// This test verifies that the frame timestamps are not retrieved when not
770// explicitly enabled via native_window_enable_frame_timestamps.
771// We want to check this to make sure there's no overhead for users
772// that don't need the timestamp information.
773TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
774 int fence;
775 ANativeWindowBuffer* buffer;
776
777 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
778 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
779
Brian Anderson1049d1d2016-12-16 17:25:57 -0800780 const uint64_t fId = getNextFrameId();
781
Brian Anderson3da8d272016-07-28 16:20:47 -0700782 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
783 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
784 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
785 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
786
787 // Verify the producer doesn't get frame timestamps piggybacked on queue.
788 // It is okay that frame timestamps are added in the consumer since it is
789 // still needed for SurfaceFlinger dumps.
790 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
791 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
792 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
793
794 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800795 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700796 EXPECT_EQ(INVALID_OPERATION, result);
797 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800798
799 // Verify compositor timing query fails.
800 nsecs_t compositeDeadline = 0;
801 nsecs_t compositeInterval = 0;
802 nsecs_t compositeToPresentLatency = 0;
803 result = native_window_get_compositor_timing(mWindow.get(),
804 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
805 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700806}
807
808// This test verifies that the frame timestamps are retrieved if explicitly
809// enabled via native_window_enable_frame_timestamps.
810TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800811 CompositorTiming initialCompositorTiming {
812 1000000000, // 1s deadline
813 16666667, // 16ms interval
814 50000000, // 50ms present latency
815 };
816 mCfeh->initializeCompositorTiming(initialCompositorTiming);
817
Brian Anderson3da8d272016-07-28 16:20:47 -0700818 enableFrameTimestamps();
819
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800820 // Verify the compositor timing query gets the initial compositor values
821 // after timststamps are enabled; even before the first frame is queued
822 // or dequeued.
823 nsecs_t compositeDeadline = 0;
824 nsecs_t compositeInterval = 0;
825 nsecs_t compositeToPresentLatency = 0;
826 mSurface->setNow(initialCompositorTiming.deadline - 1);
827 int result = native_window_get_compositor_timing(mWindow.get(),
828 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
829 EXPECT_EQ(NO_ERROR, result);
830 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
831 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
832 EXPECT_EQ(initialCompositorTiming.presentLatency,
833 compositeToPresentLatency);
834
Brian Anderson3da8d272016-07-28 16:20:47 -0700835 int fence;
836 ANativeWindowBuffer* buffer;
837
838 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800839 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700840
Brian Anderson1049d1d2016-12-16 17:25:57 -0800841 const uint64_t fId1 = getNextFrameId();
842
Brian Anderson3da8d272016-07-28 16:20:47 -0700843 // Verify getFrameTimestamps is piggybacked on dequeue.
844 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
845 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800846 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700847
848 NewFrameEventsEntry f1;
849 f1.frameNumber = 1;
850 f1.postedTime = mFrames[0].kPostedTime;
851 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
852 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
853 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
854 mFrames[0].mAcquireProducer.mFenceTime,
855 mFrames[0].mAcquireConsumer.mFenceTime);
856 mFakeConsumer->mNewFrameEntryOverride = f1;
857 mFrames[0].signalQueueFences();
858
859 // Verify getFrameTimestamps is piggybacked on queue.
860 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
861 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
862 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800863 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700864
865 // Verify queries for timestamps that the producer doesn't know about
866 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800867 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -0700868 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800869 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700870}
871
872void GetFrameTimestampsTest::QueryPresentRetireSupported(
873 bool displayPresentSupported, bool displayRetireSupported) {
874 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(
875 displayPresentSupported, displayRetireSupported);
876
877 // Verify supported bits are forwarded.
878 int supportsPresent = -1;
879 mWindow.get()->query(mWindow.get(),
880 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
881 EXPECT_EQ(displayPresentSupported, supportsPresent);
882
883 int supportsRetire = -1;
884 mWindow.get()->query(mWindow.get(),
885 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &supportsRetire);
886 EXPECT_EQ(displayRetireSupported, supportsRetire);
887}
888
889TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
890 QueryPresentRetireSupported(true, false);
891}
892
893TEST_F(GetFrameTimestampsTest, QueryRetireSupported) {
894 QueryPresentRetireSupported(false, true);
895}
896
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800897TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
898 nsecs_t phase = 4000;
899 nsecs_t interval = 1000;
900
901 // Timestamp in previous interval.
902 nsecs_t timestamp = 3500;
903 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
904 timestamp, phase, interval));
905
906 // Timestamp in next interval.
907 timestamp = 4500;
908 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
909 timestamp, phase, interval));
910
911 // Timestamp multiple intervals before.
912 timestamp = 2500;
913 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
914 timestamp, phase, interval));
915
916 // Timestamp multiple intervals after.
917 timestamp = 6500;
918 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
919 timestamp, phase, interval));
920
921 // Timestamp on previous interval.
922 timestamp = 3000;
923 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
924 timestamp, phase, interval));
925
926 // Timestamp on next interval.
927 timestamp = 5000;
928 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
929 timestamp, phase, interval));
930
931 // Timestamp equal to phase.
932 timestamp = 4000;
933 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
934 timestamp, phase, interval));
935}
936
937// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
938// if the number of intervals elapsed is internally stored in an int.
939TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
940 nsecs_t phase = 0;
941 nsecs_t interval = 4000;
942 nsecs_t big_timestamp = 8635916564000;
943 int32_t intervals = big_timestamp / interval;
944
945 EXPECT_LT(intervals, 0);
946 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
947 big_timestamp, phase, interval));
948 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
949 big_timestamp, big_timestamp, interval));
950}
951
952// This verifies the compositor timing is updated by refresh events
953// and piggy backed on a queue, dequeue, and enabling of timestamps..
954TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
955 CompositorTiming initialCompositorTiming {
956 1000000000, // 1s deadline
957 16666667, // 16ms interval
958 50000000, // 50ms present latency
959 };
960 mCfeh->initializeCompositorTiming(initialCompositorTiming);
961
962 enableFrameTimestamps();
963
964 // We get the initial values before any frames are submitted.
965 nsecs_t compositeDeadline = 0;
966 nsecs_t compositeInterval = 0;
967 nsecs_t compositeToPresentLatency = 0;
968 mSurface->setNow(initialCompositorTiming.deadline - 1);
969 int result = native_window_get_compositor_timing(mWindow.get(),
970 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
971 EXPECT_EQ(NO_ERROR, result);
972 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
973 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
974 EXPECT_EQ(initialCompositorTiming.presentLatency,
975 compositeToPresentLatency);
976
977 const uint64_t fId1 = getNextFrameId();
978 dequeueAndQueue(0);
979 addFrameEvents(true, NO_FRAME_INDEX, 0);
980
981 // Still get the initial values because the frame events for frame 0
982 // didn't get a chance to piggyback on a queue or dequeue yet.
983 result = native_window_get_compositor_timing(mWindow.get(),
984 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
985 EXPECT_EQ(NO_ERROR, result);
986 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
987 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
988 EXPECT_EQ(initialCompositorTiming.presentLatency,
989 compositeToPresentLatency);
990
991 const uint64_t fId2 = getNextFrameId();
992 dequeueAndQueue(1);
993 addFrameEvents(true, 0, 1);
994
995 // Now expect the composite values associated with frame 1.
996 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
997 result = native_window_get_compositor_timing(mWindow.get(),
998 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
999 EXPECT_EQ(NO_ERROR, result);
1000 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1001 compositeDeadline);
1002 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1003 compositeInterval);
1004 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1005 compositeToPresentLatency);
1006
1007 dequeueAndQueue(2);
1008 addFrameEvents(true, 1, 2);
1009
1010 // Now expect the composite values associated with frame 2.
1011 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1012 result = native_window_get_compositor_timing(mWindow.get(),
1013 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1014 EXPECT_EQ(NO_ERROR, result);
1015 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1016 compositeDeadline);
1017 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1018 compositeInterval);
1019 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1020 compositeToPresentLatency);
1021
1022 // Re-enabling frame timestamps should get the latest values.
1023 disableFrameTimestamps();
1024 enableFrameTimestamps();
1025
1026 // Now expect the composite values associated with frame 3.
1027 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1028 result = native_window_get_compositor_timing(mWindow.get(),
1029 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1030 EXPECT_EQ(NO_ERROR, result);
1031 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1032 compositeDeadline);
1033 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1034 compositeInterval);
1035 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1036 compositeToPresentLatency);
1037}
1038
1039// This verifies the compositor deadline properly snaps to the the next
1040// deadline based on the current time.
1041TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1042 CompositorTiming initialCompositorTiming {
1043 1000000000, // 1s deadline
1044 16666667, // 16ms interval
1045 50000000, // 50ms present latency
1046 };
1047 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1048
1049 enableFrameTimestamps();
1050
1051 nsecs_t compositeDeadline = 0;
1052 nsecs_t compositeInterval = 0;
1053 nsecs_t compositeToPresentLatency = 0;
1054
1055 // A "now" just before the deadline snaps to the deadline.
1056 mSurface->setNow(initialCompositorTiming.deadline - 1);
1057 int result = native_window_get_compositor_timing(mWindow.get(),
1058 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1059 EXPECT_EQ(NO_ERROR, result);
1060 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1061 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1062 EXPECT_EQ(expectedDeadline, compositeDeadline);
1063
1064 const uint64_t fId1 = getNextFrameId();
1065 dequeueAndQueue(0);
1066 addFrameEvents(true, NO_FRAME_INDEX, 0);
1067
1068 // A "now" just after the deadline snaps properly.
1069 mSurface->setNow(initialCompositorTiming.deadline + 1);
1070 result = native_window_get_compositor_timing(mWindow.get(),
1071 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1072 EXPECT_EQ(NO_ERROR, result);
1073 expectedDeadline =
1074 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1075 EXPECT_EQ(expectedDeadline, compositeDeadline);
1076
1077 const uint64_t fId2 = getNextFrameId();
1078 dequeueAndQueue(1);
1079 addFrameEvents(true, 0, 1);
1080
1081 // A "now" just after the next interval snaps properly.
1082 mSurface->setNow(
1083 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1084 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1085 result = native_window_get_compositor_timing(mWindow.get(),
1086 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1087 EXPECT_EQ(NO_ERROR, result);
1088 expectedDeadline =
1089 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1090 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1091 EXPECT_EQ(expectedDeadline, compositeDeadline);
1092
1093 dequeueAndQueue(2);
1094 addFrameEvents(true, 1, 2);
1095
1096 // A "now" over 1 interval before the deadline snaps properly.
1097 mSurface->setNow(
1098 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1099 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1100 result = native_window_get_compositor_timing(mWindow.get(),
1101 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1102 EXPECT_EQ(NO_ERROR, result);
1103 expectedDeadline =
1104 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1105 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1106 EXPECT_EQ(expectedDeadline, compositeDeadline);
1107
1108 // Re-enabling frame timestamps should get the latest values.
1109 disableFrameTimestamps();
1110 enableFrameTimestamps();
1111
1112 // A "now" over 2 intervals before the deadline snaps properly.
1113 mSurface->setNow(
1114 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1115 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1116 result = native_window_get_compositor_timing(mWindow.get(),
1117 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1118 EXPECT_EQ(NO_ERROR, result);
1119 expectedDeadline =
1120 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1121 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1122 EXPECT_EQ(expectedDeadline, compositeDeadline);
1123}
1124
Brian Anderson1049d1d2016-12-16 17:25:57 -08001125// This verifies the timestamps recorded in the consumer's
1126// FrameTimestampsHistory are properly retrieved by the producer for the
1127// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001128TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1129 enableFrameTimestamps();
1130
Brian Anderson1049d1d2016-12-16 17:25:57 -08001131 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001132 dequeueAndQueue(0);
1133 mFrames[0].signalQueueFences();
1134
Brian Anderson1049d1d2016-12-16 17:25:57 -08001135 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001136 dequeueAndQueue(1);
1137 mFrames[1].signalQueueFences();
1138
1139 addFrameEvents(true, NO_FRAME_INDEX, 0);
1140 mFrames[0].signalRefreshFences();
1141 addFrameEvents(true, 0, 1);
1142 mFrames[0].signalReleaseFences();
1143 mFrames[1].signalRefreshFences();
1144
1145 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001146 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001147 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001148 EXPECT_EQ(NO_ERROR, result);
1149 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1150 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001151 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1152 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1153 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001154 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1155 outGpuCompositionDoneTime);
1156 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1157 EXPECT_EQ(mFrames[0].kRetireTime, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001158 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001159 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1160
1161 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001162 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001163 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001164 EXPECT_EQ(NO_ERROR, result);
1165 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1166 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001167 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1168 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1169 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001170 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1171 outGpuCompositionDoneTime);
1172 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1173 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001174 EXPECT_EQ(0, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001175 EXPECT_EQ(0, outReleaseTime);
1176}
1177
1178// This test verifies the acquire fence recorded by the consumer is not sent
1179// back to the producer and the producer saves its own fence.
1180TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1181 enableFrameTimestamps();
1182 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1183
Brian Anderson3da8d272016-07-28 16:20:47 -07001184 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001185 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001186 dequeueAndQueue(0);
1187
1188 // Verify queue-related timestamps for f1 are available immediately in the
1189 // producer without asking the consumer again, even before signaling the
1190 // acquire fence.
1191 resetTimestamps();
1192 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001193 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001194 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001195 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001196 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1197 EXPECT_EQ(NO_ERROR, result);
1198 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1199 EXPECT_EQ(0, outAcquireTime);
1200
1201 // Signal acquire fences. Verify a sync call still isn't necessary.
1202 mFrames[0].signalQueueFences();
1203
1204 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001205 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001206 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001207 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001208 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1209 EXPECT_EQ(NO_ERROR, result);
1210 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1211 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1212
1213 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001214 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001215 dequeueAndQueue(1);
1216
1217 // Verify queue-related timestamps for f2 are available immediately in the
1218 // producer without asking the consumer again, even before signaling the
1219 // acquire fence.
1220 resetTimestamps();
1221 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001222 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001223 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001224 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001225 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1226 EXPECT_EQ(NO_ERROR, result);
1227 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1228 EXPECT_EQ(0, outAcquireTime);
1229
1230 // Signal acquire fences. Verify a sync call still isn't necessary.
1231 mFrames[1].signalQueueFences();
1232
1233 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001234 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001235 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001236 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001237 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1238 EXPECT_EQ(NO_ERROR, result);
1239 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1240 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1241}
1242
1243TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1244 enableFrameTimestamps();
1245 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1246
1247 // Dequeue and queue frame 1.
1248 dequeueAndQueue(0);
1249 mFrames[0].signalQueueFences();
1250
1251 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001252 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001253 dequeueAndQueue(1);
1254 mFrames[1].signalQueueFences();
1255
1256 addFrameEvents(true, NO_FRAME_INDEX, 0);
1257 mFrames[0].signalRefreshFences();
1258 addFrameEvents(true, 0, 1);
1259 mFrames[0].signalReleaseFences();
1260 mFrames[1].signalRefreshFences();
1261
1262 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001263 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001264 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001265 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1266 nullptr, nullptr, nullptr);
1267 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001268 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1269}
1270
1271// This test verifies that fences can signal and update timestamps producer
1272// side without an additional sync call to the consumer.
1273TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1274 enableFrameTimestamps();
1275 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1276
1277 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001278 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001279 dequeueAndQueue(0);
1280 mFrames[0].signalQueueFences();
1281
1282 // Dequeue and queue frame 2.
1283 dequeueAndQueue(1);
1284 mFrames[1].signalQueueFences();
1285
1286 addFrameEvents(true, NO_FRAME_INDEX, 0);
1287 addFrameEvents(true, 0, 1);
1288
1289 // Verify available timestamps are correct for frame 1, before any
1290 // fence has been signaled.
1291 // Note: A sync call is necessary here since the events triggered by
1292 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001293 resetTimestamps();
1294 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001295 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001296 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1297 EXPECT_EQ(NO_ERROR, result);
1298 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1299 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001300 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1301 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1302 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001303 EXPECT_EQ(0, outGpuCompositionDoneTime);
1304 EXPECT_EQ(0, outDisplayPresentTime);
1305 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001306 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001307 EXPECT_EQ(0, outReleaseTime);
1308
1309 // Verify available timestamps are correct for frame 1 again, before any
1310 // fence has been signaled.
1311 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001312 resetTimestamps();
1313 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001314 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001315 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1316 EXPECT_EQ(NO_ERROR, result);
1317 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1318 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001319 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1320 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1321 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001322 EXPECT_EQ(0, outGpuCompositionDoneTime);
1323 EXPECT_EQ(0, outDisplayPresentTime);
1324 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001325 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001326 EXPECT_EQ(0, outReleaseTime);
1327
1328 // Signal the fences for frame 1.
1329 mFrames[0].signalRefreshFences();
1330 mFrames[0].signalReleaseFences();
1331
1332 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001333 resetTimestamps();
1334 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001335 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001336 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1337 EXPECT_EQ(NO_ERROR, result);
1338 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1339 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001340 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1341 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1342 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001343 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1344 outGpuCompositionDoneTime);
1345 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1346 EXPECT_EQ(mFrames[0].kRetireTime, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001347 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001348 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1349}
1350
1351// This test verifies that if the frame wasn't GPU composited but has a refresh
1352// event a sync call isn't made to get the GPU composite done time since it will
1353// never exist.
1354TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1355 enableFrameTimestamps();
1356 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1357
Brian Anderson3da8d272016-07-28 16:20:47 -07001358 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001359 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001360 dequeueAndQueue(0);
1361 mFrames[0].signalQueueFences();
1362
1363 // Dequeue and queue frame 2.
1364 dequeueAndQueue(1);
1365 mFrames[1].signalQueueFences();
1366
1367 addFrameEvents(false, NO_FRAME_INDEX, 0);
1368 addFrameEvents(false, 0, 1);
1369
1370 // Verify available timestamps are correct for frame 1, before any
1371 // fence has been signaled.
1372 // Note: A sync call is necessary here since the events triggered by
1373 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1374 resetTimestamps();
1375 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001376 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001377 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1378 EXPECT_EQ(NO_ERROR, result);
1379 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1380 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001381 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1382 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1383 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001384 EXPECT_EQ(0, outGpuCompositionDoneTime);
1385 EXPECT_EQ(0, outDisplayPresentTime);
1386 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001387 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001388 EXPECT_EQ(0, outReleaseTime);
1389
1390 // Signal the fences for frame 1.
1391 mFrames[0].signalRefreshFences();
1392 mFrames[0].signalReleaseFences();
1393
1394 // Verify all timestamps, except GPU composition, are available without a
1395 // sync call.
1396 resetTimestamps();
1397 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001398 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001399 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1400 EXPECT_EQ(NO_ERROR, result);
1401 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1402 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001403 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1404 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1405 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001406 EXPECT_EQ(0, outGpuCompositionDoneTime);
1407 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1408 EXPECT_EQ(mFrames[0].kRetireTime, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001409 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001410 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1411}
1412
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001413// This test verifies that if the certain timestamps can't possibly exist for
1414// the most recent frame, then a sync call is not done.
Brian Anderson3da8d272016-07-28 16:20:47 -07001415TEST_F(GetFrameTimestampsTest, NoRetireOrReleaseNoSync) {
1416 enableFrameTimestamps();
1417 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1418
1419 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001420 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001421 dequeueAndQueue(0);
1422 mFrames[0].signalQueueFences();
1423
1424 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001425 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001426 dequeueAndQueue(1);
1427 mFrames[1].signalQueueFences();
1428
1429 addFrameEvents(false, NO_FRAME_INDEX, 0);
1430 addFrameEvents(false, 0, 1);
1431
1432 // Verify available timestamps are correct for frame 1, before any
1433 // fence has been signaled.
1434 // Note: A sync call is necessary here since the events triggered by
1435 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001436 resetTimestamps();
1437 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001438 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001439 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1440 EXPECT_EQ(NO_ERROR, result);
1441 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1442 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001443 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1444 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1445 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001446 EXPECT_EQ(0, outGpuCompositionDoneTime);
1447 EXPECT_EQ(0, outDisplayPresentTime);
1448 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001449 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001450 EXPECT_EQ(0, outReleaseTime);
1451
1452 mFrames[0].signalRefreshFences();
1453 mFrames[0].signalReleaseFences();
1454 mFrames[1].signalRefreshFences();
1455
Brian Anderson1049d1d2016-12-16 17:25:57 -08001456 // Verify querying for all timestmaps of f2 does not do a sync call. Even
1457 // though the lastRefresh, retire, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001458 // available, a sync call should not occur because it's not possible for f2
1459 // to encounter the final value for those events until another frame is
1460 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001461 resetTimestamps();
1462 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001463 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001464 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1465 EXPECT_EQ(NO_ERROR, result);
1466 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1467 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001468 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1469 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1470 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001471 EXPECT_EQ(0, outGpuCompositionDoneTime);
1472 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1473 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001474 EXPECT_EQ(0, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001475 EXPECT_EQ(0, outReleaseTime);
1476}
1477
1478// This test verifies there are no sync calls for present or retire times
1479// when they aren't supported and that an error is returned.
1480void GetFrameTimestampsTest::PresentOrRetireUnsupportedNoSyncTest(
1481 bool displayPresentSupported, bool displayRetireSupported) {
1482
1483 enableFrameTimestamps();
1484 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(
1485 displayPresentSupported, displayRetireSupported);
1486
1487 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001488 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001489 dequeueAndQueue(0);
1490
1491 // Verify a query for the Present and Retire times do not trigger
1492 // a sync call if they are not supported.
Brian Anderson3da8d272016-07-28 16:20:47 -07001493 resetTimestamps();
1494 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001495 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001496 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
Brian Anderson3da8d272016-07-28 16:20:47 -07001497 displayPresentSupported ? nullptr : &outDisplayPresentTime,
1498 displayRetireSupported ? nullptr : &outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001499 nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001500 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1501 EXPECT_EQ(BAD_VALUE, result);
1502 EXPECT_EQ(-1, outDisplayRetireTime);
1503 EXPECT_EQ(-1, outDisplayPresentTime);
1504}
1505
1506TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1507 PresentOrRetireUnsupportedNoSyncTest(false, true);
1508}
1509
1510TEST_F(GetFrameTimestampsTest, RetireUnsupportedNoSync) {
1511 PresentOrRetireUnsupportedNoSyncTest(true, false);
1512}
1513
Jamie Gennis134f0422011-03-08 12:18:54 -08001514}