Imported Upstream version 2.0.14
[platform/upstream/SDL.git] / src / video / winrt / SDL_winrtopengles.cpp
1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4
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.
8
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:
12
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.
20 */
21 #include "../../SDL_internal.h"
22
23 #if SDL_VIDEO_DRIVER_WINRT && SDL_VIDEO_OPENGL_EGL
24
25 /* EGL implementation of SDL OpenGL support */
26
27 #include "SDL_winrtvideo_cpp.h"
28 extern "C" {
29 #include "SDL_winrtopengles.h"
30 #include "SDL_loadso.h"
31 #include "../SDL_egl_c.h"
32 }
33
34 /* Windows includes */
35 #include <wrl/client.h>
36 using namespace Windows::UI::Core;
37
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
48
49 #define EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER   0x320B
50
51
52 /*
53  * SDL/EGL top-level implementation
54  */
55
56 extern "C" int
57 WINRT_GLES_LoadLibrary(_THIS, const char *path)
58 {
59     SDL_VideoData *video_data = (SDL_VideoData *)_this->driverdata;
60
61     if (SDL_EGL_LoadLibrary(_this, path, EGL_DEFAULT_DISPLAY, 0) != 0) {
62         return -1;
63     }
64
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.
71          */
72
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));
78         if (FAILED(result)) {
79             return -1;
80         }
81
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.
87          */
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");
92         }
93
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");
96         }
97     } else {
98         /* Declare some ANGLE/EGL initialization property-sets, as suggested by
99          * MSOpenTech's ANGLE-for-WinRT template apps:
100          */
101         const EGLint defaultDisplayAttributes[] =
102         {
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,
106             EGL_NONE,
107         };
108
109         const EGLint fl9_3DisplayAttributes[] =
110         {
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,
116             EGL_NONE,
117         };
118
119         const EGLint warpDisplayAttributes[] =
120         {
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,
125             EGL_NONE,
126         };
127
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.
131          *
132          * Try loading ANGLE as if it were the newer version.
133          */
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");
137         }
138
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.
142          */
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");
146         }
147
148         if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE)
149 #endif
150         {
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
153              * 9_3).
154              */
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");
158             }
159
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.
163                  */
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");
167                 }
168
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");
171                 }
172             }
173         }
174     }
175
176     return 0;
177 }
178
179 extern "C" void
180 WINRT_GLES_UnloadLibrary(_THIS)
181 {
182     SDL_VideoData *video_data = (SDL_VideoData *)_this->driverdata;
183
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;
188     }
189
190     /* Perform the bulk of the unloading */
191     SDL_EGL_UnloadLibrary(_this);
192 }
193
194 extern "C" {
195 SDL_EGL_CreateContext_impl(WINRT)
196 SDL_EGL_SwapWindow_impl(WINRT)
197 SDL_EGL_MakeCurrent_impl(WINRT)
198 }
199
200 #endif /* SDL_VIDEO_DRIVER_WINRT && SDL_VIDEO_OPENGL_EGL */
201
202 /* vi: set ts=4 sw=4 expandtab: */
203