1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include <gtest/gtest.h>
15
16 #include "AndroidWindow.h"
17 #include "Vsync.h"
18
19 #include "android/base/memory/ScopedPtr.h"
20 #include "android/base/system/System.h"
21
22 #include <atomic>
23 #include <memory>
24 #include <vector>
25
26 using android::base::System;
27
28 namespace aemu {
29
TEST(AndroidWindow,Basic)30 TEST(AndroidWindow, Basic) {
31 const int kWidth = 1920;
32 const int kHeight = 1080;
33
34 auto window = android::base::makeCustomScopedPtr(
35 create_host_anativewindow(kWidth, kHeight),
36 destroy_host_anativewindow);
37
38 int width = 0;
39 int height = 0;
40
41 ANativeWindow_query(window.get(), ANATIVEWINDOW_QUERY_DEFAULT_WIDTH,
42 &width);
43 ANativeWindow_query(window.get(), ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT,
44 &height);
45 EXPECT_EQ(kWidth, width);
46 EXPECT_EQ(kHeight, height);
47
48 int minSwapInterval = 0;
49 int maxSwapInterval = 0;
50 ANativeWindow_query(window.get(), ANATIVEWINDOW_QUERY_MIN_SWAP_INTERVAL,
51 &minSwapInterval);
52 ANativeWindow_query(window.get(), ANATIVEWINDOW_QUERY_MAX_SWAP_INTERVAL,
53 &maxSwapInterval);
54 EXPECT_EQ(0, minSwapInterval);
55 EXPECT_EQ(1, maxSwapInterval);
56 }
57
TEST(AndroidWindow,BufferQueue)58 TEST(AndroidWindow, BufferQueue) {
59 auto window = android::base::makeCustomScopedPtr(
60 create_host_anativewindow(256, 256), destroy_host_anativewindow);
61
62 AndroidWindow* aWindow = AndroidWindow::getSelf(window.get());
63
64 AndroidBufferQueue toWindow;
65 AndroidBufferQueue fromWindow;
66
67 aWindow->setProducer(&toWindow, &fromWindow);
68
69 std::vector<AndroidBufferQueue::Item> itemsForQueueing = {
70 {nullptr, 0},
71 {nullptr, 1},
72 {nullptr, 2},
73 };
74
75 const size_t bufferCount = itemsForQueueing.size();
76
77 for (const auto& item : itemsForQueueing) {
78 toWindow.queueBuffer(item);
79 }
80
81 for (size_t i = 0; i < bufferCount; i++) {
82 AndroidBufferQueue::Item item;
83 ANativeWindow_dequeueBuffer(window.get(), &item.buffer, &item.fenceFd);
84 EXPECT_EQ(itemsForQueueing[i].buffer, item.buffer);
85 EXPECT_EQ(itemsForQueueing[i].fenceFd, item.fenceFd);
86 ANativeWindow_queueBuffer(window.get(), item.buffer, item.fenceFd);
87 }
88
89 for (size_t i = 0; i < bufferCount; i++) {
90 AndroidBufferQueue::Item item;
91 fromWindow.dequeueBuffer(&item);
92 EXPECT_EQ(itemsForQueueing[i].buffer, item.buffer);
93 EXPECT_EQ(itemsForQueueing[i].fenceFd, item.fenceFd);
94 }
95 }
96
97 // TestSystem will totally mess this up. Disable for now
TEST(Vsync,DISABLED_Basic)98 TEST(Vsync, DISABLED_Basic) {
99 std::atomic<int> count { 0 };
100 Vsync vsync(1000, [&count]() { ++count; });
101
102 constexpr System::Duration kSleepIntervalMs = 20;
103 constexpr System::Duration kSleepLimitMs = 15000;
104
105 System::Duration totalSleepMs = 0;
106 while (count.load(std::memory_order_relaxed) == 0) {
107 totalSleepMs += kSleepIntervalMs;
108 System::get()->sleepMs(kSleepIntervalMs);
109 ASSERT_LE(totalSleepMs, kSleepLimitMs);
110 }
111
112 vsync.join();
113 }
114
115 } // namespace aemu
116