src/video/nacl/SDL_naclvideo.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 06 Apr 2015 00:10:54 -0400
changeset 9458 543298b36b28
parent 8873 77ffe680093f
child 9619 b94b6d0bff0f
permissions -rw-r--r--
This function can be static.
gabomdq@8833
     1
/*
gabomdq@8833
     2
  Simple DirectMedia Layer
gabomdq@8833
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
gabomdq@8833
     4
gabomdq@8833
     5
  This software is provided 'as-is', without any express or implied
gabomdq@8833
     6
  warranty.  In no event will the authors be held liable for any damages
gabomdq@8833
     7
  arising from the use of this software.
gabomdq@8833
     8
gabomdq@8833
     9
  Permission is granted to anyone to use this software for any purpose,
gabomdq@8833
    10
  including commercial applications, and to alter it and redistribute it
gabomdq@8833
    11
  freely, subject to the following restrictions:
gabomdq@8833
    12
gabomdq@8833
    13
  1. The origin of this software must not be misrepresented; you must not
gabomdq@8833
    14
     claim that you wrote the original software. If you use this software
gabomdq@8833
    15
     in a product, an acknowledgment in the product documentation would be
gabomdq@8833
    16
     appreciated but is not required.
gabomdq@8833
    17
  2. Altered source versions must be plainly marked as such, and must not be
gabomdq@8833
    18
     misrepresented as being the original software.
gabomdq@8833
    19
  3. This notice may not be removed or altered from any source distribution.
gabomdq@8833
    20
*/
gabomdq@8833
    21
#include "../../SDL_internal.h"
gabomdq@8833
    22
gabomdq@8833
    23
#if SDL_VIDEO_DRIVER_NACL
gabomdq@8833
    24
gabomdq@8833
    25
#include "ppapi/c/pp_errors.h"
gabomdq@8833
    26
#include "ppapi/c/pp_instance.h"
gabomdq@8833
    27
#include "ppapi_simple/ps.h"
gabomdq@8833
    28
#include "ppapi_simple/ps_interface.h"
gabomdq@8833
    29
#include "ppapi_simple/ps_event.h"
gabomdq@8833
    30
#include "nacl_io/nacl_io.h"
gabomdq@8833
    31
gabomdq@8833
    32
#include "SDL_naclvideo.h"
gabomdq@8833
    33
#include "SDL_naclwindow.h"
gabomdq@8833
    34
#include "SDL_naclevents_c.h"
gabomdq@8833
    35
#include "SDL_naclopengles.h"   
gabomdq@8833
    36
#include "SDL_video.h"
gabomdq@8833
    37
#include "../SDL_sysvideo.h"
gabomdq@8833
    38
#include "../../events/SDL_events_c.h"
gabomdq@8833
    39
gabomdq@8833
    40
#define NACLVID_DRIVER_NAME "nacl"
gabomdq@8833
    41
gabomdq@8833
    42
/* Static init required because NACL_SetScreenResolution 
gabomdq@8833
    43
 * may appear even before SDL starts and we want to remember
gabomdq@8833
    44
 * the window width and height
gabomdq@8833
    45
 */
gabomdq@8833
    46
static SDL_VideoData nacl = {0};
gabomdq@8833
    47
gabomdq@8833
    48
void
gabomdq@8833
    49
NACL_SetScreenResolution(int width, int height, Uint32 format)
gabomdq@8833
    50
{
gabomdq@8833
    51
    PP_Resource context;
gabomdq@8833
    52
    
gabomdq@8833
    53
    nacl.w = width;
gabomdq@8833
    54
    nacl.h = height;   
gabomdq@8833
    55
    nacl.format = format;
gabomdq@8833
    56
    
gabomdq@8833
    57
    if (nacl.window) {
gabomdq@8833
    58
        nacl.window->w = width;
gabomdq@8833
    59
        nacl.window->h = height;
gabomdq@8833
    60
        SDL_SendWindowEvent(nacl.window, SDL_WINDOWEVENT_RESIZED, width, height);
gabomdq@8833
    61
    }
gabomdq@8833
    62
    
gabomdq@8833
    63
    /* FIXME: Check threading issues...otherwise use a hardcoded _this->context across all threads */
gabomdq@8833
    64
    context = (PP_Resource) SDL_GL_GetCurrentContext();
gabomdq@8833
    65
    if (context) {
gabomdq@8833
    66
        PSInterfaceGraphics3D()->ResizeBuffers(context, width, height);
gabomdq@8833
    67
    }
gabomdq@8833
    68
gabomdq@8833
    69
}
gabomdq@8833
    70
