blob: ec9cba56a2d3bdf1673ec44980e989e6dc30c07e [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
Peiyong Lind8460c82020-07-28 16:04:22 -070017#include "MockConsumer.h"
Dan Stozac6f30bd2015-06-08 09:32:50 -070018
Jamie Gennis134f0422011-03-08 12:18:54 -080019#include <gtest/gtest.h>
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080020
Sundong Ahnf33c9f12020-04-23 21:31:20 +090021#include <SurfaceFlingerProperties.h>
Huihong Luo6fac5232021-11-22 16:05:23 -080022#include <android/gui/IDisplayEventConnection.h>
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060023#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070024#include <binder/ProcessState.h>
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060025#include <configstore/Utils.h>
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -070026#include <gui/BufferItemConsumer.h>
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -070027#include <gui/IProducerListener.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080028#include <gui/ISurfaceComposer.h>
29#include <gui/Surface.h>
30#include <gui/SurfaceComposerClient.h>
chaviwe7b9f272020-08-18 16:08:59 -070031#include <gui/SyncScreenCaptureListener.h>
Sundong Ahnf33c9f12020-04-23 21:31:20 +090032#include <inttypes.h>
Brian Anderson3da8d272016-07-28 16:20:47 -070033#include <private/gui/ComposerService.h>
Huihong Luof5029222021-12-16 14:33:46 -080034#include <private/gui/ComposerServiceAIDL.h>
Andy Yu2ae6b6b2021-11-18 14:51:06 -080035#include <sys/types.h>
Yiwei Zhang74c9cc32020-06-20 03:35:17 -070036#include <ui/BufferQueueDefs.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010037#include <ui/DisplayMode.h>
Dan Stozac1879002014-05-22 15:59:05 -070038#include <ui/Rect.h>
Andy Yu2ae6b6b2021-11-18 14:51:06 -080039#include <utils/Errors.h>
Jamie Gennis134f0422011-03-08 12:18:54 -080040#include <utils/String8.h>
41
Brian Anderson3da8d272016-07-28 16:20:47 -070042#include <limits>
Kalle Raita643f0942016-12-07 09:20:14 -080043#include <thread>
44
Jamie Gennis134f0422011-03-08 12:18:54 -080045namespace android {
46
Kalle Raita643f0942016-12-07 09:20:14 -080047using namespace std::chrono_literals;
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060048// retrieve wide-color and hdr settings from configstore
49using namespace android::hardware::configstore;
50using namespace android::hardware::configstore::V1_0;
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -050051using aidl::android::hardware::graphics::common::DisplayDecorationSupport;
Huihong Luo6fac5232021-11-22 16:05:23 -080052using gui::IDisplayEventConnection;
Huihong Luoecc1f902021-11-20 11:55:05 -080053using gui::IRegionSamplingListener;
Peiyong Lin9f034472018-03-28 15:29:00 -070054using ui::ColorMode;
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -060055
Robert Carr4cdc58f2017-08-23 14:22:20 -070056using Transaction = SurfaceComposerClient::Transaction;
57
Sundong Ahnf33c9f12020-04-23 21:31:20 +090058static bool hasWideColorDisplay = android::sysprop::has_wide_color_display(false);
Kalle Raita643f0942016-12-07 09:20:14 -080059
Sundong Ahnf33c9f12020-04-23 21:31:20 +090060static bool hasHdrDisplay = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -070061
Brian Anderson3da8d272016-07-28 16:20:47 -070062class FakeSurfaceComposer;
63class FakeProducerFrameEventHistory;
64
65static constexpr uint64_t NO_FRAME_INDEX = std::numeric_limits<uint64_t>::max();
66
Peiyong Lind8460c82020-07-28 16:04:22 -070067class FakeSurfaceListener : public SurfaceListener {
Shuzhen Wang067fcd32019-08-14 10:41:12 -070068public:
Peiyong Lind8460c82020-07-28 16:04:22 -070069 FakeSurfaceListener(bool enableReleasedCb = false)
70 : mEnableReleaseCb(enableReleasedCb), mBuffersReleased(0) {}
71 virtual ~FakeSurfaceListener() = default;
Shuzhen Wang067fcd32019-08-14 10:41:12 -070072
73 virtual void onBufferReleased() {
74 mBuffersReleased++;
75 }
76 virtual bool needsReleaseNotify() {
77 return mEnableReleaseCb;
78 }
79 virtual void onBuffersDiscarded(const std::vector<sp<GraphicBuffer>>& buffers) {
80 mDiscardedBuffers.insert(mDiscardedBuffers.end(), buffers.begin(), buffers.end());
81 }
82
83 int getReleaseNotifyCount() const {
84 return mBuffersReleased;
85 }
86 const std::vector<sp<GraphicBuffer>>& getDiscardedBuffers() const {
87 return mDiscardedBuffers;
88 }
89private:
90 // No need to use lock given the test triggers the listener in the same
91 // thread context.
92 bool mEnableReleaseCb;
93 int32_t mBuffersReleased;
94 std::vector<sp<GraphicBuffer>> mDiscardedBuffers;
95};
96
Jamie Gennis134f0422011-03-08 12:18:54 -080097class SurfaceTest : public ::testing::Test {
98protected:
Mathias Agopian7c1a4872013-03-20 15:56:04 -070099 SurfaceTest() {
100 ProcessState::self()->startThreadPool();
101 }
102
Jamie Gennis134f0422011-03-08 12:18:54 -0800103 virtual void SetUp() {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800104 mComposerClient = new SurfaceComposerClient;
Jamie Gennis134f0422011-03-08 12:18:54 -0800105 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
106
Brian Andersond0010582017-03-07 13:20:31 -0800107 // TODO(brianderson): The following sometimes fails and is a source of
108 // test flakiness.
Jamie Gennisfc850122011-04-25 16:40:05 -0700109 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700110 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
arthurhungdba591c2021-02-08 17:28:49 +0800111 SurfaceComposerClient::Transaction().apply(true);
Jamie Gennis134f0422011-03-08 12:18:54 -0800112
Yi Konga03e0442018-07-17 11:16:57 -0700113 ASSERT_TRUE(mSurfaceControl != nullptr);
Jamie Gennis134f0422011-03-08 12:18:54 -0800114 ASSERT_TRUE(mSurfaceControl->isValid());
115
Robert Carr4cdc58f2017-08-23 14:22:20 -0700116 Transaction t;
arthurhungdba591c2021-02-08 17:28:49 +0800117 ASSERT_EQ(NO_ERROR, t.setLayer(mSurfaceControl, 0x7fffffff).show(mSurfaceControl).apply());
Jamie Gennis134f0422011-03-08 12:18:54 -0800118
119 mSurface = mSurfaceControl->getSurface();
Yi Konga03e0442018-07-17 11:16:57 -0700120 ASSERT_TRUE(mSurface != nullptr);
Jamie Gennis134f0422011-03-08 12:18:54 -0800121 }
122
123 virtual void TearDown() {
124 mComposerClient->dispose();
125 }
126
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700127 void testSurfaceListener(bool hasSurfaceListener, bool enableReleasedCb,
128 int32_t extraDiscardedBuffers) {
129 sp<IGraphicBufferProducer> producer;
130 sp<IGraphicBufferConsumer> consumer;
131 BufferQueue::createBufferQueue(&producer, &consumer);
132
Peiyong Lind8460c82020-07-28 16:04:22 -0700133 sp<MockConsumer> mockConsumer(new MockConsumer);
134 consumer->consumerConnect(mockConsumer, false);
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700135 consumer->setConsumerName(String8("TestConsumer"));
136
137 sp<Surface> surface = new Surface(producer);
138 sp<ANativeWindow> window(surface);
Peiyong Lind8460c82020-07-28 16:04:22 -0700139 sp<FakeSurfaceListener> listener;
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700140 if (hasSurfaceListener) {
Peiyong Lind8460c82020-07-28 16:04:22 -0700141 listener = new FakeSurfaceListener(enableReleasedCb);
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700142 }
143 ASSERT_EQ(OK, surface->connect(
144 NATIVE_WINDOW_API_CPU,
145 /*reportBufferRemoval*/true,
146 /*listener*/listener));
147 const int BUFFER_COUNT = 4 + extraDiscardedBuffers;
148 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
149
150 ANativeWindowBuffer* buffers[BUFFER_COUNT];
151 // Dequeue first to allocate a number of buffers
152 for (int i = 0; i < BUFFER_COUNT; i++) {
153 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(window.get(), &buffers[i]));
154 }
155 for (int i = 0; i < BUFFER_COUNT; i++) {
156 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], -1));
157 }
158
159 ANativeWindowBuffer* buffer;
160 // Fill BUFFER_COUNT-1 buffers
161 for (int i = 0; i < BUFFER_COUNT-1; i++) {
162 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(window.get(), &buffer));
163 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, -1));
164 }
165
166 // Dequeue 1 buffer
167 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(window.get(), &buffer));
168
169 // Acquire and free 1+extraDiscardedBuffers buffer, check onBufferReleased is called.
170 std::vector<BufferItem> releasedItems;
171 releasedItems.resize(1+extraDiscardedBuffers);
172 for (int i = 0; i < releasedItems.size(); i++) {
173 ASSERT_EQ(NO_ERROR, consumer->acquireBuffer(&releasedItems[i], 0));
174 ASSERT_EQ(NO_ERROR, consumer->releaseBuffer(releasedItems[i].mSlot,
175 releasedItems[i].mFrameNumber, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR,
176 Fence::NO_FENCE));
177 }
178 int32_t expectedReleaseCb = (enableReleasedCb ? releasedItems.size() : 0);
179 if (hasSurfaceListener) {
180 ASSERT_EQ(expectedReleaseCb, listener->getReleaseNotifyCount());
181 }
182
183 // Acquire 1 buffer, leaving 1+extraDiscardedBuffers filled buffer in queue
184 BufferItem item;
185 ASSERT_EQ(NO_ERROR, consumer->acquireBuffer(&item, 0));
186
187 // Discard free buffers
188 ASSERT_EQ(NO_ERROR, consumer->discardFreeBuffers());
189
190 if (hasSurfaceListener) {
191 ASSERT_EQ(expectedReleaseCb, listener->getReleaseNotifyCount());
192
193 // Check onBufferDiscarded is called with correct buffer
194 auto discardedBuffers = listener->getDiscardedBuffers();
195 ASSERT_EQ(discardedBuffers.size(), releasedItems.size());
196 for (int i = 0; i < releasedItems.size(); i++) {
197 ASSERT_EQ(discardedBuffers[i], releasedItems[i].mGraphicBuffer);
198 }
199
200 ASSERT_EQ(expectedReleaseCb, listener->getReleaseNotifyCount());
201 }
202
203 // Disconnect the surface
204 ASSERT_EQ(NO_ERROR, surface->disconnect(NATIVE_WINDOW_API_CPU));
205 }
206
chaviw8ffc7b82020-08-18 11:25:37 -0700207 static status_t captureDisplay(DisplayCaptureArgs& captureArgs,
208 ScreenCaptureResults& captureResults) {
Huihong Luof5029222021-12-16 14:33:46 -0800209 const auto sf = ComposerServiceAIDL::getComposerService();
chaviw8ffc7b82020-08-18 11:25:37 -0700210 SurfaceComposerClient::Transaction().apply(true);
211
212 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
Huihong Luof5029222021-12-16 14:33:46 -0800213 binder::Status status = sf->captureDisplay(captureArgs, captureListener);
214 if (status.transactionError() != NO_ERROR) {
215 return status.transactionError();
chaviw8ffc7b82020-08-18 11:25:37 -0700216 }
217 captureResults = captureListener->waitForResults();
218 return captureResults.result;
219 }
220
Jamie Gennis134f0422011-03-08 12:18:54 -0800221 sp<Surface> mSurface;
222 sp<SurfaceComposerClient> mComposerClient;
223 sp<SurfaceControl> mSurfaceControl;
224};
225
Robert Carr740eaf02018-03-27 12:59:18 -0700226TEST_F(SurfaceTest, CreateSurfaceReturnsErrorBadClient) {
227 mComposerClient->dispose();
228 ASSERT_EQ(NO_INIT, mComposerClient->initCheck());
229
230 sp<SurfaceControl> sc;
231 status_t err = mComposerClient->createSurfaceChecked(
232 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, &sc, 0);
233 ASSERT_EQ(NO_INIT, err);
234}
235
Jamie Gennis134f0422011-03-08 12:18:54 -0800236TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
237 sp<ANativeWindow> anw(mSurface);
238 int result = -123;
239 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
240 &result);
241 EXPECT_EQ(NO_ERROR, err);
242 EXPECT_EQ(1, result);
243}
244
245TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
246 mSurfaceControl.clear();
Kalle Raita643f0942016-12-07 09:20:14 -0800247 // Wait for the async clean-up to complete.
248 std::this_thread::sleep_for(50ms);
Jamie Gennis134f0422011-03-08 12:18:54 -0800249
250 sp<ANativeWindow> anw(mSurface);
251 int result = -123;
252 int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
253 &result);
254 EXPECT_EQ(NO_ERROR, err);
255 EXPECT_EQ(1, result);
256}
257
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800258// This test probably doesn't belong here.
Robert Carr108b2c72019-04-02 16:32:58 -0700259TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersDontSucceed) {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800260 sp<ANativeWindow> anw(mSurface);
261
262 // Verify the screenshot works with no protected buffers.
Huihong Luo07e72362022-02-14 14:26:04 -0800263 const sp<IBinder> display = ComposerServiceAIDL::getInstance().getInternalDisplayToken();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800264 ASSERT_FALSE(display == nullptr);
265
chaviwd2432892020-07-24 17:42:39 -0700266 DisplayCaptureArgs captureArgs;
267 captureArgs.displayToken = display;
268 captureArgs.width = 64;
269 captureArgs.height = 64;
270
271 ScreenCaptureResults captureResults;
chaviw8ffc7b82020-08-18 11:25:37 -0700272 ASSERT_EQ(NO_ERROR, captureDisplay(captureArgs, captureResults));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800273
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700274 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
275 NATIVE_WINDOW_API_CPU));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800276 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
277 // that we need to dequeue a buffer in order for it to actually get
278 // allocated in SurfaceFlinger.
279 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
280 GRALLOC_USAGE_PROTECTED));
281 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Yi Konga03e0442018-07-17 11:16:57 -0700282 ANativeWindowBuffer* buf = nullptr;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700283
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700284 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700285 if (err) {
286 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
287 // that's okay as long as this is the reason for the failure.
288 // try again without the GRALLOC_USAGE_PROTECTED bit.
289 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700290 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
291 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700292 return;
293 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700294 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700295
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800296 for (int i = 0; i < 4; i++) {
297 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700298 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
299 &buf));
300 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800301 }
chaviw8ffc7b82020-08-18 11:25:37 -0700302 ASSERT_EQ(NO_ERROR, captureDisplay(captureArgs, captureResults));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800303}
304
Jamie Gennis391bbe22011-03-14 15:00:06 -0700305TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
306 sp<ANativeWindow> anw(mSurface);
307 int result = -123;
308 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
309 EXPECT_EQ(NO_ERROR, err);
310 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
311}
312
Craig Donner6ebc46a2016-10-21 15:23:44 -0700313TEST_F(SurfaceTest, LayerCountIsOne) {
314 sp<ANativeWindow> anw(mSurface);
315 int result = -123;
316 int err = anw->query(anw.get(), NATIVE_WINDOW_LAYER_COUNT, &result);
317 EXPECT_EQ(NO_ERROR, err);
318 EXPECT_EQ(1, result);
319}
320
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700321TEST_F(SurfaceTest, QueryConsumerUsage) {
322 const int TEST_USAGE_FLAGS =
323 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
Dan Stoza5603a2f2014-04-07 13:41:37 -0700324 sp<IGraphicBufferProducer> producer;
325 sp<IGraphicBufferConsumer> consumer;
326 BufferQueue::createBufferQueue(&producer, &consumer);
327 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700328 TEST_USAGE_FLAGS);
Dan Stoza5603a2f2014-04-07 13:41:37 -0700329 sp<Surface> s = new Surface(producer);
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700330
331 sp<ANativeWindow> anw(s);
332
333 int flags = -1;
334 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
335
336 ASSERT_EQ(NO_ERROR, err);
337 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
338}
339
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800340TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800341 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_V0_SRGB;
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800342 sp<IGraphicBufferProducer> producer;
343 sp<IGraphicBufferConsumer> consumer;
344 BufferQueue::createBufferQueue(&producer, &consumer);
345 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
346
347 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
348
349 sp<Surface> s = new Surface(producer);
350
351 sp<ANativeWindow> anw(s);
352
353 android_dataspace dataSpace;
354
355 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
356 reinterpret_cast<int*>(&dataSpace));
357
358 ASSERT_EQ(NO_ERROR, err);
359 ASSERT_EQ(TEST_DATASPACE, dataSpace);
360}
361
Dan Stoza812ed062015-06-02 15:45:22 -0700362TEST_F(SurfaceTest, SettingGenerationNumber) {
363 sp<IGraphicBufferProducer> producer;
364 sp<IGraphicBufferConsumer> consumer;
365 BufferQueue::createBufferQueue(&producer, &consumer);
366 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
367 sp<Surface> surface = new Surface(producer);
368 sp<ANativeWindow> window(surface);
369
370 // Allocate a buffer with a generation number of 0
371 ANativeWindowBuffer* buffer;
372 int fenceFd;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700373 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
374 NATIVE_WINDOW_API_CPU));
Dan Stoza812ed062015-06-02 15:45:22 -0700375 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
376 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
377
378 // Detach the buffer and check its generation number
379 sp<GraphicBuffer> graphicBuffer;
380 sp<Fence> fence;
381 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
382 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
383
384 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
385 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
386
387 // This should change the generation number of the GraphicBuffer
388 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
389
390 // Check that the new generation number sticks with the buffer
391 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
392 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
393 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
394 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
395}
396
Dan Stozac6f30bd2015-06-08 09:32:50 -0700397TEST_F(SurfaceTest, GetConsumerName) {
398 sp<IGraphicBufferProducer> producer;
399 sp<IGraphicBufferConsumer> consumer;
400 BufferQueue::createBufferQueue(&producer, &consumer);
401
Peiyong Lind8460c82020-07-28 16:04:22 -0700402 sp<MockConsumer> mockConsumer(new MockConsumer);
403 consumer->consumerConnect(mockConsumer, false);
Dan Stozac6f30bd2015-06-08 09:32:50 -0700404 consumer->setConsumerName(String8("TestConsumer"));
405
406 sp<Surface> surface = new Surface(producer);
407 sp<ANativeWindow> window(surface);
408 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
409
410 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
411}
412
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700413TEST_F(SurfaceTest, GetWideColorSupport) {
414 sp<IGraphicBufferProducer> producer;
415 sp<IGraphicBufferConsumer> consumer;
416 BufferQueue::createBufferQueue(&producer, &consumer);
417
Peiyong Lind8460c82020-07-28 16:04:22 -0700418 sp<MockConsumer> mockConsumer(new MockConsumer);
419 consumer->consumerConnect(mockConsumer, false);
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700420 consumer->setConsumerName(String8("TestConsumer"));
421
422 sp<Surface> surface = new Surface(producer);
423 sp<ANativeWindow> window(surface);
424 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
425
426 bool supported;
427 surface->getWideColorSupport(&supported);
428
Courtney Goeltzenleuchter6a570b62017-03-13 14:30:00 -0600429 // NOTE: This test assumes that device that supports
430 // wide-color (as indicated by BoardConfig) must also
431 // have a wide-color primary display.
432 // That assumption allows this test to cover devices
433 // that advertised a wide-color color mode without
434 // actually supporting wide-color to pass this test
435 // as well as the case of a device that does support
436 // wide-color (via BoardConfig) and has a wide-color
437 // primary display.
438 // NOT covered at this time is a device that supports
439 // wide color in the BoardConfig but does not support
440 // a wide-color color mode on the primary display.
441 ASSERT_EQ(hasWideColorDisplay, supported);
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700442}
443
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700444TEST_F(SurfaceTest, GetHdrSupport) {
445 sp<IGraphicBufferProducer> producer;
446 sp<IGraphicBufferConsumer> consumer;
447 BufferQueue::createBufferQueue(&producer, &consumer);
448
Peiyong Lind8460c82020-07-28 16:04:22 -0700449 sp<MockConsumer> mockConsumer(new MockConsumer);
450 consumer->consumerConnect(mockConsumer, false);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700451 consumer->setConsumerName(String8("TestConsumer"));
452
453 sp<Surface> surface = new Surface(producer);
454 sp<ANativeWindow> window(surface);
455 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
456
457 bool supported;
458 status_t result = surface->getHdrSupport(&supported);
459 ASSERT_EQ(NO_ERROR, result);
460
461 // NOTE: This is not a CTS test.
462 // This test verifies that when the BoardConfig TARGET_HAS_HDR_DISPLAY
463 // is TRUE, getHdrSupport is also true.
464 // TODO: Add check for an HDR color mode on the primary display.
465 ASSERT_EQ(hasHdrDisplay, supported);
466}
467
468TEST_F(SurfaceTest, SetHdrMetadata) {
469 sp<IGraphicBufferProducer> producer;
470 sp<IGraphicBufferConsumer> consumer;
471 BufferQueue::createBufferQueue(&producer, &consumer);
472
Peiyong Lind8460c82020-07-28 16:04:22 -0700473 sp<MockConsumer> mockConsumer(new MockConsumer);
474 consumer->consumerConnect(mockConsumer, false);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700475 consumer->setConsumerName(String8("TestConsumer"));
476
477 sp<Surface> surface = new Surface(producer);
478 sp<ANativeWindow> window(surface);
479 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
480
481 bool supported;
482 status_t result = surface->getHdrSupport(&supported);
483 ASSERT_EQ(NO_ERROR, result);
484
485 if (!hasHdrDisplay || !supported) {
486 return;
487 }
488 const android_smpte2086_metadata smpte2086 = {
489 {0.680, 0.320},
490 {0.265, 0.690},
491 {0.150, 0.060},
492 {0.3127, 0.3290},
493 100.0,
494 0.1,
495 };
496 const android_cta861_3_metadata cta861_3 = {
497 78.0,
498 62.0,
499 };
Valerie Haua82679d2018-11-21 09:31:43 -0800500
501 std::vector<uint8_t> hdr10plus;
502 hdr10plus.push_back(0xff);
503
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700504 int error = native_window_set_buffers_smpte2086_metadata(window.get(), &smpte2086);
505 ASSERT_EQ(error, NO_ERROR);
506 error = native_window_set_buffers_cta861_3_metadata(window.get(), &cta861_3);
507 ASSERT_EQ(error, NO_ERROR);
Valerie Haua82679d2018-11-21 09:31:43 -0800508 error = native_window_set_buffers_hdr10_plus_metadata(window.get(), hdr10plus.size(),
509 hdr10plus.data());
510 ASSERT_EQ(error, NO_ERROR);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700511}
512
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800513TEST_F(SurfaceTest, DynamicSetBufferCount) {
514 sp<IGraphicBufferProducer> producer;
515 sp<IGraphicBufferConsumer> consumer;
516 BufferQueue::createBufferQueue(&producer, &consumer);
517
Peiyong Lind8460c82020-07-28 16:04:22 -0700518 sp<MockConsumer> mockConsumer(new MockConsumer);
519 consumer->consumerConnect(mockConsumer, false);
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800520 consumer->setConsumerName(String8("TestConsumer"));
521
522 sp<Surface> surface = new Surface(producer);
523 sp<ANativeWindow> window(surface);
524
525 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
526 NATIVE_WINDOW_API_CPU));
527 native_window_set_buffer_count(window.get(), 4);
528
529 int fence;
530 ANativeWindowBuffer* buffer;
531 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
532 native_window_set_buffer_count(window.get(), 3);
533 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
534 native_window_set_buffer_count(window.get(), 2);
535 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
536 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
537}
538
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700539TEST_F(SurfaceTest, GetAndFlushRemovedBuffers) {
540 sp<IGraphicBufferProducer> producer;
541 sp<IGraphicBufferConsumer> consumer;
542 BufferQueue::createBufferQueue(&producer, &consumer);
543
Peiyong Lind8460c82020-07-28 16:04:22 -0700544 sp<MockConsumer> mockConsumer(new MockConsumer);
545 consumer->consumerConnect(mockConsumer, false);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700546 consumer->setConsumerName(String8("TestConsumer"));
547
548 sp<Surface> surface = new Surface(producer);
549 sp<ANativeWindow> window(surface);
Peiyong Lind8460c82020-07-28 16:04:22 -0700550 sp<StubProducerListener> listener = new StubProducerListener();
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700551 ASSERT_EQ(OK, surface->connect(
552 NATIVE_WINDOW_API_CPU,
553 /*listener*/listener,
554 /*reportBufferRemoval*/true));
555 const int BUFFER_COUNT = 4;
556 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
557
558 sp<GraphicBuffer> detachedBuffer;
559 sp<Fence> outFence;
560 int fences[BUFFER_COUNT];
561 ANativeWindowBuffer* buffers[BUFFER_COUNT];
562 // Allocate buffers because detachNextBuffer requires allocated buffers
563 for (int i = 0; i < BUFFER_COUNT; i++) {
564 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
565 }
566 for (int i = 0; i < BUFFER_COUNT; i++) {
567 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
568 }
569
570 // Test detached buffer is correctly reported
571 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
572 std::vector<sp<GraphicBuffer>> removedBuffers;
573 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
574 ASSERT_EQ(1u, removedBuffers.size());
575 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
576 // Test the list is flushed one getAndFlushRemovedBuffers returns
577 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
578 ASSERT_EQ(0u, removedBuffers.size());
579
580
581 // Test removed buffer list is cleanup after next dequeueBuffer call
582 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
583 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[0], &fences[0]));
584 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
585 ASSERT_EQ(0u, removedBuffers.size());
586 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[0], fences[0]));
587
588 // Test removed buffer list is cleanup after next detachNextBuffer call
589 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
590 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
591 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
592 ASSERT_EQ(1u, removedBuffers.size());
593 ASSERT_EQ(detachedBuffer->handle, removedBuffers.at(0)->handle);
594
595 // Re-allocate buffers since all buffers are detached up to now
596 for (int i = 0; i < BUFFER_COUNT; i++) {
597 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffers[i], &fences[i]));
598 }
599 for (int i = 0; i < BUFFER_COUNT; i++) {
600 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffers[i], fences[i]));
601 }
602
603 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&detachedBuffer, &outFence));
604 ASSERT_EQ(NO_ERROR, surface->attachBuffer(detachedBuffer.get()));
605 ASSERT_EQ(OK, surface->getAndFlushRemovedBuffers(&removedBuffers));
606 // Depends on which slot GraphicBufferProducer impl pick, the attach call might
607 // get 0 or 1 buffer removed.
608 ASSERT_LE(removedBuffers.size(), 1u);
609}
Brian Anderson3da8d272016-07-28 16:20:47 -0700610
Shuzhen Wang067fcd32019-08-14 10:41:12 -0700611TEST_F(SurfaceTest, SurfaceListenerTest) {
612 // Test discarding 1 free buffers with no listener
613 testSurfaceListener(/*hasListener*/false, /*enableReleaseCb*/false, /*extraDiscardedBuffers*/0);
614 // Test discarding 2 free buffers with no listener
615 testSurfaceListener(/*hasListener*/false, /*enableReleaseCb*/false, /*extraDiscardedBuffers*/1);
616 // Test discarding 1 free buffers with a listener, disabling onBufferReleased
617 testSurfaceListener(/*hasListener*/true, /*enableReleasedCb*/false, /*extraDiscardedBuffers*/0);
618 // Test discarding 2 free buffers with a listener, disabling onBufferReleased
619 testSurfaceListener(/*hasListener*/true, /*enableReleasedCb*/false, /*extraDiscardedBuffers*/1);
620 // Test discarding 1 free buffers with a listener, enabling onBufferReleased
621 testSurfaceListener(/*hasListener*/true, /*enableReleasedCb*/true, /*extraDiscardedBuffers*/0);
622 // Test discarding 3 free buffers with a listener, enabling onBufferReleased
623 testSurfaceListener(/*hasListener*/true, /*enableReleasedCb*/true, /*extraDiscardedBuffers*/2);
624}
625
Dan Stoza932f0082017-05-31 13:50:16 -0700626TEST_F(SurfaceTest, TestGetLastDequeueStartTime) {
627 sp<ANativeWindow> anw(mSurface);
628 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU));
629
630 ANativeWindowBuffer* buffer = nullptr;
631 int32_t fenceFd = -1;
632
633 nsecs_t before = systemTime(CLOCK_MONOTONIC);
634 anw->dequeueBuffer(anw.get(), &buffer, &fenceFd);
635 nsecs_t after = systemTime(CLOCK_MONOTONIC);
636
Alec Mouria1619662019-08-21 19:30:48 -0700637 nsecs_t lastDequeueTime = ANativeWindow_getLastDequeueStartTime(anw.get());
Dan Stoza932f0082017-05-31 13:50:16 -0700638 ASSERT_LE(before, lastDequeueTime);
639 ASSERT_GE(after, lastDequeueTime);
640}
641
Brian Anderson3da8d272016-07-28 16:20:47 -0700642class FakeConsumer : public BnConsumerListener {
643public:
644 void onFrameAvailable(const BufferItem& /*item*/) override {}
645 void onBuffersReleased() override {}
646 void onSidebandStreamChanged() override {}
647
648 void addAndGetFrameTimestamps(
649 const NewFrameEventsEntry* newTimestamps,
650 FrameEventHistoryDelta* outDelta) override {
651 if (newTimestamps) {
652 if (mGetFrameTimestampsEnabled) {
653 EXPECT_GT(mNewFrameEntryOverride.frameNumber, 0u) <<
654 "Test should set mNewFrameEntryOverride before queuing "
655 "a frame.";
656 EXPECT_EQ(newTimestamps->frameNumber,
657 mNewFrameEntryOverride.frameNumber) <<
658 "Test attempting to add NewFrameEntryOverride with "
659 "incorrect frame number.";
660 mFrameEventHistory.addQueue(mNewFrameEntryOverride);
661 mNewFrameEntryOverride.frameNumber = 0;
662 }
663 mAddFrameTimestampsCount++;
664 mLastAddedFrameNumber = newTimestamps->frameNumber;
665 }
666 if (outDelta) {
667 mFrameEventHistory.getAndResetDelta(outDelta);
668 mGetFrameTimestampsCount++;
669 }
670 mAddAndGetFrameTimestampsCallCount++;
671 }
672
673 bool mGetFrameTimestampsEnabled = false;
674
675 ConsumerFrameEventHistory mFrameEventHistory;
676 int mAddAndGetFrameTimestampsCallCount = 0;
677 int mAddFrameTimestampsCount = 0;
678 int mGetFrameTimestampsCount = 0;
679 uint64_t mLastAddedFrameNumber = NO_FRAME_INDEX;
680
681 NewFrameEventsEntry mNewFrameEntryOverride = { 0, 0, 0, nullptr };
682};
683
Pablo Gamito6ee484d2020-07-30 14:26:28 +0000684class FakeSurfaceComposer : public ISurfaceComposer {
Brian Anderson3da8d272016-07-28 16:20:47 -0700685public:
686 ~FakeSurfaceComposer() override {}
687
Brian Anderson6b376712017-04-04 10:51:39 -0700688 void setSupportsPresent(bool supportsPresent) {
689 mSupportsPresent = supportsPresent;
690 }
691
Brian Anderson3da8d272016-07-28 16:20:47 -0700692 sp<ISurfaceComposerClient> createConnection() override { return nullptr; }
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700693 sp<IDisplayEventConnection> createDisplayEventConnection(
Ady Abraham62f216c2020-10-13 19:07:23 -0700694 ISurfaceComposer::VsyncSource, ISurfaceComposer::EventRegistrationFlags) override {
Brian Anderson3da8d272016-07-28 16:20:47 -0700695 return nullptr;
696 }
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000697 status_t setTransactionState(const FrameTimelineInfo& /*frameTimelineInfo*/,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700698 const Vector<ComposerState>& /*state*/,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000699 const Vector<DisplayState>& /*displays*/, uint32_t /*flags*/,
700 const sp<IBinder>& /*applyToken*/,
701 const InputWindowCommands& /*inputWindowCommands*/,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800702 int64_t /*desiredPresentTime*/, bool /*isAutoTimestamp*/,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000703 const client_cache_t& /*cachedBuffer*/,
704 bool /*hasListenerCallbacks*/,
705 const std::vector<ListenerCallbacks>& /*listenerCallbacks*/,
706 uint64_t /*transactionId*/) override {
chaviw308ddba2020-08-11 16:23:51 -0700707 return NO_ERROR;
Marissa Wall3dad52d2019-03-22 14:03:19 -0700708 }
Marissa Wall17b4e452018-12-26 16:32:34 -0800709
Brian Anderson3da8d272016-07-28 16:20:47 -0700710 void bootFinished() override {}
711 bool authenticateSurfaceTexture(
712 const sp<IGraphicBufferProducer>& /*surface*/) const override {
713 return false;
714 }
Brian Anderson6b376712017-04-04 10:51:39 -0700715
716 status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported)
717 const override {
718 *outSupported = {
719 FrameEvent::REQUESTED_PRESENT,
720 FrameEvent::ACQUIRE,
721 FrameEvent::LATCH,
722 FrameEvent::FIRST_REFRESH_START,
723 FrameEvent::LAST_REFRESH_START,
724 FrameEvent::GPU_COMPOSITION_DONE,
725 FrameEvent::DEQUEUE_READY,
726 FrameEvent::RELEASE
727 };
728 if (mSupportsPresent) {
729 outSupported->push_back(
730 FrameEvent::DISPLAY_PRESENT);
731 }
732 return NO_ERROR;
733 }
734
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100735 status_t getStaticDisplayInfo(const sp<IBinder>& /*display*/, ui::StaticDisplayInfo*) override {
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800736 return NO_ERROR;
737 }
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100738 status_t getDynamicDisplayInfo(const sp<IBinder>& /*display*/,
739 ui::DynamicDisplayInfo*) override {
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800740 return NO_ERROR;
741 }
742 status_t getDisplayState(const sp<IBinder>& /*display*/, ui::DisplayState*) override {
743 return NO_ERROR;
744 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700745 status_t getDisplayStats(const sp<IBinder>& /*display*/,
746 DisplayStatInfo* /*stats*/) override { return NO_ERROR; }
Daniel Solomon42d04562019-01-20 21:03:19 -0800747 status_t getDisplayNativePrimaries(const sp<IBinder>& /*display*/,
748 ui::DisplayPrimaries& /*primaries*/) override {
749 return NO_ERROR;
750 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700751 status_t setActiveColorMode(const sp<IBinder>& /*display*/,
Peiyong Lina52f0292018-03-14 17:26:31 -0700752 ColorMode /*colorMode*/) override { return NO_ERROR; }
Kriti Dang7defaf32021-11-15 11:55:43 +0100753 status_t setBootDisplayMode(const sp<IBinder>& /*display*/, ui::DisplayModeId /*id*/) override {
754 return NO_ERROR;
755 }
Dominik Laskowskif1833852021-03-23 15:06:50 -0700756
Brian Anderson3da8d272016-07-28 16:20:47 -0700757 status_t clearAnimationFrameStats() override { return NO_ERROR; }
758 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
759 return NO_ERROR;
760 }
Kriti Dang49ad4132021-01-08 11:49:56 +0100761 status_t overrideHdrTypes(const sp<IBinder>& /*display*/,
762 const std::vector<ui::Hdr>& /*hdrTypes*/) override {
763 return NO_ERROR;
764 }
Tej Singhe2751772021-04-06 22:05:29 -0700765 status_t onPullAtom(const int32_t /*atomId*/, std::string* /*outData*/,
766 bool* /*success*/) override {
767 return NO_ERROR;
768 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700769 status_t enableVSyncInjections(bool /*enable*/) override {
770 return NO_ERROR;
771 }
772 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Vishnu Nair43bccf82020-06-05 10:53:37 -0700773 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) override {
Kalle Raitaa099a242017-01-11 11:17:29 -0800774 return NO_ERROR;
775 }
Peiyong Linc6780972018-10-28 15:24:08 -0700776 status_t getCompositionPreference(
777 ui::Dataspace* /*outDefaultDataspace*/, ui::PixelFormat* /*outDefaultPixelFormat*/,
778 ui::Dataspace* /*outWideColorGamutDataspace*/,
779 ui::PixelFormat* /*outWideColorGamutPixelFormat*/) const override {
Peiyong Lin0256f722018-08-31 15:45:10 -0700780 return NO_ERROR;
781 }
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700782 status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& /*display*/,
783 ui::PixelFormat* /*outFormat*/,
784 ui::Dataspace* /*outDataspace*/,
785 uint8_t* /*outComponentMask*/) const override {
786 return NO_ERROR;
787 }
Kevin DuBois74e53772018-11-19 10:52:38 -0800788 status_t setDisplayContentSamplingEnabled(const sp<IBinder>& /*display*/, bool /*enable*/,
789 uint8_t /*componentMask*/,
Dominik Laskowski470df5f2020-04-02 22:27:42 -0700790 uint64_t /*maxFrames*/) override {
Kevin DuBois74e53772018-11-19 10:52:38 -0800791 return NO_ERROR;
792 }
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700793 status_t getDisplayedContentSample(const sp<IBinder>& /*display*/, uint64_t /*maxFrames*/,
794 uint64_t /*timestamp*/,
795 DisplayedFrameStats* /*outStats*/) const override {
796 return NO_ERROR;
797 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700798
Peiyong Lin3c2791e2019-01-14 17:05:18 -0800799 status_t getColorManagement(bool* /*outGetColorManagement*/) const override { return NO_ERROR; }
800 status_t getProtectedContentSupport(bool* /*outSupported*/) const override { return NO_ERROR; }
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700801
Dan Stoza84ab9372018-12-17 15:27:57 -0800802 status_t addRegionSamplingListener(const Rect& /*samplingArea*/,
803 const sp<IBinder>& /*stopLayerHandle*/,
804 const sp<IRegionSamplingListener>& /*listener*/) override {
805 return NO_ERROR;
806 }
807 status_t removeRegionSamplingListener(
808 const sp<IRegionSamplingListener>& /*listener*/) override {
809 return NO_ERROR;
810 }
Alec Mouria9a68a62021-03-04 19:14:50 -0800811 status_t addFpsListener(int32_t /*taskId*/, const sp<gui::IFpsListener>& /*listener*/) {
Alec Mouriadebf5c2021-01-05 12:57:36 -0800812 return NO_ERROR;
813 }
814 status_t removeFpsListener(const sp<gui::IFpsListener>& /*listener*/) { return NO_ERROR; }
Galia Peycheva8f04b302021-04-27 13:25:38 +0200815
816 status_t addTunnelModeEnabledListener(const sp<gui::ITunnelModeEnabledListener>& /*listener*/) {
817 return NO_ERROR;
818 }
819
820 status_t removeTunnelModeEnabledListener(
821 const sp<gui::ITunnelModeEnabledListener>& /*listener*/) {
822 return NO_ERROR;
823 }
824
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100825 status_t setDesiredDisplayModeSpecs(const sp<IBinder>& /*displayToken*/,
826 ui::DisplayModeId /*defaultMode*/,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100827 bool /*allowGroupSwitching*/,
828 float /*primaryRefreshRateMin*/,
829 float /*primaryRefreshRateMax*/,
830 float /*appRequestRefreshRateMin*/,
831 float /*appRequestRefreshRateMax*/) {
Ana Krulec0782b882019-10-15 17:34:54 -0700832 return NO_ERROR;
833 }
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100834 status_t getDesiredDisplayModeSpecs(const sp<IBinder>& /*displayToken*/,
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100835 ui::DisplayModeId* /*outDefaultMode*/,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100836 bool* /*outAllowGroupSwitching*/,
837 float* /*outPrimaryRefreshRateMin*/,
838 float* /*outPrimaryRefreshRateMax*/,
839 float* /*outAppRequestRefreshRateMin*/,
840 float* /*outAppRequestRefreshRateMax*/) override {
Ana Krulec234bb162019-11-10 22:55:55 +0100841 return NO_ERROR;
842 };
Dan Stoza84ab9372018-12-17 15:27:57 -0800843
Vishnu Nairb13bb952019-11-15 10:24:08 -0800844 status_t setGlobalShadowSettings(const half4& /*ambientColor*/, const half4& /*spotColor*/,
845 float /*lightPosY*/, float /*lightPosZ*/,
846 float /*lightRadius*/) override {
847 return NO_ERROR;
848 }
849
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500850 status_t getDisplayDecorationSupport(
851 const sp<IBinder>& /*displayToken*/,
852 std::optional<DisplayDecorationSupport>* /*outSupport*/) const override {
Leon Scroggins IIIe5cff632021-12-29 11:53:36 -0500853 return NO_ERROR;
854 }
855
Steven Thomas62a4cf82020-01-31 12:04:03 -0800856 status_t setFrameRate(const sp<IGraphicBufferProducer>& /*surface*/, float /*frameRate*/,
Marin Shalamanovc5986772021-03-16 16:09:49 +0100857 int8_t /*compatibility*/, int8_t /*changeFrameRateStrategy*/) override {
Steven Thomas62a4cf82020-01-31 12:04:03 -0800858 return NO_ERROR;
859 }
860
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000861 status_t setFrameTimelineInfo(const sp<IGraphicBufferProducer>& /*surface*/,
862 const FrameTimelineInfo& /*frameTimelineInfo*/) override {
Ady Abraham74e17562020-08-24 18:18:19 -0700863 return NO_ERROR;
864 }
Steven Thomasd4071902020-03-24 16:02:53 -0700865
Pablo Gamito6ee484d2020-07-30 14:26:28 +0000866 status_t addTransactionTraceListener(
867 const sp<gui::ITransactionTraceListener>& /*listener*/) override {
868 return NO_ERROR;
869 }
870
Ana Krulec31f2b3c2020-12-14 14:30:09 -0800871 int getGPUContextPriority() override { return 0; };
872
Ady Abraham899dcdb2021-06-15 16:56:21 -0700873 status_t getMaxAcquiredBufferCount(int* /*buffers*/) const override { return NO_ERROR; }
Ady Abraham564f9de2021-02-03 18:34:33 -0800874
chaviw60c9d3e2021-06-04 12:52:17 -0500875 status_t addWindowInfosListener(
876 const sp<gui::IWindowInfosListener>& /*windowInfosListener*/) const override {
877 return NO_ERROR;
878 }
879
880 status_t removeWindowInfosListener(
881 const sp<gui::IWindowInfosListener>& /*windowInfosListener*/) const override {
882 return NO_ERROR;
883 }
884
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800885 status_t setOverrideFrameRate(uid_t /*uid*/, float /*frameRate*/) override { return NO_ERROR; }
886
Brian Anderson3da8d272016-07-28 16:20:47 -0700887protected:
888 IBinder* onAsBinder() override { return nullptr; }
889
890private:
891 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700892};
893
894class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
895public:
Chih-Hung Hsiehaaf62162018-12-20 15:45:04 -0800896 explicit FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap) : mFenceMap(fenceMap) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700897
898 ~FakeProducerFrameEventHistory() {}
899
900 void updateAcquireFence(uint64_t frameNumber,
901 std::shared_ptr<FenceTime>&& acquire) override {
902 // Verify the acquire fence being added isn't the one from the consumer.
903 EXPECT_NE(mConsumerAcquireFence, acquire);
904 // Override the fence, so we can verify this was called by the
905 // producer after the frame is queued.
906 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
907 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
908 }
909
910 void setAcquireFenceOverride(
911 const std::shared_ptr<FenceTime>& acquireFenceOverride,
912 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
913 mAcquireFenceOverride = acquireFenceOverride;
914 mConsumerAcquireFence = consumerAcquireFence;
915 }
916
917protected:
918 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
919 const override {
920 return mFenceMap->createFenceTimeForTest(fence);
921 }
922
923 FenceToFenceTimeMap* mFenceMap{nullptr};
924
925 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
926 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
927};
928
929
930class TestSurface : public Surface {
931public:
932 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
933 FenceToFenceTimeMap* fenceMap)
934 : Surface(bufferProducer),
935 mFakeSurfaceComposer(new FakeSurfaceComposer) {
936 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
937 mFrameEventHistory.reset(mFakeFrameEventHistory);
938 }
939
940 ~TestSurface() override {}
941
942 sp<ISurfaceComposer> composerService() const override {
943 return mFakeSurfaceComposer;
944 }
945
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800946 nsecs_t now() const override {
947 return mNow;
948 }
949
950 void setNow(nsecs_t now) {
951 mNow = now;
952 }
953
Brian Anderson3da8d272016-07-28 16:20:47 -0700954public:
955 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800956 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700957
958 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
959 // but this raw pointer gives access to test functionality.
960 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
961};
962
963
Brian Andersond0010582017-03-07 13:20:31 -0800964class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700965protected:
966 struct FenceAndFenceTime {
967 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
968 : mFence(new Fence),
969 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
970 sp<Fence> mFence { nullptr };
971 std::shared_ptr<FenceTime> mFenceTime { nullptr };
972 };
973
974 struct RefreshEvents {
975 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800976 : mFenceMap(fenceMap),
977 kCompositorTiming(
978 {refreshStart, refreshStart + 1, refreshStart + 2 }),
979 kStartTime(refreshStart + 3),
980 kGpuCompositionDoneTime(refreshStart + 4),
981 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700982
983 void signalPostCompositeFences() {
984 mFenceMap.signalAllForTest(
985 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
986 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
987 }
988
989 FenceToFenceTimeMap& mFenceMap;
990
991 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
992 FenceAndFenceTime mPresent { mFenceMap };
993
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800994 const CompositorTiming kCompositorTiming;
995
Brian Anderson3da8d272016-07-28 16:20:47 -0700996 const nsecs_t kStartTime;
997 const nsecs_t kGpuCompositionDoneTime;
998 const nsecs_t kPresentTime;
999 };
1000
1001 struct FrameEvents {
1002 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
1003 : mFenceMap(fenceMap),
1004 kPostedTime(frameStartTime + 100),
1005 kRequestedPresentTime(frameStartTime + 200),
1006 kProducerAcquireTime(frameStartTime + 300),
1007 kConsumerAcquireTime(frameStartTime + 301),
1008 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -07001009 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -07001010 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -07001011 mRefreshes {
1012 { mFenceMap, frameStartTime + 410 },
1013 { mFenceMap, frameStartTime + 420 },
1014 { mFenceMap, frameStartTime + 430 } } {}
1015
1016 void signalQueueFences() {
1017 mFenceMap.signalAllForTest(
1018 mAcquireConsumer.mFence, kConsumerAcquireTime);
1019 mFenceMap.signalAllForTest(
1020 mAcquireProducer.mFence, kProducerAcquireTime);
1021 }
1022
1023 void signalRefreshFences() {
1024 for (auto& re : mRefreshes) {
1025 re.signalPostCompositeFences();
1026 }
1027 }
1028
1029 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -07001030 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
1031 }
1032
1033 FenceToFenceTimeMap& mFenceMap;
1034
1035 FenceAndFenceTime mAcquireConsumer { mFenceMap };
1036 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -07001037 FenceAndFenceTime mRelease { mFenceMap };
1038
1039 const nsecs_t kPostedTime;
1040 const nsecs_t kRequestedPresentTime;
1041 const nsecs_t kProducerAcquireTime;
1042 const nsecs_t kConsumerAcquireTime;
1043 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -07001044 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -07001045 const nsecs_t kReleaseTime;
1046
1047 RefreshEvents mRefreshes[3];
1048 };
1049
Brian Andersond0010582017-03-07 13:20:31 -08001050 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -07001051
1052 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -07001053 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
1054 mFakeConsumer = new FakeConsumer;
1055 mCfeh = &mFakeConsumer->mFrameEventHistory;
1056 mConsumer->consumerConnect(mFakeConsumer, false);
1057 mConsumer->setConsumerName(String8("TestConsumer"));
1058 mSurface = new TestSurface(mProducer, &mFenceMap);
1059 mWindow = mSurface;
1060
1061 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
1062 NATIVE_WINDOW_API_CPU));
1063 native_window_set_buffer_count(mWindow.get(), 4);
1064 }
1065
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001066 void disableFrameTimestamps() {
1067 mFakeConsumer->mGetFrameTimestampsEnabled = false;
1068 native_window_enable_frame_timestamps(mWindow.get(), 0);
1069 mFrameTimestampsEnabled = false;
1070 }
1071
Brian Anderson3da8d272016-07-28 16:20:47 -07001072 void enableFrameTimestamps() {
1073 mFakeConsumer->mGetFrameTimestampsEnabled = true;
1074 native_window_enable_frame_timestamps(mWindow.get(), 1);
1075 mFrameTimestampsEnabled = true;
1076 }
1077
Brian Anderson1049d1d2016-12-16 17:25:57 -08001078 int getAllFrameTimestamps(uint64_t frameId) {
1079 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001080 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
1081 &outFirstRefreshStartTime, &outLastRefreshStartTime,
1082 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001083 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001084 }
1085
Brian Anderson3da8d272016-07-28 16:20:47 -07001086 void resetTimestamps() {
1087 outRequestedPresentTime = -1;
1088 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001089 outLatchTime = -1;
1090 outFirstRefreshStartTime = -1;
1091 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001092 outGpuCompositionDoneTime = -1;
1093 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001094 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001095 outReleaseTime = -1;
1096 }
1097
Brian Anderson1049d1d2016-12-16 17:25:57 -08001098 uint64_t getNextFrameId() {
1099 uint64_t frameId = -1;
1100 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
1101 EXPECT_EQ(status, NO_ERROR);
1102 return frameId;
1103 }
1104
Brian Anderson3da8d272016-07-28 16:20:47 -07001105 void dequeueAndQueue(uint64_t frameIndex) {
1106 int fence = -1;
1107 ANativeWindowBuffer* buffer = nullptr;
1108 ASSERT_EQ(NO_ERROR,
1109 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1110
1111 int oldAddFrameTimestampsCount =
1112 mFakeConsumer->mAddFrameTimestampsCount;
1113
1114 FrameEvents* frame = &mFrames[frameIndex];
1115 uint64_t frameNumber = frameIndex + 1;
1116
1117 NewFrameEventsEntry fe;
1118 fe.frameNumber = frameNumber;
1119 fe.postedTime = frame->kPostedTime;
1120 fe.requestedPresentTime = frame->kRequestedPresentTime;
1121 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
1122 mFakeConsumer->mNewFrameEntryOverride = fe;
1123
1124 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1125 frame->mAcquireProducer.mFenceTime,
1126 frame->mAcquireConsumer.mFenceTime);
1127
1128 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1129
1130 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
1131
1132 EXPECT_EQ(
1133 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
1134 mFakeConsumer->mAddFrameTimestampsCount);
1135 }
1136
1137 void addFrameEvents(
1138 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
1139 FrameEvents* oldFrame =
1140 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
1141 FrameEvents* newFrame = &mFrames[iNewFrame];
1142
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -08001143 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001144 uint64_t nNewFrame = iNewFrame + 1;
1145
Brian Anderson4e606e32017-03-16 15:34:57 -07001146 // Latch, Composite, and Release the frames in a plausible order.
1147 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -07001148 // that's okay for the purposes of this test.
1149 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
1150
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001151 // Composite the previous frame one more time, which helps verify
1152 // LastRefresh is updated properly.
1153 if (oldFrame != nullptr) {
1154 mCfeh->addPreComposition(nOldFrame,
1155 oldFrame->mRefreshes[2].kStartTime);
1156 gpuDoneFenceTime = gpuComposited ?
1157 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
1158 FenceTime::NO_FENCE;
1159 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001160 oldFrame->mRefreshes[2].mPresent.mFenceTime,
1161 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001162 }
1163
1164 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -07001165 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
1166
1167 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
1168 gpuDoneFenceTime = gpuComposited ?
1169 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
1170 FenceTime::NO_FENCE;
1171 // HWC2 releases the previous buffer after a new latch just before
1172 // calling postComposition.
1173 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -07001174 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -07001175 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
1176 }
1177 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001178 newFrame->mRefreshes[0].mPresent.mFenceTime,
1179 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -07001180
Brian Anderson3da8d272016-07-28 16:20:47 -07001181 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
1182 gpuDoneFenceTime = gpuComposited ?
1183 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
1184 FenceTime::NO_FENCE;
1185 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001186 newFrame->mRefreshes[1].mPresent.mFenceTime,
1187 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -07001188 }
1189
Brian Anderson3da8d272016-07-28 16:20:47 -07001190 sp<IGraphicBufferProducer> mProducer;
1191 sp<IGraphicBufferConsumer> mConsumer;
1192 sp<FakeConsumer> mFakeConsumer;
1193 ConsumerFrameEventHistory* mCfeh;
1194 sp<TestSurface> mSurface;
1195 sp<ANativeWindow> mWindow;
1196
1197 FenceToFenceTimeMap mFenceMap;
1198
1199 bool mFrameTimestampsEnabled = false;
1200
1201 int64_t outRequestedPresentTime = -1;
1202 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001203 int64_t outLatchTime = -1;
1204 int64_t outFirstRefreshStartTime = -1;
1205 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001206 int64_t outGpuCompositionDoneTime = -1;
1207 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001208 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001209 int64_t outReleaseTime = -1;
1210
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001211 FrameEvents mFrames[3] {
1212 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -07001213};
1214
1215
1216// This test verifies that the frame timestamps are not retrieved when not
1217// explicitly enabled via native_window_enable_frame_timestamps.
1218// We want to check this to make sure there's no overhead for users
1219// that don't need the timestamp information.
1220TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
1221 int fence;
1222 ANativeWindowBuffer* buffer;
1223
1224 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1225 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1226
Brian Anderson1049d1d2016-12-16 17:25:57 -08001227 const uint64_t fId = getNextFrameId();
1228
Brian Anderson3da8d272016-07-28 16:20:47 -07001229 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
1230 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1231 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1232 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1233
1234 // Verify the producer doesn't get frame timestamps piggybacked on queue.
1235 // It is okay that frame timestamps are added in the consumer since it is
1236 // still needed for SurfaceFlinger dumps.
1237 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1238 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1239 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1240
1241 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001242 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -07001243 EXPECT_EQ(INVALID_OPERATION, result);
1244 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001245
1246 // Verify compositor timing query fails.
1247 nsecs_t compositeDeadline = 0;
1248 nsecs_t compositeInterval = 0;
1249 nsecs_t compositeToPresentLatency = 0;
1250 result = native_window_get_compositor_timing(mWindow.get(),
1251 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1252 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001253}
1254
1255// This test verifies that the frame timestamps are retrieved if explicitly
1256// enabled via native_window_enable_frame_timestamps.
1257TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001258 CompositorTiming initialCompositorTiming {
1259 1000000000, // 1s deadline
1260 16666667, // 16ms interval
1261 50000000, // 50ms present latency
1262 };
1263 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1264
Brian Anderson3da8d272016-07-28 16:20:47 -07001265 enableFrameTimestamps();
1266
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001267 // Verify the compositor timing query gets the initial compositor values
1268 // after timststamps are enabled; even before the first frame is queued
1269 // or dequeued.
1270 nsecs_t compositeDeadline = 0;
1271 nsecs_t compositeInterval = 0;
1272 nsecs_t compositeToPresentLatency = 0;
1273 mSurface->setNow(initialCompositorTiming.deadline - 1);
1274 int result = native_window_get_compositor_timing(mWindow.get(),
1275 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1276 EXPECT_EQ(NO_ERROR, result);
1277 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1278 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1279 EXPECT_EQ(initialCompositorTiming.presentLatency,
1280 compositeToPresentLatency);
1281
Brian Anderson3da8d272016-07-28 16:20:47 -07001282 int fence;
1283 ANativeWindowBuffer* buffer;
1284
1285 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001286 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001287
Brian Anderson1049d1d2016-12-16 17:25:57 -08001288 const uint64_t fId1 = getNextFrameId();
1289
Brian Anderson3da8d272016-07-28 16:20:47 -07001290 // Verify getFrameTimestamps is piggybacked on dequeue.
1291 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1292 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001293 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001294
1295 NewFrameEventsEntry f1;
1296 f1.frameNumber = 1;
1297 f1.postedTime = mFrames[0].kPostedTime;
1298 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1299 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1300 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1301 mFrames[0].mAcquireProducer.mFenceTime,
1302 mFrames[0].mAcquireConsumer.mFenceTime);
1303 mFakeConsumer->mNewFrameEntryOverride = f1;
1304 mFrames[0].signalQueueFences();
1305
1306 // Verify getFrameTimestamps is piggybacked on queue.
1307 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1308 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1309 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001310 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001311
1312 // Verify queries for timestamps that the producer doesn't know about
1313 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001314 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001315 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001316 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001317}
1318
Brian Anderson6b376712017-04-04 10:51:39 -07001319TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1320 bool displayPresentSupported = true;
1321 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1322
1323 // Verify supported bits are forwarded.
1324 int supportsPresent = -1;
1325 mWindow.get()->query(mWindow.get(),
1326 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1327 EXPECT_EQ(displayPresentSupported, supportsPresent);
1328}
1329
1330TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1331 bool displayPresentSupported = false;
1332 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1333
1334 // Verify supported bits are forwarded.
1335 int supportsPresent = -1;
1336 mWindow.get()->query(mWindow.get(),
1337 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1338 EXPECT_EQ(displayPresentSupported, supportsPresent);
1339}
1340
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001341TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1342 nsecs_t phase = 4000;
1343 nsecs_t interval = 1000;
1344
1345 // Timestamp in previous interval.
1346 nsecs_t timestamp = 3500;
1347 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1348 timestamp, phase, interval));
1349
1350 // Timestamp in next interval.
1351 timestamp = 4500;
1352 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1353 timestamp, phase, interval));
1354
1355 // Timestamp multiple intervals before.
1356 timestamp = 2500;
1357 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1358 timestamp, phase, interval));
1359
1360 // Timestamp multiple intervals after.
1361 timestamp = 6500;
1362 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1363 timestamp, phase, interval));
1364
1365 // Timestamp on previous interval.
1366 timestamp = 3000;
1367 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1368 timestamp, phase, interval));
1369
1370 // Timestamp on next interval.
1371 timestamp = 5000;
1372 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1373 timestamp, phase, interval));
1374
1375 // Timestamp equal to phase.
1376 timestamp = 4000;
1377 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1378 timestamp, phase, interval));
1379}
1380
1381// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1382// if the number of intervals elapsed is internally stored in an int.
1383TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1384 nsecs_t phase = 0;
1385 nsecs_t interval = 4000;
1386 nsecs_t big_timestamp = 8635916564000;
1387 int32_t intervals = big_timestamp / interval;
1388
1389 EXPECT_LT(intervals, 0);
1390 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1391 big_timestamp, phase, interval));
1392 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1393 big_timestamp, big_timestamp, interval));
1394}
1395
1396// This verifies the compositor timing is updated by refresh events
1397// and piggy backed on a queue, dequeue, and enabling of timestamps..
1398TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1399 CompositorTiming initialCompositorTiming {
1400 1000000000, // 1s deadline
1401 16666667, // 16ms interval
1402 50000000, // 50ms present latency
1403 };
1404 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1405
1406 enableFrameTimestamps();
1407
1408 // We get the initial values before any frames are submitted.
1409 nsecs_t compositeDeadline = 0;
1410 nsecs_t compositeInterval = 0;
1411 nsecs_t compositeToPresentLatency = 0;
1412 mSurface->setNow(initialCompositorTiming.deadline - 1);
1413 int result = native_window_get_compositor_timing(mWindow.get(),
1414 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1415 EXPECT_EQ(NO_ERROR, result);
1416 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1417 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1418 EXPECT_EQ(initialCompositorTiming.presentLatency,
1419 compositeToPresentLatency);
1420
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001421 dequeueAndQueue(0);
1422 addFrameEvents(true, NO_FRAME_INDEX, 0);
1423
1424 // Still get the initial values because the frame events for frame 0
1425 // didn't get a chance to piggyback on a queue or dequeue yet.
1426 result = native_window_get_compositor_timing(mWindow.get(),
1427 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1428 EXPECT_EQ(NO_ERROR, result);
1429 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1430 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1431 EXPECT_EQ(initialCompositorTiming.presentLatency,
1432 compositeToPresentLatency);
1433
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001434 dequeueAndQueue(1);
1435 addFrameEvents(true, 0, 1);
1436
1437 // Now expect the composite values associated with frame 1.
1438 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1439 result = native_window_get_compositor_timing(mWindow.get(),
1440 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1441 EXPECT_EQ(NO_ERROR, result);
1442 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1443 compositeDeadline);
1444 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1445 compositeInterval);
1446 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1447 compositeToPresentLatency);
1448
1449 dequeueAndQueue(2);
1450 addFrameEvents(true, 1, 2);
1451
1452 // Now expect the composite values associated with frame 2.
1453 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1454 result = native_window_get_compositor_timing(mWindow.get(),
1455 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1456 EXPECT_EQ(NO_ERROR, result);
1457 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1458 compositeDeadline);
1459 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1460 compositeInterval);
1461 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1462 compositeToPresentLatency);
1463
1464 // Re-enabling frame timestamps should get the latest values.
1465 disableFrameTimestamps();
1466 enableFrameTimestamps();
1467
1468 // Now expect the composite values associated with frame 3.
1469 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1470 result = native_window_get_compositor_timing(mWindow.get(),
1471 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1472 EXPECT_EQ(NO_ERROR, result);
1473 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1474 compositeDeadline);
1475 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1476 compositeInterval);
1477 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1478 compositeToPresentLatency);
1479}
1480
1481// This verifies the compositor deadline properly snaps to the the next
1482// deadline based on the current time.
1483TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1484 CompositorTiming initialCompositorTiming {
1485 1000000000, // 1s deadline
1486 16666667, // 16ms interval
1487 50000000, // 50ms present latency
1488 };
1489 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1490
1491 enableFrameTimestamps();
1492
1493 nsecs_t compositeDeadline = 0;
1494 nsecs_t compositeInterval = 0;
1495 nsecs_t compositeToPresentLatency = 0;
1496
1497 // A "now" just before the deadline snaps to the deadline.
1498 mSurface->setNow(initialCompositorTiming.deadline - 1);
1499 int result = native_window_get_compositor_timing(mWindow.get(),
1500 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1501 EXPECT_EQ(NO_ERROR, result);
1502 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1503 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1504 EXPECT_EQ(expectedDeadline, compositeDeadline);
1505
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001506 dequeueAndQueue(0);
1507 addFrameEvents(true, NO_FRAME_INDEX, 0);
1508
1509 // A "now" just after the deadline snaps properly.
1510 mSurface->setNow(initialCompositorTiming.deadline + 1);
1511 result = native_window_get_compositor_timing(mWindow.get(),
1512 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1513 EXPECT_EQ(NO_ERROR, result);
1514 expectedDeadline =
1515 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1516 EXPECT_EQ(expectedDeadline, compositeDeadline);
1517
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001518 dequeueAndQueue(1);
1519 addFrameEvents(true, 0, 1);
1520
1521 // A "now" just after the next interval snaps properly.
1522 mSurface->setNow(
1523 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1524 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1525 result = native_window_get_compositor_timing(mWindow.get(),
1526 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1527 EXPECT_EQ(NO_ERROR, result);
1528 expectedDeadline =
1529 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1530 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1531 EXPECT_EQ(expectedDeadline, compositeDeadline);
1532
1533 dequeueAndQueue(2);
1534 addFrameEvents(true, 1, 2);
1535
1536 // A "now" over 1 interval before the deadline snaps properly.
1537 mSurface->setNow(
1538 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1539 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1540 result = native_window_get_compositor_timing(mWindow.get(),
1541 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1542 EXPECT_EQ(NO_ERROR, result);
1543 expectedDeadline =
1544 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1545 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1546 EXPECT_EQ(expectedDeadline, compositeDeadline);
1547
1548 // Re-enabling frame timestamps should get the latest values.
1549 disableFrameTimestamps();
1550 enableFrameTimestamps();
1551
1552 // A "now" over 2 intervals before the deadline snaps properly.
1553 mSurface->setNow(
1554 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1555 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1556 result = native_window_get_compositor_timing(mWindow.get(),
1557 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1558 EXPECT_EQ(NO_ERROR, result);
1559 expectedDeadline =
1560 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1561 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1562 EXPECT_EQ(expectedDeadline, compositeDeadline);
1563}
1564
Brian Anderson1049d1d2016-12-16 17:25:57 -08001565// This verifies the timestamps recorded in the consumer's
1566// FrameTimestampsHistory are properly retrieved by the producer for the
1567// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001568TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1569 enableFrameTimestamps();
1570
Brian Anderson1049d1d2016-12-16 17:25:57 -08001571 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001572 dequeueAndQueue(0);
1573 mFrames[0].signalQueueFences();
1574
Brian Anderson1049d1d2016-12-16 17:25:57 -08001575 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001576 dequeueAndQueue(1);
1577 mFrames[1].signalQueueFences();
1578
1579 addFrameEvents(true, NO_FRAME_INDEX, 0);
1580 mFrames[0].signalRefreshFences();
1581 addFrameEvents(true, 0, 1);
1582 mFrames[0].signalReleaseFences();
1583 mFrames[1].signalRefreshFences();
1584
1585 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001586 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001587 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001588 EXPECT_EQ(NO_ERROR, result);
1589 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1590 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001591 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1592 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1593 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001594 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1595 outGpuCompositionDoneTime);
1596 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001597 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001598 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1599
1600 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001601 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001602 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001603 EXPECT_EQ(NO_ERROR, result);
1604 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1605 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001606 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1607 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1608 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001609 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1610 outGpuCompositionDoneTime);
1611 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001612 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1613 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001614}
1615
1616// This test verifies the acquire fence recorded by the consumer is not sent
1617// back to the producer and the producer saves its own fence.
1618TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1619 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001620
Brian Anderson3da8d272016-07-28 16:20:47 -07001621 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001622 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001623 dequeueAndQueue(0);
1624
1625 // Verify queue-related timestamps for f1 are available immediately in the
1626 // producer without asking the consumer again, even before signaling the
1627 // acquire fence.
1628 resetTimestamps();
1629 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001630 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001631 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001632 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001633 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1634 EXPECT_EQ(NO_ERROR, result);
1635 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001636 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001637
1638 // Signal acquire fences. Verify a sync call still isn't necessary.
1639 mFrames[0].signalQueueFences();
1640
1641 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001642 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001643 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001644 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001645 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1646 EXPECT_EQ(NO_ERROR, result);
1647 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1648 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1649
1650 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001651 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001652 dequeueAndQueue(1);
1653
1654 // Verify queue-related timestamps for f2 are available immediately in the
1655 // producer without asking the consumer again, even before signaling the
1656 // acquire fence.
1657 resetTimestamps();
1658 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001659 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001660 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001661 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001662 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1663 EXPECT_EQ(NO_ERROR, result);
1664 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001665 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001666
1667 // Signal acquire fences. Verify a sync call still isn't necessary.
1668 mFrames[1].signalQueueFences();
1669
1670 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001671 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001672 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001673 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001674 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1675 EXPECT_EQ(NO_ERROR, result);
1676 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1677 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1678}
1679
1680TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1681 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001682
1683 // Dequeue and queue frame 1.
1684 dequeueAndQueue(0);
1685 mFrames[0].signalQueueFences();
1686
1687 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001688 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001689 dequeueAndQueue(1);
1690 mFrames[1].signalQueueFences();
1691
1692 addFrameEvents(true, NO_FRAME_INDEX, 0);
1693 mFrames[0].signalRefreshFences();
1694 addFrameEvents(true, 0, 1);
1695 mFrames[0].signalReleaseFences();
1696 mFrames[1].signalRefreshFences();
1697
1698 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001699 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001700 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001701 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1702 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001703 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001704 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1705}
1706
1707// This test verifies that fences can signal and update timestamps producer
1708// side without an additional sync call to the consumer.
1709TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1710 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001711
1712 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001713 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001714 dequeueAndQueue(0);
1715 mFrames[0].signalQueueFences();
1716
1717 // Dequeue and queue frame 2.
1718 dequeueAndQueue(1);
1719 mFrames[1].signalQueueFences();
1720
1721 addFrameEvents(true, NO_FRAME_INDEX, 0);
1722 addFrameEvents(true, 0, 1);
1723
1724 // Verify available timestamps are correct for frame 1, before any
1725 // fence has been signaled.
1726 // Note: A sync call is necessary here since the events triggered by
1727 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001728 resetTimestamps();
1729 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001730 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001731 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1732 EXPECT_EQ(NO_ERROR, result);
1733 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1734 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001735 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1736 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1737 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001738 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1739 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001740 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001741 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001742
1743 // Verify available timestamps are correct for frame 1 again, before any
1744 // fence has been signaled.
1745 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001746 resetTimestamps();
1747 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001748 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001749 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1750 EXPECT_EQ(NO_ERROR, result);
1751 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1752 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001753 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1754 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1755 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001756 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1757 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001758 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001759 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001760
1761 // Signal the fences for frame 1.
1762 mFrames[0].signalRefreshFences();
1763 mFrames[0].signalReleaseFences();
1764
1765 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001766 resetTimestamps();
1767 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001768 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001769 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1770 EXPECT_EQ(NO_ERROR, result);
1771 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1772 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001773 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1774 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1775 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001776 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1777 outGpuCompositionDoneTime);
1778 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001779 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001780 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1781}
1782
1783// This test verifies that if the frame wasn't GPU composited but has a refresh
1784// event a sync call isn't made to get the GPU composite done time since it will
1785// never exist.
1786TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1787 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001788
Brian Anderson3da8d272016-07-28 16:20:47 -07001789 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001790 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001791 dequeueAndQueue(0);
1792 mFrames[0].signalQueueFences();
1793
1794 // Dequeue and queue frame 2.
1795 dequeueAndQueue(1);
1796 mFrames[1].signalQueueFences();
1797
1798 addFrameEvents(false, NO_FRAME_INDEX, 0);
1799 addFrameEvents(false, 0, 1);
1800
1801 // Verify available timestamps are correct for frame 1, before any
1802 // fence has been signaled.
1803 // Note: A sync call is necessary here since the events triggered by
1804 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1805 resetTimestamps();
1806 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001807 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001808 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1809 EXPECT_EQ(NO_ERROR, result);
1810 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1811 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001812 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1813 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1814 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001815 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1816 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001817 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001818 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001819
1820 // Signal the fences for frame 1.
1821 mFrames[0].signalRefreshFences();
1822 mFrames[0].signalReleaseFences();
1823
1824 // Verify all timestamps, except GPU composition, are available without a
1825 // sync call.
1826 resetTimestamps();
1827 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001828 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001829 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1830 EXPECT_EQ(NO_ERROR, result);
1831 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1832 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001833 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1834 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1835 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001836 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001837 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001838 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001839 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1840}
1841
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001842// This test verifies that if the certain timestamps can't possibly exist for
1843// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001844TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001845 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001846
1847 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001848 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001849 dequeueAndQueue(0);
1850 mFrames[0].signalQueueFences();
1851
1852 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001853 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001854 dequeueAndQueue(1);
1855 mFrames[1].signalQueueFences();
1856
1857 addFrameEvents(false, NO_FRAME_INDEX, 0);
1858 addFrameEvents(false, 0, 1);
1859
1860 // Verify available timestamps are correct for frame 1, before any
1861 // fence has been signaled.
1862 // Note: A sync call is necessary here since the events triggered by
1863 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001864 resetTimestamps();
1865 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001866 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001867 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1868 EXPECT_EQ(NO_ERROR, result);
1869 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1870 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001871 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1872 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1873 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001874 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1875 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001876 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001877 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001878
1879 mFrames[0].signalRefreshFences();
1880 mFrames[0].signalReleaseFences();
1881 mFrames[1].signalRefreshFences();
1882
Brian Anderson1049d1d2016-12-16 17:25:57 -08001883 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001884 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001885 // available, a sync call should not occur because it's not possible for f2
1886 // to encounter the final value for those events until another frame is
1887 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001888 resetTimestamps();
1889 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001890 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001891 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1892 EXPECT_EQ(NO_ERROR, result);
1893 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1894 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001895 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1896 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1897 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001898 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001899 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001900 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1901 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001902}
1903
Brian Anderson6b376712017-04-04 10:51:39 -07001904// This test verifies there are no sync calls for present times
1905// when they aren't supported and that an error is returned.
1906
1907TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1908 enableFrameTimestamps();
1909 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1910
1911 // Dequeue and queue frame 1.
1912 const uint64_t fId1 = getNextFrameId();
1913 dequeueAndQueue(0);
1914
1915 // Verify a query for the Present times do not trigger a sync call if they
1916 // are not supported.
1917 resetTimestamps();
1918 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1919 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1920 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1921 &outDisplayPresentTime, nullptr, nullptr);
1922 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1923 EXPECT_EQ(BAD_VALUE, result);
1924 EXPECT_EQ(-1, outDisplayPresentTime);
1925}
1926
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001927TEST_F(SurfaceTest, DequeueWithConsumerDrivenSize) {
1928 sp<IGraphicBufferProducer> producer;
1929 sp<IGraphicBufferConsumer> consumer;
1930 BufferQueue::createBufferQueue(&producer, &consumer);
1931
Peiyong Lind8460c82020-07-28 16:04:22 -07001932 sp<MockConsumer> mockConsumer(new MockConsumer);
1933 consumer->consumerConnect(mockConsumer, false);
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001934 consumer->setDefaultBufferSize(10, 10);
1935
1936 sp<Surface> surface = new Surface(producer);
1937 sp<ANativeWindow> window(surface);
1938 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
1939 native_window_set_buffers_dimensions(window.get(), 0, 0);
1940
1941 int fence;
1942 ANativeWindowBuffer* buffer;
1943
1944 // Buffer size is driven by the consumer
1945 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1946 EXPECT_EQ(10, buffer->width);
1947 EXPECT_EQ(10, buffer->height);
1948 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1949
1950 // Buffer size is driven by the consumer
1951 consumer->setDefaultBufferSize(10, 20);
1952 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1953 EXPECT_EQ(10, buffer->width);
1954 EXPECT_EQ(20, buffer->height);
1955 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1956
1957 // Transform hint isn't synced to producer before queueBuffer or connect
1958 consumer->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_270);
1959 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1960 EXPECT_EQ(10, buffer->width);
1961 EXPECT_EQ(20, buffer->height);
1962 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
1963
1964 // Transform hint is synced to producer but no auto prerotation
1965 consumer->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_270);
1966 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1967 EXPECT_EQ(10, buffer->width);
1968 EXPECT_EQ(20, buffer->height);
1969 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1970
1971 // Prerotation is driven by the consumer with the transform hint used by producer
1972 native_window_set_auto_prerotation(window.get(), true);
1973 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1974 EXPECT_EQ(20, buffer->width);
1975 EXPECT_EQ(10, buffer->height);
1976 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1977
1978 // Turn off auto prerotaton
1979 native_window_set_auto_prerotation(window.get(), false);
1980 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1981 EXPECT_EQ(10, buffer->width);
1982 EXPECT_EQ(20, buffer->height);
1983 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1984
1985 // Test auto prerotation bit is disabled after disconnect
1986 native_window_set_auto_prerotation(window.get(), true);
1987 native_window_api_disconnect(window.get(), NATIVE_WINDOW_API_CPU);
1988 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
1989 consumer->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_270);
1990 native_window_set_buffers_dimensions(window.get(), 0, 0);
1991 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1992 EXPECT_EQ(10, buffer->width);
1993 EXPECT_EQ(20, buffer->height);
1994 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1995}
1996
Yiwei Zhang74c9cc32020-06-20 03:35:17 -07001997TEST_F(SurfaceTest, DefaultMaxBufferCountSetAndUpdated) {
1998 sp<IGraphicBufferProducer> producer;
1999 sp<IGraphicBufferConsumer> consumer;
2000 BufferQueue::createBufferQueue(&producer, &consumer);
2001
Peiyong Lind8460c82020-07-28 16:04:22 -07002002 sp<MockConsumer> mockConsumer(new MockConsumer);
2003 consumer->consumerConnect(mockConsumer, false);
Yiwei Zhang74c9cc32020-06-20 03:35:17 -07002004
2005 sp<Surface> surface = new Surface(producer);
2006 sp<ANativeWindow> window(surface);
2007
2008 int count = -1;
2009 ASSERT_EQ(NO_ERROR, window->query(window.get(), NATIVE_WINDOW_MAX_BUFFER_COUNT, &count));
2010 EXPECT_EQ(BufferQueueDefs::NUM_BUFFER_SLOTS, count);
2011
2012 consumer->setMaxBufferCount(10);
2013 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU));
2014 EXPECT_EQ(NO_ERROR, window->query(window.get(), NATIVE_WINDOW_MAX_BUFFER_COUNT, &count));
2015 EXPECT_EQ(10, count);
2016
2017 ASSERT_EQ(NO_ERROR, native_window_api_disconnect(window.get(), NATIVE_WINDOW_API_CPU));
2018 ASSERT_EQ(NO_ERROR, window->query(window.get(), NATIVE_WINDOW_MAX_BUFFER_COUNT, &count));
2019 EXPECT_EQ(BufferQueueDefs::NUM_BUFFER_SLOTS, count);
2020}
2021
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -08002022TEST_F(SurfaceTest, BatchOperations) {
2023 const int BUFFER_COUNT = 16;
2024 const int BATCH_SIZE = 8;
2025 sp<IGraphicBufferProducer> producer;
2026 sp<IGraphicBufferConsumer> consumer;
2027 BufferQueue::createBufferQueue(&producer, &consumer);
2028
2029 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
2030 sp<Surface> surface = new Surface(producer);
2031 sp<ANativeWindow> window(surface);
2032 sp<StubProducerListener> listener = new StubProducerListener();
2033
2034 ASSERT_EQ(OK, surface->connect(NATIVE_WINDOW_API_CPU, /*listener*/listener,
2035 /*reportBufferRemoval*/false));
2036
2037 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
2038
2039 std::vector<Surface::BatchBuffer> buffers(BATCH_SIZE);
2040
2041 // Batch dequeued buffers can be queued individually
2042 ASSERT_EQ(NO_ERROR, surface->dequeueBuffers(&buffers));
2043 for (size_t i = 0; i < BATCH_SIZE; i++) {
2044 ANativeWindowBuffer* buffer = buffers[i].buffer;
2045 int fence = buffers[i].fenceFd;
2046 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
2047 }
2048
2049 // Batch dequeued buffers can be canceled individually
2050 ASSERT_EQ(NO_ERROR, surface->dequeueBuffers(&buffers));
2051 for (size_t i = 0; i < BATCH_SIZE; i++) {
2052 ANativeWindowBuffer* buffer = buffers[i].buffer;
2053 int fence = buffers[i].fenceFd;
2054 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
2055 }
2056
2057 // Batch dequeued buffers can be batch cancelled
2058 ASSERT_EQ(NO_ERROR, surface->dequeueBuffers(&buffers));
2059 ASSERT_EQ(NO_ERROR, surface->cancelBuffers(buffers));
2060
2061 // Batch dequeued buffers can be batch queued
2062 ASSERT_EQ(NO_ERROR, surface->dequeueBuffers(&buffers));
2063 std::vector<Surface::BatchQueuedBuffer> queuedBuffers(BATCH_SIZE);
2064 for (size_t i = 0; i < BATCH_SIZE; i++) {
2065 queuedBuffers[i].buffer = buffers[i].buffer;
2066 queuedBuffers[i].fenceFd = buffers[i].fenceFd;
2067 queuedBuffers[i].timestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
2068 }
2069 ASSERT_EQ(NO_ERROR, surface->queueBuffers(queuedBuffers));
2070
2071 ASSERT_EQ(NO_ERROR, surface->disconnect(NATIVE_WINDOW_API_CPU));
2072}
2073
2074TEST_F(SurfaceTest, BatchIllegalOperations) {
2075 const int BUFFER_COUNT = 16;
2076 const int BATCH_SIZE = 8;
2077 sp<IGraphicBufferProducer> producer;
2078 sp<IGraphicBufferConsumer> consumer;
2079 BufferQueue::createBufferQueue(&producer, &consumer);
2080
2081 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
2082 sp<Surface> surface = new Surface(producer);
2083 sp<ANativeWindow> window(surface);
2084 sp<StubProducerListener> listener = new StubProducerListener();
2085
2086 ASSERT_EQ(OK, surface->connect(NATIVE_WINDOW_API_CPU, /*listener*/listener,
2087 /*reportBufferRemoval*/false));
2088
2089 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
2090
2091 std::vector<Surface::BatchBuffer> buffers(BATCH_SIZE);
2092 std::vector<Surface::BatchQueuedBuffer> queuedBuffers(BATCH_SIZE);
2093
2094 // Batch operations are invalid in shared buffer mode
2095 surface->setSharedBufferMode(true);
2096 ASSERT_EQ(INVALID_OPERATION, surface->dequeueBuffers(&buffers));
2097 ASSERT_EQ(INVALID_OPERATION, surface->cancelBuffers(buffers));
2098 ASSERT_EQ(INVALID_OPERATION, surface->queueBuffers(queuedBuffers));
2099 surface->setSharedBufferMode(false);
2100
2101 ASSERT_EQ(NO_ERROR, surface->disconnect(NATIVE_WINDOW_API_CPU));
2102}
2103
Dan Stoza932f0082017-05-31 13:50:16 -07002104} // namespace android