1 /* 2 * Copyright (c) 2019 Rockchip Corporation 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 */ 17 #ifndef _STREAM_H_ 18 #define _STREAM_H_ 19 #include <sys/mman.h> 20 #include <v4l2_device.h> 21 #include "poll_thread.h" 22 #include "xcam_mutex.h" 23 #include "rk_aiq_types_priv.h" 24 25 using namespace XCam; 26 27 namespace RkCam { 28 29 class RKStream; 30 class RkPollThread : public Thread 31 { 32 public: 33 explicit RkPollThread (const char* thName, int type, SmartPtr<V4l2Device> dev, RKStream *stream); 34 explicit RkPollThread (const char* thName, int type, SmartPtr<V4l2SubDevice> dev, RKStream *stream); 35 virtual ~RkPollThread (); 36 virtual bool setPollCallback (PollCallback *callback); 37 virtual XCamReturn start(); 38 virtual XCamReturn stop (); setCamPhyId(int phyId)39 void setCamPhyId(int phyId) { 40 mCamPhyId = phyId; 41 } 42 protected: 43 XCAM_DEAD_COPY (RkPollThread); 44 XCamReturn poll_buffer_loop (); loop()45 virtual bool loop () { 46 XCamReturn ret = poll_buffer_loop (); 47 if (ret == XCAM_RETURN_NO_ERROR || ret == XCAM_RETURN_ERROR_TIMEOUT || 48 ret == XCAM_RETURN_BYPASS) 49 return true; 50 return false; 51 } 52 XCamReturn create_stop_fds (); 53 void destroy_stop_fds (); 54 int mCamPhyId; 55 protected: 56 static const int default_poll_timeout; 57 SmartPtr<V4l2Device> _dev; 58 SmartPtr<V4l2SubDevice> _subdev; 59 PollCallback *_poll_callback; 60 RKStream *_stream; 61 //frame syncronization 62 uint32_t frameid; 63 int _dev_type; 64 int _poll_stop_fd[2]; 65 }; 66 67 class RkEventPollThread : public RkPollThread 68 { 69 public: 70 explicit RkEventPollThread (const char* thName, int type, SmartPtr<V4l2Device> dev, RKStream *stream); 71 explicit RkEventPollThread (const char* thName, int type, SmartPtr<V4l2SubDevice> subdev, RKStream *stream); 72 virtual ~RkEventPollThread (); 73 protected: 74 XCAM_DEAD_COPY (RkEventPollThread); 75 virtual XCamReturn poll_event_loop (); loop()76 virtual bool loop () { 77 XCamReturn ret = poll_event_loop(); 78 if (ret == XCAM_RETURN_NO_ERROR || ret == XCAM_RETURN_ERROR_TIMEOUT || 79 ret == XCAM_RETURN_BYPASS) 80 return true; 81 return false; 82 } 83 //SmartPtr<V4l2SubDevice> _subdev; 84 struct v4l2_event _event; 85 }; 86 87 class CamHwIsp20; 88 // listen mp/sp stream on/off event 89 class RkStreamEventPollThread : public RkEventPollThread 90 { 91 public: RkStreamEventPollThread(const char * thName,SmartPtr<V4l2Device> dev,CamHwIsp20 * isp)92 explicit RkStreamEventPollThread(const char* thName, SmartPtr<V4l2Device> dev, CamHwIsp20* isp) 93 : RkEventPollThread(thName, ISP_POLL_ISPSTREAMSYNC, dev, NULL) 94 , _pIsp(isp) {}; ~RkStreamEventPollThread()95 virtual ~RkStreamEventPollThread(){}; 96 XCamReturn poll_event_loop (); 97 virtual XCamReturn start(); 98 virtual XCamReturn stop (); 99 protected: 100 XCAM_DEAD_COPY (RkStreamEventPollThread); 101 CamHwIsp20* _pIsp; 102 }; 103 104 class RKStream 105 { 106 public: 107 RKStream (SmartPtr<V4l2Device> dev, int type); 108 RKStream (SmartPtr<V4l2SubDevice> dev, int type); 109 RKStream (const char *path, int type); 110 virtual ~RKStream (); 111 virtual void start (); 112 virtual void startThreadOnly (); 113 virtual void startDeviceOnly (); 114 virtual void stop (); 115 virtual void stopThreadOnly (); 116 virtual void stopDeviceOnly (); 117 virtual void pause (); 118 virtual void resume (); 119 virtual bool setPollCallback (PollCallback *callback); 120 virtual SmartPtr<VideoBuffer> 121 new_video_buffer(SmartPtr<V4l2Buffer> buf, SmartPtr<V4l2Device> dev); 122 virtual SmartPtr<VideoBuffer> new_video_buffer(struct v4l2_event & event,SmartPtr<V4l2Device> dev)123 new_video_buffer(struct v4l2_event &event, SmartPtr<V4l2Device> dev) { return NULL; } 124 virtual SmartPtr<V4l2BufferProxy> new_v4l2proxy_buffer(SmartPtr<V4l2Buffer> buf,SmartPtr<V4l2Device> dev)125 new_v4l2proxy_buffer(SmartPtr<V4l2Buffer> buf, SmartPtr<V4l2Device> dev) { return NULL; } 126 void set_device_prepared(bool prepare); 127 XCamReturn virtual getFormat(struct v4l2_format &format); 128 XCamReturn virtual getFormat(struct v4l2_subdev_format &format); setCamPhyId(int phyId)129 void setCamPhyId(int phyId) { 130 mCamPhyId = phyId; 131 } 132 133 static const char* poll_type_to_str[ISP_POLL_POST_MAX]; 134 protected: 135 XCAM_DEAD_COPY (RKStream); 136 protected: 137 SmartPtr<V4l2Device> _dev; 138 SmartPtr<V4l2SubDevice> _subdev; 139 int _dev_type; 140 SmartPtr<RkPollThread> _poll_thread; 141 bool _dev_prepared; 142 int mCamPhyId; 143 }; 144 145 class BaseSensorHw; 146 class LensHw; 147 class RKStatsStream : public RKStream 148 { 149 public: 150 RKStatsStream (SmartPtr<V4l2Device> dev, int type); 151 // RKStatsStream (const char *name, int type); 152 virtual ~RKStatsStream (); 153 bool set_event_handle_dev(SmartPtr<BaseSensorHw> &dev); 154 bool set_iris_handle_dev(SmartPtr<LensHw> &dev); 155 bool set_focus_handle_dev(SmartPtr<LensHw> &dev); 156 bool set_rx_handle_dev(CamHwIsp20* dev); 157 virtual SmartPtr<VideoBuffer> 158 new_video_buffer(SmartPtr<V4l2Buffer> buf, SmartPtr<V4l2Device> dev); 159 protected: 160 XCAM_DEAD_COPY (RKStatsStream); 161 private: 162 //alliance devices 163 SmartPtr<BaseSensorHw> _event_handle_dev; 164 SmartPtr<LensHw> _iris_handle_dev; 165 SmartPtr<LensHw> _focus_handle_dev; 166 CamHwIsp20* _rx_handle_dev; 167 }; 168 169 class RKSofEventStream : public RKStream 170 { 171 public: 172 RKSofEventStream (SmartPtr<V4l2SubDevice> dev, int type, bool linkedToRk1608 = false); 173 // RKSofEventStream (const char *name, int type, bool linkedTo1608 = false); 174 virtual ~RKSofEventStream (); 175 virtual void start (); 176 virtual void stop (); 177 virtual SmartPtr<VideoBuffer> 178 new_video_buffer (struct v4l2_event &event, SmartPtr<V4l2Device> dev); 179 protected: 180 bool _linked_to_1608; 181 /* the sensors which are connected to Rk1608 only subscribe a reset event */ 182 static std::atomic<bool> _is_subscribed; 183 XCAM_DEAD_COPY (RKSofEventStream); 184 }; 185 186 class RKRawStream : public RKStream 187 { 188 public: 189 RKRawStream (SmartPtr<V4l2Device> dev, int index, int type); 190 virtual ~RKRawStream (); 191 virtual SmartPtr<V4l2BufferProxy> 192 new_v4l2proxy_buffer(SmartPtr<V4l2Buffer> buf, SmartPtr<V4l2Device> dev); 193 virtual SmartPtr<VideoBuffer> 194 new_video_buffer(SmartPtr<V4l2Buffer> buf, SmartPtr<V4l2Device> dev); 195 void set_reserved_data(int bpp); 196 public: 197 int _dev_index; 198 int _bpp; 199 int _reserved[2]; 200 protected: 201 XCAM_DEAD_COPY (RKRawStream); 202 }; 203 204 class RKPdafStream : public RKStream 205 { 206 public: 207 RKPdafStream (SmartPtr<V4l2Device> dev, int type); 208 virtual ~RKPdafStream (); 209 virtual SmartPtr<V4l2BufferProxy> 210 new_v4l2proxy_buffer(SmartPtr<V4l2Buffer> buf, SmartPtr<V4l2Device> dev); 211 212 protected: 213 XCAM_DEAD_COPY (RKPdafStream); 214 }; 215 216 class SubVideoBuffer : public VideoBuffer 217 { 218 public: SubVideoBuffer(int num,int index,int fd,const VideoBufferInfo & info)219 explicit SubVideoBuffer(int num, int index, int fd, const VideoBufferInfo& info) 220 : VideoBuffer(info) { 221 _buff_num = num; 222 _buff_idx = index; 223 _buff_fd = fd; 224 _buff_size = 0; 225 _buff_ptr = MAP_FAILED; 226 _v4l2buf_proxy = nullptr; 227 } SubVideoBuffer(int fd)228 explicit SubVideoBuffer(int fd) 229 :VideoBuffer() 230 { 231 _buff_fd = fd; 232 _buff_size = 0; 233 _buff_ptr = MAP_FAILED; 234 _v4l2buf_proxy = nullptr; 235 } SubVideoBuffer(SmartPtr<V4l2BufferProxy> & buf)236 explicit SubVideoBuffer(SmartPtr<V4l2BufferProxy> &buf) 237 :VideoBuffer() 238 { 239 _buff_fd = -1; 240 _buff_size = 0; 241 _buff_ptr = MAP_FAILED; 242 _v4l2buf_proxy = buf; 243 } SubVideoBuffer()244 explicit SubVideoBuffer() 245 :VideoBuffer() 246 { 247 _buff_fd = -1; 248 _buff_size = 0; 249 _buff_ptr = MAP_FAILED; 250 _v4l2buf_proxy = nullptr; 251 } ~SubVideoBuffer()252 virtual ~SubVideoBuffer() {} map()253 virtual uint8_t *map () 254 { 255 if (_v4l2buf_proxy.ptr()) { 256 return (uint8_t *)_v4l2buf_proxy->get_v4l2_userptr(); 257 } 258 259 if (_buff_ptr == MAP_FAILED) { 260 _buff_ptr = mmap (NULL, _buff_size ? _buff_size: get_size (), PROT_READ | PROT_WRITE, MAP_SHARED, _buff_fd, 0); 261 if (_buff_ptr == MAP_FAILED) { 262 LOGE("mmap failed, size=%d,fd=%d",_buff_size,_buff_fd); 263 return nullptr; 264 } 265 } 266 return (uint8_t *)_buff_ptr; 267 } unmap()268 virtual bool unmap () 269 { 270 if (_v4l2buf_proxy.ptr()) { 271 return true; 272 } 273 274 if (_buff_ptr != MAP_FAILED) { 275 munmap(_buff_ptr, _buff_size ? _buff_size: get_size ()); 276 _buff_ptr = MAP_FAILED; 277 } 278 return true; 279 } get_fd()280 virtual int get_fd () 281 { 282 if (_v4l2buf_proxy.ptr()) { 283 return _v4l2buf_proxy->get_expbuf_fd(); 284 } 285 return _buff_fd; 286 } set_buff_info(int fd,int size)287 int set_buff_info(int fd, int size) 288 { 289 _buff_fd = fd; 290 _buff_size = size; 291 return 0; 292 } 293 get_buf_num()294 int get_buf_num() { return _buff_num; } 295 get_index()296 int get_index() { return _buff_idx; } 297 get_size()298 int get_size() { return _buff_size; } 299 300 protected: 301 XCAM_DEAD_COPY (SubVideoBuffer); 302 private: 303 int _buff_fd; 304 int _buff_size; 305 int _buff_idx; 306 int _buff_num; 307 void *_buff_ptr; 308 SmartPtr<V4l2BufferProxy> _v4l2buf_proxy; 309 }; 310 311 class SubV4l2BufferProxy : public V4l2BufferProxy 312 { 313 public: SubV4l2BufferProxy(SmartPtr<V4l2Buffer> & buf,SmartPtr<V4l2Device> & device)314 explicit SubV4l2BufferProxy(SmartPtr<V4l2Buffer> &buf, SmartPtr<V4l2Device> &device) 315 :V4l2BufferProxy(buf, device) 316 { 317 _buff_ptr = MAP_FAILED; 318 _buff_fd = -1; 319 _buff_size = 0; 320 } ~SubV4l2BufferProxy()321 virtual ~SubV4l2BufferProxy() {} map()322 virtual uint8_t *map () 323 { 324 if (_buff_ptr == MAP_FAILED) { 325 _buff_ptr = mmap (NULL, _buff_size ? _buff_size: get_size (), PROT_READ | PROT_WRITE, MAP_SHARED, _buff_fd, 0); 326 if (_buff_ptr == MAP_FAILED) { 327 LOGE("mmap failed, size=%d,fd=%d",_buff_size,_buff_fd); 328 return nullptr; 329 } 330 } 331 return (uint8_t *)_buff_ptr; 332 } unmap()333 virtual bool unmap () 334 { 335 if (_buff_ptr != MAP_FAILED) { 336 munmap(_buff_ptr, _buff_size ? _buff_size: get_size ()); 337 _buff_ptr = MAP_FAILED; 338 } 339 return true; 340 } get_fd()341 virtual int get_fd () 342 { 343 return _buff_fd; 344 } set_buff_info(int fd,int size)345 int set_buff_info(int fd, int size) 346 { 347 _buff_fd = fd; 348 _buff_size = size; 349 return 0; 350 } 351 protected: 352 XCAM_DEAD_COPY (SubV4l2BufferProxy); 353 int _buff_fd; 354 int _buff_size; 355 void *_buff_ptr; 356 }; 357 } 358 #endif 359