Home | History | Annotate | Download | only in ocl
      1 /*
      2  * cl_kernel.h - CL kernel
      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 (at) intel.com>
     19  */
     20 
     21 #ifndef XCAM_CL_KERNEL_H
     22 #define XCAM_CL_KERNEL_H
     23 
     24 #include <xcam_std.h>
     25 #include <xcam_mutex.h>
     26 #include <ocl/cl_event.h>
     27 #include <ocl/cl_argument.h>
     28 
     29 #include <CL/cl.h>
     30 #include <string>
     31 #include <unistd.h>
     32 
     33 #define XCAM_CL_KERNEL_FUNC_SOURCE_BEGIN(func)  \
     34     const char func##_body []=
     35 //const char *func##_name = #func;
     36 
     37 #define XCAM_CL_KERNEL_FUNC_BINARY_BEGIN(func)  \
     38     const uint8_t func##_body[] =
     39 
     40 #define XCAM_CL_KERNEL_FUNC_END
     41 
     42 XCAM_BEGIN_DECLARE
     43 
     44 typedef struct _XCamKernelInfo {
     45     const char   *kernel_name;
     46     const char   *kernel_body;
     47     size_t        kernel_body_len;
     48 } XCamKernelInfo;
     49 
     50 XCAM_END_DECLARE
     51 
     52 namespace XCam {
     53 
     54 class CLContext;
     55 class CLKernel;
     56 
     57 /*
     58  * Example to create a kernel
     59  * XCAM_CL_KERNEL_FUNC_SOURCE_BEGIN(kernel_demo)
     60  * #include "kernel_demo.clx"
     61  * XCAM_CL_KERNEL_FUNC_END
     62  * SmartPtr<CLKernel> kernel = new CLKernel (context, kernel_demo);
     63  * kernel->load_from_source (kernel_demo_body, strlen(kernel_demo_body));
     64  * XCAM_ASSERT (kernel->is_valid());
     65  */
     66 class CLKernel {
     67     friend class CLContext;
     68 public:
     69     explicit CLKernel (const SmartPtr<CLContext> &context, const char *name);
     70     virtual ~CLKernel ();
     71 
     72     XCamReturn build_kernel (const XCamKernelInfo& info, const char* options = NULL);
     73 
     74     cl_kernel get_kernel_id () {
     75         return _kernel_id;
     76     }
     77     bool is_valid () const {
     78         return _kernel_id != NULL;
     79     }
     80     const char *get_kernel_name () const {
     81         return _name;
     82     }
     83     SmartPtr<CLContext> &get_context () {
     84         return  _context;
     85     }
     86 
     87     XCamReturn set_arguments (const CLArgList &args, const CLWorkSize &work_size);
     88     const CLWorkSize &get_work_size () const {
     89         return _work_size;
     90     }
     91 
     92     bool is_arguments_set () const {
     93         return !_arg_list.empty ();
     94     }
     95     const CLArgList &get_args () const {
     96         return _arg_list;
     97     }
     98 
     99     XCamReturn execute (
    100         const SmartPtr<CLKernel> self,
    101         bool block = false,
    102         CLEventList &events = CLEvent::EmptyList,
    103         SmartPtr<CLEvent> &event_out = CLEvent::NullEvent);
    104 
    105     XCamReturn load_from_source (
    106         const char *source, size_t length = 0,
    107         uint8_t **gen_binary = NULL,
    108         size_t *binary_size = NULL,
    109         const char *build_option = NULL);
    110 
    111     XCamReturn load_from_binary (const uint8_t *binary, size_t length);
    112 
    113 private:
    114     XCamReturn set_argument (uint32_t arg_i, void *arg_addr, uint32_t arg_size);
    115     XCamReturn set_work_size (const CLWorkSize &work_size);
    116     void set_default_work_size ();
    117     void destroy ();
    118     XCamReturn clone (SmartPtr<CLKernel> kernel);
    119 
    120     static void event_notify (cl_event event, cl_int status, void* data);
    121     XCAM_DEAD_COPY (CLKernel);
    122 
    123 private:
    124     typedef std::map<std::string, SmartPtr<CLKernel> > KernelMap;
    125 
    126     static KernelMap      _kernel_map;
    127     static Mutex          _kernel_map_mutex;
    128     static const char    *_kernel_cache_path;
    129 
    130 private:
    131     char                 *_name;
    132     cl_kernel             _kernel_id;
    133     SmartPtr<CLContext>   _context;
    134     SmartPtr<CLKernel>    _parent_kernel;
    135     CLArgList             _arg_list;
    136     CLWorkSize            _work_size;
    137 
    138     XCAM_OBJ_PROFILING_DEFINES;
    139 };
    140 
    141 };
    142 
    143 #endif //XCAM_CL_KERNEL_H
    144