gabomdq@8833
    71
gabomdq@8833
    72
gabomdq@8833
    73
/* Initialization/Query functions */
gabomdq@8833
    74
static int NACL_VideoInit(_THIS);
gabomdq@8833
    75
static void NACL_VideoQuit(_THIS);
gabomdq@8833
    76
gabomdq@8833
    77
static int NACL_Available(void) {
gabomdq@8833
    78
    return PSGetInstanceId() != 0;
gabomdq@8833
    79
}
gabomdq@8833
    80
gabomdq@8833
    81
static void NACL_DeleteDevice(SDL_VideoDevice *device) {
gabomdq@8833
    82
    SDL_VideoData *driverdata = (SDL_VideoData*) device->driverdata;
gabomdq@8833
    83
    driverdata->ppb_core->ReleaseResource((PP_Resource) driverdata->ppb_message_loop);
gabomdq@8833
    84
    SDL_free(device->driverdata);
gabomdq@8833
    85
    SDL_free(device);
gabomdq@8833
    86
}
gabomdq@8833
    87
gabomdq@8833
    88
static int
gabomdq@8833
    89
NACL_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
gabomdq@8833
    90
{
gabomdq@8833
    91
    return 0;
gabomdq@8833
    92
}
gabomdq@8833
    93
gabomdq@8833
    94
static SDL_VideoDevice *NACL_CreateDevice(int devindex) {
gabomdq@8833
    95
    SDL_VideoDevice *device;
gabomdq@8833
    96
    
gabomdq@8833
    97
    /* Initialize all variables that we clean on shutdown */
gabomdq@8833
    98
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
gabomdq@8833
    99
    if (!device) {
gabomdq@8833
   100
        SDL_OutOfMemory();
gabomdq@8833
   101
        return NULL;
gabomdq@8833
   102
    }
gabomdq@8833
   103
    device->driverdata = &nacl;
gabomdq@8833
   104
  
gabomdq@8833
   105
    /* Set the function pointers */
gabomdq@8833
   106
    device->VideoInit = NACL_VideoInit;
gabomdq@8833
   107
    device->VideoQuit = NACL_VideoQuit;
gabomdq@8833
   108
    device->PumpEvents = NACL_PumpEvents;
gabomdq@8833
   109
    
gabomdq@8833
   110
    device->CreateWindow = NACL_CreateWindow;
gabomdq@8833
   111
    device->SetWindowTitle = NACL_SetWindowTitle;
gabomdq@8833
   112
    device->DestroyWindow = NACL_DestroyWindow;
gabomdq@8833
   113
    
gabomdq@8833
   114
    device->SetDisplayMode = NACL_SetDisplayMode;
gabomdq@8833
   115
    
gabomdq@8833
   116
    device->free = NACL_DeleteDevice;
gabomdq@8833
   117
    
gabomdq@8833
   118
    /* GL pointers */
gabomdq@8833
   119
    device->GL_LoadLibrary = NACL_GLES_LoadLibrary;
gabomdq@8833
   120
    device->GL_GetProcAddress = NACL_GLES_GetProcAddress;
gabomdq@8833
   121
    device->GL_UnloadLibrary = NACL_GLES_UnloadLibrary;
gabomdq@8833
   122
    device->GL_CreateContext = NACL_GLES_CreateContext;
gabomdq@8833
   123
    device->GL_MakeCurrent = NACL_GLES_MakeCurrent;
gabomdq@8833
   124
    device->GL_SetSwapInterval = NACL_GLES_SetSwapInterval;
gabomdq@8833
   125
    device->GL_GetSwapInterval = NACL_GLES_GetSwapInterval;
gabomdq@8833
   126
    device->GL_SwapWindow = NACL_GLES_SwapWindow;
gabomdq@8833
   127
    device->GL_DeleteContext = NACL_GLES_DeleteContext;
gabomdq@8833
   128
    
gabomdq@8833
   129
    
gabomdq@8833
   130
    return device;
gabomdq@8833
   131
}
gabomdq@8833
   132
