Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Piatakov | 2ad591f | 2018-08-03 11:41:06 +0300 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "V4L2Camera" |
| 19 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 20 | #include "v4l2_camera.h" |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 21 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 22 | #include <cstdlib> |
Sergii Piatakov | b8f073f | 2018-10-10 17:22:10 +0300 | [diff] [blame^] | 23 | #include <fcntl.h> |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 24 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 25 | #include <camera/CameraMetadata.h> |
| 26 | #include <hardware/camera3.h> |
Sergii Piatakov | b8f073f | 2018-10-10 17:22:10 +0300 | [diff] [blame^] | 27 | #include <linux/videodev2.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 30 | #include "common.h" |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 31 | #include "function_thread.h" |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 32 | #include "metadata/metadata_common.h" |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 33 | #include "stream_format.h" |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 34 | #include "v4l2_metadata_factory.h" |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 35 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 36 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) |
| 37 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 38 | namespace v4l2_camera_hal { |
| 39 | |
Ari Hausman-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame] | 40 | V4L2Camera* V4L2Camera::NewV4L2Camera(int id, const std::string path) { |
| 41 | HAL_LOG_ENTER(); |
| 42 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 43 | std::shared_ptr<V4L2Wrapper> v4l2_wrapper(V4L2Wrapper::NewV4L2Wrapper(path)); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 44 | if (!v4l2_wrapper) { |
| 45 | HAL_LOGE("Failed to initialize V4L2 wrapper."); |
Ari Hausman-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame] | 46 | return nullptr; |
| 47 | } |
| 48 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 49 | 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-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 59 | V4L2Camera::V4L2Camera(int id, |
| 60 | std::shared_ptr<V4L2Wrapper> v4l2_wrapper, |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 61 | std::unique_ptr<Metadata> metadata) |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 62 | : default_camera_hal::Camera(id), |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 63 | device_(std::move(v4l2_wrapper)), |
| 64 | metadata_(std::move(metadata)), |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 65 | buffer_enqueuer_(new FunctionThread( |
| 66 | std::bind(&V4L2Camera::enqueueRequestBuffers, this))), |
| 67 | buffer_dequeuer_(new FunctionThread( |
Sergii Piatakov | 1bd0add | 2018-08-06 09:23:22 +0300 | [diff] [blame] | 68 | std::bind(&V4L2Camera::dequeueRequestBuffers, this))), |
| 69 | max_input_streams_(0), |
| 70 | max_output_streams_({{0, 0, 0}}) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 71 | HAL_LOG_ENTER(); |
| 72 | } |
| 73 | |
| 74 | V4L2Camera::~V4L2Camera() { |
| 75 | HAL_LOG_ENTER(); |
| 76 | } |
| 77 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 78 | int V4L2Camera::connect() { |
| 79 | HAL_LOG_ENTER(); |
| 80 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 81 | if (connection_) { |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 82 | HAL_LOGE("Already connected. Please disconnect and try again."); |
| 83 | return -EIO; |
| 84 | } |
| 85 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 86 | connection_.reset(new V4L2Wrapper::Connection(device_)); |
| 87 | if (connection_->status()) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 88 | HAL_LOGE("Failed to connect to device."); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 89 | return connection_->status(); |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 92 | // TODO(b/29185945): confirm this is a supported device. |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 93 | // This is checked by the HAL, but the device at |device_|'s path may |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 94 | // 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-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 98 | |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 99 | // TODO(b/29158098): Inform service of any flashes that are no longer |
| 100 | // available because this camera is in use. |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | void V4L2Camera::disconnect() { |
| 105 | HAL_LOG_ENTER(); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 106 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 107 | connection_.reset(); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 108 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 109 | // TODO(b/29158098): Inform service of any flashes that are available again |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 110 | // because this camera is no longer in use. |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 113 | int V4L2Camera::flushBuffers() { |
| 114 | HAL_LOG_ENTER(); |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame] | 115 | return device_->StreamOff(); |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 118 | int V4L2Camera::initStaticInfo(android::CameraMetadata* out) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 119 | HAL_LOG_ENTER(); |
| 120 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 121 | int res = metadata_->FillStaticMetadata(out); |
| 122 | if (res) { |
| 123 | HAL_LOGE("Failed to get static metadata."); |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 124 | return res; |
| 125 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 126 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 127 | // Extract max streams for use in verifying stream configs. |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 128 | res = SingleTagValue( |
| 129 | *out, ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, &max_input_streams_); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 130 | if (res) { |
| 131 | HAL_LOGE("Failed to get max num input streams from static metadata."); |
| 132 | return res; |
| 133 | } |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 134 | res = SingleTagValue( |
| 135 | *out, ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS, &max_output_streams_); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 136 | if (res) { |
| 137 | HAL_LOGE("Failed to get max num output streams from static metadata."); |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 138 | return res; |
| 139 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 140 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 141 | return 0; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 144 | int V4L2Camera::initTemplate(int type, android::CameraMetadata* out) { |
| 145 | HAL_LOG_ENTER(); |
| 146 | |
| 147 | return metadata_->GetRequestTemplate(type, out); |
| 148 | } |
| 149 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 150 | void V4L2Camera::initDeviceInfo(camera_info_t* info) { |
| 151 | HAL_LOG_ENTER(); |
| 152 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 153 | // TODO(b/31044975): move this into device interface. |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 154 | // For now, just constants. |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 155 | info->resource_cost = 100; |
| 156 | info->conflicting_devices = nullptr; |
| 157 | info->conflicting_devices_length = 0; |
| 158 | } |
| 159 | |
| 160 | int V4L2Camera::initDevice() { |
| 161 | HAL_LOG_ENTER(); |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 162 | |
| 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-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 182 | int V4L2Camera::enqueueRequest( |
| 183 | std::shared_ptr<default_camera_hal::CaptureRequest> request) { |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 184 | HAL_LOG_ENTER(); |
| 185 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 186 | // Assume request validated before calling this function. |
| 187 | // (For now, always exactly 1 output buffer, no inputs). |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 188 | { |
| 189 | std::lock_guard<std::mutex> guard(request_queue_lock_); |
| 190 | request_queue_.push(request); |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 191 | requests_available_.notify_one(); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 192 | } |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 193 | |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 197 | std::shared_ptr<default_camera_hal::CaptureRequest> |
| 198 | V4L2Camera::dequeueRequest() { |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 199 | std::unique_lock<std::mutex> lock(request_queue_lock_); |
| 200 | while (request_queue_.empty()) { |
| 201 | requests_available_.wait(lock); |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 202 | } |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 203 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 204 | std::shared_ptr<default_camera_hal::CaptureRequest> request = |
| 205 | request_queue_.front(); |
| 206 | request_queue_.pop(); |
| 207 | return request; |
| 208 | } |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 209 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 210 | bool V4L2Camera::enqueueRequestBuffers() { |
| 211 | // Get a request from the queue (blocks this thread until one is available). |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 212 | std::shared_ptr<default_camera_hal::CaptureRequest> request = |
| 213 | dequeueRequest(); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 214 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 215 | // Assume request validated before being added to the queue |
| 216 | // (For now, always exactly 1 output buffer, no inputs). |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 217 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 218 | // 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-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 222 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 223 | // 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-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 231 | // Replace the requested settings with a snapshot of |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 232 | // the used settings/state immediately before enqueue. |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 233 | res = metadata_->FillResultMetadata(&request->settings); |
| 234 | if (res) { |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 235 | // 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-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 239 | HAL_LOGE("Failed to fill result metadata."); |
| 240 | completeRequest(request, res); |
| 241 | return true; |
| 242 | } |
| 243 | |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 244 | // Actually enqueue the buffer for capture. |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame] | 245 | res = device_->EnqueueRequest(request); |
| 246 | if (res) { |
| 247 | HAL_LOGE("Device failed to enqueue buffer."); |
| 248 | completeRequest(request, res); |
| 249 | return true; |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 250 | } |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 251 | |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame] | 252 | // 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-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 265 | return true; |
| 266 | } |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 267 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 268 | bool V4L2Camera::dequeueRequestBuffers() { |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 269 | // Dequeue a buffer. |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame] | 270 | std::shared_ptr<default_camera_hal::CaptureRequest> request; |
Jaesung Chung | 1fc9b61 | 2017-11-10 18:09:38 +0900 | [diff] [blame] | 271 | int res; |
| 272 | |
| 273 | { |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame] | 274 | std::unique_lock<std::mutex> lock(in_flight_lock_); |
| 275 | res = device_->DequeueRequest(&request); |
Jaesung Chung | 1fc9b61 | 2017-11-10 18:09:38 +0900 | [diff] [blame] | 276 | if (!res) { |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame] | 277 | if (request) { |
| 278 | completeRequest(request, res); |
| 279 | in_flight_buffer_count_--; |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 280 | } |
Jaesung Chung | 1fc9b61 | 2017-11-10 18:09:38 +0900 | [diff] [blame] | 281 | return true; |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 282 | } |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Jaesung Chung | 1fc9b61 | 2017-11-10 18:09:38 +0900 | [diff] [blame] | 285 | 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 Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame] | 289 | while (in_flight_buffer_count_ == 0) { |
Jaesung Chung | 1fc9b61 | 2017-11-10 18:09:38 +0900 | [diff] [blame] | 290 | buffers_in_flight_.wait(lock); |
| 291 | } |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 292 | } else { |
Jaesung Chung | 1fc9b61 | 2017-11-10 18:09:38 +0900 | [diff] [blame] | 293 | HAL_LOGW("Device failed to dequeue buffer: %d", res); |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 294 | } |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 295 | return true; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 298 | bool V4L2Camera::validateDataspacesAndRotations( |
| 299 | const camera3_stream_configuration_t* stream_config) { |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 300 | HAL_LOG_ENTER(); |
| 301 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 302 | 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-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 307 | return false; |
| 308 | } |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 309 | // Accept all dataspaces, as it will just be overwritten below anyways. |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 310 | } |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 311 | return true; |
| 312 | } |
| 313 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 314 | int V4L2Camera::setupStreams(camera3_stream_configuration_t* stream_config) { |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 315 | HAL_LOG_ENTER(); |
| 316 | |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 317 | std::lock_guard<std::mutex> guard(in_flight_lock_); |
| 318 | // The framework should be enforcing this, but doesn't hurt to be safe. |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame] | 319 | if (device_->GetInFlightBufferCount() != 0) { |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 320 | HAL_LOGE("Can't set device format while frames are in flight."); |
| 321 | return -EINVAL; |
| 322 | } |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame] | 323 | in_flight_buffer_count_ = 0; |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 324 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 325 | // 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-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 357 | // Ensure the stream is off. |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 358 | int res = device_->StreamOff(); |
| 359 | if (res) { |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 360 | HAL_LOGE("Device failed to turn off stream for reconfiguration: %d.", res); |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 361 | return -ENODEV; |
| 362 | } |
| 363 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 364 | StreamFormat stream_format(format, width, height); |
| 365 | uint32_t max_buffers = 0; |
| 366 | res = device_->SetFormat(stream_format, &max_buffers); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 367 | if (res) { |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 368 | HAL_LOGE("Failed to set device to correct format for stream: %d.", res); |
| 369 | return -ENODEV; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 370 | } |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 371 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 372 | // Sanity check. |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 373 | if (max_buffers < 1) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 374 | HAL_LOGE("Setting format resulted in an invalid maximum of %u buffers.", |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 375 | max_buffers); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 376 | return -ENODEV; |
| 377 | } |
| 378 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 379 | // 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 Chung | 6e8afea | 2017-11-15 09:30:41 +0900 | [diff] [blame] | 383 | // 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-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 388 | // 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-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 418 | bool V4L2Camera::isValidRequestSettings( |
| 419 | const android::CameraMetadata& settings) { |
| 420 | if (!metadata_->IsValidRequest(settings)) { |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 421 | HAL_LOGE("Invalid request settings."); |
| 422 | return false; |
| 423 | } |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 424 | return true; |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 427 | } // namespace v4l2_camera_hal |