blob: 937ff022416532e24bded88f57fe109436a4bb55 [file] [log] [blame]
Alec Mouri9fa2cb62019-07-15 17:36:26 -07001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "ANativeWindow_test"
18//#define LOG_NDEBUG 0
19
20#include <gtest/gtest.h>
21#include <gui/BufferItemConsumer.h>
22#include <gui/BufferQueue.h>
23#include <gui/Surface.h>
24#include <log/log.h>
25#include <sync/sync.h>
26// We need to use the private system apis since not everything is visible to
27// apexes yet.
28#include <system/window.h>
29
30using namespace android;
31
Alec Mourif629f102019-08-06 18:16:23 -070032class TestableSurface final : public Surface {
33public:
34 explicit TestableSurface(const sp<IGraphicBufferProducer>& bufferProducer)
35 : Surface(bufferProducer) {}
36
37 // Exposes the internal last dequeue duration that's stored on the Surface.
38 nsecs_t getLastDequeueDuration() const { return mLastDequeueDuration; }
Alec Mouri0d1398b2019-08-14 10:53:50 -070039
40 // Exposes the internal last queue duration that's stored on the Surface.
41 nsecs_t getLastQueueDuration() const { return mLastQueueDuration; }
Alec Mouria1619662019-08-21 19:30:48 -070042
43 // Exposes the internal last dequeue start time that's stored on the Surface.
44 nsecs_t getLastDequeueStartTime() const { return mLastDequeueStartTime; }
Alec Mourif629f102019-08-06 18:16:23 -070045};
46
Alec Mouri9fa2cb62019-07-15 17:36:26 -070047class ANativeWindowTest : public ::testing::Test {
48protected:
49 void SetUp() override {
50 const ::testing::TestInfo* const test_info =
51 ::testing::UnitTest::GetInstance()->current_test_info();
52 ALOGV("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Jim Shargoe1bf47d2024-07-30 00:06:27 +000053#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
54 mItemConsumer = new BufferItemConsumer(GRALLOC_USAGE_SW_READ_OFTEN);
55 mWindow = new TestableSurface(mItemConsumer->getSurface()->getIGraphicBufferProducer());
56#else
Alec Mouri9fa2cb62019-07-15 17:36:26 -070057 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
58 mItemConsumer = new BufferItemConsumer(mConsumer, GRALLOC_USAGE_SW_READ_OFTEN);
Alec Mourif629f102019-08-06 18:16:23 -070059 mWindow = new TestableSurface(mProducer);
Jim Shargoe1bf47d2024-07-30 00:06:27 +000060#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
Alec Mouri9fa2cb62019-07-15 17:36:26 -070061 const int success = native_window_api_connect(mWindow.get(), NATIVE_WINDOW_API_CPU);
62 EXPECT_EQ(0, success);
63 }
64
65 void TearDown() override {
66 const ::testing::TestInfo* const test_info =
67 ::testing::UnitTest::GetInstance()->current_test_info();
68 ALOGV("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
69 const int success = native_window_api_disconnect(mWindow.get(), NATIVE_WINDOW_API_CPU);
70 EXPECT_EQ(0, success);
71 }
Jim Shargoe1bf47d2024-07-30 00:06:27 +000072
73#if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
Alec Mouri9fa2cb62019-07-15 17:36:26 -070074 sp<IGraphicBufferProducer> mProducer;
75 sp<IGraphicBufferConsumer> mConsumer;
Jim Shargoe1bf47d2024-07-30 00:06:27 +000076#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
Alec Mouri9fa2cb62019-07-15 17:36:26 -070077 sp<BufferItemConsumer> mItemConsumer;
Alec Mourif629f102019-08-06 18:16:23 -070078 sp<TestableSurface> mWindow;
Alec Mouri9fa2cb62019-07-15 17:36:26 -070079};
80
81TEST_F(ANativeWindowTest, getLastDequeueDuration_noDequeue_returnsZero) {
82 int result = ANativeWindow_getLastDequeueDuration(mWindow.get());
83 EXPECT_EQ(0, result);
Alec Mouri72670c52019-08-31 01:54:33 -070084 EXPECT_EQ(0, mWindow->getLastDequeueDuration());
Alec Mouri9fa2cb62019-07-15 17:36:26 -070085}
86
87TEST_F(ANativeWindowTest, getLastDequeueDuration_withDequeue_returnsTime) {
88 ANativeWindowBuffer* buffer;
89 int fd;
90 int result = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
91 close(fd);
92 EXPECT_EQ(0, result);
93
94 result = ANativeWindow_getLastDequeueDuration(mWindow.get());
95 EXPECT_GT(result, 0);
Alec Mouri72670c52019-08-31 01:54:33 -070096 EXPECT_EQ(result, mWindow->getLastDequeueDuration());
Alec Mouri9fa2cb62019-07-15 17:36:26 -070097}
Alec Mouri0d1398b2019-08-14 10:53:50 -070098
99TEST_F(ANativeWindowTest, getLastQueueDuration_noDequeue_returnsZero) {
100 int result = ANativeWindow_getLastQueueDuration(mWindow.get());
101 EXPECT_EQ(0, result);
102 EXPECT_EQ(0, mWindow->getLastQueueDuration());
103}
104
105TEST_F(ANativeWindowTest, getLastQueueDuration_noQueue_returnsZero) {
106 ANativeWindowBuffer* buffer;
107 int fd;
108 int result = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
109 close(fd);
110 EXPECT_EQ(0, result);
111
112 result = ANativeWindow_getLastQueueDuration(mWindow.get());
113 EXPECT_EQ(result, 0);
114 EXPECT_EQ(result, mWindow->getLastQueueDuration());
115}
116
117TEST_F(ANativeWindowTest, getLastQueueDuration_withQueue_returnsTime) {
118 ANativeWindowBuffer* buffer;
119 int fd;
120 int result = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
121 close(fd);
122 EXPECT_EQ(0, result);
123
124 result = ANativeWindow_queueBuffer(mWindow.get(), buffer, 0);
125
126 result = ANativeWindow_getLastQueueDuration(mWindow.get());
127 EXPECT_GT(result, 0);
Alec Mouri72670c52019-08-31 01:54:33 -0700128 EXPECT_EQ(result, mWindow->getLastQueueDuration());
Alec Mouri0d1398b2019-08-14 10:53:50 -0700129}
Alec Mouria1619662019-08-21 19:30:48 -0700130
131TEST_F(ANativeWindowTest, getLastDequeueStartTime_noDequeue_returnsZero) {
132 int64_t result = ANativeWindow_getLastDequeueStartTime(mWindow.get());
133 EXPECT_EQ(0, result);
134 EXPECT_EQ(0, mWindow->getLastQueueDuration());
135}
136
137TEST_F(ANativeWindowTest, getLastDequeueStartTime_withDequeue_returnsTime) {
138 ANativeWindowBuffer* buffer;
139 int fd;
140 int dequeueResult = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
141 close(fd);
142 EXPECT_EQ(0, dequeueResult);
143
144 int64_t result = ANativeWindow_getLastDequeueStartTime(mWindow.get());
145 EXPECT_GT(result, 0);
146 EXPECT_EQ(result, mWindow->getLastDequeueStartTime());
147}
Alec Mouri04fdb602019-08-23 19:41:43 -0700148
149TEST_F(ANativeWindowTest, setDequeueTimeout_causesDequeueTimeout) {
150 nsecs_t timeout = milliseconds_to_nanoseconds(100);
151 int result = ANativeWindow_setDequeueTimeout(mWindow.get(), timeout);
152 EXPECT_EQ(0, result);
153
154 // The two dequeues should not timeout...
155 ANativeWindowBuffer* buffer;
156 int fd;
157 int dequeueResult = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
158 close(fd);
159 EXPECT_EQ(0, dequeueResult);
160 int queueResult = ANativeWindow_queueBuffer(mWindow.get(), buffer, -1);
161 EXPECT_EQ(0, queueResult);
162 dequeueResult = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
163 close(fd);
164 EXPECT_EQ(0, dequeueResult);
165 queueResult = ANativeWindow_queueBuffer(mWindow.get(), buffer, -1);
166 EXPECT_EQ(0, queueResult);
167
168 // ...but the third one should since the queue depth is too deep.
169 nsecs_t start = systemTime(SYSTEM_TIME_MONOTONIC);
170 dequeueResult = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
171 nsecs_t end = systemTime(SYSTEM_TIME_MONOTONIC);
172 close(fd);
173 EXPECT_EQ(TIMED_OUT, dequeueResult);
174 EXPECT_GE(end - start, timeout);
175}