SDL  2.0
SDL_offscreenvideo.c
Go to the documentation of this file.
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"
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
55 OFFSCREEN_Available(void)
56 {
57  /* Consider it always available */
58  return (1);
59 }
60 
61 static void
62 OFFSCREEN_DeleteDevice(SDL_VideoDevice * device)
63 {
65 }
66 
67 static SDL_VideoDevice *
68 OFFSCREEN_CreateDevice(int devindex)
69 {
71 
72  /* Initialize all variables that we clean on shutdown */
74  if (!device) {
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 
108  OFFSCREENVID_DRIVER_NAME, "SDL offscreen video driver",
109  OFFSCREEN_Available, OFFSCREEN_CreateDevice
110 };
111 
112 static Uint32
113 OFFSCREEN_GetGlobalMouseState(int *x, int *y)
114 {
115  if (x) {
116  *x = 0;
117  }
118 
119  if (y) {
120  *y = 0;
121  }
122  return 0;
123 }
124 
125 int
126 OFFSCREEN_VideoInit(_THIS)
127 {
129  SDL_Mouse *mouse = NULL;
130 
131  /* Use a fake 32-bpp desktop mode */
132  mode.format = SDL_PIXELFORMAT_RGB888;
133  mode.w = 1024;
134  mode.h = 768;
135  mode.refresh_rate = 0;
136  mode.driverdata = NULL;
137  if (SDL_AddBasicVideoDisplay(&mode) < 0) {
138  return -1;
139  }
140 
141  SDL_zero(mode);
143 
144  /* Init mouse */
145  mouse = SDL_GetMouse();
146  /* This function needs to be implemented by every driver */
147  mouse->GetGlobalMouseState = OFFSCREEN_GetGlobalMouseState;
148 
149  /* We're done! */
150  return 0;
151 }
152 
153 static int
154 OFFSCREEN_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
155 {
156  return 0;
157 }
158 
159 void
160 OFFSCREEN_VideoQuit(_THIS)
161 {
162 }
163 
164 #endif /* SDL_VIDEO_DRIVER_OFFSCREEN */
165 
166 /* vi: set ts=4 sw=4 expandtab: */
SDL_GetMouse
SDL_Mouse * SDL_GetMouse(void)
Definition: SDL_mouse.c:170
SDL_zero
#define SDL_zero(x)
Definition: SDL_stdinc.h:418
OFFSCREEN_CreateWindow
int OFFSCREEN_CreateWindow(_THIS, SDL_Window *window)
SDL_mouse.h
SDL_PIXELFORMAT_RGB888
@ SDL_PIXELFORMAT_RGB888
Definition: SDL_pixels.h:239
OFFSCREEN_GL_LoadLibrary
int OFFSCREEN_GL_LoadLibrary(_THIS, const char *path)
SDL_OFFSCREEN_UpdateWindowFramebuffer
int SDL_OFFSCREEN_UpdateWindowFramebuffer(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects)
NULL
#define NULL
Definition: begin_code.h:167
mode
GLenum mode
Definition: SDL_opengl_glext.h:1125
OFFSCREEN_GL_UnloadLibrary
void OFFSCREEN_GL_UnloadLibrary(_THIS)
OFFSCREEN_GL_DeleteContext
#define OFFSCREEN_GL_DeleteContext
Definition: SDL_offscreenopengl.h:29
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
SDL_offscreenvideo.h
SDL_DisplayMode
The structure that defines a display mode.
Definition: SDL_video.h:54
SDL_AddDisplayMode
SDL_bool SDL_AddDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode *mode)
Definition: SDL_video.c:772
_this
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_VideoDevice::displays
SDL_VideoDisplay * displays
Definition: SDL_sysvideo.h:324
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
OFFSCREEN_bootstrap
VideoBootStrap OFFSCREEN_bootstrap
SDL_Mouse
Definition: SDL_mouse_c.h:44
OFFSCREEN_GL_GetSwapInterval
#define OFFSCREEN_GL_GetSwapInterval
Definition: SDL_offscreenopengl.h:30
OFFSCREEN_DestroyWindow
void OFFSCREEN_DestroyWindow(_THIS, SDL_Window *window)
SDL_offscreenframebuffer_c.h
SDL_VideoDevice
Definition: SDL_sysvideo.h:150
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
SDL_calloc
#define SDL_calloc
Definition: SDL_dynapi_overrides.h:375
SDL_AddBasicVideoDisplay
int SDL_AddBasicVideoDisplay(const SDL_DisplayMode *desktop_mode)
Definition: SDL_video.c:592
OFFSCREEN_GL_CreateContext
SDL_GLContext OFFSCREEN_GL_CreateContext(_THIS, SDL_Window *window)
SDL_VideoDisplay
Definition: SDL_sysvideo.h:127
OFFSCREEN_GL_SetSwapInterval
#define OFFSCREEN_GL_SetSwapInterval
Definition: SDL_offscreenopengl.h:31
SDL_offscreenevents_c.h
SDL_offscreenopengl.h
SDL_Mouse::GetGlobalMouseState
Uint32(* GetGlobalMouseState)(int *x, int *y)
Definition: SDL_mouse_c.h:73
OFFSCREEN_PumpEvents
void OFFSCREEN_PumpEvents(_THIS)
SDL_OFFSCREEN_DestroyWindowFramebuffer
void SDL_OFFSCREEN_DestroyWindowFramebuffer(_THIS, SDL_Window *window)
SDL_video.h
OFFSCREEN_GL_MakeCurrent
int OFFSCREEN_GL_MakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context)
SDL_OFFSCREEN_CreateWindowFramebuffer
int SDL_OFFSCREEN_CreateWindowFramebuffer(_THIS, SDL_Window *window, Uint32 *format, void **pixels, int *pitch)
VideoBootStrap
Definition: SDL_sysvideo.h:406
OFFSCREEN_GL_SwapWindow
int OFFSCREEN_GL_SwapWindow(_THIS, SDL_Window *window)
device
static SDL_AudioDeviceID device
Definition: loopwave.c:37
OFFSCREEN_GL_GetProcAddress
void * OFFSCREEN_GL_GetProcAddress(_THIS, const char *proc)