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