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    mos_commandbuffer_specific.cpp
24 //! \brief   Container class for the specific command buffer
25 //!
26 
27 #include "mos_commandbuffer_specific.h"
28 #include "mos_context_specific.h"
29 #include "mos_gpucontext_specific.h"
30 #include "mos_graphicsresource_specific.h"
31 
Allocate(OsContext * osContext,uint32_t size)32 MOS_STATUS  CommandBufferSpecific::Allocate(OsContext *osContext, uint32_t size)
33 {
34     MOS_OS_FUNCTION_ENTER;
35 
36     MOS_OS_CHK_NULL_RETURN(osContext);
37 
38     if (osContext->GetOsContextValid() == false)
39     {
40         MOS_OS_ASSERTMESSAGE("The OS context got is not valid.");
41         return MOS_STATUS_INVALID_HANDLE;
42     }
43 
44     m_osContext = osContext;
45 
46     GraphicsResource::CreateParams params;
47     params.m_tileType  = MOS_TILE_LINEAR;
48     params.m_type      = MOS_GFXRES_BUFFER;
49     params.m_format    = Format_Buffer;
50     params.m_width     = size;
51     params.m_height    = 1;
52     params.m_depth     = 1;
53     params.m_arraySize = 1;
54     params.m_name      = "MOS CmdBuf";
55 
56     m_graphicsResource = GraphicsResource::CreateGraphicResource(GraphicsResource::osSpecificResource);
57     MOS_OS_CHK_NULL_RETURN(m_graphicsResource);
58 
59     MOS_OS_CHK_STATUS_RETURN(m_graphicsResource->Allocate(osContext, params));
60 
61     m_size = m_graphicsResource->GetSize(); // get real size after allocate
62 
63     return MOS_STATUS_SUCCESS;
64 }
65 
Free()66 void  CommandBufferSpecific::Free()
67 {
68     MOS_OS_FUNCTION_ENTER;
69 
70     if (!m_graphicsResource)
71     {
72         MOS_OS_ASSERTMESSAGE("graphic resource back the commnd buffer need be allocated firstly.");
73         return;
74     }
75 
76     m_graphicsResource->Free(m_osContext, 0);
77     MOS_Delete(m_graphicsResource);
78 }
79 
BindToGpuContext(GpuContext * gpuContext)80 MOS_STATUS CommandBufferSpecific::BindToGpuContext(GpuContext *gpuContext)
81 {
82     MOS_OS_FUNCTION_ENTER;
83 
84     MOS_OS_CHK_NULL_RETURN(gpuContext);
85     MOS_OS_CHK_NULL_RETURN(m_graphicsResource);
86 
87     GraphicsResource::LockParams params;
88     params.m_writeRequest = true;
89     m_lockAddr = static_cast<uint8_t *>(m_graphicsResource->Lock(m_osContext, params));
90     MOS_OS_CHK_NULL_RETURN(m_lockAddr);
91 
92     m_gpuContext = gpuContext;
93 
94     m_readyToUse = true;
95     return MOS_STATUS_SUCCESS;
96 
97 }
98 
isBusy()99 int CommandBufferSpecific::isBusy()
100 {
101     MOS_OS_FUNCTION_ENTER;
102 
103     if (m_graphicsResource == nullptr)
104     {
105          MOS_OS_ASSERTMESSAGE("Graphicresource is not initialized.");
106             // return not busy here to avoid dead lock
107          return 0;
108     }
109 
110     GraphicsResourceSpecific * graphicsResourceSpecific  = static_cast<GraphicsResourceSpecific *>(m_graphicsResource);
111     MOS_LINUX_BO* cmdBufBo = graphicsResourceSpecific->GetBufferObject();
112     if (cmdBufBo == nullptr)
113     {
114         MOS_OS_ASSERTMESSAGE("Failed to get the buffer object for the command buffer, check busy failed.");
115         // return not busy here to avoid dead lock
116         return 0;
117     }
118 
119     return mos_bo_busy(cmdBufBo);
120 }
121 
waitReady()122 void CommandBufferSpecific::waitReady()
123 {
124     MOS_OS_FUNCTION_ENTER;
125 
126     if (m_graphicsResource == nullptr)
127     {
128         MOS_OS_ASSERTMESSAGE("Graphicresource is not initialized.");
129         // return not busy here to avoid dead lock
130         return;
131     }
132 
133     GraphicsResourceSpecific * graphicsResourceSpecific  = static_cast<GraphicsResourceSpecific *>(m_graphicsResource);
134     MOS_LINUX_BO* cmdBufBo = graphicsResourceSpecific->GetBufferObject();
135     if (cmdBufBo == nullptr)
136     {
137         MOS_OS_ASSERTMESSAGE("Failed to get the buffer object for the command buffer, wait failed.");
138         return;
139     }
140 
141     mos_bo_wait_rendering(cmdBufBo);
142 }
143 
UnBindToGpuContext()144 void CommandBufferSpecific::UnBindToGpuContext()
145 {
146     MOS_OS_FUNCTION_ENTER;
147 
148     // Internal state validity check
149     if (m_osContext == nullptr) {
150         MOS_OS_ASSERTMESSAGE("m_osContext ptr should not  be nullptr");
151         return ;
152     }
153 
154     if (m_osContext->GetOsContextValid() == false)
155     {
156         MOS_OS_ASSERTMESSAGE("The OS context got is not valid.");
157         return ;
158     }
159 
160     if (!m_graphicsResource)
161     {
162         MOS_OS_ASSERTMESSAGE("graphic resource back the commnd buffer need be allocated firstly.");
163         return;
164     }
165 
166     m_graphicsResource->Unlock(m_osContext);
167 
168     m_readyToUse = false;
169 }
170 
ReSize(uint32_t newSize)171 MOS_STATUS CommandBufferSpecific:: ReSize(uint32_t newSize)
172 {
173     MOS_OS_FUNCTION_ENTER;
174 
175     if (m_readyToUse)
176     {
177         // release old command buffer
178         UnBindToGpuContext();
179         Free();
180         m_readyToUse = false;
181     }
182 
183     // re-allocate new buffer
184     MOS_OS_CHK_STATUS_RETURN(Allocate(m_osContext, newSize));
185 
186     MOS_OS_CHK_STATUS_RETURN(BindToGpuContext(m_gpuContext));
187 
188     return MOS_STATUS_SUCCESS;
189 }
190 
191