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_allocator.cpp
24 //! \brief    Defines the interface for encode resource allocate
25 //! \details  encode allocator will allocate and destory buffers, the caller
26 //!           can use directly
27 //!
28 
29 #include "encode_allocator.h"
30 #include "encode_utils.h"
31 #include "media_allocator.h"
32 #include "mos_resource_defs.h"
33 #include "mos_utilities.h"
34 
35 namespace encode {
36 
EncodeAllocator(PMOS_INTERFACE osInterface)37 EncodeAllocator::EncodeAllocator(PMOS_INTERFACE osInterface) :
38     m_osInterface(osInterface)
39 {
40     m_allocator = MOS_New(Allocator, m_osInterface);
41 }
42 
~EncodeAllocator()43 EncodeAllocator::~EncodeAllocator()
44 {
45     MOS_Delete(m_allocator);
46 }
47 
AllocateResource(MOS_ALLOC_GFXRES_PARAMS & param,bool zeroOnAllocate,MOS_HW_RESOURCE_DEF resUsageType)48 MOS_RESOURCE* EncodeAllocator::AllocateResource(
49     MOS_ALLOC_GFXRES_PARAMS &param,
50     bool zeroOnAllocate,
51     MOS_HW_RESOURCE_DEF resUsageType)
52 {
53     if (!m_allocator)
54         return nullptr;
55 
56     if (param.ResUsageType == MOS_HW_RESOURCE_DEF_MAX)
57     {
58         param.ResUsageType = resUsageType;
59     }
60 
61     return m_allocator->AllocateResource(param, zeroOnAllocate, COMPONENT_Encode);
62 }
63 
AllocateSurface(MOS_ALLOC_GFXRES_PARAMS & param,bool zeroOnAllocate,MOS_HW_RESOURCE_DEF ResUsageType)64 MOS_SURFACE* EncodeAllocator::AllocateSurface(
65     MOS_ALLOC_GFXRES_PARAMS &param,
66     bool zeroOnAllocate,
67     MOS_HW_RESOURCE_DEF ResUsageType)
68 {
69     if (!m_allocator)
70         return nullptr;
71 
72     param.ResUsageType = ResUsageType;
73     return m_allocator->AllocateSurface(param, zeroOnAllocate, COMPONENT_Encode);
74 }
75 
DestroyResource(MOS_RESOURCE * resource)76 MOS_STATUS EncodeAllocator::DestroyResource(MOS_RESOURCE* resource)
77 {
78     ENCODE_CHK_NULL_RETURN(m_allocator);
79 
80     return m_allocator->DestroyResource(resource);
81 }
82 
DestroyAllResources()83 MOS_STATUS EncodeAllocator::DestroyAllResources()
84 {
85     ENCODE_CHK_NULL_RETURN(m_allocator);
86 
87     return m_allocator->DestroyAllResources();
88 }
89 
DestroySurface(MOS_SURFACE * surface)90 MOS_STATUS EncodeAllocator::DestroySurface(MOS_SURFACE* surface)
91 {
92     ENCODE_CHK_NULL_RETURN(m_allocator);
93 
94     return m_allocator->DestroySurface(surface);
95 }
96 
Lock(MOS_RESOURCE * resource,MOS_LOCK_PARAMS * lockFlag)97 void* EncodeAllocator::Lock(MOS_RESOURCE* resource, MOS_LOCK_PARAMS* lockFlag)
98 {
99     if (!m_allocator)
100         return nullptr;
101 
102     return m_allocator->Lock(resource, lockFlag);
103 }
104 
LockResourceForWrite(MOS_RESOURCE * resource)105 void* EncodeAllocator::LockResourceForWrite(MOS_RESOURCE* resource)
106 {
107     MOS_LOCK_PARAMS lockFlags;
108     MOS_ZeroMemory(&lockFlags, sizeof(MOS_LOCK_PARAMS));
109     lockFlags.WriteOnly = 1;
110 
111     if (!m_allocator)
112         return nullptr;
113 
114     return m_allocator->Lock(resource, &lockFlags);
115 }
116 
LockResourceWithNoOverwrite(MOS_RESOURCE * resource)117 void* EncodeAllocator::LockResourceWithNoOverwrite(MOS_RESOURCE* resource)
118 {
119     MOS_LOCK_PARAMS lockFlags;
120     MOS_ZeroMemory(&lockFlags, sizeof(MOS_LOCK_PARAMS));
121     lockFlags.WriteOnly   = 1;
122     lockFlags.NoOverWrite = 1;
123 
124     if (!m_allocator)
125         return nullptr;
126 
127     return m_allocator->Lock(resource, &lockFlags);
128 }
129 
LockResourceForRead(MOS_RESOURCE * resource)130 void* EncodeAllocator::LockResourceForRead(MOS_RESOURCE* resource)
131 {
132     MOS_LOCK_PARAMS lockFlags;
133     MOS_ZeroMemory(&lockFlags, sizeof(MOS_LOCK_PARAMS));
134     lockFlags.ReadOnly = 1;
135 
136     if (!m_allocator)
137         return nullptr;
138 
139     return m_allocator->Lock(resource, &lockFlags);
140 }
141 
UnLock(MOS_RESOURCE * resource)142 MOS_STATUS EncodeAllocator::UnLock(MOS_RESOURCE* resource)
143 {
144     ENCODE_CHK_NULL_RETURN(m_allocator);
145 
146     return m_allocator->UnLock(resource);
147 }
148 
SkipResourceSync(MOS_RESOURCE * resource)149 MOS_STATUS EncodeAllocator::SkipResourceSync(MOS_RESOURCE *resource)
150 {
151     ENCODE_CHK_NULL_RETURN(m_allocator);
152 
153     return m_allocator->SkipResourceSync(resource);
154 }
155 
GetSurfaceInfo(PMOS_SURFACE surface)156 MOS_STATUS EncodeAllocator::GetSurfaceInfo(PMOS_SURFACE surface)
157 {
158     ENCODE_CHK_NULL_RETURN(m_allocator);
159     ENCODE_CHK_NULL_RETURN(surface);
160 
161     surface->Format = Format_Invalid;
162     surface->dwArraySlice = 0;
163     surface->dwMipSlice   = 0;
164     surface->S3dChannel   = MOS_S3D_NONE;
165     ENCODE_CHK_STATUS_RETURN(m_allocator->GetSurfaceInfo(&surface->OsResource, surface));
166 
167     return MOS_STATUS_SUCCESS;
168 }
169 
UpdateResourceUsageType(PMOS_RESOURCE osResource,MOS_HW_RESOURCE_DEF resUsageType)170 MOS_STATUS EncodeAllocator::UpdateResourceUsageType(PMOS_RESOURCE osResource, MOS_HW_RESOURCE_DEF resUsageType)
171 {
172     ENCODE_CHK_NULL_RETURN(m_allocator);
173 
174     return (m_allocator->UpdateResourceUsageType(osResource, resUsageType));
175 }
176 
SyncOnResource(PMOS_RESOURCE osResource,bool bWriteOperation)177 MOS_STATUS EncodeAllocator::SyncOnResource(
178     PMOS_RESOURCE osResource,
179     bool bWriteOperation)
180 {
181     ENCODE_CHK_NULL_RETURN(m_allocator);
182 
183     return (m_allocator->SyncOnResource(osResource, bWriteOperation));
184 }
185 }
186