1 /*
2 * Copyright (c) 2018, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 //!
23 //! \file     encode_buffer_allocator.h
24 //! \brief    Defines the interface for buffer allocator
25 //! \details  The allocator manages the buffers with the same type and alloc parameter
26 //!
27 #ifndef __ENCODE_TRACKED_BUFFER_QUEUE_H__
28 #define __ENCODE_TRACKED_BUFFER_QUEUE_H__
29 
30 #include "media_class_trace.h"
31 #include "mos_defs.h"
32 #include "mos_defs_specific.h"
33 #include "mos_os.h"
34 #include <stdint.h>
35 #include <vector>
36 
37 namespace encode
38 {
39 
40 enum ResourceType
41 {
42     invalidResource,
43     bufferResource,
44     surfaceResource
45 };
46 
47 class EncodeAllocator;
48 class BufferQueue
49 {
50 public:
51     //!
52     //! \brief  Constructor
53     //! \param  [in] allocator
54     //!         Pointer to EncodeAllocator
55     //! \param  [in] param
56     //!         reference to MOS_ALLOC_GFXRES_PARAMS
57     //! \param  [in] maxCount
58     //!         the max buffer count allocated
59     //!
60     BufferQueue(EncodeAllocator *allocator, MOS_ALLOC_GFXRES_PARAMS &param, uint32_t maxCount);
61 
62     //!
63     //! \brief  Destructor
64     //!
65     ~BufferQueue();
66 
67     //!
68     //! \brief  Acquire resource from the allocator pool
69     //! \return Pointer of resource
70     //!         Pointer of resource if success, else nullptr if the pool is empty
71     //!
72     void *AcquireResource();
73 
74     //!
75     //! \brief  Release the resource and return to the pool
76     //! \param  [in] resource
77     //!         Pointer of resource
78     //! \return MOS_STATUS
79     //!         MOS_STATUS_SUCCESS if success, else fail reason
80     //!
81     MOS_STATUS ReleaseResource(void *resource);
82 
83     //!
84     //! \brief  Check whether it's safe to destory the allocator
85     //! \return bool
86     //!         true if all buffers are released, otherwise return false
87     //!
88     bool SafeToDestory();
89 
90     void SetResourceType(ResourceType resType);
91 
92 protected:
93     //!
94     //! \brief  Allocate resource
95     //! \return Pointer of resource
96     //!         Pointer of resource if success, nullptr if the allocated surfaces reach the max count
97     //!
98     void *AllocateResource();
99 
100     //!
101     //! \brief  Destroy resource
102     //! \param  [in] resource
103     //!         Pointer of resource
104     //! \return MOS_STATUS
105     //!         MOS_STATUS_SUCCESS if success, else fail reason
106     //!
107     MOS_STATUS DestoryResource(void *resource);
108 
109 protected:
110     uint32_t   m_maxCount   = 0;        //!< max buffer count
111     uint32_t   m_allocCount = 0;        //!< allocated buffer count
112     PMOS_MUTEX m_mutex      = nullptr;  //!< mutex
113 
114     EncodeAllocator *          m_allocator    = nullptr;    //!< encoder allocator
115     MOS_ALLOC_GFXRES_PARAMS    m_allocParam   = {};         //!< allocate parameter
116     std::vector<void *>        m_resourcePool = {};         //!< resource pool
117     std::vector<void *>        m_resources    = {};         //!< all allocated resources
118 
119     ResourceType m_resourceType = ResourceType::bufferResource;
120 
121 MEDIA_CLASS_DEFINE_END(encode__BufferQueue)
122 };
123 
124 }  // namespace encode
125 
126 #endif  // !__ENCODE_TRACKED_BUFFER_QUEUE_H__
127