AkPlatformFuncs.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. #pragma once
  21. #include <AK/Tools/Common/AkAssert.h>
  22. #include <AK/SoundEngine/Common/AkTypes.h>
  23. #if (defined(AK_CPU_X86_64) || defined(AK_CPU_X86))
  24. #include <cpuid.h>
  25. #endif
  26. #include <mach/task.h>
  27. #include <mach/semaphore.h>
  28. #include <CoreFoundation/CFString.h>
  29. #include <libkern/OSAtomic.h>
  30. #include <mach/task.h>
  31. #include <mach/mach_init.h>
  32. #include <mach/mach_time.h>
  33. #include <wchar.h>
  34. namespace AKPLATFORM
  35. {
  36. extern inline size_t AkUtf16StrLen( const AkUtf16* in_pStr );
  37. // Simple automatic event API
  38. // ------------------------------------------------------------------
  39. /// Platform Independent Helper
  40. inline void AkClearEvent( AkEvent & out_event )
  41. {
  42. out_event = 0;
  43. }
  44. /// Platform Independent Helper
  45. inline AKRESULT AkCreateEvent( AkEvent & out_event )
  46. {
  47. kern_return_t ret = semaphore_create(
  48. mach_task_self(),
  49. &out_event,
  50. SYNC_POLICY_FIFO,
  51. 0 );
  52. return ( ret == noErr ) ? AK_Success : AK_Fail;
  53. }
  54. /// Platform Independent Helper
  55. inline void AkDestroyEvent( AkEvent & io_event )
  56. {
  57. if( io_event != 0 )
  58. {
  59. AKVERIFY( semaphore_destroy( mach_task_self(), io_event ) == noErr);
  60. }
  61. io_event = 0;
  62. }
  63. /// Platform Independent Helper
  64. inline void AkWaitForEvent( AkEvent & in_event )
  65. {
  66. AKVERIFY( semaphore_wait( in_event ) == noErr );
  67. }
  68. /// Platform Independent Helper
  69. inline void AkSignalEvent( const AkEvent & in_event )
  70. {
  71. AKVERIFY( semaphore_signal( in_event ) == noErr );
  72. }
  73. /// Platform Independent Helper
  74. AkForceInline void AkClearSemaphore(AkSemaphore& io_semaphore)
  75. {
  76. io_semaphore = 0;
  77. }
  78. /// Platform Independent Helper
  79. inline AKRESULT AkCreateSemaphore( AkSemaphore& out_semaphore, AkUInt32 in_initialCount )
  80. {
  81. kern_return_t ret = semaphore_create(
  82. mach_task_self(),
  83. &out_semaphore,
  84. SYNC_POLICY_FIFO,
  85. in_initialCount );
  86. return ( ret == noErr ) ? AK_Success : AK_Fail;
  87. }
  88. /// Platform Independent Helper
  89. inline void AkDestroySemaphore( AkSemaphore& io_semaphore )
  90. {
  91. AKVERIFY(semaphore_destroy(mach_task_self(), io_semaphore) == noErr);
  92. }
  93. /// Platform Independent Helper - Semaphore wait, aka Operation P. Decrements value of semaphore, and, if the semaphore would be less than 0, waits for the semaphore to be released.
  94. inline void AkWaitForSemaphore(AkSemaphore& in_semaphore)
  95. {
  96. AKVERIFY(semaphore_wait(in_semaphore) == noErr);
  97. }
  98. /// Platform Independent Helper - Semaphore signal, aka Operation V. Increments value of semaphore by an arbitrary count.
  99. inline void AkReleaseSemaphore(AkSemaphore& in_semaphore, AkUInt32 in_count)
  100. {
  101. for (int i=0; i < in_count; i++)
  102. {
  103. AKVERIFY(semaphore_signal(in_semaphore) == noErr);
  104. }
  105. }
  106. // Time functions
  107. // ------------------------------------------------------------------
  108. /// Platform Independent Helper
  109. inline void PerformanceCounter( AkInt64 * out_piLastTime )
  110. {
  111. *out_piLastTime = mach_absolute_time();
  112. }
  113. /// Platform Independent Helper
  114. inline void PerformanceFrequency( AkInt64 * out_piFreq )
  115. {
  116. static mach_timebase_info_data_t sTimebaseInfo;
  117. mach_timebase_info(&sTimebaseInfo);
  118. if ( sTimebaseInfo.numer !=0 )
  119. {
  120. *out_piFreq = AkInt64((1E9 * sTimebaseInfo.denom) / sTimebaseInfo.numer );
  121. }
  122. else
  123. {
  124. *out_piFreq = 0;
  125. }
  126. }
  127. template<class destType, class srcType>
  128. inline size_t AkMacConvertString( destType* in_pdDest, const srcType* in_pSrc, size_t in_MaxSize, size_t destStrLen(const destType *), size_t srcStrLen(const srcType *) )
  129. {
  130. CFStringBuiltInEncodings dstEncoding;
  131. CFStringBuiltInEncodings srcEncoding;
  132. switch(sizeof(destType))
  133. {
  134. case 1:
  135. dstEncoding = kCFStringEncodingUTF8;
  136. break;
  137. case 2:
  138. dstEncoding = kCFStringEncodingUTF16LE;
  139. break;
  140. case 4:
  141. dstEncoding = kCFStringEncodingUTF32LE;
  142. break;
  143. default:
  144. AKASSERT(!"Invalid Char size");
  145. }
  146. switch(sizeof(srcType))
  147. {
  148. case 1:
  149. srcEncoding = kCFStringEncodingUTF8;
  150. break;
  151. case 2:
  152. srcEncoding = kCFStringEncodingUTF16LE;
  153. break;
  154. case 4:
  155. srcEncoding = kCFStringEncodingUTF32LE;
  156. break;
  157. default:
  158. AKASSERT(!"Invalid Char size");
  159. }
  160. CFStringRef strRef;
  161. strRef = CFStringCreateWithBytes( nil,
  162. (UInt8 *) in_pSrc,
  163. (srcStrLen( in_pSrc ) + 1) * sizeof(srcType),
  164. srcEncoding,
  165. false );
  166. CFRange rangeToProcess = CFRangeMake(0, CFStringGetLength(strRef));
  167. CFIndex sizeConverted = CFStringGetBytes(strRef, rangeToProcess, dstEncoding, '?', false, (UInt8 *)in_pdDest , in_MaxSize * sizeof(destType), NULL);
  168. //WG-28497 Memory leak when converting strings on Mac & iOS
  169. CFRelease(strRef);
  170. return sizeConverted;
  171. }
  172. #define CONVERT_UTF16_TO_WCHAR( _astring_, _wcharstring_ ) \
  173. _wcharstring_ = (wchar_t*)AkAlloca( (1 + AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)) * sizeof(wchar_t) ); \
  174. AK_UTF16_TO_WCHAR( _wcharstring_, (const AkUtf16*)_astring_, AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)+1 )
  175. #define CONVERT_WCHAR_TO_UTF16( _astring_, _utf16string_ ) \
  176. _utf16string_ = (AkUtf16*)AkAlloca( (1 + wcslen(_astring_)) * sizeof(AkUtf16) ); \
  177. AK_WCHAR_TO_UTF16( _utf16string_, (const wchar_t*)_astring_, wcslen(_astring_)+1 )
  178. #define CONVERT_OSCHAR_TO_UTF16( _astring_, _utf16string_ ) \
  179. _utf16string_ = (AkUtf16*)AkAlloca( (1 + strlen(_astring_)) * sizeof(AkUtf16) ); \
  180. AK_OSCHAR_TO_UTF16( _utf16string_, (const AkOSChar*)_astring_, strlen(_astring_)+1 )
  181. #define CONVERT_UTF16_TO_OSCHAR( _astring_, _oscharstring_ ) \
  182. _oscharstring_ = (AkOSChar*)AkAlloca( (1 + AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)) * sizeof(AkOSChar) ); \
  183. AK_UTF16_TO_OSCHAR( _oscharstring_, (const AkUtf16*)_astring_, AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)+1 )
  184. #define AK_UTF8_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<AkOSChar, char>( in_pdDest, in_pSrc, in_MaxSize, strlen, strlen )
  185. #define AK_UTF16_TO_WCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<wchar_t, AkUtf16>( in_pdDest, in_pSrc, in_MaxSize, &wcslen , &AKPLATFORM::AkUtf16StrLen)
  186. #define AK_WCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<AkUtf16, wchar_t>( in_pdDest, in_pSrc, in_MaxSize, &AKPLATFORM::AkUtf16StrLen, &wcslen )
  187. #define AK_UTF16_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<AkOSChar, AkUtf16>( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
  188. #define AK_UTF16_TO_CHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<char, AkUtf16>( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
  189. #define AK_CHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<AkUtf16, char>( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen)
  190. #define AK_OSCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<AkUtf16, AkOSChar>( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen)
  191. /// Stack allocations.
  192. #define AkAlloca( _size_ ) alloca( _size_ )
  193. #if __BIGGEST_ALIGNMENT__ < AK_SIMD_ALIGNMENT
  194. #define AkAllocaSIMD( _size_ ) __builtin_alloca_with_align( _size_, AK_SIMD_ALIGNMENT*8 )
  195. #endif
  196. #define AK_LIBRARY_PREFIX ("")
  197. #define AK_DYNAMIC_LIBRARY_EXTENSION (".dylib")
  198. #define AK_FILEHANDLE_TO_UINTPTR(_h) ((AkUIntPtr)_h)
  199. #define AK_SET_FILEHANDLE_TO_UINTPTR(_h,_u) _h = (AkFileHandle)_u
  200. #if (defined(AK_CPU_X86_64) || defined(AK_CPU_X86))
  201. /// Support to fetch the CPUID for the platform. Only valid for X86 targets
  202. /// \remark Note that IAkProcessorFeatures should be preferred to fetch this data
  203. /// as it will have already translated the feature bits into AK-relevant enums
  204. inline void CPUID(AkUInt32 in_uLeafOpcode, AkUInt32 in_uSubLeafOpcode, unsigned int out_uCPUFeatures[4])
  205. {
  206. __get_cpuid_count( in_uLeafOpcode, in_uSubLeafOpcode,
  207. &out_uCPUFeatures[0],
  208. &out_uCPUFeatures[1],
  209. &out_uCPUFeatures[2],
  210. &out_uCPUFeatures[3]);
  211. }
  212. #endif
  213. }