1 /*
2 * Copyright (C) 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 "ZslBufferManagerTests"
18 #include <log/log.h>
19
20 #include <gtest/gtest.h>
21 #include <zsl_buffer_manager.h>
22
23 namespace android {
24 namespace google_camera_hal {
25
26 static const uint32_t kDataBytes = 256;
27 static const uint32_t kNumEntries = 10;
28 static const uint32_t kMaxBufferDepth = 16;
29
30 static constexpr HalBufferDescriptor kRawBufferDescriptor = {
31 .width = 4032,
32 .height = 3024,
33 .format = HAL_PIXEL_FORMAT_RAW10,
34 .immediate_num_buffers = kMaxBufferDepth,
35 .max_num_buffers = kMaxBufferDepth,
36 };
37
SetMetadata(std::unique_ptr<HalCameraMetadata> & hal_metadata)38 static void SetMetadata(std::unique_ptr<HalCameraMetadata>& hal_metadata) {
39 // Set current BOOT_TIME timestamp in nanoseconds
40 struct timespec ts;
41 if (clock_gettime(CLOCK_BOOTTIME, &ts) == 0) {
42 static const int64_t kNsPerSec = 1000000000;
43 int64_t buffer_timestamp = ts.tv_sec * kNsPerSec + ts.tv_nsec;
44 status_t res =
45 hal_metadata->Set(ANDROID_SENSOR_TIMESTAMP, &buffer_timestamp, 1);
46 ASSERT_EQ(res, OK) << "Set ANDROID_SENSOR_TIMESTAMP failed";
47 }
48 }
49
50 // Test ZslBufferManager AllocateBuffer.
TEST(ZslBufferManagerTests,AllocateBuffer)51 TEST(ZslBufferManagerTests, AllocateBuffer) {
52 auto manager = std::make_unique<ZslBufferManager>();
53 ASSERT_NE(manager, nullptr) << "Create ZslBufferManager failed.";
54
55 status_t res = manager->AllocateBuffers(kRawBufferDescriptor);
56 ASSERT_EQ(res, OK) << "AllocateBuffers failed: " << strerror(res);
57 }
58
59 // Test ZslBufferManager GetEmptyBuffer.
TEST(ZslBufferManagerTests,GetEmptyBuffer)60 TEST(ZslBufferManagerTests, GetEmptyBuffer) {
61 auto manager = std::make_unique<ZslBufferManager>();
62 ASSERT_NE(manager, nullptr) << "Create ZslBufferManager failed.";
63
64 status_t res = manager->AllocateBuffers(kRawBufferDescriptor);
65 ASSERT_EQ(res, OK) << "AllocateBuffers failed: " << strerror(res);
66 buffer_handle_t buffer;
67 for (uint32_t i = 0; i < kMaxBufferDepth; i++) {
68 buffer = manager->GetEmptyBuffer();
69 ASSERT_NE(buffer, kInvalidBufferHandle) << "GetEmptyBuffer failed: " << i;
70 }
71 // If get buffer number is greater than allocation, it will get nullptr.
72 buffer = manager->GetEmptyBuffer();
73 ASSERT_EQ(buffer, kInvalidBufferHandle) << "GetEmptyBuffer is not nullptr";
74 }
75
76 // Test ZslBufferManager fill buffers.
77 // For realtime pipeline case, get the buffer
78 // and then return buffer with metadata.
TEST(ZslBufferManagerTests,FillBuffers)79 TEST(ZslBufferManagerTests, FillBuffers) {
80 static const uint32_t kTestCycle = 50;
81 auto manager = std::make_unique<ZslBufferManager>();
82 ASSERT_NE(manager, nullptr) << "Creating ZslBufferManager failed.";
83
84 status_t res = manager->AllocateBuffers(kRawBufferDescriptor);
85 ASSERT_EQ(res, OK) << "AllocateBuffers failed: " << strerror(res);
86
87 // Fill the zsl buffers
88 for (uint32_t i = 0; i < kTestCycle; i++) {
89 // Get empty buffer and check whether buffer is nullptr or not
90 buffer_handle_t empty_buffer = manager->GetEmptyBuffer();
91 ASSERT_NE(empty_buffer, kInvalidBufferHandle)
92 << "GetEmptyBuffer failed at: " << i;
93
94 StreamBuffer stream_buffer;
95 stream_buffer.buffer = empty_buffer;
96 // Return the buffer with metadata.
97 res = manager->ReturnFilledBuffer(i, stream_buffer);
98 ASSERT_EQ(res, OK) << "ReturnFilledBuffer failed: " << strerror(res);
99 auto metadata = HalCameraMetadata::Create(kNumEntries, kDataBytes);
100 res = manager->ReturnMetadata(i, metadata.get(), /*partial_result=*/1);
101 ASSERT_EQ(res, OK) << "ReturnMetadata failed: " << strerror(res);
102 }
103 }
104
105 // Test ZslBufferManager GetMostRecentZslBuffers and ReturnZslBuffers
106 // For offline pipeline case, get most recent filled zsl buffers
107 // and then return zsl buffers.
TEST(ZslBufferManagerTests,GetRecentBuffers)108 TEST(ZslBufferManagerTests, GetRecentBuffers) {
109 static const uint32_t kTestCycle = 2;
110 static const uint32_t kGetTotalBufferNum = 10;
111 static const uint32_t kRequireMinBufferNum = 3;
112 auto manager = std::make_unique<ZslBufferManager>();
113 ASSERT_NE(manager, nullptr) << "Creating ZslBufferManager failed.";
114
115 status_t res = manager->AllocateBuffers(kRawBufferDescriptor);
116 ASSERT_EQ(res, OK) << "AllocateBuffers failed: " << strerror(res);
117
118 StreamBuffer stream_buffer[kMaxBufferDepth];
119
120 // Fill the zsl buffers
121 uint32_t frame_index = 0;
122 for (uint32_t j = 0; j < kTestCycle; j++) {
123 // Fill the zsl buffers
124 for (uint32_t i = 0; i < kMaxBufferDepth; i++) {
125 // Get empty buffer and check whether buffer is nullptr or not
126 buffer_handle_t empty_buffer = manager->GetEmptyBuffer();
127 ASSERT_NE(empty_buffer, kInvalidBufferHandle)
128 << "GetEmptyBuffer failed at: " << i;
129 stream_buffer[i].buffer = empty_buffer;
130 // Return the buffer with metadata.
131 res = manager->ReturnFilledBuffer(frame_index, stream_buffer[i]);
132 ASSERT_EQ(res, OK) << "ReturnFilledBuffer failed: " << strerror(res);
133
134 auto metadata = HalCameraMetadata::Create(kNumEntries, kDataBytes);
135 SetMetadata(metadata);
136 res = manager->ReturnMetadata(frame_index, metadata.get(),
137 /*partial_result=*/1);
138 ASSERT_EQ(res, OK) << "ReturnMetadata failed: " << strerror(res);
139 frame_index++;
140 }
141
142 // Get filled zsl buffers and return filled buffers
143 std::vector<ZslBufferManager::ZslBuffer> filled_buffers;
144 manager->GetMostRecentZslBuffers(&filled_buffers, kGetTotalBufferNum,
145 kRequireMinBufferNum);
146 ASSERT_EQ(filled_buffers.size(), (uint32_t)kGetTotalBufferNum)
147 << "GetMostRecentZslBuffers failed.";
148
149 // Check the zsl buffer frame number meet the most recent
150 // For the test case (kTestCycle = 0), fill frame_number (0 ~ 15)
151 // And GetMostRecentZslBuffers will get frame_number (6 ~ 15)
152 for (uint32_t k = 0; k < kGetTotalBufferNum; k++) {
153 ASSERT_EQ(filled_buffers[k].frame_number,
154 frame_index - kGetTotalBufferNum + k)
155 << "GetMostRecentZslBuffers data failed at " << k;
156 }
157 manager->ReturnZslBuffers(std::move(filled_buffers));
158 }
159 }
160
161 // Test ZslBufferManager ReturnMetadata.
162 // If allocated_metadata_ size is greater than kMaxAllcatedMetadataSize(100),
163 // ReturnMetadata() will return error and not allocate new metadata.
TEST(ZslBufferManagerTests,ReturnMetadata)164 TEST(ZslBufferManagerTests, ReturnMetadata) {
165 static const uint32_t kTestCycle = 100;
166 auto manager = std::make_unique<ZslBufferManager>();
167 ASSERT_NE(manager, nullptr) << "Creating ZslBufferManager failed.";
168 status_t res;
169 // Normal ReturnMetadata
170 // when zsl buffer manager allocated_metadata_ size < 100
171 for (uint32_t i = 0; i < kTestCycle; i++) {
172 auto metadata = HalCameraMetadata::Create(kNumEntries, kDataBytes);
173 res = manager->ReturnMetadata(i, metadata.get(), /*partial_result=*/1);
174 ASSERT_EQ(res, OK) << "ReturnMetadata failed: " << strerror(res)
175 << " at:" << i;
176 }
177
178 // Abnormal ReturnMetadata
179 // when zsl buffer manager allocated_metadata_ size >= 100
180 for (uint32_t i = kTestCycle; i < kTestCycle + 20; i++) {
181 auto metadata = HalCameraMetadata::Create(kNumEntries, kDataBytes);
182 res = manager->ReturnMetadata(i, metadata.get(), /*partial_result=*/1);
183 ASSERT_EQ(res, OK) << "ReturnMetadata failed: " << strerror(res)
184 << " at:" << i;
185 }
186 }
187
TEST(ZslBufferManagerTests,PendingBuffer)188 TEST(ZslBufferManagerTests, PendingBuffer) {
189 auto manager = std::make_unique<ZslBufferManager>();
190 ASSERT_NE(manager, nullptr) << "Creating ZslBufferManager failed.";
191 bool empty = manager->IsPendingBufferEmpty();
192 ASSERT_EQ(empty, true) << "Pending buffer is not empty.";
193
194 std::vector<ZslBufferManager::ZslBuffer> filled_buffers;
195 ZslBufferManager::ZslBuffer zsl_buffer;
196 filled_buffers.push_back(std::move(zsl_buffer));
197 manager->AddPendingBuffers(filled_buffers);
198
199 // Pending buffer is not empty after call AddPendingBuffers.
200 empty = manager->IsPendingBufferEmpty();
201 ASSERT_EQ(empty, false) << "Pending buffer is empty after AddPendingBuffers.";
202
203 status_t res = manager->CleanPendingBuffers(&filled_buffers);
204 ASSERT_EQ(res, OK) << "CleanPendingBuffers failed.";
205
206 empty = manager->IsPendingBufferEmpty();
207 ASSERT_EQ(empty, true)
208 << "Pending buffer is not empty after CleanPendingBuffers.";
209 }
210
211 } // namespace google_camera_hal
212 } // namespace android
213