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