SDL_offscreenvideo.c (4295B)
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 void 55 OFFSCREEN_DeleteDevice(SDL_VideoDevice * device) 56 { 57 SDL_free(device); 58 } 59 60 static SDL_VideoDevice * 61 OFFSCREEN_CreateDevice(int devindex) 62 { 63 SDL_VideoDevice *device; 64 65 /* Initialize all variables that we clean on shutdown */ 66 device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice)); 67 if (!device) { 68 SDL_OutOfMemory(); 69 return (0); 70 } 71 72 /* General video */ 73 device->VideoInit = OFFSCREEN_VideoInit; 74 device->VideoQuit = OFFSCREEN_VideoQuit; 75 device->SetDisplayMode = OFFSCREEN_SetDisplayMode; 76 device->PumpEvents = OFFSCREEN_PumpEvents; 77 device->CreateWindowFramebuffer = SDL_OFFSCREEN_CreateWindowFramebuffer; 78 device->UpdateWindowFramebuffer = SDL_OFFSCREEN_UpdateWindowFramebuffer; 79 device->DestroyWindowFramebuffer = SDL_OFFSCREEN_DestroyWindowFramebuffer; 80 device->free = OFFSCREEN_DeleteDevice; 81 82 /* GL context */ 83 device->GL_SwapWindow = OFFSCREEN_GL_SwapWindow; 84 device->GL_MakeCurrent = OFFSCREEN_GL_MakeCurrent; 85 device->GL_CreateContext = OFFSCREEN_GL_CreateContext; 86 device->GL_DeleteContext = OFFSCREEN_GL_DeleteContext; 87 device->GL_LoadLibrary = OFFSCREEN_GL_LoadLibrary; 88 device->GL_UnloadLibrary = OFFSCREEN_GL_UnloadLibrary; 89 device->GL_GetProcAddress = OFFSCREEN_GL_GetProcAddress; 90 device->GL_GetSwapInterval = OFFSCREEN_GL_GetSwapInterval; 91 device->GL_SetSwapInterval = OFFSCREEN_GL_SetSwapInterval; 92 93 /* "Window" */ 94 device->CreateSDLWindow = OFFSCREEN_CreateWindow; 95 device->DestroyWindow = OFFSCREEN_DestroyWindow; 96 97 return device; 98 } 99 100 VideoBootStrap OFFSCREEN_bootstrap = { 101 OFFSCREENVID_DRIVER_NAME, "SDL offscreen video driver", 102 OFFSCREEN_CreateDevice 103 }; 104 105 int 106 OFFSCREEN_VideoInit(_THIS) 107 { 108 SDL_DisplayMode mode; 109 SDL_Mouse *mouse = NULL; 110 111 /* Use a fake 32-bpp desktop mode */ 112 mode.format = SDL_PIXELFORMAT_RGB888; 113 mode.w = 1024; 114 mode.h = 768; 115 mode.refresh_rate = 0; 116 mode.driverdata = NULL; 117 if (SDL_AddBasicVideoDisplay(&mode) < 0) { 118 return -1; 119 } 120 121 SDL_zero(mode); 122 SDL_AddDisplayMode(&_this->displays[0], &mode); 123 124 /* We're done! */ 125 return 0; 126 } 127 128 static int 129 OFFSCREEN_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode) 130 { 131 return 0; 132 } 133 134 void 135 OFFSCREEN_VideoQuit(_THIS) 136 { 137 } 138 139 #endif /* SDL_VIDEO_DRIVER_OFFSCREEN */ 140 141 /* vi: set ts=4 sw=4 expandtab: */