blob: ca38168bd4d8981627e887948b605b5965c75907 [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
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070063V4L2Wrapper::~V4L2Wrapper() {
64 HAL_LOG_ENTER();
65}
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070066
67int V4L2Wrapper::Connect() {
68 HAL_LOG_ENTER();
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070069 std::lock_guard<std::mutex> lock(connection_lock_);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070070
71 if (connected()) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070072 HAL_LOGV("Camera device %s is already connected.", device_path_.c_str());
73 ++connection_count_;
74 return 0;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070075 }
76
77 int fd = TEMP_FAILURE_RETRY(open(device_path_.c_str(), O_RDWR));
78 if (fd < 0) {
79 HAL_LOGE("failed to open %s (%s)", device_path_.c_str(), strerror(errno));
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070080 return -ENODEV;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070081 }
82 device_fd_.reset(fd);
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070083 ++connection_count_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070084
85 // Check if this connection has the extended control query capability.
86 v4l2_query_ext_ctrl query;
87 query.id = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070088 extended_query_supported_ = (IoctlLocked(VIDIOC_QUERY_EXT_CTRL, &query) == 0);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070089
90 // TODO(b/29185945): confirm this is a supported device.
91 // This is checked by the HAL, but the device at device_path_ may
92 // not be the same one that was there when the HAL was loaded.
93 // (Alternatively, better hotplugging support may make this unecessary
94 // by disabling cameras that get disconnected and checking newly connected
95 // cameras, so Connect() is never called on an unsupported camera)
96 return 0;
97}
98
99void V4L2Wrapper::Disconnect() {
100 HAL_LOG_ENTER();
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700101 std::lock_guard<std::mutex> lock(connection_lock_);
102
103 if (connection_count_ == 0) {
104 // Not connected.
105 HAL_LOGE("Camera device %s is not connected, cannot disconnect.",
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700106 device_path_.c_str(),
107 connection_count_);
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700108 return;
109 }
110
111 --connection_count_;
112 if (connection_count_ > 0) {
113 HAL_LOGV("Disconnected from camera device %s. %d connections remain.",
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700114 device_path_.c_str(),
115 connection_count_);
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700116 return;
117 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700118
119 device_fd_.reset(); // Includes close().
120 format_.reset();
121 max_buffers_ = 0;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700122 // Closing the device releases all queued buffers back to the user.
123 gralloc_->unlockAllBuffers();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700124}
125
126// Helper function. Should be used instead of ioctl throughout this class.
127template <typename T>
128int V4L2Wrapper::IoctlLocked(int request, T data) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700129 // Potentially called so many times logging entry is a bad idea.
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700130 std::lock_guard<std::mutex> lock(device_lock_);
131
132 if (!connected()) {
133 HAL_LOGE("Device %s not connected.", device_path_.c_str());
134 return -ENODEV;
135 }
136 return TEMP_FAILURE_RETRY(ioctl(device_fd_.get(), request, data));
137}
138
139int V4L2Wrapper::StreamOn() {
140 HAL_LOG_ENTER();
141
142 if (!format_) {
143 HAL_LOGE("Stream format must be set before turning on stream.");
144 return -EINVAL;
145 }
146
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700147 int32_t type = format_->type();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700148 if (IoctlLocked(VIDIOC_STREAMON, &type) < 0) {
149 HAL_LOGE("STREAMON fails: %s", strerror(errno));
150 return -ENODEV;
151 }
152
153 return 0;
154}
155
156int V4L2Wrapper::StreamOff() {
157 HAL_LOG_ENTER();
158
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700159 if (!format_) {
160 HAL_LOGE("Stream format must be set to turn off stream.");
161 return -ENODEV;
162 }
163
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700164 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-Cohenc17fd092016-07-18 10:13:26 -0700169 HAL_LOGE("STREAMOFF fails: %s", strerror(errno));
170 return -ENODEV;
171 }
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700172 if (gralloc_res < 0) {
173 HAL_LOGE("Failed to unlock all buffers after turning stream off.");
174 return gralloc_res;
175 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700176
177 return 0;
178}
179
180int 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
241int V4L2Wrapper::GetControl(uint32_t control_id, int32_t* value) {
242 HAL_LOG_ENTER();
243
244 v4l2_control control;
245 control.id = control_id;
246 if (IoctlLocked(VIDIOC_G_CTRL, &control) < 0) {
247 HAL_LOGE("G_CTRL fails: %s", strerror(errno));
248 return -ENODEV;
249 }
250 *value = control.value;
251 return 0;
252}
253
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700254int V4L2Wrapper::SetControl(uint32_t control_id,
255 int32_t desired,
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700256 int32_t* result) {
257 HAL_LOG_ENTER();
258
Ari Hausman-Cohen99f3ea02016-08-02 10:47:07 -0700259 // TODO(b/29334616): When async, this may need to check if the stream
260 // is on, and if so, lock it off while setting format. Need to look
261 // into if V4L2 supports adjusting controls while the stream is on.
262
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700263 v4l2_control control{control_id, desired};
264 if (IoctlLocked(VIDIOC_S_CTRL, &control) < 0) {
265 HAL_LOGE("S_CTRL fails: %s", strerror(errno));
266 return -ENODEV;
267 }
Ari Hausman-Cohen99f3ea02016-08-02 10:47:07 -0700268 // If the caller wants to know the result, pass it back.
269 if (result != nullptr) {
270 *result = control.value;
271 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700272 return 0;
273}
274
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700275int V4L2Wrapper::GetFormats(std::set<uint32_t>* v4l2_formats) {
276 HAL_LOG_ENTER();
277
278 v4l2_fmtdesc format_query;
279 memset(&format_query, 0, sizeof(format_query));
280 // TODO(b/30000211): multiplanar support.
281 format_query.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
282 while (IoctlLocked(VIDIOC_ENUM_FMT, &format_query) >= 0) {
283 v4l2_formats->insert(format_query.pixelformat);
284 ++format_query.index;
285 }
286
287 if (errno != EINVAL) {
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700288 HAL_LOGE(
289 "ENUM_FMT fails at index %d: %s", format_query.index, strerror(errno));
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700290 return -ENODEV;
291 }
292 return 0;
293}
294
295int V4L2Wrapper::GetFormatFrameSizes(uint32_t v4l2_format,
296 std::set<std::array<int32_t, 2>>* sizes) {
297 HAL_LOG_ENTER();
298
299 v4l2_frmsizeenum size_query;
300 memset(&size_query, 0, sizeof(size_query));
301 size_query.pixel_format = v4l2_format;
302 if (IoctlLocked(VIDIOC_ENUM_FRAMESIZES, &size_query) < 0) {
303 HAL_LOGE("ENUM_FRAMESIZES failed: %s", strerror(errno));
304 return -ENODEV;
305 }
306 if (size_query.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
307 // Discrete: enumerate all sizes using VIDIOC_ENUM_FRAMESIZES.
308 // Assuming that a driver with discrete frame sizes has a reasonable number
309 // of them.
310 do {
311 sizes->insert({{{static_cast<int32_t>(size_query.discrete.width),
312 static_cast<int32_t>(size_query.discrete.height)}}});
313 ++size_query.index;
314 } while (IoctlLocked(VIDIOC_ENUM_FRAMESIZES, &size_query) >= 0);
315 if (errno != EINVAL) {
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700316 HAL_LOGE("ENUM_FRAMESIZES fails at index %d: %s",
317 size_query.index,
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700318 strerror(errno));
319 return -ENODEV;
320 }
321 } else {
322 // Continuous/Step-wise: based on the stepwise struct returned by the query.
323 // Fully listing all possible sizes, with large enough range/small enough
324 // step size, may produce far too many potential sizes. Instead, find the
325 // closest to a set of standard sizes plus largest possible.
326 sizes->insert({{{static_cast<int32_t>(size_query.stepwise.max_width),
327 static_cast<int32_t>(size_query.stepwise.max_height)}}});
328 for (const auto& size : kStandardSizes) {
329 // Find the closest size, rounding up.
330 uint32_t desired_width = size[0];
331 uint32_t desired_height = size[1];
332 if (desired_width < size_query.stepwise.min_width ||
333 desired_height < size_query.stepwise.min_height) {
334 HAL_LOGV("Standard size %u x %u is too small for format %d",
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700335 desired_width,
336 desired_height,
337 v4l2_format);
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700338 continue;
339 } else if (desired_width > size_query.stepwise.max_width &&
340 desired_height > size_query.stepwise.max_height) {
341 HAL_LOGV("Standard size %u x %u is too big for format %d",
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700342 desired_width,
343 desired_height,
344 v4l2_format);
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700345 continue;
346 }
347
348 // Round up.
349 uint32_t width_steps = (desired_width - size_query.stepwise.min_width +
350 size_query.stepwise.step_width - 1) /
351 size_query.stepwise.step_width;
352 uint32_t height_steps = (desired_height - size_query.stepwise.min_height +
353 size_query.stepwise.step_height - 1) /
354 size_query.stepwise.step_height;
355 sizes->insert(
356 {{{static_cast<int32_t>(size_query.stepwise.min_width +
357 width_steps * size_query.stepwise.step_width),
358 static_cast<int32_t>(size_query.stepwise.min_height +
359 height_steps *
360 size_query.stepwise.step_height)}}});
361 }
362 }
363 return 0;
364}
365
366// Converts a v4l2_fract with units of seconds to an int64_t with units of ns.
367inline int64_t FractToNs(const v4l2_fract& fract) {
368 return (1000000000LL * fract.numerator) / fract.denominator;
369}
370
371int V4L2Wrapper::GetFormatFrameDurationRange(
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700372 uint32_t v4l2_format,
373 const std::array<int32_t, 2>& size,
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700374 std::array<int64_t, 2>* duration_range) {
375 // Potentially called so many times logging entry is a bad idea.
376
377 v4l2_frmivalenum duration_query;
378 memset(&duration_query, 0, sizeof(duration_query));
379 duration_query.pixel_format = v4l2_format;
380 duration_query.width = size[0];
381 duration_query.height = size[1];
382 if (IoctlLocked(VIDIOC_ENUM_FRAMEINTERVALS, &duration_query) < 0) {
383 HAL_LOGE("ENUM_FRAMEINTERVALS failed: %s", strerror(errno));
384 return -ENODEV;
385 }
386
387 int64_t min = std::numeric_limits<int64_t>::max();
388 int64_t max = std::numeric_limits<int64_t>::min();
389 if (duration_query.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
390 // Discrete: enumerate all durations using VIDIOC_ENUM_FRAMEINTERVALS.
391 do {
392 min = std::min(min, FractToNs(duration_query.discrete));
393 max = std::max(max, FractToNs(duration_query.discrete));
394 ++duration_query.index;
395 } while (IoctlLocked(VIDIOC_ENUM_FRAMEINTERVALS, &duration_query) >= 0);
396 if (errno != EINVAL) {
397 HAL_LOGE("ENUM_FRAMEINTERVALS fails at index %d: %s",
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700398 duration_query.index,
399 strerror(errno));
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700400 return -ENODEV;
401 }
402 } else {
403 // Continuous/Step-wise: simply convert the given min and max.
404 min = FractToNs(duration_query.stepwise.min);
405 max = FractToNs(duration_query.stepwise.max);
406 }
407 (*duration_range)[0] = min;
408 (*duration_range)[1] = max;
409 return 0;
410}
411
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700412int V4L2Wrapper::SetFormat(const default_camera_hal::Stream& stream,
413 uint32_t* result_max_buffers) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700414 HAL_LOG_ENTER();
415
416 // Should be checked earlier; sanity check.
417 if (stream.isInputType()) {
418 HAL_LOGE("Input streams not supported.");
419 return -EINVAL;
420 }
421
422 StreamFormat desired_format(stream);
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700423 if (format_ && desired_format == *format_) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700424 HAL_LOGV("Already in correct format, skipping format setting.");
425 return 0;
426 }
427
428 // Not in the correct format, set our format.
429 v4l2_format new_format;
430 desired_format.FillFormatRequest(&new_format);
431 // TODO(b/29334616): When async, this will need to check if the stream
432 // is on, and if so, lock it off while setting format.
433 if (IoctlLocked(VIDIOC_S_FMT, &new_format) < 0) {
434 HAL_LOGE("S_FMT failed: %s", strerror(errno));
435 return -ENODEV;
436 }
437
438 // Check that the driver actually set to the requested values.
439 if (desired_format != new_format) {
440 HAL_LOGE("Device doesn't support desired stream configuration.");
441 return -EINVAL;
442 }
443
444 // Keep track of our new format.
445 format_.reset(new StreamFormat(new_format));
446
447 // Format changed, setup new buffers.
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700448 int res = SetupBuffers();
449 if (res) {
450 HAL_LOGE("Failed to set up buffers for new format.");
451 return res;
452 }
453 *result_max_buffers = max_buffers_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700454 return 0;
455}
456
457int V4L2Wrapper::SetupBuffers() {
458 HAL_LOG_ENTER();
459
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700460 if (!format_) {
461 HAL_LOGE("Stream format must be set before setting up buffers.");
462 return -ENODEV;
463 }
464
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700465 // "Request" a buffer (since we're using a userspace buffer, this just
466 // tells V4L2 to switch into userspace buffer mode).
467 v4l2_requestbuffers req_buffers;
468 memset(&req_buffers, 0, sizeof(req_buffers));
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700469 req_buffers.type = format_->type();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700470 req_buffers.memory = V4L2_MEMORY_USERPTR;
471 req_buffers.count = 1;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700472
473 int res = IoctlLocked(VIDIOC_REQBUFS, &req_buffers);
474 // Calling REQBUFS releases all queued buffers back to the user.
475 int gralloc_res = gralloc_->unlockAllBuffers();
476 if (res < 0) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700477 HAL_LOGE("REQBUFS failed: %s", strerror(errno));
478 return -ENODEV;
479 }
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700480 if (gralloc_res < 0) {
481 HAL_LOGE("Failed to unlock all buffers when setting up new buffers.");
482 return gralloc_res;
483 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700484
485 // V4L2 will set req_buffers.count to a number of buffers it can handle.
486 max_buffers_ = req_buffers.count;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700487 // Sanity check.
488 if (max_buffers_ < 1) {
489 HAL_LOGE("REQBUFS claims it can't handle any buffers.");
490 return -ENODEV;
491 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700492 return 0;
493}
494
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700495int V4L2Wrapper::EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer) {
496 HAL_LOG_ENTER();
497
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700498 if (!format_) {
499 HAL_LOGE("Stream format must be set before enqueuing buffers.");
500 return -ENODEV;
501 }
502
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700503 // Set up a v4l2 buffer struct.
504 v4l2_buffer device_buffer;
505 memset(&device_buffer, 0, sizeof(device_buffer));
506 device_buffer.type = format_->type();
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700507 // TODO(b/29334616): when this is async, actually limit the number
508 // of buffers used to the known max, and set this according to the
509 // queue length.
510 device_buffer.index = 0;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700511
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700512 // Use QUERYBUF to ensure our buffer/device is in good shape,
513 // and fill out remaining fields.
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700514 if (IoctlLocked(VIDIOC_QUERYBUF, &device_buffer) < 0) {
515 HAL_LOGE("QUERYBUF fails: %s", strerror(errno));
516 return -ENODEV;
517 }
518
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700519 // Lock the buffer for writing (fills in the user pointer field).
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700520 int res =
521 gralloc_->lock(camera_buffer, format_->bytes_per_line(), &device_buffer);
522 if (res) {
523 HAL_LOGE("Gralloc failed to lock buffer.");
524 return res;
525 }
526 if (IoctlLocked(VIDIOC_QBUF, &device_buffer) < 0) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700527 HAL_LOGE("QBUF fails: %s", strerror(errno));
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700528 gralloc_->unlock(&device_buffer);
529 return -ENODEV;
530 }
531
532 return 0;
533}
534
535int V4L2Wrapper::DequeueBuffer(v4l2_buffer* buffer) {
536 HAL_LOG_ENTER();
537
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700538 if (!format_) {
539 HAL_LOGE("Stream format must be set before dequeueing buffers.");
540 return -ENODEV;
541 }
542
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700543 memset(buffer, 0, sizeof(*buffer));
544 buffer->type = format_->type();
545 buffer->memory = V4L2_MEMORY_USERPTR;
546 if (IoctlLocked(VIDIOC_DQBUF, buffer) < 0) {
547 HAL_LOGE("DQBUF fails: %s", strerror(errno));
548 return -ENODEV;
549 }
550
551 // Now that we're done painting the buffer, we can unlock it.
552 int res = gralloc_->unlock(buffer);
553 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700554 HAL_LOGE("Gralloc failed to unlock buffer after dequeueing.");
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700555 return res;
556 }
557
558 return 0;
559}
560
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700561} // namespace v4l2_camera_hal