SDL_winrtopengles.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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 SDL_VIDEO_DRIVER_WINRT && SDL_VIDEO_OPENGL_EGL
  20. /* EGL implementation of SDL OpenGL support */
  21. #include "SDL_winrtvideo_cpp.h"
  22. extern "C" {
  23. #include "SDL_winrtopengles.h"
  24. #include "SDL_loadso.h"
  25. }
  26. /* Windows includes */
  27. #include <wrl/client.h>
  28. using namespace Windows::UI::Core;
  29. /* ANGLE/WinRT constants */
  30. static const int ANGLE_D3D_FEATURE_LEVEL_ANY = 0;
  31. #define EGL_PLATFORM_ANGLE_ANGLE 0x3201
  32. #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3202
  33. #define EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE 0x3203
  34. #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3205
  35. /*
  36. * SDL/EGL top-level implementation
  37. */
  38. extern "C" int
  39. WINRT_GLES_LoadLibrary(_THIS, const char *path)
  40. {
  41. SDL_VideoData *video_data = (SDL_VideoData *)_this->driverdata;
  42. if (SDL_EGL_LoadLibrary(_this, path, EGL_DEFAULT_DISPLAY) != 0) {
  43. return -1;
  44. }
  45. /* Load ANGLE/WinRT-specific functions */
  46. CreateWinrtEglWindow_Old_Function CreateWinrtEglWindow = (CreateWinrtEglWindow_Old_Function) SDL_LoadFunction(_this->egl_data->egl_dll_handle, "CreateWinrtEglWindow");
  47. if (CreateWinrtEglWindow) {
  48. /* 'CreateWinrtEglWindow' was found, which means that an an older
  49. * version of ANGLE/WinRT is being used. Continue setting up EGL,
  50. * as appropriate to this version of ANGLE.
  51. */
  52. /* Create an ANGLE/WinRT EGL-window */
  53. /* TODO, WinRT: check for XAML usage before accessing the CoreWindow, as not doing so could lead to a crash */
  54. CoreWindow ^ native_win = CoreWindow::GetForCurrentThread();
  55. Microsoft::WRL::ComPtr<IUnknown> cpp_win = reinterpret_cast<IUnknown *>(native_win);
  56. HRESULT result = CreateWinrtEglWindow(cpp_win, ANGLE_D3D_FEATURE_LEVEL_ANY, &(video_data->winrtEglWindow));
  57. if (FAILED(result)) {
  58. return -1;
  59. }
  60. /* Call eglGetDisplay and eglInitialize as appropriate. On other
  61. * platforms, this would probably get done by SDL_EGL_LoadLibrary,
  62. * however ANGLE/WinRT's current implementation (as of Mar 22, 2014) of
  63. * eglGetDisplay requires that a C++ object be passed into it, so the
  64. * call will be made in this file, a C++ file, instead.
  65. */
  66. Microsoft::WRL::ComPtr<IUnknown> cpp_display = video_data->winrtEglWindow;
  67. _this->egl_data->egl_display = ((eglGetDisplay_Old_Function)_this->egl_data->eglGetDisplay)(cpp_display);
  68. if (!_this->egl_data->egl_display) {
  69. return SDL_SetError("Could not get EGL display");
  70. }
  71. } else {
  72. const EGLint displayAttributes[] = {
  73. EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
  74. EGL_NONE,
  75. };
  76. /* 'CreateWinrtEglWindow' was NOT found, which either means that a
  77. * newer version of ANGLE/WinRT is being used, or that we don't have
  78. * a valid copy of ANGLE.
  79. *
  80. * Try loading ANGLE as if it were the newer version.
  81. */
  82. eglGetPlatformDisplayEXT_Function eglGetPlatformDisplayEXT = (eglGetPlatformDisplayEXT_Function)_this->egl_data->eglGetProcAddress("eglGetPlatformDisplayEXT");
  83. if (!eglGetPlatformDisplayEXT) {
  84. return SDL_SetError("Could not retrieve ANGLE/WinRT display function(s)");
  85. }
  86. _this->egl_data->egl_display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, displayAttributes);
  87. if (!_this->egl_data->egl_display) {
  88. return SDL_SetError("Could not get EGL display");
  89. }
  90. }
  91. if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
  92. return SDL_SetError("Could not initialize EGL");
  93. }
  94. return 0;
  95. }
  96. extern "C" void
  97. WINRT_GLES_UnloadLibrary(_THIS)
  98. {
  99. SDL_VideoData *video_data = (SDL_VideoData *)_this->driverdata;
  100. /* Release SDL's own COM reference to the ANGLE/WinRT IWinrtEglWindow */
  101. if (video_data->winrtEglWindow) {
  102. video_data->winrtEglWindow->Release();
  103. video_data->winrtEglWindow = nullptr;
  104. }
  105. /* Perform the bulk of the unloading */
  106. SDL_EGL_UnloadLibrary(_this);
  107. }
  108. extern "C" {
  109. SDL_EGL_CreateContext_impl(WINRT)
  110. SDL_EGL_SwapWindow_impl(WINRT)
  111. SDL_EGL_MakeCurrent_impl(WINRT)
  112. }
  113. #endif /* SDL_VIDEO_DRIVER_WINRT && SDL_VIDEO_OPENGL_EGL */
  114. /* vi: set ts=4 sw=4 expandtab: */