1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2018 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
NACL_SetScreenResolution(int width,int height,Uint32 format)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 
NACL_Available(void)77 static int NACL_Available(void) {
78     return PSGetInstanceId() != 0;
79 }
80 
NACL_DeleteDevice(SDL_VideoDevice * device)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
NACL_SetDisplayMode(_THIS,SDL_VideoDisplay * display,SDL_DisplayMode * mode)89 NACL_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
90 {
91     return 0;
92 }
93 
NACL_CreateDevice(int devindex)94 static SDL_VideoDevice *NACL_CreateDevice(int devindex) {
95     SDL_VideoDevice *device;
96 
97     /* Initialize all variables that we clean on shutdown */
98     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
99     if (!device) {
100         SDL_OutOfMemory();
101         return NULL;
102     }
103     device->driverdata = &nacl;
104 
105     /* Set the function pointers */
106     device->VideoInit = NACL_VideoInit;
107     device->VideoQuit = NACL_VideoQuit;
108     device->PumpEvents = NACL_PumpEvents;
109 
110     device->CreateSDLWindow = NACL_CreateWindow;
111     device->SetWindowTitle = NACL_SetWindowTitle;
112     device->DestroyWindow = NACL_DestroyWindow;
113 
114     device->SetDisplayMode = NACL_SetDisplayMode;
115 
116     device->free = NACL_DeleteDevice;
117 
118     /* GL pointers */
119     device->GL_LoadLibrary = NACL_GLES_LoadLibrary;
120     device->GL_GetProcAddress = NACL_GLES_GetProcAddress;
121     device->GL_UnloadLibrary = NACL_GLES_UnloadLibrary;
122     device->GL_CreateContext = NACL_GLES_CreateContext;
123     device->GL_MakeCurrent = NACL_GLES_MakeCurrent;
124     device->GL_SetSwapInterval = NACL_GLES_SetSwapInterval;
125     device->GL_GetSwapInterval = NACL_GLES_GetSwapInterval;
126     device->GL_SwapWindow = NACL_GLES_SwapWindow;
127     device->GL_DeleteContext = NACL_GLES_DeleteContext;
128 
129 
130     return device;
131 }
132 
133 VideoBootStrap NACL_bootstrap = {
134     NACLVID_DRIVER_NAME, "SDL Native Client Video Driver",
135     NACL_Available, NACL_CreateDevice
136 };
137 
NACL_VideoInit(_THIS)138 int NACL_VideoInit(_THIS) {
139     SDL_VideoData *driverdata = (SDL_VideoData *) _this->driverdata;
140     SDL_DisplayMode mode;
141 
142     SDL_zero(mode);
143     mode.format = driverdata->format;
144     mode.w = driverdata->w;
145     mode.h = driverdata->h;
146     mode.refresh_rate = 0;
147     mode.driverdata = NULL;
148     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
149         return -1;
150     }
151 
152     SDL_AddDisplayMode(&_this->displays[0], &mode);
153 
154     PSInterfaceInit();
155     driverdata->instance = PSGetInstanceId();
156     driverdata->ppb_graphics = PSInterfaceGraphics3D();
157     driverdata->ppb_message_loop = PSInterfaceMessageLoop();
158     driverdata->ppb_core = PSInterfaceCore();
159     driverdata->ppb_fullscreen = PSInterfaceFullscreen();
160     driverdata->ppb_instance = PSInterfaceInstance();
161     driverdata->ppb_image_data = PSInterfaceImageData();
162     driverdata->ppb_view = PSInterfaceView();
163     driverdata->ppb_var = PSInterfaceVar();
164     driverdata->ppb_input_event = (PPB_InputEvent*) PSGetInterface(PPB_INPUT_EVENT_INTERFACE);
165     driverdata->ppb_keyboard_input_event = (PPB_KeyboardInputEvent*) PSGetInterface(PPB_KEYBOARD_INPUT_EVENT_INTERFACE);
166     driverdata->ppb_mouse_input_event = (PPB_MouseInputEvent*) PSGetInterface(PPB_MOUSE_INPUT_EVENT_INTERFACE);
167     driverdata->ppb_wheel_input_event = (PPB_WheelInputEvent*) PSGetInterface(PPB_WHEEL_INPUT_EVENT_INTERFACE);
168     driverdata->ppb_touch_input_event = (PPB_TouchInputEvent*) PSGetInterface(PPB_TOUCH_INPUT_EVENT_INTERFACE);
169 
170 
171     driverdata->message_loop = driverdata->ppb_message_loop->Create(driverdata->instance);
172 
173     PSEventSetFilter(PSE_ALL);
174 
175     /* We're done! */
176     return 0;
177 }
178 
NACL_VideoQuit(_THIS)179 void NACL_VideoQuit(_THIS) {
180 }
181 
182 #endif /* SDL_VIDEO_DRIVER_NACL */
183 /* vi: set ts=4 sw=4 expandtab: */
184