src/video/winrt/SDL_winrtopengles.cpp
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 30 Oct 2016 21:01:33 +0100
changeset 10563 e3d84016cb79
parent 9998 f67cf37e9cd4
child 10737 3406a0f8b041
permissions -rw-r--r--
Fixed outdated info in README.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 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 }
    32 
    33 /* Windows includes */
    34 #include <wrl/client.h>
    35 using namespace Windows::UI::Core;
    36 
    37 /* ANGLE/WinRT constants */
    38 static const int ANGLE_D3D_FEATURE_LEVEL_ANY = 0;
    39 #define EGL_PLATFORM_ANGLE_ANGLE                        0x3202
    40 #define EGL_PLATFORM_ANGLE_TYPE_ANGLE                   0x3203
    41 #define EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE      0x3204
    42 #define EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE      0x3205
    43 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE             0x3208
    44 #define EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE            0x3209
    45 #define EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE       0x320B
    46 #define EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE  0x320F
    47 
    48 #define EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER   0x320B
    49 
    50 
    51 /*
    52  * SDL/EGL top-level implementation
    53  */
    54 
    55 extern "C" int
    56 WINRT_GLES_LoadLibrary(_THIS, const char *path)
    57 {
    58     SDL_VideoData *video_data = (SDL_VideoData *)_this->driverdata;
    59 
    60     if (SDL_EGL_LoadLibrary(_this, path, EGL_DEFAULT_DISPLAY) != 0) {
    61         return -1;
    62     }
    63 
    64     /* Load ANGLE/WinRT-specific functions */
    65     CreateWinrtEglWindow_Old_Function CreateWinrtEglWindow = (CreateWinrtEglWindow_Old_Function) SDL_LoadFunction(_this->egl_data->egl_dll_handle, "CreateWinrtEglWindow");
    66     if (CreateWinrtEglWindow) {
    67         /* 'CreateWinrtEglWindow' was found, which means that an an older
    68          * version of ANGLE/WinRT is being used.  Continue setting up EGL,
    69          * as appropriate to this version of ANGLE.
    70          */
    71 
    72         /* Create an ANGLE/WinRT EGL-window */
    73         /* TODO, WinRT: check for XAML usage before accessing the CoreWindow, as not doing so could lead to a crash */
    74         CoreWindow ^ native_win = CoreWindow::GetForCurrentThread();
    75         Microsoft::WRL::ComPtr<IUnknown> cpp_win = reinterpret_cast<IUnknown *>(native_win);
    76         HRESULT result = CreateWinrtEglWindow(cpp_win, ANGLE_D3D_FEATURE_LEVEL_ANY, &(video_data->winrtEglWindow));
    77         if (FAILED(result)) {
    78             return -1;
    79         }
    80 
    81         /* Call eglGetDisplay and eglInitialize as appropriate.  On other
    82          * platforms, this would probably get done by SDL_EGL_LoadLibrary,
    83          * however ANGLE/WinRT's current implementation (as of Mar 22, 2014) of
    84          * eglGetDisplay requires that a C++ object be passed into it, so the
    85          * call will be made in this file, a C++ file, instead.
    86          */
    87         Microsoft::WRL::ComPtr<IUnknown> cpp_display = video_data->winrtEglWindow;
    88         _this->egl_data->egl_display = ((eglGetDisplay_Old_Function)_this->egl_data->eglGetDisplay)(cpp_display);
    89         if (!_this->egl_data->egl_display) {
    90             return SDL_SetError("Could not get Windows 8.0 EGL display");
    91         }
    92 
    93         if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
    94             return SDL_SetError("Could not initialize Windows 8.0 EGL");
    95         }
    96     } else {
    97         /* Declare some ANGLE/EGL initialization property-sets, as suggested by
    98          * MSOpenTech's ANGLE-for-WinRT template apps:
    99          */
   100         const EGLint defaultDisplayAttributes[] =
   101         {
   102             EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
   103             EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
   104             EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
   105             EGL_NONE,
   106         };
   107 
   108         const EGLint fl9_3DisplayAttributes[] =
   109         {
   110             EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
   111             EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, 9,
   112             EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, 3,
   113             EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
   114             EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
   115             EGL_NONE,
   116         };
   117 
   118         const EGLint warpDisplayAttributes[] =
   119         {
   120             EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
   121             EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE,
   122             EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
   123             EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
   124             EGL_NONE,
   125         };
   126 
   127         /* 'CreateWinrtEglWindow' was NOT found, which either means that a
   128          * newer version of ANGLE/WinRT is being used, or that we don't have
   129          * a valid copy of ANGLE.
   130          *
   131          * Try loading ANGLE as if it were the newer version.
   132          */
   133         eglGetPlatformDisplayEXT_Function eglGetPlatformDisplayEXT = (eglGetPlatformDisplayEXT_Function)_this->egl_data->eglGetProcAddress("eglGetPlatformDisplayEXT");
   134         if (!eglGetPlatformDisplayEXT) {
   135             return SDL_SetError("Could not retrieve ANGLE/WinRT display function(s)");
   136         }
   137 
   138 #if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP)
   139         /* Try initializing EGL at D3D11 Feature Level 10_0+ (which is not
   140          * supported on WinPhone 8.x.
   141          */
   142         _this->egl_data->egl_display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, defaultDisplayAttributes);
   143         if (!_this->egl_data->egl_display) {
   144             return SDL_SetError("Could not get 10_0+ EGL display");
   145         }
   146 
   147         if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE)
   148 #endif
   149         {
   150             /* Try initializing EGL at D3D11 Feature Level 9_3, in case the
   151              * 10_0 init fails, or we're on Windows Phone (which only supports
   152              * 9_3).
   153              */
   154             _this->egl_data->egl_display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, fl9_3DisplayAttributes);
   155             if (!_this->egl_data->egl_display) {
   156                 return SDL_SetError("Could not get 9_3 EGL display");
   157             }
   158 
   159             if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
   160                 /* Try initializing EGL at D3D11 Feature Level 11_0 on WARP
   161                  * (a Windows-provided, software rasterizer) if all else fails.
   162                  */
   163                 _this->egl_data->egl_display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, warpDisplayAttributes);
   164                 if (!_this->egl_data->egl_display) {
   165                     return SDL_SetError("Could not get WARP EGL display");
   166                 }
   167 
   168                 if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
   169                     return SDL_SetError("Could not initialize WinRT 8.x+ EGL");
   170                 }
   171             }
   172         }
   173     }
   174 
   175     return 0;
   176 }
   177 
   178 extern "C" void
   179 WINRT_GLES_UnloadLibrary(_THIS)
   180 {
   181     SDL_VideoData *video_data = (SDL_VideoData *)_this->driverdata;
   182 
   183     /* Release SDL's own COM reference to the ANGLE/WinRT IWinrtEglWindow */
   184     if (video_data->winrtEglWindow) {
   185         video_data->winrtEglWindow->Release();
   186         video_data->winrtEglWindow = nullptr;
   187     }
   188 
   189     /* Perform the bulk of the unloading */
   190     SDL_EGL_UnloadLibrary(_this);
   191 }
   192 
   193 extern "C" {
   194 SDL_EGL_CreateContext_impl(WINRT)
   195 SDL_EGL_SwapWindow_impl(WINRT)
   196 SDL_EGL_MakeCurrent_impl(WINRT)
   197 }
   198 
   199 #endif /* SDL_VIDEO_DRIVER_WINRT && SDL_VIDEO_OPENGL_EGL */
   200 
   201 /* vi: set ts=4 sw=4 expandtab: */
   202