blob: eb4123908d4317acb1d4824eaf91f1628c883f81 [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
27#include <nativehelper/ScopedFd.h>
28
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070029#include "common.h"
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070030#include "stream_format.h"
31#include "v4l2_gralloc.h"
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070032
33namespace v4l2_camera_hal {
34class V4L2Wrapper {
35 public:
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070036 // Use this method to create V4L2Wrapper objects. Functionally equivalent
37 // to "new V4L2Wrapper", except that it may return nullptr in case of failure.
38 static V4L2Wrapper* NewV4L2Wrapper(const std::string device_path);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070039 virtual ~V4L2Wrapper();
40
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070041 // Helper class to ensure all opened connections are closed.
42 class Connection {
43 public:
44 Connection(std::shared_ptr<V4L2Wrapper> device)
45 : device_(std::move(device)), connect_result_(device_->Connect()) {}
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070046 ~Connection() {
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -080047 if (connect_result_ == 0) {
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070048 device_->Disconnect();
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -080049 }
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070050 }
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070051 // Check whether the connection succeeded or not.
52 inline int status() const { return connect_result_; }
53
54 private:
55 std::shared_ptr<V4L2Wrapper> device_;
56 const int connect_result_;
57 };
58
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070059 // Turn the stream on or off.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070060 virtual int StreamOn();
61 virtual int StreamOff();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070062 // Manage controls.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070063 virtual int QueryControl(uint32_t control_id, v4l2_query_ext_ctrl* result);
64 virtual int GetControl(uint32_t control_id, int32_t* value);
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070065 virtual int SetControl(uint32_t control_id,
66 int32_t desired,
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070067 int32_t* result = nullptr);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070068 // Manage format.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070069 virtual int GetFormats(std::set<uint32_t>* v4l2_formats);
70 virtual int GetFormatFrameSizes(uint32_t v4l2_format,
71 std::set<std::array<int32_t, 2>>* sizes);
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070072 // Durations are returned in ns.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070073 virtual int GetFormatFrameDurationRange(
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070074 uint32_t v4l2_format,
75 const std::array<int32_t, 2>& size,
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070076 std::array<int64_t, 2>* duration_range);
Ari Hausman-Cohenef523102016-11-21 17:02:01 -080077 virtual int SetFormat(const StreamFormat& desired_format,
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070078 uint32_t* result_max_buffers);
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070079 // Manage buffers.
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -080080 virtual int EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer,
81 uint32_t* enqueued_index = nullptr);
82 virtual int DequeueBuffer(uint32_t* dequeued_index = nullptr);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070083
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070084 private:
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070085 // Constructor is private to allow failing on bad input.
86 // Use NewV4L2Wrapper instead.
87 V4L2Wrapper(const std::string device_path,
88 std::unique_ptr<V4L2Gralloc> gralloc);
89
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070090 // Connect or disconnect to the device. Access by creating/destroying
91 // a V4L2Wrapper::Connection object.
92 int Connect();
93 void Disconnect();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070094 // Perform an ioctl call in a thread-safe fashion.
95 template <typename T>
96 int IoctlLocked(int request, T data);
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -080097 // Request/release userspace buffer mode via VIDIOC_REQBUFS.
98 int RequestBuffers(uint32_t num_buffers);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070099
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700100 inline bool connected() { return device_fd_.get() >= 0; }
101
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700102 // The camera device path. For example, /dev/video0.
103 const std::string device_path_;
104 // The opened device fd.
105 ScopedFd device_fd_;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700106 // The underlying gralloc module.
107 std::unique_ptr<V4L2Gralloc> gralloc_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700108 // Whether or not the device supports the extended control query.
109 bool extended_query_supported_;
110 // The format this device is set up for.
111 std::unique_ptr<StreamFormat> format_;
Ari Hausman-Cohen0fbcaf52016-09-28 13:21:31 -0700112 // Map indecies to buffer status. True if the index is in-flight.
113 // |buffers_.size()| will always be the maximum number of buffers this device
114 // can handle in its current format.
115 std::vector<bool> buffers_;
116 // Lock protecting use of the buffer tracker.
117 std::mutex buffer_queue_lock_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700118 // Lock protecting use of the device.
119 std::mutex device_lock_;
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700120 // Lock protecting connecting/disconnecting the device.
121 std::mutex connection_lock_;
122 // Reference count connections.
123 int connection_count_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700124
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700125 friend class Connection;
Ari Hausman-Cohencd9fef62016-07-15 15:54:13 -0700126 friend class V4L2WrapperMock;
127
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700128 DISALLOW_COPY_AND_ASSIGN(V4L2Wrapper);
129};
130
131} // namespace v4l2_camera_hal
132
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700133#endif // V4L2_CAMERA_HAL_V4L2_WRAPPER_H_