AkPlatformFuncs.h 9.5 KB

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