1 /*
2 * smart_buffer_priv.cpp - smart buffer for XCamVideoBuffer
3 *
4 * Copyright (c) 2016-2017 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 #include "smart_buffer_priv.h"
22 #include "xcam_log.h"
23 #include "rk_aiq_types_priv.h"
24
25 #if HAS_LIBDRM
26 #include "xcore/drm_buffer.h"
27 #endif
28
29 namespace XCam {
30
buf_type_to_str(int type,char * str)31 static void buf_type_to_str(int type, char *str)
32 {
33 switch (type)
34 {
35 case ISP_POLL_TX:
36 strncpy(str, "RAW_BUFFER", SMTBUF_NAME_STR_LEN);
37 break;
38 case ISP_POLL_SP:
39 strncpy(str, "SPIMG_BUFFER", SMTBUF_NAME_STR_LEN);
40 break;
41 case ISP_GAIN:
42 strncpy(str, "ISPGAIN_BUFFER", SMTBUF_NAME_STR_LEN);
43 break;
44 case ISP_NR_IMG:
45 strncpy(str, "NRIMG_BUFFER", SMTBUF_NAME_STR_LEN);
46 break;
47 case ISPP_GAIN_KG:
48 strncpy(str, "GAINKG_BUFFER", SMTBUF_NAME_STR_LEN);
49 break;
50 case ISPP_GAIN_WR:
51 strncpy(str, "GAINWR_BUFFER", SMTBUF_NAME_STR_LEN);
52 break;
53 default:
54 strncpy(str, "UNKNOWN", SMTBUF_NAME_STR_LEN);
55 break;
56 }
57 }
58
59
SmartBufferPriv(const SmartPtr<VideoBuffer> & buf)60 SmartBufferPriv::SmartBufferPriv (const SmartPtr<VideoBuffer> &buf)
61 : _ref (NULL)
62 {
63 XCAM_ASSERT (buf.ptr ());
64 this->_buf_ptr = buf;
65
66 if (!buf.ptr ()) {
67 return;
68 }
69
70 _ref = new RefCount ();
71
72 const VideoBufferInfo& video_info = buf->get_video_info ();
73
74 this->base.info = *((const XCamVideoBufferInfo*)&video_info);
75 this->base.mem_type = XCAM_MEM_TYPE_PRIVATE_BO;
76 this->base.timestamp = buf->get_timestamp ();
77 this->base.frame_id = buf->get_sequence();
78
79 this->base.ref = SmartBufferPriv::buf_ref;
80 this->base.unref = SmartBufferPriv::buf_unref;
81 this->base.map = SmartBufferPriv::buf_map;
82 this->base.unmap = SmartBufferPriv::buf_unmap;
83 this->base.get_fd = SmartBufferPriv::buf_get_fd;
84 this->get_bo = SmartBufferPriv::buf_get_bo;
85 this->notify = NULL;
86 this->pUserContext = NULL;
87 #ifndef NDEBUG
88 buf_type_to_str(buf->_buf_type, this->nameStr);
89 #endif
90
91 }
92
SmartBufferPriv(uint32_t frameId,const SmartPtr<VideoBuffer> & buf)93 SmartBufferPriv::SmartBufferPriv (uint32_t frameId, const SmartPtr<VideoBuffer> &buf)
94 : _ref (NULL)
95 {
96 XCAM_ASSERT (buf.ptr ());
97 this->_buf_ptr = buf;
98
99 if (!buf.ptr ()) {
100 return;
101 }
102
103 _ref = new RefCount ();
104
105 const VideoBufferInfo& video_info = buf->get_video_info ();
106
107 this->base.info = *((const XCamVideoBufferInfo*)&video_info);
108 this->base.mem_type = XCAM_MEM_TYPE_PRIVATE_BO;
109 this->base.timestamp = buf->get_timestamp ();
110 this->base.frame_id = frameId;
111
112 this->base.ref = SmartBufferPriv::buf_ref;
113 this->base.unref = SmartBufferPriv::buf_unref;
114 this->base.map = SmartBufferPriv::buf_map;
115 this->base.unmap = SmartBufferPriv::buf_unmap;
116 this->base.get_fd = SmartBufferPriv::buf_get_fd;
117 this->get_bo = SmartBufferPriv::buf_get_bo;
118 this->notify = NULL;
119 this->pUserContext = NULL;
120 }
121
~SmartBufferPriv()122 SmartBufferPriv::~SmartBufferPriv ()
123 {
124 XCAM_LOG_DEBUG ("SmartBufferPriv destracted");
125 delete _ref;
126 }
127
128 void
buf_ref(XCamVideoBuffer * data)129 SmartBufferPriv::buf_ref (XCamVideoBuffer *data)
130 {
131 SmartBufferPriv *buf = (SmartBufferPriv*) data;
132 XCAM_ASSERT (buf->_ref);
133 #ifndef NDEBUG
134 XCAM_LOG_DEBUG ("%s: buf ref called, fd %d", buf->nameStr, buf_get_fd(data));
135 #endif
136 if (buf->_ref)
137 buf->_ref->ref ();
138 }
139
140 uint32_t
buf_unref(XCamVideoBuffer * data)141 SmartBufferPriv::buf_unref (XCamVideoBuffer *data)
142 {
143 SmartBufferPriv *buf = (SmartBufferPriv*) data;
144 uint32_t count = 0;
145 XCAM_ASSERT (buf->_ref);
146 if (buf->_ref) {
147 count = buf->_ref->unref();
148 if (!count) {
149 XCamVideoBufferRK* upper_data = (XCamVideoBufferRK*)data;
150 if (upper_data->notify)
151 upper_data->notify (upper_data->pUserContext, data);
152 #ifndef NDEBUG
153 XCAM_LOG_DEBUG ("%s: buf released, fd %d", buf->nameStr, buf_get_fd(data));
154 #endif
155 delete buf;
156 }
157 }
158 return count;
159 }
160
161 uint8_t *
buf_map(XCamVideoBuffer * data)162 SmartBufferPriv::buf_map (XCamVideoBuffer *data)
163 {
164 SmartBufferPriv *buf = (SmartBufferPriv*) data;
165 XCAM_ASSERT (buf->_buf_ptr.ptr ());
166 return buf->_buf_ptr->map ();
167 }
168
169 void
buf_unmap(XCamVideoBuffer * data)170 SmartBufferPriv::buf_unmap (XCamVideoBuffer *data)
171 {
172 SmartBufferPriv *buf = (SmartBufferPriv*) data;
173 XCAM_ASSERT (buf->_buf_ptr.ptr ());
174 buf->_buf_ptr->unmap ();
175 }
176
177 int
buf_get_fd(XCamVideoBuffer * data)178 SmartBufferPriv::buf_get_fd (XCamVideoBuffer *data)
179 {
180 SmartBufferPriv *buf = (SmartBufferPriv*) data;
181 XCAM_ASSERT (buf->_buf_ptr.ptr ());
182 return buf->_buf_ptr->get_fd ();
183 }
184
185 void *
buf_get_bo(XCamVideoBufferRK * data)186 SmartBufferPriv::buf_get_bo (XCamVideoBufferRK *data)
187 {
188 #if HAS_LIBDRM
189 SmartBufferPriv* buf = (SmartBufferPriv*)data;
190 XCAM_ASSERT(buf->_buf_ptr.ptr());
191
192 SmartPtr<DrmBufferProxy> bo_buf = buf->_buf_ptr.dynamic_cast_ptr<DrmBufferProxy>();
193 XCAM_FAIL_RETURN(ERROR, bo_buf.ptr(), NULL, "get DrmBufferProxy failed");
194
195 return bo_buf->get_bo();
196 #else
197
198 XCAM_LOG_ERROR("VideoBuffer doesn't support DrmBoBuffer");
199
200 XCAM_UNUSED(data);
201 #endif
202 return NULL;
203 }
204
205 int
registerNotifyCb(SmartBufferCbNotify_t fpCallback,void * pUserContext)206 SmartBufferPriv::registerNotifyCb(SmartBufferCbNotify_t fpCallback, void *pUserContext)
207 {
208 if(pUserContext == NULL)
209 {
210 return -1;
211 }
212
213 if(fpCallback == NULL)
214 {
215 return -1;
216 }
217 this->notify = fpCallback;
218 this->pUserContext = pUserContext;
219 return 0;
220 }
221
222 XCamVideoBuffer *
convert_to_XCamVideoBuffer(const SmartPtr<VideoBuffer> & buf)223 convert_to_XCamVideoBuffer (const SmartPtr<VideoBuffer> &buf)
224 {
225 SmartBufferPriv *priv_buf = new SmartBufferPriv (buf);
226 XCAM_ASSERT (priv_buf);
227
228 if (priv_buf->is_valid ())
229 return (XCamVideoBuffer *)(priv_buf);
230
231 delete priv_buf;
232 return NULL;
233 }
234
235 XCamVideoBuffer *
convert_to_external_buffer(const SmartPtr<VideoBuffer> & buf)236 convert_to_external_buffer (const SmartPtr<VideoBuffer> &buf)
237 {
238 SmartBufferPriv *priv_buf = new SmartBufferPriv (buf);
239 XCAM_ASSERT (priv_buf);
240
241 if (priv_buf->is_valid ())
242 return (XCamVideoBuffer *)(priv_buf);
243
244 delete priv_buf;
245 return NULL;
246 }
247
248 }
249