• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         sp<Surface> surface;
54         std::tie(mItemConsumer, surface) = BufferItemConsumer::create(GRALLOC_USAGE_SW_READ_OFTEN);
55         mWindow = new TestableSurface(surface->getIGraphicBufferProducer());
56         const int success = native_window_api_connect(mWindow.get(), NATIVE_WINDOW_API_CPU);
57         EXPECT_EQ(0, success);
58     }
59 
TearDown()60     void TearDown() override {
61         const ::testing::TestInfo* const test_info =
62                 ::testing::UnitTest::GetInstance()->current_test_info();
63         ALOGV("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
64         const int success = native_window_api_disconnect(mWindow.get(), NATIVE_WINDOW_API_CPU);
65         EXPECT_EQ(0, success);
66     }
67 
68 #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
69     sp<IGraphicBufferProducer> mProducer;
70     sp<IGraphicBufferConsumer> mConsumer;
71 #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
72     sp<BufferItemConsumer> mItemConsumer;
73     sp<TestableSurface> mWindow;
74 };
75 
TEST_F(ANativeWindowTest,getLastDequeueDuration_noDequeue_returnsZero)76 TEST_F(ANativeWindowTest, getLastDequeueDuration_noDequeue_returnsZero) {
77     int result = ANativeWindow_getLastDequeueDuration(mWindow.get());
78     EXPECT_EQ(0, result);
79     EXPECT_EQ(0, mWindow->getLastDequeueDuration());
80 }
81 
TEST_F(ANativeWindowTest,getLastDequeueDuration_withDequeue_returnsTime)82 TEST_F(ANativeWindowTest, getLastDequeueDuration_withDequeue_returnsTime) {
83     ANativeWindowBuffer* buffer;
84     int fd;
85     int result = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
86     close(fd);
87     EXPECT_EQ(0, result);
88 
89     result = ANativeWindow_getLastDequeueDuration(mWindow.get());
90     EXPECT_GT(result, 0);
91     EXPECT_EQ(result, mWindow->getLastDequeueDuration());
92 }
93 
TEST_F(ANativeWindowTest,getLastQueueDuration_noDequeue_returnsZero)94 TEST_F(ANativeWindowTest, getLastQueueDuration_noDequeue_returnsZero) {
95     int result = ANativeWindow_getLastQueueDuration(mWindow.get());
96     EXPECT_EQ(0, result);
97     EXPECT_EQ(0, mWindow->getLastQueueDuration());
98 }
99 
TEST_F(ANativeWindowTest,getLastQueueDuration_noQueue_returnsZero)100 TEST_F(ANativeWindowTest, getLastQueueDuration_noQueue_returnsZero) {
101     ANativeWindowBuffer* buffer;
102     int fd;
103     int result = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
104     close(fd);
105     EXPECT_EQ(0, result);
106 
107     result = ANativeWindow_getLastQueueDuration(mWindow.get());
108     EXPECT_EQ(result, 0);
109     EXPECT_EQ(result, mWindow->getLastQueueDuration());
110 }
111 
TEST_F(ANativeWindowTest,getLastQueueDuration_withQueue_returnsTime)112 TEST_F(ANativeWindowTest, getLastQueueDuration_withQueue_returnsTime) {
113     ANativeWindowBuffer* buffer;
114     int fd;
115     int result = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
116     close(fd);
117     EXPECT_EQ(0, result);
118 
119     result = ANativeWindow_queueBuffer(mWindow.get(), buffer, 0);
120 
121     result = ANativeWindow_getLastQueueDuration(mWindow.get());
122     EXPECT_GT(result, 0);
123     EXPECT_EQ(result, mWindow->getLastQueueDuration());
124 }
125 
TEST_F(ANativeWindowTest,getLastDequeueStartTime_noDequeue_returnsZero)126 TEST_F(ANativeWindowTest, getLastDequeueStartTime_noDequeue_returnsZero) {
127     int64_t result = ANativeWindow_getLastDequeueStartTime(mWindow.get());
128     EXPECT_EQ(0, result);
129     EXPECT_EQ(0, mWindow->getLastQueueDuration());
130 }
131 
TEST_F(ANativeWindowTest,getLastDequeueStartTime_withDequeue_returnsTime)132 TEST_F(ANativeWindowTest, getLastDequeueStartTime_withDequeue_returnsTime) {
133     ANativeWindowBuffer* buffer;
134     int fd;
135     int dequeueResult = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
136     close(fd);
137     EXPECT_EQ(0, dequeueResult);
138 
139     int64_t result = ANativeWindow_getLastDequeueStartTime(mWindow.get());
140     EXPECT_GT(result, 0);
141     EXPECT_EQ(result, mWindow->getLastDequeueStartTime());
142 }
143 
TEST_F(ANativeWindowTest,setDequeueTimeout_causesDequeueTimeout)144 TEST_F(ANativeWindowTest, setDequeueTimeout_causesDequeueTimeout) {
145     nsecs_t timeout = milliseconds_to_nanoseconds(100);
146     int result = ANativeWindow_setDequeueTimeout(mWindow.get(), timeout);
147     EXPECT_EQ(0, result);
148 
149     // The two dequeues should not timeout...
150     ANativeWindowBuffer* buffer;
151     int fd;
152     int dequeueResult = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
153     close(fd);
154     EXPECT_EQ(0, dequeueResult);
155     int queueResult = ANativeWindow_queueBuffer(mWindow.get(), buffer, -1);
156     EXPECT_EQ(0, queueResult);
157     dequeueResult = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
158     close(fd);
159     EXPECT_EQ(0, dequeueResult);
160     queueResult = ANativeWindow_queueBuffer(mWindow.get(), buffer, -1);
161     EXPECT_EQ(0, queueResult);
162 
163     // ...but the third one should since the queue depth is too deep.
164     nsecs_t start = systemTime(SYSTEM_TIME_MONOTONIC);
165     dequeueResult = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
166     nsecs_t end = systemTime(SYSTEM_TIME_MONOTONIC);
167     close(fd);
168     EXPECT_EQ(TIMED_OUT, dequeueResult);
169     EXPECT_GE(end - start, timeout);
170 }
171