Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 1 | /* |
| 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-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 20 | #include <linux/videodev2.h> |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 24 | #include <cstdlib> |
| 25 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 26 | #include <camera/CameraMetadata.h> |
| 27 | #include <hardware/camera3.h> |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 28 | #include <nativehelper/ScopedFd.h> |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 29 | |
| 30 | #include "Common.h" |
| 31 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 32 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) |
| 33 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 34 | namespace v4l2_camera_hal { |
| 35 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 36 | // Helper function for managing metadata. |
| 37 | static std::vector<int32_t> getMetadataKeys( |
| 38 | const camera_metadata_t* metadata) { |
| 39 | std::vector<int32_t> keys; |
| 40 | size_t num_entries = get_camera_metadata_entry_count(metadata); |
| 41 | for (size_t i = 0; i < num_entries; ++i) { |
| 42 | camera_metadata_ro_entry_t entry; |
| 43 | get_camera_metadata_ro_entry(metadata, i, &entry); |
| 44 | keys.push_back(entry.tag); |
| 45 | } |
| 46 | return keys; |
| 47 | } |
| 48 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 49 | V4L2Camera::V4L2Camera(int id, std::string path) |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 50 | : default_camera_hal::Camera(id), |
| 51 | mDevicePath(std::move(path)), |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame^] | 52 | mOutStreamFormat(0), |
| 53 | mOutStreamWidth(0), |
| 54 | mOutStreamHeight(0), |
| 55 | mOutStreamMaxBuffers(0), |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 56 | mTemplatesInitialized(false), |
| 57 | mCharacteristicsInitialized(false) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 58 | HAL_LOG_ENTER(); |
| 59 | } |
| 60 | |
| 61 | V4L2Camera::~V4L2Camera() { |
| 62 | HAL_LOG_ENTER(); |
| 63 | } |
| 64 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame^] | 65 | // Helper function. Should be used instead of ioctl throughout this class. |
| 66 | template<typename T> |
| 67 | int V4L2Camera::ioctlLocked(int request, T data) { |
| 68 | android::Mutex::Autolock al(mDeviceLock); |
| 69 | if (mDeviceFd.get() < 0) { |
| 70 | return -ENODEV; |
| 71 | } |
| 72 | return TEMP_FAILURE_RETRY(ioctl(mDeviceFd.get(), request, data)); |
| 73 | } |
| 74 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 75 | int V4L2Camera::connect() { |
| 76 | HAL_LOG_ENTER(); |
| 77 | |
| 78 | if (mDeviceFd.get() >= 0) { |
| 79 | HAL_LOGE("Camera device %s is opened. Close it first", mDevicePath.c_str()); |
| 80 | return -EIO; |
| 81 | } |
| 82 | |
| 83 | int fd = TEMP_FAILURE_RETRY(open(mDevicePath.c_str(), O_RDWR)); |
| 84 | if (fd < 0) { |
| 85 | HAL_LOGE("failed to open %s (%s)", mDevicePath.c_str(), strerror(errno)); |
| 86 | return -errno; |
| 87 | } |
| 88 | mDeviceFd.reset(fd); |
| 89 | |
| 90 | // TODO(b/29185945): confirm this is a supported device. |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 91 | // This is checked by the HAL, but the device at mDevicePath may |
| 92 | // not be the same one that was there when the HAL was loaded. |
| 93 | // (Alternatively, better hotplugging support may make this unecessary |
| 94 | // by disabling cameras that get disconnected and checking newly connected |
| 95 | // cameras, so connect() is never called on an unsupported camera) |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 96 | |
| 97 | // TODO(b/29158098): Inform service of any flashes that are no longer available |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 98 | // because this camera is in use. |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | void V4L2Camera::disconnect() { |
| 103 | HAL_LOG_ENTER(); |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 104 | // TODO(b/29158098): Inform service of any flashes that are available again |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 105 | // because this camera is no longer in use. |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 106 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 107 | mDeviceFd.reset(); |
| 108 | } |
| 109 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 110 | int V4L2Camera::initStaticInfo(camera_metadata_t** out) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 111 | HAL_LOG_ENTER(); |
| 112 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 113 | android::status_t res; |
| 114 | // Device characteristics need to be queried prior |
| 115 | // to static info setup. |
| 116 | if (!mCharacteristicsInitialized) { |
| 117 | res = initCharacteristics(); |
| 118 | if (res) { |
| 119 | return res; |
| 120 | } |
| 121 | } |
| 122 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 123 | android::CameraMetadata info; |
Ari Hausman-Cohen | 63f6982 | 2016-06-10 11:40:35 -0700 | [diff] [blame] | 124 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 125 | // Static metadata characteristics from /system/media/camera/docs/docs.html. |
| 126 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 127 | /* android.colorCorrection. */ |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 128 | |
| 129 | // No easy way to turn chromatic aberration correction OFF in v4l2, |
| 130 | // though this may be supportable via a collection of other user controls. |
| 131 | uint8_t avail_aberration_modes[] = { |
| 132 | ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST, |
| 133 | ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 134 | res = info.update(ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES, |
| 135 | avail_aberration_modes, ARRAY_SIZE(avail_aberration_modes)); |
| 136 | if (res != android::OK) { |
| 137 | return res; |
| 138 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 139 | |
| 140 | /* android.control. */ |
| 141 | |
| 142 | /* 3As */ |
| 143 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 144 | res = info.update(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES, |
| 145 | mAeAntibandingModes.data(), mAeAntibandingModes.size()); |
| 146 | if (res != android::OK) { |
| 147 | return res; |
| 148 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 149 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 150 | res = info.update(ANDROID_CONTROL_AE_AVAILABLE_MODES, |
| 151 | mAeModes.data(), mAeModes.size()); |
| 152 | if (res != android::OK) { |
| 153 | return res; |
| 154 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 155 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 156 | // Flatten mFpsRanges. |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 157 | res = info.update(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, |
| 158 | mFpsRanges.data(), mFpsRanges.total_num_elements()); |
| 159 | if (res != android::OK) { |
| 160 | return res; |
| 161 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 162 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 163 | res = info.update(ANDROID_CONTROL_AE_COMPENSATION_RANGE, |
| 164 | mAeCompensationRange.data(), mAeCompensationRange.size()); |
| 165 | if (res != android::OK) { |
| 166 | return res; |
| 167 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 168 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 169 | res = info.update(ANDROID_CONTROL_AE_COMPENSATION_STEP, |
| 170 | &mAeCompensationStep, 1); |
| 171 | if (res != android::OK) { |
| 172 | return res; |
| 173 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 174 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 175 | res = info.update(ANDROID_CONTROL_AF_AVAILABLE_MODES, |
| 176 | mAfModes.data(), mAfModes.size()); |
| 177 | if (res != android::OK) { |
| 178 | return res; |
| 179 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 180 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 181 | res = info.update(ANDROID_CONTROL_AWB_AVAILABLE_MODES, |
| 182 | mAwbModes.data(), mAwbModes.size()); |
| 183 | if (res != android::OK) { |
| 184 | return res; |
| 185 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 186 | |
| 187 | // Couldn't find any V4L2 support for regions, though maybe it's out there. |
| 188 | int32_t max_regions[] = {/*AE*/ 0,/*AWB*/ 0,/*AF*/ 0}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 189 | res = info.update(ANDROID_CONTROL_MAX_REGIONS, |
| 190 | max_regions, ARRAY_SIZE(max_regions)); |
| 191 | if (res != android::OK) { |
| 192 | return res; |
| 193 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 194 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 195 | res = info.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE, |
| 196 | &mAeLockAvailable, 1); |
| 197 | if (res != android::OK) { |
| 198 | return res; |
| 199 | } |
| 200 | res = info.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE, |
| 201 | &mAwbLockAvailable, 1); |
| 202 | if (res != android::OK) { |
| 203 | return res; |
| 204 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 205 | |
| 206 | /* Scene modes. */ |
| 207 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 208 | res = info.update(ANDROID_CONTROL_AVAILABLE_SCENE_MODES, |
| 209 | mSceneModes.data(), mSceneModes.size()); |
| 210 | if (res != android::OK) { |
| 211 | return res; |
| 212 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 213 | |
| 214 | // A 3-tuple of AE, AWB, AF overrides for each scene mode. |
| 215 | // Ignored for DISABLED, FACE_PRIORITY and FACE_PRIORITY_LOW_LIGHT. |
| 216 | uint8_t scene_mode_overrides[] = { |
| 217 | /*SCENE_MODE_DISABLED*/ /*AE*/0, /*AW*/0, /*AF*/0}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 218 | res = info.update(ANDROID_CONTROL_SCENE_MODE_OVERRIDES, |
| 219 | scene_mode_overrides, ARRAY_SIZE(scene_mode_overrides)); |
| 220 | if (res != android::OK) { |
| 221 | return res; |
| 222 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 223 | |
| 224 | /* Top level 3A/Scenes switch. */ |
| 225 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 226 | res = info.update(ANDROID_CONTROL_AVAILABLE_MODES, |
| 227 | mControlModes.data(), mControlModes.size()); |
| 228 | if (res != android::OK) { |
| 229 | return res; |
| 230 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 231 | |
| 232 | /* Other android.control configuration. */ |
| 233 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 234 | res = info.update(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES, |
| 235 | mVideoStabilizationModes.data(), |
| 236 | mVideoStabilizationModes.size()); |
| 237 | if (res != android::OK) { |
| 238 | return res; |
| 239 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 240 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 241 | res = info.update(ANDROID_CONTROL_AVAILABLE_EFFECTS, |
| 242 | mEffects.data(), mEffects.size()); |
| 243 | if (res != android::OK) { |
| 244 | return res; |
| 245 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 246 | |
| 247 | // AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS only necessary |
| 248 | // for devices supporting CONSTRAINED_HIGH_SPEED_VIDEO, |
| 249 | // which this HAL doesn't support. |
| 250 | |
| 251 | // POST_RAW_SENSITIVITY_BOOST_RANGE only necessary |
| 252 | // for devices supporting RAW format outputs. |
| 253 | |
| 254 | /* android.edge. */ |
| 255 | |
| 256 | // Not sure if V4L2 does or doesn't do this, but HAL documentation says |
| 257 | // all devices must support FAST, and FAST can be equivalent to OFF, so |
| 258 | // either way it's fine to list. |
| 259 | uint8_t avail_edge_modes[] = { |
| 260 | ANDROID_EDGE_MODE_FAST}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 261 | res = info.update(ANDROID_EDGE_AVAILABLE_EDGE_MODES, |
| 262 | avail_edge_modes, ARRAY_SIZE(avail_edge_modes)); |
| 263 | if (res != android::OK) { |
| 264 | return res; |
| 265 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 266 | |
| 267 | /* android.flash. */ |
| 268 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 269 | res = info.update(ANDROID_FLASH_INFO_AVAILABLE, |
| 270 | &mFlashAvailable, 1); |
| 271 | if (res != android::OK) { |
| 272 | return res; |
| 273 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 274 | |
| 275 | // info.chargeDuration, color.Temperature, maxEnergy marked FUTURE. |
| 276 | |
| 277 | /* android.hotPixel. */ |
| 278 | |
| 279 | // No known V4L2 hot pixel correction. But it might be happening, |
| 280 | // so we report FAST/HIGH_QUALITY. |
| 281 | uint8_t avail_hot_pixel_modes[] = { |
| 282 | ANDROID_HOT_PIXEL_MODE_FAST, |
| 283 | ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 284 | res = info.update(ANDROID_HOT_PIXEL_AVAILABLE_HOT_PIXEL_MODES, |
| 285 | avail_hot_pixel_modes, ARRAY_SIZE(avail_hot_pixel_modes)); |
| 286 | if (res != android::OK) { |
| 287 | return res; |
| 288 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 289 | |
| 290 | /* android.jpeg. */ |
| 291 | |
| 292 | // For now, no thumbnails available (only [0,0], the "no thumbnail" size). |
| 293 | // TODO(b/29580107): Could end up with a mismatch between request & result, |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 294 | // since V4L2 doesn't actually allow for thumbnail size control. |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 295 | int32_t thumbnail_sizes[] = { |
| 296 | 0, 0}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 297 | res = info.update(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, |
| 298 | thumbnail_sizes, ARRAY_SIZE(thumbnail_sizes)); |
| 299 | if (res != android::OK) { |
| 300 | return res; |
| 301 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 302 | |
| 303 | // V4L2 doesn't support querying this, so we generously assume up to 3 MB. |
| 304 | int32_t max_jpeg_size = 3000000; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 305 | res = info.update(ANDROID_JPEG_MAX_SIZE, |
| 306 | &max_jpeg_size, 1); |
| 307 | if (res != android::OK) { |
| 308 | return res; |
| 309 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 310 | |
| 311 | /* android.lens. */ |
| 312 | |
| 313 | /* Misc. lens control. */ |
| 314 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 315 | res = info.update(ANDROID_LENS_INFO_AVAILABLE_APERTURES, |
| 316 | &mAperture, 1); |
| 317 | if (res != android::OK) { |
| 318 | return res; |
| 319 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 320 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 321 | res = info.update(ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES, |
| 322 | &mFilterDensity, 1); |
| 323 | if (res != android::OK) { |
| 324 | return res; |
| 325 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 326 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 327 | res = info.update(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION, |
| 328 | mOpticalStabilizationModes.data(), |
| 329 | mOpticalStabilizationModes.size()); |
| 330 | if (res != android::OK) { |
| 331 | return res; |
| 332 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 333 | |
| 334 | // No known V4L2 shading map info. |
| 335 | int32_t shading_map_size[] = {1, 1}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 336 | res = info.update(ANDROID_LENS_INFO_SHADING_MAP_SIZE, |
| 337 | shading_map_size, ARRAY_SIZE(shading_map_size)); |
| 338 | if (res != android::OK) { |
| 339 | return res; |
| 340 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 341 | |
| 342 | // All V4L2 devices are considered to be external facing. |
| 343 | uint8_t facing = ANDROID_LENS_FACING_EXTERNAL; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 344 | res = info.update(ANDROID_LENS_FACING, &facing, 1); |
| 345 | if (res != android::OK) { |
| 346 | return res; |
| 347 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 348 | |
| 349 | /* Zoom/Focus. */ |
| 350 | |
| 351 | // No way to actually get the focal length in V4L2, but it's a required key, |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 352 | // so we just fake it. |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 353 | res = info.update(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS, |
| 354 | &mFocalLength, 1); |
| 355 | if (res != android::OK) { |
| 356 | return res; |
| 357 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 358 | |
| 359 | // V4L2 focal units do not correspond to a particular physical unit. |
| 360 | uint8_t focus_calibration = |
| 361 | ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 362 | res = info.update(ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION, |
| 363 | &focus_calibration, 1); |
| 364 | if (res != android::OK) { |
| 365 | return res; |
| 366 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 367 | |
| 368 | // info.hyperfocalDistance not required for UNCALIBRATED. |
| 369 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 370 | res = info.update(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE, |
| 371 | &mFocusDistance, 1); |
| 372 | if (res != android::OK) { |
| 373 | return res; |
| 374 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 375 | |
| 376 | /* Depth. */ |
| 377 | |
| 378 | // DEPTH capability not supported by this HAL. Not implemented: |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 379 | // poseRotation |
| 380 | // poseTranslation |
| 381 | // intrinsicCalibration |
| 382 | // radialDistortion |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 383 | |
| 384 | /* anroid.noise. */ |
| 385 | |
| 386 | // Unable to control noise reduction in V4L2 devices, |
| 387 | // but FAST is allowed to be the same as OFF. |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 388 | uint8_t avail_noise_reduction_modes[] = {ANDROID_NOISE_REDUCTION_MODE_FAST}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 389 | res = info.update(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES, |
| 390 | avail_noise_reduction_modes, |
| 391 | ARRAY_SIZE(avail_noise_reduction_modes)); |
| 392 | if (res != android::OK) { |
| 393 | return res; |
| 394 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 395 | |
| 396 | /* android.request. */ |
| 397 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 398 | int32_t max_num_output_streams[] = { |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame^] | 399 | mMaxRawOutputStreams, mMaxYuvOutputStreams, mMaxJpegOutputStreams}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 400 | res = info.update(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS, |
| 401 | max_num_output_streams, ARRAY_SIZE(max_num_output_streams)); |
| 402 | if (res != android::OK) { |
| 403 | return res; |
| 404 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 405 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame^] | 406 | res = info.update(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, |
| 407 | &mMaxInputStreams, 1); |
| 408 | if (res != android::OK) { |
| 409 | return res; |
| 410 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 411 | |
| 412 | // No way to know for V4L2, so fake with max allowable latency. |
| 413 | // Doesn't mean much without per-frame controls. |
| 414 | uint8_t pipeline_max_depth = 4; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 415 | res = info.update(ANDROID_REQUEST_PIPELINE_MAX_DEPTH, |
| 416 | &pipeline_max_depth, 1); |
| 417 | if (res != android::OK) { |
| 418 | return res; |
| 419 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 420 | |
| 421 | // Partial results not supported; partialResultCount defaults to 1. |
| 422 | |
| 423 | // Available capabilities & keys queried at very end of this method. |
| 424 | |
| 425 | /* android.scaler. */ |
| 426 | |
| 427 | /* Cropping. */ |
| 428 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 429 | res = info.update(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM, |
| 430 | &mMaxZoom, 1); |
| 431 | if (res != android::OK) { |
| 432 | return res; |
| 433 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 434 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 435 | res = info.update(ANDROID_SCALER_CROPPING_TYPE, &mCropType, 1); |
| 436 | if (res != android::OK) { |
| 437 | return res; |
| 438 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 439 | |
| 440 | /* Streams. */ |
| 441 | |
| 442 | // availableInputOutputFormatsMap only required for reprocessing capability. |
| 443 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 444 | // Flatten mStreamConfigs. |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 445 | res = info.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, |
| 446 | mStreamConfigs.data(), |
| 447 | mStreamConfigs.total_num_elements()); |
| 448 | if (res != android::OK) { |
| 449 | return res; |
| 450 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 451 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 452 | // Flatten mMinFrameDurations. |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 453 | res = info.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, |
| 454 | mMinFrameDurations.data(), |
| 455 | mMinFrameDurations.total_num_elements()); |
| 456 | if (res != android::OK) { |
| 457 | return res; |
| 458 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 459 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 460 | // Flatten mStallDurations. |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 461 | res = info.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, |
| 462 | mStallDurations.data(), |
| 463 | mStallDurations.total_num_elements()); |
| 464 | if (res != android::OK) { |
| 465 | return res; |
| 466 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 467 | |
| 468 | /* android.sensor. */ |
| 469 | |
| 470 | /* Sizes. */ |
| 471 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 472 | res = info.update(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE, |
| 473 | mPixelArraySize.data(), mPixelArraySize.size()); |
| 474 | if (res != android::OK) { |
| 475 | return res; |
| 476 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 477 | // No V4L2 way to differentiate active vs. inactive parts of the rectangle. |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 478 | res = info.update(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, |
| 479 | mPixelArraySize.data(), mPixelArraySize.size()); |
| 480 | if (res != android::OK) { |
| 481 | return res; |
| 482 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 483 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 484 | res = info.update(ANDROID_SENSOR_INFO_PHYSICAL_SIZE, |
| 485 | mPhysicalSize.data(), mPhysicalSize.size()); |
| 486 | if (res != android::OK) { |
| 487 | return res; |
| 488 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 489 | |
| 490 | /* Misc sensor information. */ |
| 491 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 492 | res = info.update(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION, |
| 493 | &mMaxFrameDuration, 1); |
| 494 | if (res != android::OK) { |
| 495 | return res; |
| 496 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 497 | |
| 498 | // HAL uses BOOTTIME timestamps. |
| 499 | // TODO(b/29457051): make sure timestamps are consistent throughout the HAL. |
| 500 | uint8_t timestamp_source = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 501 | res = info.update(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE, |
| 502 | ×tamp_source, 1); |
| 503 | if (res != android::OK) { |
| 504 | return res; |
| 505 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 506 | |
| 507 | // As in initDeviceInfo, no way to actually get orientation. |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 508 | res = info.update(ANDROID_SENSOR_ORIENTATION, &mOrientation, 1); |
| 509 | if (res != android::OK) { |
| 510 | return res; |
| 511 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 512 | |
| 513 | // availableTestPatternModes just defaults to OFF, which is fine. |
| 514 | |
| 515 | // info.exposureTimeRange, info.sensitivityRange: |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 516 | // exposure/sensitivity manual control not supported. |
| 517 | // Could query V4L2_CID_ISO_SENSITIVITY to support sensitivity if desired. |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 518 | |
| 519 | // info.whiteLevel, info.lensShadingApplied, |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 520 | // info.preCorrectionPixelArraySize, referenceIlluminant1/2, |
| 521 | // calibrationTransform1/2, colorTransform1/2, forwardMatrix1/2, |
| 522 | // blackLevelPattern, profileHueSatMapDimensions |
| 523 | // all only necessary for RAW. |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 524 | |
| 525 | // baseGainFactor marked FUTURE. |
| 526 | |
| 527 | // maxAnalogSensitivity optional for LIMITED device. |
| 528 | |
| 529 | // opticalBlackRegions: No known way to get in V4L2, but not necessary. |
| 530 | |
| 531 | // opaqueRawSize not necessary since RAW_OPAQUE format not supported. |
| 532 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 533 | /* android.shading */ |
| 534 | |
| 535 | // No known V4L2 lens shading. But it might be happening, |
| 536 | // so we report FAST/HIGH_QUALITY. |
| 537 | uint8_t avail_shading_modes[] = { |
| 538 | ANDROID_SHADING_MODE_FAST, |
| 539 | ANDROID_SHADING_MODE_HIGH_QUALITY}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 540 | res = info.update(ANDROID_SHADING_AVAILABLE_MODES, |
| 541 | avail_shading_modes, ARRAY_SIZE(avail_shading_modes)); |
| 542 | if (res != android::OK) { |
| 543 | return res; |
| 544 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 545 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 546 | /* android.statistics */ |
| 547 | |
| 548 | // Face detection not supported. |
| 549 | uint8_t avail_face_detect_modes[] = { |
| 550 | ANDROID_STATISTICS_FACE_DETECT_MODE_OFF}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 551 | res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES, |
| 552 | avail_face_detect_modes, |
| 553 | ARRAY_SIZE(avail_face_detect_modes)); |
| 554 | if (res != android::OK) { |
| 555 | return res; |
| 556 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 557 | |
| 558 | int32_t max_face_count = 0; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 559 | res = info.update(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT, |
| 560 | &max_face_count, 1); |
| 561 | if (res != android::OK) { |
| 562 | return res; |
| 563 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 564 | |
| 565 | // info.histogramBucketCount, info.maxHistogramCount, |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 566 | // info.maxSharpnessMapValue, info.sharpnessMapSizemarked FUTURE. |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 567 | |
| 568 | // ON only needs to be supported for RAW capable devices. |
| 569 | uint8_t avail_hot_pixel_map_modes[] = { |
| 570 | ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 571 | res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES, |
| 572 | avail_hot_pixel_map_modes, |
| 573 | ARRAY_SIZE(avail_hot_pixel_map_modes)); |
| 574 | if (res != android::OK) { |
| 575 | return res; |
| 576 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 577 | |
| 578 | // ON only needs to be supported for RAW capable devices. |
| 579 | uint8_t avail_lens_shading_map_modes[] = { |
| 580 | ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 581 | res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES, |
| 582 | avail_lens_shading_map_modes, |
| 583 | ARRAY_SIZE(avail_lens_shading_map_modes)); |
| 584 | if (res != android::OK) { |
| 585 | return res; |
| 586 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 587 | |
| 588 | /* android.tonemap. */ |
| 589 | |
| 590 | // tonemapping only required for MANUAL_POST_PROCESSING capability. |
| 591 | |
| 592 | /* android.led. */ |
| 593 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 594 | // May or may not have LEDs available. |
| 595 | if (!mLeds.empty()) { |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 596 | res = info.update(ANDROID_LED_AVAILABLE_LEDS, mLeds.data(), mLeds.size()); |
| 597 | if (res != android::OK) { |
| 598 | return res; |
| 599 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 600 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 601 | |
| 602 | /* android.sync. */ |
| 603 | |
| 604 | // "LIMITED devices are strongly encouraged to use a non-negative value. |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 605 | // If UNKNOWN is used here then app developers do not have a way to know |
| 606 | // when sensor settings have been applied." - Unfortunately, V4L2 doesn't |
| 607 | // really help here either. Could even be that adjusting settings mid-stream |
| 608 | // blocks in V4L2, and should be avoided. |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 609 | int32_t max_latency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 610 | res = info.update(ANDROID_SYNC_MAX_LATENCY, |
| 611 | &max_latency, 1); |
| 612 | if (res != android::OK) { |
| 613 | return res; |
| 614 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 615 | |
| 616 | /* android.reprocess. */ |
| 617 | |
| 618 | // REPROCESSING not supported by this HAL. |
| 619 | |
| 620 | /* android.depth. */ |
| 621 | |
| 622 | // DEPTH not supported by this HAL. |
| 623 | |
| 624 | /* Capabilities and android.info. */ |
| 625 | |
| 626 | uint8_t hw_level = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 627 | res = info.update(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL, |
| 628 | &hw_level, 1); |
| 629 | if (res != android::OK) { |
| 630 | return res; |
| 631 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 632 | |
| 633 | uint8_t capabilities[] = { |
| 634 | ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 635 | res = info.update(ANDROID_REQUEST_AVAILABLE_CAPABILITIES, |
| 636 | capabilities, ARRAY_SIZE(capabilities)); |
| 637 | if (res != android::OK) { |
| 638 | return res; |
| 639 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 640 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 641 | // Scan a default request template for included request keys. |
| 642 | if (!mTemplatesInitialized) { |
| 643 | res = initTemplates(); |
| 644 | if (res) { |
| 645 | return res; |
| 646 | } |
| 647 | } |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 648 | const camera_metadata_t* preview_request = nullptr; |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 649 | // Search templates from the beginning for a supported one. |
| 650 | for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT; |
| 651 | ++template_id) { |
| 652 | preview_request = constructDefaultRequestSettings(template_id); |
| 653 | if (preview_request != nullptr) { |
| 654 | break; |
| 655 | } |
| 656 | } |
| 657 | if (preview_request == nullptr) { |
| 658 | HAL_LOGE("No valid templates, can't get request keys."); |
| 659 | return -ENODEV; |
| 660 | } |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 661 | std::vector<int32_t> avail_request_keys = getMetadataKeys(preview_request); |
| 662 | res = info.update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, |
| 663 | avail_request_keys.data(), avail_request_keys.size()); |
| 664 | if (res != android::OK) { |
| 665 | return res; |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 666 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 667 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 668 | // Result keys will be duplicated from the request, plus a few extras. |
| 669 | // TODO(b/29335262): additonal available result keys. |
| 670 | std::vector<int32_t> avail_result_keys(avail_request_keys); |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 671 | res = info.update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, |
| 672 | avail_result_keys.data(), avail_result_keys.size()); |
| 673 | if (res != android::OK) { |
| 674 | return res; |
| 675 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 676 | |
| 677 | // Last thing, once all the available characteristics have been added. |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 678 | const camera_metadata_t* static_characteristics = info.getAndLock(); |
| 679 | std::vector<int32_t> avail_characteristics_keys = |
| 680 | getMetadataKeys(static_characteristics); |
| 681 | res = info.unlock(static_characteristics); |
| 682 | if (res != android::OK) { |
| 683 | return res; |
| 684 | } |
| 685 | avail_characteristics_keys.push_back( |
| 686 | ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS); |
| 687 | res = info.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, |
| 688 | avail_characteristics_keys.data(), |
| 689 | avail_characteristics_keys.size()); |
| 690 | if (res != android::OK) { |
| 691 | return res; |
| 692 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 693 | |
| 694 | *out = info.release(); |
| 695 | return 0; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | void V4L2Camera::initDeviceInfo(camera_info_t* info) { |
| 699 | HAL_LOG_ENTER(); |
| 700 | |
| 701 | // For now, just constants. |
| 702 | info->facing = CAMERA_FACING_EXTERNAL; |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 703 | info->orientation = mOrientation; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 704 | info->resource_cost = 100; |
| 705 | info->conflicting_devices = nullptr; |
| 706 | info->conflicting_devices_length = 0; |
| 707 | } |
| 708 | |
| 709 | int V4L2Camera::initDevice() { |
| 710 | HAL_LOG_ENTER(); |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 711 | int res; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 712 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 713 | // Templates should be set up if they haven't already been. |
| 714 | if (!mTemplatesInitialized) { |
| 715 | res = initTemplates(); |
| 716 | if (res) { |
| 717 | return res; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | int V4L2Camera::initTemplates() { |
| 725 | HAL_LOG_ENTER(); |
| 726 | int res; |
| 727 | |
| 728 | // Device characteristics need to be queried prior |
| 729 | // to template setup. |
| 730 | if (!mCharacteristicsInitialized) { |
| 731 | res = initCharacteristics(); |
| 732 | if (res) { |
| 733 | return res; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | // Note: static metadata expects all templates/requests |
| 738 | // to provide values for all supported keys. |
| 739 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 740 | android::CameraMetadata base_metadata; |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 741 | |
| 742 | // Start with defaults for all templates. |
| 743 | |
| 744 | /* android.colorCorrection. */ |
| 745 | |
| 746 | uint8_t aberration_mode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 747 | res = base_metadata.update(ANDROID_COLOR_CORRECTION_ABERRATION_MODE, |
| 748 | &aberration_mode, 1); |
| 749 | if (res != android::OK) { |
| 750 | return res; |
| 751 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 752 | |
| 753 | uint8_t color_correction_mode = ANDROID_COLOR_CORRECTION_MODE_FAST; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 754 | res = base_metadata.update(ANDROID_COLOR_CORRECTION_MODE, |
| 755 | &color_correction_mode, 1); |
| 756 | if (res != android::OK) { |
| 757 | return res; |
| 758 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 759 | |
| 760 | // transform and gains are for the unsupported MANUAL_POST_PROCESSING only. |
| 761 | |
| 762 | /* android.control. */ |
| 763 | |
| 764 | /* AE. */ |
| 765 | uint8_t ae_antibanding_mode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 766 | res = base_metadata.update(ANDROID_CONTROL_AE_ANTIBANDING_MODE, |
| 767 | &ae_antibanding_mode, 1); |
| 768 | if (res != android::OK) { |
| 769 | return res; |
| 770 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 771 | |
| 772 | // Only matters if AE_MODE = OFF |
| 773 | int32_t ae_exposure_compensation = 0; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 774 | res = base_metadata.update(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION, |
| 775 | &ae_exposure_compensation, 1); |
| 776 | if (res != android::OK) { |
| 777 | return res; |
| 778 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 779 | |
| 780 | uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 781 | res = base_metadata.update(ANDROID_CONTROL_AE_LOCK, &ae_lock, 1); |
| 782 | if (res != android::OK) { |
| 783 | return res; |
| 784 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 785 | |
| 786 | uint8_t ae_mode = ANDROID_CONTROL_AE_MODE_ON; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 787 | res = base_metadata.update(ANDROID_CONTROL_AE_MODE, &ae_mode, 1); |
| 788 | if (res != android::OK) { |
| 789 | return res; |
| 790 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 791 | |
| 792 | // AE regions not supported. |
| 793 | |
| 794 | // FPS set per-template. |
| 795 | |
| 796 | uint8_t ae_precapture_trigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 797 | res = base_metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, |
| 798 | &ae_precapture_trigger, 1); |
| 799 | if (res != android::OK) { |
| 800 | return res; |
| 801 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 802 | |
| 803 | /* AF. */ |
| 804 | |
| 805 | // AF mode set per-template. |
| 806 | |
| 807 | // AF regions not supported. |
| 808 | |
| 809 | uint8_t af_trigger = ANDROID_CONTROL_AF_TRIGGER_IDLE; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 810 | res = base_metadata.update(ANDROID_CONTROL_AF_TRIGGER, &af_trigger, 1); |
| 811 | if (res != android::OK) { |
| 812 | return res; |
| 813 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 814 | |
| 815 | /* AWB. */ |
| 816 | |
| 817 | // Priority: auto > off > Whatever is available. |
| 818 | uint8_t default_awb_mode = mAwbModes[0]; |
| 819 | if (std::count(mAwbModes.begin(), mAwbModes.end(), |
| 820 | ANDROID_CONTROL_AWB_MODE_AUTO)) { |
| 821 | default_awb_mode = ANDROID_CONTROL_AWB_MODE_AUTO; |
| 822 | } else if (std::count(mAwbModes.begin(), mAwbModes.end(), |
| 823 | ANDROID_CONTROL_AWB_MODE_OFF)) { |
| 824 | default_awb_mode = ANDROID_CONTROL_AWB_MODE_OFF; |
| 825 | } |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 826 | res = base_metadata.update(ANDROID_CONTROL_AWB_MODE, &default_awb_mode, 1); |
| 827 | if (res != android::OK) { |
| 828 | return res; |
| 829 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 830 | |
| 831 | // AWB regions not supported. |
| 832 | |
| 833 | /* Other controls. */ |
| 834 | |
| 835 | uint8_t effect_mode = ANDROID_CONTROL_EFFECT_MODE_OFF; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 836 | res = base_metadata.update(ANDROID_CONTROL_EFFECT_MODE, &effect_mode, 1); |
| 837 | if (res != android::OK) { |
| 838 | return res; |
| 839 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 840 | |
| 841 | uint8_t control_mode = ANDROID_CONTROL_MODE_AUTO; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 842 | res = base_metadata.update(ANDROID_CONTROL_MODE, &control_mode, 1); |
| 843 | if (res != android::OK) { |
| 844 | return res; |
| 845 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 846 | |
| 847 | uint8_t scene_mode = ANDROID_CONTROL_SCENE_MODE_DISABLED; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 848 | res = base_metadata.update(ANDROID_CONTROL_SCENE_MODE, |
| 849 | &scene_mode, 1); |
| 850 | if (res != android::OK) { |
| 851 | return res; |
| 852 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 853 | |
| 854 | uint8_t video_stabilization = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 855 | res = base_metadata.update(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE, |
| 856 | &video_stabilization, 1); |
| 857 | if (res != android::OK) { |
| 858 | return res; |
| 859 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 860 | |
| 861 | // postRawSensitivityBoost: RAW not supported, leave null. |
| 862 | |
| 863 | /* android.demosaic. */ |
| 864 | |
| 865 | // mode marked FUTURE. |
| 866 | |
| 867 | /* android.edge. */ |
| 868 | |
| 869 | uint8_t edge_mode = ANDROID_EDGE_MODE_FAST; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 870 | res = base_metadata.update(ANDROID_EDGE_MODE, &edge_mode, 1); |
| 871 | if (res != android::OK) { |
| 872 | return res; |
| 873 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 874 | |
| 875 | // strength marked FUTURE. |
| 876 | |
| 877 | /* android.flash. */ |
| 878 | |
| 879 | // firingPower, firingTime marked FUTURE. |
| 880 | |
| 881 | uint8_t flash_mode = ANDROID_FLASH_MODE_OFF; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 882 | res = base_metadata.update(ANDROID_FLASH_MODE, &flash_mode, 1); |
| 883 | if (res != android::OK) { |
| 884 | return res; |
| 885 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 886 | |
| 887 | /* android.hotPixel. */ |
| 888 | |
| 889 | uint8_t hp_mode = ANDROID_HOT_PIXEL_MODE_FAST; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 890 | res = base_metadata.update(ANDROID_HOT_PIXEL_MODE, &hp_mode, 1); |
| 891 | if (res != android::OK) { |
| 892 | return res; |
| 893 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 894 | |
| 895 | /* android.jpeg. */ |
| 896 | |
| 897 | double gps_coords[] = {/*latitude*/0, /*longitude*/0, /*altitude*/0}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 898 | res = base_metadata.update(ANDROID_JPEG_GPS_COORDINATES, gps_coords, 3); |
| 899 | if (res != android::OK) { |
| 900 | return res; |
| 901 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 902 | |
| 903 | uint8_t gps_processing_method[] = "none"; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 904 | res = base_metadata.update(ANDROID_JPEG_GPS_PROCESSING_METHOD, |
| 905 | gps_processing_method, |
| 906 | ARRAY_SIZE(gps_processing_method)); |
| 907 | if (res != android::OK) { |
| 908 | return res; |
| 909 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 910 | |
| 911 | int64_t gps_timestamp = 0; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 912 | res = base_metadata.update(ANDROID_JPEG_GPS_TIMESTAMP, &gps_timestamp, 1); |
| 913 | if (res != android::OK) { |
| 914 | return res; |
| 915 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 916 | |
| 917 | // JPEG orientation is relative to sensor orientation (mOrientation). |
| 918 | int32_t jpeg_orientation = 0; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 919 | res = base_metadata.update(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1); |
| 920 | if (res != android::OK) { |
| 921 | return res; |
| 922 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 923 | |
| 924 | // 1-100, larger is higher quality. |
| 925 | uint8_t jpeg_quality = 80; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 926 | res = base_metadata.update(ANDROID_JPEG_QUALITY, &jpeg_quality, 1); |
| 927 | if (res != android::OK) { |
| 928 | return res; |
| 929 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 930 | |
| 931 | // TODO(b/29580107): If thumbnail quality actually matters/can be adjusted, |
| 932 | // adjust this. |
| 933 | uint8_t thumbnail_quality = 80; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 934 | res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_QUALITY, |
| 935 | &thumbnail_quality, 1); |
| 936 | if (res != android::OK) { |
| 937 | return res; |
| 938 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 939 | |
| 940 | // TODO(b/29580107): Choose a size matching the resolution. |
| 941 | int32_t thumbnail_size[] = {0, 0}; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 942 | res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_SIZE, thumbnail_size, 2); |
| 943 | if (res != android::OK) { |
| 944 | return res; |
| 945 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 946 | |
| 947 | /* android.lens. */ |
| 948 | |
| 949 | // Fixed values. |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 950 | res = base_metadata.update(ANDROID_LENS_APERTURE, &mAperture, 1); |
| 951 | if (res != android::OK) { |
| 952 | return res; |
| 953 | } |
| 954 | res = base_metadata.update(ANDROID_LENS_FILTER_DENSITY, &mFilterDensity, 1); |
| 955 | if (res != android::OK) { |
| 956 | return res; |
| 957 | } |
| 958 | res = base_metadata.update(ANDROID_LENS_FOCAL_LENGTH, &mFocalLength, 1); |
| 959 | if (res != android::OK) { |
| 960 | return res; |
| 961 | } |
| 962 | res = base_metadata.update(ANDROID_LENS_FOCUS_DISTANCE, &mFocusDistance, 1); |
| 963 | if (res != android::OK) { |
| 964 | return res; |
| 965 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 966 | |
| 967 | uint8_t optical_stabilization = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 968 | res = base_metadata.update(ANDROID_LENS_OPTICAL_STABILIZATION_MODE, |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 969 | &optical_stabilization, 1); |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 970 | if (res != android::OK) { |
| 971 | return res; |
| 972 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 973 | |
| 974 | /* android.noiseReduction. */ |
| 975 | |
| 976 | uint8_t noise_reduction_mode = ANDROID_NOISE_REDUCTION_MODE_FAST; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 977 | res = base_metadata.update(ANDROID_NOISE_REDUCTION_MODE, |
| 978 | &noise_reduction_mode, 1); |
| 979 | if (res != android::OK) { |
| 980 | return res; |
| 981 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 982 | |
| 983 | // strength marked FUTURE. |
| 984 | |
| 985 | /* android.request. */ |
| 986 | |
| 987 | // Request Id unused by the HAL for now, and these are just |
| 988 | // templates, so just fill it in with a dummy. |
| 989 | int32_t id = 0; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 990 | res = base_metadata.update(ANDROID_REQUEST_ID, &id, 1); |
| 991 | if (res != android::OK) { |
| 992 | return res; |
| 993 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 994 | |
| 995 | // metadataMode marked FUTURE. |
| 996 | |
| 997 | /* android.scaler. */ |
| 998 | |
| 999 | // No cropping by default; use the full active array. |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 1000 | res = base_metadata.update(ANDROID_SCALER_CROP_REGION, mPixelArraySize.data(), |
| 1001 | mPixelArraySize.size()); |
| 1002 | if (res != android::OK) { |
| 1003 | return res; |
| 1004 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1005 | |
| 1006 | /* android.sensor. */ |
| 1007 | |
| 1008 | // exposureTime, sensitivity, testPattern[Data,Mode] not supported. |
| 1009 | |
| 1010 | // Ignored when AE is OFF. |
| 1011 | int64_t frame_duration = 33333333L; // 1/30 s. |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 1012 | res = base_metadata.update(ANDROID_SENSOR_FRAME_DURATION, &frame_duration, 1); |
| 1013 | if (res != android::OK) { |
| 1014 | return res; |
| 1015 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1016 | |
| 1017 | /* android.shading. */ |
| 1018 | |
| 1019 | uint8_t shading_mode = ANDROID_SHADING_MODE_FAST; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 1020 | res = base_metadata.update(ANDROID_SHADING_MODE, &shading_mode, 1); |
| 1021 | if (res != android::OK) { |
| 1022 | return res; |
| 1023 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1024 | |
| 1025 | /* android.statistics. */ |
| 1026 | |
| 1027 | uint8_t face_detect_mode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 1028 | res = base_metadata.update(ANDROID_STATISTICS_FACE_DETECT_MODE, |
| 1029 | &face_detect_mode, 1); |
| 1030 | if (res != android::OK) { |
| 1031 | return res; |
| 1032 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1033 | |
| 1034 | // histogramMode, sharpnessMapMode marked FUTURE. |
| 1035 | |
| 1036 | uint8_t hp_map_mode = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 1037 | res = base_metadata.update(ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE, |
| 1038 | &hp_map_mode, 1); |
| 1039 | if (res != android::OK) { |
| 1040 | return res; |
| 1041 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1042 | |
| 1043 | uint8_t lens_shading_map_mode = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 1044 | res = base_metadata.update(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE, |
| 1045 | &lens_shading_map_mode, 1); |
| 1046 | if (res != android::OK) { |
| 1047 | return res; |
| 1048 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1049 | |
| 1050 | /* android.tonemap. */ |
| 1051 | |
| 1052 | // Tonemap only required for MANUAL_POST_PROCESSING capability. |
| 1053 | |
| 1054 | /* android.led. */ |
| 1055 | |
| 1056 | uint8_t transmit = ANDROID_LED_TRANSMIT_ON; |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 1057 | res = base_metadata.update(ANDROID_LED_TRANSMIT, &transmit, 1); |
| 1058 | if (res != android::OK) { |
| 1059 | return res; |
| 1060 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1061 | |
| 1062 | /* android.reprocess */ |
| 1063 | |
| 1064 | // Only needed for REPROCESS capability. |
| 1065 | |
| 1066 | |
| 1067 | /* Template variable values. */ |
| 1068 | |
| 1069 | // Find the FPS ranges "closest" to a desired range |
| 1070 | // (minimum abs distance from min to min and max to max). |
| 1071 | // Find both a fixed rate and a variable rate, for different purposes. |
| 1072 | std::array<int32_t, 2> desired_flat_fps_range = {{30, 30}}; |
| 1073 | std::array<int32_t, 2> desired_variable_fps_range = {{5, 30}}; |
| 1074 | std::array<int32_t, 2> flat_fps_range; |
| 1075 | std::array<int32_t, 2> variable_fps_range; |
| 1076 | int32_t best_flat_distance = std::numeric_limits<int32_t>::max(); |
| 1077 | int32_t best_variable_distance = std::numeric_limits<int32_t>::max(); |
| 1078 | size_t num_fps_ranges = mFpsRanges.num_arrays(); |
| 1079 | for (size_t i = 0; i < num_fps_ranges; ++i) { |
| 1080 | const int32_t* range = mFpsRanges[i]; |
| 1081 | // Variable fps. |
| 1082 | int32_t distance = std::abs(range[0] - desired_variable_fps_range[0]) + |
| 1083 | std::abs(range[1] - desired_variable_fps_range[1]); |
| 1084 | if (distance < best_variable_distance) { |
| 1085 | variable_fps_range[0] = range[0]; |
| 1086 | variable_fps_range[1] = range[1]; |
| 1087 | best_variable_distance = distance; |
| 1088 | } |
| 1089 | // Flat fps. Only do if range is actually flat. |
| 1090 | // Note at least one flat range is required, |
| 1091 | // so something will always be filled in. |
| 1092 | if (range[0] == range[1]) { |
| 1093 | distance = std::abs(range[0] - desired_flat_fps_range[0]) + |
| 1094 | std::abs(range[1] - desired_flat_fps_range[1]); |
| 1095 | if (distance < best_flat_distance) { |
| 1096 | flat_fps_range[0] = range[0]; |
| 1097 | flat_fps_range[1] = range[1]; |
| 1098 | best_flat_distance = distance; |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | // Priority: continuous > auto > off > whatever is available. |
| 1104 | bool continuous_still_avail = std::count( |
| 1105 | mAfModes.begin(), mAfModes.end(), |
| 1106 | ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE); |
| 1107 | bool continuous_video_avail = std::count( |
| 1108 | mAfModes.begin(), mAfModes.end(), |
| 1109 | ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO); |
| 1110 | uint8_t non_continuous_af_mode = mAfModes[0]; |
| 1111 | if (std::count(mAfModes.begin(), mAfModes.end(), |
| 1112 | ANDROID_CONTROL_AF_MODE_AUTO)) { |
| 1113 | non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_AUTO; |
| 1114 | } else if (std::count(mAfModes.begin(), mAfModes.end(), |
| 1115 | ANDROID_CONTROL_AF_MODE_OFF)) { |
| 1116 | non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_OFF; |
| 1117 | } |
| 1118 | uint8_t still_af_mode = continuous_still_avail ? |
| 1119 | ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE : non_continuous_af_mode; |
| 1120 | uint8_t video_af_mode = continuous_video_avail ? |
| 1121 | ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO : non_continuous_af_mode; |
| 1122 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 1123 | for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT; |
| 1124 | ++template_id) { |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1125 | // General differences/support. |
| 1126 | uint8_t intent; |
| 1127 | uint8_t af_mode; |
| 1128 | std::array<int32_t, 2> fps_range; |
| 1129 | switch(template_id) { |
| 1130 | case CAMERA3_TEMPLATE_PREVIEW: |
| 1131 | intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW; |
| 1132 | af_mode = still_af_mode; |
| 1133 | fps_range = flat_fps_range; |
| 1134 | break; |
| 1135 | case CAMERA3_TEMPLATE_STILL_CAPTURE: |
| 1136 | intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE; |
| 1137 | af_mode = still_af_mode; |
| 1138 | fps_range = variable_fps_range; |
| 1139 | break; |
| 1140 | case CAMERA3_TEMPLATE_VIDEO_RECORD: |
| 1141 | intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD; |
| 1142 | af_mode = video_af_mode; |
| 1143 | fps_range = flat_fps_range; |
| 1144 | break; |
| 1145 | case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT: |
| 1146 | intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT; |
| 1147 | af_mode = video_af_mode; |
| 1148 | fps_range = flat_fps_range; |
| 1149 | break; |
| 1150 | case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: // Fall through. |
| 1151 | case CAMERA3_TEMPLATE_MANUAL: // Fall though. |
| 1152 | default: |
| 1153 | // Unsupported/unrecognized. Don't add this template; skip it. |
| 1154 | continue; |
| 1155 | } |
| 1156 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 1157 | // Copy our base metadata and add the new items. |
| 1158 | android::CameraMetadata template_metadata(base_metadata); |
| 1159 | res = template_metadata.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent, 1); |
| 1160 | if (res != android::OK) { |
| 1161 | return res; |
| 1162 | } |
| 1163 | res = template_metadata.update(ANDROID_CONTROL_AE_TARGET_FPS_RANGE, |
| 1164 | fps_range.data(), fps_range.size()); |
| 1165 | if (res != android::OK) { |
| 1166 | return res; |
| 1167 | } |
| 1168 | res = template_metadata.update(ANDROID_CONTROL_AF_MODE, &af_mode, 1); |
| 1169 | if (res != android::OK) { |
| 1170 | return res; |
| 1171 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1172 | |
| 1173 | const camera_metadata_t* template_raw_metadata = |
| 1174 | template_metadata.getAndLock(); |
| 1175 | res = setTemplate(template_id, template_raw_metadata); |
| 1176 | if (res != android::OK) { |
| 1177 | return res; |
| 1178 | } |
| 1179 | res = template_metadata.unlock(template_raw_metadata); |
| 1180 | if (res != android::OK) { |
| 1181 | return res; |
| 1182 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | mTemplatesInitialized = true; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 1186 | return 0; |
| 1187 | } |
| 1188 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame^] | 1189 | bool V4L2Camera::isSupportedStreamSet(default_camera_hal::Stream** streams, |
| 1190 | int count, uint32_t mode) { |
| 1191 | HAL_LOG_ENTER(); |
| 1192 | |
| 1193 | if (mode != CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE) { |
| 1194 | HAL_LOGE("Unsupported stream configuration mode: %d", mode); |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
| 1198 | // This should be checked by the caller, but put here as a sanity check. |
| 1199 | if (count < 1) { |
| 1200 | HAL_LOGE("Must request at least 1 stream"); |
| 1201 | return false; |
| 1202 | } |
| 1203 | |
| 1204 | // Count the number of streams of each type. |
| 1205 | int32_t num_input = 0; |
| 1206 | int32_t num_raw = 0; |
| 1207 | int32_t num_yuv = 0; |
| 1208 | int32_t num_jpeg = 0; |
| 1209 | for (int i = 0; i < count; ++i) { |
| 1210 | default_camera_hal::Stream* stream = streams[i]; |
| 1211 | |
| 1212 | if (stream->isInputType()) { |
| 1213 | ++num_input; |
| 1214 | } |
| 1215 | |
| 1216 | if (stream->isOutputType()) { |
| 1217 | switch (halToV4L2PixelFormat(stream->getFormat())) { |
| 1218 | case V4L2_PIX_FMT_YUV420: |
| 1219 | ++num_yuv; |
| 1220 | break; |
| 1221 | case V4L2_PIX_FMT_JPEG: |
| 1222 | ++num_jpeg; |
| 1223 | break; |
| 1224 | default: |
| 1225 | // Note: no supported raw formats at this time. |
| 1226 | HAL_LOGE("Unsupported format for stream %d: %d", i, stream->getFormat()); |
| 1227 | return false; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | if (num_input > mMaxInputStreams || num_raw > mMaxRawOutputStreams || |
| 1233 | num_yuv > mMaxYuvOutputStreams || num_jpeg > mMaxJpegOutputStreams) { |
| 1234 | HAL_LOGE("Invalid stream configuration: %d input, %d RAW, %d YUV, %d JPEG " |
| 1235 | "(max supported: %d input, %d RAW, %d YUV, %d JPEG)", |
| 1236 | mMaxInputStreams, mMaxRawOutputStreams, mMaxYuvOutputStreams, |
| 1237 | mMaxJpegOutputStreams, num_input, num_raw, num_yuv, num_jpeg); |
| 1238 | return false; |
| 1239 | } |
| 1240 | |
| 1241 | // TODO(b/29939583): The above logic should be all that's necessary, |
| 1242 | // but V4L2 doesn't actually support more than 1 stream at a time. So for now, |
| 1243 | // if not all streams are the same format and size, error. Note that this |
| 1244 | // means the HAL is not spec-compliant; the requested streams are technically |
| 1245 | // valid and it is not technically allowed to error once it has reached this |
| 1246 | // point. |
| 1247 | int format = streams[0]->getFormat(); |
| 1248 | uint32_t width = streams[0]->getWidth(); |
| 1249 | uint32_t height = streams[0]->getHeight(); |
| 1250 | for (int i = 1; i < count; ++i) { |
| 1251 | const default_camera_hal::Stream* stream = streams[i]; |
| 1252 | if (stream->getFormat() != format || stream->getWidth() != width || |
| 1253 | stream->getHeight() != height) { |
| 1254 | HAL_LOGE("V4L2 only supports 1 stream configuration at a time " |
| 1255 | "(stream 0 is format %d, width %u, height %u, " |
| 1256 | "stream %d is format %d, width %u, height %u).", |
| 1257 | format, width, height, i, stream->getFormat(), |
| 1258 | stream->getWidth(), stream->getHeight()); |
| 1259 | return false; |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | return true; |
| 1264 | } |
| 1265 | |
| 1266 | int V4L2Camera::setupStream(default_camera_hal::Stream* stream, |
| 1267 | uint32_t* max_buffers) { |
| 1268 | HAL_LOG_ENTER(); |
| 1269 | |
| 1270 | if (stream->getRotation() != CAMERA3_STREAM_ROTATION_0) { |
| 1271 | HAL_LOGE("Rotation %d not supported", stream->getRotation()); |
| 1272 | return -EINVAL; |
| 1273 | } |
| 1274 | |
| 1275 | // Doesn't matter what was requested, we always use dataspace V0_JFIF. |
| 1276 | // Note: according to camera3.h, this isn't allowed, but etalvala@google.com |
| 1277 | // claims it's underdocumented; the implementation lets the HAL overwrite it. |
| 1278 | stream->setDataSpace(HAL_DATASPACE_V0_JFIF); |
| 1279 | |
| 1280 | int ret = setFormat(stream); |
| 1281 | if (ret) { |
| 1282 | return ret; |
| 1283 | } |
| 1284 | |
| 1285 | // Only do buffer setup if we don't know our maxBuffers. |
| 1286 | if (mOutStreamMaxBuffers < 1) { |
| 1287 | ret = setupBuffers(); |
| 1288 | if (ret) { |
| 1289 | return ret; |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | // Sanity check. |
| 1294 | if (mOutStreamMaxBuffers < 1) { |
| 1295 | HAL_LOGE("HAL failed to determine max number of buffers."); |
| 1296 | return -ENODEV; |
| 1297 | } |
| 1298 | *max_buffers = mOutStreamMaxBuffers; |
| 1299 | |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | int V4L2Camera::setFormat(const default_camera_hal::Stream* stream) { |
| 1304 | HAL_LOG_ENTER(); |
| 1305 | |
| 1306 | // Should be checked earlier; sanity check. |
| 1307 | if (stream->isInputType()) { |
| 1308 | HAL_LOGE("Input streams not supported."); |
| 1309 | return -EINVAL; |
| 1310 | } |
| 1311 | |
| 1312 | v4l2_format format; |
| 1313 | memset(&format, 0, sizeof(format)); |
| 1314 | format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1315 | format.fmt.pix.width = stream->getWidth(); |
| 1316 | format.fmt.pix.height = stream->getHeight(); |
| 1317 | format.fmt.pix.pixelformat = halToV4L2PixelFormat(stream->getFormat()); |
| 1318 | // Check to make sure we're not already in the correct format. |
| 1319 | if (mOutStreamFormat == format.fmt.pix.pixelformat && |
| 1320 | mOutStreamWidth == format.fmt.pix.width && |
| 1321 | mOutStreamHeight == format.fmt.pix.height) { |
| 1322 | return 0; |
| 1323 | } |
| 1324 | // Not in the correct format, set our format. |
| 1325 | int ret = ioctlLocked(VIDIOC_S_FMT, &format); |
| 1326 | if (ret < 0) { |
| 1327 | HAL_LOGE("S_FMT failed: %s", strerror(errno)); |
| 1328 | return -ENODEV; |
| 1329 | } |
| 1330 | // Check that the driver actually set to the requested values. |
| 1331 | if (format.fmt.pix.pixelformat != halToV4L2PixelFormat(stream->getFormat()) || |
| 1332 | format.fmt.pix.width != stream->getWidth() || |
| 1333 | format.fmt.pix.height != stream->getHeight()) { |
| 1334 | HAL_LOGE("Device doesn't support desired stream configuration."); |
| 1335 | return -EINVAL; |
| 1336 | } |
| 1337 | // Since our format changed, our maxBuffers may be incorrect. |
| 1338 | mOutStreamMaxBuffers = 0; |
| 1339 | |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | int V4L2Camera::setupBuffers() { |
| 1344 | HAL_LOG_ENTER(); |
| 1345 | |
| 1346 | // "Request" a buffer (since we're using a userspace buffer, this just |
| 1347 | // tells V4L2 to switch into userspace buffer mode). |
| 1348 | v4l2_requestbuffers req_buffers; |
| 1349 | memset(&req_buffers, 0, sizeof(req_buffers)); |
| 1350 | req_buffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1351 | req_buffers.memory = V4L2_MEMORY_USERPTR; |
| 1352 | req_buffers.count = 1; |
| 1353 | if (ioctlLocked(VIDIOC_REQBUFS, &req_buffers) < 0) { |
| 1354 | HAL_LOGE("REQBUFS failed: %s", strerror(errno)); |
| 1355 | return -ENODEV; |
| 1356 | } |
| 1357 | |
| 1358 | // V4L2 will set req_buffers.count to a number of buffers it can handle. |
| 1359 | mOutStreamMaxBuffers = req_buffers.count; |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 1363 | bool V4L2Camera::isValidCaptureSettings(const camera_metadata_t* settings) { |
| 1364 | HAL_LOG_ENTER(); |
| 1365 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1366 | // TODO(b/29335262): reject capture settings this camera isn't capable of. |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 1367 | return true; |
| 1368 | } |
| 1369 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1370 | int V4L2Camera::initCharacteristics() { |
| 1371 | HAL_LOG_ENTER(); |
| 1372 | |
| 1373 | /* Physical characteristics. */ |
| 1374 | // No way to get these in V4L2, so faked. |
| 1375 | // Note: While many of these are primarily informative for post-processing |
| 1376 | // calculations by the app and will potentially cause bad results there, |
| 1377 | // focal length and physical size are actually used in framework |
| 1378 | // calculations (field of view, pixel pitch, etc), so faking them may |
| 1379 | // have unexpected results. |
| 1380 | mAperture = 2.0; // RPi camera v2 is f/2.0. |
| 1381 | mFilterDensity = 0.0; |
| 1382 | mFocalLength = 3.04; // RPi camera v2 is 3.04mm. |
| 1383 | mOrientation = 0; |
| 1384 | mPhysicalSize = {{3.674, 2.760}}; // RPi camera v2 is 3.674 x 2.760 mm. |
| 1385 | |
| 1386 | /* Fixed features. */ |
| 1387 | |
| 1388 | // TODO(b/29394024): query VIDIOC_CROPCAP to get pixel rectangle. |
| 1389 | // Spoofing as 640 x 480 for now. |
| 1390 | mPixelArraySize = {{/*xmin*/0, /*ymin*/0, /*width*/640, /*height*/480}}; |
| 1391 | |
| 1392 | // V4L2 VIDIOC_CROPCAP doesn't give a way to query this; |
| 1393 | // it's driver dependent. For now, assume freeform, and |
| 1394 | // some cameras may just behave badly. |
| 1395 | // TODO(b/29579652): Figure out a way to determine this. |
| 1396 | mCropType = ANDROID_SCALER_CROPPING_TYPE_FREEFORM; |
| 1397 | |
| 1398 | // TODO(b/29394024): query VIDIOC_CROPCAP to get cropping ranges, |
| 1399 | // and VIDIOC_G_CROP to determine if cropping is supported. |
| 1400 | // If the ioctl isn't available (or cropping has non-square pixelaspect), |
| 1401 | // assume no cropping/scaling. |
| 1402 | // May need to try setting some crops to determine what the driver actually |
| 1403 | // supports (including testing center vs freeform). |
| 1404 | mMaxZoom = 1; |
| 1405 | |
| 1406 | // TODO(b/29394024): query V4L2_CID_EXPOSURE_BIAS. |
| 1407 | mAeCompensationRange = {{0, 0}}; |
| 1408 | mAeCompensationStep = {1, 1}; |
| 1409 | |
| 1410 | // TODO(b/29394024): query V4L2_CID_3A_LOCK. |
| 1411 | mAeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE; |
| 1412 | mAwbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE; |
| 1413 | |
| 1414 | // TODO(b/29394024): query V4L2_CID_FLASH_LED_MODE. |
| 1415 | mFlashAvailable = 0; |
| 1416 | |
| 1417 | // TODO(b/29394024): query V4L2_CID_FOCUS_ABSOLUTE for focus range. |
| 1418 | mFocusDistance = 0; // Fixed focus. |
| 1419 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame^] | 1420 | // TODO(b/29939583): V4L2 can only support 1 stream at a time. |
| 1421 | // For now, just reporting minimum allowable for LIMITED devices. |
| 1422 | mMaxRawOutputStreams = 0; |
| 1423 | mMaxYuvOutputStreams = 2; |
| 1424 | mMaxJpegOutputStreams = 1; |
| 1425 | // Reprocessing not supported. |
| 1426 | mMaxInputStreams = 0; |
| 1427 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1428 | /* Features with (potentially) multiple options. */ |
| 1429 | |
| 1430 | // TODO(b/29394024): query V4L2_CID_EXPOSURE_AUTO for ae modes. |
| 1431 | mAeModes.push_back(ANDROID_CONTROL_AE_MODE_ON); |
| 1432 | |
| 1433 | // TODO(b/29394024): query V4L2_CID_POWER_LINE_FREQUENCY. |
| 1434 | // Auto as the default, since it could mean anything, while OFF would |
| 1435 | // require guaranteeing no antibanding happens. |
| 1436 | mAeAntibandingModes.push_back(ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO); |
| 1437 | |
| 1438 | // TODO(b/29394024): query V4L2_CID_FOCUS_AUTO for |
| 1439 | // CONTINUOUS_VIDEO/CONTINUOUS_PICTURE. V4L2_CID_AUTO_FOCUS_START |
| 1440 | // supports what Android thinks of as auto focus (single auto focus). |
| 1441 | // V4L2_CID_AUTO_FOCUS_RANGE allows MACRO. |
| 1442 | mAfModes.push_back(ANDROID_CONTROL_AF_MODE_OFF); |
| 1443 | |
| 1444 | // TODO(b/29394024): query V4L2_CID_AUTO_WHITE_BALANCE, or |
| 1445 | // V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE if available. |
| 1446 | mAwbModes.push_back(ANDROID_CONTROL_AWB_MODE_AUTO); |
| 1447 | |
| 1448 | // TODO(b/29394024): query V4L2_CID_SCENE_MODE. |
| 1449 | mSceneModes.push_back(ANDROID_CONTROL_SCENE_MODE_DISABLED); |
| 1450 | |
| 1451 | mControlModes.push_back(ANDROID_CONTROL_MODE_AUTO); |
| 1452 | if (mSceneModes.size() > 1) { |
| 1453 | // We have some mode other than just DISABLED available. |
| 1454 | mControlModes.push_back(ANDROID_CONTROL_MODE_USE_SCENE_MODE); |
| 1455 | } |
| 1456 | |
| 1457 | // TODO(b/29394024): query V4L2_CID_COLORFX. |
| 1458 | mEffects.push_back(ANDROID_CONTROL_EFFECT_MODE_OFF); |
| 1459 | |
| 1460 | // TODO(b/29394024): query V4L2_CID_FLASH_INDICATOR_INTENSITY. |
| 1461 | // For now, no indicator LED available; nothing to push back. |
| 1462 | // When there is, push back ANDROID_LED_AVAILABLE_LEDS_TRANSMIT. |
| 1463 | |
| 1464 | // TODO(b/29394024): query V4L2_CID_IMAGE_STABILIZATION. |
| 1465 | mOpticalStabilizationModes.push_back( |
| 1466 | ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF); |
| 1467 | mVideoStabilizationModes.push_back( |
| 1468 | ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF); |
| 1469 | |
| 1470 | // Need to support YUV_420_888 and JPEG. |
| 1471 | // TODO(b/29394024): query VIDIOC_ENUM_FMT. |
| 1472 | std::vector<uint32_t> formats = {{V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420}}; |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1473 | // We want to find the smallest maximum frame duration amongst formats. |
| 1474 | mMaxFrameDuration = std::numeric_limits<int64_t>::max(); |
| 1475 | int64_t min_yuv_frame_duration = std::numeric_limits<int64_t>::max(); |
| 1476 | for (auto format : formats) { |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame^] | 1477 | int32_t hal_format = V4L2ToHalPixelFormat(format); |
| 1478 | if (hal_format < 0) { |
| 1479 | // Unrecognized/unused format. Skip it. |
| 1480 | continue; |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1481 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 1482 | // TODO(b/29394024): query VIDIOC_ENUM_FRAMESIZES. |
| 1483 | // For now, just 640x480. |
| 1484 | ArrayVector<int32_t, 2> frame_sizes; |
| 1485 | frame_sizes.push_back({{640, 480}}); |
| 1486 | size_t num_frame_sizes = frame_sizes.num_arrays(); |
| 1487 | for (size_t i = 0; i < num_frame_sizes; ++i) { |
| 1488 | const int32_t* frame_size = frame_sizes[i]; |
| 1489 | mStreamConfigs.push_back({{hal_format, frame_size[0], frame_size[1], |
| 1490 | ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT}}); |
| 1491 | |
| 1492 | // TODO(b/29394024): query VIDIOC_ENUM_FRAMEINTERVALS. |
| 1493 | // For now, using the emulator max value of .3 sec. |
| 1494 | int64_t max_frame_duration = 300000000; |
| 1495 | // For now, min value of 30 fps = 1/30 spf ~= 33333333 ns. |
| 1496 | // For whatever reason the goldfish/qcom cameras report this as |
| 1497 | // 33331760, so copying that. |
| 1498 | int64_t min_frame_duration = 33331760; |
| 1499 | |
| 1500 | mMinFrameDurations.push_back( |
| 1501 | {{hal_format, frame_size[0], frame_size[1], min_frame_duration}}); |
| 1502 | |
| 1503 | // In theory max frame duration (min frame rate) should be consistent |
| 1504 | // between all formats, but we check and only advertise the smallest |
| 1505 | // available max duration just in case. |
| 1506 | if (max_frame_duration < mMaxFrameDuration) { |
| 1507 | mMaxFrameDuration = max_frame_duration; |
| 1508 | } |
| 1509 | |
| 1510 | // We only care about min frame duration (max frame rate) for YUV. |
| 1511 | if (hal_format == HAL_PIXEL_FORMAT_YCbCr_420_888 && |
| 1512 | min_frame_duration < min_yuv_frame_duration) { |
| 1513 | min_yuv_frame_duration = min_frame_duration; |
| 1514 | } |
| 1515 | |
| 1516 | // Usually 0 for non-jpeg, non-zero for JPEG. |
| 1517 | // Randomly choosing absurd 1 sec for JPEG. Unsure what this breaks. |
| 1518 | int64_t stall_duration = 0; |
| 1519 | if (hal_format == HAL_PIXEL_FORMAT_BLOB) { |
| 1520 | stall_duration = 1000000000; |
| 1521 | } |
| 1522 | mStallDurations.push_back( |
| 1523 | {{hal_format, frame_size[0], frame_size[1], stall_duration}}); |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | // This should be at minimum {mi, ma}, {ma, ma} where mi and ma |
| 1528 | // are min and max frame rates for YUV_420_888. Min should be at most 15. |
| 1529 | // Convert from frame durations measured in ns. |
| 1530 | int32_t min_yuv_fps = 1000000000 / mMaxFrameDuration; |
| 1531 | if (min_yuv_fps > 15) { |
| 1532 | return -ENODEV; |
| 1533 | } |
| 1534 | int32_t max_yuv_fps = 1000000000 / min_yuv_frame_duration; |
| 1535 | mFpsRanges.push_back({{min_yuv_fps, max_yuv_fps}}); |
| 1536 | mFpsRanges.push_back({{max_yuv_fps, max_yuv_fps}}); |
| 1537 | // Always advertise {30, 30} if max is even higher, |
| 1538 | // since this is what the default video requests use. |
| 1539 | if (max_yuv_fps > 30) { |
| 1540 | mFpsRanges.push_back({{30, 30}}); |
| 1541 | } |
| 1542 | |
| 1543 | mCharacteristicsInitialized = true; |
| 1544 | return 0; |
| 1545 | } |
| 1546 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame^] | 1547 | int V4L2Camera::V4L2ToHalPixelFormat(uint32_t v4l2_format) { |
| 1548 | // Translate V4L2 format to HAL format. |
| 1549 | int hal_format = -1; |
| 1550 | switch (v4l2_format) { |
| 1551 | case V4L2_PIX_FMT_JPEG: |
| 1552 | hal_format = HAL_PIXEL_FORMAT_BLOB; |
| 1553 | break; |
| 1554 | case V4L2_PIX_FMT_YUV420: |
| 1555 | hal_format = HAL_PIXEL_FORMAT_YCbCr_420_888; |
| 1556 | break; |
| 1557 | default: |
| 1558 | // Unrecognized format. |
| 1559 | break; |
| 1560 | } |
| 1561 | return hal_format; |
| 1562 | } |
| 1563 | |
| 1564 | uint32_t V4L2Camera::halToV4L2PixelFormat(int hal_format) { |
| 1565 | // Translate HAL format to V4L2 format. |
| 1566 | uint32_t v4l2_format = 0; |
| 1567 | switch (hal_format) { |
| 1568 | case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: // fall-through. |
| 1569 | case HAL_PIXEL_FORMAT_YCbCr_420_888: |
| 1570 | v4l2_format = V4L2_PIX_FMT_YUV420; |
| 1571 | break; |
| 1572 | case HAL_PIXEL_FORMAT_BLOB: |
| 1573 | v4l2_format = V4L2_PIX_FMT_JPEG; |
| 1574 | break; |
| 1575 | default: |
| 1576 | // Unrecognized format. |
| 1577 | break; |
| 1578 | } |
| 1579 | return v4l2_format; |
| 1580 | } |
| 1581 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 1582 | } // namespace v4l2_camera_hal |