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 //!
24 //! \file     media_scalability.cpp
25 //! \brief    Defines the common interface for media scalability
26 //! \details  The media scalability interface is further sub-divided by component,
27 //!           this file is for the base interface which is shared by all components.
28 //!
29 
30 #include "media_scalability.h"
31 #include "mos_os_virtualengine.h"
32 
MediaScalability(MediaContext * mediaContext)33 MediaScalability::MediaScalability(MediaContext *mediaContext) :
34     m_mediaContext(mediaContext)
35 {
36     if (m_mediaContext == nullptr)
37     {
38         SCALABILITY_ASSERTMESSAGE("mediaContext is null ptr! Construct MediaScalability failed!");
39     }
40 }
41 
IsScalabilityModeMatched(ScalabilityPars * params)42 bool MediaScalability::IsScalabilityModeMatched(ScalabilityPars *params)
43 {
44     bool isMatched = false;
45 
46 
47 #if (_DEBUG || _RELEASE_INTERNAL)
48 
49     if (m_osInterface == nullptr)
50     {
51         return false;
52     }
53     if (m_osInterface->bEnableDbgOvrdInVE)
54     {
55         isMatched = true;
56     }
57     else
58 #endif
59     {
60         isMatched = m_scalabilityOption->IsScalabilityOptionMatched(params);
61     }
62 
63     return isMatched;
64 }
IsGpuCtxCreateOptionMatched(PMOS_GPUCTX_CREATOPTIONS_ENHANCED gpuCtxCreateOption1,PMOS_GPUCTX_CREATOPTIONS_ENHANCED gpuCtxCreateOption2)65 bool MediaScalability::IsGpuCtxCreateOptionMatched(PMOS_GPUCTX_CREATOPTIONS_ENHANCED gpuCtxCreateOption1, PMOS_GPUCTX_CREATOPTIONS_ENHANCED gpuCtxCreateOption2)
66 {
67     bool isMatched = false;
68     //Current only need new GpuCtxCreateOption when LRCACount changed.
69     //It can be improved if needed.
70     if (gpuCtxCreateOption1->LRCACount == gpuCtxCreateOption2->LRCACount)
71     {
72         isMatched = true;
73     }
74     return isMatched;
75 }
76 
VerifySpaceAvailable(uint32_t requestedSize,uint32_t requestedPatchListSize,bool & singleTaskPhaseSupportedInPak)77 MOS_STATUS MediaScalability::VerifySpaceAvailable(uint32_t requestedSize, uint32_t requestedPatchListSize, bool &singleTaskPhaseSupportedInPak)
78 {
79     SCALABILITY_FUNCTION_ENTER;
80     SCALABILITY_CHK_NULL_RETURN(m_osInterface);
81 
82     MOS_STATUS eStatus         = MOS_STATUS_SUCCESS;
83     MOS_STATUS statusPatchList = MOS_STATUS_SUCCESS;
84     MOS_STATUS statusCmdBuf    = MOS_STATUS_SUCCESS;
85     // Try a maximum of 3 attempts to request the required sizes from OS
86     // OS could reset the sizes if necessary, therefore, requires to re-verify
87     for (auto i = 0; i < 3; i++)
88     {
89         //Experiment shows resizing CmdBuf size and PatchList size in two calls one after the other would cause previously
90         //successfully requested size to fallback to wrong value, hence never satisfying the requirement. So we call pfnResize()
91         //only once depending on whether CmdBuf size not enough, or PatchList size not enough, or both.
92         if (requestedPatchListSize)
93         {
94             statusPatchList = (MOS_STATUS)m_osInterface->pfnVerifyPatchListSize(
95                 m_osInterface,
96                 requestedPatchListSize);
97         }
98         statusCmdBuf = (MOS_STATUS)m_osInterface->pfnVerifyCommandBufferSize(
99             m_osInterface,
100             requestedSize,
101             0);
102 
103         if (statusPatchList == MOS_STATUS_SUCCESS && statusCmdBuf == MOS_STATUS_SUCCESS)
104         {
105             // This flag is just a hint for encode, decode/vpp don't use this flag.
106             singleTaskPhaseSupportedInPak = true;
107             return eStatus;
108         }
109 
110         requestedSize          = (statusCmdBuf != MOS_STATUS_SUCCESS) ? requestedSize + COMMAND_BUFFER_RESERVED_SPACE : 0;
111         requestedPatchListSize = (statusPatchList != MOS_STATUS_SUCCESS) ? requestedPatchListSize : 0;
112 
113         SCALABILITY_CHK_STATUS_RETURN(ResizeCommandBufferAndPatchList(requestedSize, requestedPatchListSize));
114 
115     }
116     return eStatus;
117 }
118 
Destroy()119 MOS_STATUS MediaScalability::Destroy()
120 {
121     if (g_apoMosEnabled)
122     {
123         if (m_veState)
124         {
125             SCALABILITY_CHK_STATUS_RETURN(MosInterface::SetVirtualEngineState(m_osInterface->osStreamState, m_veState));
126             return MosInterface::DestroyVirtualEngineState(m_osInterface->osStreamState);
127         }
128 
129         // No VE state to destroy in some scalability instances
130         return MOS_STATUS_SUCCESS;
131     }
132 
133     if (m_veInterface)
134     {
135         if(m_veInterface->pfnVEDestroy)
136         {
137             m_veInterface->pfnVEDestroy(m_veInterface);
138         }
139         MOS_FreeMemAndSetNull(m_veInterface);
140     }
141     else
142     {
143         // For VE not enabled/supported case, such as vp vebox on some platform, m_veInterface is nullptr.
144         // MOS_STATUS_SUCCESS should be returned for such case.
145         if (MOS_VE_SUPPORTED(m_osInterface))
146         {
147             SCALABILITY_CHK_NULL_RETURN(m_veInterface);
148         }
149     }
150 
151     return MOS_STATUS_SUCCESS;
152 }
153