1 /*
2 * cl_video_buffer.cpp - cl video buffer
3 *
4 * Copyright (c) 2017 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: Yinhang Liu <yinhangx.liu@intel.com>
19 * Author: Wind Yuan <feng.yuan@intel.com>
20 */
21
22 #include "ocl/cl_memory.h"
23 #include "ocl/cl_device.h"
24 #include "ocl/cl_video_buffer.h"
25
26 namespace XCam {
27
CLVideoBufferData(SmartPtr<CLBuffer> & body)28 CLVideoBufferData::CLVideoBufferData (SmartPtr<CLBuffer> &body)
29 : _buf_ptr (NULL)
30 , _buf (body)
31 {
32 XCAM_ASSERT (body.ptr ());
33 }
34
~CLVideoBufferData()35 CLVideoBufferData::~CLVideoBufferData ()
36 {
37 unmap ();
38 _buf.release ();
39 }
40
41 cl_mem &
get_mem_id()42 CLVideoBufferData::get_mem_id () {
43 return _buf->get_mem_id ();
44 }
45
46 uint8_t *
map()47 CLVideoBufferData::map ()
48 {
49 if (_buf_ptr)
50 return _buf_ptr;
51
52 uint32_t size = _buf->get_buf_size ();
53 XCamReturn ret = _buf->enqueue_map ((void*&) _buf_ptr, 0, size);
54 XCAM_FAIL_RETURN (
55 ERROR,
56 ret == XCAM_RETURN_NO_ERROR,
57 NULL,
58 "CLVideoBufferData map data failed");
59
60 return _buf_ptr;
61 }
62
63 bool
unmap()64 CLVideoBufferData::unmap ()
65 {
66 if (!_buf_ptr)
67 return true;
68
69 XCamReturn ret = _buf->enqueue_unmap ((void*&) _buf_ptr);
70 XCAM_FAIL_RETURN (
71 ERROR,
72 ret == XCAM_RETURN_NO_ERROR,
73 NULL,
74 "CLVideoBufferData unmap data failed");
75
76 _buf_ptr = NULL;
77 return true;
78 }
79
CLVideoBuffer(const SmartPtr<CLContext> & context,const VideoBufferInfo & info,const SmartPtr<CLVideoBufferData> & data)80 CLVideoBuffer::CLVideoBuffer (
81 const SmartPtr<CLContext> &context, const VideoBufferInfo &info, const SmartPtr<CLVideoBufferData> &data)
82 : BufferProxy (info, data)
83 , CLBuffer (context)
84 {
85 XCAM_ASSERT (data.ptr ());
86
87 SmartPtr<CLBuffer> cl_buf = data->get_cl_buffer ();
88 XCAM_ASSERT (cl_buf.ptr ());
89 set_mem_id (cl_buf->get_mem_id (), false);
90 set_buf_size (cl_buf->get_buf_size ());
91 }
92
93 SmartPtr<CLBuffer>
get_cl_buffer()94 CLVideoBuffer::get_cl_buffer ()
95 {
96 SmartPtr<BufferData> data = get_buffer_data ();
97 SmartPtr<CLVideoBufferData> cl_data = data.dynamic_cast_ptr<CLVideoBufferData> ();
98 XCAM_FAIL_RETURN(
99 WARNING,
100 cl_data.ptr(),
101 NULL,
102 "CLVideoBuffer get buffer data failed with NULL");
103
104 return cl_data->get_cl_buffer ();
105 }
106
107 SmartPtr<X3aStats>
find_3a_stats()108 CLVideoBuffer::find_3a_stats ()
109 {
110 return find_typed_attach<X3aStats> ();
111 }
112
113 bool
fixate_video_info(VideoBufferInfo & info)114 CLVideoBufferPool::fixate_video_info (VideoBufferInfo &info)
115 {
116 if (info.format != V4L2_PIX_FMT_NV12)
117 return true;
118
119 VideoBufferInfo out_info;
120 out_info.init (info.format, info.width, info.height, info.aligned_width, info.aligned_height);
121
122 return true;
123 }
124
125 SmartPtr<BufferData>
allocate_data(const VideoBufferInfo & buffer_info)126 CLVideoBufferPool::allocate_data (const VideoBufferInfo &buffer_info)
127 {
128 SmartPtr<CLContext> context = CLDevice::instance ()->get_context ();
129
130 SmartPtr<CLBuffer> buf = new CLBuffer (context, buffer_info.size);
131 XCAM_ASSERT (buf.ptr ());
132
133 return new CLVideoBufferData (buf);
134 }
135
136 SmartPtr<BufferProxy>
create_buffer_from_data(SmartPtr<BufferData> & data)137 CLVideoBufferPool::create_buffer_from_data (SmartPtr<BufferData> &data)
138 {
139 SmartPtr<CLContext> context = CLDevice::instance ()->get_context ();
140 const VideoBufferInfo & info = get_video_info ();
141 SmartPtr<CLVideoBufferData> cl_data = data.dynamic_cast_ptr<CLVideoBufferData> ();
142 XCAM_ASSERT (cl_data.ptr ());
143
144 SmartPtr<CLVideoBuffer> buf = new CLVideoBuffer (context, info, cl_data);
145 XCAM_ASSERT (buf.ptr ());
146
147 return buf;
148 }
149
150 };
151