Home | History | Annotate | Download | only in intel
      1 /*
      2  * cl_intel_context.cpp - CL intel 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 (at) intel.com>
     19  */
     20 
     21 #include "cl_intel_context.h"
     22 #include "cl_device.h"
     23 #include "cl_va_memory.h"
     24 
     25 #define OCL_EXT_NAME_CREATE_BUFFER_FROM_LIBVA_INTEL "clCreateBufferFromLibvaIntel"
     26 #define OCL_EXT_NAME_CREATE_BUFFER_FROM_FD_INTEL    "clCreateBufferFromFdINTEL"
     27 #define OCL_EXT_NAME_CREATE_IMAGE_FROM_LIBVA_INTEL  "clCreateImageFromLibvaIntel"
     28 #define OCL_EXT_NAME_CREATE_IMAGE_FROM_FD_INTEL     "clCreateImageFromFdINTEL"
     29 #define OCL_EXT_NAME_GET_MEM_OBJECT_FD_INTEL        "clGetMemObjectFdIntel"
     30 
     31 namespace XCam {
     32 
     33 CLIntelContext::CLIntelContext (SmartPtr<CLDevice> &device)
     34     : CLContext (device)
     35 {
     36 }
     37 
     38 cl_mem
     39 CLIntelContext::create_va_buffer (uint32_t bo_name)
     40 {
     41     cl_mem mem_id = NULL;
     42     cl_int errcode = CL_SUCCESS;
     43     if (!is_valid())
     44         return NULL;
     45 
     46     clCreateBufferFromLibvaIntel_fn oclCreateBufferFromLibvaIntel =
     47         (clCreateBufferFromLibvaIntel_fn) _device->get_extension_function (OCL_EXT_NAME_CREATE_BUFFER_FROM_LIBVA_INTEL);
     48     XCAM_FAIL_RETURN(ERROR, oclCreateBufferFromLibvaIntel, NULL, "create buffer failed since extension was not found");
     49 
     50     mem_id = oclCreateBufferFromLibvaIntel (_context_id, bo_name, &errcode);
     51     XCAM_FAIL_RETURN(
     52         WARNING,
     53         errcode == CL_SUCCESS,
     54         NULL,
     55         "create cl memory from va image failed");
     56     return mem_id;
     57 }
     58 
     59 cl_mem
     60 CLIntelContext::import_dma_buffer (const cl_import_buffer_info_intel &import_info)
     61 {
     62     cl_mem mem_id = NULL;
     63     cl_int errcode = CL_SUCCESS;
     64     if (!is_valid())
     65         return NULL;
     66 
     67     clCreateBufferFromFdINTEL_fn oclCreateBufferFromFdINTEL =
     68         (clCreateBufferFromFdINTEL_fn) _device->get_extension_function (OCL_EXT_NAME_CREATE_BUFFER_FROM_FD_INTEL);
     69     XCAM_FAIL_RETURN(ERROR, oclCreateBufferFromFdINTEL, NULL, "import buffer failed since extension was not found");
     70 
     71     mem_id = oclCreateBufferFromFdINTEL (_context_id, &import_info, &errcode);
     72     XCAM_FAIL_RETURN(
     73         WARNING,
     74         errcode == CL_SUCCESS,
     75         NULL,
     76         "import cl memory from dma buffer failed");
     77 
     78     return mem_id;
     79 }
     80 
     81 cl_mem
     82 CLIntelContext::create_va_image (const cl_libva_image &image_info)
     83 {
     84     cl_mem mem_id = NULL;
     85     cl_int errcode = CL_SUCCESS;
     86     if (!is_valid())
     87         return NULL;
     88 
     89     clCreateImageFromLibvaIntel_fn oclCreateImageFromLibvaIntel =
     90         (clCreateImageFromLibvaIntel_fn) _device->get_extension_function (OCL_EXT_NAME_CREATE_IMAGE_FROM_LIBVA_INTEL);
     91     XCAM_FAIL_RETURN(ERROR, oclCreateImageFromLibvaIntel, NULL, "create image failed since extension was not found");
     92 
     93     mem_id = oclCreateImageFromLibvaIntel (_context_id, &image_info, &errcode);
     94     XCAM_FAIL_RETURN(
     95         WARNING,
     96         errcode == CL_SUCCESS,
     97         NULL,
     98         "create cl memory from va image failed");
     99     return mem_id;
    100 }
    101 
    102 cl_mem
    103 CLIntelContext::import_dma_image (const cl_import_image_info_intel &import_info)
    104 {
    105     cl_mem mem_id = NULL;
    106     cl_int errcode = CL_SUCCESS;
    107     if (!is_valid())
    108         return NULL;
    109 
    110     clCreateImageFromFdINTEL_fn oclCreateImageFromFdINTEL =
    111         (clCreateImageFromFdINTEL_fn) _device->get_extension_function (OCL_EXT_NAME_CREATE_IMAGE_FROM_FD_INTEL);
    112     XCAM_FAIL_RETURN(ERROR, oclCreateImageFromFdINTEL, NULL, "create image failed since extension was not found");
    113 
    114     mem_id = oclCreateImageFromFdINTEL (_context_id, &import_info, &errcode);
    115     XCAM_FAIL_RETURN(
    116         WARNING,
    117         errcode == CL_SUCCESS,
    118         NULL,
    119         "import cl memory from dma image failed, errcode:%d", errcode);
    120 
    121     return mem_id;
    122 }
    123 
    124 int32_t
    125 CLIntelContext::export_mem_fd (cl_mem mem_id)
    126 {
    127     cl_int errcode = CL_SUCCESS;
    128     int32_t fd = -1;
    129 
    130     clGetMemObjectFdIntel_fn oclGetMemObjectFdIntel =
    131         (clGetMemObjectFdIntel_fn) _device->get_extension_function (OCL_EXT_NAME_GET_MEM_OBJECT_FD_INTEL);
    132     XCAM_FAIL_RETURN(ERROR, oclGetMemObjectFdIntel, -1, "export fd failed since extension was not found");
    133 
    134     XCAM_ASSERT (mem_id);
    135     errcode = oclGetMemObjectFdIntel (_context_id, mem_id, &fd);
    136     XCAM_FAIL_RETURN (
    137         WARNING,
    138         errcode == CL_SUCCESS,
    139         -1,
    140         "export cl mem fd failed");
    141     return fd;
    142 }
    143 
    144 };
    145