AkPlatformFuncs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. #include <android/log.h>
  24. #if (defined(AK_CPU_X86_64) || defined(AK_CPU_X86))
  25. #include <cpuid.h>
  26. #endif
  27. #include <time.h>
  28. #include <stdlib.h>
  29. #define AK_THREAD_INIT_CODE(_threadProperties) syscall(__NR_sched_setaffinity, 0, sizeof(_threadProperties.dwAffinityMask), &_threadProperties.dwAffinityMask)
  30. #define AK_SEC_TO_NANOSEC 1000000000ULL
  31. namespace AKPLATFORM
  32. {
  33. /// Platform Independent Helper
  34. inline void PerformanceFrequency( AkInt64 * out_piFreq )
  35. {
  36. //Since Wall Clock is used, 1 NS is the frequency independent of the clock resolution
  37. *out_piFreq = AK_SEC_TO_NANOSEC;
  38. }
  39. }
  40. #include <AK/Tools/POSIX/AkPlatformFuncs.h>
  41. #if (defined AK_LUMIN) && !(defined AK_OPTIMIZED)
  42. #include <ml_logging.h>
  43. #endif
  44. /// Stack allocations.
  45. #define AkAlloca( _size_ ) __builtin_alloca( _size_ )
  46. namespace AKPLATFORM
  47. {
  48. /// Output a debug message on the console (Ansi string)
  49. #if defined(AK_OPTIMIZED)
  50. inline void OutputDebugMsg( const char* ){}
  51. template <int MaxSize = 0> // Unused
  52. inline void OutputDebugMsgV( const char* in_pszFmt, ... ) {}
  53. #elif defined(AK_LUMIN)
  54. inline void OutputDebugMsg( const char* in_pszMsg )
  55. {
  56. // To see output of this (mldb on lumin)
  57. // mldb logcat ActivityManager:I YourApplication:D AKDEBUG:D *:S
  58. ML_LOG_TAG(Info, "AKDEBUG", "%s", in_pszMsg);
  59. }
  60. /// Output a debug message on the console (variadic function).
  61. template <int MaxSize = 256>
  62. inline void OutputDebugMsgV( const char* in_pszFmt, ... )
  63. {
  64. // magic leap doesn't have a log that takes a va_list
  65. int size = 0;
  66. {
  67. // Try with a reasonable guess first
  68. char msg[MaxSize];
  69. msg[MaxSize - 1] = '\0';
  70. va_list args;
  71. va_start(args, in_pszFmt);
  72. size = vsnprintf(msg, MaxSize, in_pszFmt, args);
  73. va_end(args);
  74. // If it was enough, print to debug log
  75. if (0 <= size && size <= MaxSize)
  76. {
  77. ML_LOG_TAG(Info, "AKDEBUG", in_pszFmt, args);
  78. return;
  79. }
  80. }
  81. // Else, we need more memory to prevent truncation
  82. {
  83. // size + 1 more char for the last '\0'
  84. size++;
  85. char* msg = (char*)AkAlloca((size) * sizeof(char));
  86. msg[size - 1] = '\0';
  87. va_list args;
  88. va_start(args, in_pszFmt);
  89. vsnprintf(msg, size, in_pszFmt, args);
  90. va_end(args);
  91. ML_LOG_TAG(Info, "AKDEBUG", in_pszFmt, args);
  92. }
  93. }
  94. #else // AK_ANDROID
  95. inline void OutputDebugMsg( const char* in_pszMsg )
  96. {
  97. // To see output of this
  98. // adb logcat ActivityManager:I YourApplication:D AKDEBUG:D *:S
  99. __android_log_print(ANDROID_LOG_INFO, "AKDEBUG", "%s", in_pszMsg);
  100. }
  101. /// Output a debug message on the console (variadic function).
  102. template <int MaxSize = 0> // Unused
  103. inline void OutputDebugMsgV( const char* in_pszFmt, ...)
  104. {
  105. va_list args;
  106. va_start(args, in_pszFmt);
  107. __android_log_vprint(ANDROID_LOG_INFO, "AKDEBUG", in_pszFmt, args);
  108. va_end(args);
  109. }
  110. #endif
  111. // Time functions
  112. // ------------------------------------------------------------------
  113. /// Platform Independent Helper
  114. inline void PerformanceCounter( AkInt64 * out_piLastTime )
  115. {
  116. struct timespec clockNow;
  117. clock_gettime(CLOCK_MONOTONIC, &clockNow);
  118. //This give the wallclock time in NS
  119. *out_piLastTime = clockNow.tv_sec*AK_SEC_TO_NANOSEC + clockNow.tv_nsec;
  120. }
  121. template<class destType, class srcType>
  122. inline size_t AkSimpleConvertString( destType* in_pdDest, const srcType* in_pSrc, size_t in_MaxSize, size_t destStrLen(const destType *), size_t srcStrLen(const srcType *) )
  123. {
  124. size_t i;
  125. size_t lenToCopy = srcStrLen(in_pSrc);
  126. lenToCopy = (lenToCopy > in_MaxSize-1) ? in_MaxSize-1 : lenToCopy;
  127. for(i = 0; i < lenToCopy; i++)
  128. {
  129. in_pdDest[i] = (destType) in_pSrc[i];
  130. }
  131. in_pdDest[lenToCopy] = (destType)0;
  132. return lenToCopy;
  133. }
  134. #define CONVERT_UTF16_TO_CHAR( _astring_, _charstring_ ) \
  135. _charstring_ = (char*)AkAlloca( (1 + AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)) * sizeof(char) ); \
  136. AK_UTF16_TO_CHAR( _charstring_, (const AkUtf16*)_astring_, AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)+1 )
  137. #define AK_UTF8_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, strlen )
  138. #define AK_UTF16_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
  139. #define AK_UTF16_TO_CHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
  140. #define AK_CHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen)
  141. #define AK_OSCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen)
  142. #if __BIGGEST_ALIGNMENT__ < AK_SIMD_ALIGNMENT
  143. #define AkAllocaSIMD( _size_ ) __builtin_alloca_with_align( _size_, AK_SIMD_ALIGNMENT*8 )
  144. #endif
  145. /// Platform Independent Helper
  146. inline void AkCreateThread(
  147. AkThreadRoutine pStartRoutine, // Thread routine.
  148. void * pParams, // Routine params.
  149. const AkThreadProperties & in_threadProperties, // Properties. NULL for default.
  150. AkThread * out_pThread, // Returned thread handle.
  151. const char * /*in_szThreadName*/ ) // Opt thread name.
  152. {
  153. AKASSERT( out_pThread != NULL );
  154. pthread_attr_t attr;
  155. // Create the attr
  156. AKVERIFY(!pthread_attr_init(&attr));
  157. // Set the stack size
  158. AKVERIFY(!pthread_attr_setstacksize(&attr,in_threadProperties.uStackSize));
  159. AKVERIFY(!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
  160. // Create the tread
  161. int threadError = pthread_create( out_pThread, &attr, pStartRoutine, pParams);
  162. AKASSERT( threadError == 0 );
  163. AKVERIFY(!pthread_attr_destroy(&attr));
  164. if( threadError != 0 )
  165. {
  166. AkClearThread( out_pThread );
  167. return;
  168. }
  169. // ::CreateThread() return NULL if it fails.
  170. if ( !*out_pThread )
  171. {
  172. AkClearThread( out_pThread );
  173. return;
  174. }
  175. // Try to set the thread policy
  176. int sched_policy = in_threadProperties.uSchedPolicy;
  177. // Get the priority for the policy
  178. int minPriority, maxPriority;
  179. minPriority = sched_get_priority_min(sched_policy);
  180. maxPriority = sched_get_priority_max(sched_policy);
  181. // Set the thread priority if valid
  182. sched_param schedParam;
  183. schedParam.sched_priority = in_threadProperties.nPriority;
  184. AKASSERT( in_threadProperties.nPriority >= minPriority && in_threadProperties.nPriority <= maxPriority );
  185. //pthread_setschedparam WILL fail on Android Lollipop when used with SCHED_FIFO (the default). Not allowed anymore. (ignore the error code).
  186. int err = pthread_setschedparam(*out_pThread, sched_policy, &schedParam);
  187. if (err != 0)
  188. {
  189. //Make sure the priority is well set, even if the policy could not.
  190. sched_policy = SCHED_NORMAL;
  191. minPriority = sched_get_priority_min(sched_policy);
  192. maxPriority = sched_get_priority_max(sched_policy);
  193. if (in_threadProperties.nPriority == AK_THREAD_PRIORITY_ABOVE_NORMAL)
  194. schedParam.sched_priority = maxPriority;
  195. else if (in_threadProperties.nPriority == AK_THREAD_PRIORITY_BELOW_NORMAL)
  196. schedParam.sched_priority = minPriority;
  197. else
  198. schedParam.sched_priority = (maxPriority + minPriority) / 2;
  199. err = pthread_setschedparam(*out_pThread, sched_policy, &schedParam);
  200. AKASSERT(err == 0);
  201. }
  202. }
  203. #define AK_FILEHANDLE_TO_UINTPTR(_h) ((AkUIntPtr)_h)
  204. #define AK_SET_FILEHANDLE_TO_UINTPTR(_h,_u) _h = (AkFileHandle)_u
  205. #if (defined(AK_CPU_X86_64) || defined(AK_CPU_X86))
  206. // Once our minimum compiler version supports __get_cpuid_count, these asm blocks can be replaced
  207. #if defined(__i386__) && defined(__PIC__)
  208. // %ebx may be the PIC register.
  209. #define __ak_cpuid_count(level, count, a, b, c, d) \
  210. __asm__ ("xchg{l}\t{%%}ebx, %k1\n\t" \
  211. "cpuid\n\t" \
  212. "xchg{l}\t{%%}ebx, %k1\n\t" \
  213. : "=a" (a), "=&r" (b), "=c" (c), "=d" (d) \
  214. : "0" (level), "2" (count))
  215. #elif defined(__x86_64__) && defined(__PIC__)
  216. // %rbx may be the PIC register.
  217. #define __ak_cpuid_count(level, count, a, b, c, d) \
  218. __asm__ ("xchg{q}\t{%%}rbx, %q1\n\t" \
  219. "cpuid\n\t" \
  220. "xchg{q}\t{%%}rbx, %q1\n\t" \
  221. : "=a" (a), "=&r" (b), "=c" (c), "=d" (d) \
  222. : "0" (level), "2" (count))
  223. #else
  224. #define __ak_cpuid_count(level, count, a, b, c, d) \
  225. __asm__ ("cpuid\n\t" \
  226. : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
  227. : "0" (level), "2" (count))
  228. #endif
  229. static __inline int __ak_get_cpuid_count(unsigned int __leaf,
  230. unsigned int __subleaf,
  231. unsigned int *__eax, unsigned int *__ebx,
  232. unsigned int *__ecx, unsigned int *__edx)
  233. {
  234. unsigned int __max_leaf = __get_cpuid_max(__leaf & 0x80000000, 0);
  235. if (__max_leaf == 0 || __max_leaf < __leaf)
  236. return 0;
  237. __ak_cpuid_count(__leaf, __subleaf, *__eax, *__ebx, *__ecx, *__edx);
  238. return 1;
  239. }
  240. /// Support to fetch the CPUID for the platform. Only valid for X86 targets
  241. /// \remark Note that IAkProcessorFeatures should be preferred to fetch this data
  242. /// as it will have already translated the feature bits into AK-relevant enums
  243. inline void CPUID(AkUInt32 in_uLeafOpcode, AkUInt32 in_uSubLeafOpcode, unsigned int out_uCPUFeatures[4])
  244. {
  245. __ak_get_cpuid_count( in_uLeafOpcode, in_uSubLeafOpcode,
  246. &out_uCPUFeatures[0],
  247. &out_uCPUFeatures[1],
  248. &out_uCPUFeatures[2],
  249. &out_uCPUFeatures[3]);
  250. }
  251. #endif
  252. }