xref: /aosp_15_r20/external/intel-media-driver/media_driver/agnostic/common/cm/cm_surface_state.cpp (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
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      cm_surface_state.cpp
24 //! \brief     Contains Class CmSurfaceState  definitions
25 //!
26 
27 #include "cm_surface_state.h"
28 #include "cm_surface_state_manager.h"
29 #include "renderhal_platform_interface.h"
30 
31 uint16_t RenderHal_CalculateYOffset(PMOS_INTERFACE pOsInterface, PMOS_RESOURCE pOsResource);
32 
33 extern const MHW_SURFACE_PLANES g_cRenderHal_SurfacePlanes[RENDERHAL_PLANES_DEFINITION_COUNT];
34 
35 const uint32_t g_cLookup_RotationMode[8] =
36 {
37     ROTATION_IDENTITY,  // 0 - MHW_ROTATION_IDENTITY
38     ROTATION_90,        // 1 - MHW_ROTATION_90
39     ROTATION_180,       // 2 - MHW_ROTATION_180
40     ROTATION_270,       // 3 - MHW_ROTATION_270
41     ROTATION_IDENTITY,  // 4 - MHW_MIRROR_HORIZONTAL
42     ROTATION_180,       // 5 - MHW_MIRROR_VERTICAL
43     ROTATION_270,       // 6 - MHW_ROTATE_90_MIRROR_VERTICAL
44     ROTATION_90         // 7 - MHW_ROTATE_90_MIRROR_HORIZONTAL
45 };
46 
CmSurfaceState(CM_HAL_STATE * cmhal)47 CmSurfaceState::CmSurfaceState(CM_HAL_STATE *cmhal):
48     m_cmhal(cmhal),
49     m_renderhal(nullptr),
50     m_resource(nullptr),
51     m_memoryObjectControl(0),
52     m_ssh(nullptr),
53     m_btIdx(-1),
54     m_bteIdx(-1),
55     m_ssIdx(-1)
56 {
57     m_resourceData = {0};
58 }
59 
Initialize(MOS_RESOURCE * resource)60 CM_RETURN_CODE CmSurfaceState::Initialize(MOS_RESOURCE *resource)
61 {
62     if (m_cmhal)
63     {
64         m_renderhal = m_cmhal->renderHal;
65     }
66     if (!m_renderhal)
67     {
68         return CM_NULL_POINTER;
69     }
70     m_resource = resource;
71     return CM_SUCCESS;
72 }
73 
GetCacheabilityControl()74 uint32_t CmSurfaceState::GetCacheabilityControl()
75 {
76     RENDERHAL_SURFACE_STATE_PARAMS  surfaceParam;
77     MOS_ZeroMemory(&surfaceParam, sizeof(surfaceParam));
78 
79     GMM_RESOURCE_FLAG gmm_flags = m_resource->pGmmResInfo->GetResFlags();
80     if (gmm_flags.Gpu.CameraCapture)
81     {
82         m_cmhal->cmHalInterface->HwSetSurfaceMemoryObjectControl(
83             MOS_MHW_GMM_RESOURCE_USAGE_CAMERA_CAPTURE << 8, &surfaceParam);
84     }
85     else
86     {
87         m_cmhal->cmHalInterface->HwSetSurfaceMemoryObjectControl(
88             m_memoryObjectControl, &surfaceParam);
89     }
90     return surfaceParam.MemObjCtl;
91 }
92 
CmSurfaceState2Dor3D(CM_HAL_STATE * cmhal)93 CmSurfaceState2Dor3D::CmSurfaceState2Dor3D(CM_HAL_STATE *cmhal):
94     //m_device(device),
95     CmSurfaceState(cmhal),
96     m_format(Format_Invalid),
97     m_width(0),
98     m_height(0),
99     m_depth(0),
100     m_pitch(0),
101     m_qPitch(0),
102     m_tile(0),
103     m_tileModeGMM(MOS_TILE_LINEAR_GMM),
104     m_bGMMTileEnabled(0),
105     m_isCompressed(0),
106     m_compressionMode(0),
107     m_mmcState(MOS_MEMCOMP_DISABLED),
108     m_compressionFormat(0),
109     m_rotation(0),
110     m_chromaSitting(0),
111     m_surfaceXOffset(0),
112     m_surfaceYOffset(0),
113     m_frameType(CM_FRAME),
114     m_isRenderTarget(true),
115     m_paletteID(0),
116     m_userWidth(0),
117     m_userHeight(0),
118     m_userDepth(0),
119     m_maxStateSize(0),
120     m_avsUsed(0),
121     m_numPlane(0),
122     m_pixelPitch(false),
123     m_isWidthInDWord(false),
124     m_isVme(false),
125     m_direction(0),
126     m_isHalfPitchChroma(false),
127     m_isInterleaveChrome(false),
128     m_uXOffset(0),
129     m_uYOffset(0),
130     m_vXOffset(0),
131     m_vYOffset(0),
132     m_isVaSurface(false)
133 {
134     MOS_ZeroMemory(m_surfOffsets, sizeof(m_surfOffsets));
135     MOS_ZeroMemory(m_xOffsets, sizeof(m_xOffsets));
136     MOS_ZeroMemory(m_yOffsets, sizeof(m_yOffsets));
137     MOS_ZeroMemory(m_lockOffsets, sizeof(m_lockOffsets));
138     MOS_ZeroMemory(m_planeParams, sizeof(m_planeParams));
139     MOS_ZeroMemory(m_cmds, sizeof(m_cmds));
140 }
141 
Initialize(MOS_RESOURCE * resource,bool isAvs,bool isSampler)142 CM_RETURN_CODE CmSurfaceState2Dor3D::Initialize(MOS_RESOURCE *resource, bool isAvs, bool isSampler)
143 {
144     CmSurfaceState::Initialize(resource);
145 
146     m_avsUsed = isAvs;
147     m_pixelPitch = (!isAvs)&&isSampler;
148     m_isVme = isAvs&&(!isSampler);
149 
150     m_isWidthInDWord = m_avsUsed?false:(m_pixelPitch?false:true);
151     m_maxStateSize = m_renderhal->pRenderHalPltInterface->GetSurfaceStateCmdSize();
152     return CM_SUCCESS;
153 }
154 
GenerateSurfaceState(CM_HAL_SURFACE2D_SURFACE_STATE_PARAM * param)155 MOS_STATUS CmSurfaceState2Dor3D::GenerateSurfaceState(CM_HAL_SURFACE2D_SURFACE_STATE_PARAM *param)
156 {
157     RefreshSurfaceInfo(param);
158 
159     UpdateSurfaceState();
160 
161     return MOS_STATUS_SUCCESS;
162 }
163 
RefreshSurfaceInfo(CM_HAL_SURFACE2D_SURFACE_STATE_PARAM * param)164 MOS_STATUS CmSurfaceState2Dor3D::RefreshSurfaceInfo(CM_HAL_SURFACE2D_SURFACE_STATE_PARAM *param)
165 {
166     MOS_SURFACE         ResDetails;
167 
168     MHW_RENDERHAL_ASSERT(!Mos_ResourceIsNull(m_resource));
169     MOS_ZeroMemory(&ResDetails, sizeof(MOS_SURFACE));
170 
171     ResDetails.Format = (param && param->format)?(MOS_FORMAT)param->format:m_format;
172 
173     m_cmhal->osInterface->pfnGetResourceInfo(m_cmhal->osInterface, m_resource, &ResDetails);
174 
175     m_width = ResDetails.dwWidth;
176     m_height = ResDetails.dwHeight;
177     m_depth = ResDetails.dwDepth;
178     m_pitch = ResDetails.dwPitch;
179     m_qPitch = ResDetails.dwQPitch;
180     m_format = ResDetails.Format;
181     m_tile = ResDetails.TileType;
182     m_tileModeGMM = ResDetails.TileModeGMM;
183     m_bGMMTileEnabled = ResDetails.bGMMTileEnabled;
184     m_isCompressed = ResDetails.bIsCompressed;
185     m_compressionMode = ResDetails.CompressionMode;
186 
187     m_cmhal->osInterface->pfnGetMemoryCompressionMode(m_cmhal->osInterface,
188         m_resource, &m_mmcState);
189     m_cmhal->osInterface->pfnGetMemoryCompressionFormat(m_cmhal->osInterface,
190         m_resource, &m_compressionFormat);
191 
192 #ifdef __GNUC__
193     // calculate the offsets
194     switch (m_format)
195     {
196         case Format_NV12:
197             m_surfOffsets[1] = ResDetails.RenderOffset.YUV.U.BaseOffset;
198             m_yOffsets[1]    = ResDetails.RenderOffset.YUV.U.YOffset;
199             break;
200         case Format_P010:
201         case Format_P016:
202         case Format_P208:
203             m_surfOffsets[1] = (m_height - m_height % 32) * m_pitch;
204             m_yOffsets[1]    = m_height % 32;
205             break;
206         case Format_NV21:
207             m_surfOffsets[1] = m_height * m_pitch;
208             break;
209         case Format_YV12:
210             m_surfOffsets[2] = m_height * m_pitch;
211             m_yOffsets[2]    = 0;
212             m_surfOffsets[1] = m_height * m_pitch * 5 / 4;
213             m_yOffsets[1]    = 0;
214             break;
215         case Format_422H:
216             // Calculation methods are derived from Gmm's result.
217             m_surfOffsets[1] = (m_height - m_height % 32) * m_pitch;
218             m_yOffsets[1]       = m_height % 32;
219             m_surfOffsets[2] = (m_height * 2 - (m_height * 2) % 32) * m_pitch;
220             m_yOffsets[2]      = (m_height * 2) % 32;
221             break;
222         case Format_IMC3:
223         case Format_422V:
224             // Calculation methods are derived from Gmm's result.
225             m_surfOffsets[1] = (m_height - m_height % 32) * m_pitch;
226             m_yOffsets[1]       = m_height % 32;
227             m_surfOffsets[2] = (m_height * 3 / 2 - (m_height * 3 / 2) % 32) * m_pitch;
228             m_yOffsets[2]       = (m_height * 3 / 2) % 32;
229             break;
230         case Format_IMC4:
231             m_surfOffsets[1] = m_height * m_pitch;
232             m_yOffsets[1]       = m_height;
233             m_yOffsets[2]       = m_height * 3 / 2;
234             break;
235         case Format_411P:
236             m_surfOffsets[1] = m_height * m_pitch;
237             m_yOffsets[1]      = 0;
238             m_surfOffsets[2] = m_height * m_pitch * 2;
239             m_yOffsets[2]       = 0;
240             break;
241         case Format_444P:
242         case Format_RGBP:
243             m_surfOffsets[1] = m_height * m_pitch;
244             m_yOffsets[1]       = 0;
245             m_surfOffsets[2] = m_height * m_pitch * 2;
246             m_yOffsets[2]       = 0;
247             break;
248 
249         default:
250             break;
251     }
252 #else
253     if (IS_RGB32_FORMAT(m_format) ||
254         IS_RGB16_FORMAT(m_format) ||
255         IS_RGB128_FORMAT(m_format)||
256         m_format == Format_RGB    ||
257         m_format == Format_Y410)
258     {
259         m_surfOffsets[0] = ResDetails.RenderOffset.RGB.BaseOffset;
260         m_xOffsets[0] = ResDetails.RenderOffset.RGB.XOffset;
261         m_yOffsets[0] = ResDetails.RenderOffset.RGB.YOffset;
262     }
263     else // YUV or PL3_RGB
264     {
265         // Get Y plane information (plane offset, X/Y offset)
266         m_surfOffsets[0] = ResDetails.RenderOffset.YUV.Y.BaseOffset;
267         m_xOffsets[0] = ResDetails.RenderOffset.YUV.Y.XOffset;
268         m_yOffsets[0] = ResDetails.RenderOffset.YUV.Y.YOffset;
269         m_lockOffsets[0] = ResDetails.LockOffset.YUV.Y;
270 
271         // Get U/UV plane information (plane offset, X/Y offset)
272         m_surfOffsets[1] = ResDetails.RenderOffset.YUV.U.BaseOffset;
273         m_xOffsets[1] = ResDetails.RenderOffset.YUV.U.XOffset;
274         m_yOffsets[1] = ResDetails.RenderOffset.YUV.U.YOffset;
275         m_lockOffsets[1] = ResDetails.LockOffset.YUV.U;
276 
277         // Get V plane information (plane offset, X/Y offset)
278         m_surfOffsets[2] = ResDetails.RenderOffset.YUV.V.BaseOffset;
279         m_xOffsets[2] = ResDetails.RenderOffset.YUV.V.XOffset;
280         m_yOffsets[2] = ResDetails.RenderOffset.YUV.V.YOffset;
281         m_lockOffsets[2] = ResDetails.LockOffset.YUV.V;
282     }
283 #endif
284 
285     // set User-defined dimension
286     m_width = m_userWidth?m_userWidth:m_width;
287     m_height = m_userHeight?m_userHeight:m_height;
288     m_depth = m_userDepth?m_userDepth:m_depth;
289 
290     // set paramaters for alias
291     if (param)
292     {
293         m_format = param->format?(MOS_FORMAT)param->format:m_format;
294         m_width = param->width?param->width:m_width;
295         m_height = param->height?param->height:m_height;
296         m_depth = param->depth?param->depth:m_depth;
297         m_pitch = param->pitch?param->pitch:m_pitch;
298         m_surfaceXOffset = param->surfaceXOffset?param->surfaceXOffset:m_surfaceXOffset;
299         m_surfaceYOffset = param->surfaceYOffset?param->surfaceYOffset:m_surfaceYOffset;
300         m_memoryObjectControl = param->memoryObjectControl?param->memoryObjectControl:m_memoryObjectControl;
301     }
302 
303     return MOS_STATUS_SUCCESS;
304 }
305 
GetDIUVOffSet()306 void CmSurfaceState2Dor3D::GetDIUVOffSet()
307 {
308     uint32_t dwNumRowsFromTopV = 0;
309     uint32_t dwNumRowsFromTopU = 0;
310     uint32_t dwNumColsFromLeftV = 0;
311     uint32_t dwNumColsFromLeftU = 0;
312 
313     switch (m_format)
314     {
315         //  YY
316         //  YY
317         //  V-
318         //  U-
319         case Format_IMC1:
320             dwNumRowsFromTopV = m_height;
321             dwNumRowsFromTopU = m_height + (m_height >> 1);
322             break;
323 
324         //  YY
325         //  YY
326         //  VU
327         case Format_IMC2:
328             dwNumRowsFromTopV = dwNumRowsFromTopU = m_height;
329             dwNumColsFromLeftU = m_pitch >> 1;
330             break;
331 
332         //  YY
333         //  YY
334         //  U-
335         //  V-
336         case Format_IMC3:
337             dwNumRowsFromTopU = m_height;
338             dwNumRowsFromTopV = m_height + (m_height >> 1);
339             break;
340 
341         //  YY
342         //  YY
343         //  UV
344         case Format_IMC4:
345             dwNumRowsFromTopU = dwNumRowsFromTopV = m_height;
346             dwNumColsFromLeftV = m_pitch >> 1;
347             break;
348 
349         //  YY
350         //  YY
351         //  U
352         //  V
353         case Format_I420:
354         case Format_IYUV:
355             dwNumRowsFromTopU = m_height;
356             dwNumRowsFromTopV = m_height + (m_height >> 1);
357             break;
358 
359         //  YY
360         //  YY
361         //  V
362         //  U
363         case Format_YV12:
364             dwNumRowsFromTopU = m_height + (m_height >> 1);
365             dwNumRowsFromTopV = m_height;
366             break;
367 
368         //  YYYY
369         //  YYYY
370         //  YYYY
371         //  YYYY
372         //  V
373         //  U
374         case Format_YVU9:
375             dwNumRowsFromTopU = m_height;
376             dwNumRowsFromTopV = m_height + (m_height >> 2);
377             break;
378 
379         //  NV12                P208
380         //  ----                ----
381         //  YY                  YY
382         //  YY                  UV (interleaved)
383         //  UV (interleaved)
384         case Format_NV12:
385         case Format_P208:
386         case Format_P016:
387         case Format_P010:
388             dwNumRowsFromTopU = dwNumRowsFromTopV = m_height;
389             break;
390 
391         //  NV11
392         //  ----
393         //  YYYY
394         //  UV (interleaved)
395         case Format_NV11:
396             dwNumRowsFromTopU = dwNumRowsFromTopV = m_height;
397             break;
398 
399        default:
400            MHW_RENDERHAL_ASSERTMESSAGE("called with Packed or Unknown format.");
401            break;
402     }
403 
404     // the Offsets must be even numbers so we round down
405     dwNumRowsFromTopU = MOS_ALIGN_FLOOR(dwNumRowsFromTopU, 2);
406     dwNumColsFromLeftU = MOS_ALIGN_FLOOR(dwNumColsFromLeftU, 2);
407     dwNumRowsFromTopV = MOS_ALIGN_FLOOR(dwNumRowsFromTopV, 2);
408     dwNumColsFromLeftV = MOS_ALIGN_FLOOR(dwNumColsFromLeftV, 2);
409 
410     m_vYOffset = (uint16_t)dwNumRowsFromTopV;
411     m_uYOffset = (uint16_t)dwNumRowsFromTopU;
412     m_vXOffset = (uint16_t)dwNumColsFromLeftV;
413     m_uXOffset = (uint16_t)dwNumColsFromLeftU;
414 }
415 
IsFormatMMCSupported(MOS_FORMAT format)416 bool CmSurfaceState2Dor3D::IsFormatMMCSupported(MOS_FORMAT format)
417 {
418     // Check if Sample Format is supported
419     if ((format != Format_YUY2)        &&
420         (format != Format_Y410)        &&
421         (format != Format_Y216)        &&
422         (format != Format_Y210)        &&
423         (format != Format_Y416)        &&
424         (format != Format_P010)        &&
425         (format != Format_P016)        &&
426         (format != Format_AYUV)        &&
427         (format != Format_NV21)        &&
428         (format != Format_NV12)        &&
429         (format != Format_UYVY)        &&
430         (format != Format_YUYV)        &&
431         (format != Format_A8B8G8R8)    &&
432         (format != Format_X8B8G8R8)    &&
433         (format != Format_A8R8G8B8)    &&
434         (format != Format_X8R8G8B8)    &&
435         (format != Format_B10G10R10A2) &&
436         (format != Format_R10G10B10A2) &&
437         (format != Format_A16R16G16B16F))
438     {
439         return false;
440     }
441 
442     return true;
443 }
444 
GetPlaneDefinitionMedia()445 int CmSurfaceState2Dor3D::GetPlaneDefinitionMedia()
446 {
447     int planeIndex = -1;
448     bool isRenderOutTarget = false;
449     if ( (m_format == Format_NV12 || m_format == Format_YV12 || m_format == Format_Y216)
450           && (!m_pixelPitch))
451     {
452         isRenderOutTarget = true;
453     }
454 
455     uint8_t direction = GetDirection();
456 
457     switch (m_format)
458     {
459         case Format_NV12:
460         {
461             // On G8, NV12 format needs the width and Height to be a multiple
462             // of 4 for both 3D sampler and 8x8 sampler; G75 needs the width
463             // of NV12 input surface to be a multiple of 4 for 3D sampler;
464             // G9+ does not has such restriction; to simplify the implementation,
465             // we enable 2 plane NV12 for all of the platform when the width
466             // or Height is not a multiple of 4
467             bool is2PlaneNeeded = false;
468             if (!GFX_IS_GEN_10_OR_LATER(m_renderhal->Platform))
469             {
470                 is2PlaneNeeded = (!MOS_IS_ALIGNED(m_height, 4) || !MOS_IS_ALIGNED(m_width, 4));
471             }
472             else
473             {
474                 is2PlaneNeeded = (!MOS_IS_ALIGNED(m_height, 2) || !MOS_IS_ALIGNED(m_width, 2));
475             }
476             if (is2PlaneNeeded)
477             {
478                 planeIndex = RENDERHAL_PLANES_NV12_2PLANES_ADV;
479             }
480             else
481             {
482                 planeIndex = RENDERHAL_PLANES_NV12_ADV;
483                 m_uYOffset = RenderHal_CalculateYOffset(m_renderhal->pOsInterface, m_resource);
484             }
485 
486             m_isHalfPitchChroma = false;
487             m_isInterleaveChrome = true;
488 
489             // Set up chroma direction
490             m_direction = direction;
491             break;
492         }
493         case Format_P016:
494             if (m_isVme)
495             {
496                 planeIndex = RENDERHAL_PLANES_P010_1PLANE_ADV;
497             }
498             else
499             {
500                 planeIndex = RENDERHAL_PLANES_P016_2PLANES_ADV;
501             }
502             break;
503 
504         case Format_P208:
505             planeIndex = RENDERHAL_PLANES_P208_1PLANE_ADV;
506             break;
507 
508         case Format_IMC1:
509         case Format_IMC2:
510         case Format_IMC3:
511         case Format_IMC4:
512         case Format_I420:
513         case Format_IYUV:
514         case Format_YV12:
515         case Format_YVU9:
516             planeIndex     = RENDERHAL_PLANES_PL3_ADV;
517             m_isHalfPitchChroma = false;
518 
519             if (m_format == Format_I420 ||
520                 m_format == Format_IYUV ||
521                 m_format == Format_YV12 ||
522                 m_format == Format_NV11)
523             {
524                 m_isHalfPitchChroma = true;
525             }
526 
527             // Set up chroma direction
528             m_direction = direction;
529 
530             if (true) //if (!pParams->bAVS)
531             {
532                 // Get U/V offset for PL3 DNDI
533                 RENDERHAL_SURFACE surface;
534                 MOS_ZeroMemory(&surface, sizeof(surface));
535                 GetDIUVOffSet();
536                 planeIndex = RENDERHAL_PLANES_NV12_ADV;
537             }
538             break;
539 
540         case Format_400P:
541             // Single Y plane here is treated like a NV12 surface.
542             // U and V offsets fall inside this Y plane. Eventhough false UV pixels are
543             // picked by the kernel, CSC coeffecients are such that the effect of these
544             // are nullified.
545             planeIndex     = RENDERHAL_PLANES_NV12_ADV;
546             break;
547 
548         case Format_411P:
549             planeIndex     = RENDERHAL_PLANES_411P_ADV;
550             break;
551 
552         case Format_411R:
553             planeIndex     = RENDERHAL_PLANES_411R_ADV;
554             break;
555 
556         case Format_422H:
557             planeIndex     = RENDERHAL_PLANES_422H_ADV;
558             break;
559 
560         case Format_422V:
561             planeIndex     = RENDERHAL_PLANES_422V_ADV;
562             break;
563 
564         case Format_444P:
565             planeIndex     = RENDERHAL_PLANES_444P_ADV;
566             break;
567 
568         case Format_RGBP:
569             planeIndex     = RENDERHAL_PLANES_RGBP_ADV;
570             break;
571 
572         case Format_BGRP:
573             planeIndex     = RENDERHAL_PLANES_BGRP_ADV;
574             break;
575 
576         case Format_AYUV:
577             planeIndex = RENDERHAL_PLANES_AYUV_ADV;
578             break;
579 
580         case Format_YUYV:
581         case Format_YUY2:
582             if (m_isVme)
583             {
584                 //Since 422 planar is not supported on application side.
585                 //App is using 422 packed as WA with w=w/2 and h=h*2
586                 m_width = m_width * 2;
587                 m_height = m_height / 2;
588                 planeIndex = RENDERHAL_PLANES_YUY2_ADV;
589 
590                 // Set up chroma direction
591                 m_direction = direction;
592             }
593             else
594             {
595                 planeIndex = RENDERHAL_PLANES_YUY2_ADV;
596 
597                 // Set up chroma direction
598                 m_direction = direction;
599             }
600             break;
601 
602         case Format_UYVY:
603             planeIndex     = RENDERHAL_PLANES_UYVY_ADV;
604 
605             // Set up chroma direction
606             m_direction = direction;
607             break;
608 
609         case Format_YVYU:
610             planeIndex     = RENDERHAL_PLANES_YVYU_ADV;
611 
612             // Set up chroma direction
613             m_direction = direction;
614             break;
615 
616         case Format_VYUY:
617             planeIndex     = RENDERHAL_PLANES_VYUY_ADV;
618 
619             // Set up chroma direction
620             m_direction = direction;
621             break;
622 
623         case Format_A8R8G8B8:
624         case Format_X8R8G8B8:
625             if (m_isVaSurface)
626             {
627                 m_width *= 32;
628                 planeIndex     = RENDERHAL_PLANES_Y1;
629             }
630             else
631             {
632                 planeIndex     = RENDERHAL_PLANES_ARGB_ADV;
633             }
634             break;
635 
636         case Format_R8G8SN:
637             if ( m_isVaSurface )
638             {
639                 planeIndex = RENDERHAL_PLANES_Y16S;
640             }
641             break;
642 
643         case Format_V8U8:
644             if ( m_isVaSurface )
645             {
646                 planeIndex = RENDERHAL_PLANES_Y16U;
647             }
648             break;
649 
650         case Format_A8B8G8R8:
651         case Format_X8B8G8R8:
652             planeIndex     = RENDERHAL_PLANES_ABGR_ADV;
653             break;
654 
655         case Format_STMM:
656             planeIndex     = RENDERHAL_PLANES_STMM_ADV;
657             break;
658 
659         case Format_A8:
660         case Format_Buffer_2D:
661             if (m_isVaSurface)
662             {
663                 planeIndex = RENDERHAL_PLANES_Y8;
664             }
665             break;
666 
667         case Format_L8:
668                 planeIndex     = RENDERHAL_PLANES_L8_ADV;
669             break;
670 
671         case Format_A16B16G16R16:
672         case Format_Y416:
673             planeIndex        = RENDERHAL_PLANES_A16B16G16R16_ADV;
674             break;
675 
676         case Format_R10G10B10A2:
677         case Format_Y410:
678             planeIndex        = RENDERHAL_PLANES_R10G10B10A2_ADV;
679             break;
680 
681         case Format_L16:
682         case Format_R16S:
683             if (m_isVaSurface)
684             {
685                 planeIndex = RENDERHAL_PLANES_Y16S;
686             }
687             break;
688 
689         case Format_D16:
690         case Format_R16U:
691             if (m_isVaSurface)
692             {
693                 planeIndex = RENDERHAL_PLANES_Y16U;
694             }
695             break;
696 
697         case Format_P010:
698             if (m_isVme)
699             {
700                 planeIndex = RENDERHAL_PLANES_P010_1PLANE_ADV;
701             }
702             else if (m_cmhal->cmHalInterface->IsP010SinglePassSupported() && (!isRenderOutTarget))
703             {
704                 planeIndex = RENDERHAL_PLANES_P010_1PLANE_ADV;
705                 m_isHalfPitchChroma = false;
706                 m_isInterleaveChrome = true;
707                 m_uYOffset = RenderHal_CalculateYOffset(m_renderhal->pOsInterface, m_resource);
708 
709                 // Set up chroma direction
710                 m_direction = direction;
711             }
712             else
713             {
714                 // Format not supported with AVS - use regular format
715                 MHW_RENDERHAL_NORMALMESSAGE("Format not supported with AVS.");
716                 m_avsUsed = false;
717             }
718             break;
719         case Format_Y210:
720         case Format_Y216:
721             if (m_isVme)
722             {
723                 //Since 422 planar is not supported on application side.
724                 //App is using 422 packed as WA with w=w/2 and h=h*2
725                 m_width = m_width * 2;
726                 m_height = m_height / 2;
727                 planeIndex = RENDERHAL_PLANES_Y210_1PLANE_ADV;
728             }
729             else
730             {
731                 planeIndex = RENDERHAL_PLANES_Y210_ADV;
732             }
733             break;
734         default:
735             // Format not supported with AVS - use regular format
736             MHW_RENDERHAL_NORMALMESSAGE("Format not supported with AVS.");
737 
738             m_avsUsed = false;
739 
740             break;
741     }
742     MHW_RENDERHAL_ASSERT(planeIndex < RENDERHAL_PLANES_DEFINITION_COUNT);
743     return (int)planeIndex;
744 }
745 
GetPlaneDefinitionRender()746 int CmSurfaceState2Dor3D::GetPlaneDefinitionRender()
747 {
748 
749     int planeIndex = -1;
750     bool isRenderOutTarget = false;
751     if ( (m_format == Format_NV12 || m_format == Format_YV12 || m_format == Format_Y210 || m_format == Format_Y216)
752           && (!m_pixelPitch))
753     {
754         isRenderOutTarget = true;
755     }
756 
757     switch (m_format)
758     {
759         case Format_IMC1:
760         case Format_IMC2:
761         case Format_IMC3:
762         case Format_IMC4:
763         case Format_I420:
764         case Format_IYUV:
765         case Format_YVU9:
766             planeIndex = RENDERHAL_PLANES_PL3;
767             break;
768 
769         case Format_YV12:
770             m_isHalfPitchChroma = true;
771 
772             // Y_Uoffset(Height*2 + Height/2) of RENDERHAL_PLANES_YV12 define Bitfield_Range(0, 13) on gen9+.
773             // The max value is 16383. So use PL3 kernel to avoid out of range when Y_Uoffset is larger than 16383.
774             // Use PL3 plane to avoid YV12 U channel shift issue with not 4-aligned height
775             planeIndex = (m_renderhal->bEnableYV12SinglePass &&
776                                (!isRenderOutTarget) &&
777                                MOS_IS_ALIGNED(m_height, 4) &&
778                                (m_height * 2 + m_height / 2) < RENDERHAL_MAX_YV12_PLANE_Y_U_OFFSET_G9)?
779                                RENDERHAL_PLANES_YV12 : RENDERHAL_PLANES_PL3;
780             break;
781 
782         case Format_400P:
783             // Single Y plane here is treated like a NV12 surface.
784             // U and V offsets fall inside this Y plane. Eventhough false UV pixels are
785             // picked by the kernel, CSC coeffecients are such that the effect of these
786             // are nullified.
787             planeIndex = RENDERHAL_PLANES_NV12;
788             break;
789 
790         case Format_P016:
791             planeIndex = RENDERHAL_PLANES_P016;
792             break;
793 
794         case Format_P208:
795             planeIndex = RENDERHAL_PLANES_P208;
796             break;
797 
798         case Format_P010:
799             if (m_renderhal->bEnableP010SinglePass &&
800                 (!isRenderOutTarget))
801             {
802                 planeIndex = RENDERHAL_PLANES_P010_1PLANE;
803             }
804             else
805             {
806                 planeIndex = RENDERHAL_PLANES_P010;
807             }
808             break;
809 
810         case Format_411P:
811             planeIndex = RENDERHAL_PLANES_411P;
812             break;
813 
814         case Format_411R:
815             planeIndex = RENDERHAL_PLANES_411R;
816             break;
817 
818         case Format_422H:
819             planeIndex = RENDERHAL_PLANES_422H;
820             break;
821 
822         case Format_422V:
823             planeIndex = RENDERHAL_PLANES_422V;
824             break;
825 
826         case Format_444P:
827             planeIndex = RENDERHAL_PLANES_444P;
828             break;
829 
830         case Format_RGBP:
831             planeIndex = RENDERHAL_PLANES_RGBP;
832             break;
833 
834         case Format_BGRP:
835             planeIndex = RENDERHAL_PLANES_BGRP;
836             break;
837 
838         case Format_NV12:
839             // On Gen7.5 (Haswell) NV12 format needs a single plane instead
840             // of two (listed in renderhal_g75.c for RENDERHAL_PLANES_NV12),  and
841             // is also expected by the Sampler or Media Kernels. Yet, the
842             // Data Port works with two planes instead. Besides, the Sampler
843             // uses it for input only (as there is no output) while the Data
844             // Port uses it for input as well as output or both for the same
845             // surface. Hence the check added for bWidthInDword_Y &&
846             // bWidthInDword_UV, which are set in vphal_render_3P.c for the
847             // above reason. Two plane NV12 can also be explicitly spcified.
848 
849             // On G8, NV12 format needs the width and Height to be a multiple
850             // of 4 for both 3D sampler and 8x8 sampler; G75 needs the width
851             // of NV12 input surface to be a multiple of 4 for 3D sampler;
852             // G9+ does not has such restriction; to simplify the implementation,
853             // we enable 2 plane NV12 for all of the platform when the width
854             // or Height is not a multiple of 4
855             {
856                 bool is2PlaneNeeded = false;
857                 if (!GFX_IS_GEN_10_OR_LATER(m_renderhal->Platform))
858                 {
859                     is2PlaneNeeded = (!MOS_IS_ALIGNED(m_height, 4) || !MOS_IS_ALIGNED(m_width, 4));
860                 }
861                 else
862                 {
863                     is2PlaneNeeded = (!MOS_IS_ALIGNED(m_height, 4) || !MOS_IS_ALIGNED(m_width, 2));
864                 }
865                 if ( isRenderOutTarget ||
866                      m_isWidthInDWord ||
867                      is2PlaneNeeded)
868                 {
869                     planeIndex = RENDERHAL_PLANES_NV12_2PLANES;
870                 }
871                 else
872                 {
873                     planeIndex = RENDERHAL_PLANES_NV12;
874                 }
875             }
876             break;
877 
878         case Format_YUYV    :
879         case Format_YUY2    :
880             planeIndex = RENDERHAL_PLANES_YUY2;
881             break;
882 
883         case Format_G8R8_G8B8:
884         case Format_UYVY     :
885             planeIndex = RENDERHAL_PLANES_UYVY;
886             break;
887 
888         case Format_YVYU:
889             planeIndex = RENDERHAL_PLANES_YVYU;
890             break;
891 
892         case Format_VYUY:
893             planeIndex = RENDERHAL_PLANES_VYUY;
894             break;
895 
896         case Format_A8R8G8B8:
897             planeIndex = RENDERHAL_PLANES_ARGB;
898             break;
899 
900         case Format_R32U:
901             planeIndex = RENDERHAL_PLANES_R32U;
902             break;
903 
904         case Format_R32S:
905             planeIndex = RENDERHAL_PLANES_R32S;
906             break;
907 
908         case Format_R32F:
909         case Format_D32F:
910         case Format_R32:
911             planeIndex = RENDERHAL_PLANES_R32F;
912             break;
913 
914         case Format_Y8:
915             planeIndex = RENDERHAL_PLANES_Y8;
916             break;
917 
918         case Format_Y1:
919             planeIndex = RENDERHAL_PLANES_Y1;
920             break;
921 
922         case Format_Y16U:
923             planeIndex = RENDERHAL_PLANES_Y16U;
924             break;
925 
926         case Format_Y16S:
927             planeIndex = RENDERHAL_PLANES_Y16S;
928             break;
929 
930         case Format_R8G8SN:
931         case Format_V8U8:
932             planeIndex = RENDERHAL_PLANES_V8U8;
933             break;
934 
935         case Format_R16U:
936             planeIndex = RENDERHAL_PLANES_R16U;
937             break;
938 
939         case Format_R16S:
940             planeIndex = RENDERHAL_PLANES_R16S;
941             break;
942 
943         case Format_R8G8UN:
944             planeIndex = RENDERHAL_PLANES_R8G8_UNORM;
945             break;
946 
947         case Format_X8R8G8B8:
948             // h/w doesn't support XRGB render target
949             planeIndex =
950                 (m_isRenderTarget) ? RENDERHAL_PLANES_ARGB : RENDERHAL_PLANES_XRGB;
951             break;
952 
953         case Format_A8B8G8R8:
954             planeIndex = RENDERHAL_PLANES_ABGR;
955             break;
956 
957         case Format_X8B8G8R8:
958             // h/w doesn't support XBGR render target
959             planeIndex =
960                 (m_isRenderTarget) ? RENDERHAL_PLANES_ABGR : RENDERHAL_PLANES_XBGR;
961             break;
962 
963         case Format_R5G6B5:
964             planeIndex = RENDERHAL_PLANES_RGB16;
965             break;
966 
967         case Format_R8G8B8:
968             planeIndex = RENDERHAL_PLANES_RGB24;
969             break;
970 
971         case Format_AYUV    :
972             planeIndex = RENDERHAL_PLANES_AYUV;
973             break;
974 
975         case Format_AI44    :
976             planeIndex = (m_paletteID == 0) ?
977                                             RENDERHAL_PLANES_AI44_PALLETE_0 :
978                                             RENDERHAL_PLANES_AI44_PALLETE_1;
979             break;
980 
981         case Format_IA44:
982             planeIndex = (m_paletteID == 0) ?
983                                             RENDERHAL_PLANES_IA44_PALLETE_0 :
984                                             RENDERHAL_PLANES_IA44_PALLETE_1;
985             break;
986 
987         case Format_P8:
988             planeIndex = (m_paletteID == 0) ?
989                                             RENDERHAL_PLANES_P8_PALLETE_0 :
990                                             RENDERHAL_PLANES_P8_PALLETE_1;
991             break;
992 
993         case Format_A8P8:
994             planeIndex = (m_paletteID == 0) ?
995                                             RENDERHAL_PLANES_A8P8_PALLETE_0 :
996                                             RENDERHAL_PLANES_A8P8_PALLETE_1;
997             break;
998 
999         case Format_STMM:
1000             planeIndex = RENDERHAL_PLANES_STMM;
1001             break;
1002 
1003         case Format_L8:
1004             planeIndex = RENDERHAL_PLANES_L8;
1005             break;
1006 
1007         case Format_A8:
1008         case Format_Buffer_2D:
1009             planeIndex = RENDERHAL_PLANES_A8;
1010             break;
1011 
1012         case Format_R8U:
1013         case Format_R8UN:
1014             planeIndex = RENDERHAL_PLANES_R8;
1015             break;
1016 
1017         case Format_R16UN:
1018         case Format_D16:
1019         case Format_R16:
1020             planeIndex = RENDERHAL_PLANES_R16_UNORM;
1021             break;
1022 
1023         case Format_A16B16G16R16:
1024             planeIndex = RENDERHAL_PLANES_A16B16G16R16;
1025             break;
1026         case Format_Y416:
1027             if (isRenderOutTarget)
1028             {
1029                 planeIndex = RENDERHAL_PLANES_Y416_RT;
1030             }
1031             else
1032             {
1033                 planeIndex = RENDERHAL_PLANES_A16B16G16R16;
1034             }
1035             break;
1036         case Format_A16B16G16R16F:
1037             planeIndex = RENDERHAL_PLANES_A16B16G16R16F;
1038             break;
1039         case Format_A16R16G16B16F:
1040             planeIndex = RENDERHAL_PLANES_A16R16G16B16F;
1041             break;
1042         case Format_R32G32B32A32F:
1043             planeIndex = RENDERHAL_PLANES_R32G32B32A32F;
1044             break;
1045         case Format_NV21:
1046             planeIndex = RENDERHAL_PLANES_NV21;
1047             break;
1048         case Format_L16:
1049             planeIndex = RENDERHAL_PLANES_L16;
1050             break;
1051 
1052         case Format_R10G10B10A2:
1053         case Format_Y410:
1054             planeIndex = RENDERHAL_PLANES_R10G10B10A2;
1055             break;
1056 
1057         case Format_Y210:
1058         case Format_Y216:
1059         {
1060             RENDERHAL_PLANE_DEFINITION temp = RENDERHAL_PLANES_DEFINITION_COUNT;
1061             m_renderhal->pRenderHalPltInterface->GetPlaneDefForFormatY216(
1062                 isRenderOutTarget,
1063                 m_renderhal,
1064                 temp);
1065             planeIndex = temp;
1066         }
1067             break;
1068 
1069         case Format_B10G10R10A2:
1070             planeIndex = RENDERHAL_PLANES_B10G10R10A2;
1071             break;
1072 
1073         case Format_IRW0:
1074             planeIndex = RENDERHAL_PLANES_IRW0;
1075             break;
1076 
1077         case Format_IRW1:
1078             planeIndex = RENDERHAL_PLANES_IRW1;
1079             break;
1080 
1081         case Format_IRW2:
1082             planeIndex = RENDERHAL_PLANES_IRW2;
1083             break;
1084 
1085         case Format_IRW3:
1086             planeIndex = RENDERHAL_PLANES_IRW3;
1087             break;
1088 
1089         case Format_R16G16UN:
1090             planeIndex = RENDERHAL_PLANES_R16G16_UNORM;
1091             break;
1092 
1093         case Format_R16G16S:
1094             planeIndex = RENDERHAL_PLANES_R16G16_SINT;
1095             break;
1096 
1097         case Format_R16F:
1098             planeIndex = RENDERHAL_PLANES_R16_FLOAT;
1099             break;
1100 
1101         case Format_R24G8:
1102         case Format_D24S8UN:
1103             planeIndex = RENDERHAL_PLANES_R24_UNORM_X8_TYPELESS;
1104             break;
1105 
1106         case Format_R32G8X24:
1107         case Format_D32S8X24_FLOAT:
1108             planeIndex = RENDERHAL_PLANES_R32_FLOAT_X8X24_TYPELESS;
1109             break;
1110 
1111         default:
1112             return -1;
1113     }
1114 
1115     // Get plane definitions
1116     MHW_RENDERHAL_ASSERT(planeIndex < RENDERHAL_PLANES_DEFINITION_COUNT);
1117     return (int)planeIndex;
1118 }
1119 
GetDirection()1120 uint8_t CmSurfaceState2Dor3D::GetDirection()
1121 {
1122     if(GFX_IS_GEN_9_OR_LATER(m_renderhal->Platform))
1123     {
1124         // Gen9+
1125         uint8_t direction;
1126         if (m_chromaSitting & MHW_CHROMA_SITING_HORZ_CENTER)
1127         {
1128             direction = CHROMA_SITING_UDIRECTION_CENTER;
1129         }
1130         else
1131         {
1132             direction = CHROMA_SITING_UDIRECTION_LEFT;
1133         }
1134         direction = direction << 3;
1135 
1136         if (m_chromaSitting & MHW_CHROMA_SITING_VERT_TOP)
1137         {
1138             direction |= CHROMA_SITING_VDIRECTION_0;
1139         }
1140         else if (m_chromaSitting & MHW_CHROMA_SITING_VERT_BOTTOM)
1141         {
1142             direction |= CHROMA_SITING_VDIRECTION_1;
1143         }
1144         else
1145         {
1146             direction |= CHROMA_SITING_VDIRECTION_1_2;
1147         }
1148         return direction;
1149     }
1150     else
1151     {
1152         return MEDIASTATE_VDIRECTION_FULL_FRAME;
1153     }
1154 }
1155 
SetPerPlaneParam()1156 MOS_STATUS CmSurfaceState2Dor3D::SetPerPlaneParam()
1157 {
1158     int planeIndex = -1;
1159     if (m_avsUsed)
1160     {
1161         planeIndex = GetPlaneDefinitionMedia();
1162     }
1163     else
1164     {
1165         planeIndex = GetPlaneDefinitionRender();
1166     }
1167     CM_CHK_COND_RETURN((planeIndex == -1), MOS_STATUS_UNKNOWN, "Cannot find the plane definition.");
1168 
1169     bool isWidthInDword = m_avsUsed?false:(m_pixelPitch?false:true);
1170     m_numPlane = g_cRenderHal_SurfacePlanes[planeIndex].dwNumPlanes;
1171     PCMHW_PLANE_SETTING plane = g_cRenderHal_SurfacePlanes[planeIndex].Plane;
1172 
1173     uint32_t alignUnitWidth = 1;
1174     uint32_t alignUnitHeight = 1;
1175     if (m_format == Format_YUY2
1176         || m_format == Format_UYVY
1177         || m_format == Format_YVYU
1178         || m_format == Format_VYUY
1179         || m_format == Format_P208)
1180     {
1181         alignUnitHeight = 2;
1182     }
1183 
1184     for (uint32_t idx = 0; idx < m_numPlane; idx ++, plane ++)
1185     {
1186         uint32_t adjustedWidth = MOS_ALIGN_CEIL(m_width, alignUnitWidth);
1187         uint32_t adjustedHeight = MOS_ALIGN_CEIL(m_height, alignUnitHeight);
1188         adjustedHeight = (adjustedHeight + plane->ui8ScaleHeight - 1) / plane->ui8ScaleHeight;
1189         adjustedWidth  = adjustedWidth  / plane->ui8ScaleWidth;
1190 
1191         if (m_isWidthInDWord)
1192         {
1193             if (planeIndex == RENDERHAL_PLANES_R32G32B32A32F)
1194             {
1195                 adjustedWidth = adjustedWidth << 2;
1196             }
1197             else if(planeIndex == RENDERHAL_PLANES_A16B16G16R16 ||
1198                 planeIndex == RENDERHAL_PLANES_A16B16G16R16_ADV ||
1199                 planeIndex == RENDERHAL_PLANES_A16B16G16R16F    ||
1200                 planeIndex == RENDERHAL_PLANES_A16R16G16B16F    ||
1201                 planeIndex == RENDERHAL_PLANES_Y210_RT          ||
1202                 planeIndex == RENDERHAL_PLANES_Y416_RT          ||
1203                 planeIndex == RENDERHAL_PLANES_R32_FLOAT_X8X24_TYPELESS)
1204             {
1205                 adjustedWidth = adjustedWidth << 1;
1206             }
1207             else
1208             {
1209                 adjustedWidth = (adjustedWidth + plane->ui8PixelsPerDword - 1) /
1210                                                         plane->ui8PixelsPerDword;
1211             }
1212         }
1213 
1214         if ((!m_isVme) && m_frameType != CM_FRAME)
1215         {
1216             adjustedHeight /= 2;
1217             adjustedHeight = MOS_MAX(adjustedHeight, 1);
1218         }
1219 
1220         adjustedHeight = MOS_ALIGN_FLOOR(adjustedHeight, plane->ui8AlignHeight);
1221         adjustedWidth  = MOS_ALIGN_FLOOR(adjustedWidth, plane->ui8AlignWidth);
1222 
1223         m_planeParams[idx].planeID = plane->ui8PlaneID;
1224         m_planeParams[idx].format = plane->dwFormat;
1225         m_planeParams[idx].width = adjustedWidth;
1226         m_planeParams[idx].height= adjustedHeight;
1227         if (plane->ui8PlaneID == MHW_U_PLANE ||
1228             plane->ui8PlaneID == MHW_V_PLANE)
1229         {
1230             if (m_format == Format_I420 ||
1231                 m_format == Format_IYUV ||
1232                 m_format == Format_YV12 ||
1233                 m_format == Format_NV11)
1234             {
1235                 m_planeParams[idx].pitch = (m_pitch>>1);
1236             }
1237             else if (m_format == Format_YVU9)
1238             {
1239                 m_planeParams[idx].pitch = (m_pitch>>2);
1240             }
1241             else
1242             {
1243                 m_planeParams[idx].pitch = m_pitch;
1244             }
1245         }
1246         else
1247         {
1248             m_planeParams[idx].pitch = m_pitch;
1249         }
1250         m_planeParams[idx].isAvs = plane->bAdvanced;
1251         m_planeParams[idx].xoffset = m_xOffsets[idx] + m_surfaceXOffset;
1252         if (m_format == Format_NV12 && idx == 1)
1253         {
1254             m_planeParams[idx].yoffset = m_yOffsets[idx] + m_surfaceYOffset/2;
1255         }
1256         else
1257         {
1258             m_planeParams[idx].yoffset = m_yOffsets[idx] + m_surfaceYOffset;
1259         }
1260     }
1261 
1262     return MOS_STATUS_SUCCESS;
1263 }
1264 
1265 
UpdateSurfaceState()1266 MOS_STATUS CmSurfaceState2Dor3D::UpdateSurfaceState()
1267 {
1268     // get the number of planes and plane types: y, u, v
1269     SetPerPlaneParam();
1270 
1271     // Set Surface States
1272     MHW_SURFACE_STATE_PARAMS SurfStateParams;
1273     for (uint32_t idx = 0; idx < m_numPlane; idx ++)
1274     {
1275         MOS_ZeroMemory(&SurfStateParams, sizeof(SurfStateParams));
1276 
1277         SurfStateParams.pSurfaceState         = m_cmds + idx * m_maxStateSize;
1278         SurfStateParams.bUseAdvState          = m_planeParams[idx].isAvs;
1279         SurfStateParams.dwWidth               = m_planeParams[idx].width;
1280         SurfStateParams.dwHeight              = m_planeParams[idx].height;
1281         SurfStateParams.dwFormat              = m_planeParams[idx].format;
1282         SurfStateParams.dwPitch               = m_planeParams[idx].pitch;
1283         SurfStateParams.dwQPitch              = m_qPitch;
1284         SurfStateParams.bTiledSurface         = (m_tile != MOS_TILE_LINEAR)?1:0;
1285         SurfStateParams.bTileWalk             = IS_Y_MAJOR_TILE_FORMAT(m_tile)
1286                                                 ? GFX3DSTATE_TILEWALK_YMAJOR
1287                                                 : GFX3DSTATE_TILEWALK_XMAJOR;;
1288         SurfStateParams.TileModeGMM           = m_tileModeGMM;
1289         SurfStateParams.bGMMTileEnabled       = m_bGMMTileEnabled;
1290         SurfStateParams.dwCacheabilityControl = GetCacheabilityControl();
1291 
1292         if (m_cmhal->platform.eRenderCoreFamily <= IGFX_GEN11_CORE)
1293         {
1294             SurfStateParams.bCompressionEnabled   = m_isCompressed;
1295             SurfStateParams.bCompressionMode      = (m_compressionMode == MOS_MMC_VERTICAL) ? 1 : 0;
1296         }
1297         else
1298         {
1299             if (IsFormatMMCSupported(m_format) && m_renderhal->isMMCEnabled)
1300             {
1301                 // bCompressionEnabled/bCompressionMode is deprecated, use MmcState instead.
1302                 if ((!m_cmhal->cmHalInterface->SupportCompressedOutput()) &&
1303                     (m_mmcState == MOS_MEMCOMP_RC && m_isRenderTarget))
1304                 {
1305                     // RC compression mode is not supported on render output surface.
1306                     SurfStateParams.MmcState = MOS_MEMCOMP_DISABLED;
1307                     m_mmcState = MOS_MEMCOMP_DISABLED;
1308                     SurfStateParams.dwCompressionFormat = 0;
1309                 }
1310                 else if (m_mmcState == MOS_MEMCOMP_MC ||
1311                      m_mmcState == MOS_MEMCOMP_RC)
1312                 {
1313                     SurfStateParams.MmcState = m_mmcState;
1314 
1315                     if (m_planeParams[idx].planeID == MHW_U_PLANE &&
1316                        (m_format == Format_NV12 || m_format == Format_P010 || m_format == Format_P016))
1317                     {
1318                         SurfStateParams.dwCompressionFormat = (uint32_t)(0x00000010) | (m_compressionFormat & 0x0f);
1319                     }
1320                     else if ((m_format == Format_R8G8UN) && (m_mmcState == MOS_MEMCOMP_MC))
1321                     {
1322                         /* it will be an issue if the R8G8UN surface with MC enable
1323                            is not chroma plane from NV12 surface, so far there is no
1324                            such case
1325                         */
1326                         SurfStateParams.dwCompressionFormat = (uint32_t)(0x00000010) | (m_compressionFormat & 0x0f);
1327                     }
1328                     else
1329                     {
1330                         SurfStateParams.dwCompressionFormat = m_compressionFormat & 0x1f;
1331                     }
1332                 }
1333                 else
1334                 {
1335                     MHW_RENDERHAL_NORMALMESSAGE("Unsupported Compression Mode for Render Engine.");
1336                     SurfStateParams.MmcState            = MOS_MEMCOMP_DISABLED;
1337                     m_mmcState = MOS_MEMCOMP_DISABLED;
1338                     SurfStateParams.dwCompressionFormat = 0;
1339                 }
1340             }
1341         }
1342 
1343         // update if is sampler
1344         if (m_pixelPitch || (m_avsUsed && (!m_isVme)))
1345         {
1346             SurfStateParams.RotationMode          = g_cLookup_RotationMode[m_rotation];
1347         }
1348 
1349         if (m_planeParams[idx].isAvs)
1350         {
1351 
1352             SurfStateParams.bHalfPitchChroma        = m_isHalfPitchChroma;
1353             SurfStateParams.bInterleaveChroma       = m_isInterleaveChrome;
1354             SurfStateParams.UVPixelOffsetUDirection = m_direction >> 0x3;
1355             SurfStateParams.UVPixelOffsetVDirection = m_direction & 0x7;
1356 
1357             if (m_planeParams[idx].planeID == MHW_U_PLANE)         // AVS U plane
1358             {
1359                 // Lockoffset is the offset from base address of Y plane to the origin of U/V plane.
1360                 // So, We can get XOffsetforU by Lockoffset % pSurface->dwPitch, and get YOffsetForU by Lockoffset / pSurface->dwPitch
1361                 SurfStateParams.dwXOffsetForU = (uint32_t)m_lockOffsets[1] % m_pitch;
1362                 SurfStateParams.dwYOffsetForU = (uint32_t)m_lockOffsets[1] / m_pitch;
1363                 SurfStateParams.dwXOffsetForV = 0;
1364                 SurfStateParams.dwYOffsetForV = 0;
1365                 SurfStateParams.iXOffset      = m_xOffsets[1];
1366                 SurfStateParams.iYOffset      = m_yOffsets[1];
1367             }
1368             else if (m_planeParams[idx].planeID == MHW_V_PLANE)    // AVS V plane
1369             {
1370                 SurfStateParams.dwXOffsetForU = 0;
1371                 SurfStateParams.dwYOffsetForU = 0;
1372                 SurfStateParams.dwXOffsetForV = (uint32_t)m_lockOffsets[2] % m_pitch;
1373                 SurfStateParams.dwYOffsetForV = (uint32_t)m_lockOffsets[2] / m_pitch;
1374                 SurfStateParams.iXOffset      = m_xOffsets[2];
1375                 SurfStateParams.iYOffset      = m_yOffsets[2];
1376             }
1377             else // AVS/DNDI Y plane
1378             {
1379                 SurfStateParams.dwXOffsetForU = m_uXOffset;
1380                 SurfStateParams.dwYOffsetForU = m_uYOffset;
1381                 SurfStateParams.dwXOffsetForV = m_vXOffset;
1382                 SurfStateParams.dwYOffsetForV = m_vYOffset;
1383                 SurfStateParams.iXOffset      = 0;
1384                 SurfStateParams.iYOffset      = m_yOffsets[0];
1385             }
1386         }
1387         else
1388         {
1389             SurfStateParams.SurfaceType3D             = (m_depth > 1) ?
1390                                                            GFX3DSTATE_SURFACETYPE_3D :
1391                                                            GFX3DSTATE_SURFACETYPE_2D;
1392             SurfStateParams.dwDepth                   = MOS_MAX(1, m_depth);
1393             SurfStateParams.bVerticalLineStrideOffset = (m_frameType == CM_BOTTOM_FIELD)?1:0;
1394             SurfStateParams.bVerticalLineStride       = (m_frameType == CM_FRAME)?0:1;
1395             SurfStateParams.bHalfPitchChroma          = m_isHalfPitchChroma;
1396 
1397             // Setup surface g9 surface state
1398             if (m_planeParams[idx].planeID == MHW_U_PLANE ||
1399                 m_planeParams[idx].planeID == MHW_V_PLANE)
1400             {
1401                 uint32_t pixelPerSampleUV = 1;
1402                 if (m_isWidthInDWord)
1403                 {
1404                     RenderHal_GetPixelsPerSample(m_format, &pixelPerSampleUV);
1405                 }
1406                 if(pixelPerSampleUV == 1)
1407                 {
1408                     SurfStateParams.iXOffset = m_planeParams[idx].xoffset;
1409                 }
1410                 else
1411                 {
1412                     SurfStateParams.iXOffset = m_planeParams[idx].xoffset/sizeof(uint32_t);
1413                 }
1414                 SurfStateParams.iYOffset = m_planeParams[idx].yoffset;
1415             }
1416             else // Y plane
1417             {
1418                 SurfStateParams.iXOffset = m_planeParams[idx].xoffset/sizeof(uint32_t);
1419                 SurfStateParams.iYOffset = m_planeParams[idx].yoffset;
1420 
1421                 if(m_planeParams[idx].format == MHW_GFX3DSTATE_SURFACEFORMAT_PLANAR_420_8)
1422                 {
1423                     if (m_format == Format_YV12)
1424                     {
1425                         SurfStateParams.bSeperateUVPlane = true;
1426                         SurfStateParams.dwXOffsetForU    = 0;
1427                         SurfStateParams.dwYOffsetForU    = m_height * 2 + m_height/2;
1428                         SurfStateParams.dwXOffsetForV    = 0;
1429                         SurfStateParams.dwYOffsetForV    = m_height * 2;
1430                     }
1431                     else
1432                     {
1433                         SurfStateParams.bSeperateUVPlane = false;
1434                         SurfStateParams.dwXOffsetForU    = 0;
1435                         SurfStateParams.dwYOffsetForU    = m_surfOffsets[1]/m_pitch + m_yOffsets[1];
1436                         SurfStateParams.dwXOffsetForV    = 0;
1437                         SurfStateParams.dwYOffsetForV    = 0;
1438                     }
1439                 }
1440             }
1441         }
1442         MHW_CHK_STATUS_RETURN(m_renderhal->pMhwStateHeap->SetSurfaceStateEntry(&SurfStateParams));
1443     }
1444 
1445     return MOS_STATUS_SUCCESS;
1446 }
1447 
CmSurfaceStateBuffer(CM_HAL_STATE * cmhal)1448 CmSurfaceStateBuffer::CmSurfaceStateBuffer(CM_HAL_STATE *cmhal):
1449     CmSurfaceState(cmhal),
1450     m_size(0),
1451     m_offset(0)
1452 {
1453     MOS_ZeroMemory(m_cmds, sizeof(m_cmds));
1454 }
1455 
Initialize(MOS_RESOURCE * resource,uint32_t size)1456 CM_RETURN_CODE CmSurfaceStateBuffer::Initialize(MOS_RESOURCE *resource, uint32_t size)
1457 {
1458     CmSurfaceState::Initialize(resource);
1459     m_size = size;
1460     return CM_SUCCESS;
1461 }
1462 
GenerateSurfaceState(CM_HAL_BUFFER_SURFACE_STATE_ENTRY * param)1463 MOS_STATUS CmSurfaceStateBuffer::GenerateSurfaceState(CM_HAL_BUFFER_SURFACE_STATE_ENTRY *param)
1464 {
1465     if (param)
1466     {
1467         m_size = param->surfaceStateSize?param->surfaceStateSize:m_size;
1468         m_offset = param->surfaceStateOffset?param->surfaceStateOffset:m_offset;
1469         m_memoryObjectControl = param->surfaceStateMOCS?param->surfaceStateMOCS:m_memoryObjectControl;
1470     }
1471 
1472     MHW_SURFACE_STATE_PARAMS params;
1473     MOS_ZeroMemory(&params, sizeof(params));
1474 
1475     uint32_t bufferSize = m_size - 1;
1476     params.SurfaceType3D = GFX3DSTATE_SURFACETYPE_BUFFER;
1477     // Width  contains bits [ 6:0] of the number of entries in the buffer
1478     params.dwWidth = (uint8_t)(bufferSize & MOS_MASKBITS32(0, 6));
1479     // Height contains bits [20:7] of the number of entries in the buffer
1480     params.dwHeight = (uint16_t)((bufferSize & MOS_MASKBITS32(7, 20)) >> 7);
1481     // For SURFTYPE_BUFFER, this field indicates the size of the structure
1482     params.dwPitch = 0;
1483 
1484     uint32_t depthMaskRawBuffer = m_renderhal->pRenderHalPltInterface->GetDepthBitMaskForRawBuffer();
1485     params.dwFormat = MHW_GFX3DSTATE_SURFACEFORMAT_RAW;
1486     params.dwDepth = (uint16_t)((bufferSize & depthMaskRawBuffer) >> 21);
1487     params.dwCacheabilityControl = GetCacheabilityControl();
1488     params.pSurfaceState = m_cmds;
1489 
1490     // Default tile mode of surface state buffer is linear
1491     params.bGMMTileEnabled = true;
1492 
1493     MHW_CHK_STATUS_RETURN(m_renderhal->pMhwStateHeap->SetSurfaceStateEntry(&params));
1494 
1495     return MOS_STATUS_SUCCESS;
1496 }
1497 
1498 
CmSurfaceStateVME(CM_HAL_STATE * cmhal)1499 CmSurfaceStateVME::CmSurfaceStateVME(CM_HAL_STATE *cmhal):
1500     CmSurfaceState(cmhal),
1501     m_numBte(0),
1502     m_forwardCount(0),
1503     m_backwardCount(0),
1504     m_curIndex(CM_NULL_SURFACE),
1505     m_forwardIndexes(nullptr),
1506     m_backwardIndexes(nullptr)
1507 {
1508     MOS_ZeroMemory(&m_surf2DParam, sizeof(m_surf2DParam));
1509     MOS_ZeroMemory(m_offsets, sizeof(m_offsets));
1510     MOS_ZeroMemory(m_mmcStates, sizeof(m_mmcStates));
1511 }
1512 
Initialize(CM_HAL_VME_ARG_VALUE * vmeArg)1513 CM_RETURN_CODE CmSurfaceStateVME::Initialize(CM_HAL_VME_ARG_VALUE *vmeArg)
1514 {
1515     CmSurfaceState::Initialize(nullptr);
1516     m_forwardCount = vmeArg->fwRefNum;
1517     m_backwardCount = vmeArg->bwRefNum;
1518     m_curIndex = vmeArg->curSurface;
1519     m_forwardIndexes = findFwRefInVmeArg(vmeArg);
1520     m_backwardIndexes = findBwRefInVmeArg(vmeArg);
1521     uint32_t numPair = m_forwardCount>m_backwardCount?m_forwardCount:m_backwardCount;
1522     m_numBte = 2*numPair + 1;
1523 
1524     m_surf2DParam.width = vmeArg->surfStateParam.surfaceStateWidth;
1525     m_surf2DParam.height= vmeArg->surfStateParam.surfaceStateHeight;
1526 
1527     return CM_SUCCESS;
1528 }
1529 
GetSurfaceState(int index)1530 uint8_t *CmSurfaceStateVME::GetSurfaceState(int index)
1531 {
1532     int surfIndex = GetCmHalSurfaceIndex(index);
1533     if (surfIndex == -1)
1534         return nullptr;
1535 
1536     CmSurfaceState2Dor3DMgr *surfStateMgr = m_cmhal->umdSurf2DTable[surfIndex].surfStateMgr;
1537     CmSurfaceState *surfState = nullptr;
1538     if (m_surf2DParam.width == 0 && m_surf2DParam.height == 0)
1539     {
1540         surfState = surfStateMgr->GetSurfaceState(1);
1541     }
1542     else
1543     {
1544         surfState = surfStateMgr->GetSurfaceState(1, 0, &m_surf2DParam);
1545     }
1546     CM_CHK_NULL_RETURN(surfState, nullptr);
1547     m_offsets[index]=surfState->GetSurfaceOffset(0);
1548     m_mmcStates[index]=surfState->GetMmcState(0);
1549     return surfState->GetSurfaceState(0); //in vme every surface has only one plane
1550 }
1551 
GetResource(uint32_t index)1552 MOS_RESOURCE *CmSurfaceStateVME::GetResource(uint32_t index)
1553 {
1554     int surfIndex = GetCmHalSurfaceIndex(index);
1555     if (surfIndex == -1)
1556         return nullptr;
1557     return m_cmhal->umdSurf2DTable[surfIndex].surfStateMgr->GetResource();
1558 }
1559 
GetCmHalSurfaceIndex(uint32_t index)1560 int CmSurfaceStateVME::GetCmHalSurfaceIndex(uint32_t index)
1561 {
1562     uint32_t surfIndex;
1563     if (index == 0) // current surface
1564     {
1565         surfIndex = m_curIndex;
1566     }
1567     else if (index%2 == 1) // forward references
1568     {
1569         if ((index - 1)/2 >= m_forwardCount)
1570         {
1571             return -1;
1572         }
1573         surfIndex = m_forwardIndexes[(index - 1)/2];
1574     }
1575     else // backward references
1576     {
1577         if ((index - 1)/2 >= m_backwardCount)
1578         {
1579             return -1;
1580         }
1581         surfIndex = m_backwardIndexes[(index - 2)/2];
1582     }
1583     return (int)surfIndex;
1584 
1585 }
1586 
1587