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_resource.cpp
25 //! \brief    Defines the interface for recycle resource
26 //! \details  The manager for the recycle resources
27 //!
28 
29 #include "encode_recycle_resource.h"
30 #include "encode_recycle_res_queue.h"
31 #include "mos_utilities.h"
32 
33 namespace encode {
34 
35 #define CHK_NULL_RETURN(ptr) \
36     if ((ptr) == nullptr)    \
37     {                        \
38         return nullptr;      \
39     }
40 
41 #define CHK_NULL_RETURN_STATUS(ptr) \
42     if ((ptr) == nullptr)    \
43     {                        \
44         return MOS_STATUS_INVALID_PARAMETER;      \
45     }
46 
47 #define CHK_STATUS_RETURN(sts)       \
48     if ((sts) != MOS_STATUS_SUCCESS) \
49     {                                \
50         return nullptr;              \
51     }
52 
RecycleResource(EncodeAllocator * allocator)53 RecycleResource::RecycleResource(EncodeAllocator *allocator)
54     : m_allocator(allocator)
55 {
56 }
57 
~RecycleResource()58 RecycleResource::~RecycleResource()
59 {
60     for (auto pair : m_resourceQueues)
61     {
62         auto que = pair.second;
63         que->DestroyAllResources(m_allocator);
64         MOS_Delete(que);
65     }
66     m_resourceQueues.clear();
67 }
68 
RegisterResource(RecycleResId id,MOS_ALLOC_GFXRES_PARAMS param,uint32_t maxLimit)69 MOS_STATUS RecycleResource::RegisterResource(
70     RecycleResId id,
71     MOS_ALLOC_GFXRES_PARAMS param,
72     uint32_t maxLimit)
73 {
74     auto it = m_resourceQueues.find(id);
75 
76     if (it != m_resourceQueues.end())
77     {
78         return MOS_STATUS_INVALID_PARAMETER;
79     }
80 
81     RecycleQueue *que = MOS_New(RecycleQueue, param, m_allocator, maxLimit);
82     if (que == nullptr)
83     {
84         return MOS_STATUS_CLIENT_AR_NO_SPACE;
85     }
86 
87     m_resourceQueues.insert(std::make_pair(id, que));
88 
89     return MOS_STATUS_SUCCESS;
90 }
91 
GetSurface(RecycleResId id,uint32_t frameIndex)92 MOS_SURFACE *RecycleResource::GetSurface(RecycleResId id, uint32_t frameIndex)
93 {
94     CHK_NULL_RETURN(m_allocator);
95 
96     RecycleQueue *que = GetResQueue(id);
97     CHK_NULL_RETURN(que);
98 
99     if (!que->IsTypeMatched(RecycleQueue::SURFACE))
100     {
101         return nullptr;
102     }
103 
104     void *surface = que->GetResource(frameIndex, RecycleQueue::SURFACE);
105 
106     return (MOS_SURFACE *)surface;
107 }
108 
GetBuffer(RecycleResId id,uint32_t frameIndex)109 MOS_RESOURCE *RecycleResource::GetBuffer(RecycleResId id, uint32_t frameIndex)
110 {
111     CHK_NULL_RETURN(m_allocator);
112 
113     RecycleQueue *que = GetResQueue(id);
114     CHK_NULL_RETURN(que);
115 
116     if (!que->IsTypeMatched(RecycleQueue::BUFFER))
117     {
118         return nullptr;
119     }
120 
121     void *buffer = que->GetResource(frameIndex, RecycleQueue::BUFFER);
122 
123     return (MOS_RESOURCE *)buffer;
124 }
125 
126 }