2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_WINRT && SDL_VIDEO_OPENGL_EGL
25 /* EGL implementation of SDL OpenGL support */
27 #include "SDL_winrtvideo_cpp.h"
29 #include "SDL_winrtopengles.h"
30 #include "SDL_loadso.h"
31 #include "../SDL_egl_c.h"
34 /* Windows includes */
35 #include <wrl/client.h>
36 using namespace Windows::UI::Core;
38 /* ANGLE/WinRT constants */
39 static const int ANGLE_D3D_FEATURE_LEVEL_ANY = 0;
40 #define EGL_PLATFORM_ANGLE_ANGLE 0x3202
41 #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3203
42 #define EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE 0x3204
43 #define EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE 0x3205
44 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3208
45 #define EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE 0x3209
46 #define EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE 0x320B
47 #define EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE 0x320F
49 #define EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER 0x320B
53 * SDL/EGL top-level implementation
57 WINRT_GLES_LoadLibrary(_THIS, const char *path)
59 SDL_VideoData *video_data = (SDL_VideoData *)_this->driverdata;
61 if (SDL_EGL_LoadLibrary(_this, path, EGL_DEFAULT_DISPLAY, 0) != 0) {
65 /* Load ANGLE/WinRT-specific functions */
66 CreateWinrtEglWindow_Old_Function CreateWinrtEglWindow = (CreateWinrtEglWindow_Old_Function) SDL_LoadFunction(_this->egl_data->egl_dll_handle, "CreateWinrtEglWindow");
67 if (CreateWinrtEglWindow) {
68 /* 'CreateWinrtEglWindow' was found, which means that an an older
69 * version of ANGLE/WinRT is being used. Continue setting up EGL,
70 * as appropriate to this version of ANGLE.
73 /* Create an ANGLE/WinRT EGL-window */
74 /* TODO, WinRT: check for XAML usage before accessing the CoreWindow, as not doing so could lead to a crash */
75 CoreWindow ^ native_win = CoreWindow::GetForCurrentThread();
76 Microsoft::WRL::ComPtr<IUnknown> cpp_win = reinterpret_cast<IUnknown *>(native_win);
77 HRESULT result = CreateWinrtEglWindow(cpp_win, ANGLE_D3D_FEATURE_LEVEL_ANY, &(video_data->winrtEglWindow));
82 /* Call eglGetDisplay and eglInitialize as appropriate. On other
83 * platforms, this would probably get done by SDL_EGL_LoadLibrary,
84 * however ANGLE/WinRT's current implementation (as of Mar 22, 2014) of
85 * eglGetDisplay requires that a C++ object be passed into it, so the
86 * call will be made in this file, a C++ file, instead.
88 Microsoft::WRL::ComPtr<IUnknown> cpp_display = video_data->winrtEglWindow;
89 _this->egl_data->egl_display = ((eglGetDisplay_Old_Function)_this->egl_data->eglGetDisplay)(cpp_display);
90 if (!_this->egl_data->egl_display) {
91 return SDL_EGL_SetError("Could not get Windows 8.0 EGL display", "eglGetDisplay");
94 if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
95 return SDL_EGL_SetError("Could not initialize Windows 8.0 EGL", "eglInitialize");
98 /* Declare some ANGLE/EGL initialization property-sets, as suggested by
99 * MSOpenTech's ANGLE-for-WinRT template apps:
101 const EGLint defaultDisplayAttributes[] =
103 EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
104 EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
105 EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
109 const EGLint fl9_3DisplayAttributes[] =
111 EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
112 EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, 9,
113 EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, 3,
114 EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
115 EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
119 const EGLint warpDisplayAttributes[] =
121 EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
122 EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE,
123 EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
124 EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
128 /* 'CreateWinrtEglWindow' was NOT found, which either means that a
129 * newer version of ANGLE/WinRT is being used, or that we don't have
130 * a valid copy of ANGLE.
132 * Try loading ANGLE as if it were the newer version.
134 eglGetPlatformDisplayEXT_Function eglGetPlatformDisplayEXT = (eglGetPlatformDisplayEXT_Function)_this->egl_data->eglGetProcAddress("eglGetPlatformDisplayEXT");
135 if (!eglGetPlatformDisplayEXT) {
136 return SDL_EGL_SetError("Could not retrieve ANGLE/WinRT display function(s)", "eglGetProcAddress");
139 #if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP)
140 /* Try initializing EGL at D3D11 Feature Level 10_0+ (which is not
141 * supported on WinPhone 8.x.
143 _this->egl_data->egl_display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, defaultDisplayAttributes);
144 if (!_this->egl_data->egl_display) {
145 return SDL_EGL_SetError("Could not get EGL display for Direct3D 10_0+", "eglGetPlatformDisplayEXT");
148 if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE)
151 /* Try initializing EGL at D3D11 Feature Level 9_3, in case the
152 * 10_0 init fails, or we're on Windows Phone (which only supports
155 _this->egl_data->egl_display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, fl9_3DisplayAttributes);
156 if (!_this->egl_data->egl_display) {
157 return SDL_EGL_SetError("Could not get EGL display for Direct3D 9_3", "eglGetPlatformDisplayEXT");
160 if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
161 /* Try initializing EGL at D3D11 Feature Level 11_0 on WARP
162 * (a Windows-provided, software rasterizer) if all else fails.
164 _this->egl_data->egl_display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, warpDisplayAttributes);
165 if (!_this->egl_data->egl_display) {
166 return SDL_EGL_SetError("Could not get EGL display for Direct3D WARP", "eglGetPlatformDisplayEXT");
169 if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
170 return SDL_EGL_SetError("Could not initialize WinRT 8.x+ EGL", "eglInitialize");
180 WINRT_GLES_UnloadLibrary(_THIS)
182 SDL_VideoData *video_data = (SDL_VideoData *)_this->driverdata;
184 /* Release SDL's own COM reference to the ANGLE/WinRT IWinrtEglWindow */
185 if (video_data->winrtEglWindow) {
186 video_data->winrtEglWindow->Release();
187 video_data->winrtEglWindow = nullptr;
190 /* Perform the bulk of the unloading */
191 SDL_EGL_UnloadLibrary(_this);
195 SDL_EGL_CreateContext_impl(WINRT)
196 SDL_EGL_SwapWindow_impl(WINRT)
197 SDL_EGL_MakeCurrent_impl(WINRT)
200 #endif /* SDL_VIDEO_DRIVER_WINRT && SDL_VIDEO_OPENGL_EGL */
202 /* vi: set ts=4 sw=4 expandtab: */