NativeWindowAndroid.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Window/NativeWindowAndroid.h>
  6. namespace anki {
  7. template<>
  8. template<>
  9. NativeWindow& MakeSingletonPtr<NativeWindow>::allocateSingleton<>()
  10. {
  11. ANKI_ASSERT(m_global == nullptr);
  12. m_global = new NativeWindowAndroid();
  13. #if ANKI_ASSERTIONS_ENABLED
  14. ++g_singletonsAllocated;
  15. #endif
  16. return *m_global;
  17. }
  18. template<>
  19. void MakeSingletonPtr<NativeWindow>::freeSingleton()
  20. {
  21. if(m_global)
  22. {
  23. delete static_cast<NativeWindowAndroid*>(m_global);
  24. m_global = nullptr;
  25. #if ANKI_ASSERTIONS_ENABLED
  26. --g_singletonsAllocated;
  27. #endif
  28. }
  29. }
  30. Error NativeWindow::init(const NativeWindowInitInfo& inf)
  31. {
  32. return static_cast<NativeWindowAndroid*>(this)->initInternal(inf);
  33. }
  34. void NativeWindow::setWindowTitle([[maybe_unused]] CString title)
  35. {
  36. // Nothing
  37. }
  38. NativeWindowAndroid::~NativeWindowAndroid()
  39. {
  40. ANKI_WIND_LOGI("Destroying Android window");
  41. ANativeActivity_finish(g_androidApp->activity);
  42. // Loop until destroyRequested is set
  43. while(!g_androidApp->destroyRequested)
  44. {
  45. int ident;
  46. int events;
  47. android_poll_source* source;
  48. while((ident = ALooper_pollAll(0, nullptr, &events, reinterpret_cast<void**>(&source))) >= 0)
  49. {
  50. if(source != nullptr)
  51. {
  52. source->process(g_androidApp, source);
  53. }
  54. }
  55. }
  56. m_nativeWindowAndroid = nullptr;
  57. }
  58. Error NativeWindowAndroid::initInternal([[maybe_unused]] const NativeWindowInitInfo& init)
  59. {
  60. ANKI_WIND_LOGI("Initializing Android window");
  61. // Loop until the window is ready
  62. while(g_androidApp->window == nullptr)
  63. {
  64. int ident;
  65. int events;
  66. android_poll_source* source;
  67. const int timeoutMs = 5;
  68. while((ident = ALooper_pollAll(timeoutMs, nullptr, &events, reinterpret_cast<void**>(&source))) >= 0)
  69. {
  70. if(source != nullptr)
  71. {
  72. source->process(g_androidApp, source);
  73. }
  74. }
  75. }
  76. m_nativeWindowAndroid = g_androidApp->window;
  77. if(init.m_targetFps)
  78. {
  79. ANativeWindow_setFrameRate(m_nativeWindowAndroid, F32(init.m_targetFps), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT);
  80. }
  81. // Set some stuff
  82. m_width = ANativeWindow_getWidth(g_androidApp->window);
  83. m_height = ANativeWindow_getHeight(g_androidApp->window);
  84. return Error::kNone;
  85. }
  86. } // end namespace anki