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 #ifndef __MEDIA_UTILS_H__ 23 #define __MEDIA_UTILS_H__ 24 #include <mutex> 25 #include "mos_util_debug.h" 26 #include "media_class_trace.h" 27 28 //------------------------------------------------------------------------------ 29 // Macros specific to MOS_CODEC_SUBCOMP_ENCODE sub-comp 30 //------------------------------------------------------------------------------ 31 #define MEDIA_ASSERT(_expr) \ 32 MOS_ASSERT(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _expr) 33 34 #define MEDIA_ASSERTMESSAGE(_message, ...) \ 35 MOS_ASSERTMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _message, ##__VA_ARGS__) 36 37 #define MEDIA_NORMALMESSAGE(_message, ...) \ 38 MOS_NORMALMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _message, ##__VA_ARGS__) 39 40 #define MEDIA_VERBOSEMESSAGE(_message, ...) \ 41 MOS_VERBOSEMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _message, ##__VA_ARGS__) 42 43 #define MEDIA_CHK_NULL_RETURN(_ptr) \ 44 MOS_CHK_NULL_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _ptr) 45 46 #define MEDIA_CHK_STATUS_RETURN(_stmt) \ 47 MOS_CHK_STATUS_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _stmt) 48 49 #define MEDIA_CHK_STATUS_MESSAGE_RETURN(_stmt, _message, ...) \ 50 MOS_CHK_STATUS_MESSAGE_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _stmt, _message, ##__VA_ARGS__) 51 52 class Trace 53 { 54 public: Trace(const char * name)55 Trace(const char* name) : m_name(name) 56 { 57 MEDIA_VERBOSEMESSAGE("Enter function:%s\r\n", name); 58 } 59 ~Trace()60 ~Trace() 61 { 62 MEDIA_VERBOSEMESSAGE("Exit function:%s\r\n", m_name); 63 } 64 65 protected: 66 const char* m_name; 67 MEDIA_CLASS_DEFINE_END(Trace) 68 }; 69 70 class AutoLock 71 { 72 public: AutoLock(PMOS_MUTEX mutex)73 AutoLock(PMOS_MUTEX mutex) : m_mutex(mutex) { MosUtilities::MosLockMutex(mutex); } ~AutoLock()74 ~AutoLock() { MosUtilities::MosUnlockMutex(m_mutex); } 75 protected: 76 PMOS_MUTEX m_mutex; 77 MEDIA_CLASS_DEFINE_END(AutoLock) 78 }; 79 80 class Condition 81 { 82 public: Condition()83 Condition() 84 { 85 m_sem = MosUtilities::MosCreateSemaphore(0, 1); 86 } 87 ~Condition()88 ~Condition() 89 { 90 MosUtilities::MosDestroySemaphore(m_sem); 91 } Wait(PMOS_MUTEX mutex)92 MOS_STATUS Wait(PMOS_MUTEX mutex) 93 { 94 MOS_STATUS status = MOS_STATUS_SUCCESS; 95 MosUtilities::MosUnlockMutex(mutex); 96 status = MosUtilities::MosWaitSemaphore(m_sem, 5000); 97 MosUtilities::MosLockMutex(mutex); 98 return status; 99 } Signal()100 MOS_STATUS Signal() 101 { 102 MosUtilities::MosPostSemaphore(m_sem, 1); 103 return MOS_STATUS_SUCCESS; 104 } 105 106 protected: 107 PMOS_SEMAPHORE m_sem; 108 MEDIA_CLASS_DEFINE_END(Condition) 109 }; 110 111 #define MEDIA_FUNC_CALL() Trace trace(__FUNCTION__); 112 113 #endif // !__MEDIA_UTILS_H__ 114