1 /*
2  * cl_image_handler.h - CL image 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@intel.com>
19  */
20 
21 #ifndef XCAM_CL_IMAGE_HANDLER_H
22 #define XCAM_CL_IMAGE_HANDLER_H
23 
24 #include <xcam_std.h>
25 #include <swapped_buffer.h>
26 #include <x3a_result.h>
27 #include <ocl/cl_kernel.h>
28 #include <ocl/cl_argument.h>
29 #include <ocl/cl_memory.h>
30 #include <ocl/cl_video_buffer.h>
31 
32 namespace XCam {
33 
34 class CLImageHandler;
35 
36 class CLImageKernel
37     : public CLKernel
38 {
39     friend class CLImageHandler;
40 
41 public:
42     explicit CLImageKernel (const SmartPtr<CLContext> &context, const char *name = NULL, bool enable = true);
43     virtual ~CLImageKernel ();
44 
set_enable(bool enable)45     void set_enable (bool enable) {
46         _enable = enable;
47     }
48 
is_enabled()49     bool is_enabled () const {
50         return _enable;
51     }
pre_stop()52     virtual void pre_stop () {}
53 
54 protected:
55     XCamReturn pre_execute ();
56     virtual XCamReturn prepare_arguments (
57         CLArgList &args, CLWorkSize &work_size);
58 
59 private:
60     XCAM_DEAD_COPY (CLImageKernel);
61 
62 private:
63     bool                _enable;
64 };
65 
66 class CLMultiImageHandler;
67 class CLImageHandler
68 {
69     friend class CLMultiImageHandler;
70 
71 public:
72     typedef std::list<SmartPtr<CLImageKernel>> KernelList;
73     enum BufferPoolType {
74         CLVideoPoolType = 0x0000,
75         CLBoPoolType = 0x0001,
76         DrmBoPoolType = 0x0002
77     };
78 
79 public:
80     explicit CLImageHandler (const SmartPtr<CLContext> &context, const char *name);
81     virtual ~CLImageHandler ();
get_name()82     const char *get_name () const {
83         return _name;
84     }
get_context()85     SmartPtr<CLContext> &get_context () {
86         return  _context;
87     }
88 
89     void set_3a_result (SmartPtr<X3aResult> &result);
90     SmartPtr<X3aResult> get_3a_result (XCam3aResultType type);
91 
get_result_timestamp()92     int64_t get_result_timestamp () const {
93         return _result_timestamp;
94     };
95 
set_pool_type(BufferPoolType type)96     void set_pool_type (BufferPoolType type) {
97         _buf_pool_type = type;
98     }
set_pool_size(uint32_t size)99     void set_pool_size (uint32_t size) {
100         XCAM_ASSERT (size);
101         _buf_pool_size = size;
102     }
disable_buf_pool(bool flag)103     void disable_buf_pool (bool flag) {
104         _disable_buf_pool = flag;
105     }
106 
is_buf_pool_disabled()107     bool is_buf_pool_disabled () const {
108         return _disable_buf_pool;
109     }
110 
111     bool enable_buf_pool_swap_flags (
112         uint32_t flags,
113         uint32_t init_order = (uint32_t)(SwappedBuffer::OrderY0Y1));
114 
115     bool add_kernel (const SmartPtr<CLImageKernel> &kernel);
116     bool enable_handler (bool enable);
117     bool is_handler_enabled () const;
118 
119     virtual bool is_ready ();
120     XCamReturn execute (SmartPtr<VideoBuffer> &input, SmartPtr<VideoBuffer> &output);
121     virtual void emit_stop ();
122 
123     SmartPtr<VideoBuffer> &get_input_buf ();
124     SmartPtr<VideoBuffer> &get_output_buf ();
125 
126 private:
127     virtual XCamReturn prepare_buffer_pool_video_info (
128         const VideoBufferInfo &input,
129         VideoBufferInfo &output);
130 
131     // if derive prepare_output_buf, then prepare_buffer_pool_video_info is not involked
132     virtual XCamReturn prepare_parameters (SmartPtr<VideoBuffer> &input, SmartPtr<VideoBuffer> &output);
133     virtual XCamReturn execute_done (SmartPtr<VideoBuffer> &output);
134 
135 protected:
136     virtual XCamReturn prepare_output_buf (SmartPtr<VideoBuffer> &input, SmartPtr<VideoBuffer> &output);
137 
138     //only for multi-handler
139     virtual XCamReturn execute_kernels ();
140 
141     XCamReturn ensure_parameters (SmartPtr<VideoBuffer> &input, SmartPtr<VideoBuffer> &output);
142     XCamReturn execute_kernel (SmartPtr<CLImageKernel> &kernel);
143     XCamReturn create_buffer_pool (const VideoBufferInfo &video_info);
get_buffer_pool()144     SmartPtr<BufferPool> &get_buffer_pool () {
145         return _buf_pool;
146     }
147     void reset_buf_cache (const SmartPtr<VideoBuffer>& input, const SmartPtr<VideoBuffer>& output);
148 
149     bool append_kernels (SmartPtr<CLImageHandler> handler);
150 
151 private:
152     XCAM_DEAD_COPY (CLImageHandler);
153 
154 private:
155     char                      *_name;
156     bool                       _enable;
157     KernelList                 _kernels;
158     SmartPtr<CLContext>        _context;
159     SmartPtr<BufferPool>       _buf_pool;
160     BufferPoolType             _buf_pool_type;
161     bool                       _disable_buf_pool;
162     uint32_t                   _buf_pool_size;
163     uint32_t                   _buf_swap_flags;
164     uint32_t                   _buf_swap_init_order;
165     X3aResultList              _3a_results;
166     int64_t                    _result_timestamp;
167 
168     SmartPtr<VideoBuffer>      _input_buf_cache;
169     SmartPtr<VideoBuffer>      _output_buf_cache;
170 
171     XCAM_OBJ_PROFILING_DEFINES;
172 };
173 
174 // never allocate buffer, only swap output from input
175 class CLCloneImageHandler
176     : public CLImageHandler
177 {
178 public:
179     explicit CLCloneImageHandler (const SmartPtr<CLContext> &context, const char *name);
set_clone_flags(uint32_t flags)180     void set_clone_flags (uint32_t flags) {
181         _clone_flags = flags;
182     }
get_clone_flags()183     uint32_t get_clone_flags () const {
184         return _clone_flags;
185     }
186 
187 protected:
188     //derived from CLImageHandler
189     virtual XCamReturn prepare_output_buf (SmartPtr<VideoBuffer> &input, SmartPtr<VideoBuffer> &output);
190 
191 private:
192     XCAM_DEAD_COPY (CLCloneImageHandler);
193 
194     uint32_t                   _clone_flags;
195 };
196 
197 
198 };
199 
200 #endif // XCAM_CL_IMAGE_HANDLER_H
201