1 /*
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
17 #include "DummyConsumer.h"
18
19 #include <gtest/gtest.h>
20
21 #include <SurfaceFlingerProperties.h>
22 #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
23 #include <binder/ProcessState.h>
24 #include <configstore/Utils.h>
25 #include <gui/BufferItemConsumer.h>
26 #include <gui/IDisplayEventConnection.h>
27 #include <gui/IProducerListener.h>
28 #include <gui/ISurfaceComposer.h>
29 #include <gui/Surface.h>
30 #include <gui/SurfaceComposerClient.h>
31 #include <inttypes.h>
32 #include <private/gui/ComposerService.h>
33 #include <ui/BufferQueueDefs.h>
34 #include <ui/Rect.h>
35 #include <utils/String8.h>
36
37 #include <limits>
38 #include <thread>
39
40 namespace android {
41
42 using namespace std::chrono_literals;
43 // retrieve wide-color and hdr settings from configstore
44 using namespace android::hardware::configstore;
45 using namespace android::hardware::configstore::V1_0;
46 using ui::ColorMode;
47
48 using Transaction = SurfaceComposerClient::Transaction;
49
50 static bool hasWideColorDisplay = android::sysprop::has_wide_color_display(false);
51
52 static bool hasHdrDisplay = android::sysprop::has_HDR_display(false);
53
54 class FakeSurfaceComposer;
55 class FakeProducerFrameEventHistory;
56
57 static constexpr uint64_t NO_FRAME_INDEX = std::numeric_limits<uint64_t>::max();
58
59 class DummySurfaceListener : public SurfaceListener {
60 public:
DummySurfaceListener(bool enableReleasedCb=false)61 DummySurfaceListener(bool enableReleasedCb = false) :
62 mEnableReleaseCb(enableReleasedCb),
63 mBuffersReleased(0) {}
64 virtual ~DummySurfaceListener() = default;
65
onBufferReleased()66 virtual void onBufferReleased() {
67 mBuffersReleased++;
68 }
needsReleaseNotify()69 virtual bool needsReleaseNotify() {
70 return mEnableReleaseCb;
71 }
onBuffersDiscarded(const std::vector<sp<GraphicBuffer>> & buffers)72 virtual void onBuffersDiscarded(const std::vector<sp<GraphicBuffer>>& buffers) {
73 mDiscardedBuffers.insert(mDiscardedBuffers.end(), buffers.begin(), buffers.end());
74 }
75
getReleaseNotifyCount() const76 int getReleaseNotifyCount() const {
77 return mBuffersReleased;
78 }
getDiscardedBuffers() const79 const std::vector<sp<GraphicBuffer>>& getDiscardedBuffers() const {
80 return mDiscardedBuffers;
81 }
82 private:
83 // No need to use lock given the test triggers the listener in the same
84 // thread context.
85 bool mEnableReleaseCb;
86 int32_t mBuffersReleased;
87 std::vector<sp<GraphicBuffer>> mDiscardedBuffers;
88 };
89
90 class SurfaceTest : public ::testing::Test {
91 protected:
SurfaceTest()92 SurfaceTest() {
93 ProcessState::self()->startThreadPool();
94 }
95
SetUp()96 virtual void SetUp() {
97 mComposerClient = new SurfaceComposerClient;
98 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
99
100 // TODO(brianderson): The following sometimes fails and is a source of
101 // test flakiness.
102 mSurfaceControl = mComposerClient->createSurface(
103 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
104
105 ASSERT_TRUE(mSurfaceControl != nullptr);
106 ASSERT_TRUE(mSurfaceControl->isValid());
107
108 Transaction t;
109 ASSERT_EQ(NO_ERROR, t.setLayer(mSurfaceControl, 0x7fffffff)
110 .show(mSurfaceControl)
111 .apply());
112
113 mSurface = mSurfaceControl->getSurface();
114 ASSERT_TRUE(mSurface != nullptr);
115 }
116
TearDown()117 virtual void TearDown() {
118 mComposerClient->dispose();
119 }
120
testSurfaceListener(bool hasSurfaceListener,bool enableReleasedCb,int32_t extraDiscardedBuffers)121 void testSurfaceListener(bool hasSurfaceListener, bool enableReleasedCb,
122 int32_t extraDiscardedBuffers) {
123 sp<IGraphicBufferProducer> producer;
124 sp<IGraphicBufferConsumer> consumer;
125 BufferQueue::createBufferQueue(&producer, &consumer);
126
127 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
128 consumer->consumerConnect(dummyConsumer, false);
129 consumer->setConsumerName(String8("TestConsumer"));
130
131 sp<Surface> surface = new Surface(producer);
132 sp<ANativeWindow> window(surface);
133 sp<DummySurfaceListener> listener;
134 if (hasSurfaceListener) {
135 listener = new DummySurfaceListener(enableReleasedCb);
136 }
137 ASSERT_EQ(OK, surface->connect(
138 NATIVE_WINDOW_API_CPU,
139 /*reportBufferRemoval*/true,
140 /*listener*/listener));
141 const int BUFFER_COUNT = 4 + extraDiscardedBuffers;
142 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
143
144 ANativeWindowBuffer* buffers[BUFFER_COUNT];
145 // Dequeue first to allocate a number of buffers
146 for (int i = 0; i < BUFFER_COUNT; i++) {
147 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(window.get(), &buffers[i]));
148 }
149 for (int i = 0; i < BUFFER_COUNT; i++) {
150 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], -1));
151 }
152
153 ANativeWindowBuffer* buffer;
154 // Fill BUFFER_COUNT-1 buffers
155 for (int i = 0; i < BUFFER_COUNT-1; i++) {
156 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(window.get(), &buffer));
157 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, -1));
158 }
159
160 // Dequeue 1 buffer
161 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(window.get(), &buffer));
162
163 // Acquire and free 1+extraDiscardedBuffers buffer, check onBufferReleased is called.
164 std::vector<BufferItem> releasedItems;
165 releasedItems.resize(1+extraDiscardedBuffers);
166 for (int i = 0; i < releasedItems.size(); i++) {
167 ASSERT_EQ(NO_ERROR, consumer->acquireBuffer(&releasedItems[i], 0));
168 ASSERT_EQ(NO_ERROR, consumer->releaseBuffer(releasedItems[i].mSlot,
169 releasedItems[i].mFrameNumber, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR,
170 Fence::NO_FENCE));
171 }
172 int32_t expectedReleaseCb = (enableReleasedCb ? releasedItems.size() : 0);
173 if (hasSurfaceListener) {
174 ASSERT_EQ(expectedReleaseCb, listener->getReleaseNotifyCount());
175 }
176
177 // Acquire 1 buffer, leaving 1+extraDiscardedBuffers filled buffer in queue
178 BufferItem item;
179 ASSERT_EQ(NO_ERROR, consumer->acquireBuffer(&item, 0));
180
181 // Discard free buffers
182 ASSERT_EQ(NO_ERROR, consumer->discardFreeBuffers());
183
184 if (hasSurfaceListener) {
185 ASSERT_EQ(expectedReleaseCb, listener->getReleaseNotifyCount());
186
187 // Check onBufferDiscarded is called with correct buffer
188 auto discardedBuffers = listener->getDiscardedBuffers();
189 ASSERT_EQ(discardedBuffers.size(), releasedItems.size());
190 for (int i = 0; i < releasedItems.size(); i++) {
191 ASSERT_EQ(discardedBuffers[i], releasedItems[i].mGraphicBuffer);
192 }
193
194 ASSERT_EQ(expectedReleaseCb, listener->getReleaseNotifyCount());
195 }
196
197 // Disconnect the surface
198 ASSERT_EQ(NO_ERROR, surface->disconnect(NATIVE_WINDOW_API_CPU));
199 }
200
201 sp<Surface> mSurface;
202 sp<SurfaceComposerClient> mComposerClient;
203 sp<SurfaceControl> mSurfaceControl;
204 };
205
TEST_F(SurfaceTest,CreateSurfaceReturnsErrorBadClient)206 TEST_F(SurfaceTest, CreateSurfaceReturnsErrorBadClient) {
207 mComposerClient->dispose();
208 ASSERT_EQ(NO_INIT, mComposerClient->initCheck());
209
210 sp<SurfaceControl> sc;
211 status_t err = mComposerClient->createSurfaceChecked(
212 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, &sc, 0);
213 ASSERT_EQ(NO_INIT, err);
214 }
215
TEST_F(SurfaceTest,QueuesToWindowComposerIsTrueWhenVisible)216 TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
217 sp<ANativeWindow> anw(mSurface);
218 int result = -123;
219 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
220 &result);
221 EXPECT_EQ(NO_ERROR, err);
222 EXPECT_EQ(1, result);
223 }
224
TEST_F(SurfaceTest,QueuesToWindowComposerIsTrueWhenPurgatorized)225 TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
226 mSurfaceControl.clear();
227 // Wait for the async clean-up to complete.
228 std::this_thread::sleep_for(50ms);
229
230 sp<ANativeWindow> anw(mSurface);
231 int result = -123;
232 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
233 &result);
234 EXPECT_EQ(NO_ERROR, err);
235 EXPECT_EQ(1, result);
236 }
237
238 // This test probably doesn't belong here.
TEST_F(SurfaceTest,ScreenshotsOfProtectedBuffersDontSucceed)239 TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersDontSucceed) {
240 sp<ANativeWindow> anw(mSurface);
241
242 // Verify the screenshot works with no protected buffers.
243 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
244
245 const sp<IBinder> display = sf->getInternalDisplayToken();
246 ASSERT_FALSE(display == nullptr);
247
248 sp<GraphicBuffer> outBuffer;
249 bool ignored;
250 ASSERT_EQ(NO_ERROR,
251 sf->captureScreen(display, &outBuffer, ignored, ui::Dataspace::V0_SRGB,
252 ui::PixelFormat::RGBA_8888, Rect(), 64, 64, false));
253
254 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
255 NATIVE_WINDOW_API_CPU));
256 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
257 // that we need to dequeue a buffer in order for it to actually get
258 // allocated in SurfaceFlinger.
259 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
260 GRALLOC_USAGE_PROTECTED));
261 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
262 ANativeWindowBuffer* buf = nullptr;
263
264 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
265 if (err) {
266 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
267 // that's okay as long as this is the reason for the failure.
268 // try again without the GRALLOC_USAGE_PROTECTED bit.
269 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
270 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
271 &buf));
272 return;
273 }
274 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
275
276 for (int i = 0; i < 4; i++) {
277 // Loop to make sure SurfaceFlinger has retired a protected buffer.
278 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
279 &buf));
280 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
281 }
282 ASSERT_EQ(NO_ERROR,
283 sf->captureScreen(display, &outBuffer, ignored, ui::Dataspace::V0_SRGB,
284 ui::PixelFormat::RGBA_8888, Rect(), 64, 64, false));
285 }
286
TEST_F(SurfaceTest,ConcreteTypeIsSurface)287 TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
288 sp<ANativeWindow> anw(mSurface);
289 int result = -123;
290 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
291 EXPECT_EQ(NO_ERROR, err);
292 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
293 }
294
TEST_F(SurfaceTest,LayerCountIsOne)295 TEST_F(SurfaceTest, LayerCountIsOne) {
296 sp<ANativeWindow> anw(mSurface);
297 int result = -123;
298 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
299 EXPECT_EQ(NO_ERROR, err);
300 EXPECT_EQ(1, result);
301 }
302
TEST_F(SurfaceTest,QueryConsumerUsage)303 TEST_F(SurfaceTest, QueryConsumerUsage) {
304 const int TEST_USAGE_FLAGS =
305 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
306 sp<IGraphicBufferProducer> producer;
307 sp<IGraphicBufferConsumer> consumer;
308 BufferQueue::createBufferQueue(&producer, &consumer);
309 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
310 TEST_USAGE_FLAGS);
311 sp<Surface> s = new Surface(producer);
312
313 sp<ANativeWindow> anw(s);
314
315 int flags = -1;
316 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
317
318 ASSERT_EQ(NO_ERROR, err);
319 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
320 }
321
TEST_F(SurfaceTest,QueryDefaultBuffersDataSpace)322 TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
323 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_V0_SRGB;
324 sp<IGraphicBufferProducer> producer;
325 sp<IGraphicBufferConsumer> consumer;
326 BufferQueue::createBufferQueue(&producer, &consumer);
327 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
328
329 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
330
331 sp<Surface> s = new Surface(producer);
332
333 sp<ANativeWindow> anw(s);
334
335 android_dataspace dataSpace;
336
337 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
338 reinterpret_cast<int*>(&dataSpace));
339
340 ASSERT_EQ(NO_ERROR, err);
341 ASSERT_EQ(TEST_DATASPACE, dataSpace);
342 }
343
TEST_F(SurfaceTest,SettingGenerationNumber)344 TEST_F(SurfaceTest, SettingGenerationNumber) {
345 sp<IGraphicBufferProducer> producer;
346 sp<IGraphicBufferConsumer> consumer;
347 BufferQueue::createBufferQueue(&producer, &consumer);
348 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
349 sp<Surface> surface = new Surface(producer);
350 sp<ANativeWindow> window(surface);
351
352 // Allocate a buffer with a generation number of 0
353 ANativeWindowBuffer* buffer;
354 int fenceFd;
355 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
356 NATIVE_WINDOW_API_CPU));
357 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
358 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
359
360 // Detach the buffer and check its generation number
361 sp<GraphicBuffer> graphicBuffer;
362 sp<Fence> fence;
363 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
364 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
365
366 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
367 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
368
369 // This should change the generation number of the GraphicBuffer
370 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
371
372 // Check that the new generation number sticks with the buffer
373 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
374 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
375 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
376 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
377 }
378
TEST_F(SurfaceTest,GetConsumerName)379 TEST_F(SurfaceTest, GetConsumerName) {
380 sp<IGraphicBufferProducer> producer;
381 sp<IGraphicBufferConsumer> consumer;
382 BufferQueue::createBufferQueue(&producer, &consumer);
383
384 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
385 consumer->consumerConnect(dummyConsumer, false);
386 consumer->setConsumerName(String8("TestConsumer"));
387
388 sp<Surface> surface = new Surface(producer);
389 sp<ANativeWindow> window(surface);
390 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
391
392 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
393 }
394
TEST_F(SurfaceTest,GetWideColorSupport)395 TEST_F(SurfaceTest, GetWideColorSupport) {
396 sp<IGraphicBufferProducer> producer;
397 sp<IGraphicBufferConsumer> consumer;
398 BufferQueue::createBufferQueue(&producer, &consumer);
399
400 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
401 consumer->consumerConnect(dummyConsumer, false);
402 consumer->setConsumerName(String8("TestConsumer"));
403
404 sp<Surface> surface = new Surface(producer);
405 sp<ANativeWindow> window(surface);
406 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
407
408 bool supported;
409 surface->getWideColorSupport(&supported);
410
411 // NOTE: This test assumes that device that supports
412 // wide-color (as indicated by BoardConfig) must also
413 // have a wide-color primary display.
414 // That assumption allows this test to cover devices
415 // that advertised a wide-color color mode without
416 // actually supporting wide-color to pass this test
417 // as well as the case of a device that does support
418 // wide-color (via BoardConfig) and has a wide-color
419 // primary display.
420 // NOT covered at this time is a device that supports
421 // wide color in the BoardConfig but does not support
422 // a wide-color color mode on the primary display.
423 ASSERT_EQ(hasWideColorDisplay, supported);
424 }
425
TEST_F(SurfaceTest,GetHdrSupport)426 TEST_F(SurfaceTest, GetHdrSupport) {
427 sp<IGraphicBufferProducer> producer;
428 sp<IGraphicBufferConsumer> consumer;
429 BufferQueue::createBufferQueue(&producer, &consumer);
430
431 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
432 consumer->consumerConnect(dummyConsumer, false);
433 consumer->setConsumerName(String8("TestConsumer"));
434
435 sp<Surface> surface = new Surface(producer);
436 sp<ANativeWindow> window(surface);
437 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
438
439 bool supported;
440 status_t result = surface->getHdrSupport(&supported);
441 ASSERT_EQ(NO_ERROR, result);
442
443 // NOTE: This is not a CTS test.
444 // This test verifies that when the BoardConfig TARGET_HAS_HDR_DISPLAY
445 // is TRUE, getHdrSupport is also true.
446 // TODO: Add check for an HDR color mode on the primary display.
447 ASSERT_EQ(hasHdrDisplay, supported);
448 }
449
TEST_F(SurfaceTest,SetHdrMetadata)450 TEST_F(SurfaceTest, SetHdrMetadata) {
451 sp<IGraphicBufferProducer> producer;
452 sp<IGraphicBufferConsumer> consumer;
453 BufferQueue::createBufferQueue(&producer, &consumer);
454
455 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
456 consumer->consumerConnect(dummyConsumer, false);
457 consumer->setConsumerName(String8("TestConsumer"));
458
459 sp<Surface> surface = new Surface(producer);
460 sp<ANativeWindow> window(surface);
461 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
462
463 bool supported;
464 status_t result = surface->getHdrSupport(&supported);
465 ASSERT_EQ(NO_ERROR, result);
466
467 if (!hasHdrDisplay || !supported) {
468 return;
469 }
470 const android_smpte2086_metadata smpte2086 = {
471 {0.680, 0.320},
472 {0.265, 0.690},
473 {0.150, 0.060},
474 {0.3127, 0.3290},
475 100.0,
476 0.1,
477 };
478 const android_cta861_3_metadata cta861_3 = {
479 78.0,
480 62.0,
481 };
482
483 std::vector<uint8_t> hdr10plus;
484 hdr10plus.push_back(0xff);
485
486 int error = native_window_set_buffers_smpte2086_metadata(window.get(), &smpte2086);
487 ASSERT_EQ(error, NO_ERROR);
488 error = native_window_set_buffers_cta861_3_metadata(window.get(), &cta861_3);
489 ASSERT_EQ(error, NO_ERROR);
490 error = native_window_set_buffers_hdr10_plus_metadata(window.get(), hdr10plus.size(),
491 hdr10plus.data());
492 ASSERT_EQ(error, NO_ERROR);
493 }
494
TEST_F(SurfaceTest,DynamicSetBufferCount)495 TEST_F(SurfaceTest, DynamicSetBufferCount) {
496 sp<IGraphicBufferProducer> producer;
497 sp<IGraphicBufferConsumer> consumer;
498 BufferQueue::createBufferQueue(&producer, &consumer);
499
500 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
501 consumer->consumerConnect(dummyConsumer, false);
502 consumer->setConsumerName(String8("TestConsumer"));
503
504 sp<Surface> surface = new Surface(producer);
505 sp<ANativeWindow> window(surface);
506
507 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
508 NATIVE_WINDOW_API_CPU));
509 native_window_set_buffer_count(window.get(), 4);
510
511 int fence;
512 ANativeWindowBuffer* buffer;
513 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
514 native_window_set_buffer_count(window.get(), 3);
515 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
516 native_window_set_buffer_count(window.get(), 2);
517 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
518 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
519 }
520
TEST_F(SurfaceTest,GetAndFlushRemovedBuffers)521 TEST_F(SurfaceTest, GetAndFlushRemovedBuffers) {
522 sp<IGraphicBufferProducer> producer;
523 sp<IGraphicBufferConsumer> consumer;
524 BufferQueue::createBufferQueue(&producer, &consumer);
525
526 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
527 consumer->consumerConnect(dummyConsumer, false);
528 consumer->setConsumerName(String8("TestConsumer"));
529
530 sp<Surface> surface = new Surface(producer);
531 sp<ANativeWindow> window(surface);
532 sp<DummyProducerListener> listener = new DummyProducerListener();
533 ASSERT_EQ(OK, surface->connect(
534 NATIVE_WINDOW_API_CPU,
535 /*listener*/listener,
536 /*reportBufferRemoval*/true));
537 const int BUFFER_COUNT = 4;
538 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
539
540 sp<GraphicBuffer> detachedBuffer;
541 sp<Fence> outFence;
542 int fences[BUFFER_COUNT];
543 ANativeWindowBuffer* buffers[BUFFER_COUNT];
544 // Allocate buffers because detachNextBuffer requires allocated buffers
545 for (int i = 0; i < BUFFER_COUNT; i++) {
546 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
547 }
548 for (int i = 0; i < BUFFER_COUNT; i++) {
549 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
550 }
551
552 // Test detached buffer is correctly reported
553 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
554 std::vector<sp<GraphicBuffer>> removedBuffers;
555 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
556 ASSERT_EQ(1u, removedBuffers.size());
557 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
558 // Test the list is flushed one getAndFlushRemovedBuffers returns
559 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
560 ASSERT_EQ(0u, removedBuffers.size());
561
562
563 // Test removed buffer list is cleanup after next dequeueBuffer call
564 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
565 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[0], &fences[0]));
566 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
567 ASSERT_EQ(0u, removedBuffers.size());
568 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[0], fences[0]));
569
570 // Test removed buffer list is cleanup after next detachNextBuffer call
571 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
572 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
573 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
574 ASSERT_EQ(1u, removedBuffers.size());
575 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
576
577 // Re-allocate buffers since all buffers are detached up to now
578 for (int i = 0; i < BUFFER_COUNT; i++) {
579 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
580 }
581 for (int i = 0; i < BUFFER_COUNT; i++) {
582 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
583 }
584
585 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
586 ASSERT_EQ(NO_ERROR, surface->attachBuffer(detachedBuffer.get()));
587 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
588 // Depends on which slot GraphicBufferProducer impl pick, the attach call might
589 // get 0 or 1 buffer removed.
590 ASSERT_LE(removedBuffers.size(), 1u);
591 }
592
TEST_F(SurfaceTest,SurfaceListenerTest)593 TEST_F(SurfaceTest, SurfaceListenerTest) {
594 // Test discarding 1 free buffers with no listener
595 testSurfaceListener(/*hasListener*/false, /*enableReleaseCb*/false, /*extraDiscardedBuffers*/0);
596 // Test discarding 2 free buffers with no listener
597 testSurfaceListener(/*hasListener*/false, /*enableReleaseCb*/false, /*extraDiscardedBuffers*/1);
598 // Test discarding 1 free buffers with a listener, disabling onBufferReleased
599 testSurfaceListener(/*hasListener*/true, /*enableReleasedCb*/false, /*extraDiscardedBuffers*/0);
600 // Test discarding 2 free buffers with a listener, disabling onBufferReleased
601 testSurfaceListener(/*hasListener*/true, /*enableReleasedCb*/false, /*extraDiscardedBuffers*/1);
602 // Test discarding 1 free buffers with a listener, enabling onBufferReleased
603 testSurfaceListener(/*hasListener*/true, /*enableReleasedCb*/true, /*extraDiscardedBuffers*/0);
604 // Test discarding 3 free buffers with a listener, enabling onBufferReleased
605 testSurfaceListener(/*hasListener*/true, /*enableReleasedCb*/true, /*extraDiscardedBuffers*/2);
606 }
607
TEST_F(SurfaceTest,TestGetLastDequeueStartTime)608 TEST_F(SurfaceTest, TestGetLastDequeueStartTime) {
609 sp<ANativeWindow> anw(mSurface);
610 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU));
611
612 ANativeWindowBuffer* buffer = nullptr;
613 int32_t fenceFd = -1;
614
615 nsecs_t before = systemTime(CLOCK_MONOTONIC);
616 anw->dequeueBuffer(anw.get(), &buffer, &fenceFd);
617 nsecs_t after = systemTime(CLOCK_MONOTONIC);
618
619 nsecs_t lastDequeueTime = ANativeWindow_getLastDequeueStartTime(anw.get());
620 ASSERT_LE(before, lastDequeueTime);
621 ASSERT_GE(after, lastDequeueTime);
622 }
623
624 class FakeConsumer : public BnConsumerListener {
625 public:
onFrameAvailable(const BufferItem &)626 void onFrameAvailable(const BufferItem& /*item*/) override {}
onBuffersReleased()627 void onBuffersReleased() override {}
onSidebandStreamChanged()628 void onSidebandStreamChanged() override {}
629
addAndGetFrameTimestamps(const NewFrameEventsEntry * newTimestamps,FrameEventHistoryDelta * outDelta)630 void addAndGetFrameTimestamps(
631 const NewFrameEventsEntry* newTimestamps,
632 FrameEventHistoryDelta* outDelta) override {
633 if (newTimestamps) {
634 if (mGetFrameTimestampsEnabled) {
635 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
636 "Test should set mNewFrameEntryOverride before queuing "
637 "a frame.";
638 EXPECT_EQ(newTimestamps->frameNumber,
639 mNewFrameEntryOverride.frameNumber) <<
640 "Test attempting to add NewFrameEntryOverride with "
641 "incorrect frame number.";
642 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
643 mNewFrameEntryOverride.frameNumber = 0;
644 }
645 mAddFrameTimestampsCount++;
646 mLastAddedFrameNumber = newTimestamps->frameNumber;
647 }
648 if (outDelta) {
649 mFrameEventHistory.getAndResetDelta(outDelta);
650 mGetFrameTimestampsCount++;
651 }
652 mAddAndGetFrameTimestampsCallCount++;
653 }
654
655 bool mGetFrameTimestampsEnabled = false;
656
657 ConsumerFrameEventHistory mFrameEventHistory;
658 int mAddAndGetFrameTimestampsCallCount = 0;
659 int mAddFrameTimestampsCount = 0;
660 int mGetFrameTimestampsCount = 0;
661 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
662
663 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
664 };
665
666
667 class FakeSurfaceComposer : public ISurfaceComposer{
668 public:
~FakeSurfaceComposer()669 ~FakeSurfaceComposer() override {}
670
setSupportsPresent(bool supportsPresent)671 void setSupportsPresent(bool supportsPresent) {
672 mSupportsPresent = supportsPresent;
673 }
674
createConnection()675 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
createDisplayEventConnection(ISurfaceComposer::VsyncSource,ISurfaceComposer::ConfigChanged)676 sp<IDisplayEventConnection> createDisplayEventConnection(
677 ISurfaceComposer::VsyncSource, ISurfaceComposer::ConfigChanged) override {
678 return nullptr;
679 }
createDisplay(const String8 &,bool)680 sp<IBinder> createDisplay(const String8& /*displayName*/,
681 bool /*secure*/) override { return nullptr; }
destroyDisplay(const sp<IBinder> &)682 void destroyDisplay(const sp<IBinder>& /*display */) override {}
getPhysicalDisplayIds() const683 std::vector<PhysicalDisplayId> getPhysicalDisplayIds() const override { return {}; }
getPhysicalDisplayToken(PhysicalDisplayId) const684 sp<IBinder> getPhysicalDisplayToken(PhysicalDisplayId) const override { return nullptr; }
setTransactionState(const Vector<ComposerState> &,const Vector<DisplayState> &,uint32_t,const sp<IBinder> &,const InputWindowCommands &,int64_t,const client_cache_t &,bool,const std::vector<ListenerCallbacks> &)685 void setTransactionState(const Vector<ComposerState>& /*state*/,
686 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/,
687 const sp<IBinder>& /*applyToken*/,
688 const InputWindowCommands& /*inputWindowCommands*/,
689 int64_t /*desiredPresentTime*/, const client_cache_t& /*cachedBuffer*/,
690 bool /*hasListenerCallbacks*/,
691 const std::vector<ListenerCallbacks>& /*listenerCallbacks*/) override {
692 }
693
bootFinished()694 void bootFinished() override {}
authenticateSurfaceTexture(const sp<IGraphicBufferProducer> &) const695 bool authenticateSurfaceTexture(
696 const sp<IGraphicBufferProducer>& /*surface*/) const override {
697 return false;
698 }
699
getSupportedFrameTimestamps(std::vector<FrameEvent> * outSupported) const700 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
701 const override {
702 *outSupported = {
703 FrameEvent::REQUESTED_PRESENT,
704 FrameEvent::ACQUIRE,
705 FrameEvent::LATCH,
706 FrameEvent::FIRST_REFRESH_START,
707 FrameEvent::LAST_REFRESH_START,
708 FrameEvent::GPU_COMPOSITION_DONE,
709 FrameEvent::DEQUEUE_READY,
710 FrameEvent::RELEASE
711 };
712 if (mSupportsPresent) {
713 outSupported->push_back(
714 FrameEvent::DISPLAY_PRESENT);
715 }
716 return NO_ERROR;
717 }
718
setPowerMode(const sp<IBinder> &,int)719 void setPowerMode(const sp<IBinder>& /*display*/, int /*mode*/) override {}
getDisplayInfo(const sp<IBinder> &,DisplayInfo *)720 status_t getDisplayInfo(const sp<IBinder>& /*display*/, DisplayInfo*) override {
721 return NO_ERROR;
722 }
getDisplayConfigs(const sp<IBinder> &,Vector<DisplayConfig> *)723 status_t getDisplayConfigs(const sp<IBinder>& /*display*/, Vector<DisplayConfig>*) override {
724 return NO_ERROR;
725 }
getDisplayState(const sp<IBinder> &,ui::DisplayState *)726 status_t getDisplayState(const sp<IBinder>& /*display*/, ui::DisplayState*) override {
727 return NO_ERROR;
728 }
getDisplayStats(const sp<IBinder> &,DisplayStatInfo *)729 status_t getDisplayStats(const sp<IBinder>& /*display*/,
730 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
getActiveConfig(const sp<IBinder> &)731 int getActiveConfig(const sp<IBinder>& /*display*/) override { return 0; }
getDisplayColorModes(const sp<IBinder> &,Vector<ColorMode> *)732 status_t getDisplayColorModes(const sp<IBinder>& /*display*/,
733 Vector<ColorMode>* /*outColorModes*/) override {
734 return NO_ERROR;
735 }
getDisplayNativePrimaries(const sp<IBinder> &,ui::DisplayPrimaries &)736 status_t getDisplayNativePrimaries(const sp<IBinder>& /*display*/,
737 ui::DisplayPrimaries& /*primaries*/) override {
738 return NO_ERROR;
739 }
getActiveColorMode(const sp<IBinder> &)740 ColorMode getActiveColorMode(const sp<IBinder>& /*display*/)
741 override {
742 return ColorMode::NATIVE;
743 }
setActiveColorMode(const sp<IBinder> &,ColorMode)744 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
745 ColorMode /*colorMode*/) override { return NO_ERROR; }
captureScreen(const sp<IBinder> &,sp<GraphicBuffer> *,bool &,ui::Dataspace,ui::PixelFormat,const Rect &,uint32_t,uint32_t,bool,ui::Rotation,bool)746 status_t captureScreen(const sp<IBinder>& /*display*/, sp<GraphicBuffer>* /*outBuffer*/,
747 bool& /*outCapturedSecureLayers*/, ui::Dataspace /*reqDataspace*/,
748 ui::PixelFormat /*reqPixelFormat*/, const Rect& /*sourceCrop*/,
749 uint32_t /*reqWidth*/, uint32_t /*reqHeight*/,
750 bool /*useIdentityTransform*/, ui::Rotation,
751 bool /*captureSecureLayers*/) override {
752 return NO_ERROR;
753 }
getAutoLowLatencyModeSupport(const sp<IBinder> &,bool *) const754 status_t getAutoLowLatencyModeSupport(const sp<IBinder>& /*display*/,
755 bool* /*outSupport*/) const override {
756 return NO_ERROR;
757 }
setAutoLowLatencyMode(const sp<IBinder> &,bool)758 void setAutoLowLatencyMode(const sp<IBinder>& /*display*/, bool /*on*/) override {}
getGameContentTypeSupport(const sp<IBinder> &,bool *) const759 status_t getGameContentTypeSupport(const sp<IBinder>& /*display*/,
760 bool* /*outSupport*/) const override {
761 return NO_ERROR;
762 }
setGameContentType(const sp<IBinder> &,bool)763 void setGameContentType(const sp<IBinder>& /*display*/, bool /*on*/) override {}
captureScreen(uint64_t,ui::Dataspace *,sp<GraphicBuffer> *)764 status_t captureScreen(uint64_t /*displayOrLayerStack*/, ui::Dataspace* /*outDataspace*/,
765 sp<GraphicBuffer>* /*outBuffer*/) override {
766 return NO_ERROR;
767 }
captureLayers(const sp<IBinder> &,sp<GraphicBuffer> *,ui::Dataspace,ui::PixelFormat,const Rect &,const std::unordered_set<sp<IBinder>,ISurfaceComposer::SpHash<IBinder>> &,float,bool)768 virtual status_t captureLayers(
769 const sp<IBinder>& /*parentHandle*/, sp<GraphicBuffer>* /*outBuffer*/,
770 ui::Dataspace /*reqDataspace*/, ui::PixelFormat /*reqPixelFormat*/,
771 const Rect& /*sourceCrop*/,
772 const std::unordered_set<sp<IBinder>,
773 ISurfaceComposer::SpHash<IBinder>>& /*excludeHandles*/,
774 float /*frameScale*/, bool /*childrenOnly*/) override {
775 return NO_ERROR;
776 }
clearAnimationFrameStats()777 status_t clearAnimationFrameStats() override { return NO_ERROR; }
getAnimationFrameStats(FrameStats *) const778 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
779 return NO_ERROR;
780 }
getHdrCapabilities(const sp<IBinder> &,HdrCapabilities *) const781 status_t getHdrCapabilities(const sp<IBinder>& /*display*/,
782 HdrCapabilities* /*outCapabilities*/) const override {
783 return NO_ERROR;
784 }
enableVSyncInjections(bool)785 status_t enableVSyncInjections(bool /*enable*/) override {
786 return NO_ERROR;
787 }
injectVSync(nsecs_t)788 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
getLayerDebugInfo(std::vector<LayerDebugInfo> *)789 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) override {
790 return NO_ERROR;
791 }
getCompositionPreference(ui::Dataspace *,ui::PixelFormat *,ui::Dataspace *,ui::PixelFormat *) const792 status_t getCompositionPreference(
793 ui::Dataspace* /*outDefaultDataspace*/, ui::PixelFormat* /*outDefaultPixelFormat*/,
794 ui::Dataspace* /*outWideColorGamutDataspace*/,
795 ui::PixelFormat* /*outWideColorGamutPixelFormat*/) const override {
796 return NO_ERROR;
797 }
getDisplayedContentSamplingAttributes(const sp<IBinder> &,ui::PixelFormat *,ui::Dataspace *,uint8_t *) const798 status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& /*display*/,
799 ui::PixelFormat* /*outFormat*/,
800 ui::Dataspace* /*outDataspace*/,
801 uint8_t* /*outComponentMask*/) const override {
802 return NO_ERROR;
803 }
setDisplayContentSamplingEnabled(const sp<IBinder> &,bool,uint8_t,uint64_t)804 status_t setDisplayContentSamplingEnabled(const sp<IBinder>& /*display*/, bool /*enable*/,
805 uint8_t /*componentMask*/,
806 uint64_t /*maxFrames*/) override {
807 return NO_ERROR;
808 }
getDisplayedContentSample(const sp<IBinder> &,uint64_t,uint64_t,DisplayedFrameStats *) const809 status_t getDisplayedContentSample(const sp<IBinder>& /*display*/, uint64_t /*maxFrames*/,
810 uint64_t /*timestamp*/,
811 DisplayedFrameStats* /*outStats*/) const override {
812 return NO_ERROR;
813 }
814
getColorManagement(bool *) const815 status_t getColorManagement(bool* /*outGetColorManagement*/) const override { return NO_ERROR; }
getProtectedContentSupport(bool *) const816 status_t getProtectedContentSupport(bool* /*outSupported*/) const override { return NO_ERROR; }
817
isWideColorDisplay(const sp<IBinder> &,bool *) const818 status_t isWideColorDisplay(const sp<IBinder>&, bool*) const override { return NO_ERROR; }
getDisplayBrightnessSupport(const sp<IBinder> &,bool *) const819 status_t getDisplayBrightnessSupport(const sp<IBinder>& /*displayToken*/,
820 bool* /*outSupport*/) const override {
821 return NO_ERROR;
822 }
setDisplayBrightness(const sp<IBinder> &,float)823 status_t setDisplayBrightness(const sp<IBinder>& /*displayToken*/,
824 float /*brightness*/) override {
825 return NO_ERROR;
826 }
827
addRegionSamplingListener(const Rect &,const sp<IBinder> &,const sp<IRegionSamplingListener> &)828 status_t addRegionSamplingListener(const Rect& /*samplingArea*/,
829 const sp<IBinder>& /*stopLayerHandle*/,
830 const sp<IRegionSamplingListener>& /*listener*/) override {
831 return NO_ERROR;
832 }
removeRegionSamplingListener(const sp<IRegionSamplingListener> &)833 status_t removeRegionSamplingListener(
834 const sp<IRegionSamplingListener>& /*listener*/) override {
835 return NO_ERROR;
836 }
setDesiredDisplayConfigSpecs(const sp<IBinder> &,int32_t,float,float,float,float)837 status_t setDesiredDisplayConfigSpecs(const sp<IBinder>& /*displayToken*/,
838 int32_t /*defaultConfig*/,
839 float /*primaryRefreshRateMin*/,
840 float /*primaryRefreshRateMax*/,
841 float /*appRequestRefreshRateMin*/,
842 float /*appRequestRefreshRateMax*/) {
843 return NO_ERROR;
844 }
getDesiredDisplayConfigSpecs(const sp<IBinder> &,int32_t *,float *,float *,float *,float *)845 status_t getDesiredDisplayConfigSpecs(const sp<IBinder>& /*displayToken*/,
846 int32_t* /*outDefaultConfig*/,
847 float* /*outPrimaryRefreshRateMin*/,
848 float* /*outPrimaryRefreshRateMax*/,
849 float* /*outAppRequestRefreshRateMin*/,
850 float* /*outAppRequestRefreshRateMax*/) override {
851 return NO_ERROR;
852 };
notifyPowerHint(int32_t)853 status_t notifyPowerHint(int32_t /*hintId*/) override { return NO_ERROR; }
854
setGlobalShadowSettings(const half4 &,const half4 &,float,float,float)855 status_t setGlobalShadowSettings(const half4& /*ambientColor*/, const half4& /*spotColor*/,
856 float /*lightPosY*/, float /*lightPosZ*/,
857 float /*lightRadius*/) override {
858 return NO_ERROR;
859 }
860
setFrameRate(const sp<IGraphicBufferProducer> &,float,int8_t)861 status_t setFrameRate(const sp<IGraphicBufferProducer>& /*surface*/, float /*frameRate*/,
862 int8_t /*compatibility*/) override {
863 return NO_ERROR;
864 }
865
acquireFrameRateFlexibilityToken(sp<IBinder> *)866 status_t acquireFrameRateFlexibilityToken(sp<IBinder>* /*outToken*/) { return NO_ERROR; }
867
868 protected:
onAsBinder()869 IBinder* onAsBinder() override { return nullptr; }
870
871 private:
872 bool mSupportsPresent{true};
873 };
874
875 class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
876 public:
FakeProducerFrameEventHistory(FenceToFenceTimeMap * fenceMap)877 explicit FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap) : mFenceMap(fenceMap) {}
878
~FakeProducerFrameEventHistory()879 ~FakeProducerFrameEventHistory() {}
880
updateAcquireFence(uint64_t frameNumber,std::shared_ptr<FenceTime> && acquire)881 void updateAcquireFence(uint64_t frameNumber,
882 std::shared_ptr<FenceTime>&& acquire) override {
883 // Verify the acquire fence being added isn't the one from the consumer.
884 EXPECT_NE(mConsumerAcquireFence, acquire);
885 // Override the fence, so we can verify this was called by the
886 // producer after the frame is queued.
887 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
888 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
889 }
890
setAcquireFenceOverride(const std::shared_ptr<FenceTime> & acquireFenceOverride,const std::shared_ptr<FenceTime> & consumerAcquireFence)891 void setAcquireFenceOverride(
892 const std::shared_ptr<FenceTime>& acquireFenceOverride,
893 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
894 mAcquireFenceOverride = acquireFenceOverride;
895 mConsumerAcquireFence = consumerAcquireFence;
896 }
897
898 protected:
createFenceTime(const sp<Fence> & fence) const899 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
900 const override {
901 return mFenceMap->createFenceTimeForTest(fence);
902 }
903
904 FenceToFenceTimeMap* mFenceMap{nullptr};
905
906 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
907 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
908 };
909
910
911 class TestSurface : public Surface {
912 public:
TestSurface(const sp<IGraphicBufferProducer> & bufferProducer,FenceToFenceTimeMap * fenceMap)913 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
914 FenceToFenceTimeMap* fenceMap)
915 : Surface(bufferProducer),
916 mFakeSurfaceComposer(new FakeSurfaceComposer) {
917 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
918 mFrameEventHistory.reset(mFakeFrameEventHistory);
919 }
920
~TestSurface()921 ~TestSurface() override {}
922
composerService() const923 sp<ISurfaceComposer> composerService() const override {
924 return mFakeSurfaceComposer;
925 }
926
now() const927 nsecs_t now() const override {
928 return mNow;
929 }
930
setNow(nsecs_t now)931 void setNow(nsecs_t now) {
932 mNow = now;
933 }
934
935 public:
936 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
937 nsecs_t mNow = 0;
938
939 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
940 // but this raw pointer gives access to test functionality.
941 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
942 };
943
944
945 class GetFrameTimestampsTest : public ::testing::Test {
946 protected:
947 struct FenceAndFenceTime {
FenceAndFenceTimeandroid::GetFrameTimestampsTest::FenceAndFenceTime948 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
949 : mFence(new Fence),
950 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
951 sp<Fence> mFence { nullptr };
952 std::shared_ptr<FenceTime> mFenceTime { nullptr };
953 };
954
955 struct RefreshEvents {
RefreshEventsandroid::GetFrameTimestampsTest::RefreshEvents956 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
957 : mFenceMap(fenceMap),
958 kCompositorTiming(
959 {refreshStart, refreshStart + 1, refreshStart + 2 }),
960 kStartTime(refreshStart + 3),
961 kGpuCompositionDoneTime(refreshStart + 4),
962 kPresentTime(refreshStart + 5) {}
963
signalPostCompositeFencesandroid::GetFrameTimestampsTest::RefreshEvents964 void signalPostCompositeFences() {
965 mFenceMap.signalAllForTest(
966 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
967 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
968 }
969
970 FenceToFenceTimeMap& mFenceMap;
971
972 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
973 FenceAndFenceTime mPresent { mFenceMap };
974
975 const CompositorTiming kCompositorTiming;
976
977 const nsecs_t kStartTime;
978 const nsecs_t kGpuCompositionDoneTime;
979 const nsecs_t kPresentTime;
980 };
981
982 struct FrameEvents {
FrameEventsandroid::GetFrameTimestampsTest::FrameEvents983 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
984 : mFenceMap(fenceMap),
985 kPostedTime(frameStartTime + 100),
986 kRequestedPresentTime(frameStartTime + 200),
987 kProducerAcquireTime(frameStartTime + 300),
988 kConsumerAcquireTime(frameStartTime + 301),
989 kLatchTime(frameStartTime + 500),
990 kDequeueReadyTime(frameStartTime + 600),
991 kReleaseTime(frameStartTime + 700),
992 mRefreshes {
993 { mFenceMap, frameStartTime + 410 },
994 { mFenceMap, frameStartTime + 420 },
995 { mFenceMap, frameStartTime + 430 } } {}
996
signalQueueFencesandroid::GetFrameTimestampsTest::FrameEvents997 void signalQueueFences() {
998 mFenceMap.signalAllForTest(
999 mAcquireConsumer.mFence, kConsumerAcquireTime);
1000 mFenceMap.signalAllForTest(
1001 mAcquireProducer.mFence, kProducerAcquireTime);
1002 }
1003
signalRefreshFencesandroid::GetFrameTimestampsTest::FrameEvents1004 void signalRefreshFences() {
1005 for (auto& re : mRefreshes) {
1006 re.signalPostCompositeFences();
1007 }
1008 }
1009
signalReleaseFencesandroid::GetFrameTimestampsTest::FrameEvents1010 void signalReleaseFences() {
1011 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
1012 }
1013
1014 FenceToFenceTimeMap& mFenceMap;
1015
1016 FenceAndFenceTime mAcquireConsumer { mFenceMap };
1017 FenceAndFenceTime mAcquireProducer { mFenceMap };
1018 FenceAndFenceTime mRelease { mFenceMap };
1019
1020 const nsecs_t kPostedTime;
1021 const nsecs_t kRequestedPresentTime;
1022 const nsecs_t kProducerAcquireTime;
1023 const nsecs_t kConsumerAcquireTime;
1024 const nsecs_t kLatchTime;
1025 const nsecs_t kDequeueReadyTime;
1026 const nsecs_t kReleaseTime;
1027
1028 RefreshEvents mRefreshes[3];
1029 };
1030
GetFrameTimestampsTest()1031 GetFrameTimestampsTest() {}
1032
SetUp()1033 virtual void SetUp() {
1034 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
1035 mFakeConsumer = new FakeConsumer;
1036 mCfeh = &mFakeConsumer->mFrameEventHistory;
1037 mConsumer->consumerConnect(mFakeConsumer, false);
1038 mConsumer->setConsumerName(String8("TestConsumer"));
1039 mSurface = new TestSurface(mProducer, &mFenceMap);
1040 mWindow = mSurface;
1041
1042 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
1043 NATIVE_WINDOW_API_CPU));
1044 native_window_set_buffer_count(mWindow.get(), 4);
1045 }
1046
disableFrameTimestamps()1047 void disableFrameTimestamps() {
1048 mFakeConsumer->mGetFrameTimestampsEnabled = false;
1049 native_window_enable_frame_timestamps(mWindow.get(), 0);
1050 mFrameTimestampsEnabled = false;
1051 }
1052
enableFrameTimestamps()1053 void enableFrameTimestamps() {
1054 mFakeConsumer->mGetFrameTimestampsEnabled = true;
1055 native_window_enable_frame_timestamps(mWindow.get(), 1);
1056 mFrameTimestampsEnabled = true;
1057 }
1058
getAllFrameTimestamps(uint64_t frameId)1059 int getAllFrameTimestamps(uint64_t frameId) {
1060 return native_window_get_frame_timestamps(mWindow.get(), frameId,
1061 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
1062 &outFirstRefreshStartTime, &outLastRefreshStartTime,
1063 &outGpuCompositionDoneTime, &outDisplayPresentTime,
1064 &outDequeueReadyTime, &outReleaseTime);
1065 }
1066
resetTimestamps()1067 void resetTimestamps() {
1068 outRequestedPresentTime = -1;
1069 outAcquireTime = -1;
1070 outLatchTime = -1;
1071 outFirstRefreshStartTime = -1;
1072 outLastRefreshStartTime = -1;
1073 outGpuCompositionDoneTime = -1;
1074 outDisplayPresentTime = -1;
1075 outDequeueReadyTime = -1;
1076 outReleaseTime = -1;
1077 }
1078
getNextFrameId()1079 uint64_t getNextFrameId() {
1080 uint64_t frameId = -1;
1081 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
1082 EXPECT_EQ(status, NO_ERROR);
1083 return frameId;
1084 }
1085
dequeueAndQueue(uint64_t frameIndex)1086 void dequeueAndQueue(uint64_t frameIndex) {
1087 int fence = -1;
1088 ANativeWindowBuffer* buffer = nullptr;
1089 ASSERT_EQ(NO_ERROR,
1090 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1091
1092 int oldAddFrameTimestampsCount =
1093 mFakeConsumer->mAddFrameTimestampsCount;
1094
1095 FrameEvents* frame = &mFrames[frameIndex];
1096 uint64_t frameNumber = frameIndex + 1;
1097
1098 NewFrameEventsEntry fe;
1099 fe.frameNumber = frameNumber;
1100 fe.postedTime = frame->kPostedTime;
1101 fe.requestedPresentTime = frame->kRequestedPresentTime;
1102 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
1103 mFakeConsumer->mNewFrameEntryOverride = fe;
1104
1105 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1106 frame->mAcquireProducer.mFenceTime,
1107 frame->mAcquireConsumer.mFenceTime);
1108
1109 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1110
1111 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
1112
1113 EXPECT_EQ(
1114 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
1115 mFakeConsumer->mAddFrameTimestampsCount);
1116 }
1117
addFrameEvents(bool gpuComposited,uint64_t iOldFrame,int64_t iNewFrame)1118 void addFrameEvents(
1119 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
1120 FrameEvents* oldFrame =
1121 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
1122 FrameEvents* newFrame = &mFrames[iNewFrame];
1123
1124 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
1125 uint64_t nNewFrame = iNewFrame + 1;
1126
1127 // Latch, Composite, and Release the frames in a plausible order.
1128 // Note: The timestamps won't necessarily match the order, but
1129 // that's okay for the purposes of this test.
1130 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
1131
1132 // Composite the previous frame one more time, which helps verify
1133 // LastRefresh is updated properly.
1134 if (oldFrame != nullptr) {
1135 mCfeh->addPreComposition(nOldFrame,
1136 oldFrame->mRefreshes[2].kStartTime);
1137 gpuDoneFenceTime = gpuComposited ?
1138 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
1139 FenceTime::NO_FENCE;
1140 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
1141 oldFrame->mRefreshes[2].mPresent.mFenceTime,
1142 oldFrame->mRefreshes[2].kCompositorTiming);
1143 }
1144
1145 // Latch the new frame.
1146 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
1147
1148 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
1149 gpuDoneFenceTime = gpuComposited ?
1150 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
1151 FenceTime::NO_FENCE;
1152 // HWC2 releases the previous buffer after a new latch just before
1153 // calling postComposition.
1154 if (oldFrame != nullptr) {
1155 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
1156 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
1157 }
1158 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
1159 newFrame->mRefreshes[0].mPresent.mFenceTime,
1160 newFrame->mRefreshes[0].kCompositorTiming);
1161
1162 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
1163 gpuDoneFenceTime = gpuComposited ?
1164 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
1165 FenceTime::NO_FENCE;
1166 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
1167 newFrame->mRefreshes[1].mPresent.mFenceTime,
1168 newFrame->mRefreshes[1].kCompositorTiming);
1169 }
1170
1171 sp<IGraphicBufferProducer> mProducer;
1172 sp<IGraphicBufferConsumer> mConsumer;
1173 sp<FakeConsumer> mFakeConsumer;
1174 ConsumerFrameEventHistory* mCfeh;
1175 sp<TestSurface> mSurface;
1176 sp<ANativeWindow> mWindow;
1177
1178 FenceToFenceTimeMap mFenceMap;
1179
1180 bool mFrameTimestampsEnabled = false;
1181
1182 int64_t outRequestedPresentTime = -1;
1183 int64_t outAcquireTime = -1;
1184 int64_t outLatchTime = -1;
1185 int64_t outFirstRefreshStartTime = -1;
1186 int64_t outLastRefreshStartTime = -1;
1187 int64_t outGpuCompositionDoneTime = -1;
1188 int64_t outDisplayPresentTime = -1;
1189 int64_t outDequeueReadyTime = -1;
1190 int64_t outReleaseTime = -1;
1191
1192 FrameEvents mFrames[3] {
1193 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
1194 };
1195
1196
1197 // This test verifies that the frame timestamps are not retrieved when not
1198 // explicitly enabled via native_window_enable_frame_timestamps.
1199 // We want to check this to make sure there's no overhead for users
1200 // that don't need the timestamp information.
TEST_F(GetFrameTimestampsTest,DefaultDisabled)1201 TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
1202 int fence;
1203 ANativeWindowBuffer* buffer;
1204
1205 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1206 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1207
1208 const uint64_t fId = getNextFrameId();
1209
1210 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
1211 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1212 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1213 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1214
1215 // Verify the producer doesn't get frame timestamps piggybacked on queue.
1216 // It is okay that frame timestamps are added in the consumer since it is
1217 // still needed for SurfaceFlinger dumps.
1218 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1219 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1220 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1221
1222 // Verify attempts to get frame timestamps fail.
1223 int result = getAllFrameTimestamps(fId);
1224 EXPECT_EQ(INVALID_OPERATION, result);
1225 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1226
1227 // Verify compositor timing query fails.
1228 nsecs_t compositeDeadline = 0;
1229 nsecs_t compositeInterval = 0;
1230 nsecs_t compositeToPresentLatency = 0;
1231 result = native_window_get_compositor_timing(mWindow.get(),
1232 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1233 EXPECT_EQ(INVALID_OPERATION, result);
1234 }
1235
1236 // This test verifies that the frame timestamps are retrieved if explicitly
1237 // enabled via native_window_enable_frame_timestamps.
TEST_F(GetFrameTimestampsTest,EnabledSimple)1238 TEST_F(GetFrameTimestampsTest, EnabledSimple) {
1239 CompositorTiming initialCompositorTiming {
1240 1000000000, // 1s deadline
1241 16666667, // 16ms interval
1242 50000000, // 50ms present latency
1243 };
1244 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1245
1246 enableFrameTimestamps();
1247
1248 // Verify the compositor timing query gets the initial compositor values
1249 // after timststamps are enabled; even before the first frame is queued
1250 // or dequeued.
1251 nsecs_t compositeDeadline = 0;
1252 nsecs_t compositeInterval = 0;
1253 nsecs_t compositeToPresentLatency = 0;
1254 mSurface->setNow(initialCompositorTiming.deadline - 1);
1255 int result = native_window_get_compositor_timing(mWindow.get(),
1256 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1257 EXPECT_EQ(NO_ERROR, result);
1258 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1259 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1260 EXPECT_EQ(initialCompositorTiming.presentLatency,
1261 compositeToPresentLatency);
1262
1263 int fence;
1264 ANativeWindowBuffer* buffer;
1265
1266 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1267 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
1268
1269 const uint64_t fId1 = getNextFrameId();
1270
1271 // Verify getFrameTimestamps is piggybacked on dequeue.
1272 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1273 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1274 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
1275
1276 NewFrameEventsEntry f1;
1277 f1.frameNumber = 1;
1278 f1.postedTime = mFrames[0].kPostedTime;
1279 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1280 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1281 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1282 mFrames[0].mAcquireProducer.mFenceTime,
1283 mFrames[0].mAcquireConsumer.mFenceTime);
1284 mFakeConsumer->mNewFrameEntryOverride = f1;
1285 mFrames[0].signalQueueFences();
1286
1287 // Verify getFrameTimestamps is piggybacked on queue.
1288 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1289 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1290 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
1291 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
1292
1293 // Verify queries for timestamps that the producer doesn't know about
1294 // triggers a call to see if the consumer has any new timestamps.
1295 result = getAllFrameTimestamps(fId1);
1296 EXPECT_EQ(NO_ERROR, result);
1297 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
1298 }
1299
TEST_F(GetFrameTimestampsTest,QueryPresentSupported)1300 TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1301 bool displayPresentSupported = true;
1302 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1303
1304 // Verify supported bits are forwarded.
1305 int supportsPresent = -1;
1306 mWindow.get()->query(mWindow.get(),
1307 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1308 EXPECT_EQ(displayPresentSupported, supportsPresent);
1309 }
1310
TEST_F(GetFrameTimestampsTest,QueryPresentNotSupported)1311 TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1312 bool displayPresentSupported = false;
1313 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1314
1315 // Verify supported bits are forwarded.
1316 int supportsPresent = -1;
1317 mWindow.get()->query(mWindow.get(),
1318 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1319 EXPECT_EQ(displayPresentSupported, supportsPresent);
1320 }
1321
TEST_F(GetFrameTimestampsTest,SnapToNextTickBasic)1322 TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1323 nsecs_t phase = 4000;
1324 nsecs_t interval = 1000;
1325
1326 // Timestamp in previous interval.
1327 nsecs_t timestamp = 3500;
1328 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1329 timestamp, phase, interval));
1330
1331 // Timestamp in next interval.
1332 timestamp = 4500;
1333 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1334 timestamp, phase, interval));
1335
1336 // Timestamp multiple intervals before.
1337 timestamp = 2500;
1338 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1339 timestamp, phase, interval));
1340
1341 // Timestamp multiple intervals after.
1342 timestamp = 6500;
1343 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1344 timestamp, phase, interval));
1345
1346 // Timestamp on previous interval.
1347 timestamp = 3000;
1348 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1349 timestamp, phase, interval));
1350
1351 // Timestamp on next interval.
1352 timestamp = 5000;
1353 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1354 timestamp, phase, interval));
1355
1356 // Timestamp equal to phase.
1357 timestamp = 4000;
1358 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1359 timestamp, phase, interval));
1360 }
1361
1362 // int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1363 // if the number of intervals elapsed is internally stored in an int.
TEST_F(GetFrameTimestampsTest,SnapToNextTickOverflow)1364 TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1365 nsecs_t phase = 0;
1366 nsecs_t interval = 4000;
1367 nsecs_t big_timestamp = 8635916564000;
1368 int32_t intervals = big_timestamp / interval;
1369
1370 EXPECT_LT(intervals, 0);
1371 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1372 big_timestamp, phase, interval));
1373 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1374 big_timestamp, big_timestamp, interval));
1375 }
1376
1377 // This verifies the compositor timing is updated by refresh events
1378 // and piggy backed on a queue, dequeue, and enabling of timestamps..
TEST_F(GetFrameTimestampsTest,CompositorTimingUpdatesBasic)1379 TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1380 CompositorTiming initialCompositorTiming {
1381 1000000000, // 1s deadline
1382 16666667, // 16ms interval
1383 50000000, // 50ms present latency
1384 };
1385 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1386
1387 enableFrameTimestamps();
1388
1389 // We get the initial values before any frames are submitted.
1390 nsecs_t compositeDeadline = 0;
1391 nsecs_t compositeInterval = 0;
1392 nsecs_t compositeToPresentLatency = 0;
1393 mSurface->setNow(initialCompositorTiming.deadline - 1);
1394 int result = native_window_get_compositor_timing(mWindow.get(),
1395 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1396 EXPECT_EQ(NO_ERROR, result);
1397 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1398 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1399 EXPECT_EQ(initialCompositorTiming.presentLatency,
1400 compositeToPresentLatency);
1401
1402 dequeueAndQueue(0);
1403 addFrameEvents(true, NO_FRAME_INDEX, 0);
1404
1405 // Still get the initial values because the frame events for frame 0
1406 // didn't get a chance to piggyback on a queue or dequeue yet.
1407 result = native_window_get_compositor_timing(mWindow.get(),
1408 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1409 EXPECT_EQ(NO_ERROR, result);
1410 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1411 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1412 EXPECT_EQ(initialCompositorTiming.presentLatency,
1413 compositeToPresentLatency);
1414
1415 dequeueAndQueue(1);
1416 addFrameEvents(true, 0, 1);
1417
1418 // Now expect the composite values associated with frame 1.
1419 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1420 result = native_window_get_compositor_timing(mWindow.get(),
1421 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1422 EXPECT_EQ(NO_ERROR, result);
1423 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1424 compositeDeadline);
1425 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1426 compositeInterval);
1427 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1428 compositeToPresentLatency);
1429
1430 dequeueAndQueue(2);
1431 addFrameEvents(true, 1, 2);
1432
1433 // Now expect the composite values associated with frame 2.
1434 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1435 result = native_window_get_compositor_timing(mWindow.get(),
1436 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1437 EXPECT_EQ(NO_ERROR, result);
1438 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1439 compositeDeadline);
1440 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1441 compositeInterval);
1442 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1443 compositeToPresentLatency);
1444
1445 // Re-enabling frame timestamps should get the latest values.
1446 disableFrameTimestamps();
1447 enableFrameTimestamps();
1448
1449 // Now expect the composite values associated with frame 3.
1450 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1451 result = native_window_get_compositor_timing(mWindow.get(),
1452 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1453 EXPECT_EQ(NO_ERROR, result);
1454 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1455 compositeDeadline);
1456 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1457 compositeInterval);
1458 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1459 compositeToPresentLatency);
1460 }
1461
1462 // This verifies the compositor deadline properly snaps to the the next
1463 // deadline based on the current time.
TEST_F(GetFrameTimestampsTest,CompositorTimingDeadlineSnaps)1464 TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1465 CompositorTiming initialCompositorTiming {
1466 1000000000, // 1s deadline
1467 16666667, // 16ms interval
1468 50000000, // 50ms present latency
1469 };
1470 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1471
1472 enableFrameTimestamps();
1473
1474 nsecs_t compositeDeadline = 0;
1475 nsecs_t compositeInterval = 0;
1476 nsecs_t compositeToPresentLatency = 0;
1477
1478 // A "now" just before the deadline snaps to the deadline.
1479 mSurface->setNow(initialCompositorTiming.deadline - 1);
1480 int result = native_window_get_compositor_timing(mWindow.get(),
1481 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1482 EXPECT_EQ(NO_ERROR, result);
1483 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1484 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1485 EXPECT_EQ(expectedDeadline, compositeDeadline);
1486
1487 dequeueAndQueue(0);
1488 addFrameEvents(true, NO_FRAME_INDEX, 0);
1489
1490 // A "now" just after the deadline snaps properly.
1491 mSurface->setNow(initialCompositorTiming.deadline + 1);
1492 result = native_window_get_compositor_timing(mWindow.get(),
1493 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1494 EXPECT_EQ(NO_ERROR, result);
1495 expectedDeadline =
1496 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1497 EXPECT_EQ(expectedDeadline, compositeDeadline);
1498
1499 dequeueAndQueue(1);
1500 addFrameEvents(true, 0, 1);
1501
1502 // A "now" just after the next interval snaps properly.
1503 mSurface->setNow(
1504 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1505 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1506 result = native_window_get_compositor_timing(mWindow.get(),
1507 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1508 EXPECT_EQ(NO_ERROR, result);
1509 expectedDeadline =
1510 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1511 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1512 EXPECT_EQ(expectedDeadline, compositeDeadline);
1513
1514 dequeueAndQueue(2);
1515 addFrameEvents(true, 1, 2);
1516
1517 // A "now" over 1 interval before the deadline snaps properly.
1518 mSurface->setNow(
1519 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1520 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1521 result = native_window_get_compositor_timing(mWindow.get(),
1522 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1523 EXPECT_EQ(NO_ERROR, result);
1524 expectedDeadline =
1525 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1526 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1527 EXPECT_EQ(expectedDeadline, compositeDeadline);
1528
1529 // Re-enabling frame timestamps should get the latest values.
1530 disableFrameTimestamps();
1531 enableFrameTimestamps();
1532
1533 // A "now" over 2 intervals before the deadline snaps properly.
1534 mSurface->setNow(
1535 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1536 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1537 result = native_window_get_compositor_timing(mWindow.get(),
1538 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1539 EXPECT_EQ(NO_ERROR, result);
1540 expectedDeadline =
1541 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1542 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1543 EXPECT_EQ(expectedDeadline, compositeDeadline);
1544 }
1545
1546 // This verifies the timestamps recorded in the consumer's
1547 // FrameTimestampsHistory are properly retrieved by the producer for the
1548 // correct frames.
TEST_F(GetFrameTimestampsTest,TimestampsAssociatedWithCorrectFrame)1549 TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1550 enableFrameTimestamps();
1551
1552 const uint64_t fId1 = getNextFrameId();
1553 dequeueAndQueue(0);
1554 mFrames[0].signalQueueFences();
1555
1556 const uint64_t fId2 = getNextFrameId();
1557 dequeueAndQueue(1);
1558 mFrames[1].signalQueueFences();
1559
1560 addFrameEvents(true, NO_FRAME_INDEX, 0);
1561 mFrames[0].signalRefreshFences();
1562 addFrameEvents(true, 0, 1);
1563 mFrames[0].signalReleaseFences();
1564 mFrames[1].signalRefreshFences();
1565
1566 // Verify timestamps are correct for frame 1.
1567 resetTimestamps();
1568 int result = getAllFrameTimestamps(fId1);
1569 EXPECT_EQ(NO_ERROR, result);
1570 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1571 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1572 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1573 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1574 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
1575 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1576 outGpuCompositionDoneTime);
1577 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1578 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
1579 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1580
1581 // Verify timestamps are correct for frame 2.
1582 resetTimestamps();
1583 result = getAllFrameTimestamps(fId2);
1584 EXPECT_EQ(NO_ERROR, result);
1585 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1586 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1587 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1588 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1589 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
1590 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1591 outGpuCompositionDoneTime);
1592 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1593 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1594 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
1595 }
1596
1597 // This test verifies the acquire fence recorded by the consumer is not sent
1598 // back to the producer and the producer saves its own fence.
TEST_F(GetFrameTimestampsTest,QueueTimestampsNoSync)1599 TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1600 enableFrameTimestamps();
1601
1602 // Dequeue and queue frame 1.
1603 const uint64_t fId1 = getNextFrameId();
1604 dequeueAndQueue(0);
1605
1606 // Verify queue-related timestamps for f1 are available immediately in the
1607 // producer without asking the consumer again, even before signaling the
1608 // acquire fence.
1609 resetTimestamps();
1610 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1611 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1612 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
1613 nullptr, nullptr, nullptr, nullptr, nullptr);
1614 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1615 EXPECT_EQ(NO_ERROR, result);
1616 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1617 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
1618
1619 // Signal acquire fences. Verify a sync call still isn't necessary.
1620 mFrames[0].signalQueueFences();
1621
1622 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1623 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1624 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
1625 nullptr, nullptr, nullptr, nullptr, nullptr);
1626 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1627 EXPECT_EQ(NO_ERROR, result);
1628 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1629 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1630
1631 // Dequeue and queue frame 2.
1632 const uint64_t fId2 = getNextFrameId();
1633 dequeueAndQueue(1);
1634
1635 // Verify queue-related timestamps for f2 are available immediately in the
1636 // producer without asking the consumer again, even before signaling the
1637 // acquire fence.
1638 resetTimestamps();
1639 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1640 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
1641 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
1642 nullptr, nullptr, nullptr, nullptr, nullptr);
1643 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1644 EXPECT_EQ(NO_ERROR, result);
1645 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1646 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
1647
1648 // Signal acquire fences. Verify a sync call still isn't necessary.
1649 mFrames[1].signalQueueFences();
1650
1651 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1652 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
1653 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
1654 nullptr, nullptr, nullptr, nullptr, nullptr);
1655 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1656 EXPECT_EQ(NO_ERROR, result);
1657 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1658 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1659 }
1660
TEST_F(GetFrameTimestampsTest,ZeroRequestedTimestampsNoSync)1661 TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1662 enableFrameTimestamps();
1663
1664 // Dequeue and queue frame 1.
1665 dequeueAndQueue(0);
1666 mFrames[0].signalQueueFences();
1667
1668 // Dequeue and queue frame 2.
1669 const uint64_t fId2 = getNextFrameId();
1670 dequeueAndQueue(1);
1671 mFrames[1].signalQueueFences();
1672
1673 addFrameEvents(true, NO_FRAME_INDEX, 0);
1674 mFrames[0].signalRefreshFences();
1675 addFrameEvents(true, 0, 1);
1676 mFrames[0].signalReleaseFences();
1677 mFrames[1].signalRefreshFences();
1678
1679 // Verify a request for no timestamps doesn't result in a sync call.
1680 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1681 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
1682 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1683 nullptr, nullptr);
1684 EXPECT_EQ(NO_ERROR, result);
1685 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1686 }
1687
1688 // This test verifies that fences can signal and update timestamps producer
1689 // side without an additional sync call to the consumer.
TEST_F(GetFrameTimestampsTest,FencesInProducerNoSync)1690 TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1691 enableFrameTimestamps();
1692
1693 // Dequeue and queue frame 1.
1694 const uint64_t fId1 = getNextFrameId();
1695 dequeueAndQueue(0);
1696 mFrames[0].signalQueueFences();
1697
1698 // Dequeue and queue frame 2.
1699 dequeueAndQueue(1);
1700 mFrames[1].signalQueueFences();
1701
1702 addFrameEvents(true, NO_FRAME_INDEX, 0);
1703 addFrameEvents(true, 0, 1);
1704
1705 // Verify available timestamps are correct for frame 1, before any
1706 // fence has been signaled.
1707 // Note: A sync call is necessary here since the events triggered by
1708 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1709 resetTimestamps();
1710 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1711 int result = getAllFrameTimestamps(fId1);
1712 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1713 EXPECT_EQ(NO_ERROR, result);
1714 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1715 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1716 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1717 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1718 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
1719 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1720 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
1721 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
1722 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
1723
1724 // Verify available timestamps are correct for frame 1 again, before any
1725 // fence has been signaled.
1726 // This time a sync call should not be necessary.
1727 resetTimestamps();
1728 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1729 result = getAllFrameTimestamps(fId1);
1730 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1731 EXPECT_EQ(NO_ERROR, result);
1732 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1733 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1734 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1735 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1736 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
1737 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1738 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
1739 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
1740 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
1741
1742 // Signal the fences for frame 1.
1743 mFrames[0].signalRefreshFences();
1744 mFrames[0].signalReleaseFences();
1745
1746 // Verify all timestamps are available without a sync call.
1747 resetTimestamps();
1748 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1749 result = getAllFrameTimestamps(fId1);
1750 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1751 EXPECT_EQ(NO_ERROR, result);
1752 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1753 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1754 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1755 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1756 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
1757 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1758 outGpuCompositionDoneTime);
1759 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1760 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
1761 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1762 }
1763
1764 // This test verifies that if the frame wasn't GPU composited but has a refresh
1765 // event a sync call isn't made to get the GPU composite done time since it will
1766 // never exist.
TEST_F(GetFrameTimestampsTest,NoGpuNoSync)1767 TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1768 enableFrameTimestamps();
1769
1770 // Dequeue and queue frame 1.
1771 const uint64_t fId1 = getNextFrameId();
1772 dequeueAndQueue(0);
1773 mFrames[0].signalQueueFences();
1774
1775 // Dequeue and queue frame 2.
1776 dequeueAndQueue(1);
1777 mFrames[1].signalQueueFences();
1778
1779 addFrameEvents(false, NO_FRAME_INDEX, 0);
1780 addFrameEvents(false, 0, 1);
1781
1782 // Verify available timestamps are correct for frame 1, before any
1783 // fence has been signaled.
1784 // Note: A sync call is necessary here since the events triggered by
1785 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1786 resetTimestamps();
1787 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1788 int result = getAllFrameTimestamps(fId1);
1789 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1790 EXPECT_EQ(NO_ERROR, result);
1791 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1792 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1793 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1794 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1795 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
1796 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1797 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
1798 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
1799 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
1800
1801 // Signal the fences for frame 1.
1802 mFrames[0].signalRefreshFences();
1803 mFrames[0].signalReleaseFences();
1804
1805 // Verify all timestamps, except GPU composition, are available without a
1806 // sync call.
1807 resetTimestamps();
1808 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1809 result = getAllFrameTimestamps(fId1);
1810 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1811 EXPECT_EQ(NO_ERROR, result);
1812 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1813 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1814 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1815 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1816 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
1817 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1818 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1819 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
1820 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1821 }
1822
1823 // This test verifies that if the certain timestamps can't possibly exist for
1824 // the most recent frame, then a sync call is not done.
TEST_F(GetFrameTimestampsTest,NoReleaseNoSync)1825 TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
1826 enableFrameTimestamps();
1827
1828 // Dequeue and queue frame 1.
1829 const uint64_t fId1 = getNextFrameId();
1830 dequeueAndQueue(0);
1831 mFrames[0].signalQueueFences();
1832
1833 // Dequeue and queue frame 2.
1834 const uint64_t fId2 = getNextFrameId();
1835 dequeueAndQueue(1);
1836 mFrames[1].signalQueueFences();
1837
1838 addFrameEvents(false, NO_FRAME_INDEX, 0);
1839 addFrameEvents(false, 0, 1);
1840
1841 // Verify available timestamps are correct for frame 1, before any
1842 // fence has been signaled.
1843 // Note: A sync call is necessary here since the events triggered by
1844 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1845 resetTimestamps();
1846 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1847 int result = getAllFrameTimestamps(fId1);
1848 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1849 EXPECT_EQ(NO_ERROR, result);
1850 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1851 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1852 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1853 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1854 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
1855 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1856 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
1857 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
1858 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
1859
1860 mFrames[0].signalRefreshFences();
1861 mFrames[0].signalReleaseFences();
1862 mFrames[1].signalRefreshFences();
1863
1864 // Verify querying for all timestmaps of f2 does not do a sync call. Even
1865 // though the lastRefresh, dequeueReady, and release times aren't
1866 // available, a sync call should not occur because it's not possible for f2
1867 // to encounter the final value for those events until another frame is
1868 // queued.
1869 resetTimestamps();
1870 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1871 result = getAllFrameTimestamps(fId2);
1872 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1873 EXPECT_EQ(NO_ERROR, result);
1874 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1875 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1876 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1877 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1878 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
1879 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1880 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
1881 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1882 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
1883 }
1884
1885 // This test verifies there are no sync calls for present times
1886 // when they aren't supported and that an error is returned.
1887
TEST_F(GetFrameTimestampsTest,PresentUnsupportedNoSync)1888 TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1889 enableFrameTimestamps();
1890 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1891
1892 // Dequeue and queue frame 1.
1893 const uint64_t fId1 = getNextFrameId();
1894 dequeueAndQueue(0);
1895
1896 // Verify a query for the Present times do not trigger a sync call if they
1897 // are not supported.
1898 resetTimestamps();
1899 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1900 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1901 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1902 &outDisplayPresentTime, nullptr, nullptr);
1903 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1904 EXPECT_EQ(BAD_VALUE, result);
1905 EXPECT_EQ(-1, outDisplayPresentTime);
1906 }
1907
TEST_F(SurfaceTest,DequeueWithConsumerDrivenSize)1908 TEST_F(SurfaceTest, DequeueWithConsumerDrivenSize) {
1909 sp<IGraphicBufferProducer> producer;
1910 sp<IGraphicBufferConsumer> consumer;
1911 BufferQueue::createBufferQueue(&producer, &consumer);
1912
1913 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
1914 consumer->consumerConnect(dummyConsumer, false);
1915 consumer->setDefaultBufferSize(10, 10);
1916
1917 sp<Surface> surface = new Surface(producer);
1918 sp<ANativeWindow> window(surface);
1919 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
1920 native_window_set_buffers_dimensions(window.get(), 0, 0);
1921
1922 int fence;
1923 ANativeWindowBuffer* buffer;
1924
1925 // Buffer size is driven by the consumer
1926 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1927 EXPECT_EQ(10, buffer->width);
1928 EXPECT_EQ(10, buffer->height);
1929 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1930
1931 // Buffer size is driven by the consumer
1932 consumer->setDefaultBufferSize(10, 20);
1933 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1934 EXPECT_EQ(10, buffer->width);
1935 EXPECT_EQ(20, buffer->height);
1936 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1937
1938 // Transform hint isn't synced to producer before queueBuffer or connect
1939 consumer->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_270);
1940 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1941 EXPECT_EQ(10, buffer->width);
1942 EXPECT_EQ(20, buffer->height);
1943 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
1944
1945 // Transform hint is synced to producer but no auto prerotation
1946 consumer->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_270);
1947 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1948 EXPECT_EQ(10, buffer->width);
1949 EXPECT_EQ(20, buffer->height);
1950 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1951
1952 // Prerotation is driven by the consumer with the transform hint used by producer
1953 native_window_set_auto_prerotation(window.get(), true);
1954 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1955 EXPECT_EQ(20, buffer->width);
1956 EXPECT_EQ(10, buffer->height);
1957 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1958
1959 // Turn off auto prerotaton
1960 native_window_set_auto_prerotation(window.get(), false);
1961 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1962 EXPECT_EQ(10, buffer->width);
1963 EXPECT_EQ(20, buffer->height);
1964 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1965
1966 // Test auto prerotation bit is disabled after disconnect
1967 native_window_set_auto_prerotation(window.get(), true);
1968 native_window_api_disconnect(window.get(), NATIVE_WINDOW_API_CPU);
1969 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
1970 consumer->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_270);
1971 native_window_set_buffers_dimensions(window.get(), 0, 0);
1972 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1973 EXPECT_EQ(10, buffer->width);
1974 EXPECT_EQ(20, buffer->height);
1975 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1976 }
1977
TEST_F(SurfaceTest,DefaultMaxBufferCountSetAndUpdated)1978 TEST_F(SurfaceTest, DefaultMaxBufferCountSetAndUpdated) {
1979 sp<IGraphicBufferProducer> producer;
1980 sp<IGraphicBufferConsumer> consumer;
1981 BufferQueue::createBufferQueue(&producer, &consumer);
1982
1983 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
1984 consumer->consumerConnect(dummyConsumer, false);
1985
1986 sp<Surface> surface = new Surface(producer);
1987 sp<ANativeWindow> window(surface);
1988
1989 int count = -1;
1990 ASSERT_EQ(NO_ERROR, window->query(window.get(), NATIVE_WINDOW_MAX_BUFFER_COUNT, &count));
1991 EXPECT_EQ(BufferQueueDefs::NUM_BUFFER_SLOTS, count);
1992
1993 consumer->setMaxBufferCount(10);
1994 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU));
1995 EXPECT_EQ(NO_ERROR, window->query(window.get(), NATIVE_WINDOW_MAX_BUFFER_COUNT, &count));
1996 EXPECT_EQ(10, count);
1997
1998 ASSERT_EQ(NO_ERROR, native_window_api_disconnect(window.get(), NATIVE_WINDOW_API_CPU));
1999 ASSERT_EQ(NO_ERROR, window->query(window.get(), NATIVE_WINDOW_MAX_BUFFER_COUNT, &count));
2000 EXPECT_EQ(BufferQueueDefs::NUM_BUFFER_SLOTS, count);
2001 }
2002
2003 } // namespace android
2004