1 /*
2 * Copyright (c) 2019-2022 Rockchip Eletronics Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "shared_item_pool.h"
18
19 namespace RkCam {
20
21 template<typename T>
SharedItemPool(const char * name,uint32_t max_count)22 SharedItemPool<T>::SharedItemPool(const char* name, uint32_t max_count)
23 : BufferPool()
24 ,_name(name ? name : "default")
25 , _max_count(max_count)
26 {
27 if (_max_count > 0)
28 reserve (_max_count);
29 }
30
31 template<typename T>
~SharedItemPool()32 SharedItemPool<T>::~SharedItemPool()
33 {
34 }
35
36 template<typename T>
init(uint32_t max_count)37 int8_t SharedItemPool<T>::init(uint32_t max_count)
38 {
39 if (_max_count > 0)
40 return -1;
41 if (!reserve (max_count))
42 return -1;
43
44 _max_count = get_free_buffer_size ();
45
46 return 0;
47 }
48
49 template<typename T>
50 SmartPtr<SharedItemProxy<T>>
get_item()51 SharedItemPool<T>::get_item()
52 {
53 SmartPtr<SharedItemProxy<T>> ret_buf;
54 SmartPtr<BufferData> data;
55
56 {
57 SmartLock lock (_mutex);
58 if (!_started)
59 return NULL;
60 }
61
62 data = _buf_list.pop ();
63 if (!data.ptr ()) {
64 XCAM_LOG_DEBUG ("BufferPool failed to get buffer");
65 return NULL;
66 }
67 LOG1_ANALYZER("Get item : %s remain count %d", typeid(T).name(), _buf_list.size());
68 SmartPtr<T> data_t = data.dynamic_cast_ptr<T>();
69 ret_buf = new SharedItemProxy<T> (data_t);;
70 ret_buf->set_buf_pool (SmartPtr<BufferPool>(this));
71
72 return ret_buf;
73 }
74
75 template<typename T>
allocate_data(const VideoBufferInfo & buffer_info)76 SmartPtr<BufferData> SharedItemPool<T>::allocate_data (const VideoBufferInfo &buffer_info)
77 {
78 LOG1_ANALYZER("New item : %s size %d", typeid(T).name(), sizeof(T));
79 return new T();
80 }
81
82 template<typename T>
create_buffer_from_data(SmartPtr<BufferData> & data)83 SmartPtr<BufferProxy> SharedItemPool<T>::create_buffer_from_data (SmartPtr<BufferData> &data)
84 {
85 XCAM_ASSERT (data.ptr ());
86 SmartPtr<T> data_t = data.dynamic_cast_ptr<T>();
87 return new SharedItemProxy<T> (data_t);
88 }
89
90 }
91