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" |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 9 | #include "camera_ops.h" |
Umang Jain | b2b8c4d | 2020-10-16 11:07:54 +0530 | [diff] [blame] | 10 | #include "post_processor.h" |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 11 | |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 12 | #include <sys/mman.h> |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 13 | #include <tuple> |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Jacopo Mondi | 5d91c8d | 2020-09-23 09:41:52 +0200 | [diff] [blame^] | 16 | #include <libcamera/control_ids.h> |
Jacopo Mondi | 857a216 | 2019-11-20 17:00:49 +0100 | [diff] [blame] | 17 | #include <libcamera/controls.h> |
Laurent Pinchart | 8b7e073 | 2020-05-22 04:02:06 +0300 | [diff] [blame] | 18 | #include <libcamera/formats.h> |
Jacopo Mondi | 857a216 | 2019-11-20 17:00:49 +0100 | [diff] [blame] | 19 | #include <libcamera/property_ids.h> |
| 20 | |
Niklas Söderlund | 7876d63 | 2020-07-21 00:16:24 +0200 | [diff] [blame] | 21 | #include "libcamera/internal/formats.h" |
Laurent Pinchart | 93e72b6 | 2020-05-12 00:58:34 +0300 | [diff] [blame] | 22 | #include "libcamera/internal/log.h" |
| 23 | #include "libcamera/internal/utils.h" |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 24 | |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 25 | #include "camera_metadata.h" |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 26 | #include "system/graphics.h" |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 27 | |
| 28 | using namespace libcamera; |
| 29 | |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | /* |
| 33 | * \var camera3Resolutions |
| 34 | * \brief The list of image resolutions defined as mandatory to be supported by |
| 35 | * the Android Camera3 specification |
| 36 | */ |
| 37 | const std::vector<Size> camera3Resolutions = { |
| 38 | { 320, 240 }, |
| 39 | { 640, 480 }, |
| 40 | { 1280, 720 }, |
| 41 | { 1920, 1080 } |
| 42 | }; |
| 43 | |
| 44 | /* |
| 45 | * \struct Camera3Format |
| 46 | * \brief Data associated with an Android format identifier |
| 47 | * \var libcameraFormats List of libcamera pixel formats compatible with the |
| 48 | * Android format |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 49 | * \var name The human-readable representation of the Android format code |
| 50 | */ |
| 51 | struct Camera3Format { |
| 52 | std::vector<PixelFormat> libcameraFormats; |
Niklas Söderlund | 8c1fedc | 2020-07-28 19:43:12 +0200 | [diff] [blame] | 53 | bool mandatory; |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 54 | const char *name; |
| 55 | }; |
| 56 | |
| 57 | /* |
| 58 | * \var camera3FormatsMap |
| 59 | * \brief Associate Android format code with ancillary data |
| 60 | */ |
| 61 | const std::map<int, const Camera3Format> camera3FormatsMap = { |
| 62 | { |
| 63 | HAL_PIXEL_FORMAT_BLOB, { |
Laurent Pinchart | 8b7e073 | 2020-05-22 04:02:06 +0300 | [diff] [blame] | 64 | { formats::MJPEG }, |
Niklas Söderlund | 8c1fedc | 2020-07-28 19:43:12 +0200 | [diff] [blame] | 65 | true, |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 66 | "BLOB" |
| 67 | } |
| 68 | }, { |
| 69 | HAL_PIXEL_FORMAT_YCbCr_420_888, { |
Laurent Pinchart | 8b7e073 | 2020-05-22 04:02:06 +0300 | [diff] [blame] | 70 | { formats::NV12, formats::NV21 }, |
Niklas Söderlund | 8c1fedc | 2020-07-28 19:43:12 +0200 | [diff] [blame] | 71 | true, |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 72 | "YCbCr_420_888" |
| 73 | } |
| 74 | }, { |
| 75 | /* |
| 76 | * \todo Translate IMPLEMENTATION_DEFINED inspecting the gralloc |
| 77 | * usage flag. For now, copy the YCbCr_420 configuration. |
| 78 | */ |
| 79 | HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, { |
Laurent Pinchart | 8b7e073 | 2020-05-22 04:02:06 +0300 | [diff] [blame] | 80 | { formats::NV12, formats::NV21 }, |
Niklas Söderlund | 8c1fedc | 2020-07-28 19:43:12 +0200 | [diff] [blame] | 81 | true, |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 82 | "IMPLEMENTATION_DEFINED" |
| 83 | } |
Niklas Söderlund | d4de037 | 2020-07-21 00:16:14 +0200 | [diff] [blame] | 84 | }, { |
| 85 | HAL_PIXEL_FORMAT_RAW10, { |
| 86 | { |
| 87 | formats::SBGGR10_CSI2P, |
| 88 | formats::SGBRG10_CSI2P, |
| 89 | formats::SGRBG10_CSI2P, |
| 90 | formats::SRGGB10_CSI2P |
| 91 | }, |
| 92 | false, |
| 93 | "RAW10" |
| 94 | } |
| 95 | }, { |
| 96 | HAL_PIXEL_FORMAT_RAW12, { |
| 97 | { |
| 98 | formats::SBGGR12_CSI2P, |
| 99 | formats::SGBRG12_CSI2P, |
| 100 | formats::SGRBG12_CSI2P, |
| 101 | formats::SRGGB12_CSI2P |
| 102 | }, |
| 103 | false, |
| 104 | "RAW12" |
| 105 | } |
| 106 | }, { |
| 107 | HAL_PIXEL_FORMAT_RAW16, { |
| 108 | { |
| 109 | formats::SBGGR16, |
| 110 | formats::SGBRG16, |
| 111 | formats::SGRBG16, |
| 112 | formats::SRGGB16 |
| 113 | }, |
| 114 | false, |
| 115 | "RAW16" |
| 116 | } |
| 117 | }, { |
| 118 | HAL_PIXEL_FORMAT_RAW_OPAQUE, { |
| 119 | { |
| 120 | formats::SBGGR10_IPU3, |
| 121 | formats::SGBRG10_IPU3, |
| 122 | formats::SGRBG10_IPU3, |
| 123 | formats::SRGGB10_IPU3 |
| 124 | }, |
| 125 | false, |
| 126 | "RAW_OPAQUE" |
| 127 | } |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 128 | }, |
| 129 | }; |
| 130 | |
| 131 | } /* namespace */ |
| 132 | |
Hirokazu Honda | 6c5792f | 2020-10-20 18:15:01 +0900 | [diff] [blame] | 133 | LOG_DECLARE_CATEGORY(HAL) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 134 | |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 135 | MappedCamera3Buffer::MappedCamera3Buffer(const buffer_handle_t camera3buffer, |
| 136 | int flags) |
| 137 | { |
| 138 | maps_.reserve(camera3buffer->numFds); |
| 139 | error_ = 0; |
| 140 | |
| 141 | for (int i = 0; i < camera3buffer->numFds; i++) { |
| 142 | if (camera3buffer->data[i] == -1) |
| 143 | continue; |
| 144 | |
| 145 | off_t length = lseek(camera3buffer->data[i], 0, SEEK_END); |
| 146 | if (length < 0) { |
| 147 | error_ = -errno; |
| 148 | LOG(HAL, Error) << "Failed to query plane length"; |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | void *address = mmap(nullptr, length, flags, MAP_SHARED, |
| 153 | camera3buffer->data[i], 0); |
| 154 | if (address == MAP_FAILED) { |
| 155 | error_ = -errno; |
| 156 | LOG(HAL, Error) << "Failed to mmap plane"; |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | maps_.emplace_back(static_cast<uint8_t *>(address), |
| 161 | static_cast<size_t>(length)); |
| 162 | } |
| 163 | } |
| 164 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 165 | /* |
| 166 | * \struct Camera3RequestDescriptor |
| 167 | * |
| 168 | * A utility structure that groups information about a capture request to be |
| 169 | * later re-used at request complete time to notify the framework. |
| 170 | */ |
| 171 | |
| 172 | CameraDevice::Camera3RequestDescriptor::Camera3RequestDescriptor( |
Jacopo Mondi | 4b1aa21 | 2020-10-06 17:56:50 +0200 | [diff] [blame] | 173 | Camera *camera, unsigned int frameNumber, unsigned int numBuffers) |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 174 | : frameNumber_(frameNumber), numBuffers_(numBuffers) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 175 | { |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 176 | buffers_ = new camera3_stream_buffer_t[numBuffers]; |
Jacopo Mondi | 4b1aa21 | 2020-10-06 17:56:50 +0200 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * FrameBuffer instances created by wrapping a camera3 provided dmabuf |
| 180 | * are emplaced in this vector of unique_ptr<> for lifetime management. |
| 181 | */ |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 182 | frameBuffers_.reserve(numBuffers); |
Jacopo Mondi | 4b1aa21 | 2020-10-06 17:56:50 +0200 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * Create the libcamera::Request unique_ptr<> to tie its lifetime |
| 186 | * to the descriptor's one. Set the descriptor's address as the |
| 187 | * request's cookie to retrieve it at completion time. |
| 188 | */ |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 189 | request_ = std::make_unique<CaptureRequest>(camera, |
| 190 | reinterpret_cast<uint64_t>(this)); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | CameraDevice::Camera3RequestDescriptor::~Camera3RequestDescriptor() |
| 194 | { |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 195 | delete[] buffers_; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* |
| 199 | * \class CameraDevice |
| 200 | * |
| 201 | * The CameraDevice class wraps a libcamera::Camera instance, and implements |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 202 | * the camera3_device_t interface, bridging calls received from the Android |
| 203 | * camera service to the CameraDevice. |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 204 | * |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 205 | * The class translates parameters and operations from the Camera HALv3 API to |
| 206 | * the libcamera API to provide static information for a Camera, create request |
| 207 | * templates for it, process capture requests and then deliver capture results |
| 208 | * back to the framework using the designated callbacks. |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 209 | */ |
| 210 | |
Laurent Pinchart | 0ed40d2 | 2019-08-18 01:45:01 +0300 | [diff] [blame] | 211 | CameraDevice::CameraDevice(unsigned int id, const std::shared_ptr<Camera> &camera) |
Umang Jain | 035ee23 | 2020-08-05 12:53:49 +0000 | [diff] [blame] | 212 | : id_(id), running_(false), camera_(camera), staticMetadata_(nullptr), |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 213 | facing_(CAMERA_FACING_FRONT), orientation_(0) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 214 | { |
| 215 | camera_->requestCompleted.connect(this, &CameraDevice::requestComplete); |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * \todo Determine a more accurate value for this during |
| 219 | * streamConfiguration. |
| 220 | */ |
| 221 | maxJpegBufferSize_ = 13 << 20; /* 13631488 from USB HAL */ |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | CameraDevice::~CameraDevice() |
| 225 | { |
| 226 | if (staticMetadata_) |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 227 | delete staticMetadata_; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 228 | |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 229 | for (auto &it : requestTemplates_) |
| 230 | delete it.second; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Umang Jain | f8e2813 | 2020-08-21 14:46:08 +0000 | [diff] [blame] | 233 | std::shared_ptr<CameraDevice> CameraDevice::create(unsigned int id, |
| 234 | const std::shared_ptr<Camera> &cam) |
| 235 | { |
| 236 | CameraDevice *camera = new CameraDevice(id, cam); |
| 237 | return std::shared_ptr<CameraDevice>(camera); |
| 238 | } |
| 239 | |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 240 | /* |
| 241 | * Initialize the camera static information. |
| 242 | * This method is called before the camera device is opened. |
| 243 | */ |
| 244 | int CameraDevice::initialize() |
| 245 | { |
| 246 | /* Initialize orientation and facing side of the camera. */ |
| 247 | const ControlList &properties = camera_->properties(); |
| 248 | |
| 249 | if (properties.contains(properties::Location)) { |
| 250 | int32_t location = properties.get(properties::Location); |
| 251 | switch (location) { |
| 252 | case properties::CameraLocationFront: |
| 253 | facing_ = CAMERA_FACING_FRONT; |
| 254 | break; |
| 255 | case properties::CameraLocationBack: |
| 256 | facing_ = CAMERA_FACING_BACK; |
| 257 | break; |
| 258 | case properties::CameraLocationExternal: |
| 259 | facing_ = CAMERA_FACING_EXTERNAL; |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /* |
Umang Jain | e917655 | 2020-09-09 16:17:54 +0530 | [diff] [blame] | 265 | * The Android orientation metadata specifies its rotation correction |
| 266 | * value in clockwise direction whereas libcamera specifies the |
| 267 | * rotation property in anticlockwise direction. Read the libcamera's |
| 268 | * rotation property (anticlockwise) and compute the corresponding |
| 269 | * value for clockwise direction as required by the Android orientation |
| 270 | * metadata. |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 271 | */ |
Umang Jain | e917655 | 2020-09-09 16:17:54 +0530 | [diff] [blame] | 272 | if (properties.contains(properties::Rotation)) { |
| 273 | int rotation = properties.get(properties::Rotation); |
| 274 | orientation_ = (360 - rotation) % 360; |
| 275 | } |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 276 | |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 277 | int ret = camera_->acquire(); |
| 278 | if (ret) { |
| 279 | LOG(HAL, Error) << "Failed to temporarily acquire the camera"; |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | ret = initializeStreamConfigurations(); |
| 284 | camera_->release(); |
| 285 | return ret; |
| 286 | } |
| 287 | |
Jacopo Mondi | bfee631 | 2020-09-01 17:42:13 +0200 | [diff] [blame] | 288 | std::vector<Size> CameraDevice::getYUVResolutions(CameraConfiguration *cameraConfig, |
| 289 | const PixelFormat &pixelFormat, |
| 290 | const std::vector<Size> &resolutions) |
| 291 | { |
| 292 | std::vector<Size> supportedResolutions; |
| 293 | |
| 294 | StreamConfiguration &cfg = cameraConfig->at(0); |
| 295 | for (const Size &res : resolutions) { |
| 296 | cfg.pixelFormat = pixelFormat; |
| 297 | cfg.size = res; |
| 298 | |
| 299 | CameraConfiguration::Status status = cameraConfig->validate(); |
| 300 | if (status != CameraConfiguration::Valid) { |
| 301 | LOG(HAL, Debug) << cfg.toString() << " not supported"; |
| 302 | continue; |
| 303 | } |
| 304 | |
| 305 | LOG(HAL, Debug) << cfg.toString() << " supported"; |
| 306 | |
| 307 | supportedResolutions.push_back(res); |
| 308 | } |
| 309 | |
| 310 | return supportedResolutions; |
| 311 | } |
| 312 | |
Jacopo Mondi | 4961033 | 2020-09-01 18:11:34 +0200 | [diff] [blame] | 313 | std::vector<Size> CameraDevice::getRawResolutions(const libcamera::PixelFormat &pixelFormat) |
| 314 | { |
| 315 | std::unique_ptr<CameraConfiguration> cameraConfig = |
Niklas Söderlund | dbe8d27 | 2020-05-26 15:04:47 +0200 | [diff] [blame] | 316 | camera_->generateConfiguration({ StreamRole::Raw }); |
Jacopo Mondi | 4961033 | 2020-09-01 18:11:34 +0200 | [diff] [blame] | 317 | StreamConfiguration &cfg = cameraConfig->at(0); |
| 318 | const StreamFormats &formats = cfg.formats(); |
| 319 | std::vector<Size> supportedResolutions = formats.sizes(pixelFormat); |
| 320 | |
| 321 | return supportedResolutions; |
| 322 | } |
| 323 | |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 324 | /* |
| 325 | * Initialize the format conversion map to translate from Android format |
| 326 | * identifier to libcamera pixel formats and fill in the list of supported |
| 327 | * stream configurations to be reported to the Android camera framework through |
| 328 | * the static stream configuration metadata. |
| 329 | */ |
| 330 | int CameraDevice::initializeStreamConfigurations() |
| 331 | { |
| 332 | /* |
| 333 | * Get the maximum output resolutions |
| 334 | * \todo Get this from the camera properties once defined |
| 335 | */ |
| 336 | std::unique_ptr<CameraConfiguration> cameraConfig = |
| 337 | camera_->generateConfiguration({ StillCapture }); |
| 338 | if (!cameraConfig) { |
| 339 | LOG(HAL, Error) << "Failed to get maximum resolution"; |
| 340 | return -EINVAL; |
| 341 | } |
| 342 | StreamConfiguration &cfg = cameraConfig->at(0); |
| 343 | |
| 344 | /* |
| 345 | * \todo JPEG - Adjust the maximum available resolution by taking the |
| 346 | * JPEG encoder requirements into account (alignment and aspect ratio). |
| 347 | */ |
| 348 | const Size maxRes = cfg.size; |
| 349 | LOG(HAL, Debug) << "Maximum supported resolution: " << maxRes.toString(); |
| 350 | |
| 351 | /* |
| 352 | * Build the list of supported image resolutions. |
| 353 | * |
| 354 | * The resolutions listed in camera3Resolution are mandatory to be |
| 355 | * supported, up to the camera maximum resolution. |
| 356 | * |
| 357 | * Augment the list by adding resolutions calculated from the camera |
| 358 | * maximum one. |
| 359 | */ |
| 360 | std::vector<Size> cameraResolutions; |
| 361 | std::copy_if(camera3Resolutions.begin(), camera3Resolutions.end(), |
| 362 | std::back_inserter(cameraResolutions), |
| 363 | [&](const Size &res) { return res < maxRes; }); |
| 364 | |
| 365 | /* |
| 366 | * The Camera3 specification suggests adding 1/2 and 1/4 of the maximum |
| 367 | * resolution. |
| 368 | */ |
| 369 | for (unsigned int divider = 2;; divider <<= 1) { |
| 370 | Size derivedSize{ |
| 371 | maxRes.width / divider, |
| 372 | maxRes.height / divider, |
| 373 | }; |
| 374 | |
| 375 | if (derivedSize.width < 320 || |
| 376 | derivedSize.height < 240) |
| 377 | break; |
| 378 | |
| 379 | cameraResolutions.push_back(derivedSize); |
| 380 | } |
| 381 | cameraResolutions.push_back(maxRes); |
| 382 | |
| 383 | /* Remove duplicated entries from the list of supported resolutions. */ |
| 384 | std::sort(cameraResolutions.begin(), cameraResolutions.end()); |
| 385 | auto last = std::unique(cameraResolutions.begin(), cameraResolutions.end()); |
| 386 | cameraResolutions.erase(last, cameraResolutions.end()); |
| 387 | |
| 388 | /* |
| 389 | * Build the list of supported camera formats. |
| 390 | * |
| 391 | * To each Android format a list of compatible libcamera formats is |
| 392 | * associated. The first libcamera format that tests successful is added |
| 393 | * to the format translation map used when configuring the streams. |
| 394 | * It is then tested against the list of supported camera resolutions to |
| 395 | * build the stream configuration map reported through the camera static |
| 396 | * metadata. |
| 397 | */ |
| 398 | for (const auto &format : camera3FormatsMap) { |
| 399 | int androidFormat = format.first; |
| 400 | const Camera3Format &camera3Format = format.second; |
| 401 | const std::vector<PixelFormat> &libcameraFormats = |
| 402 | camera3Format.libcameraFormats; |
| 403 | |
Jacopo Mondi | af264ec | 2020-09-01 15:40:49 +0200 | [diff] [blame] | 404 | LOG(HAL, Debug) << "Trying to map Android format " |
| 405 | << camera3Format.name; |
| 406 | |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 407 | /* |
Jacopo Mondi | 843565c | 2020-09-01 15:34:13 +0200 | [diff] [blame] | 408 | * JPEG is always supported, either produced directly by the |
| 409 | * camera, or encoded in the HAL. |
| 410 | */ |
| 411 | if (androidFormat == HAL_PIXEL_FORMAT_BLOB) { |
| 412 | formatsMap_[androidFormat] = formats::MJPEG; |
Jacopo Mondi | af264ec | 2020-09-01 15:40:49 +0200 | [diff] [blame] | 413 | LOG(HAL, Debug) << "Mapped Android format " |
| 414 | << camera3Format.name << " to " |
| 415 | << formats::MJPEG.toString() |
| 416 | << " (fixed mapping)"; |
Jacopo Mondi | 843565c | 2020-09-01 15:34:13 +0200 | [diff] [blame] | 417 | continue; |
| 418 | } |
| 419 | |
| 420 | /* |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 421 | * Test the libcamera formats that can produce images |
| 422 | * compatible with the format defined by Android. |
| 423 | */ |
| 424 | PixelFormat mappedFormat; |
| 425 | for (const PixelFormat &pixelFormat : libcameraFormats) { |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 426 | |
Jacopo Mondi | af264ec | 2020-09-01 15:40:49 +0200 | [diff] [blame] | 427 | LOG(HAL, Debug) << "Testing " << pixelFormat.toString(); |
| 428 | |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 429 | /* |
| 430 | * The stream configuration size can be adjusted, |
| 431 | * not the pixel format. |
| 432 | * |
| 433 | * \todo This could be simplified once all pipeline |
| 434 | * handlers will report the StreamFormats list of |
| 435 | * supported formats. |
| 436 | */ |
| 437 | cfg.pixelFormat = pixelFormat; |
| 438 | |
| 439 | CameraConfiguration::Status status = cameraConfig->validate(); |
| 440 | if (status != CameraConfiguration::Invalid && |
| 441 | cfg.pixelFormat == pixelFormat) { |
| 442 | mappedFormat = pixelFormat; |
| 443 | break; |
| 444 | } |
| 445 | } |
Jacopo Mondi | 3533fd4 | 2020-09-01 15:31:56 +0200 | [diff] [blame] | 446 | |
| 447 | if (!mappedFormat.isValid()) { |
| 448 | /* If the format is not mandatory, skip it. */ |
| 449 | if (!camera3Format.mandatory) |
| 450 | continue; |
| 451 | |
| 452 | LOG(HAL, Error) |
| 453 | << "Failed to map mandatory Android format " |
| 454 | << camera3Format.name << " (" |
| 455 | << utils::hex(androidFormat) << "): aborting"; |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 456 | return -EINVAL; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Record the mapping and then proceed to generate the |
| 461 | * stream configurations map, by testing the image resolutions. |
| 462 | */ |
| 463 | formatsMap_[androidFormat] = mappedFormat; |
Jacopo Mondi | af264ec | 2020-09-01 15:40:49 +0200 | [diff] [blame] | 464 | LOG(HAL, Debug) << "Mapped Android format " |
| 465 | << camera3Format.name << " to " |
| 466 | << mappedFormat.toString(); |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 467 | |
Jacopo Mondi | 4961033 | 2020-09-01 18:11:34 +0200 | [diff] [blame] | 468 | std::vector<Size> resolutions; |
| 469 | const PixelFormatInfo &info = PixelFormatInfo::info(mappedFormat); |
| 470 | if (info.colourEncoding == PixelFormatInfo::ColourEncodingRAW) |
| 471 | resolutions = getRawResolutions(mappedFormat); |
| 472 | else |
| 473 | resolutions = getYUVResolutions(cameraConfig.get(), |
| 474 | mappedFormat, |
| 475 | cameraResolutions); |
| 476 | |
Jacopo Mondi | bfee631 | 2020-09-01 17:42:13 +0200 | [diff] [blame] | 477 | for (const Size &res : resolutions) { |
Niklas Söderlund | 142a9ee | 2020-07-23 18:32:23 +0200 | [diff] [blame] | 478 | streamConfigurations_.push_back({ res, androidFormat }); |
Jacopo Mondi | 843565c | 2020-09-01 15:34:13 +0200 | [diff] [blame] | 479 | |
| 480 | /* |
| 481 | * If the format is HAL_PIXEL_FORMAT_YCbCr_420_888 |
| 482 | * from which JPEG is produced, add an entry for |
| 483 | * the JPEG stream. |
| 484 | * |
| 485 | * \todo Wire the JPEG encoder to query the supported |
| 486 | * sizes provided a list of formats it can encode. |
| 487 | * |
| 488 | * \todo Support JPEG streams produced by the Camera |
| 489 | * natively. |
| 490 | */ |
| 491 | if (androidFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) |
| 492 | streamConfigurations_.push_back( |
| 493 | { res, HAL_PIXEL_FORMAT_BLOB }); |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
| 497 | LOG(HAL, Debug) << "Collected stream configuration map: "; |
| 498 | for (const auto &entry : streamConfigurations_) |
| 499 | LOG(HAL, Debug) << "{ " << entry.resolution.toString() << " - " |
Niklas Söderlund | 142a9ee | 2020-07-23 18:32:23 +0200 | [diff] [blame] | 500 | << utils::hex(entry.androidFormat) << " }"; |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 501 | |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Open a camera device. The static information on the camera shall have been |
| 507 | * initialized with a call to CameraDevice::initialize(). |
| 508 | */ |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 509 | int CameraDevice::open(const hw_module_t *hardwareModule) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 510 | { |
| 511 | int ret = camera_->acquire(); |
| 512 | if (ret) { |
| 513 | LOG(HAL, Error) << "Failed to acquire the camera"; |
| 514 | return ret; |
| 515 | } |
| 516 | |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 517 | /* Initialize the hw_device_t in the instance camera3_module_t. */ |
| 518 | camera3Device_.common.tag = HARDWARE_DEVICE_TAG; |
| 519 | camera3Device_.common.version = CAMERA_DEVICE_API_VERSION_3_3; |
| 520 | camera3Device_.common.module = (hw_module_t *)hardwareModule; |
| 521 | camera3Device_.common.close = hal_dev_close; |
| 522 | |
| 523 | /* |
| 524 | * The camera device operations. These actually implement |
| 525 | * the Android Camera HALv3 interface. |
| 526 | */ |
| 527 | camera3Device_.ops = &hal_dev_ops; |
| 528 | camera3Device_.priv = this; |
| 529 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | void CameraDevice::close() |
| 534 | { |
Jacopo Mondi | 6c4999e | 2020-10-03 16:06:34 +0200 | [diff] [blame] | 535 | streams_.clear(); |
| 536 | |
Jacopo Mondi | 4b1aa21 | 2020-10-06 17:56:50 +0200 | [diff] [blame] | 537 | worker_.stop(); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 538 | camera_->stop(); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 539 | camera_->release(); |
| 540 | |
| 541 | running_ = false; |
| 542 | } |
| 543 | |
| 544 | void CameraDevice::setCallbacks(const camera3_callback_ops_t *callbacks) |
| 545 | { |
| 546 | callbacks_ = callbacks; |
| 547 | } |
| 548 | |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 549 | std::tuple<uint32_t, uint32_t> CameraDevice::calculateStaticMetadataSize() |
| 550 | { |
| 551 | /* |
| 552 | * \todo Keep this in sync with the actual number of entries. |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 553 | * Currently: 51 entries, 687 bytes of static metadata |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 554 | */ |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 555 | uint32_t numEntries = 51; |
| 556 | uint32_t byteSize = 687; |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 557 | |
| 558 | /* |
| 559 | * Calculate space occupation in bytes for dynamically built metadata |
| 560 | * entries. |
| 561 | * |
| 562 | * Each stream configuration entry requires 52 bytes: |
| 563 | * 4 32bits integers for ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 564 | * 4 64bits integers for ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS |
| 565 | */ |
Niklas Söderlund | 3530614 | 2020-07-23 18:25:04 +0200 | [diff] [blame] | 566 | byteSize += streamConfigurations_.size() * 48; |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 567 | |
Laurent Pinchart | 7a88b21 | 2020-06-10 00:07:59 +0300 | [diff] [blame] | 568 | return std::make_tuple(numEntries, byteSize); |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 571 | /* |
| 572 | * Return static information for the camera. |
| 573 | */ |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 574 | const camera_metadata_t *CameraDevice::getStaticMetadata() |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 575 | { |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 576 | if (staticMetadata_) |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 577 | return staticMetadata_->get(); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 578 | |
| 579 | /* |
| 580 | * The here reported metadata are enough to implement a basic capture |
| 581 | * example application, but a real camera implementation will require |
| 582 | * more. |
| 583 | */ |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 584 | uint32_t numEntries; |
| 585 | uint32_t byteSize; |
| 586 | std::tie(numEntries, byteSize) = calculateStaticMetadataSize(); |
| 587 | staticMetadata_ = new CameraMetadata(numEntries, byteSize); |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 588 | if (!staticMetadata_->isValid()) { |
| 589 | LOG(HAL, Error) << "Failed to allocate static metadata"; |
| 590 | delete staticMetadata_; |
| 591 | staticMetadata_ = nullptr; |
| 592 | return nullptr; |
| 593 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 594 | |
Jacopo Mondi | 5d91c8d | 2020-09-23 09:41:52 +0200 | [diff] [blame^] | 595 | const ControlInfoMap &controlsInfo = camera_->controls(); |
| 596 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 597 | /* Color correction static metadata. */ |
| 598 | std::vector<uint8_t> aberrationModes = { |
| 599 | ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF, |
| 600 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 601 | staticMetadata_->addEntry(ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES, |
| 602 | aberrationModes.data(), |
| 603 | aberrationModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 604 | |
| 605 | /* Control static metadata. */ |
| 606 | std::vector<uint8_t> aeAvailableAntiBandingModes = { |
| 607 | ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF, |
| 608 | ANDROID_CONTROL_AE_ANTIBANDING_MODE_50HZ, |
| 609 | ANDROID_CONTROL_AE_ANTIBANDING_MODE_60HZ, |
| 610 | ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO, |
| 611 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 612 | staticMetadata_->addEntry(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES, |
| 613 | aeAvailableAntiBandingModes.data(), |
| 614 | aeAvailableAntiBandingModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 615 | |
| 616 | std::vector<uint8_t> aeAvailableModes = { |
| 617 | ANDROID_CONTROL_AE_MODE_ON, |
| 618 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 619 | staticMetadata_->addEntry(ANDROID_CONTROL_AE_AVAILABLE_MODES, |
| 620 | aeAvailableModes.data(), |
| 621 | aeAvailableModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 622 | |
| 623 | std::vector<int32_t> availableAeFpsTarget = { |
| 624 | 15, 30, |
| 625 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 626 | staticMetadata_->addEntry(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, |
| 627 | availableAeFpsTarget.data(), |
| 628 | availableAeFpsTarget.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 629 | |
| 630 | std::vector<int32_t> aeCompensationRange = { |
| 631 | 0, 0, |
| 632 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 633 | staticMetadata_->addEntry(ANDROID_CONTROL_AE_COMPENSATION_RANGE, |
| 634 | aeCompensationRange.data(), |
| 635 | aeCompensationRange.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 636 | |
| 637 | const camera_metadata_rational_t aeCompensationStep[] = { |
| 638 | { 0, 1 } |
| 639 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 640 | staticMetadata_->addEntry(ANDROID_CONTROL_AE_COMPENSATION_STEP, |
| 641 | aeCompensationStep, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 642 | |
| 643 | std::vector<uint8_t> availableAfModes = { |
| 644 | ANDROID_CONTROL_AF_MODE_OFF, |
| 645 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 646 | staticMetadata_->addEntry(ANDROID_CONTROL_AF_AVAILABLE_MODES, |
| 647 | availableAfModes.data(), |
| 648 | availableAfModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 649 | |
| 650 | std::vector<uint8_t> availableEffects = { |
| 651 | ANDROID_CONTROL_EFFECT_MODE_OFF, |
| 652 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 653 | staticMetadata_->addEntry(ANDROID_CONTROL_AVAILABLE_EFFECTS, |
| 654 | availableEffects.data(), |
| 655 | availableEffects.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 656 | |
| 657 | std::vector<uint8_t> availableSceneModes = { |
| 658 | ANDROID_CONTROL_SCENE_MODE_DISABLED, |
| 659 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 660 | staticMetadata_->addEntry(ANDROID_CONTROL_AVAILABLE_SCENE_MODES, |
| 661 | availableSceneModes.data(), |
| 662 | availableSceneModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 663 | |
| 664 | std::vector<uint8_t> availableStabilizationModes = { |
| 665 | ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF, |
| 666 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 667 | staticMetadata_->addEntry(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES, |
| 668 | availableStabilizationModes.data(), |
| 669 | availableStabilizationModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 670 | |
| 671 | std::vector<uint8_t> availableAwbModes = { |
| 672 | ANDROID_CONTROL_AWB_MODE_OFF, |
| 673 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 674 | staticMetadata_->addEntry(ANDROID_CONTROL_AWB_AVAILABLE_MODES, |
| 675 | availableAwbModes.data(), |
| 676 | availableAwbModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 677 | |
| 678 | std::vector<int32_t> availableMaxRegions = { |
| 679 | 0, 0, 0, |
| 680 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 681 | staticMetadata_->addEntry(ANDROID_CONTROL_MAX_REGIONS, |
| 682 | availableMaxRegions.data(), |
| 683 | availableMaxRegions.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 684 | |
| 685 | std::vector<uint8_t> sceneModesOverride = { |
| 686 | ANDROID_CONTROL_AE_MODE_ON, |
| 687 | ANDROID_CONTROL_AWB_MODE_AUTO, |
| 688 | ANDROID_CONTROL_AF_MODE_AUTO, |
| 689 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 690 | staticMetadata_->addEntry(ANDROID_CONTROL_SCENE_MODE_OVERRIDES, |
| 691 | sceneModesOverride.data(), |
| 692 | sceneModesOverride.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 693 | |
| 694 | uint8_t aeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 695 | staticMetadata_->addEntry(ANDROID_CONTROL_AE_LOCK_AVAILABLE, |
| 696 | &aeLockAvailable, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 697 | |
| 698 | uint8_t awbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 699 | staticMetadata_->addEntry(ANDROID_CONTROL_AWB_LOCK_AVAILABLE, |
| 700 | &awbLockAvailable, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 701 | |
| 702 | char availableControlModes = ANDROID_CONTROL_MODE_AUTO; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 703 | staticMetadata_->addEntry(ANDROID_CONTROL_AVAILABLE_MODES, |
| 704 | &availableControlModes, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 705 | |
| 706 | /* JPEG static metadata. */ |
| 707 | std::vector<int32_t> availableThumbnailSizes = { |
| 708 | 0, 0, |
| 709 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 710 | staticMetadata_->addEntry(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, |
| 711 | availableThumbnailSizes.data(), |
| 712 | availableThumbnailSizes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 713 | |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 714 | /* |
| 715 | * \todo Calculate the maximum JPEG buffer size by asking the encoder |
| 716 | * giving the maximum frame size required. |
| 717 | */ |
| 718 | staticMetadata_->addEntry(ANDROID_JPEG_MAX_SIZE, &maxJpegBufferSize_, 1); |
| 719 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 720 | /* Sensor static metadata. */ |
| 721 | int32_t pixelArraySize[] = { |
| 722 | 2592, 1944, |
| 723 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 724 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE, |
| 725 | &pixelArraySize, 2); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 726 | |
| 727 | int32_t sensorSizes[] = { |
| 728 | 0, 0, 2560, 1920, |
| 729 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 730 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, |
| 731 | &sensorSizes, 4); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 732 | |
| 733 | int32_t sensitivityRange[] = { |
| 734 | 32, 2400, |
| 735 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 736 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_SENSITIVITY_RANGE, |
| 737 | &sensitivityRange, 2); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 738 | |
| 739 | uint16_t filterArr = ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT_GRBG; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 740 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT, |
| 741 | &filterArr, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 742 | |
| 743 | int64_t exposureTimeRange[] = { |
| 744 | 100000, 200000000, |
| 745 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 746 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE, |
| 747 | &exposureTimeRange, 2); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 748 | |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 749 | staticMetadata_->addEntry(ANDROID_SENSOR_ORIENTATION, &orientation_, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 750 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 751 | std::vector<int32_t> testPatterModes = { |
| 752 | ANDROID_SENSOR_TEST_PATTERN_MODE_OFF, |
| 753 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 754 | staticMetadata_->addEntry(ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES, |
| 755 | testPatterModes.data(), |
| 756 | testPatterModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 757 | |
| 758 | std::vector<float> physicalSize = { |
| 759 | 2592, 1944, |
| 760 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 761 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_PHYSICAL_SIZE, |
| 762 | physicalSize.data(), |
| 763 | physicalSize.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 764 | |
| 765 | uint8_t timestampSource = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 766 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE, |
| 767 | ×tampSource, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 768 | |
| 769 | /* Statistics static metadata. */ |
| 770 | uint8_t faceDetectMode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 771 | staticMetadata_->addEntry(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES, |
| 772 | &faceDetectMode, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 773 | |
| 774 | int32_t maxFaceCount = 0; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 775 | staticMetadata_->addEntry(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT, |
| 776 | &maxFaceCount, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 777 | |
| 778 | /* Sync static metadata. */ |
| 779 | int32_t maxLatency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 780 | staticMetadata_->addEntry(ANDROID_SYNC_MAX_LATENCY, &maxLatency, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 781 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 782 | /* Flash static metadata. */ |
| 783 | char flashAvailable = ANDROID_FLASH_INFO_AVAILABLE_FALSE; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 784 | staticMetadata_->addEntry(ANDROID_FLASH_INFO_AVAILABLE, |
| 785 | &flashAvailable, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 786 | |
| 787 | /* Lens static metadata. */ |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 788 | std::vector<float> lensApertures = { |
| 789 | 2.53 / 100, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 790 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 791 | staticMetadata_->addEntry(ANDROID_LENS_INFO_AVAILABLE_APERTURES, |
| 792 | lensApertures.data(), |
| 793 | lensApertures.size()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 794 | |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 795 | uint8_t lensFacing; |
| 796 | switch (facing_) { |
| 797 | default: |
| 798 | case CAMERA_FACING_FRONT: |
| 799 | lensFacing = ANDROID_LENS_FACING_FRONT; |
| 800 | break; |
| 801 | case CAMERA_FACING_BACK: |
| 802 | lensFacing = ANDROID_LENS_FACING_BACK; |
| 803 | break; |
| 804 | case CAMERA_FACING_EXTERNAL: |
| 805 | lensFacing = ANDROID_LENS_FACING_EXTERNAL; |
| 806 | break; |
Jacopo Mondi | 857a216 | 2019-11-20 17:00:49 +0100 | [diff] [blame] | 807 | } |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 808 | staticMetadata_->addEntry(ANDROID_LENS_FACING, &lensFacing, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 809 | |
| 810 | std::vector<float> lensFocalLenghts = { |
| 811 | 1, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 812 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 813 | staticMetadata_->addEntry(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS, |
| 814 | lensFocalLenghts.data(), |
| 815 | lensFocalLenghts.size()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 816 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 817 | std::vector<uint8_t> opticalStabilizations = { |
| 818 | ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF, |
| 819 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 820 | staticMetadata_->addEntry(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION, |
| 821 | opticalStabilizations.data(), |
| 822 | opticalStabilizations.size()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 823 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 824 | float hypeFocalDistance = 0; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 825 | staticMetadata_->addEntry(ANDROID_LENS_INFO_HYPERFOCAL_DISTANCE, |
| 826 | &hypeFocalDistance, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 827 | |
| 828 | float minFocusDistance = 0; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 829 | staticMetadata_->addEntry(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE, |
| 830 | &minFocusDistance, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 831 | |
| 832 | /* Noise reduction modes. */ |
| 833 | uint8_t noiseReductionModes = ANDROID_NOISE_REDUCTION_MODE_OFF; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 834 | staticMetadata_->addEntry(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES, |
| 835 | &noiseReductionModes, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 836 | |
| 837 | /* Scaler static metadata. */ |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 838 | float maxDigitalZoom = 1; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 839 | staticMetadata_->addEntry(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM, |
| 840 | &maxDigitalZoom, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 841 | |
Jacopo Mondi | bde7b98 | 2020-05-25 17:18:28 +0200 | [diff] [blame] | 842 | std::vector<uint32_t> availableStreamConfigurations; |
| 843 | availableStreamConfigurations.reserve(streamConfigurations_.size() * 4); |
| 844 | for (const auto &entry : streamConfigurations_) { |
Niklas Söderlund | 142a9ee | 2020-07-23 18:32:23 +0200 | [diff] [blame] | 845 | availableStreamConfigurations.push_back(entry.androidFormat); |
Jacopo Mondi | bde7b98 | 2020-05-25 17:18:28 +0200 | [diff] [blame] | 846 | availableStreamConfigurations.push_back(entry.resolution.width); |
| 847 | availableStreamConfigurations.push_back(entry.resolution.height); |
| 848 | availableStreamConfigurations.push_back( |
| 849 | ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT); |
| 850 | } |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 851 | staticMetadata_->addEntry(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, |
| 852 | availableStreamConfigurations.data(), |
| 853 | availableStreamConfigurations.size()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 854 | |
| 855 | std::vector<int64_t> availableStallDurations = { |
| 856 | ANDROID_SCALER_AVAILABLE_FORMATS_BLOB, 2560, 1920, 33333333, |
| 857 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 858 | staticMetadata_->addEntry(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, |
| 859 | availableStallDurations.data(), |
| 860 | availableStallDurations.size()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 861 | |
Jacopo Mondi | bde7b98 | 2020-05-25 17:18:28 +0200 | [diff] [blame] | 862 | /* \todo Collect the minimum frame duration from the camera. */ |
| 863 | std::vector<int64_t> minFrameDurations; |
| 864 | minFrameDurations.reserve(streamConfigurations_.size() * 4); |
| 865 | for (const auto &entry : streamConfigurations_) { |
Niklas Söderlund | 142a9ee | 2020-07-23 18:32:23 +0200 | [diff] [blame] | 866 | minFrameDurations.push_back(entry.androidFormat); |
Jacopo Mondi | bde7b98 | 2020-05-25 17:18:28 +0200 | [diff] [blame] | 867 | minFrameDurations.push_back(entry.resolution.width); |
| 868 | minFrameDurations.push_back(entry.resolution.height); |
| 869 | minFrameDurations.push_back(33333333); |
| 870 | } |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 871 | staticMetadata_->addEntry(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, |
| 872 | minFrameDurations.data(), |
| 873 | minFrameDurations.size()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 874 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 875 | uint8_t croppingType = ANDROID_SCALER_CROPPING_TYPE_CENTER_ONLY; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 876 | staticMetadata_->addEntry(ANDROID_SCALER_CROPPING_TYPE, &croppingType, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 877 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 878 | /* Info static metadata. */ |
| 879 | uint8_t supportedHWLevel = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 880 | staticMetadata_->addEntry(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL, |
| 881 | &supportedHWLevel, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 882 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 883 | /* Request static metadata. */ |
| 884 | int32_t partialResultCount = 1; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 885 | staticMetadata_->addEntry(ANDROID_REQUEST_PARTIAL_RESULT_COUNT, |
| 886 | &partialResultCount, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 887 | |
Jacopo Mondi | 5d91c8d | 2020-09-23 09:41:52 +0200 | [diff] [blame^] | 888 | { |
| 889 | /* Default the value to 2 if not reported by the camera. */ |
| 890 | uint8_t maxPipelineDepth = 2; |
| 891 | const auto &infoMap = controlsInfo.find(&controls::draft::PipelineDepth); |
| 892 | if (infoMap != controlsInfo.end()) |
| 893 | maxPipelineDepth = infoMap->second.max().get<int32_t>(); |
| 894 | staticMetadata_->addEntry(ANDROID_REQUEST_PIPELINE_MAX_DEPTH, |
| 895 | &maxPipelineDepth, 1); |
| 896 | } |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 897 | |
Niklas Söderlund | a2a6f95 | 2020-07-21 00:16:02 +0200 | [diff] [blame] | 898 | /* LIMITED does not support reprocessing. */ |
| 899 | uint32_t maxNumInputStreams = 0; |
| 900 | staticMetadata_->addEntry(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, |
| 901 | &maxNumInputStreams, 1); |
| 902 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 903 | std::vector<uint8_t> availableCapabilities = { |
| 904 | ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE, |
| 905 | }; |
Niklas Söderlund | 7876d63 | 2020-07-21 00:16:24 +0200 | [diff] [blame] | 906 | |
| 907 | /* Report if camera supports RAW. */ |
| 908 | std::unique_ptr<CameraConfiguration> cameraConfig = |
Niklas Söderlund | dbe8d27 | 2020-05-26 15:04:47 +0200 | [diff] [blame] | 909 | camera_->generateConfiguration({ StreamRole::Raw }); |
Niklas Söderlund | 7876d63 | 2020-07-21 00:16:24 +0200 | [diff] [blame] | 910 | if (cameraConfig && !cameraConfig->empty()) { |
| 911 | const PixelFormatInfo &info = |
| 912 | PixelFormatInfo::info(cameraConfig->at(0).pixelFormat); |
| 913 | if (info.colourEncoding == PixelFormatInfo::ColourEncodingRAW) |
| 914 | availableCapabilities.push_back(ANDROID_REQUEST_AVAILABLE_CAPABILITIES_RAW); |
| 915 | } |
| 916 | |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 917 | staticMetadata_->addEntry(ANDROID_REQUEST_AVAILABLE_CAPABILITIES, |
| 918 | availableCapabilities.data(), |
| 919 | availableCapabilities.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 920 | |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 921 | std::vector<int32_t> availableCharacteristicsKeys = { |
| 922 | ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES, |
| 923 | ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES, |
| 924 | ANDROID_CONTROL_AE_AVAILABLE_MODES, |
| 925 | ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, |
| 926 | ANDROID_CONTROL_AE_COMPENSATION_RANGE, |
| 927 | ANDROID_CONTROL_AE_COMPENSATION_STEP, |
| 928 | ANDROID_CONTROL_AF_AVAILABLE_MODES, |
| 929 | ANDROID_CONTROL_AVAILABLE_EFFECTS, |
| 930 | ANDROID_CONTROL_AVAILABLE_SCENE_MODES, |
| 931 | ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES, |
| 932 | ANDROID_CONTROL_AWB_AVAILABLE_MODES, |
| 933 | ANDROID_CONTROL_MAX_REGIONS, |
| 934 | ANDROID_CONTROL_SCENE_MODE_OVERRIDES, |
| 935 | ANDROID_CONTROL_AE_LOCK_AVAILABLE, |
| 936 | ANDROID_CONTROL_AWB_LOCK_AVAILABLE, |
| 937 | ANDROID_CONTROL_AVAILABLE_MODES, |
| 938 | ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 939 | ANDROID_JPEG_MAX_SIZE, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 940 | ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE, |
| 941 | ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, |
| 942 | ANDROID_SENSOR_INFO_SENSITIVITY_RANGE, |
| 943 | ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT, |
| 944 | ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE, |
| 945 | ANDROID_SENSOR_ORIENTATION, |
| 946 | ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES, |
| 947 | ANDROID_SENSOR_INFO_PHYSICAL_SIZE, |
| 948 | ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE, |
| 949 | ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES, |
| 950 | ANDROID_STATISTICS_INFO_MAX_FACE_COUNT, |
| 951 | ANDROID_SYNC_MAX_LATENCY, |
| 952 | ANDROID_FLASH_INFO_AVAILABLE, |
| 953 | ANDROID_LENS_INFO_AVAILABLE_APERTURES, |
| 954 | ANDROID_LENS_FACING, |
| 955 | ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS, |
| 956 | ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION, |
| 957 | ANDROID_LENS_INFO_HYPERFOCAL_DISTANCE, |
| 958 | ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE, |
| 959 | ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES, |
| 960 | ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 961 | ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, |
| 962 | ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, |
| 963 | ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, |
| 964 | ANDROID_SCALER_CROPPING_TYPE, |
| 965 | ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL, |
| 966 | ANDROID_REQUEST_PARTIAL_RESULT_COUNT, |
| 967 | ANDROID_REQUEST_PIPELINE_MAX_DEPTH, |
Niklas Söderlund | a2a6f95 | 2020-07-21 00:16:02 +0200 | [diff] [blame] | 968 | ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 969 | ANDROID_REQUEST_AVAILABLE_CAPABILITIES, |
| 970 | }; |
| 971 | staticMetadata_->addEntry(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, |
| 972 | availableCharacteristicsKeys.data(), |
| 973 | availableCharacteristicsKeys.size()); |
| 974 | |
| 975 | std::vector<int32_t> availableRequestKeys = { |
| 976 | ANDROID_CONTROL_AE_MODE, |
| 977 | ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION, |
| 978 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, |
Jacopo Mondi | 09b1d0f | 2020-07-24 16:10:23 +0200 | [diff] [blame] | 979 | ANDROID_CONTROL_AE_TARGET_FPS_RANGE, |
| 980 | ANDROID_CONTROL_AE_ANTIBANDING_MODE, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 981 | ANDROID_CONTROL_AE_LOCK, |
| 982 | ANDROID_CONTROL_AF_TRIGGER, |
| 983 | ANDROID_CONTROL_AWB_MODE, |
| 984 | ANDROID_CONTROL_AWB_LOCK, |
| 985 | ANDROID_FLASH_MODE, |
| 986 | ANDROID_STATISTICS_FACE_DETECT_MODE, |
| 987 | ANDROID_NOISE_REDUCTION_MODE, |
| 988 | ANDROID_COLOR_CORRECTION_ABERRATION_MODE, |
Jacopo Mondi | 09b1d0f | 2020-07-24 16:10:23 +0200 | [diff] [blame] | 989 | ANDROID_LENS_APERTURE, |
| 990 | ANDROID_LENS_OPTICAL_STABILIZATION_MODE, |
| 991 | ANDROID_CONTROL_MODE, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 992 | ANDROID_CONTROL_CAPTURE_INTENT, |
| 993 | }; |
| 994 | staticMetadata_->addEntry(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, |
| 995 | availableRequestKeys.data(), |
| 996 | availableRequestKeys.size()); |
| 997 | |
| 998 | std::vector<int32_t> availableResultKeys = { |
| 999 | ANDROID_CONTROL_AE_STATE, |
| 1000 | ANDROID_CONTROL_AE_LOCK, |
| 1001 | ANDROID_CONTROL_AF_STATE, |
| 1002 | ANDROID_CONTROL_AWB_STATE, |
| 1003 | ANDROID_CONTROL_AWB_LOCK, |
| 1004 | ANDROID_LENS_STATE, |
| 1005 | ANDROID_SCALER_CROP_REGION, |
| 1006 | ANDROID_SENSOR_TIMESTAMP, |
| 1007 | ANDROID_SENSOR_ROLLING_SHUTTER_SKEW, |
| 1008 | ANDROID_SENSOR_EXPOSURE_TIME, |
| 1009 | ANDROID_STATISTICS_LENS_SHADING_MAP_MODE, |
| 1010 | ANDROID_STATISTICS_SCENE_FLICKER, |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1011 | ANDROID_JPEG_SIZE, |
| 1012 | ANDROID_JPEG_QUALITY, |
| 1013 | ANDROID_JPEG_ORIENTATION, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1014 | }; |
| 1015 | staticMetadata_->addEntry(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, |
| 1016 | availableResultKeys.data(), |
| 1017 | availableResultKeys.size()); |
| 1018 | |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1019 | if (!staticMetadata_->isValid()) { |
| 1020 | LOG(HAL, Error) << "Failed to construct static metadata"; |
| 1021 | delete staticMetadata_; |
| 1022 | staticMetadata_ = nullptr; |
| 1023 | return nullptr; |
| 1024 | } |
| 1025 | |
| 1026 | return staticMetadata_->get(); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1029 | CameraMetadata *CameraDevice::requestTemplatePreview() |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1030 | { |
Jacopo Mondi | 6370347 | 2019-09-04 16:18:22 +0200 | [diff] [blame] | 1031 | /* |
| 1032 | * \todo Keep this in sync with the actual number of entries. |
Jacopo Mondi | 09b1d0f | 2020-07-24 16:10:23 +0200 | [diff] [blame] | 1033 | * Currently: 20 entries, 35 bytes |
Jacopo Mondi | 6370347 | 2019-09-04 16:18:22 +0200 | [diff] [blame] | 1034 | */ |
Jacopo Mondi | 09b1d0f | 2020-07-24 16:10:23 +0200 | [diff] [blame] | 1035 | CameraMetadata *requestTemplate = new CameraMetadata(20, 35); |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1036 | if (!requestTemplate->isValid()) { |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1037 | delete requestTemplate; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1038 | return nullptr; |
| 1039 | } |
| 1040 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1041 | uint8_t aeMode = ANDROID_CONTROL_AE_MODE_ON; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1042 | requestTemplate->addEntry(ANDROID_CONTROL_AE_MODE, |
| 1043 | &aeMode, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1044 | |
| 1045 | int32_t aeExposureCompensation = 0; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1046 | requestTemplate->addEntry(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION, |
| 1047 | &aeExposureCompensation, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1048 | |
| 1049 | uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1050 | requestTemplate->addEntry(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, |
| 1051 | &aePrecaptureTrigger, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1052 | |
| 1053 | uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_OFF; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1054 | requestTemplate->addEntry(ANDROID_CONTROL_AE_LOCK, |
| 1055 | &aeLock, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1056 | |
Jacopo Mondi | 09b1d0f | 2020-07-24 16:10:23 +0200 | [diff] [blame] | 1057 | std::vector<int32_t> aeFpsTarget = { |
| 1058 | 15, 30, |
| 1059 | }; |
| 1060 | requestTemplate->addEntry(ANDROID_CONTROL_AE_TARGET_FPS_RANGE, |
| 1061 | aeFpsTarget.data(), |
| 1062 | aeFpsTarget.size()); |
| 1063 | |
| 1064 | uint8_t aeAntibandingMode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO; |
| 1065 | requestTemplate->addEntry(ANDROID_CONTROL_AE_ANTIBANDING_MODE, |
| 1066 | &aeAntibandingMode, 1); |
| 1067 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1068 | uint8_t afTrigger = ANDROID_CONTROL_AF_TRIGGER_IDLE; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1069 | requestTemplate->addEntry(ANDROID_CONTROL_AF_TRIGGER, |
| 1070 | &afTrigger, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1071 | |
| 1072 | uint8_t awbMode = ANDROID_CONTROL_AWB_MODE_AUTO; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1073 | requestTemplate->addEntry(ANDROID_CONTROL_AWB_MODE, |
| 1074 | &awbMode, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1075 | |
| 1076 | uint8_t awbLock = ANDROID_CONTROL_AWB_LOCK_OFF; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1077 | requestTemplate->addEntry(ANDROID_CONTROL_AWB_LOCK, |
| 1078 | &awbLock, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1079 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1080 | uint8_t flashMode = ANDROID_FLASH_MODE_OFF; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1081 | requestTemplate->addEntry(ANDROID_FLASH_MODE, |
| 1082 | &flashMode, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1083 | |
| 1084 | uint8_t faceDetectMode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1085 | requestTemplate->addEntry(ANDROID_STATISTICS_FACE_DETECT_MODE, |
| 1086 | &faceDetectMode, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1087 | |
Jacopo Mondi | 9b361dc | 2019-09-04 16:18:21 +0200 | [diff] [blame] | 1088 | uint8_t noiseReduction = ANDROID_NOISE_REDUCTION_MODE_OFF; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1089 | requestTemplate->addEntry(ANDROID_NOISE_REDUCTION_MODE, |
| 1090 | &noiseReduction, 1); |
Jacopo Mondi | 9b361dc | 2019-09-04 16:18:21 +0200 | [diff] [blame] | 1091 | |
| 1092 | uint8_t aberrationMode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1093 | requestTemplate->addEntry(ANDROID_COLOR_CORRECTION_ABERRATION_MODE, |
| 1094 | &aberrationMode, 1); |
Jacopo Mondi | 9b361dc | 2019-09-04 16:18:21 +0200 | [diff] [blame] | 1095 | |
Jacopo Mondi | 09b1d0f | 2020-07-24 16:10:23 +0200 | [diff] [blame] | 1096 | uint8_t controlMode = ANDROID_CONTROL_MODE_AUTO; |
| 1097 | requestTemplate->addEntry(ANDROID_CONTROL_MODE, &controlMode, 1); |
| 1098 | |
| 1099 | float lensAperture = 2.53 / 100; |
| 1100 | requestTemplate->addEntry(ANDROID_LENS_APERTURE, &lensAperture, 1); |
| 1101 | |
| 1102 | uint8_t opticalStabilization = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF; |
| 1103 | requestTemplate->addEntry(ANDROID_LENS_OPTICAL_STABILIZATION_MODE, |
| 1104 | &opticalStabilization, 1); |
| 1105 | |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1106 | uint8_t captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1107 | requestTemplate->addEntry(ANDROID_CONTROL_CAPTURE_INTENT, |
| 1108 | &captureIntent, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1109 | |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1110 | return requestTemplate; |
| 1111 | } |
| 1112 | |
| 1113 | /* |
| 1114 | * Produce a metadata pack to be used as template for a capture request. |
| 1115 | */ |
| 1116 | const camera_metadata_t *CameraDevice::constructDefaultRequestSettings(int type) |
| 1117 | { |
| 1118 | auto it = requestTemplates_.find(type); |
| 1119 | if (it != requestTemplates_.end()) |
| 1120 | return it->second->get(); |
| 1121 | |
| 1122 | /* Use the capture intent matching the requested template type. */ |
| 1123 | CameraMetadata *requestTemplate; |
| 1124 | uint8_t captureIntent; |
| 1125 | switch (type) { |
| 1126 | case CAMERA3_TEMPLATE_PREVIEW: |
| 1127 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW; |
| 1128 | break; |
| 1129 | case CAMERA3_TEMPLATE_STILL_CAPTURE: |
| 1130 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE; |
| 1131 | break; |
| 1132 | case CAMERA3_TEMPLATE_VIDEO_RECORD: |
| 1133 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD; |
| 1134 | break; |
| 1135 | case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT: |
| 1136 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT; |
| 1137 | break; |
| 1138 | case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: |
| 1139 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG; |
| 1140 | break; |
| 1141 | case CAMERA3_TEMPLATE_MANUAL: |
| 1142 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_MANUAL; |
| 1143 | break; |
| 1144 | default: |
| 1145 | LOG(HAL, Error) << "Invalid template request type: " << type; |
| 1146 | return nullptr; |
| 1147 | } |
| 1148 | |
| 1149 | requestTemplate = requestTemplatePreview(); |
| 1150 | if (!requestTemplate || !requestTemplate->isValid()) { |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1151 | LOG(HAL, Error) << "Failed to construct request template"; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1152 | delete requestTemplate; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1153 | return nullptr; |
| 1154 | } |
| 1155 | |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1156 | requestTemplate->updateEntry(ANDROID_CONTROL_CAPTURE_INTENT, |
| 1157 | &captureIntent, 1); |
| 1158 | |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1159 | requestTemplates_[type] = requestTemplate; |
| 1160 | return requestTemplate->get(); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1161 | } |
| 1162 | |
Kieran Bingham | 43e3b80 | 2020-06-26 09:58:49 +0100 | [diff] [blame] | 1163 | PixelFormat CameraDevice::toPixelFormat(int format) |
| 1164 | { |
| 1165 | /* Translate Android format code to libcamera pixel format. */ |
| 1166 | auto it = formatsMap_.find(format); |
| 1167 | if (it == formatsMap_.end()) { |
| 1168 | LOG(HAL, Error) << "Requested format " << utils::hex(format) |
| 1169 | << " not supported"; |
| 1170 | return PixelFormat(); |
| 1171 | } |
| 1172 | |
| 1173 | return it->second; |
| 1174 | } |
| 1175 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1176 | /* |
| 1177 | * Inspect the stream_list to produce a list of StreamConfiguration to |
| 1178 | * be use to configure the Camera. |
| 1179 | */ |
| 1180 | int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) |
| 1181 | { |
Kieran Bingham | 0a9244e | 2020-06-26 20:19:10 +0100 | [diff] [blame] | 1182 | /* |
| 1183 | * Generate an empty configuration, and construct a StreamConfiguration |
| 1184 | * for each camera3_stream to add to it. |
| 1185 | */ |
| 1186 | config_ = camera_->generateConfiguration(); |
| 1187 | if (!config_) { |
| 1188 | LOG(HAL, Error) << "Failed to generate camera configuration"; |
| 1189 | return -EINVAL; |
| 1190 | } |
| 1191 | |
Kieran Bingham | 2f34f5e | 2020-07-01 16:42:13 +0100 | [diff] [blame] | 1192 | /* |
| 1193 | * Clear and remove any existing configuration from previous calls, and |
| 1194 | * ensure the required entries are available without further |
Jacopo Mondi | 9ac8f3e | 2020-09-04 15:25:55 +0200 | [diff] [blame] | 1195 | * reallocation. |
Kieran Bingham | 2f34f5e | 2020-07-01 16:42:13 +0100 | [diff] [blame] | 1196 | */ |
| 1197 | streams_.clear(); |
| 1198 | streams_.reserve(stream_list->num_streams); |
| 1199 | |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1200 | /* First handle all non-MJPEG streams. */ |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1201 | camera3_stream_t *jpegStream = nullptr; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1202 | for (unsigned int i = 0; i < stream_list->num_streams; ++i) { |
| 1203 | camera3_stream_t *stream = stream_list->streams[i]; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1204 | Size size(stream->width, stream->height); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1205 | |
Kieran Bingham | 43e3b80 | 2020-06-26 09:58:49 +0100 | [diff] [blame] | 1206 | PixelFormat format = toPixelFormat(stream->format); |
| 1207 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1208 | LOG(HAL, Info) << "Stream #" << i |
| 1209 | << ", direction: " << stream->stream_type |
| 1210 | << ", width: " << stream->width |
| 1211 | << ", height: " << stream->height |
Kieran Bingham | 43e3b80 | 2020-06-26 09:58:49 +0100 | [diff] [blame] | 1212 | << ", format: " << utils::hex(stream->format) |
| 1213 | << " (" << format.toString() << ")"; |
Kieran Bingham | 0a9244e | 2020-06-26 20:19:10 +0100 | [diff] [blame] | 1214 | |
| 1215 | if (!format.isValid()) |
| 1216 | return -EINVAL; |
| 1217 | |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1218 | /* Defer handling of MJPEG streams until all others are known. */ |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1219 | if (stream->format == HAL_PIXEL_FORMAT_BLOB) { |
| 1220 | if (jpegStream) { |
| 1221 | LOG(HAL, Error) |
| 1222 | << "Multiple JPEG streams are not supported"; |
| 1223 | return -EINVAL; |
| 1224 | } |
| 1225 | |
| 1226 | jpegStream = stream; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1227 | continue; |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1228 | } |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1229 | |
Kieran Bingham | 0a9244e | 2020-06-26 20:19:10 +0100 | [diff] [blame] | 1230 | StreamConfiguration streamConfiguration; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1231 | streamConfiguration.size = size; |
Kieran Bingham | 0a9244e | 2020-06-26 20:19:10 +0100 | [diff] [blame] | 1232 | streamConfiguration.pixelFormat = format; |
| 1233 | |
| 1234 | config_->addConfiguration(streamConfiguration); |
Jacopo Mondi | e3393f1 | 2020-10-03 12:00:54 +0200 | [diff] [blame] | 1235 | streams_.emplace_back(this, CameraStream::Type::Direct, |
| 1236 | stream, config_->size() - 1); |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1237 | stream->priv = static_cast<void *>(&streams_.back()); |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1238 | } |
| 1239 | |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1240 | /* Now handle the MJPEG streams, adding a new stream if required. */ |
| 1241 | if (jpegStream) { |
Jacopo Mondi | 94c4d49 | 2020-09-04 16:06:11 +0200 | [diff] [blame] | 1242 | CameraStream::Type type; |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1243 | int index = -1; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1244 | |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1245 | /* Search for a compatible stream in the non-JPEG ones. */ |
| 1246 | for (unsigned int i = 0; i < config_->size(); i++) { |
| 1247 | StreamConfiguration &cfg = config_->at(i); |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1248 | |
| 1249 | /* |
| 1250 | * \todo The PixelFormat must also be compatible with |
| 1251 | * the encoder. |
| 1252 | */ |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1253 | if (cfg.size.width != jpegStream->width || |
| 1254 | cfg.size.height != jpegStream->height) |
| 1255 | continue; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1256 | |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1257 | LOG(HAL, Info) |
| 1258 | << "Android JPEG stream mapped to libcamera stream " << i; |
| 1259 | |
Jacopo Mondi | 94c4d49 | 2020-09-04 16:06:11 +0200 | [diff] [blame] | 1260 | type = CameraStream::Type::Mapped; |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1261 | index = i; |
| 1262 | break; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | /* |
| 1266 | * Without a compatible match for JPEG encoding we must |
| 1267 | * introduce a new stream to satisfy the request requirements. |
| 1268 | */ |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1269 | if (index < 0) { |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1270 | StreamConfiguration streamConfiguration; |
| 1271 | |
| 1272 | /* |
| 1273 | * \todo The pixelFormat should be a 'best-fit' choice |
| 1274 | * and may require a validation cycle. This is not yet |
| 1275 | * handled, and should be considered as part of any |
| 1276 | * stream configuration reworks. |
| 1277 | */ |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1278 | streamConfiguration.size.width = jpegStream->width; |
| 1279 | streamConfiguration.size.height = jpegStream->height; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1280 | streamConfiguration.pixelFormat = formats::NV12; |
| 1281 | |
| 1282 | LOG(HAL, Info) << "Adding " << streamConfiguration.toString() |
| 1283 | << " for MJPEG support"; |
| 1284 | |
Jacopo Mondi | 94c4d49 | 2020-09-04 16:06:11 +0200 | [diff] [blame] | 1285 | type = CameraStream::Type::Internal; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1286 | config_->addConfiguration(streamConfiguration); |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1287 | index = config_->size() - 1; |
| 1288 | } |
| 1289 | |
Jacopo Mondi | e3393f1 | 2020-10-03 12:00:54 +0200 | [diff] [blame] | 1290 | streams_.emplace_back(this, type, jpegStream, index); |
Jacopo Mondi | facadb1 | 2020-09-02 12:11:46 +0200 | [diff] [blame] | 1291 | jpegStream->priv = static_cast<void *>(&streams_.back()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1292 | } |
| 1293 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1294 | switch (config_->validate()) { |
| 1295 | case CameraConfiguration::Valid: |
| 1296 | break; |
| 1297 | case CameraConfiguration::Adjusted: |
| 1298 | LOG(HAL, Info) << "Camera configuration adjusted"; |
Kieran Bingham | 9f07aeb | 2020-07-20 22:52:14 +0100 | [diff] [blame] | 1299 | |
| 1300 | for (const StreamConfiguration &cfg : *config_) |
| 1301 | LOG(HAL, Info) << " - " << cfg.toString(); |
| 1302 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1303 | config_.reset(); |
| 1304 | return -EINVAL; |
| 1305 | case CameraConfiguration::Invalid: |
| 1306 | LOG(HAL, Info) << "Camera configuration invalid"; |
| 1307 | config_.reset(); |
| 1308 | return -EINVAL; |
| 1309 | } |
| 1310 | |
Jacopo Mondi | 3c84d88 | 2020-10-02 19:40:42 +0200 | [diff] [blame] | 1311 | /* |
Jacopo Mondi | b84e35d | 2020-10-03 13:21:50 +0200 | [diff] [blame] | 1312 | * Once the CameraConfiguration has been adjusted/validated |
| 1313 | * it can be applied to the camera. |
| 1314 | */ |
| 1315 | int ret = camera_->configure(config_.get()); |
| 1316 | if (ret) { |
| 1317 | LOG(HAL, Error) << "Failed to configure camera '" |
| 1318 | << camera_->id() << "'"; |
| 1319 | return ret; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
Jacopo Mondi | 3c84d88 | 2020-10-02 19:40:42 +0200 | [diff] [blame] | 1323 | * Configure the HAL CameraStream instances using the associated |
| 1324 | * StreamConfiguration and set the number of required buffers in |
| 1325 | * the Android camera3_stream_t. |
| 1326 | */ |
Jacopo Mondi | 6c8837d | 2020-10-03 13:45:29 +0200 | [diff] [blame] | 1327 | for (CameraStream &cameraStream : streams_) { |
Kieran Bingham | c7bcae0 | 2020-10-13 15:58:26 +0100 | [diff] [blame] | 1328 | ret = cameraStream.configure(); |
Jacopo Mondi | 6c8837d | 2020-10-03 13:45:29 +0200 | [diff] [blame] | 1329 | if (ret) { |
| 1330 | LOG(HAL, Error) << "Failed to configure camera stream"; |
Jacopo Mondi | 3c84d88 | 2020-10-02 19:40:42 +0200 | [diff] [blame] | 1331 | return ret; |
Jacopo Mondi | 6c8837d | 2020-10-03 13:45:29 +0200 | [diff] [blame] | 1332 | } |
Kieran Bingham | 0a9244e | 2020-06-26 20:19:10 +0100 | [diff] [blame] | 1333 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1334 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1335 | return 0; |
| 1336 | } |
| 1337 | |
Kieran Bingham | 74ab442 | 2020-07-01 13:25:38 +0100 | [diff] [blame] | 1338 | FrameBuffer *CameraDevice::createFrameBuffer(const buffer_handle_t camera3buffer) |
| 1339 | { |
| 1340 | std::vector<FrameBuffer::Plane> planes; |
Kieran Bingham | f4c65be | 2020-07-06 16:12:03 +0100 | [diff] [blame] | 1341 | for (int i = 0; i < camera3buffer->numFds; i++) { |
| 1342 | /* Skip unused planes. */ |
| 1343 | if (camera3buffer->data[i] == -1) |
| 1344 | break; |
| 1345 | |
Kieran Bingham | 74ab442 | 2020-07-01 13:25:38 +0100 | [diff] [blame] | 1346 | FrameBuffer::Plane plane; |
| 1347 | plane.fd = FileDescriptor(camera3buffer->data[i]); |
Kieran Bingham | f4c65be | 2020-07-06 16:12:03 +0100 | [diff] [blame] | 1348 | if (!plane.fd.isValid()) { |
| 1349 | LOG(HAL, Error) << "Failed to obtain FileDescriptor (" |
| 1350 | << camera3buffer->data[i] << ") " |
| 1351 | << " on plane " << i; |
| 1352 | return nullptr; |
| 1353 | } |
| 1354 | |
Kieran Bingham | 6bc652e | 2020-07-17 16:18:32 +0100 | [diff] [blame] | 1355 | off_t length = lseek(plane.fd.fd(), 0, SEEK_END); |
| 1356 | if (length == -1) { |
| 1357 | LOG(HAL, Error) << "Failed to query plane length"; |
| 1358 | return nullptr; |
| 1359 | } |
| 1360 | |
| 1361 | plane.length = length; |
Kieran Bingham | 74ab442 | 2020-07-01 13:25:38 +0100 | [diff] [blame] | 1362 | planes.push_back(std::move(plane)); |
| 1363 | } |
| 1364 | |
| 1365 | return new FrameBuffer(std::move(planes)); |
| 1366 | } |
| 1367 | |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 1368 | int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Request) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1369 | { |
Kieran Bingham | 61f4296 | 2020-07-01 13:40:17 +0100 | [diff] [blame] | 1370 | if (!camera3Request->num_output_buffers) { |
| 1371 | LOG(HAL, Error) << "No output buffers provided"; |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 1372 | return -EINVAL; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | /* Start the camera if that's the first request we handle. */ |
| 1376 | if (!running_) { |
Jacopo Mondi | 4b1aa21 | 2020-10-06 17:56:50 +0200 | [diff] [blame] | 1377 | worker_.start(); |
| 1378 | |
Niklas Söderlund | a1c5450 | 2019-11-25 17:51:06 +0100 | [diff] [blame] | 1379 | int ret = camera_->start(); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1380 | if (ret) { |
| 1381 | LOG(HAL, Error) << "Failed to start camera"; |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 1382 | return ret; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | running_ = true; |
| 1386 | } |
| 1387 | |
| 1388 | /* |
| 1389 | * Queue a request for the Camera with the provided dmabuf file |
| 1390 | * descriptors. |
| 1391 | */ |
| 1392 | const camera3_stream_buffer_t *camera3Buffers = |
| 1393 | camera3Request->output_buffers; |
| 1394 | |
| 1395 | /* |
| 1396 | * Save the request descriptors for use at completion time. |
| 1397 | * The descriptor and the associated memory reserved here are freed |
| 1398 | * at request complete time. |
| 1399 | */ |
| 1400 | Camera3RequestDescriptor *descriptor = |
Jacopo Mondi | 4b1aa21 | 2020-10-06 17:56:50 +0200 | [diff] [blame] | 1401 | new Camera3RequestDescriptor(camera_.get(), camera3Request->frame_number, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1402 | camera3Request->num_output_buffers); |
Kieran Bingham | eac0542 | 2020-07-01 13:28:16 +0100 | [diff] [blame] | 1403 | |
Jacopo Mondi | 35f726d | 2020-09-04 18:32:48 +0200 | [diff] [blame] | 1404 | LOG(HAL, Debug) << "Queueing Request to libcamera with " |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1405 | << descriptor->numBuffers_ << " HAL streams"; |
| 1406 | for (unsigned int i = 0; i < descriptor->numBuffers_; ++i) { |
Jacopo Mondi | 35f726d | 2020-09-04 18:32:48 +0200 | [diff] [blame] | 1407 | camera3_stream *camera3Stream = camera3Buffers[i].stream; |
Kieran Bingham | 0cfdf73 | 2020-07-01 13:36:24 +0100 | [diff] [blame] | 1408 | CameraStream *cameraStream = |
| 1409 | static_cast<CameraStream *>(camera3Buffers[i].stream->priv); |
| 1410 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1411 | /* |
| 1412 | * Keep track of which stream the request belongs to and store |
| 1413 | * the native buffer handles. |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1414 | */ |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1415 | descriptor->buffers_[i].stream = camera3Buffers[i].stream; |
| 1416 | descriptor->buffers_[i].buffer = camera3Buffers[i].buffer; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1417 | |
Jacopo Mondi | 35f726d | 2020-09-04 18:32:48 +0200 | [diff] [blame] | 1418 | std::stringstream ss; |
| 1419 | ss << i << " - (" << camera3Stream->width << "x" |
| 1420 | << camera3Stream->height << ")" |
| 1421 | << "[" << utils::hex(camera3Stream->format) << "] -> " |
| 1422 | << "(" << cameraStream->configuration().size.toString() << ")[" |
| 1423 | << cameraStream->configuration().pixelFormat.toString() << "]"; |
| 1424 | |
Jacopo Mondi | de8304b | 2020-09-04 17:45:39 +0200 | [diff] [blame] | 1425 | /* |
| 1426 | * Inspect the camera stream type, create buffers opportunely |
| 1427 | * and add them to the Request if required. |
| 1428 | */ |
| 1429 | FrameBuffer *buffer = nullptr; |
| 1430 | switch (cameraStream->type()) { |
| 1431 | case CameraStream::Type::Mapped: |
| 1432 | /* |
| 1433 | * Mapped streams don't need buffers added to the |
| 1434 | * Request. |
| 1435 | */ |
Jacopo Mondi | 35f726d | 2020-09-04 18:32:48 +0200 | [diff] [blame] | 1436 | LOG(HAL, Debug) << ss.str() << " (mapped)"; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1437 | continue; |
| 1438 | |
Jacopo Mondi | de8304b | 2020-09-04 17:45:39 +0200 | [diff] [blame] | 1439 | case CameraStream::Type::Direct: |
| 1440 | /* |
| 1441 | * Create a libcamera buffer using the dmabuf |
| 1442 | * descriptors of the camera3Buffer for each stream and |
| 1443 | * associate it with the Camera3RequestDescriptor for |
| 1444 | * lifetime management only. |
| 1445 | */ |
| 1446 | buffer = createFrameBuffer(*camera3Buffers[i].buffer); |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1447 | descriptor->frameBuffers_.emplace_back(buffer); |
Jacopo Mondi | 35f726d | 2020-09-04 18:32:48 +0200 | [diff] [blame] | 1448 | LOG(HAL, Debug) << ss.str() << " (direct)"; |
Jacopo Mondi | de8304b | 2020-09-04 17:45:39 +0200 | [diff] [blame] | 1449 | break; |
| 1450 | |
| 1451 | case CameraStream::Type::Internal: |
| 1452 | /* |
| 1453 | * Get the frame buffer from the CameraStream internal |
| 1454 | * buffer pool. |
| 1455 | * |
| 1456 | * The buffer has to be returned to the CameraStream |
| 1457 | * once it has been processed. |
| 1458 | */ |
| 1459 | buffer = cameraStream->getBuffer(); |
Jacopo Mondi | 35f726d | 2020-09-04 18:32:48 +0200 | [diff] [blame] | 1460 | LOG(HAL, Debug) << ss.str() << " (internal)"; |
Jacopo Mondi | de8304b | 2020-09-04 17:45:39 +0200 | [diff] [blame] | 1461 | break; |
| 1462 | } |
| 1463 | |
Kieran Bingham | 0cfdf73 | 2020-07-01 13:36:24 +0100 | [diff] [blame] | 1464 | if (!buffer) { |
| 1465 | LOG(HAL, Error) << "Failed to create buffer"; |
Kieran Bingham | 0cfdf73 | 2020-07-01 13:36:24 +0100 | [diff] [blame] | 1466 | delete descriptor; |
| 1467 | return -ENOMEM; |
| 1468 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1469 | |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1470 | descriptor->request_->addBuffer(cameraStream->stream(), buffer, |
| 1471 | camera3Buffers[i].acquire_fence); |
Kieran Bingham | 0cfdf73 | 2020-07-01 13:36:24 +0100 | [diff] [blame] | 1472 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1473 | |
Jacopo Mondi | 4b1aa21 | 2020-10-06 17:56:50 +0200 | [diff] [blame] | 1474 | /* Queue the request to the CameraWorker. */ |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1475 | worker_.queueRequest(descriptor->request_.get()); |
Paul Elder | c753223 | 2020-09-23 19:05:41 +0900 | [diff] [blame] | 1476 | |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 1477 | return 0; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1478 | } |
| 1479 | |
Niklas Söderlund | f7ddfd4 | 2019-10-21 20:01:19 +0200 | [diff] [blame] | 1480 | void CameraDevice::requestComplete(Request *request) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1481 | { |
Niklas Söderlund | dac8e95 | 2020-08-11 00:56:13 +0200 | [diff] [blame] | 1482 | const Request::BufferMap &buffers = request->buffers(); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1483 | camera3_buffer_status status = CAMERA3_BUFFER_STATUS_OK; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1484 | std::unique_ptr<CameraMetadata> resultMetadata; |
Kieran Bingham | c09aee4 | 2020-07-28 14:01:19 +0100 | [diff] [blame] | 1485 | Camera3RequestDescriptor *descriptor = |
| 1486 | reinterpret_cast<Camera3RequestDescriptor *>(request->cookie()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1487 | |
| 1488 | if (request->status() != Request::RequestComplete) { |
Kieran Bingham | 98986e0 | 2020-08-03 14:34:11 +0100 | [diff] [blame] | 1489 | LOG(HAL, Error) << "Request not successfully completed: " |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1490 | << request->status(); |
| 1491 | status = CAMERA3_BUFFER_STATUS_ERROR; |
| 1492 | } |
| 1493 | |
Kieran Bingham | c09aee4 | 2020-07-28 14:01:19 +0100 | [diff] [blame] | 1494 | /* |
| 1495 | * \todo The timestamp used for the metadata is currently always taken |
| 1496 | * from the first buffer (which may be the first stream) in the Request. |
| 1497 | * It might be appropriate to return a 'correct' (as determined by |
| 1498 | * pipeline handlers) timestamp in the Request itself. |
| 1499 | */ |
Kieran Bingham | e1f9fdb | 2020-10-21 15:31:10 +0100 | [diff] [blame] | 1500 | uint64_t timestamp = buffers.at(0)->metadata().timestamp; |
| 1501 | resultMetadata = getResultMetadata(descriptor->frameNumber_, timestamp); |
Kieran Bingham | c09aee4 | 2020-07-28 14:01:19 +0100 | [diff] [blame] | 1502 | |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1503 | /* Handle any JPEG compression. */ |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1504 | for (unsigned int i = 0; i < descriptor->numBuffers_; ++i) { |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1505 | CameraStream *cameraStream = |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1506 | static_cast<CameraStream *>(descriptor->buffers_[i].stream->priv); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1507 | |
Jacopo Mondi | 160bb09 | 2020-10-02 20:48:35 +0200 | [diff] [blame] | 1508 | if (cameraStream->camera3Stream().format != HAL_PIXEL_FORMAT_BLOB) |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1509 | continue; |
| 1510 | |
Jacopo Mondi | 216030a | 2020-10-03 11:36:41 +0200 | [diff] [blame] | 1511 | FrameBuffer *buffer = request->findBuffer(cameraStream->stream()); |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1512 | if (!buffer) { |
| 1513 | LOG(HAL, Error) << "Failed to find a source stream buffer"; |
| 1514 | continue; |
| 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | * \todo Buffer mapping and compression should be moved to a |
| 1519 | * separate thread. |
| 1520 | */ |
| 1521 | |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1522 | MappedCamera3Buffer mapped(*descriptor->buffers_[i].buffer, |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1523 | PROT_READ | PROT_WRITE); |
| 1524 | if (!mapped.isValid()) { |
| 1525 | LOG(HAL, Error) << "Failed to mmap android blob buffer"; |
| 1526 | continue; |
| 1527 | } |
| 1528 | |
Jacopo Mondi | 9e95d5e | 2020-10-03 11:28:47 +0200 | [diff] [blame] | 1529 | int ret = cameraStream->process(*buffer, &mapped, |
| 1530 | resultMetadata.get()); |
| 1531 | if (ret) { |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1532 | status = CAMERA3_BUFFER_STATUS_ERROR; |
| 1533 | continue; |
| 1534 | } |
Jacopo Mondi | de8304b | 2020-09-04 17:45:39 +0200 | [diff] [blame] | 1535 | |
| 1536 | /* |
| 1537 | * Return the FrameBuffer to the CameraStream now that we're |
| 1538 | * done processing it. |
| 1539 | */ |
| 1540 | if (cameraStream->type() == CameraStream::Type::Internal) |
| 1541 | cameraStream->putBuffer(buffer); |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | /* Prepare to call back the Android camera stack. */ |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1545 | camera3_capture_result_t captureResult = {}; |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1546 | captureResult.frame_number = descriptor->frameNumber_; |
| 1547 | captureResult.num_output_buffers = descriptor->numBuffers_; |
| 1548 | for (unsigned int i = 0; i < descriptor->numBuffers_; ++i) { |
| 1549 | descriptor->buffers_[i].acquire_fence = -1; |
| 1550 | descriptor->buffers_[i].release_fence = -1; |
| 1551 | descriptor->buffers_[i].status = status; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1552 | } |
| 1553 | captureResult.output_buffers = |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1554 | const_cast<const camera3_stream_buffer_t *>(descriptor->buffers_); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1555 | |
Kieran Bingham | 0cfdf73 | 2020-07-01 13:36:24 +0100 | [diff] [blame] | 1556 | |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1557 | if (status == CAMERA3_BUFFER_STATUS_OK) { |
Kieran Bingham | e1f9fdb | 2020-10-21 15:31:10 +0100 | [diff] [blame] | 1558 | notifyShutter(descriptor->frameNumber_, timestamp); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1559 | |
| 1560 | captureResult.partial_result = 1; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1561 | captureResult.result = resultMetadata->get(); |
| 1562 | } |
| 1563 | |
| 1564 | if (status == CAMERA3_BUFFER_STATUS_ERROR || !captureResult.result) { |
| 1565 | /* \todo Improve error handling. In case we notify an error |
| 1566 | * because the metadata generation fails, a shutter event has |
| 1567 | * already been notified for this frame number before the error |
| 1568 | * is here signalled. Make sure the error path plays well with |
| 1569 | * the camera stack state machine. |
| 1570 | */ |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1571 | notifyError(descriptor->frameNumber_, |
| 1572 | descriptor->buffers_[0].stream); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | callbacks_->process_capture_result(callbacks_, &captureResult); |
| 1576 | |
| 1577 | delete descriptor; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1578 | } |
| 1579 | |
Jacopo Mondi | a7b9277 | 2020-05-26 15:41:41 +0200 | [diff] [blame] | 1580 | std::string CameraDevice::logPrefix() const |
| 1581 | { |
Niklas Söderlund | 2e7c80a | 2020-08-02 23:57:17 +0200 | [diff] [blame] | 1582 | return "'" + camera_->id() + "'"; |
Jacopo Mondi | a7b9277 | 2020-05-26 15:41:41 +0200 | [diff] [blame] | 1583 | } |
| 1584 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1585 | void CameraDevice::notifyShutter(uint32_t frameNumber, uint64_t timestamp) |
| 1586 | { |
| 1587 | camera3_notify_msg_t notify = {}; |
| 1588 | |
| 1589 | notify.type = CAMERA3_MSG_SHUTTER; |
| 1590 | notify.message.shutter.frame_number = frameNumber; |
| 1591 | notify.message.shutter.timestamp = timestamp; |
| 1592 | |
| 1593 | callbacks_->notify(callbacks_, ¬ify); |
| 1594 | } |
| 1595 | |
| 1596 | void CameraDevice::notifyError(uint32_t frameNumber, camera3_stream_t *stream) |
| 1597 | { |
| 1598 | camera3_notify_msg_t notify = {}; |
| 1599 | |
Kieran Bingham | 8ad1b9c | 2020-07-01 16:47:58 +0100 | [diff] [blame] | 1600 | /* |
| 1601 | * \todo Report and identify the stream number or configuration to |
| 1602 | * clarify the stream that failed. |
| 1603 | */ |
| 1604 | LOG(HAL, Error) << "Error occurred on frame " << frameNumber << " (" |
| 1605 | << toPixelFormat(stream->format).toString() << ")"; |
| 1606 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1607 | notify.type = CAMERA3_MSG_ERROR; |
| 1608 | notify.message.error.error_stream = stream; |
| 1609 | notify.message.error.frame_number = frameNumber; |
| 1610 | notify.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST; |
| 1611 | |
| 1612 | callbacks_->notify(callbacks_, ¬ify); |
| 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * Produce a set of fixed result metadata. |
| 1617 | */ |
Laurent Pinchart | dbafe16 | 2019-10-27 00:36:13 +0300 | [diff] [blame] | 1618 | std::unique_ptr<CameraMetadata> |
| 1619 | CameraDevice::getResultMetadata([[maybe_unused]] int frame_number, |
| 1620 | int64_t timestamp) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1621 | { |
Jacopo Mondi | 48504ba | 2019-09-04 16:18:19 +0200 | [diff] [blame] | 1622 | /* |
| 1623 | * \todo Keep this in sync with the actual number of entries. |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1624 | * Currently: 18 entries, 62 bytes |
Jacopo Mondi | 48504ba | 2019-09-04 16:18:19 +0200 | [diff] [blame] | 1625 | */ |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1626 | std::unique_ptr<CameraMetadata> resultMetadata = |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1627 | std::make_unique<CameraMetadata>(18, 62); |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1628 | if (!resultMetadata->isValid()) { |
| 1629 | LOG(HAL, Error) << "Failed to allocate static metadata"; |
| 1630 | return nullptr; |
| 1631 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1632 | |
| 1633 | const uint8_t ae_state = ANDROID_CONTROL_AE_STATE_CONVERGED; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1634 | resultMetadata->addEntry(ANDROID_CONTROL_AE_STATE, &ae_state, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1635 | |
| 1636 | const uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1637 | resultMetadata->addEntry(ANDROID_CONTROL_AE_LOCK, &ae_lock, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1638 | |
| 1639 | uint8_t af_state = ANDROID_CONTROL_AF_STATE_INACTIVE; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1640 | resultMetadata->addEntry(ANDROID_CONTROL_AF_STATE, &af_state, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1641 | |
| 1642 | const uint8_t awb_state = ANDROID_CONTROL_AWB_STATE_CONVERGED; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1643 | resultMetadata->addEntry(ANDROID_CONTROL_AWB_STATE, &awb_state, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1644 | |
| 1645 | const uint8_t awb_lock = ANDROID_CONTROL_AWB_LOCK_OFF; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1646 | resultMetadata->addEntry(ANDROID_CONTROL_AWB_LOCK, &awb_lock, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1647 | |
| 1648 | const uint8_t lens_state = ANDROID_LENS_STATE_STATIONARY; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1649 | resultMetadata->addEntry(ANDROID_LENS_STATE, &lens_state, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1650 | |
| 1651 | int32_t sensorSizes[] = { |
| 1652 | 0, 0, 2560, 1920, |
| 1653 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1654 | resultMetadata->addEntry(ANDROID_SCALER_CROP_REGION, sensorSizes, 4); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1655 | |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1656 | resultMetadata->addEntry(ANDROID_SENSOR_TIMESTAMP, ×tamp, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1657 | |
| 1658 | /* 33.3 msec */ |
| 1659 | const int64_t rolling_shutter_skew = 33300000; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1660 | resultMetadata->addEntry(ANDROID_SENSOR_ROLLING_SHUTTER_SKEW, |
| 1661 | &rolling_shutter_skew, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1662 | |
| 1663 | /* 16.6 msec */ |
| 1664 | const int64_t exposure_time = 16600000; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1665 | resultMetadata->addEntry(ANDROID_SENSOR_EXPOSURE_TIME, |
| 1666 | &exposure_time, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1667 | |
| 1668 | const uint8_t lens_shading_map_mode = |
| 1669 | ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1670 | resultMetadata->addEntry(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE, |
| 1671 | &lens_shading_map_mode, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1672 | |
| 1673 | const uint8_t scene_flicker = ANDROID_STATISTICS_SCENE_FLICKER_NONE; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1674 | resultMetadata->addEntry(ANDROID_STATISTICS_SCENE_FLICKER, |
| 1675 | &scene_flicker, 1); |
| 1676 | |
| 1677 | /* |
| 1678 | * Return the result metadata pack even is not valid: get() will return |
| 1679 | * nullptr. |
| 1680 | */ |
| 1681 | if (!resultMetadata->isValid()) { |
| 1682 | LOG(HAL, Error) << "Failed to construct result metadata"; |
| 1683 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1684 | |
| 1685 | return resultMetadata; |
| 1686 | } |