blob: cb310a7b90c870105abea3bb31af87628c8d2052 [file] [log] [blame]
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "V4L2Camera.h"
18
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070019#include <fcntl.h>
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070020#include <linux/videodev2.h>
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070021#include <sys/types.h>
22#include <sys/stat.h>
23
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070024#include <cstdlib>
25
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070026#include <camera/CameraMetadata.h>
27#include <hardware/camera3.h>
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070028#include <nativehelper/ScopedFd.h>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070029
30#include "Common.h"
31
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070032#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
33
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070034namespace v4l2_camera_hal {
35
36V4L2Camera::V4L2Camera(int id, std::string path)
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070037 : default_camera_hal::Camera(id),
38 mDevicePath(std::move(path)),
39 mTemplatesInitialized(false),
40 mCharacteristicsInitialized(false) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070041 HAL_LOG_ENTER();
42}
43
44V4L2Camera::~V4L2Camera() {
45 HAL_LOG_ENTER();
46}
47
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070048int V4L2Camera::connect() {
49 HAL_LOG_ENTER();
50
51 if (mDeviceFd.get() >= 0) {
52 HAL_LOGE("Camera device %s is opened. Close it first", mDevicePath.c_str());
53 return -EIO;
54 }
55
56 int fd = TEMP_FAILURE_RETRY(open(mDevicePath.c_str(), O_RDWR));
57 if (fd < 0) {
58 HAL_LOGE("failed to open %s (%s)", mDevicePath.c_str(), strerror(errno));
59 return -errno;
60 }
61 mDeviceFd.reset(fd);
62
63 // TODO(b/29185945): confirm this is a supported device.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070064 // This is checked by the HAL, but the device at mDevicePath may
65 // not be the same one that was there when the HAL was loaded.
66 // (Alternatively, better hotplugging support may make this unecessary
67 // by disabling cameras that get disconnected and checking newly connected
68 // cameras, so connect() is never called on an unsupported camera)
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070069
70 // TODO(b/29158098): Inform service of any flashes that are no longer available
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070071 // because this camera is in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070072 return 0;
73}
74
75void V4L2Camera::disconnect() {
76 HAL_LOG_ENTER();
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070077 // TODO(b/29158098): Inform service of any flashes that are available again
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070078 // because this camera is no longer in use.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070079
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070080 mDeviceFd.reset();
81}
82
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070083int V4L2Camera::initStaticInfo(camera_metadata_t** out) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070084 HAL_LOG_ENTER();
85
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070086 android::status_t res;
87 // Device characteristics need to be queried prior
88 // to static info setup.
89 if (!mCharacteristicsInitialized) {
90 res = initCharacteristics();
91 if (res) {
92 return res;
93 }
94 }
95
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070096 android::CameraMetadata info;
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -070097
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070098 std::vector<int32_t> avail_characteristics_keys;
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070099
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700100#define ADD_STATIC_ENTRY(name, varptr, count) \
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700101 avail_characteristics_keys.push_back(name); \
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700102 res = info.update(name, varptr, count); \
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700103 if (res != android::OK) return res
104
105 // Static metadata characteristics from /system/media/camera/docs/docs.html.
106
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700107 /* android.colorCorrection. */
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700108
109 // No easy way to turn chromatic aberration correction OFF in v4l2,
110 // though this may be supportable via a collection of other user controls.
111 uint8_t avail_aberration_modes[] = {
112 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST,
113 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY};
114 ADD_STATIC_ENTRY(ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
115 avail_aberration_modes, ARRAY_SIZE(avail_aberration_modes));
116
117 /* android.control. */
118
119 /* 3As */
120
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700121 ADD_STATIC_ENTRY(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700122 mAeAntibandingModes.data(), mAeAntibandingModes.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700123
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700124 ADD_STATIC_ENTRY(ANDROID_CONTROL_AE_AVAILABLE_MODES,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700125 mAeModes.data(), mAeModes.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700126
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700127 // Flatten mFpsRanges.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700128 ADD_STATIC_ENTRY(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700129 mFpsRanges.data(), mFpsRanges.total_num_elements());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700130
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700131 ADD_STATIC_ENTRY(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700132 mAeCompensationRange.data(), mAeCompensationRange.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700133
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700134 ADD_STATIC_ENTRY(ANDROID_CONTROL_AE_COMPENSATION_STEP,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700135 &mAeCompensationStep, 1);
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700136
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700137 ADD_STATIC_ENTRY(ANDROID_CONTROL_AF_AVAILABLE_MODES,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700138 mAfModes.data(), mAfModes.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700139
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700140 ADD_STATIC_ENTRY(ANDROID_CONTROL_AWB_AVAILABLE_MODES,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700141 mAwbModes.data(), mAwbModes.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700142
143 // Couldn't find any V4L2 support for regions, though maybe it's out there.
144 int32_t max_regions[] = {/*AE*/ 0,/*AWB*/ 0,/*AF*/ 0};
145 ADD_STATIC_ENTRY(ANDROID_CONTROL_MAX_REGIONS,
146 max_regions, ARRAY_SIZE(max_regions));
147
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700148 ADD_STATIC_ENTRY(ANDROID_CONTROL_AE_LOCK_AVAILABLE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700149 &mAeLockAvailable, 1);
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700150 ADD_STATIC_ENTRY(ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700151 &mAwbLockAvailable, 1);
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700152
153 /* Scene modes. */
154
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700155 ADD_STATIC_ENTRY(ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700156 mSceneModes.data(), mSceneModes.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700157
158 // A 3-tuple of AE, AWB, AF overrides for each scene mode.
159 // Ignored for DISABLED, FACE_PRIORITY and FACE_PRIORITY_LOW_LIGHT.
160 uint8_t scene_mode_overrides[] = {
161 /*SCENE_MODE_DISABLED*/ /*AE*/0, /*AW*/0, /*AF*/0};
162 ADD_STATIC_ENTRY(ANDROID_CONTROL_SCENE_MODE_OVERRIDES,
163 scene_mode_overrides, ARRAY_SIZE(scene_mode_overrides));
164
165 /* Top level 3A/Scenes switch. */
166
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700167 ADD_STATIC_ENTRY(ANDROID_CONTROL_AVAILABLE_MODES,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700168 mControlModes.data(), mControlModes.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700169
170 /* Other android.control configuration. */
171
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700172 ADD_STATIC_ENTRY(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700173 mVideoStabilizationModes.data(),
174 mVideoStabilizationModes.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700175
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700176 ADD_STATIC_ENTRY(ANDROID_CONTROL_AVAILABLE_EFFECTS,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700177 mEffects.data(), mEffects.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700178
179 // AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS only necessary
180 // for devices supporting CONSTRAINED_HIGH_SPEED_VIDEO,
181 // which this HAL doesn't support.
182
183 // POST_RAW_SENSITIVITY_BOOST_RANGE only necessary
184 // for devices supporting RAW format outputs.
185
186 /* android.edge. */
187
188 // Not sure if V4L2 does or doesn't do this, but HAL documentation says
189 // all devices must support FAST, and FAST can be equivalent to OFF, so
190 // either way it's fine to list.
191 uint8_t avail_edge_modes[] = {
192 ANDROID_EDGE_MODE_FAST};
193 ADD_STATIC_ENTRY(ANDROID_EDGE_AVAILABLE_EDGE_MODES,
194 avail_edge_modes, ARRAY_SIZE(avail_edge_modes));
195
196 /* android.flash. */
197
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700198 ADD_STATIC_ENTRY(ANDROID_FLASH_INFO_AVAILABLE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700199 &mFlashAvailable, 1);
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700200
201 // info.chargeDuration, color.Temperature, maxEnergy marked FUTURE.
202
203 /* android.hotPixel. */
204
205 // No known V4L2 hot pixel correction. But it might be happening,
206 // so we report FAST/HIGH_QUALITY.
207 uint8_t avail_hot_pixel_modes[] = {
208 ANDROID_HOT_PIXEL_MODE_FAST,
209 ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY};
210 ADD_STATIC_ENTRY(ANDROID_HOT_PIXEL_AVAILABLE_HOT_PIXEL_MODES,
211 avail_hot_pixel_modes, ARRAY_SIZE(avail_hot_pixel_modes));
212
213 /* android.jpeg. */
214
215 // For now, no thumbnails available (only [0,0], the "no thumbnail" size).
216 // TODO(b/29580107): Could end up with a mismatch between request & result,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700217 // since V4L2 doesn't actually allow for thumbnail size control.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700218 int32_t thumbnail_sizes[] = {
219 0, 0};
220 ADD_STATIC_ENTRY(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
221 thumbnail_sizes, ARRAY_SIZE(thumbnail_sizes));
222
223 // V4L2 doesn't support querying this, so we generously assume up to 3 MB.
224 int32_t max_jpeg_size = 3000000;
225 ADD_STATIC_ENTRY(ANDROID_JPEG_MAX_SIZE,
226 &max_jpeg_size, 1);
227
228 /* android.lens. */
229
230 /* Misc. lens control. */
231
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700232 ADD_STATIC_ENTRY(ANDROID_LENS_INFO_AVAILABLE_APERTURES,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700233 &mAperture, 1);
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700234
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700235 ADD_STATIC_ENTRY(ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700236 &mFilterDensity, 1);
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700237
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700238 ADD_STATIC_ENTRY(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700239 mOpticalStabilizationModes.data(),
240 mOpticalStabilizationModes.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700241
242 // No known V4L2 shading map info.
243 int32_t shading_map_size[] = {1, 1};
244 ADD_STATIC_ENTRY(ANDROID_LENS_INFO_SHADING_MAP_SIZE,
245 shading_map_size, ARRAY_SIZE(shading_map_size));
246
247 // All V4L2 devices are considered to be external facing.
248 uint8_t facing = ANDROID_LENS_FACING_EXTERNAL;
249 ADD_STATIC_ENTRY(ANDROID_LENS_FACING, &facing, 1);
250
251 /* Zoom/Focus. */
252
253 // No way to actually get the focal length in V4L2, but it's a required key,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700254 // so we just fake it.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700255 ADD_STATIC_ENTRY(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700256 &mFocalLength, 1);
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700257
258 // V4L2 focal units do not correspond to a particular physical unit.
259 uint8_t focus_calibration =
260 ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED;
261 ADD_STATIC_ENTRY(ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
262 &focus_calibration, 1);
263
264 // info.hyperfocalDistance not required for UNCALIBRATED.
265
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700266 ADD_STATIC_ENTRY(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700267 &mFocusDistance, 1);
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700268
269 /* Depth. */
270
271 // DEPTH capability not supported by this HAL. Not implemented:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700272 // poseRotation
273 // poseTranslation
274 // intrinsicCalibration
275 // radialDistortion
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700276
277 /* anroid.noise. */
278
279 // Unable to control noise reduction in V4L2 devices,
280 // but FAST is allowed to be the same as OFF.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700281 uint8_t avail_noise_reduction_modes[] = {ANDROID_NOISE_REDUCTION_MODE_FAST};
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700282 ADD_STATIC_ENTRY(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
283 avail_noise_reduction_modes,
284 ARRAY_SIZE(avail_noise_reduction_modes));
285
286 /* android.request. */
287
288 // Resources may be an issue, so just using minimum allowable
289 // for LIMITED devices.
290 int32_t max_num_output_streams[] = {
291 /*Raw*/0, /*YUV*/2, /*JPEG*/1};
292 ADD_STATIC_ENTRY(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
293 max_num_output_streams, ARRAY_SIZE(max_num_output_streams));
294
295 // Reprocessing not supported, so no maxNumInputStreams.
296
297 // No way to know for V4L2, so fake with max allowable latency.
298 // Doesn't mean much without per-frame controls.
299 uint8_t pipeline_max_depth = 4;
300 ADD_STATIC_ENTRY(ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
301 &pipeline_max_depth, 1);
302
303 // Partial results not supported; partialResultCount defaults to 1.
304
305 // Available capabilities & keys queried at very end of this method.
306
307 /* android.scaler. */
308
309 /* Cropping. */
310
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700311 ADD_STATIC_ENTRY(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700312 &mMaxZoom, 1);
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700313
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700314 ADD_STATIC_ENTRY(ANDROID_SCALER_CROPPING_TYPE, &mCropType, 1);
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700315
316 /* Streams. */
317
318 // availableInputOutputFormatsMap only required for reprocessing capability.
319
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700320 // Flatten mStreamConfigs.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700321 ADD_STATIC_ENTRY(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700322 mStreamConfigs.data(),
323 mStreamConfigs.total_num_elements());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700324
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700325 // Flatten mMinFrameDurations.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700326 ADD_STATIC_ENTRY(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700327 mMinFrameDurations.data(),
328 mMinFrameDurations.total_num_elements());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700329
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700330 // Flatten mStallDurations.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700331 ADD_STATIC_ENTRY(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700332 mStallDurations.data(),
333 mStallDurations.total_num_elements());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700334
335 /* android.sensor. */
336
337 /* Sizes. */
338
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700339 ADD_STATIC_ENTRY(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700340 mPixelArraySize.data(), mPixelArraySize.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700341 // No V4L2 way to differentiate active vs. inactive parts of the rectangle.
342 ADD_STATIC_ENTRY(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700343 mPixelArraySize.data(), mPixelArraySize.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700344
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700345 ADD_STATIC_ENTRY(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700346 mPhysicalSize.data(), mPhysicalSize.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700347
348 /* Misc sensor information. */
349
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700350 ADD_STATIC_ENTRY(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700351 &mMaxFrameDuration, 1);
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700352
353 // HAL uses BOOTTIME timestamps.
354 // TODO(b/29457051): make sure timestamps are consistent throughout the HAL.
355 uint8_t timestamp_source = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN;
356 ADD_STATIC_ENTRY(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
357 &timestamp_source, 1);
358
359 // As in initDeviceInfo, no way to actually get orientation.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700360 ADD_STATIC_ENTRY(ANDROID_SENSOR_ORIENTATION, &mOrientation, 1);
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700361
362 // availableTestPatternModes just defaults to OFF, which is fine.
363
364 // info.exposureTimeRange, info.sensitivityRange:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700365 // exposure/sensitivity manual control not supported.
366 // Could query V4L2_CID_ISO_SENSITIVITY to support sensitivity if desired.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700367
368 // info.whiteLevel, info.lensShadingApplied,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700369 // info.preCorrectionPixelArraySize, referenceIlluminant1/2,
370 // calibrationTransform1/2, colorTransform1/2, forwardMatrix1/2,
371 // blackLevelPattern, profileHueSatMapDimensions
372 // all only necessary for RAW.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700373
374 // baseGainFactor marked FUTURE.
375
376 // maxAnalogSensitivity optional for LIMITED device.
377
378 // opticalBlackRegions: No known way to get in V4L2, but not necessary.
379
380 // opaqueRawSize not necessary since RAW_OPAQUE format not supported.
381
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700382 /* android.shading */
383
384 // No known V4L2 lens shading. But it might be happening,
385 // so we report FAST/HIGH_QUALITY.
386 uint8_t avail_shading_modes[] = {
387 ANDROID_SHADING_MODE_FAST,
388 ANDROID_SHADING_MODE_HIGH_QUALITY};
389 ADD_STATIC_ENTRY(ANDROID_SHADING_AVAILABLE_MODES,
390 avail_shading_modes, ARRAY_SIZE(avail_shading_modes));
391
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700392 /* android.statistics */
393
394 // Face detection not supported.
395 uint8_t avail_face_detect_modes[] = {
396 ANDROID_STATISTICS_FACE_DETECT_MODE_OFF};
397 ADD_STATIC_ENTRY(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
398 avail_face_detect_modes,
399 ARRAY_SIZE(avail_face_detect_modes));
400
401 int32_t max_face_count = 0;
402 ADD_STATIC_ENTRY(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
403 &max_face_count, 1);
404
405 // info.histogramBucketCount, info.maxHistogramCount,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700406 // info.maxSharpnessMapValue, info.sharpnessMapSizemarked FUTURE.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700407
408 // ON only needs to be supported for RAW capable devices.
409 uint8_t avail_hot_pixel_map_modes[] = {
410 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF};
411 ADD_STATIC_ENTRY(ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
412 avail_hot_pixel_map_modes,
413 ARRAY_SIZE(avail_hot_pixel_map_modes));
414
415 // ON only needs to be supported for RAW capable devices.
416 uint8_t avail_lens_shading_map_modes[] = {
417 ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF};
418 ADD_STATIC_ENTRY(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
419 avail_lens_shading_map_modes,
420 ARRAY_SIZE(avail_lens_shading_map_modes));
421
422 /* android.tonemap. */
423
424 // tonemapping only required for MANUAL_POST_PROCESSING capability.
425
426 /* android.led. */
427
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700428 // May or may not have LEDs available.
429 if (!mLeds.empty()) {
430 ADD_STATIC_ENTRY(ANDROID_LED_AVAILABLE_LEDS, mLeds.data(), mLeds.size());
431 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700432
433 /* android.sync. */
434
435 // "LIMITED devices are strongly encouraged to use a non-negative value.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700436 // If UNKNOWN is used here then app developers do not have a way to know
437 // when sensor settings have been applied." - Unfortunately, V4L2 doesn't
438 // really help here either. Could even be that adjusting settings mid-stream
439 // blocks in V4L2, and should be avoided.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700440 int32_t max_latency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN;
441 ADD_STATIC_ENTRY(ANDROID_SYNC_MAX_LATENCY,
442 &max_latency, 1);
443
444 /* android.reprocess. */
445
446 // REPROCESSING not supported by this HAL.
447
448 /* android.depth. */
449
450 // DEPTH not supported by this HAL.
451
452 /* Capabilities and android.info. */
453
454 uint8_t hw_level = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED;
455 ADD_STATIC_ENTRY(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
456 &hw_level, 1);
457
458 uint8_t capabilities[] = {
459 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE};
460 ADD_STATIC_ENTRY(ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
461 capabilities, ARRAY_SIZE(capabilities));
462
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700463 // Scan a default request template for included request keys.
464 if (!mTemplatesInitialized) {
465 res = initTemplates();
466 if (res) {
467 return res;
468 }
469 }
470 std::vector<int32_t> avail_request_keys;
471 const camera_metadata_t *preview_request = nullptr;
472 // Search templates from the beginning for a supported one.
473 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
474 ++template_id) {
475 preview_request = constructDefaultRequestSettings(template_id);
476 if (preview_request != nullptr) {
477 break;
478 }
479 }
480 if (preview_request == nullptr) {
481 HAL_LOGE("No valid templates, can't get request keys.");
482 return -ENODEV;
483 }
484 size_t num_entries = get_camera_metadata_entry_count(preview_request);
485 for (size_t i = 0; i < num_entries; ++i) {
486 camera_metadata_ro_entry_t entry;
487 get_camera_metadata_ro_entry(preview_request, i, &entry);
488 avail_request_keys.push_back(entry.tag);
489 }
490 ADD_STATIC_ENTRY(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
491 avail_request_keys.data(), avail_request_keys.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700492
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700493 // Result keys will be duplicated from the request, plus a few extras.
494 // TODO(b/29335262): additonal available result keys.
495 std::vector<int32_t> avail_result_keys(avail_request_keys);
496 ADD_STATIC_ENTRY(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
497 avail_result_keys.data(), avail_result_keys.size());
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700498
499 // Last thing, once all the available characteristics have been added.
500 info.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700501 avail_characteristics_keys.data(),
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700502 avail_characteristics_keys.size());
503
504 *out = info.release();
505 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700506}
507
508void V4L2Camera::initDeviceInfo(camera_info_t* info) {
509 HAL_LOG_ENTER();
510
511 // For now, just constants.
512 info->facing = CAMERA_FACING_EXTERNAL;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700513 info->orientation = mOrientation;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700514 info->resource_cost = 100;
515 info->conflicting_devices = nullptr;
516 info->conflicting_devices_length = 0;
517}
518
519int V4L2Camera::initDevice() {
520 HAL_LOG_ENTER();
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700521 int res;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700522
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700523 // Templates should be set up if they haven't already been.
524 if (!mTemplatesInitialized) {
525 res = initTemplates();
526 if (res) {
527 return res;
528 }
529 }
530
531 return 0;
532}
533
534int V4L2Camera::initTemplates() {
535 HAL_LOG_ENTER();
536 int res;
537
538 // Device characteristics need to be queried prior
539 // to template setup.
540 if (!mCharacteristicsInitialized) {
541 res = initCharacteristics();
542 if (res) {
543 return res;
544 }
545 }
546
547 // Note: static metadata expects all templates/requests
548 // to provide values for all supported keys.
549
550 android::CameraMetadata template_metadata;
551#define ADD_REQUEST_ENTRY(name, varptr, count) \
552 res = template_metadata.update(name, varptr, count); \
553 if (res != android::OK) return res
554
555 // Start with defaults for all templates.
556
557 /* android.colorCorrection. */
558
559 uint8_t aberration_mode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST;
560 ADD_REQUEST_ENTRY(ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
561 &aberration_mode, 1);
562
563 uint8_t color_correction_mode = ANDROID_COLOR_CORRECTION_MODE_FAST;
564 ADD_REQUEST_ENTRY(ANDROID_COLOR_CORRECTION_MODE,
565 &color_correction_mode, 1);
566
567 // transform and gains are for the unsupported MANUAL_POST_PROCESSING only.
568
569 /* android.control. */
570
571 /* AE. */
572 uint8_t ae_antibanding_mode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
573 ADD_REQUEST_ENTRY(ANDROID_CONTROL_AE_ANTIBANDING_MODE,
574 &ae_antibanding_mode, 1);
575
576 // Only matters if AE_MODE = OFF
577 int32_t ae_exposure_compensation = 0;
578 ADD_REQUEST_ENTRY(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
579 &ae_exposure_compensation, 1);
580
581 uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF;
582 ADD_REQUEST_ENTRY(ANDROID_CONTROL_AE_LOCK, &ae_lock, 1);
583
584 uint8_t ae_mode = ANDROID_CONTROL_AE_MODE_ON;
585 ADD_REQUEST_ENTRY(ANDROID_CONTROL_AE_MODE, &ae_mode, 1);
586
587 // AE regions not supported.
588
589 // FPS set per-template.
590
591 uint8_t ae_precapture_trigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
592 ADD_REQUEST_ENTRY(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
593 &ae_precapture_trigger, 1);
594
595 /* AF. */
596
597 // AF mode set per-template.
598
599 // AF regions not supported.
600
601 uint8_t af_trigger = ANDROID_CONTROL_AF_TRIGGER_IDLE;
602 ADD_REQUEST_ENTRY(ANDROID_CONTROL_AF_TRIGGER, &af_trigger, 1);
603
604 /* AWB. */
605
606 // Priority: auto > off > Whatever is available.
607 uint8_t default_awb_mode = mAwbModes[0];
608 if (std::count(mAwbModes.begin(), mAwbModes.end(),
609 ANDROID_CONTROL_AWB_MODE_AUTO)) {
610 default_awb_mode = ANDROID_CONTROL_AWB_MODE_AUTO;
611 } else if (std::count(mAwbModes.begin(), mAwbModes.end(),
612 ANDROID_CONTROL_AWB_MODE_OFF)) {
613 default_awb_mode = ANDROID_CONTROL_AWB_MODE_OFF;
614 }
615 ADD_REQUEST_ENTRY(ANDROID_CONTROL_AWB_MODE, &default_awb_mode, 1);
616
617 // AWB regions not supported.
618
619 /* Other controls. */
620
621 uint8_t effect_mode = ANDROID_CONTROL_EFFECT_MODE_OFF;
622 ADD_REQUEST_ENTRY(ANDROID_CONTROL_EFFECT_MODE, &effect_mode, 1);
623
624 uint8_t control_mode = ANDROID_CONTROL_MODE_AUTO;
625 ADD_REQUEST_ENTRY(ANDROID_CONTROL_MODE, &control_mode, 1);
626
627 uint8_t scene_mode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
628 ADD_REQUEST_ENTRY(ANDROID_CONTROL_SCENE_MODE,
629 &scene_mode, 1);
630
631 uint8_t video_stabilization = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
632 ADD_REQUEST_ENTRY(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
633 &video_stabilization, 1);
634
635 // postRawSensitivityBoost: RAW not supported, leave null.
636
637 /* android.demosaic. */
638
639 // mode marked FUTURE.
640
641 /* android.edge. */
642
643 uint8_t edge_mode = ANDROID_EDGE_MODE_FAST;
644 ADD_REQUEST_ENTRY(ANDROID_EDGE_MODE, &edge_mode, 1);
645
646 // strength marked FUTURE.
647
648 /* android.flash. */
649
650 // firingPower, firingTime marked FUTURE.
651
652 uint8_t flash_mode = ANDROID_FLASH_MODE_OFF;
653 ADD_REQUEST_ENTRY(ANDROID_FLASH_MODE, &flash_mode, 1);
654
655 /* android.hotPixel. */
656
657 uint8_t hp_mode = ANDROID_HOT_PIXEL_MODE_FAST;
658 ADD_REQUEST_ENTRY(ANDROID_HOT_PIXEL_MODE, &hp_mode, 1);
659
660 /* android.jpeg. */
661
662 double gps_coords[] = {/*latitude*/0, /*longitude*/0, /*altitude*/0};
663 ADD_REQUEST_ENTRY(ANDROID_JPEG_GPS_COORDINATES, gps_coords, 3);
664
665 uint8_t gps_processing_method[] = "none";
666 ADD_REQUEST_ENTRY(ANDROID_JPEG_GPS_PROCESSING_METHOD,
667 gps_processing_method, ARRAY_SIZE(gps_processing_method));
668
669 int64_t gps_timestamp = 0;
670 ADD_REQUEST_ENTRY(ANDROID_JPEG_GPS_TIMESTAMP, &gps_timestamp, 1);
671
672 // JPEG orientation is relative to sensor orientation (mOrientation).
673 int32_t jpeg_orientation = 0;
674 ADD_REQUEST_ENTRY(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1);
675
676 // 1-100, larger is higher quality.
677 uint8_t jpeg_quality = 80;
678 ADD_REQUEST_ENTRY(ANDROID_JPEG_QUALITY, &jpeg_quality, 1);
679
680 // TODO(b/29580107): If thumbnail quality actually matters/can be adjusted,
681 // adjust this.
682 uint8_t thumbnail_quality = 80;
683 ADD_REQUEST_ENTRY(ANDROID_JPEG_THUMBNAIL_QUALITY, &thumbnail_quality, 1);
684
685 // TODO(b/29580107): Choose a size matching the resolution.
686 int32_t thumbnail_size[] = {0, 0};
687 ADD_REQUEST_ENTRY(ANDROID_JPEG_THUMBNAIL_SIZE, thumbnail_size, 2);
688
689 /* android.lens. */
690
691 // Fixed values.
692 ADD_REQUEST_ENTRY(ANDROID_LENS_APERTURE, &mAperture, 1);
693 ADD_REQUEST_ENTRY(ANDROID_LENS_FILTER_DENSITY, &mFilterDensity, 1);
694 ADD_REQUEST_ENTRY(ANDROID_LENS_FOCAL_LENGTH, &mFocalLength, 1);
695 ADD_REQUEST_ENTRY(ANDROID_LENS_FOCUS_DISTANCE, &mFocusDistance, 1);
696
697 uint8_t optical_stabilization = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
698 ADD_REQUEST_ENTRY(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
699 &optical_stabilization, 1);
700
701 /* android.noiseReduction. */
702
703 uint8_t noise_reduction_mode = ANDROID_NOISE_REDUCTION_MODE_FAST;
704 ADD_REQUEST_ENTRY(ANDROID_NOISE_REDUCTION_MODE, &noise_reduction_mode, 1);
705
706 // strength marked FUTURE.
707
708 /* android.request. */
709
710 // Request Id unused by the HAL for now, and these are just
711 // templates, so just fill it in with a dummy.
712 int32_t id = 0;
713 ADD_REQUEST_ENTRY(ANDROID_REQUEST_ID, &id, 1);
714
715 // metadataMode marked FUTURE.
716
717 /* android.scaler. */
718
719 // No cropping by default; use the full active array.
720 ADD_REQUEST_ENTRY(ANDROID_SCALER_CROP_REGION, mPixelArraySize.data(),
721 mPixelArraySize.size());
722
723 /* android.sensor. */
724
725 // exposureTime, sensitivity, testPattern[Data,Mode] not supported.
726
727 // Ignored when AE is OFF.
728 int64_t frame_duration = 33333333L; // 1/30 s.
729 ADD_REQUEST_ENTRY(ANDROID_SENSOR_FRAME_DURATION, &frame_duration, 1);
730
731 /* android.shading. */
732
733 uint8_t shading_mode = ANDROID_SHADING_MODE_FAST;
734 ADD_REQUEST_ENTRY(ANDROID_SHADING_MODE, &shading_mode, 1);
735
736 /* android.statistics. */
737
738 uint8_t face_detect_mode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
739 ADD_REQUEST_ENTRY(ANDROID_STATISTICS_FACE_DETECT_MODE, &face_detect_mode, 1);
740
741 // histogramMode, sharpnessMapMode marked FUTURE.
742
743 uint8_t hp_map_mode = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF;
744 ADD_REQUEST_ENTRY(ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE, &hp_map_mode, 1);
745
746 uint8_t lens_shading_map_mode = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
747 ADD_REQUEST_ENTRY(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
748 &lens_shading_map_mode, 1);
749
750 /* android.tonemap. */
751
752 // Tonemap only required for MANUAL_POST_PROCESSING capability.
753
754 /* android.led. */
755
756 uint8_t transmit = ANDROID_LED_TRANSMIT_ON;
757 ADD_REQUEST_ENTRY(ANDROID_LED_TRANSMIT, &transmit, 1);
758
759 /* android.reprocess */
760
761 // Only needed for REPROCESS capability.
762
763
764 /* Template variable values. */
765
766 // Find the FPS ranges "closest" to a desired range
767 // (minimum abs distance from min to min and max to max).
768 // Find both a fixed rate and a variable rate, for different purposes.
769 std::array<int32_t, 2> desired_flat_fps_range = {{30, 30}};
770 std::array<int32_t, 2> desired_variable_fps_range = {{5, 30}};
771 std::array<int32_t, 2> flat_fps_range;
772 std::array<int32_t, 2> variable_fps_range;
773 int32_t best_flat_distance = std::numeric_limits<int32_t>::max();
774 int32_t best_variable_distance = std::numeric_limits<int32_t>::max();
775 size_t num_fps_ranges = mFpsRanges.num_arrays();
776 for (size_t i = 0; i < num_fps_ranges; ++i) {
777 const int32_t* range = mFpsRanges[i];
778 // Variable fps.
779 int32_t distance = std::abs(range[0] - desired_variable_fps_range[0]) +
780 std::abs(range[1] - desired_variable_fps_range[1]);
781 if (distance < best_variable_distance) {
782 variable_fps_range[0] = range[0];
783 variable_fps_range[1] = range[1];
784 best_variable_distance = distance;
785 }
786 // Flat fps. Only do if range is actually flat.
787 // Note at least one flat range is required,
788 // so something will always be filled in.
789 if (range[0] == range[1]) {
790 distance = std::abs(range[0] - desired_flat_fps_range[0]) +
791 std::abs(range[1] - desired_flat_fps_range[1]);
792 if (distance < best_flat_distance) {
793 flat_fps_range[0] = range[0];
794 flat_fps_range[1] = range[1];
795 best_flat_distance = distance;
796 }
797 }
798 }
799
800 // Priority: continuous > auto > off > whatever is available.
801 bool continuous_still_avail = std::count(
802 mAfModes.begin(), mAfModes.end(),
803 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE);
804 bool continuous_video_avail = std::count(
805 mAfModes.begin(), mAfModes.end(),
806 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO);
807 uint8_t non_continuous_af_mode = mAfModes[0];
808 if (std::count(mAfModes.begin(), mAfModes.end(),
809 ANDROID_CONTROL_AF_MODE_AUTO)) {
810 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_AUTO;
811 } else if (std::count(mAfModes.begin(), mAfModes.end(),
812 ANDROID_CONTROL_AF_MODE_OFF)) {
813 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_OFF;
814 }
815 uint8_t still_af_mode = continuous_still_avail ?
816 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE : non_continuous_af_mode;
817 uint8_t video_af_mode = continuous_video_avail ?
818 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO : non_continuous_af_mode;
819
820 // Copy our base metadata (note: we do it in this direction so we don't have
821 // to redefine our ADD_REQUEST_ENTRY macro).
822 android::CameraMetadata base_metadata(template_metadata);
823
824 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT; ++template_id) {
825 // General differences/support.
826 uint8_t intent;
827 uint8_t af_mode;
828 std::array<int32_t, 2> fps_range;
829 switch(template_id) {
830 case CAMERA3_TEMPLATE_PREVIEW:
831 intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
832 af_mode = still_af_mode;
833 fps_range = flat_fps_range;
834 break;
835 case CAMERA3_TEMPLATE_STILL_CAPTURE:
836 intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
837 af_mode = still_af_mode;
838 fps_range = variable_fps_range;
839 break;
840 case CAMERA3_TEMPLATE_VIDEO_RECORD:
841 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
842 af_mode = video_af_mode;
843 fps_range = flat_fps_range;
844 break;
845 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
846 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
847 af_mode = video_af_mode;
848 fps_range = flat_fps_range;
849 break;
850 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: // Fall through.
851 case CAMERA3_TEMPLATE_MANUAL: // Fall though.
852 default:
853 // Unsupported/unrecognized. Don't add this template; skip it.
854 continue;
855 }
856
857 ADD_REQUEST_ENTRY(ANDROID_CONTROL_CAPTURE_INTENT, &intent, 1);
858 ADD_REQUEST_ENTRY(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
859 fps_range.data(), fps_range.size());
860 ADD_REQUEST_ENTRY(ANDROID_CONTROL_AF_MODE, &af_mode, 1);
861
862 const camera_metadata_t* template_raw_metadata =
863 template_metadata.getAndLock();
864 res = setTemplate(template_id, template_raw_metadata);
865 if (res != android::OK) {
866 return res;
867 }
868 res = template_metadata.unlock(template_raw_metadata);
869 if (res != android::OK) {
870 return res;
871 }
872
873 // Reset the template metadata to the base.
874 template_metadata = base_metadata;
875 }
876
877 mTemplatesInitialized = true;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700878 return 0;
879}
880
881bool V4L2Camera::isValidCaptureSettings(const camera_metadata_t* settings) {
882 HAL_LOG_ENTER();
883
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700884 // TODO(b/29335262): reject capture settings this camera isn't capable of.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700885 return true;
886}
887
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700888int V4L2Camera::initCharacteristics() {
889 HAL_LOG_ENTER();
890
891 /* Physical characteristics. */
892 // No way to get these in V4L2, so faked.
893 // Note: While many of these are primarily informative for post-processing
894 // calculations by the app and will potentially cause bad results there,
895 // focal length and physical size are actually used in framework
896 // calculations (field of view, pixel pitch, etc), so faking them may
897 // have unexpected results.
898 mAperture = 2.0; // RPi camera v2 is f/2.0.
899 mFilterDensity = 0.0;
900 mFocalLength = 3.04; // RPi camera v2 is 3.04mm.
901 mOrientation = 0;
902 mPhysicalSize = {{3.674, 2.760}}; // RPi camera v2 is 3.674 x 2.760 mm.
903
904 /* Fixed features. */
905
906 // TODO(b/29394024): query VIDIOC_CROPCAP to get pixel rectangle.
907 // Spoofing as 640 x 480 for now.
908 mPixelArraySize = {{/*xmin*/0, /*ymin*/0, /*width*/640, /*height*/480}};
909
910 // V4L2 VIDIOC_CROPCAP doesn't give a way to query this;
911 // it's driver dependent. For now, assume freeform, and
912 // some cameras may just behave badly.
913 // TODO(b/29579652): Figure out a way to determine this.
914 mCropType = ANDROID_SCALER_CROPPING_TYPE_FREEFORM;
915
916 // TODO(b/29394024): query VIDIOC_CROPCAP to get cropping ranges,
917 // and VIDIOC_G_CROP to determine if cropping is supported.
918 // If the ioctl isn't available (or cropping has non-square pixelaspect),
919 // assume no cropping/scaling.
920 // May need to try setting some crops to determine what the driver actually
921 // supports (including testing center vs freeform).
922 mMaxZoom = 1;
923
924 // TODO(b/29394024): query V4L2_CID_EXPOSURE_BIAS.
925 mAeCompensationRange = {{0, 0}};
926 mAeCompensationStep = {1, 1};
927
928 // TODO(b/29394024): query V4L2_CID_3A_LOCK.
929 mAeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE;
930 mAwbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE;
931
932 // TODO(b/29394024): query V4L2_CID_FLASH_LED_MODE.
933 mFlashAvailable = 0;
934
935 // TODO(b/29394024): query V4L2_CID_FOCUS_ABSOLUTE for focus range.
936 mFocusDistance = 0; // Fixed focus.
937
938 /* Features with (potentially) multiple options. */
939
940 // TODO(b/29394024): query V4L2_CID_EXPOSURE_AUTO for ae modes.
941 mAeModes.push_back(ANDROID_CONTROL_AE_MODE_ON);
942
943 // TODO(b/29394024): query V4L2_CID_POWER_LINE_FREQUENCY.
944 // Auto as the default, since it could mean anything, while OFF would
945 // require guaranteeing no antibanding happens.
946 mAeAntibandingModes.push_back(ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO);
947
948 // TODO(b/29394024): query V4L2_CID_FOCUS_AUTO for
949 // CONTINUOUS_VIDEO/CONTINUOUS_PICTURE. V4L2_CID_AUTO_FOCUS_START
950 // supports what Android thinks of as auto focus (single auto focus).
951 // V4L2_CID_AUTO_FOCUS_RANGE allows MACRO.
952 mAfModes.push_back(ANDROID_CONTROL_AF_MODE_OFF);
953
954 // TODO(b/29394024): query V4L2_CID_AUTO_WHITE_BALANCE, or
955 // V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE if available.
956 mAwbModes.push_back(ANDROID_CONTROL_AWB_MODE_AUTO);
957
958 // TODO(b/29394024): query V4L2_CID_SCENE_MODE.
959 mSceneModes.push_back(ANDROID_CONTROL_SCENE_MODE_DISABLED);
960
961 mControlModes.push_back(ANDROID_CONTROL_MODE_AUTO);
962 if (mSceneModes.size() > 1) {
963 // We have some mode other than just DISABLED available.
964 mControlModes.push_back(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
965 }
966
967 // TODO(b/29394024): query V4L2_CID_COLORFX.
968 mEffects.push_back(ANDROID_CONTROL_EFFECT_MODE_OFF);
969
970 // TODO(b/29394024): query V4L2_CID_FLASH_INDICATOR_INTENSITY.
971 // For now, no indicator LED available; nothing to push back.
972 // When there is, push back ANDROID_LED_AVAILABLE_LEDS_TRANSMIT.
973
974 // TODO(b/29394024): query V4L2_CID_IMAGE_STABILIZATION.
975 mOpticalStabilizationModes.push_back(
976 ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF);
977 mVideoStabilizationModes.push_back(
978 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF);
979
980 // Need to support YUV_420_888 and JPEG.
981 // TODO(b/29394024): query VIDIOC_ENUM_FMT.
982 std::vector<uint32_t> formats = {{V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420}};
983 int32_t hal_format;
984 // We want to find the smallest maximum frame duration amongst formats.
985 mMaxFrameDuration = std::numeric_limits<int64_t>::max();
986 int64_t min_yuv_frame_duration = std::numeric_limits<int64_t>::max();
987 for (auto format : formats) {
988 // Translate V4L2 format to HAL format.
989 switch (format) {
990 case V4L2_PIX_FMT_JPEG:
991 hal_format = HAL_PIXEL_FORMAT_BLOB;
992 break;
993 case V4L2_PIX_FMT_YUV420:
994 hal_format = HAL_PIXEL_FORMAT_YCbCr_420_888;
995 default:
996 // Unrecognized/unused format. Skip it.
997 continue;
998 }
999
1000 // TODO(b/29394024): query VIDIOC_ENUM_FRAMESIZES.
1001 // For now, just 640x480.
1002 ArrayVector<int32_t, 2> frame_sizes;
1003 frame_sizes.push_back({{640, 480}});
1004 size_t num_frame_sizes = frame_sizes.num_arrays();
1005 for (size_t i = 0; i < num_frame_sizes; ++i) {
1006 const int32_t* frame_size = frame_sizes[i];
1007 mStreamConfigs.push_back({{hal_format, frame_size[0], frame_size[1],
1008 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT}});
1009
1010 // TODO(b/29394024): query VIDIOC_ENUM_FRAMEINTERVALS.
1011 // For now, using the emulator max value of .3 sec.
1012 int64_t max_frame_duration = 300000000;
1013 // For now, min value of 30 fps = 1/30 spf ~= 33333333 ns.
1014 // For whatever reason the goldfish/qcom cameras report this as
1015 // 33331760, so copying that.
1016 int64_t min_frame_duration = 33331760;
1017
1018 mMinFrameDurations.push_back(
1019 {{hal_format, frame_size[0], frame_size[1], min_frame_duration}});
1020
1021 // In theory max frame duration (min frame rate) should be consistent
1022 // between all formats, but we check and only advertise the smallest
1023 // available max duration just in case.
1024 if (max_frame_duration < mMaxFrameDuration) {
1025 mMaxFrameDuration = max_frame_duration;
1026 }
1027
1028 // We only care about min frame duration (max frame rate) for YUV.
1029 if (hal_format == HAL_PIXEL_FORMAT_YCbCr_420_888 &&
1030 min_frame_duration < min_yuv_frame_duration) {
1031 min_yuv_frame_duration = min_frame_duration;
1032 }
1033
1034 // Usually 0 for non-jpeg, non-zero for JPEG.
1035 // Randomly choosing absurd 1 sec for JPEG. Unsure what this breaks.
1036 int64_t stall_duration = 0;
1037 if (hal_format == HAL_PIXEL_FORMAT_BLOB) {
1038 stall_duration = 1000000000;
1039 }
1040 mStallDurations.push_back(
1041 {{hal_format, frame_size[0], frame_size[1], stall_duration}});
1042 }
1043 }
1044
1045 // This should be at minimum {mi, ma}, {ma, ma} where mi and ma
1046 // are min and max frame rates for YUV_420_888. Min should be at most 15.
1047 // Convert from frame durations measured in ns.
1048 int32_t min_yuv_fps = 1000000000 / mMaxFrameDuration;
1049 if (min_yuv_fps > 15) {
1050 return -ENODEV;
1051 }
1052 int32_t max_yuv_fps = 1000000000 / min_yuv_frame_duration;
1053 mFpsRanges.push_back({{min_yuv_fps, max_yuv_fps}});
1054 mFpsRanges.push_back({{max_yuv_fps, max_yuv_fps}});
1055 // Always advertise {30, 30} if max is even higher,
1056 // since this is what the default video requests use.
1057 if (max_yuv_fps > 30) {
1058 mFpsRanges.push_back({{30, 30}});
1059 }
1060
1061 mCharacteristicsInitialized = true;
1062 return 0;
1063}
1064
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001065} // namespace v4l2_camera_hal