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