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 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 17 | #include "v4l2_camera.h" |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 18 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 20 | #include <linux/videodev2.h> |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 23 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 24 | #include <cstdlib> |
| 25 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 26 | #include <camera/CameraMetadata.h> |
| 27 | #include <hardware/camera3.h> |
| 28 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 29 | #include "common.h" |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 30 | #include "function_thread.h" |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 31 | #include "metadata/metadata_common.h" |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 32 | #include "stream_format.h" |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 33 | #include "v4l2_metadata_factory.h" |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 34 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 35 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) |
| 36 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 37 | namespace v4l2_camera_hal { |
| 38 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 39 | // Helper function for managing metadata. |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 40 | static std::vector<int32_t> getMetadataKeys(const camera_metadata_t* metadata) { |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 41 | std::vector<int32_t> keys; |
| 42 | size_t num_entries = get_camera_metadata_entry_count(metadata); |
| 43 | for (size_t i = 0; i < num_entries; ++i) { |
| 44 | camera_metadata_ro_entry_t entry; |
| 45 | get_camera_metadata_ro_entry(metadata, i, &entry); |
| 46 | keys.push_back(entry.tag); |
| 47 | } |
| 48 | return keys; |
| 49 | } |
| 50 | |
Ari Hausman-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame] | 51 | V4L2Camera* V4L2Camera::NewV4L2Camera(int id, const std::string path) { |
| 52 | HAL_LOG_ENTER(); |
| 53 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 54 | std::shared_ptr<V4L2Wrapper> v4l2_wrapper(V4L2Wrapper::NewV4L2Wrapper(path)); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 55 | if (!v4l2_wrapper) { |
| 56 | HAL_LOGE("Failed to initialize V4L2 wrapper."); |
Ari Hausman-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame] | 57 | return nullptr; |
| 58 | } |
| 59 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 60 | std::unique_ptr<Metadata> metadata; |
| 61 | int res = GetV4L2Metadata(v4l2_wrapper, &metadata); |
| 62 | if (res) { |
| 63 | HAL_LOGE("Failed to initialize V4L2 metadata: %d", res); |
| 64 | return nullptr; |
| 65 | } |
| 66 | |
| 67 | return new V4L2Camera(id, std::move(v4l2_wrapper), std::move(metadata)); |
Ari Hausman-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 70 | V4L2Camera::V4L2Camera(int id, |
| 71 | std::shared_ptr<V4L2Wrapper> v4l2_wrapper, |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 72 | std::unique_ptr<Metadata> metadata) |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 73 | : default_camera_hal::Camera(id), |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 74 | device_(std::move(v4l2_wrapper)), |
| 75 | metadata_(std::move(metadata)), |
| 76 | max_input_streams_(0), |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 77 | max_output_streams_({{0, 0, 0}}), |
| 78 | buffer_enqueuer_(new FunctionThread( |
| 79 | std::bind(&V4L2Camera::enqueueRequestBuffers, this))), |
| 80 | buffer_dequeuer_(new FunctionThread( |
| 81 | std::bind(&V4L2Camera::dequeueRequestBuffers, this))) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 82 | HAL_LOG_ENTER(); |
| 83 | } |
| 84 | |
| 85 | V4L2Camera::~V4L2Camera() { |
| 86 | HAL_LOG_ENTER(); |
| 87 | } |
| 88 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 89 | int V4L2Camera::connect() { |
| 90 | HAL_LOG_ENTER(); |
| 91 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 92 | if (connection_) { |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 93 | HAL_LOGE("Already connected. Please disconnect and try again."); |
| 94 | return -EIO; |
| 95 | } |
| 96 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 97 | connection_.reset(new V4L2Wrapper::Connection(device_)); |
| 98 | if (connection_->status()) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 99 | HAL_LOGE("Failed to connect to device."); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 100 | return connection_->status(); |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 103 | // TODO(b/29185945): confirm this is a supported device. |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 104 | // 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] | 105 | // not be the same one that was there when the HAL was loaded. |
| 106 | // (Alternatively, better hotplugging support may make this unecessary |
| 107 | // by disabling cameras that get disconnected and checking newly connected |
| 108 | // cameras, so connect() is never called on an unsupported camera) |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 109 | |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 110 | // TODO(b/29158098): Inform service of any flashes that are no longer |
| 111 | // available because this camera is in use. |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | void V4L2Camera::disconnect() { |
| 116 | HAL_LOG_ENTER(); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 117 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 118 | connection_.reset(); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 119 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 120 | // 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] | 121 | // because this camera is no longer in use. |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 124 | int V4L2Camera::flushBuffers() { |
| 125 | HAL_LOG_ENTER(); |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 126 | return device_->StreamOff(); |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 129 | int V4L2Camera::initStaticInfo(android::CameraMetadata* out) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 130 | HAL_LOG_ENTER(); |
| 131 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 132 | int res = metadata_->FillStaticMetadata(out); |
| 133 | if (res) { |
| 134 | HAL_LOGE("Failed to get static metadata."); |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 135 | return res; |
| 136 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 137 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 138 | // Extract max streams for use in verifying stream configs. |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 139 | res = SingleTagValue( |
| 140 | *out, ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, &max_input_streams_); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 141 | if (res) { |
| 142 | HAL_LOGE("Failed to get max num input streams from static metadata."); |
| 143 | return res; |
| 144 | } |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 145 | res = SingleTagValue( |
| 146 | *out, ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS, &max_output_streams_); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 147 | if (res) { |
| 148 | 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] | 149 | return res; |
| 150 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 151 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 152 | return 0; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 155 | int V4L2Camera::initTemplate(int type, android::CameraMetadata* out) { |
| 156 | HAL_LOG_ENTER(); |
| 157 | |
| 158 | return metadata_->GetRequestTemplate(type, out); |
| 159 | } |
| 160 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 161 | void V4L2Camera::initDeviceInfo(camera_info_t* info) { |
| 162 | HAL_LOG_ENTER(); |
| 163 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 164 | // TODO(b/31044975): move this into device interface. |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 165 | // For now, just constants. |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 166 | info->resource_cost = 100; |
| 167 | info->conflicting_devices = nullptr; |
| 168 | info->conflicting_devices_length = 0; |
| 169 | } |
| 170 | |
| 171 | int V4L2Camera::initDevice() { |
| 172 | HAL_LOG_ENTER(); |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 173 | |
| 174 | // Start the buffer enqueue/dequeue threads if they're not already running. |
| 175 | if (!buffer_enqueuer_->isRunning()) { |
| 176 | android::status_t res = buffer_enqueuer_->run("Enqueue buffers"); |
| 177 | if (res != android::OK) { |
| 178 | HAL_LOGE("Failed to start buffer enqueue thread: %d", res); |
| 179 | return -ENODEV; |
| 180 | } |
| 181 | } |
| 182 | if (!buffer_dequeuer_->isRunning()) { |
| 183 | android::status_t res = buffer_dequeuer_->run("Dequeue buffers"); |
| 184 | if (res != android::OK) { |
| 185 | HAL_LOGE("Failed to start buffer dequeue thread: %d", res); |
| 186 | return -ENODEV; |
| 187 | } |
| 188 | } |
| 189 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 193 | int V4L2Camera::enqueueRequest( |
| 194 | std::shared_ptr<default_camera_hal::CaptureRequest> request) { |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 195 | HAL_LOG_ENTER(); |
| 196 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 197 | // Assume request validated before calling this function. |
| 198 | // (For now, always exactly 1 output buffer, no inputs). |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 199 | { |
| 200 | std::lock_guard<std::mutex> guard(request_queue_lock_); |
| 201 | request_queue_.push(request); |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 202 | requests_available_.notify_one(); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 203 | } |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 204 | |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 208 | std::shared_ptr<default_camera_hal::CaptureRequest> |
| 209 | V4L2Camera::dequeueRequest() { |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 210 | std::unique_lock<std::mutex> lock(request_queue_lock_); |
| 211 | while (request_queue_.empty()) { |
| 212 | requests_available_.wait(lock); |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 213 | } |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 214 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 215 | std::shared_ptr<default_camera_hal::CaptureRequest> request = |
| 216 | request_queue_.front(); |
| 217 | request_queue_.pop(); |
| 218 | return request; |
| 219 | } |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 220 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 221 | bool V4L2Camera::enqueueRequestBuffers() { |
| 222 | // 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] | 223 | std::shared_ptr<default_camera_hal::CaptureRequest> request = |
| 224 | dequeueRequest(); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 225 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 226 | // Assume request validated before being added to the queue |
| 227 | // (For now, always exactly 1 output buffer, no inputs). |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 228 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 229 | // Setting and getting settings are best effort here, |
| 230 | // since there's no way to know through V4L2 exactly what |
| 231 | // settings are used for a buffer unless we were to enqueue them |
| 232 | // one at a time, which would be too slow. |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 233 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 234 | // Set the requested settings |
| 235 | int res = metadata_->SetRequestSettings(request->settings); |
| 236 | if (res) { |
| 237 | HAL_LOGE("Failed to set settings."); |
| 238 | completeRequest(request, res); |
| 239 | return true; |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 242 | // Replace the requested settings with a snapshot of |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 243 | // the used settings/state immediately before enqueue. |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 244 | res = metadata_->FillResultMetadata(&request->settings); |
| 245 | if (res) { |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 246 | // Note: since request is a shared pointer, this may happen if another |
| 247 | // thread has already decided to complete the request (e.g. via flushing), |
| 248 | // since that locks the metadata (in that case, this failing is fine, |
| 249 | // and completeRequest will simply do nothing). |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 250 | HAL_LOGE("Failed to fill result metadata."); |
| 251 | completeRequest(request, res); |
| 252 | return true; |
| 253 | } |
| 254 | |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 255 | // Actually enqueue the buffer for capture. |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 256 | res = device_->EnqueueRequest(request); |
| 257 | if (res) { |
| 258 | HAL_LOGE("Device failed to enqueue buffer."); |
| 259 | completeRequest(request, res); |
| 260 | return true; |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 261 | } |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 262 | |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 263 | // Make sure the stream is on (no effect if already on). |
| 264 | res = device_->StreamOn(); |
| 265 | if (res) { |
| 266 | HAL_LOGE("Device failed to turn on stream."); |
| 267 | // Don't really want to send an error for only the request here, |
| 268 | // since this is a full device error. |
| 269 | // TODO: Should trigger full flush. |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | std::unique_lock<std::mutex> lock(in_flight_lock_); |
| 274 | in_flight_buffer_count_++; |
| 275 | buffers_in_flight_.notify_one(); |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 276 | return true; |
| 277 | } |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 278 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 279 | bool V4L2Camera::dequeueRequestBuffers() { |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 280 | // Dequeue a buffer. |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 281 | std::shared_ptr<default_camera_hal::CaptureRequest> request; |
Jaesung Chung | 1fc9b61 | 2017-11-10 18:09:38 +0900 | [diff] [blame] | 282 | int res; |
| 283 | |
| 284 | { |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 285 | std::unique_lock<std::mutex> lock(in_flight_lock_); |
| 286 | res = device_->DequeueRequest(&request); |
Jaesung Chung | 1fc9b61 | 2017-11-10 18:09:38 +0900 | [diff] [blame] | 287 | if (!res) { |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 288 | if (request) { |
| 289 | completeRequest(request, res); |
| 290 | in_flight_buffer_count_--; |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 291 | } |
Jaesung Chung | 1fc9b61 | 2017-11-10 18:09:38 +0900 | [diff] [blame] | 292 | return true; |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 293 | } |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Jaesung Chung | 1fc9b61 | 2017-11-10 18:09:38 +0900 | [diff] [blame] | 296 | if (res == -EAGAIN) { |
| 297 | // EAGAIN just means nothing to dequeue right now. |
| 298 | // Wait until something is available before looping again. |
| 299 | std::unique_lock<std::mutex> lock(in_flight_lock_); |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 300 | while (in_flight_buffer_count_ == 0) { |
Jaesung Chung | 1fc9b61 | 2017-11-10 18:09:38 +0900 | [diff] [blame] | 301 | buffers_in_flight_.wait(lock); |
| 302 | } |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 303 | } else { |
Jaesung Chung | 1fc9b61 | 2017-11-10 18:09:38 +0900 | [diff] [blame] | 304 | HAL_LOGW("Device failed to dequeue buffer: %d", res); |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 305 | } |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 306 | return true; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 309 | bool V4L2Camera::validateDataspacesAndRotations( |
| 310 | const camera3_stream_configuration_t* stream_config) { |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 311 | HAL_LOG_ENTER(); |
| 312 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 313 | for (uint32_t i = 0; i < stream_config->num_streams; ++i) { |
| 314 | if (stream_config->streams[i]->rotation != CAMERA3_STREAM_ROTATION_0) { |
| 315 | HAL_LOGV("Rotation %d for stream %d not supported", |
| 316 | stream_config->streams[i]->rotation, |
| 317 | i); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 318 | return false; |
| 319 | } |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 320 | // Accept all dataspaces, as it will just be overwritten below anyways. |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 321 | } |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 322 | return true; |
| 323 | } |
| 324 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 325 | int V4L2Camera::setupStreams(camera3_stream_configuration_t* stream_config) { |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 326 | HAL_LOG_ENTER(); |
| 327 | |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 328 | std::lock_guard<std::mutex> guard(in_flight_lock_); |
| 329 | // 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^] | 330 | if (device_->GetInFlightBufferCount() != 0) { |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 331 | HAL_LOGE("Can't set device format while frames are in flight."); |
| 332 | return -EINVAL; |
| 333 | } |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 334 | in_flight_buffer_count_ = 0; |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 335 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 336 | // stream_config should have been validated; assume at least 1 stream. |
| 337 | camera3_stream_t* stream = stream_config->streams[0]; |
| 338 | int format = stream->format; |
| 339 | uint32_t width = stream->width; |
| 340 | uint32_t height = stream->height; |
| 341 | |
| 342 | if (stream_config->num_streams > 1) { |
| 343 | // TODO(b/29939583): V4L2 doesn't actually support more than 1 |
| 344 | // stream at a time. If not all streams are the same format |
| 345 | // and size, error. Note that this means the HAL is not spec-compliant. |
| 346 | // Technically, this error should be thrown during validation, but |
| 347 | // since it isn't a spec-valid error validation isn't set up to check it. |
| 348 | for (uint32_t i = 1; i < stream_config->num_streams; ++i) { |
| 349 | stream = stream_config->streams[i]; |
| 350 | if (stream->format != format || stream->width != width || |
| 351 | stream->height != height) { |
| 352 | HAL_LOGE( |
| 353 | "V4L2 only supports 1 stream configuration at a time " |
| 354 | "(stream 0 is format %d, width %u, height %u, " |
| 355 | "stream %d is format %d, width %u, height %u).", |
| 356 | format, |
| 357 | width, |
| 358 | height, |
| 359 | i, |
| 360 | stream->format, |
| 361 | stream->width, |
| 362 | stream->height); |
| 363 | return -EINVAL; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 368 | // Ensure the stream is off. |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 369 | int res = device_->StreamOff(); |
| 370 | if (res) { |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 371 | 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] | 372 | return -ENODEV; |
| 373 | } |
| 374 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 375 | StreamFormat stream_format(format, width, height); |
| 376 | uint32_t max_buffers = 0; |
| 377 | res = device_->SetFormat(stream_format, &max_buffers); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 378 | if (res) { |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 379 | HAL_LOGE("Failed to set device to correct format for stream: %d.", res); |
| 380 | return -ENODEV; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 381 | } |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 382 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 383 | // Sanity check. |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 384 | if (max_buffers < 1) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 385 | 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] | 386 | max_buffers); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 387 | return -ENODEV; |
| 388 | } |
| 389 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 390 | // Set all the streams dataspaces, usages, and max buffers. |
| 391 | for (uint32_t i = 0; i < stream_config->num_streams; ++i) { |
| 392 | stream = stream_config->streams[i]; |
| 393 | |
Jaesung Chung | 6e8afea | 2017-11-15 09:30:41 +0900 | [diff] [blame] | 394 | // Override HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED format. |
| 395 | if (stream->format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { |
| 396 | stream->format = HAL_PIXEL_FORMAT_RGBA_8888; |
| 397 | } |
| 398 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 399 | // Max buffers as reported by the device. |
| 400 | stream->max_buffers = max_buffers; |
| 401 | |
| 402 | // Usage: currently using sw graphics. |
| 403 | switch (stream->stream_type) { |
| 404 | case CAMERA3_STREAM_INPUT: |
| 405 | stream->usage = GRALLOC_USAGE_SW_READ_OFTEN; |
| 406 | break; |
| 407 | case CAMERA3_STREAM_OUTPUT: |
| 408 | stream->usage = GRALLOC_USAGE_SW_WRITE_OFTEN; |
| 409 | break; |
| 410 | case CAMERA3_STREAM_BIDIRECTIONAL: |
| 411 | stream->usage = |
| 412 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN; |
| 413 | break; |
| 414 | default: |
| 415 | // nothing to do. |
| 416 | break; |
| 417 | } |
| 418 | |
| 419 | // Doesn't matter what was requested, we always use dataspace V0_JFIF. |
| 420 | // Note: according to camera3.h, this isn't allowed, but the camera |
| 421 | // framework team claims it's underdocumented; the implementation lets the |
| 422 | // HAL overwrite it. If this is changed, change the validation above. |
| 423 | stream->data_space = HAL_DATASPACE_V0_JFIF; |
| 424 | } |
| 425 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 426 | return 0; |
| 427 | } |
| 428 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 429 | bool V4L2Camera::isValidRequestSettings( |
| 430 | const android::CameraMetadata& settings) { |
| 431 | if (!metadata_->IsValidRequest(settings)) { |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 432 | HAL_LOGE("Invalid request settings."); |
| 433 | return false; |
| 434 | } |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 435 | return true; |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 438 | } // namespace v4l2_camera_hal |