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