1/*
2 * cl_rgb_pipe_handler.cpp - CL rgb pipe 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: Shincy Tu <shincy.tu@intel.com>
19 * Author: Wei Zong <wei.zong@intel.com>
20 * Author: Wangfei <feix.w.wang@intel.com>
21 */
22
23#include "cl_utils.h"
24#include "base/xcam_3a_result.h"
25#include "cl_rgb_pipe_handler.h"
26
27namespace XCam {
28
29static const XCamKernelInfo kernel_rgb_pipe_info = {
30    "kernel_rgb_pipe",
31#include "kernel_rgb_pipe.clx"
32    , 0,
33};
34
35CLRgbPipeImageKernel::CLRgbPipeImageKernel (const SmartPtr<CLContext> &context)
36    : CLImageKernel (context, "kernel_rgb_pipe")
37{
38}
39
40CLRgbPipeImageHandler::CLRgbPipeImageHandler (const SmartPtr<CLContext> &context, const char *name)
41    : CLImageHandler (context, name)
42{
43    _tnr_config.thr_r = 0.064;
44    _tnr_config.thr_g = 0.045;
45    _tnr_config.thr_b = 0.073;
46}
47
48bool
49CLRgbPipeImageHandler::set_rgb_pipe_kernel(SmartPtr<CLRgbPipeImageKernel> &kernel)
50{
51    SmartPtr<CLImageKernel> image_kernel = kernel;
52    add_kernel (image_kernel);
53    _rgb_pipe_kernel = kernel;
54    return true;
55}
56
57bool
58CLRgbPipeImageHandler::set_tnr_config (const XCam3aResultTemporalNoiseReduction& config)
59{
60    if (!_rgb_pipe_kernel->is_valid ()) {
61        XCAM_LOG_ERROR ("set config error, invalid TNR kernel !");
62    }
63
64    _tnr_config.gain = (float)config.gain;
65    _tnr_config.thr_r  = (float)config.threshold[0];
66    _tnr_config.thr_g  = (float)config.threshold[1];
67    _tnr_config.thr_b  = (float)config.threshold[2];
68    XCAM_LOG_DEBUG ("set TNR RGB config: _gain(%f), _thr_r(%f), _thr_g(%f), _thr_b(%f)",
69                    _tnr_config.gain, _tnr_config.thr_r, _tnr_config.thr_g, _tnr_config.thr_b);
70
71    return true;
72}
73
74XCamReturn
75CLRgbPipeImageHandler::prepare_parameters (
76    SmartPtr<VideoBuffer> &input, SmartPtr<VideoBuffer> &output)
77{
78    SmartPtr<CLContext> context = get_context ();
79    const VideoBufferInfo & video_info = input->get_video_info ();
80    CLArgList args;
81    CLWorkSize work_size;
82
83    CLImageDesc desc;
84    desc.format.image_channel_order = CL_RGBA;
85    desc.format.image_channel_data_type = CL_UNORM_INT16;
86    desc.width = video_info.width;
87    desc.height = video_info.height;
88    desc.array_size = 0;
89    desc.row_pitch = video_info.strides[0];
90    desc.slice_pitch = 0;
91
92    XCAM_ASSERT (_rgb_pipe_kernel.ptr ());
93    SmartPtr<CLImage> image_in = convert_to_climage (context, input, desc);
94    SmartPtr<CLImage> image_out = convert_to_climage (context, output, desc);
95
96    if (_image_in_list.size () < 4) {
97        while (_image_in_list.size () < 4) {
98            _image_in_list.push_back (image_in);
99        }
100    } else {
101        _image_in_list.pop_front ();
102        _image_in_list.push_back (image_in);
103    }
104    XCAM_FAIL_RETURN (
105        WARNING,
106        image_in->is_valid () && image_out->is_valid (),
107        XCAM_RETURN_ERROR_MEM,
108        "cl image handler(%s) in/out memory not available", XCAM_STR(get_name ()));
109
110    //set args;
111    args.push_back (new CLMemArgument (image_out));
112    args.push_back (new CLArgumentT<CLRgbPipeTnrConfig> (_tnr_config));
113
114    for (CLImagePtrList::iterator it = _image_in_list.begin (); it != _image_in_list.end (); it++) {
115        args.push_back (new CLMemArgument (*it));
116    }
117
118    work_size.dim = XCAM_DEFAULT_IMAGE_DIM;
119    work_size.global[0] = XCAM_ALIGN_UP(video_info.width, 16);
120    work_size.global[1] = XCAM_ALIGN_UP(video_info.height, 16);
121    work_size.local[0] = 8;
122    work_size.local[1] = 4;
123
124    XCAM_ASSERT (_rgb_pipe_kernel.ptr ());
125    XCamReturn ret = _rgb_pipe_kernel->set_arguments (args, work_size);
126    XCAM_FAIL_RETURN (
127        WARNING, ret == XCAM_RETURN_NO_ERROR, ret,
128        "rgb pipe kernel set arguments failed.");
129
130    return XCAM_RETURN_NO_ERROR;
131}
132
133SmartPtr<CLImageHandler>
134create_cl_rgb_pipe_image_handler (const SmartPtr<CLContext> &context)
135{
136    SmartPtr<CLRgbPipeImageHandler> rgb_pipe_handler;
137    SmartPtr<CLRgbPipeImageKernel> rgb_pipe_kernel;
138
139    rgb_pipe_kernel = new CLRgbPipeImageKernel (context);
140    XCAM_ASSERT (rgb_pipe_kernel.ptr ());
141    XCAM_FAIL_RETURN (
142        ERROR, rgb_pipe_kernel->build_kernel (kernel_rgb_pipe_info, NULL) == XCAM_RETURN_NO_ERROR, NULL,
143        "build rgb-pipe kernel(%s) failed", kernel_rgb_pipe_info.kernel_name);
144
145    XCAM_ASSERT (rgb_pipe_kernel->is_valid ());
146    rgb_pipe_handler = new CLRgbPipeImageHandler (context, "cl_handler_rgb_pipe");
147    rgb_pipe_handler->set_rgb_pipe_kernel  (rgb_pipe_kernel);
148
149    return rgb_pipe_handler;
150}
151
152};
153