src/video/winrt/SDL_winrtvideo.cpp
author David Ludwig <dludwig@pobox.com>
Mon, 04 Nov 2013 19:54:29 -0500
changeset 8541 0041edb891f3
parent 8527 e3807733fad5
child 8551 666f05b079ea
permissions -rw-r--r--
WinRT: added experimental OpenGL ES 2.0 support

A port of the ANGLE library (OpenGL ES 2.0 for Direct3D) to WinRT, via https://github.com/stammen/angleproject, is used as a base.

To enable, clone 'angleproject' into the directory one above where SDL/WinRT is, open the file SDL/include/SDL_config_winrt.h, and uncomment the #defines that begin with 'SDL_VIDEO_OPENGL'. From there, apps can create an OpenGL capable SDL_Window via the flag, SDL_WINDOW_OPENGL, and an OpenGL ES 2 context via SDL_GL_CreateContext. The Direct3D 11.1 renderer cannot be used alongside SDL_WINDOW_OPENGL. Only Windows 8/8.1 is supported for now. Shaders may need to be precompiled, in some (all?) cases.
dludwig@8327
     1
/*
dludwig@8327
     2
  Simple DirectMedia Layer
dludwig@8327
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
dludwig@8327
     4
dludwig@8327
     5
  This software is provided 'as-is', without any express or implied
dludwig@8327
     6
  warranty.  In no event will the authors be held liable for any damages
dludwig@8327
     7
  arising from the use of this software.
dludwig@8327
     8
dludwig@8327
     9
  Permission is granted to anyone to use this software for any purpose,
dludwig@8327
    10
  including commercial applications, and to alter it and redistribute it
dludwig@8327
    11
  freely, subject to the following restrictions:
dludwig@8327
    12
dludwig@8327
    13
  1. The origin of this software must not be misrepresented; you must not
dludwig@8327
    14
     claim that you wrote the original software. If you use this software
dludwig@8327
    15
     in a product, an acknowledgment in the product documentation would be
dludwig@8327
    16
     appreciated but is not required.
dludwig@8327
    17
  2. Altered source versions must be plainly marked as such, and must not be
dludwig@8327
    18
     misrepresented as being the original software.
dludwig@8327
    19
  3. This notice may not be removed or altered from any source distribution.
dludwig@8327
    20
*/
dludwig@8327
    21
#include "SDL_config.h"
dludwig@8327
    22
dludwig@8327
    23
#if SDL_VIDEO_DRIVER_WINRT
dludwig@8327
    24
dludwig@8327
    25
/* WinRT SDL video driver implementation
dludwig@8327
    26
dludwig@8327
    27
   Initial work on this was done by David Ludwig (dludwig@pobox.com), and
dludwig@8327
    28
   was based off of SDL's "dummy" video driver.
dludwig@8327
    29
 */
dludwig@8327
    30
dludwig@8494
    31
/* Windows includes */
dludwig@8494
    32
#include <agile.h>
dludwig@8494
    33
using namespace Windows::UI::Core;
dludwig@8494
    34
dludwig@8494
    35
dludwig@8494
    36
/* SDL includes */
dludwig@8329
    37
extern "C" {
dludwig@8327
    38
#include "SDL_video.h"
dludwig@8327
    39
#include "SDL_mouse.h"
dludwig@8327
    40
#include "../SDL_sysvideo.h"
dludwig@8327
    41
#include "../SDL_pixels_c.h"
dludwig@8327
    42
#include "../../events/SDL_events_c.h"
dludwig@8400
    43
#include "../../render/SDL_sysrender.h"
dludwig@8411
    44
#include "SDL_syswm.h"
dludwig@8541
    45
#include "SDL_winrtopengles.h"
dludwig@8329
    46
}
dludwig@8327
    47
dludwig@8522
    48
#include "../../core/winrt/SDL_winrtapp_direct3d.h"
dludwig@8522
    49
#include "../../core/winrt/SDL_winrtapp_xaml.h"
dludwig@8512
    50
#include "SDL_winrtvideo_cpp.h"
dludwig@8327
    51
#include "SDL_winrtevents_c.h"
dludwig@8516
    52
#include "SDL_winrtmouse_c.h"
dludwig@8505
    53
#include "SDL_main.h"
dludwig@8505
    54
#include "SDL_system.h"
dludwig@8327
    55
dludwig@8327
    56
dludwig@8327
    57
