1 /* 2 * cl_memory.h - CL memory 3 * 4 * Copyright (c) 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_CL_MEMORY_H 22 #define XCAM_CL_MEMORY_H 23 24 #include "ocl/cl_context.h" 25 #include "ocl/cl_event.h" 26 #include "video_buffer.h" 27 28 #include <unistd.h> 29 30 namespace XCam { 31 32 struct CLImageDesc { 33 cl_image_format format; 34 uint32_t width; 35 uint32_t height; 36 uint32_t row_pitch; 37 uint32_t slice_pitch; 38 uint32_t array_size; 39 uint32_t size; 40 41 CLImageDesc (); 42 bool operator == (const CLImageDesc& desc) const; 43 }; 44 45 class CLMemory { 46 public: 47 explicit CLMemory (const SmartPtr<CLContext> &context); 48 virtual ~CLMemory (); 49 get_mem_id()50 cl_mem &get_mem_id () { 51 return _mem_id; 52 } is_valid()53 bool is_valid () const { 54 return _mem_id != NULL; 55 } 56 57 bool get_cl_mem_info ( 58 cl_image_info param_name, size_t param_size, 59 void *param, size_t *param_size_ret = NULL); 60 61 int32_t export_fd (); 62 void release_fd (); 63 64 XCamReturn enqueue_unmap ( 65 void *ptr, 66 CLEventList &events_wait = CLEvent::EmptyList, 67 SmartPtr<CLEvent> &event_out = CLEvent::NullEvent); 68 69 protected: 70 void set_mem_id (cl_mem &id, bool need_destroy = true) { 71 _mem_id = id; 72 _mem_need_destroy = need_destroy; 73 } 74 set_mapped_ptr(void * ptr)75 void set_mapped_ptr (void *ptr) { 76 _mapped_ptr = ptr; 77 } 78 get_context()79 SmartPtr<CLContext> &get_context () { 80 return _context; 81 } 82 83 private: 84 XCAM_DEAD_COPY (CLMemory); 85 86 private: 87 SmartPtr<CLContext> _context; 88 cl_mem _mem_id; 89 int32_t _mem_fd; 90 bool _mem_need_destroy; 91 void *_mapped_ptr; 92 }; 93 94 class CLBuffer 95 : public CLMemory 96 { 97 protected: 98 explicit CLBuffer (const SmartPtr<CLContext> &context); 99 100 public: 101 explicit CLBuffer ( 102 const SmartPtr<CLContext> &context, uint32_t size, 103 cl_mem_flags flags = CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, 104 void *host_ptr = NULL); 105 106 XCamReturn enqueue_read ( 107 void *ptr, uint32_t offset, uint32_t size, 108 CLEventList &event_waits = CLEvent::EmptyList, 109 SmartPtr<CLEvent> &event_out = CLEvent::NullEvent); 110 XCamReturn enqueue_write ( 111 void *ptr, uint32_t offset, uint32_t size, 112 CLEventList &event_waits = CLEvent::EmptyList, 113 SmartPtr<CLEvent> &event_out = CLEvent::NullEvent); 114 XCamReturn enqueue_map ( 115 void *&ptr, uint32_t offset, uint32_t size, 116 cl_map_flags map_flags = CL_MAP_READ | CL_MAP_WRITE, 117 CLEventList &event_waits = CLEvent::EmptyList, 118 SmartPtr<CLEvent> &event_out = CLEvent::NullEvent); 119 get_buf_size()120 uint32_t get_buf_size () const { 121 return _size; 122 } 123 124 protected: set_buf_size(uint32_t size)125 void set_buf_size (uint32_t size) { 126 _size = size; 127 } 128 129 private: 130 bool init_buffer ( 131 const SmartPtr<CLContext> &context, uint32_t size, 132 cl_mem_flags flags, void *host_ptr); 133 134 XCAM_DEAD_COPY (CLBuffer); 135 136 private: 137 cl_mem_flags _flags; 138 uint32_t _size; 139 }; 140 141 class CLSubBuffer 142 : public CLBuffer 143 { 144 protected: 145 explicit CLSubBuffer (const SmartPtr<CLContext> &context); 146 147 public: 148 explicit CLSubBuffer ( 149 const SmartPtr<CLContext> &context, 150 SmartPtr<CLBuffer> main_buf, 151 cl_mem_flags flags = CL_MEM_READ_WRITE, 152 uint32_t offset = 0, 153 uint32_t size = 0); 154 155 private: 156 bool init_sub_buffer ( 157 const SmartPtr<CLContext> &context, 158 SmartPtr<CLBuffer> main_buf, 159 cl_mem_flags flags, 160 uint32_t offset, 161 uint32_t size); 162 163 XCAM_DEAD_COPY (CLSubBuffer); 164 165 private: 166 SmartPtr<CLBuffer> _main_buf; 167 cl_mem_flags _flags; 168 uint32_t _size; 169 }; 170 171 class CLImage 172 : public CLMemory 173 { 174 public: ~CLImage()175 virtual ~CLImage () {} 176 get_image_desc()177 const CLImageDesc &get_image_desc () const { 178 return _image_desc; 179 } 180 uint32_t get_pixel_bytes () const; 181 182 static uint32_t calculate_pixel_bytes (const cl_image_format &fmt); 183 static bool video_info_2_cl_image_desc ( 184 const VideoBufferInfo & video_info, 185 CLImageDesc &cl_desc); 186 187 XCamReturn enqueue_map ( 188 void *&ptr, 189 size_t *origin, size_t *region, 190 size_t *row_pitch, size_t *slice_pitch, 191 cl_map_flags map_flags = CL_MAP_READ | CL_MAP_WRITE, 192 CLEventList &event_waits = CLEvent::EmptyList, 193 SmartPtr<CLEvent> &event_out = CLEvent::NullEvent); 194 195 protected: 196 explicit CLImage (const SmartPtr<CLContext> &context); 197 void init_desc_by_image (); 198 bool get_cl_image_info ( 199 cl_image_info param_name, size_t param_size, 200 void *param, size_t *param_size_ret = NULL); 201 202 private: 203 XCAM_DEAD_COPY (CLImage); 204 205 CLImageDesc _image_desc; 206 }; 207 208 class CLImage2D 209 : public CLImage 210 { 211 public: 212 explicit CLImage2D ( 213 const SmartPtr<CLContext> &context, 214 const VideoBufferInfo &video_info, 215 cl_mem_flags flags = CL_MEM_READ_WRITE); 216 217 explicit CLImage2D ( 218 const SmartPtr<CLContext> &context, 219 const CLImageDesc &cl_desc, 220 cl_mem_flags flags = CL_MEM_READ_WRITE, 221 SmartPtr<CLBuffer> bind_buf = NULL); 222 get_bind_buf()223 SmartPtr<CLBuffer> get_bind_buf () { 224 return _bind_buf; 225 } 226 ~CLImage2D()227 ~CLImage2D () {} 228 229 private: 230 bool init_image_2d ( 231 const SmartPtr<CLContext> &context, 232 const CLImageDesc &cl_desc, 233 cl_mem_flags flags); 234 235 XCAM_DEAD_COPY (CLImage2D); 236 237 private: 238 SmartPtr<CLBuffer> _bind_buf; 239 }; 240 241 class CLImage2DArray 242 : public CLImage 243 { 244 public: 245 explicit CLImage2DArray ( 246 const SmartPtr<CLContext> &context, 247 const VideoBufferInfo &video_info, 248 cl_mem_flags flags = CL_MEM_READ_WRITE, 249 uint32_t extra_array_size = 0); 250 ~CLImage2DArray()251 ~CLImage2DArray () {} 252 253 private: 254 bool init_image_2d_array ( 255 const SmartPtr<CLContext> &context, 256 const CLImageDesc &cl_desc, 257 cl_mem_flags flags); 258 259 XCAM_DEAD_COPY (CLImage2DArray); 260 }; 261 262 263 }; 264 #endif // 265