gl_context_egl.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*************************************************************************/
  2. /* gl_context_egl.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gl_context_egl.h"
  31. #include "EGL/eglext.h"
  32. using Platform::Exception;
  33. void ContextEGL::release_current() {
  34. eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, mEglContext);
  35. };
  36. void ContextEGL::make_current() {
  37. eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext);
  38. };
  39. int ContextEGL::get_window_width() {
  40. return width;
  41. };
  42. int ContextEGL::get_window_height() {
  43. return height;
  44. };
  45. void ContextEGL::reset() {
  46. cleanup();
  47. window = CoreWindow::GetForCurrentThread();
  48. initialize();
  49. };
  50. void ContextEGL::swap_buffers() {
  51. if (eglSwapBuffers(mEglDisplay, mEglSurface) != EGL_TRUE) {
  52. cleanup();
  53. window = CoreWindow::GetForCurrentThread();
  54. initialize();
  55. // tell rasterizer to reload textures and stuff?
  56. }
  57. };
  58. Error ContextEGL::initialize() {
  59. EGLint configAttribList[] = {
  60. EGL_RED_SIZE, 8,
  61. EGL_GREEN_SIZE, 8,
  62. EGL_BLUE_SIZE, 8,
  63. EGL_ALPHA_SIZE, 8,
  64. EGL_DEPTH_SIZE, 8,
  65. EGL_STENCIL_SIZE, 8,
  66. EGL_SAMPLE_BUFFERS, 0,
  67. EGL_NONE
  68. };
  69. EGLint surfaceAttribList[] = {
  70. EGL_NONE, EGL_NONE
  71. };
  72. EGLint numConfigs = 0;
  73. EGLint majorVersion = 1;
  74. EGLint minorVersion = 0;
  75. EGLDisplay display = EGL_NO_DISPLAY;
  76. EGLContext context = EGL_NO_CONTEXT;
  77. EGLSurface surface = EGL_NO_SURFACE;
  78. EGLConfig config = nullptr;
  79. EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE, EGL_NONE };
  80. try {
  81. const EGLint displayAttributes[] = {
  82. /*EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
  83. EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, 9,
  84. EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, 3,
  85. EGL_NONE,*/
  86. // These are the default display attributes, used to request ANGLE's D3D11 renderer.
  87. // eglInitialize will only succeed with these attributes if the hardware supports D3D11 Feature Level 10_0+.
  88. EGL_PLATFORM_ANGLE_TYPE_ANGLE,
  89. EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
  90. // EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER is an optimization that can have large performance benefits on mobile devices.
  91. // Its syntax is subject to change, though. Please update your Visual Studio templates if you experience compilation issues with it.
  92. //EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
  93. // EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE is an option that enables ANGLE to automatically call
  94. // the IDXGIDevice3::Trim method on behalf of the application when it gets suspended.
  95. // Calling IDXGIDevice3::Trim when an application is suspended is a Windows Store application certification requirement.
  96. EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE,
  97. EGL_TRUE,
  98. EGL_NONE,
  99. };
  100. PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplayEXT"));
  101. if (!eglGetPlatformDisplayEXT) {
  102. throw Exception::CreateException(E_FAIL, L"Failed to get function eglGetPlatformDisplayEXT");
  103. }
  104. display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, displayAttributes);
  105. if (display == EGL_NO_DISPLAY) {
  106. throw Exception::CreateException(E_FAIL, L"Failed to get default EGL display");
  107. }
  108. if (eglInitialize(display, &majorVersion, &minorVersion) == EGL_FALSE) {
  109. throw Exception::CreateException(E_FAIL, L"Failed to initialize EGL");
  110. }
  111. if (eglGetConfigs(display, NULL, 0, &numConfigs) == EGL_FALSE) {
  112. throw Exception::CreateException(E_FAIL, L"Failed to get EGLConfig count");
  113. }
  114. if (eglChooseConfig(display, configAttribList, &config, 1, &numConfigs) == EGL_FALSE) {
  115. throw Exception::CreateException(E_FAIL, L"Failed to choose first EGLConfig count");
  116. }
  117. surface = eglCreateWindowSurface(display, config, reinterpret_cast<IInspectable *>(window), surfaceAttribList);
  118. if (surface == EGL_NO_SURFACE) {
  119. throw Exception::CreateException(E_FAIL, L"Failed to create EGL fullscreen surface");
  120. }
  121. context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
  122. if (context == EGL_NO_CONTEXT) {
  123. throw Exception::CreateException(E_FAIL, L"Failed to create EGL context");
  124. }
  125. if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
  126. throw Exception::CreateException(E_FAIL, L"Failed to make fullscreen EGLSurface current");
  127. }
  128. } catch (...) {
  129. return FAILED;
  130. };
  131. mEglDisplay = display;
  132. mEglSurface = surface;
  133. mEglContext = context;
  134. eglQuerySurface(display, surface, EGL_WIDTH, &width);
  135. eglQuerySurface(display, surface, EGL_HEIGHT, &height);
  136. return OK;
  137. };
  138. void ContextEGL::cleanup() {
  139. if (mEglDisplay != EGL_NO_DISPLAY && mEglSurface != EGL_NO_SURFACE) {
  140. eglDestroySurface(mEglDisplay, mEglSurface);
  141. mEglSurface = EGL_NO_SURFACE;
  142. }
  143. if (mEglDisplay != EGL_NO_DISPLAY && mEglContext != EGL_NO_CONTEXT) {
  144. eglDestroyContext(mEglDisplay, mEglContext);
  145. mEglContext = EGL_NO_CONTEXT;
  146. }
  147. if (mEglDisplay != EGL_NO_DISPLAY) {
  148. eglTerminate(mEglDisplay);
  149. mEglDisplay = EGL_NO_DISPLAY;
  150. }
  151. };
  152. ContextEGL::ContextEGL(CoreWindow ^ p_window) :
  153. mEglDisplay(EGL_NO_DISPLAY),
  154. mEglContext(EGL_NO_CONTEXT),
  155. mEglSurface(EGL_NO_SURFACE) {
  156. window = p_window;
  157. };
  158. ContextEGL::~ContextEGL() {
  159. cleanup();
  160. };