Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -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 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 17 | #include "v4l2_wrapper.h" |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 18 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 20 | #include <array> |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 21 | #include <limits> |
| 22 | #include <vector> |
| 23 | |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 24 | #include <fcntl.h> |
| 25 | #include <linux/videodev2.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | |
| 29 | #include <mutex> |
| 30 | |
| 31 | #include <nativehelper/ScopedFd.h> |
| 32 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 33 | #include "common.h" |
| 34 | #include "stream.h" |
| 35 | #include "stream_format.h" |
| 36 | #include "v4l2_gralloc.h" |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 37 | |
| 38 | namespace v4l2_camera_hal { |
| 39 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 40 | const int32_t kStandardSizes[][2] = { |
| 41 | {1920, 1080}, {1280, 720}, {640, 480}, {320, 240}}; |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 42 | |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 43 | V4L2Wrapper* V4L2Wrapper::NewV4L2Wrapper(const std::string device_path) { |
| 44 | HAL_LOG_ENTER(); |
| 45 | |
| 46 | std::unique_ptr<V4L2Gralloc> gralloc(V4L2Gralloc::NewV4L2Gralloc()); |
| 47 | if (!gralloc) { |
| 48 | HAL_LOGE("Failed to initialize gralloc helper."); |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | return new V4L2Wrapper(device_path, std::move(gralloc)); |
| 53 | } |
| 54 | |
| 55 | V4L2Wrapper::V4L2Wrapper(const std::string device_path, |
| 56 | std::unique_ptr<V4L2Gralloc> gralloc) |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 57 | : device_path_(std::move(device_path)), |
| 58 | gralloc_(std::move(gralloc)), |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 59 | max_buffers_(0), |
| 60 | connection_count_(0) { |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 61 | HAL_LOG_ENTER(); |
| 62 | } |
| 63 | |
Ari Hausman-Cohen | 5d75323 | 2016-08-10 14:27:36 -0700 | [diff] [blame] | 64 | V4L2Wrapper::~V4L2Wrapper() { |
| 65 | HAL_LOG_ENTER(); |
| 66 | } |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 67 | |
| 68 | int V4L2Wrapper::Connect() { |
| 69 | HAL_LOG_ENTER(); |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 70 | std::lock_guard<std::mutex> lock(connection_lock_); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 71 | |
| 72 | if (connected()) { |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 73 | HAL_LOGV("Camera device %s is already connected.", device_path_.c_str()); |
| 74 | ++connection_count_; |
| 75 | return 0; |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int fd = TEMP_FAILURE_RETRY(open(device_path_.c_str(), O_RDWR)); |
| 79 | if (fd < 0) { |
| 80 | HAL_LOGE("failed to open %s (%s)", device_path_.c_str(), strerror(errno)); |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 81 | return -ENODEV; |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 82 | } |
| 83 | device_fd_.reset(fd); |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 84 | ++connection_count_; |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 85 | |
| 86 | // Check if this connection has the extended control query capability. |
| 87 | v4l2_query_ext_ctrl query; |
| 88 | query.id = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND; |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 89 | extended_query_supported_ = (IoctlLocked(VIDIOC_QUERY_EXT_CTRL, &query) == 0); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 90 | |
| 91 | // TODO(b/29185945): confirm this is a supported device. |
| 92 | // This is checked by the HAL, but the device at device_path_ may |
| 93 | // not be the same one that was there when the HAL was loaded. |
| 94 | // (Alternatively, better hotplugging support may make this unecessary |
| 95 | // by disabling cameras that get disconnected and checking newly connected |
| 96 | // cameras, so Connect() is never called on an unsupported camera) |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | void V4L2Wrapper::Disconnect() { |
| 101 | HAL_LOG_ENTER(); |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 102 | std::lock_guard<std::mutex> lock(connection_lock_); |
| 103 | |
| 104 | if (connection_count_ == 0) { |
| 105 | // Not connected. |
| 106 | HAL_LOGE("Camera device %s is not connected, cannot disconnect.", |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 107 | device_path_.c_str()); |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
| 111 | --connection_count_; |
| 112 | if (connection_count_ > 0) { |
| 113 | HAL_LOGV("Disconnected from camera device %s. %d connections remain.", |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 114 | device_path_.c_str()); |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 115 | return; |
| 116 | } |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 117 | |
| 118 | device_fd_.reset(); // Includes close(). |
| 119 | format_.reset(); |
| 120 | max_buffers_ = 0; |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 121 | // Closing the device releases all queued buffers back to the user. |
| 122 | gralloc_->unlockAllBuffers(); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // Helper function. Should be used instead of ioctl throughout this class. |
| 126 | template <typename T> |
| 127 | int V4L2Wrapper::IoctlLocked(int request, T data) { |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 128 | // Potentially called so many times logging entry is a bad idea. |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 129 | std::lock_guard<std::mutex> lock(device_lock_); |
| 130 | |
| 131 | if (!connected()) { |
| 132 | HAL_LOGE("Device %s not connected.", device_path_.c_str()); |
| 133 | return -ENODEV; |
| 134 | } |
| 135 | return TEMP_FAILURE_RETRY(ioctl(device_fd_.get(), request, data)); |
| 136 | } |
| 137 | |
| 138 | int V4L2Wrapper::StreamOn() { |
| 139 | HAL_LOG_ENTER(); |
| 140 | |
| 141 | if (!format_) { |
| 142 | HAL_LOGE("Stream format must be set before turning on stream."); |
| 143 | return -EINVAL; |
| 144 | } |
| 145 | |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 146 | int32_t type = format_->type(); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 147 | if (IoctlLocked(VIDIOC_STREAMON, &type) < 0) { |
| 148 | HAL_LOGE("STREAMON fails: %s", strerror(errno)); |
| 149 | return -ENODEV; |
| 150 | } |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | int V4L2Wrapper::StreamOff() { |
| 156 | HAL_LOG_ENTER(); |
| 157 | |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 158 | if (!format_) { |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 159 | // Can't have turned on the stream wihtout format being set, |
| 160 | // so nothing to turn off here. |
| 161 | return 0; |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 164 | int32_t type = format_->type(); |
| 165 | int res = IoctlLocked(VIDIOC_STREAMOFF, &type); |
| 166 | // Calling STREAMOFF releases all queued buffers back to the user. |
| 167 | int gralloc_res = gralloc_->unlockAllBuffers(); |
| 168 | if (res < 0) { |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 169 | HAL_LOGE("STREAMOFF fails: %s", strerror(errno)); |
| 170 | return -ENODEV; |
| 171 | } |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 172 | if (gralloc_res < 0) { |
| 173 | HAL_LOGE("Failed to unlock all buffers after turning stream off."); |
| 174 | return gralloc_res; |
| 175 | } |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | int V4L2Wrapper::QueryControl(uint32_t control_id, |
| 181 | v4l2_query_ext_ctrl* result) { |
| 182 | HAL_LOG_ENTER(); |
| 183 | int res; |
| 184 | |
| 185 | memset(result, 0, sizeof(*result)); |
| 186 | |
| 187 | if (extended_query_supported_) { |
| 188 | result->id = control_id; |
| 189 | res = IoctlLocked(VIDIOC_QUERY_EXT_CTRL, result); |
| 190 | // Assuming the operation was supported (not ENOTTY), no more to do. |
| 191 | if (errno != ENOTTY) { |
| 192 | if (res) { |
| 193 | HAL_LOGE("QUERY_EXT_CTRL fails: %s", strerror(errno)); |
| 194 | return -ENODEV; |
| 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Extended control querying not supported, fall back to basic control query. |
| 201 | v4l2_queryctrl query; |
| 202 | query.id = control_id; |
| 203 | if (IoctlLocked(VIDIOC_QUERYCTRL, &query)) { |
| 204 | HAL_LOGE("QUERYCTRL fails: %s", strerror(errno)); |
| 205 | return -ENODEV; |
| 206 | } |
| 207 | |
| 208 | // Convert the basic result to the extended result. |
| 209 | result->id = query.id; |
| 210 | result->type = query.type; |
| 211 | memcpy(result->name, query.name, sizeof(query.name)); |
| 212 | result->minimum = query.minimum; |
| 213 | if (query.type == V4L2_CTRL_TYPE_BITMASK) { |
| 214 | // According to the V4L2 documentation, when type is BITMASK, |
| 215 | // max and default should be interpreted as __u32. Practically, |
| 216 | // this means the conversion from 32 bit to 64 will pad with 0s not 1s. |
| 217 | result->maximum = static_cast<uint32_t>(query.maximum); |
| 218 | result->default_value = static_cast<uint32_t>(query.default_value); |
| 219 | } else { |
| 220 | result->maximum = query.maximum; |
| 221 | result->default_value = query.default_value; |
| 222 | } |
| 223 | result->step = static_cast<uint32_t>(query.step); |
| 224 | result->flags = query.flags; |
| 225 | result->elems = 1; |
| 226 | switch (result->type) { |
| 227 | case V4L2_CTRL_TYPE_INTEGER64: |
| 228 | result->elem_size = sizeof(int64_t); |
| 229 | break; |
| 230 | case V4L2_CTRL_TYPE_STRING: |
| 231 | result->elem_size = result->maximum + 1; |
| 232 | break; |
| 233 | default: |
| 234 | result->elem_size = sizeof(int32_t); |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | int V4L2Wrapper::GetControl(uint32_t control_id, int32_t* value) { |
| 242 | HAL_LOG_ENTER(); |
| 243 | |
Ari Hausman-Cohen | 7a1fba6 | 2016-08-10 11:31:04 -0700 | [diff] [blame] | 244 | // For extended controls (any control class other than "user"), |
| 245 | // G_EXT_CTRL must be used instead of G_CTRL. |
| 246 | if (V4L2_CTRL_ID2CLASS(control_id) != V4L2_CTRL_CLASS_USER) { |
| 247 | v4l2_ext_control control; |
| 248 | v4l2_ext_controls controls; |
| 249 | memset(&control, 0, sizeof(control)); |
| 250 | memset(&controls, 0, sizeof(controls)); |
| 251 | |
| 252 | control.id = control_id; |
| 253 | controls.ctrl_class = V4L2_CTRL_ID2CLASS(control_id); |
| 254 | controls.count = 1; |
| 255 | controls.controls = &control; |
| 256 | |
| 257 | if (IoctlLocked(VIDIOC_G_EXT_CTRLS, &controls) < 0) { |
| 258 | HAL_LOGE("G_EXT_CTRLS fails: %s", strerror(errno)); |
| 259 | return -ENODEV; |
| 260 | } |
| 261 | *value = control.value; |
| 262 | } else { |
| 263 | v4l2_control control{control_id, 0}; |
| 264 | if (IoctlLocked(VIDIOC_G_CTRL, &control) < 0) { |
| 265 | HAL_LOGE("G_CTRL fails: %s", strerror(errno)); |
| 266 | return -ENODEV; |
| 267 | } |
| 268 | *value = control.value; |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 269 | } |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
Ari Hausman-Cohen | 5d75323 | 2016-08-10 14:27:36 -0700 | [diff] [blame] | 273 | int V4L2Wrapper::SetControl(uint32_t control_id, |
| 274 | int32_t desired, |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 275 | int32_t* result) { |
| 276 | HAL_LOG_ENTER(); |
Ari Hausman-Cohen | 7a1fba6 | 2016-08-10 11:31:04 -0700 | [diff] [blame] | 277 | int32_t result_value = 0; |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 278 | |
Ari Hausman-Cohen | 99f3ea0 | 2016-08-02 10:47:07 -0700 | [diff] [blame] | 279 | // TODO(b/29334616): When async, this may need to check if the stream |
| 280 | // is on, and if so, lock it off while setting format. Need to look |
| 281 | // into if V4L2 supports adjusting controls while the stream is on. |
| 282 | |
Ari Hausman-Cohen | 7a1fba6 | 2016-08-10 11:31:04 -0700 | [diff] [blame] | 283 | // For extended controls (any control class other than "user"), |
| 284 | // S_EXT_CTRL must be used instead of S_CTRL. |
| 285 | if (V4L2_CTRL_ID2CLASS(control_id) != V4L2_CTRL_CLASS_USER) { |
| 286 | v4l2_ext_control control; |
| 287 | v4l2_ext_controls controls; |
| 288 | memset(&control, 0, sizeof(control)); |
| 289 | memset(&controls, 0, sizeof(controls)); |
| 290 | |
| 291 | control.id = control_id; |
| 292 | control.value = desired; |
| 293 | controls.ctrl_class = V4L2_CTRL_ID2CLASS(control_id); |
| 294 | controls.count = 1; |
| 295 | controls.controls = &control; |
| 296 | |
| 297 | if (IoctlLocked(VIDIOC_S_EXT_CTRLS, &controls) < 0) { |
| 298 | HAL_LOGE("S_EXT_CTRLS fails: %s", strerror(errno)); |
| 299 | return -ENODEV; |
| 300 | } |
| 301 | result_value = control.value; |
| 302 | } else { |
| 303 | v4l2_control control{control_id, desired}; |
| 304 | if (IoctlLocked(VIDIOC_S_CTRL, &control) < 0) { |
| 305 | HAL_LOGE("S_CTRL fails: %s", strerror(errno)); |
| 306 | return -ENODEV; |
| 307 | } |
| 308 | result_value = control.value; |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 309 | } |
Ari Hausman-Cohen | 7a1fba6 | 2016-08-10 11:31:04 -0700 | [diff] [blame] | 310 | |
Ari Hausman-Cohen | 99f3ea0 | 2016-08-02 10:47:07 -0700 | [diff] [blame] | 311 | // If the caller wants to know the result, pass it back. |
| 312 | if (result != nullptr) { |
Ari Hausman-Cohen | 7a1fba6 | 2016-08-10 11:31:04 -0700 | [diff] [blame] | 313 | *result = result_value; |
Ari Hausman-Cohen | 99f3ea0 | 2016-08-02 10:47:07 -0700 | [diff] [blame] | 314 | } |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 318 | int V4L2Wrapper::GetFormats(std::set<uint32_t>* v4l2_formats) { |
| 319 | HAL_LOG_ENTER(); |
| 320 | |
| 321 | v4l2_fmtdesc format_query; |
| 322 | memset(&format_query, 0, sizeof(format_query)); |
| 323 | // TODO(b/30000211): multiplanar support. |
| 324 | format_query.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 325 | while (IoctlLocked(VIDIOC_ENUM_FMT, &format_query) >= 0) { |
| 326 | v4l2_formats->insert(format_query.pixelformat); |
| 327 | ++format_query.index; |
| 328 | } |
| 329 | |
| 330 | if (errno != EINVAL) { |
Ari Hausman-Cohen | 5d75323 | 2016-08-10 14:27:36 -0700 | [diff] [blame] | 331 | HAL_LOGE( |
| 332 | "ENUM_FMT fails at index %d: %s", format_query.index, strerror(errno)); |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 333 | return -ENODEV; |
| 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | int V4L2Wrapper::GetFormatFrameSizes(uint32_t v4l2_format, |
| 339 | std::set<std::array<int32_t, 2>>* sizes) { |
| 340 | HAL_LOG_ENTER(); |
| 341 | |
| 342 | v4l2_frmsizeenum size_query; |
| 343 | memset(&size_query, 0, sizeof(size_query)); |
| 344 | size_query.pixel_format = v4l2_format; |
| 345 | if (IoctlLocked(VIDIOC_ENUM_FRAMESIZES, &size_query) < 0) { |
| 346 | HAL_LOGE("ENUM_FRAMESIZES failed: %s", strerror(errno)); |
| 347 | return -ENODEV; |
| 348 | } |
| 349 | if (size_query.type == V4L2_FRMSIZE_TYPE_DISCRETE) { |
| 350 | // Discrete: enumerate all sizes using VIDIOC_ENUM_FRAMESIZES. |
| 351 | // Assuming that a driver with discrete frame sizes has a reasonable number |
| 352 | // of them. |
| 353 | do { |
| 354 | sizes->insert({{{static_cast<int32_t>(size_query.discrete.width), |
| 355 | static_cast<int32_t>(size_query.discrete.height)}}}); |
| 356 | ++size_query.index; |
| 357 | } while (IoctlLocked(VIDIOC_ENUM_FRAMESIZES, &size_query) >= 0); |
| 358 | if (errno != EINVAL) { |
Ari Hausman-Cohen | 5d75323 | 2016-08-10 14:27:36 -0700 | [diff] [blame] | 359 | HAL_LOGE("ENUM_FRAMESIZES fails at index %d: %s", |
| 360 | size_query.index, |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 361 | strerror(errno)); |
| 362 | return -ENODEV; |
| 363 | } |
| 364 | } else { |
| 365 | // Continuous/Step-wise: based on the stepwise struct returned by the query. |
| 366 | // Fully listing all possible sizes, with large enough range/small enough |
| 367 | // step size, may produce far too many potential sizes. Instead, find the |
| 368 | // closest to a set of standard sizes plus largest possible. |
| 369 | sizes->insert({{{static_cast<int32_t>(size_query.stepwise.max_width), |
| 370 | static_cast<int32_t>(size_query.stepwise.max_height)}}}); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 371 | for (const auto size : kStandardSizes) { |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 372 | // Find the closest size, rounding up. |
| 373 | uint32_t desired_width = size[0]; |
| 374 | uint32_t desired_height = size[1]; |
| 375 | if (desired_width < size_query.stepwise.min_width || |
| 376 | desired_height < size_query.stepwise.min_height) { |
| 377 | HAL_LOGV("Standard size %u x %u is too small for format %d", |
Ari Hausman-Cohen | 5d75323 | 2016-08-10 14:27:36 -0700 | [diff] [blame] | 378 | desired_width, |
| 379 | desired_height, |
| 380 | v4l2_format); |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 381 | continue; |
| 382 | } else if (desired_width > size_query.stepwise.max_width && |
| 383 | desired_height > size_query.stepwise.max_height) { |
| 384 | HAL_LOGV("Standard size %u x %u is too big for format %d", |
Ari Hausman-Cohen | 5d75323 | 2016-08-10 14:27:36 -0700 | [diff] [blame] | 385 | desired_width, |
| 386 | desired_height, |
| 387 | v4l2_format); |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 388 | continue; |
| 389 | } |
| 390 | |
| 391 | // Round up. |
| 392 | uint32_t width_steps = (desired_width - size_query.stepwise.min_width + |
| 393 | size_query.stepwise.step_width - 1) / |
| 394 | size_query.stepwise.step_width; |
| 395 | uint32_t height_steps = (desired_height - size_query.stepwise.min_height + |
| 396 | size_query.stepwise.step_height - 1) / |
| 397 | size_query.stepwise.step_height; |
| 398 | sizes->insert( |
| 399 | {{{static_cast<int32_t>(size_query.stepwise.min_width + |
| 400 | width_steps * size_query.stepwise.step_width), |
| 401 | static_cast<int32_t>(size_query.stepwise.min_height + |
| 402 | height_steps * |
| 403 | size_query.stepwise.step_height)}}}); |
| 404 | } |
| 405 | } |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | // Converts a v4l2_fract with units of seconds to an int64_t with units of ns. |
| 410 | inline int64_t FractToNs(const v4l2_fract& fract) { |
| 411 | return (1000000000LL * fract.numerator) / fract.denominator; |
| 412 | } |
| 413 | |
| 414 | int V4L2Wrapper::GetFormatFrameDurationRange( |
Ari Hausman-Cohen | 5d75323 | 2016-08-10 14:27:36 -0700 | [diff] [blame] | 415 | uint32_t v4l2_format, |
| 416 | const std::array<int32_t, 2>& size, |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 417 | std::array<int64_t, 2>* duration_range) { |
| 418 | // Potentially called so many times logging entry is a bad idea. |
| 419 | |
| 420 | v4l2_frmivalenum duration_query; |
| 421 | memset(&duration_query, 0, sizeof(duration_query)); |
| 422 | duration_query.pixel_format = v4l2_format; |
| 423 | duration_query.width = size[0]; |
| 424 | duration_query.height = size[1]; |
| 425 | if (IoctlLocked(VIDIOC_ENUM_FRAMEINTERVALS, &duration_query) < 0) { |
| 426 | HAL_LOGE("ENUM_FRAMEINTERVALS failed: %s", strerror(errno)); |
| 427 | return -ENODEV; |
| 428 | } |
| 429 | |
| 430 | int64_t min = std::numeric_limits<int64_t>::max(); |
| 431 | int64_t max = std::numeric_limits<int64_t>::min(); |
| 432 | if (duration_query.type == V4L2_FRMSIZE_TYPE_DISCRETE) { |
| 433 | // Discrete: enumerate all durations using VIDIOC_ENUM_FRAMEINTERVALS. |
| 434 | do { |
| 435 | min = std::min(min, FractToNs(duration_query.discrete)); |
| 436 | max = std::max(max, FractToNs(duration_query.discrete)); |
| 437 | ++duration_query.index; |
| 438 | } while (IoctlLocked(VIDIOC_ENUM_FRAMEINTERVALS, &duration_query) >= 0); |
| 439 | if (errno != EINVAL) { |
| 440 | HAL_LOGE("ENUM_FRAMEINTERVALS fails at index %d: %s", |
Ari Hausman-Cohen | 5d75323 | 2016-08-10 14:27:36 -0700 | [diff] [blame] | 441 | duration_query.index, |
| 442 | strerror(errno)); |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 443 | return -ENODEV; |
| 444 | } |
| 445 | } else { |
| 446 | // Continuous/Step-wise: simply convert the given min and max. |
| 447 | min = FractToNs(duration_query.stepwise.min); |
| 448 | max = FractToNs(duration_query.stepwise.max); |
| 449 | } |
| 450 | (*duration_range)[0] = min; |
| 451 | (*duration_range)[1] = max; |
| 452 | return 0; |
| 453 | } |
| 454 | |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 455 | int V4L2Wrapper::SetFormat(const default_camera_hal::Stream& stream, |
| 456 | uint32_t* result_max_buffers) { |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 457 | HAL_LOG_ENTER(); |
| 458 | |
| 459 | // Should be checked earlier; sanity check. |
| 460 | if (stream.isInputType()) { |
| 461 | HAL_LOGE("Input streams not supported."); |
| 462 | return -EINVAL; |
| 463 | } |
| 464 | |
| 465 | StreamFormat desired_format(stream); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 466 | if (format_ && desired_format == *format_) { |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 467 | HAL_LOGV("Already in correct format, skipping format setting."); |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | // Not in the correct format, set our format. |
| 472 | v4l2_format new_format; |
| 473 | desired_format.FillFormatRequest(&new_format); |
| 474 | // TODO(b/29334616): When async, this will need to check if the stream |
| 475 | // is on, and if so, lock it off while setting format. |
| 476 | if (IoctlLocked(VIDIOC_S_FMT, &new_format) < 0) { |
| 477 | HAL_LOGE("S_FMT failed: %s", strerror(errno)); |
| 478 | return -ENODEV; |
| 479 | } |
| 480 | |
| 481 | // Check that the driver actually set to the requested values. |
| 482 | if (desired_format != new_format) { |
| 483 | HAL_LOGE("Device doesn't support desired stream configuration."); |
| 484 | return -EINVAL; |
| 485 | } |
| 486 | |
| 487 | // Keep track of our new format. |
| 488 | format_.reset(new StreamFormat(new_format)); |
| 489 | |
| 490 | // Format changed, setup new buffers. |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 491 | int res = SetupBuffers(); |
| 492 | if (res) { |
| 493 | HAL_LOGE("Failed to set up buffers for new format."); |
| 494 | return res; |
| 495 | } |
| 496 | *result_max_buffers = max_buffers_; |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | int V4L2Wrapper::SetupBuffers() { |
| 501 | HAL_LOG_ENTER(); |
| 502 | |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 503 | if (!format_) { |
| 504 | HAL_LOGE("Stream format must be set before setting up buffers."); |
| 505 | return -ENODEV; |
| 506 | } |
| 507 | |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 508 | // "Request" a buffer (since we're using a userspace buffer, this just |
| 509 | // tells V4L2 to switch into userspace buffer mode). |
| 510 | v4l2_requestbuffers req_buffers; |
| 511 | memset(&req_buffers, 0, sizeof(req_buffers)); |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 512 | req_buffers.type = format_->type(); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 513 | req_buffers.memory = V4L2_MEMORY_USERPTR; |
| 514 | req_buffers.count = 1; |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 515 | |
| 516 | int res = IoctlLocked(VIDIOC_REQBUFS, &req_buffers); |
| 517 | // Calling REQBUFS releases all queued buffers back to the user. |
| 518 | int gralloc_res = gralloc_->unlockAllBuffers(); |
| 519 | if (res < 0) { |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 520 | HAL_LOGE("REQBUFS failed: %s", strerror(errno)); |
| 521 | return -ENODEV; |
| 522 | } |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 523 | if (gralloc_res < 0) { |
| 524 | HAL_LOGE("Failed to unlock all buffers when setting up new buffers."); |
| 525 | return gralloc_res; |
| 526 | } |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 527 | |
| 528 | // V4L2 will set req_buffers.count to a number of buffers it can handle. |
| 529 | max_buffers_ = req_buffers.count; |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 530 | // Sanity check. |
| 531 | if (max_buffers_ < 1) { |
| 532 | HAL_LOGE("REQBUFS claims it can't handle any buffers."); |
| 533 | return -ENODEV; |
| 534 | } |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 535 | return 0; |
| 536 | } |
| 537 | |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 538 | int V4L2Wrapper::EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer) { |
| 539 | HAL_LOG_ENTER(); |
| 540 | |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 541 | if (!format_) { |
| 542 | HAL_LOGE("Stream format must be set before enqueuing buffers."); |
| 543 | return -ENODEV; |
| 544 | } |
| 545 | |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 546 | // Set up a v4l2 buffer struct. |
| 547 | v4l2_buffer device_buffer; |
| 548 | memset(&device_buffer, 0, sizeof(device_buffer)); |
| 549 | device_buffer.type = format_->type(); |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 550 | // TODO(b/29334616): when this is async, actually limit the number |
| 551 | // of buffers used to the known max, and set this according to the |
| 552 | // queue length. |
| 553 | device_buffer.index = 0; |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 554 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 555 | // Use QUERYBUF to ensure our buffer/device is in good shape, |
| 556 | // and fill out remaining fields. |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 557 | if (IoctlLocked(VIDIOC_QUERYBUF, &device_buffer) < 0) { |
| 558 | HAL_LOGE("QUERYBUF fails: %s", strerror(errno)); |
| 559 | return -ENODEV; |
| 560 | } |
| 561 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 562 | // Lock the buffer for writing (fills in the user pointer field). |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 563 | int res = |
| 564 | gralloc_->lock(camera_buffer, format_->bytes_per_line(), &device_buffer); |
| 565 | if (res) { |
| 566 | HAL_LOGE("Gralloc failed to lock buffer."); |
| 567 | return res; |
| 568 | } |
| 569 | if (IoctlLocked(VIDIOC_QBUF, &device_buffer) < 0) { |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 570 | HAL_LOGE("QBUF fails: %s", strerror(errno)); |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 571 | gralloc_->unlock(&device_buffer); |
| 572 | return -ENODEV; |
| 573 | } |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | int V4L2Wrapper::DequeueBuffer(v4l2_buffer* buffer) { |
| 579 | HAL_LOG_ENTER(); |
| 580 | |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 581 | if (!format_) { |
| 582 | HAL_LOGE("Stream format must be set before dequeueing buffers."); |
| 583 | return -ENODEV; |
| 584 | } |
| 585 | |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 586 | memset(buffer, 0, sizeof(*buffer)); |
| 587 | buffer->type = format_->type(); |
| 588 | buffer->memory = V4L2_MEMORY_USERPTR; |
| 589 | if (IoctlLocked(VIDIOC_DQBUF, buffer) < 0) { |
| 590 | HAL_LOGE("DQBUF fails: %s", strerror(errno)); |
| 591 | return -ENODEV; |
| 592 | } |
| 593 | |
| 594 | // Now that we're done painting the buffer, we can unlock it. |
| 595 | int res = gralloc_->unlock(buffer); |
| 596 | if (res) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 597 | HAL_LOGE("Gralloc failed to unlock buffer after dequeueing."); |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 598 | return res; |
| 599 | } |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 604 | } // namespace v4l2_camera_hal |