1 /*
2 * Copyright (c) 2018-2021, 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_status_report.cpp
25 //! \brief    Defines the common interface for media status reporter
26 //! \details
27 //!
28 #include <algorithm>
29 #include "media_status_report.h"
30 
GetAddress(uint32_t statusReportType,PMOS_RESOURCE & osResource,uint32_t & offset)31 MOS_STATUS MediaStatusReport::GetAddress(uint32_t statusReportType, PMOS_RESOURCE &osResource, uint32_t &offset)
32 {
33     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
34 
35     if (m_statusBufAddr == nullptr)
36         return MOS_STATUS_NULL_POINTER;
37 
38     osResource = m_statusBufAddr[statusReportType].osResource;
39 
40     if (statusReportType == STATUS_REPORT_GLOBAL_COUNT)
41     {
42         offset = m_statusBufAddr[statusReportType].offset;
43     }
44     else
45     {
46         offset = m_statusBufAddr[statusReportType].offset + m_statusBufAddr[statusReportType].bufSize * CounterToIndex(m_submittedCount);
47     }
48 
49     return eStatus;
50 }
51 
GetReport(uint16_t requireNum,void * status)52 MOS_STATUS MediaStatusReport::GetReport(uint16_t requireNum, void *status)
53 
54 {
55     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
56 
57     Lock();
58     uint32_t completedCount = *m_completedCount;
59     uint32_t reportedCount = m_reportedCount;
60     uint32_t reportedCountOrigin = m_reportedCount;
61     uint32_t availableCount = m_submittedCount - reportedCount;
62     uint32_t generatedReportCount = 0;
63     uint32_t reportIndex = 0;
64     bool reverseOrder = (requireNum > 1);
65 
66     while (reportedCount != completedCount && generatedReportCount < requireNum){
67 
68         // Get reverse order index to temporally fix application get status report size bigger than 2 case.
69         reportIndex = reverseOrder ? CounterToIndex(completedCount + reportedCountOrigin - reportedCount -1) :
70                                      CounterToIndex(reportedCount);
71         // m_reportedCount is used by component. Need to assign actual index before call ParseStatus
72         m_reportedCount = reportIndex;
73         eStatus = ParseStatus(((uint8_t*)status + m_sizeOfReport * generatedReportCount), reportIndex);
74 
75         reportedCount++;
76         generatedReportCount++;
77     }
78 
79     // update incomplete/unavailable status
80     uint32_t updatedCount = reportedCount;
81     if (generatedReportCount < requireNum)
82     {
83         for (auto i = generatedReportCount; i < requireNum; i++)
84         {
85             eStatus = SetStatus(((uint8_t *)status + m_sizeOfReport * i),
86                                 CounterToIndex(updatedCount),
87                                 i >= availableCount);
88             updatedCount++;
89         }
90     }
91 
92     m_reportedCount = reportedCount;
93     UnLock();
94 
95     return eStatus;
96 }
97 
RegistObserver(MediaStatusReportObserver * observer)98 MOS_STATUS MediaStatusReport::RegistObserver(MediaStatusReportObserver *observer)
99 {
100     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
101     std::vector<MediaStatusReportObserver *>::iterator it;
102 
103     it = std::find(m_completeObservers.begin(), m_completeObservers.end(), observer);
104     if (it != m_completeObservers.end())
105     {
106         // the observer already in the vector
107         return MOS_STATUS_SUCCESS;
108     }
109 
110     Lock();
111     m_completeObservers.push_back(observer);
112     UnLock();
113 
114     return eStatus;
115 }
116 
UnregistObserver(MediaStatusReportObserver * observer)117 MOS_STATUS MediaStatusReport::UnregistObserver(MediaStatusReportObserver *observer)
118 {
119     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
120     std::vector<MediaStatusReportObserver *>::iterator it;
121 
122     it = std::find(m_completeObservers.begin(), m_completeObservers.end(), observer);
123     if (it == m_completeObservers.end())
124     {
125         // the observer not in the vector
126         return MOS_STATUS_INVALID_PARAMETER;
127     }
128 
129     Lock();
130     m_completeObservers.erase(it);
131     UnLock();
132 
133     return eStatus;
134 }
135 
NotifyObservers(void * mfxStatus,void * rcsStatus,void * statusReport)136 MOS_STATUS MediaStatusReport::NotifyObservers(void *mfxStatus, void *rcsStatus, void *statusReport)
137 {
138     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
139     std::vector<MediaStatusReportObserver *>::iterator it;
140 
141     for (it = m_completeObservers.begin(); it != m_completeObservers.end(); it++)
142     {
143         eStatus = (*it)->Completed(mfxStatus, rcsStatus, statusReport);
144     }
145 
146     return eStatus;
147 }
148 
149 
150 
151