SDL_x11framebuffer.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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_X11
  20. #include "SDL_x11video.h"
  21. #include "SDL_x11framebuffer.h"
  22. #include "SDL_x11xsync.h"
  23. #ifndef NO_SHARED_MEMORY
  24. // Shared memory error handler routine
  25. static int shm_error;
  26. static int (*X_handler)(Display *, XErrorEvent *) = NULL;
  27. static int shm_errhandler(Display *d, XErrorEvent *e)
  28. {
  29. if (e->error_code == BadAccess || e->error_code == BadRequest) {
  30. shm_error = True;
  31. return 0;
  32. }
  33. return X_handler(d, e);
  34. }
  35. static bool have_mitshm(Display *dpy)
  36. {
  37. // Only use shared memory on local X servers
  38. return X11_XShmQueryExtension(dpy) ? SDL_X11_HAVE_SHM : false;
  39. }
  40. #endif // !NO_SHARED_MEMORY
  41. bool X11_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format,
  42. void **pixels, int *pitch)
  43. {
  44. SDL_WindowData *data = window->internal;
  45. Display *display = data->videodata->display;
  46. XGCValues gcv;
  47. XVisualInfo vinfo;
  48. int w, h;
  49. SDL_GetWindowSizeInPixels(window, &w, &h);
  50. // Free the old framebuffer surface
  51. X11_DestroyWindowFramebuffer(_this, window);
  52. // Create the graphics context for drawing
  53. gcv.graphics_exposures = False;
  54. data->gc = X11_XCreateGC(display, data->xwindow, GCGraphicsExposures, &gcv);
  55. if (!data->gc) {
  56. return SDL_SetError("Couldn't create graphics context");
  57. }
  58. // Find out the pixel format and depth
  59. if (!X11_GetVisualInfoFromVisual(display, data->visual, &vinfo)) {
  60. return SDL_SetError("Couldn't get window visual information");
  61. }
  62. *format = X11_GetPixelFormatFromVisualInfo(display, &vinfo);
  63. if (*format == SDL_PIXELFORMAT_UNKNOWN) {
  64. return SDL_SetError("Unknown window pixel format");
  65. }
  66. // Calculate pitch
  67. *pitch = (((w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
  68. // Create the actual image
  69. #ifndef NO_SHARED_MEMORY
  70. if (have_mitshm(display)) {
  71. XShmSegmentInfo *shminfo = &data->shminfo;
  72. shminfo->shmid = shmget(IPC_PRIVATE, (size_t)h * (*pitch), IPC_CREAT | 0777);
  73. if (shminfo->shmid >= 0) {
  74. shminfo->shmaddr = (char *)shmat(shminfo->shmid, 0, 0);
  75. shminfo->readOnly = False;
  76. if (shminfo->shmaddr != (char *)-1) {
  77. shm_error = False;
  78. X_handler = X11_XSetErrorHandler(shm_errhandler);
  79. X11_XShmAttach(display, shminfo);
  80. X11_XSync(display, False);
  81. X11_XSetErrorHandler(X_handler);
  82. if (shm_error) {
  83. shmdt(shminfo->shmaddr);
  84. }
  85. } else {
  86. shm_error = True;
  87. }
  88. shmctl(shminfo->shmid, IPC_RMID, NULL);
  89. } else {
  90. shm_error = True;
  91. }
  92. if (!shm_error) {
  93. data->ximage = X11_XShmCreateImage(display, data->visual,
  94. vinfo.depth, ZPixmap,
  95. shminfo->shmaddr, shminfo,
  96. w, h);
  97. if (!data->ximage) {
  98. X11_XShmDetach(display, shminfo);
  99. X11_XSync(display, False);
  100. shmdt(shminfo->shmaddr);
  101. } else {
  102. // Done!
  103. data->ximage->byte_order = (SDL_BYTEORDER == SDL_BIG_ENDIAN) ? MSBFirst : LSBFirst;
  104. data->use_mitshm = true;
  105. *pixels = shminfo->shmaddr;
  106. return true;
  107. }
  108. }
  109. }
  110. #endif // not NO_SHARED_MEMORY
  111. *pixels = SDL_malloc((size_t)h * (*pitch));
  112. if (!*pixels) {
  113. return false;
  114. }
  115. data->ximage = X11_XCreateImage(display, data->visual,
  116. vinfo.depth, ZPixmap, 0, (char *)(*pixels),
  117. w, h, 32, 0);
  118. if (!data->ximage) {
  119. SDL_free(*pixels);
  120. return SDL_SetError("Couldn't create XImage");
  121. }
  122. data->ximage->byte_order = (SDL_BYTEORDER == SDL_BIG_ENDIAN) ? MSBFirst : LSBFirst;
  123. return true;
  124. }
  125. bool X11_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects,
  126. int numrects)
  127. {
  128. SDL_WindowData *data = window->internal;
  129. Display *display = data->videodata->display;
  130. int i;
  131. int x, y, w, h;
  132. int window_w, window_h;
  133. SDL_GetWindowSizeInPixels(window, &window_w, &window_h);
  134. #ifndef NO_SHARED_MEMORY
  135. if (data->use_mitshm) {
  136. for (i = 0; i < numrects; ++i) {
  137. x = rects[i].x;
  138. y = rects[i].y;
  139. w = rects[i].w;
  140. h = rects[i].h;
  141. if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0) {
  142. // Clipped?
  143. continue;
  144. }
  145. if (x < 0) {
  146. x += w;
  147. w += rects[i].x;
  148. }
  149. if (y < 0) {
  150. y += h;
  151. h += rects[i].y;
  152. }
  153. if (x + w > window_w) {
  154. w = window_w - x;
  155. }
  156. if (y + h > window_h) {
  157. h = window_h - y;
  158. }
  159. X11_XShmPutImage(display, data->xwindow, data->gc, data->ximage,
  160. x, y, x, y, w, h, False);
  161. }
  162. } else
  163. #endif // !NO_SHARED_MEMORY
  164. {
  165. for (i = 0; i < numrects; ++i) {
  166. x = rects[i].x;
  167. y = rects[i].y;
  168. w = rects[i].w;
  169. h = rects[i].h;
  170. if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0) {
  171. // Clipped?
  172. continue;
  173. }
  174. if (x < 0) {
  175. x += w;
  176. w += rects[i].x;
  177. }
  178. if (y < 0) {
  179. y += h;
  180. h += rects[i].y;
  181. }
  182. if (x + w > window_w) {
  183. w = window_w - x;
  184. }
  185. if (y + h > window_h) {
  186. h = window_h - y;
  187. }
  188. X11_XPutImage(display, data->xwindow, data->gc, data->ximage,
  189. x, y, x, y, w, h);
  190. }
  191. }
  192. #ifdef SDL_VIDEO_DRIVER_X11_XSYNC
  193. X11_HandlePresent(data->window);
  194. #endif /* SDL_VIDEO_DRIVER_X11_XSYNC */
  195. X11_XSync(display, False);
  196. return true;
  197. }
  198. void X11_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window)
  199. {
  200. SDL_WindowData *data = window->internal;
  201. Display *display;
  202. if (!data) {
  203. // The window wasn't fully initialized
  204. return;
  205. }
  206. display = data->videodata->display;
  207. if (data->ximage) {
  208. XDestroyImage(data->ximage);
  209. #ifndef NO_SHARED_MEMORY
  210. if (data->use_mitshm) {
  211. X11_XShmDetach(display, &data->shminfo);
  212. X11_XSync(display, False);
  213. shmdt(data->shminfo.shmaddr);
  214. data->use_mitshm = false;
  215. }
  216. #endif // !NO_SHARED_MEMORY
  217. data->ximage = NULL;
  218. }
  219. if (data->gc) {
  220. X11_XFreeGC(display, data->gc);
  221. data->gc = NULL;
  222. }
  223. }
  224. #endif // SDL_VIDEO_DRIVER_X11