Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | #ifndef V4L2_CAMERA_HAL_V4L2_WRAPPER_H_ |
| 18 | #define V4L2_CAMERA_HAL_V4L2_WRAPPER_H_ |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 19 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 20 | #include <array> |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <mutex> |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 23 | #include <set> |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 24 | #include <string> |
Ari Hausman-Cohen | 0fbcaf5 | 2016-09-28 13:21:31 -0700 | [diff] [blame] | 25 | #include <vector> |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 26 | |
Ari Hausman-Cohen | 2d1ea3a | 2017-03-24 18:38:16 -0700 | [diff] [blame] | 27 | #include <android-base/unique_fd.h> |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 28 | |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 29 | #include "arc/common_types.h" |
| 30 | #include "arc/frame_buffer.h" |
| 31 | #include "capture_request.h" |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 32 | #include "common.h" |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 33 | #include "stream_format.h" |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 34 | |
| 35 | namespace v4l2_camera_hal { |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 36 | |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 37 | class V4L2Wrapper { |
| 38 | public: |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 39 | // Use this method to create V4L2Wrapper objects. Functionally equivalent |
| 40 | // to "new V4L2Wrapper", except that it may return nullptr in case of failure. |
| 41 | static V4L2Wrapper* NewV4L2Wrapper(const std::string device_path); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 42 | virtual ~V4L2Wrapper(); |
| 43 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 44 | // Helper class to ensure all opened connections are closed. |
| 45 | class Connection { |
| 46 | public: |
| 47 | Connection(std::shared_ptr<V4L2Wrapper> device) |
| 48 | : device_(std::move(device)), connect_result_(device_->Connect()) {} |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 49 | ~Connection() { |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 50 | if (connect_result_ == 0) { |
Ari Hausman-Cohen | 5d75323 | 2016-08-10 14:27:36 -0700 | [diff] [blame] | 51 | device_->Disconnect(); |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 52 | } |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 53 | } |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 54 | // Check whether the connection succeeded or not. |
| 55 | inline int status() const { return connect_result_; } |
| 56 | |
| 57 | private: |
| 58 | std::shared_ptr<V4L2Wrapper> device_; |
| 59 | const int connect_result_; |
| 60 | }; |
| 61 | |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 62 | // Turn the stream on or off. |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 63 | virtual int StreamOn(); |
| 64 | virtual int StreamOff(); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 65 | // Manage controls. |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 66 | virtual int QueryControl(uint32_t control_id, v4l2_query_ext_ctrl* result); |
| 67 | virtual int GetControl(uint32_t control_id, int32_t* value); |
Ari Hausman-Cohen | 5d75323 | 2016-08-10 14:27:36 -0700 | [diff] [blame] | 68 | virtual int SetControl(uint32_t control_id, |
| 69 | int32_t desired, |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 70 | int32_t* result = nullptr); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 71 | // Manage format. |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 72 | virtual int GetFormats(std::set<uint32_t>* v4l2_formats); |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 73 | virtual int GetQualifiedFormats(std::vector<uint32_t>* v4l2_formats); |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 74 | virtual int GetFormatFrameSizes(uint32_t v4l2_format, |
| 75 | std::set<std::array<int32_t, 2>>* sizes); |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 76 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 77 | // Durations are returned in ns. |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 78 | virtual int GetFormatFrameDurationRange( |
Ari Hausman-Cohen | 5d75323 | 2016-08-10 14:27:36 -0700 | [diff] [blame] | 79 | uint32_t v4l2_format, |
| 80 | const std::array<int32_t, 2>& size, |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 81 | std::array<int64_t, 2>* duration_range); |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 82 | virtual int SetFormat(const StreamFormat& desired_format, |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 83 | uint32_t* result_max_buffers); |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 84 | // Manage buffers. |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 85 | virtual int EnqueueRequest( |
| 86 | std::shared_ptr<default_camera_hal::CaptureRequest> request); |
| 87 | virtual int DequeueRequest( |
| 88 | std::shared_ptr<default_camera_hal::CaptureRequest>* request); |
| 89 | virtual int GetInFlightBufferCount(); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 90 | |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 91 | private: |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 92 | // Constructor is private to allow failing on bad input. |
| 93 | // Use NewV4L2Wrapper instead. |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 94 | V4L2Wrapper(const std::string device_path); |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 95 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 96 | // Connect or disconnect to the device. Access by creating/destroying |
| 97 | // a V4L2Wrapper::Connection object. |
| 98 | int Connect(); |
| 99 | void Disconnect(); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 100 | // Perform an ioctl call in a thread-safe fashion. |
| 101 | template <typename T> |
| 102 | int IoctlLocked(int request, T data); |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 103 | // Request/release userspace buffer mode via VIDIOC_REQBUFS. |
| 104 | int RequestBuffers(uint32_t num_buffers); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 105 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 106 | inline bool connected() { return device_fd_.get() >= 0; } |
| 107 | |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 108 | // Format management. |
| 109 | const arc::SupportedFormats GetSupportedFormats(); |
| 110 | |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 111 | // The camera device path. For example, /dev/video0. |
| 112 | const std::string device_path_; |
| 113 | // The opened device fd. |
Ari Hausman-Cohen | 2d1ea3a | 2017-03-24 18:38:16 -0700 | [diff] [blame] | 114 | android::base::unique_fd device_fd_; |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 115 | // The underlying gralloc module. |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 116 | // std::unique_ptr<V4L2Gralloc> gralloc_; |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 117 | // Whether or not the device supports the extended control query. |
| 118 | bool extended_query_supported_; |
| 119 | // The format this device is set up for. |
| 120 | std::unique_ptr<StreamFormat> format_; |
Ari Hausman-Cohen | 0fbcaf5 | 2016-09-28 13:21:31 -0700 | [diff] [blame] | 121 | // Lock protecting use of the buffer tracker. |
| 122 | std::mutex buffer_queue_lock_; |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 123 | // Lock protecting use of the device. |
| 124 | std::mutex device_lock_; |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 125 | // Lock protecting connecting/disconnecting the device. |
| 126 | std::mutex connection_lock_; |
| 127 | // Reference count connections. |
| 128 | int connection_count_; |
Prashanth Swaminathan | 28e0f76 | 2017-04-27 11:32:11 -0700 | [diff] [blame^] | 129 | // Supported formats. |
| 130 | arc::SupportedFormats supported_formats_; |
| 131 | // Qualified formats. |
| 132 | arc::SupportedFormats qualified_formats_; |
| 133 | |
| 134 | class RequestContext { |
| 135 | public: |
| 136 | RequestContext() |
| 137 | : active(false), |
| 138 | camera_buffer(std::make_shared<arc::AllocatedFrameBuffer>(0)){}; |
| 139 | ~RequestContext(){}; |
| 140 | // Indicates whether this request context is in use. |
| 141 | bool active; |
| 142 | // Buffer handles of the context. |
| 143 | std::shared_ptr<arc::AllocatedFrameBuffer> camera_buffer; |
| 144 | std::shared_ptr<default_camera_hal::CaptureRequest> request; |
| 145 | }; |
| 146 | |
| 147 | // Map of in flight requests. |
| 148 | // |buffers_.size()| will always be the maximum number of buffers this device |
| 149 | // can handle in its current format. |
| 150 | std::vector<RequestContext> buffers_; |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 151 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 152 | friend class Connection; |
Ari Hausman-Cohen | cd9fef6 | 2016-07-15 15:54:13 -0700 | [diff] [blame] | 153 | friend class V4L2WrapperMock; |
| 154 | |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 155 | DISALLOW_COPY_AND_ASSIGN(V4L2Wrapper); |
| 156 | }; |
| 157 | |
| 158 | } // namespace v4l2_camera_hal |
| 159 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 160 | #endif // V4L2_CAMERA_HAL_V4L2_WRAPPER_H_ |