This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_x11render.c
1148 lines (1032 loc) · 35.8 KB
1
2
/*
SDL - Simple DirectMedia Layer
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
26
27
#include <limits.h> /* For INT_MIN and INT_MAX */
28
#include "SDL_x11video.h"
29
#include "SDL_x11render.h"
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#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);
53
static int X11_SetDrawBlendMode(SDL_Renderer * renderer);
54
55
56
57
58
59
60
61
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);
62
63
static int X11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
64
65
66
67
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);
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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,
82
SDL_BLENDMODE_NONE,
83
84
85
86
87
88
89
90
91
92
93
SDL_TEXTURESCALEMODE_NONE,
0,
{0},
0,
0}
};
typedef struct
{
Display *display;
int screen;
94
95
Visual *visual;
int depth;
96
int scanline_pad;
97
Window xwindow;
98
99
100
Pixmap pixmaps[3];
int current_pixmap;
Drawable drawable;
101
SDL_PixelFormat format;
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
117
XImage *scaling_image;
118
119
120
121
122
123
124
void *pixels;
int pitch;
} X11_TextureData;
#ifndef NO_SHARED_MEMORY
/* Shared memory error handler routine */
static int shm_error;
125
126
127
static int (*X_handler) (Display *, XErrorEvent *) = NULL;
static int
shm_errhandler(Display * d, XErrorEvent * e)
128
{
129
130
131
132
133
134
if (e->error_code == BadAccess) {
shm_error = True;
return (0);
} else {
return (X_handler(d, e));
}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
}
#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;
156
SDL_DisplayMode *mode = &SDL_CurrentDisplay->desktop_mode;
157
int i;
158
159
160
161
162
163
164
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;
165
166
167
168
for (i = 0; i < _this->num_displays; ++i) {
SDL_AddRenderDriver(&_this->displays[i], &X11_RenderDriver);
}
169
170
171
172
173
}
SDL_Renderer *
X11_CreateRenderer(SDL_Window * window, Uint32 flags)
{
174
SDL_VideoDisplay *display = window->display;
175
SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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;
198
199
data->visual = displaydata->visual;
data->depth = displaydata->depth;
200
data->scanline_pad = displaydata->scanline_pad;
201
data->xwindow = windowdata->xwindow;
202
203
204
205
206
207
208
209
210
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;
211
renderer->SetDrawBlendMode = X11_SetDrawBlendMode;
212
213
214
215
renderer->RenderDrawPoints = X11_RenderDrawPoints;
renderer->RenderDrawLines = X11_RenderDrawLines;
renderer->RenderDrawRects = X11_RenderDrawRects;
renderer->RenderFillRects = X11_RenderFillRects;
216
renderer->RenderCopy = X11_RenderCopy;
217
218
renderer->RenderReadPixels = X11_RenderReadPixels;
renderer->RenderWritePixels = X11_RenderWritePixels;
219
220
221
222
renderer->RenderPresent = X11_RenderPresent;
renderer->DestroyTexture = X11_DestroyTexture;
renderer->DestroyRenderer = X11_DestroyRenderer;
renderer->info = X11_RenderDriver.info;
223
renderer->window = window;
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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) {
243
data->pixmaps[i] =
244
XCreatePixmap(data->display, data->xwindow, window->w, window->h,
245
displaydata->depth);
246
247
248
249
250
251
252
253
254
255
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 {
256
data->drawable = data->xwindow;
257
258
259
260
261
data->makedirty = SDL_FALSE;
}
data->current_pixmap = 0;
/* Get the format of the window */
262
263
264
if (!SDL_PixelFormatEnumToMasks
(display->current_mode.format, &bpp, &Rmask, &Gmask, &Bmask,
&Amask)) {
265
266
267
SDL_SetError("Unknown display format");
X11_DestroyRenderer(renderer);
return NULL;
268
}
269
SDL_InitFormat(&data->format, bpp, Rmask, Gmask, Bmask, Amask);
270
271
272
/* Create the drawing context */
gcv.graphics_exposures = False;
273
data->gc =
274
XCreateGC(data->display, data->xwindow, GCGraphicsExposures, &gcv);
275
276
277
278
279
280
281
282
283
284
285
286
287
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;
288
SDL_Window *window = renderer->window;
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
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;
}
}
for (i = 0; i < n; ++i) {
307
data->pixmaps[i] =
308
XCreatePixmap(data->display, data->xwindow, window->w, window->h,
309
data->depth);
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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;
327
328
SDL_Window *window = renderer->window;
SDL_VideoDisplay *display = window->display;
329
X11_TextureData *data;
330
int pitch_alignmask = ((renderdata->scanline_pad / 8) - 1);
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
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 {
348
349
/* The image/pixmap depth must be the same as the window or you
get a BadMatch error when trying to putimage or copyarea.
350
*/
351
352
353
354
if (texture->format != display->current_mode.format) {
SDL_SetError("Texture format doesn't match window format");
return -1;
}
355
356
data->format = texture->format;
}
357
data->pitch = texture->w * SDL_BYTESPERPIXEL(data->format);
358
data->pitch = (data->pitch + pitch_alignmask) & ~pitch_alignmask;
359
360
361
362
363
364
365
366
if (data->yuv || texture->access == SDL_TEXTUREACCESS_STREAMING) {
#ifndef NO_SHARED_MEMORY
XShmSegmentInfo *shminfo = &data->shminfo;
shm_error = True;
if (SDL_X11_HAVE_SHM) {
367
368
369
shminfo->shmid =
shmget(IPC_PRIVATE, texture->h * data->pitch,
IPC_CREAT | 0777);
370
if (shminfo->shmid >= 0) {
371
shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
372
shminfo->readOnly = False;
373
if (shminfo->shmaddr != (char *) -1) {
374
375
376
shm_error = False;
X_handler = XSetErrorHandler(shm_errhandler);
XShmAttach(renderdata->display, shminfo);
377
XSync(renderdata->display, False);
378
379
380
381
382
383
384
385
386
387
388
XSetErrorHandler(X_handler);
if (shm_error) {
shmdt(shminfo->shmaddr);
}
}
shmctl(shminfo->shmid, IPC_RMID, NULL);
}
}
if (!shm_error) {
data->pixels = shminfo->shmaddr;
389
data->image =
390
391
392
XShmCreateImage(renderdata->display, renderdata->visual,
renderdata->depth, ZPixmap, shminfo->shmaddr,
shminfo, texture->w, texture->h);
393
if (!data->image) {
394
395
396
397
398
399
400
401
402
403
404
405
406
407
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) {
408
X11_DestroyTexture(renderer, texture);
409
410
411
412
SDL_OutOfMemory();
return -1;
}
413
data->image =
414
415
416
417
418
XCreateImage(renderdata->display, renderdata->visual,
renderdata->depth, ZPixmap, 0, data->pixels,
texture->w, texture->h,
SDL_BYTESPERPIXEL(data->format) * 8,
data->pitch);
419
if (!data->image) {
420
X11_DestroyTexture(renderer, texture);
421
422
423
424
425
SDL_SetError("XCreateImage() failed");
return -1;
}
}
} else {
426
data->pixmap =
427
XCreatePixmap(renderdata->display, renderdata->xwindow, texture->w,
428
texture->h, renderdata->depth);
429
if (data->pixmap == None) {
430
X11_DestroyTexture(renderer, texture);
431
SDL_SetError("XCreatePixmap() failed");
432
433
434
return -1;
}
435
data->image =
436
437
438
439
XCreateImage(renderdata->display, renderdata->visual,
renderdata->depth, ZPixmap, 0, NULL, texture->w,
texture->h, SDL_BYTESPERPIXEL(data->format) * 8,
data->pitch);
440
if (!data->image) {
441
X11_DestroyTexture(renderer, texture);
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
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) {
469
case SDL_BLENDMODE_NONE:
470
471
472
return 0;
default:
SDL_Unsupported();
473
texture->blendMode = SDL_BLENDMODE_NONE;
474
475
476
477
478
479
480
return -1;
}
}
static int
X11_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
{
481
482
X11_TextureData *data = (X11_TextureData *) texture->driverdata;
483
484
485
switch (texture->scaleMode) {
case SDL_TEXTURESCALEMODE_NONE:
return 0;
486
487
488
489
490
491
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 */
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
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;
533
data->image->data = (char *) pixels;
534
data->image->bytes_per_line = pitch;
535
536
XPutImage(renderdata->display, data->pixmap, renderdata->gc,
data->image, 0, 0, rect->x, rect->y, rect->w, rect->h);
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
}
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);
}
}
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
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)
597
return SDL_MapRGBA(&data->format, ((int) r * (int) a) / 255,
598
599
600
((int) g * (int) a) / 255,
((int) b * (int) a) / 255, 255);
else
601
return SDL_MapRGBA(&data->format, r, g, b, a);
602
603
}
604
static int
605
606
X11_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
int count)
607
608
{
X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
609
SDL_Window *window = renderer->window;
610
unsigned long foreground;
611
612
XPoint *xpoints, *xpoint;
int i, xcount;
613
614
615
616
if (data->makedirty) {
SDL_Rect rect;
617
618
619
620
621
622
623
624
625
/* 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;
}
626
627
628
629
630
SDL_AddDirtyRect(&data->dirty, &rect);
}
foreground = renderdrawcolor(renderer, 1);
XSetForeground(data->display, data->gc, foreground);
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
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);
651
652
653
return 0;
}
654
static int
655
656
X11_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
int count)
657
658
{
X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
659
SDL_Window *window = renderer->window;
660
SDL_Rect clip;
661
unsigned long foreground;
662
663
664
665
XPoint *xpoints, *xpoint;
int i, xcount;
int minx, miny;
int maxx, maxy;
666
667
668
669
670
clip.x = 0;
clip.y = 0;
clip.w = window->w;
clip.h = window->h;
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
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
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;
}
768
769
}
}
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
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);
789
790
return 0;
791
792
793
}
static int
794
795
796
X11_RenderDrawRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
{
X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
797
SDL_Window *window = renderer->window;
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
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)
840
841
{
X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
842
SDL_Window *window = renderer->window;
843
SDL_Rect clip, rect;
844
unsigned long foreground;
845
846
XRectangle *xrects, *xrect;
int i, xcount;
847
848
849
850
851
clip.x = 0;
clip.y = 0;
clip.w = window->w;
clip.h = window->h;
852
853
foreground = renderdrawcolor(renderer, 1);
854
XSetForeground(data->display, data->gc, foreground);
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
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) {
XFillRectangles(data->display, data->drawable, data->gc,
xrects, xcount);
}
SDL_stack_free(xpoints);
880
881
882
883
884
885
886
887
888
889
890
891
892
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);
}
893
if (srcrect->w == dstrect->w && srcrect->h == dstrect->h) {
894
#ifndef NO_SHARED_MEMORY
895
896
897
898
899
900
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
901
#endif
902
903
904
905
906
907
908
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,
909
dstrect->h, dstrect->x, dstrect->y);
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
}
} 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) {
void *pixels;
int pitch;
pitch = dstrect->w * SDL_BYTESPERPIXEL(texturedata->format);
pixels = SDL_malloc(dstrect->h * pitch);
if (!pixels) {
SDL_OutOfMemory();
return -1;
}
image =
930
931
XCreateImage(data->display, data->visual, data->depth,
ZPixmap, 0, pixels, dstrect->w, dstrect->h,
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
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() */
956
SDL_zero(src);
957
958
959
960
961
962
963
964
965
966
967
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;
968
SDL_zero(dst);
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
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);
986
} else {
987
988
989
XCopyArea(data->display, texturedata->pixmap, data->drawable,
data->gc, srcrect->x, srcrect->y, dstrect->w, dstrect->h,
srcrect->x, srcrect->y);
990
991
992
993
}
return 0;
}
994
995
996
997
998
static int
X11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 format, void * pixels, int pitch)
{
X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
999
1000
SDL_Window *window = renderer->window;
SDL_VideoDisplay *display = window->display;