SDL_windowsopengles.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if defined(SDL_VIDEO_DRIVER_WINDOWS) && defined(SDL_VIDEO_OPENGL_EGL) && !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  20. #include "SDL_windowsvideo.h"
  21. #include "SDL_windowsopengles.h"
  22. #include "SDL_windowsopengl.h"
  23. #include "SDL_windowswindow.h"
  24. // EGL implementation of SDL OpenGL support
  25. bool WIN_GLES_LoadLibrary(SDL_VideoDevice *_this, const char *path)
  26. {
  27. // If the profile requested is not GL ES, switch over to WIN_GL functions
  28. if (_this->gl_config.profile_mask != SDL_GL_CONTEXT_PROFILE_ES &&
  29. !SDL_GetHintBoolean(SDL_HINT_VIDEO_FORCE_EGL, false)) {
  30. #ifdef SDL_VIDEO_OPENGL_WGL
  31. WIN_GLES_UnloadLibrary(_this);
  32. _this->GL_LoadLibrary = WIN_GL_LoadLibrary;
  33. _this->GL_GetProcAddress = WIN_GL_GetProcAddress;
  34. _this->GL_UnloadLibrary = WIN_GL_UnloadLibrary;
  35. _this->GL_CreateContext = WIN_GL_CreateContext;
  36. _this->GL_MakeCurrent = WIN_GL_MakeCurrent;
  37. _this->GL_SetSwapInterval = WIN_GL_SetSwapInterval;
  38. _this->GL_GetSwapInterval = WIN_GL_GetSwapInterval;
  39. _this->GL_SwapWindow = WIN_GL_SwapWindow;
  40. _this->GL_DestroyContext = WIN_GL_DestroyContext;
  41. _this->GL_GetEGLSurface = NULL;
  42. return WIN_GL_LoadLibrary(_this, path);
  43. #else
  44. return SDL_SetError("SDL not configured with OpenGL/WGL support");
  45. #endif
  46. }
  47. if (!_this->egl_data) {
  48. return SDL_EGL_LoadLibrary(_this, NULL, EGL_DEFAULT_DISPLAY);
  49. }
  50. return true;
  51. }
  52. SDL_GLContext WIN_GLES_CreateContext(SDL_VideoDevice *_this, SDL_Window *window)
  53. {
  54. SDL_GLContext context;
  55. SDL_WindowData *data = window->internal;
  56. #ifdef SDL_VIDEO_OPENGL_WGL
  57. if (_this->gl_config.profile_mask != SDL_GL_CONTEXT_PROFILE_ES &&
  58. !SDL_GetHintBoolean(SDL_HINT_VIDEO_FORCE_EGL, false)) {
  59. // Switch to WGL based functions
  60. WIN_GLES_UnloadLibrary(_this);
  61. _this->GL_LoadLibrary = WIN_GL_LoadLibrary;
  62. _this->GL_GetProcAddress = WIN_GL_GetProcAddress;
  63. _this->GL_UnloadLibrary = WIN_GL_UnloadLibrary;
  64. _this->GL_CreateContext = WIN_GL_CreateContext;
  65. _this->GL_MakeCurrent = WIN_GL_MakeCurrent;
  66. _this->GL_SetSwapInterval = WIN_GL_SetSwapInterval;
  67. _this->GL_GetSwapInterval = WIN_GL_GetSwapInterval;
  68. _this->GL_SwapWindow = WIN_GL_SwapWindow;
  69. _this->GL_DestroyContext = WIN_GL_DestroyContext;
  70. _this->GL_GetEGLSurface = NULL;
  71. if (!WIN_GL_LoadLibrary(_this, NULL)) {
  72. return NULL;
  73. }
  74. return WIN_GL_CreateContext(_this, window);
  75. }
  76. #endif
  77. context = SDL_EGL_CreateContext(_this, data->egl_surface);
  78. return context;
  79. }
  80. bool WIN_GLES_DestroyContext(SDL_VideoDevice *_this, SDL_GLContext context)
  81. {
  82. return SDL_EGL_DestroyContext(_this, context);
  83. }
  84. /* *INDENT-OFF* */ // clang-format off
  85. SDL_EGL_SwapWindow_impl(WIN)
  86. SDL_EGL_MakeCurrent_impl(WIN)
  87. /* *INDENT-ON* */ // clang-format on
  88. bool WIN_GLES_SetupWindow(SDL_VideoDevice *_this, SDL_Window *window)
  89. {
  90. // The current context is lost in here; save it and reset it.
  91. SDL_WindowData *windowdata = window->internal;
  92. SDL_Window *current_win = SDL_GL_GetCurrentWindow();
  93. SDL_GLContext current_ctx = SDL_GL_GetCurrentContext();
  94. if (!_this->egl_data) {
  95. // !!! FIXME: commenting out this assertion is (I think) incorrect; figure out why driver_loaded is wrong for ANGLE instead. --ryan.
  96. #if 0 // When hint SDL_HINT_OPENGL_ES_DRIVER is set to "1" (e.g. for ANGLE support), _this->gl_config.driver_loaded can be 1, while the below lines function.
  97. SDL_assert(!_this->gl_config.driver_loaded);
  98. #endif
  99. if (!SDL_EGL_LoadLibrary(_this, NULL, EGL_DEFAULT_DISPLAY)) {
  100. SDL_EGL_UnloadLibrary(_this);
  101. return false;
  102. }
  103. _this->gl_config.driver_loaded = 1;
  104. }
  105. // Create the GLES window surface
  106. windowdata->egl_surface = SDL_EGL_CreateSurface(_this, window, (NativeWindowType)windowdata->hwnd);
  107. if (windowdata->egl_surface == EGL_NO_SURFACE) {
  108. return SDL_SetError("Could not create GLES window surface");
  109. }
  110. return WIN_GLES_MakeCurrent(_this, current_win, current_ctx);
  111. }
  112. EGLSurface WIN_GLES_GetEGLSurface(SDL_VideoDevice *_this, SDL_Window *window)
  113. {
  114. SDL_WindowData *windowdata = window->internal;
  115. return windowdata->egl_surface;
  116. }
  117. #endif // SDL_VIDEO_DRIVER_WINDOWS && SDL_VIDEO_OPENGL_EGL