blob: e62a17d7ea695442c7c419f359700b5f81aaacff [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
Ari Hausman-Cohen7a1fba62016-08-10 11:31:04 -0700240 // For extended controls (any control class other than "user"),
241 // G_EXT_CTRL must be used instead of G_CTRL.
242 if (V4L2_CTRL_ID2CLASS(control_id) != V4L2_CTRL_CLASS_USER) {
243 v4l2_ext_control control;
244 v4l2_ext_controls controls;
245 memset(&control, 0, sizeof(control));
246 memset(&controls, 0, sizeof(controls));
247
248 control.id = control_id;
249 controls.ctrl_class = V4L2_CTRL_ID2CLASS(control_id);
250 controls.count = 1;
251 controls.controls = &control;
252
253 if (IoctlLocked(VIDIOC_G_EXT_CTRLS, &controls) < 0) {
254 HAL_LOGE("G_EXT_CTRLS fails: %s", strerror(errno));
255 return -ENODEV;
256 }
257 *value = control.value;
258 } else {
259 v4l2_control control{control_id, 0};
260 if (IoctlLocked(VIDIOC_G_CTRL, &control) < 0) {
261 HAL_LOGE("G_CTRL fails: %s", strerror(errno));
262 return -ENODEV;
263 }
264 *value = control.value;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700265 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700266 return 0;
267}
268
269int V4L2Wrapper::SetControl(uint32_t control_id, int32_t desired,
270 int32_t* result) {
271 HAL_LOG_ENTER();
Ari Hausman-Cohen7a1fba62016-08-10 11:31:04 -0700272 int32_t result_value = 0;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700273
Ari Hausman-Cohen99f3ea02016-08-02 10:47:07 -0700274 // TODO(b/29334616): When async, this may need to check if the stream
275 // is on, and if so, lock it off while setting format. Need to look
276 // into if V4L2 supports adjusting controls while the stream is on.
277
Ari Hausman-Cohen7a1fba62016-08-10 11:31:04 -0700278 // For extended controls (any control class other than "user"),
279 // S_EXT_CTRL must be used instead of S_CTRL.
280 if (V4L2_CTRL_ID2CLASS(control_id) != V4L2_CTRL_CLASS_USER) {
281 v4l2_ext_control control;
282 v4l2_ext_controls controls;
283 memset(&control, 0, sizeof(control));
284 memset(&controls, 0, sizeof(controls));
285
286 control.id = control_id;
287 control.value = desired;
288 controls.ctrl_class = V4L2_CTRL_ID2CLASS(control_id);
289 controls.count = 1;
290 controls.controls = &control;
291
292 if (IoctlLocked(VIDIOC_S_EXT_CTRLS, &controls) < 0) {
293 HAL_LOGE("S_EXT_CTRLS fails: %s", strerror(errno));
294 return -ENODEV;
295 }
296 result_value = control.value;
297 } else {
298 v4l2_control control{control_id, desired};
299 if (IoctlLocked(VIDIOC_S_CTRL, &control) < 0) {
300 HAL_LOGE("S_CTRL fails: %s", strerror(errno));
301 return -ENODEV;
302 }
303 result_value = control.value;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700304 }
Ari Hausman-Cohen7a1fba62016-08-10 11:31:04 -0700305
Ari Hausman-Cohen99f3ea02016-08-02 10:47:07 -0700306 // If the caller wants to know the result, pass it back.
307 if (result != nullptr) {
Ari Hausman-Cohen7a1fba62016-08-10 11:31:04 -0700308 *result = result_value;
Ari Hausman-Cohen99f3ea02016-08-02 10:47:07 -0700309 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700310 return 0;
311}
312
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700313int V4L2Wrapper::GetFormats(std::set<uint32_t>* v4l2_formats) {
314 HAL_LOG_ENTER();
315
316 v4l2_fmtdesc format_query;
317 memset(&format_query, 0, sizeof(format_query));
318 // TODO(b/30000211): multiplanar support.
319 format_query.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
320 while (IoctlLocked(VIDIOC_ENUM_FMT, &format_query) >= 0) {
321 v4l2_formats->insert(format_query.pixelformat);
322 ++format_query.index;
323 }
324
325 if (errno != EINVAL) {
326 HAL_LOGE("ENUM_FMT fails at index %d: %s", format_query.index,
327 strerror(errno));
328 return -ENODEV;
329 }
330 return 0;
331}
332
333int V4L2Wrapper::GetFormatFrameSizes(uint32_t v4l2_format,
334 std::set<std::array<int32_t, 2>>* sizes) {
335 HAL_LOG_ENTER();
336
337 v4l2_frmsizeenum size_query;
338 memset(&size_query, 0, sizeof(size_query));
339 size_query.pixel_format = v4l2_format;
340 if (IoctlLocked(VIDIOC_ENUM_FRAMESIZES, &size_query) < 0) {
341 HAL_LOGE("ENUM_FRAMESIZES failed: %s", strerror(errno));
342 return -ENODEV;
343 }
344 if (size_query.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
345 // Discrete: enumerate all sizes using VIDIOC_ENUM_FRAMESIZES.
346 // Assuming that a driver with discrete frame sizes has a reasonable number
347 // of them.
348 do {
349 sizes->insert({{{static_cast<int32_t>(size_query.discrete.width),
350 static_cast<int32_t>(size_query.discrete.height)}}});
351 ++size_query.index;
352 } while (IoctlLocked(VIDIOC_ENUM_FRAMESIZES, &size_query) >= 0);
353 if (errno != EINVAL) {
354 HAL_LOGE("ENUM_FRAMESIZES fails at index %d: %s", size_query.index,
355 strerror(errno));
356 return -ENODEV;
357 }
358 } else {
359 // Continuous/Step-wise: based on the stepwise struct returned by the query.
360 // Fully listing all possible sizes, with large enough range/small enough
361 // step size, may produce far too many potential sizes. Instead, find the
362 // closest to a set of standard sizes plus largest possible.
363 sizes->insert({{{static_cast<int32_t>(size_query.stepwise.max_width),
364 static_cast<int32_t>(size_query.stepwise.max_height)}}});
365 for (const auto& size : kStandardSizes) {
366 // Find the closest size, rounding up.
367 uint32_t desired_width = size[0];
368 uint32_t desired_height = size[1];
369 if (desired_width < size_query.stepwise.min_width ||
370 desired_height < size_query.stepwise.min_height) {
371 HAL_LOGV("Standard size %u x %u is too small for format %d",
372 desired_width, desired_height, v4l2_format);
373 continue;
374 } else if (desired_width > size_query.stepwise.max_width &&
375 desired_height > size_query.stepwise.max_height) {
376 HAL_LOGV("Standard size %u x %u is too big for format %d",
377 desired_width, desired_height, v4l2_format);
378 continue;
379 }
380
381 // Round up.
382 uint32_t width_steps = (desired_width - size_query.stepwise.min_width +
383 size_query.stepwise.step_width - 1) /
384 size_query.stepwise.step_width;
385 uint32_t height_steps = (desired_height - size_query.stepwise.min_height +
386 size_query.stepwise.step_height - 1) /
387 size_query.stepwise.step_height;
388 sizes->insert(
389 {{{static_cast<int32_t>(size_query.stepwise.min_width +
390 width_steps * size_query.stepwise.step_width),
391 static_cast<int32_t>(size_query.stepwise.min_height +
392 height_steps *
393 size_query.stepwise.step_height)}}});
394 }
395 }
396 return 0;
397}
398
399// Converts a v4l2_fract with units of seconds to an int64_t with units of ns.
400inline int64_t FractToNs(const v4l2_fract& fract) {
401 return (1000000000LL * fract.numerator) / fract.denominator;
402}
403
404int V4L2Wrapper::GetFormatFrameDurationRange(
405 uint32_t v4l2_format, const std::array<int32_t, 2>& size,
406 std::array<int64_t, 2>* duration_range) {
407 // Potentially called so many times logging entry is a bad idea.
408
409 v4l2_frmivalenum duration_query;
410 memset(&duration_query, 0, sizeof(duration_query));
411 duration_query.pixel_format = v4l2_format;
412 duration_query.width = size[0];
413 duration_query.height = size[1];
414 if (IoctlLocked(VIDIOC_ENUM_FRAMEINTERVALS, &duration_query) < 0) {
415 HAL_LOGE("ENUM_FRAMEINTERVALS failed: %s", strerror(errno));
416 return -ENODEV;
417 }
418
419 int64_t min = std::numeric_limits<int64_t>::max();
420 int64_t max = std::numeric_limits<int64_t>::min();
421 if (duration_query.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
422 // Discrete: enumerate all durations using VIDIOC_ENUM_FRAMEINTERVALS.
423 do {
424 min = std::min(min, FractToNs(duration_query.discrete));
425 max = std::max(max, FractToNs(duration_query.discrete));
426 ++duration_query.index;
427 } while (IoctlLocked(VIDIOC_ENUM_FRAMEINTERVALS, &duration_query) >= 0);
428 if (errno != EINVAL) {
429 HAL_LOGE("ENUM_FRAMEINTERVALS fails at index %d: %s",
430 duration_query.index, strerror(errno));
431 return -ENODEV;
432 }
433 } else {
434 // Continuous/Step-wise: simply convert the given min and max.
435 min = FractToNs(duration_query.stepwise.min);
436 max = FractToNs(duration_query.stepwise.max);
437 }
438 (*duration_range)[0] = min;
439 (*duration_range)[1] = max;
440 return 0;
441}
442
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700443int V4L2Wrapper::SetFormat(const default_camera_hal::Stream& stream,
444 uint32_t* result_max_buffers) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700445 HAL_LOG_ENTER();
446
447 // Should be checked earlier; sanity check.
448 if (stream.isInputType()) {
449 HAL_LOGE("Input streams not supported.");
450 return -EINVAL;
451 }
452
453 StreamFormat desired_format(stream);
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700454 if (format_ && desired_format == *format_) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700455 HAL_LOGV("Already in correct format, skipping format setting.");
456 return 0;
457 }
458
459 // Not in the correct format, set our format.
460 v4l2_format new_format;
461 desired_format.FillFormatRequest(&new_format);
462 // TODO(b/29334616): When async, this will need to check if the stream
463 // is on, and if so, lock it off while setting format.
464 if (IoctlLocked(VIDIOC_S_FMT, &new_format) < 0) {
465 HAL_LOGE("S_FMT failed: %s", strerror(errno));
466 return -ENODEV;
467 }
468
469 // Check that the driver actually set to the requested values.
470 if (desired_format != new_format) {
471 HAL_LOGE("Device doesn't support desired stream configuration.");
472 return -EINVAL;
473 }
474
475 // Keep track of our new format.
476 format_.reset(new StreamFormat(new_format));
477
478 // Format changed, setup new buffers.
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700479 int res = SetupBuffers();
480 if (res) {
481 HAL_LOGE("Failed to set up buffers for new format.");
482 return res;
483 }
484 *result_max_buffers = max_buffers_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700485 return 0;
486}
487
488int V4L2Wrapper::SetupBuffers() {
489 HAL_LOG_ENTER();
490
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700491 if (!format_) {
492 HAL_LOGE("Stream format must be set before setting up buffers.");
493 return -ENODEV;
494 }
495
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700496 // "Request" a buffer (since we're using a userspace buffer, this just
497 // tells V4L2 to switch into userspace buffer mode).
498 v4l2_requestbuffers req_buffers;
499 memset(&req_buffers, 0, sizeof(req_buffers));
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700500 req_buffers.type = format_->type();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700501 req_buffers.memory = V4L2_MEMORY_USERPTR;
502 req_buffers.count = 1;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700503
504 int res = IoctlLocked(VIDIOC_REQBUFS, &req_buffers);
505 // Calling REQBUFS releases all queued buffers back to the user.
506 int gralloc_res = gralloc_->unlockAllBuffers();
507 if (res < 0) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700508 HAL_LOGE("REQBUFS failed: %s", strerror(errno));
509 return -ENODEV;
510 }
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700511 if (gralloc_res < 0) {
512 HAL_LOGE("Failed to unlock all buffers when setting up new buffers.");
513 return gralloc_res;
514 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700515
516 // V4L2 will set req_buffers.count to a number of buffers it can handle.
517 max_buffers_ = req_buffers.count;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700518 // Sanity check.
519 if (max_buffers_ < 1) {
520 HAL_LOGE("REQBUFS claims it can't handle any buffers.");
521 return -ENODEV;
522 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700523 return 0;
524}
525
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700526int V4L2Wrapper::EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer) {
527 HAL_LOG_ENTER();
528
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700529 if (!format_) {
530 HAL_LOGE("Stream format must be set before enqueuing buffers.");
531 return -ENODEV;
532 }
533
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700534 // Set up a v4l2 buffer struct.
535 v4l2_buffer device_buffer;
536 memset(&device_buffer, 0, sizeof(device_buffer));
537 device_buffer.type = format_->type();
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700538 // TODO(b/29334616): when this is async, actually limit the number
539 // of buffers used to the known max, and set this according to the
540 // queue length.
541 device_buffer.index = 0;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700542
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700543 // Use QUERYBUF to ensure our buffer/device is in good shape,
544 // and fill out remaining fields.
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700545 if (IoctlLocked(VIDIOC_QUERYBUF, &device_buffer) < 0) {
546 HAL_LOGE("QUERYBUF fails: %s", strerror(errno));
547 return -ENODEV;
548 }
549
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700550 // Lock the buffer for writing (fills in the user pointer field).
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700551 int res =
552 gralloc_->lock(camera_buffer, format_->bytes_per_line(), &device_buffer);
553 if (res) {
554 HAL_LOGE("Gralloc failed to lock buffer.");
555 return res;
556 }
557 if (IoctlLocked(VIDIOC_QBUF, &device_buffer) < 0) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700558 HAL_LOGE("QBUF fails: %s", strerror(errno));
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700559 gralloc_->unlock(&device_buffer);
560 return -ENODEV;
561 }
562
563 return 0;
564}
565
566int V4L2Wrapper::DequeueBuffer(v4l2_buffer* buffer) {
567 HAL_LOG_ENTER();
568
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700569 if (!format_) {
570 HAL_LOGE("Stream format must be set before dequeueing buffers.");
571 return -ENODEV;
572 }
573
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700574 memset(buffer, 0, sizeof(*buffer));
575 buffer->type = format_->type();
576 buffer->memory = V4L2_MEMORY_USERPTR;
577 if (IoctlLocked(VIDIOC_DQBUF, buffer) < 0) {
578 HAL_LOGE("DQBUF fails: %s", strerror(errno));
579 return -ENODEV;
580 }
581
582 // Now that we're done painting the buffer, we can unlock it.
583 int res = gralloc_->unlock(buffer);
584 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700585 HAL_LOGE("Gralloc failed to unlock buffer after dequeueing.");
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700586 return res;
587 }
588
589 return 0;
590}
591
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700592} // namespace v4l2_camera_hal