/* Initialization/Query functions */
dludwig@8327
    58
static int WINRT_VideoInit(_THIS);
dludwig@8374
    59
static int WINRT_InitModes(_THIS);
dludwig@8327
    60
static int WINRT_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
dludwig@8327
    61
static void WINRT_VideoQuit(_THIS);
dludwig@8327
    62
dludwig@8494
    63
dludwig@8333
    64
/* Window functions */
dludwig@8522
    65
static int WINRT_CreateWindow(_THIS, SDL_Window * window);
dludwig@8333
    66
static void WINRT_DestroyWindow(_THIS, SDL_Window * window);
dludwig@8411
    67
static SDL_bool WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info);
dludwig@8333
    68
dludwig@8494
    69
dludwig@8515
    70
/* SDL-internal globals: */
dludwig@8497
    71
SDL_Window * WINRT_GlobalSDLWindow = NULL;
dludwig@8498
    72
SDL_VideoDevice * WINRT_GlobalSDLVideoDevice = NULL;
dludwig@8498
    73
dludwig@8498
    74
dludwig@8327
    75
/* WinRT driver bootstrap functions */
dludwig@8327
    76
dludwig@8327
    77
static int
dludwig@8327
    78
WINRT_Available(void)
dludwig@8327
    79
{
dludwig@8328
    80
    return (1);
dludwig@8327
    81
}
dludwig@8327
    82
dludwig@8327
    83
static void
dludwig@8327
    84
WINRT_DeleteDevice(SDL_VideoDevice * device)
dludwig@8327
    85
{
dludwig@8498
    86
    if (device == WINRT_GlobalSDLVideoDevice) {
dludwig@8498
    87
        WINRT_GlobalSDLVideoDevice = NULL;
dludwig@8498
    88
    }
dludwig@8327
    89
    SDL_free(device);
dludwig@8327
    90
}
dludwig@8327
    91
dludwig@8327
    92
static SDL_VideoDevice *
dludwig@8327
    93
WINRT_CreateDevice(int devindex)
dludwig@8327
    94
{
dludwig@8327
    95
    SDL_VideoDevice *device;
dludwig@8327
    96
dludwig@8327
    97
    /* Initialize all variables that we clean on shutdown */
dludwig@8327
    98
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
dludwig@8327
    99
    if (!device) {
dludwig@8327
   100
        SDL_OutOfMemory();
dludwig@8327
   101
        if (device) {
dludwig@8327
   102
            SDL_free(device);
dludwig@8327
   103
        }
dludwig@8327
   104
        return (0);
dludwig@8327
   105
    }
dludwig@8327
   106
dludwig@8327
   107
    /* Set the function pointers */
dludwig@8327
   108
    device->VideoInit = WINRT_VideoInit;
dludwig@8327
   109
    device->VideoQuit = WINRT_VideoQuit;
dludwig@8333
   110
    device->CreateWindow = WINRT_CreateWindow;
dludwig@8333
   111
    device->DestroyWindow = WINRT_DestroyWindow;
dludwig@8327
   112
    device->SetDisplayMode = WINRT_SetDisplayMode;
dludwig@8327
   113
    device->PumpEvents = WINRT_PumpEvents;
dludwig@8411
   114
    device->GetWindowWMInfo = WINRT_GetWindowWMInfo;
dludwig@8541
   115
#ifdef SDL_VIDEO_OPENGL_EGL
dludwig@8541
   116
    device->GL_LoadLibrary = WINRT_GLES_LoadLibrary;
dludwig@8541
   117
    device->GL_GetProcAddress = WINRT_GLES_GetProcAddress;
dludwig@8541
   118
    device->GL_UnloadLibrary = WINRT_GLES_UnloadLibrary;
dludwig@8541
   119
    device->GL_CreateContext = WINRT_GLES_CreateContext;
dludwig@8541
   120
    device->GL_MakeCurrent = WINRT_GLES_MakeCurrent;
dludwig@8541
   121
    device->GL_SetSwapInterval = WINRT_GLES_SetSwapInterval;
dludwig@8541
   122
    device->GL_GetSwapInterval = WINRT_GLES_GetSwapInterval;
dludwig@8541
   123
    device->GL_SwapWindow = WINRT_GLES_SwapWindow;
dludwig@8541
   124
    device->GL_DeleteContext = WINRT_GLES_DeleteContext;
dludwig@8541
   125
#endif
dludwig@8433
   126
    device->free = WINRT_DeleteDevice;
dludwig@8501
   127
    WINRT_GlobalSDLVideoDevice = device;
dludwig@8327
   128
dludwig@8327
   129
    return device;
dludwig@8327
   130
}
dludwig@8327
   131
