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