Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | /* |
| 3 | * Copyright (C) 2019, Google Inc. |
| 4 | * |
| 5 | * camera_device.cpp - libcamera Android Camera Device |
| 6 | */ |
| 7 | |
| 8 | #include "camera_device.h" |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 9 | #include "camera_ops.h" |
Umang Jain | b2b8c4d | 2020-10-16 11:07:54 +0530 | [diff] [blame] | 10 | #include "post_processor.h" |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 11 | |
Jacopo Mondi | edd4b1d | 2021-01-04 16:18:29 +0100 | [diff] [blame] | 12 | #include <cmath> |
Paul Elder | 229653a | 2021-01-21 17:44:14 +0900 | [diff] [blame] | 13 | #include <fstream> |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 14 | #include <sys/mman.h> |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 15 | #include <tuple> |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Jacopo Mondi | 5d91c8d | 2020-09-23 09:41:52 +0200 | [diff] [blame] | 18 | #include <libcamera/control_ids.h> |
Jacopo Mondi | 857a216 | 2019-11-20 17:00:49 +0100 | [diff] [blame] | 19 | #include <libcamera/controls.h> |
Laurent Pinchart | 8b7e073 | 2020-05-22 04:02:06 +0300 | [diff] [blame] | 20 | #include <libcamera/formats.h> |
Jacopo Mondi | 857a216 | 2019-11-20 17:00:49 +0100 | [diff] [blame] | 21 | #include <libcamera/property_ids.h> |
| 22 | |
Niklas Söderlund | 7876d63 | 2020-07-21 00:16:24 +0200 | [diff] [blame] | 23 | #include "libcamera/internal/formats.h" |
Laurent Pinchart | 93e72b6 | 2020-05-12 00:58:34 +0300 | [diff] [blame] | 24 | #include "libcamera/internal/log.h" |
| 25 | #include "libcamera/internal/utils.h" |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 26 | |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 27 | #include "system/graphics.h" |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 28 | |
| 29 | using namespace libcamera; |
| 30 | |
Hirokazu Honda | 26d90af | 2020-12-11 09:53:36 +0000 | [diff] [blame] | 31 | LOG_DECLARE_CATEGORY(HAL) |
| 32 | |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | /* |
| 36 | * \var camera3Resolutions |
| 37 | * \brief The list of image resolutions defined as mandatory to be supported by |
| 38 | * the Android Camera3 specification |
| 39 | */ |
| 40 | const std::vector<Size> camera3Resolutions = { |
| 41 | { 320, 240 }, |
| 42 | { 640, 480 }, |
| 43 | { 1280, 720 }, |
| 44 | { 1920, 1080 } |
| 45 | }; |
| 46 | |
| 47 | /* |
| 48 | * \struct Camera3Format |
| 49 | * \brief Data associated with an Android format identifier |
| 50 | * \var libcameraFormats List of libcamera pixel formats compatible with the |
| 51 | * Android format |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 52 | * \var name The human-readable representation of the Android format code |
| 53 | */ |
| 54 | struct Camera3Format { |
| 55 | std::vector<PixelFormat> libcameraFormats; |
Niklas Söderlund | 8c1fedc | 2020-07-28 19:43:12 +0200 | [diff] [blame] | 56 | bool mandatory; |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 57 | const char *name; |
| 58 | }; |
| 59 | |
| 60 | /* |
| 61 | * \var camera3FormatsMap |
| 62 | * \brief Associate Android format code with ancillary data |
| 63 | */ |
| 64 | const std::map<int, const Camera3Format> camera3FormatsMap = { |
| 65 | { |
| 66 | HAL_PIXEL_FORMAT_BLOB, { |
Laurent Pinchart | 8b7e073 | 2020-05-22 04:02:06 +0300 | [diff] [blame] | 67 | { formats::MJPEG }, |
Niklas Söderlund | 8c1fedc | 2020-07-28 19:43:12 +0200 | [diff] [blame] | 68 | true, |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 69 | "BLOB" |
| 70 | } |
| 71 | }, { |
| 72 | HAL_PIXEL_FORMAT_YCbCr_420_888, { |
Laurent Pinchart | 8b7e073 | 2020-05-22 04:02:06 +0300 | [diff] [blame] | 73 | { formats::NV12, formats::NV21 }, |
Niklas Söderlund | 8c1fedc | 2020-07-28 19:43:12 +0200 | [diff] [blame] | 74 | true, |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 75 | "YCbCr_420_888" |
| 76 | } |
| 77 | }, { |
| 78 | /* |
| 79 | * \todo Translate IMPLEMENTATION_DEFINED inspecting the gralloc |
| 80 | * usage flag. For now, copy the YCbCr_420 configuration. |
| 81 | */ |
| 82 | HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, { |
Laurent Pinchart | 8b7e073 | 2020-05-22 04:02:06 +0300 | [diff] [blame] | 83 | { formats::NV12, formats::NV21 }, |
Niklas Söderlund | 8c1fedc | 2020-07-28 19:43:12 +0200 | [diff] [blame] | 84 | true, |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 85 | "IMPLEMENTATION_DEFINED" |
| 86 | } |
Niklas Söderlund | d4de037 | 2020-07-21 00:16:14 +0200 | [diff] [blame] | 87 | }, { |
| 88 | HAL_PIXEL_FORMAT_RAW10, { |
| 89 | { |
| 90 | formats::SBGGR10_CSI2P, |
| 91 | formats::SGBRG10_CSI2P, |
| 92 | formats::SGRBG10_CSI2P, |
| 93 | formats::SRGGB10_CSI2P |
| 94 | }, |
| 95 | false, |
| 96 | "RAW10" |
| 97 | } |
| 98 | }, { |
| 99 | HAL_PIXEL_FORMAT_RAW12, { |
| 100 | { |
| 101 | formats::SBGGR12_CSI2P, |
| 102 | formats::SGBRG12_CSI2P, |
| 103 | formats::SGRBG12_CSI2P, |
| 104 | formats::SRGGB12_CSI2P |
| 105 | }, |
| 106 | false, |
| 107 | "RAW12" |
| 108 | } |
| 109 | }, { |
| 110 | HAL_PIXEL_FORMAT_RAW16, { |
| 111 | { |
| 112 | formats::SBGGR16, |
| 113 | formats::SGBRG16, |
| 114 | formats::SGRBG16, |
| 115 | formats::SRGGB16 |
| 116 | }, |
| 117 | false, |
| 118 | "RAW16" |
| 119 | } |
| 120 | }, { |
| 121 | HAL_PIXEL_FORMAT_RAW_OPAQUE, { |
| 122 | { |
| 123 | formats::SBGGR10_IPU3, |
| 124 | formats::SGBRG10_IPU3, |
| 125 | formats::SGRBG10_IPU3, |
| 126 | formats::SRGGB10_IPU3 |
| 127 | }, |
| 128 | false, |
| 129 | "RAW_OPAQUE" |
| 130 | } |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 131 | }, |
| 132 | }; |
| 133 | |
Hirokazu Honda | c2df743 | 2020-12-11 09:53:34 +0000 | [diff] [blame] | 134 | /* |
| 135 | * \struct Camera3StreamConfig |
| 136 | * \brief Data to store StreamConfiguration associated with camera3_stream(s) |
| 137 | * \var streams List of the pairs of a stream requested by Android HAL client |
| 138 | * and CameraStream::Type associated with the stream |
| 139 | * \var config StreamConfiguration for streams |
| 140 | */ |
| 141 | struct Camera3StreamConfig { |
| 142 | struct Camera3Stream { |
| 143 | camera3_stream_t *stream; |
| 144 | CameraStream::Type type; |
| 145 | }; |
| 146 | |
| 147 | std::vector<Camera3Stream> streams; |
| 148 | StreamConfiguration config; |
| 149 | }; |
| 150 | |
Hirokazu Honda | 26d90af | 2020-12-11 09:53:36 +0000 | [diff] [blame] | 151 | /* |
| 152 | * Reorder the configurations so that libcamera::Camera can accept them as much |
| 153 | * as possible. The sort rule is as follows. |
| 154 | * 1.) The configuration for NV12 request whose resolution is the largest. |
| 155 | * 2.) The configuration for JPEG request. |
| 156 | * 3.) Others. Larger resolutions and different formats are put earlier. |
| 157 | */ |
| 158 | void sortCamera3StreamConfigs(std::vector<Camera3StreamConfig> &unsortedConfigs, |
| 159 | const camera3_stream_t *jpegStream) |
| 160 | { |
| 161 | const Camera3StreamConfig *jpegConfig = nullptr; |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 162 | |
Hirokazu Honda | 26d90af | 2020-12-11 09:53:36 +0000 | [diff] [blame] | 163 | std::map<PixelFormat, std::vector<const Camera3StreamConfig *>> formatToConfigs; |
| 164 | for (const auto &streamConfig : unsortedConfigs) { |
| 165 | if (jpegStream && !jpegConfig) { |
| 166 | const auto &streams = streamConfig.streams; |
| 167 | if (std::find_if(streams.begin(), streams.end(), |
| 168 | [jpegStream](const auto &stream) { |
| 169 | return stream.stream == jpegStream; |
| 170 | }) != streams.end()) { |
| 171 | jpegConfig = &streamConfig; |
| 172 | continue; |
| 173 | } |
| 174 | } |
| 175 | formatToConfigs[streamConfig.config.pixelFormat].push_back(&streamConfig); |
| 176 | } |
| 177 | |
| 178 | if (jpegStream && !jpegConfig) |
| 179 | LOG(HAL, Fatal) << "No Camera3StreamConfig is found for JPEG"; |
| 180 | |
| 181 | for (auto &fmt : formatToConfigs) { |
| 182 | auto &streamConfigs = fmt.second; |
| 183 | |
| 184 | /* Sorted by resolution. Smaller is put first. */ |
| 185 | std::sort(streamConfigs.begin(), streamConfigs.end(), |
| 186 | [](const auto *streamConfigA, const auto *streamConfigB) { |
| 187 | const Size &sizeA = streamConfigA->config.size; |
| 188 | const Size &sizeB = streamConfigB->config.size; |
| 189 | return sizeA < sizeB; |
| 190 | }); |
| 191 | } |
| 192 | |
| 193 | std::vector<Camera3StreamConfig> sortedConfigs; |
| 194 | sortedConfigs.reserve(unsortedConfigs.size()); |
| 195 | |
| 196 | /* |
| 197 | * NV12 is the most prioritized format. Put the configuration with NV12 |
| 198 | * and the largest resolution first. |
| 199 | */ |
| 200 | const auto nv12It = formatToConfigs.find(formats::NV12); |
| 201 | if (nv12It != formatToConfigs.end()) { |
| 202 | auto &nv12Configs = nv12It->second; |
Laurent Pinchart | bd4894d | 2020-12-12 05:22:31 +0200 | [diff] [blame] | 203 | const Camera3StreamConfig *nv12Largest = nv12Configs.back(); |
Hirokazu Honda | 26d90af | 2020-12-11 09:53:36 +0000 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * If JPEG will be created from NV12 and the size is larger than |
| 207 | * the largest NV12 configurations, then put the NV12 |
| 208 | * configuration for JPEG first. |
| 209 | */ |
| 210 | if (jpegConfig && jpegConfig->config.pixelFormat == formats::NV12) { |
| 211 | const Size &nv12SizeForJpeg = jpegConfig->config.size; |
| 212 | const Size &nv12LargestSize = nv12Largest->config.size; |
| 213 | |
| 214 | if (nv12LargestSize < nv12SizeForJpeg) { |
| 215 | LOG(HAL, Debug) << "Insert " << jpegConfig->config.toString(); |
| 216 | sortedConfigs.push_back(std::move(*jpegConfig)); |
| 217 | jpegConfig = nullptr; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | LOG(HAL, Debug) << "Insert " << nv12Largest->config.toString(); |
| 222 | sortedConfigs.push_back(*nv12Largest); |
| 223 | nv12Configs.pop_back(); |
| 224 | |
| 225 | if (nv12Configs.empty()) |
| 226 | formatToConfigs.erase(nv12It); |
| 227 | } |
| 228 | |
| 229 | /* If the configuration for JPEG is there, then put it. */ |
| 230 | if (jpegConfig) { |
| 231 | LOG(HAL, Debug) << "Insert " << jpegConfig->config.toString(); |
| 232 | sortedConfigs.push_back(std::move(*jpegConfig)); |
| 233 | jpegConfig = nullptr; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Put configurations with different formats and larger resolutions |
| 238 | * earlier. |
| 239 | */ |
| 240 | while (!formatToConfigs.empty()) { |
| 241 | for (auto it = formatToConfigs.begin(); it != formatToConfigs.end();) { |
| 242 | auto &configs = it->second; |
| 243 | LOG(HAL, Debug) << "Insert " << configs.back()->config.toString(); |
| 244 | sortedConfigs.push_back(*configs.back()); |
| 245 | configs.pop_back(); |
| 246 | |
| 247 | if (configs.empty()) |
| 248 | it = formatToConfigs.erase(it); |
| 249 | else |
| 250 | it++; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | ASSERT(sortedConfigs.size() == unsortedConfigs.size()); |
| 255 | |
| 256 | unsortedConfigs = sortedConfigs; |
| 257 | } |
| 258 | |
| 259 | } /* namespace */ |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 260 | |
| 261 | /* |
| 262 | * \struct Camera3RequestDescriptor |
| 263 | * |
| 264 | * A utility structure that groups information about a capture request to be |
| 265 | * later re-used at request complete time to notify the framework. |
| 266 | */ |
| 267 | |
| 268 | CameraDevice::Camera3RequestDescriptor::Camera3RequestDescriptor( |
Jacopo Mondi | dd1cd53 | 2021-01-21 10:51:05 +0100 | [diff] [blame] | 269 | Camera *camera, const camera3_capture_request_t *camera3Request) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 270 | { |
Jacopo Mondi | dd1cd53 | 2021-01-21 10:51:05 +0100 | [diff] [blame] | 271 | frameNumber_ = camera3Request->frame_number; |
| 272 | |
Jacopo Mondi | 4b18b82 | 2021-01-21 11:10:08 +0100 | [diff] [blame] | 273 | /* Copy the camera3 request stream information for later access. */ |
Hirokazu Honda | 17a6e78 | 2021-03-24 16:07:55 +0900 | [diff] [blame] | 274 | const uint32_t numBuffers = camera3Request->num_output_buffers; |
| 275 | buffers_.resize(numBuffers); |
| 276 | for (uint32_t i = 0; i < numBuffers; i++) |
Jacopo Mondi | 4b18b82 | 2021-01-21 11:10:08 +0100 | [diff] [blame] | 277 | buffers_[i] = camera3Request->output_buffers[i]; |
Jacopo Mondi | 4b1aa21 | 2020-10-06 17:56:50 +0200 | [diff] [blame] | 278 | |
| 279 | /* |
| 280 | * FrameBuffer instances created by wrapping a camera3 provided dmabuf |
| 281 | * are emplaced in this vector of unique_ptr<> for lifetime management. |
| 282 | */ |
Hirokazu Honda | 17a6e78 | 2021-03-24 16:07:55 +0900 | [diff] [blame] | 283 | frameBuffers_.reserve(numBuffers); |
Jacopo Mondi | 4b1aa21 | 2020-10-06 17:56:50 +0200 | [diff] [blame] | 284 | |
Jacopo Mondi | 9e6eece | 2021-01-21 12:52:10 +0100 | [diff] [blame] | 285 | /* Clone the controls associated with the camera3 request. */ |
| 286 | settings_ = CameraMetadata(camera3Request->settings); |
| 287 | |
Jacopo Mondi | 4b1aa21 | 2020-10-06 17:56:50 +0200 | [diff] [blame] | 288 | /* |
| 289 | * Create the libcamera::Request unique_ptr<> to tie its lifetime |
| 290 | * to the descriptor's one. Set the descriptor's address as the |
| 291 | * request's cookie to retrieve it at completion time. |
| 292 | */ |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 293 | request_ = std::make_unique<CaptureRequest>(camera, |
| 294 | reinterpret_cast<uint64_t>(this)); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Hirokazu Honda | 17a6e78 | 2021-03-24 16:07:55 +0900 | [diff] [blame] | 297 | CameraDevice::Camera3RequestDescriptor::~Camera3RequestDescriptor() = default; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 298 | |
| 299 | /* |
| 300 | * \class CameraDevice |
| 301 | * |
| 302 | * The CameraDevice class wraps a libcamera::Camera instance, and implements |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 303 | * the camera3_device_t interface, bridging calls received from the Android |
| 304 | * camera service to the CameraDevice. |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 305 | * |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 306 | * The class translates parameters and operations from the Camera HALv3 API to |
| 307 | * the libcamera API to provide static information for a Camera, create request |
| 308 | * templates for it, process capture requests and then deliver capture results |
| 309 | * back to the framework using the designated callbacks. |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 310 | */ |
| 311 | |
Hirokazu Honda | 9538ce4 | 2021-03-24 16:07:53 +0900 | [diff] [blame] | 312 | CameraDevice::CameraDevice(unsigned int id, std::shared_ptr<Camera> camera) |
| 313 | : id_(id), running_(false), camera_(std::move(camera)), |
Hirokazu Honda | dca709c | 2021-03-24 16:07:56 +0900 | [diff] [blame^] | 314 | facing_(CAMERA_FACING_FRONT), orientation_(0) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 315 | { |
| 316 | camera_->requestCompleted.connect(this, &CameraDevice::requestComplete); |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 317 | |
Paul Elder | 229653a | 2021-01-21 17:44:14 +0900 | [diff] [blame] | 318 | maker_ = "libcamera"; |
| 319 | model_ = "cameraModel"; |
| 320 | |
| 321 | /* \todo Support getting properties on Android */ |
| 322 | std::ifstream fstream("/var/cache/camera/camera.prop"); |
| 323 | if (!fstream.is_open()) |
| 324 | return; |
| 325 | |
| 326 | std::string line; |
| 327 | while (std::getline(fstream, line)) { |
| 328 | std::string::size_type delimPos = line.find("="); |
| 329 | if (delimPos == std::string::npos) |
| 330 | continue; |
| 331 | std::string key = line.substr(0, delimPos); |
| 332 | std::string val = line.substr(delimPos + 1); |
| 333 | |
| 334 | if (!key.compare("ro.product.model")) |
| 335 | model_ = val; |
| 336 | else if (!key.compare("ro.product.manufacturer")) |
| 337 | maker_ = val; |
| 338 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | CameraDevice::~CameraDevice() |
| 342 | { |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 343 | for (auto &it : requestTemplates_) |
| 344 | delete it.second; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Hirokazu Honda | 212f410 | 2021-03-24 16:07:50 +0900 | [diff] [blame] | 347 | std::unique_ptr<CameraDevice> CameraDevice::create(unsigned int id, |
Hirokazu Honda | 9538ce4 | 2021-03-24 16:07:53 +0900 | [diff] [blame] | 348 | std::shared_ptr<Camera> cam) |
Umang Jain | f8e2813 | 2020-08-21 14:46:08 +0000 | [diff] [blame] | 349 | { |
Hirokazu Honda | 9538ce4 | 2021-03-24 16:07:53 +0900 | [diff] [blame] | 350 | return std::unique_ptr<CameraDevice>( |
| 351 | new CameraDevice(id, std::move(cam))); |
Umang Jain | f8e2813 | 2020-08-21 14:46:08 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 354 | /* |
| 355 | * Initialize the camera static information. |
| 356 | * This method is called before the camera device is opened. |
| 357 | */ |
| 358 | int CameraDevice::initialize() |
| 359 | { |
| 360 | /* Initialize orientation and facing side of the camera. */ |
| 361 | const ControlList &properties = camera_->properties(); |
| 362 | |
| 363 | if (properties.contains(properties::Location)) { |
| 364 | int32_t location = properties.get(properties::Location); |
| 365 | switch (location) { |
| 366 | case properties::CameraLocationFront: |
| 367 | facing_ = CAMERA_FACING_FRONT; |
| 368 | break; |
| 369 | case properties::CameraLocationBack: |
| 370 | facing_ = CAMERA_FACING_BACK; |
| 371 | break; |
| 372 | case properties::CameraLocationExternal: |
Jacopo Mondi | 5154e14 | 2021-03-10 14:00:50 +0100 | [diff] [blame] | 373 | facing_ = CAMERA_FACING_EXTERNAL; |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 374 | break; |
| 375 | } |
Jacopo Mondi | 5154e14 | 2021-03-10 14:00:50 +0100 | [diff] [blame] | 376 | } else { |
| 377 | /* |
| 378 | * \todo Retrieve the camera location from configuration file |
| 379 | * if not available from the library. |
| 380 | */ |
| 381 | facing_ = CAMERA_FACING_FRONT; |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* |
Umang Jain | e917655 | 2020-09-09 16:17:54 +0530 | [diff] [blame] | 385 | * The Android orientation metadata specifies its rotation correction |
| 386 | * value in clockwise direction whereas libcamera specifies the |
| 387 | * rotation property in anticlockwise direction. Read the libcamera's |
| 388 | * rotation property (anticlockwise) and compute the corresponding |
| 389 | * value for clockwise direction as required by the Android orientation |
| 390 | * metadata. |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 391 | */ |
Umang Jain | e917655 | 2020-09-09 16:17:54 +0530 | [diff] [blame] | 392 | if (properties.contains(properties::Rotation)) { |
| 393 | int rotation = properties.get(properties::Rotation); |
| 394 | orientation_ = (360 - rotation) % 360; |
| 395 | } |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 396 | |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 397 | int ret = camera_->acquire(); |
| 398 | if (ret) { |
| 399 | LOG(HAL, Error) << "Failed to temporarily acquire the camera"; |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | ret = initializeStreamConfigurations(); |
| 404 | camera_->release(); |
| 405 | return ret; |
| 406 | } |
| 407 | |
Jacopo Mondi | bfee631 | 2020-09-01 17:42:13 +0200 | [diff] [blame] | 408 | std::vector<Size> CameraDevice::getYUVResolutions(CameraConfiguration *cameraConfig, |
| 409 | const PixelFormat &pixelFormat, |
| 410 | const std::vector<Size> &resolutions) |
| 411 | { |
| 412 | std::vector<Size> supportedResolutions; |
| 413 | |
| 414 | StreamConfiguration &cfg = cameraConfig->at(0); |
| 415 | for (const Size &res : resolutions) { |
| 416 | cfg.pixelFormat = pixelFormat; |
| 417 | cfg.size = res; |
| 418 | |
| 419 | CameraConfiguration::Status status = cameraConfig->validate(); |
| 420 | if (status != CameraConfiguration::Valid) { |
| 421 | LOG(HAL, Debug) << cfg.toString() << " not supported"; |
| 422 | continue; |
| 423 | } |
| 424 | |
| 425 | LOG(HAL, Debug) << cfg.toString() << " supported"; |
| 426 | |
| 427 | supportedResolutions.push_back(res); |
| 428 | } |
| 429 | |
| 430 | return supportedResolutions; |
| 431 | } |
| 432 | |
Jacopo Mondi | 4961033 | 2020-09-01 18:11:34 +0200 | [diff] [blame] | 433 | std::vector<Size> CameraDevice::getRawResolutions(const libcamera::PixelFormat &pixelFormat) |
| 434 | { |
| 435 | std::unique_ptr<CameraConfiguration> cameraConfig = |
Niklas Söderlund | dbe8d27 | 2020-05-26 15:04:47 +0200 | [diff] [blame] | 436 | camera_->generateConfiguration({ StreamRole::Raw }); |
Jacopo Mondi | 4961033 | 2020-09-01 18:11:34 +0200 | [diff] [blame] | 437 | StreamConfiguration &cfg = cameraConfig->at(0); |
| 438 | const StreamFormats &formats = cfg.formats(); |
| 439 | std::vector<Size> supportedResolutions = formats.sizes(pixelFormat); |
| 440 | |
| 441 | return supportedResolutions; |
| 442 | } |
| 443 | |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 444 | /* |
| 445 | * Initialize the format conversion map to translate from Android format |
| 446 | * identifier to libcamera pixel formats and fill in the list of supported |
| 447 | * stream configurations to be reported to the Android camera framework through |
| 448 | * the static stream configuration metadata. |
| 449 | */ |
| 450 | int CameraDevice::initializeStreamConfigurations() |
| 451 | { |
| 452 | /* |
| 453 | * Get the maximum output resolutions |
| 454 | * \todo Get this from the camera properties once defined |
| 455 | */ |
| 456 | std::unique_ptr<CameraConfiguration> cameraConfig = |
| 457 | camera_->generateConfiguration({ StillCapture }); |
| 458 | if (!cameraConfig) { |
| 459 | LOG(HAL, Error) << "Failed to get maximum resolution"; |
| 460 | return -EINVAL; |
| 461 | } |
| 462 | StreamConfiguration &cfg = cameraConfig->at(0); |
| 463 | |
| 464 | /* |
| 465 | * \todo JPEG - Adjust the maximum available resolution by taking the |
| 466 | * JPEG encoder requirements into account (alignment and aspect ratio). |
| 467 | */ |
| 468 | const Size maxRes = cfg.size; |
| 469 | LOG(HAL, Debug) << "Maximum supported resolution: " << maxRes.toString(); |
| 470 | |
| 471 | /* |
| 472 | * Build the list of supported image resolutions. |
| 473 | * |
| 474 | * The resolutions listed in camera3Resolution are mandatory to be |
| 475 | * supported, up to the camera maximum resolution. |
| 476 | * |
| 477 | * Augment the list by adding resolutions calculated from the camera |
| 478 | * maximum one. |
| 479 | */ |
| 480 | std::vector<Size> cameraResolutions; |
| 481 | std::copy_if(camera3Resolutions.begin(), camera3Resolutions.end(), |
| 482 | std::back_inserter(cameraResolutions), |
| 483 | [&](const Size &res) { return res < maxRes; }); |
| 484 | |
| 485 | /* |
| 486 | * The Camera3 specification suggests adding 1/2 and 1/4 of the maximum |
| 487 | * resolution. |
| 488 | */ |
| 489 | for (unsigned int divider = 2;; divider <<= 1) { |
| 490 | Size derivedSize{ |
| 491 | maxRes.width / divider, |
| 492 | maxRes.height / divider, |
| 493 | }; |
| 494 | |
| 495 | if (derivedSize.width < 320 || |
| 496 | derivedSize.height < 240) |
| 497 | break; |
| 498 | |
| 499 | cameraResolutions.push_back(derivedSize); |
| 500 | } |
| 501 | cameraResolutions.push_back(maxRes); |
| 502 | |
| 503 | /* Remove duplicated entries from the list of supported resolutions. */ |
| 504 | std::sort(cameraResolutions.begin(), cameraResolutions.end()); |
| 505 | auto last = std::unique(cameraResolutions.begin(), cameraResolutions.end()); |
| 506 | cameraResolutions.erase(last, cameraResolutions.end()); |
| 507 | |
| 508 | /* |
| 509 | * Build the list of supported camera formats. |
| 510 | * |
| 511 | * To each Android format a list of compatible libcamera formats is |
| 512 | * associated. The first libcamera format that tests successful is added |
| 513 | * to the format translation map used when configuring the streams. |
| 514 | * It is then tested against the list of supported camera resolutions to |
| 515 | * build the stream configuration map reported through the camera static |
| 516 | * metadata. |
| 517 | */ |
Jacopo Mondi | 756fcba | 2021-01-28 15:26:20 +0100 | [diff] [blame] | 518 | Size maxJpegSize; |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 519 | for (const auto &format : camera3FormatsMap) { |
| 520 | int androidFormat = format.first; |
| 521 | const Camera3Format &camera3Format = format.second; |
| 522 | const std::vector<PixelFormat> &libcameraFormats = |
| 523 | camera3Format.libcameraFormats; |
| 524 | |
Jacopo Mondi | af264ec | 2020-09-01 15:40:49 +0200 | [diff] [blame] | 525 | LOG(HAL, Debug) << "Trying to map Android format " |
| 526 | << camera3Format.name; |
| 527 | |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 528 | /* |
Jacopo Mondi | 843565c | 2020-09-01 15:34:13 +0200 | [diff] [blame] | 529 | * JPEG is always supported, either produced directly by the |
| 530 | * camera, or encoded in the HAL. |
| 531 | */ |
| 532 | if (androidFormat == HAL_PIXEL_FORMAT_BLOB) { |
| 533 | formatsMap_[androidFormat] = formats::MJPEG; |
Jacopo Mondi | af264ec | 2020-09-01 15:40:49 +0200 | [diff] [blame] | 534 | LOG(HAL, Debug) << "Mapped Android format " |
| 535 | << camera3Format.name << " to " |
| 536 | << formats::MJPEG.toString() |
| 537 | << " (fixed mapping)"; |
Jacopo Mondi | 843565c | 2020-09-01 15:34:13 +0200 | [diff] [blame] | 538 | continue; |
| 539 | } |
| 540 | |
| 541 | /* |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 542 | * Test the libcamera formats that can produce images |
| 543 | * compatible with the format defined by Android. |
| 544 | */ |
| 545 | PixelFormat mappedFormat; |
| 546 | for (const PixelFormat &pixelFormat : libcameraFormats) { |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 547 | |
Jacopo Mondi | af264ec | 2020-09-01 15:40:49 +0200 | [diff] [blame] | 548 | LOG(HAL, Debug) << "Testing " << pixelFormat.toString(); |
| 549 | |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 550 | /* |
| 551 | * The stream configuration size can be adjusted, |
| 552 | * not the pixel format. |
| 553 | * |
| 554 | * \todo This could be simplified once all pipeline |
| 555 | * handlers will report the StreamFormats list of |
| 556 | * supported formats. |
| 557 | */ |
| 558 | cfg.pixelFormat = pixelFormat; |
| 559 | |
| 560 | CameraConfiguration::Status status = cameraConfig->validate(); |
| 561 | if (status != CameraConfiguration::Invalid && |
| 562 | cfg.pixelFormat == pixelFormat) { |
| 563 | mappedFormat = pixelFormat; |
| 564 | break; |
| 565 | } |
| 566 | } |
Jacopo Mondi | 3533fd4 | 2020-09-01 15:31:56 +0200 | [diff] [blame] | 567 | |
| 568 | if (!mappedFormat.isValid()) { |
| 569 | /* If the format is not mandatory, skip it. */ |
| 570 | if (!camera3Format.mandatory) |
| 571 | continue; |
| 572 | |
| 573 | LOG(HAL, Error) |
| 574 | << "Failed to map mandatory Android format " |
| 575 | << camera3Format.name << " (" |
| 576 | << utils::hex(androidFormat) << "): aborting"; |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 577 | return -EINVAL; |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | * Record the mapping and then proceed to generate the |
| 582 | * stream configurations map, by testing the image resolutions. |
| 583 | */ |
| 584 | formatsMap_[androidFormat] = mappedFormat; |
Jacopo Mondi | af264ec | 2020-09-01 15:40:49 +0200 | [diff] [blame] | 585 | LOG(HAL, Debug) << "Mapped Android format " |
| 586 | << camera3Format.name << " to " |
| 587 | << mappedFormat.toString(); |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 588 | |
Jacopo Mondi | 4961033 | 2020-09-01 18:11:34 +0200 | [diff] [blame] | 589 | std::vector<Size> resolutions; |
| 590 | const PixelFormatInfo &info = PixelFormatInfo::info(mappedFormat); |
| 591 | if (info.colourEncoding == PixelFormatInfo::ColourEncodingRAW) |
| 592 | resolutions = getRawResolutions(mappedFormat); |
| 593 | else |
| 594 | resolutions = getYUVResolutions(cameraConfig.get(), |
| 595 | mappedFormat, |
| 596 | cameraResolutions); |
| 597 | |
Jacopo Mondi | bfee631 | 2020-09-01 17:42:13 +0200 | [diff] [blame] | 598 | for (const Size &res : resolutions) { |
Niklas Söderlund | 142a9ee | 2020-07-23 18:32:23 +0200 | [diff] [blame] | 599 | streamConfigurations_.push_back({ res, androidFormat }); |
Jacopo Mondi | 843565c | 2020-09-01 15:34:13 +0200 | [diff] [blame] | 600 | |
| 601 | /* |
| 602 | * If the format is HAL_PIXEL_FORMAT_YCbCr_420_888 |
| 603 | * from which JPEG is produced, add an entry for |
| 604 | * the JPEG stream. |
| 605 | * |
| 606 | * \todo Wire the JPEG encoder to query the supported |
| 607 | * sizes provided a list of formats it can encode. |
| 608 | * |
| 609 | * \todo Support JPEG streams produced by the Camera |
| 610 | * natively. |
| 611 | */ |
Jacopo Mondi | 756fcba | 2021-01-28 15:26:20 +0100 | [diff] [blame] | 612 | if (androidFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) { |
Jacopo Mondi | 843565c | 2020-09-01 15:34:13 +0200 | [diff] [blame] | 613 | streamConfigurations_.push_back( |
| 614 | { res, HAL_PIXEL_FORMAT_BLOB }); |
Jacopo Mondi | 756fcba | 2021-01-28 15:26:20 +0100 | [diff] [blame] | 615 | maxJpegSize = std::max(maxJpegSize, res); |
| 616 | } |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 617 | } |
Jacopo Mondi | 756fcba | 2021-01-28 15:26:20 +0100 | [diff] [blame] | 618 | |
| 619 | /* |
| 620 | * \todo Calculate the maximum JPEG buffer size by asking the |
| 621 | * encoder giving the maximum frame size required. |
| 622 | */ |
| 623 | maxJpegBufferSize_ = maxJpegSize.width * maxJpegSize.height * 1.5; |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | LOG(HAL, Debug) << "Collected stream configuration map: "; |
| 627 | for (const auto &entry : streamConfigurations_) |
| 628 | LOG(HAL, Debug) << "{ " << entry.resolution.toString() << " - " |
Niklas Söderlund | 142a9ee | 2020-07-23 18:32:23 +0200 | [diff] [blame] | 629 | << utils::hex(entry.androidFormat) << " }"; |
Jacopo Mondi | 117588b | 2020-05-23 18:53:54 +0200 | [diff] [blame] | 630 | |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | /* |
| 635 | * Open a camera device. The static information on the camera shall have been |
| 636 | * initialized with a call to CameraDevice::initialize(). |
| 637 | */ |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 638 | int CameraDevice::open(const hw_module_t *hardwareModule) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 639 | { |
| 640 | int ret = camera_->acquire(); |
| 641 | if (ret) { |
| 642 | LOG(HAL, Error) << "Failed to acquire the camera"; |
| 643 | return ret; |
| 644 | } |
| 645 | |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 646 | /* Initialize the hw_device_t in the instance camera3_module_t. */ |
| 647 | camera3Device_.common.tag = HARDWARE_DEVICE_TAG; |
| 648 | camera3Device_.common.version = CAMERA_DEVICE_API_VERSION_3_3; |
| 649 | camera3Device_.common.module = (hw_module_t *)hardwareModule; |
| 650 | camera3Device_.common.close = hal_dev_close; |
| 651 | |
| 652 | /* |
| 653 | * The camera device operations. These actually implement |
| 654 | * the Android Camera HALv3 interface. |
| 655 | */ |
| 656 | camera3Device_.ops = &hal_dev_ops; |
| 657 | camera3Device_.priv = this; |
| 658 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | void CameraDevice::close() |
| 663 | { |
Jacopo Mondi | 6c4999e | 2020-10-03 16:06:34 +0200 | [diff] [blame] | 664 | streams_.clear(); |
| 665 | |
Jacopo Mondi | 4b1aa21 | 2020-10-06 17:56:50 +0200 | [diff] [blame] | 666 | worker_.stop(); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 667 | camera_->stop(); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 668 | camera_->release(); |
| 669 | |
| 670 | running_ = false; |
| 671 | } |
| 672 | |
| 673 | void CameraDevice::setCallbacks(const camera3_callback_ops_t *callbacks) |
| 674 | { |
| 675 | callbacks_ = callbacks; |
| 676 | } |
| 677 | |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 678 | std::tuple<uint32_t, uint32_t> CameraDevice::calculateStaticMetadataSize() |
| 679 | { |
| 680 | /* |
| 681 | * \todo Keep this in sync with the actual number of entries. |
Jacopo Mondi | edd4b1d | 2021-01-04 16:18:29 +0100 | [diff] [blame] | 682 | * Currently: 54 entries, 874 bytes of static metadata |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 683 | */ |
Jacopo Mondi | edd4b1d | 2021-01-04 16:18:29 +0100 | [diff] [blame] | 684 | uint32_t numEntries = 54; |
| 685 | uint32_t byteSize = 874; |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 686 | |
| 687 | /* |
| 688 | * Calculate space occupation in bytes for dynamically built metadata |
| 689 | * entries. |
| 690 | * |
Jacopo Mondi | 00982fb | 2021-02-04 16:57:55 +0100 | [diff] [blame] | 691 | * Each stream configuration entry requires 48 bytes: |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 692 | * 4 32bits integers for ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 693 | * 4 64bits integers for ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS |
| 694 | */ |
Niklas Söderlund | 3530614 | 2020-07-23 18:25:04 +0200 | [diff] [blame] | 695 | byteSize += streamConfigurations_.size() * 48; |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 696 | |
Jacopo Mondi | 9e80705 | 2021-02-04 15:32:00 +0100 | [diff] [blame] | 697 | /* |
| 698 | * 2 32bits integers for each HAL_PIXEL_FORMAT_BLOB for thumbnail sizes |
| 699 | * 2 32bits integers for the (0, 0) thumbnail size |
| 700 | * |
| 701 | * This is a worst case estimates as different configurations with the |
| 702 | * same aspect ratio will generate the same size. |
| 703 | */ |
| 704 | for (const auto &entry : streamConfigurations_) { |
| 705 | if (entry.androidFormat != HAL_PIXEL_FORMAT_BLOB) |
| 706 | continue; |
| 707 | |
| 708 | byteSize += 8; |
| 709 | } |
| 710 | byteSize += 8; |
| 711 | |
Laurent Pinchart | 7a88b21 | 2020-06-10 00:07:59 +0300 | [diff] [blame] | 712 | return std::make_tuple(numEntries, byteSize); |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 713 | } |
| 714 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 715 | /* |
| 716 | * Return static information for the camera. |
| 717 | */ |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 718 | const camera_metadata_t *CameraDevice::getStaticMetadata() |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 719 | { |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 720 | if (staticMetadata_) |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 721 | return staticMetadata_->get(); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 722 | |
| 723 | /* |
| 724 | * The here reported metadata are enough to implement a basic capture |
| 725 | * example application, but a real camera implementation will require |
| 726 | * more. |
| 727 | */ |
Jacopo Mondi | a80d381 | 2020-05-26 12:31:35 +0200 | [diff] [blame] | 728 | uint32_t numEntries; |
| 729 | uint32_t byteSize; |
| 730 | std::tie(numEntries, byteSize) = calculateStaticMetadataSize(); |
Hirokazu Honda | dca709c | 2021-03-24 16:07:56 +0900 | [diff] [blame^] | 731 | staticMetadata_ = std::make_unique<CameraMetadata>(numEntries, byteSize); |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 732 | if (!staticMetadata_->isValid()) { |
| 733 | LOG(HAL, Error) << "Failed to allocate static metadata"; |
Hirokazu Honda | dca709c | 2021-03-24 16:07:56 +0900 | [diff] [blame^] | 734 | staticMetadata_.reset(); |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 735 | return nullptr; |
| 736 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 737 | |
Jacopo Mondi | 5d91c8d | 2020-09-23 09:41:52 +0200 | [diff] [blame] | 738 | const ControlInfoMap &controlsInfo = camera_->controls(); |
Jacopo Mondi | 1889cdc | 2020-11-06 16:08:16 +0100 | [diff] [blame] | 739 | const ControlList &properties = camera_->properties(); |
Jacopo Mondi | 5d91c8d | 2020-09-23 09:41:52 +0200 | [diff] [blame] | 740 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 741 | /* Color correction static metadata. */ |
Jacopo Mondi | 6333686 | 2020-10-08 16:18:26 +0200 | [diff] [blame] | 742 | { |
Jacopo Mondi | c268e4f | 2020-12-01 15:42:15 +0100 | [diff] [blame] | 743 | std::vector<uint8_t> data; |
| 744 | data.reserve(3); |
Jacopo Mondi | 6333686 | 2020-10-08 16:18:26 +0200 | [diff] [blame] | 745 | const auto &infoMap = controlsInfo.find(&controls::draft::ColorCorrectionAberrationMode); |
| 746 | if (infoMap != controlsInfo.end()) { |
| 747 | for (const auto &value : infoMap->second.values()) |
| 748 | data.push_back(value.get<int32_t>()); |
| 749 | } else { |
| 750 | data.push_back(ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF); |
| 751 | } |
| 752 | staticMetadata_->addEntry(ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES, |
| 753 | data.data(), data.size()); |
| 754 | } |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 755 | |
| 756 | /* Control static metadata. */ |
| 757 | std::vector<uint8_t> aeAvailableAntiBandingModes = { |
| 758 | ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF, |
| 759 | ANDROID_CONTROL_AE_ANTIBANDING_MODE_50HZ, |
| 760 | ANDROID_CONTROL_AE_ANTIBANDING_MODE_60HZ, |
| 761 | ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO, |
| 762 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 763 | staticMetadata_->addEntry(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES, |
| 764 | aeAvailableAntiBandingModes.data(), |
| 765 | aeAvailableAntiBandingModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 766 | |
| 767 | std::vector<uint8_t> aeAvailableModes = { |
| 768 | ANDROID_CONTROL_AE_MODE_ON, |
| 769 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 770 | staticMetadata_->addEntry(ANDROID_CONTROL_AE_AVAILABLE_MODES, |
| 771 | aeAvailableModes.data(), |
| 772 | aeAvailableModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 773 | |
Jacopo Mondi | edd4b1d | 2021-01-04 16:18:29 +0100 | [diff] [blame] | 774 | int64_t minFrameDurationNsec = -1; |
| 775 | int64_t maxFrameDurationNsec = -1; |
| 776 | const auto frameDurationsInfo = controlsInfo.find(&controls::FrameDurations); |
| 777 | if (frameDurationsInfo != controlsInfo.end()) { |
| 778 | minFrameDurationNsec = frameDurationsInfo->second.min().get<int64_t>() * 1000; |
| 779 | maxFrameDurationNsec = frameDurationsInfo->second.max().get<int64_t>() * 1000; |
| 780 | |
| 781 | /* |
| 782 | * Adjust the minimum frame duration to comply with Android |
| 783 | * requirements. The camera service mandates all preview/record |
| 784 | * streams to have a minimum frame duration < 33,366 milliseconds |
| 785 | * (see MAX_PREVIEW_RECORD_DURATION_NS in the camera service |
| 786 | * implementation). |
| 787 | * |
| 788 | * If we're close enough (+ 500 useconds) to that value, round |
| 789 | * the minimum frame duration of the camera to an accepted |
| 790 | * value. |
| 791 | */ |
| 792 | static constexpr int64_t MAX_PREVIEW_RECORD_DURATION_NS = 1e9 / 29.97; |
| 793 | if (minFrameDurationNsec > MAX_PREVIEW_RECORD_DURATION_NS && |
| 794 | minFrameDurationNsec < MAX_PREVIEW_RECORD_DURATION_NS + 500000) |
| 795 | minFrameDurationNsec = MAX_PREVIEW_RECORD_DURATION_NS - 1000; |
| 796 | |
| 797 | /* |
| 798 | * The AE routine frame rate limits are computed using the frame |
| 799 | * duration limits, as libcamera clips the AE routine to the |
| 800 | * frame durations. |
| 801 | */ |
| 802 | int32_t maxFps = std::round(1e9 / minFrameDurationNsec); |
| 803 | int32_t minFps = std::round(1e9 / maxFrameDurationNsec); |
| 804 | minFps = std::max(1, minFps); |
| 805 | |
| 806 | /* |
| 807 | * Register to the camera service {min, max} and {max, max} |
| 808 | * intervals as requested by the metadata documentation. |
| 809 | */ |
| 810 | int32_t availableAeFpsTarget[] = { |
| 811 | minFps, maxFps, maxFps, maxFps |
| 812 | }; |
| 813 | staticMetadata_->addEntry(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, |
| 814 | availableAeFpsTarget, 4); |
| 815 | } |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 816 | |
| 817 | std::vector<int32_t> aeCompensationRange = { |
| 818 | 0, 0, |
| 819 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 820 | staticMetadata_->addEntry(ANDROID_CONTROL_AE_COMPENSATION_RANGE, |
| 821 | aeCompensationRange.data(), |
| 822 | aeCompensationRange.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 823 | |
| 824 | const camera_metadata_rational_t aeCompensationStep[] = { |
| 825 | { 0, 1 } |
| 826 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 827 | staticMetadata_->addEntry(ANDROID_CONTROL_AE_COMPENSATION_STEP, |
| 828 | aeCompensationStep, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 829 | |
| 830 | std::vector<uint8_t> availableAfModes = { |
| 831 | ANDROID_CONTROL_AF_MODE_OFF, |
| 832 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 833 | staticMetadata_->addEntry(ANDROID_CONTROL_AF_AVAILABLE_MODES, |
| 834 | availableAfModes.data(), |
| 835 | availableAfModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 836 | |
| 837 | std::vector<uint8_t> availableEffects = { |
| 838 | ANDROID_CONTROL_EFFECT_MODE_OFF, |
| 839 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 840 | staticMetadata_->addEntry(ANDROID_CONTROL_AVAILABLE_EFFECTS, |
| 841 | availableEffects.data(), |
| 842 | availableEffects.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 843 | |
| 844 | std::vector<uint8_t> availableSceneModes = { |
| 845 | ANDROID_CONTROL_SCENE_MODE_DISABLED, |
| 846 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 847 | staticMetadata_->addEntry(ANDROID_CONTROL_AVAILABLE_SCENE_MODES, |
| 848 | availableSceneModes.data(), |
| 849 | availableSceneModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 850 | |
| 851 | std::vector<uint8_t> availableStabilizationModes = { |
| 852 | ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF, |
| 853 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 854 | staticMetadata_->addEntry(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES, |
| 855 | availableStabilizationModes.data(), |
| 856 | availableStabilizationModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 857 | |
Jacopo Mondi | af088b8 | 2021-01-04 18:26:28 +0100 | [diff] [blame] | 858 | /* |
| 859 | * \todo Inspect the Camera capabilities to report the available |
| 860 | * AWB modes. Default to AUTO as CTS tests require it. |
| 861 | */ |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 862 | std::vector<uint8_t> availableAwbModes = { |
Jacopo Mondi | af088b8 | 2021-01-04 18:26:28 +0100 | [diff] [blame] | 863 | ANDROID_CONTROL_AWB_MODE_AUTO, |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 864 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 865 | staticMetadata_->addEntry(ANDROID_CONTROL_AWB_AVAILABLE_MODES, |
| 866 | availableAwbModes.data(), |
| 867 | availableAwbModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 868 | |
| 869 | std::vector<int32_t> availableMaxRegions = { |
| 870 | 0, 0, 0, |
| 871 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 872 | staticMetadata_->addEntry(ANDROID_CONTROL_MAX_REGIONS, |
| 873 | availableMaxRegions.data(), |
| 874 | availableMaxRegions.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 875 | |
| 876 | std::vector<uint8_t> sceneModesOverride = { |
| 877 | ANDROID_CONTROL_AE_MODE_ON, |
| 878 | ANDROID_CONTROL_AWB_MODE_AUTO, |
Jacopo Mondi | f266c0e | 2021-02-03 16:34:40 +0100 | [diff] [blame] | 879 | ANDROID_CONTROL_AF_MODE_OFF, |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 880 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 881 | staticMetadata_->addEntry(ANDROID_CONTROL_SCENE_MODE_OVERRIDES, |
| 882 | sceneModesOverride.data(), |
| 883 | sceneModesOverride.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 884 | |
| 885 | uint8_t aeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 886 | staticMetadata_->addEntry(ANDROID_CONTROL_AE_LOCK_AVAILABLE, |
| 887 | &aeLockAvailable, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 888 | |
| 889 | uint8_t awbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 890 | staticMetadata_->addEntry(ANDROID_CONTROL_AWB_LOCK_AVAILABLE, |
| 891 | &awbLockAvailable, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 892 | |
| 893 | char availableControlModes = ANDROID_CONTROL_MODE_AUTO; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 894 | staticMetadata_->addEntry(ANDROID_CONTROL_AVAILABLE_MODES, |
| 895 | &availableControlModes, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 896 | |
| 897 | /* JPEG static metadata. */ |
Jacopo Mondi | 9e80705 | 2021-02-04 15:32:00 +0100 | [diff] [blame] | 898 | |
| 899 | /* |
| 900 | * Create the list of supported thumbnail sizes by inspecting the |
| 901 | * available JPEG resolutions collected in streamConfigurations_ and |
| 902 | * generate one entry for each aspect ratio. |
| 903 | * |
| 904 | * The JPEG thumbnailer can freely scale, so pick an arbitrary |
| 905 | * (160, 160) size as the bounding rectangle, which is then cropped to |
| 906 | * the different supported aspect ratios. |
| 907 | */ |
| 908 | constexpr Size maxJpegThumbnail(160, 160); |
| 909 | std::vector<Size> thumbnailSizes; |
| 910 | thumbnailSizes.push_back({ 0, 0 }); |
| 911 | for (const auto &entry : streamConfigurations_) { |
| 912 | if (entry.androidFormat != HAL_PIXEL_FORMAT_BLOB) |
| 913 | continue; |
| 914 | |
| 915 | Size thumbnailSize = maxJpegThumbnail |
| 916 | .boundedToAspectRatio({ entry.resolution.width, |
| 917 | entry.resolution.height }); |
| 918 | thumbnailSizes.push_back(thumbnailSize); |
| 919 | } |
| 920 | |
| 921 | std::sort(thumbnailSizes.begin(), thumbnailSizes.end()); |
| 922 | auto last = std::unique(thumbnailSizes.begin(), thumbnailSizes.end()); |
| 923 | thumbnailSizes.erase(last, thumbnailSizes.end()); |
| 924 | |
| 925 | /* Transform sizes in to a list of integers that can be consumed. */ |
| 926 | std::vector<int32_t> thumbnailEntries; |
| 927 | thumbnailEntries.reserve(thumbnailSizes.size() * 2); |
| 928 | for (const auto &size : thumbnailSizes) { |
| 929 | thumbnailEntries.push_back(size.width); |
| 930 | thumbnailEntries.push_back(size.height); |
| 931 | } |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 932 | staticMetadata_->addEntry(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, |
Jacopo Mondi | 9e80705 | 2021-02-04 15:32:00 +0100 | [diff] [blame] | 933 | thumbnailEntries.data(), thumbnailEntries.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 934 | |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 935 | staticMetadata_->addEntry(ANDROID_JPEG_MAX_SIZE, &maxJpegBufferSize_, 1); |
| 936 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 937 | /* Sensor static metadata. */ |
Jacopo Mondi | cda41ff | 2020-12-30 17:18:51 +0100 | [diff] [blame] | 938 | { |
Jacopo Mondi | 1889cdc | 2020-11-06 16:08:16 +0100 | [diff] [blame] | 939 | const Size &size = |
Jacopo Mondi | 0ed0532 | 2020-12-23 18:34:34 +0100 | [diff] [blame] | 940 | properties.get(properties::PixelArraySize); |
Jacopo Mondi | 1889cdc | 2020-11-06 16:08:16 +0100 | [diff] [blame] | 941 | std::vector<int32_t> data{ |
| 942 | static_cast<int32_t>(size.width), |
| 943 | static_cast<int32_t>(size.height), |
| 944 | }; |
| 945 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE, |
| 946 | data.data(), data.size()); |
| 947 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 948 | |
Jacopo Mondi | cda41ff | 2020-12-30 17:18:51 +0100 | [diff] [blame] | 949 | { |
Jacopo Mondi | 1889cdc | 2020-11-06 16:08:16 +0100 | [diff] [blame] | 950 | const Span<const Rectangle> &rects = |
Jacopo Mondi | 0ed0532 | 2020-12-23 18:34:34 +0100 | [diff] [blame] | 951 | properties.get(properties::PixelArrayActiveAreas); |
Jacopo Mondi | 1889cdc | 2020-11-06 16:08:16 +0100 | [diff] [blame] | 952 | std::vector<int32_t> data{ |
| 953 | static_cast<int32_t>(rects[0].x), |
| 954 | static_cast<int32_t>(rects[0].y), |
| 955 | static_cast<int32_t>(rects[0].width), |
| 956 | static_cast<int32_t>(rects[0].height), |
| 957 | }; |
| 958 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, |
| 959 | data.data(), data.size()); |
| 960 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 961 | |
| 962 | int32_t sensitivityRange[] = { |
| 963 | 32, 2400, |
| 964 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 965 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_SENSITIVITY_RANGE, |
| 966 | &sensitivityRange, 2); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 967 | |
Jacopo Mondi | cb35ed2 | 2020-12-18 16:28:43 +0100 | [diff] [blame] | 968 | /* Report the color filter arrangement if the camera reports it. */ |
| 969 | if (properties.contains(properties::draft::ColorFilterArrangement)) { |
| 970 | uint8_t filterArr = properties.get(properties::draft::ColorFilterArrangement); |
| 971 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT, |
| 972 | &filterArr, 1); |
| 973 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 974 | |
Jacopo Mondi | 753d753 | 2021-01-02 12:34:06 +0100 | [diff] [blame] | 975 | const auto &exposureInfo = controlsInfo.find(&controls::ExposureTime); |
| 976 | if (exposureInfo != controlsInfo.end()) { |
| 977 | int64_t exposureTimeRange[2] = { |
| 978 | exposureInfo->second.min().get<int32_t>() * 1000LL, |
| 979 | exposureInfo->second.max().get<int32_t>() * 1000LL, |
| 980 | }; |
| 981 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE, |
| 982 | &exposureTimeRange, 2); |
| 983 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 984 | |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 985 | staticMetadata_->addEntry(ANDROID_SENSOR_ORIENTATION, &orientation_, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 986 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 987 | std::vector<int32_t> testPatterModes = { |
| 988 | ANDROID_SENSOR_TEST_PATTERN_MODE_OFF, |
| 989 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 990 | staticMetadata_->addEntry(ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES, |
| 991 | testPatterModes.data(), |
| 992 | testPatterModes.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 993 | |
| 994 | std::vector<float> physicalSize = { |
| 995 | 2592, 1944, |
| 996 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 997 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_PHYSICAL_SIZE, |
| 998 | physicalSize.data(), |
| 999 | physicalSize.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1000 | |
| 1001 | uint8_t timestampSource = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1002 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE, |
| 1003 | ×tampSource, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1004 | |
Jacopo Mondi | edd4b1d | 2021-01-04 16:18:29 +0100 | [diff] [blame] | 1005 | if (maxFrameDurationNsec > 0) |
| 1006 | staticMetadata_->addEntry(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION, |
| 1007 | &maxFrameDurationNsec, 1); |
| 1008 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1009 | /* Statistics static metadata. */ |
| 1010 | uint8_t faceDetectMode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1011 | staticMetadata_->addEntry(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES, |
| 1012 | &faceDetectMode, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1013 | |
| 1014 | int32_t maxFaceCount = 0; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1015 | staticMetadata_->addEntry(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT, |
| 1016 | &maxFaceCount, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1017 | |
Jacopo Mondi | 2c61850 | 2020-10-08 16:47:36 +0200 | [diff] [blame] | 1018 | { |
Jacopo Mondi | c268e4f | 2020-12-01 15:42:15 +0100 | [diff] [blame] | 1019 | std::vector<uint8_t> data; |
| 1020 | data.reserve(2); |
Jacopo Mondi | 2c61850 | 2020-10-08 16:47:36 +0200 | [diff] [blame] | 1021 | const auto &infoMap = controlsInfo.find(&controls::draft::LensShadingMapMode); |
| 1022 | if (infoMap != controlsInfo.end()) { |
| 1023 | for (const auto &value : infoMap->second.values()) |
| 1024 | data.push_back(value.get<int32_t>()); |
| 1025 | } else { |
| 1026 | data.push_back(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF); |
| 1027 | } |
| 1028 | staticMetadata_->addEntry(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES, |
| 1029 | data.data(), data.size()); |
| 1030 | } |
| 1031 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1032 | /* Sync static metadata. */ |
| 1033 | int32_t maxLatency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1034 | staticMetadata_->addEntry(ANDROID_SYNC_MAX_LATENCY, &maxLatency, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1035 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1036 | /* Flash static metadata. */ |
| 1037 | char flashAvailable = ANDROID_FLASH_INFO_AVAILABLE_FALSE; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1038 | staticMetadata_->addEntry(ANDROID_FLASH_INFO_AVAILABLE, |
| 1039 | &flashAvailable, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1040 | |
| 1041 | /* Lens static metadata. */ |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1042 | std::vector<float> lensApertures = { |
| 1043 | 2.53 / 100, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1044 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1045 | staticMetadata_->addEntry(ANDROID_LENS_INFO_AVAILABLE_APERTURES, |
| 1046 | lensApertures.data(), |
| 1047 | lensApertures.size()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1048 | |
Jacopo Mondi | 64f4f66 | 2020-05-25 16:08:22 +0200 | [diff] [blame] | 1049 | uint8_t lensFacing; |
| 1050 | switch (facing_) { |
| 1051 | default: |
| 1052 | case CAMERA_FACING_FRONT: |
| 1053 | lensFacing = ANDROID_LENS_FACING_FRONT; |
| 1054 | break; |
| 1055 | case CAMERA_FACING_BACK: |
| 1056 | lensFacing = ANDROID_LENS_FACING_BACK; |
| 1057 | break; |
| 1058 | case CAMERA_FACING_EXTERNAL: |
| 1059 | lensFacing = ANDROID_LENS_FACING_EXTERNAL; |
| 1060 | break; |
Jacopo Mondi | 857a216 | 2019-11-20 17:00:49 +0100 | [diff] [blame] | 1061 | } |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1062 | staticMetadata_->addEntry(ANDROID_LENS_FACING, &lensFacing, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1063 | |
| 1064 | std::vector<float> lensFocalLenghts = { |
| 1065 | 1, |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1066 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1067 | staticMetadata_->addEntry(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS, |
| 1068 | lensFocalLenghts.data(), |
| 1069 | lensFocalLenghts.size()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1070 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1071 | std::vector<uint8_t> opticalStabilizations = { |
| 1072 | ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF, |
| 1073 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1074 | staticMetadata_->addEntry(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION, |
| 1075 | opticalStabilizations.data(), |
| 1076 | opticalStabilizations.size()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1077 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1078 | float hypeFocalDistance = 0; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1079 | staticMetadata_->addEntry(ANDROID_LENS_INFO_HYPERFOCAL_DISTANCE, |
| 1080 | &hypeFocalDistance, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1081 | |
| 1082 | float minFocusDistance = 0; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1083 | staticMetadata_->addEntry(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE, |
| 1084 | &minFocusDistance, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1085 | |
| 1086 | /* Noise reduction modes. */ |
Jacopo Mondi | 0b5fecc | 2020-10-08 16:01:35 +0200 | [diff] [blame] | 1087 | { |
Jacopo Mondi | c268e4f | 2020-12-01 15:42:15 +0100 | [diff] [blame] | 1088 | std::vector<uint8_t> data; |
| 1089 | data.reserve(5); |
Jacopo Mondi | 0b5fecc | 2020-10-08 16:01:35 +0200 | [diff] [blame] | 1090 | const auto &infoMap = controlsInfo.find(&controls::draft::NoiseReductionMode); |
| 1091 | if (infoMap != controlsInfo.end()) { |
| 1092 | for (const auto &value : infoMap->second.values()) |
| 1093 | data.push_back(value.get<int32_t>()); |
| 1094 | } else { |
| 1095 | data.push_back(ANDROID_NOISE_REDUCTION_MODE_OFF); |
| 1096 | } |
| 1097 | staticMetadata_->addEntry(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES, |
| 1098 | data.data(), data.size()); |
| 1099 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1100 | |
| 1101 | /* Scaler static metadata. */ |
Jacopo Mondi | 31a1a62 | 2021-01-03 19:57:36 +0100 | [diff] [blame] | 1102 | { |
| 1103 | /* |
| 1104 | * \todo The digital zoom factor is a property that depends |
| 1105 | * on the desired output configuration and the sensor frame size |
| 1106 | * input to the ISP. This information is not available to the |
| 1107 | * Android HAL, not at initialization time at least. |
| 1108 | * |
| 1109 | * As a workaround rely on pipeline handlers initializing the |
| 1110 | * ScalerCrop control with the camera default configuration and |
| 1111 | * use the maximum and minimum crop rectangles to calculate the |
| 1112 | * digital zoom factor. |
| 1113 | */ |
| 1114 | const auto info = controlsInfo.find(&controls::ScalerCrop); |
| 1115 | Rectangle min = info->second.min().get<Rectangle>(); |
| 1116 | Rectangle max = info->second.max().get<Rectangle>(); |
| 1117 | float maxZoom = std::min(1.0f * max.width / min.width, |
| 1118 | 1.0f * max.height / min.height); |
| 1119 | staticMetadata_->addEntry(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM, |
| 1120 | &maxZoom, 1); |
| 1121 | } |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1122 | |
Jacopo Mondi | bde7b98 | 2020-05-25 17:18:28 +0200 | [diff] [blame] | 1123 | std::vector<uint32_t> availableStreamConfigurations; |
| 1124 | availableStreamConfigurations.reserve(streamConfigurations_.size() * 4); |
| 1125 | for (const auto &entry : streamConfigurations_) { |
Niklas Söderlund | 142a9ee | 2020-07-23 18:32:23 +0200 | [diff] [blame] | 1126 | availableStreamConfigurations.push_back(entry.androidFormat); |
Jacopo Mondi | bde7b98 | 2020-05-25 17:18:28 +0200 | [diff] [blame] | 1127 | availableStreamConfigurations.push_back(entry.resolution.width); |
| 1128 | availableStreamConfigurations.push_back(entry.resolution.height); |
| 1129 | availableStreamConfigurations.push_back( |
| 1130 | ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT); |
| 1131 | } |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1132 | staticMetadata_->addEntry(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, |
| 1133 | availableStreamConfigurations.data(), |
| 1134 | availableStreamConfigurations.size()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1135 | |
| 1136 | std::vector<int64_t> availableStallDurations = { |
| 1137 | ANDROID_SCALER_AVAILABLE_FORMATS_BLOB, 2560, 1920, 33333333, |
| 1138 | }; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1139 | staticMetadata_->addEntry(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, |
| 1140 | availableStallDurations.data(), |
| 1141 | availableStallDurations.size()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1142 | |
Jacopo Mondi | edd4b1d | 2021-01-04 16:18:29 +0100 | [diff] [blame] | 1143 | /* Use the minimum frame duration for all the YUV/RGB formats. */ |
| 1144 | if (minFrameDurationNsec > 0) { |
| 1145 | std::vector<int64_t> minFrameDurations; |
| 1146 | minFrameDurations.reserve(streamConfigurations_.size() * 4); |
| 1147 | for (const auto &entry : streamConfigurations_) { |
| 1148 | minFrameDurations.push_back(entry.androidFormat); |
| 1149 | minFrameDurations.push_back(entry.resolution.width); |
| 1150 | minFrameDurations.push_back(entry.resolution.height); |
| 1151 | minFrameDurations.push_back(minFrameDurationNsec); |
| 1152 | } |
| 1153 | staticMetadata_->addEntry(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, |
| 1154 | minFrameDurations.data(), |
| 1155 | minFrameDurations.size()); |
Jacopo Mondi | bde7b98 | 2020-05-25 17:18:28 +0200 | [diff] [blame] | 1156 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1157 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1158 | uint8_t croppingType = ANDROID_SCALER_CROPPING_TYPE_CENTER_ONLY; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1159 | staticMetadata_->addEntry(ANDROID_SCALER_CROPPING_TYPE, &croppingType, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1160 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1161 | /* Info static metadata. */ |
| 1162 | uint8_t supportedHWLevel = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1163 | staticMetadata_->addEntry(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL, |
| 1164 | &supportedHWLevel, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1165 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1166 | /* Request static metadata. */ |
| 1167 | int32_t partialResultCount = 1; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1168 | staticMetadata_->addEntry(ANDROID_REQUEST_PARTIAL_RESULT_COUNT, |
| 1169 | &partialResultCount, 1); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1170 | |
Jacopo Mondi | 5d91c8d | 2020-09-23 09:41:52 +0200 | [diff] [blame] | 1171 | { |
| 1172 | /* Default the value to 2 if not reported by the camera. */ |
| 1173 | uint8_t maxPipelineDepth = 2; |
| 1174 | const auto &infoMap = controlsInfo.find(&controls::draft::PipelineDepth); |
| 1175 | if (infoMap != controlsInfo.end()) |
| 1176 | maxPipelineDepth = infoMap->second.max().get<int32_t>(); |
| 1177 | staticMetadata_->addEntry(ANDROID_REQUEST_PIPELINE_MAX_DEPTH, |
| 1178 | &maxPipelineDepth, 1); |
| 1179 | } |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1180 | |
Niklas Söderlund | a2a6f95 | 2020-07-21 00:16:02 +0200 | [diff] [blame] | 1181 | /* LIMITED does not support reprocessing. */ |
| 1182 | uint32_t maxNumInputStreams = 0; |
| 1183 | staticMetadata_->addEntry(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, |
| 1184 | &maxNumInputStreams, 1); |
| 1185 | |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1186 | std::vector<uint8_t> availableCapabilities = { |
| 1187 | ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE, |
| 1188 | }; |
Niklas Söderlund | 7876d63 | 2020-07-21 00:16:24 +0200 | [diff] [blame] | 1189 | |
| 1190 | /* Report if camera supports RAW. */ |
Jacopo Mondi | eec6c54 | 2020-12-09 18:16:11 +0100 | [diff] [blame] | 1191 | bool rawStreamAvailable = false; |
Niklas Söderlund | 7876d63 | 2020-07-21 00:16:24 +0200 | [diff] [blame] | 1192 | std::unique_ptr<CameraConfiguration> cameraConfig = |
Niklas Söderlund | dbe8d27 | 2020-05-26 15:04:47 +0200 | [diff] [blame] | 1193 | camera_->generateConfiguration({ StreamRole::Raw }); |
Niklas Söderlund | 7876d63 | 2020-07-21 00:16:24 +0200 | [diff] [blame] | 1194 | if (cameraConfig && !cameraConfig->empty()) { |
| 1195 | const PixelFormatInfo &info = |
| 1196 | PixelFormatInfo::info(cameraConfig->at(0).pixelFormat); |
Niklas Söderlund | 35de31e | 2020-12-31 00:11:59 +0100 | [diff] [blame] | 1197 | /* Only advertise RAW support if RAW16 is possible. */ |
| 1198 | if (info.colourEncoding == PixelFormatInfo::ColourEncodingRAW && |
| 1199 | info.bitsPerPixel == 16) { |
Jacopo Mondi | eec6c54 | 2020-12-09 18:16:11 +0100 | [diff] [blame] | 1200 | rawStreamAvailable = true; |
Niklas Söderlund | 7876d63 | 2020-07-21 00:16:24 +0200 | [diff] [blame] | 1201 | availableCapabilities.push_back(ANDROID_REQUEST_AVAILABLE_CAPABILITIES_RAW); |
Jacopo Mondi | eec6c54 | 2020-12-09 18:16:11 +0100 | [diff] [blame] | 1202 | } |
Niklas Söderlund | 7876d63 | 2020-07-21 00:16:24 +0200 | [diff] [blame] | 1203 | } |
| 1204 | |
Jacopo Mondi | eec6c54 | 2020-12-09 18:16:11 +0100 | [diff] [blame] | 1205 | /* Number of { RAW, YUV, JPEG } supported output streams */ |
| 1206 | int32_t numOutStreams[] = { rawStreamAvailable, 2, 1 }; |
| 1207 | staticMetadata_->addEntry(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS, |
| 1208 | &numOutStreams, 3); |
| 1209 | |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1210 | staticMetadata_->addEntry(ANDROID_REQUEST_AVAILABLE_CAPABILITIES, |
| 1211 | availableCapabilities.data(), |
| 1212 | availableCapabilities.size()); |
Jacopo Mondi | b4893fc | 2019-09-04 16:18:18 +0200 | [diff] [blame] | 1213 | |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1214 | std::vector<int32_t> availableCharacteristicsKeys = { |
| 1215 | ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES, |
| 1216 | ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES, |
| 1217 | ANDROID_CONTROL_AE_AVAILABLE_MODES, |
| 1218 | ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, |
| 1219 | ANDROID_CONTROL_AE_COMPENSATION_RANGE, |
| 1220 | ANDROID_CONTROL_AE_COMPENSATION_STEP, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1221 | ANDROID_CONTROL_AE_LOCK_AVAILABLE, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1222 | ANDROID_CONTROL_AF_AVAILABLE_MODES, |
| 1223 | ANDROID_CONTROL_AVAILABLE_EFFECTS, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1224 | ANDROID_CONTROL_AVAILABLE_MODES, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1225 | ANDROID_CONTROL_AVAILABLE_SCENE_MODES, |
| 1226 | ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES, |
| 1227 | ANDROID_CONTROL_AWB_AVAILABLE_MODES, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1228 | ANDROID_CONTROL_AWB_LOCK_AVAILABLE, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1229 | ANDROID_CONTROL_MAX_REGIONS, |
| 1230 | ANDROID_CONTROL_SCENE_MODE_OVERRIDES, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1231 | ANDROID_FLASH_INFO_AVAILABLE, |
| 1232 | ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1233 | ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1234 | ANDROID_JPEG_MAX_SIZE, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1235 | ANDROID_LENS_FACING, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1236 | ANDROID_LENS_INFO_AVAILABLE_APERTURES, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1237 | ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS, |
| 1238 | ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION, |
| 1239 | ANDROID_LENS_INFO_HYPERFOCAL_DISTANCE, |
| 1240 | ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE, |
| 1241 | ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1242 | ANDROID_REQUEST_AVAILABLE_CAPABILITIES, |
| 1243 | ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, |
| 1244 | ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1245 | ANDROID_REQUEST_PARTIAL_RESULT_COUNT, |
| 1246 | ANDROID_REQUEST_PIPELINE_MAX_DEPTH, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1247 | ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM, |
| 1248 | ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, |
| 1249 | ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, |
| 1250 | ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, |
| 1251 | ANDROID_SCALER_CROPPING_TYPE, |
| 1252 | ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES, |
| 1253 | ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, |
| 1254 | ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT, |
| 1255 | ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE, |
Jacopo Mondi | edd4b1d | 2021-01-04 16:18:29 +0100 | [diff] [blame] | 1256 | ANDROID_SENSOR_INFO_MAX_FRAME_DURATION, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1257 | ANDROID_SENSOR_INFO_PHYSICAL_SIZE, |
| 1258 | ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE, |
| 1259 | ANDROID_SENSOR_INFO_SENSITIVITY_RANGE, |
| 1260 | ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE, |
| 1261 | ANDROID_SENSOR_ORIENTATION, |
| 1262 | ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES, |
| 1263 | ANDROID_STATISTICS_INFO_MAX_FACE_COUNT, |
| 1264 | ANDROID_SYNC_MAX_LATENCY, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1265 | }; |
| 1266 | staticMetadata_->addEntry(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, |
| 1267 | availableCharacteristicsKeys.data(), |
| 1268 | availableCharacteristicsKeys.size()); |
| 1269 | |
| 1270 | std::vector<int32_t> availableRequestKeys = { |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1271 | ANDROID_COLOR_CORRECTION_ABERRATION_MODE, |
| 1272 | ANDROID_CONTROL_AE_ANTIBANDING_MODE, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1273 | ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1274 | ANDROID_CONTROL_AE_LOCK, |
| 1275 | ANDROID_CONTROL_AE_MODE, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1276 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, |
Jacopo Mondi | 09b1d0f | 2020-07-24 16:10:23 +0200 | [diff] [blame] | 1277 | ANDROID_CONTROL_AE_TARGET_FPS_RANGE, |
Jacopo Mondi | da4b325 | 2021-01-29 14:21:56 +0100 | [diff] [blame] | 1278 | ANDROID_CONTROL_AF_MODE, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1279 | ANDROID_CONTROL_AF_TRIGGER, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1280 | ANDROID_CONTROL_AWB_LOCK, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1281 | ANDROID_CONTROL_AWB_MODE, |
| 1282 | ANDROID_CONTROL_CAPTURE_INTENT, |
Jacopo Mondi | da4b325 | 2021-01-29 14:21:56 +0100 | [diff] [blame] | 1283 | ANDROID_CONTROL_EFFECT_MODE, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1284 | ANDROID_CONTROL_MODE, |
Jacopo Mondi | da4b325 | 2021-01-29 14:21:56 +0100 | [diff] [blame] | 1285 | ANDROID_CONTROL_SCENE_MODE, |
| 1286 | ANDROID_CONTROL_VIDEO_STABILIZATION_MODE, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1287 | ANDROID_FLASH_MODE, |
Jacopo Mondi | da4b325 | 2021-01-29 14:21:56 +0100 | [diff] [blame] | 1288 | ANDROID_JPEG_ORIENTATION, |
| 1289 | ANDROID_JPEG_QUALITY, |
| 1290 | ANDROID_JPEG_THUMBNAIL_QUALITY, |
| 1291 | ANDROID_JPEG_THUMBNAIL_SIZE, |
Jacopo Mondi | 09b1d0f | 2020-07-24 16:10:23 +0200 | [diff] [blame] | 1292 | ANDROID_LENS_APERTURE, |
| 1293 | ANDROID_LENS_OPTICAL_STABILIZATION_MODE, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1294 | ANDROID_NOISE_REDUCTION_MODE, |
Jacopo Mondi | da4b325 | 2021-01-29 14:21:56 +0100 | [diff] [blame] | 1295 | ANDROID_SCALER_CROP_REGION, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1296 | ANDROID_STATISTICS_FACE_DETECT_MODE |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1297 | }; |
| 1298 | staticMetadata_->addEntry(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, |
| 1299 | availableRequestKeys.data(), |
| 1300 | availableRequestKeys.size()); |
| 1301 | |
| 1302 | std::vector<int32_t> availableResultKeys = { |
Jacopo Mondi | 520c366 | 2021-02-03 14:44:34 +0100 | [diff] [blame] | 1303 | ANDROID_COLOR_CORRECTION_ABERRATION_MODE, |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 1304 | ANDROID_CONTROL_AE_ANTIBANDING_MODE, |
Jacopo Mondi | e9c2875 | 2021-02-03 14:47:45 +0100 | [diff] [blame] | 1305 | ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1306 | ANDROID_CONTROL_AE_LOCK, |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 1307 | ANDROID_CONTROL_AE_MODE, |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 1308 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, |
| 1309 | ANDROID_CONTROL_AE_STATE, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1310 | ANDROID_CONTROL_AE_TARGET_FPS_RANGE, |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 1311 | ANDROID_CONTROL_AF_MODE, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1312 | ANDROID_CONTROL_AF_STATE, |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 1313 | ANDROID_CONTROL_AF_TRIGGER, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1314 | ANDROID_CONTROL_AWB_LOCK, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1315 | ANDROID_CONTROL_AWB_MODE, |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 1316 | ANDROID_CONTROL_AWB_STATE, |
| 1317 | ANDROID_CONTROL_CAPTURE_INTENT, |
| 1318 | ANDROID_CONTROL_EFFECT_MODE, |
| 1319 | ANDROID_CONTROL_MODE, |
| 1320 | ANDROID_CONTROL_SCENE_MODE, |
| 1321 | ANDROID_CONTROL_VIDEO_STABILIZATION_MODE, |
| 1322 | ANDROID_FLASH_MODE, |
| 1323 | ANDROID_FLASH_STATE, |
Paul Elder | abfabdd | 2021-01-23 13:54:28 +0900 | [diff] [blame] | 1324 | ANDROID_JPEG_GPS_COORDINATES, |
| 1325 | ANDROID_JPEG_GPS_PROCESSING_METHOD, |
| 1326 | ANDROID_JPEG_GPS_TIMESTAMP, |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1327 | ANDROID_JPEG_ORIENTATION, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1328 | ANDROID_JPEG_QUALITY, |
| 1329 | ANDROID_JPEG_SIZE, |
Paul Elder | 1264628 | 2021-01-23 13:56:01 +0900 | [diff] [blame] | 1330 | ANDROID_JPEG_THUMBNAIL_QUALITY, |
| 1331 | ANDROID_JPEG_THUMBNAIL_SIZE, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1332 | ANDROID_LENS_APERTURE, |
| 1333 | ANDROID_LENS_FOCAL_LENGTH, |
| 1334 | ANDROID_LENS_OPTICAL_STABILIZATION_MODE, |
| 1335 | ANDROID_LENS_STATE, |
| 1336 | ANDROID_NOISE_REDUCTION_MODE, |
| 1337 | ANDROID_REQUEST_PIPELINE_DEPTH, |
| 1338 | ANDROID_SCALER_CROP_REGION, |
| 1339 | ANDROID_SENSOR_EXPOSURE_TIME, |
| 1340 | ANDROID_SENSOR_ROLLING_SHUTTER_SKEW, |
Jacopo Mondi | 5360d80 | 2021-02-03 16:43:22 +0100 | [diff] [blame] | 1341 | ANDROID_SENSOR_TEST_PATTERN_MODE, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1342 | ANDROID_SENSOR_TIMESTAMP, |
| 1343 | ANDROID_STATISTICS_FACE_DETECT_MODE, |
| 1344 | ANDROID_STATISTICS_LENS_SHADING_MAP_MODE, |
Jacopo Mondi | f29601e | 2021-02-03 16:47:30 +0100 | [diff] [blame] | 1345 | ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE, |
Jacopo Mondi | 6741ab8 | 2021-01-29 14:15:17 +0100 | [diff] [blame] | 1346 | ANDROID_STATISTICS_SCENE_FLICKER, |
Jacopo Mondi | 19f85f4 | 2019-09-04 16:18:25 +0200 | [diff] [blame] | 1347 | }; |
| 1348 | staticMetadata_->addEntry(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, |
| 1349 | availableResultKeys.data(), |
| 1350 | availableResultKeys.size()); |
| 1351 | |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1352 | if (!staticMetadata_->isValid()) { |
| 1353 | LOG(HAL, Error) << "Failed to construct static metadata"; |
Hirokazu Honda | dca709c | 2021-03-24 16:07:56 +0900 | [diff] [blame^] | 1354 | staticMetadata_.reset(); |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1355 | return nullptr; |
| 1356 | } |
| 1357 | |
| 1358 | return staticMetadata_->get(); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1359 | } |
| 1360 | |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1361 | CameraMetadata *CameraDevice::requestTemplatePreview() |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1362 | { |
Jacopo Mondi | 6370347 | 2019-09-04 16:18:22 +0200 | [diff] [blame] | 1363 | /* |
| 1364 | * \todo Keep this in sync with the actual number of entries. |
Jacopo Mondi | 09b1d0f | 2020-07-24 16:10:23 +0200 | [diff] [blame] | 1365 | * Currently: 20 entries, 35 bytes |
Jacopo Mondi | 6370347 | 2019-09-04 16:18:22 +0200 | [diff] [blame] | 1366 | */ |
Jacopo Mondi | 9690d08 | 2021-02-03 16:37:20 +0100 | [diff] [blame] | 1367 | CameraMetadata *requestTemplate = new CameraMetadata(21, 36); |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1368 | if (!requestTemplate->isValid()) { |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1369 | delete requestTemplate; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1370 | return nullptr; |
| 1371 | } |
| 1372 | |
Jacopo Mondi | f179616 | 2021-03-08 17:03:14 +0100 | [diff] [blame] | 1373 | /* Get the FPS range registered in the static metadata. */ |
| 1374 | camera_metadata_ro_entry_t entry; |
| 1375 | bool found = staticMetadata_->getEntry(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, |
| 1376 | &entry); |
| 1377 | if (!found) { |
| 1378 | LOG(HAL, Error) << "Cannot create capture template without FPS range"; |
| 1379 | return nullptr; |
| 1380 | } |
| 1381 | |
| 1382 | /* |
Jacopo Mondi | eb5a9d8 | 2021-03-08 17:00:39 +0100 | [diff] [blame] | 1383 | * Assume the AE_AVAILABLE_TARGET_FPS_RANGE static metadata |
Jacopo Mondi | f179616 | 2021-03-08 17:03:14 +0100 | [diff] [blame] | 1384 | * has been assembled as {{min, max} {max, max}}. |
| 1385 | */ |
| 1386 | requestTemplate->addEntry(ANDROID_CONTROL_AE_TARGET_FPS_RANGE, |
| 1387 | entry.data.i32, 2); |
| 1388 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1389 | uint8_t aeMode = ANDROID_CONTROL_AE_MODE_ON; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1390 | requestTemplate->addEntry(ANDROID_CONTROL_AE_MODE, |
| 1391 | &aeMode, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1392 | |
| 1393 | int32_t aeExposureCompensation = 0; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1394 | requestTemplate->addEntry(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION, |
| 1395 | &aeExposureCompensation, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1396 | |
| 1397 | uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1398 | requestTemplate->addEntry(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, |
| 1399 | &aePrecaptureTrigger, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1400 | |
| 1401 | uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_OFF; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1402 | requestTemplate->addEntry(ANDROID_CONTROL_AE_LOCK, |
| 1403 | &aeLock, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1404 | |
Jacopo Mondi | 09b1d0f | 2020-07-24 16:10:23 +0200 | [diff] [blame] | 1405 | uint8_t aeAntibandingMode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO; |
| 1406 | requestTemplate->addEntry(ANDROID_CONTROL_AE_ANTIBANDING_MODE, |
| 1407 | &aeAntibandingMode, 1); |
| 1408 | |
Jacopo Mondi | 9690d08 | 2021-02-03 16:37:20 +0100 | [diff] [blame] | 1409 | uint8_t afMode = ANDROID_CONTROL_AF_MODE_OFF; |
| 1410 | requestTemplate->addEntry(ANDROID_CONTROL_AF_MODE, &afMode, 1); |
| 1411 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1412 | uint8_t afTrigger = ANDROID_CONTROL_AF_TRIGGER_IDLE; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1413 | requestTemplate->addEntry(ANDROID_CONTROL_AF_TRIGGER, |
| 1414 | &afTrigger, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1415 | |
| 1416 | uint8_t awbMode = ANDROID_CONTROL_AWB_MODE_AUTO; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1417 | requestTemplate->addEntry(ANDROID_CONTROL_AWB_MODE, |
| 1418 | &awbMode, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1419 | |
| 1420 | uint8_t awbLock = ANDROID_CONTROL_AWB_LOCK_OFF; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1421 | requestTemplate->addEntry(ANDROID_CONTROL_AWB_LOCK, |
| 1422 | &awbLock, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1423 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1424 | uint8_t flashMode = ANDROID_FLASH_MODE_OFF; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1425 | requestTemplate->addEntry(ANDROID_FLASH_MODE, |
| 1426 | &flashMode, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1427 | |
| 1428 | uint8_t faceDetectMode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1429 | requestTemplate->addEntry(ANDROID_STATISTICS_FACE_DETECT_MODE, |
| 1430 | &faceDetectMode, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1431 | |
Jacopo Mondi | 9b361dc | 2019-09-04 16:18:21 +0200 | [diff] [blame] | 1432 | uint8_t noiseReduction = ANDROID_NOISE_REDUCTION_MODE_OFF; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1433 | requestTemplate->addEntry(ANDROID_NOISE_REDUCTION_MODE, |
| 1434 | &noiseReduction, 1); |
Jacopo Mondi | 9b361dc | 2019-09-04 16:18:21 +0200 | [diff] [blame] | 1435 | |
| 1436 | uint8_t aberrationMode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1437 | requestTemplate->addEntry(ANDROID_COLOR_CORRECTION_ABERRATION_MODE, |
| 1438 | &aberrationMode, 1); |
Jacopo Mondi | 9b361dc | 2019-09-04 16:18:21 +0200 | [diff] [blame] | 1439 | |
Jacopo Mondi | 09b1d0f | 2020-07-24 16:10:23 +0200 | [diff] [blame] | 1440 | uint8_t controlMode = ANDROID_CONTROL_MODE_AUTO; |
| 1441 | requestTemplate->addEntry(ANDROID_CONTROL_MODE, &controlMode, 1); |
| 1442 | |
| 1443 | float lensAperture = 2.53 / 100; |
| 1444 | requestTemplate->addEntry(ANDROID_LENS_APERTURE, &lensAperture, 1); |
| 1445 | |
| 1446 | uint8_t opticalStabilization = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF; |
| 1447 | requestTemplate->addEntry(ANDROID_LENS_OPTICAL_STABILIZATION_MODE, |
| 1448 | &opticalStabilization, 1); |
| 1449 | |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1450 | uint8_t captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1451 | requestTemplate->addEntry(ANDROID_CONTROL_CAPTURE_INTENT, |
| 1452 | &captureIntent, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1453 | |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1454 | return requestTemplate; |
| 1455 | } |
| 1456 | |
Jacopo Mondi | eb5a9d8 | 2021-03-08 17:00:39 +0100 | [diff] [blame] | 1457 | CameraMetadata *CameraDevice::requestTemplateVideo() |
| 1458 | { |
| 1459 | CameraMetadata *previewTemplate = requestTemplatePreview(); |
| 1460 | if (!previewTemplate) |
| 1461 | return nullptr; |
| 1462 | |
| 1463 | /* |
| 1464 | * The video template requires a fixed FPS range. Everything else |
| 1465 | * stays the same as the preview template. |
| 1466 | */ |
| 1467 | camera_metadata_ro_entry_t entry; |
| 1468 | staticMetadata_->getEntry(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, |
| 1469 | &entry); |
| 1470 | |
| 1471 | /* |
| 1472 | * Assume the AE_AVAILABLE_TARGET_FPS_RANGE static metadata |
| 1473 | * has been assembled as {{min, max} {max, max}}. |
| 1474 | */ |
| 1475 | previewTemplate->updateEntry(ANDROID_CONTROL_AE_TARGET_FPS_RANGE, |
| 1476 | entry.data.i32 + 2, 2); |
| 1477 | |
| 1478 | return previewTemplate; |
| 1479 | } |
| 1480 | |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1481 | /* |
| 1482 | * Produce a metadata pack to be used as template for a capture request. |
| 1483 | */ |
| 1484 | const camera_metadata_t *CameraDevice::constructDefaultRequestSettings(int type) |
| 1485 | { |
| 1486 | auto it = requestTemplates_.find(type); |
| 1487 | if (it != requestTemplates_.end()) |
| 1488 | return it->second->get(); |
| 1489 | |
| 1490 | /* Use the capture intent matching the requested template type. */ |
| 1491 | CameraMetadata *requestTemplate; |
| 1492 | uint8_t captureIntent; |
| 1493 | switch (type) { |
| 1494 | case CAMERA3_TEMPLATE_PREVIEW: |
| 1495 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW; |
Jacopo Mondi | eb5a9d8 | 2021-03-08 17:00:39 +0100 | [diff] [blame] | 1496 | requestTemplate = requestTemplatePreview(); |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1497 | break; |
| 1498 | case CAMERA3_TEMPLATE_STILL_CAPTURE: |
Jacopo Mondi | eb5a9d8 | 2021-03-08 17:00:39 +0100 | [diff] [blame] | 1499 | /* |
| 1500 | * Use the preview template for still capture, they only differ |
| 1501 | * for the torch mode we currently do not support. |
| 1502 | */ |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1503 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE; |
Jacopo Mondi | eb5a9d8 | 2021-03-08 17:00:39 +0100 | [diff] [blame] | 1504 | requestTemplate = requestTemplatePreview(); |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1505 | break; |
| 1506 | case CAMERA3_TEMPLATE_VIDEO_RECORD: |
| 1507 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD; |
Jacopo Mondi | eb5a9d8 | 2021-03-08 17:00:39 +0100 | [diff] [blame] | 1508 | requestTemplate = requestTemplateVideo(); |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1509 | break; |
| 1510 | case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT: |
| 1511 | captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT; |
Jacopo Mondi | eb5a9d8 | 2021-03-08 17:00:39 +0100 | [diff] [blame] | 1512 | requestTemplate = requestTemplateVideo(); |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1513 | break; |
Jacopo Mondi | 2c34991 | 2021-03-08 16:13:58 +0100 | [diff] [blame] | 1514 | /* \todo Implement templates generation for the remaining use cases. */ |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1515 | case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1516 | case CAMERA3_TEMPLATE_MANUAL: |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1517 | default: |
Jacopo Mondi | 2c34991 | 2021-03-08 16:13:58 +0100 | [diff] [blame] | 1518 | LOG(HAL, Error) << "Unsupported template request type: " << type; |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1519 | return nullptr; |
| 1520 | } |
| 1521 | |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1522 | if (!requestTemplate || !requestTemplate->isValid()) { |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1523 | LOG(HAL, Error) << "Failed to construct request template"; |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1524 | delete requestTemplate; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1525 | return nullptr; |
| 1526 | } |
| 1527 | |
Jacopo Mondi | 8a02d44 | 2020-07-24 15:47:50 +0200 | [diff] [blame] | 1528 | requestTemplate->updateEntry(ANDROID_CONTROL_CAPTURE_INTENT, |
| 1529 | &captureIntent, 1); |
| 1530 | |
Jacopo Mondi | fcd5a4f | 2019-09-04 16:18:23 +0200 | [diff] [blame] | 1531 | requestTemplates_[type] = requestTemplate; |
| 1532 | return requestTemplate->get(); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1533 | } |
| 1534 | |
Hirokazu Honda | c1ae905 | 2020-10-28 18:50:23 +0900 | [diff] [blame] | 1535 | PixelFormat CameraDevice::toPixelFormat(int format) const |
Kieran Bingham | 43e3b80 | 2020-06-26 09:58:49 +0100 | [diff] [blame] | 1536 | { |
| 1537 | /* Translate Android format code to libcamera pixel format. */ |
| 1538 | auto it = formatsMap_.find(format); |
| 1539 | if (it == formatsMap_.end()) { |
| 1540 | LOG(HAL, Error) << "Requested format " << utils::hex(format) |
| 1541 | << " not supported"; |
| 1542 | return PixelFormat(); |
| 1543 | } |
| 1544 | |
| 1545 | return it->second; |
| 1546 | } |
| 1547 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1548 | /* |
| 1549 | * Inspect the stream_list to produce a list of StreamConfiguration to |
| 1550 | * be use to configure the Camera. |
| 1551 | */ |
| 1552 | int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) |
| 1553 | { |
Jacopo Mondi | 8fed613 | 2020-12-04 15:26:47 +0100 | [diff] [blame] | 1554 | /* Before any configuration attempt, stop the camera if it's running. */ |
| 1555 | if (running_) { |
| 1556 | worker_.stop(); |
| 1557 | camera_->stop(); |
| 1558 | running_ = false; |
| 1559 | } |
| 1560 | |
Kieran Bingham | 0a9244e | 2020-06-26 20:19:10 +0100 | [diff] [blame] | 1561 | /* |
| 1562 | * Generate an empty configuration, and construct a StreamConfiguration |
| 1563 | * for each camera3_stream to add to it. |
| 1564 | */ |
| 1565 | config_ = camera_->generateConfiguration(); |
| 1566 | if (!config_) { |
| 1567 | LOG(HAL, Error) << "Failed to generate camera configuration"; |
| 1568 | return -EINVAL; |
| 1569 | } |
| 1570 | |
Kieran Bingham | 2f34f5e | 2020-07-01 16:42:13 +0100 | [diff] [blame] | 1571 | /* |
| 1572 | * Clear and remove any existing configuration from previous calls, and |
| 1573 | * ensure the required entries are available without further |
Jacopo Mondi | 9ac8f3e | 2020-09-04 15:25:55 +0200 | [diff] [blame] | 1574 | * reallocation. |
Kieran Bingham | 2f34f5e | 2020-07-01 16:42:13 +0100 | [diff] [blame] | 1575 | */ |
| 1576 | streams_.clear(); |
| 1577 | streams_.reserve(stream_list->num_streams); |
| 1578 | |
Hirokazu Honda | 2bc6ba2 | 2020-12-11 09:53:35 +0000 | [diff] [blame] | 1579 | std::vector<Camera3StreamConfig> streamConfigs; |
| 1580 | streamConfigs.reserve(stream_list->num_streams); |
| 1581 | |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1582 | /* First handle all non-MJPEG streams. */ |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1583 | camera3_stream_t *jpegStream = nullptr; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1584 | for (unsigned int i = 0; i < stream_list->num_streams; ++i) { |
| 1585 | camera3_stream_t *stream = stream_list->streams[i]; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1586 | Size size(stream->width, stream->height); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1587 | |
Kieran Bingham | 43e3b80 | 2020-06-26 09:58:49 +0100 | [diff] [blame] | 1588 | PixelFormat format = toPixelFormat(stream->format); |
| 1589 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1590 | LOG(HAL, Info) << "Stream #" << i |
| 1591 | << ", direction: " << stream->stream_type |
| 1592 | << ", width: " << stream->width |
| 1593 | << ", height: " << stream->height |
Kieran Bingham | 43e3b80 | 2020-06-26 09:58:49 +0100 | [diff] [blame] | 1594 | << ", format: " << utils::hex(stream->format) |
| 1595 | << " (" << format.toString() << ")"; |
Kieran Bingham | 0a9244e | 2020-06-26 20:19:10 +0100 | [diff] [blame] | 1596 | |
| 1597 | if (!format.isValid()) |
| 1598 | return -EINVAL; |
| 1599 | |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1600 | /* Defer handling of MJPEG streams until all others are known. */ |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1601 | if (stream->format == HAL_PIXEL_FORMAT_BLOB) { |
| 1602 | if (jpegStream) { |
| 1603 | LOG(HAL, Error) |
| 1604 | << "Multiple JPEG streams are not supported"; |
| 1605 | return -EINVAL; |
| 1606 | } |
| 1607 | |
| 1608 | jpegStream = stream; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1609 | continue; |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1610 | } |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1611 | |
Hirokazu Honda | 2bc6ba2 | 2020-12-11 09:53:35 +0000 | [diff] [blame] | 1612 | Camera3StreamConfig streamConfig; |
| 1613 | streamConfig.streams = { { stream, CameraStream::Type::Direct } }; |
| 1614 | streamConfig.config.size = size; |
| 1615 | streamConfig.config.pixelFormat = format; |
| 1616 | streamConfigs.push_back(std::move(streamConfig)); |
Laurent Pinchart | de1b994 | 2021-03-05 13:53:53 +0200 | [diff] [blame] | 1617 | |
| 1618 | /* This stream will be produced by hardware. */ |
| 1619 | stream->usage |= GRALLOC_USAGE_HW_CAMERA_WRITE; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1620 | } |
| 1621 | |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1622 | /* Now handle the MJPEG streams, adding a new stream if required. */ |
| 1623 | if (jpegStream) { |
Jacopo Mondi | 94c4d49 | 2020-09-04 16:06:11 +0200 | [diff] [blame] | 1624 | CameraStream::Type type; |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1625 | int index = -1; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1626 | |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1627 | /* Search for a compatible stream in the non-JPEG ones. */ |
Hirokazu Honda | 2bc6ba2 | 2020-12-11 09:53:35 +0000 | [diff] [blame] | 1628 | for (size_t i = 0; i < streamConfigs.size(); ++i) { |
Laurent Pinchart | de1b994 | 2021-03-05 13:53:53 +0200 | [diff] [blame] | 1629 | Camera3StreamConfig &streamConfig = streamConfigs[i]; |
| 1630 | const auto &cfg = streamConfig.config; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1631 | |
| 1632 | /* |
| 1633 | * \todo The PixelFormat must also be compatible with |
| 1634 | * the encoder. |
| 1635 | */ |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1636 | if (cfg.size.width != jpegStream->width || |
| 1637 | cfg.size.height != jpegStream->height) |
| 1638 | continue; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1639 | |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1640 | LOG(HAL, Info) |
| 1641 | << "Android JPEG stream mapped to libcamera stream " << i; |
| 1642 | |
Jacopo Mondi | 94c4d49 | 2020-09-04 16:06:11 +0200 | [diff] [blame] | 1643 | type = CameraStream::Type::Mapped; |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1644 | index = i; |
Laurent Pinchart | de1b994 | 2021-03-05 13:53:53 +0200 | [diff] [blame] | 1645 | |
| 1646 | /* |
| 1647 | * The source stream will be read by software to |
| 1648 | * produce the JPEG stream. |
| 1649 | */ |
| 1650 | camera3_stream_t *stream = streamConfig.streams[0].stream; |
| 1651 | stream->usage |= GRALLOC_USAGE_SW_READ_OFTEN; |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1652 | break; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | /* |
| 1656 | * Without a compatible match for JPEG encoding we must |
| 1657 | * introduce a new stream to satisfy the request requirements. |
| 1658 | */ |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1659 | if (index < 0) { |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1660 | /* |
| 1661 | * \todo The pixelFormat should be a 'best-fit' choice |
| 1662 | * and may require a validation cycle. This is not yet |
| 1663 | * handled, and should be considered as part of any |
| 1664 | * stream configuration reworks. |
| 1665 | */ |
Hirokazu Honda | 2bc6ba2 | 2020-12-11 09:53:35 +0000 | [diff] [blame] | 1666 | Camera3StreamConfig streamConfig; |
| 1667 | streamConfig.config.size.width = jpegStream->width; |
| 1668 | streamConfig.config.size.height = jpegStream->height; |
| 1669 | streamConfig.config.pixelFormat = formats::NV12; |
| 1670 | streamConfigs.push_back(std::move(streamConfig)); |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1671 | |
Hirokazu Honda | 2bc6ba2 | 2020-12-11 09:53:35 +0000 | [diff] [blame] | 1672 | LOG(HAL, Info) << "Adding " << streamConfig.config.toString() |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1673 | << " for MJPEG support"; |
| 1674 | |
Jacopo Mondi | 94c4d49 | 2020-09-04 16:06:11 +0200 | [diff] [blame] | 1675 | type = CameraStream::Type::Internal; |
Hirokazu Honda | 2bc6ba2 | 2020-12-11 09:53:35 +0000 | [diff] [blame] | 1676 | index = streamConfigs.size() - 1; |
Jacopo Mondi | c82f944 | 2020-09-02 11:58:00 +0200 | [diff] [blame] | 1677 | } |
| 1678 | |
Laurent Pinchart | de1b994 | 2021-03-05 13:53:53 +0200 | [diff] [blame] | 1679 | /* The JPEG stream will be produced by software. */ |
| 1680 | jpegStream->usage |= GRALLOC_USAGE_SW_WRITE_OFTEN; |
| 1681 | |
Hirokazu Honda | 2bc6ba2 | 2020-12-11 09:53:35 +0000 | [diff] [blame] | 1682 | streamConfigs[index].streams.push_back({ jpegStream, type }); |
| 1683 | } |
| 1684 | |
Hirokazu Honda | 26d90af | 2020-12-11 09:53:36 +0000 | [diff] [blame] | 1685 | sortCamera3StreamConfigs(streamConfigs, jpegStream); |
Hirokazu Honda | 2bc6ba2 | 2020-12-11 09:53:35 +0000 | [diff] [blame] | 1686 | for (const auto &streamConfig : streamConfigs) { |
| 1687 | config_->addConfiguration(streamConfig.config); |
| 1688 | |
| 1689 | for (auto &stream : streamConfig.streams) { |
| 1690 | streams_.emplace_back(this, stream.type, stream.stream, |
| 1691 | config_->size() - 1); |
| 1692 | stream.stream->priv = static_cast<void *>(&streams_.back()); |
| 1693 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1694 | } |
| 1695 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1696 | switch (config_->validate()) { |
| 1697 | case CameraConfiguration::Valid: |
| 1698 | break; |
| 1699 | case CameraConfiguration::Adjusted: |
| 1700 | LOG(HAL, Info) << "Camera configuration adjusted"; |
Kieran Bingham | 9f07aeb | 2020-07-20 22:52:14 +0100 | [diff] [blame] | 1701 | |
| 1702 | for (const StreamConfiguration &cfg : *config_) |
| 1703 | LOG(HAL, Info) << " - " << cfg.toString(); |
| 1704 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1705 | config_.reset(); |
| 1706 | return -EINVAL; |
| 1707 | case CameraConfiguration::Invalid: |
| 1708 | LOG(HAL, Info) << "Camera configuration invalid"; |
| 1709 | config_.reset(); |
| 1710 | return -EINVAL; |
| 1711 | } |
| 1712 | |
Jacopo Mondi | 3c84d88 | 2020-10-02 19:40:42 +0200 | [diff] [blame] | 1713 | /* |
Jacopo Mondi | b84e35d | 2020-10-03 13:21:50 +0200 | [diff] [blame] | 1714 | * Once the CameraConfiguration has been adjusted/validated |
| 1715 | * it can be applied to the camera. |
| 1716 | */ |
| 1717 | int ret = camera_->configure(config_.get()); |
| 1718 | if (ret) { |
| 1719 | LOG(HAL, Error) << "Failed to configure camera '" |
| 1720 | << camera_->id() << "'"; |
| 1721 | return ret; |
| 1722 | } |
| 1723 | |
| 1724 | /* |
Jacopo Mondi | 3c84d88 | 2020-10-02 19:40:42 +0200 | [diff] [blame] | 1725 | * Configure the HAL CameraStream instances using the associated |
| 1726 | * StreamConfiguration and set the number of required buffers in |
| 1727 | * the Android camera3_stream_t. |
| 1728 | */ |
Jacopo Mondi | 6c8837d | 2020-10-03 13:45:29 +0200 | [diff] [blame] | 1729 | for (CameraStream &cameraStream : streams_) { |
Kieran Bingham | c7bcae0 | 2020-10-13 15:58:26 +0100 | [diff] [blame] | 1730 | ret = cameraStream.configure(); |
Jacopo Mondi | 6c8837d | 2020-10-03 13:45:29 +0200 | [diff] [blame] | 1731 | if (ret) { |
| 1732 | LOG(HAL, Error) << "Failed to configure camera stream"; |
Jacopo Mondi | 3c84d88 | 2020-10-02 19:40:42 +0200 | [diff] [blame] | 1733 | return ret; |
Jacopo Mondi | 6c8837d | 2020-10-03 13:45:29 +0200 | [diff] [blame] | 1734 | } |
Kieran Bingham | 0a9244e | 2020-06-26 20:19:10 +0100 | [diff] [blame] | 1735 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1736 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1737 | return 0; |
| 1738 | } |
| 1739 | |
Kieran Bingham | 74ab442 | 2020-07-01 13:25:38 +0100 | [diff] [blame] | 1740 | FrameBuffer *CameraDevice::createFrameBuffer(const buffer_handle_t camera3buffer) |
| 1741 | { |
| 1742 | std::vector<FrameBuffer::Plane> planes; |
Kieran Bingham | f4c65be | 2020-07-06 16:12:03 +0100 | [diff] [blame] | 1743 | for (int i = 0; i < camera3buffer->numFds; i++) { |
| 1744 | /* Skip unused planes. */ |
| 1745 | if (camera3buffer->data[i] == -1) |
| 1746 | break; |
| 1747 | |
Kieran Bingham | 74ab442 | 2020-07-01 13:25:38 +0100 | [diff] [blame] | 1748 | FrameBuffer::Plane plane; |
| 1749 | plane.fd = FileDescriptor(camera3buffer->data[i]); |
Kieran Bingham | f4c65be | 2020-07-06 16:12:03 +0100 | [diff] [blame] | 1750 | if (!plane.fd.isValid()) { |
| 1751 | LOG(HAL, Error) << "Failed to obtain FileDescriptor (" |
| 1752 | << camera3buffer->data[i] << ") " |
| 1753 | << " on plane " << i; |
| 1754 | return nullptr; |
| 1755 | } |
| 1756 | |
Kieran Bingham | 6bc652e | 2020-07-17 16:18:32 +0100 | [diff] [blame] | 1757 | off_t length = lseek(plane.fd.fd(), 0, SEEK_END); |
| 1758 | if (length == -1) { |
| 1759 | LOG(HAL, Error) << "Failed to query plane length"; |
| 1760 | return nullptr; |
| 1761 | } |
| 1762 | |
| 1763 | plane.length = length; |
Kieran Bingham | 74ab442 | 2020-07-01 13:25:38 +0100 | [diff] [blame] | 1764 | planes.push_back(std::move(plane)); |
| 1765 | } |
| 1766 | |
| 1767 | return new FrameBuffer(std::move(planes)); |
| 1768 | } |
| 1769 | |
Jacopo Mondi | bb2b400 | 2021-01-04 16:20:05 +0100 | [diff] [blame] | 1770 | int CameraDevice::processControls(Camera3RequestDescriptor *descriptor) |
| 1771 | { |
| 1772 | const CameraMetadata &settings = descriptor->settings_; |
| 1773 | if (!settings.isValid()) |
| 1774 | return 0; |
| 1775 | |
| 1776 | /* Translate the Android request settings to libcamera controls. */ |
| 1777 | camera_metadata_ro_entry_t entry; |
| 1778 | if (settings.getEntry(ANDROID_SCALER_CROP_REGION, &entry)) { |
| 1779 | const int32_t *data = entry.data.i32; |
| 1780 | Rectangle cropRegion{ data[0], data[1], |
| 1781 | static_cast<unsigned int>(data[2]), |
| 1782 | static_cast<unsigned int>(data[3]) }; |
| 1783 | ControlList &controls = descriptor->request_->controls(); |
| 1784 | controls.set(controls::ScalerCrop, cropRegion); |
| 1785 | } |
| 1786 | |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 1790 | int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Request) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1791 | { |
Jacopo Mondi | c5b732b | 2020-12-01 17:12:19 +0100 | [diff] [blame] | 1792 | if (!camera3Request) { |
| 1793 | LOG(HAL, Error) << "No capture request provided"; |
| 1794 | return -EINVAL; |
| 1795 | } |
| 1796 | |
Kieran Bingham | 61f4296 | 2020-07-01 13:40:17 +0100 | [diff] [blame] | 1797 | if (!camera3Request->num_output_buffers) { |
| 1798 | LOG(HAL, Error) << "No output buffers provided"; |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 1799 | return -EINVAL; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | /* Start the camera if that's the first request we handle. */ |
| 1803 | if (!running_) { |
Jacopo Mondi | 4b1aa21 | 2020-10-06 17:56:50 +0200 | [diff] [blame] | 1804 | worker_.start(); |
| 1805 | |
Niklas Söderlund | a1c5450 | 2019-11-25 17:51:06 +0100 | [diff] [blame] | 1806 | int ret = camera_->start(); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1807 | if (ret) { |
| 1808 | LOG(HAL, Error) << "Failed to start camera"; |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 1809 | return ret; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | running_ = true; |
| 1813 | } |
| 1814 | |
| 1815 | /* |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1816 | * Save the request descriptors for use at completion time. |
| 1817 | * The descriptor and the associated memory reserved here are freed |
| 1818 | * at request complete time. |
| 1819 | */ |
| 1820 | Camera3RequestDescriptor *descriptor = |
Jacopo Mondi | dd1cd53 | 2021-01-21 10:51:05 +0100 | [diff] [blame] | 1821 | new Camera3RequestDescriptor(camera_.get(), camera3Request); |
Paul Elder | a6de3f0 | 2021-01-21 18:59:24 +0900 | [diff] [blame] | 1822 | /* |
| 1823 | * \todo The Android request model is incremental, settings passed in |
| 1824 | * previous requests are to be effective until overridden explicitly in |
| 1825 | * a new request. Do we need to cache settings incrementally here, or is |
| 1826 | * it handled by the Android camera service ? |
| 1827 | */ |
| 1828 | if (camera3Request->settings) |
| 1829 | lastSettings_ = camera3Request->settings; |
| 1830 | else |
| 1831 | descriptor->settings_ = lastSettings_; |
Kieran Bingham | eac0542 | 2020-07-01 13:28:16 +0100 | [diff] [blame] | 1832 | |
Jacopo Mondi | bc64407 | 2021-01-28 09:58:06 +0100 | [diff] [blame] | 1833 | LOG(HAL, Debug) << "Queueing request " << descriptor->request_->cookie() |
Hirokazu Honda | 17a6e78 | 2021-03-24 16:07:55 +0900 | [diff] [blame] | 1834 | << " with " << descriptor->buffers_.size() << " streams"; |
| 1835 | for (unsigned int i = 0; i < descriptor->buffers_.size(); ++i) { |
| 1836 | const camera3_stream_buffer_t &camera3Buffer = descriptor->buffers_[i]; |
| 1837 | camera3_stream *camera3Stream = camera3Buffer.stream; |
Jacopo Mondi | 4b18b82 | 2021-01-21 11:10:08 +0100 | [diff] [blame] | 1838 | CameraStream *cameraStream = static_cast<CameraStream *>(camera3Stream->priv); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1839 | |
Jacopo Mondi | 35f726d | 2020-09-04 18:32:48 +0200 | [diff] [blame] | 1840 | std::stringstream ss; |
| 1841 | ss << i << " - (" << camera3Stream->width << "x" |
| 1842 | << camera3Stream->height << ")" |
| 1843 | << "[" << utils::hex(camera3Stream->format) << "] -> " |
| 1844 | << "(" << cameraStream->configuration().size.toString() << ")[" |
| 1845 | << cameraStream->configuration().pixelFormat.toString() << "]"; |
| 1846 | |
Jacopo Mondi | de8304b | 2020-09-04 17:45:39 +0200 | [diff] [blame] | 1847 | /* |
| 1848 | * Inspect the camera stream type, create buffers opportunely |
| 1849 | * and add them to the Request if required. |
| 1850 | */ |
| 1851 | FrameBuffer *buffer = nullptr; |
| 1852 | switch (cameraStream->type()) { |
| 1853 | case CameraStream::Type::Mapped: |
| 1854 | /* |
| 1855 | * Mapped streams don't need buffers added to the |
| 1856 | * Request. |
| 1857 | */ |
Jacopo Mondi | 35f726d | 2020-09-04 18:32:48 +0200 | [diff] [blame] | 1858 | LOG(HAL, Debug) << ss.str() << " (mapped)"; |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1859 | continue; |
| 1860 | |
Jacopo Mondi | de8304b | 2020-09-04 17:45:39 +0200 | [diff] [blame] | 1861 | case CameraStream::Type::Direct: |
| 1862 | /* |
| 1863 | * Create a libcamera buffer using the dmabuf |
| 1864 | * descriptors of the camera3Buffer for each stream and |
| 1865 | * associate it with the Camera3RequestDescriptor for |
| 1866 | * lifetime management only. |
| 1867 | */ |
Hirokazu Honda | 17a6e78 | 2021-03-24 16:07:55 +0900 | [diff] [blame] | 1868 | buffer = createFrameBuffer(*camera3Buffer.buffer); |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1869 | descriptor->frameBuffers_.emplace_back(buffer); |
Jacopo Mondi | 35f726d | 2020-09-04 18:32:48 +0200 | [diff] [blame] | 1870 | LOG(HAL, Debug) << ss.str() << " (direct)"; |
Jacopo Mondi | de8304b | 2020-09-04 17:45:39 +0200 | [diff] [blame] | 1871 | break; |
| 1872 | |
| 1873 | case CameraStream::Type::Internal: |
| 1874 | /* |
| 1875 | * Get the frame buffer from the CameraStream internal |
| 1876 | * buffer pool. |
| 1877 | * |
| 1878 | * The buffer has to be returned to the CameraStream |
| 1879 | * once it has been processed. |
| 1880 | */ |
| 1881 | buffer = cameraStream->getBuffer(); |
Jacopo Mondi | 35f726d | 2020-09-04 18:32:48 +0200 | [diff] [blame] | 1882 | LOG(HAL, Debug) << ss.str() << " (internal)"; |
Jacopo Mondi | de8304b | 2020-09-04 17:45:39 +0200 | [diff] [blame] | 1883 | break; |
| 1884 | } |
| 1885 | |
Kieran Bingham | 0cfdf73 | 2020-07-01 13:36:24 +0100 | [diff] [blame] | 1886 | if (!buffer) { |
| 1887 | LOG(HAL, Error) << "Failed to create buffer"; |
Kieran Bingham | 0cfdf73 | 2020-07-01 13:36:24 +0100 | [diff] [blame] | 1888 | delete descriptor; |
| 1889 | return -ENOMEM; |
| 1890 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1891 | |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1892 | descriptor->request_->addBuffer(cameraStream->stream(), buffer, |
Hirokazu Honda | 17a6e78 | 2021-03-24 16:07:55 +0900 | [diff] [blame] | 1893 | camera3Buffer.acquire_fence); |
Kieran Bingham | 0cfdf73 | 2020-07-01 13:36:24 +0100 | [diff] [blame] | 1894 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1895 | |
Jacopo Mondi | bb2b400 | 2021-01-04 16:20:05 +0100 | [diff] [blame] | 1896 | /* |
| 1897 | * Translate controls from Android to libcamera and queue the request |
| 1898 | * to the CameraWorker thread. |
| 1899 | */ |
| 1900 | int ret = processControls(descriptor); |
| 1901 | if (ret) |
| 1902 | return ret; |
| 1903 | |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1904 | worker_.queueRequest(descriptor->request_.get()); |
Paul Elder | c753223 | 2020-09-23 19:05:41 +0900 | [diff] [blame] | 1905 | |
Laurent Pinchart | da3f50e | 2020-01-20 01:09:34 +0200 | [diff] [blame] | 1906 | return 0; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1907 | } |
| 1908 | |
Niklas Söderlund | f7ddfd4 | 2019-10-21 20:01:19 +0200 | [diff] [blame] | 1909 | void CameraDevice::requestComplete(Request *request) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1910 | { |
Niklas Söderlund | dac8e95 | 2020-08-11 00:56:13 +0200 | [diff] [blame] | 1911 | const Request::BufferMap &buffers = request->buffers(); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1912 | camera3_buffer_status status = CAMERA3_BUFFER_STATUS_OK; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1913 | std::unique_ptr<CameraMetadata> resultMetadata; |
Kieran Bingham | c09aee4 | 2020-07-28 14:01:19 +0100 | [diff] [blame] | 1914 | Camera3RequestDescriptor *descriptor = |
| 1915 | reinterpret_cast<Camera3RequestDescriptor *>(request->cookie()); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1916 | |
| 1917 | if (request->status() != Request::RequestComplete) { |
Kieran Bingham | 98986e0 | 2020-08-03 14:34:11 +0100 | [diff] [blame] | 1918 | LOG(HAL, Error) << "Request not successfully completed: " |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1919 | << request->status(); |
| 1920 | status = CAMERA3_BUFFER_STATUS_ERROR; |
| 1921 | } |
| 1922 | |
Jacopo Mondi | bc64407 | 2021-01-28 09:58:06 +0100 | [diff] [blame] | 1923 | LOG(HAL, Debug) << "Request " << request->cookie() << " completed with " |
Hirokazu Honda | 17a6e78 | 2021-03-24 16:07:55 +0900 | [diff] [blame] | 1924 | << descriptor->buffers_.size() << " streams"; |
Jacopo Mondi | bc64407 | 2021-01-28 09:58:06 +0100 | [diff] [blame] | 1925 | |
Kieran Bingham | c09aee4 | 2020-07-28 14:01:19 +0100 | [diff] [blame] | 1926 | /* |
| 1927 | * \todo The timestamp used for the metadata is currently always taken |
| 1928 | * from the first buffer (which may be the first stream) in the Request. |
| 1929 | * It might be appropriate to return a 'correct' (as determined by |
| 1930 | * pipeline handlers) timestamp in the Request itself. |
| 1931 | */ |
Hirokazu Honda | 3a777d8 | 2020-10-28 17:57:26 +0900 | [diff] [blame] | 1932 | uint64_t timestamp = buffers.begin()->second->metadata().timestamp; |
Jacopo Mondi | 3535e0c | 2020-12-09 17:51:57 +0100 | [diff] [blame] | 1933 | resultMetadata = getResultMetadata(descriptor, timestamp); |
Kieran Bingham | c09aee4 | 2020-07-28 14:01:19 +0100 | [diff] [blame] | 1934 | |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1935 | /* Handle any JPEG compression. */ |
Hirokazu Honda | 17a6e78 | 2021-03-24 16:07:55 +0900 | [diff] [blame] | 1936 | for (camera3_stream_buffer_t &buffer : descriptor->buffers_) { |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1937 | CameraStream *cameraStream = |
Hirokazu Honda | 17a6e78 | 2021-03-24 16:07:55 +0900 | [diff] [blame] | 1938 | static_cast<CameraStream *>(buffer.stream->priv); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1939 | |
Jacopo Mondi | 160bb09 | 2020-10-02 20:48:35 +0200 | [diff] [blame] | 1940 | if (cameraStream->camera3Stream().format != HAL_PIXEL_FORMAT_BLOB) |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1941 | continue; |
| 1942 | |
Jacopo Mondi | d5473c9 | 2021-02-17 16:21:59 +0100 | [diff] [blame] | 1943 | FrameBuffer *src = request->findBuffer(cameraStream->stream()); |
| 1944 | if (!src) { |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1945 | LOG(HAL, Error) << "Failed to find a source stream buffer"; |
| 1946 | continue; |
| 1947 | } |
| 1948 | |
Jacopo Mondi | a725baf | 2021-02-24 12:50:40 +0100 | [diff] [blame] | 1949 | int ret = cameraStream->process(*src, |
Hirokazu Honda | 17a6e78 | 2021-03-24 16:07:55 +0900 | [diff] [blame] | 1950 | *buffer.buffer, |
Paul Elder | abfabdd | 2021-01-23 13:54:28 +0900 | [diff] [blame] | 1951 | descriptor->settings_, |
Jacopo Mondi | 9e95d5e | 2020-10-03 11:28:47 +0200 | [diff] [blame] | 1952 | resultMetadata.get()); |
| 1953 | if (ret) { |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1954 | status = CAMERA3_BUFFER_STATUS_ERROR; |
| 1955 | continue; |
| 1956 | } |
Jacopo Mondi | de8304b | 2020-09-04 17:45:39 +0200 | [diff] [blame] | 1957 | |
| 1958 | /* |
| 1959 | * Return the FrameBuffer to the CameraStream now that we're |
| 1960 | * done processing it. |
| 1961 | */ |
| 1962 | if (cameraStream->type() == CameraStream::Type::Internal) |
Jacopo Mondi | d5473c9 | 2021-02-17 16:21:59 +0100 | [diff] [blame] | 1963 | cameraStream->putBuffer(src); |
Kieran Bingham | 83ae84e | 2020-07-03 12:34:59 +0100 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | /* Prepare to call back the Android camera stack. */ |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1967 | camera3_capture_result_t captureResult = {}; |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1968 | captureResult.frame_number = descriptor->frameNumber_; |
Hirokazu Honda | 17a6e78 | 2021-03-24 16:07:55 +0900 | [diff] [blame] | 1969 | captureResult.num_output_buffers = descriptor->buffers_.size(); |
| 1970 | for (camera3_stream_buffer_t &buffer : descriptor->buffers_) { |
| 1971 | buffer.acquire_fence = -1; |
| 1972 | buffer.release_fence = -1; |
| 1973 | buffer.status = status; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1974 | } |
Hirokazu Honda | 17a6e78 | 2021-03-24 16:07:55 +0900 | [diff] [blame] | 1975 | captureResult.output_buffers = descriptor->buffers_.data(); |
Kieran Bingham | 0cfdf73 | 2020-07-01 13:36:24 +0100 | [diff] [blame] | 1976 | |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1977 | if (status == CAMERA3_BUFFER_STATUS_OK) { |
Kieran Bingham | e1f9fdb | 2020-10-21 15:31:10 +0100 | [diff] [blame] | 1978 | notifyShutter(descriptor->frameNumber_, timestamp); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1979 | |
| 1980 | captureResult.partial_result = 1; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 1981 | captureResult.result = resultMetadata->get(); |
| 1982 | } |
| 1983 | |
| 1984 | if (status == CAMERA3_BUFFER_STATUS_ERROR || !captureResult.result) { |
| 1985 | /* \todo Improve error handling. In case we notify an error |
| 1986 | * because the metadata generation fails, a shutter event has |
| 1987 | * already been notified for this frame number before the error |
| 1988 | * is here signalled. Make sure the error path plays well with |
| 1989 | * the camera stack state machine. |
| 1990 | */ |
Kieran Bingham | 37c18c2 | 2020-10-21 14:54:11 +0100 | [diff] [blame] | 1991 | notifyError(descriptor->frameNumber_, |
| 1992 | descriptor->buffers_[0].stream); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | callbacks_->process_capture_result(callbacks_, &captureResult); |
| 1996 | |
| 1997 | delete descriptor; |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 1998 | } |
| 1999 | |
Jacopo Mondi | a7b9277 | 2020-05-26 15:41:41 +0200 | [diff] [blame] | 2000 | std::string CameraDevice::logPrefix() const |
| 2001 | { |
Niklas Söderlund | 2e7c80a | 2020-08-02 23:57:17 +0200 | [diff] [blame] | 2002 | return "'" + camera_->id() + "'"; |
Jacopo Mondi | a7b9277 | 2020-05-26 15:41:41 +0200 | [diff] [blame] | 2003 | } |
| 2004 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 2005 | void CameraDevice::notifyShutter(uint32_t frameNumber, uint64_t timestamp) |
| 2006 | { |
| 2007 | camera3_notify_msg_t notify = {}; |
| 2008 | |
| 2009 | notify.type = CAMERA3_MSG_SHUTTER; |
| 2010 | notify.message.shutter.frame_number = frameNumber; |
| 2011 | notify.message.shutter.timestamp = timestamp; |
| 2012 | |
| 2013 | callbacks_->notify(callbacks_, ¬ify); |
| 2014 | } |
| 2015 | |
| 2016 | void CameraDevice::notifyError(uint32_t frameNumber, camera3_stream_t *stream) |
| 2017 | { |
| 2018 | camera3_notify_msg_t notify = {}; |
| 2019 | |
Kieran Bingham | 8ad1b9c | 2020-07-01 16:47:58 +0100 | [diff] [blame] | 2020 | /* |
| 2021 | * \todo Report and identify the stream number or configuration to |
| 2022 | * clarify the stream that failed. |
| 2023 | */ |
| 2024 | LOG(HAL, Error) << "Error occurred on frame " << frameNumber << " (" |
| 2025 | << toPixelFormat(stream->format).toString() << ")"; |
| 2026 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 2027 | notify.type = CAMERA3_MSG_ERROR; |
| 2028 | notify.message.error.error_stream = stream; |
| 2029 | notify.message.error.frame_number = frameNumber; |
| 2030 | notify.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST; |
| 2031 | |
| 2032 | callbacks_->notify(callbacks_, ¬ify); |
| 2033 | } |
| 2034 | |
| 2035 | /* |
| 2036 | * Produce a set of fixed result metadata. |
| 2037 | */ |
Laurent Pinchart | dbafe16 | 2019-10-27 00:36:13 +0300 | [diff] [blame] | 2038 | std::unique_ptr<CameraMetadata> |
Jacopo Mondi | 3535e0c | 2020-12-09 17:51:57 +0100 | [diff] [blame] | 2039 | CameraDevice::getResultMetadata(Camera3RequestDescriptor *descriptor, |
Laurent Pinchart | dbafe16 | 2019-10-27 00:36:13 +0300 | [diff] [blame] | 2040 | int64_t timestamp) |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 2041 | { |
Jacopo Mondi | 3535e0c | 2020-12-09 17:51:57 +0100 | [diff] [blame] | 2042 | const ControlList &metadata = descriptor->request_->metadata(); |
Jacopo Mondi | 94d42ce | 2021-01-29 14:54:39 +0100 | [diff] [blame] | 2043 | const CameraMetadata &settings = descriptor->settings_; |
Paul Elder | 958c80a | 2021-01-26 09:47:33 +0900 | [diff] [blame] | 2044 | camera_metadata_ro_entry_t entry; |
Jacopo Mondi | 94d42ce | 2021-01-29 14:54:39 +0100 | [diff] [blame] | 2045 | bool found; |
Jacopo Mondi | 3535e0c | 2020-12-09 17:51:57 +0100 | [diff] [blame] | 2046 | |
Jacopo Mondi | 48504ba | 2019-09-04 16:18:19 +0200 | [diff] [blame] | 2047 | /* |
| 2048 | * \todo Keep this in sync with the actual number of entries. |
Paul Elder | 1264628 | 2021-01-23 13:56:01 +0900 | [diff] [blame] | 2049 | * Currently: 40 entries, 156 bytes |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 2050 | * |
| 2051 | * Reserve more space for the JPEG metadata set by the post-processor. |
Paul Elder | abfabdd | 2021-01-23 13:54:28 +0900 | [diff] [blame] | 2052 | * Currently: |
| 2053 | * ANDROID_JPEG_GPS_COORDINATES (double x 3) = 24 bytes |
| 2054 | * ANDROID_JPEG_GPS_PROCESSING_METHOD (byte x 32) = 32 bytes |
| 2055 | * ANDROID_JPEG_GPS_TIMESTAMP (int64) = 8 bytes |
| 2056 | * ANDROID_JPEG_SIZE (int32_t) = 4 bytes |
| 2057 | * ANDROID_JPEG_QUALITY (byte) = 1 byte |
| 2058 | * ANDROID_JPEG_ORIENTATION (int32_t) = 4 bytes |
Paul Elder | 1264628 | 2021-01-23 13:56:01 +0900 | [diff] [blame] | 2059 | * ANDROID_JPEG_THUMBNAIL_QUALITY (byte) = 1 byte |
| 2060 | * ANDROID_JPEG_THUMBNAIL_SIZE (int32 x 2) = 8 bytes |
| 2061 | * Total bytes for JPEG metadata: 82 |
Jacopo Mondi | 48504ba | 2019-09-04 16:18:19 +0200 | [diff] [blame] | 2062 | */ |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 2063 | std::unique_ptr<CameraMetadata> resultMetadata = |
Jacopo Mondi | f29601e | 2021-02-03 16:47:30 +0100 | [diff] [blame] | 2064 | std::make_unique<CameraMetadata>(44, 166); |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 2065 | if (!resultMetadata->isValid()) { |
| 2066 | LOG(HAL, Error) << "Failed to allocate static metadata"; |
| 2067 | return nullptr; |
| 2068 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 2069 | |
Jacopo Mondi | 94d42ce | 2021-01-29 14:54:39 +0100 | [diff] [blame] | 2070 | /* |
| 2071 | * \todo The value of the results metadata copied from the settings |
| 2072 | * will have to be passed to the libcamera::Camera and extracted |
| 2073 | * from libcamera::Request::metadata. |
| 2074 | */ |
| 2075 | |
Jacopo Mondi | 520c366 | 2021-02-03 14:44:34 +0100 | [diff] [blame] | 2076 | uint8_t value = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF; |
| 2077 | resultMetadata->addEntry(ANDROID_COLOR_CORRECTION_ABERRATION_MODE, |
| 2078 | &value, 1); |
| 2079 | |
| 2080 | value = ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF; |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 2081 | resultMetadata->addEntry(ANDROID_CONTROL_AE_ANTIBANDING_MODE, &value, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 2082 | |
Jacopo Mondi | e9c2875 | 2021-02-03 14:47:45 +0100 | [diff] [blame] | 2083 | int32_t value32 = 0; |
| 2084 | resultMetadata->addEntry(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION, |
| 2085 | &value32, 1); |
| 2086 | |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 2087 | value = ANDROID_CONTROL_AE_LOCK_OFF; |
| 2088 | resultMetadata->addEntry(ANDROID_CONTROL_AE_LOCK, &value, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 2089 | |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 2090 | value = ANDROID_CONTROL_AE_MODE_ON; |
| 2091 | resultMetadata->addEntry(ANDROID_CONTROL_AE_MODE, &value, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 2092 | |
Jacopo Mondi | d150860 | 2021-01-21 18:21:28 +0100 | [diff] [blame] | 2093 | if (settings.getEntry(ANDROID_CONTROL_AE_TARGET_FPS_RANGE, &entry)) |
| 2094 | /* |
| 2095 | * \todo Retrieve the AE FPS range from the libcamera metadata. |
| 2096 | * As libcamera does not support that control, as a temporary |
| 2097 | * workaround return what the framework asked. |
| 2098 | */ |
| 2099 | resultMetadata->addEntry(ANDROID_CONTROL_AE_TARGET_FPS_RANGE, |
| 2100 | entry.data.i32, 2); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 2101 | |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 2102 | value = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE; |
Jacopo Mondi | 94d42ce | 2021-01-29 14:54:39 +0100 | [diff] [blame] | 2103 | found = settings.getEntry(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &entry); |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 2104 | resultMetadata->addEntry(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, |
Jacopo Mondi | 94d42ce | 2021-01-29 14:54:39 +0100 | [diff] [blame] | 2105 | found ? entry.data.u8 : &value, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 2106 | |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 2107 | value = ANDROID_CONTROL_AE_STATE_CONVERGED; |
| 2108 | resultMetadata->addEntry(ANDROID_CONTROL_AE_STATE, &value, 1); |
| 2109 | |
| 2110 | value = ANDROID_CONTROL_AF_MODE_OFF; |
| 2111 | resultMetadata->addEntry(ANDROID_CONTROL_AF_MODE, &value, 1); |
| 2112 | |
| 2113 | value = ANDROID_CONTROL_AF_STATE_INACTIVE; |
| 2114 | resultMetadata->addEntry(ANDROID_CONTROL_AF_STATE, &value, 1); |
| 2115 | |
| 2116 | value = ANDROID_CONTROL_AF_TRIGGER_IDLE; |
| 2117 | resultMetadata->addEntry(ANDROID_CONTROL_AF_TRIGGER, &value, 1); |
| 2118 | |
| 2119 | value = ANDROID_CONTROL_AWB_MODE_AUTO; |
| 2120 | resultMetadata->addEntry(ANDROID_CONTROL_AWB_MODE, &value, 1); |
| 2121 | |
| 2122 | value = ANDROID_CONTROL_AWB_LOCK_OFF; |
| 2123 | resultMetadata->addEntry(ANDROID_CONTROL_AWB_LOCK, &value, 1); |
| 2124 | |
| 2125 | value = ANDROID_CONTROL_AWB_STATE_CONVERGED; |
| 2126 | resultMetadata->addEntry(ANDROID_CONTROL_AWB_STATE, &value, 1); |
| 2127 | |
| 2128 | value = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW; |
| 2129 | resultMetadata->addEntry(ANDROID_CONTROL_CAPTURE_INTENT, &value, 1); |
| 2130 | |
| 2131 | value = ANDROID_CONTROL_EFFECT_MODE_OFF; |
| 2132 | resultMetadata->addEntry(ANDROID_CONTROL_EFFECT_MODE, &value, 1); |
| 2133 | |
| 2134 | value = ANDROID_CONTROL_MODE_AUTO; |
| 2135 | resultMetadata->addEntry(ANDROID_CONTROL_MODE, &value, 1); |
| 2136 | |
| 2137 | value = ANDROID_CONTROL_SCENE_MODE_DISABLED; |
| 2138 | resultMetadata->addEntry(ANDROID_CONTROL_SCENE_MODE, &value, 1); |
| 2139 | |
| 2140 | value = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF; |
| 2141 | resultMetadata->addEntry(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE, &value, 1); |
| 2142 | |
| 2143 | value = ANDROID_FLASH_MODE_OFF; |
| 2144 | resultMetadata->addEntry(ANDROID_FLASH_MODE, &value, 1); |
| 2145 | |
| 2146 | value = ANDROID_FLASH_STATE_UNAVAILABLE; |
| 2147 | resultMetadata->addEntry(ANDROID_FLASH_STATE, &value, 1); |
| 2148 | |
Jacopo Mondi | 94d42ce | 2021-01-29 14:54:39 +0100 | [diff] [blame] | 2149 | if (settings.getEntry(ANDROID_LENS_APERTURE, &entry)) |
Paul Elder | abfabdd | 2021-01-23 13:54:28 +0900 | [diff] [blame] | 2150 | resultMetadata->addEntry(ANDROID_LENS_APERTURE, entry.data.f, 1); |
| 2151 | |
| 2152 | float focal_length = 1.0; |
| 2153 | resultMetadata->addEntry(ANDROID_LENS_FOCAL_LENGTH, &focal_length, 1); |
| 2154 | |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 2155 | value = ANDROID_LENS_STATE_STATIONARY; |
| 2156 | resultMetadata->addEntry(ANDROID_LENS_STATE, &value, 1); |
| 2157 | |
| 2158 | value = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF; |
| 2159 | resultMetadata->addEntry(ANDROID_LENS_OPTICAL_STABILIZATION_MODE, |
| 2160 | &value, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 2161 | |
Jacopo Mondi | 5360d80 | 2021-02-03 16:43:22 +0100 | [diff] [blame] | 2162 | value32 = ANDROID_SENSOR_TEST_PATTERN_MODE_OFF; |
| 2163 | resultMetadata->addEntry(ANDROID_SENSOR_TEST_PATTERN_MODE, |
| 2164 | &value32, 1); |
| 2165 | |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 2166 | resultMetadata->addEntry(ANDROID_SENSOR_TIMESTAMP, ×tamp, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 2167 | |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 2168 | value = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF; |
| 2169 | resultMetadata->addEntry(ANDROID_STATISTICS_FACE_DETECT_MODE, |
| 2170 | &value, 1); |
| 2171 | |
| 2172 | value = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF; |
| 2173 | resultMetadata->addEntry(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE, |
| 2174 | &value, 1); |
| 2175 | |
Jacopo Mondi | f29601e | 2021-02-03 16:47:30 +0100 | [diff] [blame] | 2176 | value = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF; |
| 2177 | resultMetadata->addEntry(ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE, |
| 2178 | &value, 1); |
| 2179 | |
Jacopo Mondi | 3e979d7 | 2021-01-04 16:28:24 +0100 | [diff] [blame] | 2180 | value = ANDROID_STATISTICS_SCENE_FLICKER_NONE; |
| 2181 | resultMetadata->addEntry(ANDROID_STATISTICS_SCENE_FLICKER, |
| 2182 | &value, 1); |
| 2183 | |
| 2184 | value = ANDROID_NOISE_REDUCTION_MODE_OFF; |
| 2185 | resultMetadata->addEntry(ANDROID_NOISE_REDUCTION_MODE, &value, 1); |
| 2186 | |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 2187 | /* 33.3 msec */ |
| 2188 | const int64_t rolling_shutter_skew = 33300000; |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 2189 | resultMetadata->addEntry(ANDROID_SENSOR_ROLLING_SHUTTER_SKEW, |
| 2190 | &rolling_shutter_skew, 1); |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 2191 | |
Jacopo Mondi | 3535e0c | 2020-12-09 17:51:57 +0100 | [diff] [blame] | 2192 | /* Add metadata tags reported by libcamera. */ |
| 2193 | if (metadata.contains(controls::draft::PipelineDepth)) { |
| 2194 | uint8_t pipeline_depth = |
| 2195 | metadata.get<int32_t>(controls::draft::PipelineDepth); |
| 2196 | resultMetadata->addEntry(ANDROID_REQUEST_PIPELINE_DEPTH, |
| 2197 | &pipeline_depth, 1); |
| 2198 | } |
| 2199 | |
Jacopo Mondi | c9705a5 | 2021-01-05 19:30:48 +0100 | [diff] [blame] | 2200 | if (metadata.contains(controls::ExposureTime)) { |
Paul Elder | 78f49d5 | 2021-01-28 17:45:44 +0900 | [diff] [blame] | 2201 | int64_t exposure = metadata.get(controls::ExposureTime) * 1000ULL; |
Jacopo Mondi | c9705a5 | 2021-01-05 19:30:48 +0100 | [diff] [blame] | 2202 | resultMetadata->addEntry(ANDROID_SENSOR_EXPOSURE_TIME, |
| 2203 | &exposure, 1); |
| 2204 | } |
| 2205 | |
Jacopo Mondi | bb2b400 | 2021-01-04 16:20:05 +0100 | [diff] [blame] | 2206 | if (metadata.contains(controls::ScalerCrop)) { |
| 2207 | Rectangle crop = metadata.get(controls::ScalerCrop); |
| 2208 | int32_t cropRect[] = { |
| 2209 | crop.x, crop.y, static_cast<int32_t>(crop.width), |
| 2210 | static_cast<int32_t>(crop.height), |
| 2211 | }; |
| 2212 | resultMetadata->addEntry(ANDROID_SCALER_CROP_REGION, cropRect, 4); |
| 2213 | } |
| 2214 | |
Laurent Pinchart | 3986009 | 2019-09-05 03:12:34 +0300 | [diff] [blame] | 2215 | /* |
| 2216 | * Return the result metadata pack even is not valid: get() will return |
| 2217 | * nullptr. |
| 2218 | */ |
| 2219 | if (!resultMetadata->isValid()) { |
| 2220 | LOG(HAL, Error) << "Failed to construct result metadata"; |
| 2221 | } |
Jacopo Mondi | 667d8ea | 2019-05-10 17:40:02 +0200 | [diff] [blame] | 2222 | |
| 2223 | return resultMetadata; |
| 2224 | } |