Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
877 lines (790 loc) · 27.6 KB

SDL_x11render.c

File metadata and controls

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