blob: 2682ce7dc9fe22e0f457775f60cbf512d4f46845 [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>
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -070028#include "arc/common_types.h"
29#include "arc/frame_buffer.h"
30#include "capture_request.h"
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070031#include "common.h"
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070032#include "stream_format.h"
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070033
34namespace v4l2_camera_hal {
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -070035
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070036class V4L2Wrapper {
37 public:
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070038 // Use this method to create V4L2Wrapper objects. Functionally equivalent
39 // to "new V4L2Wrapper", except that it may return nullptr in case of failure.
40 static V4L2Wrapper* NewV4L2Wrapper(const std::string device_path);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070041 virtual ~V4L2Wrapper();
42
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070043 // Helper class to ensure all opened connections are closed.
44 class Connection {
45 public:
46 Connection(std::shared_ptr<V4L2Wrapper> device)
47 : device_(std::move(device)), connect_result_(device_->Connect()) {}
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070048 ~Connection() {
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -080049 if (connect_result_ == 0) {
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070050 device_->Disconnect();
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -080051 }
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070052 }
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070053 // Check whether the connection succeeded or not.
54 inline int status() const { return connect_result_; }
55
56 private:
57 std::shared_ptr<V4L2Wrapper> device_;
58 const int connect_result_;
59 };
60
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070061 // Turn the stream on or off.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070062 virtual int StreamOn();
63 virtual int StreamOff();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070064 // Manage controls.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070065 virtual int QueryControl(uint32_t control_id, v4l2_query_ext_ctrl* result);
66 virtual int GetControl(uint32_t control_id, int32_t* value);
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070067 virtual int SetControl(uint32_t control_id,
68 int32_t desired,
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070069 int32_t* result = nullptr);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070070 // Manage format.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070071 virtual int GetFormats(std::set<uint32_t>* v4l2_formats);
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -070072 virtual int GetQualifiedFormats(std::vector<uint32_t>* v4l2_formats);
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070073 virtual int GetFormatFrameSizes(uint32_t v4l2_format,
74 std::set<std::array<int32_t, 2>>* sizes);
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -070075
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070076 // Durations are returned in ns.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070077 virtual int GetFormatFrameDurationRange(
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070078 uint32_t v4l2_format,
79 const std::array<int32_t, 2>& size,
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070080 std::array<int64_t, 2>* duration_range);
Ari Hausman-Cohenef523102016-11-21 17:02:01 -080081 virtual int SetFormat(const StreamFormat& desired_format,
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070082 uint32_t* result_max_buffers);
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070083 // Manage buffers.
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -070084 virtual int EnqueueRequest(
85 std::shared_ptr<default_camera_hal::CaptureRequest> request);
86 virtual int DequeueRequest(
87 std::shared_ptr<default_camera_hal::CaptureRequest>* request);
88 virtual int GetInFlightBufferCount();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070089
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070090 private:
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070091 // Constructor is private to allow failing on bad input.
92 // Use NewV4L2Wrapper instead.
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -070093 V4L2Wrapper(const std::string device_path);
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070094
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070095 // Connect or disconnect to the device. Access by creating/destroying
96 // a V4L2Wrapper::Connection object.
97 int Connect();
98 void Disconnect();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070099 // Perform an ioctl call in a thread-safe fashion.
100 template <typename T>
Sergii Piatakov028763d2018-10-10 17:47:03 +0300101 int IoctlLocked(unsigned long request, T data);
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800102 // Request/release userspace buffer mode via VIDIOC_REQBUFS.
103 int RequestBuffers(uint32_t num_buffers);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700104
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700105 inline bool connected() { return device_fd_.get() >= 0; }
106
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700107 // Format management.
108 const arc::SupportedFormats GetSupportedFormats();
109
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700110 // The camera device path. For example, /dev/video0.
111 const std::string device_path_;
112 // The opened device fd.
Ari Hausman-Cohen2d1ea3a2017-03-24 18:38:16 -0700113 android::base::unique_fd device_fd_;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700114 // The underlying gralloc module.
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700115 // std::unique_ptr<V4L2Gralloc> gralloc_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700116 // Whether or not the device supports the extended control query.
117 bool extended_query_supported_;
118 // The format this device is set up for.
119 std::unique_ptr<StreamFormat> format_;
Ari Hausman-Cohen0fbcaf52016-09-28 13:21:31 -0700120 // Lock protecting use of the buffer tracker.
121 std::mutex buffer_queue_lock_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700122 // Lock protecting use of the device.
123 std::mutex device_lock_;
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700124 // Lock protecting connecting/disconnecting the device.
125 std::mutex connection_lock_;
126 // Reference count connections.
127 int connection_count_;
Prashanth Swaminathan28e0f762017-04-27 11:32:11 -0700128 // Supported formats.
129 arc::SupportedFormats supported_formats_;
130 // Qualified formats.
131 arc::SupportedFormats qualified_formats_;
132
133 class RequestContext {
134 public:
135 RequestContext()
136 : active(false),
137 camera_buffer(std::make_shared<arc::AllocatedFrameBuffer>(0)){};
138 ~RequestContext(){};
139 // Indicates whether this request context is in use.
140 bool active;
141 // Buffer handles of the context.
142 std::shared_ptr<arc::AllocatedFrameBuffer> camera_buffer;
143 std::shared_ptr<default_camera_hal::CaptureRequest> request;
144 };
145
146 // Map of in flight requests.
147 // |buffers_.size()| will always be the maximum number of buffers this device
148 // can handle in its current format.
149 std::vector<RequestContext> buffers_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700150
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700151 friend class Connection;
Ari Hausman-Cohencd9fef62016-07-15 15:54:13 -0700152 friend class V4L2WrapperMock;
153
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700154 DISALLOW_COPY_AND_ASSIGN(V4L2Wrapper);
155};
156
157} // namespace v4l2_camera_hal
158
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700159#endif // V4L2_CAMERA_HAL_V4L2_WRAPPER_H_