xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/hwi/isp20/SPStreamProcUnit.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #include "rk_aiq_comm.h"
2 #include "rkispp-config.h"
3 #include "rkisp2-config.h"
4 #include "SPStreamProcUnit.h"
5 #include "CamHwIsp20.h"
6 namespace RkCam {
7 
SPStreamProcUnit(SmartPtr<V4l2Device> isp_sp_dev,int type,int isp_ver)8 SPStreamProcUnit::SPStreamProcUnit (SmartPtr<V4l2Device> isp_sp_dev, int type, int isp_ver)
9     : RKStream(isp_sp_dev, type)
10     , _first(true)
11 {
12     setPollCallback (this);
13     _ds_width = 0;
14     _ds_height = 0;
15     _ds_width_align = 0;
16     _ds_height_align = 0;
17     _isp_ver = isp_ver;
18 }
19 
~SPStreamProcUnit()20 SPStreamProcUnit::~SPStreamProcUnit ()
21 {
22 }
23 
start()24 void SPStreamProcUnit::start()
25 {
26     if (_isp_ver == ISP_V20) {
27         struct rkispp_trigger_mode tnr_trigger;
28         tnr_trigger.module = ISPP_MODULE_TNR;
29         tnr_trigger.on = 1;
30         int ret = _ispp_dev->io_control(RKISPP_CMD_TRIGGER_MODE, &tnr_trigger);
31     }
32 
33     if (ldg_enable) {
34         pAfTmp = (uint8_t*)malloc(_ds_width_align * _ds_height_align * sizeof(pAfTmp[0]) * 3 / 2);
35     }
36 
37     RKStream::start();
38     return;
39 }
40 
stop()41 void SPStreamProcUnit::stop()
42 {
43     if (_isp_ver == ISP_V20) {
44         struct rkispp_trigger_mode tnr_trigger;
45         tnr_trigger.module = ISPP_MODULE_TNR;
46         tnr_trigger.on = 0;
47         int ret = _ispp_dev->io_control(RKISPP_CMD_TRIGGER_MODE, &tnr_trigger);
48     }
49 
50     RKStream::stop();//stopDeviceOnly&stopThreadOnly
51     if (_isp_ver == ISP_V20)
52         deinit_fbcbuf_fd();
53     if (pAfTmp) {
54         free(pAfTmp);
55         pAfTmp = NULL;
56     }
57 }
58 
59 SmartPtr<VideoBuffer>
new_video_buffer(SmartPtr<V4l2Buffer> buf,SmartPtr<V4l2Device> dev)60 SPStreamProcUnit::new_video_buffer(SmartPtr<V4l2Buffer> buf,
61                                        SmartPtr<V4l2Device> dev)
62 {
63     if (_isp_ver == ISP_V21) {
64         SmartPtr<V4l2BufferProxy> img_buf = new V4l2BufferProxy (buf, dev);
65         img_buf->_buf_type = _dev_type;
66 
67         return img_buf;
68     }
69 
70     if (_first) {
71         init_fbcbuf_fd();
72         _first = false;
73     }
74 
75     //image
76     struct VideoBufferInfo vbufInfo;
77     vbufInfo.init(V4L2_PIX_FMT_NV12, _ds_width_align, _ds_height_align,
78                  XCAM_ALIGN_UP(_ds_width_align, 4), _ds_height_align, 0);
79     SmartPtr<SPImagBufferProxy> img_buf = new SPImagBufferProxy (buf, dev);
80     struct isp2x_ispgain_buf *ispgain = (struct isp2x_ispgain_buf *)img_buf->get_v4l2_planar_userptr(1);
81     img_buf->_buf_type = _dev_type;
82     img_buf->set_buff_fd(img_buf->get_expbuf_fd());
83     img_buf->set_video_info(vbufInfo);
84 
85     //ispgain
86     SmartPtr<V4l2BufferProxy> v4l2buf = img_buf.dynamic_cast_ptr<V4l2BufferProxy>();
87     _ispgain = new SubVideoBuffer (v4l2buf);
88     _ispgain->_buf_type = ISP_GAIN;
89     _ispgain->set_sequence(img_buf->get_sequence());
90     _ispgain->set_buff_info(get_fd_by_index(ispgain->gain_dmaidx), ispgain->gain_size);
91 
92     return img_buf;
93 }
94 
get_lowpass_fv(uint32_t sequence,SmartPtr<V4l2BufferProxy> buf_proxy)95 int SPStreamProcUnit::get_lowpass_fv(uint32_t sequence, SmartPtr<V4l2BufferProxy> buf_proxy)
96 {
97 #if RKAIQ_HAVE_AF_V20
98     SmartPtr<LensHw> lensHw = _focus_dev.dynamic_cast_ptr<LensHw>();
99     uint8_t *image_buf = (uint8_t *)buf_proxy->get_v4l2_planar_userptr(0);
100     rk_aiq_af_algo_meas_t meas_param;
101 
102     _afmeas_param_mutex.lock();
103     meas_param = _af_meas_params;
104     _afmeas_param_mutex.unlock();
105 
106     if (meas_param.sp_meas.enable) {
107         meas_param.wina_h_offs /= img_ds_size_x;
108         meas_param.wina_v_offs /= img_ds_size_y;
109         meas_param.wina_h_size /= img_ds_size_x;
110         meas_param.wina_v_size /= img_ds_size_y;
111         get_lpfv(sequence, image_buf, af_img_width, af_img_height,
112             af_img_width_align, af_img_height_align, pAfTmp, sub_shp4_4,
113             sub_shp8_8, high_light, high_light2, &meas_param);
114 
115         lensHw->setLowPassFv(sub_shp4_4, sub_shp8_8, high_light, high_light2, sequence);
116     }
117 #endif
118 
119     return 0;
120 }
121 
122 XCamReturn
poll_buffer_ready(SmartPtr<VideoBuffer> & buf)123 SPStreamProcUnit::poll_buffer_ready (SmartPtr<VideoBuffer> &buf)
124 {
125     XCamReturn ret = XCAM_RETURN_NO_ERROR;
126 
127 #if RKAIQ_HAVE_AF_V20
128     if (ldg_enable) {
129         SmartPtr<V4l2BufferProxy> buf_proxy = buf.dynamic_cast_ptr<V4l2BufferProxy>();
130         get_lowpass_fv(buf->get_sequence(), buf_proxy);
131     }
132 #endif
133 
134     if (_isp_ver == ISP_V20) {
135         if (_camHw->mHwResLintener) {
136             _camHw->mHwResLintener->hwResCb(buf);
137             SmartPtr<VideoBuffer> vbuf = _ispgain.dynamic_cast_ptr<VideoBuffer>();
138             _camHw->mHwResLintener->hwResCb(vbuf);
139         }
140     }
141 
142     return ret;
143 }
144 
prepare(CalibDbV2_Af_LdgParam_t * ldg_param,CalibDbV2_Af_HighLightParam_t * highlight,int width,int height,int stride)145 XCamReturn SPStreamProcUnit::prepare(CalibDbV2_Af_LdgParam_t *ldg_param, CalibDbV2_Af_HighLightParam_t *highlight, int width, int height, int stride)
146 {
147     XCamReturn ret = XCAM_RETURN_NO_ERROR;
148     uint32_t pixelformat, plane_cnt;
149     uint32_t ds_size_w = 4;
150     uint32_t ds_size_h = 4;
151 
152     if (_isp_ver == ISP_V20) {
153         pixelformat = V4L2_PIX_FMT_FBCG;
154         plane_cnt = 2;
155     } else {
156         pixelformat = V4L2_PIX_FMT_NV12;
157         plane_cnt = 1;
158     }
159 
160     if (!width && !height) {
161         struct v4l2_subdev_format isp_src_fmt;
162         isp_src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
163         isp_src_fmt.pad = 2;
164         ret = _isp_core_dev->getFormat(isp_src_fmt);
165         if (ret) {
166             LOGE("get mIspCoreDev src fmt failed !\n");
167             return XCAM_RETURN_ERROR_FAILED;
168         }
169         _src_width           = isp_src_fmt.format.width;
170         _src_height          = isp_src_fmt.format.height;
171         _ds_width            = (_src_width + ds_size_w - 1) / ds_size_w;
172         _ds_height           = (_src_height + ds_size_h - 1) / ds_size_h;
173         _ds_width_align      = (_ds_width  + 7) & (~7);
174         _ds_height_align     = (_ds_height + 7) & (~7);
175         int _stride          = XCAM_ALIGN_UP(_ds_width_align, 64);
176         img_ds_size_x        = ds_size_w;
177         img_ds_size_y        = ds_size_h;
178         LOGD( "set sp format: _src_width %d, _src_height %d, width %d %d height %d %d, stride %d\n",
179                _src_width, _src_height, _ds_width, _ds_width_align, _ds_height, _ds_height_align, _stride);
180         ret = _dev->set_format(_ds_width_align, _ds_height_align, pixelformat, V4L2_FIELD_NONE, 0);
181         if (ret) {
182             LOGE("set isp_sp_dev src fmt failed !\n");
183             ret = XCAM_RETURN_ERROR_FAILED;
184         }
185 
186         struct v4l2_format format;
187         _dev->get_format (format);
188         set_af_img_size(format.fmt.pix_mp.width, format.fmt.pix_mp.height,
189                         format.fmt.pix_mp.plane_fmt[0].bytesperline, format.fmt.pix_mp.height);
190     } else {
191         LOGD( "set sp format: width %d height %d\n", width, height);
192         ret = _dev->set_format(width, height, pixelformat, V4L2_FIELD_NONE, stride);
193         if (ret) {
194             LOGE("set isp_sp_dev src fmt failed !\n");
195             ret = XCAM_RETURN_ERROR_FAILED;
196         }
197     }
198     _dev->set_mem_type(V4L2_MEMORY_MMAP);
199     _dev->set_buf_type(V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
200     _dev->set_buffer_count(6);
201     _dev->set_mplanes_count(plane_cnt);
202 
203     ldg_enable = ldg_param->enable;
204     if (ldg_enable) {
205         _af_meas_params.sp_meas.ldg_xl      = ldg_param->ldg_xl;
206         _af_meas_params.sp_meas.ldg_yl      = ldg_param->ldg_yl;
207         _af_meas_params.sp_meas.ldg_kl      = ldg_param->ldg_kl;
208         _af_meas_params.sp_meas.ldg_xh      = ldg_param->ldg_xh;
209         _af_meas_params.sp_meas.ldg_yh      = ldg_param->ldg_yh;
210         _af_meas_params.sp_meas.ldg_kh      = ldg_param->ldg_kh;
211         _af_meas_params.sp_meas.highlight_th  = highlight->ther0;
212         _af_meas_params.sp_meas.highlight2_th = highlight->ther1;
213     }
214 
215     return ret;
216 }
217 
set_devices(CamHwIsp20 * camHw,SmartPtr<V4l2SubDevice> isp_core_dev,SmartPtr<V4l2SubDevice> ispp_dev,SmartPtr<V4l2SubDevice> lensdev)218 void SPStreamProcUnit::set_devices(CamHwIsp20* camHw, SmartPtr<V4l2SubDevice> isp_core_dev, SmartPtr<V4l2SubDevice> ispp_dev, SmartPtr<V4l2SubDevice> lensdev)
219 {
220     _camHw = camHw;
221     _isp_core_dev = isp_core_dev;
222     _ispp_dev = ispp_dev;
223     _focus_dev = lensdev;
224 }
225 
init_fbcbuf_fd()226 bool SPStreamProcUnit::init_fbcbuf_fd()
227 {
228     struct isp2x_buf_idxfd ispbuf_fd;
229     int res = -1;
230 
231     memset(&ispbuf_fd, 0, sizeof(ispbuf_fd));
232     res = _isp_core_dev->io_control(RKISP_CMD_GET_FBCBUF_FD , &ispbuf_fd);
233     if (res) {
234         LOGE("ioctl RKISP_CMD_GET_FBCBUF_FD failed");
235         return false;
236     }
237     LOGD(" fbc buf num=%d",ispbuf_fd.buf_num);
238     for (uint32_t i = 0; i < ispbuf_fd.buf_num; i++) {
239         if (ispbuf_fd.dmafd[i] < 0) {
240             LOGE("fbcbuf_fd[%u]:%d is illegal!", ispbuf_fd.index[i], ispbuf_fd.dmafd[i]);
241             LOGE("\n*** ASSERT: In File %s,line %d ***\n", __FILE__, __LINE__);
242             assert(0);
243         }
244         _buf_fd_map[ispbuf_fd.index[i]] = ispbuf_fd.dmafd[i];
245         LOGD("fbcbuf_fd[%u]:%d", ispbuf_fd.index[i], ispbuf_fd.dmafd[i]);
246     }
247     return true;
248 }
249 
deinit_fbcbuf_fd()250 bool SPStreamProcUnit::deinit_fbcbuf_fd()
251 {
252     std::map<int,int>::iterator it;
253      for (it = _buf_fd_map.begin(); it != _buf_fd_map.end(); ++it)
254         ::close(it->second);
255 
256     _buf_fd_map.clear();
257     return true;
258 }
259 
get_fd_by_index(int index)260 int SPStreamProcUnit::get_fd_by_index(int index)
261 {
262     if (index < 0)
263         return -1;
264     if (_buf_fd_map.find(index) == _buf_fd_map.end())
265         return -1;
266     return _buf_fd_map[index];
267 }
268 
269 //void SPStreamProcUnit::connect_tnr_stream(SmartPtr<TnrStatsStream> stream)
270 //{
271 //    _tnr_stream_unit = stream;
272 //}
273 
get_sp_resolution(int & width,int & height,int & aligned_w,int & aligned_h)274 XCamReturn SPStreamProcUnit::get_sp_resolution(int &width, int &height, int &aligned_w, int &aligned_h)
275 {
276     width = _ds_width;
277     height = _ds_height;
278     aligned_w = _ds_width_align;
279     aligned_h = _ds_height_align;
280     return XCAM_RETURN_NO_ERROR;
281 }
282 
update_af_meas_params(rk_aiq_af_algo_meas_t * af_meas)283 void SPStreamProcUnit::update_af_meas_params(rk_aiq_af_algo_meas_t *af_meas)
284 {
285     SmartLock locker (_afmeas_param_mutex);
286     if (af_meas && (0 != memcmp(af_meas, &_af_meas_params, sizeof(rk_aiq_af_algo_meas_t)))) {
287         _af_meas_params = *af_meas;
288     }
289 }
290 
set_af_img_size(int w,int h,int w_align,int h_align)291 void SPStreamProcUnit::set_af_img_size(int w, int h, int w_align, int h_align)
292 {
293     af_img_width         = w;
294     af_img_height        = h;
295     af_img_width_align   = w_align;
296     af_img_height_align  = h_align;
297     LOGI("af_img_width %d af_img_height %d af_img_width_align: %d af_img_height_align: %d\n",
298         w, h, w_align, h_align);
299 }
300 
301 }
302