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

Latest commit

 

History

History
1288 lines (1174 loc) · 41.6 KB

SDL_x11render.c

File metadata and controls

1288 lines (1174 loc) · 41.6 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Jan 24, 2010
Jan 24, 2010
3
Copyright (C) 1997-2010 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
Dec 11, 2009
Dec 11, 2009
26
27
#include <limits.h> /* For INT_MIN and INT_MAX */
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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);
Dec 20, 2008
Dec 20, 2008
52
static int X11_SetDrawBlendMode(SDL_Renderer * renderer);
Dec 23, 2009
Dec 23, 2009
53
54
55
56
57
58
59
60
static int X11_RenderDrawPoints(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int X11_RenderDrawLines(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int X11_RenderDrawRects(SDL_Renderer * renderer,
const SDL_Rect ** rects, int count);
static int X11_RenderFillRects(SDL_Renderer * renderer,
const SDL_Rect ** rects, int count);
61
62
static int X11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
Dec 14, 2009
Dec 14, 2009
63
64
65
66
static int X11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 format, void * pixels, int pitch);
static int X11_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 format, const void * pixels, int pitch);
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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,
Dec 20, 2008
Dec 20, 2008
81
SDL_BLENDMODE_NONE,
82
83
84
85
86
87
88
89
90
91
92
SDL_TEXTURESCALEMODE_NONE,
0,
{0},
0,
0}
};
typedef struct
{
Display *display;
int screen;
Dec 14, 2008
Dec 14, 2008
93
94
Visual *visual;
int depth;
Dec 25, 2008
Dec 25, 2008
95
int scanline_pad;
Jan 21, 2010
Jan 21, 2010
96
Window xwindow;
May 24, 2010
May 24, 2010
98
99
#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
Picture xwindow_pict;
May 26, 2010
May 26, 2010
100
Picture pixmap_picts[3];
May 30, 2010
May 30, 2010
101
Picture drawable_pict;
May 24, 2010
May 24, 2010
102
103
104
105
106
XRenderPictFormat* xwindow_pict_fmt;
XRenderPictureAttributes xwindow_pict_attr;
unsigned int xwindow_pict_attr_valuemask;
SDL_bool xrender_available;
#endif
107
108
int current_pixmap;
Drawable drawable;
Jan 2, 2009
Jan 2, 2009
109
SDL_PixelFormat format;
110
111
112
113
114
115
116
117
118
119
GC gc;
SDL_DirtyRectList dirty;
SDL_bool makedirty;
} X11_RenderData;
typedef struct
{
SDL_SW_YUVTexture *yuv;
Uint32 format;
Pixmap pixmap;
May 24, 2010
May 24, 2010
120
121
#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
Picture picture;
May 26, 2010
May 26, 2010
122
123
124
XRenderPictFormat* picture_fmt;
XRenderPictureAttributes picture_attr;
unsigned int picture_attr_valuemask;
May 28, 2010
May 28, 2010
125
SDL_bool xrender_available;
May 24, 2010
May 24, 2010
126
#endif
127
128
129
130
131
XImage *image;
#ifndef NO_SHARED_MEMORY
/* MIT shared memory extension information */
XShmSegmentInfo shminfo;
#endif
Dec 3, 2008
Dec 3, 2008
132
XImage *scaling_image;
133
134
135
136
137
138
139
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
140
141
142
static int (*X_handler) (Display *, XErrorEvent *) = NULL;
static int
shm_errhandler(Display * d, XErrorEvent * e)
Dec 1, 2008
Dec 1, 2008
144
145
146
147
148
149
if (e->error_code == BadAccess) {
shm_error = True;
return (0);
} else {
return (X_handler(d, e));
}
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
}
#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);
}
void
X11_AddRenderDriver(_THIS)
{
SDL_RendererInfo *info = &X11_RenderDriver.info;
Jan 21, 2010
Jan 21, 2010
171
SDL_DisplayMode *mode = &SDL_CurrentDisplay->desktop_mode;
Dec 4, 2009
Dec 4, 2009
172
int i;
Dec 14, 2008
Dec 14, 2008
174
175
176
177
178
179
info->texture_formats[info->num_texture_formats++] = mode->format;
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;
Dec 4, 2009
Dec 4, 2009
181
182
183
for (i = 0; i < _this->num_displays; ++i) {
SDL_AddRenderDriver(&_this->displays[i], &X11_RenderDriver);
}
184
185
186
187
188
}
SDL_Renderer *
X11_CreateRenderer(SDL_Window * window, Uint32 flags)
{
Jan 21, 2010
Jan 21, 2010
189
SDL_VideoDisplay *display = window->display;
Dec 14, 2008
Dec 14, 2008
190
SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
SDL_Renderer *renderer;
X11_RenderData *data;
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;
Dec 14, 2008
Dec 14, 2008
213
214
data->visual = displaydata->visual;
data->depth = displaydata->depth;
Dec 25, 2008
Dec 25, 2008
215
data->scanline_pad = displaydata->scanline_pad;
Jan 21, 2010
Jan 21, 2010
216
data->xwindow = windowdata->xwindow;
May 24, 2010
May 24, 2010
217
218
219
220
221
#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
int event_basep, error_basep;
if(XRenderQueryExtension(data->display, &event_basep, &error_basep) == True) {
data->xrender_available = SDL_TRUE;
data->xwindow_pict_fmt = XRenderFindVisualFormat(data->display, data->visual);
May 31, 2010
May 31, 2010
222
if(!data->xwindow_pict_fmt) {
May 28, 2010
May 28, 2010
223
224
data->xrender_available = SDL_FALSE;
}
May 26, 2010
May 26, 2010
225
226
227
228
data->xwindow_pict_attr.graphics_exposures = False;
data->xwindow_pict_attr_valuemask = CPGraphicsExposure;
data->xwindow_pict = XRenderCreatePicture(data->display, data->xwindow, data->xwindow_pict_fmt,
data->xwindow_pict_attr_valuemask, &data->xwindow_pict_attr);
May 28, 2010
May 28, 2010
229
230
231
if(!data->xwindow_pict) {
data->xrender_available = SDL_FALSE;
}
May 24, 2010
May 24, 2010
232
233
234
235
236
}
else {
data->xrender_available = SDL_FALSE;
}
#endif
237
238
239
240
241
242
243
244
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;
Dec 20, 2008
Dec 20, 2008
245
renderer->SetDrawBlendMode = X11_SetDrawBlendMode;
Dec 23, 2009
Dec 23, 2009
246
247
248
249
renderer->RenderDrawPoints = X11_RenderDrawPoints;
renderer->RenderDrawLines = X11_RenderDrawLines;
renderer->RenderDrawRects = X11_RenderDrawRects;
renderer->RenderFillRects = X11_RenderFillRects;
250
renderer->RenderCopy = X11_RenderCopy;
Dec 14, 2009
Dec 14, 2009
251
252
renderer->RenderReadPixels = X11_RenderReadPixels;
renderer->RenderWritePixels = X11_RenderWritePixels;
253
254
255
256
renderer->RenderPresent = X11_RenderPresent;
renderer->DestroyTexture = X11_DestroyTexture;
renderer->DestroyRenderer = X11_DestroyRenderer;
renderer->info = X11_RenderDriver.info;
Jan 21, 2010
Jan 21, 2010
257
renderer->window = window;
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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;
}
for (i = 0; i < n; ++i) {
Dec 1, 2008
Dec 1, 2008
277
data->pixmaps[i] =
Jan 21, 2010
Jan 21, 2010
278
XCreatePixmap(data->display, data->xwindow, window->w, window->h,
Dec 14, 2008
Dec 14, 2008
279
displaydata->depth);
280
281
282
283
284
if (data->pixmaps[i] == None) {
X11_DestroyRenderer(renderer);
SDL_SetError("XCreatePixmap() failed");
return NULL;
}
May 28, 2010
May 28, 2010
285
286
287
#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
if(data->xrender_available == SDL_TRUE) {
data->pixmap_picts[i] =
May 31, 2010
May 31, 2010
288
289
XRenderCreatePicture(data->display, data->pixmaps[i], data->xwindow_pict_fmt,
data->xwindow_pict_attr_valuemask, &data->xwindow_pict_attr);
May 28, 2010
May 28, 2010
290
291
292
293
294
if(!data->pixmap_picts[i]) {
data->xrender_available = SDL_FALSE;
}
}
#endif
295
296
297
}
if (n > 0) {
data->drawable = data->pixmaps[0];
May 26, 2010
May 26, 2010
298
299
#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
if(data->xrender_available == SDL_TRUE)
May 30, 2010
May 30, 2010
300
data->drawable_pict = data->pixmap_picts[0];
May 26, 2010
May 26, 2010
301
#endif
302
303
data->makedirty = SDL_TRUE;
} else {
Jan 21, 2010
Jan 21, 2010
304
data->drawable = data->xwindow;
May 26, 2010
May 26, 2010
305
306
#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
if(data->xrender_available == SDL_TRUE)
May 30, 2010
May 30, 2010
307
data->drawable_pict = data->xwindow_pict;
May 26, 2010
May 26, 2010
308
#endif
309
310
311
312
313
data->makedirty = SDL_FALSE;
}
data->current_pixmap = 0;
/* Get the format of the window */
Dec 16, 2008
Dec 16, 2008
314
315
316
if (!SDL_PixelFormatEnumToMasks
(display->current_mode.format, &bpp, &Rmask, &Gmask, &Bmask,
&Amask)) {
Dec 14, 2008
Dec 14, 2008
317
318
319
SDL_SetError("Unknown display format");
X11_DestroyRenderer(renderer);
return NULL;
Jan 2, 2009
Jan 2, 2009
321
SDL_InitFormat(&data->format, bpp, Rmask, Gmask, Bmask, Amask);
322
323
324
/* Create the drawing context */
gcv.graphics_exposures = False;
Dec 1, 2008
Dec 1, 2008
325
data->gc =
Jan 21, 2010
Jan 21, 2010
326
XCreateGC(data->display, data->xwindow, GCGraphicsExposures, &gcv);
327
328
329
330
331
332
333
334
335
336
337
338
339
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;
Jan 21, 2010
Jan 21, 2010
340
SDL_Window *window = renderer->window;
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
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;
May 26, 2010
May 26, 2010
356
#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
May 31, 2010
May 31, 2010
357
data->pixmap_picts[i] = None;
May 26, 2010
May 26, 2010
358
#endif
359
360
361
}
}
for (i = 0; i < n; ++i) {
Dec 1, 2008
Dec 1, 2008
362
data->pixmaps[i] =
Jan 21, 2010
Jan 21, 2010
363
XCreatePixmap(data->display, data->xwindow, window->w, window->h,
Dec 14, 2008
Dec 14, 2008
364
data->depth);
365
366
367
368
if (data->pixmaps[i] == None) {
SDL_SetError("XCreatePixmap() failed");
return -1;
}
May 26, 2010
May 26, 2010
369
#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
May 28, 2010
May 28, 2010
370
if(data->xrender_available == SDL_TRUE) {
May 31, 2010
May 31, 2010
371
372
data->pixmap_picts[i] =
XRenderCreatePicture(data->display, data->pixmaps[i], data->xwindow_pict_fmt,
May 28, 2010
May 28, 2010
373
data->xwindow_pict_attr_valuemask, &data->xwindow_pict_attr);
May 31, 2010
May 31, 2010
374
if(!data->pixmap_picts[i]) {
May 28, 2010
May 28, 2010
375
376
377
data->xrender_available = SDL_FALSE;
}
}
May 26, 2010
May 26, 2010
378
#endif
379
380
381
}
if (n > 0) {
data->drawable = data->pixmaps[0];
May 26, 2010
May 26, 2010
382
#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
May 31, 2010
May 31, 2010
383
data->drawable_pict = data->pixmap_picts[0];
May 26, 2010
May 26, 2010
384
#endif
385
386
387
388
389
390
391
392
393
394
}
data->current_pixmap = 0;
return 0;
}
static int
X11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
X11_RenderData *renderdata = (X11_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
395
396
SDL_Window *window = renderer->window;
SDL_VideoDisplay *display = window->display;
397
X11_TextureData *data;
Dec 25, 2008
Dec 25, 2008
398
int pitch_alignmask = ((renderdata->scanline_pad / 8) - 1);
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
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 {
May 28, 2010
May 28, 2010
415
416
417
418
/* If Xrender support is builtin we only need to check whether
Xrender is available at runtime. If it is available there
can be no BadMatch error since Xrender takes care of that.
*/
May 26, 2010
May 26, 2010
419
#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
May 28, 2010
May 28, 2010
420
// Assume the texture is supported by Xrender
May 28, 2010
May 28, 2010
421
data->xrender_available = SDL_TRUE;
May 31, 2010
May 31, 2010
422
if(renderdata->xrender_available == SDL_FALSE) {
May 26, 2010
May 26, 2010
423
424
425
426
427
428
if (texture->format != display->current_mode.format) {
SDL_SetError("Texture format doesn't match window format");
return -1;
}
}
#else
May 28, 2010
May 28, 2010
429
430
431
432
/* The image/pixmap depth must be the same as the window or you
get a BadMatch error when trying to putimage or copyarea.
This BadMatch error
*/
Dec 14, 2008
Dec 14, 2008
433
434
435
436
if (texture->format != display->current_mode.format) {
SDL_SetError("Texture format doesn't match window format");
return -1;
}
May 26, 2010
May 26, 2010
437
#endif
438
439
data->format = texture->format;
}
Dec 1, 2008
Dec 1, 2008
440
data->pitch = texture->w * SDL_BYTESPERPIXEL(data->format);
Dec 25, 2008
Dec 25, 2008
441
data->pitch = (data->pitch + pitch_alignmask) & ~pitch_alignmask;
442
443
444
445
446
447
448
449
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
450
451
452
shminfo->shmid =
shmget(IPC_PRIVATE, texture->h * data->pitch,
IPC_CREAT | 0777);
453
if (shminfo->shmid >= 0) {
Dec 1, 2008
Dec 1, 2008
454
shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
455
shminfo->readOnly = False;
Dec 1, 2008
Dec 1, 2008
456
if (shminfo->shmaddr != (char *) -1) {
457
458
459
shm_error = False;
X_handler = XSetErrorHandler(shm_errhandler);
XShmAttach(renderdata->display, shminfo);
Aug 20, 2009
Aug 20, 2009
460
XSync(renderdata->display, False);
461
462
463
464
465
466
467
468
469
470
471
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
472
data->image =
Dec 16, 2008
Dec 16, 2008
473
474
475
XShmCreateImage(renderdata->display, renderdata->visual,
renderdata->depth, ZPixmap, shminfo->shmaddr,
shminfo, texture->w, texture->h);
Dec 1, 2008
Dec 1, 2008
476
if (!data->image) {
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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
491
X11_DestroyTexture(renderer, texture);
492
493
494
495
SDL_OutOfMemory();
return -1;
}
Dec 1, 2008
Dec 1, 2008
496
data->image =
Dec 16, 2008
Dec 16, 2008
497
498
499
500
501
XCreateImage(renderdata->display, renderdata->visual,
renderdata->depth, ZPixmap, 0, data->pixels,
texture->w, texture->h,
SDL_BYTESPERPIXEL(data->format) * 8,
data->pitch);
Dec 13, 2008
Dec 13, 2008
503
X11_DestroyTexture(renderer, texture);
504
505
506
507
508
SDL_SetError("XCreateImage() failed");
return -1;
}
}
} else {
Dec 1, 2008
Dec 1, 2008
509
data->pixmap =
Jan 21, 2010
Jan 21, 2010
510
XCreatePixmap(renderdata->display, renderdata->xwindow, texture->w,
Dec 14, 2008
Dec 14, 2008
511
texture->h, renderdata->depth);
512
if (data->pixmap == None) {
Dec 13, 2008
Dec 13, 2008
513
X11_DestroyTexture(renderer, texture);
Jan 15, 2009
Jan 15, 2009
514
SDL_SetError("XCreatePixmap() failed");
May 26, 2010
May 26, 2010
517
518
#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
May 28, 2010
May 28, 2010
519
520
521
522
523
524
525
if(renderdata->xrender_available) {
data->xrender_available = SDL_TRUE;
unsigned long x11_fmt_mask; // Format mask
XRenderPictFormat x11_templ_fmt; // Format template
x11_fmt_mask =
(PictFormatDepth | PictFormatRedMask | PictFormatGreenMask
| PictFormatBlueMask);
May 31, 2010
May 31, 2010
526
527
528
529
530
531
532
533
Uint32 Rmask, Gmask, Bmask, Amask;
int bpp;
SDL_PixelFormatEnumToMasks(data->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
x11_templ_fmt.depth = bpp;
x11_templ_fmt.direct.redMask = Rmask;
x11_templ_fmt.direct.greenMask = Gmask;
x11_templ_fmt.direct.blueMask = Bmask;
x11_templ_fmt.direct.alphaMask = Amask;
May 28, 2010
May 28, 2010
534
/* Return one matching XRenderPictFormat */
May 31, 2010
May 31, 2010
535
data->picture_fmt =
May 28, 2010
May 28, 2010
536
XRenderFindFormat(renderdata->display, x11_fmt_mask, &x11_templ_fmt, 1);
May 31, 2010
May 31, 2010
537
if(!data->picture_fmt) {
May 28, 2010
May 28, 2010
538
539
540
541
542
data->xrender_available = SDL_FALSE;
}
data->picture_attr_valuemask = CPGraphicsExposure;
(data->picture_attr).graphics_exposures = False;
data->picture =
May 31, 2010
May 31, 2010
543
XRenderCreatePicture(renderdata->display, data->pixmap, data->picture_fmt,
May 28, 2010
May 28, 2010
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
data->picture_attr_valuemask, &(data->picture_attr));
if(!data->picture) {
data->xrender_available = SDL_FALSE;
}
}
/* We thought we could render the texture with Xrender but this was
not possible for some reason. Now we must ensure that texture
format and window format match to avoid a BadMatch error.
*/
if(data->xrender_available == SDL_FALSE) {
if (texture->format != display->current_mode.format) {
SDL_SetError("Texture format doesn't match window format");
return -1;
}
}
#endif
Dec 1, 2008
Dec 1, 2008
560
data->image =
Dec 16, 2008
Dec 16, 2008
561
562
563
564
XCreateImage(renderdata->display, renderdata->visual,
renderdata->depth, ZPixmap, 0, NULL, texture->w,
texture->h, SDL_BYTESPERPIXEL(data->format) * 8,
data->pitch);
Dec 13, 2008
Dec 13, 2008
566
X11_DestroyTexture(renderer, texture);
567
568
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
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) {
Dec 20, 2008
Dec 20, 2008
594
case SDL_BLENDMODE_NONE:
595
596
597
return 0;
default:
SDL_Unsupported();
Dec 20, 2008
Dec 20, 2008
598
texture->blendMode = SDL_BLENDMODE_NONE;
599
600
601
602
603
604
605
return -1;
}
}
static int
X11_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
{
Dec 3, 2008
Dec 3, 2008
606
607
X11_TextureData *data = (X11_TextureData *) texture->driverdata;
608
609
610
switch (texture->scaleMode) {
case SDL_TEXTURESCALEMODE_NONE:
return 0;
Dec 3, 2008
Dec 3, 2008
611
612
613
614
615
616
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 */
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
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
658
data->image->data = (char *) pixels;
659
data->image->bytes_per_line = pitch;
Dec 1, 2008
Dec 1, 2008
660
661
XPutImage(renderdata->display, data->pixmap, renderdata->gc,
data->image, 0, 0, rect->x, rect->y, rect->w, rect->h);
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
}
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);
}
}
Dec 20, 2008
Dec 20, 2008
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
static int
X11_SetDrawBlendMode(SDL_Renderer * renderer)
{
switch (renderer->blendMode) {
case SDL_BLENDMODE_NONE:
return 0;
default:
SDL_Unsupported();
renderer->blendMode = SDL_BLENDMODE_NONE;
return -1;
}
}
static Uint32
renderdrawcolor(SDL_Renderer * renderer, int premult)
{
X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
Uint8 r = renderer->r;
Uint8 g = renderer->g;
Uint8 b = renderer->b;
Uint8 a = renderer->a;
if (premult)
Jan 2, 2009
Jan 2, 2009
722
return SDL_MapRGBA(&data->format, ((int) r * (int) a) / 255,
Dec 20, 2008
Dec 20, 2008
723
724
725
((int) g * (int) a) / 255,
((int) b * (int) a) / 255, 255);
else
Jan 2, 2009
Jan 2, 2009
726
return SDL_MapRGBA(&data->format, r, g, b, a);
Dec 20, 2008
Dec 20, 2008
727
728
}
Dec 21, 2008
Dec 21, 2008
729
static int
Dec 23, 2009
Dec 23, 2009
730
731
X11_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
int count)
Dec 21, 2008
Dec 21, 2008
732
733
{
X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
734
SDL_Window *window = renderer->window;
Dec 21, 2008
Dec 21, 2008
735
unsigned long foreground;
Dec 10, 2009
Dec 10, 2009
736
737
XPoint *xpoints, *xpoint;
int i, xcount;
Dec 21, 2008
Dec 21, 2008
738
739
740
741
if (data->makedirty) {
SDL_Rect rect;
Dec 10, 2009
Dec 10, 2009
742
743
744
745
746
747
748
749
750
/* Get the smallest rectangle that contains everything */
rect.x = 0;
rect.y = 0;
rect.w = window->w;
rect.h = window->h;
if (!SDL_EnclosePoints(points, count, &rect, &rect)) {
/* Nothing to draw */
return 0;
}
Dec 21, 2008
Dec 21, 2008
751
752
753
754
755
SDL_AddDirtyRect(&data->dirty, &rect);
}
foreground = renderdrawcolor(renderer, 1);
XSetForeground(data->display, data->gc, foreground);
Dec 10, 2009
Dec 10, 2009
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
xpoint = xpoints = SDL_stack_alloc(XPoint, count);
xcount = 0;
for (i = 0; i < count; ++i) {
int x = points[i].x;
int y = points[i].y;
if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
continue;
}
xpoint->x = (short)x;
xpoint->y = (short)y;
++xpoint;
++xcount;
}
if (xcount > 0) {
XDrawPoints(data->display, data->drawable, data->gc, xpoints, xcount,
CoordModeOrigin);
}
SDL_stack_free(xpoints);
Dec 21, 2008
Dec 21, 2008
776
777
778
return 0;
}
Dec 20, 2008
Dec 20, 2008
779
static int
Dec 23, 2009
Dec 23, 2009
780
781
X11_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
int count)
Dec 20, 2008
Dec 20, 2008
782
783
{
X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
784
SDL_Window *window = renderer->window;
May 9, 2010
May 9, 2010
785
SDL_Rect clip;
Dec 20, 2008
Dec 20, 2008
786
unsigned long foreground;
Dec 11, 2009
Dec 11, 2009
787
788
789
790
XPoint *xpoints, *xpoint;
int i, xcount;
int minx, miny;
int maxx, maxy;
Dec 20, 2008
Dec 20, 2008
791
Dec 10, 2009
Dec 10, 2009
792
793
794
795
clip.x = 0;
clip.y = 0;
clip.w = window->w;
clip.h = window->h;
Dec 20, 2008
Dec 20, 2008
796
Dec 11, 2009
Dec 11, 2009
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
839
840
841
842
843
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
foreground = renderdrawcolor(renderer, 1);
XSetForeground(data->display, data->gc, foreground);
xpoint = xpoints = SDL_stack_alloc(XPoint, count);
xcount = 0;
minx = INT_MAX;
miny = INT_MAX;
maxx = INT_MIN;
maxy = INT_MIN;
for (i = 0; i < count; ++i) {
int x = points[i].x;
int y = points[i].y;
/* If the point is inside the window, add it to the list */
if (x >= 0 && x < window->w && y >= 0 && y < window->h) {
if (x < minx) {
minx = x;
} else if (x > maxx) {
maxx = x;
}
if (y < miny) {
miny = y;
} else if (y > maxy) {
maxy = y;
}
xpoint->x = (short)x;
xpoint->y = (short)y;
++xpoint;
++xcount;
continue;
}
/* We need to clip the line segments joined by this point */
if (xcount > 0) {
int x1 = xpoint[-1].x;
int y1 = xpoint[-1].y;
int x2 = x;
int y2 = y;
if (SDL_IntersectRectAndLine(&clip, &x1, &y1, &x2, &y2)) {
if (x2 < minx) {
minx = x2;
} else if (x2 > maxx) {
maxx = x2;
}
if (y2 < miny) {
miny = y2;
} else if (y2 > maxy) {
maxy = y2;
}
xpoint->x = (short)x2;
xpoint->y = (short)y2;
++xpoint;
++xcount;
}
XDrawLines(data->display, data->drawable, data->gc,
xpoints, xcount, CoordModeOrigin);
if (xpoints[0].x != x2 || xpoints[0].y != y2) {
XDrawPoint(data->display, data->drawable, data->gc, x2, y2);
}
if (data->makedirty) {
SDL_Rect rect;
rect.x = minx;
rect.y = miny;
rect.w = (maxx - minx) + 1;
rect.h = (maxy - miny) + 1;
SDL_AddDirtyRect(&data->dirty, &rect);
}
xpoint = xpoints;
xcount = 0;
minx = INT_MAX;
miny = INT_MAX;
maxx = INT_MIN;
maxy = INT_MIN;
}
if (i < (count-1)) {
int x1 = x;
int y1 = y;
int x2 = points[i+1].x;
int y2 = points[i+1].y;
if (SDL_IntersectRectAndLine(&clip, &x1, &y1, &x2, &y2)) {
if (x1 < minx) {
minx = x1;
} else if (x1 > maxx) {
maxx = x1;
}
if (y1 < miny) {
miny = y1;
} else if (y1 > maxy) {
maxy = y1;
}
xpoint->x = (short)x1;
xpoint->y = (short)y1;
++xpoint;
++xcount;
}
Dec 20, 2008
Dec 20, 2008
893
894
}
}
Dec 11, 2009
Dec 11, 2009
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
if (xcount > 1) {
int x2 = xpoint[-1].x;
int y2 = xpoint[-1].y;
XDrawLines(data->display, data->drawable, data->gc, xpoints, xcount,
CoordModeOrigin);
if (xpoints[0].x != x2 || xpoints[0].y != y2) {
XDrawPoint(data->display, data->drawable, data->gc, x2, y2);
}
if (data->makedirty) {
SDL_Rect rect;
rect.x = minx;
rect.y = miny;
rect.w = (maxx - minx) + 1;
rect.h = (maxy - miny) + 1;
SDL_AddDirtyRect(&data->dirty, &rect);
}
}
SDL_stack_free(xpoints);
Dec 20, 2008
Dec 20, 2008
914
Dec 20, 2008
Dec 20, 2008
915
return 0;
Dec 20, 2008
Dec 20, 2008
916
917
918
}
static int
Dec 23, 2009
Dec 23, 2009
919
920
921
X11_RenderDrawRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
{
X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
922
SDL_Window *window = renderer->window;
Dec 23, 2009
Dec 23, 2009
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
SDL_Rect clip, rect;
unsigned long foreground;
XRectangle *xrects, *xrect;
int i, xcount;
clip.x = 0;
clip.y = 0;
clip.w = window->w;
clip.h = window->h;
foreground = renderdrawcolor(renderer, 1);
XSetForeground(data->display, data->gc, foreground);
xrect = xrects = SDL_stack_alloc(XRectangle, count);
xcount = 0;
for (i = 0; i < count; ++i) {
if (!SDL_IntersectRect(rects[i], &clip, &rect)) {
continue;
}
xrect->x = (short)rect.x;
xrect->y = (short)rect.y;
xrect->width = (unsigned short)rect.w;
xrect->height = (unsigned short)rect.h;
++xrect;
++xcount;
if (data->makedirty) {
SDL_AddDirtyRect(&data->dirty, &rect);
}
}
if (xcount > 0) {
XDrawRectangles(data->display, data->drawable, data->gc,
xrects, xcount);
}
SDL_stack_free(xpoints);
return 0;
}
static int
X11_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
965
966
{
X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
967
SDL_Window *window = renderer->window;
Dec 10, 2009
Dec 10, 2009
968
SDL_Rect clip, rect;
969
unsigned long foreground;
Dec 10, 2009
Dec 10, 2009
970
971
XRectangle *xrects, *xrect;
int i, xcount;
Dec 10, 2009
Dec 10, 2009
973
974
975
976
clip.x = 0;
clip.y = 0;
clip.w = window->w;
clip.h = window->h;
Dec 20, 2008
Dec 20, 2008
978
foreground = renderdrawcolor(renderer, 1);
979
XSetForeground(data->display, data->gc, foreground);
Dec 10, 2009
Dec 10, 2009
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
xrect = xrects = SDL_stack_alloc(XRectangle, count);
xcount = 0;
for (i = 0; i < count; ++i) {
if (!SDL_IntersectRect(rects[i], &clip, &rect)) {
continue;
}
xrect->x = (short)rect.x;
xrect->y = (short)rect.y;
xrect->width = (unsigned short)rect.w;
xrect->height = (unsigned short)rect.h;
++xrect;
++xcount;
if (data->makedirty) {
SDL_AddDirtyRect(&data->dirty, &rect);
}
}
if (xcount > 0) {
May 30, 2010
May 30, 2010
1000
#ifdef SDL_VIDEO_DRIVER_X11_XRENDER