blob: 4a8d5170ddfba7b68c201b542709be08f816977c [file] [log] [blame]
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -07001/*
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-Cohen3841a7f2016-07-19 17:27:52 -070017#ifndef V4L2_CAMERA_HAL_V4L2_WRAPPER_H_
18#define V4L2_CAMERA_HAL_V4L2_WRAPPER_H_
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070019
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070020#include <array>
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070021#include <memory>
22#include <mutex>
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070023#include <set>
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070024#include <string>
Ari Hausman-Cohen0fbcaf52016-09-28 13:21:31 -070025#include <vector>
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070026
Ari Hausman-Cohen2d1ea3a2017-03-24 18:38:16 -070027#include <android-base/unique_fd.h>
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070028
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -070029#include "arc/common_types.h"
30#include "arc/frame_buffer.h"
31#include "capture_request.h"
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070032#include "common.h"
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070033#include "stream_format.h"
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070034
35namespace v4l2_camera_hal {
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -070036
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070037class V4L2Wrapper {
38 public:
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070039 // 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-Cohenc17fd092016-07-18 10:13:26 -070042 virtual ~V4L2Wrapper();
43
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070044 // 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-Cohen3a4c3bb2016-08-01 17:16:01 -070049 ~Connection() {
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -080050 if (connect_result_ == 0) {
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070051 device_->Disconnect();
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -080052 }
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070053 }
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070054 // 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-Cohenc17fd092016-07-18 10:13:26 -070062 // Turn the stream on or off.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070063 virtual int StreamOn();
64 virtual int StreamOff();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070065 // Manage controls.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070066 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-Cohen5d753232016-08-10 14:27:36 -070068 virtual int SetControl(uint32_t control_id,
69 int32_t desired,
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070070 int32_t* result = nullptr);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070071 // Manage format.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070072 virtual int GetFormats(std::set<uint32_t>* v4l2_formats);
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -070073 virtual int GetQualifiedFormats(std::vector<uint32_t>* v4l2_formats);
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070074 virtual int GetFormatFrameSizes(uint32_t v4l2_format,
75 std::set<std::array<int32_t, 2>>* sizes);
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -070076
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070077 // Durations are returned in ns.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070078 virtual int GetFormatFrameDurationRange(
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070079 uint32_t v4l2_format,
80 const std::array<int32_t, 2>& size,
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070081 std::array<int64_t, 2>* duration_range);
Ari Hausman-Cohenef523102016-11-21 17:02:01 -080082 virtual int SetFormat(const StreamFormat& desired_format,
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070083 uint32_t* result_max_buffers);
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070084 // Manage buffers.
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -070085 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-Cohenc17fd092016-07-18 10:13:26 -070090
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070091 private:
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070092 // Constructor is private to allow failing on bad input.
93 // Use NewV4L2Wrapper instead.
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -070094 V4L2Wrapper(const std::string device_path);
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070095
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070096 // Connect or disconnect to the device. Access by creating/destroying
97 // a V4L2Wrapper::Connection object.
98 int Connect();
99 void Disconnect();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700100 // Perform an ioctl call in a thread-safe fashion.
101 template <typename T>
102 int IoctlLocked(int request, T data);
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800103 // Request/release userspace buffer mode via VIDIOC_REQBUFS.
104 int RequestBuffers(uint32_t num_buffers);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700105
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700106 inline bool connected() { return device_fd_.get() >= 0; }
107
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700108 // Format management.
109 const arc::SupportedFormats GetSupportedFormats();
110
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700111 // The camera device path. For example, /dev/video0.
112 const std::string device_path_;
113 // The opened device fd.
Ari Hausman-Cohen2d1ea3a2017-03-24 18:38:16 -0700114 android::base::unique_fd device_fd_;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700115 // The underlying gralloc module.
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700116 // std::unique_ptr<V4L2Gralloc> gralloc_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700117 // 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-Cohen0fbcaf52016-09-28 13:21:31 -0700121 // Lock protecting use of the buffer tracker.
122 std::mutex buffer_queue_lock_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700123 // Lock protecting use of the device.
124 std::mutex device_lock_;
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700125 // Lock protecting connecting/disconnecting the device.
126 std::mutex connection_lock_;
127 // Reference count connections.
128 int connection_count_;
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700129 // 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-Cohenc17fd092016-07-18 10:13:26 -0700151
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700152 friend class Connection;
Ari Hausman-Cohencd9fef62016-07-15 15:54:13 -0700153 friend class V4L2WrapperMock;
154
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700155 DISALLOW_COPY_AND_ASSIGN(V4L2Wrapper);
156};
157
158} // namespace v4l2_camera_hal
159
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700160#endif // V4L2_CAMERA_HAL_V4L2_WRAPPER_H_