gabomdq@8833
   133
VideoBootStrap NACL_bootstrap = {
gabomdq@8833
   134
    NACLVID_DRIVER_NAME, "SDL Native Client Video Driver",
gabomdq@8833
   135
    NACL_Available, NACL_CreateDevice
gabomdq@8833
   136
};
gabomdq@8833
   137
gabomdq@8833
   138
int NACL_VideoInit(_THIS) {
gabomdq@8833
   139
    SDL_VideoData *driverdata = (SDL_VideoData *) _this->driverdata;
gabomdq@8833
   140
    SDL_DisplayMode mode;
gabomdq@8833
   141
gabomdq@8873
   142
    SDL_zero(mode);
gabomdq@8833
   143
    mode.format = driverdata->format;
gabomdq@8833
   144
    mode.w = driverdata->w;
gabomdq@8833
   145
    mode.h = driverdata->h;
gabomdq@8833
   146
    mode.refresh_rate = 0;
gabomdq@8833
   147
    mode.driverdata = NULL;
gabomdq@8833
   148
    if (SDL_AddBasicVideoDisplay(&mode) < 0) {
gabomdq@8833
   149
        return -1;
gabomdq@8833
   150
    }
gabomdq@8873
   151
gabomdq@8833
   152
    SDL_AddDisplayMode(&_this->displays[0], &mode);
gabomdq@8833
   153
    
gabomdq@8833
   154
    PSInterfaceInit();
gabomdq@8833
   155
    driverdata->instance = PSGetInstanceId();
gabomdq@8833
   156
    driverdata->ppb_graphics = PSInterfaceGraphics3D();
gabomdq@8833
   157
    driverdata->ppb_message_loop = PSInterfaceMessageLoop();
gabomdq@8833
   158
    driverdata->ppb_core = PSInterfaceCore();
gabomdq@8833
   159
    driverdata->ppb_fullscreen = PSInterfaceFullscreen();
gabomdq@8833
   160
    driverdata->ppb_instance = PSInterfaceInstance();
gabomdq@8833
   161
    driverdata->ppb_image_data = PSInterfaceImageData();
gabomdq@8833
   162
    driverdata->ppb_view = PSInterfaceView();
gabomdq@8833
   163
    driverdata->ppb_var = PSInterfaceVar();
gabomdq@8833
   164
    driverdata->ppb_input_event = (PPB_InputEvent*) PSGetInterface(PPB_INPUT_EVENT_INTERFACE);
gabomdq@8833
   165
    driverdata->ppb_keyboard_input_event = (PPB_KeyboardInputEvent*) PSGetInterface(PPB_KEYBOARD_INPUT_EVENT_INTERFACE);
gabomdq@8833
   166
    driverdata->ppb_mouse_input_event = (PPB_MouseInputEvent*) PSGetInterface(PPB_MOUSE_INPUT_EVENT_INTERFACE);
gabomdq@8833
   167
    driverdata->ppb_wheel_input_event = (PPB_WheelInputEvent*) PSGetInterface(PPB_WHEEL_INPUT_EVENT_INTERFACE);
gabomdq@8833
   168
    driverdata->ppb_touch_input_event = (PPB_TouchInputEvent*) PSGetInterface(PPB_TOUCH_INPUT_EVENT_INTERFACE);
gabomdq@8833
   169
    
gabomdq@8833
   170
    
gabomdq@8833
   171
    driverdata->message_loop = driverdata->ppb_message_loop->Create(driverdata->instance);
gabomdq@8833
   172
    
gabomdq@8833
   173
    PSEventSetFilter(PSE_ALL);
gabomdq@8833
   174
    
gabomdq@8833
   175
    /* We're done! */
gabomdq@8833
   176
    return 0;
gabomdq@8833
   177
}
gabomdq@8833
   178
gabomdq@8833
   179
void NACL_VideoQuit(_THIS) {
gabomdq@8833
   180
}
gabomdq@8833
   181
gabomdq@8833
   182
#endif /* SDL_VIDEO_DRIVER_NACL */
gabomdq@8833
   183
/* vi: set ts=4 sw=4 expandtab: */