blob: a761af419ad3f2eb24a91a5d931d8ba39f0fe689 [file] [log] [blame]
Wind Yuan4bbe4ec2015-04-02 18:00:35 +08001/*
2 * buffer_pool.cpp - buffer pool
3 *
4 * Copyright (c) 2014-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#include "xcam_utils.h"
22#include "buffer_pool.h"
23
24namespace XCam {
25
26BufferProxy::BufferProxy (const VideoBufferInfo &info, const SmartPtr<BufferData> &data)
27 : VideoBuffer (info)
28 , _data (data)
29{
30 XCAM_ASSERT (data.ptr ());
31}
32
33BufferProxy::BufferProxy (const SmartPtr<BufferData> &data)
34 : _data (data)
35{
36 XCAM_ASSERT (data.ptr ());
37}
38
39BufferProxy::~BufferProxy ()
40{
41 if (_pool.ptr ()) {
42 _pool->release (_data);
43 }
44 _data.release ();
45 _parent.release ();
46}
47
48uint8_t *
49BufferProxy::map ()
50{
51 XCAM_ASSERT (_data.ptr ());
52 return _data->map ();
53}
54
55bool
56BufferProxy::unmap ()
57{
58 XCAM_ASSERT (_data.ptr ());
59 return _data->unmap ();
60}
61
Jia Meng10a616d2015-04-23 20:57:09 +000062int
63BufferProxy::get_fd ()
64{
65 XCAM_ASSERT (_data.ptr ());
66 return _data->get_fd ();
67}
68
Wind Yuan4bbe4ec2015-04-02 18:00:35 +080069BufferPool::BufferPool ()
70 : _allocated_num (0)
71 , _max_count (0)
72 , _started (false)
73{
74}
75
76BufferPool::~BufferPool ()
77{
78}
79
80bool
81BufferPool::set_video_info (const VideoBufferInfo &info)
82{
83 VideoBufferInfo new_info = info;
84 SmartLock lock (_mutex);
85
86 XCAM_FAIL_RETURN (
87 ERROR,
88 fixate_video_info (new_info),
89 false,
90 "BufferPool fixate video info failed");
91 _buffer_info = new_info;
92 return true;
93}
94
95bool
96BufferPool::reserve (uint32_t max_count)
97{
98 uint32_t i = 0;
99
Wind Yuan4bbe4ec2015-04-02 18:00:35 +0800100 XCAM_ASSERT (max_count);
101
102 SmartLock lock (_mutex);
103
Wind Yuanb90a89c2015-04-08 18:38:53 +0800104 for (i = _allocated_num; i < max_count; ++i) {
Wind Yuan4bbe4ec2015-04-02 18:00:35 +0800105 SmartPtr<BufferData> new_data = allocate_data (_buffer_info);
106 if (!new_data.ptr ())
107 break;
108 _buf_list.push (new_data);
109 }
110
111 XCAM_FAIL_RETURN (
112 ERROR,
113 i > 0,
114 false,
115 "BufferPool reserve failed with none buffer data allocated");
116
117 if (i != max_count) {
118 XCAM_LOG_WARNING ("BufferPool expect to reserve %d data but only reserved %d", max_count, i);
119 }
120 _max_count = i;
Wind Yuanb90a89c2015-04-08 18:38:53 +0800121 _allocated_num = _max_count;
Wind Yuan4bbe4ec2015-04-02 18:00:35 +0800122 _started = true;
123
124 return true;
125}
126
Wind Yuanb90a89c2015-04-08 18:38:53 +0800127bool
128BufferPool::add_data_unsafe (SmartPtr<BufferData> data)
129{
130 if (!data.ptr ())
131 return false;
132
133 _buf_list.push (data);
134 ++_allocated_num;
135
136 XCAM_ASSERT (_allocated_num <= _max_count || !_max_count);
137 return true;
138}
139
Wind Yuan4bbe4ec2015-04-02 18:00:35 +0800140SmartPtr<BufferProxy>
141BufferPool::get_buffer (const SmartPtr<BufferPool> &self)
142{
143 SmartPtr<BufferProxy> ret_buf;
144 SmartPtr<BufferData> data;
145
146 {
147 SmartLock lock (_mutex);
148 if (!_started)
149 return NULL;
150 }
151
152 XCAM_ASSERT (self.ptr () == this);
153 XCAM_FAIL_RETURN(
154 WARNING,
155 self.ptr () == this,
156 NULL,
157 "BufferPool get_buffer failed since parameter<self> not this");
158
159 data = _buf_list.pop ();
160 if (!data.ptr ()) {
161 XCAM_LOG_DEBUG ("BufferPool failed to get buffer");
162 return NULL;
163 }
164 ret_buf = create_buffer_from_data (data);
165 ret_buf->set_buf_pool (self);
166
167 return ret_buf;
168}
169
170void
171BufferPool::stop ()
172{
173 {
174 SmartLock lock (_mutex);
175 _started = false;
176 }
177 _buf_list.wakeup ();
178}
179
180void
181BufferPool::release (SmartPtr<BufferData> &data)
182{
183 {
184 SmartLock lock (_mutex);
185 if (!_started)
186 return;
187 }
188 _buf_list.push (data);
189}
190
191bool
192BufferPool::fixate_video_info (VideoBufferInfo &info)
193{
194 XCAM_UNUSED (info);
195 return true;
196}
197
198SmartPtr<BufferProxy>
199BufferPool::create_buffer_from_data (SmartPtr<BufferData> &data)
200{
201 const VideoBufferInfo &info = get_video_info ();
202
203 XCAM_ASSERT (data.ptr ());
204 return new BufferProxy (info, data);
205}
206
207};