SDL_x11framebuffer.c
Go to the documentation of this file.
/*
Simple DirectMedia Layer
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_X11
#include "SDL_x11video.h"
#include "SDL_x11framebuffer.h"
#ifndef NO_SHARED_MEMORY
/* Shared memory error handler routine */
static int shm_error;
static int (*X_handler)(Display *, XErrorEvent *) = NULL;
static int shm_errhandler(Display *d, XErrorEvent *e)
{
if ( e->error_code == BadAccess ) {
shm_error = True;
return(0);
} else
return(X_handler(d,e));
}
static SDL_bool have_mitshm(void)
{
/* Only use shared memory on local X servers */
if ( (SDL_strncmp(X11_XDisplayName(NULL), ":", 1) == 0) ||
(SDL_strncmp(X11_XDisplayName(NULL), "unix:", 5) == 0) ) {
return SDL_X11_HAVE_SHM;
}
return SDL_FALSE;
}
#endif /* !NO_SHARED_MEMORY */
int
X11_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format,
void ** pixels, int *pitch)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
Display *display = data->videodata->display;
XGCValues gcv;
XVisualInfo vinfo;
/* Free the old framebuffer surface */
X11_DestroyWindowFramebuffer(_this, window);
/* Create the graphics context for drawing */
gcv.graphics_exposures = False;
data->gc = X11_XCreateGC(display, data->xwindow, GCGraphicsExposures, &gcv);
if (!data->gc) {
return SDL_SetError("Couldn't create graphics context");
}
/* Find out the pixel format and depth */
if (X11_GetVisualInfoFromVisual(display, data->visual, &vinfo) < 0) {
return SDL_SetError("Couldn't get window visual information");
}
*format = X11_GetPixelFormatFromVisualInfo(display, &vinfo);
if (*format == SDL_PIXELFORMAT_UNKNOWN) {
return SDL_SetError("Unknown window pixel format");
}
/* Calculate pitch */
*pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
/* Create the actual image */
#ifndef NO_SHARED_MEMORY
if (have_mitshm()) {
XShmSegmentInfo *shminfo = &data->shminfo;
shminfo->shmid = shmget(IPC_PRIVATE, window->h*(*pitch), IPC_CREAT | 0777);
if ( shminfo->shmid >= 0 ) {
shminfo->shmaddr = (char *)shmat(shminfo->shmid, 0, 0);
shminfo->readOnly = False;
if ( shminfo->shmaddr != (char *)-1 ) {
shm_error = False;
X_handler = X11_XSetErrorHandler(shm_errhandler);
X11_XShmAttach(display, shminfo);
X11_XSync(display, False);
X11_XSetErrorHandler(X_handler);
if ( shm_error )
shmdt(shminfo->shmaddr);
} else {
shm_error = True;
}
shmctl(shminfo->shmid, IPC_RMID, NULL);
} else {
shm_error = True;
}
if (!shm_error) {
data->ximage = X11_XShmCreateImage(display, data->visual,
vinfo.depth, ZPixmap,
shminfo->shmaddr, shminfo,
window->w, window->h);
if (!data->ximage) {
X11_XShmDetach(display, shminfo);
X11_XSync(display, False);
shmdt(shminfo->shmaddr);
} else {
/* Done! */
data->use_mitshm = SDL_TRUE;
*pixels = shminfo->shmaddr;
return 0;
}
}
}
#endif /* not NO_SHARED_MEMORY */
*pixels = SDL_malloc(window->h*(*pitch));
if (*pixels == NULL) {
return SDL_OutOfMemory();
}
data->ximage = X11_XCreateImage(display, data->visual,
vinfo.depth, ZPixmap, 0, (char *)(*pixels),
window->w, window->h, 32, 0);
if (!data->ximage) {
SDL_free(*pixels);
return SDL_SetError("Couldn't create XImage");
}
return 0;
}
int
X11_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects,
int numrects)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
Display *display = data->videodata->display;
int i;
int x, y, w ,h;
#ifndef NO_SHARED_MEMORY
if (data->use_mitshm) {
for (i = 0; i < numrects; ++i) {
x = rects[i].x;
y = rects[i].y;
w = rects[i].w;
h = rects[i].h;
if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0) {
/* Clipped? */
continue;
}
if (x < 0)
{
x += w;
w += rects[i].x;
}
if (y < 0)
{
y += h;
h += rects[i].y;
}
if (x + w > window->w)
w = window->w - x;
if (y + h > window->h)
h = window->h - y;
X11_XShmPutImage(display, data->xwindow, data->gc, data->ximage,
x, y, x, y, w, h, False);
}
}
else
#endif /* !NO_SHARED_MEMORY */
{
for (i = 0; i < numrects; ++i) {
x = rects[i].x;
y = rects[i].y;
w = rects[i].w;
h = rects[i].h;
if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0) {
/* Clipped? */
continue;
}
if (x < 0)
{
x += w;
w += rects[i].x;
}
if (y < 0)
{
y += h;
h += rects[i].y;
}
if (x + w > window->w)
w = window->w - x;
if (y + h > window->h)
h = window->h - y;
X11_XPutImage(display, data->xwindow, data->gc, data->ximage,
x, y, x, y, w, h);
}
}
X11_XSync(display, False);
return 0;
}
void
X11_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
Display *display;
if (!data) {
/* The window wasn't fully initialized */
return;
}
display = data->videodata->display;
if (data->ximage) {
XDestroyImage(data->ximage);
#ifndef NO_SHARED_MEMORY
if (data->use_mitshm) {
X11_XShmDetach(display, &data->shminfo);
X11_XSync(display, False);
shmdt(data->shminfo.shmaddr);
data->use_mitshm = SDL_FALSE;
}
#endif /* !NO_SHARED_MEMORY */
data->ximage = NULL;
}
if (data->gc) {
X11_XFreeGC(display, data->gc);
data->gc = NULL;
}
}
#endif /* SDL_VIDEO_DRIVER_X11 */
/* 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)
NULL
#define NULL
Definition: begin_code.h:167
SDL_WindowData
Definition: SDL_androidwindow.h:38
SDL_strncmp
#define SDL_strncmp
Definition: SDL_dynapi_overrides.h:418
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1946
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:73
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
_this
static SDL_VideoDevice * _this
Definition: SDL_video.c:118
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:731