• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * cl_context.h - CL context
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_CONTEXT_H
22 #define XCAM_CL_CONTEXT_H
23 
24 #include <xcam_std.h>
25 #include <map>
26 #include <list>
27 #include <CL/cl.h>
28 #include <ocl/cl_event.h>
29 
30 namespace XCam {
31 
32 class CLKernel;
33 class CLDevice;
34 class CLCommandQueue;
35 
36 class CLVaBuffer;
37 class CLVaImage;
38 class CLIntelContext;
39 
40 /* default context:
41  *  SmartPtr<CLContext> context = CLDevice::instance()->get_context();
42  */
43 
44 class CLContext {
45     //typedef std::map<std::string, SmartPtr<CLKernel>> CLKernelMap;
46     typedef std::list<SmartPtr<CLCommandQueue>> CLCmdQueueList;
47 
48     friend class CLDevice;
49     friend class CLKernel;
50     friend class CLMemory;
51     friend class CLBuffer;
52     friend class CLSubBuffer;
53     friend class CLImage;
54     friend class CLImage2D;
55     friend class CLImage2DArray;
56 
57     friend class CLVaBuffer;
58     friend class CLVaImage;
59     friend class CLIntelContext;
60 
61 public:
62     enum KernelBuildType {
63         KERNEL_BUILD_BINARY = 0,
64         KERNEL_BUILD_SOURCE,
65     };
66 
67     virtual ~CLContext ();
get_context_id()68     cl_context get_context_id () {
69         return _context_id;
70     }
71 
72     XCamReturn flush ();
73     XCamReturn finish ();
74 
75     void terminate ();
76 
77 private:
78     static void context_pfn_notify (
79         const char* erro_info, const void *private_info,
80         size_t cb, void *user_data);
81     static void program_pfn_notify (
82         cl_program program, void *user_data);
83 
84     explicit CLContext (SmartPtr<CLDevice> &device);
85     SmartPtr<CLCommandQueue> create_cmd_queue (SmartPtr<CLContext> &self);
86     cl_kernel generate_kernel_id (
87         CLKernel *kernel,
88         const uint8_t *source,
89         size_t length,
90         KernelBuildType type,
91         uint8_t **gen_binary,
92         size_t *binary_size,
93         const char *build_option);
94     void destroy_kernel_id (cl_kernel &kernel_id);
95     XCamReturn execute_kernel (
96         const SmartPtr<CLKernel> kernel,
97         const SmartPtr<CLCommandQueue> queue,
98         CLEventList &events_wait = CLEvent::EmptyList,
99         SmartPtr<CLEvent> &event_out = CLEvent::NullEvent);
100 
101     XCamReturn set_event_callback (
102         SmartPtr<CLEvent> &event, cl_int status,
103         void (*callback) (cl_event, cl_int, void*),
104         void *user_data);
105     //bool insert_kernel (SmartPtr<CLKernel> &kernel);
106 
107     bool init_context ();
108     void destroy_context ();
is_valid()109     bool is_valid () const {
110         return (_context_id != NULL);
111     }
112 
113     bool init_cmd_queue (SmartPtr<CLContext> &self);
114     SmartPtr<CLCommandQueue> get_default_cmd_queue ();
115 
116     //Memory, Image
117     cl_mem create_image (
118         cl_mem_flags flags, const cl_image_format& format,
119         const cl_image_desc &image_info, void *host_ptr = NULL);
120     void destroy_mem (cl_mem mem_id);
121 
122     // Buffer
123     cl_mem create_buffer (uint32_t size, cl_mem_flags flags, void *host_ptr);
124 
125     cl_mem create_sub_buffer (
126         cl_mem main_mem,
127         cl_buffer_region region,
128         cl_mem_flags flags = CL_MEM_READ_WRITE);
129 
130     XCamReturn enqueue_read_buffer (
131         cl_mem buf_id, void *ptr,
132         uint32_t offset, uint32_t size,
133         bool block = true,
134         CLEventList &events_wait = CLEvent::EmptyList,
135         SmartPtr<CLEvent> &event_out = CLEvent::NullEvent);
136 
137     XCamReturn enqueue_write_buffer (
138         cl_mem buf_id, void *ptr,
139         uint32_t offset, uint32_t size,
140         bool block = true,
141         CLEventList &events_wait = CLEvent::EmptyList,
142         SmartPtr<CLEvent> &event_out = CLEvent::NullEvent);
143 
144     XCamReturn enqueue_map_buffer (
145         cl_mem buf_id, void *&ptr,
146         uint32_t offset, uint32_t size,
147         bool block = true,
148         cl_map_flags map_flags = CL_MAP_READ | CL_MAP_WRITE,
149         CLEventList &events_wait = CLEvent::EmptyList,
150         SmartPtr<CLEvent> &event_out = CLEvent::NullEvent);
151 
152     XCamReturn enqueue_map_image (
153         cl_mem buf_id, void *&ptr,
154         const size_t *origin,
155         const size_t *region,
156         size_t *image_row_pitch,
157         size_t *image_slice_pitch,
158         bool block = true,
159         cl_map_flags map_flags = CL_MAP_READ | CL_MAP_WRITE,
160         CLEventList &events_wait = CLEvent::EmptyList,
161         SmartPtr<CLEvent> &event_out = CLEvent::NullEvent);
162 
163     XCamReturn enqueue_unmap (
164         cl_mem mem_id,
165         void *ptr,
166         CLEventList &events_wait = CLEvent::EmptyList,
167         SmartPtr<CLEvent> &event_out = CLEvent::NullEvent);
168 
169     // return valid event count
170     static uint32_t event_list_2_id_array (
171         CLEventList &events_wait,
172         cl_event *cl_events, uint32_t max_count);
173 
174     XCAM_DEAD_COPY (CLContext);
175 
176 private:
177     cl_context                  _context_id;
178     SmartPtr<CLDevice>          _device;
179     //CLKernelMap                 _kernel_map;
180     CLCmdQueueList              _cmd_queue_list;
181 };
182 
183 class CLCommandQueue {
184     friend class CLContext;
185 
186 public:
187     virtual ~CLCommandQueue ();
get_cmd_queue_id()188     cl_command_queue get_cmd_queue_id () {
189         return _cmd_queue_id;
190     }
191 
192 private:
193     explicit CLCommandQueue (SmartPtr<CLContext> &context, cl_command_queue id);
194     void destroy ();
195     XCAM_DEAD_COPY (CLCommandQueue);
196 
197 private:
198     SmartPtr<CLContext>     _context;
199     cl_command_queue        _cmd_queue_id;
200 };
201 
202 };
203 
204 #endif //XCAM_CL_CONTEXT_H
205