src/video/winrt/SDL_winrtopengles.cpp
author Ryan C. Gordon <icculus@icculus.org>
Thu, 09 Apr 2015 22:28:37 -0400
changeset 9541 cf8fab52e33b
parent 9386 65e802af5d0e
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Merged Alex Szpakowski's iOS-improvement branch to default.

Fixes Bugzilla #2798.
Fixes Bugzilla #2212.
Fixes Bugzilla #2826.
Fixes Bugzilla #2661.
Fixes Bugzilla #1885.
Fixes Bugzilla #1578.
Fixes Bugzilla #2751.

(whew!)

Notable changes, from Alex's notes:

- The SDL_WINDOW_ALLOW_HIGHDPI flag is now needed (along with SDL_GL_GetDrawableSize or SDL_GetRendererOutputSize) to use Retina / high DPI resolutions, bringing SDL’s Retina-related behavior on iOS in line with Mac OS X. Window dimensions and display modes are now in the “points” (non-high DPI) coordinate system rather than pixels, whereas SDL_GL_GetDrawableSize is in pixels.

- Reworked the custom extended launch screen code:
- It now hides after the first SDL_PumpEvents call rather than SDL_CreateWindow, and it fades out in a similar manner to the system launch screen behavior.
- It now mirrors the system launch screen behavior when deciding which image to display: it falls back to using the Launch Images dictionary in Info.plist if the iOS 8+ launch screen nib isn’t available, and if the Launch Images dictionary doesn’t exist it uses the old standard launch image names.
- The extended launch screen can now be disabled via the SDL_IPHONE_LAUNCHSCREEN define in SDL_config_iphoneos.h.

- Added support for SDL_HINT_ACCELEROMETER_AS_JOYSTICK.

- Added access to a window view's renderbuffer and framebuffer to syswm.

- Added OpenGL ES debug labels for the Renderbuffer and Framebuffer Objects created with SDL_GL_CreateContext.

- Added support for sRGB OpenGL ES contexts on iOS 7+.

- Updated OpenGL ES contexts to support native-resolution rendering (when SDL_WINDOW_ALLOW_HIGHDPI is enabled) on the iPhone 6 Plus, i.e. 1080x1920 rather than 1242x2208.

- Updated SDL_GL_CreateContext, SDL_GL_SwapWindow, SDL_GL_MakeCurrent, and SDL_GL_DeleteContext to be more robust.

- Updated SDL windows to display a UIView at all times, even when an OpenGL context is not active. This allows rotation, touch events, and other windowing-related events to work properly without an active OpenGL context. It also makes it easier to use SDL_GetWindowWMInfo after creating a SDL window.

- Updated the iOS-specific Objective-C code to use cleaner and more modern language features and APIs, including ARC instead of manual reference counting.

- Updated SDL_HINT_ORIENTATIONS to allow disabling custom orientations if the hint is set with no valid orientation names.

- Fixed several rotation and orientation bugs with windows and display modes, especially in iOS 8+.

- Fixed SDL_SetWindowFullscreen failing to update the status bar visibility on iOS 7+.

- Fixed the orientation of the offset applied to the window’s view when the onscreen keyboard is shown in iOS 8+.

- Fixed SDL_IsScreenKeyboardShown (patch by Phil Hassey.)

- Fixed several major memory leaks caused by missing autorelease pool blocks in the iOS-specific Objective-C code.

- Removed several dead code paths.

- The iOS 7 SDK (Xcode 5) or newer is now required to build SDL for iOS.
     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                        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