NativeWindowAndroid.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (C) 2009-2022, 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/Core/NativeWindowAndroid.h>
  6. namespace anki {
  7. Error NativeWindow::newInstance(const NativeWindowInitInfo& initInfo, NativeWindow*& nativeWindow)
  8. {
  9. HeapAllocator<U8> alloc(initInfo.m_allocCallback, initInfo.m_allocCallbackUserData, "NativeWindow");
  10. NativeWindowAndroid* andwin = alloc.newInstance<NativeWindowAndroid>();
  11. andwin->m_alloc = alloc;
  12. const Error err = andwin->init(initInfo);
  13. if(err)
  14. {
  15. alloc.deleteInstance(andwin);
  16. nativeWindow = nullptr;
  17. return err;
  18. }
  19. else
  20. {
  21. nativeWindow = andwin;
  22. return Error::kNone;
  23. }
  24. }
  25. void NativeWindow::deleteInstance(NativeWindow* window)
  26. {
  27. if(window)
  28. {
  29. NativeWindowAndroid* self = static_cast<NativeWindowAndroid*>(window);
  30. HeapAllocator<U8> alloc = self->m_alloc;
  31. alloc.deleteInstance(self);
  32. }
  33. }
  34. void NativeWindow::setWindowTitle([[maybe_unused]] CString title)
  35. {
  36. // Nothing
  37. }
  38. NativeWindowAndroid::~NativeWindowAndroid()
  39. {
  40. ANKI_CORE_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_nativeWindow = nullptr;
  57. }
  58. Error NativeWindowAndroid::init([[maybe_unused]] const NativeWindowInitInfo& init)
  59. {
  60. ANKI_CORE_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_nativeWindow = g_androidApp->window;
  77. // Set some stuff
  78. m_width = ANativeWindow_getWidth(g_androidApp->window);
  79. m_height = ANativeWindow_getHeight(g_androidApp->window);
  80. return Error::kNone;
  81. }
  82. } // end namespace anki