blob: 25a59553a47d1f668c504fb8d754c361279edc29 [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) {
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -070071 HAL_LOG_ENTER();
72
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070073 android::Mutex::Autolock al(mDeviceLock);
74 if (mDeviceFd.get() < 0) {
75 return -ENODEV;
76 }
77 return TEMP_FAILURE_RETRY(ioctl(mDeviceFd.get(), request, data));
78}
79
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070080int V4L2Camera::connect() {
81 HAL_LOG_ENTER();
82
83 if (mDeviceFd.get() >= 0) {
84 HAL_LOGE("Camera device %s is opened. Close it first", mDevicePath.c_str());
85 return -EIO;
86 }
87
88 int fd = TEMP_FAILURE_RETRY(open(mDevicePath.c_str(), O_RDWR));
89 if (fd < 0) {
90 HAL_LOGE("failed to open %s (%s)", mDevicePath.c_str(), strerror(errno));
91 return -errno;
92 }
93 mDeviceFd.reset(fd);
94
95 // TODO(b/29185945): confirm this is a supported device.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070096 // This is checked by the HAL, but the device at mDevicePath may
97 // not be the same one that was there when the HAL was loaded.
98 // (Alternatively, better hotplugging support may make this unecessary
99 // by disabling cameras that get disconnected and checking newly connected
100 // cameras, so connect() is never called on an unsupported camera)
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700101
102 // TODO(b/29158098): Inform service of any flashes that are no longer available
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700103 // because this camera is in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700104 return 0;
105}
106
107void V4L2Camera::disconnect() {
108 HAL_LOG_ENTER();
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700109 // TODO(b/29158098): Inform service of any flashes that are available again
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700110 // because this camera is no longer in use.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700111
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700112 mDeviceFd.reset();
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -0700113
114 // Clear out our variables tracking device settings.
115 mOutStreamType = 0;
116 mOutStreamFormat = 0;
117 mOutStreamWidth = 0;
118 mOutStreamHeight = 0;
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700119 mOutStreamBytesPerLine = 0;
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -0700120 mOutStreamMaxBuffers = 0;
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700121}
122
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700123int V4L2Camera::streamOn() {
124 HAL_LOG_ENTER();
125
126 if (!mOutStreamType) {
127 HAL_LOGE("Stream format must be set before turning on stream.");
128 return -EINVAL;
129 }
130
131 if (ioctlLocked(VIDIOC_STREAMON, &mOutStreamType) < 0) {
132 HAL_LOGE("STREAMON fails: %s", strerror(errno));
133 return -ENODEV;
134 }
135
136 return 0;
137}
138
139int V4L2Camera::streamOff() {
140 HAL_LOG_ENTER();
141
142 // TODO(b/30000211): support mplane as well.
143 if (ioctlLocked(VIDIOC_STREAMOFF, &mOutStreamType) < 0) {
144 HAL_LOGE("STREAMOFF fails: %s", strerror(errno));
145 return -ENODEV;
146 }
147
148 return 0;
149}
150
151
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700152int V4L2Camera::initStaticInfo(camera_metadata_t** out) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700153 HAL_LOG_ENTER();
154
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700155 android::status_t res;
156 // Device characteristics need to be queried prior
157 // to static info setup.
158 if (!mCharacteristicsInitialized) {
159 res = initCharacteristics();
160 if (res) {
161 return res;
162 }
163 }
164
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700165 android::CameraMetadata info;
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -0700166
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700167 // Static metadata characteristics from /system/media/camera/docs/docs.html.
168
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700169 /* android.colorCorrection. */
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700170
171 // No easy way to turn chromatic aberration correction OFF in v4l2,
172 // though this may be supportable via a collection of other user controls.
173 uint8_t avail_aberration_modes[] = {
174 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST,
175 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700176 res = info.update(ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
177 avail_aberration_modes, ARRAY_SIZE(avail_aberration_modes));
178 if (res != android::OK) {
179 return res;
180 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700181
182 /* android.control. */
183
184 /* 3As */
185
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700186 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
187 mAeAntibandingModes.data(), mAeAntibandingModes.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_AE_AVAILABLE_MODES,
193 mAeModes.data(), mAeModes.size());
194 if (res != android::OK) {
195 return res;
196 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700197
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700198 // Flatten mFpsRanges.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700199 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
200 mFpsRanges.data(), mFpsRanges.total_num_elements());
201 if (res != android::OK) {
202 return res;
203 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700204
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700205 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
206 mAeCompensationRange.data(), mAeCompensationRange.size());
207 if (res != android::OK) {
208 return res;
209 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700210
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700211 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_STEP,
212 &mAeCompensationStep, 1);
213 if (res != android::OK) {
214 return res;
215 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700216
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700217 res = info.update(ANDROID_CONTROL_AF_AVAILABLE_MODES,
218 mAfModes.data(), mAfModes.size());
219 if (res != android::OK) {
220 return res;
221 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700222
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700223 res = info.update(ANDROID_CONTROL_AWB_AVAILABLE_MODES,
224 mAwbModes.data(), mAwbModes.size());
225 if (res != android::OK) {
226 return res;
227 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700228
229 // Couldn't find any V4L2 support for regions, though maybe it's out there.
230 int32_t max_regions[] = {/*AE*/ 0,/*AWB*/ 0,/*AF*/ 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700231 res = info.update(ANDROID_CONTROL_MAX_REGIONS,
232 max_regions, ARRAY_SIZE(max_regions));
233 if (res != android::OK) {
234 return res;
235 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700236
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700237 res = info.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE,
238 &mAeLockAvailable, 1);
239 if (res != android::OK) {
240 return res;
241 }
242 res = info.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
243 &mAwbLockAvailable, 1);
244 if (res != android::OK) {
245 return res;
246 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700247
248 /* Scene modes. */
249
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700250 res = info.update(ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
251 mSceneModes.data(), mSceneModes.size());
252 if (res != android::OK) {
253 return res;
254 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700255
256 // A 3-tuple of AE, AWB, AF overrides for each scene mode.
257 // Ignored for DISABLED, FACE_PRIORITY and FACE_PRIORITY_LOW_LIGHT.
258 uint8_t scene_mode_overrides[] = {
259 /*SCENE_MODE_DISABLED*/ /*AE*/0, /*AW*/0, /*AF*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700260 res = info.update(ANDROID_CONTROL_SCENE_MODE_OVERRIDES,
261 scene_mode_overrides, ARRAY_SIZE(scene_mode_overrides));
262 if (res != android::OK) {
263 return res;
264 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700265
266 /* Top level 3A/Scenes switch. */
267
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700268 res = info.update(ANDROID_CONTROL_AVAILABLE_MODES,
269 mControlModes.data(), mControlModes.size());
270 if (res != android::OK) {
271 return res;
272 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700273
274 /* Other android.control configuration. */
275
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700276 res = info.update(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
277 mVideoStabilizationModes.data(),
278 mVideoStabilizationModes.size());
279 if (res != android::OK) {
280 return res;
281 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700282
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700283 res = info.update(ANDROID_CONTROL_AVAILABLE_EFFECTS,
284 mEffects.data(), mEffects.size());
285 if (res != android::OK) {
286 return res;
287 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700288
289 // AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS only necessary
290 // for devices supporting CONSTRAINED_HIGH_SPEED_VIDEO,
291 // which this HAL doesn't support.
292
293 // POST_RAW_SENSITIVITY_BOOST_RANGE only necessary
294 // for devices supporting RAW format outputs.
295
296 /* android.edge. */
297
298 // Not sure if V4L2 does or doesn't do this, but HAL documentation says
299 // all devices must support FAST, and FAST can be equivalent to OFF, so
300 // either way it's fine to list.
301 uint8_t avail_edge_modes[] = {
302 ANDROID_EDGE_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700303 res = info.update(ANDROID_EDGE_AVAILABLE_EDGE_MODES,
304 avail_edge_modes, ARRAY_SIZE(avail_edge_modes));
305 if (res != android::OK) {
306 return res;
307 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700308
309 /* android.flash. */
310
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700311 res = info.update(ANDROID_FLASH_INFO_AVAILABLE,
312 &mFlashAvailable, 1);
313 if (res != android::OK) {
314 return res;
315 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700316
317 // info.chargeDuration, color.Temperature, maxEnergy marked FUTURE.
318
319 /* android.hotPixel. */
320
321 // No known V4L2 hot pixel correction. But it might be happening,
322 // so we report FAST/HIGH_QUALITY.
323 uint8_t avail_hot_pixel_modes[] = {
324 ANDROID_HOT_PIXEL_MODE_FAST,
325 ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700326 res = info.update(ANDROID_HOT_PIXEL_AVAILABLE_HOT_PIXEL_MODES,
327 avail_hot_pixel_modes, ARRAY_SIZE(avail_hot_pixel_modes));
328 if (res != android::OK) {
329 return res;
330 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700331
332 /* android.jpeg. */
333
334 // For now, no thumbnails available (only [0,0], the "no thumbnail" size).
335 // TODO(b/29580107): Could end up with a mismatch between request & result,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700336 // since V4L2 doesn't actually allow for thumbnail size control.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700337 int32_t thumbnail_sizes[] = {
338 0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700339 res = info.update(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
340 thumbnail_sizes, ARRAY_SIZE(thumbnail_sizes));
341 if (res != android::OK) {
342 return res;
343 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700344
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700345 // V4L2 doesn't support querying this,
346 // so instead use a constant (defined in V4L2Gralloc.h).
347 int32_t max_jpeg_size = V4L2_MAX_JPEG_SIZE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700348 res = info.update(ANDROID_JPEG_MAX_SIZE,
349 &max_jpeg_size, 1);
350 if (res != android::OK) {
351 return res;
352 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700353
354 /* android.lens. */
355
356 /* Misc. lens control. */
357
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700358 res = info.update(ANDROID_LENS_INFO_AVAILABLE_APERTURES,
359 &mAperture, 1);
360 if (res != android::OK) {
361 return res;
362 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700363
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700364 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES,
365 &mFilterDensity, 1);
366 if (res != android::OK) {
367 return res;
368 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700369
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700370 res = info.update(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
371 mOpticalStabilizationModes.data(),
372 mOpticalStabilizationModes.size());
373 if (res != android::OK) {
374 return res;
375 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700376
377 // No known V4L2 shading map info.
378 int32_t shading_map_size[] = {1, 1};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700379 res = info.update(ANDROID_LENS_INFO_SHADING_MAP_SIZE,
380 shading_map_size, ARRAY_SIZE(shading_map_size));
381 if (res != android::OK) {
382 return res;
383 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700384
385 // All V4L2 devices are considered to be external facing.
386 uint8_t facing = ANDROID_LENS_FACING_EXTERNAL;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700387 res = info.update(ANDROID_LENS_FACING, &facing, 1);
388 if (res != android::OK) {
389 return res;
390 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700391
392 /* Zoom/Focus. */
393
394 // No way to actually get the focal length in V4L2, but it's a required key,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700395 // so we just fake it.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700396 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
397 &mFocalLength, 1);
398 if (res != android::OK) {
399 return res;
400 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700401
402 // V4L2 focal units do not correspond to a particular physical unit.
403 uint8_t focus_calibration =
404 ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700405 res = info.update(ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
406 &focus_calibration, 1);
407 if (res != android::OK) {
408 return res;
409 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700410
411 // info.hyperfocalDistance not required for UNCALIBRATED.
412
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700413 res = info.update(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
414 &mFocusDistance, 1);
415 if (res != android::OK) {
416 return res;
417 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700418
419 /* Depth. */
420
421 // DEPTH capability not supported by this HAL. Not implemented:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700422 // poseRotation
423 // poseTranslation
424 // intrinsicCalibration
425 // radialDistortion
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700426
427 /* anroid.noise. */
428
429 // Unable to control noise reduction in V4L2 devices,
430 // but FAST is allowed to be the same as OFF.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700431 uint8_t avail_noise_reduction_modes[] = {ANDROID_NOISE_REDUCTION_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700432 res = info.update(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
433 avail_noise_reduction_modes,
434 ARRAY_SIZE(avail_noise_reduction_modes));
435 if (res != android::OK) {
436 return res;
437 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700438
439 /* android.request. */
440
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700441 int32_t max_num_output_streams[] = {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700442 mMaxRawOutputStreams, mMaxYuvOutputStreams, mMaxJpegOutputStreams};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700443 res = info.update(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
444 max_num_output_streams, ARRAY_SIZE(max_num_output_streams));
445 if (res != android::OK) {
446 return res;
447 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700448
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700449 res = info.update(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS,
450 &mMaxInputStreams, 1);
451 if (res != android::OK) {
452 return res;
453 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700454
455 // No way to know for V4L2, so fake with max allowable latency.
456 // Doesn't mean much without per-frame controls.
457 uint8_t pipeline_max_depth = 4;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700458 res = info.update(ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
459 &pipeline_max_depth, 1);
460 if (res != android::OK) {
461 return res;
462 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700463
464 // Partial results not supported; partialResultCount defaults to 1.
465
466 // Available capabilities & keys queried at very end of this method.
467
468 /* android.scaler. */
469
470 /* Cropping. */
471
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700472 res = info.update(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
473 &mMaxZoom, 1);
474 if (res != android::OK) {
475 return res;
476 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700477
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700478 res = info.update(ANDROID_SCALER_CROPPING_TYPE, &mCropType, 1);
479 if (res != android::OK) {
480 return res;
481 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700482
483 /* Streams. */
484
485 // availableInputOutputFormatsMap only required for reprocessing capability.
486
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700487 // Flatten mStreamConfigs.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700488 res = info.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
489 mStreamConfigs.data(),
490 mStreamConfigs.total_num_elements());
491 if (res != android::OK) {
492 return res;
493 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700494
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700495 // Flatten mMinFrameDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700496 res = info.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
497 mMinFrameDurations.data(),
498 mMinFrameDurations.total_num_elements());
499 if (res != android::OK) {
500 return res;
501 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700502
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700503 // Flatten mStallDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700504 res = info.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
505 mStallDurations.data(),
506 mStallDurations.total_num_elements());
507 if (res != android::OK) {
508 return res;
509 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700510
511 /* android.sensor. */
512
513 /* Sizes. */
514
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700515 res = info.update(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
516 mPixelArraySize.data(), mPixelArraySize.size());
517 if (res != android::OK) {
518 return res;
519 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700520 // No V4L2 way to differentiate active vs. inactive parts of the rectangle.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700521 res = info.update(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
522 mPixelArraySize.data(), mPixelArraySize.size());
523 if (res != android::OK) {
524 return res;
525 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700526
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700527 res = info.update(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
528 mPhysicalSize.data(), mPhysicalSize.size());
529 if (res != android::OK) {
530 return res;
531 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700532
533 /* Misc sensor information. */
534
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700535 res = info.update(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
536 &mMaxFrameDuration, 1);
537 if (res != android::OK) {
538 return res;
539 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700540
541 // HAL uses BOOTTIME timestamps.
542 // TODO(b/29457051): make sure timestamps are consistent throughout the HAL.
543 uint8_t timestamp_source = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700544 res = info.update(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
545 &timestamp_source, 1);
546 if (res != android::OK) {
547 return res;
548 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700549
550 // As in initDeviceInfo, no way to actually get orientation.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700551 res = info.update(ANDROID_SENSOR_ORIENTATION, &mOrientation, 1);
552 if (res != android::OK) {
553 return res;
554 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700555
556 // availableTestPatternModes just defaults to OFF, which is fine.
557
558 // info.exposureTimeRange, info.sensitivityRange:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700559 // exposure/sensitivity manual control not supported.
560 // Could query V4L2_CID_ISO_SENSITIVITY to support sensitivity if desired.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700561
562 // info.whiteLevel, info.lensShadingApplied,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700563 // info.preCorrectionPixelArraySize, referenceIlluminant1/2,
564 // calibrationTransform1/2, colorTransform1/2, forwardMatrix1/2,
565 // blackLevelPattern, profileHueSatMapDimensions
566 // all only necessary for RAW.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700567
568 // baseGainFactor marked FUTURE.
569
570 // maxAnalogSensitivity optional for LIMITED device.
571
572 // opticalBlackRegions: No known way to get in V4L2, but not necessary.
573
574 // opaqueRawSize not necessary since RAW_OPAQUE format not supported.
575
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700576 /* android.shading */
577
578 // No known V4L2 lens shading. But it might be happening,
579 // so we report FAST/HIGH_QUALITY.
580 uint8_t avail_shading_modes[] = {
581 ANDROID_SHADING_MODE_FAST,
582 ANDROID_SHADING_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700583 res = info.update(ANDROID_SHADING_AVAILABLE_MODES,
584 avail_shading_modes, ARRAY_SIZE(avail_shading_modes));
585 if (res != android::OK) {
586 return res;
587 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700588
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700589 /* android.statistics */
590
591 // Face detection not supported.
592 uint8_t avail_face_detect_modes[] = {
593 ANDROID_STATISTICS_FACE_DETECT_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700594 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
595 avail_face_detect_modes,
596 ARRAY_SIZE(avail_face_detect_modes));
597 if (res != android::OK) {
598 return res;
599 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700600
601 int32_t max_face_count = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700602 res = info.update(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
603 &max_face_count, 1);
604 if (res != android::OK) {
605 return res;
606 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700607
608 // info.histogramBucketCount, info.maxHistogramCount,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700609 // info.maxSharpnessMapValue, info.sharpnessMapSizemarked FUTURE.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700610
611 // ON only needs to be supported for RAW capable devices.
612 uint8_t avail_hot_pixel_map_modes[] = {
613 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700614 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
615 avail_hot_pixel_map_modes,
616 ARRAY_SIZE(avail_hot_pixel_map_modes));
617 if (res != android::OK) {
618 return res;
619 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700620
621 // ON only needs to be supported for RAW capable devices.
622 uint8_t avail_lens_shading_map_modes[] = {
623 ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700624 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
625 avail_lens_shading_map_modes,
626 ARRAY_SIZE(avail_lens_shading_map_modes));
627 if (res != android::OK) {
628 return res;
629 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700630
631 /* android.tonemap. */
632
633 // tonemapping only required for MANUAL_POST_PROCESSING capability.
634
635 /* android.led. */
636
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700637 // May or may not have LEDs available.
638 if (!mLeds.empty()) {
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700639 res = info.update(ANDROID_LED_AVAILABLE_LEDS, mLeds.data(), mLeds.size());
640 if (res != android::OK) {
641 return res;
642 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700643 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700644
645 /* android.sync. */
646
647 // "LIMITED devices are strongly encouraged to use a non-negative value.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700648 // If UNKNOWN is used here then app developers do not have a way to know
649 // when sensor settings have been applied." - Unfortunately, V4L2 doesn't
650 // really help here either. Could even be that adjusting settings mid-stream
651 // blocks in V4L2, and should be avoided.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700652 int32_t max_latency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700653 res = info.update(ANDROID_SYNC_MAX_LATENCY,
654 &max_latency, 1);
655 if (res != android::OK) {
656 return res;
657 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700658
659 /* android.reprocess. */
660
661 // REPROCESSING not supported by this HAL.
662
663 /* android.depth. */
664
665 // DEPTH not supported by this HAL.
666
667 /* Capabilities and android.info. */
668
669 uint8_t hw_level = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700670 res = info.update(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
671 &hw_level, 1);
672 if (res != android::OK) {
673 return res;
674 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700675
676 uint8_t capabilities[] = {
677 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700678 res = info.update(ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
679 capabilities, ARRAY_SIZE(capabilities));
680 if (res != android::OK) {
681 return res;
682 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700683
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700684 // Scan a default request template for included request keys.
685 if (!mTemplatesInitialized) {
686 res = initTemplates();
687 if (res) {
688 return res;
689 }
690 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700691 const camera_metadata_t* preview_request = nullptr;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700692 // Search templates from the beginning for a supported one.
693 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
694 ++template_id) {
695 preview_request = constructDefaultRequestSettings(template_id);
696 if (preview_request != nullptr) {
697 break;
698 }
699 }
700 if (preview_request == nullptr) {
701 HAL_LOGE("No valid templates, can't get request keys.");
702 return -ENODEV;
703 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700704 std::vector<int32_t> avail_request_keys = getMetadataKeys(preview_request);
705 res = info.update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
706 avail_request_keys.data(), avail_request_keys.size());
707 if (res != android::OK) {
708 return res;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700709 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700710
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700711 // Result keys will be duplicated from the request, plus a few extras.
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700712 // TODO(b/30035628): additonal available result keys.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700713 std::vector<int32_t> avail_result_keys(avail_request_keys);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700714 res = info.update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
715 avail_result_keys.data(), avail_result_keys.size());
716 if (res != android::OK) {
717 return res;
718 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700719
720 // Last thing, once all the available characteristics have been added.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700721 const camera_metadata_t* static_characteristics = info.getAndLock();
722 std::vector<int32_t> avail_characteristics_keys =
723 getMetadataKeys(static_characteristics);
724 res = info.unlock(static_characteristics);
725 if (res != android::OK) {
726 return res;
727 }
728 avail_characteristics_keys.push_back(
729 ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
730 res = info.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
731 avail_characteristics_keys.data(),
732 avail_characteristics_keys.size());
733 if (res != android::OK) {
734 return res;
735 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700736
737 *out = info.release();
738 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700739}
740
741void V4L2Camera::initDeviceInfo(camera_info_t* info) {
742 HAL_LOG_ENTER();
743
744 // For now, just constants.
745 info->facing = CAMERA_FACING_EXTERNAL;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700746 info->orientation = mOrientation;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700747 info->resource_cost = 100;
748 info->conflicting_devices = nullptr;
749 info->conflicting_devices_length = 0;
750}
751
752int V4L2Camera::initDevice() {
753 HAL_LOG_ENTER();
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700754 int res;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700755
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700756 // Initialize and check the gralloc module.
757 const hw_module_t* module;
758 res = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
759 if (res) {
760 HAL_LOGE("Couldn't get gralloc module.");
761 return -ENODEV;
762 }
763 const gralloc_module_t* gralloc =
764 reinterpret_cast<const gralloc_module_t*>(module);
765 mGralloc = V4L2Gralloc(gralloc);
766 if (!mGralloc.isValid()) {
767 HAL_LOGE("Invalid gralloc module.");
768 return -ENODEV;
769 }
770
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700771 // Templates should be set up if they haven't already been.
772 if (!mTemplatesInitialized) {
773 res = initTemplates();
774 if (res) {
775 return res;
776 }
777 }
778
779 return 0;
780}
781
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700782int V4L2Camera::enqueueBuffer(const camera3_stream_buffer_t* camera_buffer) {
783 HAL_LOG_ENTER();
784
785 // Set up a v4l2 buffer struct.
786 v4l2_buffer device_buffer;
787 memset(&device_buffer, 0, sizeof(device_buffer));
788 device_buffer.type = mOutStreamType;
789
790 // Use QUERYBUF to ensure our buffer/device is in good shape.
791 if (ioctlLocked(VIDIOC_QUERYBUF, &device_buffer) < 0) {
792 HAL_LOGE("QUERYBUF (%d) fails: %s", 0, strerror(errno));
793 return -ENODEV;
794 }
795
796 // Configure the device buffer based on the stream buffer.
797 device_buffer.memory = V4L2_MEMORY_USERPTR;
798 // TODO(b/29334616): when this is async, actually limit the number
799 // of buffers used to the known max, and set this according to the
800 // queue length.
801 device_buffer.index = 0;
802 // Lock the buffer for writing.
803 int res = mGralloc.lock(camera_buffer, mOutStreamBytesPerLine,
804 &device_buffer);
805 if (res) {
806 return res;
807 }
808 if (ioctlLocked(VIDIOC_QBUF, &device_buffer) < 0) {
809 HAL_LOGE("QBUF (%d) fails: %s", 0, strerror(errno));
810 mGralloc.unlock(&device_buffer);
811 return -ENODEV;
812 }
813
814 // Turn on the stream.
815 // TODO(b/29334616): Lock around stream on/off access, only start stream
816 // if not already on. (For now, since it's synchronous, it will always be
817 // turned off before another call to this function).
818 res = streamOn();
819 if (res) {
820 mGralloc.unlock(&device_buffer);
821 return res;
822 }
823
824 // TODO(b/29334616): Enqueueing and dequeueing should be separate worker
825 // threads, not in the same function.
826
827 // Dequeue the buffer.
828 v4l2_buffer result_buffer;
829 res = dequeueBuffer(&result_buffer);
830 if (res) {
831 mGralloc.unlock(&result_buffer);
832 return res;
833 }
834 // Now that we're done painting the buffer, we can unlock it.
835 res = mGralloc.unlock(&result_buffer);
836 if (res) {
837 return res;
838 }
839
840 // All done, cleanup.
841 // TODO(b/29334616): Lock around stream on/off access, only stop stream if
842 // buffer queue is empty (synchronously, there's only ever 1 buffer in the
843 // queue at a time, so this is safe).
844 res = streamOff();
845 if (res) {
846 return res;
847 }
848
849 // Sanity check.
850 if (result_buffer.m.userptr != device_buffer.m.userptr) {
851 HAL_LOGE("Got the wrong buffer 0x%x back (expected 0x%x)",
852 result_buffer.m.userptr, device_buffer.m.userptr);
853 return -ENODEV;
854 }
855
856 return 0;
857}
858
859int V4L2Camera::dequeueBuffer(v4l2_buffer* buffer) {
860 HAL_LOG_ENTER();
861
862 memset(buffer, 0, sizeof(*buffer));
863 buffer->type = mOutStreamType;
864 buffer->memory = V4L2_MEMORY_USERPTR;
865 if (ioctlLocked(VIDIOC_DQBUF, buffer) < 0) {
866 HAL_LOGE("DQBUF fails: %s", strerror(errno));
867 return -ENODEV;
868 }
869 return 0;
870}
871
872int V4L2Camera::getResultSettings(camera_metadata** metadata,
873 uint64_t* timestamp) {
874 HAL_LOG_ENTER();
875
876 int res;
877 android::CameraMetadata frame_metadata(*metadata);
878
879 // TODO(b/30035628): fill in.
880 // For now just spoof the timestamp to a non-0 value and send it back.
881 int64_t frame_time = 1;
882 res = frame_metadata.update(ANDROID_SENSOR_TIMESTAMP, &frame_time, 1);
883 if (res != android::OK) {
884 return res;
885 }
886
887 *metadata = frame_metadata.release();
888 *timestamp = frame_time;
889
890 return 0;
891}
892
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700893int V4L2Camera::initTemplates() {
894 HAL_LOG_ENTER();
895 int res;
896
897 // Device characteristics need to be queried prior
898 // to template setup.
899 if (!mCharacteristicsInitialized) {
900 res = initCharacteristics();
901 if (res) {
902 return res;
903 }
904 }
905
906 // Note: static metadata expects all templates/requests
907 // to provide values for all supported keys.
908
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700909 android::CameraMetadata base_metadata;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700910
911 // Start with defaults for all templates.
912
913 /* android.colorCorrection. */
914
915 uint8_t aberration_mode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700916 res = base_metadata.update(ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
917 &aberration_mode, 1);
918 if (res != android::OK) {
919 return res;
920 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700921
922 uint8_t color_correction_mode = ANDROID_COLOR_CORRECTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700923 res = base_metadata.update(ANDROID_COLOR_CORRECTION_MODE,
924 &color_correction_mode, 1);
925 if (res != android::OK) {
926 return res;
927 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700928
929 // transform and gains are for the unsupported MANUAL_POST_PROCESSING only.
930
931 /* android.control. */
932
933 /* AE. */
934 uint8_t ae_antibanding_mode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700935 res = base_metadata.update(ANDROID_CONTROL_AE_ANTIBANDING_MODE,
936 &ae_antibanding_mode, 1);
937 if (res != android::OK) {
938 return res;
939 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700940
941 // Only matters if AE_MODE = OFF
942 int32_t ae_exposure_compensation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700943 res = base_metadata.update(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
944 &ae_exposure_compensation, 1);
945 if (res != android::OK) {
946 return res;
947 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700948
949 uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700950 res = base_metadata.update(ANDROID_CONTROL_AE_LOCK, &ae_lock, 1);
951 if (res != android::OK) {
952 return res;
953 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700954
955 uint8_t ae_mode = ANDROID_CONTROL_AE_MODE_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700956 res = base_metadata.update(ANDROID_CONTROL_AE_MODE, &ae_mode, 1);
957 if (res != android::OK) {
958 return res;
959 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700960
961 // AE regions not supported.
962
963 // FPS set per-template.
964
965 uint8_t ae_precapture_trigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700966 res = base_metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
967 &ae_precapture_trigger, 1);
968 if (res != android::OK) {
969 return res;
970 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700971
972 /* AF. */
973
974 // AF mode set per-template.
975
976 // AF regions not supported.
977
978 uint8_t af_trigger = ANDROID_CONTROL_AF_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700979 res = base_metadata.update(ANDROID_CONTROL_AF_TRIGGER, &af_trigger, 1);
980 if (res != android::OK) {
981 return res;
982 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700983
984 /* AWB. */
985
986 // Priority: auto > off > Whatever is available.
987 uint8_t default_awb_mode = mAwbModes[0];
988 if (std::count(mAwbModes.begin(), mAwbModes.end(),
989 ANDROID_CONTROL_AWB_MODE_AUTO)) {
990 default_awb_mode = ANDROID_CONTROL_AWB_MODE_AUTO;
991 } else if (std::count(mAwbModes.begin(), mAwbModes.end(),
992 ANDROID_CONTROL_AWB_MODE_OFF)) {
993 default_awb_mode = ANDROID_CONTROL_AWB_MODE_OFF;
994 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700995 res = base_metadata.update(ANDROID_CONTROL_AWB_MODE, &default_awb_mode, 1);
996 if (res != android::OK) {
997 return res;
998 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700999
1000 // AWB regions not supported.
1001
1002 /* Other controls. */
1003
1004 uint8_t effect_mode = ANDROID_CONTROL_EFFECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001005 res = base_metadata.update(ANDROID_CONTROL_EFFECT_MODE, &effect_mode, 1);
1006 if (res != android::OK) {
1007 return res;
1008 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001009
1010 uint8_t control_mode = ANDROID_CONTROL_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001011 res = base_metadata.update(ANDROID_CONTROL_MODE, &control_mode, 1);
1012 if (res != android::OK) {
1013 return res;
1014 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001015
1016 uint8_t scene_mode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001017 res = base_metadata.update(ANDROID_CONTROL_SCENE_MODE,
1018 &scene_mode, 1);
1019 if (res != android::OK) {
1020 return res;
1021 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001022
1023 uint8_t video_stabilization = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001024 res = base_metadata.update(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
1025 &video_stabilization, 1);
1026 if (res != android::OK) {
1027 return res;
1028 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001029
1030 // postRawSensitivityBoost: RAW not supported, leave null.
1031
1032 /* android.demosaic. */
1033
1034 // mode marked FUTURE.
1035
1036 /* android.edge. */
1037
1038 uint8_t edge_mode = ANDROID_EDGE_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001039 res = base_metadata.update(ANDROID_EDGE_MODE, &edge_mode, 1);
1040 if (res != android::OK) {
1041 return res;
1042 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001043
1044 // strength marked FUTURE.
1045
1046 /* android.flash. */
1047
1048 // firingPower, firingTime marked FUTURE.
1049
1050 uint8_t flash_mode = ANDROID_FLASH_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001051 res = base_metadata.update(ANDROID_FLASH_MODE, &flash_mode, 1);
1052 if (res != android::OK) {
1053 return res;
1054 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001055
1056 /* android.hotPixel. */
1057
1058 uint8_t hp_mode = ANDROID_HOT_PIXEL_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001059 res = base_metadata.update(ANDROID_HOT_PIXEL_MODE, &hp_mode, 1);
1060 if (res != android::OK) {
1061 return res;
1062 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001063
1064 /* android.jpeg. */
1065
1066 double gps_coords[] = {/*latitude*/0, /*longitude*/0, /*altitude*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001067 res = base_metadata.update(ANDROID_JPEG_GPS_COORDINATES, gps_coords, 3);
1068 if (res != android::OK) {
1069 return res;
1070 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001071
1072 uint8_t gps_processing_method[] = "none";
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001073 res = base_metadata.update(ANDROID_JPEG_GPS_PROCESSING_METHOD,
1074 gps_processing_method,
1075 ARRAY_SIZE(gps_processing_method));
1076 if (res != android::OK) {
1077 return res;
1078 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001079
1080 int64_t gps_timestamp = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001081 res = base_metadata.update(ANDROID_JPEG_GPS_TIMESTAMP, &gps_timestamp, 1);
1082 if (res != android::OK) {
1083 return res;
1084 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001085
1086 // JPEG orientation is relative to sensor orientation (mOrientation).
1087 int32_t jpeg_orientation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001088 res = base_metadata.update(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1);
1089 if (res != android::OK) {
1090 return res;
1091 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001092
1093 // 1-100, larger is higher quality.
1094 uint8_t jpeg_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001095 res = base_metadata.update(ANDROID_JPEG_QUALITY, &jpeg_quality, 1);
1096 if (res != android::OK) {
1097 return res;
1098 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001099
1100 // TODO(b/29580107): If thumbnail quality actually matters/can be adjusted,
1101 // adjust this.
1102 uint8_t thumbnail_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001103 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_QUALITY,
1104 &thumbnail_quality, 1);
1105 if (res != android::OK) {
1106 return res;
1107 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001108
1109 // TODO(b/29580107): Choose a size matching the resolution.
1110 int32_t thumbnail_size[] = {0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001111 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_SIZE, thumbnail_size, 2);
1112 if (res != android::OK) {
1113 return res;
1114 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001115
1116 /* android.lens. */
1117
1118 // Fixed values.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001119 res = base_metadata.update(ANDROID_LENS_APERTURE, &mAperture, 1);
1120 if (res != android::OK) {
1121 return res;
1122 }
1123 res = base_metadata.update(ANDROID_LENS_FILTER_DENSITY, &mFilterDensity, 1);
1124 if (res != android::OK) {
1125 return res;
1126 }
1127 res = base_metadata.update(ANDROID_LENS_FOCAL_LENGTH, &mFocalLength, 1);
1128 if (res != android::OK) {
1129 return res;
1130 }
1131 res = base_metadata.update(ANDROID_LENS_FOCUS_DISTANCE, &mFocusDistance, 1);
1132 if (res != android::OK) {
1133 return res;
1134 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001135
1136 uint8_t optical_stabilization = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001137 res = base_metadata.update(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001138 &optical_stabilization, 1);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001139 if (res != android::OK) {
1140 return res;
1141 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001142
1143 /* android.noiseReduction. */
1144
1145 uint8_t noise_reduction_mode = ANDROID_NOISE_REDUCTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001146 res = base_metadata.update(ANDROID_NOISE_REDUCTION_MODE,
1147 &noise_reduction_mode, 1);
1148 if (res != android::OK) {
1149 return res;
1150 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001151
1152 // strength marked FUTURE.
1153
1154 /* android.request. */
1155
1156 // Request Id unused by the HAL for now, and these are just
1157 // templates, so just fill it in with a dummy.
1158 int32_t id = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001159 res = base_metadata.update(ANDROID_REQUEST_ID, &id, 1);
1160 if (res != android::OK) {
1161 return res;
1162 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001163
1164 // metadataMode marked FUTURE.
1165
1166 /* android.scaler. */
1167
1168 // No cropping by default; use the full active array.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001169 res = base_metadata.update(ANDROID_SCALER_CROP_REGION, mPixelArraySize.data(),
1170 mPixelArraySize.size());
1171 if (res != android::OK) {
1172 return res;
1173 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001174
1175 /* android.sensor. */
1176
1177 // exposureTime, sensitivity, testPattern[Data,Mode] not supported.
1178
1179 // Ignored when AE is OFF.
1180 int64_t frame_duration = 33333333L; // 1/30 s.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001181 res = base_metadata.update(ANDROID_SENSOR_FRAME_DURATION, &frame_duration, 1);
1182 if (res != android::OK) {
1183 return res;
1184 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001185
1186 /* android.shading. */
1187
1188 uint8_t shading_mode = ANDROID_SHADING_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001189 res = base_metadata.update(ANDROID_SHADING_MODE, &shading_mode, 1);
1190 if (res != android::OK) {
1191 return res;
1192 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001193
1194 /* android.statistics. */
1195
1196 uint8_t face_detect_mode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001197 res = base_metadata.update(ANDROID_STATISTICS_FACE_DETECT_MODE,
1198 &face_detect_mode, 1);
1199 if (res != android::OK) {
1200 return res;
1201 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001202
1203 // histogramMode, sharpnessMapMode marked FUTURE.
1204
1205 uint8_t hp_map_mode = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001206 res = base_metadata.update(ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
1207 &hp_map_mode, 1);
1208 if (res != android::OK) {
1209 return res;
1210 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001211
1212 uint8_t lens_shading_map_mode = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001213 res = base_metadata.update(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
1214 &lens_shading_map_mode, 1);
1215 if (res != android::OK) {
1216 return res;
1217 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001218
1219 /* android.tonemap. */
1220
1221 // Tonemap only required for MANUAL_POST_PROCESSING capability.
1222
1223 /* android.led. */
1224
1225 uint8_t transmit = ANDROID_LED_TRANSMIT_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001226 res = base_metadata.update(ANDROID_LED_TRANSMIT, &transmit, 1);
1227 if (res != android::OK) {
1228 return res;
1229 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001230
1231 /* android.reprocess */
1232
1233 // Only needed for REPROCESS capability.
1234
1235
1236 /* Template variable values. */
1237
1238 // Find the FPS ranges "closest" to a desired range
1239 // (minimum abs distance from min to min and max to max).
1240 // Find both a fixed rate and a variable rate, for different purposes.
1241 std::array<int32_t, 2> desired_flat_fps_range = {{30, 30}};
1242 std::array<int32_t, 2> desired_variable_fps_range = {{5, 30}};
1243 std::array<int32_t, 2> flat_fps_range;
1244 std::array<int32_t, 2> variable_fps_range;
1245 int32_t best_flat_distance = std::numeric_limits<int32_t>::max();
1246 int32_t best_variable_distance = std::numeric_limits<int32_t>::max();
1247 size_t num_fps_ranges = mFpsRanges.num_arrays();
1248 for (size_t i = 0; i < num_fps_ranges; ++i) {
1249 const int32_t* range = mFpsRanges[i];
1250 // Variable fps.
1251 int32_t distance = std::abs(range[0] - desired_variable_fps_range[0]) +
1252 std::abs(range[1] - desired_variable_fps_range[1]);
1253 if (distance < best_variable_distance) {
1254 variable_fps_range[0] = range[0];
1255 variable_fps_range[1] = range[1];
1256 best_variable_distance = distance;
1257 }
1258 // Flat fps. Only do if range is actually flat.
1259 // Note at least one flat range is required,
1260 // so something will always be filled in.
1261 if (range[0] == range[1]) {
1262 distance = std::abs(range[0] - desired_flat_fps_range[0]) +
1263 std::abs(range[1] - desired_flat_fps_range[1]);
1264 if (distance < best_flat_distance) {
1265 flat_fps_range[0] = range[0];
1266 flat_fps_range[1] = range[1];
1267 best_flat_distance = distance;
1268 }
1269 }
1270 }
1271
1272 // Priority: continuous > auto > off > whatever is available.
1273 bool continuous_still_avail = std::count(
1274 mAfModes.begin(), mAfModes.end(),
1275 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE);
1276 bool continuous_video_avail = std::count(
1277 mAfModes.begin(), mAfModes.end(),
1278 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO);
1279 uint8_t non_continuous_af_mode = mAfModes[0];
1280 if (std::count(mAfModes.begin(), mAfModes.end(),
1281 ANDROID_CONTROL_AF_MODE_AUTO)) {
1282 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_AUTO;
1283 } else if (std::count(mAfModes.begin(), mAfModes.end(),
1284 ANDROID_CONTROL_AF_MODE_OFF)) {
1285 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_OFF;
1286 }
1287 uint8_t still_af_mode = continuous_still_avail ?
1288 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE : non_continuous_af_mode;
1289 uint8_t video_af_mode = continuous_video_avail ?
1290 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO : non_continuous_af_mode;
1291
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001292 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
1293 ++template_id) {
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001294 // General differences/support.
1295 uint8_t intent;
1296 uint8_t af_mode;
1297 std::array<int32_t, 2> fps_range;
1298 switch(template_id) {
1299 case CAMERA3_TEMPLATE_PREVIEW:
1300 intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
1301 af_mode = still_af_mode;
1302 fps_range = flat_fps_range;
1303 break;
1304 case CAMERA3_TEMPLATE_STILL_CAPTURE:
1305 intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
1306 af_mode = still_af_mode;
1307 fps_range = variable_fps_range;
1308 break;
1309 case CAMERA3_TEMPLATE_VIDEO_RECORD:
1310 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
1311 af_mode = video_af_mode;
1312 fps_range = flat_fps_range;
1313 break;
1314 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
1315 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
1316 af_mode = video_af_mode;
1317 fps_range = flat_fps_range;
1318 break;
1319 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: // Fall through.
1320 case CAMERA3_TEMPLATE_MANUAL: // Fall though.
1321 default:
1322 // Unsupported/unrecognized. Don't add this template; skip it.
1323 continue;
1324 }
1325
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001326 // Copy our base metadata and add the new items.
1327 android::CameraMetadata template_metadata(base_metadata);
1328 res = template_metadata.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent, 1);
1329 if (res != android::OK) {
1330 return res;
1331 }
1332 res = template_metadata.update(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
1333 fps_range.data(), fps_range.size());
1334 if (res != android::OK) {
1335 return res;
1336 }
1337 res = template_metadata.update(ANDROID_CONTROL_AF_MODE, &af_mode, 1);
1338 if (res != android::OK) {
1339 return res;
1340 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001341
1342 const camera_metadata_t* template_raw_metadata =
1343 template_metadata.getAndLock();
1344 res = setTemplate(template_id, template_raw_metadata);
1345 if (res != android::OK) {
1346 return res;
1347 }
1348 res = template_metadata.unlock(template_raw_metadata);
1349 if (res != android::OK) {
1350 return res;
1351 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001352 }
1353
1354 mTemplatesInitialized = true;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001355 return 0;
1356}
1357
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001358bool V4L2Camera::isSupportedStreamSet(default_camera_hal::Stream** streams,
1359 int count, uint32_t mode) {
1360 HAL_LOG_ENTER();
1361
1362 if (mode != CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE) {
1363 HAL_LOGE("Unsupported stream configuration mode: %d", mode);
1364 return false;
1365 }
1366
1367 // This should be checked by the caller, but put here as a sanity check.
1368 if (count < 1) {
1369 HAL_LOGE("Must request at least 1 stream");
1370 return false;
1371 }
1372
1373 // Count the number of streams of each type.
1374 int32_t num_input = 0;
1375 int32_t num_raw = 0;
1376 int32_t num_yuv = 0;
1377 int32_t num_jpeg = 0;
1378 for (int i = 0; i < count; ++i) {
1379 default_camera_hal::Stream* stream = streams[i];
1380
1381 if (stream->isInputType()) {
1382 ++num_input;
1383 }
1384
1385 if (stream->isOutputType()) {
1386 switch (halToV4L2PixelFormat(stream->getFormat())) {
1387 case V4L2_PIX_FMT_YUV420:
1388 ++num_yuv;
1389 break;
1390 case V4L2_PIX_FMT_JPEG:
1391 ++num_jpeg;
1392 break;
1393 default:
1394 // Note: no supported raw formats at this time.
1395 HAL_LOGE("Unsupported format for stream %d: %d", i, stream->getFormat());
1396 return false;
1397 }
1398 }
1399 }
1400
1401 if (num_input > mMaxInputStreams || num_raw > mMaxRawOutputStreams ||
1402 num_yuv > mMaxYuvOutputStreams || num_jpeg > mMaxJpegOutputStreams) {
1403 HAL_LOGE("Invalid stream configuration: %d input, %d RAW, %d YUV, %d JPEG "
1404 "(max supported: %d input, %d RAW, %d YUV, %d JPEG)",
1405 mMaxInputStreams, mMaxRawOutputStreams, mMaxYuvOutputStreams,
1406 mMaxJpegOutputStreams, num_input, num_raw, num_yuv, num_jpeg);
1407 return false;
1408 }
1409
1410 // TODO(b/29939583): The above logic should be all that's necessary,
1411 // but V4L2 doesn't actually support more than 1 stream at a time. So for now,
1412 // if not all streams are the same format and size, error. Note that this
1413 // means the HAL is not spec-compliant; the requested streams are technically
1414 // valid and it is not technically allowed to error once it has reached this
1415 // point.
1416 int format = streams[0]->getFormat();
1417 uint32_t width = streams[0]->getWidth();
1418 uint32_t height = streams[0]->getHeight();
1419 for (int i = 1; i < count; ++i) {
1420 const default_camera_hal::Stream* stream = streams[i];
1421 if (stream->getFormat() != format || stream->getWidth() != width ||
1422 stream->getHeight() != height) {
1423 HAL_LOGE("V4L2 only supports 1 stream configuration at a time "
1424 "(stream 0 is format %d, width %u, height %u, "
1425 "stream %d is format %d, width %u, height %u).",
1426 format, width, height, i, stream->getFormat(),
1427 stream->getWidth(), stream->getHeight());
1428 return false;
1429 }
1430 }
1431
1432 return true;
1433}
1434
1435int V4L2Camera::setupStream(default_camera_hal::Stream* stream,
1436 uint32_t* max_buffers) {
1437 HAL_LOG_ENTER();
1438
1439 if (stream->getRotation() != CAMERA3_STREAM_ROTATION_0) {
1440 HAL_LOGE("Rotation %d not supported", stream->getRotation());
1441 return -EINVAL;
1442 }
1443
1444 // Doesn't matter what was requested, we always use dataspace V0_JFIF.
1445 // Note: according to camera3.h, this isn't allowed, but etalvala@google.com
1446 // claims it's underdocumented; the implementation lets the HAL overwrite it.
1447 stream->setDataSpace(HAL_DATASPACE_V0_JFIF);
1448
1449 int ret = setFormat(stream);
1450 if (ret) {
1451 return ret;
1452 }
1453
1454 // Only do buffer setup if we don't know our maxBuffers.
1455 if (mOutStreamMaxBuffers < 1) {
1456 ret = setupBuffers();
1457 if (ret) {
1458 return ret;
1459 }
1460 }
1461
1462 // Sanity check.
1463 if (mOutStreamMaxBuffers < 1) {
1464 HAL_LOGE("HAL failed to determine max number of buffers.");
1465 return -ENODEV;
1466 }
1467 *max_buffers = mOutStreamMaxBuffers;
1468
1469 return 0;
1470}
1471
1472int V4L2Camera::setFormat(const default_camera_hal::Stream* stream) {
1473 HAL_LOG_ENTER();
1474
1475 // Should be checked earlier; sanity check.
1476 if (stream->isInputType()) {
1477 HAL_LOGE("Input streams not supported.");
1478 return -EINVAL;
1479 }
1480
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001481 // TODO(b/30000211): Check if appropriate to do multi-planar capture instead.
1482 uint32_t desired_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1483 uint32_t desired_format = halToV4L2PixelFormat(stream->getFormat());
1484 uint32_t desired_width = stream->getWidth();
1485 uint32_t desired_height = stream->getHeight();
1486
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001487 // Check to make sure we're not already in the correct format.
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001488 if (desired_type == mOutStreamType &&
1489 desired_format == mOutStreamFormat &&
1490 desired_width == mOutStreamWidth &&
1491 desired_height == mOutStreamHeight) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001492 return 0;
1493 }
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001494
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001495 // Not in the correct format, set our format.
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001496 v4l2_format format;
1497 memset(&format, 0, sizeof(format));
1498 format.type = desired_type;
1499 format.fmt.pix.width = desired_width;
1500 format.fmt.pix.height = desired_height;
1501 format.fmt.pix.pixelformat = desired_format;
1502 // TODO(b/29334616): When async, this will need to check if the stream
1503 // is on, and if so, lock it off while setting format.
1504 if (ioctlLocked(VIDIOC_S_FMT, &format) < 0) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001505 HAL_LOGE("S_FMT failed: %s", strerror(errno));
1506 return -ENODEV;
1507 }
1508 // Check that the driver actually set to the requested values.
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001509 if (format.type != desired_type ||
1510 format.fmt.pix.pixelformat != desired_format ||
1511 format.fmt.pix.width != desired_width ||
1512 format.fmt.pix.height != desired_height) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001513 HAL_LOGE("Device doesn't support desired stream configuration.");
1514 return -EINVAL;
1515 }
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001516
1517 // Keep track of the new format.
1518 mOutStreamType = format.type;
1519 mOutStreamFormat = format.fmt.pix.pixelformat;
1520 mOutStreamWidth = format.fmt.pix.width;
1521 mOutStreamHeight = format.fmt.pix.height;
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -07001522 mOutStreamBytesPerLine = format.fmt.pix.bytesperline;
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -07001523 HAL_LOGV("New format: type %u, pixel format %u, width %u, height %u, "
1524 "bytes/line %u", mOutStreamType, mOutStreamFormat, mOutStreamWidth,
1525 mOutStreamHeight, mOutStreamBytesPerLine);
1526
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001527 // Since our format changed, our maxBuffers may be incorrect.
1528 mOutStreamMaxBuffers = 0;
1529
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -07001530
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001531 return 0;
1532}
1533
1534int V4L2Camera::setupBuffers() {
1535 HAL_LOG_ENTER();
1536
1537 // "Request" a buffer (since we're using a userspace buffer, this just
1538 // tells V4L2 to switch into userspace buffer mode).
1539 v4l2_requestbuffers req_buffers;
1540 memset(&req_buffers, 0, sizeof(req_buffers));
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -07001541 req_buffers.type = mOutStreamType;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001542 req_buffers.memory = V4L2_MEMORY_USERPTR;
1543 req_buffers.count = 1;
1544 if (ioctlLocked(VIDIOC_REQBUFS, &req_buffers) < 0) {
1545 HAL_LOGE("REQBUFS failed: %s", strerror(errno));
1546 return -ENODEV;
1547 }
1548
1549 // V4L2 will set req_buffers.count to a number of buffers it can handle.
1550 mOutStreamMaxBuffers = req_buffers.count;
1551 return 0;
1552}
1553
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001554bool V4L2Camera::isValidCaptureSettings(const camera_metadata_t* settings) {
1555 HAL_LOG_ENTER();
1556
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001557 // TODO(b/29335262): reject capture settings this camera isn't capable of.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001558 return true;
1559}
1560
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001561int V4L2Camera::initCharacteristics() {
1562 HAL_LOG_ENTER();
1563
1564 /* Physical characteristics. */
1565 // No way to get these in V4L2, so faked.
1566 // Note: While many of these are primarily informative for post-processing
1567 // calculations by the app and will potentially cause bad results there,
1568 // focal length and physical size are actually used in framework
1569 // calculations (field of view, pixel pitch, etc), so faking them may
1570 // have unexpected results.
1571 mAperture = 2.0; // RPi camera v2 is f/2.0.
1572 mFilterDensity = 0.0;
1573 mFocalLength = 3.04; // RPi camera v2 is 3.04mm.
1574 mOrientation = 0;
1575 mPhysicalSize = {{3.674, 2.760}}; // RPi camera v2 is 3.674 x 2.760 mm.
1576
1577 /* Fixed features. */
1578
1579 // TODO(b/29394024): query VIDIOC_CROPCAP to get pixel rectangle.
1580 // Spoofing as 640 x 480 for now.
1581 mPixelArraySize = {{/*xmin*/0, /*ymin*/0, /*width*/640, /*height*/480}};
1582
1583 // V4L2 VIDIOC_CROPCAP doesn't give a way to query this;
1584 // it's driver dependent. For now, assume freeform, and
1585 // some cameras may just behave badly.
1586 // TODO(b/29579652): Figure out a way to determine this.
1587 mCropType = ANDROID_SCALER_CROPPING_TYPE_FREEFORM;
1588
1589 // TODO(b/29394024): query VIDIOC_CROPCAP to get cropping ranges,
1590 // and VIDIOC_G_CROP to determine if cropping is supported.
1591 // If the ioctl isn't available (or cropping has non-square pixelaspect),
1592 // assume no cropping/scaling.
1593 // May need to try setting some crops to determine what the driver actually
1594 // supports (including testing center vs freeform).
1595 mMaxZoom = 1;
1596
1597 // TODO(b/29394024): query V4L2_CID_EXPOSURE_BIAS.
1598 mAeCompensationRange = {{0, 0}};
1599 mAeCompensationStep = {1, 1};
1600
1601 // TODO(b/29394024): query V4L2_CID_3A_LOCK.
1602 mAeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE;
1603 mAwbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE;
1604
1605 // TODO(b/29394024): query V4L2_CID_FLASH_LED_MODE.
1606 mFlashAvailable = 0;
1607
1608 // TODO(b/29394024): query V4L2_CID_FOCUS_ABSOLUTE for focus range.
1609 mFocusDistance = 0; // Fixed focus.
1610
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001611 // TODO(b/29939583): V4L2 can only support 1 stream at a time.
1612 // For now, just reporting minimum allowable for LIMITED devices.
1613 mMaxRawOutputStreams = 0;
1614 mMaxYuvOutputStreams = 2;
1615 mMaxJpegOutputStreams = 1;
1616 // Reprocessing not supported.
1617 mMaxInputStreams = 0;
1618
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001619 /* Features with (potentially) multiple options. */
1620
1621 // TODO(b/29394024): query V4L2_CID_EXPOSURE_AUTO for ae modes.
1622 mAeModes.push_back(ANDROID_CONTROL_AE_MODE_ON);
1623
1624 // TODO(b/29394024): query V4L2_CID_POWER_LINE_FREQUENCY.
1625 // Auto as the default, since it could mean anything, while OFF would
1626 // require guaranteeing no antibanding happens.
1627 mAeAntibandingModes.push_back(ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO);
1628
1629 // TODO(b/29394024): query V4L2_CID_FOCUS_AUTO for
1630 // CONTINUOUS_VIDEO/CONTINUOUS_PICTURE. V4L2_CID_AUTO_FOCUS_START
1631 // supports what Android thinks of as auto focus (single auto focus).
1632 // V4L2_CID_AUTO_FOCUS_RANGE allows MACRO.
1633 mAfModes.push_back(ANDROID_CONTROL_AF_MODE_OFF);
1634
1635 // TODO(b/29394024): query V4L2_CID_AUTO_WHITE_BALANCE, or
1636 // V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE if available.
1637 mAwbModes.push_back(ANDROID_CONTROL_AWB_MODE_AUTO);
1638
1639 // TODO(b/29394024): query V4L2_CID_SCENE_MODE.
1640 mSceneModes.push_back(ANDROID_CONTROL_SCENE_MODE_DISABLED);
1641
1642 mControlModes.push_back(ANDROID_CONTROL_MODE_AUTO);
1643 if (mSceneModes.size() > 1) {
1644 // We have some mode other than just DISABLED available.
1645 mControlModes.push_back(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
1646 }
1647
1648 // TODO(b/29394024): query V4L2_CID_COLORFX.
1649 mEffects.push_back(ANDROID_CONTROL_EFFECT_MODE_OFF);
1650
1651 // TODO(b/29394024): query V4L2_CID_FLASH_INDICATOR_INTENSITY.
1652 // For now, no indicator LED available; nothing to push back.
1653 // When there is, push back ANDROID_LED_AVAILABLE_LEDS_TRANSMIT.
1654
1655 // TODO(b/29394024): query V4L2_CID_IMAGE_STABILIZATION.
1656 mOpticalStabilizationModes.push_back(
1657 ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF);
1658 mVideoStabilizationModes.push_back(
1659 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF);
1660
1661 // Need to support YUV_420_888 and JPEG.
1662 // TODO(b/29394024): query VIDIOC_ENUM_FMT.
1663 std::vector<uint32_t> formats = {{V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420}};
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001664 // We want to find the smallest maximum frame duration amongst formats.
1665 mMaxFrameDuration = std::numeric_limits<int64_t>::max();
1666 int64_t min_yuv_frame_duration = std::numeric_limits<int64_t>::max();
1667 for (auto format : formats) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001668 int32_t hal_format = V4L2ToHalPixelFormat(format);
1669 if (hal_format < 0) {
1670 // Unrecognized/unused format. Skip it.
1671 continue;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001672 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001673 // TODO(b/29394024): query VIDIOC_ENUM_FRAMESIZES.
1674 // For now, just 640x480.
1675 ArrayVector<int32_t, 2> frame_sizes;
1676 frame_sizes.push_back({{640, 480}});
1677 size_t num_frame_sizes = frame_sizes.num_arrays();
1678 for (size_t i = 0; i < num_frame_sizes; ++i) {
1679 const int32_t* frame_size = frame_sizes[i];
1680 mStreamConfigs.push_back({{hal_format, frame_size[0], frame_size[1],
1681 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT}});
1682
1683 // TODO(b/29394024): query VIDIOC_ENUM_FRAMEINTERVALS.
1684 // For now, using the emulator max value of .3 sec.
1685 int64_t max_frame_duration = 300000000;
1686 // For now, min value of 30 fps = 1/30 spf ~= 33333333 ns.
1687 // For whatever reason the goldfish/qcom cameras report this as
1688 // 33331760, so copying that.
1689 int64_t min_frame_duration = 33331760;
1690
1691 mMinFrameDurations.push_back(
1692 {{hal_format, frame_size[0], frame_size[1], min_frame_duration}});
1693
1694 // In theory max frame duration (min frame rate) should be consistent
1695 // between all formats, but we check and only advertise the smallest
1696 // available max duration just in case.
1697 if (max_frame_duration < mMaxFrameDuration) {
1698 mMaxFrameDuration = max_frame_duration;
1699 }
1700
1701 // We only care about min frame duration (max frame rate) for YUV.
1702 if (hal_format == HAL_PIXEL_FORMAT_YCbCr_420_888 &&
1703 min_frame_duration < min_yuv_frame_duration) {
1704 min_yuv_frame_duration = min_frame_duration;
1705 }
1706
1707 // Usually 0 for non-jpeg, non-zero for JPEG.
1708 // Randomly choosing absurd 1 sec for JPEG. Unsure what this breaks.
1709 int64_t stall_duration = 0;
1710 if (hal_format == HAL_PIXEL_FORMAT_BLOB) {
1711 stall_duration = 1000000000;
1712 }
1713 mStallDurations.push_back(
1714 {{hal_format, frame_size[0], frame_size[1], stall_duration}});
1715 }
1716 }
1717
1718 // This should be at minimum {mi, ma}, {ma, ma} where mi and ma
1719 // are min and max frame rates for YUV_420_888. Min should be at most 15.
1720 // Convert from frame durations measured in ns.
1721 int32_t min_yuv_fps = 1000000000 / mMaxFrameDuration;
1722 if (min_yuv_fps > 15) {
1723 return -ENODEV;
1724 }
1725 int32_t max_yuv_fps = 1000000000 / min_yuv_frame_duration;
1726 mFpsRanges.push_back({{min_yuv_fps, max_yuv_fps}});
1727 mFpsRanges.push_back({{max_yuv_fps, max_yuv_fps}});
1728 // Always advertise {30, 30} if max is even higher,
1729 // since this is what the default video requests use.
1730 if (max_yuv_fps > 30) {
1731 mFpsRanges.push_back({{30, 30}});
1732 }
1733
1734 mCharacteristicsInitialized = true;
1735 return 0;
1736}
1737
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001738int V4L2Camera::V4L2ToHalPixelFormat(uint32_t v4l2_format) {
1739 // Translate V4L2 format to HAL format.
1740 int hal_format = -1;
1741 switch (v4l2_format) {
1742 case V4L2_PIX_FMT_JPEG:
1743 hal_format = HAL_PIXEL_FORMAT_BLOB;
1744 break;
1745 case V4L2_PIX_FMT_YUV420:
1746 hal_format = HAL_PIXEL_FORMAT_YCbCr_420_888;
1747 break;
1748 default:
1749 // Unrecognized format.
1750 break;
1751 }
1752 return hal_format;
1753}
1754
1755uint32_t V4L2Camera::halToV4L2PixelFormat(int hal_format) {
1756 // Translate HAL format to V4L2 format.
1757 uint32_t v4l2_format = 0;
1758 switch (hal_format) {
1759 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: // fall-through.
1760 case HAL_PIXEL_FORMAT_YCbCr_420_888:
1761 v4l2_format = V4L2_PIX_FMT_YUV420;
1762 break;
1763 case HAL_PIXEL_FORMAT_BLOB:
1764 v4l2_format = V4L2_PIX_FMT_JPEG;
1765 break;
1766 default:
1767 // Unrecognized format.
1768 break;
1769 }
1770 return v4l2_format;
1771}
1772
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001773} // namespace v4l2_camera_hal