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