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