blob: 74f6915c8d5fe7ba4383012a753c2f35b313ea54 [file] [log] [blame]
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001/* 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 Pinchartda3f50e2020-01-20 01:09:34 +02009#include "camera_ops.h"
Umang Jainb2b8c4d2020-10-16 11:07:54 +053010#include "post_processor.h"
Jacopo Mondi667d8ea2019-05-10 17:40:02 +020011
Jacopo Mondi45c6a7e2020-12-18 16:35:35 +010012#include <array>
Jacopo Mondiedd4b1d2021-01-04 16:18:29 +010013#include <cmath>
Paul Elder229653a2021-01-21 17:44:14 +090014#include <fstream>
Kieran Bingham83ae84e2020-07-03 12:34:59 +010015#include <sys/mman.h>
Jacopo Mondia80d3812020-05-26 12:31:35 +020016#include <tuple>
Jacopo Mondi117588b2020-05-23 18:53:54 +020017#include <vector>
18
Jacopo Mondi5d91c8d2020-09-23 09:41:52 +020019#include <libcamera/control_ids.h>
Jacopo Mondi857a2162019-11-20 17:00:49 +010020#include <libcamera/controls.h>
Laurent Pinchart8b7e0732020-05-22 04:02:06 +030021#include <libcamera/formats.h>
Jacopo Mondi857a2162019-11-20 17:00:49 +010022#include <libcamera/property_ids.h>
23
Niklas Söderlund7876d632020-07-21 00:16:24 +020024#include "libcamera/internal/formats.h"
Laurent Pinchart93e72b62020-05-12 00:58:34 +030025#include "libcamera/internal/log.h"
26#include "libcamera/internal/utils.h"
Jacopo Mondi667d8ea2019-05-10 17:40:02 +020027
Jacopo Mondi117588b2020-05-23 18:53:54 +020028#include "system/graphics.h"
Jacopo Mondi667d8ea2019-05-10 17:40:02 +020029
30using namespace libcamera;
31
Hirokazu Honda26d90af2020-12-11 09:53:36 +000032LOG_DECLARE_CATEGORY(HAL)
33
Jacopo Mondi117588b2020-05-23 18:53:54 +020034namespace {
35
36/*
37 * \var camera3Resolutions
38 * \brief The list of image resolutions defined as mandatory to be supported by
39 * the Android Camera3 specification
40 */
41const std::vector<Size> camera3Resolutions = {
42 { 320, 240 },
43 { 640, 480 },
44 { 1280, 720 },
45 { 1920, 1080 }
46};
47
48/*
49 * \struct Camera3Format
50 * \brief Data associated with an Android format identifier
51 * \var libcameraFormats List of libcamera pixel formats compatible with the
52 * Android format
Jacopo Mondi117588b2020-05-23 18:53:54 +020053 * \var name The human-readable representation of the Android format code
54 */
55struct Camera3Format {
56 std::vector<PixelFormat> libcameraFormats;
Niklas Söderlund8c1fedc2020-07-28 19:43:12 +020057 bool mandatory;
Jacopo Mondi117588b2020-05-23 18:53:54 +020058 const char *name;
59};
60
61/*
62 * \var camera3FormatsMap
63 * \brief Associate Android format code with ancillary data
64 */
65const std::map<int, const Camera3Format> camera3FormatsMap = {
66 {
67 HAL_PIXEL_FORMAT_BLOB, {
Laurent Pinchart8b7e0732020-05-22 04:02:06 +030068 { formats::MJPEG },
Niklas Söderlund8c1fedc2020-07-28 19:43:12 +020069 true,
Jacopo Mondi117588b2020-05-23 18:53:54 +020070 "BLOB"
71 }
72 }, {
73 HAL_PIXEL_FORMAT_YCbCr_420_888, {
Laurent Pinchart8b7e0732020-05-22 04:02:06 +030074 { formats::NV12, formats::NV21 },
Niklas Söderlund8c1fedc2020-07-28 19:43:12 +020075 true,
Jacopo Mondi117588b2020-05-23 18:53:54 +020076 "YCbCr_420_888"
77 }
78 }, {
79 /*
80 * \todo Translate IMPLEMENTATION_DEFINED inspecting the gralloc
81 * usage flag. For now, copy the YCbCr_420 configuration.
82 */
83 HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, {
Laurent Pinchart8b7e0732020-05-22 04:02:06 +030084 { formats::NV12, formats::NV21 },
Niklas Söderlund8c1fedc2020-07-28 19:43:12 +020085 true,
Jacopo Mondi117588b2020-05-23 18:53:54 +020086 "IMPLEMENTATION_DEFINED"
87 }
Niklas Söderlundd4de0372020-07-21 00:16:14 +020088 }, {
89 HAL_PIXEL_FORMAT_RAW10, {
90 {
91 formats::SBGGR10_CSI2P,
92 formats::SGBRG10_CSI2P,
93 formats::SGRBG10_CSI2P,
94 formats::SRGGB10_CSI2P
95 },
96 false,
97 "RAW10"
98 }
99 }, {
100 HAL_PIXEL_FORMAT_RAW12, {
101 {
102 formats::SBGGR12_CSI2P,
103 formats::SGBRG12_CSI2P,
104 formats::SGRBG12_CSI2P,
105 formats::SRGGB12_CSI2P
106 },
107 false,
108 "RAW12"
109 }
110 }, {
111 HAL_PIXEL_FORMAT_RAW16, {
112 {
113 formats::SBGGR16,
114 formats::SGBRG16,
115 formats::SGRBG16,
116 formats::SRGGB16
117 },
118 false,
119 "RAW16"
120 }
121 }, {
122 HAL_PIXEL_FORMAT_RAW_OPAQUE, {
123 {
124 formats::SBGGR10_IPU3,
125 formats::SGBRG10_IPU3,
126 formats::SGRBG10_IPU3,
127 formats::SRGGB10_IPU3
128 },
129 false,
130 "RAW_OPAQUE"
131 }
Jacopo Mondi117588b2020-05-23 18:53:54 +0200132 },
133};
134
Hirokazu Hondac2df7432020-12-11 09:53:34 +0000135/*
136 * \struct Camera3StreamConfig
137 * \brief Data to store StreamConfiguration associated with camera3_stream(s)
138 * \var streams List of the pairs of a stream requested by Android HAL client
139 * and CameraStream::Type associated with the stream
140 * \var config StreamConfiguration for streams
141 */
142struct Camera3StreamConfig {
143 struct Camera3Stream {
144 camera3_stream_t *stream;
145 CameraStream::Type type;
146 };
147
148 std::vector<Camera3Stream> streams;
149 StreamConfiguration config;
150};
151
Hirokazu Honda26d90af2020-12-11 09:53:36 +0000152/*
153 * Reorder the configurations so that libcamera::Camera can accept them as much
154 * as possible. The sort rule is as follows.
155 * 1.) The configuration for NV12 request whose resolution is the largest.
156 * 2.) The configuration for JPEG request.
157 * 3.) Others. Larger resolutions and different formats are put earlier.
158 */
159void sortCamera3StreamConfigs(std::vector<Camera3StreamConfig> &unsortedConfigs,
160 const camera3_stream_t *jpegStream)
161{
162 const Camera3StreamConfig *jpegConfig = nullptr;
Jacopo Mondi117588b2020-05-23 18:53:54 +0200163
Hirokazu Honda26d90af2020-12-11 09:53:36 +0000164 std::map<PixelFormat, std::vector<const Camera3StreamConfig *>> formatToConfigs;
165 for (const auto &streamConfig : unsortedConfigs) {
166 if (jpegStream && !jpegConfig) {
167 const auto &streams = streamConfig.streams;
168 if (std::find_if(streams.begin(), streams.end(),
169 [jpegStream](const auto &stream) {
170 return stream.stream == jpegStream;
171 }) != streams.end()) {
172 jpegConfig = &streamConfig;
173 continue;
174 }
175 }
176 formatToConfigs[streamConfig.config.pixelFormat].push_back(&streamConfig);
177 }
178
179 if (jpegStream && !jpegConfig)
180 LOG(HAL, Fatal) << "No Camera3StreamConfig is found for JPEG";
181
182 for (auto &fmt : formatToConfigs) {
183 auto &streamConfigs = fmt.second;
184
185 /* Sorted by resolution. Smaller is put first. */
186 std::sort(streamConfigs.begin(), streamConfigs.end(),
187 [](const auto *streamConfigA, const auto *streamConfigB) {
188 const Size &sizeA = streamConfigA->config.size;
189 const Size &sizeB = streamConfigB->config.size;
190 return sizeA < sizeB;
191 });
192 }
193
194 std::vector<Camera3StreamConfig> sortedConfigs;
195 sortedConfigs.reserve(unsortedConfigs.size());
196
197 /*
198 * NV12 is the most prioritized format. Put the configuration with NV12
199 * and the largest resolution first.
200 */
201 const auto nv12It = formatToConfigs.find(formats::NV12);
202 if (nv12It != formatToConfigs.end()) {
203 auto &nv12Configs = nv12It->second;
Laurent Pinchartbd4894d2020-12-12 05:22:31 +0200204 const Camera3StreamConfig *nv12Largest = nv12Configs.back();
Hirokazu Honda26d90af2020-12-11 09:53:36 +0000205
206 /*
207 * If JPEG will be created from NV12 and the size is larger than
208 * the largest NV12 configurations, then put the NV12
209 * configuration for JPEG first.
210 */
211 if (jpegConfig && jpegConfig->config.pixelFormat == formats::NV12) {
212 const Size &nv12SizeForJpeg = jpegConfig->config.size;
213 const Size &nv12LargestSize = nv12Largest->config.size;
214
215 if (nv12LargestSize < nv12SizeForJpeg) {
216 LOG(HAL, Debug) << "Insert " << jpegConfig->config.toString();
217 sortedConfigs.push_back(std::move(*jpegConfig));
218 jpegConfig = nullptr;
219 }
220 }
221
222 LOG(HAL, Debug) << "Insert " << nv12Largest->config.toString();
223 sortedConfigs.push_back(*nv12Largest);
224 nv12Configs.pop_back();
225
226 if (nv12Configs.empty())
227 formatToConfigs.erase(nv12It);
228 }
229
230 /* If the configuration for JPEG is there, then put it. */
231 if (jpegConfig) {
232 LOG(HAL, Debug) << "Insert " << jpegConfig->config.toString();
233 sortedConfigs.push_back(std::move(*jpegConfig));
234 jpegConfig = nullptr;
235 }
236
237 /*
238 * Put configurations with different formats and larger resolutions
239 * earlier.
240 */
241 while (!formatToConfigs.empty()) {
242 for (auto it = formatToConfigs.begin(); it != formatToConfigs.end();) {
243 auto &configs = it->second;
244 LOG(HAL, Debug) << "Insert " << configs.back()->config.toString();
245 sortedConfigs.push_back(*configs.back());
246 configs.pop_back();
247
248 if (configs.empty())
249 it = formatToConfigs.erase(it);
250 else
251 it++;
252 }
253 }
254
255 ASSERT(sortedConfigs.size() == unsortedConfigs.size());
256
257 unsortedConfigs = sortedConfigs;
258}
259
Hirokazu Honda7633a2d2021-04-03 22:37:40 +0900260bool isValidRequest(camera3_capture_request_t *camera3Request)
261{
262 if (!camera3Request) {
263 LOG(HAL, Error) << "No capture request provided";
264 return false;
265 }
266
Hirokazu Honda90a04302021-04-03 22:37:41 +0900267 if (!camera3Request->num_output_buffers ||
268 !camera3Request->output_buffers) {
Hirokazu Honda7633a2d2021-04-03 22:37:40 +0900269 LOG(HAL, Error) << "No output buffers provided";
270 return false;
271 }
272
Hirokazu Honda90a04302021-04-03 22:37:41 +0900273 for (uint32_t i = 0; i < camera3Request->num_output_buffers; i++) {
274 const camera3_stream_buffer_t &outputBuffer =
275 camera3Request->output_buffers[i];
276 if (!outputBuffer.buffer || !(*outputBuffer.buffer)) {
277 LOG(HAL, Error) << "Invalid native handle";
278 return false;
279 }
280
281 const native_handle_t *handle = *outputBuffer.buffer;
282 constexpr int kNativeHandleMaxFds = 1024;
283 if (handle->numFds < 0 || handle->numFds > kNativeHandleMaxFds) {
284 LOG(HAL, Error)
285 << "Invalid number of fds (" << handle->numFds
286 << ") in buffer " << i;
287 return false;
288 }
289
290 constexpr int kNativeHandleMaxInts = 1024;
291 if (handle->numInts < 0 || handle->numInts > kNativeHandleMaxInts) {
292 LOG(HAL, Error)
293 << "Invalid number of ints (" << handle->numInts
294 << ") in buffer " << i;
295 return false;
296 }
297 }
298
Hirokazu Honda7633a2d2021-04-03 22:37:40 +0900299 return true;
300}
301
Hirokazu Hondaac209ef2021-04-03 22:10:14 +0900302const char *rotationToString(int rotation)
303{
304 switch (rotation) {
305 case CAMERA3_STREAM_ROTATION_0:
306 return "0";
307 case CAMERA3_STREAM_ROTATION_90:
308 return "90";
309 case CAMERA3_STREAM_ROTATION_180:
310 return "180";
311 case CAMERA3_STREAM_ROTATION_270:
312 return "270";
313 }
314 return "INVALID";
315}
316
Hirokazu Honda4ae2a752021-04-03 22:10:13 +0900317#if defined(OS_CHROMEOS)
318/*
319 * Check whether the crop_rotate_scale_degrees values for all streams in
320 * the list are valid according to the Chrome OS camera HAL API.
321 */
322bool validateCropRotate(const camera3_stream_configuration_t &streamList)
323{
324 ASSERT(streamList.num_streams > 0);
325 const int cropRotateScaleDegrees =
326 streamList.streams[0]->crop_rotate_scale_degrees;
327 for (unsigned int i = 0; i < streamList.num_streams; ++i) {
328 const camera3_stream_t &stream = *streamList.streams[i];
329
330 switch (stream.crop_rotate_scale_degrees) {
331 case CAMERA3_STREAM_ROTATION_0:
332 case CAMERA3_STREAM_ROTATION_90:
333 case CAMERA3_STREAM_ROTATION_270:
334 break;
335
336 /* 180° rotation is specified by Chrome OS as invalid. */
337 case CAMERA3_STREAM_ROTATION_180:
338 default:
339 LOG(HAL, Error) << "Invalid crop_rotate_scale_degrees: "
340 << stream.crop_rotate_scale_degrees;
341 return false;
342 }
343
344 if (cropRotateScaleDegrees != stream.crop_rotate_scale_degrees) {
345 LOG(HAL, Error) << "crop_rotate_scale_degrees in all "
346 << "streams are not identical";
347 return false;
348 }
349 }
350
351 return true;
352}
353#endif
354
Hirokazu Honda26d90af2020-12-11 09:53:36 +0000355} /* namespace */
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200356
357/*
358 * \struct Camera3RequestDescriptor
359 *
360 * A utility structure that groups information about a capture request to be
361 * later re-used at request complete time to notify the framework.
362 */
363
364CameraDevice::Camera3RequestDescriptor::Camera3RequestDescriptor(
Jacopo Mondidd1cd532021-01-21 10:51:05 +0100365 Camera *camera, const camera3_capture_request_t *camera3Request)
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200366{
Jacopo Mondidd1cd532021-01-21 10:51:05 +0100367 frameNumber_ = camera3Request->frame_number;
368
Jacopo Mondi4b18b822021-01-21 11:10:08 +0100369 /* Copy the camera3 request stream information for later access. */
Hirokazu Honda17a6e782021-03-24 16:07:55 +0900370 const uint32_t numBuffers = camera3Request->num_output_buffers;
371 buffers_.resize(numBuffers);
372 for (uint32_t i = 0; i < numBuffers; i++)
Jacopo Mondi4b18b822021-01-21 11:10:08 +0100373 buffers_[i] = camera3Request->output_buffers[i];
Jacopo Mondi4b1aa212020-10-06 17:56:50 +0200374
375 /*
376 * FrameBuffer instances created by wrapping a camera3 provided dmabuf
377 * are emplaced in this vector of unique_ptr<> for lifetime management.
378 */
Hirokazu Honda17a6e782021-03-24 16:07:55 +0900379 frameBuffers_.reserve(numBuffers);
Jacopo Mondi4b1aa212020-10-06 17:56:50 +0200380
Jacopo Mondi9e6eece2021-01-21 12:52:10 +0100381 /* Clone the controls associated with the camera3 request. */
382 settings_ = CameraMetadata(camera3Request->settings);
383
Jacopo Mondi4b1aa212020-10-06 17:56:50 +0200384 /*
Hirokazu Hondad4043012021-04-03 22:57:34 +0900385 * Create the CaptureRequest, stored as a unique_ptr<> to tie its
386 * lifetime to the descriptor.
Jacopo Mondi4b1aa212020-10-06 17:56:50 +0200387 */
Hirokazu Hondad4043012021-04-03 22:57:34 +0900388 request_ = std::make_unique<CaptureRequest>(camera);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200389}
390
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200391/*
392 * \class CameraDevice
393 *
394 * The CameraDevice class wraps a libcamera::Camera instance, and implements
Laurent Pinchartda3f50e2020-01-20 01:09:34 +0200395 * the camera3_device_t interface, bridging calls received from the Android
396 * camera service to the CameraDevice.
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200397 *
Laurent Pinchartda3f50e2020-01-20 01:09:34 +0200398 * The class translates parameters and operations from the Camera HALv3 API to
399 * the libcamera API to provide static information for a Camera, create request
400 * templates for it, process capture requests and then deliver capture results
401 * back to the framework using the designated callbacks.
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200402 */
403
Hirokazu Honda9538ce42021-03-24 16:07:53 +0900404CameraDevice::CameraDevice(unsigned int id, std::shared_ptr<Camera> camera)
405 : id_(id), running_(false), camera_(std::move(camera)),
Hirokazu Hondadca709c2021-03-24 16:07:56 +0900406 facing_(CAMERA_FACING_FRONT), orientation_(0)
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200407{
408 camera_->requestCompleted.connect(this, &CameraDevice::requestComplete);
Kieran Bingham83ae84e2020-07-03 12:34:59 +0100409
Paul Elder229653a2021-01-21 17:44:14 +0900410 maker_ = "libcamera";
411 model_ = "cameraModel";
412
413 /* \todo Support getting properties on Android */
414 std::ifstream fstream("/var/cache/camera/camera.prop");
415 if (!fstream.is_open())
416 return;
417
418 std::string line;
419 while (std::getline(fstream, line)) {
420 std::string::size_type delimPos = line.find("=");
421 if (delimPos == std::string::npos)
422 continue;
423 std::string key = line.substr(0, delimPos);
424 std::string val = line.substr(delimPos + 1);
425
426 if (!key.compare("ro.product.model"))
427 model_ = val;
428 else if (!key.compare("ro.product.manufacturer"))
429 maker_ = val;
430 }
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200431}
432
Hirokazu Hondaf101cc62021-03-24 16:07:57 +0900433CameraDevice::~CameraDevice() = default;
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200434
Hirokazu Honda212f4102021-03-24 16:07:50 +0900435std::unique_ptr<CameraDevice> CameraDevice::create(unsigned int id,
Hirokazu Honda9538ce42021-03-24 16:07:53 +0900436 std::shared_ptr<Camera> cam)
Umang Jainf8e28132020-08-21 14:46:08 +0000437{
Hirokazu Honda9538ce42021-03-24 16:07:53 +0900438 return std::unique_ptr<CameraDevice>(
439 new CameraDevice(id, std::move(cam)));
Umang Jainf8e28132020-08-21 14:46:08 +0000440}
441
Jacopo Mondi64f4f662020-05-25 16:08:22 +0200442/*
443 * Initialize the camera static information.
444 * This method is called before the camera device is opened.
445 */
446int CameraDevice::initialize()
447{
448 /* Initialize orientation and facing side of the camera. */
449 const ControlList &properties = camera_->properties();
450
451 if (properties.contains(properties::Location)) {
452 int32_t location = properties.get(properties::Location);
453 switch (location) {
454 case properties::CameraLocationFront:
455 facing_ = CAMERA_FACING_FRONT;
456 break;
457 case properties::CameraLocationBack:
458 facing_ = CAMERA_FACING_BACK;
459 break;
460 case properties::CameraLocationExternal:
Jacopo Mondi5154e142021-03-10 14:00:50 +0100461 facing_ = CAMERA_FACING_EXTERNAL;
Jacopo Mondi64f4f662020-05-25 16:08:22 +0200462 break;
463 }
Jacopo Mondi5154e142021-03-10 14:00:50 +0100464 } else {
465 /*
466 * \todo Retrieve the camera location from configuration file
467 * if not available from the library.
468 */
469 facing_ = CAMERA_FACING_FRONT;
Jacopo Mondi64f4f662020-05-25 16:08:22 +0200470 }
471
472 /*
Umang Jaine9176552020-09-09 16:17:54 +0530473 * The Android orientation metadata specifies its rotation correction
474 * value in clockwise direction whereas libcamera specifies the
475 * rotation property in anticlockwise direction. Read the libcamera's
476 * rotation property (anticlockwise) and compute the corresponding
477 * value for clockwise direction as required by the Android orientation
478 * metadata.
Jacopo Mondi64f4f662020-05-25 16:08:22 +0200479 */
Umang Jaine9176552020-09-09 16:17:54 +0530480 if (properties.contains(properties::Rotation)) {
481 int rotation = properties.get(properties::Rotation);
482 orientation_ = (360 - rotation) % 360;
483 }
Jacopo Mondi64f4f662020-05-25 16:08:22 +0200484
Jacopo Mondi117588b2020-05-23 18:53:54 +0200485 int ret = camera_->acquire();
486 if (ret) {
487 LOG(HAL, Error) << "Failed to temporarily acquire the camera";
488 return ret;
489 }
490
491 ret = initializeStreamConfigurations();
492 camera_->release();
493 return ret;
494}
495
Jacopo Mondibfee6312020-09-01 17:42:13 +0200496std::vector<Size> CameraDevice::getYUVResolutions(CameraConfiguration *cameraConfig,
497 const PixelFormat &pixelFormat,
498 const std::vector<Size> &resolutions)
499{
500 std::vector<Size> supportedResolutions;
501
502 StreamConfiguration &cfg = cameraConfig->at(0);
503 for (const Size &res : resolutions) {
504 cfg.pixelFormat = pixelFormat;
505 cfg.size = res;
506
507 CameraConfiguration::Status status = cameraConfig->validate();
508 if (status != CameraConfiguration::Valid) {
509 LOG(HAL, Debug) << cfg.toString() << " not supported";
510 continue;
511 }
512
513 LOG(HAL, Debug) << cfg.toString() << " supported";
514
515 supportedResolutions.push_back(res);
516 }
517
518 return supportedResolutions;
519}
520
Jacopo Mondi49610332020-09-01 18:11:34 +0200521std::vector<Size> CameraDevice::getRawResolutions(const libcamera::PixelFormat &pixelFormat)
522{
523 std::unique_ptr<CameraConfiguration> cameraConfig =
Niklas Söderlunddbe8d272020-05-26 15:04:47 +0200524 camera_->generateConfiguration({ StreamRole::Raw });
Jacopo Mondi49610332020-09-01 18:11:34 +0200525 StreamConfiguration &cfg = cameraConfig->at(0);
526 const StreamFormats &formats = cfg.formats();
527 std::vector<Size> supportedResolutions = formats.sizes(pixelFormat);
528
529 return supportedResolutions;
530}
531
Jacopo Mondi117588b2020-05-23 18:53:54 +0200532/*
533 * Initialize the format conversion map to translate from Android format
534 * identifier to libcamera pixel formats and fill in the list of supported
535 * stream configurations to be reported to the Android camera framework through
536 * the static stream configuration metadata.
537 */
538int CameraDevice::initializeStreamConfigurations()
539{
540 /*
541 * Get the maximum output resolutions
542 * \todo Get this from the camera properties once defined
543 */
544 std::unique_ptr<CameraConfiguration> cameraConfig =
545 camera_->generateConfiguration({ StillCapture });
546 if (!cameraConfig) {
547 LOG(HAL, Error) << "Failed to get maximum resolution";
548 return -EINVAL;
549 }
550 StreamConfiguration &cfg = cameraConfig->at(0);
551
552 /*
553 * \todo JPEG - Adjust the maximum available resolution by taking the
554 * JPEG encoder requirements into account (alignment and aspect ratio).
555 */
556 const Size maxRes = cfg.size;
557 LOG(HAL, Debug) << "Maximum supported resolution: " << maxRes.toString();
558
559 /*
560 * Build the list of supported image resolutions.
561 *
562 * The resolutions listed in camera3Resolution are mandatory to be
563 * supported, up to the camera maximum resolution.
564 *
565 * Augment the list by adding resolutions calculated from the camera
566 * maximum one.
567 */
568 std::vector<Size> cameraResolutions;
569 std::copy_if(camera3Resolutions.begin(), camera3Resolutions.end(),
570 std::back_inserter(cameraResolutions),
571 [&](const Size &res) { return res < maxRes; });
572
573 /*
574 * The Camera3 specification suggests adding 1/2 and 1/4 of the maximum
575 * resolution.
576 */
577 for (unsigned int divider = 2;; divider <<= 1) {
578 Size derivedSize{
579 maxRes.width / divider,
580 maxRes.height / divider,
581 };
582
583 if (derivedSize.width < 320 ||
584 derivedSize.height < 240)
585 break;
586
587 cameraResolutions.push_back(derivedSize);
588 }
589 cameraResolutions.push_back(maxRes);
590
591 /* Remove duplicated entries from the list of supported resolutions. */
592 std::sort(cameraResolutions.begin(), cameraResolutions.end());
593 auto last = std::unique(cameraResolutions.begin(), cameraResolutions.end());
594 cameraResolutions.erase(last, cameraResolutions.end());
595
596 /*
597 * Build the list of supported camera formats.
598 *
599 * To each Android format a list of compatible libcamera formats is
600 * associated. The first libcamera format that tests successful is added
601 * to the format translation map used when configuring the streams.
602 * It is then tested against the list of supported camera resolutions to
603 * build the stream configuration map reported through the camera static
604 * metadata.
605 */
Jacopo Mondi756fcba2021-01-28 15:26:20 +0100606 Size maxJpegSize;
Jacopo Mondi117588b2020-05-23 18:53:54 +0200607 for (const auto &format : camera3FormatsMap) {
608 int androidFormat = format.first;
609 const Camera3Format &camera3Format = format.second;
610 const std::vector<PixelFormat> &libcameraFormats =
611 camera3Format.libcameraFormats;
612
Jacopo Mondiaf264ec2020-09-01 15:40:49 +0200613 LOG(HAL, Debug) << "Trying to map Android format "
614 << camera3Format.name;
615
Jacopo Mondi117588b2020-05-23 18:53:54 +0200616 /*
Jacopo Mondi843565c2020-09-01 15:34:13 +0200617 * JPEG is always supported, either produced directly by the
618 * camera, or encoded in the HAL.
619 */
620 if (androidFormat == HAL_PIXEL_FORMAT_BLOB) {
621 formatsMap_[androidFormat] = formats::MJPEG;
Jacopo Mondiaf264ec2020-09-01 15:40:49 +0200622 LOG(HAL, Debug) << "Mapped Android format "
623 << camera3Format.name << " to "
624 << formats::MJPEG.toString()
625 << " (fixed mapping)";
Jacopo Mondi843565c2020-09-01 15:34:13 +0200626 continue;
627 }
628
629 /*
Jacopo Mondi117588b2020-05-23 18:53:54 +0200630 * Test the libcamera formats that can produce images
631 * compatible with the format defined by Android.
632 */
633 PixelFormat mappedFormat;
634 for (const PixelFormat &pixelFormat : libcameraFormats) {
Jacopo Mondi117588b2020-05-23 18:53:54 +0200635
Jacopo Mondiaf264ec2020-09-01 15:40:49 +0200636 LOG(HAL, Debug) << "Testing " << pixelFormat.toString();
637
Jacopo Mondi117588b2020-05-23 18:53:54 +0200638 /*
639 * The stream configuration size can be adjusted,
640 * not the pixel format.
641 *
642 * \todo This could be simplified once all pipeline
643 * handlers will report the StreamFormats list of
644 * supported formats.
645 */
646 cfg.pixelFormat = pixelFormat;
647
648 CameraConfiguration::Status status = cameraConfig->validate();
649 if (status != CameraConfiguration::Invalid &&
650 cfg.pixelFormat == pixelFormat) {
651 mappedFormat = pixelFormat;
652 break;
653 }
654 }
Jacopo Mondi3533fd42020-09-01 15:31:56 +0200655
656 if (!mappedFormat.isValid()) {
657 /* If the format is not mandatory, skip it. */
658 if (!camera3Format.mandatory)
659 continue;
660
661 LOG(HAL, Error)
662 << "Failed to map mandatory Android format "
663 << camera3Format.name << " ("
664 << utils::hex(androidFormat) << "): aborting";
Jacopo Mondi117588b2020-05-23 18:53:54 +0200665 return -EINVAL;
666 }
667
668 /*
669 * Record the mapping and then proceed to generate the
670 * stream configurations map, by testing the image resolutions.
671 */
672 formatsMap_[androidFormat] = mappedFormat;
Jacopo Mondiaf264ec2020-09-01 15:40:49 +0200673 LOG(HAL, Debug) << "Mapped Android format "
674 << camera3Format.name << " to "
675 << mappedFormat.toString();
Jacopo Mondi117588b2020-05-23 18:53:54 +0200676
Jacopo Mondi49610332020-09-01 18:11:34 +0200677 std::vector<Size> resolutions;
678 const PixelFormatInfo &info = PixelFormatInfo::info(mappedFormat);
679 if (info.colourEncoding == PixelFormatInfo::ColourEncodingRAW)
680 resolutions = getRawResolutions(mappedFormat);
681 else
682 resolutions = getYUVResolutions(cameraConfig.get(),
683 mappedFormat,
684 cameraResolutions);
685
Jacopo Mondibfee6312020-09-01 17:42:13 +0200686 for (const Size &res : resolutions) {
Niklas Söderlund142a9ee2020-07-23 18:32:23 +0200687 streamConfigurations_.push_back({ res, androidFormat });
Jacopo Mondi843565c2020-09-01 15:34:13 +0200688
689 /*
690 * If the format is HAL_PIXEL_FORMAT_YCbCr_420_888
691 * from which JPEG is produced, add an entry for
692 * the JPEG stream.
693 *
694 * \todo Wire the JPEG encoder to query the supported
695 * sizes provided a list of formats it can encode.
696 *
697 * \todo Support JPEG streams produced by the Camera
698 * natively.
699 */
Jacopo Mondi756fcba2021-01-28 15:26:20 +0100700 if (androidFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Jacopo Mondi843565c2020-09-01 15:34:13 +0200701 streamConfigurations_.push_back(
702 { res, HAL_PIXEL_FORMAT_BLOB });
Jacopo Mondi756fcba2021-01-28 15:26:20 +0100703 maxJpegSize = std::max(maxJpegSize, res);
704 }
Jacopo Mondi117588b2020-05-23 18:53:54 +0200705 }
Jacopo Mondi756fcba2021-01-28 15:26:20 +0100706
707 /*
708 * \todo Calculate the maximum JPEG buffer size by asking the
709 * encoder giving the maximum frame size required.
710 */
711 maxJpegBufferSize_ = maxJpegSize.width * maxJpegSize.height * 1.5;
Jacopo Mondi117588b2020-05-23 18:53:54 +0200712 }
713
714 LOG(HAL, Debug) << "Collected stream configuration map: ";
715 for (const auto &entry : streamConfigurations_)
716 LOG(HAL, Debug) << "{ " << entry.resolution.toString() << " - "
Niklas Söderlund142a9ee2020-07-23 18:32:23 +0200717 << utils::hex(entry.androidFormat) << " }";
Jacopo Mondi117588b2020-05-23 18:53:54 +0200718
Jacopo Mondi64f4f662020-05-25 16:08:22 +0200719 return 0;
720}
721
722/*
723 * Open a camera device. The static information on the camera shall have been
724 * initialized with a call to CameraDevice::initialize().
725 */
Laurent Pinchartda3f50e2020-01-20 01:09:34 +0200726int CameraDevice::open(const hw_module_t *hardwareModule)
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200727{
728 int ret = camera_->acquire();
729 if (ret) {
730 LOG(HAL, Error) << "Failed to acquire the camera";
731 return ret;
732 }
733
Laurent Pinchartda3f50e2020-01-20 01:09:34 +0200734 /* Initialize the hw_device_t in the instance camera3_module_t. */
735 camera3Device_.common.tag = HARDWARE_DEVICE_TAG;
736 camera3Device_.common.version = CAMERA_DEVICE_API_VERSION_3_3;
737 camera3Device_.common.module = (hw_module_t *)hardwareModule;
738 camera3Device_.common.close = hal_dev_close;
739
740 /*
741 * The camera device operations. These actually implement
742 * the Android Camera HALv3 interface.
743 */
744 camera3Device_.ops = &hal_dev_ops;
745 camera3Device_.priv = this;
746
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200747 return 0;
748}
749
750void CameraDevice::close()
751{
Jacopo Mondi6c4999e2020-10-03 16:06:34 +0200752 streams_.clear();
753
Hirokazu Honda0b661d72021-04-02 11:12:37 +0900754 stop();
755
756 camera_->release();
757}
758
759void CameraDevice::stop()
760{
761 if (!running_)
762 return;
763
Jacopo Mondi4b1aa212020-10-06 17:56:50 +0200764 worker_.stop();
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200765 camera_->stop();
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200766
Hirokazu Hondad4043012021-04-03 22:57:34 +0900767 descriptors_.clear();
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200768 running_ = false;
769}
770
771void CameraDevice::setCallbacks(const camera3_callback_ops_t *callbacks)
772{
773 callbacks_ = callbacks;
774}
775
776/*
777 * Return static information for the camera.
778 */
Laurent Pinchartda3f50e2020-01-20 01:09:34 +0200779const camera_metadata_t *CameraDevice::getStaticMetadata()
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200780{
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200781 if (staticMetadata_)
Laurent Pinchart39860092019-09-05 03:12:34 +0300782 return staticMetadata_->get();
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200783
Paul Elder1ff68872021-04-30 17:10:05 +0900784 staticMetadata_ = std::make_unique<CameraMetadata>(64, 1024);
Laurent Pinchart39860092019-09-05 03:12:34 +0300785 if (!staticMetadata_->isValid()) {
786 LOG(HAL, Error) << "Failed to allocate static metadata";
Hirokazu Hondadca709c2021-03-24 16:07:56 +0900787 staticMetadata_.reset();
Laurent Pinchart39860092019-09-05 03:12:34 +0300788 return nullptr;
789 }
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200790
Jacopo Mondi5d91c8d2020-09-23 09:41:52 +0200791 const ControlInfoMap &controlsInfo = camera_->controls();
Jacopo Mondi1889cdc2020-11-06 16:08:16 +0100792 const ControlList &properties = camera_->properties();
Jacopo Mondi5d91c8d2020-09-23 09:41:52 +0200793
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200794 /* Color correction static metadata. */
Jacopo Mondi63336862020-10-08 16:18:26 +0200795 {
Jacopo Mondic268e4f2020-12-01 15:42:15 +0100796 std::vector<uint8_t> data;
797 data.reserve(3);
Jacopo Mondi63336862020-10-08 16:18:26 +0200798 const auto &infoMap = controlsInfo.find(&controls::draft::ColorCorrectionAberrationMode);
799 if (infoMap != controlsInfo.end()) {
800 for (const auto &value : infoMap->second.values())
801 data.push_back(value.get<int32_t>());
802 } else {
803 data.push_back(ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF);
804 }
805 staticMetadata_->addEntry(ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
806 data.data(), data.size());
807 }
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200808
809 /* Control static metadata. */
810 std::vector<uint8_t> aeAvailableAntiBandingModes = {
811 ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF,
812 ANDROID_CONTROL_AE_ANTIBANDING_MODE_50HZ,
813 ANDROID_CONTROL_AE_ANTIBANDING_MODE_60HZ,
814 ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO,
815 };
Laurent Pinchart39860092019-09-05 03:12:34 +0300816 staticMetadata_->addEntry(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
817 aeAvailableAntiBandingModes.data(),
818 aeAvailableAntiBandingModes.size());
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200819
820 std::vector<uint8_t> aeAvailableModes = {
821 ANDROID_CONTROL_AE_MODE_ON,
822 };
Laurent Pinchart39860092019-09-05 03:12:34 +0300823 staticMetadata_->addEntry(ANDROID_CONTROL_AE_AVAILABLE_MODES,
824 aeAvailableModes.data(),
825 aeAvailableModes.size());
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200826
Jacopo Mondiedd4b1d2021-01-04 16:18:29 +0100827 int64_t minFrameDurationNsec = -1;
828 int64_t maxFrameDurationNsec = -1;
829 const auto frameDurationsInfo = controlsInfo.find(&controls::FrameDurations);
830 if (frameDurationsInfo != controlsInfo.end()) {
831 minFrameDurationNsec = frameDurationsInfo->second.min().get<int64_t>() * 1000;
832 maxFrameDurationNsec = frameDurationsInfo->second.max().get<int64_t>() * 1000;
833
834 /*
835 * Adjust the minimum frame duration to comply with Android
836 * requirements. The camera service mandates all preview/record
837 * streams to have a minimum frame duration < 33,366 milliseconds
838 * (see MAX_PREVIEW_RECORD_DURATION_NS in the camera service
839 * implementation).
840 *
841 * If we're close enough (+ 500 useconds) to that value, round
842 * the minimum frame duration of the camera to an accepted
843 * value.
844 */
845 static constexpr int64_t MAX_PREVIEW_RECORD_DURATION_NS = 1e9 / 29.97;
846 if (minFrameDurationNsec > MAX_PREVIEW_RECORD_DURATION_NS &&
847 minFrameDurationNsec < MAX_PREVIEW_RECORD_DURATION_NS + 500000)
848 minFrameDurationNsec = MAX_PREVIEW_RECORD_DURATION_NS - 1000;
849
850 /*
851 * The AE routine frame rate limits are computed using the frame
852 * duration limits, as libcamera clips the AE routine to the
853 * frame durations.
854 */
855 int32_t maxFps = std::round(1e9 / minFrameDurationNsec);
856 int32_t minFps = std::round(1e9 / maxFrameDurationNsec);
857 minFps = std::max(1, minFps);
858
859 /*
860 * Register to the camera service {min, max} and {max, max}
861 * intervals as requested by the metadata documentation.
862 */
863 int32_t availableAeFpsTarget[] = {
864 minFps, maxFps, maxFps, maxFps
865 };
866 staticMetadata_->addEntry(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
867 availableAeFpsTarget, 4);
868 }
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200869
870 std::vector<int32_t> aeCompensationRange = {
871 0, 0,
872 };
Laurent Pinchart39860092019-09-05 03:12:34 +0300873 staticMetadata_->addEntry(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
874 aeCompensationRange.data(),
875 aeCompensationRange.size());
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200876
877 const camera_metadata_rational_t aeCompensationStep[] = {
878 { 0, 1 }
879 };
Laurent Pinchart39860092019-09-05 03:12:34 +0300880 staticMetadata_->addEntry(ANDROID_CONTROL_AE_COMPENSATION_STEP,
881 aeCompensationStep, 1);
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200882
883 std::vector<uint8_t> availableAfModes = {
884 ANDROID_CONTROL_AF_MODE_OFF,
885 };
Laurent Pinchart39860092019-09-05 03:12:34 +0300886 staticMetadata_->addEntry(ANDROID_CONTROL_AF_AVAILABLE_MODES,
887 availableAfModes.data(),
888 availableAfModes.size());
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200889
890 std::vector<uint8_t> availableEffects = {
891 ANDROID_CONTROL_EFFECT_MODE_OFF,
892 };
Laurent Pinchart39860092019-09-05 03:12:34 +0300893 staticMetadata_->addEntry(ANDROID_CONTROL_AVAILABLE_EFFECTS,
894 availableEffects.data(),
895 availableEffects.size());
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200896
897 std::vector<uint8_t> availableSceneModes = {
898 ANDROID_CONTROL_SCENE_MODE_DISABLED,
899 };
Laurent Pinchart39860092019-09-05 03:12:34 +0300900 staticMetadata_->addEntry(ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
901 availableSceneModes.data(),
902 availableSceneModes.size());
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200903
904 std::vector<uint8_t> availableStabilizationModes = {
905 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF,
906 };
Laurent Pinchart39860092019-09-05 03:12:34 +0300907 staticMetadata_->addEntry(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
908 availableStabilizationModes.data(),
909 availableStabilizationModes.size());
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200910
Jacopo Mondiaf088b82021-01-04 18:26:28 +0100911 /*
912 * \todo Inspect the Camera capabilities to report the available
913 * AWB modes. Default to AUTO as CTS tests require it.
914 */
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200915 std::vector<uint8_t> availableAwbModes = {
Jacopo Mondiaf088b82021-01-04 18:26:28 +0100916 ANDROID_CONTROL_AWB_MODE_AUTO,
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200917 };
Laurent Pinchart39860092019-09-05 03:12:34 +0300918 staticMetadata_->addEntry(ANDROID_CONTROL_AWB_AVAILABLE_MODES,
919 availableAwbModes.data(),
920 availableAwbModes.size());
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200921
922 std::vector<int32_t> availableMaxRegions = {
923 0, 0, 0,
924 };
Laurent Pinchart39860092019-09-05 03:12:34 +0300925 staticMetadata_->addEntry(ANDROID_CONTROL_MAX_REGIONS,
926 availableMaxRegions.data(),
927 availableMaxRegions.size());
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200928
929 std::vector<uint8_t> sceneModesOverride = {
930 ANDROID_CONTROL_AE_MODE_ON,
931 ANDROID_CONTROL_AWB_MODE_AUTO,
Jacopo Mondif266c0e2021-02-03 16:34:40 +0100932 ANDROID_CONTROL_AF_MODE_OFF,
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200933 };
Laurent Pinchart39860092019-09-05 03:12:34 +0300934 staticMetadata_->addEntry(ANDROID_CONTROL_SCENE_MODE_OVERRIDES,
935 sceneModesOverride.data(),
936 sceneModesOverride.size());
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200937
938 uint8_t aeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE;
Laurent Pinchart39860092019-09-05 03:12:34 +0300939 staticMetadata_->addEntry(ANDROID_CONTROL_AE_LOCK_AVAILABLE,
940 &aeLockAvailable, 1);
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200941
942 uint8_t awbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE;
Laurent Pinchart39860092019-09-05 03:12:34 +0300943 staticMetadata_->addEntry(ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
944 &awbLockAvailable, 1);
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200945
946 char availableControlModes = ANDROID_CONTROL_MODE_AUTO;
Laurent Pinchart39860092019-09-05 03:12:34 +0300947 staticMetadata_->addEntry(ANDROID_CONTROL_AVAILABLE_MODES,
948 &availableControlModes, 1);
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200949
950 /* JPEG static metadata. */
Jacopo Mondi9e807052021-02-04 15:32:00 +0100951
952 /*
953 * Create the list of supported thumbnail sizes by inspecting the
954 * available JPEG resolutions collected in streamConfigurations_ and
955 * generate one entry for each aspect ratio.
956 *
957 * The JPEG thumbnailer can freely scale, so pick an arbitrary
958 * (160, 160) size as the bounding rectangle, which is then cropped to
959 * the different supported aspect ratios.
960 */
961 constexpr Size maxJpegThumbnail(160, 160);
962 std::vector<Size> thumbnailSizes;
963 thumbnailSizes.push_back({ 0, 0 });
964 for (const auto &entry : streamConfigurations_) {
965 if (entry.androidFormat != HAL_PIXEL_FORMAT_BLOB)
966 continue;
967
968 Size thumbnailSize = maxJpegThumbnail
969 .boundedToAspectRatio({ entry.resolution.width,
970 entry.resolution.height });
971 thumbnailSizes.push_back(thumbnailSize);
972 }
973
974 std::sort(thumbnailSizes.begin(), thumbnailSizes.end());
975 auto last = std::unique(thumbnailSizes.begin(), thumbnailSizes.end());
976 thumbnailSizes.erase(last, thumbnailSizes.end());
977
978 /* Transform sizes in to a list of integers that can be consumed. */
979 std::vector<int32_t> thumbnailEntries;
980 thumbnailEntries.reserve(thumbnailSizes.size() * 2);
981 for (const auto &size : thumbnailSizes) {
982 thumbnailEntries.push_back(size.width);
983 thumbnailEntries.push_back(size.height);
984 }
Laurent Pinchart39860092019-09-05 03:12:34 +0300985 staticMetadata_->addEntry(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
Jacopo Mondi9e807052021-02-04 15:32:00 +0100986 thumbnailEntries.data(), thumbnailEntries.size());
Jacopo Mondib4893fc2019-09-04 16:18:18 +0200987
Kieran Bingham83ae84e2020-07-03 12:34:59 +0100988 staticMetadata_->addEntry(ANDROID_JPEG_MAX_SIZE, &maxJpegBufferSize_, 1);
989
Jacopo Mondi667d8ea2019-05-10 17:40:02 +0200990 /* Sensor static metadata. */
Jacopo Mondi45c6a7e2020-12-18 16:35:35 +0100991 std::array<int32_t, 2> pixelArraySize;
Jacopo Mondicda41ff2020-12-30 17:18:51 +0100992 {
Jacopo Mondi45c6a7e2020-12-18 16:35:35 +0100993 const Size &size = properties.get(properties::PixelArraySize);
994 pixelArraySize[0] = size.width;
995 pixelArraySize[1] = size.height;
Jacopo Mondi1889cdc2020-11-06 16:08:16 +0100996 staticMetadata_->addEntry(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
Jacopo Mondi45c6a7e2020-12-18 16:35:35 +0100997 pixelArraySize.data(), pixelArraySize.size());
998 }
999
1000 if (properties.contains(properties::UnitCellSize)) {
1001 const Size &cellSize = properties.get<Size>(properties::UnitCellSize);
1002 std::array<float, 2> physicalSize{
1003 cellSize.width * pixelArraySize[0] / 1e6f,
1004 cellSize.height * pixelArraySize[1] / 1e6f
1005 };
1006 staticMetadata_->addEntry(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
1007 physicalSize.data(), physicalSize.size());
Jacopo Mondi1889cdc2020-11-06 16:08:16 +01001008 }
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001009
Jacopo Mondicda41ff2020-12-30 17:18:51 +01001010 {
Jacopo Mondi1889cdc2020-11-06 16:08:16 +01001011 const Span<const Rectangle> &rects =
Jacopo Mondi0ed05322020-12-23 18:34:34 +01001012 properties.get(properties::PixelArrayActiveAreas);
Jacopo Mondi1889cdc2020-11-06 16:08:16 +01001013 std::vector<int32_t> data{
1014 static_cast<int32_t>(rects[0].x),
1015 static_cast<int32_t>(rects[0].y),
1016 static_cast<int32_t>(rects[0].width),
1017 static_cast<int32_t>(rects[0].height),
1018 };
1019 staticMetadata_->addEntry(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
1020 data.data(), data.size());
1021 }
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001022
1023 int32_t sensitivityRange[] = {
1024 32, 2400,
1025 };
Laurent Pinchart39860092019-09-05 03:12:34 +03001026 staticMetadata_->addEntry(ANDROID_SENSOR_INFO_SENSITIVITY_RANGE,
1027 &sensitivityRange, 2);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001028
Jacopo Mondicb35ed22020-12-18 16:28:43 +01001029 /* Report the color filter arrangement if the camera reports it. */
1030 if (properties.contains(properties::draft::ColorFilterArrangement)) {
1031 uint8_t filterArr = properties.get(properties::draft::ColorFilterArrangement);
1032 staticMetadata_->addEntry(ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT,
1033 &filterArr, 1);
1034 }
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001035
Jacopo Mondi753d7532021-01-02 12:34:06 +01001036 const auto &exposureInfo = controlsInfo.find(&controls::ExposureTime);
1037 if (exposureInfo != controlsInfo.end()) {
1038 int64_t exposureTimeRange[2] = {
1039 exposureInfo->second.min().get<int32_t>() * 1000LL,
1040 exposureInfo->second.max().get<int32_t>() * 1000LL,
1041 };
1042 staticMetadata_->addEntry(ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE,
1043 &exposureTimeRange, 2);
1044 }
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001045
Jacopo Mondi64f4f662020-05-25 16:08:22 +02001046 staticMetadata_->addEntry(ANDROID_SENSOR_ORIENTATION, &orientation_, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001047
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001048 std::vector<int32_t> testPatterModes = {
1049 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF,
1050 };
Laurent Pinchart39860092019-09-05 03:12:34 +03001051 staticMetadata_->addEntry(ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES,
1052 testPatterModes.data(),
1053 testPatterModes.size());
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001054
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001055 uint8_t timestampSource = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN;
Laurent Pinchart39860092019-09-05 03:12:34 +03001056 staticMetadata_->addEntry(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
1057 &timestampSource, 1);
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001058
Jacopo Mondiedd4b1d2021-01-04 16:18:29 +01001059 if (maxFrameDurationNsec > 0)
1060 staticMetadata_->addEntry(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
1061 &maxFrameDurationNsec, 1);
1062
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001063 /* Statistics static metadata. */
1064 uint8_t faceDetectMode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
Laurent Pinchart39860092019-09-05 03:12:34 +03001065 staticMetadata_->addEntry(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
1066 &faceDetectMode, 1);
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001067
1068 int32_t maxFaceCount = 0;
Laurent Pinchart39860092019-09-05 03:12:34 +03001069 staticMetadata_->addEntry(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
1070 &maxFaceCount, 1);
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001071
Jacopo Mondi2c618502020-10-08 16:47:36 +02001072 {
Jacopo Mondic268e4f2020-12-01 15:42:15 +01001073 std::vector<uint8_t> data;
1074 data.reserve(2);
Jacopo Mondi2c618502020-10-08 16:47:36 +02001075 const auto &infoMap = controlsInfo.find(&controls::draft::LensShadingMapMode);
1076 if (infoMap != controlsInfo.end()) {
1077 for (const auto &value : infoMap->second.values())
1078 data.push_back(value.get<int32_t>());
1079 } else {
1080 data.push_back(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF);
1081 }
1082 staticMetadata_->addEntry(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
1083 data.data(), data.size());
1084 }
1085
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001086 /* Sync static metadata. */
1087 int32_t maxLatency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN;
Laurent Pinchart39860092019-09-05 03:12:34 +03001088 staticMetadata_->addEntry(ANDROID_SYNC_MAX_LATENCY, &maxLatency, 1);
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001089
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001090 /* Flash static metadata. */
1091 char flashAvailable = ANDROID_FLASH_INFO_AVAILABLE_FALSE;
Laurent Pinchart39860092019-09-05 03:12:34 +03001092 staticMetadata_->addEntry(ANDROID_FLASH_INFO_AVAILABLE,
1093 &flashAvailable, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001094
1095 /* Lens static metadata. */
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001096 std::vector<float> lensApertures = {
1097 2.53 / 100,
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001098 };
Laurent Pinchart39860092019-09-05 03:12:34 +03001099 staticMetadata_->addEntry(ANDROID_LENS_INFO_AVAILABLE_APERTURES,
1100 lensApertures.data(),
1101 lensApertures.size());
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001102
Jacopo Mondi64f4f662020-05-25 16:08:22 +02001103 uint8_t lensFacing;
1104 switch (facing_) {
1105 default:
1106 case CAMERA_FACING_FRONT:
1107 lensFacing = ANDROID_LENS_FACING_FRONT;
1108 break;
1109 case CAMERA_FACING_BACK:
1110 lensFacing = ANDROID_LENS_FACING_BACK;
1111 break;
1112 case CAMERA_FACING_EXTERNAL:
1113 lensFacing = ANDROID_LENS_FACING_EXTERNAL;
1114 break;
Jacopo Mondi857a2162019-11-20 17:00:49 +01001115 }
Laurent Pinchart39860092019-09-05 03:12:34 +03001116 staticMetadata_->addEntry(ANDROID_LENS_FACING, &lensFacing, 1);
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001117
1118 std::vector<float> lensFocalLenghts = {
1119 1,
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001120 };
Laurent Pinchart39860092019-09-05 03:12:34 +03001121 staticMetadata_->addEntry(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
1122 lensFocalLenghts.data(),
1123 lensFocalLenghts.size());
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001124
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001125 std::vector<uint8_t> opticalStabilizations = {
1126 ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF,
1127 };
Laurent Pinchart39860092019-09-05 03:12:34 +03001128 staticMetadata_->addEntry(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
1129 opticalStabilizations.data(),
1130 opticalStabilizations.size());
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001131
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001132 float hypeFocalDistance = 0;
Laurent Pinchart39860092019-09-05 03:12:34 +03001133 staticMetadata_->addEntry(ANDROID_LENS_INFO_HYPERFOCAL_DISTANCE,
1134 &hypeFocalDistance, 1);
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001135
1136 float minFocusDistance = 0;
Laurent Pinchart39860092019-09-05 03:12:34 +03001137 staticMetadata_->addEntry(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
1138 &minFocusDistance, 1);
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001139
1140 /* Noise reduction modes. */
Jacopo Mondi0b5fecc2020-10-08 16:01:35 +02001141 {
Jacopo Mondic268e4f2020-12-01 15:42:15 +01001142 std::vector<uint8_t> data;
1143 data.reserve(5);
Jacopo Mondi0b5fecc2020-10-08 16:01:35 +02001144 const auto &infoMap = controlsInfo.find(&controls::draft::NoiseReductionMode);
1145 if (infoMap != controlsInfo.end()) {
1146 for (const auto &value : infoMap->second.values())
1147 data.push_back(value.get<int32_t>());
1148 } else {
1149 data.push_back(ANDROID_NOISE_REDUCTION_MODE_OFF);
1150 }
1151 staticMetadata_->addEntry(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
1152 data.data(), data.size());
1153 }
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001154
1155 /* Scaler static metadata. */
Phi-Bang Nguyen8634c382021-03-29 19:40:46 +02001156
1157 /*
1158 * \todo The digital zoom factor is a property that depends on the
1159 * desired output configuration and the sensor frame size input to the
1160 * ISP. This information is not available to the Android HAL, not at
1161 * initialization time at least.
1162 *
1163 * As a workaround rely on pipeline handlers initializing the
1164 * ScalerCrop control with the camera default configuration and use the
1165 * maximum and minimum crop rectangles to calculate the digital zoom
1166 * factor.
1167 */
1168 float maxZoom = 1.0f;
1169 const auto scalerCrop = controlsInfo.find(&controls::ScalerCrop);
1170 if (scalerCrop != controlsInfo.end()) {
1171 Rectangle min = scalerCrop->second.min().get<Rectangle>();
1172 Rectangle max = scalerCrop->second.max().get<Rectangle>();
1173 maxZoom = std::min(1.0f * max.width / min.width,
1174 1.0f * max.height / min.height);
Jacopo Mondi31a1a622021-01-03 19:57:36 +01001175 }
Phi-Bang Nguyen8634c382021-03-29 19:40:46 +02001176 staticMetadata_->addEntry(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
1177 &maxZoom, 1);
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001178
Jacopo Mondibde7b982020-05-25 17:18:28 +02001179 std::vector<uint32_t> availableStreamConfigurations;
1180 availableStreamConfigurations.reserve(streamConfigurations_.size() * 4);
1181 for (const auto &entry : streamConfigurations_) {
Niklas Söderlund142a9ee2020-07-23 18:32:23 +02001182 availableStreamConfigurations.push_back(entry.androidFormat);
Jacopo Mondibde7b982020-05-25 17:18:28 +02001183 availableStreamConfigurations.push_back(entry.resolution.width);
1184 availableStreamConfigurations.push_back(entry.resolution.height);
1185 availableStreamConfigurations.push_back(
1186 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
1187 }
Laurent Pinchart39860092019-09-05 03:12:34 +03001188 staticMetadata_->addEntry(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
1189 availableStreamConfigurations.data(),
1190 availableStreamConfigurations.size());
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001191
1192 std::vector<int64_t> availableStallDurations = {
1193 ANDROID_SCALER_AVAILABLE_FORMATS_BLOB, 2560, 1920, 33333333,
1194 };
Laurent Pinchart39860092019-09-05 03:12:34 +03001195 staticMetadata_->addEntry(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
1196 availableStallDurations.data(),
1197 availableStallDurations.size());
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001198
Jacopo Mondiedd4b1d2021-01-04 16:18:29 +01001199 /* Use the minimum frame duration for all the YUV/RGB formats. */
1200 if (minFrameDurationNsec > 0) {
1201 std::vector<int64_t> minFrameDurations;
1202 minFrameDurations.reserve(streamConfigurations_.size() * 4);
1203 for (const auto &entry : streamConfigurations_) {
1204 minFrameDurations.push_back(entry.androidFormat);
1205 minFrameDurations.push_back(entry.resolution.width);
1206 minFrameDurations.push_back(entry.resolution.height);
1207 minFrameDurations.push_back(minFrameDurationNsec);
1208 }
1209 staticMetadata_->addEntry(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
1210 minFrameDurations.data(),
1211 minFrameDurations.size());
Jacopo Mondibde7b982020-05-25 17:18:28 +02001212 }
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001213
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001214 uint8_t croppingType = ANDROID_SCALER_CROPPING_TYPE_CENTER_ONLY;
Laurent Pinchart39860092019-09-05 03:12:34 +03001215 staticMetadata_->addEntry(ANDROID_SCALER_CROPPING_TYPE, &croppingType, 1);
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001216
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001217 /* Info static metadata. */
1218 uint8_t supportedHWLevel = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED;
Laurent Pinchart39860092019-09-05 03:12:34 +03001219 staticMetadata_->addEntry(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
1220 &supportedHWLevel, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001221
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001222 /* Request static metadata. */
1223 int32_t partialResultCount = 1;
Laurent Pinchart39860092019-09-05 03:12:34 +03001224 staticMetadata_->addEntry(ANDROID_REQUEST_PARTIAL_RESULT_COUNT,
1225 &partialResultCount, 1);
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001226
Jacopo Mondi5d91c8d2020-09-23 09:41:52 +02001227 {
1228 /* Default the value to 2 if not reported by the camera. */
1229 uint8_t maxPipelineDepth = 2;
1230 const auto &infoMap = controlsInfo.find(&controls::draft::PipelineDepth);
1231 if (infoMap != controlsInfo.end())
1232 maxPipelineDepth = infoMap->second.max().get<int32_t>();
1233 staticMetadata_->addEntry(ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
1234 &maxPipelineDepth, 1);
1235 }
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001236
Niklas Söderlunda2a6f952020-07-21 00:16:02 +02001237 /* LIMITED does not support reprocessing. */
1238 uint32_t maxNumInputStreams = 0;
1239 staticMetadata_->addEntry(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS,
1240 &maxNumInputStreams, 1);
1241
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001242 std::vector<uint8_t> availableCapabilities = {
1243 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE,
1244 };
Niklas Söderlund7876d632020-07-21 00:16:24 +02001245
1246 /* Report if camera supports RAW. */
Jacopo Mondieec6c542020-12-09 18:16:11 +01001247 bool rawStreamAvailable = false;
Niklas Söderlund7876d632020-07-21 00:16:24 +02001248 std::unique_ptr<CameraConfiguration> cameraConfig =
Niklas Söderlunddbe8d272020-05-26 15:04:47 +02001249 camera_->generateConfiguration({ StreamRole::Raw });
Niklas Söderlund7876d632020-07-21 00:16:24 +02001250 if (cameraConfig && !cameraConfig->empty()) {
1251 const PixelFormatInfo &info =
1252 PixelFormatInfo::info(cameraConfig->at(0).pixelFormat);
Niklas Söderlund35de31e2020-12-31 00:11:59 +01001253 /* Only advertise RAW support if RAW16 is possible. */
1254 if (info.colourEncoding == PixelFormatInfo::ColourEncodingRAW &&
1255 info.bitsPerPixel == 16) {
Jacopo Mondieec6c542020-12-09 18:16:11 +01001256 rawStreamAvailable = true;
Niklas Söderlund7876d632020-07-21 00:16:24 +02001257 availableCapabilities.push_back(ANDROID_REQUEST_AVAILABLE_CAPABILITIES_RAW);
Jacopo Mondieec6c542020-12-09 18:16:11 +01001258 }
Niklas Söderlund7876d632020-07-21 00:16:24 +02001259 }
1260
Jacopo Mondieec6c542020-12-09 18:16:11 +01001261 /* Number of { RAW, YUV, JPEG } supported output streams */
1262 int32_t numOutStreams[] = { rawStreamAvailable, 2, 1 };
1263 staticMetadata_->addEntry(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
1264 &numOutStreams, 3);
1265
Laurent Pinchart39860092019-09-05 03:12:34 +03001266 staticMetadata_->addEntry(ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
1267 availableCapabilities.data(),
1268 availableCapabilities.size());
Jacopo Mondib4893fc2019-09-04 16:18:18 +02001269
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001270 std::vector<int32_t> availableCharacteristicsKeys = {
1271 ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
1272 ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
1273 ANDROID_CONTROL_AE_AVAILABLE_MODES,
1274 ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
1275 ANDROID_CONTROL_AE_COMPENSATION_RANGE,
1276 ANDROID_CONTROL_AE_COMPENSATION_STEP,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001277 ANDROID_CONTROL_AE_LOCK_AVAILABLE,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001278 ANDROID_CONTROL_AF_AVAILABLE_MODES,
1279 ANDROID_CONTROL_AVAILABLE_EFFECTS,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001280 ANDROID_CONTROL_AVAILABLE_MODES,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001281 ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
1282 ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
1283 ANDROID_CONTROL_AWB_AVAILABLE_MODES,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001284 ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001285 ANDROID_CONTROL_MAX_REGIONS,
1286 ANDROID_CONTROL_SCENE_MODE_OVERRIDES,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001287 ANDROID_FLASH_INFO_AVAILABLE,
1288 ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001289 ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001290 ANDROID_JPEG_MAX_SIZE,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001291 ANDROID_LENS_FACING,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001292 ANDROID_LENS_INFO_AVAILABLE_APERTURES,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001293 ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
1294 ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
1295 ANDROID_LENS_INFO_HYPERFOCAL_DISTANCE,
1296 ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
1297 ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001298 ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
1299 ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS,
1300 ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001301 ANDROID_REQUEST_PARTIAL_RESULT_COUNT,
1302 ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001303 ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
1304 ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
1305 ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
1306 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
1307 ANDROID_SCALER_CROPPING_TYPE,
1308 ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES,
1309 ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
1310 ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT,
1311 ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE,
Jacopo Mondiedd4b1d2021-01-04 16:18:29 +01001312 ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001313 ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
1314 ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
1315 ANDROID_SENSOR_INFO_SENSITIVITY_RANGE,
1316 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
1317 ANDROID_SENSOR_ORIENTATION,
1318 ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
1319 ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
1320 ANDROID_SYNC_MAX_LATENCY,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001321 };
1322 staticMetadata_->addEntry(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
1323 availableCharacteristicsKeys.data(),
1324 availableCharacteristicsKeys.size());
1325
1326 std::vector<int32_t> availableRequestKeys = {
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001327 ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
1328 ANDROID_CONTROL_AE_ANTIBANDING_MODE,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001329 ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001330 ANDROID_CONTROL_AE_LOCK,
1331 ANDROID_CONTROL_AE_MODE,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001332 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
Jacopo Mondi09b1d0f2020-07-24 16:10:23 +02001333 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
Jacopo Mondida4b3252021-01-29 14:21:56 +01001334 ANDROID_CONTROL_AF_MODE,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001335 ANDROID_CONTROL_AF_TRIGGER,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001336 ANDROID_CONTROL_AWB_LOCK,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001337 ANDROID_CONTROL_AWB_MODE,
1338 ANDROID_CONTROL_CAPTURE_INTENT,
Jacopo Mondida4b3252021-01-29 14:21:56 +01001339 ANDROID_CONTROL_EFFECT_MODE,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001340 ANDROID_CONTROL_MODE,
Jacopo Mondida4b3252021-01-29 14:21:56 +01001341 ANDROID_CONTROL_SCENE_MODE,
1342 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001343 ANDROID_FLASH_MODE,
Jacopo Mondida4b3252021-01-29 14:21:56 +01001344 ANDROID_JPEG_ORIENTATION,
1345 ANDROID_JPEG_QUALITY,
1346 ANDROID_JPEG_THUMBNAIL_QUALITY,
1347 ANDROID_JPEG_THUMBNAIL_SIZE,
Jacopo Mondi09b1d0f2020-07-24 16:10:23 +02001348 ANDROID_LENS_APERTURE,
1349 ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001350 ANDROID_NOISE_REDUCTION_MODE,
Jacopo Mondida4b3252021-01-29 14:21:56 +01001351 ANDROID_SCALER_CROP_REGION,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001352 ANDROID_STATISTICS_FACE_DETECT_MODE
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001353 };
1354 staticMetadata_->addEntry(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
1355 availableRequestKeys.data(),
1356 availableRequestKeys.size());
1357
1358 std::vector<int32_t> availableResultKeys = {
Jacopo Mondi520c3662021-02-03 14:44:34 +01001359 ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
Jacopo Mondi3e979d72021-01-04 16:28:24 +01001360 ANDROID_CONTROL_AE_ANTIBANDING_MODE,
Jacopo Mondie9c28752021-02-03 14:47:45 +01001361 ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001362 ANDROID_CONTROL_AE_LOCK,
Jacopo Mondi3e979d72021-01-04 16:28:24 +01001363 ANDROID_CONTROL_AE_MODE,
Jacopo Mondi3e979d72021-01-04 16:28:24 +01001364 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1365 ANDROID_CONTROL_AE_STATE,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001366 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
Jacopo Mondi3e979d72021-01-04 16:28:24 +01001367 ANDROID_CONTROL_AF_MODE,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001368 ANDROID_CONTROL_AF_STATE,
Jacopo Mondi3e979d72021-01-04 16:28:24 +01001369 ANDROID_CONTROL_AF_TRIGGER,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001370 ANDROID_CONTROL_AWB_LOCK,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001371 ANDROID_CONTROL_AWB_MODE,
Jacopo Mondi3e979d72021-01-04 16:28:24 +01001372 ANDROID_CONTROL_AWB_STATE,
1373 ANDROID_CONTROL_CAPTURE_INTENT,
1374 ANDROID_CONTROL_EFFECT_MODE,
1375 ANDROID_CONTROL_MODE,
1376 ANDROID_CONTROL_SCENE_MODE,
1377 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
1378 ANDROID_FLASH_MODE,
1379 ANDROID_FLASH_STATE,
Paul Elderabfabdd2021-01-23 13:54:28 +09001380 ANDROID_JPEG_GPS_COORDINATES,
1381 ANDROID_JPEG_GPS_PROCESSING_METHOD,
1382 ANDROID_JPEG_GPS_TIMESTAMP,
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001383 ANDROID_JPEG_ORIENTATION,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001384 ANDROID_JPEG_QUALITY,
1385 ANDROID_JPEG_SIZE,
Paul Elder12646282021-01-23 13:56:01 +09001386 ANDROID_JPEG_THUMBNAIL_QUALITY,
1387 ANDROID_JPEG_THUMBNAIL_SIZE,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001388 ANDROID_LENS_APERTURE,
1389 ANDROID_LENS_FOCAL_LENGTH,
1390 ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
1391 ANDROID_LENS_STATE,
1392 ANDROID_NOISE_REDUCTION_MODE,
1393 ANDROID_REQUEST_PIPELINE_DEPTH,
1394 ANDROID_SCALER_CROP_REGION,
1395 ANDROID_SENSOR_EXPOSURE_TIME,
1396 ANDROID_SENSOR_ROLLING_SHUTTER_SKEW,
Jacopo Mondi5360d802021-02-03 16:43:22 +01001397 ANDROID_SENSOR_TEST_PATTERN_MODE,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001398 ANDROID_SENSOR_TIMESTAMP,
1399 ANDROID_STATISTICS_FACE_DETECT_MODE,
1400 ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
Jacopo Mondif29601e2021-02-03 16:47:30 +01001401 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
Jacopo Mondi6741ab82021-01-29 14:15:17 +01001402 ANDROID_STATISTICS_SCENE_FLICKER,
Jacopo Mondi19f85f42019-09-04 16:18:25 +02001403 };
1404 staticMetadata_->addEntry(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
1405 availableResultKeys.data(),
1406 availableResultKeys.size());
1407
Laurent Pinchart39860092019-09-05 03:12:34 +03001408 if (!staticMetadata_->isValid()) {
1409 LOG(HAL, Error) << "Failed to construct static metadata";
Hirokazu Hondadca709c2021-03-24 16:07:56 +09001410 staticMetadata_.reset();
Laurent Pinchart39860092019-09-05 03:12:34 +03001411 return nullptr;
1412 }
1413
1414 return staticMetadata_->get();
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001415}
1416
Hirokazu Hondaf101cc62021-03-24 16:07:57 +09001417std::unique_ptr<CameraMetadata> CameraDevice::requestTemplatePreview()
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001418{
Jacopo Mondi63703472019-09-04 16:18:22 +02001419 /*
1420 * \todo Keep this in sync with the actual number of entries.
Jacopo Mondi09b1d0f2020-07-24 16:10:23 +02001421 * Currently: 20 entries, 35 bytes
Jacopo Mondi63703472019-09-04 16:18:22 +02001422 */
Hirokazu Hondaf101cc62021-03-24 16:07:57 +09001423 auto requestTemplate = std::make_unique<CameraMetadata>(21, 36);
Jacopo Mondifcd5a4f2019-09-04 16:18:23 +02001424 if (!requestTemplate->isValid()) {
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001425 return nullptr;
1426 }
1427
Jacopo Mondif1796162021-03-08 17:03:14 +01001428 /* Get the FPS range registered in the static metadata. */
1429 camera_metadata_ro_entry_t entry;
1430 bool found = staticMetadata_->getEntry(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
1431 &entry);
1432 if (!found) {
1433 LOG(HAL, Error) << "Cannot create capture template without FPS range";
1434 return nullptr;
1435 }
1436
1437 /*
Jacopo Mondieb5a9d82021-03-08 17:00:39 +01001438 * Assume the AE_AVAILABLE_TARGET_FPS_RANGE static metadata
Jacopo Mondif1796162021-03-08 17:03:14 +01001439 * has been assembled as {{min, max} {max, max}}.
1440 */
1441 requestTemplate->addEntry(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
1442 entry.data.i32, 2);
1443
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001444 uint8_t aeMode = ANDROID_CONTROL_AE_MODE_ON;
Jacopo Mondifcd5a4f2019-09-04 16:18:23 +02001445 requestTemplate->addEntry(ANDROID_CONTROL_AE_MODE,
1446 &aeMode, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001447
1448 int32_t aeExposureCompensation = 0;
Jacopo Mondifcd5a4f2019-09-04 16:18:23 +02001449 requestTemplate->addEntry(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
1450 &aeExposureCompensation, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001451
1452 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
Jacopo Mondifcd5a4f2019-09-04 16:18:23 +02001453 requestTemplate->addEntry(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1454 &aePrecaptureTrigger, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001455
1456 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
Jacopo Mondifcd5a4f2019-09-04 16:18:23 +02001457 requestTemplate->addEntry(ANDROID_CONTROL_AE_LOCK,
1458 &aeLock, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001459
Jacopo Mondi09b1d0f2020-07-24 16:10:23 +02001460 uint8_t aeAntibandingMode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
1461 requestTemplate->addEntry(ANDROID_CONTROL_AE_ANTIBANDING_MODE,
1462 &aeAntibandingMode, 1);
1463
Jacopo Mondi9690d082021-02-03 16:37:20 +01001464 uint8_t afMode = ANDROID_CONTROL_AF_MODE_OFF;
1465 requestTemplate->addEntry(ANDROID_CONTROL_AF_MODE, &afMode, 1);
1466
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001467 uint8_t afTrigger = ANDROID_CONTROL_AF_TRIGGER_IDLE;
Jacopo Mondifcd5a4f2019-09-04 16:18:23 +02001468 requestTemplate->addEntry(ANDROID_CONTROL_AF_TRIGGER,
1469 &afTrigger, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001470
1471 uint8_t awbMode = ANDROID_CONTROL_AWB_MODE_AUTO;
Jacopo Mondifcd5a4f2019-09-04 16:18:23 +02001472 requestTemplate->addEntry(ANDROID_CONTROL_AWB_MODE,
1473 &awbMode, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001474
1475 uint8_t awbLock = ANDROID_CONTROL_AWB_LOCK_OFF;
Jacopo Mondifcd5a4f2019-09-04 16:18:23 +02001476 requestTemplate->addEntry(ANDROID_CONTROL_AWB_LOCK,
1477 &awbLock, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001478
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001479 uint8_t flashMode = ANDROID_FLASH_MODE_OFF;
Jacopo Mondifcd5a4f2019-09-04 16:18:23 +02001480 requestTemplate->addEntry(ANDROID_FLASH_MODE,
1481 &flashMode, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001482
1483 uint8_t faceDetectMode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
Jacopo Mondifcd5a4f2019-09-04 16:18:23 +02001484 requestTemplate->addEntry(ANDROID_STATISTICS_FACE_DETECT_MODE,
1485 &faceDetectMode, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001486
Jacopo Mondi9b361dc2019-09-04 16:18:21 +02001487 uint8_t noiseReduction = ANDROID_NOISE_REDUCTION_MODE_OFF;
Jacopo Mondifcd5a4f2019-09-04 16:18:23 +02001488 requestTemplate->addEntry(ANDROID_NOISE_REDUCTION_MODE,
1489 &noiseReduction, 1);
Jacopo Mondi9b361dc2019-09-04 16:18:21 +02001490
1491 uint8_t aberrationMode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF;
Jacopo Mondifcd5a4f2019-09-04 16:18:23 +02001492 requestTemplate->addEntry(ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
1493 &aberrationMode, 1);
Jacopo Mondi9b361dc2019-09-04 16:18:21 +02001494
Jacopo Mondi09b1d0f2020-07-24 16:10:23 +02001495 uint8_t controlMode = ANDROID_CONTROL_MODE_AUTO;
1496 requestTemplate->addEntry(ANDROID_CONTROL_MODE, &controlMode, 1);
1497
1498 float lensAperture = 2.53 / 100;
1499 requestTemplate->addEntry(ANDROID_LENS_APERTURE, &lensAperture, 1);
1500
1501 uint8_t opticalStabilization = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
1502 requestTemplate->addEntry(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
1503 &opticalStabilization, 1);
1504
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001505 uint8_t captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
Jacopo Mondifcd5a4f2019-09-04 16:18:23 +02001506 requestTemplate->addEntry(ANDROID_CONTROL_CAPTURE_INTENT,
1507 &captureIntent, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001508
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001509 return requestTemplate;
1510}
1511
Hirokazu Hondaf101cc62021-03-24 16:07:57 +09001512std::unique_ptr<CameraMetadata> CameraDevice::requestTemplateVideo()
Jacopo Mondieb5a9d82021-03-08 17:00:39 +01001513{
Hirokazu Hondaf101cc62021-03-24 16:07:57 +09001514 std::unique_ptr<CameraMetadata> previewTemplate = requestTemplatePreview();
Jacopo Mondieb5a9d82021-03-08 17:00:39 +01001515 if (!previewTemplate)
1516 return nullptr;
1517
1518 /*
1519 * The video template requires a fixed FPS range. Everything else
1520 * stays the same as the preview template.
1521 */
1522 camera_metadata_ro_entry_t entry;
1523 staticMetadata_->getEntry(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
1524 &entry);
1525
1526 /*
1527 * Assume the AE_AVAILABLE_TARGET_FPS_RANGE static metadata
1528 * has been assembled as {{min, max} {max, max}}.
1529 */
1530 previewTemplate->updateEntry(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
1531 entry.data.i32 + 2, 2);
1532
1533 return previewTemplate;
1534}
1535
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001536/*
1537 * Produce a metadata pack to be used as template for a capture request.
1538 */
1539const camera_metadata_t *CameraDevice::constructDefaultRequestSettings(int type)
1540{
1541 auto it = requestTemplates_.find(type);
1542 if (it != requestTemplates_.end())
1543 return it->second->get();
1544
1545 /* Use the capture intent matching the requested template type. */
Hirokazu Hondaf101cc62021-03-24 16:07:57 +09001546 std::unique_ptr<CameraMetadata> requestTemplate;
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001547 uint8_t captureIntent;
1548 switch (type) {
1549 case CAMERA3_TEMPLATE_PREVIEW:
1550 captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
Jacopo Mondieb5a9d82021-03-08 17:00:39 +01001551 requestTemplate = requestTemplatePreview();
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001552 break;
1553 case CAMERA3_TEMPLATE_STILL_CAPTURE:
Jacopo Mondieb5a9d82021-03-08 17:00:39 +01001554 /*
1555 * Use the preview template for still capture, they only differ
1556 * for the torch mode we currently do not support.
1557 */
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001558 captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
Jacopo Mondieb5a9d82021-03-08 17:00:39 +01001559 requestTemplate = requestTemplatePreview();
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001560 break;
1561 case CAMERA3_TEMPLATE_VIDEO_RECORD:
1562 captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
Jacopo Mondieb5a9d82021-03-08 17:00:39 +01001563 requestTemplate = requestTemplateVideo();
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001564 break;
1565 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
1566 captureIntent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
Jacopo Mondieb5a9d82021-03-08 17:00:39 +01001567 requestTemplate = requestTemplateVideo();
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001568 break;
Jacopo Mondi2c349912021-03-08 16:13:58 +01001569 /* \todo Implement templates generation for the remaining use cases. */
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001570 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001571 case CAMERA3_TEMPLATE_MANUAL:
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001572 default:
Jacopo Mondi2c349912021-03-08 16:13:58 +01001573 LOG(HAL, Error) << "Unsupported template request type: " << type;
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001574 return nullptr;
1575 }
1576
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001577 if (!requestTemplate || !requestTemplate->isValid()) {
Laurent Pinchart39860092019-09-05 03:12:34 +03001578 LOG(HAL, Error) << "Failed to construct request template";
Laurent Pinchart39860092019-09-05 03:12:34 +03001579 return nullptr;
1580 }
1581
Jacopo Mondi8a02d442020-07-24 15:47:50 +02001582 requestTemplate->updateEntry(ANDROID_CONTROL_CAPTURE_INTENT,
1583 &captureIntent, 1);
1584
Hirokazu Hondaf101cc62021-03-24 16:07:57 +09001585 requestTemplates_[type] = std::move(requestTemplate);
1586 return requestTemplates_[type]->get();
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001587}
1588
Hirokazu Hondac1ae9052020-10-28 18:50:23 +09001589PixelFormat CameraDevice::toPixelFormat(int format) const
Kieran Bingham43e3b802020-06-26 09:58:49 +01001590{
1591 /* Translate Android format code to libcamera pixel format. */
1592 auto it = formatsMap_.find(format);
1593 if (it == formatsMap_.end()) {
1594 LOG(HAL, Error) << "Requested format " << utils::hex(format)
1595 << " not supported";
1596 return PixelFormat();
1597 }
1598
1599 return it->second;
1600}
1601
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001602/*
1603 * Inspect the stream_list to produce a list of StreamConfiguration to
1604 * be use to configure the Camera.
1605 */
1606int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list)
1607{
Hirokazu Honda0b661d72021-04-02 11:12:37 +09001608 /* Before any configuration attempt, stop the camera. */
1609 stop();
Jacopo Mondi8fed6132020-12-04 15:26:47 +01001610
Hirokazu Hondad13462f2021-04-03 22:18:28 +09001611 if (stream_list->num_streams == 0) {
1612 LOG(HAL, Error) << "No streams in configuration";
1613 return -EINVAL;
1614 }
1615
Hirokazu Honda4ae2a752021-04-03 22:10:13 +09001616#if defined(OS_CHROMEOS)
1617 if (!validateCropRotate(*stream_list))
1618 return -EINVAL;
1619#endif
1620
Kieran Bingham0a9244e2020-06-26 20:19:10 +01001621 /*
1622 * Generate an empty configuration, and construct a StreamConfiguration
1623 * for each camera3_stream to add to it.
1624 */
1625 config_ = camera_->generateConfiguration();
1626 if (!config_) {
1627 LOG(HAL, Error) << "Failed to generate camera configuration";
1628 return -EINVAL;
1629 }
1630
Kieran Bingham2f34f5e2020-07-01 16:42:13 +01001631 /*
1632 * Clear and remove any existing configuration from previous calls, and
1633 * ensure the required entries are available without further
Jacopo Mondi9ac8f3e2020-09-04 15:25:55 +02001634 * reallocation.
Kieran Bingham2f34f5e2020-07-01 16:42:13 +01001635 */
1636 streams_.clear();
1637 streams_.reserve(stream_list->num_streams);
1638
Hirokazu Honda2bc6ba22020-12-11 09:53:35 +00001639 std::vector<Camera3StreamConfig> streamConfigs;
1640 streamConfigs.reserve(stream_list->num_streams);
1641
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001642 /* First handle all non-MJPEG streams. */
Jacopo Mondic82f9442020-09-02 11:58:00 +02001643 camera3_stream_t *jpegStream = nullptr;
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001644 for (unsigned int i = 0; i < stream_list->num_streams; ++i) {
1645 camera3_stream_t *stream = stream_list->streams[i];
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001646 Size size(stream->width, stream->height);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001647
Kieran Bingham43e3b802020-06-26 09:58:49 +01001648 PixelFormat format = toPixelFormat(stream->format);
1649
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001650 LOG(HAL, Info) << "Stream #" << i
1651 << ", direction: " << stream->stream_type
1652 << ", width: " << stream->width
1653 << ", height: " << stream->height
Kieran Bingham43e3b802020-06-26 09:58:49 +01001654 << ", format: " << utils::hex(stream->format)
Hirokazu Hondaac209ef2021-04-03 22:10:14 +09001655 << ", rotation: " << rotationToString(stream->rotation)
1656#if defined(OS_CHROMEOS)
1657 << ", crop_rotate_scale_degrees: "
1658 << rotationToString(stream->crop_rotate_scale_degrees)
1659#endif
Kieran Bingham43e3b802020-06-26 09:58:49 +01001660 << " (" << format.toString() << ")";
Kieran Bingham0a9244e2020-06-26 20:19:10 +01001661
1662 if (!format.isValid())
1663 return -EINVAL;
1664
Hirokazu Hondac85b6c82021-04-03 22:10:15 +09001665 /* \todo Support rotation. */
1666 if (stream->rotation != CAMERA3_STREAM_ROTATION_0) {
1667 LOG(HAL, Error) << "Rotation is not supported";
1668 return -EINVAL;
1669 }
1670#if defined(OS_CHROMEOS)
1671 if (stream->crop_rotate_scale_degrees != CAMERA3_STREAM_ROTATION_0) {
1672 LOG(HAL, Error) << "Rotation is not supported";
1673 return -EINVAL;
1674 }
1675#endif
1676
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001677 /* Defer handling of MJPEG streams until all others are known. */
Jacopo Mondic82f9442020-09-02 11:58:00 +02001678 if (stream->format == HAL_PIXEL_FORMAT_BLOB) {
1679 if (jpegStream) {
1680 LOG(HAL, Error)
1681 << "Multiple JPEG streams are not supported";
1682 return -EINVAL;
1683 }
1684
1685 jpegStream = stream;
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001686 continue;
Jacopo Mondic82f9442020-09-02 11:58:00 +02001687 }
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001688
Hirokazu Honda2bc6ba22020-12-11 09:53:35 +00001689 Camera3StreamConfig streamConfig;
1690 streamConfig.streams = { { stream, CameraStream::Type::Direct } };
1691 streamConfig.config.size = size;
1692 streamConfig.config.pixelFormat = format;
1693 streamConfigs.push_back(std::move(streamConfig));
Laurent Pinchartde1b9942021-03-05 13:53:53 +02001694
1695 /* This stream will be produced by hardware. */
1696 stream->usage |= GRALLOC_USAGE_HW_CAMERA_WRITE;
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001697 }
1698
Jacopo Mondic82f9442020-09-02 11:58:00 +02001699 /* Now handle the MJPEG streams, adding a new stream if required. */
1700 if (jpegStream) {
Jacopo Mondi94c4d492020-09-04 16:06:11 +02001701 CameraStream::Type type;
Jacopo Mondic82f9442020-09-02 11:58:00 +02001702 int index = -1;
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001703
Jacopo Mondic82f9442020-09-02 11:58:00 +02001704 /* Search for a compatible stream in the non-JPEG ones. */
Hirokazu Honda2bc6ba22020-12-11 09:53:35 +00001705 for (size_t i = 0; i < streamConfigs.size(); ++i) {
Laurent Pinchartde1b9942021-03-05 13:53:53 +02001706 Camera3StreamConfig &streamConfig = streamConfigs[i];
1707 const auto &cfg = streamConfig.config;
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001708
1709 /*
1710 * \todo The PixelFormat must also be compatible with
1711 * the encoder.
1712 */
Jacopo Mondic82f9442020-09-02 11:58:00 +02001713 if (cfg.size.width != jpegStream->width ||
1714 cfg.size.height != jpegStream->height)
1715 continue;
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001716
Jacopo Mondic82f9442020-09-02 11:58:00 +02001717 LOG(HAL, Info)
1718 << "Android JPEG stream mapped to libcamera stream " << i;
1719
Jacopo Mondi94c4d492020-09-04 16:06:11 +02001720 type = CameraStream::Type::Mapped;
Jacopo Mondic82f9442020-09-02 11:58:00 +02001721 index = i;
Laurent Pinchartde1b9942021-03-05 13:53:53 +02001722
1723 /*
1724 * The source stream will be read by software to
1725 * produce the JPEG stream.
1726 */
1727 camera3_stream_t *stream = streamConfig.streams[0].stream;
1728 stream->usage |= GRALLOC_USAGE_SW_READ_OFTEN;
Jacopo Mondic82f9442020-09-02 11:58:00 +02001729 break;
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001730 }
1731
1732 /*
1733 * Without a compatible match for JPEG encoding we must
1734 * introduce a new stream to satisfy the request requirements.
1735 */
Jacopo Mondic82f9442020-09-02 11:58:00 +02001736 if (index < 0) {
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001737 /*
1738 * \todo The pixelFormat should be a 'best-fit' choice
1739 * and may require a validation cycle. This is not yet
1740 * handled, and should be considered as part of any
1741 * stream configuration reworks.
1742 */
Hirokazu Honda2bc6ba22020-12-11 09:53:35 +00001743 Camera3StreamConfig streamConfig;
1744 streamConfig.config.size.width = jpegStream->width;
1745 streamConfig.config.size.height = jpegStream->height;
1746 streamConfig.config.pixelFormat = formats::NV12;
1747 streamConfigs.push_back(std::move(streamConfig));
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001748
Hirokazu Honda2bc6ba22020-12-11 09:53:35 +00001749 LOG(HAL, Info) << "Adding " << streamConfig.config.toString()
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001750 << " for MJPEG support";
1751
Jacopo Mondi94c4d492020-09-04 16:06:11 +02001752 type = CameraStream::Type::Internal;
Hirokazu Honda2bc6ba22020-12-11 09:53:35 +00001753 index = streamConfigs.size() - 1;
Jacopo Mondic82f9442020-09-02 11:58:00 +02001754 }
1755
Laurent Pinchartde1b9942021-03-05 13:53:53 +02001756 /* The JPEG stream will be produced by software. */
1757 jpegStream->usage |= GRALLOC_USAGE_SW_WRITE_OFTEN;
1758
Hirokazu Honda2bc6ba22020-12-11 09:53:35 +00001759 streamConfigs[index].streams.push_back({ jpegStream, type });
1760 }
1761
Hirokazu Honda26d90af2020-12-11 09:53:36 +00001762 sortCamera3StreamConfigs(streamConfigs, jpegStream);
Hirokazu Honda2bc6ba22020-12-11 09:53:35 +00001763 for (const auto &streamConfig : streamConfigs) {
1764 config_->addConfiguration(streamConfig.config);
1765
1766 for (auto &stream : streamConfig.streams) {
1767 streams_.emplace_back(this, stream.type, stream.stream,
1768 config_->size() - 1);
1769 stream.stream->priv = static_cast<void *>(&streams_.back());
1770 }
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001771 }
1772
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001773 switch (config_->validate()) {
1774 case CameraConfiguration::Valid:
1775 break;
1776 case CameraConfiguration::Adjusted:
1777 LOG(HAL, Info) << "Camera configuration adjusted";
Kieran Bingham9f07aeb2020-07-20 22:52:14 +01001778
1779 for (const StreamConfiguration &cfg : *config_)
1780 LOG(HAL, Info) << " - " << cfg.toString();
1781
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001782 config_.reset();
1783 return -EINVAL;
1784 case CameraConfiguration::Invalid:
1785 LOG(HAL, Info) << "Camera configuration invalid";
1786 config_.reset();
1787 return -EINVAL;
1788 }
1789
Jacopo Mondi3c84d882020-10-02 19:40:42 +02001790 /*
Jacopo Mondib84e35d2020-10-03 13:21:50 +02001791 * Once the CameraConfiguration has been adjusted/validated
1792 * it can be applied to the camera.
1793 */
1794 int ret = camera_->configure(config_.get());
1795 if (ret) {
1796 LOG(HAL, Error) << "Failed to configure camera '"
1797 << camera_->id() << "'";
1798 return ret;
1799 }
1800
1801 /*
Jacopo Mondi3c84d882020-10-02 19:40:42 +02001802 * Configure the HAL CameraStream instances using the associated
1803 * StreamConfiguration and set the number of required buffers in
1804 * the Android camera3_stream_t.
1805 */
Jacopo Mondi6c8837d2020-10-03 13:45:29 +02001806 for (CameraStream &cameraStream : streams_) {
Kieran Binghamc7bcae02020-10-13 15:58:26 +01001807 ret = cameraStream.configure();
Jacopo Mondi6c8837d2020-10-03 13:45:29 +02001808 if (ret) {
1809 LOG(HAL, Error) << "Failed to configure camera stream";
Jacopo Mondi3c84d882020-10-02 19:40:42 +02001810 return ret;
Jacopo Mondi6c8837d2020-10-03 13:45:29 +02001811 }
Kieran Bingham0a9244e2020-06-26 20:19:10 +01001812 }
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001813
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001814 return 0;
1815}
1816
Kieran Bingham74ab4422020-07-01 13:25:38 +01001817FrameBuffer *CameraDevice::createFrameBuffer(const buffer_handle_t camera3buffer)
1818{
1819 std::vector<FrameBuffer::Plane> planes;
Kieran Binghamf4c65be2020-07-06 16:12:03 +01001820 for (int i = 0; i < camera3buffer->numFds; i++) {
1821 /* Skip unused planes. */
1822 if (camera3buffer->data[i] == -1)
1823 break;
1824
Kieran Bingham74ab4422020-07-01 13:25:38 +01001825 FrameBuffer::Plane plane;
1826 plane.fd = FileDescriptor(camera3buffer->data[i]);
Kieran Binghamf4c65be2020-07-06 16:12:03 +01001827 if (!plane.fd.isValid()) {
1828 LOG(HAL, Error) << "Failed to obtain FileDescriptor ("
1829 << camera3buffer->data[i] << ") "
1830 << " on plane " << i;
1831 return nullptr;
1832 }
1833
Kieran Bingham6bc652e2020-07-17 16:18:32 +01001834 off_t length = lseek(plane.fd.fd(), 0, SEEK_END);
1835 if (length == -1) {
1836 LOG(HAL, Error) << "Failed to query plane length";
1837 return nullptr;
1838 }
1839
1840 plane.length = length;
Kieran Bingham74ab4422020-07-01 13:25:38 +01001841 planes.push_back(std::move(plane));
1842 }
1843
1844 return new FrameBuffer(std::move(planes));
1845}
1846
Jacopo Mondibb2b4002021-01-04 16:20:05 +01001847int CameraDevice::processControls(Camera3RequestDescriptor *descriptor)
1848{
1849 const CameraMetadata &settings = descriptor->settings_;
1850 if (!settings.isValid())
1851 return 0;
1852
1853 /* Translate the Android request settings to libcamera controls. */
1854 camera_metadata_ro_entry_t entry;
1855 if (settings.getEntry(ANDROID_SCALER_CROP_REGION, &entry)) {
1856 const int32_t *data = entry.data.i32;
1857 Rectangle cropRegion{ data[0], data[1],
1858 static_cast<unsigned int>(data[2]),
1859 static_cast<unsigned int>(data[3]) };
1860 ControlList &controls = descriptor->request_->controls();
1861 controls.set(controls::ScalerCrop, cropRegion);
1862 }
1863
1864 return 0;
1865}
1866
Laurent Pinchartda3f50e2020-01-20 01:09:34 +02001867int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Request)
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001868{
Hirokazu Honda7633a2d2021-04-03 22:37:40 +09001869 if (!isValidRequest(camera3Request))
Jacopo Mondic5b732b2020-12-01 17:12:19 +01001870 return -EINVAL;
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001871
1872 /* Start the camera if that's the first request we handle. */
1873 if (!running_) {
Jacopo Mondi4b1aa212020-10-06 17:56:50 +02001874 worker_.start();
1875
Niklas Söderlunda1c54502019-11-25 17:51:06 +01001876 int ret = camera_->start();
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001877 if (ret) {
1878 LOG(HAL, Error) << "Failed to start camera";
Laurent Pinchartda3f50e2020-01-20 01:09:34 +02001879 return ret;
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001880 }
1881
1882 running_ = true;
1883 }
1884
1885 /*
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001886 * Save the request descriptors for use at completion time.
1887 * The descriptor and the associated memory reserved here are freed
1888 * at request complete time.
1889 */
Hirokazu Hondad4043012021-04-03 22:57:34 +09001890 Camera3RequestDescriptor descriptor(camera_.get(), camera3Request);
1891
Paul Eldera6de3f02021-01-21 18:59:24 +09001892 /*
1893 * \todo The Android request model is incremental, settings passed in
1894 * previous requests are to be effective until overridden explicitly in
1895 * a new request. Do we need to cache settings incrementally here, or is
1896 * it handled by the Android camera service ?
1897 */
1898 if (camera3Request->settings)
1899 lastSettings_ = camera3Request->settings;
1900 else
Hirokazu Hondad4043012021-04-03 22:57:34 +09001901 descriptor.settings_ = lastSettings_;
Kieran Binghameac05422020-07-01 13:28:16 +01001902
Hirokazu Hondad4043012021-04-03 22:57:34 +09001903 LOG(HAL, Debug) << "Queueing request " << descriptor.request_->cookie()
1904 << " with " << descriptor.buffers_.size() << " streams";
1905 for (unsigned int i = 0; i < descriptor.buffers_.size(); ++i) {
1906 const camera3_stream_buffer_t &camera3Buffer = descriptor.buffers_[i];
Hirokazu Honda17a6e782021-03-24 16:07:55 +09001907 camera3_stream *camera3Stream = camera3Buffer.stream;
Jacopo Mondi4b18b822021-01-21 11:10:08 +01001908 CameraStream *cameraStream = static_cast<CameraStream *>(camera3Stream->priv);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001909
Jacopo Mondi35f726d2020-09-04 18:32:48 +02001910 std::stringstream ss;
1911 ss << i << " - (" << camera3Stream->width << "x"
1912 << camera3Stream->height << ")"
1913 << "[" << utils::hex(camera3Stream->format) << "] -> "
1914 << "(" << cameraStream->configuration().size.toString() << ")["
1915 << cameraStream->configuration().pixelFormat.toString() << "]";
1916
Jacopo Mondide8304b2020-09-04 17:45:39 +02001917 /*
1918 * Inspect the camera stream type, create buffers opportunely
1919 * and add them to the Request if required.
1920 */
1921 FrameBuffer *buffer = nullptr;
1922 switch (cameraStream->type()) {
1923 case CameraStream::Type::Mapped:
1924 /*
1925 * Mapped streams don't need buffers added to the
1926 * Request.
1927 */
Jacopo Mondi35f726d2020-09-04 18:32:48 +02001928 LOG(HAL, Debug) << ss.str() << " (mapped)";
Kieran Bingham83ae84e2020-07-03 12:34:59 +01001929 continue;
1930
Jacopo Mondide8304b2020-09-04 17:45:39 +02001931 case CameraStream::Type::Direct:
1932 /*
1933 * Create a libcamera buffer using the dmabuf
1934 * descriptors of the camera3Buffer for each stream and
1935 * associate it with the Camera3RequestDescriptor for
1936 * lifetime management only.
1937 */
Hirokazu Honda17a6e782021-03-24 16:07:55 +09001938 buffer = createFrameBuffer(*camera3Buffer.buffer);
Hirokazu Hondad4043012021-04-03 22:57:34 +09001939 descriptor.frameBuffers_.emplace_back(buffer);
Jacopo Mondi35f726d2020-09-04 18:32:48 +02001940 LOG(HAL, Debug) << ss.str() << " (direct)";
Jacopo Mondide8304b2020-09-04 17:45:39 +02001941 break;
1942
1943 case CameraStream::Type::Internal:
1944 /*
1945 * Get the frame buffer from the CameraStream internal
1946 * buffer pool.
1947 *
1948 * The buffer has to be returned to the CameraStream
1949 * once it has been processed.
1950 */
1951 buffer = cameraStream->getBuffer();
Jacopo Mondi35f726d2020-09-04 18:32:48 +02001952 LOG(HAL, Debug) << ss.str() << " (internal)";
Jacopo Mondide8304b2020-09-04 17:45:39 +02001953 break;
1954 }
1955
Kieran Bingham0cfdf732020-07-01 13:36:24 +01001956 if (!buffer) {
1957 LOG(HAL, Error) << "Failed to create buffer";
Kieran Bingham0cfdf732020-07-01 13:36:24 +01001958 return -ENOMEM;
1959 }
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001960
Hirokazu Hondad4043012021-04-03 22:57:34 +09001961 descriptor.request_->addBuffer(cameraStream->stream(), buffer,
Hirokazu Honda17a6e782021-03-24 16:07:55 +09001962 camera3Buffer.acquire_fence);
Kieran Bingham0cfdf732020-07-01 13:36:24 +01001963 }
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001964
Jacopo Mondibb2b4002021-01-04 16:20:05 +01001965 /*
1966 * Translate controls from Android to libcamera and queue the request
1967 * to the CameraWorker thread.
1968 */
Hirokazu Hondad4043012021-04-03 22:57:34 +09001969 int ret = processControls(&descriptor);
Jacopo Mondibb2b4002021-01-04 16:20:05 +01001970 if (ret)
1971 return ret;
1972
Hirokazu Hondad4043012021-04-03 22:57:34 +09001973 worker_.queueRequest(descriptor.request_.get());
1974
1975 {
1976 std::scoped_lock<std::mutex> lock(mutex_);
1977 descriptors_[descriptor.request_->cookie()] = std::move(descriptor);
1978 }
Paul Elderc7532232020-09-23 19:05:41 +09001979
Laurent Pinchartda3f50e2020-01-20 01:09:34 +02001980 return 0;
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001981}
1982
Niklas Söderlundf7ddfd42019-10-21 20:01:19 +02001983void CameraDevice::requestComplete(Request *request)
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001984{
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02001985 camera3_buffer_status status = CAMERA3_BUFFER_STATUS_OK;
Laurent Pinchart39860092019-09-05 03:12:34 +03001986 std::unique_ptr<CameraMetadata> resultMetadata;
Hirokazu Hondad4043012021-04-03 22:57:34 +09001987
1988 decltype(descriptors_)::node_type node;
1989 {
1990 std::scoped_lock<std::mutex> lock(mutex_);
1991 auto it = descriptors_.find(request->cookie());
1992 if (it == descriptors_.end()) {
1993 LOG(HAL, Fatal)
1994 << "Unknown request: " << request->cookie();
1995 status = CAMERA3_BUFFER_STATUS_ERROR;
1996 return;
1997 }
1998
1999 node = descriptors_.extract(it);
2000 }
2001 Camera3RequestDescriptor &descriptor = node.mapped();
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002002
2003 if (request->status() != Request::RequestComplete) {
Kieran Bingham98986e02020-08-03 14:34:11 +01002004 LOG(HAL, Error) << "Request not successfully completed: "
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002005 << request->status();
2006 status = CAMERA3_BUFFER_STATUS_ERROR;
2007 }
2008
Jacopo Mondibc644072021-01-28 09:58:06 +01002009 LOG(HAL, Debug) << "Request " << request->cookie() << " completed with "
Hirokazu Hondad4043012021-04-03 22:57:34 +09002010 << descriptor.buffers_.size() << " streams";
Jacopo Mondibc644072021-01-28 09:58:06 +01002011
Jacopo Mondid66fd812021-04-07 16:25:32 +02002012 resultMetadata = getResultMetadata(descriptor);
Kieran Binghamc09aee42020-07-28 14:01:19 +01002013
Kieran Bingham83ae84e2020-07-03 12:34:59 +01002014 /* Handle any JPEG compression. */
Hirokazu Hondad4043012021-04-03 22:57:34 +09002015 for (camera3_stream_buffer_t &buffer : descriptor.buffers_) {
Kieran Bingham83ae84e2020-07-03 12:34:59 +01002016 CameraStream *cameraStream =
Hirokazu Honda17a6e782021-03-24 16:07:55 +09002017 static_cast<CameraStream *>(buffer.stream->priv);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002018
Jacopo Mondi160bb092020-10-02 20:48:35 +02002019 if (cameraStream->camera3Stream().format != HAL_PIXEL_FORMAT_BLOB)
Kieran Bingham83ae84e2020-07-03 12:34:59 +01002020 continue;
2021
Jacopo Mondid5473c92021-02-17 16:21:59 +01002022 FrameBuffer *src = request->findBuffer(cameraStream->stream());
2023 if (!src) {
Kieran Bingham83ae84e2020-07-03 12:34:59 +01002024 LOG(HAL, Error) << "Failed to find a source stream buffer";
2025 continue;
2026 }
2027
Jacopo Mondia725baf2021-02-24 12:50:40 +01002028 int ret = cameraStream->process(*src,
Hirokazu Honda17a6e782021-03-24 16:07:55 +09002029 *buffer.buffer,
Hirokazu Hondad4043012021-04-03 22:57:34 +09002030 descriptor.settings_,
Jacopo Mondi9e95d5e2020-10-03 11:28:47 +02002031 resultMetadata.get());
2032 if (ret) {
Kieran Bingham83ae84e2020-07-03 12:34:59 +01002033 status = CAMERA3_BUFFER_STATUS_ERROR;
2034 continue;
2035 }
Jacopo Mondide8304b2020-09-04 17:45:39 +02002036
2037 /*
2038 * Return the FrameBuffer to the CameraStream now that we're
2039 * done processing it.
2040 */
2041 if (cameraStream->type() == CameraStream::Type::Internal)
Jacopo Mondid5473c92021-02-17 16:21:59 +01002042 cameraStream->putBuffer(src);
Kieran Bingham83ae84e2020-07-03 12:34:59 +01002043 }
2044
2045 /* Prepare to call back the Android camera stack. */
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002046 camera3_capture_result_t captureResult = {};
Hirokazu Hondad4043012021-04-03 22:57:34 +09002047 captureResult.frame_number = descriptor.frameNumber_;
2048 captureResult.num_output_buffers = descriptor.buffers_.size();
2049 for (camera3_stream_buffer_t &buffer : descriptor.buffers_) {
Hirokazu Honda17a6e782021-03-24 16:07:55 +09002050 buffer.acquire_fence = -1;
2051 buffer.release_fence = -1;
2052 buffer.status = status;
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002053 }
Hirokazu Hondad4043012021-04-03 22:57:34 +09002054 captureResult.output_buffers = descriptor.buffers_.data();
Kieran Bingham0cfdf732020-07-01 13:36:24 +01002055
Laurent Pinchart39860092019-09-05 03:12:34 +03002056 if (status == CAMERA3_BUFFER_STATUS_OK) {
Jacopo Mondid66fd812021-04-07 16:25:32 +02002057 uint64_t timestamp =
2058 static_cast<uint64_t>(request->metadata()
2059 .get(controls::SensorTimestamp));
Hirokazu Hondad4043012021-04-03 22:57:34 +09002060 notifyShutter(descriptor.frameNumber_, timestamp);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002061
2062 captureResult.partial_result = 1;
Laurent Pinchart39860092019-09-05 03:12:34 +03002063 captureResult.result = resultMetadata->get();
2064 }
2065
2066 if (status == CAMERA3_BUFFER_STATUS_ERROR || !captureResult.result) {
2067 /* \todo Improve error handling. In case we notify an error
2068 * because the metadata generation fails, a shutter event has
2069 * already been notified for this frame number before the error
2070 * is here signalled. Make sure the error path plays well with
2071 * the camera stack state machine.
2072 */
Hirokazu Hondad4043012021-04-03 22:57:34 +09002073 notifyError(descriptor.frameNumber_,
2074 descriptor.buffers_[0].stream);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002075 }
2076
2077 callbacks_->process_capture_result(callbacks_, &captureResult);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002078}
2079
Jacopo Mondia7b92772020-05-26 15:41:41 +02002080std::string CameraDevice::logPrefix() const
2081{
Niklas Söderlund2e7c80a2020-08-02 23:57:17 +02002082 return "'" + camera_->id() + "'";
Jacopo Mondia7b92772020-05-26 15:41:41 +02002083}
2084
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002085void CameraDevice::notifyShutter(uint32_t frameNumber, uint64_t timestamp)
2086{
2087 camera3_notify_msg_t notify = {};
2088
2089 notify.type = CAMERA3_MSG_SHUTTER;
2090 notify.message.shutter.frame_number = frameNumber;
2091 notify.message.shutter.timestamp = timestamp;
2092
2093 callbacks_->notify(callbacks_, &notify);
2094}
2095
2096void CameraDevice::notifyError(uint32_t frameNumber, camera3_stream_t *stream)
2097{
2098 camera3_notify_msg_t notify = {};
2099
Kieran Bingham8ad1b9c2020-07-01 16:47:58 +01002100 /*
2101 * \todo Report and identify the stream number or configuration to
2102 * clarify the stream that failed.
2103 */
2104 LOG(HAL, Error) << "Error occurred on frame " << frameNumber << " ("
2105 << toPixelFormat(stream->format).toString() << ")";
2106
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002107 notify.type = CAMERA3_MSG_ERROR;
2108 notify.message.error.error_stream = stream;
2109 notify.message.error.frame_number = frameNumber;
2110 notify.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
2111
2112 callbacks_->notify(callbacks_, &notify);
2113}
2114
2115/*
2116 * Produce a set of fixed result metadata.
2117 */
Laurent Pinchartdbafe162019-10-27 00:36:13 +03002118std::unique_ptr<CameraMetadata>
Jacopo Mondid66fd812021-04-07 16:25:32 +02002119CameraDevice::getResultMetadata(const Camera3RequestDescriptor &descriptor) const
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002120{
Hirokazu Honda82b81512021-03-25 20:13:56 +09002121 const ControlList &metadata = descriptor.request_->metadata();
2122 const CameraMetadata &settings = descriptor.settings_;
Paul Elder958c80a2021-01-26 09:47:33 +09002123 camera_metadata_ro_entry_t entry;
Jacopo Mondi94d42ce2021-01-29 14:54:39 +01002124 bool found;
Jacopo Mondi3535e0c2020-12-09 17:51:57 +01002125
Jacopo Mondi48504ba2019-09-04 16:18:19 +02002126 /*
2127 * \todo Keep this in sync with the actual number of entries.
Paul Elder12646282021-01-23 13:56:01 +09002128 * Currently: 40 entries, 156 bytes
Jacopo Mondi3e979d72021-01-04 16:28:24 +01002129 *
2130 * Reserve more space for the JPEG metadata set by the post-processor.
Paul Elderabfabdd2021-01-23 13:54:28 +09002131 * Currently:
2132 * ANDROID_JPEG_GPS_COORDINATES (double x 3) = 24 bytes
2133 * ANDROID_JPEG_GPS_PROCESSING_METHOD (byte x 32) = 32 bytes
2134 * ANDROID_JPEG_GPS_TIMESTAMP (int64) = 8 bytes
2135 * ANDROID_JPEG_SIZE (int32_t) = 4 bytes
2136 * ANDROID_JPEG_QUALITY (byte) = 1 byte
2137 * ANDROID_JPEG_ORIENTATION (int32_t) = 4 bytes
Paul Elder12646282021-01-23 13:56:01 +09002138 * ANDROID_JPEG_THUMBNAIL_QUALITY (byte) = 1 byte
2139 * ANDROID_JPEG_THUMBNAIL_SIZE (int32 x 2) = 8 bytes
2140 * Total bytes for JPEG metadata: 82
Jacopo Mondi48504ba2019-09-04 16:18:19 +02002141 */
Laurent Pinchart39860092019-09-05 03:12:34 +03002142 std::unique_ptr<CameraMetadata> resultMetadata =
Jacopo Mondif29601e2021-02-03 16:47:30 +01002143 std::make_unique<CameraMetadata>(44, 166);
Laurent Pinchart39860092019-09-05 03:12:34 +03002144 if (!resultMetadata->isValid()) {
Paul Elder2cd13cd2021-03-31 20:24:06 +09002145 LOG(HAL, Error) << "Failed to allocate result metadata";
Laurent Pinchart39860092019-09-05 03:12:34 +03002146 return nullptr;
2147 }
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002148
Jacopo Mondi94d42ce2021-01-29 14:54:39 +01002149 /*
2150 * \todo The value of the results metadata copied from the settings
2151 * will have to be passed to the libcamera::Camera and extracted
2152 * from libcamera::Request::metadata.
2153 */
2154
Jacopo Mondi520c3662021-02-03 14:44:34 +01002155 uint8_t value = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF;
2156 resultMetadata->addEntry(ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
2157 &value, 1);
2158
2159 value = ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF;
Jacopo Mondi3e979d72021-01-04 16:28:24 +01002160 resultMetadata->addEntry(ANDROID_CONTROL_AE_ANTIBANDING_MODE, &value, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002161
Jacopo Mondie9c28752021-02-03 14:47:45 +01002162 int32_t value32 = 0;
2163 resultMetadata->addEntry(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
2164 &value32, 1);
2165
Jacopo Mondi3e979d72021-01-04 16:28:24 +01002166 value = ANDROID_CONTROL_AE_LOCK_OFF;
2167 resultMetadata->addEntry(ANDROID_CONTROL_AE_LOCK, &value, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002168
Jacopo Mondi3e979d72021-01-04 16:28:24 +01002169 value = ANDROID_CONTROL_AE_MODE_ON;
2170 resultMetadata->addEntry(ANDROID_CONTROL_AE_MODE, &value, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002171
Jacopo Mondid1508602021-01-21 18:21:28 +01002172 if (settings.getEntry(ANDROID_CONTROL_AE_TARGET_FPS_RANGE, &entry))
2173 /*
2174 * \todo Retrieve the AE FPS range from the libcamera metadata.
2175 * As libcamera does not support that control, as a temporary
2176 * workaround return what the framework asked.
2177 */
2178 resultMetadata->addEntry(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
2179 entry.data.i32, 2);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002180
Jacopo Mondi3e979d72021-01-04 16:28:24 +01002181 value = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
Jacopo Mondi94d42ce2021-01-29 14:54:39 +01002182 found = settings.getEntry(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &entry);
Jacopo Mondi3e979d72021-01-04 16:28:24 +01002183 resultMetadata->addEntry(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
Jacopo Mondi94d42ce2021-01-29 14:54:39 +01002184 found ? entry.data.u8 : &value, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002185
Jacopo Mondi3e979d72021-01-04 16:28:24 +01002186 value = ANDROID_CONTROL_AE_STATE_CONVERGED;
2187 resultMetadata->addEntry(ANDROID_CONTROL_AE_STATE, &value, 1);
2188
2189 value = ANDROID_CONTROL_AF_MODE_OFF;
2190 resultMetadata->addEntry(ANDROID_CONTROL_AF_MODE, &value, 1);
2191
2192 value = ANDROID_CONTROL_AF_STATE_INACTIVE;
2193 resultMetadata->addEntry(ANDROID_CONTROL_AF_STATE, &value, 1);
2194
2195 value = ANDROID_CONTROL_AF_TRIGGER_IDLE;
2196 resultMetadata->addEntry(ANDROID_CONTROL_AF_TRIGGER, &value, 1);
2197
2198 value = ANDROID_CONTROL_AWB_MODE_AUTO;
2199 resultMetadata->addEntry(ANDROID_CONTROL_AWB_MODE, &value, 1);
2200
2201 value = ANDROID_CONTROL_AWB_LOCK_OFF;
2202 resultMetadata->addEntry(ANDROID_CONTROL_AWB_LOCK, &value, 1);
2203
2204 value = ANDROID_CONTROL_AWB_STATE_CONVERGED;
2205 resultMetadata->addEntry(ANDROID_CONTROL_AWB_STATE, &value, 1);
2206
2207 value = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
2208 resultMetadata->addEntry(ANDROID_CONTROL_CAPTURE_INTENT, &value, 1);
2209
2210 value = ANDROID_CONTROL_EFFECT_MODE_OFF;
2211 resultMetadata->addEntry(ANDROID_CONTROL_EFFECT_MODE, &value, 1);
2212
2213 value = ANDROID_CONTROL_MODE_AUTO;
2214 resultMetadata->addEntry(ANDROID_CONTROL_MODE, &value, 1);
2215
2216 value = ANDROID_CONTROL_SCENE_MODE_DISABLED;
2217 resultMetadata->addEntry(ANDROID_CONTROL_SCENE_MODE, &value, 1);
2218
2219 value = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
2220 resultMetadata->addEntry(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE, &value, 1);
2221
2222 value = ANDROID_FLASH_MODE_OFF;
2223 resultMetadata->addEntry(ANDROID_FLASH_MODE, &value, 1);
2224
2225 value = ANDROID_FLASH_STATE_UNAVAILABLE;
2226 resultMetadata->addEntry(ANDROID_FLASH_STATE, &value, 1);
2227
Jacopo Mondi94d42ce2021-01-29 14:54:39 +01002228 if (settings.getEntry(ANDROID_LENS_APERTURE, &entry))
Paul Elderabfabdd2021-01-23 13:54:28 +09002229 resultMetadata->addEntry(ANDROID_LENS_APERTURE, entry.data.f, 1);
2230
2231 float focal_length = 1.0;
2232 resultMetadata->addEntry(ANDROID_LENS_FOCAL_LENGTH, &focal_length, 1);
2233
Jacopo Mondi3e979d72021-01-04 16:28:24 +01002234 value = ANDROID_LENS_STATE_STATIONARY;
2235 resultMetadata->addEntry(ANDROID_LENS_STATE, &value, 1);
2236
2237 value = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
2238 resultMetadata->addEntry(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
2239 &value, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002240
Jacopo Mondi5360d802021-02-03 16:43:22 +01002241 value32 = ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
2242 resultMetadata->addEntry(ANDROID_SENSOR_TEST_PATTERN_MODE,
2243 &value32, 1);
2244
Jacopo Mondi3e979d72021-01-04 16:28:24 +01002245 value = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
2246 resultMetadata->addEntry(ANDROID_STATISTICS_FACE_DETECT_MODE,
2247 &value, 1);
2248
2249 value = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
2250 resultMetadata->addEntry(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
2251 &value, 1);
2252
Jacopo Mondif29601e2021-02-03 16:47:30 +01002253 value = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF;
2254 resultMetadata->addEntry(ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
2255 &value, 1);
2256
Jacopo Mondi3e979d72021-01-04 16:28:24 +01002257 value = ANDROID_STATISTICS_SCENE_FLICKER_NONE;
2258 resultMetadata->addEntry(ANDROID_STATISTICS_SCENE_FLICKER,
2259 &value, 1);
2260
2261 value = ANDROID_NOISE_REDUCTION_MODE_OFF;
2262 resultMetadata->addEntry(ANDROID_NOISE_REDUCTION_MODE, &value, 1);
2263
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002264 /* 33.3 msec */
2265 const int64_t rolling_shutter_skew = 33300000;
Laurent Pinchart39860092019-09-05 03:12:34 +03002266 resultMetadata->addEntry(ANDROID_SENSOR_ROLLING_SHUTTER_SKEW,
2267 &rolling_shutter_skew, 1);
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002268
Jacopo Mondi3535e0c2020-12-09 17:51:57 +01002269 /* Add metadata tags reported by libcamera. */
Jacopo Mondid66fd812021-04-07 16:25:32 +02002270 const int64_t timestamp = metadata.get(controls::SensorTimestamp);
2271 resultMetadata->addEntry(ANDROID_SENSOR_TIMESTAMP, &timestamp, 1);
2272
Jacopo Mondi3535e0c2020-12-09 17:51:57 +01002273 if (metadata.contains(controls::draft::PipelineDepth)) {
2274 uint8_t pipeline_depth =
2275 metadata.get<int32_t>(controls::draft::PipelineDepth);
2276 resultMetadata->addEntry(ANDROID_REQUEST_PIPELINE_DEPTH,
2277 &pipeline_depth, 1);
2278 }
2279
Jacopo Mondic9705a52021-01-05 19:30:48 +01002280 if (metadata.contains(controls::ExposureTime)) {
Paul Elder78f49d52021-01-28 17:45:44 +09002281 int64_t exposure = metadata.get(controls::ExposureTime) * 1000ULL;
Jacopo Mondic9705a52021-01-05 19:30:48 +01002282 resultMetadata->addEntry(ANDROID_SENSOR_EXPOSURE_TIME,
2283 &exposure, 1);
2284 }
2285
Jacopo Mondibb2b4002021-01-04 16:20:05 +01002286 if (metadata.contains(controls::ScalerCrop)) {
2287 Rectangle crop = metadata.get(controls::ScalerCrop);
2288 int32_t cropRect[] = {
2289 crop.x, crop.y, static_cast<int32_t>(crop.width),
2290 static_cast<int32_t>(crop.height),
2291 };
2292 resultMetadata->addEntry(ANDROID_SCALER_CROP_REGION, cropRect, 4);
2293 }
2294
Laurent Pinchart39860092019-09-05 03:12:34 +03002295 /*
2296 * Return the result metadata pack even is not valid: get() will return
2297 * nullptr.
2298 */
2299 if (!resultMetadata->isValid()) {
2300 LOG(HAL, Error) << "Failed to construct result metadata";
2301 }
Jacopo Mondi667d8ea2019-05-10 17:40:02 +02002302
2303 return resultMetadata;
2304}