blob: 99b7a93546909f2107f808a873e4482f0a117e61 [file] [log] [blame]
Ari Hausman-Cohen73442152016-06-08 15:50:49 -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
Sergii Piatakov2ad591f2018-08-03 11:41:06 +030017//#define LOG_NDEBUG 0
18#define LOG_TAG "V4L2Camera"
19
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070020#include "v4l2_camera.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070021
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070022#include <cstdlib>
Sergii Piatakovb8f073f2018-10-10 17:22:10 +030023#include <fcntl.h>
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070024
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070025#include <camera/CameraMetadata.h>
26#include <hardware/camera3.h>
Sergii Piatakovb8f073f2018-10-10 17:22:10 +030027#include <linux/videodev2.h>
28#include <sys/stat.h>
29#include <sys/types.h>
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070030#include "common.h"
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -070031#include "function_thread.h"
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070032#include "metadata/metadata_common.h"
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070033#include "stream_format.h"
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070034#include "v4l2_metadata_factory.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070035
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070036#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
37
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070038namespace v4l2_camera_hal {
39
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070040V4L2Camera* V4L2Camera::NewV4L2Camera(int id, const std::string path) {
41 HAL_LOG_ENTER();
42
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070043 std::shared_ptr<V4L2Wrapper> v4l2_wrapper(V4L2Wrapper::NewV4L2Wrapper(path));
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070044 if (!v4l2_wrapper) {
45 HAL_LOGE("Failed to initialize V4L2 wrapper.");
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070046 return nullptr;
47 }
48
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070049 std::unique_ptr<Metadata> metadata;
50 int res = GetV4L2Metadata(v4l2_wrapper, &metadata);
51 if (res) {
52 HAL_LOGE("Failed to initialize V4L2 metadata: %d", res);
53 return nullptr;
54 }
55
56 return new V4L2Camera(id, std::move(v4l2_wrapper), std::move(metadata));
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070057}
58
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -070059V4L2Camera::V4L2Camera(int id,
60 std::shared_ptr<V4L2Wrapper> v4l2_wrapper,
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070061 std::unique_ptr<Metadata> metadata)
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070062 : default_camera_hal::Camera(id),
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070063 device_(std::move(v4l2_wrapper)),
64 metadata_(std::move(metadata)),
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -070065 buffer_enqueuer_(new FunctionThread(
66 std::bind(&V4L2Camera::enqueueRequestBuffers, this))),
67 buffer_dequeuer_(new FunctionThread(
Sergii Piatakov1bd0add2018-08-06 09:23:22 +030068 std::bind(&V4L2Camera::dequeueRequestBuffers, this))),
69 max_input_streams_(0),
70 max_output_streams_({{0, 0, 0}}) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070071 HAL_LOG_ENTER();
72}
73
74V4L2Camera::~V4L2Camera() {
75 HAL_LOG_ENTER();
76}
77
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070078int V4L2Camera::connect() {
79 HAL_LOG_ENTER();
80
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070081 if (connection_) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070082 HAL_LOGE("Already connected. Please disconnect and try again.");
83 return -EIO;
84 }
85
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070086 connection_.reset(new V4L2Wrapper::Connection(device_));
87 if (connection_->status()) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070088 HAL_LOGE("Failed to connect to device.");
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070089 return connection_->status();
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070090 }
91
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070092 // TODO(b/29185945): confirm this is a supported device.
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070093 // This is checked by the HAL, but the device at |device_|'s path may
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070094 // not be the same one that was there when the HAL was loaded.
95 // (Alternatively, better hotplugging support may make this unecessary
96 // by disabling cameras that get disconnected and checking newly connected
97 // cameras, so connect() is never called on an unsupported camera)
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070098
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -070099 // TODO(b/29158098): Inform service of any flashes that are no longer
100 // available because this camera is in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700101 return 0;
102}
103
104void V4L2Camera::disconnect() {
105 HAL_LOG_ENTER();
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700106
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700107 connection_.reset();
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700108
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700109 // TODO(b/29158098): Inform service of any flashes that are available again
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700110 // because this camera is no longer in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700111}
112
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800113int V4L2Camera::flushBuffers() {
114 HAL_LOG_ENTER();
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700115 return device_->StreamOff();
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800116}
117
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700118int V4L2Camera::initStaticInfo(android::CameraMetadata* out) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700119 HAL_LOG_ENTER();
120
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700121 int res = metadata_->FillStaticMetadata(out);
122 if (res) {
123 HAL_LOGE("Failed to get static metadata.");
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700124 return res;
125 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700126
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700127 // Extract max streams for use in verifying stream configs.
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -0700128 res = SingleTagValue(
129 *out, ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, &max_input_streams_);
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700130 if (res) {
131 HAL_LOGE("Failed to get max num input streams from static metadata.");
132 return res;
133 }
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -0700134 res = SingleTagValue(
135 *out, ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS, &max_output_streams_);
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700136 if (res) {
137 HAL_LOGE("Failed to get max num output streams from static metadata.");
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700138 return res;
139 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700140
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700141 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700142}
143
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700144int V4L2Camera::initTemplate(int type, android::CameraMetadata* out) {
145 HAL_LOG_ENTER();
146
147 return metadata_->GetRequestTemplate(type, out);
148}
149
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700150void V4L2Camera::initDeviceInfo(camera_info_t* info) {
151 HAL_LOG_ENTER();
152
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700153 // TODO(b/31044975): move this into device interface.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700154 // For now, just constants.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700155 info->resource_cost = 100;
156 info->conflicting_devices = nullptr;
157 info->conflicting_devices_length = 0;
158}
159
160int V4L2Camera::initDevice() {
161 HAL_LOG_ENTER();
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700162
163 // Start the buffer enqueue/dequeue threads if they're not already running.
164 if (!buffer_enqueuer_->isRunning()) {
165 android::status_t res = buffer_enqueuer_->run("Enqueue buffers");
166 if (res != android::OK) {
167 HAL_LOGE("Failed to start buffer enqueue thread: %d", res);
168 return -ENODEV;
169 }
170 }
171 if (!buffer_dequeuer_->isRunning()) {
172 android::status_t res = buffer_dequeuer_->run("Dequeue buffers");
173 if (res != android::OK) {
174 HAL_LOGE("Failed to start buffer dequeue thread: %d", res);
175 return -ENODEV;
176 }
177 }
178
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700179 return 0;
180}
181
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700182int V4L2Camera::enqueueRequest(
183 std::shared_ptr<default_camera_hal::CaptureRequest> request) {
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700184 HAL_LOG_ENTER();
185
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700186 // Assume request validated before calling this function.
187 // (For now, always exactly 1 output buffer, no inputs).
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700188 {
189 std::lock_guard<std::mutex> guard(request_queue_lock_);
190 request_queue_.push(request);
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800191 requests_available_.notify_one();
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700192 }
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700193
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700194 return 0;
195}
196
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700197std::shared_ptr<default_camera_hal::CaptureRequest>
198V4L2Camera::dequeueRequest() {
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700199 std::unique_lock<std::mutex> lock(request_queue_lock_);
200 while (request_queue_.empty()) {
201 requests_available_.wait(lock);
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700202 }
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700203
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700204 std::shared_ptr<default_camera_hal::CaptureRequest> request =
205 request_queue_.front();
206 request_queue_.pop();
207 return request;
208}
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700209
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700210bool V4L2Camera::enqueueRequestBuffers() {
211 // Get a request from the queue (blocks this thread until one is available).
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700212 std::shared_ptr<default_camera_hal::CaptureRequest> request =
213 dequeueRequest();
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700214
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700215 // Assume request validated before being added to the queue
216 // (For now, always exactly 1 output buffer, no inputs).
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700217
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700218 // Setting and getting settings are best effort here,
219 // since there's no way to know through V4L2 exactly what
220 // settings are used for a buffer unless we were to enqueue them
221 // one at a time, which would be too slow.
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700222
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700223 // Set the requested settings
224 int res = metadata_->SetRequestSettings(request->settings);
225 if (res) {
226 HAL_LOGE("Failed to set settings.");
227 completeRequest(request, res);
228 return true;
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700229 }
230
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700231 // Replace the requested settings with a snapshot of
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800232 // the used settings/state immediately before enqueue.
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700233 res = metadata_->FillResultMetadata(&request->settings);
234 if (res) {
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800235 // Note: since request is a shared pointer, this may happen if another
236 // thread has already decided to complete the request (e.g. via flushing),
237 // since that locks the metadata (in that case, this failing is fine,
238 // and completeRequest will simply do nothing).
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700239 HAL_LOGE("Failed to fill result metadata.");
240 completeRequest(request, res);
241 return true;
242 }
243
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800244 // Actually enqueue the buffer for capture.
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700245 res = device_->EnqueueRequest(request);
246 if (res) {
247 HAL_LOGE("Device failed to enqueue buffer.");
248 completeRequest(request, res);
249 return true;
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700250 }
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700251
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700252 // Make sure the stream is on (no effect if already on).
253 res = device_->StreamOn();
254 if (res) {
255 HAL_LOGE("Device failed to turn on stream.");
256 // Don't really want to send an error for only the request here,
257 // since this is a full device error.
258 // TODO: Should trigger full flush.
259 return true;
260 }
261
262 std::unique_lock<std::mutex> lock(in_flight_lock_);
263 in_flight_buffer_count_++;
264 buffers_in_flight_.notify_one();
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700265 return true;
266}
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700267
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700268bool V4L2Camera::dequeueRequestBuffers() {
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800269 // Dequeue a buffer.
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700270 std::shared_ptr<default_camera_hal::CaptureRequest> request;
Jaesung Chung1fc9b612017-11-10 18:09:38 +0900271 int res;
272
273 {
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700274 std::unique_lock<std::mutex> lock(in_flight_lock_);
275 res = device_->DequeueRequest(&request);
Jaesung Chung1fc9b612017-11-10 18:09:38 +0900276 if (!res) {
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700277 if (request) {
278 completeRequest(request, res);
279 in_flight_buffer_count_--;
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800280 }
Jaesung Chung1fc9b612017-11-10 18:09:38 +0900281 return true;
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800282 }
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700283 }
284
Jaesung Chung1fc9b612017-11-10 18:09:38 +0900285 if (res == -EAGAIN) {
286 // EAGAIN just means nothing to dequeue right now.
287 // Wait until something is available before looping again.
288 std::unique_lock<std::mutex> lock(in_flight_lock_);
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700289 while (in_flight_buffer_count_ == 0) {
Jaesung Chung1fc9b612017-11-10 18:09:38 +0900290 buffers_in_flight_.wait(lock);
291 }
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800292 } else {
Jaesung Chung1fc9b612017-11-10 18:09:38 +0900293 HAL_LOGW("Device failed to dequeue buffer: %d", res);
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800294 }
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700295 return true;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700296}
297
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800298bool V4L2Camera::validateDataspacesAndRotations(
299 const camera3_stream_configuration_t* stream_config) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700300 HAL_LOG_ENTER();
301
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800302 for (uint32_t i = 0; i < stream_config->num_streams; ++i) {
303 if (stream_config->streams[i]->rotation != CAMERA3_STREAM_ROTATION_0) {
304 HAL_LOGV("Rotation %d for stream %d not supported",
305 stream_config->streams[i]->rotation,
306 i);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700307 return false;
308 }
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800309 // Accept all dataspaces, as it will just be overwritten below anyways.
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700310 }
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700311 return true;
312}
313
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800314int V4L2Camera::setupStreams(camera3_stream_configuration_t* stream_config) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700315 HAL_LOG_ENTER();
316
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800317 std::lock_guard<std::mutex> guard(in_flight_lock_);
318 // The framework should be enforcing this, but doesn't hurt to be safe.
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700319 if (device_->GetInFlightBufferCount() != 0) {
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800320 HAL_LOGE("Can't set device format while frames are in flight.");
321 return -EINVAL;
322 }
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700323 in_flight_buffer_count_ = 0;
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800324
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800325 // stream_config should have been validated; assume at least 1 stream.
326 camera3_stream_t* stream = stream_config->streams[0];
327 int format = stream->format;
328 uint32_t width = stream->width;
329 uint32_t height = stream->height;
330
331 if (stream_config->num_streams > 1) {
332 // TODO(b/29939583): V4L2 doesn't actually support more than 1
333 // stream at a time. If not all streams are the same format
334 // and size, error. Note that this means the HAL is not spec-compliant.
335 // Technically, this error should be thrown during validation, but
336 // since it isn't a spec-valid error validation isn't set up to check it.
337 for (uint32_t i = 1; i < stream_config->num_streams; ++i) {
338 stream = stream_config->streams[i];
339 if (stream->format != format || stream->width != width ||
340 stream->height != height) {
341 HAL_LOGE(
342 "V4L2 only supports 1 stream configuration at a time "
343 "(stream 0 is format %d, width %u, height %u, "
344 "stream %d is format %d, width %u, height %u).",
345 format,
346 width,
347 height,
348 i,
349 stream->format,
350 stream->width,
351 stream->height);
352 return -EINVAL;
353 }
354 }
355 }
356
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800357 // Ensure the stream is off.
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700358 int res = device_->StreamOff();
359 if (res) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800360 HAL_LOGE("Device failed to turn off stream for reconfiguration: %d.", res);
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700361 return -ENODEV;
362 }
363
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800364 StreamFormat stream_format(format, width, height);
365 uint32_t max_buffers = 0;
366 res = device_->SetFormat(stream_format, &max_buffers);
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700367 if (res) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800368 HAL_LOGE("Failed to set device to correct format for stream: %d.", res);
369 return -ENODEV;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700370 }
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800371
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700372 // Sanity check.
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800373 if (max_buffers < 1) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700374 HAL_LOGE("Setting format resulted in an invalid maximum of %u buffers.",
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800375 max_buffers);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700376 return -ENODEV;
377 }
378
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800379 // Set all the streams dataspaces, usages, and max buffers.
380 for (uint32_t i = 0; i < stream_config->num_streams; ++i) {
381 stream = stream_config->streams[i];
382
Jaesung Chung6e8afea2017-11-15 09:30:41 +0900383 // Override HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED format.
384 if (stream->format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
385 stream->format = HAL_PIXEL_FORMAT_RGBA_8888;
386 }
387
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800388 // Max buffers as reported by the device.
389 stream->max_buffers = max_buffers;
390
391 // Usage: currently using sw graphics.
392 switch (stream->stream_type) {
393 case CAMERA3_STREAM_INPUT:
394 stream->usage = GRALLOC_USAGE_SW_READ_OFTEN;
395 break;
396 case CAMERA3_STREAM_OUTPUT:
397 stream->usage = GRALLOC_USAGE_SW_WRITE_OFTEN;
398 break;
399 case CAMERA3_STREAM_BIDIRECTIONAL:
400 stream->usage =
401 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
402 break;
403 default:
404 // nothing to do.
405 break;
406 }
407
408 // Doesn't matter what was requested, we always use dataspace V0_JFIF.
409 // Note: according to camera3.h, this isn't allowed, but the camera
410 // framework team claims it's underdocumented; the implementation lets the
411 // HAL overwrite it. If this is changed, change the validation above.
412 stream->data_space = HAL_DATASPACE_V0_JFIF;
413 }
414
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700415 return 0;
416}
417
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800418bool V4L2Camera::isValidRequestSettings(
419 const android::CameraMetadata& settings) {
420 if (!metadata_->IsValidRequest(settings)) {
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700421 HAL_LOGE("Invalid request settings.");
422 return false;
423 }
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700424 return true;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700425}
426
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700427} // namespace v4l2_camera_hal