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_slot.cpp
24 //! \brief    Defines the interface for buffer slot
25 //! \details  The slot manages the buffers with the same type
26 //!
27 #include "encode_tracked_buffer_slot.h"
28 #include <utility>
29 #include "encode_tracked_buffer_queue.h"
30 
31 namespace encode {
32 
BufferSlot(TrackedBuffer * tracker)33 BufferSlot::BufferSlot(TrackedBuffer* tracker) :
34     m_tracker(tracker)
35 {
36 }
37 
~BufferSlot()38 BufferSlot::~BufferSlot()
39 {
40     for (auto iter = m_buffers.begin(); iter != m_buffers.end(); iter++)
41     {
42         std::shared_ptr<BufferQueue> queue = m_bufferQueues[iter->first];
43         queue->ReleaseResource(iter->second);
44     }
45     m_buffers.clear();
46     m_bufferQueues.clear();
47 }
48 
Reset()49 MOS_STATUS BufferSlot::Reset()
50 {
51     m_isBusy = false;
52     for (auto iter = m_buffers.begin(); iter != m_buffers.end(); iter++)
53     {
54         std::shared_ptr<BufferQueue> queue = m_bufferQueues[iter->first];
55         queue->ReleaseResource(iter->second);
56     }
57     m_buffers.clear();
58     m_bufferQueues.clear();
59 
60     return MOS_STATUS_SUCCESS;
61 }
62 
GetResource(BufferType type)63 void *BufferSlot::GetResource(BufferType type)
64 {
65     // when the slot is in free status, should not return any surfaces
66     if (IsFree())
67     {
68         return nullptr;
69     }
70 
71     // if surface already in the pool, return it directly
72     auto iter = m_buffers.find(type);
73     if (iter != m_buffers.end())
74     {
75         return iter->second;
76     }
77 
78     std::shared_ptr<BufferQueue> queue = m_tracker->GetBufferQueue(type);
79     if (queue == nullptr)
80     {
81         return nullptr;
82     }
83 
84     void* resource = queue->AcquireResource();
85     // record the surface acquired, only one surface for each type should be kept in the slot
86     m_buffers.insert(std::make_pair(type, resource));
87     m_bufferQueues.insert(std::make_pair(type, queue));
88     return resource;
89 }
90 
91 }