xref: /aosp_15_r20/external/intel-media-driver/media_driver/agnostic/common/cm/cm_surface_sampler.cpp (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2017, 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_sampler.cpp
24 //! \brief     Contains Class CmSurfaceSampler definitions
25 //!
26 
27 #include "cm_surface_sampler.h"
28 
29 #include "cm_surface_manager.h"
30 #include "cm_device_rt.h"
31 #include "cm_hal.h"
32 
33 namespace CMRT_UMD
34 {
Create(uint32_t index,uint32_t handleFor2D3D,uint32_t indexForCurrent,SAMPLER_SURFACE_TYPE surfaceType,CmSurfaceManager * surfaceManager,CmSurfaceSampler * & surfaceSampler,CM_FLAG * flag)35 int32_t CmSurfaceSampler::Create(
36     uint32_t index,                //SurfaceSampler's index in surface array
37     uint32_t handleFor2D3D,        //indexing resource array of HalCm
38     uint32_t indexForCurrent,      //SurfaceIndex's ID for 2D/3D, also indexing surface array
39     SAMPLER_SURFACE_TYPE surfaceType,
40     CmSurfaceManager* surfaceManager,
41     CmSurfaceSampler* &surfaceSampler,
42     CM_FLAG* flag)
43 {
44     int32_t result = CM_SUCCESS;
45 
46     surfaceSampler = new (std::nothrow) CmSurfaceSampler( indexForCurrent, handleFor2D3D, surfaceType, surfaceManager, flag);
47     if( surfaceSampler )
48     {
49         result = surfaceSampler->Initialize( index );
50         if( result != CM_SUCCESS )
51         {
52             CmSurface* baseSurface = surfaceSampler;
53             CmSurface::Destroy( baseSurface );
54         }
55     }
56     else
57     {
58         CM_ASSERTMESSAGE("Error: Failed to create CmSurfaceSampler due to out of system memory.");
59         result = CM_OUT_OF_HOST_MEMORY;
60     }
61 
62     return result;
63 }
64 
CmSurfaceSampler(uint32_t indexForCurrent,uint32_t handleFor2D3D,SAMPLER_SURFACE_TYPE surfaceType,CmSurfaceManager * surfaceManager,CM_FLAG * flag)65 CmSurfaceSampler::CmSurfaceSampler(
66     uint32_t indexForCurrent,
67     uint32_t handleFor2D3D,
68     SAMPLER_SURFACE_TYPE surfaceType,
69     CmSurfaceManager* surfaceManager,
70     CM_FLAG* flag):
71     CmSurface(surfaceManager,false ),
72     m_handleFor2D3D( handleFor2D3D),
73     m_indexForCurrent(indexForCurrent),
74     m_surfaceType(surfaceType)
75 {
76     if (flag != nullptr)
77     {
78         m_flag.rotationFlag = flag->rotationFlag;
79         m_flag.chromaSiting = flag->chromaSiting;
80     }
81 }
82 
~CmSurfaceSampler(void)83 CmSurfaceSampler::~CmSurfaceSampler( void )
84 {
85 }
86 
Initialize(uint32_t index)87 int32_t CmSurfaceSampler::Initialize( uint32_t index )
88 {
89     CmSurfaceManager* surfMgr = m_surfaceMgr;
90     surfMgr->UpdateSurface2DTableRotation(m_handleFor2D3D, m_flag.rotationFlag);
91     return CmSurface::Initialize( index );
92 }
93 
GetSurfaceIndex(SurfaceIndex * & index)94 int32_t CmSurfaceSampler::GetSurfaceIndex( SurfaceIndex*& index )
95 {
96     index = m_index;
97     return CM_SUCCESS;
98 }
99 
GetHandle(uint32_t & handle)100 int32_t CmSurfaceSampler::GetHandle( uint32_t& handle )
101 {
102     handle = m_handleFor2D3D;
103     return CM_SUCCESS;
104 }
105 
GetSurfaceType(SAMPLER_SURFACE_TYPE & type)106 int32_t CmSurfaceSampler::GetSurfaceType(SAMPLER_SURFACE_TYPE& type)
107 {
108     type = m_surfaceType;
109     return CM_SUCCESS;
110 }
111 
GetCmIndexCurrent(uint16_t & index)112 int32_t CmSurfaceSampler::GetCmIndexCurrent( uint16_t & index )
113 {
114     index = (uint16_t)m_indexForCurrent;
115     return CM_SUCCESS;
116 }
117 
SetMemoryObjectControl(MEMORY_OBJECT_CONTROL memCtrl,MEMORY_TYPE memType,uint32_t age)118 int32_t CmSurfaceSampler::SetMemoryObjectControl( MEMORY_OBJECT_CONTROL memCtrl, MEMORY_TYPE memType, uint32_t age)
119 {
120     CM_RETURN_CODE  hr = CM_SUCCESS;
121     uint16_t mocs = 0;
122 
123     CmSurface::SetMemoryObjectControl( memCtrl, memType, age );
124 
125     CmDeviceRT *cmDevice = nullptr;
126     m_surfaceMgr->GetCmDevice(cmDevice);
127     CM_CHK_NULL_RETURN_CMERROR(cmDevice);
128     PCM_CONTEXT_DATA cmData = (PCM_CONTEXT_DATA)cmDevice->GetAccelData();
129     CM_CHK_NULL_RETURN_CMERROR(cmData);
130     CM_CHK_NULL_RETURN_CMERROR(cmData->cmHalState);
131 
132     mocs = (m_memObjCtrl.mem_ctrl << 8) | (m_memObjCtrl.mem_type<<4) | m_memObjCtrl.age;
133 
134     CM_ARG_KIND argType;
135 
136     if (m_surfaceType == SAMPLER_SURFACE_TYPE_2D)
137     {
138         argType = ARG_KIND_SURFACE_SAMPLER;
139     }
140     else if (m_surfaceType == SAMPLER_SURFACE_TYPE_2DUP)
141     {
142         argType = ARG_KIND_SURFACE2DUP_SAMPLER;
143     }
144     else
145     {
146         argType = ARG_KIND_SURFACE_3D;
147     }
148 
149     CM_CHK_MOSSTATUS_GOTOFINISH_CMERROR(cmData->cmHalState->pfnSetSurfaceMOCS(cmData->cmHalState, m_handleFor2D3D, mocs, argType));
150 
151 finish:
152     return hr;
153 }
154 }
155