blob: 3d7ba85e72f267dfdffecff0496da994f82d1baa [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>
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070020#include <array>
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070021#include <limits>
22#include <vector>
23
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070024#include <fcntl.h>
25#include <linux/videodev2.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28
29#include <mutex>
30
31#include <nativehelper/ScopedFd.h>
32
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070033#include "common.h"
34#include "stream.h"
35#include "stream_format.h"
36#include "v4l2_gralloc.h"
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070037
38namespace v4l2_camera_hal {
39
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070040const int32_t kStandardSizes[][2] = {
41 {1920, 1080}, {1280, 720}, {640, 480}, {320, 240}};
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070042
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070043V4L2Wrapper* V4L2Wrapper::NewV4L2Wrapper(const std::string device_path) {
44 HAL_LOG_ENTER();
45
46 std::unique_ptr<V4L2Gralloc> gralloc(V4L2Gralloc::NewV4L2Gralloc());
47 if (!gralloc) {
48 HAL_LOGE("Failed to initialize gralloc helper.");
49 return nullptr;
50 }
51
52 return new V4L2Wrapper(device_path, std::move(gralloc));
53}
54
55V4L2Wrapper::V4L2Wrapper(const std::string device_path,
56 std::unique_ptr<V4L2Gralloc> gralloc)
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070057 : device_path_(std::move(device_path)),
58 gralloc_(std::move(gralloc)),
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070059 max_buffers_(0),
60 connection_count_(0) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070061 HAL_LOG_ENTER();
62}
63
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070064V4L2Wrapper::~V4L2Wrapper() {
65 HAL_LOG_ENTER();
66}
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070067
68int V4L2Wrapper::Connect() {
69 HAL_LOG_ENTER();
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070070 std::lock_guard<std::mutex> lock(connection_lock_);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070071
72 if (connected()) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070073 HAL_LOGV("Camera device %s is already connected.", device_path_.c_str());
74 ++connection_count_;
75 return 0;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070076 }
77
78 int fd = TEMP_FAILURE_RETRY(open(device_path_.c_str(), O_RDWR));
79 if (fd < 0) {
80 HAL_LOGE("failed to open %s (%s)", device_path_.c_str(), strerror(errno));
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070081 return -ENODEV;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070082 }
83 device_fd_.reset(fd);
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070084 ++connection_count_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070085
86 // Check if this connection has the extended control query capability.
87 v4l2_query_ext_ctrl query;
88 query.id = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070089 extended_query_supported_ = (IoctlLocked(VIDIOC_QUERY_EXT_CTRL, &query) == 0);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070090
91 // TODO(b/29185945): confirm this is a supported device.
92 // This is checked by the HAL, but the device at device_path_ may
93 // not be the same one that was there when the HAL was loaded.
94 // (Alternatively, better hotplugging support may make this unecessary
95 // by disabling cameras that get disconnected and checking newly connected
96 // cameras, so Connect() is never called on an unsupported camera)
97 return 0;
98}
99
100void V4L2Wrapper::Disconnect() {
101 HAL_LOG_ENTER();
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700102 std::lock_guard<std::mutex> lock(connection_lock_);
103
104 if (connection_count_ == 0) {
105 // Not connected.
106 HAL_LOGE("Camera device %s is not connected, cannot disconnect.",
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700107 device_path_.c_str());
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-Cohenabbf9cc2016-08-23 11:59:59 -0700114 device_path_.c_str());
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700115 return;
116 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700117
118 device_fd_.reset(); // Includes close().
119 format_.reset();
120 max_buffers_ = 0;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700121 // Closing the device releases all queued buffers back to the user.
122 gralloc_->unlockAllBuffers();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700123}
124
125// Helper function. Should be used instead of ioctl throughout this class.
126template <typename T>
127int V4L2Wrapper::IoctlLocked(int request, T data) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700128 // Potentially called so many times logging entry is a bad idea.
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700129 std::lock_guard<std::mutex> lock(device_lock_);
130
131 if (!connected()) {
132 HAL_LOGE("Device %s not connected.", device_path_.c_str());
133 return -ENODEV;
134 }
135 return TEMP_FAILURE_RETRY(ioctl(device_fd_.get(), request, data));
136}
137
138int V4L2Wrapper::StreamOn() {
139 HAL_LOG_ENTER();
140
141 if (!format_) {
142 HAL_LOGE("Stream format must be set before turning on stream.");
143 return -EINVAL;
144 }
145
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700146 int32_t type = format_->type();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700147 if (IoctlLocked(VIDIOC_STREAMON, &type) < 0) {
148 HAL_LOGE("STREAMON fails: %s", strerror(errno));
149 return -ENODEV;
150 }
151
152 return 0;
153}
154
155int V4L2Wrapper::StreamOff() {
156 HAL_LOG_ENTER();
157
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700158 if (!format_) {
159 HAL_LOGE("Stream format must be set to turn off stream.");
160 return -ENODEV;
161 }
162
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700163 int32_t type = format_->type();
164 int res = IoctlLocked(VIDIOC_STREAMOFF, &type);
165 // Calling STREAMOFF releases all queued buffers back to the user.
166 int gralloc_res = gralloc_->unlockAllBuffers();
167 if (res < 0) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700168 HAL_LOGE("STREAMOFF fails: %s", strerror(errno));
169 return -ENODEV;
170 }
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700171 if (gralloc_res < 0) {
172 HAL_LOGE("Failed to unlock all buffers after turning stream off.");
173 return gralloc_res;
174 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700175
176 return 0;
177}
178
179int V4L2Wrapper::QueryControl(uint32_t control_id,
180 v4l2_query_ext_ctrl* result) {
181 HAL_LOG_ENTER();
182 int res;
183
184 memset(result, 0, sizeof(*result));
185
186 if (extended_query_supported_) {
187 result->id = control_id;
188 res = IoctlLocked(VIDIOC_QUERY_EXT_CTRL, result);
189 // Assuming the operation was supported (not ENOTTY), no more to do.
190 if (errno != ENOTTY) {
191 if (res) {
192 HAL_LOGE("QUERY_EXT_CTRL fails: %s", strerror(errno));
193 return -ENODEV;
194 }
195 return 0;
196 }
197 }
198
199 // Extended control querying not supported, fall back to basic control query.
200 v4l2_queryctrl query;
201 query.id = control_id;
202 if (IoctlLocked(VIDIOC_QUERYCTRL, &query)) {
203 HAL_LOGE("QUERYCTRL fails: %s", strerror(errno));
204 return -ENODEV;
205 }
206
207 // Convert the basic result to the extended result.
208 result->id = query.id;
209 result->type = query.type;
210 memcpy(result->name, query.name, sizeof(query.name));
211 result->minimum = query.minimum;
212 if (query.type == V4L2_CTRL_TYPE_BITMASK) {
213 // According to the V4L2 documentation, when type is BITMASK,
214 // max and default should be interpreted as __u32. Practically,
215 // this means the conversion from 32 bit to 64 will pad with 0s not 1s.
216 result->maximum = static_cast<uint32_t>(query.maximum);
217 result->default_value = static_cast<uint32_t>(query.default_value);
218 } else {
219 result->maximum = query.maximum;
220 result->default_value = query.default_value;
221 }
222 result->step = static_cast<uint32_t>(query.step);
223 result->flags = query.flags;
224 result->elems = 1;
225 switch (result->type) {
226 case V4L2_CTRL_TYPE_INTEGER64:
227 result->elem_size = sizeof(int64_t);
228 break;
229 case V4L2_CTRL_TYPE_STRING:
230 result->elem_size = result->maximum + 1;
231 break;
232 default:
233 result->elem_size = sizeof(int32_t);
234 break;
235 }
236
237 return 0;
238}
239
240int V4L2Wrapper::GetControl(uint32_t control_id, int32_t* value) {
241 HAL_LOG_ENTER();
242
Ari Hausman-Cohen7a1fba62016-08-10 11:31:04 -0700243 // For extended controls (any control class other than "user"),
244 // G_EXT_CTRL must be used instead of G_CTRL.
245 if (V4L2_CTRL_ID2CLASS(control_id) != V4L2_CTRL_CLASS_USER) {
246 v4l2_ext_control control;
247 v4l2_ext_controls controls;
248 memset(&control, 0, sizeof(control));
249 memset(&controls, 0, sizeof(controls));
250
251 control.id = control_id;
252 controls.ctrl_class = V4L2_CTRL_ID2CLASS(control_id);
253 controls.count = 1;
254 controls.controls = &control;
255
256 if (IoctlLocked(VIDIOC_G_EXT_CTRLS, &controls) < 0) {
257 HAL_LOGE("G_EXT_CTRLS fails: %s", strerror(errno));
258 return -ENODEV;
259 }
260 *value = control.value;
261 } else {
262 v4l2_control control{control_id, 0};
263 if (IoctlLocked(VIDIOC_G_CTRL, &control) < 0) {
264 HAL_LOGE("G_CTRL fails: %s", strerror(errno));
265 return -ENODEV;
266 }
267 *value = control.value;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700268 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700269 return 0;
270}
271
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700272int V4L2Wrapper::SetControl(uint32_t control_id,
273 int32_t desired,
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700274 int32_t* result) {
275 HAL_LOG_ENTER();
Ari Hausman-Cohen7a1fba62016-08-10 11:31:04 -0700276 int32_t result_value = 0;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700277
Ari Hausman-Cohen99f3ea02016-08-02 10:47:07 -0700278 // TODO(b/29334616): When async, this may need to check if the stream
279 // is on, and if so, lock it off while setting format. Need to look
280 // into if V4L2 supports adjusting controls while the stream is on.
281
Ari Hausman-Cohen7a1fba62016-08-10 11:31:04 -0700282 // For extended controls (any control class other than "user"),
283 // S_EXT_CTRL must be used instead of S_CTRL.
284 if (V4L2_CTRL_ID2CLASS(control_id) != V4L2_CTRL_CLASS_USER) {
285 v4l2_ext_control control;
286 v4l2_ext_controls controls;
287 memset(&control, 0, sizeof(control));
288 memset(&controls, 0, sizeof(controls));
289
290 control.id = control_id;
291 control.value = desired;
292 controls.ctrl_class = V4L2_CTRL_ID2CLASS(control_id);
293 controls.count = 1;
294 controls.controls = &control;
295
296 if (IoctlLocked(VIDIOC_S_EXT_CTRLS, &controls) < 0) {
297 HAL_LOGE("S_EXT_CTRLS fails: %s", strerror(errno));
298 return -ENODEV;
299 }
300 result_value = control.value;
301 } else {
302 v4l2_control control{control_id, desired};
303 if (IoctlLocked(VIDIOC_S_CTRL, &control) < 0) {
304 HAL_LOGE("S_CTRL fails: %s", strerror(errno));
305 return -ENODEV;
306 }
307 result_value = control.value;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700308 }
Ari Hausman-Cohen7a1fba62016-08-10 11:31:04 -0700309
Ari Hausman-Cohen99f3ea02016-08-02 10:47:07 -0700310 // If the caller wants to know the result, pass it back.
311 if (result != nullptr) {
Ari Hausman-Cohen7a1fba62016-08-10 11:31:04 -0700312 *result = result_value;
Ari Hausman-Cohen99f3ea02016-08-02 10:47:07 -0700313 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700314 return 0;
315}
316
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700317int V4L2Wrapper::GetFormats(std::set<uint32_t>* v4l2_formats) {
318 HAL_LOG_ENTER();
319
320 v4l2_fmtdesc format_query;
321 memset(&format_query, 0, sizeof(format_query));
322 // TODO(b/30000211): multiplanar support.
323 format_query.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
324 while (IoctlLocked(VIDIOC_ENUM_FMT, &format_query) >= 0) {
325 v4l2_formats->insert(format_query.pixelformat);
326 ++format_query.index;
327 }
328
329 if (errno != EINVAL) {
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700330 HAL_LOGE(
331 "ENUM_FMT fails at index %d: %s", format_query.index, strerror(errno));
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700332 return -ENODEV;
333 }
334 return 0;
335}
336
337int V4L2Wrapper::GetFormatFrameSizes(uint32_t v4l2_format,
338 std::set<std::array<int32_t, 2>>* sizes) {
339 HAL_LOG_ENTER();
340
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-Cohen5d753232016-08-10 14:27:36 -0700358 HAL_LOGE("ENUM_FRAMESIZES fails at index %d: %s",
359 size_query.index,
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700360 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
367 // closest to a set of standard sizes plus largest possible.
368 sizes->insert({{{static_cast<int32_t>(size_query.stepwise.max_width),
369 static_cast<int32_t>(size_query.stepwise.max_height)}}});
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700370 for (const auto size : kStandardSizes) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700371 // Find the closest size, rounding up.
372 uint32_t desired_width = size[0];
373 uint32_t desired_height = size[1];
374 if (desired_width < size_query.stepwise.min_width ||
375 desired_height < size_query.stepwise.min_height) {
376 HAL_LOGV("Standard size %u x %u is too small for format %d",
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700377 desired_width,
378 desired_height,
379 v4l2_format);
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700380 continue;
381 } else if (desired_width > size_query.stepwise.max_width &&
382 desired_height > size_query.stepwise.max_height) {
383 HAL_LOGV("Standard size %u x %u is too big for format %d",
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700384 desired_width,
385 desired_height,
386 v4l2_format);
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700387 continue;
388 }
389
390 // Round up.
391 uint32_t width_steps = (desired_width - size_query.stepwise.min_width +
392 size_query.stepwise.step_width - 1) /
393 size_query.stepwise.step_width;
394 uint32_t height_steps = (desired_height - size_query.stepwise.min_height +
395 size_query.stepwise.step_height - 1) /
396 size_query.stepwise.step_height;
397 sizes->insert(
398 {{{static_cast<int32_t>(size_query.stepwise.min_width +
399 width_steps * size_query.stepwise.step_width),
400 static_cast<int32_t>(size_query.stepwise.min_height +
401 height_steps *
402 size_query.stepwise.step_height)}}});
403 }
404 }
405 return 0;
406}
407
408// Converts a v4l2_fract with units of seconds to an int64_t with units of ns.
409inline int64_t FractToNs(const v4l2_fract& fract) {
410 return (1000000000LL * fract.numerator) / fract.denominator;
411}
412
413int V4L2Wrapper::GetFormatFrameDurationRange(
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700414 uint32_t v4l2_format,
415 const std::array<int32_t, 2>& size,
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700416 std::array<int64_t, 2>* duration_range) {
417 // Potentially called so many times logging entry is a bad idea.
418
419 v4l2_frmivalenum duration_query;
420 memset(&duration_query, 0, sizeof(duration_query));
421 duration_query.pixel_format = v4l2_format;
422 duration_query.width = size[0];
423 duration_query.height = size[1];
424 if (IoctlLocked(VIDIOC_ENUM_FRAMEINTERVALS, &duration_query) < 0) {
425 HAL_LOGE("ENUM_FRAMEINTERVALS failed: %s", strerror(errno));
426 return -ENODEV;
427 }
428
429 int64_t min = std::numeric_limits<int64_t>::max();
430 int64_t max = std::numeric_limits<int64_t>::min();
431 if (duration_query.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
432 // Discrete: enumerate all durations using VIDIOC_ENUM_FRAMEINTERVALS.
433 do {
434 min = std::min(min, FractToNs(duration_query.discrete));
435 max = std::max(max, FractToNs(duration_query.discrete));
436 ++duration_query.index;
437 } while (IoctlLocked(VIDIOC_ENUM_FRAMEINTERVALS, &duration_query) >= 0);
438 if (errno != EINVAL) {
439 HAL_LOGE("ENUM_FRAMEINTERVALS fails at index %d: %s",
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700440 duration_query.index,
441 strerror(errno));
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700442 return -ENODEV;
443 }
444 } else {
445 // Continuous/Step-wise: simply convert the given min and max.
446 min = FractToNs(duration_query.stepwise.min);
447 max = FractToNs(duration_query.stepwise.max);
448 }
449 (*duration_range)[0] = min;
450 (*duration_range)[1] = max;
451 return 0;
452}
453
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700454int V4L2Wrapper::SetFormat(const default_camera_hal::Stream& stream,
455 uint32_t* result_max_buffers) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700456 HAL_LOG_ENTER();
457
458 // Should be checked earlier; sanity check.
459 if (stream.isInputType()) {
460 HAL_LOGE("Input streams not supported.");
461 return -EINVAL;
462 }
463
464 StreamFormat desired_format(stream);
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700465 if (format_ && desired_format == *format_) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700466 HAL_LOGV("Already in correct format, skipping format setting.");
467 return 0;
468 }
469
470 // Not in the correct format, set our format.
471 v4l2_format new_format;
472 desired_format.FillFormatRequest(&new_format);
473 // TODO(b/29334616): When async, this will need to check if the stream
474 // is on, and if so, lock it off while setting format.
475 if (IoctlLocked(VIDIOC_S_FMT, &new_format) < 0) {
476 HAL_LOGE("S_FMT failed: %s", strerror(errno));
477 return -ENODEV;
478 }
479
480 // Check that the driver actually set to the requested values.
481 if (desired_format != new_format) {
482 HAL_LOGE("Device doesn't support desired stream configuration.");
483 return -EINVAL;
484 }
485
486 // Keep track of our new format.
487 format_.reset(new StreamFormat(new_format));
488
489 // Format changed, setup new buffers.
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700490 int res = SetupBuffers();
491 if (res) {
492 HAL_LOGE("Failed to set up buffers for new format.");
493 return res;
494 }
495 *result_max_buffers = max_buffers_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700496 return 0;
497}
498
499int V4L2Wrapper::SetupBuffers() {
500 HAL_LOG_ENTER();
501
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700502 if (!format_) {
503 HAL_LOGE("Stream format must be set before setting up buffers.");
504 return -ENODEV;
505 }
506
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700507 // "Request" a buffer (since we're using a userspace buffer, this just
508 // tells V4L2 to switch into userspace buffer mode).
509 v4l2_requestbuffers req_buffers;
510 memset(&req_buffers, 0, sizeof(req_buffers));
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700511 req_buffers.type = format_->type();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700512 req_buffers.memory = V4L2_MEMORY_USERPTR;
513 req_buffers.count = 1;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700514
515 int res = IoctlLocked(VIDIOC_REQBUFS, &req_buffers);
516 // Calling REQBUFS releases all queued buffers back to the user.
517 int gralloc_res = gralloc_->unlockAllBuffers();
518 if (res < 0) {
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700519 HAL_LOGE("REQBUFS failed: %s", strerror(errno));
520 return -ENODEV;
521 }
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700522 if (gralloc_res < 0) {
523 HAL_LOGE("Failed to unlock all buffers when setting up new buffers.");
524 return gralloc_res;
525 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700526
527 // V4L2 will set req_buffers.count to a number of buffers it can handle.
528 max_buffers_ = req_buffers.count;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700529 // Sanity check.
530 if (max_buffers_ < 1) {
531 HAL_LOGE("REQBUFS claims it can't handle any buffers.");
532 return -ENODEV;
533 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700534 return 0;
535}
536
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700537int V4L2Wrapper::EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer) {
538 HAL_LOG_ENTER();
539
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700540 if (!format_) {
541 HAL_LOGE("Stream format must be set before enqueuing buffers.");
542 return -ENODEV;
543 }
544
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700545 // Set up a v4l2 buffer struct.
546 v4l2_buffer device_buffer;
547 memset(&device_buffer, 0, sizeof(device_buffer));
548 device_buffer.type = format_->type();
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700549 // TODO(b/29334616): when this is async, actually limit the number
550 // of buffers used to the known max, and set this according to the
551 // queue length.
552 device_buffer.index = 0;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700553
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700554 // Use QUERYBUF to ensure our buffer/device is in good shape,
555 // and fill out remaining fields.
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700556 if (IoctlLocked(VIDIOC_QUERYBUF, &device_buffer) < 0) {
557 HAL_LOGE("QUERYBUF fails: %s", strerror(errno));
558 return -ENODEV;
559 }
560
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700561 // Lock the buffer for writing (fills in the user pointer field).
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700562 int res =
563 gralloc_->lock(camera_buffer, format_->bytes_per_line(), &device_buffer);
564 if (res) {
565 HAL_LOGE("Gralloc failed to lock buffer.");
566 return res;
567 }
568 if (IoctlLocked(VIDIOC_QBUF, &device_buffer) < 0) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700569 HAL_LOGE("QBUF fails: %s", strerror(errno));
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700570 gralloc_->unlock(&device_buffer);
571 return -ENODEV;
572 }
573
574 return 0;
575}
576
577int V4L2Wrapper::DequeueBuffer(v4l2_buffer* buffer) {
578 HAL_LOG_ENTER();
579
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700580 if (!format_) {
581 HAL_LOGE("Stream format must be set before dequeueing buffers.");
582 return -ENODEV;
583 }
584
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700585 memset(buffer, 0, sizeof(*buffer));
586 buffer->type = format_->type();
587 buffer->memory = V4L2_MEMORY_USERPTR;
588 if (IoctlLocked(VIDIOC_DQBUF, buffer) < 0) {
589 HAL_LOGE("DQBUF fails: %s", strerror(errno));
590 return -ENODEV;
591 }
592
593 // Now that we're done painting the buffer, we can unlock it.
594 int res = gralloc_->unlock(buffer);
595 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700596 HAL_LOGE("Gralloc failed to unlock buffer after dequeueing.");
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700597 return res;
598 }
599
600 return 0;
601}
602
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700603} // namespace v4l2_camera_hal