dludwig@8494
   132
#define WINRTVID_DRIVER_NAME "winrt"
dludwig@8327
   133
VideoBootStrap WINRT_bootstrap = {
dludwig@8500
   134
    WINRTVID_DRIVER_NAME, "SDL WinRT video driver",
dludwig@8327
   135
    WINRT_Available, WINRT_CreateDevice
dludwig@8327
   136
};
dludwig@8327
   137
dludwig@8327
   138
int
dludwig@8327
   139
WINRT_VideoInit(_THIS)
dludwig@8327
   140
{
dludwig@8374
   141
    if (WINRT_InitModes(_this) < 0) {
dludwig@8374
   142
        return -1;
dludwig@8374
   143
    }
dludwig@8374
   144
    WINRT_InitMouse(_this);
dludwig@8515
   145
    WINRT_InitTouch(_this);
dludwig@8522
   146
dludwig@8374
   147
    return 0;
dludwig@8374
   148
}
dludwig@8374
   149
dludwig@8522
   150
SDL_DisplayMode
dludwig@8522
   151
WINRT_CalcDisplayModeUsingNativeWindow()
dludwig@8522
   152
{
dludwig@8522
   153
    using namespace Windows::Graphics::Display;
dludwig@8522
   154
dludwig@8522
   155
    // Create an empty, zeroed-out display mode:
dludwig@8522
   156
    SDL_DisplayMode mode;
dludwig@8522
   157
    SDL_zero(mode);
dludwig@8522
   158
dludwig@8522
   159
    // Go no further if a native window cannot be accessed.  This can happen,
dludwig@8522
   160
    // for example, if this function is called from certain threads, such as
dludwig@8522
   161
    // the SDL/XAML thread.
dludwig@8522
   162
    if (!CoreWindow::GetForCurrentThread()) {
dludwig@8522
   163
        return mode;
dludwig@8522
   164
    }
dludwig@8522
   165
dludwig@8522
   166
    // Fill in most fields:
dludwig@8522
   167
    mode.format = SDL_PIXELFORMAT_RGB888;
dludwig@8522
   168
    mode.refresh_rate = 0;  // TODO, WinRT: see if refresh rate data is available, or relevant (for WinRT apps)
dludwig@8522
   169
    mode.driverdata = (void *) DisplayProperties::CurrentOrientation;
dludwig@8522
   170
dludwig@8522
   171
    // Calculate the display size given the window size, taking into account
dludwig@8522
   172
    // the current display's DPI:
dludwig@8522
   173
    const float currentDPI = Windows::Graphics::Display::DisplayProperties::LogicalDpi; 
dludwig@8522
   174
    const float dipsPerInch = 96.0f;
dludwig@8522
   175
    mode.w = (int) ((CoreWindow::GetForCurrentThread()->Bounds.Width * currentDPI) / dipsPerInch);
dludwig@8522
   176
    mode.h = (int) ((CoreWindow::GetForCurrentThread()->Bounds.Height * currentDPI) / dipsPerInch);
dludwig@8522
   177
dludwig@8522
   178
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8522
   179
    // On Windows Phone, the native window's size is always in portrait,
dludwig@8522
   180
    // regardless of the device's orientation.  This is in contrast to
dludwig@8522
   181
    // Windows 8/RT, which will resize the native window as the device's
dludwig@8522
   182
    // orientation changes.  In order to compensate for this behavior,
dludwig@8522
   183
    // on Windows Phone, the mode's width and height will be swapped when
dludwig@8522
   184
    // the device is in a landscape (non-portrait) mode.
dludwig@8522
   185
    switch (DisplayProperties::CurrentOrientation) {
dludwig@8522
   186
        case DisplayOrientations::Landscape:
dludwig@8522
   187
        case DisplayOrientations::LandscapeFlipped:
dludwig@8522
   188
        {
dludwig@8522
   189
            const int tmp = mode.h;
dludwig@8522
   190
            mode.h = mode.w;
dludwig@8522
   191
            mode.w = tmp;
dludwig@8522
   192
            break;
dludwig@8522
   193
        }
dludwig@8522
   194
dludwig@8522
   195
        default:
dludwig@8522
   196
            break;
dludwig@8522
   197
    }
dludwig@8522
   198
dludwig@8522
   199
    // Attach the mode to te
dludwig@8522
   200
#endif
dludwig@8522
   201
dludwig@8522
   202
    return mode;
dludwig@8522
   203
}
dludwig@8503
   204
