sdl

FORK: Simple Directmedia Layer
git clone https://git.neptards.moe/neptards/sdl.git
Log | Files | Refs

SDL_naclvideo.c (6080B)


      1 /*
      2   Simple DirectMedia Layer
      3   Copyright (C) 1997-2020 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_NACL
     24 
     25 #include "ppapi/c/pp_errors.h"
     26 #include "ppapi/c/pp_instance.h"
     27 #include "ppapi_simple/ps.h"
     28 #include "ppapi_simple/ps_interface.h"
     29 #include "ppapi_simple/ps_event.h"
     30 #include "nacl_io/nacl_io.h"
     31 
     32 #include "SDL_naclvideo.h"
     33 #include "SDL_naclwindow.h"
     34 #include "SDL_naclevents_c.h"
     35 #include "SDL_naclopengles.h"   
     36 #include "SDL_video.h"
     37 #include "../SDL_sysvideo.h"
     38 #include "../../events/SDL_events_c.h"
     39 
     40 #define NACLVID_DRIVER_NAME "nacl"
     41 
     42 /* Static init required because NACL_SetScreenResolution 
     43  * may appear even before SDL starts and we want to remember
     44  * the window width and height
     45  */
     46 static SDL_VideoData nacl = {0};
     47 
     48 void
     49 NACL_SetScreenResolution(int width, int height, Uint32 format)
     50 {
     51     PP_Resource context;
     52     
     53     nacl.w = width;
     54     nacl.h = height;   
     55     nacl.format = format;
     56     
     57     if (nacl.window) {
     58         nacl.window->w = width;
     59         nacl.window->h = height;
     60         SDL_SendWindowEvent(nacl.window, SDL_WINDOWEVENT_RESIZED, width, height);
     61     }
     62     
     63     /* FIXME: Check threading issues...otherwise use a hardcoded _this->context across all threads */
     64     context = (PP_Resource) SDL_GL_GetCurrentContext();
     65     if (context) {
     66         PSInterfaceGraphics3D()->ResizeBuffers(context, width, height);
     67     }
     68 
     69 }
     70 
     71 
     72 
     73 /* Initialization/Query functions */
     74 static int NACL_VideoInit(_THIS);
     75 static void NACL_VideoQuit(_THIS);
     76 
     77 static int NACL_Available(void) {
     78     return PSGetInstanceId() != 0;
     79 }
     80 
     81 static void NACL_DeleteDevice(SDL_VideoDevice *device) {
     82     SDL_VideoData *driverdata = (SDL_VideoData*) device->driverdata;
     83     driverdata->ppb_core->ReleaseResource((PP_Resource) driverdata->ppb_message_loop);
     84     /* device->driverdata is not freed because it points to static memory */
     85     SDL_free(device);
     86 }
     87 
     88 static int
     89 NACL_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
     90 {
     91     return 0;
     92 }
     93 
     94 static SDL_VideoDevice *NACL_CreateDevice(int devindex) {
     95     SDL_VideoDevice *device;
     96     
     97     if (!NACL_Available()) {
     98         return NULL;
     99     }
    100     
    101     /* Initialize all variables that we clean on shutdown */
    102     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    103     if (!device) {
    104         SDL_OutOfMemory();
    105         return NULL;
    106     }
    107     device->driverdata = &nacl;
    108   
    109     /* Set the function pointers */
    110     device->VideoInit = NACL_VideoInit;
    111     device->VideoQuit = NACL_VideoQuit;
    112     device->PumpEvents = NACL_PumpEvents;
    113     
    114     device->CreateSDLWindow = NACL_CreateWindow;
    115     device->SetWindowTitle = NACL_SetWindowTitle;
    116     device->DestroyWindow = NACL_DestroyWindow;
    117     
    118     device->SetDisplayMode = NACL_SetDisplayMode;
    119     
    120     device->free = NACL_DeleteDevice;
    121     
    122     /* GL pointers */
    123     device->GL_LoadLibrary = NACL_GLES_LoadLibrary;
    124     device->GL_GetProcAddress = NACL_GLES_GetProcAddress;
    125     device->GL_UnloadLibrary = NACL_GLES_UnloadLibrary;
    126     device->GL_CreateContext = NACL_GLES_CreateContext;
    127     device->GL_MakeCurrent = NACL_GLES_MakeCurrent;
    128     device->GL_SetSwapInterval = NACL_GLES_SetSwapInterval;
    129     device->GL_GetSwapInterval = NACL_GLES_GetSwapInterval;
    130     device->GL_SwapWindow = NACL_GLES_SwapWindow;
    131     device->GL_DeleteContext = NACL_GLES_DeleteContext;
    132     
    133     
    134     return device;
    135 }
    136 
    137 VideoBootStrap NACL_bootstrap = {
    138     NACLVID_DRIVER_NAME, "SDL Native Client Video Driver",
    139     NACL_CreateDevice
    140 };
    141 
    142 int NACL_VideoInit(_THIS) {
    143     SDL_VideoData *driverdata = (SDL_VideoData *) _this->driverdata;
    144     SDL_DisplayMode mode;
    145 
    146     SDL_zero(mode);
    147     mode.format = driverdata->format;
    148     mode.w = driverdata->w;
    149     mode.h = driverdata->h;
    150     mode.refresh_rate = 0;
    151     mode.driverdata = NULL;
    152     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
    153         return -1;
    154     }
    155 
    156     SDL_AddDisplayMode(&_this->displays[0], &mode);
    157     
    158     PSInterfaceInit();
    159     driverdata->instance = PSGetInstanceId();
    160     driverdata->ppb_graphics = PSInterfaceGraphics3D();
    161     driverdata->ppb_message_loop = PSInterfaceMessageLoop();
    162     driverdata->ppb_core = PSInterfaceCore();
    163     driverdata->ppb_fullscreen = PSInterfaceFullscreen();
    164     driverdata->ppb_instance = PSInterfaceInstance();
    165     driverdata->ppb_image_data = PSInterfaceImageData();
    166     driverdata->ppb_view = PSInterfaceView();
    167     driverdata->ppb_var = PSInterfaceVar();
    168     driverdata->ppb_input_event = (PPB_InputEvent*) PSGetInterface(PPB_INPUT_EVENT_INTERFACE);
    169     driverdata->ppb_keyboard_input_event = (PPB_KeyboardInputEvent*) PSGetInterface(PPB_KEYBOARD_INPUT_EVENT_INTERFACE);
    170     driverdata->ppb_mouse_input_event = (PPB_MouseInputEvent*) PSGetInterface(PPB_MOUSE_INPUT_EVENT_INTERFACE);
    171     driverdata->ppb_wheel_input_event = (PPB_WheelInputEvent*) PSGetInterface(PPB_WHEEL_INPUT_EVENT_INTERFACE);
    172     driverdata->ppb_touch_input_event = (PPB_TouchInputEvent*) PSGetInterface(PPB_TOUCH_INPUT_EVENT_INTERFACE);
    173     
    174     
    175     driverdata->message_loop = driverdata->ppb_message_loop->Create(driverdata->instance);
    176     
    177     PSEventSetFilter(PSE_ALL);
    178     
    179     /* We're done! */
    180     return 0;
    181 }
    182 
    183 void NACL_VideoQuit(_THIS) {
    184 }
    185 
    186 #endif /* SDL_VIDEO_DRIVER_NACL */
    187 /* vi: set ts=4 sw=4 expandtab: */