| 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 | 895ed34 | 2012-05-20 17:25:53 -0700 | [diff] [blame] | 242 | if (format != CAMERA2_HAL_PIXEL_FORMAT_OPAQUE) { | 
|  | 243 | camera_metadata_entry_t availableFormats; | 
|  | 244 | res = find_camera_metadata_entry(mStaticInfo, | 
|  | 245 | ANDROID_SCALER_AVAILABLE_FORMATS, | 
|  | 246 | &availableFormats); | 
|  | 247 | ASSERT_EQ(OK, res); | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 248 |  | 
| Eino-Ville Talvala | 895ed34 | 2012-05-20 17:25:53 -0700 | [diff] [blame] | 249 | uint32_t formatIdx; | 
|  | 250 | for (formatIdx=0; formatIdx < availableFormats.count; formatIdx++) { | 
|  | 251 | if (availableFormats.data.i32[formatIdx] == format) break; | 
|  | 252 | } | 
|  | 253 | ASSERT_NE(availableFormats.count, formatIdx) | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 254 | << "No support found for format 0x" << std::hex << format; | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 255 | } | 
|  | 256 |  | 
| Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 257 | camera_metadata_entry_t availableSizes; | 
| Eino-Ville Talvala | 895ed34 | 2012-05-20 17:25:53 -0700 | [diff] [blame] | 258 | if (format == HAL_PIXEL_FORMAT_RAW_SENSOR) { | 
|  | 259 | res = find_camera_metadata_entry(mStaticInfo, | 
|  | 260 | ANDROID_SCALER_AVAILABLE_RAW_SIZES, | 
|  | 261 | &availableSizes); | 
|  | 262 | ASSERT_EQ(OK, res); | 
|  | 263 | } else { | 
|  | 264 | res = find_camera_metadata_entry(mStaticInfo, | 
|  | 265 | ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES, | 
|  | 266 | &availableSizes); | 
|  | 267 | ASSERT_EQ(OK, res); | 
|  | 268 | } | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 269 |  | 
| Eino-Ville Talvala | 895ed34 | 2012-05-20 17:25:53 -0700 | [diff] [blame] | 270 | *list = availableSizes.data.i32; | 
|  | 271 | *count = availableSizes.count; | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 272 | } | 
|  | 273 |  | 
|  | 274 | virtual void SetUp() { | 
|  | 275 | const ::testing::TestInfo* const testInfo = | 
|  | 276 | ::testing::UnitTest::GetInstance()->current_test_info(); | 
|  | 277 |  | 
|  | 278 | ALOGV("*** Starting test %s in test case %s", testInfo->name(), testInfo->test_case_name()); | 
|  | 279 | mDevice = NULL; | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | virtual void TearDown() { | 
|  | 283 | for (unsigned int i = 0; i < mStreams.size(); i++) { | 
|  | 284 | delete mStreams[i]; | 
|  | 285 | } | 
|  | 286 | if (mDevice != NULL) { | 
|  | 287 | closeCameraDevice(mDevice); | 
|  | 288 | } | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | camera2_device    *mDevice; | 
|  | 292 | camera_metadata_t *mStaticInfo; | 
|  | 293 |  | 
|  | 294 | MetadataQueue    mRequests; | 
|  | 295 | MetadataQueue    mFrames; | 
|  | 296 | NotifierListener mNotifications; | 
|  | 297 |  | 
|  | 298 | Vector<StreamAdapter*> mStreams; | 
|  | 299 |  | 
|  | 300 | private: | 
| Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 301 | static camera_module_t *sCameraModule; | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 302 | static int              sNumCameras; | 
|  | 303 | static bool            *sCameraSupportsHal2; | 
| Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 304 | }; | 
|  | 305 |  | 
|  | 306 | camera_module_t *Camera2Test::sCameraModule = NULL; | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 307 | bool *Camera2Test::sCameraSupportsHal2      = NULL; | 
|  | 308 | int Camera2Test::sNumCameras                = 0; | 
|  | 309 |  | 
|  | 310 | static const nsecs_t USEC = 1000; | 
|  | 311 | static const nsecs_t MSEC = 1000*USEC; | 
|  | 312 | static const nsecs_t SEC = 1000*MSEC; | 
| Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 313 |  | 
|  | 314 |  | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 315 | TEST_F(Camera2Test, OpenClose) { | 
|  | 316 | status_t res; | 
|  | 317 |  | 
|  | 318 | for (int id = 0; id < getNumCameras(); id++) { | 
|  | 319 | if (!isHal2Supported(id)) continue; | 
|  | 320 |  | 
|  | 321 | camera2_device_t *d = openCameraDevice(id); | 
|  | 322 | ASSERT_TRUE(NULL != d) << "Failed to open camera device"; | 
|  | 323 |  | 
|  | 324 | res = closeCameraDevice(d); | 
|  | 325 | ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device"; | 
|  | 326 | } | 
| Eino-Ville Talvala | fed0c02 | 2012-03-22 13:11:05 -0700 | [diff] [blame] | 327 | } | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 328 |  | 
|  | 329 | TEST_F(Camera2Test, Capture1Raw) { | 
|  | 330 | status_t res; | 
|  | 331 |  | 
|  | 332 | for (int id = 0; id < getNumCameras(); id++) { | 
|  | 333 | if (!isHal2Supported(id)) continue; | 
|  | 334 |  | 
|  | 335 | ASSERT_NO_FATAL_FAILURE(setUpCamera(id)); | 
|  | 336 |  | 
|  | 337 | sp<CpuConsumer> rawConsumer = new CpuConsumer(1); | 
|  | 338 | sp<FrameWaiter> rawWaiter = new FrameWaiter(); | 
|  | 339 | rawConsumer->setFrameAvailableListener(rawWaiter); | 
|  | 340 |  | 
| Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 341 | int32_t *rawResolutions; | 
|  | 342 | size_t   rawResolutionsCount; | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 343 |  | 
|  | 344 | int format = HAL_PIXEL_FORMAT_RAW_SENSOR; | 
|  | 345 |  | 
|  | 346 | getResolutionList(format, | 
|  | 347 | &rawResolutions, &rawResolutionsCount); | 
| Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 348 | ASSERT_LT((size_t)0, rawResolutionsCount); | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 349 |  | 
|  | 350 | // Pick first available raw resolution | 
|  | 351 | int width = rawResolutions[0]; | 
|  | 352 | int height = rawResolutions[1]; | 
|  | 353 |  | 
|  | 354 | int streamId; | 
|  | 355 | ASSERT_NO_FATAL_FAILURE( | 
|  | 356 | setUpStream(rawConsumer->getProducerInterface(), | 
|  | 357 | width, height, format, &streamId) ); | 
|  | 358 |  | 
|  | 359 | camera_metadata_t *request; | 
|  | 360 | request = allocate_camera_metadata(20, 2000); | 
|  | 361 |  | 
|  | 362 | uint8_t metadataMode = ANDROID_REQUEST_METADATA_FULL; | 
|  | 363 | add_camera_metadata_entry(request, | 
|  | 364 | ANDROID_REQUEST_METADATA_MODE, | 
|  | 365 | (void**)&metadataMode, 1); | 
|  | 366 | uint32_t outputStreams = streamId; | 
|  | 367 | add_camera_metadata_entry(request, | 
|  | 368 | ANDROID_REQUEST_OUTPUT_STREAMS, | 
|  | 369 | (void**)&outputStreams, 1); | 
|  | 370 |  | 
| Eino-Ville Talvala | 895ed34 | 2012-05-20 17:25:53 -0700 | [diff] [blame] | 371 | uint64_t exposureTime = 10*MSEC; | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 372 | add_camera_metadata_entry(request, | 
|  | 373 | ANDROID_SENSOR_EXPOSURE_TIME, | 
|  | 374 | (void**)&exposureTime, 1); | 
|  | 375 | uint64_t frameDuration = 30*MSEC; | 
|  | 376 | add_camera_metadata_entry(request, | 
|  | 377 | ANDROID_SENSOR_FRAME_DURATION, | 
|  | 378 | (void**)&frameDuration, 1); | 
|  | 379 | uint32_t sensitivity = 100; | 
|  | 380 | add_camera_metadata_entry(request, | 
|  | 381 | ANDROID_SENSOR_SENSITIVITY, | 
|  | 382 | (void**)&sensitivity, 1); | 
|  | 383 |  | 
| Eino-Ville Talvala | 895ed34 | 2012-05-20 17:25:53 -0700 | [diff] [blame] | 384 | uint32_t hourOfDay = 12; | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 385 | add_camera_metadata_entry(request, | 
|  | 386 | 0x80000000, // EMULATOR_HOUROFDAY | 
|  | 387 | &hourOfDay, 1); | 
|  | 388 |  | 
|  | 389 | IF_ALOGV() { | 
|  | 390 | std::cout << "Input request: " << std::endl; | 
|  | 391 | dump_camera_metadata(request, 0, 1); | 
|  | 392 | } | 
|  | 393 |  | 
|  | 394 | res = mRequests.enqueue(request); | 
|  | 395 | ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " << strerror(-res); | 
|  | 396 |  | 
|  | 397 | res = mFrames.waitForBuffer(exposureTime + SEC); | 
|  | 398 | ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res); | 
|  | 399 |  | 
|  | 400 | camera_metadata_t *frame; | 
|  | 401 | res = mFrames.dequeue(&frame); | 
|  | 402 | ASSERT_EQ(NO_ERROR, res); | 
|  | 403 | ASSERT_TRUE(frame != NULL); | 
|  | 404 |  | 
|  | 405 | IF_ALOGV() { | 
|  | 406 | std::cout << "Output frame:" << std::endl; | 
|  | 407 | dump_camera_metadata(frame, 0, 1); | 
|  | 408 | } | 
|  | 409 |  | 
|  | 410 | res = rawWaiter->waitForFrame(exposureTime + SEC); | 
|  | 411 | ASSERT_EQ(NO_ERROR, res); | 
|  | 412 |  | 
|  | 413 | CpuConsumer::LockedBuffer buffer; | 
|  | 414 | res = rawConsumer->lockNextBuffer(&buffer); | 
|  | 415 | ASSERT_EQ(NO_ERROR, res); | 
|  | 416 |  | 
|  | 417 | IF_ALOGV() { | 
|  | 418 | const char *dumpname = | 
|  | 419 | "/data/local/tmp/camera2_test-capture1raw-dump.raw"; | 
|  | 420 | ALOGV("Dumping raw buffer to %s", dumpname); | 
|  | 421 | // Write to file | 
|  | 422 | std::ofstream rawFile(dumpname); | 
|  | 423 | for (unsigned int y = 0; y < buffer.height; y++) { | 
|  | 424 | rawFile.write((const char *)(buffer.data + y * buffer.stride * 2), | 
|  | 425 | buffer.width * 2); | 
|  | 426 | } | 
|  | 427 | rawFile.close(); | 
|  | 428 | } | 
|  | 429 |  | 
|  | 430 | res = rawConsumer->unlockBuffer(buffer); | 
|  | 431 | ASSERT_EQ(NO_ERROR, res); | 
|  | 432 |  | 
|  | 433 | ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId)); | 
|  | 434 |  | 
|  | 435 | res = closeCameraDevice(mDevice); | 
|  | 436 | ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device"; | 
|  | 437 |  | 
|  | 438 | } | 
|  | 439 | } | 
|  | 440 |  | 
|  | 441 | TEST_F(Camera2Test, CaptureBurstRaw) { | 
|  | 442 | status_t res; | 
|  | 443 |  | 
|  | 444 | for (int id = 0; id < getNumCameras(); id++) { | 
|  | 445 | if (!isHal2Supported(id)) continue; | 
|  | 446 |  | 
|  | 447 | ASSERT_NO_FATAL_FAILURE(setUpCamera(id)); | 
|  | 448 |  | 
|  | 449 | sp<CpuConsumer> rawConsumer = new CpuConsumer(1); | 
|  | 450 | sp<FrameWaiter> rawWaiter = new FrameWaiter(); | 
|  | 451 | rawConsumer->setFrameAvailableListener(rawWaiter); | 
|  | 452 |  | 
| Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 453 | int32_t *rawResolutions; | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 454 | size_t    rawResolutionsCount; | 
|  | 455 |  | 
|  | 456 | int format = HAL_PIXEL_FORMAT_RAW_SENSOR; | 
|  | 457 |  | 
|  | 458 | getResolutionList(format, | 
|  | 459 | &rawResolutions, &rawResolutionsCount); | 
|  | 460 | ASSERT_LT((uint32_t)0, rawResolutionsCount); | 
|  | 461 |  | 
|  | 462 | // Pick first available raw resolution | 
|  | 463 | int width = rawResolutions[0]; | 
|  | 464 | int height = rawResolutions[1]; | 
|  | 465 |  | 
|  | 466 | int streamId; | 
|  | 467 | ASSERT_NO_FATAL_FAILURE( | 
|  | 468 | setUpStream(rawConsumer->getProducerInterface(), | 
|  | 469 | width, height, format, &streamId) ); | 
|  | 470 |  | 
|  | 471 | camera_metadata_t *request; | 
|  | 472 | request = allocate_camera_metadata(20, 2000); | 
|  | 473 |  | 
|  | 474 | uint8_t metadataMode = ANDROID_REQUEST_METADATA_FULL; | 
|  | 475 | add_camera_metadata_entry(request, | 
|  | 476 | ANDROID_REQUEST_METADATA_MODE, | 
|  | 477 | (void**)&metadataMode, 1); | 
|  | 478 | uint32_t outputStreams = streamId; | 
|  | 479 | add_camera_metadata_entry(request, | 
|  | 480 | ANDROID_REQUEST_OUTPUT_STREAMS, | 
|  | 481 | (void**)&outputStreams, 1); | 
|  | 482 |  | 
|  | 483 | uint64_t frameDuration = 30*MSEC; | 
|  | 484 | add_camera_metadata_entry(request, | 
|  | 485 | ANDROID_SENSOR_FRAME_DURATION, | 
|  | 486 | (void**)&frameDuration, 1); | 
|  | 487 | uint32_t sensitivity = 100; | 
|  | 488 | add_camera_metadata_entry(request, | 
|  | 489 | ANDROID_SENSOR_SENSITIVITY, | 
|  | 490 | (void**)&sensitivity, 1); | 
|  | 491 |  | 
|  | 492 | uint32_t hourOfDay = 12; | 
|  | 493 | add_camera_metadata_entry(request, | 
|  | 494 | 0x80000000, // EMULATOR_HOUROFDAY | 
|  | 495 | &hourOfDay, 1); | 
|  | 496 |  | 
|  | 497 | IF_ALOGV() { | 
|  | 498 | std::cout << "Input request template: " << std::endl; | 
|  | 499 | dump_camera_metadata(request, 0, 1); | 
|  | 500 | } | 
|  | 501 |  | 
|  | 502 | int numCaptures = 10; | 
|  | 503 |  | 
|  | 504 | // Enqueue numCaptures requests with increasing exposure time | 
|  | 505 |  | 
| Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 506 | uint64_t exposureTime = 100 * USEC; | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 507 | for (int reqCount = 0; reqCount < numCaptures; reqCount++ ) { | 
|  | 508 | camera_metadata_t *req; | 
|  | 509 | req = allocate_camera_metadata(20, 2000); | 
|  | 510 | append_camera_metadata(req, request); | 
|  | 511 |  | 
|  | 512 | add_camera_metadata_entry(req, | 
|  | 513 | ANDROID_SENSOR_EXPOSURE_TIME, | 
|  | 514 | (void**)&exposureTime, 1); | 
|  | 515 | exposureTime *= 2; | 
|  | 516 |  | 
|  | 517 | res = mRequests.enqueue(req); | 
|  | 518 | ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " | 
|  | 519 | << strerror(-res); | 
|  | 520 | } | 
|  | 521 |  | 
|  | 522 | // Get frames and image buffers one by one | 
| Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 523 | uint64_t expectedExposureTime = 100 * USEC; | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 524 | for (int frameCount = 0; frameCount < 10; frameCount++) { | 
| Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 525 | res = mFrames.waitForBuffer(SEC + expectedExposureTime); | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 526 | ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res); | 
|  | 527 |  | 
|  | 528 | camera_metadata_t *frame; | 
|  | 529 | res = mFrames.dequeue(&frame); | 
|  | 530 | ASSERT_EQ(NO_ERROR, res); | 
|  | 531 | ASSERT_TRUE(frame != NULL); | 
|  | 532 |  | 
| Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 533 | camera_metadata_entry_t frameNumber; | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 534 | res = find_camera_metadata_entry(frame, | 
|  | 535 | ANDROID_REQUEST_FRAME_COUNT, | 
| Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 536 | &frameNumber); | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 537 | ASSERT_EQ(NO_ERROR, res); | 
| Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 538 | ASSERT_EQ(frameCount, *frameNumber.data.i32); | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 539 |  | 
| Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 540 | res = rawWaiter->waitForFrame(SEC + expectedExposureTime); | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 541 | ASSERT_EQ(NO_ERROR, res) << | 
|  | 542 | "Never got raw data for capture " << frameCount; | 
|  | 543 |  | 
|  | 544 | CpuConsumer::LockedBuffer buffer; | 
|  | 545 | res = rawConsumer->lockNextBuffer(&buffer); | 
|  | 546 | ASSERT_EQ(NO_ERROR, res); | 
|  | 547 |  | 
|  | 548 | IF_ALOGV() { | 
|  | 549 | char dumpname[60]; | 
|  | 550 | snprintf(dumpname, 60, | 
| Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 551 | "/data/local/tmp/camera2_test-" | 
|  | 552 | "captureBurstRaw-dump_%d.raw", | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 553 | frameCount); | 
|  | 554 | ALOGV("Dumping raw buffer to %s", dumpname); | 
|  | 555 | // Write to file | 
|  | 556 | std::ofstream rawFile(dumpname); | 
|  | 557 | for (unsigned int y = 0; y < buffer.height; y++) { | 
|  | 558 | rawFile.write( | 
|  | 559 | (const char *)(buffer.data + y * buffer.stride * 2), | 
|  | 560 | buffer.width * 2); | 
|  | 561 | } | 
|  | 562 | rawFile.close(); | 
|  | 563 | } | 
|  | 564 |  | 
|  | 565 | res = rawConsumer->unlockBuffer(buffer); | 
|  | 566 | ASSERT_EQ(NO_ERROR, res); | 
| Eino-Ville Talvala | e6a3c3c | 2012-05-11 16:18:42 -0700 | [diff] [blame] | 567 |  | 
|  | 568 | expectedExposureTime *= 2; | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 569 | } | 
|  | 570 | } | 
|  | 571 | } | 
|  | 572 |  | 
| Eino-Ville Talvala | 6adfd6b | 2012-05-14 15:25:27 -0700 | [diff] [blame] | 573 | TEST_F(Camera2Test, ConstructDefaultRequests) { | 
|  | 574 | status_t res; | 
|  | 575 |  | 
|  | 576 | for (int id = 0; id < getNumCameras(); id++) { | 
|  | 577 | if (!isHal2Supported(id)) continue; | 
|  | 578 |  | 
|  | 579 | ASSERT_NO_FATAL_FAILURE(setUpCamera(id)); | 
|  | 580 |  | 
|  | 581 | for (int i = CAMERA2_TEMPLATE_PREVIEW; i < CAMERA2_TEMPLATE_COUNT; | 
|  | 582 | i++) { | 
|  | 583 | camera_metadata_t *request = NULL; | 
|  | 584 | res = mDevice->ops->construct_default_request(mDevice, | 
|  | 585 | i, | 
|  | 586 | &request); | 
|  | 587 | EXPECT_EQ(NO_ERROR, res) << | 
|  | 588 | "Unable to construct request from template type %d", i; | 
|  | 589 | EXPECT_TRUE(request != NULL); | 
|  | 590 | EXPECT_LT((size_t)0, get_camera_metadata_entry_count(request)); | 
|  | 591 | EXPECT_LT((size_t)0, get_camera_metadata_data_count(request)); | 
|  | 592 |  | 
|  | 593 | IF_ALOGV() { | 
|  | 594 | std::cout << "  ** Template type " << i << ":"<<std::endl; | 
|  | 595 | dump_camera_metadata(request, 0, 2); | 
|  | 596 | } | 
|  | 597 |  | 
|  | 598 | free_camera_metadata(request); | 
|  | 599 | } | 
|  | 600 | } | 
|  | 601 | } | 
|  | 602 |  | 
| Eino-Ville Talvala | 567b4a2 | 2012-04-23 09:29:38 -0700 | [diff] [blame] | 603 | } // namespace android |