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 //!
24 //! \file     encode_recycle_res_queue.cpp
25 //! \brief    Defines the interface for resource queue
26 //!
27 
28 #include "encode_recycle_res_queue.h"
29 #include "encode_allocator.h"
30 #include "encode_utils.h"
31 #include "mos_os_specific.h"
32 #include "mos_utilities.h"
33 
34 namespace encode {
35 
RecycleQueue(const MOS_ALLOC_GFXRES_PARAMS & param,EncodeAllocator * allocator,uint32_t maxLimit)36 RecycleQueue::RecycleQueue(const MOS_ALLOC_GFXRES_PARAMS &param,
37     EncodeAllocator *allocator,
38     uint32_t maxLimit) :
39     m_maxLimit(maxLimit),
40     m_allocator(allocator)
41 
42 {
43     MOS_SecureMemcpy(
44         &m_param,
45         sizeof(MOS_ALLOC_GFXRES_PARAMS),
46         &param,
47         sizeof(MOS_ALLOC_GFXRES_PARAMS));
48 }
49 
~RecycleQueue()50 RecycleQueue::~RecycleQueue()
51 {
52     m_resources.clear();
53 }
54 
GetResource(uint32_t frameIndex,ResourceType type)55 void *RecycleQueue::GetResource(uint32_t frameIndex, ResourceType type)
56 {
57     if (m_allocator == nullptr)
58     {
59         return nullptr;
60     }
61 
62     uint32_t currIndex = frameIndex % m_maxLimit;
63 
64     while (currIndex >= m_resources.size())
65     {
66         m_type = type;
67 
68         void *resource = nullptr;
69 
70         if (m_type == ResourceType::SURFACE)
71         {
72             resource = m_allocator->AllocateSurface(m_param, true);
73         }
74         else if (m_type == ResourceType::BUFFER)
75         {
76             resource = m_allocator->AllocateResource(m_param, true);
77         }
78         else
79         {
80             return nullptr;
81         }
82 
83         m_resources.push_back(resource);
84     }
85 
86     void *res = m_resources[currIndex];
87 
88     return res;
89 }
90 
DestroyAllResources(EncodeAllocator * allocator)91 MOS_STATUS RecycleQueue::DestroyAllResources(EncodeAllocator *allocator)
92 {
93     ENCODE_CHK_NULL_RETURN(allocator);
94 
95     for (auto res : m_resources)
96     {
97         if (res == nullptr)
98         {
99             continue;
100         }
101 
102         if (m_type == SURFACE)
103         {
104             ENCODE_CHK_STATUS_RETURN(allocator->DestroySurface((MOS_SURFACE *)res));
105         }
106         else if (m_type == BUFFER)
107         {
108              ENCODE_CHK_STATUS_RETURN(allocator->DestroyResource((MOS_RESOURCE *)res));
109         }
110     }
111 
112     m_resources.clear();
113 
114     return MOS_STATUS_SUCCESS;
115 }
116 
117 }