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 
22 #include "../../SDL_internal.h"
23 
24 #if SDL_VIDEO_DRIVER_OFFSCREEN
25 
26 /* Offscreen video driver is similar to dummy driver, however its purpose
27  * is enabling applications to use some of the SDL video functionality
28  * (notably context creation) while not requiring a display output.
29  *
30  * An example would be running a graphical program on a headless box
31  * for automated testing.
32  */
33 
34 #include "SDL_video.h"
35 #include "SDL_mouse.h"
36 #include "../SDL_sysvideo.h"
37 #include "../SDL_pixels_c.h"
38 #include "../../events/SDL_events_c.h"
39 
40 #include "SDL_offscreenvideo.h"
41 #include "SDL_offscreenevents_c.h"
42 #include "SDL_offscreenframebuffer_c.h"
43 #include "SDL_offscreenopengl.h"
44 
45 #define OFFSCREENVID_DRIVER_NAME "offscreen"
46 
47 /* Initialization/Query functions */
48 static int OFFSCREEN_VideoInit(_THIS);
49 static int OFFSCREEN_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
50 static void OFFSCREEN_VideoQuit(_THIS);
51 
52 /* OFFSCREEN driver bootstrap functions */
53 
54 static int
OFFSCREEN_Available(void)55 OFFSCREEN_Available(void)
56 {
57     /* Consider it always available */
58     return (1);
59 }
60 
61 static void
OFFSCREEN_DeleteDevice(SDL_VideoDevice * device)62 OFFSCREEN_DeleteDevice(SDL_VideoDevice * device)
63 {
64     SDL_free(device);
65 }
66 
67 static SDL_VideoDevice *
OFFSCREEN_CreateDevice(int devindex)68 OFFSCREEN_CreateDevice(int devindex)
69 {
70     SDL_VideoDevice *device;
71 
72     /* Initialize all variables that we clean on shutdown */
73     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
74     if (!device) {
75         SDL_OutOfMemory();
76         return (0);
77     }
78 
79     /* General video */
80     device->VideoInit = OFFSCREEN_VideoInit;
81     device->VideoQuit = OFFSCREEN_VideoQuit;
82     device->SetDisplayMode = OFFSCREEN_SetDisplayMode;
83     device->PumpEvents = OFFSCREEN_PumpEvents;
84     device->CreateWindowFramebuffer = SDL_OFFSCREEN_CreateWindowFramebuffer;
85     device->UpdateWindowFramebuffer = SDL_OFFSCREEN_UpdateWindowFramebuffer;
86     device->DestroyWindowFramebuffer = SDL_OFFSCREEN_DestroyWindowFramebuffer;
87     device->free = OFFSCREEN_DeleteDevice;
88 
89     /* GL context */
90     device->GL_SwapWindow = OFFSCREEN_GL_SwapWindow;
91     device->GL_MakeCurrent = OFFSCREEN_GL_MakeCurrent;
92     device->GL_CreateContext = OFFSCREEN_GL_CreateContext;
93     device->GL_DeleteContext = OFFSCREEN_GL_DeleteContext;
94     device->GL_LoadLibrary = OFFSCREEN_GL_LoadLibrary;
95     device->GL_UnloadLibrary = OFFSCREEN_GL_UnloadLibrary;
96     device->GL_GetProcAddress = OFFSCREEN_GL_GetProcAddress;
97     device->GL_GetSwapInterval = OFFSCREEN_GL_GetSwapInterval;
98     device->GL_SetSwapInterval = OFFSCREEN_GL_SetSwapInterval;
99 
100     /* "Window" */
101     device->CreateSDLWindow = OFFSCREEN_CreateWindow;
102     device->DestroyWindow = OFFSCREEN_DestroyWindow;
103 
104     return device;
105 }
106 
107 VideoBootStrap OFFSCREEN_bootstrap = {
108     OFFSCREENVID_DRIVER_NAME, "SDL offscreen video driver",
109     OFFSCREEN_Available, OFFSCREEN_CreateDevice
110 };
111 
112 int
OFFSCREEN_VideoInit(_THIS)113 OFFSCREEN_VideoInit(_THIS)
114 {
115     SDL_DisplayMode mode;
116     SDL_Mouse *mouse = NULL;
117 
118     /* Use a fake 32-bpp desktop mode */
119     mode.format = SDL_PIXELFORMAT_RGB888;
120     mode.w = 1024;
121     mode.h = 768;
122     mode.refresh_rate = 0;
123     mode.driverdata = NULL;
124     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
125         return -1;
126     }
127 
128     SDL_zero(mode);
129     SDL_AddDisplayMode(&_this->displays[0], &mode);
130 
131     /* We're done! */
132     return 0;
133 }
134 
135 static int
OFFSCREEN_SetDisplayMode(_THIS,SDL_VideoDisplay * display,SDL_DisplayMode * mode)136 OFFSCREEN_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
137 {
138     return 0;
139 }
140 
141 void
OFFSCREEN_VideoQuit(_THIS)142 OFFSCREEN_VideoQuit(_THIS)
143 {
144 }
145 
146 #endif /* SDL_VIDEO_DRIVER_OFFSCREEN */
147 
148 /* vi: set ts=4 sw=4 expandtab: */
149