Home | History | Annotate | Download | only in ocl
      1 /*
      2  * cl_demo_handler.cpp - CL demo handler
      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 #include "cl_utils.h"
     21 #include "cl_demo_handler.h"
     22 #include "cl_device.h"
     23 #include "cl_kernel.h"
     24 
     25 namespace XCam {
     26 
     27 static const XCamKernelInfo kernel_demo_info = {
     28     "kernel_demo",
     29 #include "kernel_demo.clx"
     30     , 0,
     31 };
     32 
     33 CLDemoImageHandler::CLDemoImageHandler (const SmartPtr<CLContext> &context)
     34     : CLImageHandler (context, "cl_demo_handler")
     35 {
     36 }
     37 
     38 XCamReturn
     39 CLDemoImageHandler::prepare_output_buf (SmartPtr<VideoBuffer> &input, SmartPtr<VideoBuffer> &output)
     40 {
     41     const VideoBufferInfo &info = input->get_video_info ();
     42     XCAM_FAIL_RETURN (
     43         ERROR,
     44         info.format == V4L2_PIX_FMT_RGBA32,
     45         XCAM_RETURN_ERROR_PARAM,
     46         "CLDemoImageHandler support only RGBA format");
     47 
     48     return CLImageHandler::prepare_output_buf (input, output);
     49 }
     50 
     51 XCamReturn
     52 CLDemoImageHandler::prepare_parameters (SmartPtr<VideoBuffer> &input, SmartPtr<VideoBuffer> &output)
     53 {
     54     SmartPtr<CLContext> context = CLDevice::instance ()->get_context ();
     55     const VideoBufferInfo &info = input->get_video_info ();
     56     CLArgList args;
     57     CLWorkSize work_size;
     58 
     59     CLImageDesc desc;
     60     desc.format.image_channel_order = CL_RGBA;
     61     desc.format.image_channel_data_type = CL_UNORM_INT8;
     62     desc.width = info.aligned_width;
     63     desc.height = info.height;
     64     desc.row_pitch = info.strides[0];
     65     desc.array_size = 0;
     66     desc.slice_pitch = 0;
     67 
     68     SmartPtr<CLImage> input_image = convert_to_climage (context, input, desc);
     69     SmartPtr<CLImage> output_image = convert_to_climage (context, output, desc);
     70 
     71     XCAM_ASSERT (input_image.ptr () && output_image.ptr ());
     72     XCAM_ASSERT (input_image->is_valid () && output_image->is_valid ());
     73     args.push_back (new CLMemArgument (input_image));
     74     args.push_back (new CLMemArgument (output_image));
     75 
     76     work_size.dim = XCAM_DEFAULT_IMAGE_DIM;
     77     work_size.global[0] = desc.width;
     78     work_size.global[1] = desc.height;
     79     work_size.local[0] = 8;
     80     work_size.local[1] = 4;
     81 
     82     _copy_kernel->set_arguments (args, work_size);
     83 
     84     return XCAM_RETURN_NO_ERROR;
     85 }
     86 
     87 SmartPtr<CLImageHandler>
     88 create_cl_demo_image_handler (const SmartPtr<CLContext> &context)
     89 {
     90     SmartPtr<CLDemoImageHandler> demo_handler;
     91     SmartPtr<CLImageKernel> demo_kernel;
     92 
     93     demo_kernel = new CLImageKernel (context);
     94     XCAM_ASSERT (demo_kernel.ptr ());
     95     XCAM_FAIL_RETURN (
     96         ERROR, demo_kernel->build_kernel (kernel_demo_info, NULL) == XCAM_RETURN_NO_ERROR,
     97         NULL, "build demo kernel failed");
     98 
     99     XCAM_ASSERT (demo_kernel->is_valid ());
    100     demo_handler = new CLDemoImageHandler (context);
    101     XCAM_ASSERT (demo_handler.ptr ());
    102     demo_handler->set_copy_kernel (demo_kernel);
    103 
    104     return demo_handler;
    105 }
    106 
    107 SmartPtr<CLImageHandler>
    108 create_cl_binary_demo_image_handler (const SmartPtr<CLContext> &context, const uint8_t *binary, size_t size)
    109 {
    110     SmartPtr<CLDemoImageHandler> demo_handler;
    111     SmartPtr<CLImageKernel> demo_kernel;
    112     XCamReturn ret = XCAM_RETURN_NO_ERROR;
    113 
    114     demo_kernel = new CLImageKernel (context, "kernel_demo");
    115     {
    116 #if 0
    117         XCAM_CL_KERNEL_FUNC_BINARY_BEGIN(kernel_demo)
    118 #include "kernel_demo.clx.bin"
    119         XCAM_CL_KERNEL_FUNC_END;
    120         ret = demo_kernel->load_from_binary (kernel_demo_body, sizeof (kernel_demo_body));
    121 #endif
    122         ret = demo_kernel->load_from_binary (binary, size);
    123         XCAM_FAIL_RETURN (
    124             WARNING,
    125             ret == XCAM_RETURN_NO_ERROR,
    126             NULL,
    127             "CL image handler(%s) load binary failed", demo_kernel->get_kernel_name());
    128     }
    129     XCAM_ASSERT (demo_kernel->is_valid ());
    130     demo_handler = new CLDemoImageHandler (context);
    131     demo_handler->set_copy_kernel (demo_kernel);
    132 
    133     return demo_handler;
    134 }
    135 
    136 };
    137