blob: 558b45356b80b5e9e26ccfd2539c3a00e582a6e2 [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-Cohen345bd3a2016-06-13 15:33:53 -070022#include <fcntl.h>
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070023#include <linux/videodev2.h>
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070024#include <sys/stat.h>
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -070025#include <sys/types.h>
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070026
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070027#include <cstdlib>
28
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070029#include <camera/CameraMetadata.h>
30#include <hardware/camera3.h>
31
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070032#include "common.h"
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -070033#include "function_thread.h"
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070034#include "metadata/metadata_common.h"
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070035#include "stream_format.h"
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070036#include "v4l2_metadata_factory.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070037
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070038#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
39
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070040namespace v4l2_camera_hal {
41
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070042V4L2Camera* V4L2Camera::NewV4L2Camera(int id, const std::string path) {
43 HAL_LOG_ENTER();
44
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070045 std::shared_ptr<V4L2Wrapper> v4l2_wrapper(V4L2Wrapper::NewV4L2Wrapper(path));
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070046 if (!v4l2_wrapper) {
47 HAL_LOGE("Failed to initialize V4L2 wrapper.");
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070048 return nullptr;
49 }
50
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070051 std::unique_ptr<Metadata> metadata;
52 int res = GetV4L2Metadata(v4l2_wrapper, &metadata);
53 if (res) {
54 HAL_LOGE("Failed to initialize V4L2 metadata: %d", res);
55 return nullptr;
56 }
57
58 return new V4L2Camera(id, std::move(v4l2_wrapper), std::move(metadata));
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070059}
60
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -070061V4L2Camera::V4L2Camera(int id,
62 std::shared_ptr<V4L2Wrapper> v4l2_wrapper,
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070063 std::unique_ptr<Metadata> metadata)
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070064 : default_camera_hal::Camera(id),
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070065 device_(std::move(v4l2_wrapper)),
66 metadata_(std::move(metadata)),
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -070067 buffer_enqueuer_(new FunctionThread(
68 std::bind(&V4L2Camera::enqueueRequestBuffers, this))),
69 buffer_dequeuer_(new FunctionThread(
Sergii Piatakov1bd0add2018-08-06 09:23:22 +030070 std::bind(&V4L2Camera::dequeueRequestBuffers, this))),
71 max_input_streams_(0),
72 max_output_streams_({{0, 0, 0}}) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070073 HAL_LOG_ENTER();
74}
75
76V4L2Camera::~V4L2Camera() {
77 HAL_LOG_ENTER();
78}
79
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070080int V4L2Camera::connect() {
81 HAL_LOG_ENTER();
82
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070083 if (connection_) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070084 HAL_LOGE("Already connected. Please disconnect and try again.");
85 return -EIO;
86 }
87
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070088 connection_.reset(new V4L2Wrapper::Connection(device_));
89 if (connection_->status()) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070090 HAL_LOGE("Failed to connect to device.");
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070091 return connection_->status();
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070092 }
93
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070094 // TODO(b/29185945): confirm this is a supported device.
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070095 // This is checked by the HAL, but the device at |device_|'s path may
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070096 // not be the same one that was there when the HAL was loaded.
97 // (Alternatively, better hotplugging support may make this unecessary
98 // by disabling cameras that get disconnected and checking newly connected
99 // cameras, so connect() is never called on an unsupported camera)
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700100
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -0700101 // TODO(b/29158098): Inform service of any flashes that are no longer
102 // available because this camera is in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700103 return 0;
104}
105
106void V4L2Camera::disconnect() {
107 HAL_LOG_ENTER();
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700108
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700109 connection_.reset();
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700110
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700111 // TODO(b/29158098): Inform service of any flashes that are available again
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700112 // because this camera is no longer in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700113}
114
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800115int V4L2Camera::flushBuffers() {
116 HAL_LOG_ENTER();
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700117 return device_->StreamOff();
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800118}
119
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700120int V4L2Camera::initStaticInfo(android::CameraMetadata* out) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700121 HAL_LOG_ENTER();
122
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700123 int res = metadata_->FillStaticMetadata(out);
124 if (res) {
125 HAL_LOGE("Failed to get static metadata.");
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700126 return res;
127 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700128
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700129 // Extract max streams for use in verifying stream configs.
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -0700130 res = SingleTagValue(
131 *out, ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, &max_input_streams_);
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700132 if (res) {
133 HAL_LOGE("Failed to get max num input streams from static metadata.");
134 return res;
135 }
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -0700136 res = SingleTagValue(
137 *out, ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS, &max_output_streams_);
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700138 if (res) {
139 HAL_LOGE("Failed to get max num output streams from static metadata.");
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700140 return res;
141 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700142
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700143 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700144}
145
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700146int V4L2Camera::initTemplate(int type, android::CameraMetadata* out) {
147 HAL_LOG_ENTER();
148
149 return metadata_->GetRequestTemplate(type, out);
150}
151
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700152void V4L2Camera::initDeviceInfo(camera_info_t* info) {
153 HAL_LOG_ENTER();
154
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700155 // TODO(b/31044975): move this into device interface.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700156 // For now, just constants.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700157 info->resource_cost = 100;
158 info->conflicting_devices = nullptr;
159 info->conflicting_devices_length = 0;
160}
161
162int V4L2Camera::initDevice() {
163 HAL_LOG_ENTER();
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700164
165 // Start the buffer enqueue/dequeue threads if they're not already running.
166 if (!buffer_enqueuer_->isRunning()) {
167 android::status_t res = buffer_enqueuer_->run("Enqueue buffers");
168 if (res != android::OK) {
169 HAL_LOGE("Failed to start buffer enqueue thread: %d", res);
170 return -ENODEV;
171 }
172 }
173 if (!buffer_dequeuer_->isRunning()) {
174 android::status_t res = buffer_dequeuer_->run("Dequeue buffers");
175 if (res != android::OK) {
176 HAL_LOGE("Failed to start buffer dequeue thread: %d", res);
177 return -ENODEV;
178 }
179 }
180
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700181 return 0;
182}
183
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700184int V4L2Camera::enqueueRequest(
185 std::shared_ptr<default_camera_hal::CaptureRequest> request) {
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700186 HAL_LOG_ENTER();
187
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700188 // Assume request validated before calling this function.
189 // (For now, always exactly 1 output buffer, no inputs).
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700190 {
191 std::lock_guard<std::mutex> guard(request_queue_lock_);
192 request_queue_.push(request);
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800193 requests_available_.notify_one();
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700194 }
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700195
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700196 return 0;
197}
198
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700199std::shared_ptr<default_camera_hal::CaptureRequest>
200V4L2Camera::dequeueRequest() {
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700201 std::unique_lock<std::mutex> lock(request_queue_lock_);
202 while (request_queue_.empty()) {
203 requests_available_.wait(lock);
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700204 }
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700205
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700206 std::shared_ptr<default_camera_hal::CaptureRequest> request =
207 request_queue_.front();
208 request_queue_.pop();
209 return request;
210}
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700211
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700212bool V4L2Camera::enqueueRequestBuffers() {
213 // Get a request from the queue (blocks this thread until one is available).
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700214 std::shared_ptr<default_camera_hal::CaptureRequest> request =
215 dequeueRequest();
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700216
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700217 // Assume request validated before being added to the queue
218 // (For now, always exactly 1 output buffer, no inputs).
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700219
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700220 // Setting and getting settings are best effort here,
221 // since there's no way to know through V4L2 exactly what
222 // settings are used for a buffer unless we were to enqueue them
223 // one at a time, which would be too slow.
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700224
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700225 // Set the requested settings
226 int res = metadata_->SetRequestSettings(request->settings);
227 if (res) {
228 HAL_LOGE("Failed to set settings.");
229 completeRequest(request, res);
230 return true;
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700231 }
232
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700233 // Replace the requested settings with a snapshot of
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800234 // the used settings/state immediately before enqueue.
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700235 res = metadata_->FillResultMetadata(&request->settings);
236 if (res) {
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800237 // Note: since request is a shared pointer, this may happen if another
238 // thread has already decided to complete the request (e.g. via flushing),
239 // since that locks the metadata (in that case, this failing is fine,
240 // and completeRequest will simply do nothing).
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700241 HAL_LOGE("Failed to fill result metadata.");
242 completeRequest(request, res);
243 return true;
244 }
245
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800246 // Actually enqueue the buffer for capture.
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700247 res = device_->EnqueueRequest(request);
248 if (res) {
249 HAL_LOGE("Device failed to enqueue buffer.");
250 completeRequest(request, res);
251 return true;
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700252 }
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700253
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700254 // Make sure the stream is on (no effect if already on).
255 res = device_->StreamOn();
256 if (res) {
257 HAL_LOGE("Device failed to turn on stream.");
258 // Don't really want to send an error for only the request here,
259 // since this is a full device error.
260 // TODO: Should trigger full flush.
261 return true;
262 }
263
264 std::unique_lock<std::mutex> lock(in_flight_lock_);
265 in_flight_buffer_count_++;
266 buffers_in_flight_.notify_one();
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700267 return true;
268}
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700269
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700270bool V4L2Camera::dequeueRequestBuffers() {
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800271 // Dequeue a buffer.
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700272 std::shared_ptr<default_camera_hal::CaptureRequest> request;
Jaesung Chung1fc9b612017-11-10 18:09:38 +0900273 int res;
274
275 {
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700276 std::unique_lock<std::mutex> lock(in_flight_lock_);
277 res = device_->DequeueRequest(&request);
Jaesung Chung1fc9b612017-11-10 18:09:38 +0900278 if (!res) {
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700279 if (request) {
280 completeRequest(request, res);
281 in_flight_buffer_count_--;
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800282 }
Jaesung Chung1fc9b612017-11-10 18:09:38 +0900283 return true;
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800284 }
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700285 }
286
Jaesung Chung1fc9b612017-11-10 18:09:38 +0900287 if (res == -EAGAIN) {
288 // EAGAIN just means nothing to dequeue right now.
289 // Wait until something is available before looping again.
290 std::unique_lock<std::mutex> lock(in_flight_lock_);
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700291 while (in_flight_buffer_count_ == 0) {
Jaesung Chung1fc9b612017-11-10 18:09:38 +0900292 buffers_in_flight_.wait(lock);
293 }
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800294 } else {
Jaesung Chung1fc9b612017-11-10 18:09:38 +0900295 HAL_LOGW("Device failed to dequeue buffer: %d", res);
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800296 }
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700297 return true;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700298}
299
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800300bool V4L2Camera::validateDataspacesAndRotations(
301 const camera3_stream_configuration_t* stream_config) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700302 HAL_LOG_ENTER();
303
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800304 for (uint32_t i = 0; i < stream_config->num_streams; ++i) {
305 if (stream_config->streams[i]->rotation != CAMERA3_STREAM_ROTATION_0) {
306 HAL_LOGV("Rotation %d for stream %d not supported",
307 stream_config->streams[i]->rotation,
308 i);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700309 return false;
310 }
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800311 // Accept all dataspaces, as it will just be overwritten below anyways.
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700312 }
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700313 return true;
314}
315
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800316int V4L2Camera::setupStreams(camera3_stream_configuration_t* stream_config) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700317 HAL_LOG_ENTER();
318
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800319 std::lock_guard<std::mutex> guard(in_flight_lock_);
320 // The framework should be enforcing this, but doesn't hurt to be safe.
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700321 if (device_->GetInFlightBufferCount() != 0) {
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800322 HAL_LOGE("Can't set device format while frames are in flight.");
323 return -EINVAL;
324 }
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700325 in_flight_buffer_count_ = 0;
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800326
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800327 // stream_config should have been validated; assume at least 1 stream.
328 camera3_stream_t* stream = stream_config->streams[0];
329 int format = stream->format;
330 uint32_t width = stream->width;
331 uint32_t height = stream->height;
332
333 if (stream_config->num_streams > 1) {
334 // TODO(b/29939583): V4L2 doesn't actually support more than 1
335 // stream at a time. If not all streams are the same format
336 // and size, error. Note that this means the HAL is not spec-compliant.
337 // Technically, this error should be thrown during validation, but
338 // since it isn't a spec-valid error validation isn't set up to check it.
339 for (uint32_t i = 1; i < stream_config->num_streams; ++i) {
340 stream = stream_config->streams[i];
341 if (stream->format != format || stream->width != width ||
342 stream->height != height) {
343 HAL_LOGE(
344 "V4L2 only supports 1 stream configuration at a time "
345 "(stream 0 is format %d, width %u, height %u, "
346 "stream %d is format %d, width %u, height %u).",
347 format,
348 width,
349 height,
350 i,
351 stream->format,
352 stream->width,
353 stream->height);
354 return -EINVAL;
355 }
356 }
357 }
358
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800359 // Ensure the stream is off.
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700360 int res = device_->StreamOff();
361 if (res) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800362 HAL_LOGE("Device failed to turn off stream for reconfiguration: %d.", res);
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700363 return -ENODEV;
364 }
365
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800366 StreamFormat stream_format(format, width, height);
367 uint32_t max_buffers = 0;
368 res = device_->SetFormat(stream_format, &max_buffers);
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700369 if (res) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800370 HAL_LOGE("Failed to set device to correct format for stream: %d.", res);
371 return -ENODEV;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700372 }
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800373
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700374 // Sanity check.
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800375 if (max_buffers < 1) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700376 HAL_LOGE("Setting format resulted in an invalid maximum of %u buffers.",
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800377 max_buffers);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700378 return -ENODEV;
379 }
380
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800381 // Set all the streams dataspaces, usages, and max buffers.
382 for (uint32_t i = 0; i < stream_config->num_streams; ++i) {
383 stream = stream_config->streams[i];
384
Jaesung Chung6e8afea2017-11-15 09:30:41 +0900385 // Override HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED format.
386 if (stream->format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
387 stream->format = HAL_PIXEL_FORMAT_RGBA_8888;
388 }
389
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800390 // Max buffers as reported by the device.
391 stream->max_buffers = max_buffers;
392
393 // Usage: currently using sw graphics.
394 switch (stream->stream_type) {
395 case CAMERA3_STREAM_INPUT:
396 stream->usage = GRALLOC_USAGE_SW_READ_OFTEN;
397 break;
398 case CAMERA3_STREAM_OUTPUT:
399 stream->usage = GRALLOC_USAGE_SW_WRITE_OFTEN;
400 break;
401 case CAMERA3_STREAM_BIDIRECTIONAL:
402 stream->usage =
403 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
404 break;
405 default:
406 // nothing to do.
407 break;
408 }
409
410 // Doesn't matter what was requested, we always use dataspace V0_JFIF.
411 // Note: according to camera3.h, this isn't allowed, but the camera
412 // framework team claims it's underdocumented; the implementation lets the
413 // HAL overwrite it. If this is changed, change the validation above.
414 stream->data_space = HAL_DATASPACE_V0_JFIF;
415 }
416
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700417 return 0;
418}
419
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800420bool V4L2Camera::isValidRequestSettings(
421 const android::CameraMetadata& settings) {
422 if (!metadata_->IsValidRequest(settings)) {
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700423 HAL_LOGE("Invalid request settings.");
424 return false;
425 }
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700426 return true;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700427}
428
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700429} // namespace v4l2_camera_hal