1 /* 2 * buffer_pool.h - buffer pool 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_BUFFER_POOL_H 22 #define XCAM_BUFFER_POOL_H 23 24 #include <xcam_std.h> 25 #include <safe_list.h> 26 #include <video_buffer.h> 27 28 namespace XCam { 29 30 class BufferPool; 31 32 class BufferData { 33 protected: BufferData()34 explicit BufferData () {} 35 36 public: ~BufferData()37 virtual ~BufferData () {} 38 39 virtual uint8_t *map () = 0; 40 virtual bool unmap () = 0; get_fd()41 virtual int get_fd () { 42 return -1; 43 } 44 45 private: 46 XCAM_DEAD_COPY (BufferData); 47 }; 48 49 class BufferProxy 50 : public VideoBuffer 51 { 52 public: 53 explicit BufferProxy (const VideoBufferInfo &info, const SmartPtr<BufferData> &data); 54 explicit BufferProxy (const SmartPtr<BufferData> &data); 55 virtual ~BufferProxy (); 56 set_buf_pool(const SmartPtr<BufferPool> & pool)57 void set_buf_pool (const SmartPtr<BufferPool> &pool) { 58 _pool = pool; 59 } 60 61 // derived from VideoBuffer 62 virtual uint8_t *map (); 63 virtual bool unmap (); 64 virtual int get_fd(); 65 66 protected: get_buffer_data()67 SmartPtr<BufferData> &get_buffer_data () { 68 return _data; 69 } 70 71 private: 72 XCAM_DEAD_COPY (BufferProxy); 73 74 private: 75 SmartPtr<BufferData> _data; 76 SmartPtr<BufferPool> _pool; 77 }; 78 79 class BufferPool 80 : public RefObj 81 { 82 friend class BufferProxy; 83 84 public: 85 explicit BufferPool (); 86 virtual ~BufferPool (); 87 88 bool set_video_info (const VideoBufferInfo &info); 89 bool reserve (uint32_t max_count = 4); 90 SmartPtr<VideoBuffer> get_buffer (const SmartPtr<BufferPool> &self); 91 SmartPtr<VideoBuffer> get_buffer (); 92 93 void stop (); 94 get_video_info()95 const VideoBufferInfo & get_video_info () const { 96 return _buffer_info; 97 } 98 has_free_buffers()99 bool has_free_buffers () { 100 return !_buf_list.is_empty (); 101 } 102 get_free_buffer_size()103 uint32_t get_free_buffer_size () { 104 return _buf_list.size (); 105 } 106 107 protected: 108 virtual bool fixate_video_info (VideoBufferInfo &info); 109 virtual SmartPtr<BufferData> allocate_data (const VideoBufferInfo &buffer_info) = 0; 110 virtual SmartPtr<BufferProxy> create_buffer_from_data (SmartPtr<BufferData> &data); 111 112 bool add_data_unsafe (const SmartPtr<BufferData> &data); 113 114 void update_video_info_unsafe (const VideoBufferInfo &info); 115 116 private: 117 void release (SmartPtr<BufferData> &data); 118 XCAM_DEAD_COPY (BufferPool); 119 120 private: 121 Mutex _mutex; 122 VideoBufferInfo _buffer_info; 123 SafeList<BufferData> _buf_list; 124 uint32_t _allocated_num; 125 uint32_t _max_count; 126 bool _started; 127 }; 128 129 }; 130 131 #endif //XCAM_BUFFER_POOL_H 132 133