1 /*
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
30 using namespace android;
31
32 class TestableSurface final : public Surface {
33 public:
TestableSurface(const sp<IGraphicBufferProducer> & bufferProducer)34 explicit TestableSurface(const sp<IGraphicBufferProducer>& bufferProducer)
35 : Surface(bufferProducer) {}
36
37 // Exposes the internal last dequeue duration that's stored on the Surface.
getLastDequeueDuration() const38 nsecs_t getLastDequeueDuration() const { return mLastDequeueDuration; }
39
40 // Exposes the internal last queue duration that's stored on the Surface.
getLastQueueDuration() const41 nsecs_t getLastQueueDuration() const { return mLastQueueDuration; }
42
43 // Exposes the internal last dequeue start time that's stored on the Surface.
getLastDequeueStartTime() const44 nsecs_t getLastDequeueStartTime() const { return mLastDequeueStartTime; }
45 };
46
47 class ANativeWindowTest : public ::testing::Test {
48 protected:
SetUp()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());
53 #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
57 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
58 mItemConsumer = new BufferItemConsumer(mConsumer, GRALLOC_USAGE_SW_READ_OFTEN);
59 mWindow = new TestableSurface(mProducer);
60 #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
61 const int success = native_window_api_connect(mWindow.get(), NATIVE_WINDOW_API_CPU);
62 EXPECT_EQ(0, success);
63 }
64
TearDown()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 }
72
73 #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
74 sp<IGraphicBufferProducer> mProducer;
75 sp<IGraphicBufferConsumer> mConsumer;
76 #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
77 sp<BufferItemConsumer> mItemConsumer;
78 sp<TestableSurface> mWindow;
79 };
80
TEST_F(ANativeWindowTest,getLastDequeueDuration_noDequeue_returnsZero)81 TEST_F(ANativeWindowTest, getLastDequeueDuration_noDequeue_returnsZero) {
82 int result = ANativeWindow_getLastDequeueDuration(mWindow.get());
83 EXPECT_EQ(0, result);
84 EXPECT_EQ(0, mWindow->getLastDequeueDuration());
85 }
86
TEST_F(ANativeWindowTest,getLastDequeueDuration_withDequeue_returnsTime)87 TEST_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);
96 EXPECT_EQ(result, mWindow->getLastDequeueDuration());
97 }
98
TEST_F(ANativeWindowTest,getLastQueueDuration_noDequeue_returnsZero)99 TEST_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
TEST_F(ANativeWindowTest,getLastQueueDuration_noQueue_returnsZero)105 TEST_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
TEST_F(ANativeWindowTest,getLastQueueDuration_withQueue_returnsTime)117 TEST_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);
128 EXPECT_EQ(result, mWindow->getLastQueueDuration());
129 }
130
TEST_F(ANativeWindowTest,getLastDequeueStartTime_noDequeue_returnsZero)131 TEST_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
TEST_F(ANativeWindowTest,getLastDequeueStartTime_withDequeue_returnsTime)137 TEST_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 }
148
TEST_F(ANativeWindowTest,setDequeueTimeout_causesDequeueTimeout)149 TEST_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 }
176