1 // Copyright (c) 2020 Intel Corporation
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #include "ehw_resources_pool.h"
22 
23 namespace MfxEncodeHW
24 {
25 
ResPool(VideoCORE & core)26 ResPool::ResPool(VideoCORE& core)
27     : m_core(core)
28 {
29 }
30 
~ResPool()31 ResPool::~ResPool()
32 {
33     Free();
34 }
35 
Acquire()36 ResPool::Resource ResPool::Acquire()
37 {
38     Resource res;
39     res.Idx = mfxU8(std::find(m_locked.begin(), m_locked.end(), 0u) - m_locked.begin());
40 
41     if (res.Idx >= GetResponse().NumFrameActual)
42     {
43         res.Idx = IDX_INVALID;
44         return res;
45     }
46 
47     Lock(res.Idx);
48     ClearFlag(res.Idx);
49 
50     res.Mid = GetResponse().mids[res.Idx];
51 
52     return res;
53 }
54 
Free()55 void ResPool::Free()
56 {
57     if (m_response.mids)
58     {
59         m_response.NumFrameActual = m_numFrameActual;
60 
61         m_core.FreeFrames(&m_response);
62 
63         m_response.mids = 0;
64     }
65 }
66 
Alloc(const mfxFrameAllocRequest & request,bool isCopyRequired)67 mfxStatus ResPool::Alloc(
68     const mfxFrameAllocRequest& request
69     , bool isCopyRequired)
70 {
71     auto req = request;
72     req.NumFrameSuggested = req.NumFrameMin;
73 
74     mfxStatus sts = m_core.AllocFrames(&req, &m_response, isCopyRequired);
75     MFX_CHECK_STS(sts);
76 
77     MFX_CHECK(m_response.NumFrameActual >= req.NumFrameMin, MFX_ERR_MEMORY_ALLOC);
78 
79     m_locked.resize(req.NumFrameMin, 0);
80     std::fill(m_locked.begin(), m_locked.end(), 0);
81 
82     m_flag.resize(req.NumFrameMin, 0);
83     std::fill(m_flag.begin(), m_flag.end(), 0);
84 
85     m_info                    = req.Info;
86     m_numFrameActual          = m_response.NumFrameActual;
87     m_response.NumFrameActual = req.NumFrameMin;
88     m_bExternal               = false;
89     m_bOpaque                 = false;
90 
91     return MFX_ERR_NONE;
92 }
93 
AllocOpaque(const mfxFrameInfo & info,mfxU16 type,mfxFrameSurface1 ** surfaces,mfxU16 numSurface)94 mfxStatus ResPool::AllocOpaque(
95     const mfxFrameInfo & info
96     , mfxU16 type
97     , mfxFrameSurface1 **surfaces
98     , mfxU16 numSurface)
99 {
100     mfxFrameAllocRequest req = {};
101 
102     req.Info = info;
103     req.NumFrameMin = req.NumFrameSuggested = numSurface;
104     req.Type = type;
105 
106     mfxStatus sts = m_core.AllocFrames(&req, &m_response, surfaces, numSurface);
107     MFX_CHECK_STS(sts);
108     MFX_CHECK(m_response.NumFrameActual >= numSurface, MFX_ERR_MEMORY_ALLOC);
109 
110     m_info                    = info;
111     m_numFrameActual          = m_response.NumFrameActual;
112     m_response.NumFrameActual = req.NumFrameMin;
113     m_bOpaque                 = true;
114 
115     return sts;
116 }
117 
Lock(mfxU32 idx)118 mfxU32 ResPool::Lock(mfxU32 idx)
119 {
120     if (idx >= m_locked.size())
121         return 0;
122     assert(m_locked[idx] < 0xffffffff);
123     return ++m_locked[idx];
124 }
125 
ClearFlag(mfxU32 idx)126 void ResPool::ClearFlag(mfxU32 idx)
127 {
128     assert(idx < m_flag.size());
129     if (idx < m_flag.size())
130     {
131         m_flag[idx] = 0;
132     }
133 }
134 
SetFlag(mfxU32 idx,mfxU32 flag)135 void ResPool::SetFlag(mfxU32 idx, mfxU32 flag)
136 {
137     assert(idx < m_flag.size());
138     if (idx < m_flag.size())
139     {
140         m_flag[idx] |= flag;
141     }
142 }
143 
GetFlag(mfxU32 idx)144 mfxU32 ResPool::GetFlag(mfxU32 idx)
145 {
146     assert(idx < m_flag.size());
147     if (idx < m_flag.size())
148     {
149         return m_flag[idx];
150     }
151     return 0;
152 }
153 
UnlockAll()154 void ResPool::UnlockAll()
155 {
156     std::fill(m_locked.begin(), m_locked.end(), 0);
157     std::fill(m_flag.begin(), m_flag.end(), 0);
158 }
159 
Unlock(mfxU32 idx)160 mfxU32 ResPool::Unlock(mfxU32 idx)
161 {
162     if (idx >= m_locked.size())
163         return mfxU32(-1);
164     assert(m_locked[idx] > 0);
165     return --m_locked[idx];
166 }
167 
Locked(mfxU32 idx) const168 mfxU32 ResPool::Locked(mfxU32 idx) const
169 {
170     return (idx < m_locked.size()) ? m_locked[idx] : 1;
171 }
172 
173 } //namespace MfxEHW