blob: e94a56120806b97cadd048f3349cd4af70359509 [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-Cohen44d84322016-07-11 11:08:26 -070052 mOutStreamType(0),
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070053 mOutStreamFormat(0),
54 mOutStreamWidth(0),
55 mOutStreamHeight(0),
56 mOutStreamMaxBuffers(0),
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070057 mTemplatesInitialized(false),
58 mCharacteristicsInitialized(false) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070059 HAL_LOG_ENTER();
60}
61
62V4L2Camera::~V4L2Camera() {
63 HAL_LOG_ENTER();
64}
65
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070066// Helper function. Should be used instead of ioctl throughout this class.
67template<typename T>
68int V4L2Camera::ioctlLocked(int request, T data) {
69 android::Mutex::Autolock al(mDeviceLock);
70 if (mDeviceFd.get() < 0) {
71 return -ENODEV;
72 }
73 return TEMP_FAILURE_RETRY(ioctl(mDeviceFd.get(), request, data));
74}
75
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070076int V4L2Camera::connect() {
77 HAL_LOG_ENTER();
78
79 if (mDeviceFd.get() >= 0) {
80 HAL_LOGE("Camera device %s is opened. Close it first", mDevicePath.c_str());
81 return -EIO;
82 }
83
84 int fd = TEMP_FAILURE_RETRY(open(mDevicePath.c_str(), O_RDWR));
85 if (fd < 0) {
86 HAL_LOGE("failed to open %s (%s)", mDevicePath.c_str(), strerror(errno));
87 return -errno;
88 }
89 mDeviceFd.reset(fd);
90
91 // TODO(b/29185945): confirm this is a supported device.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070092 // This is checked by the HAL, but the device at mDevicePath may
93 // not be the same one that was there when the HAL was loaded.
94 // (Alternatively, better hotplugging support may make this unecessary
95 // by disabling cameras that get disconnected and checking newly connected
96 // cameras, so connect() is never called on an unsupported camera)
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070097
98 // TODO(b/29158098): Inform service of any flashes that are no longer available
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070099 // because this camera is in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700100 return 0;
101}
102
103void V4L2Camera::disconnect() {
104 HAL_LOG_ENTER();
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700105 // TODO(b/29158098): Inform service of any flashes that are available again
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700106 // because this camera is no longer in use.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700107
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700108 mDeviceFd.reset();
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -0700109
110 // Clear out our variables tracking device settings.
111 mOutStreamType = 0;
112 mOutStreamFormat = 0;
113 mOutStreamWidth = 0;
114 mOutStreamHeight = 0;
115 mOutStreamMaxBuffers = 0;
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700116}
117
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700118int V4L2Camera::initStaticInfo(camera_metadata_t** out) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700119 HAL_LOG_ENTER();
120
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700121 android::status_t res;
122 // Device characteristics need to be queried prior
123 // to static info setup.
124 if (!mCharacteristicsInitialized) {
125 res = initCharacteristics();
126 if (res) {
127 return res;
128 }
129 }
130
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700131 android::CameraMetadata info;
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -0700132
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700133 // Static metadata characteristics from /system/media/camera/docs/docs.html.
134
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700135 /* android.colorCorrection. */
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700136
137 // No easy way to turn chromatic aberration correction OFF in v4l2,
138 // though this may be supportable via a collection of other user controls.
139 uint8_t avail_aberration_modes[] = {
140 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST,
141 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700142 res = info.update(ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
143 avail_aberration_modes, ARRAY_SIZE(avail_aberration_modes));
144 if (res != android::OK) {
145 return res;
146 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700147
148 /* android.control. */
149
150 /* 3As */
151
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700152 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
153 mAeAntibandingModes.data(), mAeAntibandingModes.size());
154 if (res != android::OK) {
155 return res;
156 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700157
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700158 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_MODES,
159 mAeModes.data(), mAeModes.size());
160 if (res != android::OK) {
161 return res;
162 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700163
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700164 // Flatten mFpsRanges.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700165 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
166 mFpsRanges.data(), mFpsRanges.total_num_elements());
167 if (res != android::OK) {
168 return res;
169 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700170
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700171 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
172 mAeCompensationRange.data(), mAeCompensationRange.size());
173 if (res != android::OK) {
174 return res;
175 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700176
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700177 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_STEP,
178 &mAeCompensationStep, 1);
179 if (res != android::OK) {
180 return res;
181 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700182
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700183 res = info.update(ANDROID_CONTROL_AF_AVAILABLE_MODES,
184 mAfModes.data(), mAfModes.size());
185 if (res != android::OK) {
186 return res;
187 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700188
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700189 res = info.update(ANDROID_CONTROL_AWB_AVAILABLE_MODES,
190 mAwbModes.data(), mAwbModes.size());
191 if (res != android::OK) {
192 return res;
193 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700194
195 // Couldn't find any V4L2 support for regions, though maybe it's out there.
196 int32_t max_regions[] = {/*AE*/ 0,/*AWB*/ 0,/*AF*/ 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700197 res = info.update(ANDROID_CONTROL_MAX_REGIONS,
198 max_regions, ARRAY_SIZE(max_regions));
199 if (res != android::OK) {
200 return res;
201 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700202
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700203 res = info.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE,
204 &mAeLockAvailable, 1);
205 if (res != android::OK) {
206 return res;
207 }
208 res = info.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
209 &mAwbLockAvailable, 1);
210 if (res != android::OK) {
211 return res;
212 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700213
214 /* Scene modes. */
215
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700216 res = info.update(ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
217 mSceneModes.data(), mSceneModes.size());
218 if (res != android::OK) {
219 return res;
220 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700221
222 // A 3-tuple of AE, AWB, AF overrides for each scene mode.
223 // Ignored for DISABLED, FACE_PRIORITY and FACE_PRIORITY_LOW_LIGHT.
224 uint8_t scene_mode_overrides[] = {
225 /*SCENE_MODE_DISABLED*/ /*AE*/0, /*AW*/0, /*AF*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700226 res = info.update(ANDROID_CONTROL_SCENE_MODE_OVERRIDES,
227 scene_mode_overrides, ARRAY_SIZE(scene_mode_overrides));
228 if (res != android::OK) {
229 return res;
230 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700231
232 /* Top level 3A/Scenes switch. */
233
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700234 res = info.update(ANDROID_CONTROL_AVAILABLE_MODES,
235 mControlModes.data(), mControlModes.size());
236 if (res != android::OK) {
237 return res;
238 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700239
240 /* Other android.control configuration. */
241
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700242 res = info.update(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
243 mVideoStabilizationModes.data(),
244 mVideoStabilizationModes.size());
245 if (res != android::OK) {
246 return res;
247 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700248
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700249 res = info.update(ANDROID_CONTROL_AVAILABLE_EFFECTS,
250 mEffects.data(), mEffects.size());
251 if (res != android::OK) {
252 return res;
253 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700254
255 // AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS only necessary
256 // for devices supporting CONSTRAINED_HIGH_SPEED_VIDEO,
257 // which this HAL doesn't support.
258
259 // POST_RAW_SENSITIVITY_BOOST_RANGE only necessary
260 // for devices supporting RAW format outputs.
261
262 /* android.edge. */
263
264 // Not sure if V4L2 does or doesn't do this, but HAL documentation says
265 // all devices must support FAST, and FAST can be equivalent to OFF, so
266 // either way it's fine to list.
267 uint8_t avail_edge_modes[] = {
268 ANDROID_EDGE_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700269 res = info.update(ANDROID_EDGE_AVAILABLE_EDGE_MODES,
270 avail_edge_modes, ARRAY_SIZE(avail_edge_modes));
271 if (res != android::OK) {
272 return res;
273 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700274
275 /* android.flash. */
276
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700277 res = info.update(ANDROID_FLASH_INFO_AVAILABLE,
278 &mFlashAvailable, 1);
279 if (res != android::OK) {
280 return res;
281 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700282
283 // info.chargeDuration, color.Temperature, maxEnergy marked FUTURE.
284
285 /* android.hotPixel. */
286
287 // No known V4L2 hot pixel correction. But it might be happening,
288 // so we report FAST/HIGH_QUALITY.
289 uint8_t avail_hot_pixel_modes[] = {
290 ANDROID_HOT_PIXEL_MODE_FAST,
291 ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700292 res = info.update(ANDROID_HOT_PIXEL_AVAILABLE_HOT_PIXEL_MODES,
293 avail_hot_pixel_modes, ARRAY_SIZE(avail_hot_pixel_modes));
294 if (res != android::OK) {
295 return res;
296 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700297
298 /* android.jpeg. */
299
300 // For now, no thumbnails available (only [0,0], the "no thumbnail" size).
301 // TODO(b/29580107): Could end up with a mismatch between request & result,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700302 // since V4L2 doesn't actually allow for thumbnail size control.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700303 int32_t thumbnail_sizes[] = {
304 0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700305 res = info.update(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
306 thumbnail_sizes, ARRAY_SIZE(thumbnail_sizes));
307 if (res != android::OK) {
308 return res;
309 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700310
311 // V4L2 doesn't support querying this, so we generously assume up to 3 MB.
312 int32_t max_jpeg_size = 3000000;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700313 res = info.update(ANDROID_JPEG_MAX_SIZE,
314 &max_jpeg_size, 1);
315 if (res != android::OK) {
316 return res;
317 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700318
319 /* android.lens. */
320
321 /* Misc. lens control. */
322
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700323 res = info.update(ANDROID_LENS_INFO_AVAILABLE_APERTURES,
324 &mAperture, 1);
325 if (res != android::OK) {
326 return res;
327 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700328
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700329 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES,
330 &mFilterDensity, 1);
331 if (res != android::OK) {
332 return res;
333 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700334
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700335 res = info.update(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
336 mOpticalStabilizationModes.data(),
337 mOpticalStabilizationModes.size());
338 if (res != android::OK) {
339 return res;
340 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700341
342 // No known V4L2 shading map info.
343 int32_t shading_map_size[] = {1, 1};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700344 res = info.update(ANDROID_LENS_INFO_SHADING_MAP_SIZE,
345 shading_map_size, ARRAY_SIZE(shading_map_size));
346 if (res != android::OK) {
347 return res;
348 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700349
350 // All V4L2 devices are considered to be external facing.
351 uint8_t facing = ANDROID_LENS_FACING_EXTERNAL;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700352 res = info.update(ANDROID_LENS_FACING, &facing, 1);
353 if (res != android::OK) {
354 return res;
355 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700356
357 /* Zoom/Focus. */
358
359 // No way to actually get the focal length in V4L2, but it's a required key,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700360 // so we just fake it.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700361 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
362 &mFocalLength, 1);
363 if (res != android::OK) {
364 return res;
365 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700366
367 // V4L2 focal units do not correspond to a particular physical unit.
368 uint8_t focus_calibration =
369 ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700370 res = info.update(ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
371 &focus_calibration, 1);
372 if (res != android::OK) {
373 return res;
374 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700375
376 // info.hyperfocalDistance not required for UNCALIBRATED.
377
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700378 res = info.update(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
379 &mFocusDistance, 1);
380 if (res != android::OK) {
381 return res;
382 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700383
384 /* Depth. */
385
386 // DEPTH capability not supported by this HAL. Not implemented:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700387 // poseRotation
388 // poseTranslation
389 // intrinsicCalibration
390 // radialDistortion
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700391
392 /* anroid.noise. */
393
394 // Unable to control noise reduction in V4L2 devices,
395 // but FAST is allowed to be the same as OFF.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700396 uint8_t avail_noise_reduction_modes[] = {ANDROID_NOISE_REDUCTION_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700397 res = info.update(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
398 avail_noise_reduction_modes,
399 ARRAY_SIZE(avail_noise_reduction_modes));
400 if (res != android::OK) {
401 return res;
402 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700403
404 /* android.request. */
405
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700406 int32_t max_num_output_streams[] = {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700407 mMaxRawOutputStreams, mMaxYuvOutputStreams, mMaxJpegOutputStreams};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700408 res = info.update(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
409 max_num_output_streams, ARRAY_SIZE(max_num_output_streams));
410 if (res != android::OK) {
411 return res;
412 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700413
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700414 res = info.update(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS,
415 &mMaxInputStreams, 1);
416 if (res != android::OK) {
417 return res;
418 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700419
420 // No way to know for V4L2, so fake with max allowable latency.
421 // Doesn't mean much without per-frame controls.
422 uint8_t pipeline_max_depth = 4;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700423 res = info.update(ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
424 &pipeline_max_depth, 1);
425 if (res != android::OK) {
426 return res;
427 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700428
429 // Partial results not supported; partialResultCount defaults to 1.
430
431 // Available capabilities & keys queried at very end of this method.
432
433 /* android.scaler. */
434
435 /* Cropping. */
436
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700437 res = info.update(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
438 &mMaxZoom, 1);
439 if (res != android::OK) {
440 return res;
441 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700442
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700443 res = info.update(ANDROID_SCALER_CROPPING_TYPE, &mCropType, 1);
444 if (res != android::OK) {
445 return res;
446 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700447
448 /* Streams. */
449
450 // availableInputOutputFormatsMap only required for reprocessing capability.
451
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700452 // Flatten mStreamConfigs.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700453 res = info.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
454 mStreamConfigs.data(),
455 mStreamConfigs.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 mMinFrameDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700461 res = info.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
462 mMinFrameDurations.data(),
463 mMinFrameDurations.total_num_elements());
464 if (res != android::OK) {
465 return res;
466 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700467
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700468 // Flatten mStallDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700469 res = info.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
470 mStallDurations.data(),
471 mStallDurations.total_num_elements());
472 if (res != android::OK) {
473 return res;
474 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700475
476 /* android.sensor. */
477
478 /* Sizes. */
479
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700480 res = info.update(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
481 mPixelArraySize.data(), mPixelArraySize.size());
482 if (res != android::OK) {
483 return res;
484 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700485 // No V4L2 way to differentiate active vs. inactive parts of the rectangle.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700486 res = info.update(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
487 mPixelArraySize.data(), mPixelArraySize.size());
488 if (res != android::OK) {
489 return res;
490 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700491
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700492 res = info.update(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
493 mPhysicalSize.data(), mPhysicalSize.size());
494 if (res != android::OK) {
495 return res;
496 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700497
498 /* Misc sensor information. */
499
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700500 res = info.update(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
501 &mMaxFrameDuration, 1);
502 if (res != android::OK) {
503 return res;
504 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700505
506 // HAL uses BOOTTIME timestamps.
507 // TODO(b/29457051): make sure timestamps are consistent throughout the HAL.
508 uint8_t timestamp_source = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700509 res = info.update(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
510 &timestamp_source, 1);
511 if (res != android::OK) {
512 return res;
513 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700514
515 // As in initDeviceInfo, no way to actually get orientation.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700516 res = info.update(ANDROID_SENSOR_ORIENTATION, &mOrientation, 1);
517 if (res != android::OK) {
518 return res;
519 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700520
521 // availableTestPatternModes just defaults to OFF, which is fine.
522
523 // info.exposureTimeRange, info.sensitivityRange:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700524 // exposure/sensitivity manual control not supported.
525 // Could query V4L2_CID_ISO_SENSITIVITY to support sensitivity if desired.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700526
527 // info.whiteLevel, info.lensShadingApplied,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700528 // info.preCorrectionPixelArraySize, referenceIlluminant1/2,
529 // calibrationTransform1/2, colorTransform1/2, forwardMatrix1/2,
530 // blackLevelPattern, profileHueSatMapDimensions
531 // all only necessary for RAW.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700532
533 // baseGainFactor marked FUTURE.
534
535 // maxAnalogSensitivity optional for LIMITED device.
536
537 // opticalBlackRegions: No known way to get in V4L2, but not necessary.
538
539 // opaqueRawSize not necessary since RAW_OPAQUE format not supported.
540
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700541 /* android.shading */
542
543 // No known V4L2 lens shading. But it might be happening,
544 // so we report FAST/HIGH_QUALITY.
545 uint8_t avail_shading_modes[] = {
546 ANDROID_SHADING_MODE_FAST,
547 ANDROID_SHADING_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700548 res = info.update(ANDROID_SHADING_AVAILABLE_MODES,
549 avail_shading_modes, ARRAY_SIZE(avail_shading_modes));
550 if (res != android::OK) {
551 return res;
552 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700553
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700554 /* android.statistics */
555
556 // Face detection not supported.
557 uint8_t avail_face_detect_modes[] = {
558 ANDROID_STATISTICS_FACE_DETECT_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700559 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
560 avail_face_detect_modes,
561 ARRAY_SIZE(avail_face_detect_modes));
562 if (res != android::OK) {
563 return res;
564 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700565
566 int32_t max_face_count = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700567 res = info.update(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
568 &max_face_count, 1);
569 if (res != android::OK) {
570 return res;
571 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700572
573 // info.histogramBucketCount, info.maxHistogramCount,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700574 // info.maxSharpnessMapValue, info.sharpnessMapSizemarked FUTURE.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700575
576 // ON only needs to be supported for RAW capable devices.
577 uint8_t avail_hot_pixel_map_modes[] = {
578 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700579 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
580 avail_hot_pixel_map_modes,
581 ARRAY_SIZE(avail_hot_pixel_map_modes));
582 if (res != android::OK) {
583 return res;
584 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700585
586 // ON only needs to be supported for RAW capable devices.
587 uint8_t avail_lens_shading_map_modes[] = {
588 ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700589 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
590 avail_lens_shading_map_modes,
591 ARRAY_SIZE(avail_lens_shading_map_modes));
592 if (res != android::OK) {
593 return res;
594 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700595
596 /* android.tonemap. */
597
598 // tonemapping only required for MANUAL_POST_PROCESSING capability.
599
600 /* android.led. */
601
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700602 // May or may not have LEDs available.
603 if (!mLeds.empty()) {
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700604 res = info.update(ANDROID_LED_AVAILABLE_LEDS, mLeds.data(), mLeds.size());
605 if (res != android::OK) {
606 return res;
607 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700608 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700609
610 /* android.sync. */
611
612 // "LIMITED devices are strongly encouraged to use a non-negative value.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700613 // If UNKNOWN is used here then app developers do not have a way to know
614 // when sensor settings have been applied." - Unfortunately, V4L2 doesn't
615 // really help here either. Could even be that adjusting settings mid-stream
616 // blocks in V4L2, and should be avoided.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700617 int32_t max_latency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700618 res = info.update(ANDROID_SYNC_MAX_LATENCY,
619 &max_latency, 1);
620 if (res != android::OK) {
621 return res;
622 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700623
624 /* android.reprocess. */
625
626 // REPROCESSING not supported by this HAL.
627
628 /* android.depth. */
629
630 // DEPTH not supported by this HAL.
631
632 /* Capabilities and android.info. */
633
634 uint8_t hw_level = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700635 res = info.update(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
636 &hw_level, 1);
637 if (res != android::OK) {
638 return res;
639 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700640
641 uint8_t capabilities[] = {
642 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700643 res = info.update(ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
644 capabilities, ARRAY_SIZE(capabilities));
645 if (res != android::OK) {
646 return res;
647 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700648
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700649 // Scan a default request template for included request keys.
650 if (!mTemplatesInitialized) {
651 res = initTemplates();
652 if (res) {
653 return res;
654 }
655 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700656 const camera_metadata_t* preview_request = nullptr;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700657 // Search templates from the beginning for a supported one.
658 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
659 ++template_id) {
660 preview_request = constructDefaultRequestSettings(template_id);
661 if (preview_request != nullptr) {
662 break;
663 }
664 }
665 if (preview_request == nullptr) {
666 HAL_LOGE("No valid templates, can't get request keys.");
667 return -ENODEV;
668 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700669 std::vector<int32_t> avail_request_keys = getMetadataKeys(preview_request);
670 res = info.update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
671 avail_request_keys.data(), avail_request_keys.size());
672 if (res != android::OK) {
673 return res;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700674 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700675
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700676 // Result keys will be duplicated from the request, plus a few extras.
677 // TODO(b/29335262): additonal available result keys.
678 std::vector<int32_t> avail_result_keys(avail_request_keys);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700679 res = info.update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
680 avail_result_keys.data(), avail_result_keys.size());
681 if (res != android::OK) {
682 return res;
683 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700684
685 // Last thing, once all the available characteristics have been added.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700686 const camera_metadata_t* static_characteristics = info.getAndLock();
687 std::vector<int32_t> avail_characteristics_keys =
688 getMetadataKeys(static_characteristics);
689 res = info.unlock(static_characteristics);
690 if (res != android::OK) {
691 return res;
692 }
693 avail_characteristics_keys.push_back(
694 ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
695 res = info.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
696 avail_characteristics_keys.data(),
697 avail_characteristics_keys.size());
698 if (res != android::OK) {
699 return res;
700 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700701
702 *out = info.release();
703 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700704}
705
706void V4L2Camera::initDeviceInfo(camera_info_t* info) {
707 HAL_LOG_ENTER();
708
709 // For now, just constants.
710 info->facing = CAMERA_FACING_EXTERNAL;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700711 info->orientation = mOrientation;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700712 info->resource_cost = 100;
713 info->conflicting_devices = nullptr;
714 info->conflicting_devices_length = 0;
715}
716
717int V4L2Camera::initDevice() {
718 HAL_LOG_ENTER();
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700719 int res;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700720
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700721 // Templates should be set up if they haven't already been.
722 if (!mTemplatesInitialized) {
723 res = initTemplates();
724 if (res) {
725 return res;
726 }
727 }
728
729 return 0;
730}
731
732int V4L2Camera::initTemplates() {
733 HAL_LOG_ENTER();
734 int res;
735
736 // Device characteristics need to be queried prior
737 // to template setup.
738 if (!mCharacteristicsInitialized) {
739 res = initCharacteristics();
740 if (res) {
741 return res;
742 }
743 }
744
745 // Note: static metadata expects all templates/requests
746 // to provide values for all supported keys.
747
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700748 android::CameraMetadata base_metadata;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700749
750 // Start with defaults for all templates.
751
752 /* android.colorCorrection. */
753
754 uint8_t aberration_mode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700755 res = base_metadata.update(ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
756 &aberration_mode, 1);
757 if (res != android::OK) {
758 return res;
759 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700760
761 uint8_t color_correction_mode = ANDROID_COLOR_CORRECTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700762 res = base_metadata.update(ANDROID_COLOR_CORRECTION_MODE,
763 &color_correction_mode, 1);
764 if (res != android::OK) {
765 return res;
766 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700767
768 // transform and gains are for the unsupported MANUAL_POST_PROCESSING only.
769
770 /* android.control. */
771
772 /* AE. */
773 uint8_t ae_antibanding_mode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700774 res = base_metadata.update(ANDROID_CONTROL_AE_ANTIBANDING_MODE,
775 &ae_antibanding_mode, 1);
776 if (res != android::OK) {
777 return res;
778 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700779
780 // Only matters if AE_MODE = OFF
781 int32_t ae_exposure_compensation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700782 res = base_metadata.update(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
783 &ae_exposure_compensation, 1);
784 if (res != android::OK) {
785 return res;
786 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700787
788 uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700789 res = base_metadata.update(ANDROID_CONTROL_AE_LOCK, &ae_lock, 1);
790 if (res != android::OK) {
791 return res;
792 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700793
794 uint8_t ae_mode = ANDROID_CONTROL_AE_MODE_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700795 res = base_metadata.update(ANDROID_CONTROL_AE_MODE, &ae_mode, 1);
796 if (res != android::OK) {
797 return res;
798 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700799
800 // AE regions not supported.
801
802 // FPS set per-template.
803
804 uint8_t ae_precapture_trigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700805 res = base_metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
806 &ae_precapture_trigger, 1);
807 if (res != android::OK) {
808 return res;
809 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700810
811 /* AF. */
812
813 // AF mode set per-template.
814
815 // AF regions not supported.
816
817 uint8_t af_trigger = ANDROID_CONTROL_AF_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700818 res = base_metadata.update(ANDROID_CONTROL_AF_TRIGGER, &af_trigger, 1);
819 if (res != android::OK) {
820 return res;
821 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700822
823 /* AWB. */
824
825 // Priority: auto > off > Whatever is available.
826 uint8_t default_awb_mode = mAwbModes[0];
827 if (std::count(mAwbModes.begin(), mAwbModes.end(),
828 ANDROID_CONTROL_AWB_MODE_AUTO)) {
829 default_awb_mode = ANDROID_CONTROL_AWB_MODE_AUTO;
830 } else if (std::count(mAwbModes.begin(), mAwbModes.end(),
831 ANDROID_CONTROL_AWB_MODE_OFF)) {
832 default_awb_mode = ANDROID_CONTROL_AWB_MODE_OFF;
833 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700834 res = base_metadata.update(ANDROID_CONTROL_AWB_MODE, &default_awb_mode, 1);
835 if (res != android::OK) {
836 return res;
837 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700838
839 // AWB regions not supported.
840
841 /* Other controls. */
842
843 uint8_t effect_mode = ANDROID_CONTROL_EFFECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700844 res = base_metadata.update(ANDROID_CONTROL_EFFECT_MODE, &effect_mode, 1);
845 if (res != android::OK) {
846 return res;
847 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700848
849 uint8_t control_mode = ANDROID_CONTROL_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700850 res = base_metadata.update(ANDROID_CONTROL_MODE, &control_mode, 1);
851 if (res != android::OK) {
852 return res;
853 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700854
855 uint8_t scene_mode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700856 res = base_metadata.update(ANDROID_CONTROL_SCENE_MODE,
857 &scene_mode, 1);
858 if (res != android::OK) {
859 return res;
860 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700861
862 uint8_t video_stabilization = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700863 res = base_metadata.update(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
864 &video_stabilization, 1);
865 if (res != android::OK) {
866 return res;
867 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700868
869 // postRawSensitivityBoost: RAW not supported, leave null.
870
871 /* android.demosaic. */
872
873 // mode marked FUTURE.
874
875 /* android.edge. */
876
877 uint8_t edge_mode = ANDROID_EDGE_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700878 res = base_metadata.update(ANDROID_EDGE_MODE, &edge_mode, 1);
879 if (res != android::OK) {
880 return res;
881 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700882
883 // strength marked FUTURE.
884
885 /* android.flash. */
886
887 // firingPower, firingTime marked FUTURE.
888
889 uint8_t flash_mode = ANDROID_FLASH_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700890 res = base_metadata.update(ANDROID_FLASH_MODE, &flash_mode, 1);
891 if (res != android::OK) {
892 return res;
893 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700894
895 /* android.hotPixel. */
896
897 uint8_t hp_mode = ANDROID_HOT_PIXEL_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700898 res = base_metadata.update(ANDROID_HOT_PIXEL_MODE, &hp_mode, 1);
899 if (res != android::OK) {
900 return res;
901 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700902
903 /* android.jpeg. */
904
905 double gps_coords[] = {/*latitude*/0, /*longitude*/0, /*altitude*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700906 res = base_metadata.update(ANDROID_JPEG_GPS_COORDINATES, gps_coords, 3);
907 if (res != android::OK) {
908 return res;
909 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700910
911 uint8_t gps_processing_method[] = "none";
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700912 res = base_metadata.update(ANDROID_JPEG_GPS_PROCESSING_METHOD,
913 gps_processing_method,
914 ARRAY_SIZE(gps_processing_method));
915 if (res != android::OK) {
916 return res;
917 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700918
919 int64_t gps_timestamp = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700920 res = base_metadata.update(ANDROID_JPEG_GPS_TIMESTAMP, &gps_timestamp, 1);
921 if (res != android::OK) {
922 return res;
923 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700924
925 // JPEG orientation is relative to sensor orientation (mOrientation).
926 int32_t jpeg_orientation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700927 res = base_metadata.update(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1);
928 if (res != android::OK) {
929 return res;
930 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700931
932 // 1-100, larger is higher quality.
933 uint8_t jpeg_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700934 res = base_metadata.update(ANDROID_JPEG_QUALITY, &jpeg_quality, 1);
935 if (res != android::OK) {
936 return res;
937 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700938
939 // TODO(b/29580107): If thumbnail quality actually matters/can be adjusted,
940 // adjust this.
941 uint8_t thumbnail_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700942 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_QUALITY,
943 &thumbnail_quality, 1);
944 if (res != android::OK) {
945 return res;
946 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700947
948 // TODO(b/29580107): Choose a size matching the resolution.
949 int32_t thumbnail_size[] = {0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700950 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_SIZE, thumbnail_size, 2);
951 if (res != android::OK) {
952 return res;
953 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700954
955 /* android.lens. */
956
957 // Fixed values.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700958 res = base_metadata.update(ANDROID_LENS_APERTURE, &mAperture, 1);
959 if (res != android::OK) {
960 return res;
961 }
962 res = base_metadata.update(ANDROID_LENS_FILTER_DENSITY, &mFilterDensity, 1);
963 if (res != android::OK) {
964 return res;
965 }
966 res = base_metadata.update(ANDROID_LENS_FOCAL_LENGTH, &mFocalLength, 1);
967 if (res != android::OK) {
968 return res;
969 }
970 res = base_metadata.update(ANDROID_LENS_FOCUS_DISTANCE, &mFocusDistance, 1);
971 if (res != android::OK) {
972 return res;
973 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700974
975 uint8_t optical_stabilization = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700976 res = base_metadata.update(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700977 &optical_stabilization, 1);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700978 if (res != android::OK) {
979 return res;
980 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700981
982 /* android.noiseReduction. */
983
984 uint8_t noise_reduction_mode = ANDROID_NOISE_REDUCTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700985 res = base_metadata.update(ANDROID_NOISE_REDUCTION_MODE,
986 &noise_reduction_mode, 1);
987 if (res != android::OK) {
988 return res;
989 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700990
991 // strength marked FUTURE.
992
993 /* android.request. */
994
995 // Request Id unused by the HAL for now, and these are just
996 // templates, so just fill it in with a dummy.
997 int32_t id = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700998 res = base_metadata.update(ANDROID_REQUEST_ID, &id, 1);
999 if (res != android::OK) {
1000 return res;
1001 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001002
1003 // metadataMode marked FUTURE.
1004
1005 /* android.scaler. */
1006
1007 // No cropping by default; use the full active array.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001008 res = base_metadata.update(ANDROID_SCALER_CROP_REGION, mPixelArraySize.data(),
1009 mPixelArraySize.size());
1010 if (res != android::OK) {
1011 return res;
1012 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001013
1014 /* android.sensor. */
1015
1016 // exposureTime, sensitivity, testPattern[Data,Mode] not supported.
1017
1018 // Ignored when AE is OFF.
1019 int64_t frame_duration = 33333333L; // 1/30 s.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001020 res = base_metadata.update(ANDROID_SENSOR_FRAME_DURATION, &frame_duration, 1);
1021 if (res != android::OK) {
1022 return res;
1023 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001024
1025 /* android.shading. */
1026
1027 uint8_t shading_mode = ANDROID_SHADING_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001028 res = base_metadata.update(ANDROID_SHADING_MODE, &shading_mode, 1);
1029 if (res != android::OK) {
1030 return res;
1031 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001032
1033 /* android.statistics. */
1034
1035 uint8_t face_detect_mode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001036 res = base_metadata.update(ANDROID_STATISTICS_FACE_DETECT_MODE,
1037 &face_detect_mode, 1);
1038 if (res != android::OK) {
1039 return res;
1040 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001041
1042 // histogramMode, sharpnessMapMode marked FUTURE.
1043
1044 uint8_t hp_map_mode = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001045 res = base_metadata.update(ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
1046 &hp_map_mode, 1);
1047 if (res != android::OK) {
1048 return res;
1049 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001050
1051 uint8_t lens_shading_map_mode = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001052 res = base_metadata.update(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
1053 &lens_shading_map_mode, 1);
1054 if (res != android::OK) {
1055 return res;
1056 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001057
1058 /* android.tonemap. */
1059
1060 // Tonemap only required for MANUAL_POST_PROCESSING capability.
1061
1062 /* android.led. */
1063
1064 uint8_t transmit = ANDROID_LED_TRANSMIT_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001065 res = base_metadata.update(ANDROID_LED_TRANSMIT, &transmit, 1);
1066 if (res != android::OK) {
1067 return res;
1068 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001069
1070 /* android.reprocess */
1071
1072 // Only needed for REPROCESS capability.
1073
1074
1075 /* Template variable values. */
1076
1077 // Find the FPS ranges "closest" to a desired range
1078 // (minimum abs distance from min to min and max to max).
1079 // Find both a fixed rate and a variable rate, for different purposes.
1080 std::array<int32_t, 2> desired_flat_fps_range = {{30, 30}};
1081 std::array<int32_t, 2> desired_variable_fps_range = {{5, 30}};
1082 std::array<int32_t, 2> flat_fps_range;
1083 std::array<int32_t, 2> variable_fps_range;
1084 int32_t best_flat_distance = std::numeric_limits<int32_t>::max();
1085 int32_t best_variable_distance = std::numeric_limits<int32_t>::max();
1086 size_t num_fps_ranges = mFpsRanges.num_arrays();
1087 for (size_t i = 0; i < num_fps_ranges; ++i) {
1088 const int32_t* range = mFpsRanges[i];
1089 // Variable fps.
1090 int32_t distance = std::abs(range[0] - desired_variable_fps_range[0]) +
1091 std::abs(range[1] - desired_variable_fps_range[1]);
1092 if (distance < best_variable_distance) {
1093 variable_fps_range[0] = range[0];
1094 variable_fps_range[1] = range[1];
1095 best_variable_distance = distance;
1096 }
1097 // Flat fps. Only do if range is actually flat.
1098 // Note at least one flat range is required,
1099 // so something will always be filled in.
1100 if (range[0] == range[1]) {
1101 distance = std::abs(range[0] - desired_flat_fps_range[0]) +
1102 std::abs(range[1] - desired_flat_fps_range[1]);
1103 if (distance < best_flat_distance) {
1104 flat_fps_range[0] = range[0];
1105 flat_fps_range[1] = range[1];
1106 best_flat_distance = distance;
1107 }
1108 }
1109 }
1110
1111 // Priority: continuous > auto > off > whatever is available.
1112 bool continuous_still_avail = std::count(
1113 mAfModes.begin(), mAfModes.end(),
1114 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE);
1115 bool continuous_video_avail = std::count(
1116 mAfModes.begin(), mAfModes.end(),
1117 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO);
1118 uint8_t non_continuous_af_mode = mAfModes[0];
1119 if (std::count(mAfModes.begin(), mAfModes.end(),
1120 ANDROID_CONTROL_AF_MODE_AUTO)) {
1121 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_AUTO;
1122 } else if (std::count(mAfModes.begin(), mAfModes.end(),
1123 ANDROID_CONTROL_AF_MODE_OFF)) {
1124 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_OFF;
1125 }
1126 uint8_t still_af_mode = continuous_still_avail ?
1127 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE : non_continuous_af_mode;
1128 uint8_t video_af_mode = continuous_video_avail ?
1129 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO : non_continuous_af_mode;
1130
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001131 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
1132 ++template_id) {
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001133 // General differences/support.
1134 uint8_t intent;
1135 uint8_t af_mode;
1136 std::array<int32_t, 2> fps_range;
1137 switch(template_id) {
1138 case CAMERA3_TEMPLATE_PREVIEW:
1139 intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
1140 af_mode = still_af_mode;
1141 fps_range = flat_fps_range;
1142 break;
1143 case CAMERA3_TEMPLATE_STILL_CAPTURE:
1144 intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
1145 af_mode = still_af_mode;
1146 fps_range = variable_fps_range;
1147 break;
1148 case CAMERA3_TEMPLATE_VIDEO_RECORD:
1149 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
1150 af_mode = video_af_mode;
1151 fps_range = flat_fps_range;
1152 break;
1153 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
1154 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
1155 af_mode = video_af_mode;
1156 fps_range = flat_fps_range;
1157 break;
1158 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: // Fall through.
1159 case CAMERA3_TEMPLATE_MANUAL: // Fall though.
1160 default:
1161 // Unsupported/unrecognized. Don't add this template; skip it.
1162 continue;
1163 }
1164
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001165 // Copy our base metadata and add the new items.
1166 android::CameraMetadata template_metadata(base_metadata);
1167 res = template_metadata.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent, 1);
1168 if (res != android::OK) {
1169 return res;
1170 }
1171 res = template_metadata.update(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
1172 fps_range.data(), fps_range.size());
1173 if (res != android::OK) {
1174 return res;
1175 }
1176 res = template_metadata.update(ANDROID_CONTROL_AF_MODE, &af_mode, 1);
1177 if (res != android::OK) {
1178 return res;
1179 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001180
1181 const camera_metadata_t* template_raw_metadata =
1182 template_metadata.getAndLock();
1183 res = setTemplate(template_id, template_raw_metadata);
1184 if (res != android::OK) {
1185 return res;
1186 }
1187 res = template_metadata.unlock(template_raw_metadata);
1188 if (res != android::OK) {
1189 return res;
1190 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001191 }
1192
1193 mTemplatesInitialized = true;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001194 return 0;
1195}
1196
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001197bool V4L2Camera::isSupportedStreamSet(default_camera_hal::Stream** streams,
1198 int count, uint32_t mode) {
1199 HAL_LOG_ENTER();
1200
1201 if (mode != CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE) {
1202 HAL_LOGE("Unsupported stream configuration mode: %d", mode);
1203 return false;
1204 }
1205
1206 // This should be checked by the caller, but put here as a sanity check.
1207 if (count < 1) {
1208 HAL_LOGE("Must request at least 1 stream");
1209 return false;
1210 }
1211
1212 // Count the number of streams of each type.
1213 int32_t num_input = 0;
1214 int32_t num_raw = 0;
1215 int32_t num_yuv = 0;
1216 int32_t num_jpeg = 0;
1217 for (int i = 0; i < count; ++i) {
1218 default_camera_hal::Stream* stream = streams[i];
1219
1220 if (stream->isInputType()) {
1221 ++num_input;
1222 }
1223
1224 if (stream->isOutputType()) {
1225 switch (halToV4L2PixelFormat(stream->getFormat())) {
1226 case V4L2_PIX_FMT_YUV420:
1227 ++num_yuv;
1228 break;
1229 case V4L2_PIX_FMT_JPEG:
1230 ++num_jpeg;
1231 break;
1232 default:
1233 // Note: no supported raw formats at this time.
1234 HAL_LOGE("Unsupported format for stream %d: %d", i, stream->getFormat());
1235 return false;
1236 }
1237 }
1238 }
1239
1240 if (num_input > mMaxInputStreams || num_raw > mMaxRawOutputStreams ||
1241 num_yuv > mMaxYuvOutputStreams || num_jpeg > mMaxJpegOutputStreams) {
1242 HAL_LOGE("Invalid stream configuration: %d input, %d RAW, %d YUV, %d JPEG "
1243 "(max supported: %d input, %d RAW, %d YUV, %d JPEG)",
1244 mMaxInputStreams, mMaxRawOutputStreams, mMaxYuvOutputStreams,
1245 mMaxJpegOutputStreams, num_input, num_raw, num_yuv, num_jpeg);
1246 return false;
1247 }
1248
1249 // TODO(b/29939583): The above logic should be all that's necessary,
1250 // but V4L2 doesn't actually support more than 1 stream at a time. So for now,
1251 // if not all streams are the same format and size, error. Note that this
1252 // means the HAL is not spec-compliant; the requested streams are technically
1253 // valid and it is not technically allowed to error once it has reached this
1254 // point.
1255 int format = streams[0]->getFormat();
1256 uint32_t width = streams[0]->getWidth();
1257 uint32_t height = streams[0]->getHeight();
1258 for (int i = 1; i < count; ++i) {
1259 const default_camera_hal::Stream* stream = streams[i];
1260 if (stream->getFormat() != format || stream->getWidth() != width ||
1261 stream->getHeight() != height) {
1262 HAL_LOGE("V4L2 only supports 1 stream configuration at a time "
1263 "(stream 0 is format %d, width %u, height %u, "
1264 "stream %d is format %d, width %u, height %u).",
1265 format, width, height, i, stream->getFormat(),
1266 stream->getWidth(), stream->getHeight());
1267 return false;
1268 }
1269 }
1270
1271 return true;
1272}
1273
1274int V4L2Camera::setupStream(default_camera_hal::Stream* stream,
1275 uint32_t* max_buffers) {
1276 HAL_LOG_ENTER();
1277
1278 if (stream->getRotation() != CAMERA3_STREAM_ROTATION_0) {
1279 HAL_LOGE("Rotation %d not supported", stream->getRotation());
1280 return -EINVAL;
1281 }
1282
1283 // Doesn't matter what was requested, we always use dataspace V0_JFIF.
1284 // Note: according to camera3.h, this isn't allowed, but etalvala@google.com
1285 // claims it's underdocumented; the implementation lets the HAL overwrite it.
1286 stream->setDataSpace(HAL_DATASPACE_V0_JFIF);
1287
1288 int ret = setFormat(stream);
1289 if (ret) {
1290 return ret;
1291 }
1292
1293 // Only do buffer setup if we don't know our maxBuffers.
1294 if (mOutStreamMaxBuffers < 1) {
1295 ret = setupBuffers();
1296 if (ret) {
1297 return ret;
1298 }
1299 }
1300
1301 // Sanity check.
1302 if (mOutStreamMaxBuffers < 1) {
1303 HAL_LOGE("HAL failed to determine max number of buffers.");
1304 return -ENODEV;
1305 }
1306 *max_buffers = mOutStreamMaxBuffers;
1307
1308 return 0;
1309}
1310
1311int V4L2Camera::setFormat(const default_camera_hal::Stream* stream) {
1312 HAL_LOG_ENTER();
1313
1314 // Should be checked earlier; sanity check.
1315 if (stream->isInputType()) {
1316 HAL_LOGE("Input streams not supported.");
1317 return -EINVAL;
1318 }
1319
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001320 // TODO(b/30000211): Check if appropriate to do multi-planar capture instead.
1321 uint32_t desired_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1322 uint32_t desired_format = halToV4L2PixelFormat(stream->getFormat());
1323 uint32_t desired_width = stream->getWidth();
1324 uint32_t desired_height = stream->getHeight();
1325
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001326 // Check to make sure we're not already in the correct format.
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001327 if (desired_type == mOutStreamType &&
1328 desired_format == mOutStreamFormat &&
1329 desired_width == mOutStreamWidth &&
1330 desired_height == mOutStreamHeight) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001331 return 0;
1332 }
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001333
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001334 // Not in the correct format, set our format.
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001335 v4l2_format format;
1336 memset(&format, 0, sizeof(format));
1337 format.type = desired_type;
1338 format.fmt.pix.width = desired_width;
1339 format.fmt.pix.height = desired_height;
1340 format.fmt.pix.pixelformat = desired_format;
1341 // TODO(b/29334616): When async, this will need to check if the stream
1342 // is on, and if so, lock it off while setting format.
1343 if (ioctlLocked(VIDIOC_S_FMT, &format) < 0) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001344 HAL_LOGE("S_FMT failed: %s", strerror(errno));
1345 return -ENODEV;
1346 }
1347 // Check that the driver actually set to the requested values.
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001348 if (format.type != desired_type ||
1349 format.fmt.pix.pixelformat != desired_format ||
1350 format.fmt.pix.width != desired_width ||
1351 format.fmt.pix.height != desired_height) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001352 HAL_LOGE("Device doesn't support desired stream configuration.");
1353 return -EINVAL;
1354 }
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001355
1356 // Keep track of the new format.
1357 mOutStreamType = format.type;
1358 mOutStreamFormat = format.fmt.pix.pixelformat;
1359 mOutStreamWidth = format.fmt.pix.width;
1360 mOutStreamHeight = format.fmt.pix.height;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001361 // Since our format changed, our maxBuffers may be incorrect.
1362 mOutStreamMaxBuffers = 0;
1363
1364 return 0;
1365}
1366
1367int V4L2Camera::setupBuffers() {
1368 HAL_LOG_ENTER();
1369
1370 // "Request" a buffer (since we're using a userspace buffer, this just
1371 // tells V4L2 to switch into userspace buffer mode).
1372 v4l2_requestbuffers req_buffers;
1373 memset(&req_buffers, 0, sizeof(req_buffers));
1374 req_buffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1375 req_buffers.memory = V4L2_MEMORY_USERPTR;
1376 req_buffers.count = 1;
1377 if (ioctlLocked(VIDIOC_REQBUFS, &req_buffers) < 0) {
1378 HAL_LOGE("REQBUFS failed: %s", strerror(errno));
1379 return -ENODEV;
1380 }
1381
1382 // V4L2 will set req_buffers.count to a number of buffers it can handle.
1383 mOutStreamMaxBuffers = req_buffers.count;
1384 return 0;
1385}
1386
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001387bool V4L2Camera::isValidCaptureSettings(const camera_metadata_t* settings) {
1388 HAL_LOG_ENTER();
1389
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001390 // TODO(b/29335262): reject capture settings this camera isn't capable of.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001391 return true;
1392}
1393
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001394int V4L2Camera::initCharacteristics() {
1395 HAL_LOG_ENTER();
1396
1397 /* Physical characteristics. */
1398 // No way to get these in V4L2, so faked.
1399 // Note: While many of these are primarily informative for post-processing
1400 // calculations by the app and will potentially cause bad results there,
1401 // focal length and physical size are actually used in framework
1402 // calculations (field of view, pixel pitch, etc), so faking them may
1403 // have unexpected results.
1404 mAperture = 2.0; // RPi camera v2 is f/2.0.
1405 mFilterDensity = 0.0;
1406 mFocalLength = 3.04; // RPi camera v2 is 3.04mm.
1407 mOrientation = 0;
1408 mPhysicalSize = {{3.674, 2.760}}; // RPi camera v2 is 3.674 x 2.760 mm.
1409
1410 /* Fixed features. */
1411
1412 // TODO(b/29394024): query VIDIOC_CROPCAP to get pixel rectangle.
1413 // Spoofing as 640 x 480 for now.
1414 mPixelArraySize = {{/*xmin*/0, /*ymin*/0, /*width*/640, /*height*/480}};
1415
1416 // V4L2 VIDIOC_CROPCAP doesn't give a way to query this;
1417 // it's driver dependent. For now, assume freeform, and
1418 // some cameras may just behave badly.
1419 // TODO(b/29579652): Figure out a way to determine this.
1420 mCropType = ANDROID_SCALER_CROPPING_TYPE_FREEFORM;
1421
1422 // TODO(b/29394024): query VIDIOC_CROPCAP to get cropping ranges,
1423 // and VIDIOC_G_CROP to determine if cropping is supported.
1424 // If the ioctl isn't available (or cropping has non-square pixelaspect),
1425 // assume no cropping/scaling.
1426 // May need to try setting some crops to determine what the driver actually
1427 // supports (including testing center vs freeform).
1428 mMaxZoom = 1;
1429
1430 // TODO(b/29394024): query V4L2_CID_EXPOSURE_BIAS.
1431 mAeCompensationRange = {{0, 0}};
1432 mAeCompensationStep = {1, 1};
1433
1434 // TODO(b/29394024): query V4L2_CID_3A_LOCK.
1435 mAeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE;
1436 mAwbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE;
1437
1438 // TODO(b/29394024): query V4L2_CID_FLASH_LED_MODE.
1439 mFlashAvailable = 0;
1440
1441 // TODO(b/29394024): query V4L2_CID_FOCUS_ABSOLUTE for focus range.
1442 mFocusDistance = 0; // Fixed focus.
1443
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001444 // TODO(b/29939583): V4L2 can only support 1 stream at a time.
1445 // For now, just reporting minimum allowable for LIMITED devices.
1446 mMaxRawOutputStreams = 0;
1447 mMaxYuvOutputStreams = 2;
1448 mMaxJpegOutputStreams = 1;
1449 // Reprocessing not supported.
1450 mMaxInputStreams = 0;
1451
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001452 /* Features with (potentially) multiple options. */
1453
1454 // TODO(b/29394024): query V4L2_CID_EXPOSURE_AUTO for ae modes.
1455 mAeModes.push_back(ANDROID_CONTROL_AE_MODE_ON);
1456
1457 // TODO(b/29394024): query V4L2_CID_POWER_LINE_FREQUENCY.
1458 // Auto as the default, since it could mean anything, while OFF would
1459 // require guaranteeing no antibanding happens.
1460 mAeAntibandingModes.push_back(ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO);
1461
1462 // TODO(b/29394024): query V4L2_CID_FOCUS_AUTO for
1463 // CONTINUOUS_VIDEO/CONTINUOUS_PICTURE. V4L2_CID_AUTO_FOCUS_START
1464 // supports what Android thinks of as auto focus (single auto focus).
1465 // V4L2_CID_AUTO_FOCUS_RANGE allows MACRO.
1466 mAfModes.push_back(ANDROID_CONTROL_AF_MODE_OFF);
1467
1468 // TODO(b/29394024): query V4L2_CID_AUTO_WHITE_BALANCE, or
1469 // V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE if available.
1470 mAwbModes.push_back(ANDROID_CONTROL_AWB_MODE_AUTO);
1471
1472 // TODO(b/29394024): query V4L2_CID_SCENE_MODE.
1473 mSceneModes.push_back(ANDROID_CONTROL_SCENE_MODE_DISABLED);
1474
1475 mControlModes.push_back(ANDROID_CONTROL_MODE_AUTO);
1476 if (mSceneModes.size() > 1) {
1477 // We have some mode other than just DISABLED available.
1478 mControlModes.push_back(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
1479 }
1480
1481 // TODO(b/29394024): query V4L2_CID_COLORFX.
1482 mEffects.push_back(ANDROID_CONTROL_EFFECT_MODE_OFF);
1483
1484 // TODO(b/29394024): query V4L2_CID_FLASH_INDICATOR_INTENSITY.
1485 // For now, no indicator LED available; nothing to push back.
1486 // When there is, push back ANDROID_LED_AVAILABLE_LEDS_TRANSMIT.
1487
1488 // TODO(b/29394024): query V4L2_CID_IMAGE_STABILIZATION.
1489 mOpticalStabilizationModes.push_back(
1490 ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF);
1491 mVideoStabilizationModes.push_back(
1492 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF);
1493
1494 // Need to support YUV_420_888 and JPEG.
1495 // TODO(b/29394024): query VIDIOC_ENUM_FMT.
1496 std::vector<uint32_t> formats = {{V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420}};
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001497 // We want to find the smallest maximum frame duration amongst formats.
1498 mMaxFrameDuration = std::numeric_limits<int64_t>::max();
1499 int64_t min_yuv_frame_duration = std::numeric_limits<int64_t>::max();
1500 for (auto format : formats) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001501 int32_t hal_format = V4L2ToHalPixelFormat(format);
1502 if (hal_format < 0) {
1503 // Unrecognized/unused format. Skip it.
1504 continue;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001505 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001506 // TODO(b/29394024): query VIDIOC_ENUM_FRAMESIZES.
1507 // For now, just 640x480.
1508 ArrayVector<int32_t, 2> frame_sizes;
1509 frame_sizes.push_back({{640, 480}});
1510 size_t num_frame_sizes = frame_sizes.num_arrays();
1511 for (size_t i = 0; i < num_frame_sizes; ++i) {
1512 const int32_t* frame_size = frame_sizes[i];
1513 mStreamConfigs.push_back({{hal_format, frame_size[0], frame_size[1],
1514 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT}});
1515
1516 // TODO(b/29394024): query VIDIOC_ENUM_FRAMEINTERVALS.
1517 // For now, using the emulator max value of .3 sec.
1518 int64_t max_frame_duration = 300000000;
1519 // For now, min value of 30 fps = 1/30 spf ~= 33333333 ns.
1520 // For whatever reason the goldfish/qcom cameras report this as
1521 // 33331760, so copying that.
1522 int64_t min_frame_duration = 33331760;
1523
1524 mMinFrameDurations.push_back(
1525 {{hal_format, frame_size[0], frame_size[1], min_frame_duration}});
1526
1527 // In theory max frame duration (min frame rate) should be consistent
1528 // between all formats, but we check and only advertise the smallest
1529 // available max duration just in case.
1530 if (max_frame_duration < mMaxFrameDuration) {
1531 mMaxFrameDuration = max_frame_duration;
1532 }
1533
1534 // We only care about min frame duration (max frame rate) for YUV.
1535 if (hal_format == HAL_PIXEL_FORMAT_YCbCr_420_888 &&
1536 min_frame_duration < min_yuv_frame_duration) {
1537 min_yuv_frame_duration = min_frame_duration;
1538 }
1539
1540 // Usually 0 for non-jpeg, non-zero for JPEG.
1541 // Randomly choosing absurd 1 sec for JPEG. Unsure what this breaks.
1542 int64_t stall_duration = 0;
1543 if (hal_format == HAL_PIXEL_FORMAT_BLOB) {
1544 stall_duration = 1000000000;
1545 }
1546 mStallDurations.push_back(
1547 {{hal_format, frame_size[0], frame_size[1], stall_duration}});
1548 }
1549 }
1550
1551 // This should be at minimum {mi, ma}, {ma, ma} where mi and ma
1552 // are min and max frame rates for YUV_420_888. Min should be at most 15.
1553 // Convert from frame durations measured in ns.
1554 int32_t min_yuv_fps = 1000000000 / mMaxFrameDuration;
1555 if (min_yuv_fps > 15) {
1556 return -ENODEV;
1557 }
1558 int32_t max_yuv_fps = 1000000000 / min_yuv_frame_duration;
1559 mFpsRanges.push_back({{min_yuv_fps, max_yuv_fps}});
1560 mFpsRanges.push_back({{max_yuv_fps, max_yuv_fps}});
1561 // Always advertise {30, 30} if max is even higher,
1562 // since this is what the default video requests use.
1563 if (max_yuv_fps > 30) {
1564 mFpsRanges.push_back({{30, 30}});
1565 }
1566
1567 mCharacteristicsInitialized = true;
1568 return 0;
1569}
1570
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001571int V4L2Camera::V4L2ToHalPixelFormat(uint32_t v4l2_format) {
1572 // Translate V4L2 format to HAL format.
1573 int hal_format = -1;
1574 switch (v4l2_format) {
1575 case V4L2_PIX_FMT_JPEG:
1576 hal_format = HAL_PIXEL_FORMAT_BLOB;
1577 break;
1578 case V4L2_PIX_FMT_YUV420:
1579 hal_format = HAL_PIXEL_FORMAT_YCbCr_420_888;
1580 break;
1581 default:
1582 // Unrecognized format.
1583 break;
1584 }
1585 return hal_format;
1586}
1587
1588uint32_t V4L2Camera::halToV4L2PixelFormat(int hal_format) {
1589 // Translate HAL format to V4L2 format.
1590 uint32_t v4l2_format = 0;
1591 switch (hal_format) {
1592 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: // fall-through.
1593 case HAL_PIXEL_FORMAT_YCbCr_420_888:
1594 v4l2_format = V4L2_PIX_FMT_YUV420;
1595 break;
1596 case HAL_PIXEL_FORMAT_BLOB:
1597 v4l2_format = V4L2_PIX_FMT_JPEG;
1598 break;
1599 default:
1600 // Unrecognized format.
1601 break;
1602 }
1603 return v4l2_format;
1604}
1605
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001606} // namespace v4l2_camera_hal