SDL_offscreenvideo.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_VIDEO_DRIVER_OFFSCREEN
  20. /* Offscreen video driver is similar to dummy driver, however its purpose
  21. * is enabling applications to use some of the SDL video functionality
  22. * (notably context creation) while not requiring a display output.
  23. *
  24. * An example would be running a graphical program on a headless box
  25. * for automated testing.
  26. */
  27. #include "SDL_offscreenvideo.h"
  28. #include "SDL_offscreenevents_c.h"
  29. #include "SDL_offscreenframebuffer_c.h"
  30. #include "SDL_offscreenopengles.h"
  31. #include "SDL_offscreenvulkan.h"
  32. #include "SDL_offscreenwindow.h"
  33. #define OFFSCREENVID_DRIVER_NAME "offscreen"
  34. // Initialization/Query functions
  35. static bool OFFSCREEN_VideoInit(SDL_VideoDevice *_this);
  36. static bool OFFSCREEN_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode);
  37. static void OFFSCREEN_VideoQuit(SDL_VideoDevice *_this);
  38. // OFFSCREEN driver bootstrap functions
  39. static void OFFSCREEN_DeleteDevice(SDL_VideoDevice *device)
  40. {
  41. SDL_free(device);
  42. }
  43. static bool OFFSCREEN_Available(const char *drivername)
  44. {
  45. const char *hint = SDL_GetHint(SDL_HINT_VIDEO_DRIVER);
  46. if (hint && SDL_strstr(hint, drivername) != NULL) {
  47. return true;
  48. }
  49. return false;
  50. }
  51. static SDL_VideoDevice *OFFSCREEN_CreateDevice(void)
  52. {
  53. SDL_VideoDevice *device;
  54. if (!OFFSCREEN_Available(OFFSCREENVID_DRIVER_NAME)) {
  55. return NULL;
  56. }
  57. // Initialize all variables that we clean on shutdown
  58. device = (SDL_VideoDevice *)SDL_calloc(1, sizeof(SDL_VideoDevice));
  59. if (!device) {
  60. return NULL;
  61. }
  62. // General video
  63. device->VideoInit = OFFSCREEN_VideoInit;
  64. device->VideoQuit = OFFSCREEN_VideoQuit;
  65. device->SetDisplayMode = OFFSCREEN_SetDisplayMode;
  66. device->PumpEvents = OFFSCREEN_PumpEvents;
  67. device->CreateWindowFramebuffer = SDL_OFFSCREEN_CreateWindowFramebuffer;
  68. device->UpdateWindowFramebuffer = SDL_OFFSCREEN_UpdateWindowFramebuffer;
  69. device->DestroyWindowFramebuffer = SDL_OFFSCREEN_DestroyWindowFramebuffer;
  70. device->free = OFFSCREEN_DeleteDevice;
  71. #ifdef SDL_VIDEO_OPENGL_EGL
  72. // GL context
  73. device->GL_SwapWindow = OFFSCREEN_GLES_SwapWindow;
  74. device->GL_MakeCurrent = OFFSCREEN_GLES_MakeCurrent;
  75. device->GL_CreateContext = OFFSCREEN_GLES_CreateContext;
  76. device->GL_DestroyContext = OFFSCREEN_GLES_DestroyContext;
  77. device->GL_LoadLibrary = OFFSCREEN_GLES_LoadLibrary;
  78. device->GL_UnloadLibrary = OFFSCREEN_GLES_UnloadLibrary;
  79. device->GL_GetProcAddress = OFFSCREEN_GLES_GetProcAddress;
  80. device->GL_GetSwapInterval = OFFSCREEN_GLES_GetSwapInterval;
  81. device->GL_SetSwapInterval = OFFSCREEN_GLES_SetSwapInterval;
  82. #endif
  83. #ifdef SDL_VIDEO_VULKAN
  84. device->Vulkan_LoadLibrary = OFFSCREEN_Vulkan_LoadLibrary;
  85. device->Vulkan_UnloadLibrary = OFFSCREEN_Vulkan_UnloadLibrary;
  86. device->Vulkan_GetInstanceExtensions = OFFSCREEN_Vulkan_GetInstanceExtensions;
  87. device->Vulkan_CreateSurface = OFFSCREEN_Vulkan_CreateSurface;
  88. device->Vulkan_DestroySurface = OFFSCREEN_Vulkan_DestroySurface;
  89. #endif
  90. // "Window"
  91. device->CreateSDLWindow = OFFSCREEN_CreateWindow;
  92. device->DestroyWindow = OFFSCREEN_DestroyWindow;
  93. device->SetWindowSize = OFFSCREEN_SetWindowSize;
  94. return device;
  95. }
  96. VideoBootStrap OFFSCREEN_bootstrap = {
  97. OFFSCREENVID_DRIVER_NAME, "SDL offscreen video driver",
  98. OFFSCREEN_CreateDevice,
  99. NULL, // no ShowMessageBox implementation
  100. false
  101. };
  102. static bool OFFSCREEN_VideoInit(SDL_VideoDevice *_this)
  103. {
  104. SDL_DisplayMode mode;
  105. // Use a fake 32-bpp desktop mode
  106. SDL_zero(mode);
  107. mode.format = SDL_PIXELFORMAT_XRGB8888;
  108. mode.w = 1024;
  109. mode.h = 768;
  110. if (SDL_AddBasicVideoDisplay(&mode) == 0) {
  111. return false;
  112. }
  113. // We're done!
  114. return true;
  115. }
  116. static bool OFFSCREEN_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
  117. {
  118. return true;
  119. }
  120. void OFFSCREEN_VideoQuit(SDL_VideoDevice *_this)
  121. {
  122. }
  123. #endif // SDL_VIDEO_DRIVER_OFFSCREEN