blob: 2e3ab40e2fc5122de37b199495507a3608185035 [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 Luo9e84f332021-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 Luo9e84f332021-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 Luo9e84f332021-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 Luob5cb7c12022-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 }
Huihong Luo694aeff2022-02-15 10:43:00 -0800751 status_t setActiveColorMode(const sp<IBinder>& /*display*/, ColorMode /*colorMode*/) override {
752 return NO_ERROR;
753 }
Kriti Dang7defaf32021-11-15 11:55:43 +0100754 status_t setBootDisplayMode(const sp<IBinder>& /*display*/, ui::DisplayModeId /*id*/) override {
755 return NO_ERROR;
756 }
Dominik Laskowskif1833852021-03-23 15:06:50 -0700757
Brian Anderson3da8d272016-07-28 16:20:47 -0700758 status_t clearAnimationFrameStats() override { return NO_ERROR; }
759 status_t getAnimationFrameStats(FrameStats* /*outStats*/) const override {
760 return NO_ERROR;
761 }
Kriti Dang49ad4132021-01-08 11:49:56 +0100762 status_t overrideHdrTypes(const sp<IBinder>& /*display*/,
763 const std::vector<ui::Hdr>& /*hdrTypes*/) override {
764 return NO_ERROR;
765 }
Tej Singhe2751772021-04-06 22:05:29 -0700766 status_t onPullAtom(const int32_t /*atomId*/, std::string* /*outData*/,
767 bool* /*success*/) override {
768 return NO_ERROR;
769 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700770 status_t enableVSyncInjections(bool /*enable*/) override {
771 return NO_ERROR;
772 }
773 status_t injectVSync(nsecs_t /*when*/) override { return NO_ERROR; }
Vishnu Nair43bccf82020-06-05 10:53:37 -0700774 status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* /*layers*/) override {
Kalle Raitaa099a242017-01-11 11:17:29 -0800775 return NO_ERROR;
776 }
Peiyong Linc6780972018-10-28 15:24:08 -0700777 status_t getCompositionPreference(
778 ui::Dataspace* /*outDefaultDataspace*/, ui::PixelFormat* /*outDefaultPixelFormat*/,
779 ui::Dataspace* /*outWideColorGamutDataspace*/,
780 ui::PixelFormat* /*outWideColorGamutPixelFormat*/) const override {
Peiyong Lin0256f722018-08-31 15:45:10 -0700781 return NO_ERROR;
782 }
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700783 status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& /*display*/,
784 ui::PixelFormat* /*outFormat*/,
785 ui::Dataspace* /*outDataspace*/,
786 uint8_t* /*outComponentMask*/) const override {
787 return NO_ERROR;
788 }
Kevin DuBois74e53772018-11-19 10:52:38 -0800789 status_t setDisplayContentSamplingEnabled(const sp<IBinder>& /*display*/, bool /*enable*/,
790 uint8_t /*componentMask*/,
Dominik Laskowski470df5f2020-04-02 22:27:42 -0700791 uint64_t /*maxFrames*/) override {
Kevin DuBois74e53772018-11-19 10:52:38 -0800792 return NO_ERROR;
793 }
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700794 status_t getDisplayedContentSample(const sp<IBinder>& /*display*/, uint64_t /*maxFrames*/,
795 uint64_t /*timestamp*/,
796 DisplayedFrameStats* /*outStats*/) const override {
797 return NO_ERROR;
798 }
Brian Anderson3da8d272016-07-28 16:20:47 -0700799
Peiyong Lin3c2791e2019-01-14 17:05:18 -0800800 status_t getColorManagement(bool* /*outGetColorManagement*/) const override { return NO_ERROR; }
801 status_t getProtectedContentSupport(bool* /*outSupported*/) const override { return NO_ERROR; }
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700802
Dan Stoza84ab9372018-12-17 15:27:57 -0800803 status_t addRegionSamplingListener(const Rect& /*samplingArea*/,
804 const sp<IBinder>& /*stopLayerHandle*/,
805 const sp<IRegionSamplingListener>& /*listener*/) override {
806 return NO_ERROR;
807 }
808 status_t removeRegionSamplingListener(
809 const sp<IRegionSamplingListener>& /*listener*/) override {
810 return NO_ERROR;
811 }
Alec Mouria9a68a62021-03-04 19:14:50 -0800812 status_t addFpsListener(int32_t /*taskId*/, const sp<gui::IFpsListener>& /*listener*/) {
Alec Mouriadebf5c2021-01-05 12:57:36 -0800813 return NO_ERROR;
814 }
815 status_t removeFpsListener(const sp<gui::IFpsListener>& /*listener*/) { return NO_ERROR; }
Galia Peycheva8f04b302021-04-27 13:25:38 +0200816
817 status_t addTunnelModeEnabledListener(const sp<gui::ITunnelModeEnabledListener>& /*listener*/) {
818 return NO_ERROR;
819 }
820
821 status_t removeTunnelModeEnabledListener(
822 const sp<gui::ITunnelModeEnabledListener>& /*listener*/) {
823 return NO_ERROR;
824 }
825
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100826 status_t setDesiredDisplayModeSpecs(const sp<IBinder>& /*displayToken*/,
827 ui::DisplayModeId /*defaultMode*/,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100828 bool /*allowGroupSwitching*/,
829 float /*primaryRefreshRateMin*/,
830 float /*primaryRefreshRateMax*/,
831 float /*appRequestRefreshRateMin*/,
832 float /*appRequestRefreshRateMax*/) {
Ana Krulec0782b882019-10-15 17:34:54 -0700833 return NO_ERROR;
834 }
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100835 status_t getDesiredDisplayModeSpecs(const sp<IBinder>& /*displayToken*/,
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100836 ui::DisplayModeId* /*outDefaultMode*/,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100837 bool* /*outAllowGroupSwitching*/,
838 float* /*outPrimaryRefreshRateMin*/,
839 float* /*outPrimaryRefreshRateMax*/,
840 float* /*outAppRequestRefreshRateMin*/,
841 float* /*outAppRequestRefreshRateMax*/) override {
Ana Krulec234bb162019-11-10 22:55:55 +0100842 return NO_ERROR;
843 };
Dan Stoza84ab9372018-12-17 15:27:57 -0800844
Vishnu Nairb13bb952019-11-15 10:24:08 -0800845 status_t setGlobalShadowSettings(const half4& /*ambientColor*/, const half4& /*spotColor*/,
846 float /*lightPosY*/, float /*lightPosZ*/,
847 float /*lightRadius*/) override {
848 return NO_ERROR;
849 }
850
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500851 status_t getDisplayDecorationSupport(
852 const sp<IBinder>& /*displayToken*/,
853 std::optional<DisplayDecorationSupport>* /*outSupport*/) const override {
Leon Scroggins IIIe5cff632021-12-29 11:53:36 -0500854 return NO_ERROR;
855 }
856
Steven Thomas62a4cf82020-01-31 12:04:03 -0800857 status_t setFrameRate(const sp<IGraphicBufferProducer>& /*surface*/, float /*frameRate*/,
Marin Shalamanovc5986772021-03-16 16:09:49 +0100858 int8_t /*compatibility*/, int8_t /*changeFrameRateStrategy*/) override {
Steven Thomas62a4cf82020-01-31 12:04:03 -0800859 return NO_ERROR;
860 }
861
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000862 status_t setFrameTimelineInfo(const sp<IGraphicBufferProducer>& /*surface*/,
863 const FrameTimelineInfo& /*frameTimelineInfo*/) override {
Ady Abraham74e17562020-08-24 18:18:19 -0700864 return NO_ERROR;
865 }
Steven Thomasd4071902020-03-24 16:02:53 -0700866
Pablo Gamito6ee484d2020-07-30 14:26:28 +0000867 status_t addTransactionTraceListener(
868 const sp<gui::ITransactionTraceListener>& /*listener*/) override {
869 return NO_ERROR;
870 }
871
Ana Krulec31f2b3c2020-12-14 14:30:09 -0800872 int getGPUContextPriority() override { return 0; };
873
Ady Abraham899dcdb2021-06-15 16:56:21 -0700874 status_t getMaxAcquiredBufferCount(int* /*buffers*/) const override { return NO_ERROR; }
Ady Abraham564f9de2021-02-03 18:34:33 -0800875
chaviw60c9d3e2021-06-04 12:52:17 -0500876 status_t addWindowInfosListener(
877 const sp<gui::IWindowInfosListener>& /*windowInfosListener*/) const override {
878 return NO_ERROR;
879 }
880
881 status_t removeWindowInfosListener(
882 const sp<gui::IWindowInfosListener>& /*windowInfosListener*/) const override {
883 return NO_ERROR;
884 }
885
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800886 status_t setOverrideFrameRate(uid_t /*uid*/, float /*frameRate*/) override { return NO_ERROR; }
887
Brian Anderson3da8d272016-07-28 16:20:47 -0700888protected:
889 IBinder* onAsBinder() override { return nullptr; }
890
891private:
892 bool mSupportsPresent{true};
Brian Anderson3da8d272016-07-28 16:20:47 -0700893};
894
895class FakeProducerFrameEventHistory : public ProducerFrameEventHistory {
896public:
Chih-Hung Hsiehaaf62162018-12-20 15:45:04 -0800897 explicit FakeProducerFrameEventHistory(FenceToFenceTimeMap* fenceMap) : mFenceMap(fenceMap) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700898
899 ~FakeProducerFrameEventHistory() {}
900
901 void updateAcquireFence(uint64_t frameNumber,
902 std::shared_ptr<FenceTime>&& acquire) override {
903 // Verify the acquire fence being added isn't the one from the consumer.
904 EXPECT_NE(mConsumerAcquireFence, acquire);
905 // Override the fence, so we can verify this was called by the
906 // producer after the frame is queued.
907 ProducerFrameEventHistory::updateAcquireFence(frameNumber,
908 std::shared_ptr<FenceTime>(mAcquireFenceOverride));
909 }
910
911 void setAcquireFenceOverride(
912 const std::shared_ptr<FenceTime>& acquireFenceOverride,
913 const std::shared_ptr<FenceTime>& consumerAcquireFence) {
914 mAcquireFenceOverride = acquireFenceOverride;
915 mConsumerAcquireFence = consumerAcquireFence;
916 }
917
918protected:
919 std::shared_ptr<FenceTime> createFenceTime(const sp<Fence>& fence)
920 const override {
921 return mFenceMap->createFenceTimeForTest(fence);
922 }
923
924 FenceToFenceTimeMap* mFenceMap{nullptr};
925
926 std::shared_ptr<FenceTime> mAcquireFenceOverride{FenceTime::NO_FENCE};
927 std::shared_ptr<FenceTime> mConsumerAcquireFence{FenceTime::NO_FENCE};
928};
929
930
931class TestSurface : public Surface {
932public:
933 TestSurface(const sp<IGraphicBufferProducer>& bufferProducer,
934 FenceToFenceTimeMap* fenceMap)
935 : Surface(bufferProducer),
936 mFakeSurfaceComposer(new FakeSurfaceComposer) {
937 mFakeFrameEventHistory = new FakeProducerFrameEventHistory(fenceMap);
938 mFrameEventHistory.reset(mFakeFrameEventHistory);
939 }
940
941 ~TestSurface() override {}
942
943 sp<ISurfaceComposer> composerService() const override {
944 return mFakeSurfaceComposer;
945 }
946
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800947 nsecs_t now() const override {
948 return mNow;
949 }
950
951 void setNow(nsecs_t now) {
952 mNow = now;
953 }
954
Brian Anderson3da8d272016-07-28 16:20:47 -0700955public:
956 sp<FakeSurfaceComposer> mFakeSurfaceComposer;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800957 nsecs_t mNow = 0;
Brian Anderson3da8d272016-07-28 16:20:47 -0700958
959 // mFrameEventHistory owns the instance of FakeProducerFrameEventHistory,
960 // but this raw pointer gives access to test functionality.
961 FakeProducerFrameEventHistory* mFakeFrameEventHistory;
962};
963
964
Brian Andersond0010582017-03-07 13:20:31 -0800965class GetFrameTimestampsTest : public ::testing::Test {
Brian Anderson3da8d272016-07-28 16:20:47 -0700966protected:
967 struct FenceAndFenceTime {
968 explicit FenceAndFenceTime(FenceToFenceTimeMap& fenceMap)
969 : mFence(new Fence),
970 mFenceTime(fenceMap.createFenceTimeForTest(mFence)) {}
971 sp<Fence> mFence { nullptr };
972 std::shared_ptr<FenceTime> mFenceTime { nullptr };
973 };
974
975 struct RefreshEvents {
976 RefreshEvents(FenceToFenceTimeMap& fenceMap, nsecs_t refreshStart)
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800977 : mFenceMap(fenceMap),
978 kCompositorTiming(
979 {refreshStart, refreshStart + 1, refreshStart + 2 }),
980 kStartTime(refreshStart + 3),
981 kGpuCompositionDoneTime(refreshStart + 4),
982 kPresentTime(refreshStart + 5) {}
Brian Anderson3da8d272016-07-28 16:20:47 -0700983
984 void signalPostCompositeFences() {
985 mFenceMap.signalAllForTest(
986 mGpuCompositionDone.mFence, kGpuCompositionDoneTime);
987 mFenceMap.signalAllForTest(mPresent.mFence, kPresentTime);
988 }
989
990 FenceToFenceTimeMap& mFenceMap;
991
992 FenceAndFenceTime mGpuCompositionDone { mFenceMap };
993 FenceAndFenceTime mPresent { mFenceMap };
994
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800995 const CompositorTiming kCompositorTiming;
996
Brian Anderson3da8d272016-07-28 16:20:47 -0700997 const nsecs_t kStartTime;
998 const nsecs_t kGpuCompositionDoneTime;
999 const nsecs_t kPresentTime;
1000 };
1001
1002 struct FrameEvents {
1003 FrameEvents(FenceToFenceTimeMap& fenceMap, nsecs_t frameStartTime)
1004 : mFenceMap(fenceMap),
1005 kPostedTime(frameStartTime + 100),
1006 kRequestedPresentTime(frameStartTime + 200),
1007 kProducerAcquireTime(frameStartTime + 300),
1008 kConsumerAcquireTime(frameStartTime + 301),
1009 kLatchTime(frameStartTime + 500),
Brian Andersonf6386862016-10-31 16:34:13 -07001010 kDequeueReadyTime(frameStartTime + 600),
Brian Anderson4e606e32017-03-16 15:34:57 -07001011 kReleaseTime(frameStartTime + 700),
Brian Anderson3da8d272016-07-28 16:20:47 -07001012 mRefreshes {
1013 { mFenceMap, frameStartTime + 410 },
1014 { mFenceMap, frameStartTime + 420 },
1015 { mFenceMap, frameStartTime + 430 } } {}
1016
1017 void signalQueueFences() {
1018 mFenceMap.signalAllForTest(
1019 mAcquireConsumer.mFence, kConsumerAcquireTime);
1020 mFenceMap.signalAllForTest(
1021 mAcquireProducer.mFence, kProducerAcquireTime);
1022 }
1023
1024 void signalRefreshFences() {
1025 for (auto& re : mRefreshes) {
1026 re.signalPostCompositeFences();
1027 }
1028 }
1029
1030 void signalReleaseFences() {
Brian Anderson3da8d272016-07-28 16:20:47 -07001031 mFenceMap.signalAllForTest(mRelease.mFence, kReleaseTime);
1032 }
1033
1034 FenceToFenceTimeMap& mFenceMap;
1035
1036 FenceAndFenceTime mAcquireConsumer { mFenceMap };
1037 FenceAndFenceTime mAcquireProducer { mFenceMap };
Brian Anderson3da8d272016-07-28 16:20:47 -07001038 FenceAndFenceTime mRelease { mFenceMap };
1039
1040 const nsecs_t kPostedTime;
1041 const nsecs_t kRequestedPresentTime;
1042 const nsecs_t kProducerAcquireTime;
1043 const nsecs_t kConsumerAcquireTime;
1044 const nsecs_t kLatchTime;
Brian Andersonf6386862016-10-31 16:34:13 -07001045 const nsecs_t kDequeueReadyTime;
Brian Anderson3da8d272016-07-28 16:20:47 -07001046 const nsecs_t kReleaseTime;
1047
1048 RefreshEvents mRefreshes[3];
1049 };
1050
Brian Andersond0010582017-03-07 13:20:31 -08001051 GetFrameTimestampsTest() {}
Brian Anderson3da8d272016-07-28 16:20:47 -07001052
1053 virtual void SetUp() {
Brian Anderson3da8d272016-07-28 16:20:47 -07001054 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
1055 mFakeConsumer = new FakeConsumer;
1056 mCfeh = &mFakeConsumer->mFrameEventHistory;
1057 mConsumer->consumerConnect(mFakeConsumer, false);
1058 mConsumer->setConsumerName(String8("TestConsumer"));
1059 mSurface = new TestSurface(mProducer, &mFenceMap);
1060 mWindow = mSurface;
1061
1062 ASSERT_EQ(NO_ERROR, native_window_api_connect(mWindow.get(),
1063 NATIVE_WINDOW_API_CPU));
1064 native_window_set_buffer_count(mWindow.get(), 4);
1065 }
1066
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001067 void disableFrameTimestamps() {
1068 mFakeConsumer->mGetFrameTimestampsEnabled = false;
1069 native_window_enable_frame_timestamps(mWindow.get(), 0);
1070 mFrameTimestampsEnabled = false;
1071 }
1072
Brian Anderson3da8d272016-07-28 16:20:47 -07001073 void enableFrameTimestamps() {
1074 mFakeConsumer->mGetFrameTimestampsEnabled = true;
1075 native_window_enable_frame_timestamps(mWindow.get(), 1);
1076 mFrameTimestampsEnabled = true;
1077 }
1078
Brian Anderson1049d1d2016-12-16 17:25:57 -08001079 int getAllFrameTimestamps(uint64_t frameId) {
1080 return native_window_get_frame_timestamps(mWindow.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001081 &outRequestedPresentTime, &outAcquireTime, &outLatchTime,
1082 &outFirstRefreshStartTime, &outLastRefreshStartTime,
1083 &outGpuCompositionDoneTime, &outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001084 &outDequeueReadyTime, &outReleaseTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001085 }
1086
Brian Anderson3da8d272016-07-28 16:20:47 -07001087 void resetTimestamps() {
1088 outRequestedPresentTime = -1;
1089 outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001090 outLatchTime = -1;
1091 outFirstRefreshStartTime = -1;
1092 outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001093 outGpuCompositionDoneTime = -1;
1094 outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001095 outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001096 outReleaseTime = -1;
1097 }
1098
Brian Anderson1049d1d2016-12-16 17:25:57 -08001099 uint64_t getNextFrameId() {
1100 uint64_t frameId = -1;
1101 int status = native_window_get_next_frame_id(mWindow.get(), &frameId);
1102 EXPECT_EQ(status, NO_ERROR);
1103 return frameId;
1104 }
1105
Brian Anderson3da8d272016-07-28 16:20:47 -07001106 void dequeueAndQueue(uint64_t frameIndex) {
1107 int fence = -1;
1108 ANativeWindowBuffer* buffer = nullptr;
1109 ASSERT_EQ(NO_ERROR,
1110 mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1111
1112 int oldAddFrameTimestampsCount =
1113 mFakeConsumer->mAddFrameTimestampsCount;
1114
1115 FrameEvents* frame = &mFrames[frameIndex];
1116 uint64_t frameNumber = frameIndex + 1;
1117
1118 NewFrameEventsEntry fe;
1119 fe.frameNumber = frameNumber;
1120 fe.postedTime = frame->kPostedTime;
1121 fe.requestedPresentTime = frame->kRequestedPresentTime;
1122 fe.acquireFence = frame->mAcquireConsumer.mFenceTime;
1123 mFakeConsumer->mNewFrameEntryOverride = fe;
1124
1125 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1126 frame->mAcquireProducer.mFenceTime,
1127 frame->mAcquireConsumer.mFenceTime);
1128
1129 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1130
1131 EXPECT_EQ(frameNumber, mFakeConsumer->mLastAddedFrameNumber);
1132
1133 EXPECT_EQ(
1134 oldAddFrameTimestampsCount + (mFrameTimestampsEnabled ? 1 : 0),
1135 mFakeConsumer->mAddFrameTimestampsCount);
1136 }
1137
1138 void addFrameEvents(
1139 bool gpuComposited, uint64_t iOldFrame, int64_t iNewFrame) {
1140 FrameEvents* oldFrame =
1141 (iOldFrame == NO_FRAME_INDEX) ? nullptr : &mFrames[iOldFrame];
1142 FrameEvents* newFrame = &mFrames[iNewFrame];
1143
Courtney Goeltzenleuchter5e921442018-01-19 13:43:43 -08001144 uint64_t nOldFrame = (iOldFrame == NO_FRAME_INDEX) ? 0 : iOldFrame + 1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001145 uint64_t nNewFrame = iNewFrame + 1;
1146
Brian Anderson4e606e32017-03-16 15:34:57 -07001147 // Latch, Composite, and Release the frames in a plausible order.
1148 // Note: The timestamps won't necessarily match the order, but
Brian Anderson3da8d272016-07-28 16:20:47 -07001149 // that's okay for the purposes of this test.
1150 std::shared_ptr<FenceTime> gpuDoneFenceTime = FenceTime::NO_FENCE;
1151
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001152 // Composite the previous frame one more time, which helps verify
1153 // LastRefresh is updated properly.
1154 if (oldFrame != nullptr) {
1155 mCfeh->addPreComposition(nOldFrame,
1156 oldFrame->mRefreshes[2].kStartTime);
1157 gpuDoneFenceTime = gpuComposited ?
1158 oldFrame->mRefreshes[2].mGpuCompositionDone.mFenceTime :
1159 FenceTime::NO_FENCE;
1160 mCfeh->addPostComposition(nOldFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001161 oldFrame->mRefreshes[2].mPresent.mFenceTime,
1162 oldFrame->mRefreshes[2].kCompositorTiming);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001163 }
1164
1165 // Latch the new frame.
Brian Anderson3da8d272016-07-28 16:20:47 -07001166 mCfeh->addLatch(nNewFrame, newFrame->kLatchTime);
1167
1168 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[0].kStartTime);
1169 gpuDoneFenceTime = gpuComposited ?
1170 newFrame->mRefreshes[0].mGpuCompositionDone.mFenceTime :
1171 FenceTime::NO_FENCE;
1172 // HWC2 releases the previous buffer after a new latch just before
1173 // calling postComposition.
1174 if (oldFrame != nullptr) {
Brian Andersonf6386862016-10-31 16:34:13 -07001175 mCfeh->addRelease(nOldFrame, oldFrame->kDequeueReadyTime,
Brian Anderson3da8d272016-07-28 16:20:47 -07001176 std::shared_ptr<FenceTime>(oldFrame->mRelease.mFenceTime));
1177 }
1178 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001179 newFrame->mRefreshes[0].mPresent.mFenceTime,
1180 newFrame->mRefreshes[0].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -07001181
Brian Anderson3da8d272016-07-28 16:20:47 -07001182 mCfeh->addPreComposition(nNewFrame, newFrame->mRefreshes[1].kStartTime);
1183 gpuDoneFenceTime = gpuComposited ?
1184 newFrame->mRefreshes[1].mGpuCompositionDone.mFenceTime :
1185 FenceTime::NO_FENCE;
1186 mCfeh->addPostComposition(nNewFrame, gpuDoneFenceTime,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001187 newFrame->mRefreshes[1].mPresent.mFenceTime,
1188 newFrame->mRefreshes[1].kCompositorTiming);
Brian Anderson3da8d272016-07-28 16:20:47 -07001189 }
1190
Brian Anderson3da8d272016-07-28 16:20:47 -07001191 sp<IGraphicBufferProducer> mProducer;
1192 sp<IGraphicBufferConsumer> mConsumer;
1193 sp<FakeConsumer> mFakeConsumer;
1194 ConsumerFrameEventHistory* mCfeh;
1195 sp<TestSurface> mSurface;
1196 sp<ANativeWindow> mWindow;
1197
1198 FenceToFenceTimeMap mFenceMap;
1199
1200 bool mFrameTimestampsEnabled = false;
1201
1202 int64_t outRequestedPresentTime = -1;
1203 int64_t outAcquireTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001204 int64_t outLatchTime = -1;
1205 int64_t outFirstRefreshStartTime = -1;
1206 int64_t outLastRefreshStartTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001207 int64_t outGpuCompositionDoneTime = -1;
1208 int64_t outDisplayPresentTime = -1;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001209 int64_t outDequeueReadyTime = -1;
Brian Anderson3da8d272016-07-28 16:20:47 -07001210 int64_t outReleaseTime = -1;
1211
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001212 FrameEvents mFrames[3] {
1213 { mFenceMap, 1000 }, { mFenceMap, 2000 }, { mFenceMap, 3000 } };
Brian Anderson3da8d272016-07-28 16:20:47 -07001214};
1215
1216
1217// This test verifies that the frame timestamps are not retrieved when not
1218// explicitly enabled via native_window_enable_frame_timestamps.
1219// We want to check this to make sure there's no overhead for users
1220// that don't need the timestamp information.
1221TEST_F(GetFrameTimestampsTest, DefaultDisabled) {
1222 int fence;
1223 ANativeWindowBuffer* buffer;
1224
1225 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1226 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1227
Brian Anderson1049d1d2016-12-16 17:25:57 -08001228 const uint64_t fId = getNextFrameId();
1229
Brian Anderson3da8d272016-07-28 16:20:47 -07001230 // Verify the producer doesn't get frame timestamps piggybacked on dequeue.
1231 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1232 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
1233 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1234
1235 // Verify the producer doesn't get frame timestamps piggybacked on queue.
1236 // It is okay that frame timestamps are added in the consumer since it is
1237 // still needed for SurfaceFlinger dumps.
1238 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1239 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1240 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
1241
1242 // Verify attempts to get frame timestamps fail.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001243 int result = getAllFrameTimestamps(fId);
Brian Anderson3da8d272016-07-28 16:20:47 -07001244 EXPECT_EQ(INVALID_OPERATION, result);
1245 EXPECT_EQ(0, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001246
1247 // Verify compositor timing query fails.
1248 nsecs_t compositeDeadline = 0;
1249 nsecs_t compositeInterval = 0;
1250 nsecs_t compositeToPresentLatency = 0;
1251 result = native_window_get_compositor_timing(mWindow.get(),
1252 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1253 EXPECT_EQ(INVALID_OPERATION, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001254}
1255
1256// This test verifies that the frame timestamps are retrieved if explicitly
1257// enabled via native_window_enable_frame_timestamps.
1258TEST_F(GetFrameTimestampsTest, EnabledSimple) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001259 CompositorTiming initialCompositorTiming {
1260 1000000000, // 1s deadline
1261 16666667, // 16ms interval
1262 50000000, // 50ms present latency
1263 };
1264 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1265
Brian Anderson3da8d272016-07-28 16:20:47 -07001266 enableFrameTimestamps();
1267
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001268 // Verify the compositor timing query gets the initial compositor values
1269 // after timststamps are enabled; even before the first frame is queued
1270 // or dequeued.
1271 nsecs_t compositeDeadline = 0;
1272 nsecs_t compositeInterval = 0;
1273 nsecs_t compositeToPresentLatency = 0;
1274 mSurface->setNow(initialCompositorTiming.deadline - 1);
1275 int result = native_window_get_compositor_timing(mWindow.get(),
1276 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1277 EXPECT_EQ(NO_ERROR, result);
1278 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1279 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1280 EXPECT_EQ(initialCompositorTiming.presentLatency,
1281 compositeToPresentLatency);
1282
Brian Anderson3da8d272016-07-28 16:20:47 -07001283 int fence;
1284 ANativeWindowBuffer* buffer;
1285
1286 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001287 EXPECT_EQ(1, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001288
Brian Anderson1049d1d2016-12-16 17:25:57 -08001289 const uint64_t fId1 = getNextFrameId();
1290
Brian Anderson3da8d272016-07-28 16:20:47 -07001291 // Verify getFrameTimestamps is piggybacked on dequeue.
1292 ASSERT_EQ(NO_ERROR, mWindow->dequeueBuffer(mWindow.get(), &buffer, &fence));
1293 EXPECT_EQ(0, mFakeConsumer->mAddFrameTimestampsCount);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001294 EXPECT_EQ(2, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001295
1296 NewFrameEventsEntry f1;
1297 f1.frameNumber = 1;
1298 f1.postedTime = mFrames[0].kPostedTime;
1299 f1.requestedPresentTime = mFrames[0].kRequestedPresentTime;
1300 f1.acquireFence = mFrames[0].mAcquireConsumer.mFenceTime;
1301 mSurface->mFakeFrameEventHistory->setAcquireFenceOverride(
1302 mFrames[0].mAcquireProducer.mFenceTime,
1303 mFrames[0].mAcquireConsumer.mFenceTime);
1304 mFakeConsumer->mNewFrameEntryOverride = f1;
1305 mFrames[0].signalQueueFences();
1306
1307 // Verify getFrameTimestamps is piggybacked on queue.
1308 ASSERT_EQ(NO_ERROR, mWindow->queueBuffer(mWindow.get(), buffer, fence));
1309 EXPECT_EQ(1, mFakeConsumer->mAddFrameTimestampsCount);
1310 EXPECT_EQ(1u, mFakeConsumer->mLastAddedFrameNumber);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001311 EXPECT_EQ(3, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001312
1313 // Verify queries for timestamps that the producer doesn't know about
1314 // triggers a call to see if the consumer has any new timestamps.
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001315 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001316 EXPECT_EQ(NO_ERROR, result);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001317 EXPECT_EQ(4, mFakeConsumer->mGetFrameTimestampsCount);
Brian Anderson3da8d272016-07-28 16:20:47 -07001318}
1319
Brian Anderson6b376712017-04-04 10:51:39 -07001320TEST_F(GetFrameTimestampsTest, QueryPresentSupported) {
1321 bool displayPresentSupported = true;
1322 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1323
1324 // Verify supported bits are forwarded.
1325 int supportsPresent = -1;
1326 mWindow.get()->query(mWindow.get(),
1327 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1328 EXPECT_EQ(displayPresentSupported, supportsPresent);
1329}
1330
1331TEST_F(GetFrameTimestampsTest, QueryPresentNotSupported) {
1332 bool displayPresentSupported = false;
1333 mSurface->mFakeSurfaceComposer->setSupportsPresent(displayPresentSupported);
1334
1335 // Verify supported bits are forwarded.
1336 int supportsPresent = -1;
1337 mWindow.get()->query(mWindow.get(),
1338 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &supportsPresent);
1339 EXPECT_EQ(displayPresentSupported, supportsPresent);
1340}
1341
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001342TEST_F(GetFrameTimestampsTest, SnapToNextTickBasic) {
1343 nsecs_t phase = 4000;
1344 nsecs_t interval = 1000;
1345
1346 // Timestamp in previous interval.
1347 nsecs_t timestamp = 3500;
1348 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1349 timestamp, phase, interval));
1350
1351 // Timestamp in next interval.
1352 timestamp = 4500;
1353 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1354 timestamp, phase, interval));
1355
1356 // Timestamp multiple intervals before.
1357 timestamp = 2500;
1358 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1359 timestamp, phase, interval));
1360
1361 // Timestamp multiple intervals after.
1362 timestamp = 6500;
1363 EXPECT_EQ(7000, ProducerFrameEventHistory::snapToNextTick(
1364 timestamp, phase, interval));
1365
1366 // Timestamp on previous interval.
1367 timestamp = 3000;
1368 EXPECT_EQ(3000, ProducerFrameEventHistory::snapToNextTick(
1369 timestamp, phase, interval));
1370
1371 // Timestamp on next interval.
1372 timestamp = 5000;
1373 EXPECT_EQ(5000, ProducerFrameEventHistory::snapToNextTick(
1374 timestamp, phase, interval));
1375
1376 // Timestamp equal to phase.
1377 timestamp = 4000;
1378 EXPECT_EQ(4000, ProducerFrameEventHistory::snapToNextTick(
1379 timestamp, phase, interval));
1380}
1381
1382// int(big_timestamp / interval) < 0, which can cause a crash or invalid result
1383// if the number of intervals elapsed is internally stored in an int.
1384TEST_F(GetFrameTimestampsTest, SnapToNextTickOverflow) {
1385 nsecs_t phase = 0;
1386 nsecs_t interval = 4000;
1387 nsecs_t big_timestamp = 8635916564000;
1388 int32_t intervals = big_timestamp / interval;
1389
1390 EXPECT_LT(intervals, 0);
1391 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1392 big_timestamp, phase, interval));
1393 EXPECT_EQ(8635916564000, ProducerFrameEventHistory::snapToNextTick(
1394 big_timestamp, big_timestamp, interval));
1395}
1396
1397// This verifies the compositor timing is updated by refresh events
1398// and piggy backed on a queue, dequeue, and enabling of timestamps..
1399TEST_F(GetFrameTimestampsTest, CompositorTimingUpdatesBasic) {
1400 CompositorTiming initialCompositorTiming {
1401 1000000000, // 1s deadline
1402 16666667, // 16ms interval
1403 50000000, // 50ms present latency
1404 };
1405 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1406
1407 enableFrameTimestamps();
1408
1409 // We get the initial values before any frames are submitted.
1410 nsecs_t compositeDeadline = 0;
1411 nsecs_t compositeInterval = 0;
1412 nsecs_t compositeToPresentLatency = 0;
1413 mSurface->setNow(initialCompositorTiming.deadline - 1);
1414 int result = native_window_get_compositor_timing(mWindow.get(),
1415 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1416 EXPECT_EQ(NO_ERROR, result);
1417 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1418 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1419 EXPECT_EQ(initialCompositorTiming.presentLatency,
1420 compositeToPresentLatency);
1421
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001422 dequeueAndQueue(0);
1423 addFrameEvents(true, NO_FRAME_INDEX, 0);
1424
1425 // Still get the initial values because the frame events for frame 0
1426 // didn't get a chance to piggyback on a queue or dequeue yet.
1427 result = native_window_get_compositor_timing(mWindow.get(),
1428 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1429 EXPECT_EQ(NO_ERROR, result);
1430 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1431 EXPECT_EQ(initialCompositorTiming.interval, compositeInterval);
1432 EXPECT_EQ(initialCompositorTiming.presentLatency,
1433 compositeToPresentLatency);
1434
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001435 dequeueAndQueue(1);
1436 addFrameEvents(true, 0, 1);
1437
1438 // Now expect the composite values associated with frame 1.
1439 mSurface->setNow(mFrames[0].mRefreshes[1].kCompositorTiming.deadline);
1440 result = native_window_get_compositor_timing(mWindow.get(),
1441 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1442 EXPECT_EQ(NO_ERROR, result);
1443 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.deadline,
1444 compositeDeadline);
1445 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.interval,
1446 compositeInterval);
1447 EXPECT_EQ(mFrames[0].mRefreshes[1].kCompositorTiming.presentLatency,
1448 compositeToPresentLatency);
1449
1450 dequeueAndQueue(2);
1451 addFrameEvents(true, 1, 2);
1452
1453 // Now expect the composite values associated with frame 2.
1454 mSurface->setNow(mFrames[1].mRefreshes[1].kCompositorTiming.deadline);
1455 result = native_window_get_compositor_timing(mWindow.get(),
1456 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1457 EXPECT_EQ(NO_ERROR, result);
1458 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.deadline,
1459 compositeDeadline);
1460 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.interval,
1461 compositeInterval);
1462 EXPECT_EQ(mFrames[1].mRefreshes[1].kCompositorTiming.presentLatency,
1463 compositeToPresentLatency);
1464
1465 // Re-enabling frame timestamps should get the latest values.
1466 disableFrameTimestamps();
1467 enableFrameTimestamps();
1468
1469 // Now expect the composite values associated with frame 3.
1470 mSurface->setNow(mFrames[2].mRefreshes[1].kCompositorTiming.deadline);
1471 result = native_window_get_compositor_timing(mWindow.get(),
1472 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1473 EXPECT_EQ(NO_ERROR, result);
1474 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.deadline,
1475 compositeDeadline);
1476 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.interval,
1477 compositeInterval);
1478 EXPECT_EQ(mFrames[2].mRefreshes[1].kCompositorTiming.presentLatency,
1479 compositeToPresentLatency);
1480}
1481
1482// This verifies the compositor deadline properly snaps to the the next
1483// deadline based on the current time.
1484TEST_F(GetFrameTimestampsTest, CompositorTimingDeadlineSnaps) {
1485 CompositorTiming initialCompositorTiming {
1486 1000000000, // 1s deadline
1487 16666667, // 16ms interval
1488 50000000, // 50ms present latency
1489 };
1490 mCfeh->initializeCompositorTiming(initialCompositorTiming);
1491
1492 enableFrameTimestamps();
1493
1494 nsecs_t compositeDeadline = 0;
1495 nsecs_t compositeInterval = 0;
1496 nsecs_t compositeToPresentLatency = 0;
1497
1498 // A "now" just before the deadline snaps to the deadline.
1499 mSurface->setNow(initialCompositorTiming.deadline - 1);
1500 int result = native_window_get_compositor_timing(mWindow.get(),
1501 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1502 EXPECT_EQ(NO_ERROR, result);
1503 EXPECT_EQ(initialCompositorTiming.deadline, compositeDeadline);
1504 nsecs_t expectedDeadline = initialCompositorTiming.deadline;
1505 EXPECT_EQ(expectedDeadline, compositeDeadline);
1506
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001507 dequeueAndQueue(0);
1508 addFrameEvents(true, NO_FRAME_INDEX, 0);
1509
1510 // A "now" just after the deadline snaps properly.
1511 mSurface->setNow(initialCompositorTiming.deadline + 1);
1512 result = native_window_get_compositor_timing(mWindow.get(),
1513 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1514 EXPECT_EQ(NO_ERROR, result);
1515 expectedDeadline =
1516 initialCompositorTiming.deadline +initialCompositorTiming.interval;
1517 EXPECT_EQ(expectedDeadline, compositeDeadline);
1518
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001519 dequeueAndQueue(1);
1520 addFrameEvents(true, 0, 1);
1521
1522 // A "now" just after the next interval snaps properly.
1523 mSurface->setNow(
1524 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1525 mFrames[0].mRefreshes[1].kCompositorTiming.interval + 1);
1526 result = native_window_get_compositor_timing(mWindow.get(),
1527 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1528 EXPECT_EQ(NO_ERROR, result);
1529 expectedDeadline =
1530 mFrames[0].mRefreshes[1].kCompositorTiming.deadline +
1531 mFrames[0].mRefreshes[1].kCompositorTiming.interval * 2;
1532 EXPECT_EQ(expectedDeadline, compositeDeadline);
1533
1534 dequeueAndQueue(2);
1535 addFrameEvents(true, 1, 2);
1536
1537 // A "now" over 1 interval before the deadline snaps properly.
1538 mSurface->setNow(
1539 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1540 mFrames[1].mRefreshes[1].kCompositorTiming.interval - 1);
1541 result = native_window_get_compositor_timing(mWindow.get(),
1542 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1543 EXPECT_EQ(NO_ERROR, result);
1544 expectedDeadline =
1545 mFrames[1].mRefreshes[1].kCompositorTiming.deadline -
1546 mFrames[1].mRefreshes[1].kCompositorTiming.interval;
1547 EXPECT_EQ(expectedDeadline, compositeDeadline);
1548
1549 // Re-enabling frame timestamps should get the latest values.
1550 disableFrameTimestamps();
1551 enableFrameTimestamps();
1552
1553 // A "now" over 2 intervals before the deadline snaps properly.
1554 mSurface->setNow(
1555 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1556 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2 - 1);
1557 result = native_window_get_compositor_timing(mWindow.get(),
1558 &compositeDeadline, &compositeInterval, &compositeToPresentLatency);
1559 EXPECT_EQ(NO_ERROR, result);
1560 expectedDeadline =
1561 mFrames[2].mRefreshes[1].kCompositorTiming.deadline -
1562 mFrames[2].mRefreshes[1].kCompositorTiming.interval * 2;
1563 EXPECT_EQ(expectedDeadline, compositeDeadline);
1564}
1565
Brian Anderson1049d1d2016-12-16 17:25:57 -08001566// This verifies the timestamps recorded in the consumer's
1567// FrameTimestampsHistory are properly retrieved by the producer for the
1568// correct frames.
Brian Anderson3da8d272016-07-28 16:20:47 -07001569TEST_F(GetFrameTimestampsTest, TimestampsAssociatedWithCorrectFrame) {
1570 enableFrameTimestamps();
1571
Brian Anderson1049d1d2016-12-16 17:25:57 -08001572 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001573 dequeueAndQueue(0);
1574 mFrames[0].signalQueueFences();
1575
Brian Anderson1049d1d2016-12-16 17:25:57 -08001576 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001577 dequeueAndQueue(1);
1578 mFrames[1].signalQueueFences();
1579
1580 addFrameEvents(true, NO_FRAME_INDEX, 0);
1581 mFrames[0].signalRefreshFences();
1582 addFrameEvents(true, 0, 1);
1583 mFrames[0].signalReleaseFences();
1584 mFrames[1].signalRefreshFences();
1585
1586 // Verify timestamps are correct for frame 1.
Brian Anderson3da8d272016-07-28 16:20:47 -07001587 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001588 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001589 EXPECT_EQ(NO_ERROR, result);
1590 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1591 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001592 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1593 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1594 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001595 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1596 outGpuCompositionDoneTime);
1597 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001598 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001599 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1600
1601 // Verify timestamps are correct for frame 2.
Brian Anderson3da8d272016-07-28 16:20:47 -07001602 resetTimestamps();
Brian Anderson1049d1d2016-12-16 17:25:57 -08001603 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001604 EXPECT_EQ(NO_ERROR, result);
1605 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1606 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001607 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1608 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1609 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001610 EXPECT_EQ(mFrames[1].mRefreshes[0].kGpuCompositionDoneTime,
1611 outGpuCompositionDoneTime);
1612 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001613 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1614 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001615}
1616
1617// This test verifies the acquire fence recorded by the consumer is not sent
1618// back to the producer and the producer saves its own fence.
1619TEST_F(GetFrameTimestampsTest, QueueTimestampsNoSync) {
1620 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001621
Brian Anderson3da8d272016-07-28 16:20:47 -07001622 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001623 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001624 dequeueAndQueue(0);
1625
1626 // Verify queue-related timestamps for f1 are available immediately in the
1627 // producer without asking the consumer again, even before signaling the
1628 // acquire fence.
1629 resetTimestamps();
1630 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001631 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001632 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001633 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001634 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1635 EXPECT_EQ(NO_ERROR, result);
1636 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001637 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001638
1639 // Signal acquire fences. Verify a sync call still isn't necessary.
1640 mFrames[0].signalQueueFences();
1641
1642 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001643 result = native_window_get_frame_timestamps(mWindow.get(), fId1,
Brian Anderson3da8d272016-07-28 16:20:47 -07001644 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001645 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001646 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1647 EXPECT_EQ(NO_ERROR, result);
1648 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1649 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
1650
1651 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001652 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001653 dequeueAndQueue(1);
1654
1655 // Verify queue-related timestamps for f2 are available immediately in the
1656 // producer without asking the consumer again, even before signaling the
1657 // acquire fence.
1658 resetTimestamps();
1659 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001660 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001661 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001662 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001663 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1664 EXPECT_EQ(NO_ERROR, result);
1665 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001666 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outAcquireTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001667
1668 // Signal acquire fences. Verify a sync call still isn't necessary.
1669 mFrames[1].signalQueueFences();
1670
1671 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001672 result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson3da8d272016-07-28 16:20:47 -07001673 &outRequestedPresentTime, &outAcquireTime, nullptr, nullptr,
Brian Anderson4e606e32017-03-16 15:34:57 -07001674 nullptr, nullptr, nullptr, nullptr, nullptr);
Brian Anderson3da8d272016-07-28 16:20:47 -07001675 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1676 EXPECT_EQ(NO_ERROR, result);
1677 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1678 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
1679}
1680
1681TEST_F(GetFrameTimestampsTest, ZeroRequestedTimestampsNoSync) {
1682 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001683
1684 // Dequeue and queue frame 1.
1685 dequeueAndQueue(0);
1686 mFrames[0].signalQueueFences();
1687
1688 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001689 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001690 dequeueAndQueue(1);
1691 mFrames[1].signalQueueFences();
1692
1693 addFrameEvents(true, NO_FRAME_INDEX, 0);
1694 mFrames[0].signalRefreshFences();
1695 addFrameEvents(true, 0, 1);
1696 mFrames[0].signalReleaseFences();
1697 mFrames[1].signalRefreshFences();
1698
1699 // Verify a request for no timestamps doesn't result in a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001700 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001701 int result = native_window_get_frame_timestamps(mWindow.get(), fId2,
Brian Anderson6b376712017-04-04 10:51:39 -07001702 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1703 nullptr, nullptr);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001704 EXPECT_EQ(NO_ERROR, result);
Brian Anderson3da8d272016-07-28 16:20:47 -07001705 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1706}
1707
1708// This test verifies that fences can signal and update timestamps producer
1709// side without an additional sync call to the consumer.
1710TEST_F(GetFrameTimestampsTest, FencesInProducerNoSync) {
1711 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001712
1713 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001714 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001715 dequeueAndQueue(0);
1716 mFrames[0].signalQueueFences();
1717
1718 // Dequeue and queue frame 2.
1719 dequeueAndQueue(1);
1720 mFrames[1].signalQueueFences();
1721
1722 addFrameEvents(true, NO_FRAME_INDEX, 0);
1723 addFrameEvents(true, 0, 1);
1724
1725 // Verify available timestamps are correct for frame 1, before any
1726 // fence has been signaled.
1727 // Note: A sync call is necessary here since the events triggered by
1728 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001729 resetTimestamps();
1730 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001731 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001732 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1733 EXPECT_EQ(NO_ERROR, result);
1734 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1735 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001736 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1737 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1738 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001739 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1740 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001741 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001742 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001743
1744 // Verify available timestamps are correct for frame 1 again, before any
1745 // fence has been signaled.
1746 // This time a sync call should not be necessary.
Brian Anderson3da8d272016-07-28 16:20:47 -07001747 resetTimestamps();
1748 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001749 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001750 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1751 EXPECT_EQ(NO_ERROR, result);
1752 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1753 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001754 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1755 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1756 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001757 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outGpuCompositionDoneTime);
1758 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001759 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001760 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001761
1762 // Signal the fences for frame 1.
1763 mFrames[0].signalRefreshFences();
1764 mFrames[0].signalReleaseFences();
1765
1766 // Verify all timestamps are available without a sync call.
Brian Anderson3da8d272016-07-28 16:20:47 -07001767 resetTimestamps();
1768 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001769 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001770 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1771 EXPECT_EQ(NO_ERROR, result);
1772 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1773 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001774 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1775 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1776 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001777 EXPECT_EQ(mFrames[0].mRefreshes[0].kGpuCompositionDoneTime,
1778 outGpuCompositionDoneTime);
1779 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001780 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001781 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1782}
1783
1784// This test verifies that if the frame wasn't GPU composited but has a refresh
1785// event a sync call isn't made to get the GPU composite done time since it will
1786// never exist.
1787TEST_F(GetFrameTimestampsTest, NoGpuNoSync) {
1788 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001789
Brian Anderson3da8d272016-07-28 16:20:47 -07001790 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001791 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001792 dequeueAndQueue(0);
1793 mFrames[0].signalQueueFences();
1794
1795 // Dequeue and queue frame 2.
1796 dequeueAndQueue(1);
1797 mFrames[1].signalQueueFences();
1798
1799 addFrameEvents(false, NO_FRAME_INDEX, 0);
1800 addFrameEvents(false, 0, 1);
1801
1802 // Verify available timestamps are correct for frame 1, before any
1803 // fence has been signaled.
1804 // Note: A sync call is necessary here since the events triggered by
1805 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
1806 resetTimestamps();
1807 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001808 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001809 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1810 EXPECT_EQ(NO_ERROR, result);
1811 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1812 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001813 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1814 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1815 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001816 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1817 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001818 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001819 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001820
1821 // Signal the fences for frame 1.
1822 mFrames[0].signalRefreshFences();
1823 mFrames[0].signalReleaseFences();
1824
1825 // Verify all timestamps, except GPU composition, are available without a
1826 // sync call.
1827 resetTimestamps();
1828 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001829 result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001830 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1831 EXPECT_EQ(NO_ERROR, result);
1832 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1833 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001834 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1835 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1836 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001837 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001838 EXPECT_EQ(mFrames[0].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001839 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001840 EXPECT_EQ(mFrames[0].kReleaseTime, outReleaseTime);
1841}
1842
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001843// This test verifies that if the certain timestamps can't possibly exist for
1844// the most recent frame, then a sync call is not done.
Brian Anderson6b376712017-04-04 10:51:39 -07001845TEST_F(GetFrameTimestampsTest, NoReleaseNoSync) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001846 enableFrameTimestamps();
Brian Anderson3da8d272016-07-28 16:20:47 -07001847
1848 // Dequeue and queue frame 1.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001849 const uint64_t fId1 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001850 dequeueAndQueue(0);
1851 mFrames[0].signalQueueFences();
1852
1853 // Dequeue and queue frame 2.
Brian Anderson1049d1d2016-12-16 17:25:57 -08001854 const uint64_t fId2 = getNextFrameId();
Brian Anderson3da8d272016-07-28 16:20:47 -07001855 dequeueAndQueue(1);
1856 mFrames[1].signalQueueFences();
1857
1858 addFrameEvents(false, NO_FRAME_INDEX, 0);
1859 addFrameEvents(false, 0, 1);
1860
1861 // Verify available timestamps are correct for frame 1, before any
1862 // fence has been signaled.
1863 // Note: A sync call is necessary here since the events triggered by
1864 // addFrameEvents didn't get to piggyback on the earlier queues/dequeues.
Brian Anderson3da8d272016-07-28 16:20:47 -07001865 resetTimestamps();
1866 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001867 int result = getAllFrameTimestamps(fId1);
Brian Anderson3da8d272016-07-28 16:20:47 -07001868 EXPECT_EQ(oldCount + 1, mFakeConsumer->mGetFrameTimestampsCount);
1869 EXPECT_EQ(NO_ERROR, result);
1870 EXPECT_EQ(mFrames[0].kRequestedPresentTime, outRequestedPresentTime);
1871 EXPECT_EQ(mFrames[0].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001872 EXPECT_EQ(mFrames[0].kLatchTime, outLatchTime);
1873 EXPECT_EQ(mFrames[0].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1874 EXPECT_EQ(mFrames[0].mRefreshes[2].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001875 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
1876 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDisplayPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001877 EXPECT_EQ(mFrames[0].kDequeueReadyTime, outDequeueReadyTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001878 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001879
1880 mFrames[0].signalRefreshFences();
1881 mFrames[0].signalReleaseFences();
1882 mFrames[1].signalRefreshFences();
1883
Brian Anderson1049d1d2016-12-16 17:25:57 -08001884 // Verify querying for all timestmaps of f2 does not do a sync call. Even
Brian Anderson4e606e32017-03-16 15:34:57 -07001885 // though the lastRefresh, dequeueReady, and release times aren't
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001886 // available, a sync call should not occur because it's not possible for f2
1887 // to encounter the final value for those events until another frame is
1888 // queued.
Brian Anderson3da8d272016-07-28 16:20:47 -07001889 resetTimestamps();
1890 oldCount = mFakeConsumer->mGetFrameTimestampsCount;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001891 result = getAllFrameTimestamps(fId2);
Brian Anderson3da8d272016-07-28 16:20:47 -07001892 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1893 EXPECT_EQ(NO_ERROR, result);
1894 EXPECT_EQ(mFrames[1].kRequestedPresentTime, outRequestedPresentTime);
1895 EXPECT_EQ(mFrames[1].kProducerAcquireTime, outAcquireTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001896 EXPECT_EQ(mFrames[1].kLatchTime, outLatchTime);
1897 EXPECT_EQ(mFrames[1].mRefreshes[0].kStartTime, outFirstRefreshStartTime);
1898 EXPECT_EQ(mFrames[1].mRefreshes[1].kStartTime, outLastRefreshStartTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001899 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_INVALID, outGpuCompositionDoneTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001900 EXPECT_EQ(mFrames[1].mRefreshes[0].kPresentTime, outDisplayPresentTime);
Brian Andersondc96fdf2017-03-20 16:54:25 -07001901 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outDequeueReadyTime);
1902 EXPECT_EQ(NATIVE_WINDOW_TIMESTAMP_PENDING, outReleaseTime);
Brian Anderson3da8d272016-07-28 16:20:47 -07001903}
1904
Brian Anderson6b376712017-04-04 10:51:39 -07001905// This test verifies there are no sync calls for present times
1906// when they aren't supported and that an error is returned.
1907
1908TEST_F(GetFrameTimestampsTest, PresentUnsupportedNoSync) {
1909 enableFrameTimestamps();
1910 mSurface->mFakeSurfaceComposer->setSupportsPresent(false);
1911
1912 // Dequeue and queue frame 1.
1913 const uint64_t fId1 = getNextFrameId();
1914 dequeueAndQueue(0);
1915
1916 // Verify a query for the Present times do not trigger a sync call if they
1917 // are not supported.
1918 resetTimestamps();
1919 int oldCount = mFakeConsumer->mGetFrameTimestampsCount;
1920 int result = native_window_get_frame_timestamps(mWindow.get(), fId1,
1921 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1922 &outDisplayPresentTime, nullptr, nullptr);
1923 EXPECT_EQ(oldCount, mFakeConsumer->mGetFrameTimestampsCount);
1924 EXPECT_EQ(BAD_VALUE, result);
1925 EXPECT_EQ(-1, outDisplayPresentTime);
1926}
1927
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001928TEST_F(SurfaceTest, DequeueWithConsumerDrivenSize) {
1929 sp<IGraphicBufferProducer> producer;
1930 sp<IGraphicBufferConsumer> consumer;
1931 BufferQueue::createBufferQueue(&producer, &consumer);
1932
Peiyong Lind8460c82020-07-28 16:04:22 -07001933 sp<MockConsumer> mockConsumer(new MockConsumer);
1934 consumer->consumerConnect(mockConsumer, false);
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001935 consumer->setDefaultBufferSize(10, 10);
1936
1937 sp<Surface> surface = new Surface(producer);
1938 sp<ANativeWindow> window(surface);
1939 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
1940 native_window_set_buffers_dimensions(window.get(), 0, 0);
1941
1942 int fence;
1943 ANativeWindowBuffer* buffer;
1944
1945 // Buffer size is driven by the consumer
1946 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1947 EXPECT_EQ(10, buffer->width);
1948 EXPECT_EQ(10, buffer->height);
1949 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1950
1951 // Buffer size is driven by the consumer
1952 consumer->setDefaultBufferSize(10, 20);
1953 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1954 EXPECT_EQ(10, buffer->width);
1955 EXPECT_EQ(20, buffer->height);
1956 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1957
1958 // Transform hint isn't synced to producer before queueBuffer or connect
1959 consumer->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_270);
1960 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1961 EXPECT_EQ(10, buffer->width);
1962 EXPECT_EQ(20, buffer->height);
1963 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
1964
1965 // Transform hint is synced to producer but no auto prerotation
1966 consumer->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_270);
1967 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1968 EXPECT_EQ(10, buffer->width);
1969 EXPECT_EQ(20, buffer->height);
1970 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1971
1972 // Prerotation is driven by the consumer with the transform hint used by producer
1973 native_window_set_auto_prerotation(window.get(), true);
1974 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1975 EXPECT_EQ(20, buffer->width);
1976 EXPECT_EQ(10, buffer->height);
1977 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1978
1979 // Turn off auto prerotaton
1980 native_window_set_auto_prerotation(window.get(), false);
1981 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1982 EXPECT_EQ(10, buffer->width);
1983 EXPECT_EQ(20, buffer->height);
1984 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1985
1986 // Test auto prerotation bit is disabled after disconnect
1987 native_window_set_auto_prerotation(window.get(), true);
1988 native_window_api_disconnect(window.get(), NATIVE_WINDOW_API_CPU);
1989 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
1990 consumer->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_270);
1991 native_window_set_buffers_dimensions(window.get(), 0, 0);
1992 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
1993 EXPECT_EQ(10, buffer->width);
1994 EXPECT_EQ(20, buffer->height);
1995 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
1996}
1997
Yiwei Zhang74c9cc32020-06-20 03:35:17 -07001998TEST_F(SurfaceTest, DefaultMaxBufferCountSetAndUpdated) {
1999 sp<IGraphicBufferProducer> producer;
2000 sp<IGraphicBufferConsumer> consumer;
2001 BufferQueue::createBufferQueue(&producer, &consumer);
2002
Peiyong Lind8460c82020-07-28 16:04:22 -07002003 sp<MockConsumer> mockConsumer(new MockConsumer);
2004 consumer->consumerConnect(mockConsumer, false);
Yiwei Zhang74c9cc32020-06-20 03:35:17 -07002005
2006 sp<Surface> surface = new Surface(producer);
2007 sp<ANativeWindow> window(surface);
2008
2009 int count = -1;
2010 ASSERT_EQ(NO_ERROR, window->query(window.get(), NATIVE_WINDOW_MAX_BUFFER_COUNT, &count));
2011 EXPECT_EQ(BufferQueueDefs::NUM_BUFFER_SLOTS, count);
2012
2013 consumer->setMaxBufferCount(10);
2014 ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU));
2015 EXPECT_EQ(NO_ERROR, window->query(window.get(), NATIVE_WINDOW_MAX_BUFFER_COUNT, &count));
2016 EXPECT_EQ(10, count);
2017
2018 ASSERT_EQ(NO_ERROR, native_window_api_disconnect(window.get(), NATIVE_WINDOW_API_CPU));
2019 ASSERT_EQ(NO_ERROR, window->query(window.get(), NATIVE_WINDOW_MAX_BUFFER_COUNT, &count));
2020 EXPECT_EQ(BufferQueueDefs::NUM_BUFFER_SLOTS, count);
2021}
2022
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -08002023TEST_F(SurfaceTest, BatchOperations) {
2024 const int BUFFER_COUNT = 16;
2025 const int BATCH_SIZE = 8;
2026 sp<IGraphicBufferProducer> producer;
2027 sp<IGraphicBufferConsumer> consumer;
2028 BufferQueue::createBufferQueue(&producer, &consumer);
2029
2030 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
2031 sp<Surface> surface = new Surface(producer);
2032 sp<ANativeWindow> window(surface);
2033 sp<StubProducerListener> listener = new StubProducerListener();
2034
2035 ASSERT_EQ(OK, surface->connect(NATIVE_WINDOW_API_CPU, /*listener*/listener,
2036 /*reportBufferRemoval*/false));
2037
2038 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
2039
2040 std::vector<Surface::BatchBuffer> buffers(BATCH_SIZE);
2041
2042 // Batch dequeued buffers can be queued individually
2043 ASSERT_EQ(NO_ERROR, surface->dequeueBuffers(&buffers));
2044 for (size_t i = 0; i < BATCH_SIZE; i++) {
2045 ANativeWindowBuffer* buffer = buffers[i].buffer;
2046 int fence = buffers[i].fenceFd;
2047 ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
2048 }
2049
2050 // Batch dequeued buffers can be canceled individually
2051 ASSERT_EQ(NO_ERROR, surface->dequeueBuffers(&buffers));
2052 for (size_t i = 0; i < BATCH_SIZE; i++) {
2053 ANativeWindowBuffer* buffer = buffers[i].buffer;
2054 int fence = buffers[i].fenceFd;
2055 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fence));
2056 }
2057
2058 // Batch dequeued buffers can be batch cancelled
2059 ASSERT_EQ(NO_ERROR, surface->dequeueBuffers(&buffers));
2060 ASSERT_EQ(NO_ERROR, surface->cancelBuffers(buffers));
2061
2062 // Batch dequeued buffers can be batch queued
2063 ASSERT_EQ(NO_ERROR, surface->dequeueBuffers(&buffers));
2064 std::vector<Surface::BatchQueuedBuffer> queuedBuffers(BATCH_SIZE);
2065 for (size_t i = 0; i < BATCH_SIZE; i++) {
2066 queuedBuffers[i].buffer = buffers[i].buffer;
2067 queuedBuffers[i].fenceFd = buffers[i].fenceFd;
2068 queuedBuffers[i].timestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
2069 }
2070 ASSERT_EQ(NO_ERROR, surface->queueBuffers(queuedBuffers));
2071
2072 ASSERT_EQ(NO_ERROR, surface->disconnect(NATIVE_WINDOW_API_CPU));
2073}
2074
2075TEST_F(SurfaceTest, BatchIllegalOperations) {
2076 const int BUFFER_COUNT = 16;
2077 const int BATCH_SIZE = 8;
2078 sp<IGraphicBufferProducer> producer;
2079 sp<IGraphicBufferConsumer> consumer;
2080 BufferQueue::createBufferQueue(&producer, &consumer);
2081
2082 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
2083 sp<Surface> surface = new Surface(producer);
2084 sp<ANativeWindow> window(surface);
2085 sp<StubProducerListener> listener = new StubProducerListener();
2086
2087 ASSERT_EQ(OK, surface->connect(NATIVE_WINDOW_API_CPU, /*listener*/listener,
2088 /*reportBufferRemoval*/false));
2089
2090 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(window.get(), BUFFER_COUNT));
2091
2092 std::vector<Surface::BatchBuffer> buffers(BATCH_SIZE);
2093 std::vector<Surface::BatchQueuedBuffer> queuedBuffers(BATCH_SIZE);
2094
2095 // Batch operations are invalid in shared buffer mode
2096 surface->setSharedBufferMode(true);
2097 ASSERT_EQ(INVALID_OPERATION, surface->dequeueBuffers(&buffers));
2098 ASSERT_EQ(INVALID_OPERATION, surface->cancelBuffers(buffers));
2099 ASSERT_EQ(INVALID_OPERATION, surface->queueBuffers(queuedBuffers));
2100 surface->setSharedBufferMode(false);
2101
2102 ASSERT_EQ(NO_ERROR, surface->disconnect(NATIVE_WINDOW_API_CPU));
2103}
2104
Dan Stoza932f0082017-05-31 13:50:16 -07002105} // namespace android