blob: 623e0a5f3d21fca3908aaee0b3d1c2da1fc975fb [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-Cohen681eaa22016-07-21 16:28:17 -070050V4L2Camera* V4L2Camera::NewV4L2Camera(int id, const std::string path) {
51 HAL_LOG_ENTER();
52
53 std::unique_ptr<V4L2Gralloc> gralloc(V4L2Gralloc::NewV4L2Gralloc());
54 if (!gralloc) {
55 HAL_LOGE("Failed to initialize gralloc helper.");
56 return nullptr;
57 }
58
59 return new V4L2Camera(id, path, std::move(gralloc));
60}
61
62V4L2Camera::V4L2Camera(int id, std::string path,
63 std::unique_ptr<V4L2Gralloc> gralloc)
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070064 : default_camera_hal::Camera(id),
65 mDevicePath(std::move(path)),
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070066 mGralloc(std::move(gralloc)),
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -070067 mOutStreamType(0),
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070068 mOutStreamFormat(0),
69 mOutStreamWidth(0),
70 mOutStreamHeight(0),
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070071 mOutStreamBytesPerLine(0),
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070072 mOutStreamMaxBuffers(0),
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070073 mTemplatesInitialized(false),
74 mCharacteristicsInitialized(false) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070075 HAL_LOG_ENTER();
76}
77
78V4L2Camera::~V4L2Camera() {
79 HAL_LOG_ENTER();
80}
81
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070082// Helper function. Should be used instead of ioctl throughout this class.
83template<typename T>
84int V4L2Camera::ioctlLocked(int request, T data) {
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -070085 HAL_LOG_ENTER();
86
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070087 android::Mutex::Autolock al(mDeviceLock);
88 if (mDeviceFd.get() < 0) {
89 return -ENODEV;
90 }
91 return TEMP_FAILURE_RETRY(ioctl(mDeviceFd.get(), request, data));
92}
93
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070094int V4L2Camera::connect() {
95 HAL_LOG_ENTER();
96
97 if (mDeviceFd.get() >= 0) {
98 HAL_LOGE("Camera device %s is opened. Close it first", mDevicePath.c_str());
99 return -EIO;
100 }
101
102 int fd = TEMP_FAILURE_RETRY(open(mDevicePath.c_str(), O_RDWR));
103 if (fd < 0) {
104 HAL_LOGE("failed to open %s (%s)", mDevicePath.c_str(), strerror(errno));
105 return -errno;
106 }
107 mDeviceFd.reset(fd);
108
109 // TODO(b/29185945): confirm this is a supported device.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700110 // This is checked by the HAL, but the device at mDevicePath may
111 // not be the same one that was there when the HAL was loaded.
112 // (Alternatively, better hotplugging support may make this unecessary
113 // by disabling cameras that get disconnected and checking newly connected
114 // cameras, so connect() is never called on an unsupported camera)
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700115
116 // TODO(b/29158098): Inform service of any flashes that are no longer available
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700117 // because this camera is in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700118 return 0;
119}
120
121void V4L2Camera::disconnect() {
122 HAL_LOG_ENTER();
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700123 // TODO(b/29158098): Inform service of any flashes that are available again
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700124 // because this camera is no longer in use.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700125
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700126 mDeviceFd.reset();
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -0700127
128 // Clear out our variables tracking device settings.
129 mOutStreamType = 0;
130 mOutStreamFormat = 0;
131 mOutStreamWidth = 0;
132 mOutStreamHeight = 0;
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700133 mOutStreamBytesPerLine = 0;
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -0700134 mOutStreamMaxBuffers = 0;
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700135}
136
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700137int V4L2Camera::streamOn() {
138 HAL_LOG_ENTER();
139
140 if (!mOutStreamType) {
141 HAL_LOGE("Stream format must be set before turning on stream.");
142 return -EINVAL;
143 }
144
145 if (ioctlLocked(VIDIOC_STREAMON, &mOutStreamType) < 0) {
146 HAL_LOGE("STREAMON fails: %s", strerror(errno));
147 return -ENODEV;
148 }
149
150 return 0;
151}
152
153int V4L2Camera::streamOff() {
154 HAL_LOG_ENTER();
155
156 // TODO(b/30000211): support mplane as well.
157 if (ioctlLocked(VIDIOC_STREAMOFF, &mOutStreamType) < 0) {
158 HAL_LOGE("STREAMOFF fails: %s", strerror(errno));
159 return -ENODEV;
160 }
161
162 return 0;
163}
164
165
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700166int V4L2Camera::initStaticInfo(camera_metadata_t** out) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700167 HAL_LOG_ENTER();
168
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700169 android::status_t res;
170 // Device characteristics need to be queried prior
171 // to static info setup.
172 if (!mCharacteristicsInitialized) {
173 res = initCharacteristics();
174 if (res) {
175 return res;
176 }
177 }
178
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700179 android::CameraMetadata info;
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -0700180
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700181 // Static metadata characteristics from /system/media/camera/docs/docs.html.
182
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700183 /* android.colorCorrection. */
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700184
185 // No easy way to turn chromatic aberration correction OFF in v4l2,
186 // though this may be supportable via a collection of other user controls.
187 uint8_t avail_aberration_modes[] = {
188 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST,
189 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700190 res = info.update(ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
191 avail_aberration_modes, ARRAY_SIZE(avail_aberration_modes));
192 if (res != android::OK) {
193 return res;
194 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700195
196 /* android.control. */
197
198 /* 3As */
199
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700200 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
201 mAeAntibandingModes.data(), mAeAntibandingModes.size());
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_AVAILABLE_MODES,
207 mAeModes.data(), mAeModes.size());
208 if (res != android::OK) {
209 return res;
210 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700211
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700212 // Flatten mFpsRanges.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700213 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
214 mFpsRanges.data(), mFpsRanges.total_num_elements());
215 if (res != android::OK) {
216 return res;
217 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700218
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700219 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
220 mAeCompensationRange.data(), mAeCompensationRange.size());
221 if (res != android::OK) {
222 return res;
223 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700224
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700225 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_STEP,
226 &mAeCompensationStep, 1);
227 if (res != android::OK) {
228 return res;
229 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700230
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700231 res = info.update(ANDROID_CONTROL_AF_AVAILABLE_MODES,
232 mAfModes.data(), mAfModes.size());
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_AWB_AVAILABLE_MODES,
238 mAwbModes.data(), mAwbModes.size());
239 if (res != android::OK) {
240 return res;
241 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700242
243 // Couldn't find any V4L2 support for regions, though maybe it's out there.
244 int32_t max_regions[] = {/*AE*/ 0,/*AWB*/ 0,/*AF*/ 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700245 res = info.update(ANDROID_CONTROL_MAX_REGIONS,
246 max_regions, ARRAY_SIZE(max_regions));
247 if (res != android::OK) {
248 return res;
249 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700250
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700251 res = info.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE,
252 &mAeLockAvailable, 1);
253 if (res != android::OK) {
254 return res;
255 }
256 res = info.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
257 &mAwbLockAvailable, 1);
258 if (res != android::OK) {
259 return res;
260 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700261
262 /* Scene modes. */
263
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700264 res = info.update(ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
265 mSceneModes.data(), mSceneModes.size());
266 if (res != android::OK) {
267 return res;
268 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700269
270 // A 3-tuple of AE, AWB, AF overrides for each scene mode.
271 // Ignored for DISABLED, FACE_PRIORITY and FACE_PRIORITY_LOW_LIGHT.
272 uint8_t scene_mode_overrides[] = {
273 /*SCENE_MODE_DISABLED*/ /*AE*/0, /*AW*/0, /*AF*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700274 res = info.update(ANDROID_CONTROL_SCENE_MODE_OVERRIDES,
275 scene_mode_overrides, ARRAY_SIZE(scene_mode_overrides));
276 if (res != android::OK) {
277 return res;
278 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700279
280 /* Top level 3A/Scenes switch. */
281
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700282 res = info.update(ANDROID_CONTROL_AVAILABLE_MODES,
283 mControlModes.data(), mControlModes.size());
284 if (res != android::OK) {
285 return res;
286 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700287
288 /* Other android.control configuration. */
289
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700290 res = info.update(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
291 mVideoStabilizationModes.data(),
292 mVideoStabilizationModes.size());
293 if (res != android::OK) {
294 return res;
295 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700296
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700297 res = info.update(ANDROID_CONTROL_AVAILABLE_EFFECTS,
298 mEffects.data(), mEffects.size());
299 if (res != android::OK) {
300 return res;
301 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700302
303 // AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS only necessary
304 // for devices supporting CONSTRAINED_HIGH_SPEED_VIDEO,
305 // which this HAL doesn't support.
306
307 // POST_RAW_SENSITIVITY_BOOST_RANGE only necessary
308 // for devices supporting RAW format outputs.
309
310 /* android.edge. */
311
312 // Not sure if V4L2 does or doesn't do this, but HAL documentation says
313 // all devices must support FAST, and FAST can be equivalent to OFF, so
314 // either way it's fine to list.
315 uint8_t avail_edge_modes[] = {
316 ANDROID_EDGE_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700317 res = info.update(ANDROID_EDGE_AVAILABLE_EDGE_MODES,
318 avail_edge_modes, ARRAY_SIZE(avail_edge_modes));
319 if (res != android::OK) {
320 return res;
321 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700322
323 /* android.flash. */
324
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700325 res = info.update(ANDROID_FLASH_INFO_AVAILABLE,
326 &mFlashAvailable, 1);
327 if (res != android::OK) {
328 return res;
329 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700330
331 // info.chargeDuration, color.Temperature, maxEnergy marked FUTURE.
332
333 /* android.hotPixel. */
334
335 // No known V4L2 hot pixel correction. But it might be happening,
336 // so we report FAST/HIGH_QUALITY.
337 uint8_t avail_hot_pixel_modes[] = {
338 ANDROID_HOT_PIXEL_MODE_FAST,
339 ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700340 res = info.update(ANDROID_HOT_PIXEL_AVAILABLE_HOT_PIXEL_MODES,
341 avail_hot_pixel_modes, ARRAY_SIZE(avail_hot_pixel_modes));
342 if (res != android::OK) {
343 return res;
344 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700345
346 /* android.jpeg. */
347
348 // For now, no thumbnails available (only [0,0], the "no thumbnail" size).
349 // TODO(b/29580107): Could end up with a mismatch between request & result,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700350 // since V4L2 doesn't actually allow for thumbnail size control.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700351 int32_t thumbnail_sizes[] = {
352 0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700353 res = info.update(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
354 thumbnail_sizes, ARRAY_SIZE(thumbnail_sizes));
355 if (res != android::OK) {
356 return res;
357 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700358
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700359 // V4L2 doesn't support querying this,
360 // so instead use a constant (defined in V4L2Gralloc.h).
361 int32_t max_jpeg_size = V4L2_MAX_JPEG_SIZE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700362 res = info.update(ANDROID_JPEG_MAX_SIZE,
363 &max_jpeg_size, 1);
364 if (res != android::OK) {
365 return res;
366 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700367
368 /* android.lens. */
369
370 /* Misc. lens control. */
371
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700372 res = info.update(ANDROID_LENS_INFO_AVAILABLE_APERTURES,
373 &mAperture, 1);
374 if (res != android::OK) {
375 return res;
376 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700377
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700378 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES,
379 &mFilterDensity, 1);
380 if (res != android::OK) {
381 return res;
382 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700383
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700384 res = info.update(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
385 mOpticalStabilizationModes.data(),
386 mOpticalStabilizationModes.size());
387 if (res != android::OK) {
388 return res;
389 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700390
391 // No known V4L2 shading map info.
392 int32_t shading_map_size[] = {1, 1};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700393 res = info.update(ANDROID_LENS_INFO_SHADING_MAP_SIZE,
394 shading_map_size, ARRAY_SIZE(shading_map_size));
395 if (res != android::OK) {
396 return res;
397 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700398
399 // All V4L2 devices are considered to be external facing.
400 uint8_t facing = ANDROID_LENS_FACING_EXTERNAL;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700401 res = info.update(ANDROID_LENS_FACING, &facing, 1);
402 if (res != android::OK) {
403 return res;
404 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700405
406 /* Zoom/Focus. */
407
408 // No way to actually get the focal length in V4L2, but it's a required key,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700409 // so we just fake it.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700410 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
411 &mFocalLength, 1);
412 if (res != android::OK) {
413 return res;
414 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700415
416 // V4L2 focal units do not correspond to a particular physical unit.
417 uint8_t focus_calibration =
418 ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700419 res = info.update(ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
420 &focus_calibration, 1);
421 if (res != android::OK) {
422 return res;
423 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700424
425 // info.hyperfocalDistance not required for UNCALIBRATED.
426
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700427 res = info.update(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
428 &mFocusDistance, 1);
429 if (res != android::OK) {
430 return res;
431 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700432
433 /* Depth. */
434
435 // DEPTH capability not supported by this HAL. Not implemented:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700436 // poseRotation
437 // poseTranslation
438 // intrinsicCalibration
439 // radialDistortion
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700440
441 /* anroid.noise. */
442
443 // Unable to control noise reduction in V4L2 devices,
444 // but FAST is allowed to be the same as OFF.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700445 uint8_t avail_noise_reduction_modes[] = {ANDROID_NOISE_REDUCTION_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700446 res = info.update(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
447 avail_noise_reduction_modes,
448 ARRAY_SIZE(avail_noise_reduction_modes));
449 if (res != android::OK) {
450 return res;
451 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700452
453 /* android.request. */
454
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700455 int32_t max_num_output_streams[] = {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700456 mMaxRawOutputStreams, mMaxYuvOutputStreams, mMaxJpegOutputStreams};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700457 res = info.update(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
458 max_num_output_streams, ARRAY_SIZE(max_num_output_streams));
459 if (res != android::OK) {
460 return res;
461 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700462
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700463 res = info.update(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS,
464 &mMaxInputStreams, 1);
465 if (res != android::OK) {
466 return res;
467 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700468
469 // No way to know for V4L2, so fake with max allowable latency.
470 // Doesn't mean much without per-frame controls.
471 uint8_t pipeline_max_depth = 4;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700472 res = info.update(ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
473 &pipeline_max_depth, 1);
474 if (res != android::OK) {
475 return res;
476 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700477
478 // Partial results not supported; partialResultCount defaults to 1.
479
480 // Available capabilities & keys queried at very end of this method.
481
482 /* android.scaler. */
483
484 /* Cropping. */
485
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700486 res = info.update(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
487 &mMaxZoom, 1);
488 if (res != android::OK) {
489 return res;
490 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700491
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700492 res = info.update(ANDROID_SCALER_CROPPING_TYPE, &mCropType, 1);
493 if (res != android::OK) {
494 return res;
495 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700496
497 /* Streams. */
498
499 // availableInputOutputFormatsMap only required for reprocessing capability.
500
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700501 // Flatten mStreamConfigs.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700502 res = info.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
503 mStreamConfigs.data(),
504 mStreamConfigs.total_num_elements());
505 if (res != android::OK) {
506 return res;
507 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700508
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700509 // Flatten mMinFrameDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700510 res = info.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
511 mMinFrameDurations.data(),
512 mMinFrameDurations.total_num_elements());
513 if (res != android::OK) {
514 return res;
515 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700516
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700517 // Flatten mStallDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700518 res = info.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
519 mStallDurations.data(),
520 mStallDurations.total_num_elements());
521 if (res != android::OK) {
522 return res;
523 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700524
525 /* android.sensor. */
526
527 /* Sizes. */
528
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700529 res = info.update(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
530 mPixelArraySize.data(), mPixelArraySize.size());
531 if (res != android::OK) {
532 return res;
533 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700534 // No V4L2 way to differentiate active vs. inactive parts of the rectangle.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700535 res = info.update(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
536 mPixelArraySize.data(), mPixelArraySize.size());
537 if (res != android::OK) {
538 return res;
539 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700540
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700541 res = info.update(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
542 mPhysicalSize.data(), mPhysicalSize.size());
543 if (res != android::OK) {
544 return res;
545 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700546
547 /* Misc sensor information. */
548
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700549 res = info.update(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
550 &mMaxFrameDuration, 1);
551 if (res != android::OK) {
552 return res;
553 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700554
555 // HAL uses BOOTTIME timestamps.
556 // TODO(b/29457051): make sure timestamps are consistent throughout the HAL.
557 uint8_t timestamp_source = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700558 res = info.update(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
559 &timestamp_source, 1);
560 if (res != android::OK) {
561 return res;
562 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700563
564 // As in initDeviceInfo, no way to actually get orientation.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700565 res = info.update(ANDROID_SENSOR_ORIENTATION, &mOrientation, 1);
566 if (res != android::OK) {
567 return res;
568 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700569
570 // availableTestPatternModes just defaults to OFF, which is fine.
571
572 // info.exposureTimeRange, info.sensitivityRange:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700573 // exposure/sensitivity manual control not supported.
574 // Could query V4L2_CID_ISO_SENSITIVITY to support sensitivity if desired.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700575
576 // info.whiteLevel, info.lensShadingApplied,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700577 // info.preCorrectionPixelArraySize, referenceIlluminant1/2,
578 // calibrationTransform1/2, colorTransform1/2, forwardMatrix1/2,
579 // blackLevelPattern, profileHueSatMapDimensions
580 // all only necessary for RAW.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700581
582 // baseGainFactor marked FUTURE.
583
584 // maxAnalogSensitivity optional for LIMITED device.
585
586 // opticalBlackRegions: No known way to get in V4L2, but not necessary.
587
588 // opaqueRawSize not necessary since RAW_OPAQUE format not supported.
589
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700590 /* android.shading */
591
592 // No known V4L2 lens shading. But it might be happening,
593 // so we report FAST/HIGH_QUALITY.
594 uint8_t avail_shading_modes[] = {
595 ANDROID_SHADING_MODE_FAST,
596 ANDROID_SHADING_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700597 res = info.update(ANDROID_SHADING_AVAILABLE_MODES,
598 avail_shading_modes, ARRAY_SIZE(avail_shading_modes));
599 if (res != android::OK) {
600 return res;
601 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700602
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700603 /* android.statistics */
604
605 // Face detection not supported.
606 uint8_t avail_face_detect_modes[] = {
607 ANDROID_STATISTICS_FACE_DETECT_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700608 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
609 avail_face_detect_modes,
610 ARRAY_SIZE(avail_face_detect_modes));
611 if (res != android::OK) {
612 return res;
613 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700614
615 int32_t max_face_count = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700616 res = info.update(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
617 &max_face_count, 1);
618 if (res != android::OK) {
619 return res;
620 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700621
622 // info.histogramBucketCount, info.maxHistogramCount,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700623 // info.maxSharpnessMapValue, info.sharpnessMapSizemarked FUTURE.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700624
625 // ON only needs to be supported for RAW capable devices.
626 uint8_t avail_hot_pixel_map_modes[] = {
627 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700628 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
629 avail_hot_pixel_map_modes,
630 ARRAY_SIZE(avail_hot_pixel_map_modes));
631 if (res != android::OK) {
632 return res;
633 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700634
635 // ON only needs to be supported for RAW capable devices.
636 uint8_t avail_lens_shading_map_modes[] = {
637 ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700638 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
639 avail_lens_shading_map_modes,
640 ARRAY_SIZE(avail_lens_shading_map_modes));
641 if (res != android::OK) {
642 return res;
643 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700644
645 /* android.tonemap. */
646
647 // tonemapping only required for MANUAL_POST_PROCESSING capability.
648
649 /* android.led. */
650
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700651 // May or may not have LEDs available.
652 if (!mLeds.empty()) {
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700653 res = info.update(ANDROID_LED_AVAILABLE_LEDS, mLeds.data(), mLeds.size());
654 if (res != android::OK) {
655 return res;
656 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700657 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700658
659 /* android.sync. */
660
661 // "LIMITED devices are strongly encouraged to use a non-negative value.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700662 // If UNKNOWN is used here then app developers do not have a way to know
663 // when sensor settings have been applied." - Unfortunately, V4L2 doesn't
664 // really help here either. Could even be that adjusting settings mid-stream
665 // blocks in V4L2, and should be avoided.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700666 int32_t max_latency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700667 res = info.update(ANDROID_SYNC_MAX_LATENCY,
668 &max_latency, 1);
669 if (res != android::OK) {
670 return res;
671 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700672
673 /* android.reprocess. */
674
675 // REPROCESSING not supported by this HAL.
676
677 /* android.depth. */
678
679 // DEPTH not supported by this HAL.
680
681 /* Capabilities and android.info. */
682
683 uint8_t hw_level = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700684 res = info.update(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
685 &hw_level, 1);
686 if (res != android::OK) {
687 return res;
688 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700689
690 uint8_t capabilities[] = {
691 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700692 res = info.update(ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
693 capabilities, ARRAY_SIZE(capabilities));
694 if (res != android::OK) {
695 return res;
696 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700697
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700698 // Scan a default request template for included request keys.
699 if (!mTemplatesInitialized) {
700 res = initTemplates();
701 if (res) {
702 return res;
703 }
704 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700705 const camera_metadata_t* preview_request = nullptr;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700706 // Search templates from the beginning for a supported one.
707 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
708 ++template_id) {
709 preview_request = constructDefaultRequestSettings(template_id);
710 if (preview_request != nullptr) {
711 break;
712 }
713 }
714 if (preview_request == nullptr) {
715 HAL_LOGE("No valid templates, can't get request keys.");
716 return -ENODEV;
717 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700718 std::vector<int32_t> avail_request_keys = getMetadataKeys(preview_request);
719 res = info.update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
720 avail_request_keys.data(), avail_request_keys.size());
721 if (res != android::OK) {
722 return res;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700723 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700724
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700725 // Result keys will be duplicated from the request, plus a few extras.
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700726 // TODO(b/30035628): additonal available result keys.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700727 std::vector<int32_t> avail_result_keys(avail_request_keys);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700728 res = info.update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
729 avail_result_keys.data(), avail_result_keys.size());
730 if (res != android::OK) {
731 return res;
732 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700733
734 // Last thing, once all the available characteristics have been added.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700735 const camera_metadata_t* static_characteristics = info.getAndLock();
736 std::vector<int32_t> avail_characteristics_keys =
737 getMetadataKeys(static_characteristics);
738 res = info.unlock(static_characteristics);
739 if (res != android::OK) {
740 return res;
741 }
742 avail_characteristics_keys.push_back(
743 ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
744 res = info.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
745 avail_characteristics_keys.data(),
746 avail_characteristics_keys.size());
747 if (res != android::OK) {
748 return res;
749 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700750
751 *out = info.release();
752 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700753}
754
755void V4L2Camera::initDeviceInfo(camera_info_t* info) {
756 HAL_LOG_ENTER();
757
758 // For now, just constants.
759 info->facing = CAMERA_FACING_EXTERNAL;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700760 info->orientation = mOrientation;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700761 info->resource_cost = 100;
762 info->conflicting_devices = nullptr;
763 info->conflicting_devices_length = 0;
764}
765
766int V4L2Camera::initDevice() {
767 HAL_LOG_ENTER();
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700768 int res;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700769
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700770 // Templates should be set up if they haven't already been.
771 if (!mTemplatesInitialized) {
772 res = initTemplates();
773 if (res) {
774 return res;
775 }
776 }
777
778 return 0;
779}
780
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700781int V4L2Camera::enqueueBuffer(const camera3_stream_buffer_t* camera_buffer) {
782 HAL_LOG_ENTER();
783
784 // Set up a v4l2 buffer struct.
785 v4l2_buffer device_buffer;
786 memset(&device_buffer, 0, sizeof(device_buffer));
787 device_buffer.type = mOutStreamType;
788
789 // Use QUERYBUF to ensure our buffer/device is in good shape.
790 if (ioctlLocked(VIDIOC_QUERYBUF, &device_buffer) < 0) {
791 HAL_LOGE("QUERYBUF (%d) fails: %s", 0, strerror(errno));
792 return -ENODEV;
793 }
794
795 // Configure the device buffer based on the stream buffer.
796 device_buffer.memory = V4L2_MEMORY_USERPTR;
797 // TODO(b/29334616): when this is async, actually limit the number
798 // of buffers used to the known max, and set this according to the
799 // queue length.
800 device_buffer.index = 0;
801 // Lock the buffer for writing.
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -0700802 int res = mGralloc->lock(camera_buffer, mOutStreamBytesPerLine,
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700803 &device_buffer);
804 if (res) {
805 return res;
806 }
807 if (ioctlLocked(VIDIOC_QBUF, &device_buffer) < 0) {
808 HAL_LOGE("QBUF (%d) fails: %s", 0, strerror(errno));
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -0700809 mGralloc->unlock(&device_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700810 return -ENODEV;
811 }
812
813 // Turn on the stream.
814 // TODO(b/29334616): Lock around stream on/off access, only start stream
815 // if not already on. (For now, since it's synchronous, it will always be
816 // turned off before another call to this function).
817 res = streamOn();
818 if (res) {
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -0700819 mGralloc->unlock(&device_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700820 return res;
821 }
822
823 // TODO(b/29334616): Enqueueing and dequeueing should be separate worker
824 // threads, not in the same function.
825
826 // Dequeue the buffer.
827 v4l2_buffer result_buffer;
828 res = dequeueBuffer(&result_buffer);
829 if (res) {
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -0700830 mGralloc->unlock(&result_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700831 return res;
832 }
833 // Now that we're done painting the buffer, we can unlock it.
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -0700834 res = mGralloc->unlock(&result_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700835 if (res) {
836 return res;
837 }
838
839 // All done, cleanup.
840 // TODO(b/29334616): Lock around stream on/off access, only stop stream if
841 // buffer queue is empty (synchronously, there's only ever 1 buffer in the
842 // queue at a time, so this is safe).
843 res = streamOff();
844 if (res) {
845 return res;
846 }
847
848 // Sanity check.
849 if (result_buffer.m.userptr != device_buffer.m.userptr) {
850 HAL_LOGE("Got the wrong buffer 0x%x back (expected 0x%x)",
851 result_buffer.m.userptr, device_buffer.m.userptr);
852 return -ENODEV;
853 }
854
855 return 0;
856}
857
858int V4L2Camera::dequeueBuffer(v4l2_buffer* buffer) {
859 HAL_LOG_ENTER();
860
861 memset(buffer, 0, sizeof(*buffer));
862 buffer->type = mOutStreamType;
863 buffer->memory = V4L2_MEMORY_USERPTR;
864 if (ioctlLocked(VIDIOC_DQBUF, buffer) < 0) {
865 HAL_LOGE("DQBUF fails: %s", strerror(errno));
866 return -ENODEV;
867 }
868 return 0;
869}
870
871int V4L2Camera::getResultSettings(camera_metadata** metadata,
872 uint64_t* timestamp) {
873 HAL_LOG_ENTER();
874
875 int res;
876 android::CameraMetadata frame_metadata(*metadata);
877
878 // TODO(b/30035628): fill in.
879 // For now just spoof the timestamp to a non-0 value and send it back.
880 int64_t frame_time = 1;
881 res = frame_metadata.update(ANDROID_SENSOR_TIMESTAMP, &frame_time, 1);
882 if (res != android::OK) {
883 return res;
884 }
885
886 *metadata = frame_metadata.release();
887 *timestamp = frame_time;
888
889 return 0;
890}
891
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700892int V4L2Camera::initTemplates() {
893 HAL_LOG_ENTER();
894 int res;
895
896 // Device characteristics need to be queried prior
897 // to template setup.
898 if (!mCharacteristicsInitialized) {
899 res = initCharacteristics();
900 if (res) {
901 return res;
902 }
903 }
904
905 // Note: static metadata expects all templates/requests
906 // to provide values for all supported keys.
907
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700908 android::CameraMetadata base_metadata;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700909
910 // Start with defaults for all templates.
911
912 /* android.colorCorrection. */
913
914 uint8_t aberration_mode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700915 res = base_metadata.update(ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
916 &aberration_mode, 1);
917 if (res != android::OK) {
918 return res;
919 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700920
921 uint8_t color_correction_mode = ANDROID_COLOR_CORRECTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700922 res = base_metadata.update(ANDROID_COLOR_CORRECTION_MODE,
923 &color_correction_mode, 1);
924 if (res != android::OK) {
925 return res;
926 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700927
928 // transform and gains are for the unsupported MANUAL_POST_PROCESSING only.
929
930 /* android.control. */
931
932 /* AE. */
933 uint8_t ae_antibanding_mode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700934 res = base_metadata.update(ANDROID_CONTROL_AE_ANTIBANDING_MODE,
935 &ae_antibanding_mode, 1);
936 if (res != android::OK) {
937 return res;
938 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700939
940 // Only matters if AE_MODE = OFF
941 int32_t ae_exposure_compensation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700942 res = base_metadata.update(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
943 &ae_exposure_compensation, 1);
944 if (res != android::OK) {
945 return res;
946 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700947
948 uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700949 res = base_metadata.update(ANDROID_CONTROL_AE_LOCK, &ae_lock, 1);
950 if (res != android::OK) {
951 return res;
952 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700953
954 uint8_t ae_mode = ANDROID_CONTROL_AE_MODE_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700955 res = base_metadata.update(ANDROID_CONTROL_AE_MODE, &ae_mode, 1);
956 if (res != android::OK) {
957 return res;
958 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700959
960 // AE regions not supported.
961
962 // FPS set per-template.
963
964 uint8_t ae_precapture_trigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700965 res = base_metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
966 &ae_precapture_trigger, 1);
967 if (res != android::OK) {
968 return res;
969 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700970
971 /* AF. */
972
973 // AF mode set per-template.
974
975 // AF regions not supported.
976
977 uint8_t af_trigger = ANDROID_CONTROL_AF_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700978 res = base_metadata.update(ANDROID_CONTROL_AF_TRIGGER, &af_trigger, 1);
979 if (res != android::OK) {
980 return res;
981 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700982
983 /* AWB. */
984
985 // Priority: auto > off > Whatever is available.
986 uint8_t default_awb_mode = mAwbModes[0];
987 if (std::count(mAwbModes.begin(), mAwbModes.end(),
988 ANDROID_CONTROL_AWB_MODE_AUTO)) {
989 default_awb_mode = ANDROID_CONTROL_AWB_MODE_AUTO;
990 } else if (std::count(mAwbModes.begin(), mAwbModes.end(),
991 ANDROID_CONTROL_AWB_MODE_OFF)) {
992 default_awb_mode = ANDROID_CONTROL_AWB_MODE_OFF;
993 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700994 res = base_metadata.update(ANDROID_CONTROL_AWB_MODE, &default_awb_mode, 1);
995 if (res != android::OK) {
996 return res;
997 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700998
999 // AWB regions not supported.
1000
1001 /* Other controls. */
1002
1003 uint8_t effect_mode = ANDROID_CONTROL_EFFECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001004 res = base_metadata.update(ANDROID_CONTROL_EFFECT_MODE, &effect_mode, 1);
1005 if (res != android::OK) {
1006 return res;
1007 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001008
1009 uint8_t control_mode = ANDROID_CONTROL_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001010 res = base_metadata.update(ANDROID_CONTROL_MODE, &control_mode, 1);
1011 if (res != android::OK) {
1012 return res;
1013 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001014
1015 uint8_t scene_mode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001016 res = base_metadata.update(ANDROID_CONTROL_SCENE_MODE,
1017 &scene_mode, 1);
1018 if (res != android::OK) {
1019 return res;
1020 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001021
1022 uint8_t video_stabilization = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001023 res = base_metadata.update(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
1024 &video_stabilization, 1);
1025 if (res != android::OK) {
1026 return res;
1027 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001028
1029 // postRawSensitivityBoost: RAW not supported, leave null.
1030
1031 /* android.demosaic. */
1032
1033 // mode marked FUTURE.
1034
1035 /* android.edge. */
1036
1037 uint8_t edge_mode = ANDROID_EDGE_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001038 res = base_metadata.update(ANDROID_EDGE_MODE, &edge_mode, 1);
1039 if (res != android::OK) {
1040 return res;
1041 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001042
1043 // strength marked FUTURE.
1044
1045 /* android.flash. */
1046
1047 // firingPower, firingTime marked FUTURE.
1048
1049 uint8_t flash_mode = ANDROID_FLASH_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001050 res = base_metadata.update(ANDROID_FLASH_MODE, &flash_mode, 1);
1051 if (res != android::OK) {
1052 return res;
1053 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001054
1055 /* android.hotPixel. */
1056
1057 uint8_t hp_mode = ANDROID_HOT_PIXEL_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001058 res = base_metadata.update(ANDROID_HOT_PIXEL_MODE, &hp_mode, 1);
1059 if (res != android::OK) {
1060 return res;
1061 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001062
1063 /* android.jpeg. */
1064
1065 double gps_coords[] = {/*latitude*/0, /*longitude*/0, /*altitude*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001066 res = base_metadata.update(ANDROID_JPEG_GPS_COORDINATES, gps_coords, 3);
1067 if (res != android::OK) {
1068 return res;
1069 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001070
1071 uint8_t gps_processing_method[] = "none";
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001072 res = base_metadata.update(ANDROID_JPEG_GPS_PROCESSING_METHOD,
1073 gps_processing_method,
1074 ARRAY_SIZE(gps_processing_method));
1075 if (res != android::OK) {
1076 return res;
1077 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001078
1079 int64_t gps_timestamp = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001080 res = base_metadata.update(ANDROID_JPEG_GPS_TIMESTAMP, &gps_timestamp, 1);
1081 if (res != android::OK) {
1082 return res;
1083 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001084
1085 // JPEG orientation is relative to sensor orientation (mOrientation).
1086 int32_t jpeg_orientation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001087 res = base_metadata.update(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1);
1088 if (res != android::OK) {
1089 return res;
1090 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001091
1092 // 1-100, larger is higher quality.
1093 uint8_t jpeg_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001094 res = base_metadata.update(ANDROID_JPEG_QUALITY, &jpeg_quality, 1);
1095 if (res != android::OK) {
1096 return res;
1097 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001098
1099 // TODO(b/29580107): If thumbnail quality actually matters/can be adjusted,
1100 // adjust this.
1101 uint8_t thumbnail_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001102 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_QUALITY,
1103 &thumbnail_quality, 1);
1104 if (res != android::OK) {
1105 return res;
1106 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001107
1108 // TODO(b/29580107): Choose a size matching the resolution.
1109 int32_t thumbnail_size[] = {0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001110 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_SIZE, thumbnail_size, 2);
1111 if (res != android::OK) {
1112 return res;
1113 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001114
1115 /* android.lens. */
1116
1117 // Fixed values.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001118 res = base_metadata.update(ANDROID_LENS_APERTURE, &mAperture, 1);
1119 if (res != android::OK) {
1120 return res;
1121 }
1122 res = base_metadata.update(ANDROID_LENS_FILTER_DENSITY, &mFilterDensity, 1);
1123 if (res != android::OK) {
1124 return res;
1125 }
1126 res = base_metadata.update(ANDROID_LENS_FOCAL_LENGTH, &mFocalLength, 1);
1127 if (res != android::OK) {
1128 return res;
1129 }
1130 res = base_metadata.update(ANDROID_LENS_FOCUS_DISTANCE, &mFocusDistance, 1);
1131 if (res != android::OK) {
1132 return res;
1133 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001134
1135 uint8_t optical_stabilization = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001136 res = base_metadata.update(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001137 &optical_stabilization, 1);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001138 if (res != android::OK) {
1139 return res;
1140 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001141
1142 /* android.noiseReduction. */
1143
1144 uint8_t noise_reduction_mode = ANDROID_NOISE_REDUCTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001145 res = base_metadata.update(ANDROID_NOISE_REDUCTION_MODE,
1146 &noise_reduction_mode, 1);
1147 if (res != android::OK) {
1148 return res;
1149 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001150
1151 // strength marked FUTURE.
1152
1153 /* android.request. */
1154
1155 // Request Id unused by the HAL for now, and these are just
1156 // templates, so just fill it in with a dummy.
1157 int32_t id = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001158 res = base_metadata.update(ANDROID_REQUEST_ID, &id, 1);
1159 if (res != android::OK) {
1160 return res;
1161 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001162
1163 // metadataMode marked FUTURE.
1164
1165 /* android.scaler. */
1166
1167 // No cropping by default; use the full active array.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001168 res = base_metadata.update(ANDROID_SCALER_CROP_REGION, mPixelArraySize.data(),
1169 mPixelArraySize.size());
1170 if (res != android::OK) {
1171 return res;
1172 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001173
1174 /* android.sensor. */
1175
1176 // exposureTime, sensitivity, testPattern[Data,Mode] not supported.
1177
1178 // Ignored when AE is OFF.
1179 int64_t frame_duration = 33333333L; // 1/30 s.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001180 res = base_metadata.update(ANDROID_SENSOR_FRAME_DURATION, &frame_duration, 1);
1181 if (res != android::OK) {
1182 return res;
1183 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001184
1185 /* android.shading. */
1186
1187 uint8_t shading_mode = ANDROID_SHADING_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001188 res = base_metadata.update(ANDROID_SHADING_MODE, &shading_mode, 1);
1189 if (res != android::OK) {
1190 return res;
1191 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001192
1193 /* android.statistics. */
1194
1195 uint8_t face_detect_mode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001196 res = base_metadata.update(ANDROID_STATISTICS_FACE_DETECT_MODE,
1197 &face_detect_mode, 1);
1198 if (res != android::OK) {
1199 return res;
1200 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001201
1202 // histogramMode, sharpnessMapMode marked FUTURE.
1203
1204 uint8_t hp_map_mode = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001205 res = base_metadata.update(ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
1206 &hp_map_mode, 1);
1207 if (res != android::OK) {
1208 return res;
1209 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001210
1211 uint8_t lens_shading_map_mode = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001212 res = base_metadata.update(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
1213 &lens_shading_map_mode, 1);
1214 if (res != android::OK) {
1215 return res;
1216 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001217
1218 /* android.tonemap. */
1219
1220 // Tonemap only required for MANUAL_POST_PROCESSING capability.
1221
1222 /* android.led. */
1223
1224 uint8_t transmit = ANDROID_LED_TRANSMIT_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001225 res = base_metadata.update(ANDROID_LED_TRANSMIT, &transmit, 1);
1226 if (res != android::OK) {
1227 return res;
1228 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001229
1230 /* android.reprocess */
1231
1232 // Only needed for REPROCESS capability.
1233
1234
1235 /* Template variable values. */
1236
1237 // Find the FPS ranges "closest" to a desired range
1238 // (minimum abs distance from min to min and max to max).
1239 // Find both a fixed rate and a variable rate, for different purposes.
1240 std::array<int32_t, 2> desired_flat_fps_range = {{30, 30}};
1241 std::array<int32_t, 2> desired_variable_fps_range = {{5, 30}};
1242 std::array<int32_t, 2> flat_fps_range;
1243 std::array<int32_t, 2> variable_fps_range;
1244 int32_t best_flat_distance = std::numeric_limits<int32_t>::max();
1245 int32_t best_variable_distance = std::numeric_limits<int32_t>::max();
1246 size_t num_fps_ranges = mFpsRanges.num_arrays();
1247 for (size_t i = 0; i < num_fps_ranges; ++i) {
1248 const int32_t* range = mFpsRanges[i];
1249 // Variable fps.
1250 int32_t distance = std::abs(range[0] - desired_variable_fps_range[0]) +
1251 std::abs(range[1] - desired_variable_fps_range[1]);
1252 if (distance < best_variable_distance) {
1253 variable_fps_range[0] = range[0];
1254 variable_fps_range[1] = range[1];
1255 best_variable_distance = distance;
1256 }
1257 // Flat fps. Only do if range is actually flat.
1258 // Note at least one flat range is required,
1259 // so something will always be filled in.
1260 if (range[0] == range[1]) {
1261 distance = std::abs(range[0] - desired_flat_fps_range[0]) +
1262 std::abs(range[1] - desired_flat_fps_range[1]);
1263 if (distance < best_flat_distance) {
1264 flat_fps_range[0] = range[0];
1265 flat_fps_range[1] = range[1];
1266 best_flat_distance = distance;
1267 }
1268 }
1269 }
1270
1271 // Priority: continuous > auto > off > whatever is available.
1272 bool continuous_still_avail = std::count(
1273 mAfModes.begin(), mAfModes.end(),
1274 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE);
1275 bool continuous_video_avail = std::count(
1276 mAfModes.begin(), mAfModes.end(),
1277 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO);
1278 uint8_t non_continuous_af_mode = mAfModes[0];
1279 if (std::count(mAfModes.begin(), mAfModes.end(),
1280 ANDROID_CONTROL_AF_MODE_AUTO)) {
1281 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_AUTO;
1282 } else if (std::count(mAfModes.begin(), mAfModes.end(),
1283 ANDROID_CONTROL_AF_MODE_OFF)) {
1284 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_OFF;
1285 }
1286 uint8_t still_af_mode = continuous_still_avail ?
1287 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE : non_continuous_af_mode;
1288 uint8_t video_af_mode = continuous_video_avail ?
1289 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO : non_continuous_af_mode;
1290
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001291 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
1292 ++template_id) {
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001293 // General differences/support.
1294 uint8_t intent;
1295 uint8_t af_mode;
1296 std::array<int32_t, 2> fps_range;
1297 switch(template_id) {
1298 case CAMERA3_TEMPLATE_PREVIEW:
1299 intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
1300 af_mode = still_af_mode;
1301 fps_range = flat_fps_range;
1302 break;
1303 case CAMERA3_TEMPLATE_STILL_CAPTURE:
1304 intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
1305 af_mode = still_af_mode;
1306 fps_range = variable_fps_range;
1307 break;
1308 case CAMERA3_TEMPLATE_VIDEO_RECORD:
1309 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
1310 af_mode = video_af_mode;
1311 fps_range = flat_fps_range;
1312 break;
1313 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
1314 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
1315 af_mode = video_af_mode;
1316 fps_range = flat_fps_range;
1317 break;
1318 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: // Fall through.
1319 case CAMERA3_TEMPLATE_MANUAL: // Fall though.
1320 default:
1321 // Unsupported/unrecognized. Don't add this template; skip it.
1322 continue;
1323 }
1324
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001325 // Copy our base metadata and add the new items.
1326 android::CameraMetadata template_metadata(base_metadata);
1327 res = template_metadata.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent, 1);
1328 if (res != android::OK) {
1329 return res;
1330 }
1331 res = template_metadata.update(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
1332 fps_range.data(), fps_range.size());
1333 if (res != android::OK) {
1334 return res;
1335 }
1336 res = template_metadata.update(ANDROID_CONTROL_AF_MODE, &af_mode, 1);
1337 if (res != android::OK) {
1338 return res;
1339 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001340
1341 const camera_metadata_t* template_raw_metadata =
1342 template_metadata.getAndLock();
1343 res = setTemplate(template_id, template_raw_metadata);
1344 if (res != android::OK) {
1345 return res;
1346 }
1347 res = template_metadata.unlock(template_raw_metadata);
1348 if (res != android::OK) {
1349 return res;
1350 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001351 }
1352
1353 mTemplatesInitialized = true;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001354 return 0;
1355}
1356
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001357bool V4L2Camera::isSupportedStreamSet(default_camera_hal::Stream** streams,
1358 int count, uint32_t mode) {
1359 HAL_LOG_ENTER();
1360
1361 if (mode != CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE) {
1362 HAL_LOGE("Unsupported stream configuration mode: %d", mode);
1363 return false;
1364 }
1365
1366 // This should be checked by the caller, but put here as a sanity check.
1367 if (count < 1) {
1368 HAL_LOGE("Must request at least 1 stream");
1369 return false;
1370 }
1371
1372 // Count the number of streams of each type.
1373 int32_t num_input = 0;
1374 int32_t num_raw = 0;
1375 int32_t num_yuv = 0;
1376 int32_t num_jpeg = 0;
1377 for (int i = 0; i < count; ++i) {
1378 default_camera_hal::Stream* stream = streams[i];
1379
1380 if (stream->isInputType()) {
1381 ++num_input;
1382 }
1383
1384 if (stream->isOutputType()) {
1385 switch (halToV4L2PixelFormat(stream->getFormat())) {
1386 case V4L2_PIX_FMT_YUV420:
1387 ++num_yuv;
1388 break;
1389 case V4L2_PIX_FMT_JPEG:
1390 ++num_jpeg;
1391 break;
1392 default:
1393 // Note: no supported raw formats at this time.
1394 HAL_LOGE("Unsupported format for stream %d: %d", i, stream->getFormat());
1395 return false;
1396 }
1397 }
1398 }
1399
1400 if (num_input > mMaxInputStreams || num_raw > mMaxRawOutputStreams ||
1401 num_yuv > mMaxYuvOutputStreams || num_jpeg > mMaxJpegOutputStreams) {
1402 HAL_LOGE("Invalid stream configuration: %d input, %d RAW, %d YUV, %d JPEG "
1403 "(max supported: %d input, %d RAW, %d YUV, %d JPEG)",
1404 mMaxInputStreams, mMaxRawOutputStreams, mMaxYuvOutputStreams,
1405 mMaxJpegOutputStreams, num_input, num_raw, num_yuv, num_jpeg);
1406 return false;
1407 }
1408
1409 // TODO(b/29939583): The above logic should be all that's necessary,
1410 // but V4L2 doesn't actually support more than 1 stream at a time. So for now,
1411 // if not all streams are the same format and size, error. Note that this
1412 // means the HAL is not spec-compliant; the requested streams are technically
1413 // valid and it is not technically allowed to error once it has reached this
1414 // point.
1415 int format = streams[0]->getFormat();
1416 uint32_t width = streams[0]->getWidth();
1417 uint32_t height = streams[0]->getHeight();
1418 for (int i = 1; i < count; ++i) {
1419 const default_camera_hal::Stream* stream = streams[i];
1420 if (stream->getFormat() != format || stream->getWidth() != width ||
1421 stream->getHeight() != height) {
1422 HAL_LOGE("V4L2 only supports 1 stream configuration at a time "
1423 "(stream 0 is format %d, width %u, height %u, "
1424 "stream %d is format %d, width %u, height %u).",
1425 format, width, height, i, stream->getFormat(),
1426 stream->getWidth(), stream->getHeight());
1427 return false;
1428 }
1429 }
1430
1431 return true;
1432}
1433
1434int V4L2Camera::setupStream(default_camera_hal::Stream* stream,
1435 uint32_t* max_buffers) {
1436 HAL_LOG_ENTER();
1437
1438 if (stream->getRotation() != CAMERA3_STREAM_ROTATION_0) {
1439 HAL_LOGE("Rotation %d not supported", stream->getRotation());
1440 return -EINVAL;
1441 }
1442
1443 // Doesn't matter what was requested, we always use dataspace V0_JFIF.
1444 // Note: according to camera3.h, this isn't allowed, but etalvala@google.com
1445 // claims it's underdocumented; the implementation lets the HAL overwrite it.
1446 stream->setDataSpace(HAL_DATASPACE_V0_JFIF);
1447
1448 int ret = setFormat(stream);
1449 if (ret) {
1450 return ret;
1451 }
1452
1453 // Only do buffer setup if we don't know our maxBuffers.
1454 if (mOutStreamMaxBuffers < 1) {
1455 ret = setupBuffers();
1456 if (ret) {
1457 return ret;
1458 }
1459 }
1460
1461 // Sanity check.
1462 if (mOutStreamMaxBuffers < 1) {
1463 HAL_LOGE("HAL failed to determine max number of buffers.");
1464 return -ENODEV;
1465 }
1466 *max_buffers = mOutStreamMaxBuffers;
1467
1468 return 0;
1469}
1470
1471int V4L2Camera::setFormat(const default_camera_hal::Stream* stream) {
1472 HAL_LOG_ENTER();
1473
1474 // Should be checked earlier; sanity check.
1475 if (stream->isInputType()) {
1476 HAL_LOGE("Input streams not supported.");
1477 return -EINVAL;
1478 }
1479
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001480 // TODO(b/30000211): Check if appropriate to do multi-planar capture instead.
1481 uint32_t desired_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1482 uint32_t desired_format = halToV4L2PixelFormat(stream->getFormat());
1483 uint32_t desired_width = stream->getWidth();
1484 uint32_t desired_height = stream->getHeight();
1485
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001486 // Check to make sure we're not already in the correct format.
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001487 if (desired_type == mOutStreamType &&
1488 desired_format == mOutStreamFormat &&
1489 desired_width == mOutStreamWidth &&
1490 desired_height == mOutStreamHeight) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001491 return 0;
1492 }
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001493
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001494 // Not in the correct format, set our format.
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001495 v4l2_format format;
1496 memset(&format, 0, sizeof(format));
1497 format.type = desired_type;
1498 format.fmt.pix.width = desired_width;
1499 format.fmt.pix.height = desired_height;
1500 format.fmt.pix.pixelformat = desired_format;
1501 // TODO(b/29334616): When async, this will need to check if the stream
1502 // is on, and if so, lock it off while setting format.
1503 if (ioctlLocked(VIDIOC_S_FMT, &format) < 0) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001504 HAL_LOGE("S_FMT failed: %s", strerror(errno));
1505 return -ENODEV;
1506 }
1507 // Check that the driver actually set to the requested values.
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001508 if (format.type != desired_type ||
1509 format.fmt.pix.pixelformat != desired_format ||
1510 format.fmt.pix.width != desired_width ||
1511 format.fmt.pix.height != desired_height) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001512 HAL_LOGE("Device doesn't support desired stream configuration.");
1513 return -EINVAL;
1514 }
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -07001515
1516 // Keep track of the new format.
1517 mOutStreamType = format.type;
1518 mOutStreamFormat = format.fmt.pix.pixelformat;
1519 mOutStreamWidth = format.fmt.pix.width;
1520 mOutStreamHeight = format.fmt.pix.height;
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -07001521 mOutStreamBytesPerLine = format.fmt.pix.bytesperline;
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -07001522 HAL_LOGV("New format: type %u, pixel format %u, width %u, height %u, "
1523 "bytes/line %u", mOutStreamType, mOutStreamFormat, mOutStreamWidth,
1524 mOutStreamHeight, mOutStreamBytesPerLine);
1525
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001526 // Since our format changed, our maxBuffers may be incorrect.
1527 mOutStreamMaxBuffers = 0;
1528
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -07001529
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001530 return 0;
1531}
1532
1533int V4L2Camera::setupBuffers() {
1534 HAL_LOG_ENTER();
1535
1536 // "Request" a buffer (since we're using a userspace buffer, this just
1537 // tells V4L2 to switch into userspace buffer mode).
1538 v4l2_requestbuffers req_buffers;
1539 memset(&req_buffers, 0, sizeof(req_buffers));
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -07001540 req_buffers.type = mOutStreamType;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001541 req_buffers.memory = V4L2_MEMORY_USERPTR;
1542 req_buffers.count = 1;
1543 if (ioctlLocked(VIDIOC_REQBUFS, &req_buffers) < 0) {
1544 HAL_LOGE("REQBUFS failed: %s", strerror(errno));
1545 return -ENODEV;
1546 }
1547
1548 // V4L2 will set req_buffers.count to a number of buffers it can handle.
1549 mOutStreamMaxBuffers = req_buffers.count;
1550 return 0;
1551}
1552
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001553bool V4L2Camera::isValidCaptureSettings(const camera_metadata_t* settings) {
1554 HAL_LOG_ENTER();
1555
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001556 // TODO(b/29335262): reject capture settings this camera isn't capable of.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001557 return true;
1558}
1559
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001560int V4L2Camera::initCharacteristics() {
1561 HAL_LOG_ENTER();
1562
1563 /* Physical characteristics. */
1564 // No way to get these in V4L2, so faked.
1565 // Note: While many of these are primarily informative for post-processing
1566 // calculations by the app and will potentially cause bad results there,
1567 // focal length and physical size are actually used in framework
1568 // calculations (field of view, pixel pitch, etc), so faking them may
1569 // have unexpected results.
1570 mAperture = 2.0; // RPi camera v2 is f/2.0.
1571 mFilterDensity = 0.0;
1572 mFocalLength = 3.04; // RPi camera v2 is 3.04mm.
1573 mOrientation = 0;
1574 mPhysicalSize = {{3.674, 2.760}}; // RPi camera v2 is 3.674 x 2.760 mm.
1575
1576 /* Fixed features. */
1577
1578 // TODO(b/29394024): query VIDIOC_CROPCAP to get pixel rectangle.
1579 // Spoofing as 640 x 480 for now.
1580 mPixelArraySize = {{/*xmin*/0, /*ymin*/0, /*width*/640, /*height*/480}};
1581
1582 // V4L2 VIDIOC_CROPCAP doesn't give a way to query this;
1583 // it's driver dependent. For now, assume freeform, and
1584 // some cameras may just behave badly.
1585 // TODO(b/29579652): Figure out a way to determine this.
1586 mCropType = ANDROID_SCALER_CROPPING_TYPE_FREEFORM;
1587
1588 // TODO(b/29394024): query VIDIOC_CROPCAP to get cropping ranges,
1589 // and VIDIOC_G_CROP to determine if cropping is supported.
1590 // If the ioctl isn't available (or cropping has non-square pixelaspect),
1591 // assume no cropping/scaling.
1592 // May need to try setting some crops to determine what the driver actually
1593 // supports (including testing center vs freeform).
1594 mMaxZoom = 1;
1595
1596 // TODO(b/29394024): query V4L2_CID_EXPOSURE_BIAS.
1597 mAeCompensationRange = {{0, 0}};
1598 mAeCompensationStep = {1, 1};
1599
1600 // TODO(b/29394024): query V4L2_CID_3A_LOCK.
1601 mAeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE;
1602 mAwbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE;
1603
1604 // TODO(b/29394024): query V4L2_CID_FLASH_LED_MODE.
1605 mFlashAvailable = 0;
1606
1607 // TODO(b/29394024): query V4L2_CID_FOCUS_ABSOLUTE for focus range.
1608 mFocusDistance = 0; // Fixed focus.
1609
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001610 // TODO(b/29939583): V4L2 can only support 1 stream at a time.
1611 // For now, just reporting minimum allowable for LIMITED devices.
1612 mMaxRawOutputStreams = 0;
1613 mMaxYuvOutputStreams = 2;
1614 mMaxJpegOutputStreams = 1;
1615 // Reprocessing not supported.
1616 mMaxInputStreams = 0;
1617
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001618 /* Features with (potentially) multiple options. */
1619
1620 // TODO(b/29394024): query V4L2_CID_EXPOSURE_AUTO for ae modes.
1621 mAeModes.push_back(ANDROID_CONTROL_AE_MODE_ON);
1622
1623 // TODO(b/29394024): query V4L2_CID_POWER_LINE_FREQUENCY.
1624 // Auto as the default, since it could mean anything, while OFF would
1625 // require guaranteeing no antibanding happens.
1626 mAeAntibandingModes.push_back(ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO);
1627
1628 // TODO(b/29394024): query V4L2_CID_FOCUS_AUTO for
1629 // CONTINUOUS_VIDEO/CONTINUOUS_PICTURE. V4L2_CID_AUTO_FOCUS_START
1630 // supports what Android thinks of as auto focus (single auto focus).
1631 // V4L2_CID_AUTO_FOCUS_RANGE allows MACRO.
1632 mAfModes.push_back(ANDROID_CONTROL_AF_MODE_OFF);
1633
1634 // TODO(b/29394024): query V4L2_CID_AUTO_WHITE_BALANCE, or
1635 // V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE if available.
1636 mAwbModes.push_back(ANDROID_CONTROL_AWB_MODE_AUTO);
1637
1638 // TODO(b/29394024): query V4L2_CID_SCENE_MODE.
1639 mSceneModes.push_back(ANDROID_CONTROL_SCENE_MODE_DISABLED);
1640
1641 mControlModes.push_back(ANDROID_CONTROL_MODE_AUTO);
1642 if (mSceneModes.size() > 1) {
1643 // We have some mode other than just DISABLED available.
1644 mControlModes.push_back(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
1645 }
1646
1647 // TODO(b/29394024): query V4L2_CID_COLORFX.
1648 mEffects.push_back(ANDROID_CONTROL_EFFECT_MODE_OFF);
1649
1650 // TODO(b/29394024): query V4L2_CID_FLASH_INDICATOR_INTENSITY.
1651 // For now, no indicator LED available; nothing to push back.
1652 // When there is, push back ANDROID_LED_AVAILABLE_LEDS_TRANSMIT.
1653
1654 // TODO(b/29394024): query V4L2_CID_IMAGE_STABILIZATION.
1655 mOpticalStabilizationModes.push_back(
1656 ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF);
1657 mVideoStabilizationModes.push_back(
1658 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF);
1659
1660 // Need to support YUV_420_888 and JPEG.
1661 // TODO(b/29394024): query VIDIOC_ENUM_FMT.
1662 std::vector<uint32_t> formats = {{V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420}};
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001663 // We want to find the smallest maximum frame duration amongst formats.
1664 mMaxFrameDuration = std::numeric_limits<int64_t>::max();
1665 int64_t min_yuv_frame_duration = std::numeric_limits<int64_t>::max();
1666 for (auto format : formats) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001667 int32_t hal_format = V4L2ToHalPixelFormat(format);
1668 if (hal_format < 0) {
1669 // Unrecognized/unused format. Skip it.
1670 continue;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001671 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001672 // TODO(b/29394024): query VIDIOC_ENUM_FRAMESIZES.
1673 // For now, just 640x480.
1674 ArrayVector<int32_t, 2> frame_sizes;
1675 frame_sizes.push_back({{640, 480}});
1676 size_t num_frame_sizes = frame_sizes.num_arrays();
1677 for (size_t i = 0; i < num_frame_sizes; ++i) {
1678 const int32_t* frame_size = frame_sizes[i];
1679 mStreamConfigs.push_back({{hal_format, frame_size[0], frame_size[1],
1680 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT}});
1681
1682 // TODO(b/29394024): query VIDIOC_ENUM_FRAMEINTERVALS.
1683 // For now, using the emulator max value of .3 sec.
1684 int64_t max_frame_duration = 300000000;
1685 // For now, min value of 30 fps = 1/30 spf ~= 33333333 ns.
1686 // For whatever reason the goldfish/qcom cameras report this as
1687 // 33331760, so copying that.
1688 int64_t min_frame_duration = 33331760;
1689
1690 mMinFrameDurations.push_back(
1691 {{hal_format, frame_size[0], frame_size[1], min_frame_duration}});
1692
1693 // In theory max frame duration (min frame rate) should be consistent
1694 // between all formats, but we check and only advertise the smallest
1695 // available max duration just in case.
1696 if (max_frame_duration < mMaxFrameDuration) {
1697 mMaxFrameDuration = max_frame_duration;
1698 }
1699
1700 // We only care about min frame duration (max frame rate) for YUV.
1701 if (hal_format == HAL_PIXEL_FORMAT_YCbCr_420_888 &&
1702 min_frame_duration < min_yuv_frame_duration) {
1703 min_yuv_frame_duration = min_frame_duration;
1704 }
1705
1706 // Usually 0 for non-jpeg, non-zero for JPEG.
1707 // Randomly choosing absurd 1 sec for JPEG. Unsure what this breaks.
1708 int64_t stall_duration = 0;
1709 if (hal_format == HAL_PIXEL_FORMAT_BLOB) {
1710 stall_duration = 1000000000;
1711 }
1712 mStallDurations.push_back(
1713 {{hal_format, frame_size[0], frame_size[1], stall_duration}});
1714 }
1715 }
1716
1717 // This should be at minimum {mi, ma}, {ma, ma} where mi and ma
1718 // are min and max frame rates for YUV_420_888. Min should be at most 15.
1719 // Convert from frame durations measured in ns.
1720 int32_t min_yuv_fps = 1000000000 / mMaxFrameDuration;
1721 if (min_yuv_fps > 15) {
1722 return -ENODEV;
1723 }
1724 int32_t max_yuv_fps = 1000000000 / min_yuv_frame_duration;
1725 mFpsRanges.push_back({{min_yuv_fps, max_yuv_fps}});
1726 mFpsRanges.push_back({{max_yuv_fps, max_yuv_fps}});
1727 // Always advertise {30, 30} if max is even higher,
1728 // since this is what the default video requests use.
1729 if (max_yuv_fps > 30) {
1730 mFpsRanges.push_back({{30, 30}});
1731 }
1732
1733 mCharacteristicsInitialized = true;
1734 return 0;
1735}
1736
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001737int V4L2Camera::V4L2ToHalPixelFormat(uint32_t v4l2_format) {
1738 // Translate V4L2 format to HAL format.
1739 int hal_format = -1;
1740 switch (v4l2_format) {
1741 case V4L2_PIX_FMT_JPEG:
1742 hal_format = HAL_PIXEL_FORMAT_BLOB;
1743 break;
1744 case V4L2_PIX_FMT_YUV420:
1745 hal_format = HAL_PIXEL_FORMAT_YCbCr_420_888;
1746 break;
1747 default:
1748 // Unrecognized format.
1749 break;
1750 }
1751 return hal_format;
1752}
1753
1754uint32_t V4L2Camera::halToV4L2PixelFormat(int hal_format) {
1755 // Translate HAL format to V4L2 format.
1756 uint32_t v4l2_format = 0;
1757 switch (hal_format) {
1758 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: // fall-through.
1759 case HAL_PIXEL_FORMAT_YCbCr_420_888:
1760 v4l2_format = V4L2_PIX_FMT_YUV420;
1761 break;
1762 case HAL_PIXEL_FORMAT_BLOB:
1763 v4l2_format = V4L2_PIX_FMT_JPEG;
1764 break;
1765 default:
1766 // Unrecognized format.
1767 break;
1768 }
1769 return v4l2_format;
1770}
1771
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001772} // namespace v4l2_camera_hal