Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | /* |
| 3 | * Copyright (C) 2019, Google Inc. |
| 4 | * |
| 5 | * camera_device.cpp - libcamera Android Camera Device |
| 6 | */ |
| 7 | |
| 8 | #include "camera_device.h" |
| 9 | |
| 10 | #include <system/camera_metadata.h> |
| 11 | |
| 12 | #include "log.h" |
| 13 | |
| 14 | #include "thread_rpc.h" |
| 15 | |
| 16 | using namespace libcamera; |
| 17 | |
| 18 | LOG_DECLARE_CATEGORY(HAL); |
| 19 | |
| 20 | /* |
| 21 | * \struct Camera3RequestDescriptor |
| 22 | * |
| 23 | * A utility structure that groups information about a capture request to be |
| 24 | * later re-used at request complete time to notify the framework. |
| 25 | */ |
| 26 | |
| 27 | CameraDevice::Camera3RequestDescriptor::Camera3RequestDescriptor( |
| 28 | unsigned int frameNumber, unsigned int numBuffers) |
| 29 | : frameNumber(frameNumber), numBuffers(numBuffers) |
| 30 | { |
| 31 | buffers = new camera3_stream_buffer_t[numBuffers]; |
| 32 | } |
| 33 | |
| 34 | CameraDevice::Camera3RequestDescriptor::~Camera3RequestDescriptor() |
| 35 | { |
| 36 | delete[] buffers; |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * \class CameraDevice |
| 41 | * |
| 42 | * The CameraDevice class wraps a libcamera::Camera instance, and implements |
| 43 | * the camera_device_t interface by handling RPC requests received from its |
| 44 | * associated CameraProxy. |
| 45 | * |
| 46 | * It translate parameters and operations from Camera HALv3 API to the libcamera |
| 47 | * ones to provide static information for a Camera, create request templates |
| 48 | * for it, process capture requests and then deliver capture results back |
| 49 | * to the framework using the designated callbacks. |
| 50 | */ |
| 51 | |
Laurent Pinchart | 0ed40d2 | 2019-08-18 01:45:01 +0300 | [diff] [blame] | 52 | CameraDevice::CameraDevice(unsigned int id, const std::shared_ptr<Camera> &camera) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 53 | : running_(false), camera_(camera), staticMetadata_(nullptr), |
| 54 | requestTemplate_(nullptr) |
| 55 | { |
| 56 | camera_->requestCompleted.connect(this, &CameraDevice::requestComplete); |
| 57 | } |
| 58 | |
| 59 | CameraDevice::~CameraDevice() |
| 60 | { |
| 61 | if (staticMetadata_) |
| 62 | free_camera_metadata(staticMetadata_); |
| 63 | staticMetadata_ = nullptr; |
| 64 | |
| 65 | if (requestTemplate_) |
| 66 | free_camera_metadata(requestTemplate_); |
| 67 | requestTemplate_ = nullptr; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Handle RPC request received from the associated proxy. |
| 72 | */ |
Laurent Pinchart | 0c32433 | 2019-08-12 05:30:06 +0300 | [diff] [blame] | 73 | void CameraDevice::call(ThreadRpc *rpc) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 74 | { |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 75 | switch (rpc->tag) { |
| 76 | case ThreadRpc::ProcessCaptureRequest: |
| 77 | processCaptureRequest(rpc->request); |
| 78 | break; |
| 79 | case ThreadRpc::Close: |
| 80 | close(); |
| 81 | break; |
| 82 | default: |
| 83 | LOG(HAL, Error) << "Unknown RPC operation: " << rpc->tag; |
| 84 | } |
| 85 | |
| 86 | rpc->notifyReception(); |
| 87 | } |
| 88 | |
| 89 | int CameraDevice::open() |
| 90 | { |
| 91 | int ret = camera_->acquire(); |
| 92 | if (ret) { |
| 93 | LOG(HAL, Error) << "Failed to acquire the camera"; |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | void CameraDevice::close() |
| 101 | { |
| 102 | camera_->stop(); |
| 103 | |
| 104 | camera_->freeBuffers(); |
| 105 | camera_->release(); |
| 106 | |
| 107 | running_ = false; |
| 108 | } |
| 109 | |
| 110 | void CameraDevice::setCallbacks(const camera3_callback_ops_t *callbacks) |
| 111 | { |
| 112 | callbacks_ = callbacks; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Return static information for the camera. |
| 117 | */ |
| 118 | camera_metadata_t *CameraDevice::getStaticMetadata() |
| 119 | { |
| 120 | int ret; |
| 121 | |
| 122 | if (staticMetadata_) |
| 123 | return staticMetadata_; |
| 124 | |
| 125 | /* |
| 126 | * The here reported metadata are enough to implement a basic capture |
| 127 | * example application, but a real camera implementation will require |
| 128 | * more. |
| 129 | */ |
| 130 | |
Jacopo Mondi | 48504ba | 2019-09-04 16:18:19 +0200 | [diff] [blame] | 131 | /* |
| 132 | * \todo Keep this in sync with the actual number of entries. |
| 133 | * Currently: 46 entries, 390 bytes |
| 134 | */ |
| 135 | staticMetadata_ = allocate_camera_metadata(50, 500); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 136 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 137 | /* Color correction static metadata. */ |
| 138 | std::vector<uint8_t> aberrationModes = { |
| 139 | ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF, |
| 140 | }; |
| 141 | ret = add_camera_metadata_entry(staticMetadata_, |
| 142 | ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES, |
| 143 | aberrationModes.data(), aberrationModes.size()); |
| 144 | METADATA_ASSERT(ret); |
| 145 | |
| 146 | /* Control static metadata. */ |
| 147 | std::vector<uint8_t> aeAvailableAntiBandingModes = { |
| 148 | ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF, |
| 149 | ANDROID_CONTROL_AE_ANTIBANDING_MODE_50HZ, |
| 150 | ANDROID_CONTROL_AE_ANTIBANDING_MODE_60HZ, |
| 151 | ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO, |
| 152 | }; |
| 153 | ret = add_camera_metadata_entry(staticMetadata_, |
| 154 | ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES, |
| 155 | aeAvailableAntiBandingModes.data(), |
| 156 | aeAvailableAntiBandingModes.size()); |
| 157 | METADATA_ASSERT(ret); |
| 158 | |
| 159 | std::vector<uint8_t> aeAvailableModes = { |
| 160 | ANDROID_CONTROL_AE_MODE_ON, |
| 161 | }; |
| 162 | ret = add_camera_metadata_entry(staticMetadata_, |
| 163 | ANDROID_CONTROL_AE_AVAILABLE_MODES, |
| 164 | aeAvailableModes.data(), aeAvailableModes.size()); |
| 165 | METADATA_ASSERT(ret); |
| 166 | |
| 167 | std::vector<int32_t> availableAeFpsTarget = { |
| 168 | 15, 30, |
| 169 | }; |
| 170 | ret = add_camera_metadata_entry(staticMetadata_, |
| 171 | ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, |
| 172 | availableAeFpsTarget.data(), |
| 173 | availableAeFpsTarget.size()); |
| 174 | METADATA_ASSERT(ret); |
| 175 | |
| 176 | std::vector<int32_t> aeCompensationRange = { |
| 177 | 0, 0, |
| 178 | }; |
| 179 | ret = add_camera_metadata_entry(staticMetadata_, |
| 180 | ANDROID_CONTROL_AE_COMPENSATION_RANGE, |
| 181 | aeCompensationRange.data(), |
| 182 | aeCompensationRange.size()); |
| 183 | METADATA_ASSERT(ret); |
| 184 | |
| 185 | const camera_metadata_rational_t aeCompensationStep[] = { |
| 186 | { 0, 1 } |
| 187 | }; |
| 188 | ret = add_camera_metadata_entry(staticMetadata_, |
| 189 | ANDROID_CONTROL_AE_COMPENSATION_STEP, |
| 190 | aeCompensationStep, 1); |
| 191 | METADATA_ASSERT(ret); |
| 192 | |
| 193 | std::vector<uint8_t> availableAfModes = { |
| 194 | ANDROID_CONTROL_AF_MODE_OFF, |
| 195 | }; |
| 196 | ret = add_camera_metadata_entry(staticMetadata_, |
| 197 | ANDROID_CONTROL_AF_AVAILABLE_MODES, |
| 198 | availableAfModes.data(), availableAfModes.size()); |
| 199 | METADATA_ASSERT(ret); |
| 200 | |
| 201 | std::vector<uint8_t> availableEffects = { |
| 202 | ANDROID_CONTROL_EFFECT_MODE_OFF, |
| 203 | }; |
| 204 | ret = add_camera_metadata_entry(staticMetadata_, |
| 205 | ANDROID_CONTROL_AVAILABLE_EFFECTS, |
| 206 | availableEffects.data(), availableEffects.size()); |
| 207 | METADATA_ASSERT(ret); |
| 208 | |
| 209 | std::vector<uint8_t> availableSceneModes = { |
| 210 | ANDROID_CONTROL_SCENE_MODE_DISABLED, |
| 211 | }; |
| 212 | ret = add_camera_metadata_entry(staticMetadata_, |
| 213 | ANDROID_CONTROL_AVAILABLE_SCENE_MODES, |
| 214 | availableSceneModes.data(), availableSceneModes.size()); |
| 215 | METADATA_ASSERT(ret); |
| 216 | |
| 217 | std::vector<uint8_t> availableStabilizationModes = { |
| 218 | ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF, |
| 219 | }; |
| 220 | ret = add_camera_metadata_entry(staticMetadata_, |
| 221 | ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES, |
| 222 | availableStabilizationModes.data(), |
| 223 | availableStabilizationModes.size()); |
| 224 | METADATA_ASSERT(ret); |
| 225 | |
| 226 | std::vector<uint8_t> availableAwbModes = { |
| 227 | ANDROID_CONTROL_AWB_MODE_OFF, |
| 228 | }; |
| 229 | ret = add_camera_metadata_entry(staticMetadata_, |
| 230 | ANDROID_CONTROL_AWB_AVAILABLE_MODES, |
| 231 | availableAwbModes.data(), availableAwbModes.size()); |
| 232 | METADATA_ASSERT(ret); |
| 233 | |
| 234 | std::vector<int32_t> availableMaxRegions = { |
| 235 | 0, 0, 0, |
| 236 | }; |
| 237 | ret = add_camera_metadata_entry(staticMetadata_, |
| 238 | ANDROID_CONTROL_MAX_REGIONS, |
| 239 | availableMaxRegions.data(), availableMaxRegions.size()); |
| 240 | METADATA_ASSERT(ret); |
| 241 | |
| 242 | std::vector<uint8_t> sceneModesOverride = { |
| 243 | ANDROID_CONTROL_AE_MODE_ON, |
| 244 | ANDROID_CONTROL_AWB_MODE_AUTO, |
| 245 | ANDROID_CONTROL_AF_MODE_AUTO, |
| 246 | }; |
| 247 | ret = add_camera_metadata_entry(staticMetadata_, |
| 248 | ANDROID_CONTROL_SCENE_MODE_OVERRIDES, |
| 249 | sceneModesOverride.data(), sceneModesOverride.size()); |
| 250 | METADATA_ASSERT(ret); |
| 251 | |
| 252 | uint8_t aeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE; |
| 253 | ret = add_camera_metadata_entry(staticMetadata_, |
| 254 | ANDROID_CONTROL_AE_LOCK_AVAILABLE, |
| 255 | &aeLockAvailable, 1); |
| 256 | METADATA_ASSERT(ret); |
| 257 | |
| 258 | uint8_t awbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE; |
| 259 | ret = add_camera_metadata_entry(staticMetadata_, |
| 260 | ANDROID_CONTROL_AWB_LOCK_AVAILABLE, |
| 261 | &awbLockAvailable, 1); |
| 262 | METADATA_ASSERT(ret); |
| 263 | |
| 264 | char availableControlModes = ANDROID_CONTROL_MODE_AUTO; |
| 265 | ret = add_camera_metadata_entry(staticMetadata_, |
| 266 | ANDROID_CONTROL_AVAILABLE_MODES, |
| 267 | &availableControlModes, 1); |
| 268 | METADATA_ASSERT(ret); |
| 269 | |
| 270 | /* JPEG static metadata. */ |
| 271 | std::vector<int32_t> availableThumbnailSizes = { |
| 272 | 0, 0, |
| 273 | }; |
| 274 | ret = add_camera_metadata_entry(staticMetadata_, |
| 275 | ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, |
| 276 | availableThumbnailSizes.data(), |
| 277 | availableThumbnailSizes.size()); |
| 278 | METADATA_ASSERT(ret); |
| 279 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 280 | /* Sensor static metadata. */ |
| 281 | int32_t pixelArraySize[] = { |
| 282 | 2592, 1944, |
| 283 | }; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 284 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 285 | ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE, |
| 286 | &pixelArraySize, 2); |
| 287 | METADATA_ASSERT(ret); |
| 288 | |
| 289 | int32_t sensorSizes[] = { |
| 290 | 0, 0, 2560, 1920, |
| 291 | }; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 292 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 293 | ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, |
| 294 | &sensorSizes, 4); |
| 295 | METADATA_ASSERT(ret); |
| 296 | |
| 297 | int32_t sensitivityRange[] = { |
| 298 | 32, 2400, |
| 299 | }; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 300 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 301 | ANDROID_SENSOR_INFO_SENSITIVITY_RANGE, |
| 302 | &sensitivityRange, 2); |
| 303 | METADATA_ASSERT(ret); |
| 304 | |
| 305 | uint16_t filterArr = ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT_GRBG; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 306 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 307 | ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT, |
| 308 | &filterArr, 1); |
| 309 | METADATA_ASSERT(ret); |
| 310 | |
| 311 | int64_t exposureTimeRange[] = { |
| 312 | 100000, 200000000, |
| 313 | }; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 314 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 315 | ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE, |
| 316 | &exposureTimeRange, 2); |
| 317 | METADATA_ASSERT(ret); |
| 318 | |
| 319 | int32_t orientation = 0; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 320 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 321 | ANDROID_SENSOR_ORIENTATION, |
| 322 | &orientation, 1); |
| 323 | METADATA_ASSERT(ret); |
| 324 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 325 | std::vector<int32_t> testPatterModes = { |
| 326 | ANDROID_SENSOR_TEST_PATTERN_MODE_OFF, |
| 327 | }; |
| 328 | ret = add_camera_metadata_entry(staticMetadata_, |
| 329 | ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES, |
| 330 | testPatterModes.data(), testPatterModes.size()); |
| 331 | METADATA_ASSERT(ret); |
| 332 | |
| 333 | std::vector<float> physicalSize = { |
| 334 | 2592, 1944, |
| 335 | }; |
| 336 | ret = add_camera_metadata_entry(staticMetadata_, |
| 337 | ANDROID_SENSOR_INFO_PHYSICAL_SIZE, |
| 338 | physicalSize.data(), physicalSize.size()); |
| 339 | METADATA_ASSERT(ret); |
| 340 | |
| 341 | uint8_t timestampSource = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN; |
| 342 | ret = add_camera_metadata_entry(staticMetadata_, |
| 343 | ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE, |
| 344 | ×tampSource, 1); |
| 345 | METADATA_ASSERT(ret); |
| 346 | |
| 347 | /* Statistics static metadata. */ |
| 348 | uint8_t faceDetectMode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF; |
| 349 | ret = add_camera_metadata_entry(staticMetadata_, |
| 350 | ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES, |
| 351 | &faceDetectMode, 1); |
| 352 | METADATA_ASSERT(ret); |
| 353 | |
| 354 | int32_t maxFaceCount = 0; |
| 355 | ret = add_camera_metadata_entry(staticMetadata_, |
| 356 | ANDROID_STATISTICS_INFO_MAX_FACE_COUNT, |
| 357 | &maxFaceCount, 1); |
| 358 | METADATA_ASSERT(ret); |
| 359 | |
| 360 | /* Sync static metadata. */ |
| 361 | int32_t maxLatency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN; |
| 362 | ret = add_camera_metadata_entry(staticMetadata_, |
| 363 | ANDROID_SYNC_MAX_LATENCY, &maxLatency, 1); |
| 364 | METADATA_ASSERT(ret); |
| 365 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 366 | /* Flash static metadata. */ |
| 367 | char flashAvailable = ANDROID_FLASH_INFO_AVAILABLE_FALSE; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 368 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 369 | ANDROID_FLASH_INFO_AVAILABLE, |
| 370 | &flashAvailable, 1); |
| 371 | METADATA_ASSERT(ret); |
| 372 | |
| 373 | /* Lens static metadata. */ |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 374 | std::vector<float> lensApertures = { |
| 375 | 2.53 / 100, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 376 | }; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 377 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 378 | ANDROID_LENS_INFO_AVAILABLE_APERTURES, |
| 379 | lensApertures.data(), lensApertures.size()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 380 | METADATA_ASSERT(ret); |
| 381 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 382 | uint8_t lensFacing = ANDROID_LENS_FACING_FRONT; |
| 383 | ret = add_camera_metadata_entry(staticMetadata_, |
| 384 | ANDROID_LENS_FACING, &lensFacing, 1); |
| 385 | METADATA_ASSERT(ret); |
| 386 | |
| 387 | std::vector<float> lensFocalLenghts = { |
| 388 | 1, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 389 | }; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 390 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 391 | ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS, |
| 392 | lensFocalLenghts.data(), |
| 393 | lensFocalLenghts.size()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 394 | METADATA_ASSERT(ret); |
| 395 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 396 | std::vector<uint8_t> opticalStabilizations = { |
| 397 | ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF, |
| 398 | }; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 399 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 400 | ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION, |
| 401 | opticalStabilizations.data(), |
| 402 | opticalStabilizations.size()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 403 | METADATA_ASSERT(ret); |
| 404 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 405 | float hypeFocalDistance = 0; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 406 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 407 | ANDROID_LENS_INFO_HYPERFOCAL_DISTANCE, |
| 408 | &hypeFocalDistance, 1); |
| 409 | METADATA_ASSERT(ret); |
| 410 | |
| 411 | float minFocusDistance = 0; |
| 412 | ret = add_camera_metadata_entry(staticMetadata_, |
| 413 | ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE, |
| 414 | &minFocusDistance, 1); |
| 415 | METADATA_ASSERT(ret); |
| 416 | |
| 417 | /* Noise reduction modes. */ |
| 418 | uint8_t noiseReductionModes = ANDROID_NOISE_REDUCTION_MODE_OFF; |
| 419 | ret = add_camera_metadata_entry(staticMetadata_, |
| 420 | ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES, |
| 421 | &noiseReductionModes, 1); |
| 422 | METADATA_ASSERT(ret); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 423 | |
| 424 | /* Scaler static metadata. */ |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 425 | float maxDigitalZoom = 1; |
| 426 | ret = add_camera_metadata_entry(staticMetadata_, |
| 427 | ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM, |
| 428 | &maxDigitalZoom, 1); |
| 429 | METADATA_ASSERT(ret); |
| 430 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 431 | std::vector<uint32_t> availableStreamFormats = { |
| 432 | ANDROID_SCALER_AVAILABLE_FORMATS_BLOB, |
| 433 | ANDROID_SCALER_AVAILABLE_FORMATS_YCbCr_420_888, |
| 434 | ANDROID_SCALER_AVAILABLE_FORMATS_IMPLEMENTATION_DEFINED, |
| 435 | }; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 436 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 437 | ANDROID_SCALER_AVAILABLE_FORMATS, |
| 438 | availableStreamFormats.data(), |
| 439 | availableStreamFormats.size()); |
| 440 | METADATA_ASSERT(ret); |
| 441 | |
| 442 | std::vector<uint32_t> availableStreamConfigurations = { |
| 443 | ANDROID_SCALER_AVAILABLE_FORMATS_BLOB, 2560, 1920, |
| 444 | ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT, |
| 445 | ANDROID_SCALER_AVAILABLE_FORMATS_YCbCr_420_888, 2560, 1920, |
| 446 | ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT, |
| 447 | ANDROID_SCALER_AVAILABLE_FORMATS_IMPLEMENTATION_DEFINED, 2560, 1920, |
| 448 | ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT, |
| 449 | }; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 450 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 451 | ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, |
| 452 | availableStreamConfigurations.data(), |
| 453 | availableStreamConfigurations.size()); |
| 454 | METADATA_ASSERT(ret); |
| 455 | |
| 456 | std::vector<int64_t> availableStallDurations = { |
| 457 | ANDROID_SCALER_AVAILABLE_FORMATS_BLOB, 2560, 1920, 33333333, |
| 458 | }; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 459 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 460 | ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, |
| 461 | availableStallDurations.data(), |
| 462 | availableStallDurations.size()); |
| 463 | METADATA_ASSERT(ret); |
| 464 | |
| 465 | std::vector<int64_t> minFrameDurations = { |
| 466 | ANDROID_SCALER_AVAILABLE_FORMATS_BLOB, 2560, 1920, 33333333, |
| 467 | ANDROID_SCALER_AVAILABLE_FORMATS_IMPLEMENTATION_DEFINED, 2560, 1920, 33333333, |
| 468 | ANDROID_SCALER_AVAILABLE_FORMATS_YCbCr_420_888, 2560, 1920, 33333333, |
| 469 | }; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 470 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 471 | ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, |
| 472 | minFrameDurations.data(), minFrameDurations.size()); |
| 473 | METADATA_ASSERT(ret); |
| 474 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 475 | uint8_t croppingType = ANDROID_SCALER_CROPPING_TYPE_CENTER_ONLY; |
| 476 | ret = add_camera_metadata_entry(staticMetadata_, |
| 477 | ANDROID_SCALER_CROPPING_TYPE, &croppingType, 1); |
| 478 | METADATA_ASSERT(ret); |
| 479 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 480 | /* Info static metadata. */ |
| 481 | uint8_t supportedHWLevel = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED; |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 482 | ret = add_camera_metadata_entry(staticMetadata_, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 483 | ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL, |
| 484 | &supportedHWLevel, 1); |
| 485 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 486 | /* Request static metadata. */ |
| 487 | int32_t partialResultCount = 1; |
| 488 | ret = add_camera_metadata_entry(staticMetadata_, |
| 489 | ANDROID_REQUEST_PARTIAL_RESULT_COUNT, |
| 490 | &partialResultCount, 1); |
| 491 | METADATA_ASSERT(ret); |
| 492 | |
| 493 | uint8_t maxPipelineDepth = 2; |
| 494 | ret = add_camera_metadata_entry(staticMetadata_, |
| 495 | ANDROID_REQUEST_PIPELINE_MAX_DEPTH, |
| 496 | &maxPipelineDepth, 1); |
| 497 | METADATA_ASSERT(ret); |
| 498 | |
| 499 | std::vector<uint8_t> availableCapabilities = { |
| 500 | ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE, |
| 501 | }; |
| 502 | ret = add_camera_metadata_entry(staticMetadata_, |
| 503 | ANDROID_REQUEST_AVAILABLE_CAPABILITIES, |
| 504 | availableCapabilities.data(), |
| 505 | availableCapabilities.size()); |
| 506 | METADATA_ASSERT(ret); |
| 507 | |
Laurent Pinchart | 82bdcc9 | 2019-08-18 03:49:58 +0300 | [diff] [blame] | 508 | return staticMetadata_; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /* |
| 512 | * Produce a metadata pack to be used as template for a capture request. |
| 513 | */ |
| 514 | const camera_metadata_t *CameraDevice::constructDefaultRequestSettings(int type) |
| 515 | { |
| 516 | int ret; |
| 517 | |
| 518 | /* |
| 519 | * \todo Inspect type and pick the right metadata pack. |
| 520 | * As of now just use a single one for all templates. |
| 521 | */ |
| 522 | uint8_t captureIntent; |
| 523 | switch (type) { |
| 524 | case CAMERA3_TEMPLATE_PREVIEW: |
| 525 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW; |
| 526 | break; |
| 527 | case CAMERA3_TEMPLATE_STILL_CAPTURE: |
| 528 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE; |
| 529 | break; |
| 530 | case CAMERA3_TEMPLATE_VIDEO_RECORD: |
| 531 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD; |
| 532 | break; |
| 533 | case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT: |
| 534 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT; |
| 535 | break; |
| 536 | case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: |
| 537 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG; |
| 538 | break; |
| 539 | case CAMERA3_TEMPLATE_MANUAL: |
| 540 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_MANUAL; |
| 541 | break; |
| 542 | default: |
| 543 | LOG(HAL, Error) << "Invalid template request type: " << type; |
| 544 | return nullptr; |
| 545 | } |
| 546 | |
| 547 | if (requestTemplate_) |
| 548 | return requestTemplate_; |
| 549 | |
| 550 | /* \todo Use correct sizes */ |
| 551 | #define REQUEST_TEMPLATE_ENTRIES 30 |
| 552 | #define REQUEST_TEMPLATE_DATA 2048 |
| 553 | requestTemplate_ = allocate_camera_metadata(REQUEST_TEMPLATE_ENTRIES, |
| 554 | REQUEST_TEMPLATE_DATA); |
| 555 | if (!requestTemplate_) { |
| 556 | LOG(HAL, Error) << "Failed to allocate template metadata"; |
| 557 | return nullptr; |
| 558 | } |
| 559 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 560 | uint8_t aeMode = ANDROID_CONTROL_AE_MODE_ON; |
| 561 | ret = add_camera_metadata_entry(requestTemplate_, |
| 562 | ANDROID_CONTROL_AE_MODE, |
| 563 | &aeMode, 1); |
| 564 | METADATA_ASSERT(ret); |
| 565 | |
| 566 | int32_t aeExposureCompensation = 0; |
| 567 | ret = add_camera_metadata_entry(requestTemplate_, |
| 568 | ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION, |
| 569 | &aeExposureCompensation, 1); |
| 570 | METADATA_ASSERT(ret); |
| 571 | |
| 572 | uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE; |
| 573 | ret = add_camera_metadata_entry(requestTemplate_, |
| 574 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, |
| 575 | &aePrecaptureTrigger, 1); |
| 576 | METADATA_ASSERT(ret); |
| 577 | |
| 578 | uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_OFF; |
| 579 | ret = add_camera_metadata_entry(requestTemplate_, |
| 580 | ANDROID_CONTROL_AE_LOCK, |
| 581 | &aeLock, 1); |
| 582 | METADATA_ASSERT(ret); |
| 583 | |
| 584 | uint8_t afTrigger = ANDROID_CONTROL_AF_TRIGGER_IDLE; |
| 585 | ret = add_camera_metadata_entry(requestTemplate_, |
| 586 | ANDROID_CONTROL_AF_TRIGGER, |
| 587 | &afTrigger, 1); |
| 588 | METADATA_ASSERT(ret); |
| 589 | |
| 590 | uint8_t awbMode = ANDROID_CONTROL_AWB_MODE_AUTO; |
| 591 | ret = add_camera_metadata_entry(requestTemplate_, |
| 592 | ANDROID_CONTROL_AWB_MODE, |
| 593 | &awbMode, 1); |
| 594 | METADATA_ASSERT(ret); |
| 595 | |
| 596 | uint8_t awbLock = ANDROID_CONTROL_AWB_LOCK_OFF; |
| 597 | ret = add_camera_metadata_entry(requestTemplate_, |
| 598 | ANDROID_CONTROL_AWB_LOCK, |
| 599 | &awbLock, 1); |
| 600 | METADATA_ASSERT(ret); |
| 601 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 602 | uint8_t flashMode = ANDROID_FLASH_MODE_OFF; |
| 603 | ret = add_camera_metadata_entry(requestTemplate_, |
| 604 | ANDROID_FLASH_MODE, |
| 605 | &flashMode, 1); |
| 606 | METADATA_ASSERT(ret); |
| 607 | |
| 608 | uint8_t faceDetectMode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF; |
| 609 | ret = add_camera_metadata_entry(requestTemplate_, |
| 610 | ANDROID_STATISTICS_FACE_DETECT_MODE, |
| 611 | &faceDetectMode, 1); |
| 612 | METADATA_ASSERT(ret); |
| 613 | |
Jacopo Mondi | 9b361dc | 2019-09-04 16:18:21 +0200 | [diff] [blame^] | 614 | uint8_t noiseReduction = ANDROID_NOISE_REDUCTION_MODE_OFF; |
| 615 | ret = add_camera_metadata_entry(requestTemplate_, |
| 616 | ANDROID_NOISE_REDUCTION_MODE, &noiseReduction, 1); |
| 617 | METADATA_ASSERT(ret); |
| 618 | |
| 619 | uint8_t aberrationMode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF; |
| 620 | ret = add_camera_metadata_entry(requestTemplate_, |
| 621 | ANDROID_COLOR_CORRECTION_ABERRATION_MODE, |
| 622 | &aberrationMode, 1); |
| 623 | METADATA_ASSERT(ret); |
| 624 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 625 | ret = add_camera_metadata_entry(requestTemplate_, |
| 626 | ANDROID_CONTROL_CAPTURE_INTENT, |
| 627 | &captureIntent, 1); |
| 628 | METADATA_ASSERT(ret); |
| 629 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 630 | return requestTemplate_; |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * Inspect the stream_list to produce a list of StreamConfiguration to |
| 635 | * be use to configure the Camera. |
| 636 | */ |
| 637 | int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) |
| 638 | { |
| 639 | for (unsigned int i = 0; i < stream_list->num_streams; ++i) { |
| 640 | camera3_stream_t *stream = stream_list->streams[i]; |
| 641 | |
| 642 | LOG(HAL, Info) << "Stream #" << i |
| 643 | << ", direction: " << stream->stream_type |
| 644 | << ", width: " << stream->width |
| 645 | << ", height: " << stream->height |
| 646 | << ", format: " << std::hex << stream->format; |
| 647 | } |
| 648 | |
| 649 | /* Hardcode viewfinder role, collecting sizes from the stream config. */ |
| 650 | if (stream_list->num_streams != 1) { |
| 651 | LOG(HAL, Error) << "Only one stream supported"; |
| 652 | return -EINVAL; |
| 653 | } |
| 654 | |
| 655 | StreamRoles roles = { StreamRole::Viewfinder }; |
| 656 | config_ = camera_->generateConfiguration(roles); |
| 657 | if (!config_ || config_->empty()) { |
| 658 | LOG(HAL, Error) << "Failed to generate camera configuration"; |
| 659 | return -EINVAL; |
| 660 | } |
| 661 | |
| 662 | /* Only one stream is supported. */ |
| 663 | camera3_stream_t *camera3Stream = stream_list->streams[0]; |
| 664 | StreamConfiguration *streamConfiguration = &config_->at(0); |
| 665 | streamConfiguration->size.width = camera3Stream->width; |
| 666 | streamConfiguration->size.height = camera3Stream->height; |
| 667 | streamConfiguration->memoryType = ExternalMemory; |
| 668 | |
| 669 | /* |
| 670 | * \todo We'll need to translate from Android defined pixel format codes |
| 671 | * to the libcamera image format codes. For now, do not change the |
| 672 | * format returned from Camera::generateConfiguration(). |
| 673 | */ |
| 674 | |
| 675 | switch (config_->validate()) { |
| 676 | case CameraConfiguration::Valid: |
| 677 | break; |
| 678 | case CameraConfiguration::Adjusted: |
| 679 | LOG(HAL, Info) << "Camera configuration adjusted"; |
| 680 | config_.reset(); |
| 681 | return -EINVAL; |
| 682 | case CameraConfiguration::Invalid: |
| 683 | LOG(HAL, Info) << "Camera configuration invalid"; |
| 684 | config_.reset(); |
| 685 | return -EINVAL; |
| 686 | } |
| 687 | |
| 688 | camera3Stream->max_buffers = streamConfiguration->bufferCount; |
| 689 | |
| 690 | /* |
| 691 | * Once the CameraConfiguration has been adjusted/validated |
| 692 | * it can be applied to the camera. |
| 693 | */ |
| 694 | int ret = camera_->configure(config_.get()); |
| 695 | if (ret) { |
| 696 | LOG(HAL, Error) << "Failed to configure camera '" |
| 697 | << camera_->name() << "'"; |
| 698 | return ret; |
| 699 | } |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Request) |
| 705 | { |
| 706 | StreamConfiguration *streamConfiguration = &config_->at(0); |
| 707 | Stream *stream = streamConfiguration->stream(); |
| 708 | |
| 709 | if (camera3Request->num_output_buffers != 1) { |
| 710 | LOG(HAL, Error) << "Invalid number of output buffers: " |
| 711 | << camera3Request->num_output_buffers; |
| 712 | return -EINVAL; |
| 713 | } |
| 714 | |
| 715 | /* Start the camera if that's the first request we handle. */ |
| 716 | if (!running_) { |
| 717 | int ret = camera_->allocateBuffers(); |
| 718 | if (ret) { |
| 719 | LOG(HAL, Error) << "Failed to allocate buffers"; |
| 720 | return ret; |
| 721 | } |
| 722 | |
| 723 | ret = camera_->start(); |
| 724 | if (ret) { |
| 725 | LOG(HAL, Error) << "Failed to start camera"; |
| 726 | camera_->freeBuffers(); |
| 727 | return ret; |
| 728 | } |
| 729 | |
| 730 | running_ = true; |
| 731 | } |
| 732 | |
| 733 | /* |
| 734 | * Queue a request for the Camera with the provided dmabuf file |
| 735 | * descriptors. |
| 736 | */ |
| 737 | const camera3_stream_buffer_t *camera3Buffers = |
| 738 | camera3Request->output_buffers; |
| 739 | |
| 740 | /* |
| 741 | * Save the request descriptors for use at completion time. |
| 742 | * The descriptor and the associated memory reserved here are freed |
| 743 | * at request complete time. |
| 744 | */ |
| 745 | Camera3RequestDescriptor *descriptor = |
| 746 | new Camera3RequestDescriptor(camera3Request->frame_number, |
| 747 | camera3Request->num_output_buffers); |
| 748 | for (unsigned int i = 0; i < descriptor->numBuffers; ++i) { |
| 749 | /* |
| 750 | * Keep track of which stream the request belongs to and store |
| 751 | * the native buffer handles. |
| 752 | * |
| 753 | * \todo Currently we only support one capture buffer. Copy |
| 754 | * all of them to be ready once we'll support more. |
| 755 | */ |
| 756 | descriptor->buffers[i].stream = camera3Buffers[i].stream; |
| 757 | descriptor->buffers[i].buffer = camera3Buffers[i].buffer; |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * Create a libcamera buffer using the dmabuf descriptors of the first |
| 762 | * and (currently) only supported request buffer. |
| 763 | */ |
| 764 | const buffer_handle_t camera3Handle = *camera3Buffers[0].buffer; |
| 765 | std::array<int, 3> fds = { |
| 766 | camera3Handle->data[0], |
| 767 | camera3Handle->data[1], |
| 768 | camera3Handle->data[2], |
| 769 | }; |
| 770 | |
| 771 | std::unique_ptr<Buffer> buffer = stream->createBuffer(fds); |
| 772 | if (!buffer) { |
| 773 | LOG(HAL, Error) << "Failed to create buffer"; |
| 774 | delete descriptor; |
| 775 | return -EINVAL; |
| 776 | } |
| 777 | |
| 778 | Request *request = |
| 779 | camera_->createRequest(reinterpret_cast<uint64_t>(descriptor)); |
| 780 | request->addBuffer(std::move(buffer)); |
| 781 | |
| 782 | int ret = camera_->queueRequest(request); |
| 783 | if (ret) { |
| 784 | LOG(HAL, Error) << "Failed to queue request"; |
| 785 | goto error; |
| 786 | } |
| 787 | |
| 788 | return 0; |
| 789 | |
| 790 | error: |
| 791 | delete request; |
| 792 | delete descriptor; |
| 793 | |
| 794 | return ret; |
| 795 | } |
| 796 | |
| 797 | void CameraDevice::requestComplete(Request *request, |
| 798 | const std::map<Stream *, Buffer *> &buffers) |
| 799 | { |
| 800 | Buffer *libcameraBuffer = buffers.begin()->second; |
| 801 | camera3_buffer_status status = CAMERA3_BUFFER_STATUS_OK; |
| 802 | camera_metadata_t *resultMetadata = nullptr; |
| 803 | |
| 804 | if (request->status() != Request::RequestComplete) { |
| 805 | LOG(HAL, Error) << "Request not succesfully completed: " |
| 806 | << request->status(); |
| 807 | status = CAMERA3_BUFFER_STATUS_ERROR; |
| 808 | } |
| 809 | |
| 810 | /* Prepare to call back the Android camera stack. */ |
| 811 | Camera3RequestDescriptor *descriptor = |
| 812 | reinterpret_cast<Camera3RequestDescriptor *>(request->cookie()); |
| 813 | |
| 814 | camera3_capture_result_t captureResult = {}; |
| 815 | captureResult.frame_number = descriptor->frameNumber; |
| 816 | captureResult.num_output_buffers = descriptor->numBuffers; |
| 817 | for (unsigned int i = 0; i < descriptor->numBuffers; ++i) { |
| 818 | /* |
| 819 | * \todo Currently we only support one capture buffer. Prepare |
| 820 | * all of them to be ready once we'll support more. |
| 821 | */ |
| 822 | descriptor->buffers[i].acquire_fence = -1; |
| 823 | descriptor->buffers[i].release_fence = -1; |
| 824 | descriptor->buffers[i].status = status; |
| 825 | } |
| 826 | captureResult.output_buffers = |
| 827 | const_cast<const camera3_stream_buffer_t *>(descriptor->buffers); |
| 828 | |
| 829 | if (status == CAMERA3_BUFFER_STATUS_ERROR) { |
| 830 | /* \todo Improve error handling. */ |
| 831 | notifyError(descriptor->frameNumber, |
| 832 | descriptor->buffers[0].stream); |
| 833 | } else { |
| 834 | notifyShutter(descriptor->frameNumber, |
| 835 | libcameraBuffer->timestamp()); |
| 836 | |
| 837 | captureResult.partial_result = 1; |
| 838 | resultMetadata = getResultMetadata(descriptor->frameNumber, |
| 839 | libcameraBuffer->timestamp()); |
| 840 | captureResult.result = resultMetadata; |
| 841 | } |
| 842 | |
| 843 | callbacks_->process_capture_result(callbacks_, &captureResult); |
| 844 | |
| 845 | delete descriptor; |
| 846 | if (resultMetadata) |
| 847 | free_camera_metadata(resultMetadata); |
| 848 | |
| 849 | return; |
| 850 | } |
| 851 | |
| 852 | void CameraDevice::notifyShutter(uint32_t frameNumber, uint64_t timestamp) |
| 853 | { |
| 854 | camera3_notify_msg_t notify = {}; |
| 855 | |
| 856 | notify.type = CAMERA3_MSG_SHUTTER; |
| 857 | notify.message.shutter.frame_number = frameNumber; |
| 858 | notify.message.shutter.timestamp = timestamp; |
| 859 | |
| 860 | callbacks_->notify(callbacks_, ¬ify); |
| 861 | } |
| 862 | |
| 863 | void CameraDevice::notifyError(uint32_t frameNumber, camera3_stream_t *stream) |
| 864 | { |
| 865 | camera3_notify_msg_t notify = {}; |
| 866 | |
| 867 | notify.type = CAMERA3_MSG_ERROR; |
| 868 | notify.message.error.error_stream = stream; |
| 869 | notify.message.error.frame_number = frameNumber; |
| 870 | notify.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST; |
| 871 | |
| 872 | callbacks_->notify(callbacks_, ¬ify); |
| 873 | } |
| 874 | |
| 875 | /* |
| 876 | * Produce a set of fixed result metadata. |
| 877 | */ |
| 878 | camera_metadata_t *CameraDevice::getResultMetadata(int frame_number, |
| 879 | int64_t timestamp) |
| 880 | { |
| 881 | int ret; |
| 882 | |
Jacopo Mondi | 48504ba | 2019-09-04 16:18:19 +0200 | [diff] [blame] | 883 | /* |
| 884 | * \todo Keep this in sync with the actual number of entries. |
| 885 | * Currently: 13 entries, 36 bytes |
| 886 | */ |
| 887 | camera_metadata_t *resultMetadata = allocate_camera_metadata(15, 50); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 888 | |
| 889 | const uint8_t ae_state = ANDROID_CONTROL_AE_STATE_CONVERGED; |
| 890 | ret = add_camera_metadata_entry(resultMetadata, ANDROID_CONTROL_AE_STATE, |
| 891 | &ae_state, 1); |
| 892 | METADATA_ASSERT(ret); |
| 893 | |
| 894 | const uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF; |
| 895 | ret = add_camera_metadata_entry(resultMetadata, ANDROID_CONTROL_AE_LOCK, |
| 896 | &ae_lock, 1); |
| 897 | METADATA_ASSERT(ret); |
| 898 | |
| 899 | uint8_t af_state = ANDROID_CONTROL_AF_STATE_INACTIVE; |
| 900 | ret = add_camera_metadata_entry(resultMetadata, ANDROID_CONTROL_AF_STATE, |
| 901 | &af_state, 1); |
| 902 | METADATA_ASSERT(ret); |
| 903 | |
| 904 | const uint8_t awb_state = ANDROID_CONTROL_AWB_STATE_CONVERGED; |
| 905 | ret = add_camera_metadata_entry(resultMetadata, |
| 906 | ANDROID_CONTROL_AWB_STATE, |
| 907 | &awb_state, 1); |
| 908 | METADATA_ASSERT(ret); |
| 909 | |
| 910 | const uint8_t awb_lock = ANDROID_CONTROL_AWB_LOCK_OFF; |
| 911 | ret = add_camera_metadata_entry(resultMetadata, |
| 912 | ANDROID_CONTROL_AWB_LOCK, |
| 913 | &awb_lock, 1); |
| 914 | METADATA_ASSERT(ret); |
| 915 | |
| 916 | const uint8_t lens_state = ANDROID_LENS_STATE_STATIONARY; |
| 917 | ret = add_camera_metadata_entry(resultMetadata, |
| 918 | ANDROID_LENS_STATE, |
| 919 | &lens_state, 1); |
| 920 | METADATA_ASSERT(ret); |
| 921 | |
| 922 | int32_t sensorSizes[] = { |
| 923 | 0, 0, 2560, 1920, |
| 924 | }; |
| 925 | ret = add_camera_metadata_entry(resultMetadata, |
| 926 | ANDROID_SCALER_CROP_REGION, |
| 927 | sensorSizes, 4); |
| 928 | METADATA_ASSERT(ret); |
| 929 | |
| 930 | ret = add_camera_metadata_entry(resultMetadata, |
| 931 | ANDROID_SENSOR_TIMESTAMP, |
| 932 | ×tamp, 1); |
| 933 | METADATA_ASSERT(ret); |
| 934 | |
| 935 | /* 33.3 msec */ |
| 936 | const int64_t rolling_shutter_skew = 33300000; |
| 937 | ret = add_camera_metadata_entry(resultMetadata, |
| 938 | ANDROID_SENSOR_ROLLING_SHUTTER_SKEW, |
| 939 | &rolling_shutter_skew, 1); |
| 940 | METADATA_ASSERT(ret); |
| 941 | |
| 942 | /* 16.6 msec */ |
| 943 | const int64_t exposure_time = 16600000; |
| 944 | ret = add_camera_metadata_entry(resultMetadata, |
| 945 | ANDROID_SENSOR_EXPOSURE_TIME, |
| 946 | &exposure_time, 1); |
| 947 | METADATA_ASSERT(ret); |
| 948 | |
| 949 | const uint8_t lens_shading_map_mode = |
| 950 | ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF; |
| 951 | ret = add_camera_metadata_entry(resultMetadata, |
| 952 | ANDROID_STATISTICS_LENS_SHADING_MAP_MODE, |
| 953 | &lens_shading_map_mode, 1); |
| 954 | METADATA_ASSERT(ret); |
| 955 | |
| 956 | const uint8_t scene_flicker = ANDROID_STATISTICS_SCENE_FLICKER_NONE; |
| 957 | ret = add_camera_metadata_entry(resultMetadata, |
| 958 | ANDROID_STATISTICS_SCENE_FLICKER, |
| 959 | &scene_flicker, 1); |
| 960 | METADATA_ASSERT(ret); |
| 961 | |
| 962 | return resultMetadata; |
| 963 | } |