blob: 289a2cf9b07035a0f971c9583a860c07c8498103 [file] [log] [blame]
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "V4L2Camera.h"
18
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070019#include <fcntl.h>
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070020#include <linux/videodev2.h>
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070021#include <sys/types.h>
22#include <sys/stat.h>
23
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070024#include <cstdlib>
25
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070026#include <camera/CameraMetadata.h>
27#include <hardware/camera3.h>
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070028#include <nativehelper/ScopedFd.h>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070029
30#include "Common.h"
31
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070032#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
33
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070034namespace v4l2_camera_hal {
35
Ari Hausman-Cohendde80172016-07-01 16:20:36 -070036// Helper function for managing metadata.
37static std::vector<int32_t> getMetadataKeys(
38 const camera_metadata_t* metadata) {
39 std::vector<int32_t> keys;
40 size_t num_entries = get_camera_metadata_entry_count(metadata);
41 for (size_t i = 0; i < num_entries; ++i) {
42 camera_metadata_ro_entry_t entry;
43 get_camera_metadata_ro_entry(metadata, i, &entry);
44 keys.push_back(entry.tag);
45 }
46 return keys;
47}
48
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070049V4L2Camera::V4L2Camera(int id, std::string path)
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070050 : default_camera_hal::Camera(id),
51 mDevicePath(std::move(path)),
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070052 mOutStreamFormat(0),
53 mOutStreamWidth(0),
54 mOutStreamHeight(0),
55 mOutStreamMaxBuffers(0),
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070056 mTemplatesInitialized(false),
57 mCharacteristicsInitialized(false) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070058 HAL_LOG_ENTER();
59}
60
61V4L2Camera::~V4L2Camera() {
62 HAL_LOG_ENTER();
63}
64
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070065// Helper function. Should be used instead of ioctl throughout this class.
66template<typename T>
67int V4L2Camera::ioctlLocked(int request, T data) {
68 android::Mutex::Autolock al(mDeviceLock);
69 if (mDeviceFd.get() < 0) {
70 return -ENODEV;
71 }
72 return TEMP_FAILURE_RETRY(ioctl(mDeviceFd.get(), request, data));
73}
74
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070075int V4L2Camera::connect() {
76 HAL_LOG_ENTER();
77
78 if (mDeviceFd.get() >= 0) {
79 HAL_LOGE("Camera device %s is opened. Close it first", mDevicePath.c_str());
80 return -EIO;
81 }
82
83 int fd = TEMP_FAILURE_RETRY(open(mDevicePath.c_str(), O_RDWR));
84 if (fd < 0) {
85 HAL_LOGE("failed to open %s (%s)", mDevicePath.c_str(), strerror(errno));
86 return -errno;
87 }
88 mDeviceFd.reset(fd);
89
90 // TODO(b/29185945): confirm this is a supported device.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070091 // This is checked by the HAL, but the device at mDevicePath may
92 // not be the same one that was there when the HAL was loaded.
93 // (Alternatively, better hotplugging support may make this unecessary
94 // by disabling cameras that get disconnected and checking newly connected
95 // cameras, so connect() is never called on an unsupported camera)
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070096
97 // TODO(b/29158098): Inform service of any flashes that are no longer available
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070098 // because this camera is in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070099 return 0;
100}
101
102void V4L2Camera::disconnect() {
103 HAL_LOG_ENTER();
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700104 // TODO(b/29158098): Inform service of any flashes that are available again
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700105 // because this camera is no longer in use.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700106
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700107 mDeviceFd.reset();
108}
109
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700110int V4L2Camera::initStaticInfo(camera_metadata_t** out) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700111 HAL_LOG_ENTER();
112
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700113 android::status_t res;
114 // Device characteristics need to be queried prior
115 // to static info setup.
116 if (!mCharacteristicsInitialized) {
117 res = initCharacteristics();
118 if (res) {
119 return res;
120 }
121 }
122
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700123 android::CameraMetadata info;
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -0700124
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700125 // Static metadata characteristics from /system/media/camera/docs/docs.html.
126
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700127 /* android.colorCorrection. */
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700128
129 // No easy way to turn chromatic aberration correction OFF in v4l2,
130 // though this may be supportable via a collection of other user controls.
131 uint8_t avail_aberration_modes[] = {
132 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST,
133 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700134 res = info.update(ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
135 avail_aberration_modes, ARRAY_SIZE(avail_aberration_modes));
136 if (res != android::OK) {
137 return res;
138 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700139
140 /* android.control. */
141
142 /* 3As */
143
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700144 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
145 mAeAntibandingModes.data(), mAeAntibandingModes.size());
146 if (res != android::OK) {
147 return res;
148 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700149
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700150 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_MODES,
151 mAeModes.data(), mAeModes.size());
152 if (res != android::OK) {
153 return res;
154 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700155
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700156 // Flatten mFpsRanges.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700157 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
158 mFpsRanges.data(), mFpsRanges.total_num_elements());
159 if (res != android::OK) {
160 return res;
161 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700162
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700163 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
164 mAeCompensationRange.data(), mAeCompensationRange.size());
165 if (res != android::OK) {
166 return res;
167 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700168
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700169 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_STEP,
170 &mAeCompensationStep, 1);
171 if (res != android::OK) {
172 return res;
173 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700174
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700175 res = info.update(ANDROID_CONTROL_AF_AVAILABLE_MODES,
176 mAfModes.data(), mAfModes.size());
177 if (res != android::OK) {
178 return res;
179 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700180
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700181 res = info.update(ANDROID_CONTROL_AWB_AVAILABLE_MODES,
182 mAwbModes.data(), mAwbModes.size());
183 if (res != android::OK) {
184 return res;
185 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700186
187 // Couldn't find any V4L2 support for regions, though maybe it's out there.
188 int32_t max_regions[] = {/*AE*/ 0,/*AWB*/ 0,/*AF*/ 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700189 res = info.update(ANDROID_CONTROL_MAX_REGIONS,
190 max_regions, ARRAY_SIZE(max_regions));
191 if (res != android::OK) {
192 return res;
193 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700194
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700195 res = info.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE,
196 &mAeLockAvailable, 1);
197 if (res != android::OK) {
198 return res;
199 }
200 res = info.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
201 &mAwbLockAvailable, 1);
202 if (res != android::OK) {
203 return res;
204 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700205
206 /* Scene modes. */
207
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700208 res = info.update(ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
209 mSceneModes.data(), mSceneModes.size());
210 if (res != android::OK) {
211 return res;
212 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700213
214 // A 3-tuple of AE, AWB, AF overrides for each scene mode.
215 // Ignored for DISABLED, FACE_PRIORITY and FACE_PRIORITY_LOW_LIGHT.
216 uint8_t scene_mode_overrides[] = {
217 /*SCENE_MODE_DISABLED*/ /*AE*/0, /*AW*/0, /*AF*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700218 res = info.update(ANDROID_CONTROL_SCENE_MODE_OVERRIDES,
219 scene_mode_overrides, ARRAY_SIZE(scene_mode_overrides));
220 if (res != android::OK) {
221 return res;
222 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700223
224 /* Top level 3A/Scenes switch. */
225
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700226 res = info.update(ANDROID_CONTROL_AVAILABLE_MODES,
227 mControlModes.data(), mControlModes.size());
228 if (res != android::OK) {
229 return res;
230 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700231
232 /* Other android.control configuration. */
233
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700234 res = info.update(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
235 mVideoStabilizationModes.data(),
236 mVideoStabilizationModes.size());
237 if (res != android::OK) {
238 return res;
239 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700240
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700241 res = info.update(ANDROID_CONTROL_AVAILABLE_EFFECTS,
242 mEffects.data(), mEffects.size());
243 if (res != android::OK) {
244 return res;
245 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700246
247 // AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS only necessary
248 // for devices supporting CONSTRAINED_HIGH_SPEED_VIDEO,
249 // which this HAL doesn't support.
250
251 // POST_RAW_SENSITIVITY_BOOST_RANGE only necessary
252 // for devices supporting RAW format outputs.
253
254 /* android.edge. */
255
256 // Not sure if V4L2 does or doesn't do this, but HAL documentation says
257 // all devices must support FAST, and FAST can be equivalent to OFF, so
258 // either way it's fine to list.
259 uint8_t avail_edge_modes[] = {
260 ANDROID_EDGE_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700261 res = info.update(ANDROID_EDGE_AVAILABLE_EDGE_MODES,
262 avail_edge_modes, ARRAY_SIZE(avail_edge_modes));
263 if (res != android::OK) {
264 return res;
265 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700266
267 /* android.flash. */
268
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700269 res = info.update(ANDROID_FLASH_INFO_AVAILABLE,
270 &mFlashAvailable, 1);
271 if (res != android::OK) {
272 return res;
273 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700274
275 // info.chargeDuration, color.Temperature, maxEnergy marked FUTURE.
276
277 /* android.hotPixel. */
278
279 // No known V4L2 hot pixel correction. But it might be happening,
280 // so we report FAST/HIGH_QUALITY.
281 uint8_t avail_hot_pixel_modes[] = {
282 ANDROID_HOT_PIXEL_MODE_FAST,
283 ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700284 res = info.update(ANDROID_HOT_PIXEL_AVAILABLE_HOT_PIXEL_MODES,
285 avail_hot_pixel_modes, ARRAY_SIZE(avail_hot_pixel_modes));
286 if (res != android::OK) {
287 return res;
288 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700289
290 /* android.jpeg. */
291
292 // For now, no thumbnails available (only [0,0], the "no thumbnail" size).
293 // TODO(b/29580107): Could end up with a mismatch between request & result,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700294 // since V4L2 doesn't actually allow for thumbnail size control.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700295 int32_t thumbnail_sizes[] = {
296 0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700297 res = info.update(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
298 thumbnail_sizes, ARRAY_SIZE(thumbnail_sizes));
299 if (res != android::OK) {
300 return res;
301 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700302
303 // V4L2 doesn't support querying this, so we generously assume up to 3 MB.
304 int32_t max_jpeg_size = 3000000;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700305 res = info.update(ANDROID_JPEG_MAX_SIZE,
306 &max_jpeg_size, 1);
307 if (res != android::OK) {
308 return res;
309 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700310
311 /* android.lens. */
312
313 /* Misc. lens control. */
314
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700315 res = info.update(ANDROID_LENS_INFO_AVAILABLE_APERTURES,
316 &mAperture, 1);
317 if (res != android::OK) {
318 return res;
319 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700320
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700321 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES,
322 &mFilterDensity, 1);
323 if (res != android::OK) {
324 return res;
325 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700326
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700327 res = info.update(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
328 mOpticalStabilizationModes.data(),
329 mOpticalStabilizationModes.size());
330 if (res != android::OK) {
331 return res;
332 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700333
334 // No known V4L2 shading map info.
335 int32_t shading_map_size[] = {1, 1};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700336 res = info.update(ANDROID_LENS_INFO_SHADING_MAP_SIZE,
337 shading_map_size, ARRAY_SIZE(shading_map_size));
338 if (res != android::OK) {
339 return res;
340 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700341
342 // All V4L2 devices are considered to be external facing.
343 uint8_t facing = ANDROID_LENS_FACING_EXTERNAL;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700344 res = info.update(ANDROID_LENS_FACING, &facing, 1);
345 if (res != android::OK) {
346 return res;
347 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700348
349 /* Zoom/Focus. */
350
351 // No way to actually get the focal length in V4L2, but it's a required key,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700352 // so we just fake it.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700353 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
354 &mFocalLength, 1);
355 if (res != android::OK) {
356 return res;
357 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700358
359 // V4L2 focal units do not correspond to a particular physical unit.
360 uint8_t focus_calibration =
361 ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700362 res = info.update(ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
363 &focus_calibration, 1);
364 if (res != android::OK) {
365 return res;
366 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700367
368 // info.hyperfocalDistance not required for UNCALIBRATED.
369
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700370 res = info.update(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
371 &mFocusDistance, 1);
372 if (res != android::OK) {
373 return res;
374 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700375
376 /* Depth. */
377
378 // DEPTH capability not supported by this HAL. Not implemented:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700379 // poseRotation
380 // poseTranslation
381 // intrinsicCalibration
382 // radialDistortion
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700383
384 /* anroid.noise. */
385
386 // Unable to control noise reduction in V4L2 devices,
387 // but FAST is allowed to be the same as OFF.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700388 uint8_t avail_noise_reduction_modes[] = {ANDROID_NOISE_REDUCTION_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700389 res = info.update(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
390 avail_noise_reduction_modes,
391 ARRAY_SIZE(avail_noise_reduction_modes));
392 if (res != android::OK) {
393 return res;
394 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700395
396 /* android.request. */
397
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700398 int32_t max_num_output_streams[] = {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700399 mMaxRawOutputStreams, mMaxYuvOutputStreams, mMaxJpegOutputStreams};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700400 res = info.update(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
401 max_num_output_streams, ARRAY_SIZE(max_num_output_streams));
402 if (res != android::OK) {
403 return res;
404 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700405
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700406 res = info.update(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS,
407 &mMaxInputStreams, 1);
408 if (res != android::OK) {
409 return res;
410 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700411
412 // No way to know for V4L2, so fake with max allowable latency.
413 // Doesn't mean much without per-frame controls.
414 uint8_t pipeline_max_depth = 4;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700415 res = info.update(ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
416 &pipeline_max_depth, 1);
417 if (res != android::OK) {
418 return res;
419 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700420
421 // Partial results not supported; partialResultCount defaults to 1.
422
423 // Available capabilities & keys queried at very end of this method.
424
425 /* android.scaler. */
426
427 /* Cropping. */
428
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700429 res = info.update(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
430 &mMaxZoom, 1);
431 if (res != android::OK) {
432 return res;
433 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700434
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700435 res = info.update(ANDROID_SCALER_CROPPING_TYPE, &mCropType, 1);
436 if (res != android::OK) {
437 return res;
438 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700439
440 /* Streams. */
441
442 // availableInputOutputFormatsMap only required for reprocessing capability.
443
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700444 // Flatten mStreamConfigs.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700445 res = info.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
446 mStreamConfigs.data(),
447 mStreamConfigs.total_num_elements());
448 if (res != android::OK) {
449 return res;
450 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700451
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700452 // Flatten mMinFrameDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700453 res = info.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
454 mMinFrameDurations.data(),
455 mMinFrameDurations.total_num_elements());
456 if (res != android::OK) {
457 return res;
458 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700459
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700460 // Flatten mStallDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700461 res = info.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
462 mStallDurations.data(),
463 mStallDurations.total_num_elements());
464 if (res != android::OK) {
465 return res;
466 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700467
468 /* android.sensor. */
469
470 /* Sizes. */
471
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700472 res = info.update(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
473 mPixelArraySize.data(), mPixelArraySize.size());
474 if (res != android::OK) {
475 return res;
476 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700477 // No V4L2 way to differentiate active vs. inactive parts of the rectangle.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700478 res = info.update(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
479 mPixelArraySize.data(), mPixelArraySize.size());
480 if (res != android::OK) {
481 return res;
482 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700483
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700484 res = info.update(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
485 mPhysicalSize.data(), mPhysicalSize.size());
486 if (res != android::OK) {
487 return res;
488 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700489
490 /* Misc sensor information. */
491
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700492 res = info.update(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
493 &mMaxFrameDuration, 1);
494 if (res != android::OK) {
495 return res;
496 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700497
498 // HAL uses BOOTTIME timestamps.
499 // TODO(b/29457051): make sure timestamps are consistent throughout the HAL.
500 uint8_t timestamp_source = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700501 res = info.update(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
502 &timestamp_source, 1);
503 if (res != android::OK) {
504 return res;
505 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700506
507 // As in initDeviceInfo, no way to actually get orientation.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700508 res = info.update(ANDROID_SENSOR_ORIENTATION, &mOrientation, 1);
509 if (res != android::OK) {
510 return res;
511 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700512
513 // availableTestPatternModes just defaults to OFF, which is fine.
514
515 // info.exposureTimeRange, info.sensitivityRange:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700516 // exposure/sensitivity manual control not supported.
517 // Could query V4L2_CID_ISO_SENSITIVITY to support sensitivity if desired.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700518
519 // info.whiteLevel, info.lensShadingApplied,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700520 // info.preCorrectionPixelArraySize, referenceIlluminant1/2,
521 // calibrationTransform1/2, colorTransform1/2, forwardMatrix1/2,
522 // blackLevelPattern, profileHueSatMapDimensions
523 // all only necessary for RAW.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700524
525 // baseGainFactor marked FUTURE.
526
527 // maxAnalogSensitivity optional for LIMITED device.
528
529 // opticalBlackRegions: No known way to get in V4L2, but not necessary.
530
531 // opaqueRawSize not necessary since RAW_OPAQUE format not supported.
532
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700533 /* android.shading */
534
535 // No known V4L2 lens shading. But it might be happening,
536 // so we report FAST/HIGH_QUALITY.
537 uint8_t avail_shading_modes[] = {
538 ANDROID_SHADING_MODE_FAST,
539 ANDROID_SHADING_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700540 res = info.update(ANDROID_SHADING_AVAILABLE_MODES,
541 avail_shading_modes, ARRAY_SIZE(avail_shading_modes));
542 if (res != android::OK) {
543 return res;
544 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700545
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700546 /* android.statistics */
547
548 // Face detection not supported.
549 uint8_t avail_face_detect_modes[] = {
550 ANDROID_STATISTICS_FACE_DETECT_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700551 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
552 avail_face_detect_modes,
553 ARRAY_SIZE(avail_face_detect_modes));
554 if (res != android::OK) {
555 return res;
556 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700557
558 int32_t max_face_count = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700559 res = info.update(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
560 &max_face_count, 1);
561 if (res != android::OK) {
562 return res;
563 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700564
565 // info.histogramBucketCount, info.maxHistogramCount,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700566 // info.maxSharpnessMapValue, info.sharpnessMapSizemarked FUTURE.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700567
568 // ON only needs to be supported for RAW capable devices.
569 uint8_t avail_hot_pixel_map_modes[] = {
570 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700571 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
572 avail_hot_pixel_map_modes,
573 ARRAY_SIZE(avail_hot_pixel_map_modes));
574 if (res != android::OK) {
575 return res;
576 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700577
578 // ON only needs to be supported for RAW capable devices.
579 uint8_t avail_lens_shading_map_modes[] = {
580 ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700581 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
582 avail_lens_shading_map_modes,
583 ARRAY_SIZE(avail_lens_shading_map_modes));
584 if (res != android::OK) {
585 return res;
586 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700587
588 /* android.tonemap. */
589
590 // tonemapping only required for MANUAL_POST_PROCESSING capability.
591
592 /* android.led. */
593
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700594 // May or may not have LEDs available.
595 if (!mLeds.empty()) {
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700596 res = info.update(ANDROID_LED_AVAILABLE_LEDS, mLeds.data(), mLeds.size());
597 if (res != android::OK) {
598 return res;
599 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700600 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700601
602 /* android.sync. */
603
604 // "LIMITED devices are strongly encouraged to use a non-negative value.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700605 // If UNKNOWN is used here then app developers do not have a way to know
606 // when sensor settings have been applied." - Unfortunately, V4L2 doesn't
607 // really help here either. Could even be that adjusting settings mid-stream
608 // blocks in V4L2, and should be avoided.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700609 int32_t max_latency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700610 res = info.update(ANDROID_SYNC_MAX_LATENCY,
611 &max_latency, 1);
612 if (res != android::OK) {
613 return res;
614 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700615
616 /* android.reprocess. */
617
618 // REPROCESSING not supported by this HAL.
619
620 /* android.depth. */
621
622 // DEPTH not supported by this HAL.
623
624 /* Capabilities and android.info. */
625
626 uint8_t hw_level = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700627 res = info.update(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
628 &hw_level, 1);
629 if (res != android::OK) {
630 return res;
631 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700632
633 uint8_t capabilities[] = {
634 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700635 res = info.update(ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
636 capabilities, ARRAY_SIZE(capabilities));
637 if (res != android::OK) {
638 return res;
639 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700640
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700641 // Scan a default request template for included request keys.
642 if (!mTemplatesInitialized) {
643 res = initTemplates();
644 if (res) {
645 return res;
646 }
647 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700648 const camera_metadata_t* preview_request = nullptr;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700649 // Search templates from the beginning for a supported one.
650 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
651 ++template_id) {
652 preview_request = constructDefaultRequestSettings(template_id);
653 if (preview_request != nullptr) {
654 break;
655 }
656 }
657 if (preview_request == nullptr) {
658 HAL_LOGE("No valid templates, can't get request keys.");
659 return -ENODEV;
660 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700661 std::vector<int32_t> avail_request_keys = getMetadataKeys(preview_request);
662 res = info.update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
663 avail_request_keys.data(), avail_request_keys.size());
664 if (res != android::OK) {
665 return res;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700666 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700667
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700668 // Result keys will be duplicated from the request, plus a few extras.
669 // TODO(b/29335262): additonal available result keys.
670 std::vector<int32_t> avail_result_keys(avail_request_keys);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700671 res = info.update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
672 avail_result_keys.data(), avail_result_keys.size());
673 if (res != android::OK) {
674 return res;
675 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700676
677 // Last thing, once all the available characteristics have been added.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700678 const camera_metadata_t* static_characteristics = info.getAndLock();
679 std::vector<int32_t> avail_characteristics_keys =
680 getMetadataKeys(static_characteristics);
681 res = info.unlock(static_characteristics);
682 if (res != android::OK) {
683 return res;
684 }
685 avail_characteristics_keys.push_back(
686 ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
687 res = info.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
688 avail_characteristics_keys.data(),
689 avail_characteristics_keys.size());
690 if (res != android::OK) {
691 return res;
692 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700693
694 *out = info.release();
695 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700696}
697
698void V4L2Camera::initDeviceInfo(camera_info_t* info) {
699 HAL_LOG_ENTER();
700
701 // For now, just constants.
702 info->facing = CAMERA_FACING_EXTERNAL;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700703 info->orientation = mOrientation;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700704 info->resource_cost = 100;
705 info->conflicting_devices = nullptr;
706 info->conflicting_devices_length = 0;
707}
708
709int V4L2Camera::initDevice() {
710 HAL_LOG_ENTER();
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700711 int res;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700712
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700713 // Templates should be set up if they haven't already been.
714 if (!mTemplatesInitialized) {
715 res = initTemplates();
716 if (res) {
717 return res;
718 }
719 }
720
721 return 0;
722}
723
724int V4L2Camera::initTemplates() {
725 HAL_LOG_ENTER();
726 int res;
727
728 // Device characteristics need to be queried prior
729 // to template setup.
730 if (!mCharacteristicsInitialized) {
731 res = initCharacteristics();
732 if (res) {
733 return res;
734 }
735 }
736
737 // Note: static metadata expects all templates/requests
738 // to provide values for all supported keys.
739
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700740 android::CameraMetadata base_metadata;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700741
742 // Start with defaults for all templates.
743
744 /* android.colorCorrection. */
745
746 uint8_t aberration_mode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700747 res = base_metadata.update(ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
748 &aberration_mode, 1);
749 if (res != android::OK) {
750 return res;
751 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700752
753 uint8_t color_correction_mode = ANDROID_COLOR_CORRECTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700754 res = base_metadata.update(ANDROID_COLOR_CORRECTION_MODE,
755 &color_correction_mode, 1);
756 if (res != android::OK) {
757 return res;
758 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700759
760 // transform and gains are for the unsupported MANUAL_POST_PROCESSING only.
761
762 /* android.control. */
763
764 /* AE. */
765 uint8_t ae_antibanding_mode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700766 res = base_metadata.update(ANDROID_CONTROL_AE_ANTIBANDING_MODE,
767 &ae_antibanding_mode, 1);
768 if (res != android::OK) {
769 return res;
770 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700771
772 // Only matters if AE_MODE = OFF
773 int32_t ae_exposure_compensation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700774 res = base_metadata.update(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
775 &ae_exposure_compensation, 1);
776 if (res != android::OK) {
777 return res;
778 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700779
780 uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700781 res = base_metadata.update(ANDROID_CONTROL_AE_LOCK, &ae_lock, 1);
782 if (res != android::OK) {
783 return res;
784 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700785
786 uint8_t ae_mode = ANDROID_CONTROL_AE_MODE_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700787 res = base_metadata.update(ANDROID_CONTROL_AE_MODE, &ae_mode, 1);
788 if (res != android::OK) {
789 return res;
790 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700791
792 // AE regions not supported.
793
794 // FPS set per-template.
795
796 uint8_t ae_precapture_trigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700797 res = base_metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
798 &ae_precapture_trigger, 1);
799 if (res != android::OK) {
800 return res;
801 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700802
803 /* AF. */
804
805 // AF mode set per-template.
806
807 // AF regions not supported.
808
809 uint8_t af_trigger = ANDROID_CONTROL_AF_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700810 res = base_metadata.update(ANDROID_CONTROL_AF_TRIGGER, &af_trigger, 1);
811 if (res != android::OK) {
812 return res;
813 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700814
815 /* AWB. */
816
817 // Priority: auto > off > Whatever is available.
818 uint8_t default_awb_mode = mAwbModes[0];
819 if (std::count(mAwbModes.begin(), mAwbModes.end(),
820 ANDROID_CONTROL_AWB_MODE_AUTO)) {
821 default_awb_mode = ANDROID_CONTROL_AWB_MODE_AUTO;
822 } else if (std::count(mAwbModes.begin(), mAwbModes.end(),
823 ANDROID_CONTROL_AWB_MODE_OFF)) {
824 default_awb_mode = ANDROID_CONTROL_AWB_MODE_OFF;
825 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700826 res = base_metadata.update(ANDROID_CONTROL_AWB_MODE, &default_awb_mode, 1);
827 if (res != android::OK) {
828 return res;
829 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700830
831 // AWB regions not supported.
832
833 /* Other controls. */
834
835 uint8_t effect_mode = ANDROID_CONTROL_EFFECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700836 res = base_metadata.update(ANDROID_CONTROL_EFFECT_MODE, &effect_mode, 1);
837 if (res != android::OK) {
838 return res;
839 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700840
841 uint8_t control_mode = ANDROID_CONTROL_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700842 res = base_metadata.update(ANDROID_CONTROL_MODE, &control_mode, 1);
843 if (res != android::OK) {
844 return res;
845 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700846
847 uint8_t scene_mode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700848 res = base_metadata.update(ANDROID_CONTROL_SCENE_MODE,
849 &scene_mode, 1);
850 if (res != android::OK) {
851 return res;
852 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700853
854 uint8_t video_stabilization = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700855 res = base_metadata.update(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
856 &video_stabilization, 1);
857 if (res != android::OK) {
858 return res;
859 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700860
861 // postRawSensitivityBoost: RAW not supported, leave null.
862
863 /* android.demosaic. */
864
865 // mode marked FUTURE.
866
867 /* android.edge. */
868
869 uint8_t edge_mode = ANDROID_EDGE_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700870 res = base_metadata.update(ANDROID_EDGE_MODE, &edge_mode, 1);
871 if (res != android::OK) {
872 return res;
873 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700874
875 // strength marked FUTURE.
876
877 /* android.flash. */
878
879 // firingPower, firingTime marked FUTURE.
880
881 uint8_t flash_mode = ANDROID_FLASH_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700882 res = base_metadata.update(ANDROID_FLASH_MODE, &flash_mode, 1);
883 if (res != android::OK) {
884 return res;
885 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700886
887 /* android.hotPixel. */
888
889 uint8_t hp_mode = ANDROID_HOT_PIXEL_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700890 res = base_metadata.update(ANDROID_HOT_PIXEL_MODE, &hp_mode, 1);
891 if (res != android::OK) {
892 return res;
893 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700894
895 /* android.jpeg. */
896
897 double gps_coords[] = {/*latitude*/0, /*longitude*/0, /*altitude*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700898 res = base_metadata.update(ANDROID_JPEG_GPS_COORDINATES, gps_coords, 3);
899 if (res != android::OK) {
900 return res;
901 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700902
903 uint8_t gps_processing_method[] = "none";
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700904 res = base_metadata.update(ANDROID_JPEG_GPS_PROCESSING_METHOD,
905 gps_processing_method,
906 ARRAY_SIZE(gps_processing_method));
907 if (res != android::OK) {
908 return res;
909 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700910
911 int64_t gps_timestamp = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700912 res = base_metadata.update(ANDROID_JPEG_GPS_TIMESTAMP, &gps_timestamp, 1);
913 if (res != android::OK) {
914 return res;
915 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700916
917 // JPEG orientation is relative to sensor orientation (mOrientation).
918 int32_t jpeg_orientation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700919 res = base_metadata.update(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1);
920 if (res != android::OK) {
921 return res;
922 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700923
924 // 1-100, larger is higher quality.
925 uint8_t jpeg_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700926 res = base_metadata.update(ANDROID_JPEG_QUALITY, &jpeg_quality, 1);
927 if (res != android::OK) {
928 return res;
929 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700930
931 // TODO(b/29580107): If thumbnail quality actually matters/can be adjusted,
932 // adjust this.
933 uint8_t thumbnail_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700934 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_QUALITY,
935 &thumbnail_quality, 1);
936 if (res != android::OK) {
937 return res;
938 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700939
940 // TODO(b/29580107): Choose a size matching the resolution.
941 int32_t thumbnail_size[] = {0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700942 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_SIZE, thumbnail_size, 2);
943 if (res != android::OK) {
944 return res;
945 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700946
947 /* android.lens. */
948
949 // Fixed values.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700950 res = base_metadata.update(ANDROID_LENS_APERTURE, &mAperture, 1);
951 if (res != android::OK) {
952 return res;
953 }
954 res = base_metadata.update(ANDROID_LENS_FILTER_DENSITY, &mFilterDensity, 1);
955 if (res != android::OK) {
956 return res;
957 }
958 res = base_metadata.update(ANDROID_LENS_FOCAL_LENGTH, &mFocalLength, 1);
959 if (res != android::OK) {
960 return res;
961 }
962 res = base_metadata.update(ANDROID_LENS_FOCUS_DISTANCE, &mFocusDistance, 1);
963 if (res != android::OK) {
964 return res;
965 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700966
967 uint8_t optical_stabilization = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700968 res = base_metadata.update(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700969 &optical_stabilization, 1);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700970 if (res != android::OK) {
971 return res;
972 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700973
974 /* android.noiseReduction. */
975
976 uint8_t noise_reduction_mode = ANDROID_NOISE_REDUCTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700977 res = base_metadata.update(ANDROID_NOISE_REDUCTION_MODE,
978 &noise_reduction_mode, 1);
979 if (res != android::OK) {
980 return res;
981 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700982
983 // strength marked FUTURE.
984
985 /* android.request. */
986
987 // Request Id unused by the HAL for now, and these are just
988 // templates, so just fill it in with a dummy.
989 int32_t id = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700990 res = base_metadata.update(ANDROID_REQUEST_ID, &id, 1);
991 if (res != android::OK) {
992 return res;
993 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700994
995 // metadataMode marked FUTURE.
996
997 /* android.scaler. */
998
999 // No cropping by default; use the full active array.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001000 res = base_metadata.update(ANDROID_SCALER_CROP_REGION, mPixelArraySize.data(),
1001 mPixelArraySize.size());
1002 if (res != android::OK) {
1003 return res;
1004 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001005
1006 /* android.sensor. */
1007
1008 // exposureTime, sensitivity, testPattern[Data,Mode] not supported.
1009
1010 // Ignored when AE is OFF.
1011 int64_t frame_duration = 33333333L; // 1/30 s.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001012 res = base_metadata.update(ANDROID_SENSOR_FRAME_DURATION, &frame_duration, 1);
1013 if (res != android::OK) {
1014 return res;
1015 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001016
1017 /* android.shading. */
1018
1019 uint8_t shading_mode = ANDROID_SHADING_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001020 res = base_metadata.update(ANDROID_SHADING_MODE, &shading_mode, 1);
1021 if (res != android::OK) {
1022 return res;
1023 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001024
1025 /* android.statistics. */
1026
1027 uint8_t face_detect_mode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001028 res = base_metadata.update(ANDROID_STATISTICS_FACE_DETECT_MODE,
1029 &face_detect_mode, 1);
1030 if (res != android::OK) {
1031 return res;
1032 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001033
1034 // histogramMode, sharpnessMapMode marked FUTURE.
1035
1036 uint8_t hp_map_mode = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001037 res = base_metadata.update(ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
1038 &hp_map_mode, 1);
1039 if (res != android::OK) {
1040 return res;
1041 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001042
1043 uint8_t lens_shading_map_mode = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001044 res = base_metadata.update(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
1045 &lens_shading_map_mode, 1);
1046 if (res != android::OK) {
1047 return res;
1048 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001049
1050 /* android.tonemap. */
1051
1052 // Tonemap only required for MANUAL_POST_PROCESSING capability.
1053
1054 /* android.led. */
1055
1056 uint8_t transmit = ANDROID_LED_TRANSMIT_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001057 res = base_metadata.update(ANDROID_LED_TRANSMIT, &transmit, 1);
1058 if (res != android::OK) {
1059 return res;
1060 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001061
1062 /* android.reprocess */
1063
1064 // Only needed for REPROCESS capability.
1065
1066
1067 /* Template variable values. */
1068
1069 // Find the FPS ranges "closest" to a desired range
1070 // (minimum abs distance from min to min and max to max).
1071 // Find both a fixed rate and a variable rate, for different purposes.
1072 std::array<int32_t, 2> desired_flat_fps_range = {{30, 30}};
1073 std::array<int32_t, 2> desired_variable_fps_range = {{5, 30}};
1074 std::array<int32_t, 2> flat_fps_range;
1075 std::array<int32_t, 2> variable_fps_range;
1076 int32_t best_flat_distance = std::numeric_limits<int32_t>::max();
1077 int32_t best_variable_distance = std::numeric_limits<int32_t>::max();
1078 size_t num_fps_ranges = mFpsRanges.num_arrays();
1079 for (size_t i = 0; i < num_fps_ranges; ++i) {
1080 const int32_t* range = mFpsRanges[i];
1081 // Variable fps.
1082 int32_t distance = std::abs(range[0] - desired_variable_fps_range[0]) +
1083 std::abs(range[1] - desired_variable_fps_range[1]);
1084 if (distance < best_variable_distance) {
1085 variable_fps_range[0] = range[0];
1086 variable_fps_range[1] = range[1];
1087 best_variable_distance = distance;
1088 }
1089 // Flat fps. Only do if range is actually flat.
1090 // Note at least one flat range is required,
1091 // so something will always be filled in.
1092 if (range[0] == range[1]) {
1093 distance = std::abs(range[0] - desired_flat_fps_range[0]) +
1094 std::abs(range[1] - desired_flat_fps_range[1]);
1095 if (distance < best_flat_distance) {
1096 flat_fps_range[0] = range[0];
1097 flat_fps_range[1] = range[1];
1098 best_flat_distance = distance;
1099 }
1100 }
1101 }
1102
1103 // Priority: continuous > auto > off > whatever is available.
1104 bool continuous_still_avail = std::count(
1105 mAfModes.begin(), mAfModes.end(),
1106 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE);
1107 bool continuous_video_avail = std::count(
1108 mAfModes.begin(), mAfModes.end(),
1109 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO);
1110 uint8_t non_continuous_af_mode = mAfModes[0];
1111 if (std::count(mAfModes.begin(), mAfModes.end(),
1112 ANDROID_CONTROL_AF_MODE_AUTO)) {
1113 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_AUTO;
1114 } else if (std::count(mAfModes.begin(), mAfModes.end(),
1115 ANDROID_CONTROL_AF_MODE_OFF)) {
1116 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_OFF;
1117 }
1118 uint8_t still_af_mode = continuous_still_avail ?
1119 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE : non_continuous_af_mode;
1120 uint8_t video_af_mode = continuous_video_avail ?
1121 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO : non_continuous_af_mode;
1122
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001123 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
1124 ++template_id) {
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001125 // General differences/support.
1126 uint8_t intent;
1127 uint8_t af_mode;
1128 std::array<int32_t, 2> fps_range;
1129 switch(template_id) {
1130 case CAMERA3_TEMPLATE_PREVIEW:
1131 intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
1132 af_mode = still_af_mode;
1133 fps_range = flat_fps_range;
1134 break;
1135 case CAMERA3_TEMPLATE_STILL_CAPTURE:
1136 intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
1137 af_mode = still_af_mode;
1138 fps_range = variable_fps_range;
1139 break;
1140 case CAMERA3_TEMPLATE_VIDEO_RECORD:
1141 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
1142 af_mode = video_af_mode;
1143 fps_range = flat_fps_range;
1144 break;
1145 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
1146 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
1147 af_mode = video_af_mode;
1148 fps_range = flat_fps_range;
1149 break;
1150 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: // Fall through.
1151 case CAMERA3_TEMPLATE_MANUAL: // Fall though.
1152 default:
1153 // Unsupported/unrecognized. Don't add this template; skip it.
1154 continue;
1155 }
1156
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001157 // Copy our base metadata and add the new items.
1158 android::CameraMetadata template_metadata(base_metadata);
1159 res = template_metadata.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent, 1);
1160 if (res != android::OK) {
1161 return res;
1162 }
1163 res = template_metadata.update(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
1164 fps_range.data(), fps_range.size());
1165 if (res != android::OK) {
1166 return res;
1167 }
1168 res = template_metadata.update(ANDROID_CONTROL_AF_MODE, &af_mode, 1);
1169 if (res != android::OK) {
1170 return res;
1171 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001172
1173 const camera_metadata_t* template_raw_metadata =
1174 template_metadata.getAndLock();
1175 res = setTemplate(template_id, template_raw_metadata);
1176 if (res != android::OK) {
1177 return res;
1178 }
1179 res = template_metadata.unlock(template_raw_metadata);
1180 if (res != android::OK) {
1181 return res;
1182 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001183 }
1184
1185 mTemplatesInitialized = true;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001186 return 0;
1187}
1188
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001189bool V4L2Camera::isSupportedStreamSet(default_camera_hal::Stream** streams,
1190 int count, uint32_t mode) {
1191 HAL_LOG_ENTER();
1192
1193 if (mode != CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE) {
1194 HAL_LOGE("Unsupported stream configuration mode: %d", mode);
1195 return false;
1196 }
1197
1198 // This should be checked by the caller, but put here as a sanity check.
1199 if (count < 1) {
1200 HAL_LOGE("Must request at least 1 stream");
1201 return false;
1202 }
1203
1204 // Count the number of streams of each type.
1205 int32_t num_input = 0;
1206 int32_t num_raw = 0;
1207 int32_t num_yuv = 0;
1208 int32_t num_jpeg = 0;
1209 for (int i = 0; i < count; ++i) {
1210 default_camera_hal::Stream* stream = streams[i];
1211
1212 if (stream->isInputType()) {
1213 ++num_input;
1214 }
1215
1216 if (stream->isOutputType()) {
1217 switch (halToV4L2PixelFormat(stream->getFormat())) {
1218 case V4L2_PIX_FMT_YUV420:
1219 ++num_yuv;
1220 break;
1221 case V4L2_PIX_FMT_JPEG:
1222 ++num_jpeg;
1223 break;
1224 default:
1225 // Note: no supported raw formats at this time.
1226 HAL_LOGE("Unsupported format for stream %d: %d", i, stream->getFormat());
1227 return false;
1228 }
1229 }
1230 }
1231
1232 if (num_input > mMaxInputStreams || num_raw > mMaxRawOutputStreams ||
1233 num_yuv > mMaxYuvOutputStreams || num_jpeg > mMaxJpegOutputStreams) {
1234 HAL_LOGE("Invalid stream configuration: %d input, %d RAW, %d YUV, %d JPEG "
1235 "(max supported: %d input, %d RAW, %d YUV, %d JPEG)",
1236 mMaxInputStreams, mMaxRawOutputStreams, mMaxYuvOutputStreams,
1237 mMaxJpegOutputStreams, num_input, num_raw, num_yuv, num_jpeg);
1238 return false;
1239 }
1240
1241 // TODO(b/29939583): The above logic should be all that's necessary,
1242 // but V4L2 doesn't actually support more than 1 stream at a time. So for now,
1243 // if not all streams are the same format and size, error. Note that this
1244 // means the HAL is not spec-compliant; the requested streams are technically
1245 // valid and it is not technically allowed to error once it has reached this
1246 // point.
1247 int format = streams[0]->getFormat();
1248 uint32_t width = streams[0]->getWidth();
1249 uint32_t height = streams[0]->getHeight();
1250 for (int i = 1; i < count; ++i) {
1251 const default_camera_hal::Stream* stream = streams[i];
1252 if (stream->getFormat() != format || stream->getWidth() != width ||
1253 stream->getHeight() != height) {
1254 HAL_LOGE("V4L2 only supports 1 stream configuration at a time "
1255 "(stream 0 is format %d, width %u, height %u, "
1256 "stream %d is format %d, width %u, height %u).",
1257 format, width, height, i, stream->getFormat(),
1258 stream->getWidth(), stream->getHeight());
1259 return false;
1260 }
1261 }
1262
1263 return true;
1264}
1265
1266int V4L2Camera::setupStream(default_camera_hal::Stream* stream,
1267 uint32_t* max_buffers) {
1268 HAL_LOG_ENTER();
1269
1270 if (stream->getRotation() != CAMERA3_STREAM_ROTATION_0) {
1271 HAL_LOGE("Rotation %d not supported", stream->getRotation());
1272 return -EINVAL;
1273 }
1274
1275 // Doesn't matter what was requested, we always use dataspace V0_JFIF.
1276 // Note: according to camera3.h, this isn't allowed, but etalvala@google.com
1277 // claims it's underdocumented; the implementation lets the HAL overwrite it.
1278 stream->setDataSpace(HAL_DATASPACE_V0_JFIF);
1279
1280 int ret = setFormat(stream);
1281 if (ret) {
1282 return ret;
1283 }
1284
1285 // Only do buffer setup if we don't know our maxBuffers.
1286 if (mOutStreamMaxBuffers < 1) {
1287 ret = setupBuffers();
1288 if (ret) {
1289 return ret;
1290 }
1291 }
1292
1293 // Sanity check.
1294 if (mOutStreamMaxBuffers < 1) {
1295 HAL_LOGE("HAL failed to determine max number of buffers.");
1296 return -ENODEV;
1297 }
1298 *max_buffers = mOutStreamMaxBuffers;
1299
1300 return 0;
1301}
1302
1303int V4L2Camera::setFormat(const default_camera_hal::Stream* stream) {
1304 HAL_LOG_ENTER();
1305
1306 // Should be checked earlier; sanity check.
1307 if (stream->isInputType()) {
1308 HAL_LOGE("Input streams not supported.");
1309 return -EINVAL;
1310 }
1311
1312 v4l2_format format;
1313 memset(&format, 0, sizeof(format));
1314 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1315 format.fmt.pix.width = stream->getWidth();
1316 format.fmt.pix.height = stream->getHeight();
1317 format.fmt.pix.pixelformat = halToV4L2PixelFormat(stream->getFormat());
1318 // Check to make sure we're not already in the correct format.
1319 if (mOutStreamFormat == format.fmt.pix.pixelformat &&
1320 mOutStreamWidth == format.fmt.pix.width &&
1321 mOutStreamHeight == format.fmt.pix.height) {
1322 return 0;
1323 }
1324 // Not in the correct format, set our format.
1325 int ret = ioctlLocked(VIDIOC_S_FMT, &format);
1326 if (ret < 0) {
1327 HAL_LOGE("S_FMT failed: %s", strerror(errno));
1328 return -ENODEV;
1329 }
1330 // Check that the driver actually set to the requested values.
1331 if (format.fmt.pix.pixelformat != halToV4L2PixelFormat(stream->getFormat()) ||
1332 format.fmt.pix.width != stream->getWidth() ||
1333 format.fmt.pix.height != stream->getHeight()) {
1334 HAL_LOGE("Device doesn't support desired stream configuration.");
1335 return -EINVAL;
1336 }
1337 // Since our format changed, our maxBuffers may be incorrect.
1338 mOutStreamMaxBuffers = 0;
1339
1340 return 0;
1341}
1342
1343int V4L2Camera::setupBuffers() {
1344 HAL_LOG_ENTER();
1345
1346 // "Request" a buffer (since we're using a userspace buffer, this just
1347 // tells V4L2 to switch into userspace buffer mode).
1348 v4l2_requestbuffers req_buffers;
1349 memset(&req_buffers, 0, sizeof(req_buffers));
1350 req_buffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1351 req_buffers.memory = V4L2_MEMORY_USERPTR;
1352 req_buffers.count = 1;
1353 if (ioctlLocked(VIDIOC_REQBUFS, &req_buffers) < 0) {
1354 HAL_LOGE("REQBUFS failed: %s", strerror(errno));
1355 return -ENODEV;
1356 }
1357
1358 // V4L2 will set req_buffers.count to a number of buffers it can handle.
1359 mOutStreamMaxBuffers = req_buffers.count;
1360 return 0;
1361}
1362
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001363bool V4L2Camera::isValidCaptureSettings(const camera_metadata_t* settings) {
1364 HAL_LOG_ENTER();
1365
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001366 // TODO(b/29335262): reject capture settings this camera isn't capable of.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001367 return true;
1368}
1369
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001370int V4L2Camera::initCharacteristics() {
1371 HAL_LOG_ENTER();
1372
1373 /* Physical characteristics. */
1374 // No way to get these in V4L2, so faked.
1375 // Note: While many of these are primarily informative for post-processing
1376 // calculations by the app and will potentially cause bad results there,
1377 // focal length and physical size are actually used in framework
1378 // calculations (field of view, pixel pitch, etc), so faking them may
1379 // have unexpected results.
1380 mAperture = 2.0; // RPi camera v2 is f/2.0.
1381 mFilterDensity = 0.0;
1382 mFocalLength = 3.04; // RPi camera v2 is 3.04mm.
1383 mOrientation = 0;
1384 mPhysicalSize = {{3.674, 2.760}}; // RPi camera v2 is 3.674 x 2.760 mm.
1385
1386 /* Fixed features. */
1387
1388 // TODO(b/29394024): query VIDIOC_CROPCAP to get pixel rectangle.
1389 // Spoofing as 640 x 480 for now.
1390 mPixelArraySize = {{/*xmin*/0, /*ymin*/0, /*width*/640, /*height*/480}};
1391
1392 // V4L2 VIDIOC_CROPCAP doesn't give a way to query this;
1393 // it's driver dependent. For now, assume freeform, and
1394 // some cameras may just behave badly.
1395 // TODO(b/29579652): Figure out a way to determine this.
1396 mCropType = ANDROID_SCALER_CROPPING_TYPE_FREEFORM;
1397
1398 // TODO(b/29394024): query VIDIOC_CROPCAP to get cropping ranges,
1399 // and VIDIOC_G_CROP to determine if cropping is supported.
1400 // If the ioctl isn't available (or cropping has non-square pixelaspect),
1401 // assume no cropping/scaling.
1402 // May need to try setting some crops to determine what the driver actually
1403 // supports (including testing center vs freeform).
1404 mMaxZoom = 1;
1405
1406 // TODO(b/29394024): query V4L2_CID_EXPOSURE_BIAS.
1407 mAeCompensationRange = {{0, 0}};
1408 mAeCompensationStep = {1, 1};
1409
1410 // TODO(b/29394024): query V4L2_CID_3A_LOCK.
1411 mAeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE;
1412 mAwbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE;
1413
1414 // TODO(b/29394024): query V4L2_CID_FLASH_LED_MODE.
1415 mFlashAvailable = 0;
1416
1417 // TODO(b/29394024): query V4L2_CID_FOCUS_ABSOLUTE for focus range.
1418 mFocusDistance = 0; // Fixed focus.
1419
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001420 // TODO(b/29939583): V4L2 can only support 1 stream at a time.
1421 // For now, just reporting minimum allowable for LIMITED devices.
1422 mMaxRawOutputStreams = 0;
1423 mMaxYuvOutputStreams = 2;
1424 mMaxJpegOutputStreams = 1;
1425 // Reprocessing not supported.
1426 mMaxInputStreams = 0;
1427
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001428 /* Features with (potentially) multiple options. */
1429
1430 // TODO(b/29394024): query V4L2_CID_EXPOSURE_AUTO for ae modes.
1431 mAeModes.push_back(ANDROID_CONTROL_AE_MODE_ON);
1432
1433 // TODO(b/29394024): query V4L2_CID_POWER_LINE_FREQUENCY.
1434 // Auto as the default, since it could mean anything, while OFF would
1435 // require guaranteeing no antibanding happens.
1436 mAeAntibandingModes.push_back(ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO);
1437
1438 // TODO(b/29394024): query V4L2_CID_FOCUS_AUTO for
1439 // CONTINUOUS_VIDEO/CONTINUOUS_PICTURE. V4L2_CID_AUTO_FOCUS_START
1440 // supports what Android thinks of as auto focus (single auto focus).
1441 // V4L2_CID_AUTO_FOCUS_RANGE allows MACRO.
1442 mAfModes.push_back(ANDROID_CONTROL_AF_MODE_OFF);
1443
1444 // TODO(b/29394024): query V4L2_CID_AUTO_WHITE_BALANCE, or
1445 // V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE if available.
1446 mAwbModes.push_back(ANDROID_CONTROL_AWB_MODE_AUTO);
1447
1448 // TODO(b/29394024): query V4L2_CID_SCENE_MODE.
1449 mSceneModes.push_back(ANDROID_CONTROL_SCENE_MODE_DISABLED);
1450
1451 mControlModes.push_back(ANDROID_CONTROL_MODE_AUTO);
1452 if (mSceneModes.size() > 1) {
1453 // We have some mode other than just DISABLED available.
1454 mControlModes.push_back(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
1455 }
1456
1457 // TODO(b/29394024): query V4L2_CID_COLORFX.
1458 mEffects.push_back(ANDROID_CONTROL_EFFECT_MODE_OFF);
1459
1460 // TODO(b/29394024): query V4L2_CID_FLASH_INDICATOR_INTENSITY.
1461 // For now, no indicator LED available; nothing to push back.
1462 // When there is, push back ANDROID_LED_AVAILABLE_LEDS_TRANSMIT.
1463
1464 // TODO(b/29394024): query V4L2_CID_IMAGE_STABILIZATION.
1465 mOpticalStabilizationModes.push_back(
1466 ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF);
1467 mVideoStabilizationModes.push_back(
1468 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF);
1469
1470 // Need to support YUV_420_888 and JPEG.
1471 // TODO(b/29394024): query VIDIOC_ENUM_FMT.
1472 std::vector<uint32_t> formats = {{V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420}};
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001473 // We want to find the smallest maximum frame duration amongst formats.
1474 mMaxFrameDuration = std::numeric_limits<int64_t>::max();
1475 int64_t min_yuv_frame_duration = std::numeric_limits<int64_t>::max();
1476 for (auto format : formats) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001477 int32_t hal_format = V4L2ToHalPixelFormat(format);
1478 if (hal_format < 0) {
1479 // Unrecognized/unused format. Skip it.
1480 continue;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001481 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001482 // TODO(b/29394024): query VIDIOC_ENUM_FRAMESIZES.
1483 // For now, just 640x480.
1484 ArrayVector<int32_t, 2> frame_sizes;
1485 frame_sizes.push_back({{640, 480}});
1486 size_t num_frame_sizes = frame_sizes.num_arrays();
1487 for (size_t i = 0; i < num_frame_sizes; ++i) {
1488 const int32_t* frame_size = frame_sizes[i];
1489 mStreamConfigs.push_back({{hal_format, frame_size[0], frame_size[1],
1490 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT}});
1491
1492 // TODO(b/29394024): query VIDIOC_ENUM_FRAMEINTERVALS.
1493 // For now, using the emulator max value of .3 sec.
1494 int64_t max_frame_duration = 300000000;
1495 // For now, min value of 30 fps = 1/30 spf ~= 33333333 ns.
1496 // For whatever reason the goldfish/qcom cameras report this as
1497 // 33331760, so copying that.
1498 int64_t min_frame_duration = 33331760;
1499
1500 mMinFrameDurations.push_back(
1501 {{hal_format, frame_size[0], frame_size[1], min_frame_duration}});
1502
1503 // In theory max frame duration (min frame rate) should be consistent
1504 // between all formats, but we check and only advertise the smallest
1505 // available max duration just in case.
1506 if (max_frame_duration < mMaxFrameDuration) {
1507 mMaxFrameDuration = max_frame_duration;
1508 }
1509
1510 // We only care about min frame duration (max frame rate) for YUV.
1511 if (hal_format == HAL_PIXEL_FORMAT_YCbCr_420_888 &&
1512 min_frame_duration < min_yuv_frame_duration) {
1513 min_yuv_frame_duration = min_frame_duration;
1514 }
1515
1516 // Usually 0 for non-jpeg, non-zero for JPEG.
1517 // Randomly choosing absurd 1 sec for JPEG. Unsure what this breaks.
1518 int64_t stall_duration = 0;
1519 if (hal_format == HAL_PIXEL_FORMAT_BLOB) {
1520 stall_duration = 1000000000;
1521 }
1522 mStallDurations.push_back(
1523 {{hal_format, frame_size[0], frame_size[1], stall_duration}});
1524 }
1525 }
1526
1527 // This should be at minimum {mi, ma}, {ma, ma} where mi and ma
1528 // are min and max frame rates for YUV_420_888. Min should be at most 15.
1529 // Convert from frame durations measured in ns.
1530 int32_t min_yuv_fps = 1000000000 / mMaxFrameDuration;
1531 if (min_yuv_fps > 15) {
1532 return -ENODEV;
1533 }
1534 int32_t max_yuv_fps = 1000000000 / min_yuv_frame_duration;
1535 mFpsRanges.push_back({{min_yuv_fps, max_yuv_fps}});
1536 mFpsRanges.push_back({{max_yuv_fps, max_yuv_fps}});
1537 // Always advertise {30, 30} if max is even higher,
1538 // since this is what the default video requests use.
1539 if (max_yuv_fps > 30) {
1540 mFpsRanges.push_back({{30, 30}});
1541 }
1542
1543 mCharacteristicsInitialized = true;
1544 return 0;
1545}
1546
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001547int V4L2Camera::V4L2ToHalPixelFormat(uint32_t v4l2_format) {
1548 // Translate V4L2 format to HAL format.
1549 int hal_format = -1;
1550 switch (v4l2_format) {
1551 case V4L2_PIX_FMT_JPEG:
1552 hal_format = HAL_PIXEL_FORMAT_BLOB;
1553 break;
1554 case V4L2_PIX_FMT_YUV420:
1555 hal_format = HAL_PIXEL_FORMAT_YCbCr_420_888;
1556 break;
1557 default:
1558 // Unrecognized format.
1559 break;
1560 }
1561 return hal_format;
1562}
1563
1564uint32_t V4L2Camera::halToV4L2PixelFormat(int hal_format) {
1565 // Translate HAL format to V4L2 format.
1566 uint32_t v4l2_format = 0;
1567 switch (hal_format) {
1568 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: // fall-through.
1569 case HAL_PIXEL_FORMAT_YCbCr_420_888:
1570 v4l2_format = V4L2_PIX_FMT_YUV420;
1571 break;
1572 case HAL_PIXEL_FORMAT_BLOB:
1573 v4l2_format = V4L2_PIX_FMT_JPEG;
1574 break;
1575 default:
1576 // Unrecognized format.
1577 break;
1578 }
1579 return v4l2_format;
1580}
1581
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001582} // namespace v4l2_camera_hal