xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/xcore/v4l2_device.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * v4l2_device.h - v4l2 device
3  *
4  *  Copyright (c) 2014-2015 Intel Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Wind Yuan <feng.yuan@intel.com>
19  */
20 
21 #ifndef XCAM_V4L2_DEVICE_H
22 #define XCAM_V4L2_DEVICE_H
23 
24 #include <xcam_std.h>
25 #include <xcam_mutex.h>
26 #include <linux/videodev2.h>
27 #include <linux/v4l2-subdev.h>
28 #include <list>
29 #include <vector>
30 
31 extern "C" {
32     struct v4l2_event;
33     struct v4l2_format;
34     struct v4l2_fmtdesc;
35     struct v4l2_frmsizeenum;
36 }
37 
38 namespace XCam {
39 #define FMT_NUM_PLANES 1
40 #define POLL_STOP_RET 3
41 
42 class V4l2Buffer;
43 
44 class V4l2Device {
45     friend class V4l2BufferProxy;
46     typedef std::vector<SmartPtr<V4l2Buffer>> BufferPool;
47 
48 public:
49     V4l2Device (const char *name = NULL);
50     virtual ~V4l2Device ();
51 
52     // before device open
53     bool set_device_name (const char *name);
54     bool set_sensor_id (int id);
55     bool set_capture_mode (uint32_t capture_mode);
56     bool set_mplanes_count (uint32_t planes_count);
57 
get_fd()58     int get_fd () const {
59         return _fd;
60     }
get_device_name()61     const char *get_device_name () const {
62         return _name;
63     }
is_opened()64     bool is_opened () const    {
65         return (_fd != -1);
66     }
is_activated()67     bool is_activated () const {
68         return _active;
69     }
70 
71     // set_mem_type must before set_format
72     bool set_mem_type (enum v4l2_memory type);
get_mem_type()73     enum v4l2_memory get_mem_type () const {
74         return _memory_type;
75     }
76     bool set_buf_type (enum v4l2_buf_type type);
77     bool set_buf_sync (bool sync);
get_buf_type()78     enum v4l2_buf_type get_buf_type () const {
79         return _buf_type;
80     }
get_size(uint32_t & width,uint32_t & height)81     void get_size (uint32_t &width, uint32_t &height) const {
82         width = _format.fmt.pix.width;
83         height = _format.fmt.pix.height;
84     }
get_pixel_format()85     uint32_t get_pixel_format () const {
86         return _format.fmt.pix.pixelformat;
87     }
88 
89     bool set_buffer_count (uint32_t buf_count);
get_buffer_count()90     int get_buffer_count () {
91         return _buf_count;
92     }
get_queued_bufcnt()93     int get_queued_bufcnt () {
94         return _queued_bufcnt;
95     }
96 
97     // set_framerate must before set_format
98     bool set_framerate (uint32_t n, uint32_t d);
99     void get_framerate (uint32_t &n, uint32_t &d);
100 
101     virtual XCamReturn open (bool nonblock = false);
102     virtual XCamReturn close ();
103 
104     XCamReturn query_cap(struct v4l2_capability &cap);
105     XCamReturn get_crop (struct v4l2_crop &crop);
106     XCamReturn set_crop (struct v4l2_crop &crop);
107     XCamReturn set_selection (struct v4l2_selection &select);
108     // set_format
109     virtual XCamReturn get_format (struct v4l2_format &format);
110     XCamReturn set_format (struct v4l2_format &format);
111     XCamReturn set_format (
112         uint32_t width, uint32_t height, uint32_t pixelformat,
113         enum v4l2_field field = V4L2_FIELD_NONE, uint32_t bytes_perline = 0);
114 
115     std::list<struct v4l2_fmtdesc> enum_formats ();
116 
117     virtual XCamReturn start (bool prepared = false);
118     virtual XCamReturn stop ();
119     virtual XCamReturn stop_streamoff ();
120     virtual XCamReturn stop_freebuffer ();
121     XCamReturn prepare ();
122 
123     virtual int poll_event (int timeout_msec, int stop_fd);
124 
125     SmartPtr<V4l2Buffer> get_buffer_by_index (int index);
126     virtual XCamReturn dequeue_buffer (SmartPtr<V4l2Buffer> &buf);
127     virtual XCamReturn queue_buffer (SmartPtr<V4l2Buffer> &buf, bool locked = false);
128     XCamReturn return_buffer (SmartPtr<V4l2Buffer> &buf);
129     XCamReturn return_buffer_to_pool (SmartPtr<V4l2Buffer> &buf);
130     // get free buf for type V4L2_BUF_TYPE_xxx_OUTPUT
131     XCamReturn get_buffer (SmartPtr<V4l2Buffer> &buf, int index = -1) const;
132     // use as less as possible
133     virtual int io_control (unsigned long cmd, void *arg);
get_use_type()134     virtual int get_use_type() { return 0;}
set_use_type(int type)135     virtual void set_use_type(int type) {}
136     SmartPtr<V4l2Buffer> get_available_buffer ();
137     virtual XCamReturn subscribe_event (int event);
138     virtual XCamReturn unsubscribe_event (int event);
139     virtual XCamReturn dequeue_event (struct v4l2_event &event);
140 
141 protected:
142 
143     //virtual functions, handle private actions on set_format
144     virtual XCamReturn pre_set_format (struct v4l2_format &format);
145     virtual XCamReturn post_set_format (struct v4l2_format &format);
146     virtual XCamReturn allocate_buffer (
147         SmartPtr<V4l2Buffer> &buf,
148         const struct v4l2_format &format,
149         const uint32_t index);
150     virtual XCamReturn release_buffer (SmartPtr<V4l2Buffer> &buf);
151 
152 private:
153     XCamReturn request_buffer ();
154     XCamReturn init_buffer_pool ();
155     XCamReturn fini_buffer_pool ();
156 
157     XCAM_DEAD_COPY (V4l2Device);
158 
159 protected:
160     char               *_name;
161     int                 _fd;
162     int32_t             _sensor_id;
163     uint32_t            _capture_mode;
164     enum v4l2_buf_type  _buf_type;
165     bool                _buf_sync;
166     enum v4l2_memory    _memory_type;
167     struct v4l2_plane  *_planes;
168 
169     struct v4l2_format  _format;
170     uint32_t            _fps_n;
171     uint32_t            _fps_d;
172 
173     bool                _active;
174 
175     // buffer pool
176     BufferPool          _buf_pool;
177     uint32_t            _buf_count;
178     uint32_t            _queued_bufcnt;
179     mutable Mutex      _buf_mutex;
180     int32_t            _mplanes_count;
181     XCamReturn buffer_new();
182     XCamReturn buffer_del();
183 };
184 
185 class V4l2SubDevice
186     : public V4l2Device
187 {
188 public:
189     explicit V4l2SubDevice (const char *name = NULL);
190 
191     virtual XCamReturn get_selection (int pad, uint32_t target, struct v4l2_subdev_selection &select);
192     virtual XCamReturn setFormat(struct v4l2_subdev_format &aFormat);
193     virtual XCamReturn getFormat(struct v4l2_subdev_format &aFormat);
194     virtual XCamReturn set_selection (struct v4l2_subdev_selection &aSelection);
195 
196     virtual XCamReturn start (bool prepared = false);
197     virtual XCamReturn stop ();
198 
199 private:
200     XCAM_DEAD_COPY (V4l2SubDevice);
201 };
202 
203 }
204 #endif // XCAM_V4L2_DEVICE_H
205 
206