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