dludwig@8505
   205
int
dludwig@8374
   206
WINRT_InitModes(_THIS)
dludwig@8374
   207
{
dludwig@8505
   208
    // Retrieve the display mode:
dludwig@8522
   209
    SDL_DisplayMode mode = WINRT_CalcDisplayModeUsingNativeWindow();
dludwig@8522
   210
    if (mode.w == 0 || mode.h == 0) {
dludwig@8522
   211
        return SDL_SetError("Unable to calculate the WinRT window/display's size");
dludwig@8522
   212
    }
dludwig@8522
   213
dludwig@8522
   214
    if (SDL_AddBasicVideoDisplay(&mode) < 0) {
dludwig@8522
   215
        return -1;
dludwig@8374
   216
    }
dludwig@8374
   217
dludwig@8374
   218
    SDL_AddDisplayMode(&_this->displays[0], &mode);
dludwig@8327
   219
    return 0;
dludwig@8327
   220
}
dludwig@8327
   221
dludwig@8327
   222
static int
dludwig@8327
   223
WINRT_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
dludwig@8327
   224
{
dludwig@8327
   225
    return 0;
dludwig@8327
   226
}
dludwig@8327
   227
dludwig@8327
   228
void
dludwig@8327
   229
WINRT_VideoQuit(_THIS)
dludwig@8327
   230
{
dludwig@8374
   231
    WINRT_QuitMouse(_this);
dludwig@8327
   232
}
dludwig@8327
   233
dludwig@8522
   234
int
dludwig@8522
   235
WINRT_CreateWindow(_THIS, SDL_Window * window)
dludwig@8522
   236
{
dludwig@8522
   237
    // Make sure that only one window gets created, at least until multimonitor
dludwig@8522
   238
    // support is added.
dludwig@8522
   239
    if (WINRT_GlobalSDLWindow != NULL) {
dludwig@8522
   240
        SDL_SetError("WinRT only supports one window");
dludwig@8522
   241
        return -1;
dludwig@8522
   242
    }
dludwig@8522
   243
dludwig@8522
   244
    SDL_WindowData *data = new SDL_WindowData;
dludwig@8522
   245
    if (!data) {
dludwig@8522
   246
        SDL_OutOfMemory();
dludwig@8522
   247
        return -1;
dludwig@8522
   248
    }
dludwig@8522
   249
    window->driverdata = data;
dludwig@8522
   250
    data->sdlWindow = window;
dludwig@8522
   251
dludwig@8522
   252
    /* To note, when XAML support is enabled, access to the CoreWindow will not
dludwig@8522
   253
       be possible, at least not via the SDL/XAML thread.  Attempts to access it
dludwig@8522
   254
       from there will throw exceptions.  As such, the SDL_WindowData's
dludwig@8522
   255
       'coreWindow' field will only be set (to a non-null value) if XAML isn't
dludwig@8522
   256
       enabled.
dludwig@8522
   257
    */
dludwig@8522
   258
    if (!WINRT_XAMLWasEnabled) {
dludwig@8522
   259
        data->coreWindow = CoreWindow::GetForCurrentThread();
dludwig@8522
   260
    }
dludwig@8522
   261
dludwig@8541
   262
#if SDL_VIDEO_OPENGL_EGL
dludwig@8541
   263
    /* Setup the EGL surface, but only if OpenGL ES 2 was requested. */
dludwig@8541
   264
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
dludwig@8541
   265
        /* OpenGL ES 2 wasn't requested.  Don't set up an EGL surface. */
dludwig@8541
   266
        data->egl_surface = EGL_NO_SURFACE;
dludwig@8541
   267
    } else {
dludwig@8541
   268
        /* OpenGL ES 2 was reuqested.  Set up an EGL surface. */
dludwig@8541
   269
        IUnknown * nativeWindow = reinterpret_cast<IUnknown *>(data->coreWindow.Get());
dludwig@8541
   270
        data->egl_surface = SDL_EGL_CreateSurface(_this, nativeWindow);
dludwig@8541
   271
        if (data->egl_surface == NULL) {
dludwig@8541
   272
            // TODO, WinRT: see if SDL_EGL_CreateSurface, or its callee(s), sets an error message.  If so, attach it to the SDL error.
dludwig@8541
   273
            return SDL_SetError("SDL_EGL_CreateSurface failed");
dludwig@8541
   274
        }
dludwig@8541
   275
    }
