SDL  2.0
SDL_x11framebuffer.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 #include "../../SDL_internal.h"
22 
23 #if SDL_VIDEO_DRIVER_X11
24 
25 #include "SDL_x11video.h"
26 #include "SDL_x11framebuffer.h"
27 
28 
29 #ifndef NO_SHARED_MEMORY
30 
31 /* Shared memory error handler routine */
32 static int shm_error;
33 static int (*X_handler)(Display *, XErrorEvent *) = NULL;
34 static int shm_errhandler(Display *d, XErrorEvent *e)
35 {
36  if ( e->error_code == BadAccess ) {
37  shm_error = True;
38  return(0);
39  } else
40  return(X_handler(d,e));
41 }
42 
43 static SDL_bool have_mitshm(Display *dpy)
44 {
45  /* Only use shared memory on local X servers */
46  return X11_XShmQueryExtension(dpy) ? SDL_X11_HAVE_SHM : SDL_FALSE;
47 }
48 
49 #endif /* !NO_SHARED_MEMORY */
50 
51 int
53  void ** pixels, int *pitch)
54 {
55  SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
56  Display *display = data->videodata->display;
57  XGCValues gcv;
58  XVisualInfo vinfo;
59 
60  /* Free the old framebuffer surface */
62 
63  /* Create the graphics context for drawing */
64  gcv.graphics_exposures = False;
65  data->gc = X11_XCreateGC(display, data->xwindow, GCGraphicsExposures, &gcv);
66  if (!data->gc) {
67  return SDL_SetError("Couldn't create graphics context");
68  }
69 
70  /* Find out the pixel format and depth */
71  if (X11_GetVisualInfoFromVisual(display, data->visual, &vinfo) < 0) {
72  return SDL_SetError("Couldn't get window visual information");
73  }
74 
75  *format = X11_GetPixelFormatFromVisualInfo(display, &vinfo);
77  return SDL_SetError("Unknown window pixel format");
78  }
79 
80  /* Calculate pitch */
81  *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
82 
83  /* Create the actual image */
84 #ifndef NO_SHARED_MEMORY
85  if (have_mitshm(display)) {
86  XShmSegmentInfo *shminfo = &data->shminfo;
87 
88  shminfo->shmid = shmget(IPC_PRIVATE, window->h*(*pitch), IPC_CREAT | 0777);
89  if ( shminfo->shmid >= 0 ) {
90  shminfo->shmaddr = (char *)shmat(shminfo->shmid, 0, 0);
91  shminfo->readOnly = False;
92  if ( shminfo->shmaddr != (char *)-1 ) {
93  shm_error = False;
94  X_handler = X11_XSetErrorHandler(shm_errhandler);
95  X11_XShmAttach(display, shminfo);
96  X11_XSync(display, False);
97  X11_XSetErrorHandler(X_handler);
98  if ( shm_error )
99  shmdt(shminfo->shmaddr);
100  } else {
101  shm_error = True;
102  }
103  shmctl(shminfo->shmid, IPC_RMID, NULL);
104  } else {
105  shm_error = True;
106  }
107  if (!shm_error) {
108  data->ximage = X11_XShmCreateImage(display, data->visual,
109  vinfo.depth, ZPixmap,
110  shminfo->shmaddr, shminfo,
111  window->w, window->h);
112  if (!data->ximage) {
113  X11_XShmDetach(display, shminfo);
114  X11_XSync(display, False);
115  shmdt(shminfo->shmaddr);
116  } else {
117  /* Done! */
118  data->use_mitshm = SDL_TRUE;
119  *pixels = shminfo->shmaddr;
120  return 0;
121  }
122  }
123  }
124 #endif /* not NO_SHARED_MEMORY */
125 
126  *pixels = SDL_malloc(window->h*(*pitch));
127  if (*pixels == NULL) {
128  return SDL_OutOfMemory();
129  }
130 
131  data->ximage = X11_XCreateImage(display, data->visual,
132  vinfo.depth, ZPixmap, 0, (char *)(*pixels),
133  window->w, window->h, 32, 0);
134  if (!data->ximage) {
135  SDL_free(*pixels);
136  return SDL_SetError("Couldn't create XImage");
137  }
138  return 0;
139 }
140 
141 int
143  int numrects)
144 {
145  SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
146  Display *display = data->videodata->display;
147  int i;
148  int x, y, w ,h;
149 #ifndef NO_SHARED_MEMORY
150  if (data->use_mitshm) {
151  for (i = 0; i < numrects; ++i) {
152  x = rects[i].x;
153  y = rects[i].y;
154  w = rects[i].w;
155  h = rects[i].h;
156 
157  if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0) {
158  /* Clipped? */
159  continue;
160  }
161  if (x < 0)
162  {
163  x += w;
164  w += rects[i].x;
165  }
166  if (y < 0)
167  {
168  y += h;
169  h += rects[i].y;
170  }
171  if (x + w > window->w)
172  w = window->w - x;
173  if (y + h > window->h)
174  h = window->h - y;
175 
176  X11_XShmPutImage(display, data->xwindow, data->gc, data->ximage,
177  x, y, x, y, w, h, False);
178  }
179  }
180  else
181 #endif /* !NO_SHARED_MEMORY */
182  {
183  for (i = 0; i < numrects; ++i) {
184  x = rects[i].x;
185  y = rects[i].y;
186  w = rects[i].w;
187  h = rects[i].h;
188 
189  if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0) {
190  /* Clipped? */
191  continue;
192  }
193  if (x < 0)
194  {
195  x += w;
196  w += rects[i].x;
197  }
198  if (y < 0)
199  {
200  y += h;
201  h += rects[i].y;
202  }
203  if (x + w > window->w)
204  w = window->w - x;
205  if (y + h > window->h)
206  h = window->h - y;
207 
208  X11_XPutImage(display, data->xwindow, data->gc, data->ximage,
209  x, y, x, y, w, h);
210  }
211  }
212 
213  X11_XSync(display, False);
214 
215  return 0;
216 }
217 
218 void
220 {
221  SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
222  Display *display;
223 
224  if (!data) {
225  /* The window wasn't fully initialized */
226  return;
227  }
228 
229  display = data->videodata->display;
230 
231  if (data->ximage) {
232  XDestroyImage(data->ximage);
233 
234 #ifndef NO_SHARED_MEMORY
235  if (data->use_mitshm) {
236  X11_XShmDetach(display, &data->shminfo);
237  X11_XSync(display, False);
238  shmdt(data->shminfo.shmaddr);
239  data->use_mitshm = SDL_FALSE;
240  }
241 #endif /* !NO_SHARED_MEMORY */
242 
243  data->ximage = NULL;
244  }
245  if (data->gc) {
246  X11_XFreeGC(display, data->gc);
247  data->gc = NULL;
248  }
249 }
250 
251 #endif /* SDL_VIDEO_DRIVER_X11 */
252 
253 /* vi: set ts=4 sw=4 expandtab: */
format
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
X11_DestroyWindowFramebuffer
void X11_DestroyWindowFramebuffer(_THIS, SDL_Window *window)
SDL_x11video.h
X11_GetPixelFormatFromVisualInfo
Uint32 X11_GetPixelFormatFromVisualInfo(Display *display, XVisualInfo *vinfo)
if
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro pixld1_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl else error unsupported endif endm macro pixld2_s mem_operand if mov asr add asl add asl mov asr sub UNIT_X add asl mov asr add asl add asl mov asr add UNIT_X add asl else pixld1_s mem_operand pixld1_s mem_operand endif endm macro pixld0_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl endif endm macro pixld_s_internal mem_operand if mem_operand pixld2_s mem_operand pixdeinterleave basereg elseif mem_operand elseif mem_operand elseif mem_operand elseif mem_operand pixld0_s mem_operand else pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else error unsupported mem_operand if bpp mem_operand endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld[DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld if[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro fetch_mask_pixblock pixld mask_basereg pixblock_size MASK endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp skip1(dst_w_bpp<=(lowbit *8)) &&((lowbit *8)<(pixblock_size *dst_w_bpp)) .if lowbit< 16 tst DST_R
Definition: pixman-arm-neon-asm.h:469
NULL
#define NULL
Definition: begin_code.h:167
dpy
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display dpy)
Definition: SDL_x11sym.h:44
SDL_WindowData
Definition: SDL_androidwindow.h:38
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1949
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:74
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
_this
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
X11_CreateWindowFramebuffer
int X11_CreateWindowFramebuffer(_THIS, SDL_Window *window, Uint32 *format, void **pixels, int *pitch)
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
X11_UpdateWindowFramebuffer
int X11_UpdateWindowFramebuffer(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects)
X11_GetVisualInfoFromVisual
int X11_GetVisualInfoFromVisual(Display *display, Visual *visual, XVisualInfo *vinfo)
pixels
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
SDL_VideoData::display
struct wl_display * display
Definition: SDL_waylandvideo.h:50
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_BYTESPERPIXEL
#define SDL_BYTESPERPIXEL(X)
Definition: SDL_pixels.h:128
SDL_Rect
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:77
e
const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char const char const SDL_SCANF_FORMAT_STRING char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
Definition: SDL_dynapi_procs.h:117
SDL_PIXELFORMAT_UNKNOWN
@ SDL_PIXELFORMAT_UNKNOWN
Definition: SDL_pixels.h:173
SDL_WindowData::videodata
struct SDL_VideoData * videodata
Definition: SDL_cocoawindow.h:121
SDL_x11framebuffer.h
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
rects
EGLSurface EGLint * rects
Definition: eglext.h:282
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
d
const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char const char const SDL_SCANF_FORMAT_STRING char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 ** d
Definition: SDL_dynapi_procs.h:117
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:161
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:734