blob: be48c11663d6928a82fda02942623aafc34bb726 [file] [log] [blame]
Jamie Gennis134f0422011-03-08 12:18:54 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stozac6f30bd2015-06-08 09:32:50 -070017#include "DummyConsumer.h"
18
Jamie Gennis134f0422011-03-08 12:18:54 -080019#include <gtest/gtest.h>
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080020
Brian Anderson3da8d272016-07-28 16:20:47 -070021#include <binder/ProcessState.h>
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -070022#include <cutils/properties.h>
23#include <gui/BufferItemConsumer.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070024#include <gui/IDisplayEventConnection.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080025#include <gui/ISurfaceComposer.h>
26#include <gui/Surface.h>
27#include <gui/SurfaceComposerClient.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070028#include <private/gui/ComposerService.h>
Dan Stozac1879002014-05-22 15:59:05 -070029#include <ui/Rect.h>
Jamie Gennis134f0422011-03-08 12:18:54 -080030#include <utils/String8.h>
31
Brian Anderson3da8d272016-07-28 16:20:47 -070032#include <limits>
Kalle Raita643f0942016-12-07 09:20:14 -080033#include <thread>
34
Jamie Gennis134f0422011-03-08 12:18:54 -080035namespace android {
36
Kalle Raita643f0942016-12-07 09:20:14 -080037using namespace std::chrono_literals;
38
Brian Anderson3da8d272016-07-28 16:20:47 -070039class FakeSurfaceComposer;
40class FakeProducerFrameEventHistory;
41
42static constexpr uint64_t NO_FRAME_INDEX = std::numeric_limits<uint64_t>::max();
43
Jamie Gennis134f0422011-03-08 12:18:54 -080044class SurfaceTest : public ::testing::Test {
45protected:
Mathias Agopian7c1a4872013-03-20 15:56:04 -070046
47 SurfaceTest() {
48 ProcessState::self()->startThreadPool();
49 }
50
Jamie Gennis134f0422011-03-08 12:18:54 -080051 virtual void SetUp() {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080052 mComposerClient = new SurfaceComposerClient;
Jamie Gennis134f0422011-03-08 12:18:54 -080053 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
54
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
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700256TEST_F(SurfaceTest, GetWideColorSupport) {
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 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
268
269 bool supported;
270 surface->getWideColorSupport(&supported);
271
272 // TODO(courtneygo): How can we know what device we are on to
273 // verify that this is correct?
274 char product[PROPERTY_VALUE_MAX] = "0";
275 property_get("ro.build.product", product, "0");
276 std::cerr << "[ ] product = " << product << std::endl;
277
278 if (strcmp("marlin", product) == 0 || strcmp("sailfish", product) == 0) {
279 ASSERT_EQ(true, supported);
280 } else {
281 ASSERT_EQ(false, supported);
282 }
283}
284
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800285TEST_F(SurfaceTest, DynamicSetBufferCount) {
286 sp<IGraphicBufferProducer> producer;
287 sp<IGraphicBufferConsumer> consumer;
288 BufferQueue::createBufferQueue(&producer, &consumer);
289
290 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
291 consumer->consumerConnect(dummyConsumer, false);
292 consumer->setConsumerName(String8("TestConsumer"));
293
294 sp<Surface> surface = new Surface(producer);
295 sp<ANativeWindow> window(surface);
296
297 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
298 NATIVE_WINDOW_API_CPU));
299 native_window_set_buffer_count(window.get(), 4);
300
301 int fence;
302 ANativeWindowBuffer* buffer;
303 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
304 native_window_set_buffer_count(window.get(), 3);
305 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
306 native_window_set_buffer_count(window.get(), 2);
307 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
308 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
309}
310
Brian Anderson3da8d272016-07-28 16:20:47 -0700311
312class FakeConsumer : public BnConsumerListener {
313public:
314 void onFrameAvailable(const BufferItem& /*item*/) override {}
315 void onBuffersReleased() override {}
316 void onSidebandStreamChanged() override {}
317
318 void addAndGetFrameTimestamps(
319 const NewFrameEventsEntry* newTimestamps,
320 FrameEventHistoryDelta* outDelta) override {
321 if (newTimestamps) {
322 if (mGetFrameTimestampsEnabled) {
323 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
324 "Test should set mNewFrameEntryOverride before queuing "
325 "a frame.";
326 EXPECT_EQ(newTimestamps->frameNumber,
327 mNewFrameEntryOverride.frameNumber) <<
328 "Test attempting to add NewFrameEntryOverride with "
329 "incorrect frame number.";
330 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
331 mNewFrameEntryOverride.frameNumber = 0;
332 }
333 mAddFrameTimestampsCount++;
334 mLastAddedFrameNumber = newTimestamps->frameNumber;
335 }
336 if (outDelta) {
337 mFrameEventHistory.getAndResetDelta(outDelta);
338 mGetFrameTimestampsCount++;
339 }
340 mAddAndGetFrameTimestampsCallCount++;
341 }
342
343 bool mGetFrameTimestampsEnabled = false;
344
345 ConsumerFrameEventHistory mFrameEventHistory;
346 int mAddAndGetFrameTimestampsCallCount = 0;
347 int mAddFrameTimestampsCount = 0;
348 int mGetFrameTimestampsCount = 0;
349 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
350
351 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
352};
353
354
355class FakeSurfaceComposer : public ISurfaceComposer{
356public:
357 ~FakeSurfaceComposer() override {}
358
359 void setSupportedTimestamps(bool supportsPresent, bool supportsRetire) {
360 mSupportsPresent = supportsPresent;
361 mSupportsRetire = supportsRetire;
362 }
363
364 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Robert Carr1db73f62016-12-21 12:58:51 -0800365 sp<ISurfaceComposerClient> createScopedConnection(
366 const sp<IGraphicBufferProducer>& /* parent */) override {
367 return nullptr;
368 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700369 sp<IDisplayEventConnection> createDisplayEventConnection() override {
370 return nullptr;
371 }
372 sp<IBinder> createDisplay(const String8& /*displayName*/,
373 bool /*secure*/) override { return nullptr; }
374 void destroyDisplay(const sp<IBinder>& /*display */) override {}
375 sp<IBinder> getBuiltInDisplay(int32_t /*id*/) override { return nullptr; }
376 void setTransactionState(const Vector<ComposerState>& /*state*/,
377 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/)
378 override {}
379 void bootFinished() override {}
380 bool authenticateSurfaceTexture(
381 const sp<IGraphicBufferProducer>& /*surface*/) const override {
382 return false;
383 }
384
385 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
386 const override {
387 *outSupported = {
388 FrameEvent::REQUESTED_PRESENT,
389 FrameEvent::ACQUIRE,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700390 FrameEvent::LATCH,
Brian Anderson3da8d272016-07-28 16:20:47 -0700391 FrameEvent::FIRST_REFRESH_START,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700392 FrameEvent::LAST_REFRESH_START,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700393 FrameEvent::GPU_COMPOSITION_DONE,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700394 FrameEvent::DEQUEUE_READY,
Brian Anderson3da8d272016-07-28 16:20:47 -0700395 FrameEvent::RELEASE
396 };
397 if (mSupportsPresent) {
398 outSupported->push_back(
399 FrameEvent::DISPLAY_PRESENT);
400 }
401 if (mSupportsRetire) {
402 outSupported->push_back(
403 FrameEvent::DISPLAY_RETIRE);
404 }
405 return NO_ERROR;
406 }
407
408 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
409 status_t getDisplayConfigs(const sp<IBinder>& /*display*/,
410 Vector<DisplayInfo>* /*configs*/) override { return NO_ERROR; }
411 status_t getDisplayStats(const sp<IBinder>& /*display*/,
412 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
413 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
414 status_t setActiveConfig(const sp<IBinder>& /*display*/, int /*id*/)
415 override {
416 return NO_ERROR;
417 }
418 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
419 Vector<android_color_mode_t>* /*outColorModes*/) override {
420 return NO_ERROR;
421 }
422 android_color_mode_t getActiveColorMode(const sp<IBinder>& /*display*/)
423 override {
424 return HAL_COLOR_MODE_NATIVE;
425 }
426 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
427 android_color_mode_t /*colorMode*/) override { return NO_ERROR; }
428 status_t captureScreen(const sp<IBinder>& /*display*/,
429 const sp<IGraphicBufferProducer>& /*producer*/,
430 Rect /*sourceCrop*/, uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
Robert Carrae060832016-11-28 10:51:00 -0800431 int32_t /*minLayerZ*/, int32_t /*maxLayerZ*/,
Brian Anderson3da8d272016-07-28 16:20:47 -0700432 bool /*useIdentityTransform*/,
433 Rotation /*rotation*/) override { return NO_ERROR; }
434 status_t clearAnimationFrameStats() override { return NO_ERROR; }
435 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
436 return NO_ERROR;
437 }
438 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
439 HdrCapabilities* /*outCapabilities*/) const override {
440 return NO_ERROR;
441 }
442 status_t enableVSyncInjections(bool /*enable*/) override {
443 return NO_ERROR;
444 }
445 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
446
447protected:
448 IBinder* onAsBinder() override { return nullptr; }
449
450private:
451 bool mSupportsPresent{true};
452 bool mSupportsRetire{true};
453};
454
455class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
456public:
457 FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap)
458 : mFenceMap(fenceMap) {}
459
460 ~FakeProducerFrameEventHistory() {}
461
462 void updateAcquireFence(uint64_t frameNumber,
463 std::shared_ptr<FenceTime>&& acquire) override {
464 // Verify the acquire fence being added isn't the one from the consumer.
465 EXPECT_NE(mConsumerAcquireFence, acquire);
466 // Override the fence, so we can verify this was called by the
467 // producer after the frame is queued.
468 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
469 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
470 }
471
472 void setAcquireFenceOverride(
473 const std::shared_ptr<FenceTime>& acquireFenceOverride,
474 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
475 mAcquireFenceOverride = acquireFenceOverride;
476 mConsumerAcquireFence = consumerAcquireFence;
477 }
478
479protected:
480 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
481 const override {
482 return mFenceMap->createFenceTimeForTest(fence);
483 }
484
485 FenceToFenceTimeMap* mFenceMap{nullptr};
486
487 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
488 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
489};
490
491
492class TestSurface : public Surface {
493public:
494 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
495 FenceToFenceTimeMap* fenceMap)
496 : Surface(bufferProducer),
497 mFakeSurfaceComposer(new FakeSurfaceComposer) {
498 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
499 mFrameEventHistory.reset(mFakeFrameEventHistory);
500 }
501
502 ~TestSurface() override {}
503
504 sp<ISurfaceComposer> composerService() const override {
505 return mFakeSurfaceComposer;
506 }
507
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800508 nsecs_t now() const override {
509 return mNow;
510 }
511
512 void setNow(nsecs_t now) {
513 mNow = now;
514 }
515
Brian Anderson3da8d272016-07-28 16:20:47 -0700516public:
517 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800518 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700519
520 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
521 // but this raw pointer gives access to test functionality.
522 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
523};
524
525
526class GetFrameTimestampsTest : public SurfaceTest {
527protected:
528 struct FenceAndFenceTime {
529 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
530 : mFence(new Fence),
531 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
532 sp<Fence> mFence { nullptr };
533 std::shared_ptr<FenceTime> mFenceTime { nullptr };
534 };
535
536 struct RefreshEvents {
537 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800538 : mFenceMap(fenceMap),
539 kCompositorTiming(
540 {refreshStart, refreshStart + 1, refreshStart + 2 }),
541 kStartTime(refreshStart + 3),
542 kGpuCompositionDoneTime(refreshStart + 4),
543 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700544
545 void signalPostCompositeFences() {
546 mFenceMap.signalAllForTest(
547 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
548 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
549 }
550
551 FenceToFenceTimeMap& mFenceMap;
552
553 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
554 FenceAndFenceTime mPresent { mFenceMap };
555
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800556 const CompositorTiming kCompositorTiming;
557
Brian Anderson3da8d272016-07-28 16:20:47 -0700558 const nsecs_t kStartTime;
559 const nsecs_t kGpuCompositionDoneTime;
560 const nsecs_t kPresentTime;
561 };
562
563 struct FrameEvents {
564 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
565 : mFenceMap(fenceMap),
566 kPostedTime(frameStartTime + 100),
567 kRequestedPresentTime(frameStartTime + 200),
568 kProducerAcquireTime(frameStartTime + 300),
569 kConsumerAcquireTime(frameStartTime + 301),
570 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -0700571 kDequeueReadyTime(frameStartTime + 600),
572 kRetireTime(frameStartTime + 700),
573 kReleaseTime(frameStartTime + 800),
Brian Anderson3da8d272016-07-28 16:20:47 -0700574 mRefreshes {
575 { mFenceMap, frameStartTime + 410 },
576 { mFenceMap, frameStartTime + 420 },
577 { mFenceMap, frameStartTime + 430 } } {}
578
579 void signalQueueFences() {
580 mFenceMap.signalAllForTest(
581 mAcquireConsumer.mFence, kConsumerAcquireTime);
582 mFenceMap.signalAllForTest(
583 mAcquireProducer.mFence, kProducerAcquireTime);
584 }
585
586 void signalRefreshFences() {
587 for (auto& re : mRefreshes) {
588 re.signalPostCompositeFences();
589 }
590 }
591
592 void signalReleaseFences() {
593 mFenceMap.signalAllForTest(mRetire.mFence, kRetireTime);
594 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
595 }
596
597 FenceToFenceTimeMap& mFenceMap;
598
599 FenceAndFenceTime mAcquireConsumer { mFenceMap };
600 FenceAndFenceTime mAcquireProducer { mFenceMap };
601 FenceAndFenceTime mRetire { mFenceMap };
602 FenceAndFenceTime mRelease { mFenceMap };
603
604 const nsecs_t kPostedTime;
605 const nsecs_t kRequestedPresentTime;
606 const nsecs_t kProducerAcquireTime;
607 const nsecs_t kConsumerAcquireTime;
608 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -0700609 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -0700610 const nsecs_t kRetireTime;
611 const nsecs_t kReleaseTime;
612
613 RefreshEvents mRefreshes[3];
614 };
615
616 GetFrameTimestampsTest() : SurfaceTest() {}
617
618 virtual void SetUp() {
619 SurfaceTest::SetUp();
620
621 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
622 mFakeConsumer = new FakeConsumer;
623 mCfeh = &mFakeConsumer->mFrameEventHistory;
624 mConsumer->consumerConnect(mFakeConsumer, false);
625 mConsumer->setConsumerName(String8("TestConsumer"));
626 mSurface = new TestSurface(mProducer, &mFenceMap);
627 mWindow = mSurface;
628
629 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
630 NATIVE_WINDOW_API_CPU));
631 native_window_set_buffer_count(mWindow.get(), 4);
632 }
633
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800634 void disableFrameTimestamps() {
635 mFakeConsumer->mGetFrameTimestampsEnabled = false;
636 native_window_enable_frame_timestamps(mWindow.get(), 0);
637 mFrameTimestampsEnabled = false;
638 }
639
Brian Anderson3da8d272016-07-28 16:20:47 -0700640 void enableFrameTimestamps() {
641 mFakeConsumer->mGetFrameTimestampsEnabled = true;
642 native_window_enable_frame_timestamps(mWindow.get(), 1);
643 mFrameTimestampsEnabled = true;
644 }
645
Brian Anderson1049d1d2016-12-16 17:25:57 -0800646 int getAllFrameTimestamps(uint64_t frameId) {
647 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700648 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
649 &outFirstRefreshStartTime, &outLastRefreshStartTime,
650 &outGpuCompositionDoneTime, &outDisplayPresentTime,
651 &outDisplayRetireTime, &outDequeueReadyTime, &outReleaseTime);
652 }
653
Brian Anderson3da8d272016-07-28 16:20:47 -0700654 void resetTimestamps() {
655 outRequestedPresentTime = -1;
656 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700657 outLatchTime = -1;
658 outFirstRefreshStartTime = -1;
659 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700660 outGpuCompositionDoneTime = -1;
661 outDisplayPresentTime = -1;
662 outDisplayRetireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700663 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700664 outReleaseTime = -1;
665 }
666
Brian Anderson1049d1d2016-12-16 17:25:57 -0800667 uint64_t getNextFrameId() {
668 uint64_t frameId = -1;
669 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
670 EXPECT_EQ(status, NO_ERROR);
671 return frameId;
672 }
673
Brian Anderson3da8d272016-07-28 16:20:47 -0700674 void dequeueAndQueue(uint64_t frameIndex) {
675 int fence = -1;
676 ANativeWindowBuffer* buffer = nullptr;
677 ASSERT_EQ(NO_ERROR,
678 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
679
680 int oldAddFrameTimestampsCount =
681 mFakeConsumer->mAddFrameTimestampsCount;
682
683 FrameEvents* frame = &mFrames[frameIndex];
684 uint64_t frameNumber = frameIndex + 1;
685
686 NewFrameEventsEntry fe;
687 fe.frameNumber = frameNumber;
688 fe.postedTime = frame->kPostedTime;
689 fe.requestedPresentTime = frame->kRequestedPresentTime;
690 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
691 mFakeConsumer->mNewFrameEntryOverride = fe;
692
693 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
694 frame->mAcquireProducer.mFenceTime,
695 frame->mAcquireConsumer.mFenceTime);
696
697 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
698
699 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
700
701 EXPECT_EQ(
702 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
703 mFakeConsumer->mAddFrameTimestampsCount);
704 }
705
706 void addFrameEvents(
707 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
708 FrameEvents* oldFrame =
709 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
710 FrameEvents* newFrame = &mFrames[iNewFrame];
711
712 uint64_t nOldFrame = iOldFrame + 1;
713 uint64_t nNewFrame = iNewFrame + 1;
714
715 // Latch, Composite, Retire, and Release the frames in a plausible
716 // order. Note: The timestamps won't necessarily match the order, but
717 // that's okay for the purposes of this test.
718 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
719
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700720 // Composite the previous frame one more time, which helps verify
721 // LastRefresh is updated properly.
722 if (oldFrame != nullptr) {
723 mCfeh->addPreComposition(nOldFrame,
724 oldFrame->mRefreshes[2].kStartTime);
725 gpuDoneFenceTime = gpuComposited ?
726 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
727 FenceTime::NO_FENCE;
728 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800729 oldFrame->mRefreshes[2].mPresent.mFenceTime,
730 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700731 }
732
733 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -0700734 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
735
736 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
737 gpuDoneFenceTime = gpuComposited ?
738 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
739 FenceTime::NO_FENCE;
740 // HWC2 releases the previous buffer after a new latch just before
741 // calling postComposition.
742 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -0700743 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -0700744 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
745 }
746 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800747 newFrame->mRefreshes[0].mPresent.mFenceTime,
748 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700749
750 // Retire the previous buffer just after compositing the new buffer.
751 if (oldFrame != nullptr) {
752 mCfeh->addRetire(nOldFrame, oldFrame->mRetire.mFenceTime);
753 }
754
755 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
756 gpuDoneFenceTime = gpuComposited ?
757 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
758 FenceTime::NO_FENCE;
759 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800760 newFrame->mRefreshes[1].mPresent.mFenceTime,
761 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -0700762 }
763
764 void QueryPresentRetireSupported(
765 bool displayPresentSupported, bool displayRetireSupported);
766 void PresentOrRetireUnsupportedNoSyncTest(
767 bool displayPresentSupported, bool displayRetireSupported);
768
769 sp<IGraphicBufferProducer> mProducer;
770 sp<IGraphicBufferConsumer> mConsumer;
771 sp<FakeConsumer> mFakeConsumer;
772 ConsumerFrameEventHistory* mCfeh;
773 sp<TestSurface> mSurface;
774 sp<ANativeWindow> mWindow;
775
776 FenceToFenceTimeMap mFenceMap;
777
778 bool mFrameTimestampsEnabled = false;
779
780 int64_t outRequestedPresentTime = -1;
781 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700782 int64_t outLatchTime = -1;
783 int64_t outFirstRefreshStartTime = -1;
784 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700785 int64_t outGpuCompositionDoneTime = -1;
786 int64_t outDisplayPresentTime = -1;
787 int64_t outDisplayRetireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700788 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -0700789 int64_t outReleaseTime = -1;
790
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800791 FrameEvents mFrames[3] {
792 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -0700793};
794
795
796// This test verifies that the frame timestamps are not retrieved when not
797// explicitly enabled via native_window_enable_frame_timestamps.
798// We want to check this to make sure there's no overhead for users
799// that don't need the timestamp information.
800TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
801 int fence;
802 ANativeWindowBuffer* buffer;
803
804 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
805 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
806
Brian Anderson1049d1d2016-12-16 17:25:57 -0800807 const uint64_t fId = getNextFrameId();
808
Brian Anderson3da8d272016-07-28 16:20:47 -0700809 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
810 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
811 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
812 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
813
814 // Verify the producer doesn't get frame timestamps piggybacked on queue.
815 // It is okay that frame timestamps are added in the consumer since it is
816 // still needed for SurfaceFlinger dumps.
817 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
818 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
819 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
820
821 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -0800822 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -0700823 EXPECT_EQ(INVALID_OPERATION, result);
824 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800825
826 // Verify compositor timing query fails.
827 nsecs_t compositeDeadline = 0;
828 nsecs_t compositeInterval = 0;
829 nsecs_t compositeToPresentLatency = 0;
830 result = native_window_get_compositor_timing(mWindow.get(),
831 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
832 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -0700833}
834
835// This test verifies that the frame timestamps are retrieved if explicitly
836// enabled via native_window_enable_frame_timestamps.
837TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800838 CompositorTiming initialCompositorTiming {
839 1000000000, // 1s deadline
840 16666667, // 16ms interval
841 50000000, // 50ms present latency
842 };
843 mCfeh->initializeCompositorTiming(initialCompositorTiming);
844
Brian Anderson3da8d272016-07-28 16:20:47 -0700845 enableFrameTimestamps();
846
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800847 // Verify the compositor timing query gets the initial compositor values
848 // after timststamps are enabled; even before the first frame is queued
849 // or dequeued.
850 nsecs_t compositeDeadline = 0;
851 nsecs_t compositeInterval = 0;
852 nsecs_t compositeToPresentLatency = 0;
853 mSurface->setNow(initialCompositorTiming.deadline - 1);
854 int result = native_window_get_compositor_timing(mWindow.get(),
855 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
856 EXPECT_EQ(NO_ERROR, result);
857 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
858 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
859 EXPECT_EQ(initialCompositorTiming.presentLatency,
860 compositeToPresentLatency);
861
Brian Anderson3da8d272016-07-28 16:20:47 -0700862 int fence;
863 ANativeWindowBuffer* buffer;
864
865 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800866 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700867
Brian Anderson1049d1d2016-12-16 17:25:57 -0800868 const uint64_t fId1 = getNextFrameId();
869
Brian Anderson3da8d272016-07-28 16:20:47 -0700870 // Verify getFrameTimestamps is piggybacked on dequeue.
871 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
872 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800873 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700874
875 NewFrameEventsEntry f1;
876 f1.frameNumber = 1;
877 f1.postedTime = mFrames[0].kPostedTime;
878 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
879 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
880 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
881 mFrames[0].mAcquireProducer.mFenceTime,
882 mFrames[0].mAcquireConsumer.mFenceTime);
883 mFakeConsumer->mNewFrameEntryOverride = f1;
884 mFrames[0].signalQueueFences();
885
886 // Verify getFrameTimestamps is piggybacked on queue.
887 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
888 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
889 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800890 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700891
892 // Verify queries for timestamps that the producer doesn't know about
893 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800894 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -0700895 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800896 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -0700897}
898
899void GetFrameTimestampsTest::QueryPresentRetireSupported(
900 bool displayPresentSupported, bool displayRetireSupported) {
901 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(
902 displayPresentSupported, displayRetireSupported);
903
904 // Verify supported bits are forwarded.
905 int supportsPresent = -1;
906 mWindow.get()->query(mWindow.get(),
907 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
908 EXPECT_EQ(displayPresentSupported, supportsPresent);
909
910 int supportsRetire = -1;
911 mWindow.get()->query(mWindow.get(),
912 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &supportsRetire);
913 EXPECT_EQ(displayRetireSupported, supportsRetire);
914}
915
916TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
917 QueryPresentRetireSupported(true, false);
918}
919
920TEST_F(GetFrameTimestampsTest, QueryRetireSupported) {
921 QueryPresentRetireSupported(false, true);
922}
923
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800924TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
925 nsecs_t phase = 4000;
926 nsecs_t interval = 1000;
927
928 // Timestamp in previous interval.
929 nsecs_t timestamp = 3500;
930 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
931 timestamp, phase, interval));
932
933 // Timestamp in next interval.
934 timestamp = 4500;
935 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
936 timestamp, phase, interval));
937
938 // Timestamp multiple intervals before.
939 timestamp = 2500;
940 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
941 timestamp, phase, interval));
942
943 // Timestamp multiple intervals after.
944 timestamp = 6500;
945 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
946 timestamp, phase, interval));
947
948 // Timestamp on previous interval.
949 timestamp = 3000;
950 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
951 timestamp, phase, interval));
952
953 // Timestamp on next interval.
954 timestamp = 5000;
955 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
956 timestamp, phase, interval));
957
958 // Timestamp equal to phase.
959 timestamp = 4000;
960 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
961 timestamp, phase, interval));
962}
963
964// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
965// if the number of intervals elapsed is internally stored in an int.
966TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
967 nsecs_t phase = 0;
968 nsecs_t interval = 4000;
969 nsecs_t big_timestamp = 8635916564000;
970 int32_t intervals = big_timestamp / interval;
971
972 EXPECT_LT(intervals, 0);
973 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
974 big_timestamp, phase, interval));
975 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
976 big_timestamp, big_timestamp, interval));
977}
978
979// This verifies the compositor timing is updated by refresh events
980// and piggy backed on a queue, dequeue, and enabling of timestamps..
981TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
982 CompositorTiming initialCompositorTiming {
983 1000000000, // 1s deadline
984 16666667, // 16ms interval
985 50000000, // 50ms present latency
986 };
987 mCfeh->initializeCompositorTiming(initialCompositorTiming);
988
989 enableFrameTimestamps();
990
991 // We get the initial values before any frames are submitted.
992 nsecs_t compositeDeadline = 0;
993 nsecs_t compositeInterval = 0;
994 nsecs_t compositeToPresentLatency = 0;
995 mSurface->setNow(initialCompositorTiming.deadline - 1);
996 int result = native_window_get_compositor_timing(mWindow.get(),
997 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
998 EXPECT_EQ(NO_ERROR, result);
999 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1000 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1001 EXPECT_EQ(initialCompositorTiming.presentLatency,
1002 compositeToPresentLatency);
1003
1004 const uint64_t fId1 = getNextFrameId();
1005 dequeueAndQueue(0);
1006 addFrameEvents(true, NO_FRAME_INDEX, 0);
1007
1008 // Still get the initial values because the frame events for frame 0
1009 // didn't get a chance to piggyback on a queue or dequeue yet.
1010 result = native_window_get_compositor_timing(mWindow.get(),
1011 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1012 EXPECT_EQ(NO_ERROR, result);
1013 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1014 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1015 EXPECT_EQ(initialCompositorTiming.presentLatency,
1016 compositeToPresentLatency);
1017
1018 const uint64_t fId2 = getNextFrameId();
1019 dequeueAndQueue(1);
1020 addFrameEvents(true, 0, 1);
1021
1022 // Now expect the composite values associated with frame 1.
1023 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1024 result = native_window_get_compositor_timing(mWindow.get(),
1025 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1026 EXPECT_EQ(NO_ERROR, result);
1027 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1028 compositeDeadline);
1029 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1030 compositeInterval);
1031 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1032 compositeToPresentLatency);
1033
1034 dequeueAndQueue(2);
1035 addFrameEvents(true, 1, 2);
1036
1037 // Now expect the composite values associated with frame 2.
1038 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1039 result = native_window_get_compositor_timing(mWindow.get(),
1040 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1041 EXPECT_EQ(NO_ERROR, result);
1042 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1043 compositeDeadline);
1044 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1045 compositeInterval);
1046 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1047 compositeToPresentLatency);
1048
1049 // Re-enabling frame timestamps should get the latest values.
1050 disableFrameTimestamps();
1051 enableFrameTimestamps();
1052
1053 // Now expect the composite values associated with frame 3.
1054 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1055 result = native_window_get_compositor_timing(mWindow.get(),
1056 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1057 EXPECT_EQ(NO_ERROR, result);
1058 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1059 compositeDeadline);
1060 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1061 compositeInterval);
1062 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1063 compositeToPresentLatency);
1064}
1065
1066// This verifies the compositor deadline properly snaps to the the next
1067// deadline based on the current time.
1068TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1069 CompositorTiming initialCompositorTiming {
1070 1000000000, // 1s deadline
1071 16666667, // 16ms interval
1072 50000000, // 50ms present latency
1073 };
1074 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1075
1076 enableFrameTimestamps();
1077
1078 nsecs_t compositeDeadline = 0;
1079 nsecs_t compositeInterval = 0;
1080 nsecs_t compositeToPresentLatency = 0;
1081
1082 // A "now" just before the deadline snaps to the deadline.
1083 mSurface->setNow(initialCompositorTiming.deadline - 1);
1084 int result = native_window_get_compositor_timing(mWindow.get(),
1085 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1086 EXPECT_EQ(NO_ERROR, result);
1087 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1088 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1089 EXPECT_EQ(expectedDeadline, compositeDeadline);
1090
1091 const uint64_t fId1 = getNextFrameId();
1092 dequeueAndQueue(0);
1093 addFrameEvents(true, NO_FRAME_INDEX, 0);
1094
1095 // A "now" just after the deadline snaps properly.
1096 mSurface->setNow(initialCompositorTiming.deadline + 1);
1097 result = native_window_get_compositor_timing(mWindow.get(),
1098 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1099 EXPECT_EQ(NO_ERROR, result);
1100 expectedDeadline =
1101 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1102 EXPECT_EQ(expectedDeadline, compositeDeadline);
1103
1104 const uint64_t fId2 = getNextFrameId();
1105 dequeueAndQueue(1);
1106 addFrameEvents(true, 0, 1);
1107
1108 // A "now" just after the next interval snaps properly.
1109 mSurface->setNow(
1110 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1111 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1112 result = native_window_get_compositor_timing(mWindow.get(),
1113 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1114 EXPECT_EQ(NO_ERROR, result);
1115 expectedDeadline =
1116 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1117 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1118 EXPECT_EQ(expectedDeadline, compositeDeadline);
1119
1120 dequeueAndQueue(2);
1121 addFrameEvents(true, 1, 2);
1122
1123 // A "now" over 1 interval before the deadline snaps properly.
1124 mSurface->setNow(
1125 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1126 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1127 result = native_window_get_compositor_timing(mWindow.get(),
1128 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1129 EXPECT_EQ(NO_ERROR, result);
1130 expectedDeadline =
1131 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1132 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1133 EXPECT_EQ(expectedDeadline, compositeDeadline);
1134
1135 // Re-enabling frame timestamps should get the latest values.
1136 disableFrameTimestamps();
1137 enableFrameTimestamps();
1138
1139 // A "now" over 2 intervals before the deadline snaps properly.
1140 mSurface->setNow(
1141 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1142 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1143 result = native_window_get_compositor_timing(mWindow.get(),
1144 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1145 EXPECT_EQ(NO_ERROR, result);
1146 expectedDeadline =
1147 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1148 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1149 EXPECT_EQ(expectedDeadline, compositeDeadline);
1150}
1151
Brian Anderson1049d1d2016-12-16 17:25:57 -08001152// This verifies the timestamps recorded in the consumer's
1153// FrameTimestampsHistory are properly retrieved by the producer for the
1154// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001155TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1156 enableFrameTimestamps();
1157
Brian Anderson1049d1d2016-12-16 17:25:57 -08001158 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001159 dequeueAndQueue(0);
1160 mFrames[0].signalQueueFences();
1161
Brian Anderson1049d1d2016-12-16 17:25:57 -08001162 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001163 dequeueAndQueue(1);
1164 mFrames[1].signalQueueFences();
1165
1166 addFrameEvents(true, NO_FRAME_INDEX, 0);
1167 mFrames[0].signalRefreshFences();
1168 addFrameEvents(true, 0, 1);
1169 mFrames[0].signalReleaseFences();
1170 mFrames[1].signalRefreshFences();
1171
1172 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001173 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001174 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001175 EXPECT_EQ(NO_ERROR, result);
1176 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1177 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001178 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1179 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1180 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001181 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1182 outGpuCompositionDoneTime);
1183 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1184 EXPECT_EQ(mFrames[0].kRetireTime, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001185 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001186 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1187
1188 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001189 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001190 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001191 EXPECT_EQ(NO_ERROR, result);
1192 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1193 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001194 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1195 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1196 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001197 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1198 outGpuCompositionDoneTime);
1199 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1200 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001201 EXPECT_EQ(0, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001202 EXPECT_EQ(0, outReleaseTime);
1203}
1204
1205// This test verifies the acquire fence recorded by the consumer is not sent
1206// back to the producer and the producer saves its own fence.
1207TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1208 enableFrameTimestamps();
1209 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1210
Brian Anderson3da8d272016-07-28 16:20:47 -07001211 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001212 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001213 dequeueAndQueue(0);
1214
1215 // Verify queue-related timestamps for f1 are available immediately in the
1216 // producer without asking the consumer again, even before signaling the
1217 // acquire fence.
1218 resetTimestamps();
1219 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001220 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001221 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001222 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001223 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1224 EXPECT_EQ(NO_ERROR, result);
1225 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1226 EXPECT_EQ(0, outAcquireTime);
1227
1228 // Signal acquire fences. Verify a sync call still isn't necessary.
1229 mFrames[0].signalQueueFences();
1230
1231 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001232 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001233 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001234 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001235 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1236 EXPECT_EQ(NO_ERROR, result);
1237 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1238 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1239
1240 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001241 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001242 dequeueAndQueue(1);
1243
1244 // Verify queue-related timestamps for f2 are available immediately in the
1245 // producer without asking the consumer again, even before signaling the
1246 // acquire fence.
1247 resetTimestamps();
1248 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001249 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001250 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001251 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001252 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1253 EXPECT_EQ(NO_ERROR, result);
1254 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1255 EXPECT_EQ(0, outAcquireTime);
1256
1257 // Signal acquire fences. Verify a sync call still isn't necessary.
1258 mFrames[1].signalQueueFences();
1259
1260 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001261 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001262 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001263 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001264 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1265 EXPECT_EQ(NO_ERROR, result);
1266 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1267 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1268}
1269
1270TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1271 enableFrameTimestamps();
1272 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1273
1274 // Dequeue and queue frame 1.
1275 dequeueAndQueue(0);
1276 mFrames[0].signalQueueFences();
1277
1278 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001279 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001280 dequeueAndQueue(1);
1281 mFrames[1].signalQueueFences();
1282
1283 addFrameEvents(true, NO_FRAME_INDEX, 0);
1284 mFrames[0].signalRefreshFences();
1285 addFrameEvents(true, 0, 1);
1286 mFrames[0].signalReleaseFences();
1287 mFrames[1].signalRefreshFences();
1288
1289 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001290 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001291 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001292 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1293 nullptr, nullptr, nullptr);
1294 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001295 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1296}
1297
1298// This test verifies that fences can signal and update timestamps producer
1299// side without an additional sync call to the consumer.
1300TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1301 enableFrameTimestamps();
1302 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1303
1304 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001305 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001306 dequeueAndQueue(0);
1307 mFrames[0].signalQueueFences();
1308
1309 // Dequeue and queue frame 2.
1310 dequeueAndQueue(1);
1311 mFrames[1].signalQueueFences();
1312
1313 addFrameEvents(true, NO_FRAME_INDEX, 0);
1314 addFrameEvents(true, 0, 1);
1315
1316 // Verify available timestamps are correct for frame 1, before any
1317 // fence has been signaled.
1318 // Note: A sync call is necessary here since the events triggered by
1319 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001320 resetTimestamps();
1321 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001322 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001323 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1324 EXPECT_EQ(NO_ERROR, result);
1325 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1326 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001327 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1328 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1329 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001330 EXPECT_EQ(0, outGpuCompositionDoneTime);
1331 EXPECT_EQ(0, outDisplayPresentTime);
1332 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001333 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001334 EXPECT_EQ(0, outReleaseTime);
1335
1336 // Verify available timestamps are correct for frame 1 again, before any
1337 // fence has been signaled.
1338 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001339 resetTimestamps();
1340 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001341 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001342 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1343 EXPECT_EQ(NO_ERROR, result);
1344 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1345 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001346 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1347 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1348 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001349 EXPECT_EQ(0, outGpuCompositionDoneTime);
1350 EXPECT_EQ(0, outDisplayPresentTime);
1351 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001352 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001353 EXPECT_EQ(0, outReleaseTime);
1354
1355 // Signal the fences for frame 1.
1356 mFrames[0].signalRefreshFences();
1357 mFrames[0].signalReleaseFences();
1358
1359 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001360 resetTimestamps();
1361 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001362 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001363 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1364 EXPECT_EQ(NO_ERROR, result);
1365 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1366 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001367 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1368 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1369 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001370 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1371 outGpuCompositionDoneTime);
1372 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1373 EXPECT_EQ(mFrames[0].kRetireTime, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001374 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001375 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1376}
1377
1378// This test verifies that if the frame wasn't GPU composited but has a refresh
1379// event a sync call isn't made to get the GPU composite done time since it will
1380// never exist.
1381TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1382 enableFrameTimestamps();
1383 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1384
Brian Anderson3da8d272016-07-28 16:20:47 -07001385 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001386 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001387 dequeueAndQueue(0);
1388 mFrames[0].signalQueueFences();
1389
1390 // Dequeue and queue frame 2.
1391 dequeueAndQueue(1);
1392 mFrames[1].signalQueueFences();
1393
1394 addFrameEvents(false, NO_FRAME_INDEX, 0);
1395 addFrameEvents(false, 0, 1);
1396
1397 // Verify available timestamps are correct for frame 1, before any
1398 // fence has been signaled.
1399 // Note: A sync call is necessary here since the events triggered by
1400 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1401 resetTimestamps();
1402 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001403 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001404 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1405 EXPECT_EQ(NO_ERROR, result);
1406 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1407 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001408 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1409 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1410 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001411 EXPECT_EQ(0, outGpuCompositionDoneTime);
1412 EXPECT_EQ(0, outDisplayPresentTime);
1413 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001414 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001415 EXPECT_EQ(0, outReleaseTime);
1416
1417 // Signal the fences for frame 1.
1418 mFrames[0].signalRefreshFences();
1419 mFrames[0].signalReleaseFences();
1420
1421 // Verify all timestamps, except GPU composition, are available without a
1422 // sync call.
1423 resetTimestamps();
1424 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001425 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001426 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1427 EXPECT_EQ(NO_ERROR, result);
1428 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1429 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001430 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1431 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1432 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001433 EXPECT_EQ(0, outGpuCompositionDoneTime);
1434 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1435 EXPECT_EQ(mFrames[0].kRetireTime, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001436 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001437 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1438}
1439
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001440// This test verifies that if the certain timestamps can't possibly exist for
1441// the most recent frame, then a sync call is not done.
Brian Anderson3da8d272016-07-28 16:20:47 -07001442TEST_F(GetFrameTimestampsTest, NoRetireOrReleaseNoSync) {
1443 enableFrameTimestamps();
1444 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(true, true);
1445
1446 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001447 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001448 dequeueAndQueue(0);
1449 mFrames[0].signalQueueFences();
1450
1451 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001452 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001453 dequeueAndQueue(1);
1454 mFrames[1].signalQueueFences();
1455
1456 addFrameEvents(false, NO_FRAME_INDEX, 0);
1457 addFrameEvents(false, 0, 1);
1458
1459 // Verify available timestamps are correct for frame 1, before any
1460 // fence has been signaled.
1461 // Note: A sync call is necessary here since the events triggered by
1462 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001463 resetTimestamps();
1464 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001465 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001466 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1467 EXPECT_EQ(NO_ERROR, result);
1468 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1469 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001470 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1471 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1472 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001473 EXPECT_EQ(0, outGpuCompositionDoneTime);
1474 EXPECT_EQ(0, outDisplayPresentTime);
1475 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001476 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001477 EXPECT_EQ(0, outReleaseTime);
1478
1479 mFrames[0].signalRefreshFences();
1480 mFrames[0].signalReleaseFences();
1481 mFrames[1].signalRefreshFences();
1482
Brian Anderson1049d1d2016-12-16 17:25:57 -08001483 // Verify querying for all timestmaps of f2 does not do a sync call. Even
1484 // though the lastRefresh, retire, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001485 // available, a sync call should not occur because it's not possible for f2
1486 // to encounter the final value for those events until another frame is
1487 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001488 resetTimestamps();
1489 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001490 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001491 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1492 EXPECT_EQ(NO_ERROR, result);
1493 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1494 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001495 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1496 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1497 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001498 EXPECT_EQ(0, outGpuCompositionDoneTime);
1499 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1500 EXPECT_EQ(0, outDisplayRetireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001501 EXPECT_EQ(0, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001502 EXPECT_EQ(0, outReleaseTime);
1503}
1504
1505// This test verifies there are no sync calls for present or retire times
1506// when they aren't supported and that an error is returned.
1507void GetFrameTimestampsTest::PresentOrRetireUnsupportedNoSyncTest(
1508 bool displayPresentSupported, bool displayRetireSupported) {
1509
1510 enableFrameTimestamps();
1511 mSurface->mFakeSurfaceComposer->setSupportedTimestamps(
1512 displayPresentSupported, displayRetireSupported);
1513
1514 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001515 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001516 dequeueAndQueue(0);
1517
1518 // Verify a query for the Present and Retire times do not trigger
1519 // a sync call if they are not supported.
Brian Anderson3da8d272016-07-28 16:20:47 -07001520 resetTimestamps();
1521 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001522 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001523 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
Brian Anderson3da8d272016-07-28 16:20:47 -07001524 displayPresentSupported ? nullptr : &outDisplayPresentTime,
1525 displayRetireSupported ? nullptr : &outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001526 nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001527 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1528 EXPECT_EQ(BAD_VALUE, result);
1529 EXPECT_EQ(-1, outDisplayRetireTime);
1530 EXPECT_EQ(-1, outDisplayPresentTime);
1531}
1532
1533TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1534 PresentOrRetireUnsupportedNoSyncTest(false, true);
1535}
1536
1537TEST_F(GetFrameTimestampsTest, RetireUnsupportedNoSync) {
1538 PresentOrRetireUnsupportedNoSyncTest(true, false);
1539}
1540
Jamie Gennis134f0422011-03-08 12:18:54 -08001541}