xref: /aosp_15_r20/external/intel-media-driver/media_driver/agnostic/common/cm/cm_notifier.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
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 #ifndef __CM_NOTIFIER_H__
24 #define __CM_NOTIFIER_H__
25 
26 #include <vector>
27 #include <stdio.h>
28 #include "mos_utilities.h"
29 
30 struct CM_KERNEL_INFO;
31 
32 class CmSSH;
33 namespace CMRT_UMD
34 {
35 class CmDevice;
36 class CmKernel;
37 class CmTaskInternal;
38 class CmTask;
39 
40 const uint32_t NOTIFIER_NULL_ID = 0;
41 
42 class CmNotifier
43 {
44 public:
CmNotifier()45     CmNotifier() {};
~CmNotifier()46     virtual ~CmNotifier() {};
Valid()47     virtual bool Valid() {return false;}
ID()48     virtual unsigned int ID() {return NOTIFIER_NULL_ID;}
NotifyDeviceCreated(CmDevice * device)49     virtual int NotifyDeviceCreated(CmDevice *device)
50     {
51         // if the derived class doesn't implement this, just return 0
52         return 0;
53     }
NotifyDeviceDestroyed(CmDevice * device)54     virtual int NotifyDeviceDestroyed(CmDevice *device)
55     {
56         // if the derived class doesn't implement this, just return 0
57         return 0;
58     }
59 
NotifyKernelCreated(CmKernel * kernel)60     virtual int NotifyKernelCreated(CmKernel *kernel)
61     {
62         // if the derived class doesn't implement this, just return 0
63         return 0;
64     }
65 
NotifyTaskFlushed(CmDevice * device,CmTaskInternal * task)66     virtual int NotifyTaskFlushed(CmDevice* device, CmTaskInternal *task)
67     {
68         // if the derived class doesn't implement this, just return 0
69         return 0;
70     }
71 
NotifyTaskCompleted(CmTaskInternal * task)72     virtual int NotifyTaskCompleted(CmTaskInternal *task)
73     {
74         // if the derived class doesn't implement this, just return 0
75         return 0;
76     }
77 
NotifyCallingJitter(void ** extraInfo)78     virtual int NotifyCallingJitter(void **extraInfo)
79     {
80         // if the derived class doesn't implement this, just return 0
81         return 0;
82     }
83 
NotifyTaskFlushed(CmDevice * device,CmTask * task,CmSSH * ssh,uint32_t taskId)84     virtual int NotifyTaskFlushed(CmDevice *device, CmTask *task, CmSSH *ssh, uint32_t taskId)
85     {
86         // if the derived class doesn't implement this, just return 0
87         return 0;
88     }
89 
NotifyTaskCompleted(uint32_t taskId)90     virtual int NotifyTaskCompleted(uint32_t taskId)
91     {
92         // if the derived class doesn't implement this, just return 0
93         return 0;
94     }
95 };
96 
97 class CmNotifierGroup
98 {
99 public:
100     typedef CmNotifier* (*Creator)();
101     typedef std::vector<Creator>  Creators;
102     typedef typename Creators::iterator Iterator;
103 
CmNotifierGroup()104     CmNotifierGroup() : m_ids(0)
105     {
106         Creators &creators = GetCreators();
107         for (Iterator iter = creators.begin(); iter != creators.end(); iter ++)
108         {
109             CmNotifier *notifier = (*iter)();
110             if (notifier != nullptr && notifier->Valid() && !IsAdded(notifier->ID()))
111             {
112                 m_notifiers.push_back(notifier);
113                 m_ids = m_ids | (1 << notifier->ID());
114             }
115             else if (notifier != nullptr)
116             {
117                 MOS_Delete(notifier);
118             }
119         }
120     }
121 
~CmNotifierGroup()122     ~CmNotifierGroup()
123     {
124         for (unsigned int i = 0; i < m_notifiers.size(); i++)
125         {
126             MOS_Delete(m_notifiers[i]);
127         }
128     }
129 
NotifyDeviceCreated(CmDevice * device)130     int NotifyDeviceCreated(CmDevice *device)
131     {
132         int ret = 0;
133         for (unsigned int i = 0; i < m_notifiers.size(); i++)
134         {
135             if (m_notifiers[i]->NotifyDeviceCreated(device) != 0)
136             {
137                 ret = -1;
138             }
139         }
140         return ret;
141     }
142 
NotifyDeviceDestroyed(CmDevice * device)143     int NotifyDeviceDestroyed(CmDevice *device)
144     {
145         int ret = 0;
146         for (unsigned int i = 0; i < m_notifiers.size(); i++)
147         {
148             if (m_notifiers[i]->NotifyDeviceDestroyed(device) != 0)
149             {
150                 ret = -1;
151             }
152         }
153         return ret;
154     }
155 
NotifyKernelCreated(CmKernel * kernel)156     int NotifyKernelCreated(CmKernel *kernel)
157     {
158         int ret = 0;
159         for (unsigned int i = 0; i < m_notifiers.size(); i++)
160         {
161             if (m_notifiers[i]->NotifyKernelCreated(kernel) != 0)
162             {
163                 ret = -1;
164             }
165         }
166         return ret;
167     }
168 
NotifyTaskFlushed(CmDevice * device,CmTaskInternal * task)169     int NotifyTaskFlushed(CmDevice* device, CmTaskInternal *task)
170     {
171         int ret = 0;
172         for (unsigned int i = 0; i < m_notifiers.size(); i++)
173         {
174             if (m_notifiers[i]->NotifyTaskFlushed(device, task) != 0)
175             {
176                 ret = -1;
177             }
178         }
179         return ret;
180     }
181 
NotifyTaskCompleted(CmTaskInternal * task)182     int NotifyTaskCompleted(CmTaskInternal *task)
183     {
184         int ret = 0;
185         for (unsigned int i = 0; i < m_notifiers.size(); i++)
186         {
187             if (m_notifiers[i]->NotifyTaskCompleted(task) != 0)
188             {
189                 ret = -1;
190             }
191         }
192         return ret;
193     }
194 
NotifyCallingJitter(void ** extraInfo)195     int NotifyCallingJitter(void **extraInfo)
196     {
197         int ret = 0;
198         for (unsigned int i = 0; i < m_notifiers.size(); i++)
199         {
200             if (m_notifiers[i]->NotifyCallingJitter(extraInfo) != 0)
201             {
202                 ret = -1;
203             }
204         }
205         return ret;
206     }
207 
NotifyTaskFlushed(CmDevice * device,CmTask * task,CmSSH * ssh,uint32_t taskId)208     int NotifyTaskFlushed(CmDevice *device, CmTask *task, CmSSH *ssh, uint32_t taskId)
209     {
210         int ret = 0;
211         for (unsigned int i = 0; i < m_notifiers.size(); i++)
212         {
213             if (m_notifiers[i]->NotifyTaskFlushed(device, task, ssh, taskId) != 0)
214             {
215                 ret = -1;
216             }
217         }
218         return ret;
219     }
220 
NotifyTaskCompleted(uint32_t taskId)221     int NotifyTaskCompleted(uint32_t taskId)
222     {
223         int ret = 0;
224         for (unsigned int i = 0; i < m_notifiers.size(); i++)
225         {
226             if (m_notifiers[i]->NotifyTaskCompleted(taskId) != 0)
227             {
228                 ret = -1;
229             }
230         }
231         return ret;
232     }
233 
234     template <class T>
RegisterNotifier()235     static bool RegisterNotifier()
236     {
237         Creators &creators = GetCreators();
238         creators.push_back(Create<T>);
239         return true;
240     }
241 
242 protected:
243     std::vector<CmNotifier *> m_notifiers;
244     uint32_t m_ids;
245 
GetCreators()246     static Creators& GetCreators()
247     {
248         static Creators creators;
249         return creators;
250     }
251 
252     template <class T>
Create()253     static CmNotifier* Create()
254     {
255         return MOS_New(T);
256     }
257 
IsAdded(uint32_t id)258     bool IsAdded(uint32_t id)
259     {
260         return m_ids & (1<<id);
261     }
262 
263 };
264 };
265 #endif // __CM_NOTIFIER_H__
266