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