1 /*
2 * Copyright (c) 2018-2019, 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      cm_surface_state_manager.cpp
24 //! \brief     Contains Class CmSurfaceState2Dor3DMgr  definitions
25 //!
26 
27 #include "cm_surface_state_manager.h"
28 #include "cm_surface_state.h"
29 
CmSurfaceState2Dor3DMgr(CM_HAL_STATE * cmhal,MOS_RESOURCE * resource)30 CmSurfaceState2Dor3DMgr::CmSurfaceState2Dor3DMgr(CM_HAL_STATE *cmhal, MOS_RESOURCE *resource):
31     m_cmhal (cmhal),
32     m_resource (resource),
33     m_defaultMoc(MOS_CM_RESOURCE_USAGE_SurfaceState << 8),
34     m_defaultRenderTarget (true),
35     m_defaultFrameType (CM_FRAME),
36     m_defaultFormat(Format_Invalid),
37     m_defaultWidth(0),
38     m_defaultHeight(0),
39     m_defaultDepth(0),
40     m_rotationFlag(0),
41     m_chromaSitting(0),
42     m_dirty(false)
43 {
44     m_resourceData = {0};
45     MOS_ZeroMemory(m_defaultSurfState, sizeof(m_defaultSurfState));
46     if (m_cmhal && m_cmhal->cmHalInterface)
47     {
48         m_defaultMoc = (m_cmhal->cmHalInterface->GetDefaultMOCS()) << 8;
49     }
50 }
51 
~CmSurfaceState2Dor3DMgr()52 CmSurfaceState2Dor3DMgr::~CmSurfaceState2Dor3DMgr()
53 {
54     clean();
55 }
56 
clean()57 void CmSurfaceState2Dor3DMgr::clean()
58 {
59     for (int i = 0; i < 4; i++)
60     {
61         MOS_Delete(m_defaultSurfState[i]);
62         for (auto ite = m_surfStateMap[i].begin(); ite != m_surfStateMap[i].end(); ite++)
63         {
64             MOS_Delete(ite->second);
65         }
66         m_surfStateMap[i].clear();
67     }
68 }
69 
GetSurfaceState(int isAvs,int isSampler,CM_HAL_SURFACE2D_SURFACE_STATE_PARAM * param)70 CmSurfaceState* CmSurfaceState2Dor3DMgr::GetSurfaceState(int isAvs, int isSampler, CM_HAL_SURFACE2D_SURFACE_STATE_PARAM *param)
71 {
72     int index = isAvs * 2 + isSampler;
73     CM_ASSERT(index < 4);
74     if (m_dirty)
75     {
76         clean();
77         m_dirty = false;
78     }
79     if (param == nullptr) // get default
80     {
81         if (! m_defaultSurfState[index])
82         {
83             m_defaultSurfState[index] = MOS_New(CmSurfaceState2Dor3D, m_cmhal);
84             if (m_defaultSurfState[index] == nullptr)
85             {
86                 return nullptr;
87             }
88             m_defaultSurfState[index]->Initialize(m_resource, isAvs, isSampler);
89             m_defaultSurfState[index]->SetFrameType(m_defaultFrameType);
90             m_defaultSurfState[index]->SetMemoryObjectControl(m_defaultMoc);
91             m_defaultSurfState[index]->SetRenderTarget(m_defaultRenderTarget);
92             m_defaultSurfState[index]->SetFormat(m_defaultFormat);
93             m_defaultSurfState[index]->SetUserDimension(m_defaultWidth, m_defaultHeight, m_defaultDepth);
94             m_defaultSurfState[index]->SetRotationFlag(m_rotationFlag);
95             m_defaultSurfState[index]->SetChromaSitting(m_chromaSitting);
96             m_defaultSurfState[index]->GenerateSurfaceState();
97         }
98         return m_defaultSurfState[index];
99     }
100     else
101     {
102         uint32_t hashIdx = Hash(param);
103         auto search = m_surfStateMap[index].find(hashIdx);
104         if (search == m_surfStateMap[index].end())
105         {
106             CmSurfaceState2Dor3D *ss = MOS_New(CmSurfaceState2Dor3D, m_cmhal);
107             if (ss == nullptr)
108             {
109                 return nullptr;
110             }
111             ss->Initialize(m_resource, isAvs, isSampler);
112             ss->SetFrameType(m_defaultFrameType);
113             ss->SetMemoryObjectControl(m_defaultMoc);
114             ss->SetRenderTarget(m_defaultRenderTarget);
115             ss->SetFormat(m_defaultFormat);
116             ss->SetUserDimension(m_defaultWidth, m_defaultHeight, m_defaultDepth);
117             ss->SetRotationFlag(m_rotationFlag);
118             ss->SetChromaSitting(m_chromaSitting);
119             ss->GenerateSurfaceState(param);
120             m_surfStateMap[index][hashIdx] = ss;
121             return ss;
122         }
123         else
124         {
125             return search->second;
126         }
127     }
128     return nullptr;
129 }
130 
SetRotationFlag(uint32_t rotation)131 void CmSurfaceState2Dor3DMgr::SetRotationFlag(uint32_t rotation)
132 {
133     if (m_rotationFlag != rotation)
134     {
135         m_rotationFlag = rotation;
136 
137         // clean the 3D sampler surface state
138         int index = _3D_SAMPLER_SURFACE;
139         MOS_Delete(m_defaultSurfState[index]);
140         for (auto ite = m_surfStateMap[index].begin(); ite != m_surfStateMap[index].end(); ite++)
141         {
142             MOS_Delete(ite->second);
143         }
144         m_surfStateMap[index].clear();
145 
146         // clean the AVS sampler surface state
147 
148         index = _AVS_SAMPLER_SURFACE;
149         MOS_Delete(m_defaultSurfState[index]);
150         for (auto ite = m_surfStateMap[index].begin(); ite != m_surfStateMap[index].end(); ite++)
151         {
152             MOS_Delete(ite->second);
153         }
154         m_surfStateMap[index].clear();
155     }
156 }
157 
SetChromaSitting(uint8_t chromaSitting)158 void CmSurfaceState2Dor3DMgr::SetChromaSitting(uint8_t chromaSitting)
159 {
160     if (m_chromaSitting != chromaSitting)
161     {
162         m_chromaSitting = chromaSitting;
163         // clean the AVS sampler surface state
164         int index = _AVS_SAMPLER_SURFACE;
165         MOS_Delete(m_defaultSurfState[index]);
166         for (auto ite = m_surfStateMap[index].begin(); ite != m_surfStateMap[index].end(); ite++)
167         {
168             MOS_Delete(ite->second);
169         }
170         m_surfStateMap[index].clear();
171     }
172 }
173 
174 
175 template <typename T>
hash_combine(uint32_t & res,const T & field)176 inline void hash_combine(uint32_t &res, const T &field)
177 {
178     res ^= field + 0x9e3779b9 + (res << 6) + (res >> 2);
179 }
180 
Hash(CM_HAL_SURFACE2D_SURFACE_STATE_PARAM * param)181 uint32_t CmSurfaceState2Dor3DMgr::Hash(CM_HAL_SURFACE2D_SURFACE_STATE_PARAM *param)
182 {
183     CM_HAL_SURFACE2D_SURFACE_STATE_PARAM temp;
184     if (param == nullptr)
185     {
186         MOS_ZeroMemory(&temp, sizeof(temp));
187         param = &temp;
188     }
189     uint32_t value = 0;
190     hash_combine(value, param->depth);
191     hash_combine(value, param->format);
192     hash_combine(value, param->height);
193     hash_combine(value, param->memoryObjectControl);
194     hash_combine(value, param->pitch);
195     hash_combine(value, param->surfaceXOffset);
196     hash_combine(value, param->surfaceYOffset);
197     hash_combine(value, param->width);
198 
199     return value;
200 }
201 
202 
203 
CmSurfaceStateBufferMgr(CM_HAL_STATE * cmhal,MOS_RESOURCE * resource)204 CmSurfaceStateBufferMgr::CmSurfaceStateBufferMgr(CM_HAL_STATE *cmhal, MOS_RESOURCE *resource):
205     m_defaultSurfState(nullptr),
206     m_cmhal (cmhal),
207     m_resource (resource),
208     m_origSize(0),
209     m_defaultMoc(MOS_CM_RESOURCE_USAGE_SurfaceState << 8),
210     m_dirty(false)
211 {
212     if (m_cmhal && m_cmhal->cmHalInterface)
213     {
214         m_defaultMoc = (m_cmhal->cmHalInterface->GetDefaultMOCS()) << 8;
215     }
216 }
217 
~CmSurfaceStateBufferMgr()218 CmSurfaceStateBufferMgr::~CmSurfaceStateBufferMgr()
219 {
220     clean();
221 }
222 
clean()223 void CmSurfaceStateBufferMgr::clean()
224 {
225     MOS_Delete(m_defaultSurfState);
226     for (auto ite = m_surfStateMap.begin(); ite != m_surfStateMap.end(); ite++)
227     {
228         MOS_Delete(ite->second);
229     }
230     m_surfStateMap.clear();
231 }
GetSurfaceState(CM_HAL_BUFFER_SURFACE_STATE_ENTRY * param)232 CmSurfaceState* CmSurfaceStateBufferMgr::GetSurfaceState(CM_HAL_BUFFER_SURFACE_STATE_ENTRY *param)
233 {
234     if (m_dirty)
235     {
236         clean();
237         m_dirty = false;
238     }
239     if (param == nullptr) // get default
240     {
241         if (! m_defaultSurfState)
242         {
243             m_defaultSurfState = MOS_New(CmSurfaceStateBuffer, m_cmhal);
244             if (m_defaultSurfState == nullptr)
245             {
246                 return nullptr;
247             }
248             m_defaultSurfState->Initialize(m_resource, m_origSize);
249             m_defaultSurfState->SetMemoryObjectControl(m_defaultMoc);
250             m_defaultSurfState->GenerateSurfaceState();
251         }
252         return m_defaultSurfState;
253     }
254     else
255     {
256         uint32_t hashIdx = Hash(param);
257         auto search = m_surfStateMap.find(hashIdx);
258         if (search == m_surfStateMap.end())
259         {
260             CmSurfaceStateBuffer *ss = MOS_New(CmSurfaceStateBuffer, m_cmhal);
261             if (ss == nullptr)
262             {
263                 return nullptr;
264             }
265             ss->Initialize(m_resource, m_origSize);
266             ss->SetMemoryObjectControl(m_defaultMoc);
267             ss->GenerateSurfaceState(param);
268             m_surfStateMap[hashIdx] = ss;
269             return ss;
270         }
271         else
272         {
273             return search->second;
274         }
275     }
276     return nullptr;
277 
278 }
279 
Hash(CM_HAL_BUFFER_SURFACE_STATE_ENTRY * param)280 uint32_t CmSurfaceStateBufferMgr::Hash(CM_HAL_BUFFER_SURFACE_STATE_ENTRY *param)
281 {
282     CM_HAL_BUFFER_SURFACE_STATE_ENTRY temp;
283     if (param == nullptr)
284     {
285         MOS_ZeroMemory(&temp, sizeof(temp));
286         param = &temp;
287     }
288     uint32_t value = 0;
289     hash_combine(value, param->surfaceStateSize);
290     hash_combine(value, param->surfaceStateOffset);
291     hash_combine(value, param->surfaceStateMOCS);
292 
293     return value;
294 
295 }
296 
297