comparison src/video/x11/SDL_x11render.c @ 2810:27cb878a278e

Implemented the X11 (non-OpenGL) renderer, no alpha or scaling available.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 30 Nov 2008 21:58:23 +0000
parents
children 7af2419ad5b0
comparison
equal deleted inserted replaced
2809:7e257c3a3bf0 2810:27cb878a278e
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2006 Sam Lantinga
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Sam Lantinga
20 slouken@libsdl.org
21 */
22 #include "SDL_config.h"
23
24 #if SDL_VIDEO_RENDER_X11
25
26 #include "SDL_x11video.h"
27 #include "../SDL_rect_c.h"
28 #include "../SDL_pixels_c.h"
29 #include "../SDL_yuv_sw_c.h"
30
31 /* X11 renderer implementation */
32
33 static SDL_Renderer *X11_CreateRenderer(SDL_Window * window, Uint32 flags);
34 static int X11_DisplayModeChanged(SDL_Renderer * renderer);
35 static int X11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
36 static int X11_QueryTexturePixels(SDL_Renderer * renderer,
37 SDL_Texture * texture, void **pixels,
38 int *pitch);
39 static int X11_SetTextureBlendMode(SDL_Renderer * renderer,
40 SDL_Texture * texture);
41 static int X11_SetTextureScaleMode(SDL_Renderer * renderer,
42 SDL_Texture * texture);
43 static int X11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
44 const SDL_Rect * rect, const void *pixels,
45 int pitch);
46 static int X11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
47 const SDL_Rect * rect, int markDirty,
48 void **pixels, int *pitch);
49 static void X11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
50 static int X11_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
51 Uint8 a, const SDL_Rect * rect);
52 static int X11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
53 const SDL_Rect * srcrect, const SDL_Rect * dstrect);
54 static void X11_RenderPresent(SDL_Renderer * renderer);
55 static void X11_DestroyTexture(SDL_Renderer * renderer,
56 SDL_Texture * texture);
57 static void X11_DestroyRenderer(SDL_Renderer * renderer);
58
59
60 SDL_RenderDriver X11_RenderDriver = {
61 X11_CreateRenderer,
62 {
63 "x11",
64 (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
65 SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
66 SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED),
67 SDL_TEXTUREMODULATE_NONE,
68 SDL_TEXTUREBLENDMODE_NONE,
69 SDL_TEXTURESCALEMODE_NONE,
70 0,
71 {0},
72 0,
73 0}
74 };
75
76 typedef struct
77 {
78 Display *display;
79 int screen;
80 Window window;
81 Pixmap pixmaps[3];
82 int current_pixmap;
83 Drawable drawable;
84 SDL_PixelFormat *format;
85 GC gc;
86 SDL_DirtyRectList dirty;
87 SDL_bool makedirty;
88 } X11_RenderData;
89
90 typedef struct
91 {
92 SDL_SW_YUVTexture *yuv;
93 Uint32 format;
94 Pixmap pixmap;
95 XImage *image;
96 #ifndef NO_SHARED_MEMORY
97 /* MIT shared memory extension information */
98 XShmSegmentInfo shminfo;
99 #endif
100 void *pixels;
101 int pitch;
102 } X11_TextureData;
103
104 #ifndef NO_SHARED_MEMORY
105 /* Shared memory error handler routine */
106 static int shm_error;
107 static int (*X_handler)(Display *, XErrorEvent *) = NULL;
108 static int shm_errhandler(Display *d, XErrorEvent *e)
109 {
110 if (e->error_code == BadAccess) {
111 shm_error = True;
112 return(0);
113 } else {
114 return(X_handler(d,e));
115 }
116 }
117 #endif /* ! NO_SHARED_MEMORY */
118
119 static void
120 UpdateYUVTextureData(SDL_Texture * texture)
121 {
122 X11_TextureData *data = (X11_TextureData *) texture->driverdata;
123 SDL_Rect rect;
124
125 rect.x = 0;
126 rect.y = 0;
127 rect.w = texture->w;
128 rect.h = texture->h;
129 SDL_SW_CopyYUVToRGB(data->yuv, &rect, data->format, texture->w,
130 texture->h, data->pixels, data->pitch);
131 }
132
133 static Uint32
134 X11_GetPixelFormatFromDepth(Display *display, int screen, int depth, int bpp)
135 {
136 XVisualInfo vinfo;
137
138 if (XMatchVisualInfo(display, screen, depth, DirectColor, &vinfo) ||
139 XMatchVisualInfo(display, screen, depth, TrueColor, &vinfo)) {
140 Uint32 Rmask, Gmask, Bmask, Amask;
141
142 Rmask = vinfo.visual->red_mask;
143 Gmask = vinfo.visual->green_mask;
144 Bmask = vinfo.visual->blue_mask;
145 if (vinfo.depth == 32) {
146 Amask = (0xFFFFFFFF & ~(Rmask | Gmask | Bmask));
147 } else {
148 Amask = 0;
149 }
150 return SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
151 }
152
153 /* No matching visual, try to pick a safe default */
154 switch (depth) {
155 case 15:
156 return SDL_PIXELFORMAT_RGB555;
157 case 16:
158 return SDL_PIXELFORMAT_RGB565;
159 default:
160 break;
161 }
162 return SDL_PIXELFORMAT_UNKNOWN;
163 }
164
165 void
166 X11_AddRenderDriver(_THIS)
167 {
168 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
169 SDL_RendererInfo *info = &X11_RenderDriver.info;
170 XPixmapFormatValues *pixmapFormats;
171 int i, n, bpp;
172
173 /* Query the available texture formats */
174 pixmapFormats = XListPixmapFormats(data->display, &n);
175 if (pixmapFormats) {
176 info->num_texture_formats = 0;
177 for (i = 0; i < n; ++i) {
178 Uint32 format;
179
180 if (pixmapFormats[i].depth == 24) {
181 bpp = pixmapFormats[i].bits_per_pixel;
182 } else {
183 bpp = pixmapFormats[i].depth;
184 }
185 format = X11_GetPixelFormatFromDepth(data->display, DefaultScreen(data->display), pixmapFormats[i].depth, bpp);
186 if (format != SDL_PIXELFORMAT_UNKNOWN) {
187 info->texture_formats[info->num_texture_formats++] = format;
188 }
189 }
190 XFree(pixmapFormats);
191
192 if (info->num_texture_formats == 0) {
193 return;
194 }
195 info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_YUY2;
196 info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_UYVY;
197 info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_YVYU;
198 }
199
200 SDL_AddRenderDriver(0, &X11_RenderDriver);
201 }
202
203 SDL_Renderer *
204 X11_CreateRenderer(SDL_Window * window, Uint32 flags)
205 {
206 SDL_DisplayData *displaydata =
207 (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
208 SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
209 SDL_Renderer *renderer;
210 SDL_RendererInfo *info;
211 X11_RenderData *data;
212 XWindowAttributes attributes;
213 XGCValues gcv;
214 int i, n;
215 int bpp;
216 Uint32 Rmask, Gmask, Bmask, Amask;
217
218 renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
219 if (!renderer) {
220 SDL_OutOfMemory();
221 return NULL;
222 }
223
224 data = (X11_RenderData *) SDL_calloc(1, sizeof(*data));
225 if (!data) {
226 X11_DestroyRenderer(renderer);
227 SDL_OutOfMemory();
228 return NULL;
229 }
230 data->display = windowdata->videodata->display;
231 data->screen = displaydata->screen;
232 data->window = windowdata->window;
233
234 renderer->DisplayModeChanged = X11_DisplayModeChanged;
235 renderer->CreateTexture = X11_CreateTexture;
236 renderer->QueryTexturePixels = X11_QueryTexturePixels;
237 renderer->SetTextureBlendMode = X11_SetTextureBlendMode;
238 renderer->SetTextureScaleMode = X11_SetTextureScaleMode;
239 renderer->UpdateTexture = X11_UpdateTexture;
240 renderer->LockTexture = X11_LockTexture;
241 renderer->UnlockTexture = X11_UnlockTexture;
242 renderer->RenderFill = X11_RenderFill;
243 renderer->RenderCopy = X11_RenderCopy;
244 renderer->RenderPresent = X11_RenderPresent;
245 renderer->DestroyTexture = X11_DestroyTexture;
246 renderer->DestroyRenderer = X11_DestroyRenderer;
247 renderer->info = X11_RenderDriver.info;
248 renderer->window = window->id;
249 renderer->driverdata = data;
250
251 renderer->info.flags = SDL_RENDERER_ACCELERATED;
252
253 if (flags & SDL_RENDERER_SINGLEBUFFER) {
254 renderer->info.flags |=
255 (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY);
256 n = 0;
257 } else if (flags & SDL_RENDERER_PRESENTFLIP2) {
258 renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
259 n = 2;
260 } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
261 renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
262 n = 3;
263 } else {
264 renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
265 n = 1;
266 }
267 XGetWindowAttributes(data->display, data->window, &attributes);
268 for (i = 0; i < n; ++i) {
269 data->pixmaps[i] = XCreatePixmap(data->display, data->window, window->w, window->h, attributes.depth);
270 if (data->pixmaps[i] == None) {
271 X11_DestroyRenderer(renderer);
272 SDL_SetError("XCreatePixmap() failed");
273 return NULL;
274 }
275 }
276 if (n > 0) {
277 data->drawable = data->pixmaps[0];
278 data->makedirty = SDL_TRUE;
279 } else {
280 data->drawable = data->window;
281 data->makedirty = SDL_FALSE;
282 }
283 data->current_pixmap = 0;
284
285 /* Get the format of the window */
286 bpp = attributes.depth;
287 if (bpp == 24) {
288 XPixmapFormatValues *p = XListPixmapFormats(data->display, &n);
289 if (p) {
290 for (i = 0; i < n; ++i) {
291 if (p[i].depth == 24) {
292 bpp = p[i].bits_per_pixel;
293 break;
294 }
295 }
296 XFree(p);
297 }
298 }
299 Rmask = attributes.visual->red_mask;
300 Gmask = attributes.visual->green_mask;
301 Bmask = attributes.visual->blue_mask;
302 if (attributes.depth == 32) {
303 Amask = (0xFFFFFFFF & ~(Rmask | Gmask | Bmask));
304 } else {
305 Amask = 0;
306 }
307 data->format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
308 if (!data->format) {
309 X11_DestroyRenderer(renderer);
310 return NULL;
311 }
312
313 /* Create the drawing context */
314 gcv.graphics_exposures = False;
315 data->gc = XCreateGC(data->display, data->window, GCGraphicsExposures, &gcv);
316 if (!data->gc) {
317 X11_DestroyRenderer(renderer);
318 SDL_SetError("XCreateGC() failed");
319 return NULL;
320 }
321
322 return renderer;
323 }
324
325 static int
326 X11_DisplayModeChanged(SDL_Renderer * renderer)
327 {
328 X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
329 SDL_Window *window = SDL_GetWindowFromID(renderer->window);
330 XWindowAttributes attributes;
331 int i, n;
332
333 if (renderer->info.flags & SDL_RENDERER_SINGLEBUFFER) {
334 n = 0;
335 } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
336 n = 2;
337 } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
338 n = 3;
339 } else {
340 n = 1;
341 }
342 for (i = 0; i < n; ++i) {
343 if (data->pixmaps[i] != None) {
344 XFreePixmap(data->display, data->pixmaps[i]);
345 data->pixmaps[i] = None;
346 }
347 }
348 XGetWindowAttributes(data->display, data->window, &attributes);
349 for (i = 0; i < n; ++i) {
350 data->pixmaps[i] = XCreatePixmap(data->display, data->window, window->w, window->h, attributes.depth);
351 if (data->pixmaps[i] == None) {
352 SDL_SetError("XCreatePixmap() failed");
353 return -1;
354 }
355 }
356 if (n > 0) {
357 data->drawable = data->pixmaps[0];
358 }
359 data->current_pixmap = 0;
360
361 return 0;
362 }
363
364 static int
365 X11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
366 {
367 X11_RenderData *renderdata = (X11_RenderData *) renderer->driverdata;
368 SDL_Window *window = SDL_GetWindowFromID(renderer->window);
369 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
370 X11_TextureData *data;
371 XWindowAttributes attributes;
372 int depth, order;
373
374 data = (X11_TextureData *) SDL_calloc(1, sizeof(*data));
375 if (!data) {
376 SDL_OutOfMemory();
377 return -1;
378 }
379
380 texture->driverdata = data;
381
382 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
383 data->yuv =
384 SDL_SW_CreateYUVTexture(texture->format, texture->w, texture->h);
385 if (!data->yuv) {
386 return -1;
387 }
388 data->format = display->current_mode.format;
389 } else {
390 data->format = texture->format;
391 }
392 data->pitch = texture->w*SDL_BYTESPERPIXEL(data->format);
393
394 XGetWindowAttributes(renderdata->display, renderdata->window, &attributes);
395 depth = SDL_BITSPERPIXEL(data->format);
396 order = SDL_PIXELORDER(data->format);
397 if (depth == 32 && (order == SDL_PACKEDORDER_XRGB || order == SDL_PACKEDORDER_RGBX || SDL_PACKEDORDER_XBGR || order == SDL_PACKEDORDER_BGRX)) {
398 depth = 24;
399 }
400
401 if (data->yuv || texture->access == SDL_TEXTUREACCESS_STREAMING) {
402 #ifndef NO_SHARED_MEMORY
403 XShmSegmentInfo *shminfo = &data->shminfo;
404
405 shm_error = True;
406
407 if (SDL_X11_HAVE_SHM) {
408 shminfo->shmid = shmget(IPC_PRIVATE, texture->h*data->pitch, IPC_CREAT | 0777);
409 if (shminfo->shmid >= 0) {
410 shminfo->shmaddr = (char *)shmat(shminfo->shmid, 0, 0);
411 shminfo->readOnly = False;
412 if (shminfo->shmaddr != (char *)-1) {
413 shm_error = False;
414 X_handler = XSetErrorHandler(shm_errhandler);
415 XShmAttach(renderdata->display, shminfo);
416 XSync(renderdata->display, True);
417 XSetErrorHandler(X_handler);
418 if (shm_error) {
419 shmdt(shminfo->shmaddr);
420 }
421 }
422 shmctl(shminfo->shmid, IPC_RMID, NULL);
423 }
424 }
425 if (!shm_error) {
426 data->pixels = shminfo->shmaddr;
427
428 data->image = XShmCreateImage(renderdata->display, attributes.visual, depth, ZPixmap, shminfo->shmaddr, shminfo, texture->w, texture->h);
429 if(!data->image) {
430 XShmDetach(renderdata->display, shminfo);
431 XSync(renderdata->display, False);
432 shmdt(shminfo->shmaddr);
433 shm_error = True;
434 }
435 }
436 if (shm_error) {
437 shminfo->shmaddr = NULL;
438 }
439 if (!data->image)
440 #endif /* not NO_SHARED_MEMORY */
441 {
442 data->pixels = SDL_malloc(texture->h * data->pitch);
443 if (!data->pixels) {
444 SDL_OutOfMemory();
445 return -1;
446 }
447
448 data->image = XCreateImage(renderdata->display, attributes.visual, depth, ZPixmap, 0, data->pixels, texture->w, texture->h, SDL_BYTESPERPIXEL(data->format)*8, data->pitch);
449 if (!data->image) {
450 SDL_SetError("XCreateImage() failed");
451 return -1;
452 }
453 }
454 } else {
455 data->pixmap = XCreatePixmap(renderdata->display, renderdata->window, texture->w, texture->h, depth);
456 if (data->pixmap == None) {
457 SDL_SetError("XCteatePixmap() failed");
458 return -1;
459 }
460
461 data->image = XCreateImage(renderdata->display, attributes.visual, depth, ZPixmap, 0, NULL, texture->w, texture->h, SDL_BYTESPERPIXEL(data->format)*8, data->pitch);
462 if (!data->image) {
463 SDL_SetError("XCreateImage() failed");
464 return -1;
465 }
466 }
467
468 return 0;
469 }
470
471 static int
472 X11_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
473 void **pixels, int *pitch)
474 {
475 X11_TextureData *data = (X11_TextureData *) texture->driverdata;
476
477 if (data->yuv) {
478 return SDL_SW_QueryYUVTexturePixels(data->yuv, pixels, pitch);
479 } else {
480 *pixels = data->pixels;
481 *pitch = data->pitch;
482 return 0;
483 }
484 }
485
486 static int
487 X11_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
488 {
489 switch (texture->blendMode) {
490 case SDL_TEXTUREBLENDMODE_NONE:
491 return 0;
492 default:
493 SDL_Unsupported();
494 texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
495 return -1;
496 }
497 }
498
499 static int
500 X11_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
501 {
502 switch (texture->scaleMode) {
503 case SDL_TEXTURESCALEMODE_NONE:
504 return 0;
505 default:
506 SDL_Unsupported();
507 texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
508 return -1;
509 }
510 return 0;
511 }
512
513 static int
514 X11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
515 const SDL_Rect * rect, const void *pixels, int pitch)
516 {
517 X11_TextureData *data = (X11_TextureData *) texture->driverdata;
518
519 if (data->yuv) {
520 if (SDL_SW_UpdateYUVTexture(data->yuv, rect, pixels, pitch) < 0) {
521 return -1;
522 }
523 UpdateYUVTextureData(texture);
524 return 0;
525 } else {
526 X11_RenderData *renderdata = (X11_RenderData *) renderer->driverdata;
527
528 if (data->pixels) {
529 Uint8 *src, *dst;
530 int row;
531 size_t length;
532
533 src = (Uint8 *) pixels;
534 dst =
535 (Uint8 *) data->pixels + rect->y * data->pitch +
536 rect->x * SDL_BYTESPERPIXEL(texture->format);
537 length = rect->w * SDL_BYTESPERPIXEL(texture->format);
538 for (row = 0; row < rect->h; ++row) {
539 SDL_memcpy(dst, src, length);
540 src += pitch;
541 dst += data->pitch;
542 }
543 } else {
544 data->image->width = rect->w;
545 data->image->height = rect->h;
546 data->image->data = (char *)pixels;
547 data->image->bytes_per_line = pitch;
548 XPutImage(renderdata->display, data->pixmap, renderdata->gc, data->image, 0, 0, rect->x, rect->y, rect->w, rect->h);
549 }
550 return 0;
551 }
552 }
553
554 static int
555 X11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
556 const SDL_Rect * rect, int markDirty, void **pixels,
557 int *pitch)
558 {
559 X11_TextureData *data = (X11_TextureData *) texture->driverdata;
560
561 if (data->yuv) {
562 return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
563 pitch);
564 } else if (data->pixels) {
565 *pixels =
566 (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
567 rect->x * SDL_BYTESPERPIXEL(texture->format));
568 *pitch = data->pitch;
569 return 0;
570 } else {
571 SDL_SetError("No pixels available");
572 return -1;
573 }
574 }
575
576 static void
577 X11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
578 {
579 X11_TextureData *data = (X11_TextureData *) texture->driverdata;
580
581 if (data->yuv) {
582 SDL_SW_UnlockYUVTexture(data->yuv);
583 UpdateYUVTextureData(texture);
584 }
585 }
586
587 static int
588 X11_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
589 const SDL_Rect * rect)
590 {
591 X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
592 unsigned long foreground;
593
594 if (data->makedirty) {
595 SDL_AddDirtyRect(&data->dirty, rect);
596 }
597
598 foreground = SDL_MapRGBA(data->format, r, g, b, a);
599 XSetForeground(data->display, data->gc, foreground);
600 XDrawRectangle(data->display, data->drawable, data->gc, rect->x, rect->y, rect->w, rect->h);
601 return 0;
602 }
603
604 static int
605 X11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
606 const SDL_Rect * srcrect, const SDL_Rect * dstrect)
607 {
608 X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
609 X11_TextureData *texturedata = (X11_TextureData *) texture->driverdata;
610
611 if (data->makedirty) {
612 SDL_AddDirtyRect(&data->dirty, dstrect);
613 }
614
615 if (srcrect->w == dstrect->w && srcrect->h == dstrect->h) {
616 #ifndef NO_SHARED_MEMORY
617 if (texturedata->shminfo.shmaddr) {
618 XShmPutImage(data->display, data->drawable, data->gc, texturedata->image, srcrect->x, srcrect->y, dstrect->x, dstrect->y, dstrect->w, dstrect->h, False);
619 }
620 else
621 #endif
622 if (texturedata->pixels) {
623 XPutImage(data->display, data->drawable, data->gc, texturedata->image, srcrect->x, srcrect->y, dstrect->x, dstrect->y, dstrect->w, dstrect->h);
624 } else {
625 XCopyArea(data->display, texturedata->pixmap, data->drawable,
626 data->gc, srcrect->x, srcrect->y, dstrect->w, dstrect->h,
627 dstrect->x, dstrect->y);
628 }
629 } else {
630 SDL_SetError("Scaling not supported in the X11 renderer");
631 return -1;
632 }
633 return 0;
634 }
635
636 static void
637 X11_RenderPresent(SDL_Renderer * renderer)
638 {
639 X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
640 SDL_DirtyRect *dirty;
641
642 /* Send the data to the display */
643 if (!(renderer->info.flags & SDL_RENDERER_SINGLEBUFFER)) {
644 for (dirty = data->dirty.list; dirty; dirty = dirty->next) {
645 const SDL_Rect *rect = &dirty->rect;
646 XCopyArea(data->display, data->drawable, data->window,
647 data->gc, rect->x, rect->y, rect->w, rect->h,
648 rect->x, rect->y);
649 }
650 SDL_ClearDirtyRects(&data->dirty);
651 }
652 XSync(data->display, False);
653
654 /* Update the flipping chain, if any */
655 if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
656 data->current_pixmap = (data->current_pixmap + 1) % 2;
657 data->drawable = data->pixmaps[data->current_pixmap];
658 } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
659 data->current_pixmap = (data->current_pixmap + 1) % 3;
660 data->drawable = data->pixmaps[data->current_pixmap];
661 }
662 }
663
664 static void
665 X11_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
666 {
667 X11_RenderData *renderdata = (X11_RenderData *) renderer->driverdata;
668 X11_TextureData *data = (X11_TextureData *) texture->driverdata;
669
670 if (!data) {
671 return;
672 }
673 if (data->yuv) {
674 SDL_SW_DestroyYUVTexture(data->yuv);
675 }
676 if (data->pixmap != None) {
677 XFreePixmap(renderdata->display, data->pixmap);
678 }
679 if (data->image) {
680 data->image->data = NULL;
681 XDestroyImage(data->image);
682 }
683 #ifndef NO_SHARED_MEMORY
684 if (data->shminfo.shmaddr) {
685 XShmDetach(renderdata->display, &data->shminfo);
686 XSync(renderdata->display, False);
687 shmdt(data->shminfo.shmaddr);
688 data->pixels = NULL;
689 }
690 #endif
691 if (data->pixels) {
692 SDL_free(data->pixels);
693 }
694 SDL_free(data);
695 texture->driverdata = NULL;
696 }
697
698 static void
699 X11_DestroyRenderer(SDL_Renderer * renderer)
700 {
701 X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
702 int i;
703
704 if (data) {
705 for (i = 0; i < SDL_arraysize(data->pixmaps); ++i) {
706 if (data->pixmaps[i] != None) {
707 XFreePixmap(data->display, data->pixmaps[i]);
708 }
709 }
710 if (data->format) {
711 SDL_FreeFormat(data->format);
712 }
713 if (data->gc) {
714 XFreeGC(data->display, data->gc);
715 }
716 SDL_FreeDirtyRects(&data->dirty);
717 SDL_free(data);
718 }
719 SDL_free(renderer);
720 }
721
722 #endif /* SDL_VIDEO_RENDER_X11 */
723
724 /* vi: set ts=4 sw=4 expandtab: */