| 1 | /* |
| 2 | Simple DirectMedia Layer |
| 3 | Copyright (C) 1997-2025 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 | #ifdef SDL_VIDEO_DRIVER_OFFSCREEN |
| 24 | |
| 25 | /* Offscreen video driver is similar to dummy driver, however its purpose |
| 26 | * is enabling applications to use some of the SDL video functionality |
| 27 | * (notably context creation) while not requiring a display output. |
| 28 | * |
| 29 | * An example would be running a graphical program on a headless box |
| 30 | * for automated testing. |
| 31 | */ |
| 32 | |
| 33 | #include "SDL_offscreenvideo.h" |
| 34 | #include "SDL_offscreenevents_c.h" |
| 35 | #include "SDL_offscreenframebuffer_c.h" |
| 36 | #include "SDL_offscreenopengles.h" |
| 37 | #include "SDL_offscreenvulkan.h" |
| 38 | #include "SDL_offscreenwindow.h" |
| 39 | |
| 40 | #define OFFSCREENVID_DRIVER_NAME "offscreen" |
| 41 | |
| 42 | // Initialization/Query functions |
| 43 | static bool OFFSCREEN_VideoInit(SDL_VideoDevice *_this); |
| 44 | static bool OFFSCREEN_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode); |
| 45 | static void OFFSCREEN_VideoQuit(SDL_VideoDevice *_this); |
| 46 | |
| 47 | // OFFSCREEN driver bootstrap functions |
| 48 | |
| 49 | static void OFFSCREEN_DeleteDevice(SDL_VideoDevice *device) |
| 50 | { |
| 51 | SDL_free(device); |
| 52 | } |
| 53 | |
| 54 | static bool OFFSCREEN_Available(const char *enable_hint) |
| 55 | { |
| 56 | const char *hint = SDL_GetHint(SDL_HINT_VIDEO_DRIVER); |
| 57 | if (hint) { |
| 58 | if (SDL_strcmp(hint, enable_hint) == 0) { |
| 59 | return true; |
| 60 | } |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | static SDL_VideoDevice *OFFSCREEN_CreateDevice(void) |
| 66 | { |
| 67 | SDL_VideoDevice *device; |
| 68 | |
| 69 | if (!OFFSCREEN_Available(OFFSCREENVID_DRIVER_NAME)) { |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | // Initialize all variables that we clean on shutdown |
| 74 | device = (SDL_VideoDevice *)SDL_calloc(1, sizeof(SDL_VideoDevice)); |
| 75 | if (!device) { |
| 76 | return NULL; |
| 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 | #ifdef SDL_VIDEO_OPENGL_EGL |
| 90 | // GL context |
| 91 | device->GL_SwapWindow = OFFSCREEN_GLES_SwapWindow; |
| 92 | device->GL_MakeCurrent = OFFSCREEN_GLES_MakeCurrent; |
| 93 | device->GL_CreateContext = OFFSCREEN_GLES_CreateContext; |
| 94 | device->GL_DestroyContext = OFFSCREEN_GLES_DestroyContext; |
| 95 | device->GL_LoadLibrary = OFFSCREEN_GLES_LoadLibrary; |
| 96 | device->GL_UnloadLibrary = OFFSCREEN_GLES_UnloadLibrary; |
| 97 | device->GL_GetProcAddress = OFFSCREEN_GLES_GetProcAddress; |
| 98 | device->GL_GetSwapInterval = OFFSCREEN_GLES_GetSwapInterval; |
| 99 | device->GL_SetSwapInterval = OFFSCREEN_GLES_SetSwapInterval; |
| 100 | #endif |
| 101 | |
| 102 | #ifdef SDL_VIDEO_VULKAN |
| 103 | device->Vulkan_LoadLibrary = OFFSCREEN_Vulkan_LoadLibrary; |
| 104 | device->Vulkan_UnloadLibrary = OFFSCREEN_Vulkan_UnloadLibrary; |
| 105 | device->Vulkan_GetInstanceExtensions = OFFSCREEN_Vulkan_GetInstanceExtensions; |
| 106 | device->Vulkan_CreateSurface = OFFSCREEN_Vulkan_CreateSurface; |
| 107 | device->Vulkan_DestroySurface = OFFSCREEN_Vulkan_DestroySurface; |
| 108 | #endif |
| 109 | |
| 110 | // "Window" |
| 111 | device->CreateSDLWindow = OFFSCREEN_CreateWindow; |
| 112 | device->DestroyWindow = OFFSCREEN_DestroyWindow; |
| 113 | device->SetWindowSize = OFFSCREEN_SetWindowSize; |
| 114 | |
| 115 | return device; |
| 116 | } |
| 117 | |
| 118 | VideoBootStrap OFFSCREEN_bootstrap = { |
| 119 | OFFSCREENVID_DRIVER_NAME, "SDL offscreen video driver" , |
| 120 | OFFSCREEN_CreateDevice, |
| 121 | NULL, // no ShowMessageBox implementation |
| 122 | false |
| 123 | }; |
| 124 | |
| 125 | static bool OFFSCREEN_VideoInit(SDL_VideoDevice *_this) |
| 126 | { |
| 127 | SDL_DisplayMode mode; |
| 128 | |
| 129 | // Use a fake 32-bpp desktop mode |
| 130 | SDL_zero(mode); |
| 131 | mode.format = SDL_PIXELFORMAT_XRGB8888; |
| 132 | mode.w = 1024; |
| 133 | mode.h = 768; |
| 134 | if (SDL_AddBasicVideoDisplay(&mode) == 0) { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | // We're done! |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | static bool OFFSCREEN_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode) |
| 143 | { |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | void OFFSCREEN_VideoQuit(SDL_VideoDevice *_this) |
| 148 | { |
| 149 | } |
| 150 | |
| 151 | #endif // SDL_VIDEO_DRIVER_OFFSCREEN |
| 152 | |