Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 17 | #define LOG_TAG "Camera2_test" |
| 18 | #define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <utils/Log.h> |
Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 21 | #include <gtest/gtest.h> |
| 22 | #include <iostream> |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 23 | #include <fstream> |
| 24 | |
| 25 | #include <utils/Vector.h> |
| 26 | #include <gui/CpuConsumer.h> |
| 27 | #include <system/camera_metadata.h> |
| 28 | |
| 29 | #include "camera2_utils.h" |
| 30 | |
| 31 | namespace android { |
Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 32 | |
| 33 | class Camera2Test: public testing::Test { |
| 34 | public: |
| 35 | static void SetUpTestCase() { |
| 36 | int res; |
| 37 | |
| 38 | hw_module_t *module = NULL; |
| 39 | res = hw_get_module(CAMERA_HARDWARE_MODULE_ID, |
| 40 | (const hw_module_t **)&module); |
| 41 | |
| 42 | ASSERT_EQ(0, res) |
| 43 | << "Failure opening camera hardware module: " << res; |
| 44 | ASSERT_TRUE(NULL != module) |
| 45 | << "No camera module was set by hw_get_module"; |
| 46 | |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 47 | IF_ALOGV() { |
| 48 | std::cout << " Camera module name: " |
| 49 | << module->name << std::endl; |
| 50 | std::cout << " Camera module author: " |
| 51 | << module->author << std::endl; |
| 52 | std::cout << " Camera module API version: 0x" << std::hex |
| 53 | << module->module_api_version << std::endl; |
| 54 | std::cout << " Camera module HAL API version: 0x" << std::hex |
| 55 | << module->hal_api_version << std::endl; |
| 56 | } |
Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 57 | |
| 58 | int16_t version2_0 = CAMERA_MODULE_API_VERSION_2_0; |
| 59 | ASSERT_EQ(version2_0, module->module_api_version) |
| 60 | << "Camera module version is 0x" |
| 61 | << std::hex << module->module_api_version |
| 62 | << ", not 2.0. (0x" |
| 63 | << std::hex << CAMERA_MODULE_API_VERSION_2_0 << ")"; |
| 64 | |
| 65 | sCameraModule = reinterpret_cast<camera_module_t*>(module); |
| 66 | |
| 67 | sNumCameras = sCameraModule->get_number_of_cameras(); |
| 68 | ASSERT_LT(0, sNumCameras) << "No camera devices available!"; |
| 69 | |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 70 | IF_ALOGV() { |
| 71 | std::cout << " Camera device count: " << sNumCameras << std::endl; |
| 72 | } |
| 73 | |
Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 74 | sCameraSupportsHal2 = new bool[sNumCameras]; |
| 75 | |
| 76 | for (int i = 0; i < sNumCameras; i++) { |
| 77 | camera_info info; |
| 78 | res = sCameraModule->get_camera_info(i, &info); |
| 79 | ASSERT_EQ(0, res) |
| 80 | << "Failure getting camera info for camera " << i; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 81 | IF_ALOGV() { |
| 82 | std::cout << " Camera device: " << std::dec |
| 83 | << i << std::endl;; |
| 84 | std::cout << " Facing: " << std::dec |
| 85 | << info.facing << std::endl; |
| 86 | std::cout << " Orientation: " << std::dec |
| 87 | << info.orientation << std::endl; |
| 88 | std::cout << " Version: 0x" << std::hex << |
| 89 | info.device_version << std::endl; |
| 90 | } |
Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 91 | if (info.device_version >= CAMERA_DEVICE_API_VERSION_2_0) { |
| 92 | sCameraSupportsHal2[i] = true; |
| 93 | ASSERT_TRUE(NULL != info.static_camera_characteristics); |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 94 | IF_ALOGV() { |
| 95 | std::cout << " Static camera metadata:" << std::endl; |
| 96 | dump_camera_metadata(info.static_camera_characteristics, |
| 97 | 0, 1); |
| 98 | } |
Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 99 | } else { |
| 100 | sCameraSupportsHal2[i] = false; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static const camera_module_t *getCameraModule() { |
| 106 | return sCameraModule; |
| 107 | } |
| 108 | |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 109 | static int getNumCameras() { |
| 110 | return sNumCameras; |
| 111 | } |
| 112 | |
| 113 | static bool isHal2Supported(int id) { |
| 114 | return sCameraSupportsHal2[id]; |
| 115 | } |
| 116 | |
| 117 | static camera2_device_t *openCameraDevice(int id) { |
| 118 | ALOGV("Opening camera %d", id); |
Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 119 | if (NULL == sCameraSupportsHal2) return NULL; |
| 120 | if (id >= sNumCameras) return NULL; |
| 121 | if (!sCameraSupportsHal2[id]) return NULL; |
| 122 | |
| 123 | hw_device_t *device = NULL; |
| 124 | const camera_module_t *cam_module = getCameraModule(); |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 125 | if (cam_module == NULL) { |
| 126 | return NULL; |
| 127 | } |
| 128 | |
Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 129 | char camId[10]; |
| 130 | int res; |
| 131 | |
| 132 | snprintf(camId, 10, "%d", id); |
| 133 | res = cam_module->common.methods->open( |
| 134 | (const hw_module_t*)cam_module, |
| 135 | camId, |
| 136 | &device); |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 137 | if (res != NO_ERROR || device == NULL) { |
Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 138 | return NULL; |
| 139 | } |
| 140 | camera2_device_t *cam_device = |
| 141 | reinterpret_cast<camera2_device_t*>(device); |
| 142 | return cam_device; |
| 143 | } |
| 144 | |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 145 | static status_t configureCameraDevice(camera2_device_t *dev, |
| 146 | MetadataQueue &requestQueue, |
| 147 | MetadataQueue &frameQueue, |
| 148 | NotifierListener &listener) { |
Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 149 | |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 150 | status_t err; |
| 151 | |
| 152 | err = dev->ops->set_request_queue_src_ops(dev, |
| 153 | requestQueue.getToConsumerInterface()); |
| 154 | if (err != OK) return err; |
| 155 | |
| 156 | requestQueue.setFromConsumerInterface(dev); |
| 157 | |
| 158 | err = dev->ops->set_frame_queue_dst_ops(dev, |
| 159 | frameQueue.getToProducerInterface()); |
| 160 | if (err != OK) return err; |
| 161 | |
| 162 | err = listener.getNotificationsFrom(dev); |
| 163 | if (err != OK) return err; |
| 164 | |
| 165 | vendor_tag_query_ops_t *vendor_metadata_tag_ops; |
| 166 | err = dev->ops->get_metadata_vendor_tag_ops(dev, &vendor_metadata_tag_ops); |
| 167 | if (err != OK) return err; |
| 168 | |
| 169 | err = set_camera_metadata_vendor_tag_ops(vendor_metadata_tag_ops); |
| 170 | if (err != OK) return err; |
| 171 | |
| 172 | return OK; |
| 173 | } |
| 174 | |
| 175 | static status_t closeCameraDevice(camera2_device_t *cam_dev) { |
| 176 | int res; |
| 177 | ALOGV("Closing camera %p", cam_dev); |
| 178 | |
| 179 | hw_device_t *dev = reinterpret_cast<hw_device_t *>(cam_dev); |
| 180 | res = dev->close(dev); |
| 181 | return res; |
| 182 | } |
| 183 | |
| 184 | void setUpCamera(int id) { |
| 185 | ASSERT_GT(sNumCameras, id); |
| 186 | status_t res; |
| 187 | |
| 188 | if (mDevice != NULL) { |
| 189 | closeCameraDevice(mDevice); |
| 190 | } |
| 191 | mDevice = openCameraDevice(id); |
| 192 | ASSERT_TRUE(NULL != mDevice) << "Failed to open camera device"; |
| 193 | |
| 194 | camera_info info; |
| 195 | res = sCameraModule->get_camera_info(id, &info); |
| 196 | ASSERT_EQ(OK, res); |
| 197 | |
| 198 | mStaticInfo = info.static_camera_characteristics; |
| 199 | |
| 200 | res = configureCameraDevice(mDevice, |
| 201 | mRequests, |
| 202 | mFrames, |
| 203 | mNotifications); |
| 204 | ASSERT_EQ(OK, res) << "Failure to configure camera device"; |
| 205 | |
| 206 | } |
| 207 | |
| 208 | void setUpStream(sp<ISurfaceTexture> consumer, |
| 209 | int width, int height, int format, int *id) { |
| 210 | status_t res; |
| 211 | |
| 212 | StreamAdapter* stream = new StreamAdapter(consumer); |
| 213 | |
| 214 | ALOGV("Creating stream, format 0x%x, %d x %d", format, width, height); |
| 215 | res = stream->connectToDevice(mDevice, width, height, format); |
| 216 | ASSERT_EQ(NO_ERROR, res) << "Failed to connect to stream: " |
| 217 | << strerror(-res); |
| 218 | mStreams.push_back(stream); |
| 219 | |
| 220 | *id = stream->getId(); |
| 221 | } |
| 222 | |
| 223 | void disconnectStream(int id) { |
| 224 | status_t res; |
| 225 | unsigned int i=0; |
| 226 | for (; i < mStreams.size(); i++) { |
| 227 | if (mStreams[i]->getId() == id) { |
| 228 | res = mStreams[i]->disconnect(); |
| 229 | ASSERT_EQ(NO_ERROR, res) << |
| 230 | "Failed to disconnect stream " << id; |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | ASSERT_GT(mStreams.size(), i) << "Stream id not found:" << id; |
| 235 | } |
| 236 | |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 237 | void getResolutionList(int32_t format, |
| 238 | int32_t **list, |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 239 | size_t *count) { |
| 240 | |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 241 | status_t res; |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 242 | camera_metadata_entry_t availableFormats; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 243 | res = find_camera_metadata_entry(mStaticInfo, |
| 244 | ANDROID_SCALER_AVAILABLE_FORMATS, |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 245 | &availableFormats); |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 246 | ASSERT_EQ(OK, res); |
| 247 | |
| 248 | uint32_t formatIdx; |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 249 | for (formatIdx=0; formatIdx < availableFormats.count; formatIdx++) { |
| 250 | if (availableFormats.data.i32[formatIdx] == format) break; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 251 | } |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 252 | ASSERT_NE(availableFormats.count, formatIdx) |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 253 | << "No support found for format 0x" << std::hex << format; |
| 254 | |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 255 | camera_metadata_entry_t availableSizesPerFormat; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 256 | res = find_camera_metadata_entry(mStaticInfo, |
| 257 | ANDROID_SCALER_AVAILABLE_SIZES_PER_FORMAT, |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 258 | &availableSizesPerFormat); |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 259 | ASSERT_EQ(OK, res); |
| 260 | |
| 261 | int size_offset = 0; |
| 262 | for (unsigned int i=0; i < formatIdx; i++) { |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 263 | size_offset += availableSizesPerFormat.data.i32[i]; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 266 | camera_metadata_entry_t availableSizes; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 267 | res = find_camera_metadata_entry(mStaticInfo, |
| 268 | ANDROID_SCALER_AVAILABLE_SIZES, |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 269 | &availableSizes); |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 270 | ASSERT_EQ(OK, res); |
| 271 | |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 272 | *list = availableSizes.data.i32 + size_offset; |
| 273 | *count = availableSizesPerFormat.data.i32[formatIdx]; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | virtual void SetUp() { |
| 277 | const ::testing::TestInfo* const testInfo = |
| 278 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 279 | |
| 280 | ALOGV("*** Starting test %s in test case %s", testInfo->name(), testInfo->test_case_name()); |
| 281 | mDevice = NULL; |
| 282 | } |
| 283 | |
| 284 | virtual void TearDown() { |
| 285 | for (unsigned int i = 0; i < mStreams.size(); i++) { |
| 286 | delete mStreams[i]; |
| 287 | } |
| 288 | if (mDevice != NULL) { |
| 289 | closeCameraDevice(mDevice); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | camera2_device *mDevice; |
| 294 | camera_metadata_t *mStaticInfo; |
| 295 | |
| 296 | MetadataQueue mRequests; |
| 297 | MetadataQueue mFrames; |
| 298 | NotifierListener mNotifications; |
| 299 | |
| 300 | Vector<StreamAdapter*> mStreams; |
| 301 | |
| 302 | private: |
Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 303 | static camera_module_t *sCameraModule; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 304 | static int sNumCameras; |
| 305 | static bool *sCameraSupportsHal2; |
Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | camera_module_t *Camera2Test::sCameraModule = NULL; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 309 | bool *Camera2Test::sCameraSupportsHal2 = NULL; |
| 310 | int Camera2Test::sNumCameras = 0; |
| 311 | |
| 312 | static const nsecs_t USEC = 1000; |
| 313 | static const nsecs_t MSEC = 1000*USEC; |
| 314 | static const nsecs_t SEC = 1000*MSEC; |
Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 315 | |
| 316 | |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 317 | TEST_F(Camera2Test, OpenClose) { |
| 318 | status_t res; |
| 319 | |
| 320 | for (int id = 0; id < getNumCameras(); id++) { |
| 321 | if (!isHal2Supported(id)) continue; |
| 322 | |
| 323 | camera2_device_t *d = openCameraDevice(id); |
| 324 | ASSERT_TRUE(NULL != d) << "Failed to open camera device"; |
| 325 | |
| 326 | res = closeCameraDevice(d); |
| 327 | ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device"; |
| 328 | } |
Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 329 | } |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 330 | |
| 331 | TEST_F(Camera2Test, Capture1Raw) { |
| 332 | status_t res; |
| 333 | |
| 334 | for (int id = 0; id < getNumCameras(); id++) { |
| 335 | if (!isHal2Supported(id)) continue; |
| 336 | |
| 337 | ASSERT_NO_FATAL_FAILURE(setUpCamera(id)); |
| 338 | |
| 339 | sp<CpuConsumer> rawConsumer = new CpuConsumer(1); |
| 340 | sp<FrameWaiter> rawWaiter = new FrameWaiter(); |
| 341 | rawConsumer->setFrameAvailableListener(rawWaiter); |
| 342 | |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 343 | int32_t *rawResolutions; |
| 344 | size_t rawResolutionsCount; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 345 | |
| 346 | int format = HAL_PIXEL_FORMAT_RAW_SENSOR; |
| 347 | |
| 348 | getResolutionList(format, |
| 349 | &rawResolutions, &rawResolutionsCount); |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 350 | ASSERT_LT((size_t)0, rawResolutionsCount); |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 351 | |
| 352 | // Pick first available raw resolution |
| 353 | int width = rawResolutions[0]; |
| 354 | int height = rawResolutions[1]; |
| 355 | |
| 356 | int streamId; |
| 357 | ASSERT_NO_FATAL_FAILURE( |
| 358 | setUpStream(rawConsumer->getProducerInterface(), |
| 359 | width, height, format, &streamId) ); |
| 360 | |
| 361 | camera_metadata_t *request; |
| 362 | request = allocate_camera_metadata(20, 2000); |
| 363 | |
| 364 | uint8_t metadataMode = ANDROID_REQUEST_METADATA_FULL; |
| 365 | add_camera_metadata_entry(request, |
| 366 | ANDROID_REQUEST_METADATA_MODE, |
| 367 | (void**)&metadataMode, 1); |
| 368 | uint32_t outputStreams = streamId; |
| 369 | add_camera_metadata_entry(request, |
| 370 | ANDROID_REQUEST_OUTPUT_STREAMS, |
| 371 | (void**)&outputStreams, 1); |
| 372 | |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 373 | uint64_t exposureTime = 10000*MSEC; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 374 | add_camera_metadata_entry(request, |
| 375 | ANDROID_SENSOR_EXPOSURE_TIME, |
| 376 | (void**)&exposureTime, 1); |
| 377 | uint64_t frameDuration = 30*MSEC; |
| 378 | add_camera_metadata_entry(request, |
| 379 | ANDROID_SENSOR_FRAME_DURATION, |
| 380 | (void**)&frameDuration, 1); |
| 381 | uint32_t sensitivity = 100; |
| 382 | add_camera_metadata_entry(request, |
| 383 | ANDROID_SENSOR_SENSITIVITY, |
| 384 | (void**)&sensitivity, 1); |
| 385 | |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 386 | uint32_t hourOfDay = 22; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 387 | add_camera_metadata_entry(request, |
| 388 | 0x80000000, // EMULATOR_HOUROFDAY |
| 389 | &hourOfDay, 1); |
| 390 | |
| 391 | IF_ALOGV() { |
| 392 | std::cout << "Input request: " << std::endl; |
| 393 | dump_camera_metadata(request, 0, 1); |
| 394 | } |
| 395 | |
| 396 | res = mRequests.enqueue(request); |
| 397 | ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " << strerror(-res); |
| 398 | |
| 399 | res = mFrames.waitForBuffer(exposureTime + SEC); |
| 400 | ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res); |
| 401 | |
| 402 | camera_metadata_t *frame; |
| 403 | res = mFrames.dequeue(&frame); |
| 404 | ASSERT_EQ(NO_ERROR, res); |
| 405 | ASSERT_TRUE(frame != NULL); |
| 406 | |
| 407 | IF_ALOGV() { |
| 408 | std::cout << "Output frame:" << std::endl; |
| 409 | dump_camera_metadata(frame, 0, 1); |
| 410 | } |
| 411 | |
| 412 | res = rawWaiter->waitForFrame(exposureTime + SEC); |
| 413 | ASSERT_EQ(NO_ERROR, res); |
| 414 | |
| 415 | CpuConsumer::LockedBuffer buffer; |
| 416 | res = rawConsumer->lockNextBuffer(&buffer); |
| 417 | ASSERT_EQ(NO_ERROR, res); |
| 418 | |
| 419 | IF_ALOGV() { |
| 420 | const char *dumpname = |
| 421 | "/data/local/tmp/camera2_test-capture1raw-dump.raw"; |
| 422 | ALOGV("Dumping raw buffer to %s", dumpname); |
| 423 | // Write to file |
| 424 | std::ofstream rawFile(dumpname); |
| 425 | for (unsigned int y = 0; y < buffer.height; y++) { |
| 426 | rawFile.write((const char *)(buffer.data + y * buffer.stride * 2), |
| 427 | buffer.width * 2); |
| 428 | } |
| 429 | rawFile.close(); |
| 430 | } |
| 431 | |
| 432 | res = rawConsumer->unlockBuffer(buffer); |
| 433 | ASSERT_EQ(NO_ERROR, res); |
| 434 | |
| 435 | ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId)); |
| 436 | |
| 437 | res = closeCameraDevice(mDevice); |
| 438 | ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device"; |
| 439 | |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | TEST_F(Camera2Test, CaptureBurstRaw) { |
| 444 | status_t res; |
| 445 | |
| 446 | for (int id = 0; id < getNumCameras(); id++) { |
| 447 | if (!isHal2Supported(id)) continue; |
| 448 | |
| 449 | ASSERT_NO_FATAL_FAILURE(setUpCamera(id)); |
| 450 | |
| 451 | sp<CpuConsumer> rawConsumer = new CpuConsumer(1); |
| 452 | sp<FrameWaiter> rawWaiter = new FrameWaiter(); |
| 453 | rawConsumer->setFrameAvailableListener(rawWaiter); |
| 454 | |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 455 | int32_t *rawResolutions; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 456 | size_t rawResolutionsCount; |
| 457 | |
| 458 | int format = HAL_PIXEL_FORMAT_RAW_SENSOR; |
| 459 | |
| 460 | getResolutionList(format, |
| 461 | &rawResolutions, &rawResolutionsCount); |
| 462 | ASSERT_LT((uint32_t)0, rawResolutionsCount); |
| 463 | |
| 464 | // Pick first available raw resolution |
| 465 | int width = rawResolutions[0]; |
| 466 | int height = rawResolutions[1]; |
| 467 | |
| 468 | int streamId; |
| 469 | ASSERT_NO_FATAL_FAILURE( |
| 470 | setUpStream(rawConsumer->getProducerInterface(), |
| 471 | width, height, format, &streamId) ); |
| 472 | |
| 473 | camera_metadata_t *request; |
| 474 | request = allocate_camera_metadata(20, 2000); |
| 475 | |
| 476 | uint8_t metadataMode = ANDROID_REQUEST_METADATA_FULL; |
| 477 | add_camera_metadata_entry(request, |
| 478 | ANDROID_REQUEST_METADATA_MODE, |
| 479 | (void**)&metadataMode, 1); |
| 480 | uint32_t outputStreams = streamId; |
| 481 | add_camera_metadata_entry(request, |
| 482 | ANDROID_REQUEST_OUTPUT_STREAMS, |
| 483 | (void**)&outputStreams, 1); |
| 484 | |
| 485 | uint64_t frameDuration = 30*MSEC; |
| 486 | add_camera_metadata_entry(request, |
| 487 | ANDROID_SENSOR_FRAME_DURATION, |
| 488 | (void**)&frameDuration, 1); |
| 489 | uint32_t sensitivity = 100; |
| 490 | add_camera_metadata_entry(request, |
| 491 | ANDROID_SENSOR_SENSITIVITY, |
| 492 | (void**)&sensitivity, 1); |
| 493 | |
| 494 | uint32_t hourOfDay = 12; |
| 495 | add_camera_metadata_entry(request, |
| 496 | 0x80000000, // EMULATOR_HOUROFDAY |
| 497 | &hourOfDay, 1); |
| 498 | |
| 499 | IF_ALOGV() { |
| 500 | std::cout << "Input request template: " << std::endl; |
| 501 | dump_camera_metadata(request, 0, 1); |
| 502 | } |
| 503 | |
| 504 | int numCaptures = 10; |
| 505 | |
| 506 | // Enqueue numCaptures requests with increasing exposure time |
| 507 | |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 508 | uint64_t exposureTime = 100 * USEC; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 509 | for (int reqCount = 0; reqCount < numCaptures; reqCount++ ) { |
| 510 | camera_metadata_t *req; |
| 511 | req = allocate_camera_metadata(20, 2000); |
| 512 | append_camera_metadata(req, request); |
| 513 | |
| 514 | add_camera_metadata_entry(req, |
| 515 | ANDROID_SENSOR_EXPOSURE_TIME, |
| 516 | (void**)&exposureTime, 1); |
| 517 | exposureTime *= 2; |
| 518 | |
| 519 | res = mRequests.enqueue(req); |
| 520 | ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " |
| 521 | << strerror(-res); |
| 522 | } |
| 523 | |
| 524 | // Get frames and image buffers one by one |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 525 | uint64_t expectedExposureTime = 100 * USEC; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 526 | for (int frameCount = 0; frameCount < 10; frameCount++) { |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 527 | res = mFrames.waitForBuffer(SEC + expectedExposureTime); |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 528 | ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res); |
| 529 | |
| 530 | camera_metadata_t *frame; |
| 531 | res = mFrames.dequeue(&frame); |
| 532 | ASSERT_EQ(NO_ERROR, res); |
| 533 | ASSERT_TRUE(frame != NULL); |
| 534 | |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 535 | camera_metadata_entry_t frameNumber; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 536 | res = find_camera_metadata_entry(frame, |
| 537 | ANDROID_REQUEST_FRAME_COUNT, |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 538 | &frameNumber); |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 539 | ASSERT_EQ(NO_ERROR, res); |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 540 | ASSERT_EQ(frameCount, *frameNumber.data.i32); |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 541 | |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 542 | res = rawWaiter->waitForFrame(SEC + expectedExposureTime); |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 543 | ASSERT_EQ(NO_ERROR, res) << |
| 544 | "Never got raw data for capture " << frameCount; |
| 545 | |
| 546 | CpuConsumer::LockedBuffer buffer; |
| 547 | res = rawConsumer->lockNextBuffer(&buffer); |
| 548 | ASSERT_EQ(NO_ERROR, res); |
| 549 | |
| 550 | IF_ALOGV() { |
| 551 | char dumpname[60]; |
| 552 | snprintf(dumpname, 60, |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 553 | "/data/local/tmp/camera2_test-" |
| 554 | "captureBurstRaw-dump_%d.raw", |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 555 | frameCount); |
| 556 | ALOGV("Dumping raw buffer to %s", dumpname); |
| 557 | // Write to file |
| 558 | std::ofstream rawFile(dumpname); |
| 559 | for (unsigned int y = 0; y < buffer.height; y++) { |
| 560 | rawFile.write( |
| 561 | (const char *)(buffer.data + y * buffer.stride * 2), |
| 562 | buffer.width * 2); |
| 563 | } |
| 564 | rawFile.close(); |
| 565 | } |
| 566 | |
| 567 | res = rawConsumer->unlockBuffer(buffer); |
| 568 | ASSERT_EQ(NO_ERROR, res); |
Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 569 | |
| 570 | expectedExposureTime *= 2; |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
Eino-Ville Talvala | 6adfd6b | 2012-05-14 15:25:27 -0700 | [diff] [blame^] | 575 | TEST_F(Camera2Test, ConstructDefaultRequests) { |
| 576 | status_t res; |
| 577 | |
| 578 | for (int id = 0; id < getNumCameras(); id++) { |
| 579 | if (!isHal2Supported(id)) continue; |
| 580 | |
| 581 | ASSERT_NO_FATAL_FAILURE(setUpCamera(id)); |
| 582 | |
| 583 | for (int i = CAMERA2_TEMPLATE_PREVIEW; i < CAMERA2_TEMPLATE_COUNT; |
| 584 | i++) { |
| 585 | camera_metadata_t *request = NULL; |
| 586 | res = mDevice->ops->construct_default_request(mDevice, |
| 587 | i, |
| 588 | &request); |
| 589 | EXPECT_EQ(NO_ERROR, res) << |
| 590 | "Unable to construct request from template type %d", i; |
| 591 | EXPECT_TRUE(request != NULL); |
| 592 | EXPECT_LT((size_t)0, get_camera_metadata_entry_count(request)); |
| 593 | EXPECT_LT((size_t)0, get_camera_metadata_data_count(request)); |
| 594 | |
| 595 | IF_ALOGV() { |
| 596 | std::cout << " ** Template type " << i << ":"<<std::endl; |
| 597 | dump_camera_metadata(request, 0, 2); |
| 598 | } |
| 599 | |
| 600 | free_camera_metadata(request); |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 605 | } // namespace android |