blob: f27dc1f1dc622af1aa29a382f0720b2bd5004778 [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"
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070031#include "V4L2Gralloc.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070032
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070033#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
34
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070035namespace v4l2_camera_hal {
36
Ari Hausman-Cohendde80172016-07-01 16:20:36 -070037// Helper function for managing metadata.
38static std::vector<int32_t> getMetadataKeys(
39 const camera_metadata_t* metadata) {
40 std::vector<int32_t> keys;
41 size_t num_entries = get_camera_metadata_entry_count(metadata);
42 for (size_t i = 0; i < num_entries; ++i) {
43 camera_metadata_ro_entry_t entry;
44 get_camera_metadata_ro_entry(metadata, i, &entry);
45 keys.push_back(entry.tag);
46 }
47 return keys;
48}
49
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070050V4L2Camera::V4L2Camera(int id, std::string path)
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070051 : default_camera_hal::Camera(id),
52 mDevicePath(std::move(path)),
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -070053 mOutStreamType(0),
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070054 mOutStreamFormat(0),
55 mOutStreamWidth(0),
56 mOutStreamHeight(0),
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070057 mOutStreamBytesPerLine(0),
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070058 mOutStreamMaxBuffers(0),
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070059 mTemplatesInitialized(false),
60 mCharacteristicsInitialized(false) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070061 HAL_LOG_ENTER();
62}
63
64V4L2Camera::~V4L2Camera() {
65 HAL_LOG_ENTER();
66}
67
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070068// Helper function. Should be used instead of ioctl throughout this class.
69template<typename T>
70int V4L2Camera::ioctlLocked(int request, T data) {
71 android::Mutex::Autolock al(mDeviceLock);
72 if (mDeviceFd.get() < 0) {
73 return -ENODEV;
74 }
75 return TEMP_FAILURE_RETRY(ioctl(mDeviceFd.get(), request, data));
76}
77
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070078int V4L2Camera::connect() {
79 HAL_LOG_ENTER();
80
81 if (mDeviceFd.get() >= 0) {
82 HAL_LOGE("Camera device %s is opened. Close it first", mDevicePath.c_str());
83 return -EIO;
84 }
85
86 int fd = TEMP_FAILURE_RETRY(open(mDevicePath.c_str(), O_RDWR));
87 if (fd < 0) {
88 HAL_LOGE("failed to open %s (%s)", mDevicePath.c_str(), strerror(errno));
89 return -errno;
90 }
91 mDeviceFd.reset(fd);
92
93 // TODO(b/29185945): confirm this is a supported device.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070094 // This is checked by the HAL, but the device at mDevicePath may
95 // not be the same one that was there when the HAL was loaded.
96 // (Alternatively, better hotplugging support may make this unecessary
97 // by disabling cameras that get disconnected and checking newly connected
98 // cameras, so connect() is never called on an unsupported camera)
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070099
100 // TODO(b/29158098): Inform service of any flashes that are no longer available
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700101 // because this camera is in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700102 return 0;
103}
104
105void V4L2Camera::disconnect() {
106 HAL_LOG_ENTER();
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700107 // TODO(b/29158098): Inform service of any flashes that are available again
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700108 // because this camera is no longer in use.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700109
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700110 mDeviceFd.reset();
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -0700111
112 // Clear out our variables tracking device settings.
113 mOutStreamType = 0;
114 mOutStreamFormat = 0;
115 mOutStreamWidth = 0;
116 mOutStreamHeight = 0;
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700117 mOutStreamBytesPerLine = 0;
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -0700118 mOutStreamMaxBuffers = 0;
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700119}
120
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700121int V4L2Camera::initStaticInfo(camera_metadata_t** out) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700122 HAL_LOG_ENTER();
123
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700124 android::status_t res;
125 // Device characteristics need to be queried prior
126 // to static info setup.
127 if (!mCharacteristicsInitialized) {
128 res = initCharacteristics();
129 if (res) {
130 return res;
131 }
132 }
133
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700134 android::CameraMetadata info;
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -0700135
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700136 // Static metadata characteristics from /system/media/camera/docs/docs.html.
137
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700138 /* android.colorCorrection. */
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700139
140 // No easy way to turn chromatic aberration correction OFF in v4l2,
141 // though this may be supportable via a collection of other user controls.
142 uint8_t avail_aberration_modes[] = {
143 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST,
144 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700145 res = info.update(ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
146 avail_aberration_modes, ARRAY_SIZE(avail_aberration_modes));
147 if (res != android::OK) {
148 return res;
149 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700150
151 /* android.control. */
152
153 /* 3As */
154
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700155 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
156 mAeAntibandingModes.data(), mAeAntibandingModes.size());
157 if (res != android::OK) {
158 return res;
159 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700160
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700161 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_MODES,
162 mAeModes.data(), mAeModes.size());
163 if (res != android::OK) {
164 return res;
165 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700166
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700167 // Flatten mFpsRanges.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700168 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
169 mFpsRanges.data(), mFpsRanges.total_num_elements());
170 if (res != android::OK) {
171 return res;
172 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700173
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700174 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
175 mAeCompensationRange.data(), mAeCompensationRange.size());
176 if (res != android::OK) {
177 return res;
178 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700179
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700180 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_STEP,
181 &mAeCompensationStep, 1);
182 if (res != android::OK) {
183 return res;
184 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700185
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700186 res = info.update(ANDROID_CONTROL_AF_AVAILABLE_MODES,
187 mAfModes.data(), mAfModes.size());
188 if (res != android::OK) {
189 return res;
190 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700191
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700192 res = info.update(ANDROID_CONTROL_AWB_AVAILABLE_MODES,
193 mAwbModes.data(), mAwbModes.size());
194 if (res != android::OK) {
195 return res;
196 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700197
198 // Couldn't find any V4L2 support for regions, though maybe it's out there.
199 int32_t max_regions[] = {/*AE*/ 0,/*AWB*/ 0,/*AF*/ 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700200 res = info.update(ANDROID_CONTROL_MAX_REGIONS,
201 max_regions, ARRAY_SIZE(max_regions));
202 if (res != android::OK) {
203 return res;
204 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700205
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700206 res = info.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE,
207 &mAeLockAvailable, 1);
208 if (res != android::OK) {
209 return res;
210 }
211 res = info.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
212 &mAwbLockAvailable, 1);
213 if (res != android::OK) {
214 return res;
215 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700216
217 /* Scene modes. */
218
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700219 res = info.update(ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
220 mSceneModes.data(), mSceneModes.size());
221 if (res != android::OK) {
222 return res;
223 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700224
225 // A 3-tuple of AE, AWB, AF overrides for each scene mode.
226 // Ignored for DISABLED, FACE_PRIORITY and FACE_PRIORITY_LOW_LIGHT.
227 uint8_t scene_mode_overrides[] = {
228 /*SCENE_MODE_DISABLED*/ /*AE*/0, /*AW*/0, /*AF*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700229 res = info.update(ANDROID_CONTROL_SCENE_MODE_OVERRIDES,
230 scene_mode_overrides, ARRAY_SIZE(scene_mode_overrides));
231 if (res != android::OK) {
232 return res;
233 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700234
235 /* Top level 3A/Scenes switch. */
236
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700237 res = info.update(ANDROID_CONTROL_AVAILABLE_MODES,
238 mControlModes.data(), mControlModes.size());
239 if (res != android::OK) {
240 return res;
241 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700242
243 /* Other android.control configuration. */
244
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700245 res = info.update(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
246 mVideoStabilizationModes.data(),
247 mVideoStabilizationModes.size());
248 if (res != android::OK) {
249 return res;
250 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700251
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700252 res = info.update(ANDROID_CONTROL_AVAILABLE_EFFECTS,
253 mEffects.data(), mEffects.size());
254 if (res != android::OK) {
255 return res;
256 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700257
258 // AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS only necessary
259 // for devices supporting CONSTRAINED_HIGH_SPEED_VIDEO,
260 // which this HAL doesn't support.
261
262 // POST_RAW_SENSITIVITY_BOOST_RANGE only necessary
263 // for devices supporting RAW format outputs.
264
265 /* android.edge. */
266
267 // Not sure if V4L2 does or doesn't do this, but HAL documentation says
268 // all devices must support FAST, and FAST can be equivalent to OFF, so
269 // either way it's fine to list.
270 uint8_t avail_edge_modes[] = {
271 ANDROID_EDGE_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700272 res = info.update(ANDROID_EDGE_AVAILABLE_EDGE_MODES,
273 avail_edge_modes, ARRAY_SIZE(avail_edge_modes));
274 if (res != android::OK) {
275 return res;
276 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700277
278 /* android.flash. */
279
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700280 res = info.update(ANDROID_FLASH_INFO_AVAILABLE,
281 &mFlashAvailable, 1);
282 if (res != android::OK) {
283 return res;
284 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700285
286 // info.chargeDuration, color.Temperature, maxEnergy marked FUTURE.
287
288 /* android.hotPixel. */
289
290 // No known V4L2 hot pixel correction. But it might be happening,
291 // so we report FAST/HIGH_QUALITY.
292 uint8_t avail_hot_pixel_modes[] = {
293 ANDROID_HOT_PIXEL_MODE_FAST,
294 ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700295 res = info.update(ANDROID_HOT_PIXEL_AVAILABLE_HOT_PIXEL_MODES,
296 avail_hot_pixel_modes, ARRAY_SIZE(avail_hot_pixel_modes));
297 if (res != android::OK) {
298 return res;
299 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700300
301 /* android.jpeg. */
302
303 // For now, no thumbnails available (only [0,0], the "no thumbnail" size).
304 // TODO(b/29580107): Could end up with a mismatch between request & result,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700305 // since V4L2 doesn't actually allow for thumbnail size control.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700306 int32_t thumbnail_sizes[] = {
307 0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700308 res = info.update(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
309 thumbnail_sizes, ARRAY_SIZE(thumbnail_sizes));
310 if (res != android::OK) {
311 return res;
312 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700313
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700314 // V4L2 doesn't support querying this,
315 // so instead use a constant (defined in V4L2Gralloc.h).
316 int32_t max_jpeg_size = V4L2_MAX_JPEG_SIZE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700317 res = info.update(ANDROID_JPEG_MAX_SIZE,
318 &max_jpeg_size, 1);
319 if (res != android::OK) {
320 return res;
321 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700322
323 /* android.lens. */
324
325 /* Misc. lens control. */
326
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700327 res = info.update(ANDROID_LENS_INFO_AVAILABLE_APERTURES,
328 &mAperture, 1);
329 if (res != android::OK) {
330 return res;
331 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700332
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700333 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES,
334 &mFilterDensity, 1);
335 if (res != android::OK) {
336 return res;
337 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700338
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700339 res = info.update(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
340 mOpticalStabilizationModes.data(),
341 mOpticalStabilizationModes.size());
342 if (res != android::OK) {
343 return res;
344 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700345
346 // No known V4L2 shading map info.
347 int32_t shading_map_size[] = {1, 1};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700348 res = info.update(ANDROID_LENS_INFO_SHADING_MAP_SIZE,
349 shading_map_size, ARRAY_SIZE(shading_map_size));
350 if (res != android::OK) {
351 return res;
352 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700353
354 // All V4L2 devices are considered to be external facing.
355 uint8_t facing = ANDROID_LENS_FACING_EXTERNAL;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700356 res = info.update(ANDROID_LENS_FACING, &facing, 1);
357 if (res != android::OK) {
358 return res;
359 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700360
361 /* Zoom/Focus. */
362
363 // No way to actually get the focal length in V4L2, but it's a required key,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700364 // so we just fake it.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700365 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
366 &mFocalLength, 1);
367 if (res != android::OK) {
368 return res;
369 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700370
371 // V4L2 focal units do not correspond to a particular physical unit.
372 uint8_t focus_calibration =
373 ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700374 res = info.update(ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
375 &focus_calibration, 1);
376 if (res != android::OK) {
377 return res;
378 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700379
380 // info.hyperfocalDistance not required for UNCALIBRATED.
381
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700382 res = info.update(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
383 &mFocusDistance, 1);
384 if (res != android::OK) {
385 return res;
386 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700387
388 /* Depth. */
389
390 // DEPTH capability not supported by this HAL. Not implemented:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700391 // poseRotation
392 // poseTranslation
393 // intrinsicCalibration
394 // radialDistortion
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700395
396 /* anroid.noise. */
397
398 // Unable to control noise reduction in V4L2 devices,
399 // but FAST is allowed to be the same as OFF.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700400 uint8_t avail_noise_reduction_modes[] = {ANDROID_NOISE_REDUCTION_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700401 res = info.update(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
402 avail_noise_reduction_modes,
403 ARRAY_SIZE(avail_noise_reduction_modes));
404 if (res != android::OK) {
405 return res;
406 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700407
408 /* android.request. */
409
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700410 int32_t max_num_output_streams[] = {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700411 mMaxRawOutputStreams, mMaxYuvOutputStreams, mMaxJpegOutputStreams};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700412 res = info.update(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
413 max_num_output_streams, ARRAY_SIZE(max_num_output_streams));
414 if (res != android::OK) {
415 return res;
416 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700417
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700418 res = info.update(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS,
419 &mMaxInputStreams, 1);
420 if (res != android::OK) {
421 return res;
422 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700423
424 // No way to know for V4L2, so fake with max allowable latency.
425 // Doesn't mean much without per-frame controls.
426 uint8_t pipeline_max_depth = 4;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700427 res = info.update(ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
428 &pipeline_max_depth, 1);
429 if (res != android::OK) {
430 return res;
431 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700432
433 // Partial results not supported; partialResultCount defaults to 1.
434
435 // Available capabilities & keys queried at very end of this method.
436
437 /* android.scaler. */
438
439 /* Cropping. */
440
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700441 res = info.update(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
442 &mMaxZoom, 1);
443 if (res != android::OK) {
444 return res;
445 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700446
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700447 res = info.update(ANDROID_SCALER_CROPPING_TYPE, &mCropType, 1);
448 if (res != android::OK) {
449 return res;
450 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700451
452 /* Streams. */
453
454 // availableInputOutputFormatsMap only required for reprocessing capability.
455
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700456 // Flatten mStreamConfigs.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700457 res = info.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
458 mStreamConfigs.data(),
459 mStreamConfigs.total_num_elements());
460 if (res != android::OK) {
461 return res;
462 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700463
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700464 // Flatten mMinFrameDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700465 res = info.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
466 mMinFrameDurations.data(),
467 mMinFrameDurations.total_num_elements());
468 if (res != android::OK) {
469 return res;
470 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700471
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700472 // Flatten mStallDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700473 res = info.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
474 mStallDurations.data(),
475 mStallDurations.total_num_elements());
476 if (res != android::OK) {
477 return res;
478 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700479
480 /* android.sensor. */
481
482 /* Sizes. */
483
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700484 res = info.update(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
485 mPixelArraySize.data(), mPixelArraySize.size());
486 if (res != android::OK) {
487 return res;
488 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700489 // No V4L2 way to differentiate active vs. inactive parts of the rectangle.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700490 res = info.update(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
491 mPixelArraySize.data(), mPixelArraySize.size());
492 if (res != android::OK) {
493 return res;
494 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700495
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700496 res = info.update(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
497 mPhysicalSize.data(), mPhysicalSize.size());
498 if (res != android::OK) {
499 return res;
500 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700501
502 /* Misc sensor information. */
503
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700504 res = info.update(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
505 &mMaxFrameDuration, 1);
506 if (res != android::OK) {
507 return res;
508 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700509
510 // HAL uses BOOTTIME timestamps.
511 // TODO(b/29457051): make sure timestamps are consistent throughout the HAL.
512 uint8_t timestamp_source = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700513 res = info.update(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
514 &timestamp_source, 1);
515 if (res != android::OK) {
516 return res;
517 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700518
519 // As in initDeviceInfo, no way to actually get orientation.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700520 res = info.update(ANDROID_SENSOR_ORIENTATION, &mOrientation, 1);
521 if (res != android::OK) {
522 return res;
523 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700524
525 // availableTestPatternModes just defaults to OFF, which is fine.
526
527 // info.exposureTimeRange, info.sensitivityRange:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700528 // exposure/sensitivity manual control not supported.
529 // Could query V4L2_CID_ISO_SENSITIVITY to support sensitivity if desired.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700530
531 // info.whiteLevel, info.lensShadingApplied,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700532 // info.preCorrectionPixelArraySize, referenceIlluminant1/2,
533 // calibrationTransform1/2, colorTransform1/2, forwardMatrix1/2,
534 // blackLevelPattern, profileHueSatMapDimensions
535 // all only necessary for RAW.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700536
537 // baseGainFactor marked FUTURE.
538
539 // maxAnalogSensitivity optional for LIMITED device.
540
541 // opticalBlackRegions: No known way to get in V4L2, but not necessary.
542
543 // opaqueRawSize not necessary since RAW_OPAQUE format not supported.
544
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700545 /* android.shading */
546
547 // No known V4L2 lens shading. But it might be happening,
548 // so we report FAST/HIGH_QUALITY.
549 uint8_t avail_shading_modes[] = {
550 ANDROID_SHADING_MODE_FAST,
551 ANDROID_SHADING_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700552 res = info.update(ANDROID_SHADING_AVAILABLE_MODES,
553 avail_shading_modes, ARRAY_SIZE(avail_shading_modes));
554 if (res != android::OK) {
555 return res;
556 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700557
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700558 /* android.statistics */
559
560 // Face detection not supported.
561 uint8_t avail_face_detect_modes[] = {
562 ANDROID_STATISTICS_FACE_DETECT_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700563 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
564 avail_face_detect_modes,
565 ARRAY_SIZE(avail_face_detect_modes));
566 if (res != android::OK) {
567 return res;
568 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700569
570 int32_t max_face_count = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700571 res = info.update(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
572 &max_face_count, 1);
573 if (res != android::OK) {
574 return res;
575 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700576
577 // info.histogramBucketCount, info.maxHistogramCount,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700578 // info.maxSharpnessMapValue, info.sharpnessMapSizemarked FUTURE.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700579
580 // ON only needs to be supported for RAW capable devices.
581 uint8_t avail_hot_pixel_map_modes[] = {
582 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700583 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
584 avail_hot_pixel_map_modes,
585 ARRAY_SIZE(avail_hot_pixel_map_modes));
586 if (res != android::OK) {
587 return res;
588 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700589
590 // ON only needs to be supported for RAW capable devices.
591 uint8_t avail_lens_shading_map_modes[] = {
592 ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700593 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
594 avail_lens_shading_map_modes,
595 ARRAY_SIZE(avail_lens_shading_map_modes));
596 if (res != android::OK) {
597 return res;
598 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700599
600 /* android.tonemap. */
601
602 // tonemapping only required for MANUAL_POST_PROCESSING capability.
603
604 /* android.led. */
605
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700606 // May or may not have LEDs available.
607 if (!mLeds.empty()) {
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700608 res = info.update(ANDROID_LED_AVAILABLE_LEDS, mLeds.data(), mLeds.size());
609 if (res != android::OK) {
610 return res;
611 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700612 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700613
614 /* android.sync. */
615
616 // "LIMITED devices are strongly encouraged to use a non-negative value.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700617 // If UNKNOWN is used here then app developers do not have a way to know
618 // when sensor settings have been applied." - Unfortunately, V4L2 doesn't
619 // really help here either. Could even be that adjusting settings mid-stream
620 // blocks in V4L2, and should be avoided.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700621 int32_t max_latency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700622 res = info.update(ANDROID_SYNC_MAX_LATENCY,
623 &max_latency, 1);
624 if (res != android::OK) {
625 return res;
626 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700627
628 /* android.reprocess. */
629
630 // REPROCESSING not supported by this HAL.
631
632 /* android.depth. */
633
634 // DEPTH not supported by this HAL.
635
636 /* Capabilities and android.info. */
637
638 uint8_t hw_level = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700639 res = info.update(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
640 &hw_level, 1);
641 if (res != android::OK) {
642 return res;
643 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700644
645 uint8_t capabilities[] = {
646 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700647 res = info.update(ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
648 capabilities, ARRAY_SIZE(capabilities));
649 if (res != android::OK) {
650 return res;
651 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700652
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700653 // Scan a default request template for included request keys.
654 if (!mTemplatesInitialized) {
655 res = initTemplates();
656 if (res) {
657 return res;
658 }
659 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700660 const camera_metadata_t* preview_request = nullptr;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700661 // Search templates from the beginning for a supported one.
662 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
663 ++template_id) {
664 preview_request = constructDefaultRequestSettings(template_id);
665 if (preview_request != nullptr) {
666 break;
667 }
668 }
669 if (preview_request == nullptr) {
670 HAL_LOGE("No valid templates, can't get request keys.");
671 return -ENODEV;
672 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700673 std::vector<int32_t> avail_request_keys = getMetadataKeys(preview_request);
674 res = info.update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
675 avail_request_keys.data(), avail_request_keys.size());
676 if (res != android::OK) {
677 return res;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700678 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700679
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700680 // Result keys will be duplicated from the request, plus a few extras.
Ari Hausman-Cohen2934eb92016-07-20 17:25:39 +0000681 // TODO(b/29335262): additonal available result keys.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700682 std::vector<int32_t> avail_result_keys(avail_request_keys);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700683 res = info.update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
684 avail_result_keys.data(), avail_result_keys.size());
685 if (res != android::OK) {
686 return res;
687 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700688
689 // Last thing, once all the available characteristics have been added.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700690 const camera_metadata_t* static_characteristics = info.getAndLock();
691 std::vector<int32_t> avail_characteristics_keys =
692 getMetadataKeys(static_characteristics);
693 res = info.unlock(static_characteristics);
694 if (res != android::OK) {
695 return res;
696 }
697 avail_characteristics_keys.push_back(
698 ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
699 res = info.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
700 avail_characteristics_keys.data(),
701 avail_characteristics_keys.size());
702 if (res != android::OK) {
703 return res;
704 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700705
706 *out = info.release();
707 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700708}
709
710void V4L2Camera::initDeviceInfo(camera_info_t* info) {
711 HAL_LOG_ENTER();
712
713 // For now, just constants.
714 info->facing = CAMERA_FACING_EXTERNAL;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700715 info->orientation = mOrientation;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700716 info->resource_cost = 100;
717 info->conflicting_devices = nullptr;
718 info->conflicting_devices_length = 0;
719}
720
721int V4L2Camera::initDevice() {
722 HAL_LOG_ENTER();
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700723 int res;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700724
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700725 // Initialize and check the gralloc module.
726 const hw_module_t* module;
727 res = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
728 if (res) {
729 HAL_LOGE("Couldn't get gralloc module.");
730 return -ENODEV;
731 }
732 const gralloc_module_t* gralloc =
733 reinterpret_cast<const gralloc_module_t*>(module);
734 mGralloc = V4L2Gralloc(gralloc);
735 if (!mGralloc.isValid()) {
736 HAL_LOGE("Invalid gralloc module.");
737 return -ENODEV;
738 }
739
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700740 // Templates should be set up if they haven't already been.
741 if (!mTemplatesInitialized) {
742 res = initTemplates();
743 if (res) {
744 return res;
745 }
746 }
747
748 return 0;
749}
750
751int V4L2Camera::initTemplates() {
752 HAL_LOG_ENTER();
753 int res;
754
755 // Device characteristics need to be queried prior
756 // to template setup.
757 if (!mCharacteristicsInitialized) {
758 res = initCharacteristics();
759 if (res) {
760 return res;
761 }
762 }
763
764 // Note: static metadata expects all templates/requests
765 // to provide values for all supported keys.
766
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700767 android::CameraMetadata base_metadata;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700768
769 // Start with defaults for all templates.
770
771 /* android.colorCorrection. */
772
773 uint8_t aberration_mode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700774 res = base_metadata.update(ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
775 &aberration_mode, 1);
776 if (res != android::OK) {
777 return res;
778 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700779
780 uint8_t color_correction_mode = ANDROID_COLOR_CORRECTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700781 res = base_metadata.update(ANDROID_COLOR_CORRECTION_MODE,
782 &color_correction_mode, 1);
783 if (res != android::OK) {
784 return res;
785 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700786
787 // transform and gains are for the unsupported MANUAL_POST_PROCESSING only.
788
789 /* android.control. */
790
791 /* AE. */
792 uint8_t ae_antibanding_mode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700793 res = base_metadata.update(ANDROID_CONTROL_AE_ANTIBANDING_MODE,
794 &ae_antibanding_mode, 1);
795 if (res != android::OK) {
796 return res;
797 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700798
799 // Only matters if AE_MODE = OFF
800 int32_t ae_exposure_compensation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700801 res = base_metadata.update(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
802 &ae_exposure_compensation, 1);
803 if (res != android::OK) {
804 return res;
805 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700806
807 uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700808 res = base_metadata.update(ANDROID_CONTROL_AE_LOCK, &ae_lock, 1);
809 if (res != android::OK) {
810 return res;
811 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700812
813 uint8_t ae_mode = ANDROID_CONTROL_AE_MODE_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700814 res = base_metadata.update(ANDROID_CONTROL_AE_MODE, &ae_mode, 1);
815 if (res != android::OK) {
816 return res;
817 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700818
819 // AE regions not supported.
820
821 // FPS set per-template.
822
823 uint8_t ae_precapture_trigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700824 res = base_metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
825 &ae_precapture_trigger, 1);
826 if (res != android::OK) {
827 return res;
828 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700829
830 /* AF. */
831
832 // AF mode set per-template.
833
834 // AF regions not supported.
835
836 uint8_t af_trigger = ANDROID_CONTROL_AF_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700837 res = base_metadata.update(ANDROID_CONTROL_AF_TRIGGER, &af_trigger, 1);
838 if (res != android::OK) {
839 return res;
840 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700841
842 /* AWB. */
843
844 // Priority: auto > off > Whatever is available.
845 uint8_t default_awb_mode = mAwbModes[0];
846 if (std::count(mAwbModes.begin(), mAwbModes.end(),
847 ANDROID_CONTROL_AWB_MODE_AUTO)) {
848 default_awb_mode = ANDROID_CONTROL_AWB_MODE_AUTO;
849 } else if (std::count(mAwbModes.begin(), mAwbModes.end(),
850 ANDROID_CONTROL_AWB_MODE_OFF)) {
851 default_awb_mode = ANDROID_CONTROL_AWB_MODE_OFF;
852 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700853 res = base_metadata.update(ANDROID_CONTROL_AWB_MODE, &default_awb_mode, 1);
854 if (res != android::OK) {
855 return res;
856 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700857
858 // AWB regions not supported.
859
860 /* Other controls. */
861
862 uint8_t effect_mode = ANDROID_CONTROL_EFFECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700863 res = base_metadata.update(ANDROID_CONTROL_EFFECT_MODE, &effect_mode, 1);
864 if (res != android::OK) {
865 return res;
866 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700867
868 uint8_t control_mode = ANDROID_CONTROL_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700869 res = base_metadata.update(ANDROID_CONTROL_MODE, &control_mode, 1);
870 if (res != android::OK) {
871 return res;
872 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700873
874 uint8_t scene_mode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700875 res = base_metadata.update(ANDROID_CONTROL_SCENE_MODE,
876 &scene_mode, 1);
877 if (res != android::OK) {
878 return res;
879 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700880
881 uint8_t video_stabilization = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700882 res = base_metadata.update(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
883 &video_stabilization, 1);
884 if (res != android::OK) {
885 return res;
886 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700887
888 // postRawSensitivityBoost: RAW not supported, leave null.
889
890 /* android.demosaic. */
891
892 // mode marked FUTURE.
893
894 /* android.edge. */
895
896 uint8_t edge_mode = ANDROID_EDGE_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700897 res = base_metadata.update(ANDROID_EDGE_MODE, &edge_mode, 1);
898 if (res != android::OK) {
899 return res;
900 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700901
902 // strength marked FUTURE.
903
904 /* android.flash. */
905
906 // firingPower, firingTime marked FUTURE.
907
908 uint8_t flash_mode = ANDROID_FLASH_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700909 res = base_metadata.update(ANDROID_FLASH_MODE, &flash_mode, 1);
910 if (res != android::OK) {
911 return res;
912 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700913
914 /* android.hotPixel. */
915
916 uint8_t hp_mode = ANDROID_HOT_PIXEL_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700917 res = base_metadata.update(ANDROID_HOT_PIXEL_MODE, &hp_mode, 1);
918 if (res != android::OK) {
919 return res;
920 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700921
922 /* android.jpeg. */
923
924 double gps_coords[] = {/*latitude*/0, /*longitude*/0, /*altitude*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700925 res = base_metadata.update(ANDROID_JPEG_GPS_COORDINATES, gps_coords, 3);
926 if (res != android::OK) {
927 return res;
928 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700929
930 uint8_t gps_processing_method[] = "none";
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700931 res = base_metadata.update(ANDROID_JPEG_GPS_PROCESSING_METHOD,
932 gps_processing_method,
933 ARRAY_SIZE(gps_processing_method));
934 if (res != android::OK) {
935 return res;
936 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700937
938 int64_t gps_timestamp = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700939 res = base_metadata.update(ANDROID_JPEG_GPS_TIMESTAMP, &gps_timestamp, 1);
940 if (res != android::OK) {
941 return res;
942 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700943
944 // JPEG orientation is relative to sensor orientation (mOrientation).
945 int32_t jpeg_orientation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700946 res = base_metadata.update(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1);
947 if (res != android::OK) {
948 return res;
949 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700950
951 // 1-100, larger is higher quality.
952 uint8_t jpeg_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700953 res = base_metadata.update(ANDROID_JPEG_QUALITY, &jpeg_quality, 1);
954 if (res != android::OK) {
955 return res;
956 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700957
958 // TODO(b/29580107): If thumbnail quality actually matters/can be adjusted,
959 // adjust this.
960 uint8_t thumbnail_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700961 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_QUALITY,
962 &thumbnail_quality, 1);
963 if (res != android::OK) {
964 return res;
965 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700966
967 // TODO(b/29580107): Choose a size matching the resolution.
968 int32_t thumbnail_size[] = {0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700969 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_SIZE, thumbnail_size, 2);
970 if (res != android::OK) {
971 return res;
972 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700973
974 /* android.lens. */
975
976 // Fixed values.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700977 res = base_metadata.update(ANDROID_LENS_APERTURE, &mAperture, 1);
978 if (res != android::OK) {
979 return res;
980 }
981 res = base_metadata.update(ANDROID_LENS_FILTER_DENSITY, &mFilterDensity, 1);
982 if (res != android::OK) {
983 return res;
984 }
985 res = base_metadata.update(ANDROID_LENS_FOCAL_LENGTH, &mFocalLength, 1);
986 if (res != android::OK) {
987 return res;
988 }
989 res = base_metadata.update(ANDROID_LENS_FOCUS_DISTANCE, &mFocusDistance, 1);
990 if (res != android::OK) {
991 return res;
992 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700993
994 uint8_t optical_stabilization = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700995 res = base_metadata.update(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700996 &optical_stabilization, 1);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700997 if (res != android::OK) {
998 return res;
999 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001000
1001 /* android.noiseReduction. */
1002
1003 uint8_t noise_reduction_mode = ANDROID_NOISE_REDUCTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001004 res = base_metadata.update(ANDROID_NOISE_REDUCTION_MODE,
1005 &noise_reduction_mode, 1);
1006 if (res != android::OK) {
1007 return res;
1008 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001009
1010 // strength marked FUTURE.
1011
1012 /* android.request. */
1013
1014 // Request Id unused by the HAL for now, and these are just
1015 // templates, so just fill it in with a dummy.
1016 int32_t id = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001017 res = base_metadata.update(ANDROID_REQUEST_ID, &id, 1);
1018 if (res != android::OK) {
1019 return res;
1020 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001021
1022 // metadataMode marked FUTURE.
1023
1024 /* android.scaler. */
1025
1026 // No cropping by default; use the full active array.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001027 res = base_metadata.update(ANDROID_SCALER_CROP_REGION, mPixelArraySize.data(),
1028 mPixelArraySize.size());
1029 if (res != android::OK) {
1030 return res;
1031 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001032
1033 /* android.sensor. */
1034
1035 // exposureTime, sensitivity, testPattern[Data,Mode] not supported.
1036
1037 // Ignored when AE is OFF.
1038 int64_t frame_duration = 33333333L; // 1/30 s.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001039 res = base_metadata.update(ANDROID_SENSOR_FRAME_DURATION, &frame_duration, 1);
1040 if (res != android::OK) {
1041 return res;
1042 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001043
1044 /* android.shading. */
1045
1046 uint8_t shading_mode = ANDROID_SHADING_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001047 res = base_metadata.update(ANDROID_SHADING_MODE, &shading_mode, 1);
1048 if (res != android::OK) {
1049 return res;
1050 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001051
1052 /* android.statistics. */
1053
1054 uint8_t face_detect_mode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001055 res = base_metadata.update(ANDROID_STATISTICS_FACE_DETECT_MODE,
1056 &face_detect_mode, 1);
1057 if (res != android::OK) {
1058 return res;
1059 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001060
1061 // histogramMode, sharpnessMapMode marked FUTURE.
1062
1063 uint8_t hp_map_mode = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001064 res = base_metadata.update(ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
1065 &hp_map_mode, 1);
1066 if (res != android::OK) {
1067 return res;
1068 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001069
1070 uint8_t lens_shading_map_mode = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001071 res = base_metadata.update(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
1072 &lens_shading_map_mode, 1);
1073 if (res != android::OK) {
1074 return res;
1075 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001076
1077 /* android.tonemap. */
1078
1079 // Tonemap only required for MANUAL_POST_PROCESSING capability.
1080
1081 /* android.led. */
1082
1083 uint8_t transmit = ANDROID_LED_TRANSMIT_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001084 res = base_metadata.update(ANDROID_LED_TRANSMIT, &transmit, 1);
1085 if (res != android::OK) {
1086 return res;
1087 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001088
1089 /* android.reprocess */
1090
1091 // Only needed for REPROCESS capability.
1092
1093
1094 /* Template variable values. */
1095
1096 // Find the FPS ranges "closest" to a desired range
1097 // (minimum abs distance from min to min and max to max).
1098 // Find both a fixed rate and a variable rate, for different purposes.
1099 std::array<int32_t, 2> desired_flat_fps_range = {{30, 30}};
1100 std::array<int32_t, 2> desired_variable_fps_range = {{5, 30}};
1101 std::array<int32_t, 2> flat_fps_range;
1102 std::array<int32_t, 2> variable_fps_range;
1103 int32_t best_flat_distance = std::numeric_limits<int32_t>::max();
1104 int32_t best_variable_distance = std::numeric_limits<int32_t>::max();
1105 size_t num_fps_ranges = mFpsRanges.num_arrays();
1106 for (size_t i = 0; i < num_fps_ranges; ++i) {
1107 const int32_t* range = mFpsRanges[i];
1108 // Variable fps.
1109 int32_t distance = std::abs(range[0] - desired_variable_fps_range[0]) +
1110 std::abs(range[1] - desired_variable_fps_range[1]);
1111 if (distance < best_variable_distance) {
1112 variable_fps_range[0] = range[0];
1113 variable_fps_range[1] = range[1];
1114 best_variable_distance = distance;
1115 }
1116 // Flat fps. Only do if range is actually flat.
1117 // Note at least one flat range is required,
1118 // so something will always be filled in.
1119 if (range[0] == range[1]) {
1120 distance = std::abs(range[0] - desired_flat_fps_range[0]) +
1121 std::abs(range[1] - desired_flat_fps_range[1]);
1122 if (distance < best_flat_distance) {
1123 flat_fps_range[0] = range[0];
1124 flat_fps_range[1] = range[1];
1125 best_flat_distance = distance;
1126 }
1127 }
1128 }
1129
1130 // Priority: continuous > auto > off > whatever is available.
1131 bool continuous_still_avail = std::count(
1132 mAfModes.begin(), mAfModes.end(),
1133 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE);
1134 bool continuous_video_avail = std::count(
1135 mAfModes.begin(), mAfModes.end(),
1136 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO);
1137 uint8_t non_continuous_af_mode = mAfModes[0];
1138 if (std::count(mAfModes.begin(), mAfModes.end(),
1139 ANDROID_CONTROL_AF_MODE_AUTO)) {
1140 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_AUTO;
1141 } else if (std::count(mAfModes.begin(), mAfModes.end(),
1142 ANDROID_CONTROL_AF_MODE_OFF)) {
1143 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_OFF;
1144 }
1145 uint8_t still_af_mode = continuous_still_avail ?
1146 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE : non_continuous_af_mode;
1147 uint8_t video_af_mode = continuous_video_avail ?
1148 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO : non_continuous_af_mode;
1149
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001150 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
1151 ++template_id) {
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001152 // General differences/support.
1153 uint8_t intent;
1154 uint8_t af_mode;
1155 std::array<int32_t, 2> fps_range;
1156 switch(template_id) {
1157 case CAMERA3_TEMPLATE_PREVIEW:
1158 intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
1159 af_mode = still_af_mode;
1160 fps_range = flat_fps_range;
1161 break;
1162 case CAMERA3_TEMPLATE_STILL_CAPTURE:
1163 intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
1164 af_mode = still_af_mode;
1165 fps_range = variable_fps_range;
1166 break;
1167 case CAMERA3_TEMPLATE_VIDEO_RECORD:
1168 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
1169 af_mode = video_af_mode;
1170 fps_range = flat_fps_range;
1171 break;
1172 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
1173 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
1174 af_mode = video_af_mode;
1175 fps_range = flat_fps_range;
1176 break;
1177 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: // Fall through.
1178 case CAMERA3_TEMPLATE_MANUAL: // Fall though.
1179 default:
1180 // Unsupported/unrecognized. Don't add this template; skip it.
1181 continue;
1182 }
1183
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001184 // Copy our base metadata and add the new items.
1185 android::CameraMetadata template_metadata(base_metadata);
1186 res = template_metadata.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent, 1);
1187 if (res != android::OK) {
1188 return res;
1189 }
1190 res = template_metadata.update(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
1191 fps_range.data(), fps_range.size());
1192 if (res != android::OK) {
1193 return res;
1194 }
1195 res = template_metadata.update(ANDROID_CONTROL_AF_MODE, &af_mode, 1);
1196 if (res != android::OK) {
1197 return res;
1198 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001199
1200 const camera_metadata_t* template_raw_metadata =
1201 template_metadata.getAndLock();
1202 res = setTemplate(template_id, template_raw_metadata);
1203 if (res != android::OK) {
1204 return res;
1205 }
1206 res = template_metadata.unlock(template_raw_metadata);
1207 if (res != android::OK) {
1208 return res;
1209 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001210 }
1211
1212 mTemplatesInitialized = true;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001213 return 0;
1214}
1215
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001216bool V4L2Camera::isSupportedStreamSet(default_camera_hal::Stream** streams,
1217 int count, uint32_t mode) {
1218 HAL_LOG_ENTER();
1219
1220 if (mode != CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE) {
1221 HAL_LOGE("Unsupported stream configuration mode: %d", mode);
1222 return false;
1223 }
1224
1225 // This should be checked by the caller, but put here as a sanity check.
1226 if (count < 1) {
1227 HAL_LOGE("Must request at least 1 stream");
1228 return false;
1229 }
1230
1231 // Count the number of streams of each type.
1232 int32_t num_input = 0;
1233 int32_t num_raw = 0;
1234 int32_t num_yuv = 0;
1235 int32_t num_jpeg = 0;
1236 for (int i = 0; i < count; ++i) {
1237 default_camera_hal::Stream* stream = streams[i];
1238
1239 if (stream->isInputType()) {
1240 ++num_input;
1241 }
1242
1243 if (stream->isOutputType()) {
1244 switch (halToV4L2PixelFormat(stream->getFormat())) {
1245 case V4L2_PIX_FMT_YUV420:
1246 ++num_yuv;
1247 break;
1248 case V4L2_PIX_FMT_JPEG:
1249 ++num_jpeg;
1250 break;
1251 default:
1252 // Note: no supported raw formats at this time.
1253 HAL_LOGE("Unsupported format for stream %d: %d", i, stream->getFormat());
1254 return false;
1255 }
1256 }
1257 }
1258
1259 if (num_input > mMaxInputStreams || num_raw > mMaxRawOutputStreams ||
1260 num_yuv > mMaxYuvOutputStreams || num_jpeg > mMaxJpegOutputStreams) {
1261 HAL_LOGE("Invalid stream configuration: %d input, %d RAW, %d YUV, %d JPEG "
1262 "(max supported: %d input, %d RAW, %d YUV, %d JPEG)",
1263 mMaxInputStreams, mMaxRawOutputStreams, mMaxYuvOutputStreams,
1264 mMaxJpegOutputStreams, num_input, num_raw, num_yuv, num_jpeg);
1265 return false;
1266 }
1267
1268 // TODO(b/29939583): The above logic should be all that's necessary,
1269 // but V4L2 doesn't actually support more than 1 stream at a time. So for now,
1270 // if not all streams are the same format and size, error. Note that this
1271 // means the HAL is not spec-compliant; the requested streams are technically
1272 // valid and it is not technically allowed to error once it has reached this
1273 // point.
1274 int format = streams[0]->getFormat();
1275 uint32_t width = streams[0]->getWidth();
1276 uint32_t height = streams[0]->getHeight();
1277 for (int i = 1; i < count; ++i) {
1278 const default_camera_hal::Stream* stream = streams[i];
1279 if (stream->getFormat() != format || stream->getWidth() != width ||
1280 stream->getHeight() != height) {
1281 HAL_LOGE("V4L2 only supports 1 stream configuration at a time "
1282 "(stream 0 is format %d, width %u, height %u, "
1283 "stream %d is format %d, width %u, height %u).",
1284 format, width, height, i, stream->getFormat(),
1285 stream->getWidth(), stream->getHeight());
1286 return false;
1287 }
1288 }
1289
1290 return true;
1291}
1292
1293int V4L2Camera::setupStream(default_camera_hal::Stream* stream,
1294 uint32_t* max_buffers) {
1295 HAL_LOG_ENTER();
1296
1297 if (stream->getRotation() != CAMERA3_STREAM_ROTATION_0) {
1298 HAL_LOGE("Rotation %d not supported", stream->getRotation());
1299 return -EINVAL;
1300 }
1301
1302 // Doesn't matter what was requested, we always use dataspace V0_JFIF.
1303 // Note: according to camera3.h, this isn't allowed, but etalvala@google.com
1304 // claims it's underdocumented; the implementation lets the HAL overwrite it.
1305 stream->setDataSpace(HAL_DATASPACE_V0_JFIF);
1306
1307 int ret = setFormat(stream);
1308 if (ret) {
1309 return ret;
1310 }
1311
1312 // Only do buffer setup if we don't know our maxBuffers.
1313 if (mOutStreamMaxBuffers < 1) {
1314 ret = setupBuffers();
1315 if (ret) {
1316 return ret;
1317 }
1318 }
1319
1320 // Sanity check.
1321 if (mOutStreamMaxBuffers < 1) {
1322 HAL_LOGE("HAL failed to determine max number of buffers.");
1323 return -ENODEV;
1324 }
1325 *max_buffers = mOutStreamMaxBuffers;
1326
1327 return 0;
1328}
1329
1330int V4L2Camera::setFormat(const default_camera_hal::Stream* stream) {
1331 HAL_LOG_ENTER();
1332
1333 // Should be checked earlier; sanity check.
1334 if (stream->isInputType()) {
1335 HAL_LOGE("Input streams not supported.");
1336 return -EINVAL;
1337 }
1338
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001339 // TODO(b/30000211): Check if appropriate to do multi-planar capture instead.
1340 uint32_t desired_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1341 uint32_t desired_format = halToV4L2PixelFormat(stream->getFormat());
1342 uint32_t desired_width = stream->getWidth();
1343 uint32_t desired_height = stream->getHeight();
1344
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001345 // Check to make sure we're not already in the correct format.
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001346 if (desired_type == mOutStreamType &&
1347 desired_format == mOutStreamFormat &&
1348 desired_width == mOutStreamWidth &&
1349 desired_height == mOutStreamHeight) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001350 return 0;
1351 }
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001352
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001353 // Not in the correct format, set our format.
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001354 v4l2_format format;
1355 memset(&format, 0, sizeof(format));
1356 format.type = desired_type;
1357 format.fmt.pix.width = desired_width;
1358 format.fmt.pix.height = desired_height;
1359 format.fmt.pix.pixelformat = desired_format;
1360 // TODO(b/29334616): When async, this will need to check if the stream
1361 // is on, and if so, lock it off while setting format.
1362 if (ioctlLocked(VIDIOC_S_FMT, &format) < 0) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001363 HAL_LOGE("S_FMT failed: %s", strerror(errno));
1364 return -ENODEV;
1365 }
1366 // Check that the driver actually set to the requested values.
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001367 if (format.type != desired_type ||
1368 format.fmt.pix.pixelformat != desired_format ||
1369 format.fmt.pix.width != desired_width ||
1370 format.fmt.pix.height != desired_height) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001371 HAL_LOGE("Device doesn't support desired stream configuration.");
1372 return -EINVAL;
1373 }
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001374
1375 // Keep track of the new format.
1376 mOutStreamType = format.type;
1377 mOutStreamFormat = format.fmt.pix.pixelformat;
1378 mOutStreamWidth = format.fmt.pix.width;
1379 mOutStreamHeight = format.fmt.pix.height;
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -07001380 mOutStreamBytesPerLine = format.fmt.pix.bytesperline;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001381 // Since our format changed, our maxBuffers may be incorrect.
1382 mOutStreamMaxBuffers = 0;
1383
1384 return 0;
1385}
1386
1387int V4L2Camera::setupBuffers() {
1388 HAL_LOG_ENTER();
1389
1390 // "Request" a buffer (since we're using a userspace buffer, this just
1391 // tells V4L2 to switch into userspace buffer mode).
1392 v4l2_requestbuffers req_buffers;
1393 memset(&req_buffers, 0, sizeof(req_buffers));
Ari Hausman-Cohen2934eb92016-07-20 17:25:39 +00001394 req_buffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001395 req_buffers.memory = V4L2_MEMORY_USERPTR;
1396 req_buffers.count = 1;
1397 if (ioctlLocked(VIDIOC_REQBUFS, &req_buffers) < 0) {
1398 HAL_LOGE("REQBUFS failed: %s", strerror(errno));
1399 return -ENODEV;
1400 }
1401
1402 // V4L2 will set req_buffers.count to a number of buffers it can handle.
1403 mOutStreamMaxBuffers = req_buffers.count;
1404 return 0;
1405}
1406
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001407bool V4L2Camera::isValidCaptureSettings(const camera_metadata_t* settings) {
1408 HAL_LOG_ENTER();
1409
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001410 // TODO(b/29335262): reject capture settings this camera isn't capable of.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001411 return true;
1412}
1413
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001414int V4L2Camera::initCharacteristics() {
1415 HAL_LOG_ENTER();
1416
1417 /* Physical characteristics. */
1418 // No way to get these in V4L2, so faked.
1419 // Note: While many of these are primarily informative for post-processing
1420 // calculations by the app and will potentially cause bad results there,
1421 // focal length and physical size are actually used in framework
1422 // calculations (field of view, pixel pitch, etc), so faking them may
1423 // have unexpected results.
1424 mAperture = 2.0; // RPi camera v2 is f/2.0.
1425 mFilterDensity = 0.0;
1426 mFocalLength = 3.04; // RPi camera v2 is 3.04mm.
1427 mOrientation = 0;
1428 mPhysicalSize = {{3.674, 2.760}}; // RPi camera v2 is 3.674 x 2.760 mm.
1429
1430 /* Fixed features. */
1431
1432 // TODO(b/29394024): query VIDIOC_CROPCAP to get pixel rectangle.
1433 // Spoofing as 640 x 480 for now.
1434 mPixelArraySize = {{/*xmin*/0, /*ymin*/0, /*width*/640, /*height*/480}};
1435
1436 // V4L2 VIDIOC_CROPCAP doesn't give a way to query this;
1437 // it's driver dependent. For now, assume freeform, and
1438 // some cameras may just behave badly.
1439 // TODO(b/29579652): Figure out a way to determine this.
1440 mCropType = ANDROID_SCALER_CROPPING_TYPE_FREEFORM;
1441
1442 // TODO(b/29394024): query VIDIOC_CROPCAP to get cropping ranges,
1443 // and VIDIOC_G_CROP to determine if cropping is supported.
1444 // If the ioctl isn't available (or cropping has non-square pixelaspect),
1445 // assume no cropping/scaling.
1446 // May need to try setting some crops to determine what the driver actually
1447 // supports (including testing center vs freeform).
1448 mMaxZoom = 1;
1449
1450 // TODO(b/29394024): query V4L2_CID_EXPOSURE_BIAS.
1451 mAeCompensationRange = {{0, 0}};
1452 mAeCompensationStep = {1, 1};
1453
1454 // TODO(b/29394024): query V4L2_CID_3A_LOCK.
1455 mAeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE;
1456 mAwbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE;
1457
1458 // TODO(b/29394024): query V4L2_CID_FLASH_LED_MODE.
1459 mFlashAvailable = 0;
1460
1461 // TODO(b/29394024): query V4L2_CID_FOCUS_ABSOLUTE for focus range.
1462 mFocusDistance = 0; // Fixed focus.
1463
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001464 // TODO(b/29939583): V4L2 can only support 1 stream at a time.
1465 // For now, just reporting minimum allowable for LIMITED devices.
1466 mMaxRawOutputStreams = 0;
1467 mMaxYuvOutputStreams = 2;
1468 mMaxJpegOutputStreams = 1;
1469 // Reprocessing not supported.
1470 mMaxInputStreams = 0;
1471
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001472 /* Features with (potentially) multiple options. */
1473
1474 // TODO(b/29394024): query V4L2_CID_EXPOSURE_AUTO for ae modes.
1475 mAeModes.push_back(ANDROID_CONTROL_AE_MODE_ON);
1476
1477 // TODO(b/29394024): query V4L2_CID_POWER_LINE_FREQUENCY.
1478 // Auto as the default, since it could mean anything, while OFF would
1479 // require guaranteeing no antibanding happens.
1480 mAeAntibandingModes.push_back(ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO);
1481
1482 // TODO(b/29394024): query V4L2_CID_FOCUS_AUTO for
1483 // CONTINUOUS_VIDEO/CONTINUOUS_PICTURE. V4L2_CID_AUTO_FOCUS_START
1484 // supports what Android thinks of as auto focus (single auto focus).
1485 // V4L2_CID_AUTO_FOCUS_RANGE allows MACRO.
1486 mAfModes.push_back(ANDROID_CONTROL_AF_MODE_OFF);
1487
1488 // TODO(b/29394024): query V4L2_CID_AUTO_WHITE_BALANCE, or
1489 // V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE if available.
1490 mAwbModes.push_back(ANDROID_CONTROL_AWB_MODE_AUTO);
1491
1492 // TODO(b/29394024): query V4L2_CID_SCENE_MODE.
1493 mSceneModes.push_back(ANDROID_CONTROL_SCENE_MODE_DISABLED);
1494
1495 mControlModes.push_back(ANDROID_CONTROL_MODE_AUTO);
1496 if (mSceneModes.size() > 1) {
1497 // We have some mode other than just DISABLED available.
1498 mControlModes.push_back(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
1499 }
1500
1501 // TODO(b/29394024): query V4L2_CID_COLORFX.
1502 mEffects.push_back(ANDROID_CONTROL_EFFECT_MODE_OFF);
1503
1504 // TODO(b/29394024): query V4L2_CID_FLASH_INDICATOR_INTENSITY.
1505 // For now, no indicator LED available; nothing to push back.
1506 // When there is, push back ANDROID_LED_AVAILABLE_LEDS_TRANSMIT.
1507
1508 // TODO(b/29394024): query V4L2_CID_IMAGE_STABILIZATION.
1509 mOpticalStabilizationModes.push_back(
1510 ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF);
1511 mVideoStabilizationModes.push_back(
1512 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF);
1513
1514 // Need to support YUV_420_888 and JPEG.
1515 // TODO(b/29394024): query VIDIOC_ENUM_FMT.
1516 std::vector<uint32_t> formats = {{V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420}};
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001517 // We want to find the smallest maximum frame duration amongst formats.
1518 mMaxFrameDuration = std::numeric_limits<int64_t>::max();
1519 int64_t min_yuv_frame_duration = std::numeric_limits<int64_t>::max();
1520 for (auto format : formats) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001521 int32_t hal_format = V4L2ToHalPixelFormat(format);
1522 if (hal_format < 0) {
1523 // Unrecognized/unused format. Skip it.
1524 continue;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001525 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001526 // TODO(b/29394024): query VIDIOC_ENUM_FRAMESIZES.
1527 // For now, just 640x480.
1528 ArrayVector<int32_t, 2> frame_sizes;
1529 frame_sizes.push_back({{640, 480}});
1530 size_t num_frame_sizes = frame_sizes.num_arrays();
1531 for (size_t i = 0; i < num_frame_sizes; ++i) {
1532 const int32_t* frame_size = frame_sizes[i];
1533 mStreamConfigs.push_back({{hal_format, frame_size[0], frame_size[1],
1534 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT}});
1535
1536 // TODO(b/29394024): query VIDIOC_ENUM_FRAMEINTERVALS.
1537 // For now, using the emulator max value of .3 sec.
1538 int64_t max_frame_duration = 300000000;
1539 // For now, min value of 30 fps = 1/30 spf ~= 33333333 ns.
1540 // For whatever reason the goldfish/qcom cameras report this as
1541 // 33331760, so copying that.
1542 int64_t min_frame_duration = 33331760;
1543
1544 mMinFrameDurations.push_back(
1545 {{hal_format, frame_size[0], frame_size[1], min_frame_duration}});
1546
1547 // In theory max frame duration (min frame rate) should be consistent
1548 // between all formats, but we check and only advertise the smallest
1549 // available max duration just in case.
1550 if (max_frame_duration < mMaxFrameDuration) {
1551 mMaxFrameDuration = max_frame_duration;
1552 }
1553
1554 // We only care about min frame duration (max frame rate) for YUV.
1555 if (hal_format == HAL_PIXEL_FORMAT_YCbCr_420_888 &&
1556 min_frame_duration < min_yuv_frame_duration) {
1557 min_yuv_frame_duration = min_frame_duration;
1558 }
1559
1560 // Usually 0 for non-jpeg, non-zero for JPEG.
1561 // Randomly choosing absurd 1 sec for JPEG. Unsure what this breaks.
1562 int64_t stall_duration = 0;
1563 if (hal_format == HAL_PIXEL_FORMAT_BLOB) {
1564 stall_duration = 1000000000;
1565 }
1566 mStallDurations.push_back(
1567 {{hal_format, frame_size[0], frame_size[1], stall_duration}});
1568 }
1569 }
1570
1571 // This should be at minimum {mi, ma}, {ma, ma} where mi and ma
1572 // are min and max frame rates for YUV_420_888. Min should be at most 15.
1573 // Convert from frame durations measured in ns.
1574 int32_t min_yuv_fps = 1000000000 / mMaxFrameDuration;
1575 if (min_yuv_fps > 15) {
1576 return -ENODEV;
1577 }
1578 int32_t max_yuv_fps = 1000000000 / min_yuv_frame_duration;
1579 mFpsRanges.push_back({{min_yuv_fps, max_yuv_fps}});
1580 mFpsRanges.push_back({{max_yuv_fps, max_yuv_fps}});
1581 // Always advertise {30, 30} if max is even higher,
1582 // since this is what the default video requests use.
1583 if (max_yuv_fps > 30) {
1584 mFpsRanges.push_back({{30, 30}});
1585 }
1586
1587 mCharacteristicsInitialized = true;
1588 return 0;
1589}
1590
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001591int V4L2Camera::V4L2ToHalPixelFormat(uint32_t v4l2_format) {
1592 // Translate V4L2 format to HAL format.
1593 int hal_format = -1;
1594 switch (v4l2_format) {
1595 case V4L2_PIX_FMT_JPEG:
1596 hal_format = HAL_PIXEL_FORMAT_BLOB;
1597 break;
1598 case V4L2_PIX_FMT_YUV420:
1599 hal_format = HAL_PIXEL_FORMAT_YCbCr_420_888;
1600 break;
1601 default:
1602 // Unrecognized format.
1603 break;
1604 }
1605 return hal_format;
1606}
1607
1608uint32_t V4L2Camera::halToV4L2PixelFormat(int hal_format) {
1609 // Translate HAL format to V4L2 format.
1610 uint32_t v4l2_format = 0;
1611 switch (hal_format) {
1612 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: // fall-through.
1613 case HAL_PIXEL_FORMAT_YCbCr_420_888:
1614 v4l2_format = V4L2_PIX_FMT_YUV420;
1615 break;
1616 case HAL_PIXEL_FORMAT_BLOB:
1617 v4l2_format = V4L2_PIX_FMT_JPEG;
1618 break;
1619 default:
1620 // Unrecognized format.
1621 break;
1622 }
1623 return v4l2_format;
1624}
1625
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001626} // namespace v4l2_camera_hal