blob: a0c5f04dbb3446fcd05103dc6f862deee9ee51e6 [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 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-Cohenabbf9cc2016-08-23 11:59:59 -0700106 device_path_.c_str());
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700107 return;
108 }
109
110 --connection_count_;
111 if (connection_count_ > 0) {
112 HAL_LOGV("Disconnected from camera device %s. %d connections remain.",
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700113 device_path_.c_str());
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700114 return;
115 }
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700116
117 device_fd_.reset(); // Includes close().
118 format_.reset();
Ari Hausman-Cohen0fbcaf52016-09-28 13:21:31 -0700119 buffers_.clear();
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700120 // Closing the device releases all queued buffers back to the user.
121 gralloc_->unlockAllBuffers();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700122}
123
124// Helper function. Should be used instead of ioctl throughout this class.
125template <typename T>
126int V4L2Wrapper::IoctlLocked(int request, T data) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700127 // Potentially called so many times logging entry is a bad idea.
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700128 std::lock_guard<std::mutex> lock(device_lock_);
129
130 if (!connected()) {
131 HAL_LOGE("Device %s not connected.", device_path_.c_str());
132 return -ENODEV;
133 }
134 return TEMP_FAILURE_RETRY(ioctl(device_fd_.get(), request, data));
135}
136
137int V4L2Wrapper::StreamOn() {
138 HAL_LOG_ENTER();
139
140 if (!format_) {
141 HAL_LOGE("Stream format must be set before turning on stream.");
142 return -EINVAL;
143 }
144
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700145 int32_t type = format_->type();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700146 if (IoctlLocked(VIDIOC_STREAMON, &type) < 0) {
147 HAL_LOGE("STREAMON fails: %s", strerror(errno));
148 return -ENODEV;
149 }
150
151 return 0;
152}
153
154int V4L2Wrapper::StreamOff() {
155 HAL_LOG_ENTER();
156
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700157 if (!format_) {
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700158 // Can't have turned on the stream wihtout format being set,
159 // so nothing to turn off here.
160 return 0;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700161 }
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 }
Ari Hausman-Cohen0fbcaf52016-09-28 13:21:31 -0700495 *result_max_buffers = buffers_.size();
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.
Ari Hausman-Cohen0fbcaf52016-09-28 13:21:31 -0700528 if (req_buffers.count < 1) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700529 HAL_LOGE("REQBUFS claims it can't handle any buffers.");
530 return -ENODEV;
531 }
Ari Hausman-Cohen0fbcaf52016-09-28 13:21:31 -0700532 buffers_.resize(req_buffers.count, false);
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-Cohen0fbcaf52016-09-28 13:21:31 -0700545 // Find a free buffer index. Could use some sort of persistent hinting
546 // here to improve expected efficiency, but buffers_.size() is expected
547 // to be low enough (<10 experimentally) that it's not worth it.
548 int index = -1;
549 {
550 std::lock_guard<std::mutex> guard(buffer_queue_lock_);
551 for (int i = 0; i < buffers_.size(); ++i) {
552 if (!buffers_[i]) {
553 index = i;
554 break;
555 }
556 }
557 }
558 if (index < 0) {
559 // Note: The HAL should be tracking the number of buffers in flight
560 // for each stream, and should never overflow the device.
561 HAL_LOGE("Cannot enqueue buffer: stream is already full.");
562 return -ENODEV;
563 }
564
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700565 // Set up a v4l2 buffer struct.
566 v4l2_buffer device_buffer;
567 memset(&device_buffer, 0, sizeof(device_buffer));
568 device_buffer.type = format_->type();
Ari Hausman-Cohen0fbcaf52016-09-28 13:21:31 -0700569 device_buffer.index = index;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700570
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700571 // Use QUERYBUF to ensure our buffer/device is in good shape,
572 // and fill out remaining fields.
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700573 if (IoctlLocked(VIDIOC_QUERYBUF, &device_buffer) < 0) {
574 HAL_LOGE("QUERYBUF fails: %s", strerror(errno));
575 return -ENODEV;
576 }
577
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700578 // Lock the buffer for writing (fills in the user pointer field).
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700579 int res =
580 gralloc_->lock(camera_buffer, format_->bytes_per_line(), &device_buffer);
581 if (res) {
582 HAL_LOGE("Gralloc failed to lock buffer.");
583 return res;
584 }
585 if (IoctlLocked(VIDIOC_QBUF, &device_buffer) < 0) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700586 HAL_LOGE("QBUF fails: %s", strerror(errno));
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700587 gralloc_->unlock(&device_buffer);
588 return -ENODEV;
589 }
590
Ari Hausman-Cohen0fbcaf52016-09-28 13:21:31 -0700591 // Mark the buffer as in flight.
592 std::lock_guard<std::mutex> guard(buffer_queue_lock_);
593 buffers_[index] = true;
594
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700595 return 0;
596}
597
598int V4L2Wrapper::DequeueBuffer(v4l2_buffer* buffer) {
599 HAL_LOG_ENTER();
600
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700601 if (!format_) {
602 HAL_LOGE("Stream format must be set before dequeueing buffers.");
603 return -ENODEV;
604 }
605
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700606 memset(buffer, 0, sizeof(*buffer));
607 buffer->type = format_->type();
608 buffer->memory = V4L2_MEMORY_USERPTR;
609 if (IoctlLocked(VIDIOC_DQBUF, buffer) < 0) {
610 HAL_LOGE("DQBUF fails: %s", strerror(errno));
611 return -ENODEV;
612 }
613
Ari Hausman-Cohen0fbcaf52016-09-28 13:21:31 -0700614 // Mark the buffer as no longer in flight.
615 {
616 std::lock_guard<std::mutex> guard(buffer_queue_lock_);
617 buffers_[buffer->index] = false;
618 }
619
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700620 // Now that we're done painting the buffer, we can unlock it.
621 int res = gralloc_->unlock(buffer);
622 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700623 HAL_LOGE("Gralloc failed to unlock buffer after dequeueing.");
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700624 return res;
625 }
626
627 return 0;
628}
629
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700630} // namespace v4l2_camera_hal