xref: /aosp_15_r20/external/intel-media-driver/media_driver/agnostic/common/cm/cm_event_ex_base.cpp (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2018-2019, 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_event_ex_base.cpp
24 //! \brief     Contains Class CmEventExBase  definitions
25 //!
26 
27 #include "cm_event_ex.h"
28 #include "cm_hal.h"
29 #include "cm_log.h"
30 #include "cm_tracker.h"
31 #include "cm_notifier.h"
32 
~CmEventExBase()33 CmEventExBase::~CmEventExBase()
34 {
35     if (m_cmTracker)
36     {
37         m_cmTracker->InvalidFrameTracker(m_taskId);
38     }
39 }
40 
Query()41 CM_STATUS CmEventExBase::Query()
42 {
43     if (m_state == CM_STATUS_FINISHED)
44     {
45         return CM_STATUS_FINISHED;
46     }
47     switch(m_cmTracker->Query(m_taskId))
48     {
49         case CM_TASK_FINISHED:
50             m_state = CM_STATUS_FINISHED;
51             break;
52         case CM_TASK_IN_PROGRESS:
53             m_state = CM_STATUS_FLUSHED;
54             break;
55         case CM_TASK_QUEUED:
56             m_state = CM_STATUS_QUEUED;
57             break;
58         default:
59             m_state = CM_STATUS_RESET;
60             break;
61     }
62     if (m_state == CM_STATUS_FINISHED)
63     {
64         m_start = m_cmTracker->GetStart(m_taskId);
65         m_end = m_cmTracker->GetEnd(m_taskId);
66 
67         m_cmTracker->InvalidFrameTracker(m_taskId);
68 
69         RleaseOsData();
70 
71         if (m_notifier)
72         {
73             m_notifier->NotifyTaskCompleted(m_taskId);
74         }
75         LogTimestamps(__FUNCTION__, __LINE__);
76     }
77 
78     return m_state;
79 }
80 
GetExecutionTime(uint64_t & time)81 int32_t CmEventExBase::GetExecutionTime(uint64_t &time)
82 {
83     if (m_state != CM_STATUS_FINISHED)
84     {
85         Query();
86     }
87     if (m_state == CM_STATUS_FINISHED)
88     {
89         uint64_t ticks = m_end - m_start;
90         time = HalCm_ConvertTicksToNanoSeconds(m_cmhal, ticks);
91         return CM_SUCCESS;
92     }
93     return CM_FAILURE;
94 }
95 
GetExecutionTickTime(uint64_t & ticks)96 int32_t CmEventExBase::GetExecutionTickTime(uint64_t &ticks)
97 {
98     if (m_state != CM_STATUS_FINISHED)
99     {
100         Query();
101     }
102     if (m_state == CM_STATUS_FINISHED)
103     {
104         ticks = m_end - m_start;
105         return CM_SUCCESS;
106     }
107     return CM_FAILURE;
108 }
109 
LogTimestamps(const char * callerFunctionName,int callerLineNumber)110 bool CmEventExBase::LogTimestamps(const char *callerFunctionName, int callerLineNumber)
111 {
112 #if CM_LOG_ON
113     static const char *status_strings[] = {"CM_STATUS_QUEUED",
114                                            "CM_STATUS_FLUSHED",
115                                            "CM_STATUS_FINISHED",
116                                            "CM_STATUS_STARTED",
117                                            "CM_STATUS_RESET"};
118     uint64_t ticks = m_end - m_start;
119     uint64_t duration = HalCm_ConvertTicksToNanoSeconds(m_cmhal, ticks);
120 
121     std::ostringstream log_stream;
122     log_stream << callerFunctionName << "():\n"
123                << "<CmEvent>:" << static_cast<void*>(this) << ".\n"
124                << " Status: " << status_strings[m_state] << ".\n"
125                << " Duration: " << duration << "ns.\n"
126                << " Duration in ticks: " << ticks << ".\n"
127                << " Start time in ticks: " << m_start << "\n"
128                << " End time in ticks: " << m_end << "\n" << std::endl;
129     if (!log_stream.good())
130     {
131         return false;
132     }
133     CmLogger* logTmp = CmLogger::GetInstance(m_cmhal);
134     if(logTmp != nullptr)
135     {
136         logTmp->Print(CM_LOG_LEVEL_DEBUG, __FILE__,
137                                          callerLineNumber, log_stream.str());
138     }
139 #endif  // #if CM_LOG_ON
140     return true;
141 }
142