xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/xcore/v4l2_buffer_proxy.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * v4l2_buffer_proxy.h - v4l2 buffer proxy
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_BUFFER_PROXY_H
22 #define XCAM_V4L2_BUFFER_PROXY_H
23 
24 #include <xcam_std.h>
25 #include <buffer_pool.h>
26 #include <linux/videodev2.h>
27 
28 namespace XCam {
29 
30 class V4l2Device;
31 
32 class V4l2Buffer
33     : public BufferData
34 {
35 public:
36     explicit V4l2Buffer (const struct v4l2_buffer &buf, const struct v4l2_format &format);
37     virtual ~V4l2Buffer ();
38 
get_buf()39     const struct v4l2_buffer & get_buf () const {
40         return _buf;
41     }
42 
set_timestamp(const struct timeval & time)43     void set_timestamp (const struct timeval &time) {
44         _buf.timestamp = time;
45     }
46 
set_timecode(const struct v4l2_timecode & code)47     void set_timecode (const struct v4l2_timecode &code) {
48         _buf.timecode = code;
49     }
50 
set_sequence(const uint32_t sequence)51     void set_sequence (const uint32_t sequence) {
52         _buf.sequence = sequence;
53     }
54 
set_length(const uint32_t value)55     void set_length (const uint32_t value) {
56         _length = value;
57     }
58 
get_length()59     uint32_t get_length () const {
60         return _length;
61     }
62 
set_queued(bool queued)63     void set_queued (bool queued) {
64         if (queued)
65             _queued = 1;
66         else
67             _queued = 0;
68     }
69 
get_queued()70     bool get_queued () const {
71         return !!_queued;
72     }
73 
set_expbuf_fd(const int fd)74     void set_expbuf_fd (const int fd) {
75         _expbuf_fd = fd;
76     }
77 
get_expbuf_fd()78     int get_expbuf_fd () {
79         return _expbuf_fd;
80     }
81 
set_expbuf_usrptr(uintptr_t ptr)82     void set_expbuf_usrptr(uintptr_t ptr) {
83         _expbuf_usrptr = ptr;
84     }
85 
get_expbuf_usrptr()86     uintptr_t get_expbuf_usrptr () {
87         return _expbuf_usrptr;
88     }
89 
reset()90     void reset () {
91         xcam_mem_clear (_buf.timestamp);
92         xcam_mem_clear (_buf.timecode);
93         _buf.sequence = 0;
94         _queued = 0;
95     }
96 
get_format()97     const struct v4l2_format & get_format () const {
98         return _format;
99     }
100 
101     // derived from BufferData
102     virtual uint8_t *map ();
103     virtual bool unmap ();
104     virtual int get_fd ();
set_reserved(uintptr_t reserved)105     void set_reserved(uintptr_t reserved) {
106         _reserved = reserved;
107     }
get_reserved()108     uintptr_t get_reserved() {
109         return _reserved;
110     }
111 
112 private:
113     XCAM_DEAD_COPY (V4l2Buffer);
114 
115 private:
116     struct v4l2_buffer  _buf;
117     struct v4l2_format  _format;
118     int _length;
119     int _expbuf_fd;
120     uintptr_t _expbuf_usrptr;
121     std::atomic<char> _queued;
122     uintptr_t _reserved;
123 };
124 
125 class V4l2BufferProxy
126     : public BufferProxy
127 {
128 public:
129     explicit V4l2BufferProxy (SmartPtr<V4l2Buffer> &buf, SmartPtr<V4l2Device> &device);
130 
131     ~V4l2BufferProxy ();
132 
get_v4l2_buf_index()133     int get_v4l2_buf_index () {
134         return get_v4l2_buf().index;
135     }
136 
get_v4l2_mem_type()137     enum v4l2_memory get_v4l2_mem_type () {
138         return (enum v4l2_memory)(get_v4l2_buf().memory);
139     }
140 
get_v4l2_buf_length()141     int get_v4l2_buf_length () {
142         return get_v4l2_buf().length;
143     }
144 
get_v4l2_buf_planar_length(int planar_index)145     int get_v4l2_buf_planar_length (int planar_index) {
146         return get_v4l2_buf().m.planes[planar_index].length;
147     }
148 
get_expbuf_fd()149     int get_expbuf_fd () {
150         SmartPtr<V4l2Buffer> v4l2buf = get_buffer_data().dynamic_cast_ptr<V4l2Buffer> ();
151         XCAM_ASSERT (v4l2buf.ptr());
152         return v4l2buf->get_expbuf_fd();
153     }
154 
get_v4l2_userptr()155     uintptr_t get_v4l2_userptr () {
156     #if 0
157         if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == get_v4l2_buf().type ||
158                 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE == get_v4l2_buf().type)
159             return get_v4l2_buf().m.planes[0].m.userptr;
160         else
161             return get_v4l2_buf().m.userptr;
162     #else
163         SmartPtr<V4l2Buffer> v4l2buf = get_buffer_data().dynamic_cast_ptr<V4l2Buffer> ();
164         XCAM_ASSERT (v4l2buf.ptr());
165         return v4l2buf->get_expbuf_usrptr();
166     #endif
167     }
168 
get_v4l2_planar_userptr(int planar_index)169     uintptr_t get_v4l2_planar_userptr (int planar_index) {
170         if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == get_v4l2_buf().type ||
171                 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE == get_v4l2_buf().type)
172             return get_v4l2_buf().m.planes[planar_index].m.userptr;
173         else
174             return get_v4l2_buf().m.userptr;
175     }
176 
get_sequence()177     uint32_t get_sequence () {
178         return get_v4l2_buf().sequence;
179     }
180 
get_reserved()181     uintptr_t get_reserved() {
182         SmartPtr<V4l2Buffer> v4l2buf = get_buffer_data().dynamic_cast_ptr<V4l2Buffer> ();
183         XCAM_ASSERT (v4l2buf.ptr());
184         return v4l2buf->get_reserved();
185     }
186 
187 private:
188     const struct v4l2_buffer & get_v4l2_buf ();
189 
190     void v4l2_format_to_video_info (
191         const struct v4l2_format &format, VideoBufferInfo &info);
192 
193     XCAM_DEAD_COPY (V4l2BufferProxy);
194 
195 private:
196     SmartPtr<V4l2Device>  _device;
197 };
198 }
199 
200 #endif //XCAM_V4L2_BUFFER_PROXY_H
201