blob: 7d8e38c5d592bf505fadacb41160e26e8dd31c4e [file] [log] [blame]
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -07001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070017#include "v4l2_wrapper.h"
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070018
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070019#include <algorithm>
20#include <limits>
21#include <vector>
22
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070023#include <fcntl.h>
24#include <linux/videodev2.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27
28#include <mutex>
29
30#include <nativehelper/ScopedFd.h>
31
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070032#include "common.h"
33#include "stream.h"
34#include "stream_format.h"
35#include "v4l2_gralloc.h"
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070036
37namespace v4l2_camera_hal {
38
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070039const std::vector<std::array<int32_t, 2>> kStandardSizes(
40 {{{1920, 1080}}, {{1280, 720}}, {{640, 480}}, {{320, 240}}});
41
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070042V4L2Wrapper* V4L2Wrapper::NewV4L2Wrapper(const std::string device_path) {
43 HAL_LOG_ENTER();
44
45 std::unique_ptr<V4L2Gralloc> gralloc(V4L2Gralloc::NewV4L2Gralloc());
46 if (!gralloc) {
47 HAL_LOGE("Failed to initialize gralloc helper.");
48 return nullptr;
49 }
50
51 return new V4L2Wrapper(device_path, std::move(gralloc));
52}
53
54V4L2Wrapper::V4L2Wrapper(const std::string device_path,
55 std::unique_ptr<V4L2Gralloc> gralloc)
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070056 : device_path_(std::move(device_path)),
57 gralloc_(std::move(gralloc)),
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070058 max_buffers_(0),
59 connection_count_(0) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070060 HAL_LOG_ENTER();
61}
62
63V4L2Wrapper::~V4L2Wrapper() { HAL_LOG_ENTER(); }
64
65int V4L2Wrapper::Connect() {
66 HAL_LOG_ENTER();
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070067 std::lock_guard<std::mutex> lock(connection_lock_);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070068
69 if (connected()) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070070 HAL_LOGV("Camera device %s is already connected.", device_path_.c_str());
71 ++connection_count_;
72 return 0;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070073 }
74
75 int fd = TEMP_FAILURE_RETRY(open(device_path_.c_str(), O_RDWR));
76 if (fd < 0) {
77 HAL_LOGE("failed to open %s (%s)", device_path_.c_str(), strerror(errno));
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070078 return -ENODEV;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070079 }
80 device_fd_.reset(fd);
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070081 ++connection_count_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070082
83 // Check if this connection has the extended control query capability.
84 v4l2_query_ext_ctrl query;
85 query.id = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070086 extended_query_supported_ = (IoctlLocked(VIDIOC_QUERY_EXT_CTRL, &query) == 0);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070087
88 // TODO(b/29185945): confirm this is a supported device.
89 // This is checked by the HAL, but the device at device_path_ may
90 // not be the same one that was there when the HAL was loaded.
91 // (Alternatively, better hotplugging support may make this unecessary
92 // by disabling cameras that get disconnected and checking newly connected
93 // cameras, so Connect() is never called on an unsupported camera)
94 return 0;
95}
96
97void V4L2Wrapper::Disconnect() {
98 HAL_LOG_ENTER();
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070099 std::lock_guard<std::mutex> lock(connection_lock_);
100
101 if (connection_count_ == 0) {
102 // Not connected.
103 HAL_LOGE("Camera device %s is not connected, cannot disconnect.",
104 device_path_.c_str(), connection_count_);
105 return;
106 }
107
108 --connection_count_;
109 if (connection_count_ > 0) {
110 HAL_LOGV("Disconnected from camera device %s. %d connections remain.",
111 device_path_.c_str(), connection_count_);
112 return;
113 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700114
115 device_fd_.reset(); // Includes close().
116 format_.reset();
117 max_buffers_ = 0;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700118 // Closing the device releases all queued buffers back to the user.
119 gralloc_->unlockAllBuffers();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700120}
121
122// Helper function. Should be used instead of ioctl throughout this class.
123template <typename T>
124int V4L2Wrapper::IoctlLocked(int request, T data) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700125 // Potentially called so many times logging entry is a bad idea.
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700126 std::lock_guard<std::mutex> lock(device_lock_);
127
128 if (!connected()) {
129 HAL_LOGE("Device %s not connected.", device_path_.c_str());
130 return -ENODEV;
131 }
132 return TEMP_FAILURE_RETRY(ioctl(device_fd_.get(), request, data));
133}
134
135int V4L2Wrapper::StreamOn() {
136 HAL_LOG_ENTER();
137
138 if (!format_) {
139 HAL_LOGE("Stream format must be set before turning on stream.");
140 return -EINVAL;
141 }
142
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700143 int32_t type = format_->type();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700144 if (IoctlLocked(VIDIOC_STREAMON, &type) < 0) {
145 HAL_LOGE("STREAMON fails: %s", strerror(errno));
146 return -ENODEV;
147 }
148
149 return 0;
150}
151
152int V4L2Wrapper::StreamOff() {
153 HAL_LOG_ENTER();
154
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700155 if (!format_) {
156 HAL_LOGE("Stream format must be set to turn off stream.");
157 return -ENODEV;
158 }
159
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700160 int32_t type = format_->type();
161 int res = IoctlLocked(VIDIOC_STREAMOFF, &type);
162 // Calling STREAMOFF releases all queued buffers back to the user.
163 int gralloc_res = gralloc_->unlockAllBuffers();
164 if (res < 0) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700165 HAL_LOGE("STREAMOFF fails: %s", strerror(errno));
166 return -ENODEV;
167 }
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700168 if (gralloc_res < 0) {
169 HAL_LOGE("Failed to unlock all buffers after turning stream off.");
170 return gralloc_res;
171 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700172
173 return 0;
174}
175
176int V4L2Wrapper::QueryControl(uint32_t control_id,
177 v4l2_query_ext_ctrl* result) {
178 HAL_LOG_ENTER();
179 int res;
180
181 memset(result, 0, sizeof(*result));
182
183 if (extended_query_supported_) {
184 result->id = control_id;
185 res = IoctlLocked(VIDIOC_QUERY_EXT_CTRL, result);
186 // Assuming the operation was supported (not ENOTTY), no more to do.
187 if (errno != ENOTTY) {
188 if (res) {
189 HAL_LOGE("QUERY_EXT_CTRL fails: %s", strerror(errno));
190 return -ENODEV;
191 }
192 return 0;
193 }
194 }
195
196 // Extended control querying not supported, fall back to basic control query.
197 v4l2_queryctrl query;
198 query.id = control_id;
199 if (IoctlLocked(VIDIOC_QUERYCTRL, &query)) {
200 HAL_LOGE("QUERYCTRL fails: %s", strerror(errno));
201 return -ENODEV;
202 }
203
204 // Convert the basic result to the extended result.
205 result->id = query.id;
206 result->type = query.type;
207 memcpy(result->name, query.name, sizeof(query.name));
208 result->minimum = query.minimum;
209 if (query.type == V4L2_CTRL_TYPE_BITMASK) {
210 // According to the V4L2 documentation, when type is BITMASK,
211 // max and default should be interpreted as __u32. Practically,
212 // this means the conversion from 32 bit to 64 will pad with 0s not 1s.
213 result->maximum = static_cast<uint32_t>(query.maximum);
214 result->default_value = static_cast<uint32_t>(query.default_value);
215 } else {
216 result->maximum = query.maximum;
217 result->default_value = query.default_value;
218 }
219 result->step = static_cast<uint32_t>(query.step);
220 result->flags = query.flags;
221 result->elems = 1;
222 switch (result->type) {
223 case V4L2_CTRL_TYPE_INTEGER64:
224 result->elem_size = sizeof(int64_t);
225 break;
226 case V4L2_CTRL_TYPE_STRING:
227 result->elem_size = result->maximum + 1;
228 break;
229 default:
230 result->elem_size = sizeof(int32_t);
231 break;
232 }
233
234 return 0;
235}
236
237int V4L2Wrapper::GetControl(uint32_t control_id, int32_t* value) {
238 HAL_LOG_ENTER();
239
240 v4l2_control control;
241 control.id = control_id;
242 if (IoctlLocked(VIDIOC_G_CTRL, &control) < 0) {
243 HAL_LOGE("G_CTRL fails: %s", strerror(errno));
244 return -ENODEV;
245 }
246 *value = control.value;
247 return 0;
248}
249
250int V4L2Wrapper::SetControl(uint32_t control_id, int32_t desired,
251 int32_t* result) {
252 HAL_LOG_ENTER();
253
Ari Hausman-Cohen99f3ea02016-08-02 10:47:07 -0700254 // TODO(b/29334616): When async, this may need to check if the stream
255 // is on, and if so, lock it off while setting format. Need to look
256 // into if V4L2 supports adjusting controls while the stream is on.
257
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700258 v4l2_control control{control_id, desired};
259 if (IoctlLocked(VIDIOC_S_CTRL, &control) < 0) {
260 HAL_LOGE("S_CTRL fails: %s", strerror(errno));
261 return -ENODEV;
262 }
Ari Hausman-Cohen99f3ea02016-08-02 10:47:07 -0700263 // If the caller wants to know the result, pass it back.
264 if (result != nullptr) {
265 *result = control.value;
266 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700267 return 0;
268}
269
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700270int V4L2Wrapper::GetFormats(std::set<uint32_t>* v4l2_formats) {
271 HAL_LOG_ENTER();
272
273 v4l2_fmtdesc format_query;
274 memset(&format_query, 0, sizeof(format_query));
275 // TODO(b/30000211): multiplanar support.
276 format_query.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
277 while (IoctlLocked(VIDIOC_ENUM_FMT, &format_query) >= 0) {
278 v4l2_formats->insert(format_query.pixelformat);
279 ++format_query.index;
280 }
281
282 if (errno != EINVAL) {
283 HAL_LOGE("ENUM_FMT fails at index %d: %s", format_query.index,
284 strerror(errno));
285 return -ENODEV;
286 }
287 return 0;
288}
289
290int V4L2Wrapper::GetFormatFrameSizes(uint32_t v4l2_format,
291 std::set<std::array<int32_t, 2>>* sizes) {
292 HAL_LOG_ENTER();
293
294 v4l2_frmsizeenum size_query;
295 memset(&size_query, 0, sizeof(size_query));
296 size_query.pixel_format = v4l2_format;
297 if (IoctlLocked(VIDIOC_ENUM_FRAMESIZES, &size_query) < 0) {
298 HAL_LOGE("ENUM_FRAMESIZES failed: %s", strerror(errno));
299 return -ENODEV;
300 }
301 if (size_query.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
302 // Discrete: enumerate all sizes using VIDIOC_ENUM_FRAMESIZES.
303 // Assuming that a driver with discrete frame sizes has a reasonable number
304 // of them.
305 do {
306 sizes->insert({{{static_cast<int32_t>(size_query.discrete.width),
307 static_cast<int32_t>(size_query.discrete.height)}}});
308 ++size_query.index;
309 } while (IoctlLocked(VIDIOC_ENUM_FRAMESIZES, &size_query) >= 0);
310 if (errno != EINVAL) {
311 HAL_LOGE("ENUM_FRAMESIZES fails at index %d: %s", size_query.index,
312 strerror(errno));
313 return -ENODEV;
314 }
315 } else {
316 // Continuous/Step-wise: based on the stepwise struct returned by the query.
317 // Fully listing all possible sizes, with large enough range/small enough
318 // step size, may produce far too many potential sizes. Instead, find the
319 // closest to a set of standard sizes plus largest possible.
320 sizes->insert({{{static_cast<int32_t>(size_query.stepwise.max_width),
321 static_cast<int32_t>(size_query.stepwise.max_height)}}});
322 for (const auto& size : kStandardSizes) {
323 // Find the closest size, rounding up.
324 uint32_t desired_width = size[0];
325 uint32_t desired_height = size[1];
326 if (desired_width < size_query.stepwise.min_width ||
327 desired_height < size_query.stepwise.min_height) {
328 HAL_LOGV("Standard size %u x %u is too small for format %d",
329 desired_width, desired_height, v4l2_format);
330 continue;
331 } else if (desired_width > size_query.stepwise.max_width &&
332 desired_height > size_query.stepwise.max_height) {
333 HAL_LOGV("Standard size %u x %u is too big for format %d",
334 desired_width, desired_height, v4l2_format);
335 continue;
336 }
337
338 // Round up.
339 uint32_t width_steps = (desired_width - size_query.stepwise.min_width +
340 size_query.stepwise.step_width - 1) /
341 size_query.stepwise.step_width;
342 uint32_t height_steps = (desired_height - size_query.stepwise.min_height +
343 size_query.stepwise.step_height - 1) /
344 size_query.stepwise.step_height;
345 sizes->insert(
346 {{{static_cast<int32_t>(size_query.stepwise.min_width +
347 width_steps * size_query.stepwise.step_width),
348 static_cast<int32_t>(size_query.stepwise.min_height +
349 height_steps *
350 size_query.stepwise.step_height)}}});
351 }
352 }
353 return 0;
354}
355
356// Converts a v4l2_fract with units of seconds to an int64_t with units of ns.
357inline int64_t FractToNs(const v4l2_fract& fract) {
358 return (1000000000LL * fract.numerator) / fract.denominator;
359}
360
361int V4L2Wrapper::GetFormatFrameDurationRange(
362 uint32_t v4l2_format, const std::array<int32_t, 2>& size,
363 std::array<int64_t, 2>* duration_range) {
364 // Potentially called so many times logging entry is a bad idea.
365
366 v4l2_frmivalenum duration_query;
367 memset(&duration_query, 0, sizeof(duration_query));
368 duration_query.pixel_format = v4l2_format;
369 duration_query.width = size[0];
370 duration_query.height = size[1];
371 if (IoctlLocked(VIDIOC_ENUM_FRAMEINTERVALS, &duration_query) < 0) {
372 HAL_LOGE("ENUM_FRAMEINTERVALS failed: %s", strerror(errno));
373 return -ENODEV;
374 }
375
376 int64_t min = std::numeric_limits<int64_t>::max();
377 int64_t max = std::numeric_limits<int64_t>::min();
378 if (duration_query.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
379 // Discrete: enumerate all durations using VIDIOC_ENUM_FRAMEINTERVALS.
380 do {
381 min = std::min(min, FractToNs(duration_query.discrete));
382 max = std::max(max, FractToNs(duration_query.discrete));
383 ++duration_query.index;
384 } while (IoctlLocked(VIDIOC_ENUM_FRAMEINTERVALS, &duration_query) >= 0);
385 if (errno != EINVAL) {
386 HAL_LOGE("ENUM_FRAMEINTERVALS fails at index %d: %s",
387 duration_query.index, strerror(errno));
388 return -ENODEV;
389 }
390 } else {
391 // Continuous/Step-wise: simply convert the given min and max.
392 min = FractToNs(duration_query.stepwise.min);
393 max = FractToNs(duration_query.stepwise.max);
394 }
395 (*duration_range)[0] = min;
396 (*duration_range)[1] = max;
397 return 0;
398}
399
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700400int V4L2Wrapper::SetFormat(const default_camera_hal::Stream& stream,
401 uint32_t* result_max_buffers) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700402 HAL_LOG_ENTER();
403
404 // Should be checked earlier; sanity check.
405 if (stream.isInputType()) {
406 HAL_LOGE("Input streams not supported.");
407 return -EINVAL;
408 }
409
410 StreamFormat desired_format(stream);
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700411 if (format_ && desired_format == *format_) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700412 HAL_LOGV("Already in correct format, skipping format setting.");
413 return 0;
414 }
415
416 // Not in the correct format, set our format.
417 v4l2_format new_format;
418 desired_format.FillFormatRequest(&new_format);
419 // TODO(b/29334616): When async, this will need to check if the stream
420 // is on, and if so, lock it off while setting format.
421 if (IoctlLocked(VIDIOC_S_FMT, &new_format) < 0) {
422 HAL_LOGE("S_FMT failed: %s", strerror(errno));
423 return -ENODEV;
424 }
425
426 // Check that the driver actually set to the requested values.
427 if (desired_format != new_format) {
428 HAL_LOGE("Device doesn't support desired stream configuration.");
429 return -EINVAL;
430 }
431
432 // Keep track of our new format.
433 format_.reset(new StreamFormat(new_format));
434
435 // Format changed, setup new buffers.
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700436 int res = SetupBuffers();
437 if (res) {
438 HAL_LOGE("Failed to set up buffers for new format.");
439 return res;
440 }
441 *result_max_buffers = max_buffers_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700442 return 0;
443}
444
445int V4L2Wrapper::SetupBuffers() {
446 HAL_LOG_ENTER();
447
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700448 if (!format_) {
449 HAL_LOGE("Stream format must be set before setting up buffers.");
450 return -ENODEV;
451 }
452
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700453 // "Request" a buffer (since we're using a userspace buffer, this just
454 // tells V4L2 to switch into userspace buffer mode).
455 v4l2_requestbuffers req_buffers;
456 memset(&req_buffers, 0, sizeof(req_buffers));
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700457 req_buffers.type = format_->type();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700458 req_buffers.memory = V4L2_MEMORY_USERPTR;
459 req_buffers.count = 1;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700460
461 int res = IoctlLocked(VIDIOC_REQBUFS, &req_buffers);
462 // Calling REQBUFS releases all queued buffers back to the user.
463 int gralloc_res = gralloc_->unlockAllBuffers();
464 if (res < 0) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700465 HAL_LOGE("REQBUFS failed: %s", strerror(errno));
466 return -ENODEV;
467 }
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700468 if (gralloc_res < 0) {
469 HAL_LOGE("Failed to unlock all buffers when setting up new buffers.");
470 return gralloc_res;
471 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700472
473 // V4L2 will set req_buffers.count to a number of buffers it can handle.
474 max_buffers_ = req_buffers.count;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700475 // Sanity check.
476 if (max_buffers_ < 1) {
477 HAL_LOGE("REQBUFS claims it can't handle any buffers.");
478 return -ENODEV;
479 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700480 return 0;
481}
482
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700483int V4L2Wrapper::EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer) {
484 HAL_LOG_ENTER();
485
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700486 if (!format_) {
487 HAL_LOGE("Stream format must be set before enqueuing buffers.");
488 return -ENODEV;
489 }
490
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700491 // Set up a v4l2 buffer struct.
492 v4l2_buffer device_buffer;
493 memset(&device_buffer, 0, sizeof(device_buffer));
494 device_buffer.type = format_->type();
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700495 // TODO(b/29334616): when this is async, actually limit the number
496 // of buffers used to the known max, and set this according to the
497 // queue length.
498 device_buffer.index = 0;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700499
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700500 // Use QUERYBUF to ensure our buffer/device is in good shape,
501 // and fill out remaining fields.
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700502 if (IoctlLocked(VIDIOC_QUERYBUF, &device_buffer) < 0) {
503 HAL_LOGE("QUERYBUF fails: %s", strerror(errno));
504 return -ENODEV;
505 }
506
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700507 // Lock the buffer for writing (fills in the user pointer field).
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700508 int res =
509 gralloc_->lock(camera_buffer, format_->bytes_per_line(), &device_buffer);
510 if (res) {
511 HAL_LOGE("Gralloc failed to lock buffer.");
512 return res;
513 }
514 if (IoctlLocked(VIDIOC_QBUF, &device_buffer) < 0) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700515 HAL_LOGE("QBUF fails: %s", strerror(errno));
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700516 gralloc_->unlock(&device_buffer);
517 return -ENODEV;
518 }
519
520 return 0;
521}
522
523int V4L2Wrapper::DequeueBuffer(v4l2_buffer* buffer) {
524 HAL_LOG_ENTER();
525
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700526 if (!format_) {
527 HAL_LOGE("Stream format must be set before dequeueing buffers.");
528 return -ENODEV;
529 }
530
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700531 memset(buffer, 0, sizeof(*buffer));
532 buffer->type = format_->type();
533 buffer->memory = V4L2_MEMORY_USERPTR;
534 if (IoctlLocked(VIDIOC_DQBUF, buffer) < 0) {
535 HAL_LOGE("DQBUF fails: %s", strerror(errno));
536 return -ENODEV;
537 }
538
539 // Now that we're done painting the buffer, we can unlock it.
540 int res = gralloc_->unlock(buffer);
541 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700542 HAL_LOGE("Gralloc failed to unlock buffer after dequeueing.");
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700543 return res;
544 }
545
546 return 0;
547}
548
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700549} // namespace v4l2_camera_hal