dludwig@8541
   276
#endif
dludwig@8541
   277
dludwig@8522
   278
    /* Make sure the window is considered to be positioned at {0,0},
dludwig@8522
   279
       and is considered fullscreen, shown, and the like.
dludwig@8522
   280
    */
dludwig@8522
   281
    window->x = 0;
dludwig@8522
   282
    window->y = 0;
dludwig@8522
   283
    window->flags =
dludwig@8522
   284
        SDL_WINDOW_FULLSCREEN |
dludwig@8522
   285
        SDL_WINDOW_SHOWN |
dludwig@8522
   286
        SDL_WINDOW_BORDERLESS |
dludwig@8522
   287
        SDL_WINDOW_MAXIMIZED |
dludwig@8522
   288
        SDL_WINDOW_INPUT_GRABBED;
dludwig@8522
   289
dludwig@8541
   290
#if SDL_VIDEO_OPENGL_EGL
dludwig@8541
   291
    if (data->egl_surface) {
dludwig@8541
   292
        window->flags |= SDL_WINDOW_OPENGL;
dludwig@8541
   293
    }
dludwig@8541
   294
#endif
dludwig@8541
   295
dludwig@8522
   296
    /* WinRT does not, as of this writing, appear to support app-adjustable
dludwig@8522
   297
       window sizes.  Set the window size to whatever the native WinRT
dludwig@8522
   298
       CoreWindow is set at.
dludwig@8522
   299
dludwig@8522
   300
       TODO, WinRT: if and when non-fullscreen XAML control support is added to SDL, consider making those resizable via SDL_Window's interfaces.
dludwig@8522
   301
    */
dludwig@8522
   302
    window->w = _this->displays[0].current_mode.w;
dludwig@8522
   303
    window->h = _this->displays[0].current_mode.h;
dludwig@8522
   304
 
dludwig@8522
   305
    /* Make sure the WinRT app's IFramworkView can post events on
dludwig@8522
   306
       behalf of SDL:
dludwig@8522
   307
    */
dludwig@8522
   308
    WINRT_GlobalSDLWindow = window;
dludwig@8522
   309
dludwig@8522
   310
    /* All done! */
dludwig@8522
   311
    return 0;
dludwig@8522
   312
}
dludwig@8522
   313
dludwig@8333
   314
void
dludwig@8333
   315
WINRT_DestroyWindow(_THIS, SDL_Window * window)
dludwig@8333
   316
{
dludwig@8411
   317
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8411
   318
dludwig@8497
   319
    if (WINRT_GlobalSDLWindow == window) {
dludwig@8497
   320
        WINRT_GlobalSDLWindow = NULL;
dludwig@8424
   321
    }
dludwig@8424
   322
dludwig@8411
   323
    if (data) {
dludwig@8411
   324
        // Delete the internal window data:
dludwig@8411
   325
        delete data;
dludwig@8411
   326
        data = NULL;
dludwig@8411
   327
    }
dludwig@8333
   328
}
dludwig@8333
   329
dludwig@8522
   330
SDL_bool
dludwig@8522
   331
WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
dludwig@8522
   332
{
dludwig@8522
   333
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8522
   334
dludwig@8522
   335
    if (info->version.major <= SDL_MAJOR_VERSION) {
dludwig@8522
   336
        info->subsystem = SDL_SYSWM_WINRT;
dludwig@8527
   337
        info->info.winrt.window = reinterpret_cast<IInspectable *>(data->coreWindow.Get());
dludwig@8522
   338
        return SDL_TRUE;
dludwig@8522
   339
    } else {
dludwig@8522
   340
        SDL_SetError("Application not compiled with SDL %d.%d\n",
dludwig@8522
   341
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
dludwig@8522
   342
        return SDL_FALSE;
dludwig@8522
   343
    }
dludwig@8522
   344
    return SDL_FALSE;
dludwig@8411
   345
}
dludwig@8333
   346
dludwig@8327
   347
#endif /* SDL_VIDEO_DRIVER_WINRT */
dludwig@8327
   348
dludwig@8327
   349
/* vi: set ts=4 sw=4 expandtab: */