blob: 72b7b610bd0fe389956f9e838a5366878f7eed72 [file] [log] [blame]
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -07001/*
2 * Copyright (C) 2012 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
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070017#define LOG_TAG "Camera2_test"
Igor Murashkineab33fc2012-11-06 17:02:54 -080018//#define LOG_NDEBUG 0
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070019
20#include <utils/Log.h>
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070021#include <gtest/gtest.h>
22#include <iostream>
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070023#include <fstream>
24
25#include <utils/Vector.h>
Yin-Chia Yeh8df990b2014-10-31 15:10:11 -070026#include <utils/KeyedVector.h>
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070027#include <gui/CpuConsumer.h>
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -070028#include <ui/PixelFormat.h>
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070029#include <system/camera_metadata.h>
30
31#include "camera2_utils.h"
Igor Murashkineab33fc2012-11-06 17:02:54 -080032#include "TestExtensions.h"
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070033
34namespace android {
Igor Murashkine302ee32012-11-05 11:14:49 -080035namespace camera2 {
36namespace tests {
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070037
38class Camera2Test: public testing::Test {
39 public:
Igor Murashkineab33fc2012-11-06 17:02:54 -080040 void SetUpModule() {
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070041 int res;
42
43 hw_module_t *module = NULL;
44 res = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
45 (const hw_module_t **)&module);
46
47 ASSERT_EQ(0, res)
48 << "Failure opening camera hardware module: " << res;
49 ASSERT_TRUE(NULL != module)
50 << "No camera module was set by hw_get_module";
51
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070052 IF_ALOGV() {
53 std::cout << " Camera module name: "
54 << module->name << std::endl;
55 std::cout << " Camera module author: "
56 << module->author << std::endl;
57 std::cout << " Camera module API version: 0x" << std::hex
58 << module->module_api_version << std::endl;
59 std::cout << " Camera module HAL API version: 0x" << std::hex
60 << module->hal_api_version << std::endl;
61 }
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070062
63 int16_t version2_0 = CAMERA_MODULE_API_VERSION_2_0;
Igor Murashkinfb40d5d2013-03-26 18:07:31 -070064 ASSERT_LE(version2_0, module->module_api_version)
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070065 << "Camera module version is 0x"
66 << std::hex << module->module_api_version
Igor Murashkinfb40d5d2013-03-26 18:07:31 -070067 << ", should be at least 2.0. (0x"
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070068 << std::hex << CAMERA_MODULE_API_VERSION_2_0 << ")";
69
70 sCameraModule = reinterpret_cast<camera_module_t*>(module);
71
72 sNumCameras = sCameraModule->get_number_of_cameras();
73 ASSERT_LT(0, sNumCameras) << "No camera devices available!";
74
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070075 IF_ALOGV() {
76 std::cout << " Camera device count: " << sNumCameras << std::endl;
77 }
78
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070079 sCameraSupportsHal2 = new bool[sNumCameras];
80
81 for (int i = 0; i < sNumCameras; i++) {
82 camera_info info;
83 res = sCameraModule->get_camera_info(i, &info);
84 ASSERT_EQ(0, res)
85 << "Failure getting camera info for camera " << i;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070086 IF_ALOGV() {
87 std::cout << " Camera device: " << std::dec
88 << i << std::endl;;
89 std::cout << " Facing: " << std::dec
90 << info.facing << std::endl;
91 std::cout << " Orientation: " << std::dec
92 << info.orientation << std::endl;
93 std::cout << " Version: 0x" << std::hex <<
94 info.device_version << std::endl;
95 }
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070096 if (info.device_version >= CAMERA_DEVICE_API_VERSION_2_0 &&
97 info.device_version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070098 sCameraSupportsHal2[i] = true;
99 ASSERT_TRUE(NULL != info.static_camera_characteristics);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700100 IF_ALOGV() {
101 std::cout << " Static camera metadata:" << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700102 dump_indented_camera_metadata(info.static_camera_characteristics,
103 0, 1, 6);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700104 }
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700105 } else {
106 sCameraSupportsHal2[i] = false;
107 }
108 }
109 }
110
Igor Murashkineab33fc2012-11-06 17:02:54 -0800111 void TearDownModule() {
Igor Murashkine302ee32012-11-05 11:14:49 -0800112 hw_module_t *module = reinterpret_cast<hw_module_t*>(sCameraModule);
113 ASSERT_EQ(0, HWModuleHelpers::closeModule(module));
114 }
115
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700116 static const camera_module_t *getCameraModule() {
117 return sCameraModule;
118 }
119
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700120 static int getNumCameras() {
121 return sNumCameras;
122 }
123
124 static bool isHal2Supported(int id) {
125 return sCameraSupportsHal2[id];
126 }
127
128 static camera2_device_t *openCameraDevice(int id) {
129 ALOGV("Opening camera %d", id);
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700130 if (NULL == sCameraSupportsHal2) return NULL;
131 if (id >= sNumCameras) return NULL;
132 if (!sCameraSupportsHal2[id]) return NULL;
133
134 hw_device_t *device = NULL;
135 const camera_module_t *cam_module = getCameraModule();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700136 if (cam_module == NULL) {
137 return NULL;
138 }
139
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700140 char camId[10];
141 int res;
142
143 snprintf(camId, 10, "%d", id);
144 res = cam_module->common.methods->open(
145 (const hw_module_t*)cam_module,
146 camId,
147 &device);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700148 if (res != NO_ERROR || device == NULL) {
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700149 return NULL;
150 }
151 camera2_device_t *cam_device =
152 reinterpret_cast<camera2_device_t*>(device);
153 return cam_device;
154 }
155
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700156 static status_t configureCameraDevice(camera2_device_t *dev,
157 MetadataQueue &requestQueue,
158 MetadataQueue &frameQueue,
159 NotifierListener &listener) {
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700160
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700161 status_t err;
162
163 err = dev->ops->set_request_queue_src_ops(dev,
164 requestQueue.getToConsumerInterface());
165 if (err != OK) return err;
166
167 requestQueue.setFromConsumerInterface(dev);
168
169 err = dev->ops->set_frame_queue_dst_ops(dev,
170 frameQueue.getToProducerInterface());
171 if (err != OK) return err;
172
173 err = listener.getNotificationsFrom(dev);
174 if (err != OK) return err;
175
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700176 return OK;
177 }
178
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700179 static status_t closeCameraDevice(camera2_device_t **cam_dev) {
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700180 int res;
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700181 if (*cam_dev == NULL ) return OK;
182
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700183 ALOGV("Closing camera %p", cam_dev);
184
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700185 hw_device_t *dev = reinterpret_cast<hw_device_t *>(*cam_dev);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700186 res = dev->close(dev);
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700187 *cam_dev = NULL;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700188 return res;
189 }
190
191 void setUpCamera(int id) {
192 ASSERT_GT(sNumCameras, id);
193 status_t res;
194
195 if (mDevice != NULL) {
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700196 closeCameraDevice(&mDevice);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700197 }
Zhijun He8ef01442013-08-13 17:36:17 -0700198 mId = id;
199 mDevice = openCameraDevice(mId);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700200 ASSERT_TRUE(NULL != mDevice) << "Failed to open camera device";
201
202 camera_info info;
203 res = sCameraModule->get_camera_info(id, &info);
204 ASSERT_EQ(OK, res);
205
Yin-Chia Yeh8df990b2014-10-31 15:10:11 -0700206 mDeviceVersion = info.device_version;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700207 mStaticInfo = info.static_camera_characteristics;
Yin-Chia Yeh8df990b2014-10-31 15:10:11 -0700208 buildOutputResolutions();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700209
210 res = configureCameraDevice(mDevice,
211 mRequests,
212 mFrames,
213 mNotifications);
214 ASSERT_EQ(OK, res) << "Failure to configure camera device";
215
216 }
217
Andy McFaddeneda79df2012-12-18 09:50:24 -0800218 void setUpStream(sp<IGraphicBufferProducer> consumer,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700219 int width, int height, int format, int *id) {
220 status_t res;
221
222 StreamAdapter* stream = new StreamAdapter(consumer);
223
224 ALOGV("Creating stream, format 0x%x, %d x %d", format, width, height);
225 res = stream->connectToDevice(mDevice, width, height, format);
226 ASSERT_EQ(NO_ERROR, res) << "Failed to connect to stream: "
227 << strerror(-res);
228 mStreams.push_back(stream);
229
230 *id = stream->getId();
231 }
232
233 void disconnectStream(int id) {
234 status_t res;
235 unsigned int i=0;
236 for (; i < mStreams.size(); i++) {
237 if (mStreams[i]->getId() == id) {
238 res = mStreams[i]->disconnect();
239 ASSERT_EQ(NO_ERROR, res) <<
240 "Failed to disconnect stream " << id;
241 break;
242 }
243 }
244 ASSERT_GT(mStreams.size(), i) << "Stream id not found:" << id;
245 }
246
Yin-Chia Yeh8df990b2014-10-31 15:10:11 -0700247 void buildOutputResolutions() {
248 status_t res;
249 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
250 return;
251 }
252 if (mOutputResolutions.isEmpty()) {
253 camera_metadata_ro_entry_t availableStrmConfigs;
254 res = find_camera_metadata_ro_entry(mStaticInfo,
255 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
256 &availableStrmConfigs);
257 ASSERT_EQ(OK, res);
258 ASSERT_EQ(0u, availableStrmConfigs.count % 4);
259 for (uint32_t i = 0; i < availableStrmConfigs.count; i += 4) {
260 int32_t format = availableStrmConfigs.data.i32[i];
261 int32_t width = availableStrmConfigs.data.i32[i + 1];
262 int32_t height = availableStrmConfigs.data.i32[i + 2];
263 int32_t inOrOut = availableStrmConfigs.data.i32[i + 3];
264 if (inOrOut == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT) {
265 int index = mOutputResolutions.indexOfKey(format);
266 if (index < 0) {
267 index = mOutputResolutions.add(format, new Vector<int32_t>());
268 ASSERT_TRUE(index >= 0);
269 }
270 Vector<int32_t> *resolutions = mOutputResolutions.editValueAt(index);
271 resolutions->add(width);
272 resolutions->add(height);
273 }
274 }
275 }
276 }
277
278 void deleteOutputResolutions() {
279 for (uint32_t i = 0; i < mOutputResolutions.size(); i++) {
280 Vector<int32_t>* resolutions = mOutputResolutions.editValueAt(i);
281 delete resolutions;
282 }
283 mOutputResolutions.clear();
284 }
285
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700286 void getResolutionList(int32_t format,
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700287 const int32_t **list,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700288 size_t *count) {
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700289 status_t res;
Yin-Chia Yeh8df990b2014-10-31 15:10:11 -0700290 ALOGV("Getting resolutions for format %x", format);
291 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
292 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
293 camera_metadata_ro_entry_t availableFormats;
294 res = find_camera_metadata_ro_entry(mStaticInfo,
295 ANDROID_SCALER_AVAILABLE_FORMATS,
296 &availableFormats);
297 ASSERT_EQ(OK, res);
298
299 uint32_t formatIdx;
300 for (formatIdx=0; formatIdx < availableFormats.count; formatIdx++) {
301 if (availableFormats.data.i32[formatIdx] == format) break;
302 }
303 ASSERT_NE(availableFormats.count, formatIdx)
304 << "No support found for format 0x" << std::hex << format;
305 }
306
307 camera_metadata_ro_entry_t availableSizes;
308 if (format == HAL_PIXEL_FORMAT_RAW_SENSOR) {
309 res = find_camera_metadata_ro_entry(mStaticInfo,
310 ANDROID_SCALER_AVAILABLE_RAW_SIZES,
311 &availableSizes);
312 } else if (format == HAL_PIXEL_FORMAT_BLOB) {
313 res = find_camera_metadata_ro_entry(mStaticInfo,
314 ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
315 &availableSizes);
316 } else {
317 res = find_camera_metadata_ro_entry(mStaticInfo,
318 ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
319 &availableSizes);
320 }
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700321 ASSERT_EQ(OK, res);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700322
Yin-Chia Yeh8df990b2014-10-31 15:10:11 -0700323 *list = availableSizes.data.i32;
324 *count = availableSizes.count;
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700325 } else {
Yin-Chia Yeh8df990b2014-10-31 15:10:11 -0700326 int index = mOutputResolutions.indexOfKey(format);
327 ASSERT_TRUE(index >= 0);
328 Vector<int32_t>* resolutions = mOutputResolutions.valueAt(index);
329 *list = resolutions->array();
330 *count = resolutions->size();
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700331 }
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700332 }
333
Igor Murashkin599b76f2012-12-18 16:06:07 -0800334 status_t waitUntilDrained() {
335 static const uint32_t kSleepTime = 50000; // 50 ms
336 static const uint32_t kMaxSleepTime = 10000000; // 10 s
337 ALOGV("%s: Camera %d: Starting wait", __FUNCTION__, mId);
338
339 // TODO: Set up notifications from HAL, instead of sleeping here
340 uint32_t totalTime = 0;
341 while (mDevice->ops->get_in_progress_count(mDevice) > 0) {
342 usleep(kSleepTime);
343 totalTime += kSleepTime;
344 if (totalTime > kMaxSleepTime) {
345 ALOGE("%s: Waited %d us, %d requests still in flight", __FUNCTION__,
346 mDevice->ops->get_in_progress_count(mDevice), totalTime);
347 return TIMED_OUT;
348 }
349 }
350 ALOGV("%s: Camera %d: HAL is idle", __FUNCTION__, mId);
351 return OK;
352 }
353
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700354 virtual void SetUp() {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800355 TEST_EXTENSION_FORKING_SET_UP;
356
357 SetUpModule();
358
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700359 const ::testing::TestInfo* const testInfo =
360 ::testing::UnitTest::GetInstance()->current_test_info();
Igor Murashkineab33fc2012-11-06 17:02:54 -0800361 (void)testInfo;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700362
Igor Murashkineab33fc2012-11-06 17:02:54 -0800363 ALOGV("*** Starting test %s in test case %s", testInfo->name(),
364 testInfo->test_case_name());
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700365 mDevice = NULL;
366 }
367
368 virtual void TearDown() {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800369 TEST_EXTENSION_FORKING_TEAR_DOWN;
370
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700371 for (unsigned int i = 0; i < mStreams.size(); i++) {
372 delete mStreams[i];
373 }
374 if (mDevice != NULL) {
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700375 closeCameraDevice(&mDevice);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700376 }
Igor Murashkineab33fc2012-11-06 17:02:54 -0800377
Yin-Chia Yeh8df990b2014-10-31 15:10:11 -0700378 deleteOutputResolutions();
Igor Murashkineab33fc2012-11-06 17:02:54 -0800379 TearDownModule();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700380 }
381
Zhijun He8ef01442013-08-13 17:36:17 -0700382 int mId;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700383 camera2_device *mDevice;
Yin-Chia Yeh8df990b2014-10-31 15:10:11 -0700384 uint32_t mDeviceVersion;
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700385 const camera_metadata_t *mStaticInfo;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700386
387 MetadataQueue mRequests;
388 MetadataQueue mFrames;
389 NotifierListener mNotifications;
390
391 Vector<StreamAdapter*> mStreams;
Yin-Chia Yeh8df990b2014-10-31 15:10:11 -0700392 KeyedVector<int32_t, Vector<int32_t>* > mOutputResolutions;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700393
394 private:
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700395 static camera_module_t *sCameraModule;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700396 static int sNumCameras;
397 static bool *sCameraSupportsHal2;
Yin-Chia Yeh8df990b2014-10-31 15:10:11 -0700398
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700399};
400
401camera_module_t *Camera2Test::sCameraModule = NULL;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700402bool *Camera2Test::sCameraSupportsHal2 = NULL;
403int Camera2Test::sNumCameras = 0;
404
405static const nsecs_t USEC = 1000;
406static const nsecs_t MSEC = 1000*USEC;
407static const nsecs_t SEC = 1000*MSEC;
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700408
409
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700410TEST_F(Camera2Test, OpenClose) {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800411
412 TEST_EXTENSION_FORKING_INIT;
413
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700414 status_t res;
415
416 for (int id = 0; id < getNumCameras(); id++) {
417 if (!isHal2Supported(id)) continue;
418
419 camera2_device_t *d = openCameraDevice(id);
420 ASSERT_TRUE(NULL != d) << "Failed to open camera device";
421
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700422 res = closeCameraDevice(&d);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700423 ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
424 }
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700425}
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700426
427TEST_F(Camera2Test, Capture1Raw) {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800428
429 TEST_EXTENSION_FORKING_INIT;
430
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700431 status_t res;
432
433 for (int id = 0; id < getNumCameras(); id++) {
434 if (!isHal2Supported(id)) continue;
435
436 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
437
Dan Stoza5dce9e42014-04-07 13:39:37 -0700438 sp<IGraphicBufferProducer> bqProducer;
439 sp<IGraphicBufferConsumer> bqConsumer;
440 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer);
441 sp<CpuConsumer> rawConsumer = new CpuConsumer(bqConsumer, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700442 sp<FrameWaiter> rawWaiter = new FrameWaiter();
443 rawConsumer->setFrameAvailableListener(rawWaiter);
444
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700445 const int32_t *rawResolutions;
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700446 size_t rawResolutionsCount;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700447
448 int format = HAL_PIXEL_FORMAT_RAW_SENSOR;
449
450 getResolutionList(format,
451 &rawResolutions, &rawResolutionsCount);
Igor Murashkin3d991c82013-04-18 10:09:16 -0700452
453 if (rawResolutionsCount <= 0) {
454 const ::testing::TestInfo* const test_info =
455 ::testing::UnitTest::GetInstance()->current_test_info();
456 std::cerr << "Skipping test "
457 << test_info->test_case_name() << "."
458 << test_info->name()
459 << " because the optional format was not available: "
460 << "RAW_SENSOR" << std::endl;
461 return;
462 }
463
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700464 ASSERT_LT((size_t)0, rawResolutionsCount);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700465
466 // Pick first available raw resolution
467 int width = rawResolutions[0];
468 int height = rawResolutions[1];
469
470 int streamId;
471 ASSERT_NO_FATAL_FAILURE(
Dan Stoza5dce9e42014-04-07 13:39:37 -0700472 setUpStream(bqProducer, width, height, format, &streamId) );
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700473
474 camera_metadata_t *request;
475 request = allocate_camera_metadata(20, 2000);
476
Igor Murashkin09ad0a32012-12-03 13:33:08 -0800477 uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700478 add_camera_metadata_entry(request,
479 ANDROID_REQUEST_METADATA_MODE,
480 (void**)&metadataMode, 1);
481 uint32_t outputStreams = streamId;
482 add_camera_metadata_entry(request,
483 ANDROID_REQUEST_OUTPUT_STREAMS,
484 (void**)&outputStreams, 1);
485
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700486 uint64_t exposureTime = 10*MSEC;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700487 add_camera_metadata_entry(request,
488 ANDROID_SENSOR_EXPOSURE_TIME,
489 (void**)&exposureTime, 1);
490 uint64_t frameDuration = 30*MSEC;
491 add_camera_metadata_entry(request,
492 ANDROID_SENSOR_FRAME_DURATION,
493 (void**)&frameDuration, 1);
494 uint32_t sensitivity = 100;
495 add_camera_metadata_entry(request,
496 ANDROID_SENSOR_SENSITIVITY,
497 (void**)&sensitivity, 1);
Igor Murashkin599b76f2012-12-18 16:06:07 -0800498 uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
499 add_camera_metadata_entry(request,
500 ANDROID_REQUEST_TYPE,
501 (void**)&requestType, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700502
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700503 uint32_t hourOfDay = 12;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700504 add_camera_metadata_entry(request,
505 0x80000000, // EMULATOR_HOUROFDAY
506 &hourOfDay, 1);
507
508 IF_ALOGV() {
509 std::cout << "Input request: " << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700510 dump_indented_camera_metadata(request, 0, 1, 2);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700511 }
512
513 res = mRequests.enqueue(request);
514 ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " << strerror(-res);
515
516 res = mFrames.waitForBuffer(exposureTime + SEC);
517 ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
518
519 camera_metadata_t *frame;
520 res = mFrames.dequeue(&frame);
521 ASSERT_EQ(NO_ERROR, res);
522 ASSERT_TRUE(frame != NULL);
523
524 IF_ALOGV() {
525 std::cout << "Output frame:" << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700526 dump_indented_camera_metadata(frame, 0, 1, 2);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700527 }
528
529 res = rawWaiter->waitForFrame(exposureTime + SEC);
530 ASSERT_EQ(NO_ERROR, res);
531
532 CpuConsumer::LockedBuffer buffer;
533 res = rawConsumer->lockNextBuffer(&buffer);
534 ASSERT_EQ(NO_ERROR, res);
535
536 IF_ALOGV() {
537 const char *dumpname =
538 "/data/local/tmp/camera2_test-capture1raw-dump.raw";
539 ALOGV("Dumping raw buffer to %s", dumpname);
540 // Write to file
541 std::ofstream rawFile(dumpname);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700542 size_t bpp = 2;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700543 for (unsigned int y = 0; y < buffer.height; y++) {
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700544 rawFile.write(
545 (const char *)(buffer.data + y * buffer.stride * bpp),
546 buffer.width * bpp);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700547 }
548 rawFile.close();
549 }
550
551 res = rawConsumer->unlockBuffer(buffer);
552 ASSERT_EQ(NO_ERROR, res);
553
Igor Murashkin599b76f2012-12-18 16:06:07 -0800554 ASSERT_EQ(OK, waitUntilDrained());
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700555 ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId));
556
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700557 res = closeCameraDevice(&mDevice);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700558 ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
559
560 }
561}
562
563TEST_F(Camera2Test, CaptureBurstRaw) {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800564
565 TEST_EXTENSION_FORKING_INIT;
566
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700567 status_t res;
568
569 for (int id = 0; id < getNumCameras(); id++) {
570 if (!isHal2Supported(id)) continue;
571
572 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
573
Dan Stoza5dce9e42014-04-07 13:39:37 -0700574 sp<IGraphicBufferProducer> bqProducer;
575 sp<IGraphicBufferConsumer> bqConsumer;
576 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer);
577 sp<CpuConsumer> rawConsumer = new CpuConsumer(bqConsumer, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700578 sp<FrameWaiter> rawWaiter = new FrameWaiter();
579 rawConsumer->setFrameAvailableListener(rawWaiter);
580
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700581 const int32_t *rawResolutions;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700582 size_t rawResolutionsCount;
583
584 int format = HAL_PIXEL_FORMAT_RAW_SENSOR;
585
586 getResolutionList(format,
587 &rawResolutions, &rawResolutionsCount);
Igor Murashkin3d991c82013-04-18 10:09:16 -0700588
589 if (rawResolutionsCount <= 0) {
590 const ::testing::TestInfo* const test_info =
591 ::testing::UnitTest::GetInstance()->current_test_info();
592 std::cerr << "Skipping test "
593 << test_info->test_case_name() << "."
594 << test_info->name()
595 << " because the optional format was not available: "
596 << "RAW_SENSOR" << std::endl;
597 return;
598 }
599
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700600 ASSERT_LT((uint32_t)0, rawResolutionsCount);
601
602 // Pick first available raw resolution
603 int width = rawResolutions[0];
604 int height = rawResolutions[1];
605
606 int streamId;
607 ASSERT_NO_FATAL_FAILURE(
Dan Stoza5dce9e42014-04-07 13:39:37 -0700608 setUpStream(bqProducer, width, height, format, &streamId) );
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700609
610 camera_metadata_t *request;
611 request = allocate_camera_metadata(20, 2000);
612
Igor Murashkin09ad0a32012-12-03 13:33:08 -0800613 uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700614 add_camera_metadata_entry(request,
615 ANDROID_REQUEST_METADATA_MODE,
616 (void**)&metadataMode, 1);
617 uint32_t outputStreams = streamId;
618 add_camera_metadata_entry(request,
619 ANDROID_REQUEST_OUTPUT_STREAMS,
620 (void**)&outputStreams, 1);
621
622 uint64_t frameDuration = 30*MSEC;
623 add_camera_metadata_entry(request,
624 ANDROID_SENSOR_FRAME_DURATION,
625 (void**)&frameDuration, 1);
626 uint32_t sensitivity = 100;
627 add_camera_metadata_entry(request,
628 ANDROID_SENSOR_SENSITIVITY,
629 (void**)&sensitivity, 1);
Igor Murashkin599b76f2012-12-18 16:06:07 -0800630 uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
631 add_camera_metadata_entry(request,
632 ANDROID_REQUEST_TYPE,
633 (void**)&requestType, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700634
635 uint32_t hourOfDay = 12;
636 add_camera_metadata_entry(request,
637 0x80000000, // EMULATOR_HOUROFDAY
638 &hourOfDay, 1);
639
640 IF_ALOGV() {
641 std::cout << "Input request template: " << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700642 dump_indented_camera_metadata(request, 0, 1, 2);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700643 }
644
645 int numCaptures = 10;
646
647 // Enqueue numCaptures requests with increasing exposure time
648
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700649 uint64_t exposureTime = 100 * USEC;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700650 for (int reqCount = 0; reqCount < numCaptures; reqCount++ ) {
651 camera_metadata_t *req;
652 req = allocate_camera_metadata(20, 2000);
653 append_camera_metadata(req, request);
654
655 add_camera_metadata_entry(req,
656 ANDROID_SENSOR_EXPOSURE_TIME,
657 (void**)&exposureTime, 1);
658 exposureTime *= 2;
659
660 res = mRequests.enqueue(req);
661 ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: "
662 << strerror(-res);
663 }
664
665 // Get frames and image buffers one by one
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700666 uint64_t expectedExposureTime = 100 * USEC;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700667 for (int frameCount = 0; frameCount < 10; frameCount++) {
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700668 res = mFrames.waitForBuffer(SEC + expectedExposureTime);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700669 ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
670
671 camera_metadata_t *frame;
672 res = mFrames.dequeue(&frame);
673 ASSERT_EQ(NO_ERROR, res);
674 ASSERT_TRUE(frame != NULL);
675
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700676 camera_metadata_entry_t frameNumber;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700677 res = find_camera_metadata_entry(frame,
678 ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700679 &frameNumber);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700680 ASSERT_EQ(NO_ERROR, res);
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700681 ASSERT_EQ(frameCount, *frameNumber.data.i32);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700682
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700683 res = rawWaiter->waitForFrame(SEC + expectedExposureTime);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700684 ASSERT_EQ(NO_ERROR, res) <<
685 "Never got raw data for capture " << frameCount;
686
687 CpuConsumer::LockedBuffer buffer;
688 res = rawConsumer->lockNextBuffer(&buffer);
689 ASSERT_EQ(NO_ERROR, res);
690
691 IF_ALOGV() {
692 char dumpname[60];
693 snprintf(dumpname, 60,
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700694 "/data/local/tmp/camera2_test-"
695 "captureBurstRaw-dump_%d.raw",
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700696 frameCount);
697 ALOGV("Dumping raw buffer to %s", dumpname);
698 // Write to file
699 std::ofstream rawFile(dumpname);
700 for (unsigned int y = 0; y < buffer.height; y++) {
701 rawFile.write(
702 (const char *)(buffer.data + y * buffer.stride * 2),
703 buffer.width * 2);
704 }
705 rawFile.close();
706 }
707
708 res = rawConsumer->unlockBuffer(buffer);
709 ASSERT_EQ(NO_ERROR, res);
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700710
711 expectedExposureTime *= 2;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700712 }
713 }
714}
715
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700716TEST_F(Camera2Test, ConstructDefaultRequests) {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800717
718 TEST_EXTENSION_FORKING_INIT;
719
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700720 status_t res;
721
722 for (int id = 0; id < getNumCameras(); id++) {
723 if (!isHal2Supported(id)) continue;
724
725 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
726
727 for (int i = CAMERA2_TEMPLATE_PREVIEW; i < CAMERA2_TEMPLATE_COUNT;
728 i++) {
729 camera_metadata_t *request = NULL;
730 res = mDevice->ops->construct_default_request(mDevice,
731 i,
732 &request);
733 EXPECT_EQ(NO_ERROR, res) <<
Igor Murashkin3d991c82013-04-18 10:09:16 -0700734 "Unable to construct request from template type " << i;
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700735 EXPECT_TRUE(request != NULL);
736 EXPECT_LT((size_t)0, get_camera_metadata_entry_count(request));
737 EXPECT_LT((size_t)0, get_camera_metadata_data_count(request));
738
739 IF_ALOGV() {
740 std::cout << " ** Template type " << i << ":"<<std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700741 dump_indented_camera_metadata(request, 0, 2, 4);
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700742 }
743
744 free_camera_metadata(request);
745 }
746 }
747}
748
Igor Murashkineab33fc2012-11-06 17:02:54 -0800749TEST_F(Camera2Test, Capture1Jpeg) {
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700750 status_t res;
751
752 for (int id = 0; id < getNumCameras(); id++) {
753 if (!isHal2Supported(id)) continue;
754
755 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
756
Dan Stoza5dce9e42014-04-07 13:39:37 -0700757 sp<IGraphicBufferProducer> bqProducer;
758 sp<IGraphicBufferConsumer> bqConsumer;
759 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer);
760 sp<CpuConsumer> jpegConsumer = new CpuConsumer(bqConsumer, 1);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700761 sp<FrameWaiter> jpegWaiter = new FrameWaiter();
762 jpegConsumer->setFrameAvailableListener(jpegWaiter);
763
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700764 const int32_t *jpegResolutions;
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700765 size_t jpegResolutionsCount;
766
767 int format = HAL_PIXEL_FORMAT_BLOB;
768
769 getResolutionList(format,
770 &jpegResolutions, &jpegResolutionsCount);
771 ASSERT_LT((size_t)0, jpegResolutionsCount);
772
773 // Pick first available JPEG resolution
774 int width = jpegResolutions[0];
775 int height = jpegResolutions[1];
776
777 int streamId;
778 ASSERT_NO_FATAL_FAILURE(
Dan Stoza5dce9e42014-04-07 13:39:37 -0700779 setUpStream(bqProducer, width, height, format, &streamId) );
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700780
781 camera_metadata_t *request;
782 request = allocate_camera_metadata(20, 2000);
783
Igor Murashkin09ad0a32012-12-03 13:33:08 -0800784 uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700785 add_camera_metadata_entry(request,
786 ANDROID_REQUEST_METADATA_MODE,
787 (void**)&metadataMode, 1);
788 uint32_t outputStreams = streamId;
789 add_camera_metadata_entry(request,
790 ANDROID_REQUEST_OUTPUT_STREAMS,
791 (void**)&outputStreams, 1);
792
793 uint64_t exposureTime = 10*MSEC;
794 add_camera_metadata_entry(request,
795 ANDROID_SENSOR_EXPOSURE_TIME,
796 (void**)&exposureTime, 1);
797 uint64_t frameDuration = 30*MSEC;
798 add_camera_metadata_entry(request,
799 ANDROID_SENSOR_FRAME_DURATION,
800 (void**)&frameDuration, 1);
801 uint32_t sensitivity = 100;
802 add_camera_metadata_entry(request,
803 ANDROID_SENSOR_SENSITIVITY,
804 (void**)&sensitivity, 1);
Igor Murashkin599b76f2012-12-18 16:06:07 -0800805 uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
806 add_camera_metadata_entry(request,
807 ANDROID_REQUEST_TYPE,
808 (void**)&requestType, 1);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700809
810 uint32_t hourOfDay = 12;
811 add_camera_metadata_entry(request,
812 0x80000000, // EMULATOR_HOUROFDAY
813 &hourOfDay, 1);
814
815 IF_ALOGV() {
816 std::cout << "Input request: " << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700817 dump_indented_camera_metadata(request, 0, 1, 4);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700818 }
819
820 res = mRequests.enqueue(request);
821 ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " << strerror(-res);
822
823 res = mFrames.waitForBuffer(exposureTime + SEC);
824 ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
825
826 camera_metadata_t *frame;
827 res = mFrames.dequeue(&frame);
828 ASSERT_EQ(NO_ERROR, res);
829 ASSERT_TRUE(frame != NULL);
830
831 IF_ALOGV() {
832 std::cout << "Output frame:" << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700833 dump_indented_camera_metadata(frame, 0, 1, 4);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700834 }
835
836 res = jpegWaiter->waitForFrame(exposureTime + SEC);
837 ASSERT_EQ(NO_ERROR, res);
838
839 CpuConsumer::LockedBuffer buffer;
840 res = jpegConsumer->lockNextBuffer(&buffer);
841 ASSERT_EQ(NO_ERROR, res);
842
843 IF_ALOGV() {
844 const char *dumpname =
845 "/data/local/tmp/camera2_test-capture1jpeg-dump.jpeg";
846 ALOGV("Dumping raw buffer to %s", dumpname);
847 // Write to file
848 std::ofstream jpegFile(dumpname);
849 size_t bpp = 1;
850 for (unsigned int y = 0; y < buffer.height; y++) {
851 jpegFile.write(
852 (const char *)(buffer.data + y * buffer.stride * bpp),
853 buffer.width * bpp);
854 }
855 jpegFile.close();
856 }
857
858 res = jpegConsumer->unlockBuffer(buffer);
859 ASSERT_EQ(NO_ERROR, res);
860
Igor Murashkin599b76f2012-12-18 16:06:07 -0800861 ASSERT_EQ(OK, waitUntilDrained());
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700862 ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId));
863
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700864 res = closeCameraDevice(&mDevice);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700865 ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
866
867 }
868}
869
Igor Murashkine302ee32012-11-05 11:14:49 -0800870} // namespace tests
871} // namespace camera2
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700872} // namespace android