Skip to content

Latest commit

 

History

History
3304 lines (2854 loc) · 98.8 KB

SDL_render.c

File metadata and controls

3304 lines (2854 loc) · 98.8 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 3, 2018
Jan 3, 2018
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../SDL_internal.h"
/* The SDL 2D rendering system */
#include "SDL_assert.h"
#include "SDL_hints.h"
#include "SDL_log.h"
#include "SDL_render.h"
#include "SDL_sysrender.h"
#include "software/SDL_render_sw_c.h"
#define SDL_WINDOWRENDERDATA "_SDL_WindowRenderData"
#define CHECK_RENDERER_MAGIC(renderer, retval) \
Aug 14, 2017
Aug 14, 2017
36
SDL_assert(renderer && renderer->magic == &renderer_magic); \
37
38
39
40
41
42
if (!renderer || renderer->magic != &renderer_magic) { \
SDL_SetError("Invalid renderer"); \
return retval; \
}
#define CHECK_TEXTURE_MAGIC(texture, retval) \
Aug 14, 2017
Aug 14, 2017
43
SDL_assert(texture && texture->magic == &texture_magic); \
44
45
46
47
48
if (!texture || texture->magic != &texture_magic) { \
SDL_SetError("Invalid texture"); \
return retval; \
}
Aug 14, 2017
Aug 14, 2017
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
/* Predefined blend modes */
#define SDL_COMPOSE_BLENDMODE(srcColorFactor, dstColorFactor, colorOperation, \
srcAlphaFactor, dstAlphaFactor, alphaOperation) \
(SDL_BlendMode)(((Uint32)colorOperation << 0) | \
((Uint32)srcColorFactor << 4) | \
((Uint32)dstColorFactor << 8) | \
((Uint32)alphaOperation << 16) | \
((Uint32)srcAlphaFactor << 20) | \
((Uint32)dstAlphaFactor << 24))
#define SDL_BLENDMODE_NONE_FULL \
SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ZERO, SDL_BLENDOPERATION_ADD, \
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ZERO, SDL_BLENDOPERATION_ADD)
#define SDL_BLENDMODE_BLEND_FULL \
SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, \
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD)
#define SDL_BLENDMODE_ADD_FULL \
SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD, \
SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
#define SDL_BLENDMODE_MOD_FULL \
SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_SRC_COLOR, SDL_BLENDOPERATION_ADD, \
SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#if !SDL_RENDER_DISABLED
static const SDL_RenderDriver *render_drivers[] = {
#if SDL_VIDEO_RENDER_D3D
&D3D_RenderDriver,
#endif
#if SDL_VIDEO_RENDER_D3D11
&D3D11_RenderDriver,
#endif
#if SDL_VIDEO_RENDER_OGL
&GL_RenderDriver,
#endif
#if SDL_VIDEO_RENDER_OGL_ES2
&GLES2_RenderDriver,
#endif
#if SDL_VIDEO_RENDER_OGL_ES
&GLES_RenderDriver,
#endif
#if SDL_VIDEO_RENDER_DIRECTFB
&DirectFB_RenderDriver,
#endif
Apr 21, 2016
Apr 21, 2016
95
96
97
#if SDL_VIDEO_RENDER_METAL
&METAL_RenderDriver,
#endif
98
99
100
101
102
103
104
105
106
107
#if SDL_VIDEO_RENDER_PSP
&PSP_RenderDriver,
#endif
&SW_RenderDriver
};
#endif /* !SDL_RENDER_DISABLED */
static char renderer_magic;
static char texture_magic;
Sep 24, 2018
Sep 24, 2018
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
static SDL_INLINE void
DebugLogRenderCommands(const SDL_RenderCommand *cmd)
{
#if 0
unsigned int i = 1;
SDL_Log("Render commands to flush:");
while (cmd) {
switch (cmd->command) {
case SDL_RENDERCMD_NO_OP:
SDL_Log(" %u. no-op", i++);
break;
case SDL_RENDERCMD_SETVIEWPORT:
SDL_Log(" %u. set viewport (first=%u, rect={(%d, %d), %dx%d})", i++,
(unsigned int) cmd->data.viewport.first,
cmd->data.viewport.rect.x, cmd->data.viewport.rect.y,
cmd->data.viewport.rect.w, cmd->data.viewport.rect.h);
break;
case SDL_RENDERCMD_SETCLIPRECT:
SDL_Log(" %u. set cliprect (enabled=%s, rect={(%d, %d), %dx%d})", i++,
cmd->data.cliprect.enabled ? "true" : "false",
cmd->data.cliprect.rect.x, cmd->data.cliprect.rect.y,
cmd->data.cliprect.rect.w, cmd->data.cliprect.rect.h);
break;
case SDL_RENDERCMD_SETDRAWCOLOR:
SDL_Log(" %u. set draw color (first=%u, r=%d, g=%d, b=%d, a=%d)", i++,
(unsigned int) cmd->data.color.first,
(int) cmd->data.color.r, (int) cmd->data.color.g,
(int) cmd->data.color.b, (int) cmd->data.color.a);
break;
case SDL_RENDERCMD_CLEAR:
SDL_Log(" %u. clear (first=%u, r=%d, g=%d, b=%d, a=%d)", i++,
(unsigned int) cmd->data.color.first,
(int) cmd->data.color.r, (int) cmd->data.color.g,
(int) cmd->data.color.b, (int) cmd->data.color.a);
break;
case SDL_RENDERCMD_DRAW_POINTS:
SDL_Log(" %u. draw points (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
(unsigned int) cmd->data.draw.first,
(unsigned int) cmd->data.draw.count,
(int) cmd->data.draw.r, (int) cmd->data.draw.g,
(int) cmd->data.draw.b, (int) cmd->data.draw.a,
(int) cmd->data.draw.blend);
break;
case SDL_RENDERCMD_DRAW_LINES:
SDL_Log(" %u. draw lines (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
(unsigned int) cmd->data.draw.first,
(unsigned int) cmd->data.draw.count,
(int) cmd->data.draw.r, (int) cmd->data.draw.g,
(int) cmd->data.draw.b, (int) cmd->data.draw.a,
(int) cmd->data.draw.blend);
break;
case SDL_RENDERCMD_FILL_RECTS:
SDL_Log(" %u. fill rects (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
(unsigned int) cmd->data.draw.first,
(unsigned int) cmd->data.draw.count,
(int) cmd->data.draw.r, (int) cmd->data.draw.g,
(int) cmd->data.draw.b, (int) cmd->data.draw.a,
(int) cmd->data.draw.blend);
break;
case SDL_RENDERCMD_COPY:
SDL_Log(" %u. copy (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, tex=%p)", i++,
(unsigned int) cmd->data.draw.first,
(unsigned int) cmd->data.draw.count,
(int) cmd->data.draw.r, (int) cmd->data.draw.g,
(int) cmd->data.draw.b, (int) cmd->data.draw.a,
(int) cmd->data.draw.blend, cmd->data.draw.texture);
break;
case SDL_RENDERCMD_COPY_EX:
SDL_Log(" %u. copyex (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, tex=%p)", i++,
(unsigned int) cmd->data.draw.first,
(unsigned int) cmd->data.draw.count,
(int) cmd->data.draw.r, (int) cmd->data.draw.g,
(int) cmd->data.draw.b, (int) cmd->data.draw.a,
(int) cmd->data.draw.blend, cmd->data.draw.texture);
break;
}
cmd = cmd->next;
}
#endif
}
Sep 20, 2018
Sep 20, 2018
198
199
200
201
static int
FlushRenderCommands(SDL_Renderer *renderer)
{
Sep 24, 2018
Sep 24, 2018
202
203
SDL_AllocVertGap *prevgap = &renderer->vertex_data_gaps;
SDL_AllocVertGap *gap = prevgap;
Sep 20, 2018
Sep 20, 2018
204
int retval;
Sep 24, 2018
Sep 24, 2018
205
Sep 20, 2018
Sep 20, 2018
206
207
208
209
210
211
212
SDL_assert((renderer->render_commands == NULL) == (renderer->render_commands_tail == NULL));
if (renderer->render_commands == NULL) { /* nothing to do! */
SDL_assert(renderer->vertex_data_used == 0);
return 0;
}
Sep 24, 2018
Sep 24, 2018
213
214
DebugLogRenderCommands(renderer->render_commands);
Sep 20, 2018
Sep 20, 2018
215
216
retval = renderer->RunCommandQueue(renderer, renderer->render_commands, renderer->vertex_data, renderer->vertex_data_used);
Sep 24, 2018
Sep 24, 2018
217
218
219
220
221
222
223
224
while (gap) {
prevgap = gap;
gap = gap->next;
}
prevgap->next = renderer->vertex_data_gaps_pool;
renderer->vertex_data_gaps_pool = renderer->vertex_data_gaps.next;
renderer->vertex_data_gaps.next = NULL;
Sep 20, 2018
Sep 20, 2018
225
226
227
228
229
230
231
232
233
/* Move the whole render command queue to the unused pool so we can reuse them next time. */
if (renderer->render_commands_tail != NULL) {
renderer->render_commands_tail->next = renderer->render_commands_pool;
renderer->render_commands_pool = renderer->render_commands;
renderer->render_commands_tail = NULL;
renderer->render_commands = NULL;
}
renderer->vertex_data_used = 0;
renderer->render_command_generation++;
Sep 24, 2018
Sep 24, 2018
234
235
236
renderer->color_queued = SDL_FALSE;
renderer->viewport_queued = SDL_FALSE;
renderer->cliprect_queued = SDL_FALSE;
Sep 20, 2018
Sep 20, 2018
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
return retval;
}
static int
FlushRenderCommandsIfTextureNeeded(SDL_Texture *texture)
{
SDL_Renderer *renderer = texture->renderer;
if (texture->last_command_generation == renderer->render_command_generation) {
/* the current command queue depends on this texture, flush the queue now before it changes */
return FlushRenderCommands(renderer);
}
return 0;
}
static SDL_INLINE int
FlushRenderCommandsIfNotBatching(SDL_Renderer *renderer)
{
return renderer->batching ? 0 : FlushRenderCommands(renderer);
}
Oct 4, 2018
Oct 4, 2018
257
258
259
260
261
262
int
SDL_RenderFlush(SDL_Renderer * renderer)
{
return FlushRenderCommands(renderer);
}
Sep 24, 2018
Sep 24, 2018
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
static SDL_AllocVertGap *
AllocateVertexGap(SDL_Renderer *renderer)
{
SDL_AllocVertGap *retval = renderer->vertex_data_gaps_pool;
if (retval) {
renderer->vertex_data_gaps_pool = retval->next;
retval->next = NULL;
} else {
retval = (SDL_AllocVertGap *) SDL_malloc(sizeof (SDL_AllocVertGap));
if (!retval) {
SDL_OutOfMemory();
}
}
return retval;
}
Sep 20, 2018
Sep 20, 2018
280
void *
Sep 24, 2018
Sep 24, 2018
281
SDL_AllocateRenderVertices(SDL_Renderer *renderer, const size_t numbytes, const size_t alignment, size_t *offset)
Sep 20, 2018
Sep 20, 2018
282
{
Sep 24, 2018
Sep 24, 2018
283
284
const size_t needed = renderer->vertex_data_used + numbytes + alignment;
size_t aligner, aligned;
Sep 20, 2018
Sep 20, 2018
285
286
void *retval;
Sep 24, 2018
Sep 24, 2018
287
288
289
290
291
292
293
294
295
296
297
298
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
SDL_AllocVertGap *prevgap = &renderer->vertex_data_gaps;
SDL_AllocVertGap *gap = prevgap->next;
while (gap) {
const size_t gapoffset = gap->offset;
aligner = (alignment && ((gap->offset % alignment) != 0)) ? (alignment - (gap->offset % alignment)) : 0;
aligned = gapoffset + aligner;
/* Can we use this gap? */
if ((aligner < gap->len) && ((gap->len - aligner) >= numbytes)) {
/* we either finished this gap off, trimmed the left, trimmed the right, or split it into two gaps. */
if (gap->len == numbytes) { /* finished it off, remove it */
SDL_assert(aligned == gapoffset);
prevgap->next = gap->next;
gap->next = renderer->vertex_data_gaps_pool;
renderer->vertex_data_gaps_pool = gap;
} else if (aligned == gapoffset) { /* trimmed the left */
gap->offset += numbytes;
gap->len -= numbytes;
} else if (((aligned - gapoffset) + numbytes) == gap->len) { /* trimmed the right */
gap->len -= numbytes;
} else { /* split into two gaps */
SDL_AllocVertGap *newgap = AllocateVertexGap(renderer);
if (!newgap) {
return NULL;
}
newgap->offset = aligned + numbytes;
newgap->len = gap->len - (aligner + numbytes);
newgap->next = gap->next;
// gap->offset doesn't change.
gap->len = aligner;
gap->next = newgap;
}
if (offset) {
*offset = aligned;
}
return ((Uint8 *) renderer->vertex_data) + aligned;
}
/* Try the next gap */
prevgap = gap;
gap = gap->next;
}
/* no gaps with enough space; get a new piece of the vertex buffer */
Sep 20, 2018
Sep 20, 2018
332
while (needed > renderer->vertex_data_allocation) {
Sep 24, 2018
Sep 24, 2018
333
const size_t current_allocation = renderer->vertex_data ? renderer->vertex_data_allocation : 1024;
Sep 20, 2018
Sep 20, 2018
334
335
336
337
338
339
340
341
342
343
const size_t newsize = current_allocation * 2;
void *ptr = SDL_realloc(renderer->vertex_data, newsize);
if (ptr == NULL) {
SDL_OutOfMemory();
return NULL;
}
renderer->vertex_data = ptr;
renderer->vertex_data_allocation = newsize;
}
Sep 24, 2018
Sep 24, 2018
344
345
346
347
aligner = (alignment && ((renderer->vertex_data_used % alignment) != 0)) ? (alignment - (renderer->vertex_data_used % alignment)) : 0;
aligned = renderer->vertex_data_used + aligner;
retval = ((Uint8 *) renderer->vertex_data) + aligned;
Sep 20, 2018
Sep 20, 2018
348
if (offset) {
Sep 24, 2018
Sep 24, 2018
349
*offset = aligned;
Sep 20, 2018
Sep 20, 2018
350
351
}
Sep 24, 2018
Sep 24, 2018
352
353
354
355
356
357
358
359
360
361
362
363
if (aligner) { /* made a new gap... */
SDL_AllocVertGap *newgap = AllocateVertexGap(renderer);
if (newgap) { /* just let it slide as lost space if malloc fails. */
newgap->offset = renderer->vertex_data_used;
newgap->len = aligner;
newgap->next = NULL;
prevgap->next = newgap;
}
}
renderer->vertex_data_used += aligner + numbytes;
Sep 20, 2018
Sep 20, 2018
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
return retval;
}
static SDL_RenderCommand *
AllocateRenderCommand(SDL_Renderer *renderer)
{
SDL_RenderCommand *retval = NULL;
/* !!! FIXME: are there threading limitations in SDL's render API? If not, we need to mutex this. */
retval = renderer->render_commands_pool;
if (retval != NULL) {
renderer->render_commands_pool = retval->next;
retval->next = NULL;
} else {
retval = SDL_calloc(1, sizeof (*retval));
if (!retval) {
SDL_OutOfMemory();
return NULL;
}
}
SDL_assert((renderer->render_commands == NULL) == (renderer->render_commands_tail == NULL));
if (renderer->render_commands_tail != NULL) {
renderer->render_commands_tail->next = retval;
} else {
renderer->render_commands = retval;
}
renderer->render_commands_tail = retval;
return retval;
}
static int
Sep 24, 2018
Sep 24, 2018
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
QueueCmdSetViewport(SDL_Renderer *renderer)
{
int retval = 0;
if (!renderer->viewport_queued || (SDL_memcmp(&renderer->viewport, &renderer->last_queued_viewport, sizeof (SDL_Rect)) != 0)) {
SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
retval = -1;
if (cmd != NULL) {
cmd->command = SDL_RENDERCMD_SETVIEWPORT;
cmd->data.viewport.first = 0; /* render backend will fill this in. */
SDL_memcpy(&cmd->data.viewport.rect, &renderer->viewport, sizeof (renderer->viewport));
retval = renderer->QueueSetViewport(renderer, cmd);
if (retval < 0) {
cmd->command = SDL_RENDERCMD_NO_OP;
} else {
SDL_memcpy(&renderer->last_queued_viewport, &renderer->viewport, sizeof (SDL_Rect));
renderer->viewport_queued = SDL_TRUE;
}
}
Sep 20, 2018
Sep 20, 2018
415
}
Sep 25, 2018
Sep 25, 2018
416
return retval;
Sep 20, 2018
Sep 20, 2018
417
418
419
}
static int
Sep 24, 2018
Sep 24, 2018
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
QueueCmdSetClipRect(SDL_Renderer *renderer)
{
int retval = 0;
if ((!renderer->cliprect_queued) ||
(renderer->clipping_enabled != renderer->last_queued_cliprect_enabled) ||
(SDL_memcmp(&renderer->clip_rect, &renderer->last_queued_cliprect, sizeof (SDL_Rect)) != 0)) {
SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
if (cmd == NULL) {
retval = -1;
} else {
cmd->command = SDL_RENDERCMD_SETCLIPRECT;
cmd->data.cliprect.enabled = renderer->clipping_enabled;
SDL_memcpy(&cmd->data.cliprect.rect, &renderer->clip_rect, sizeof (cmd->data.cliprect.rect));
SDL_memcpy(&renderer->last_queued_cliprect, &renderer->clip_rect, sizeof (SDL_Rect));
renderer->last_queued_cliprect_enabled = renderer->clipping_enabled;
renderer->cliprect_queued = SDL_TRUE;
}
Sep 20, 2018
Sep 20, 2018
437
}
Sep 25, 2018
Sep 25, 2018
438
return retval;
Sep 24, 2018
Sep 24, 2018
439
}
Sep 20, 2018
Sep 20, 2018
440
Sep 24, 2018
Sep 24, 2018
441
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
static int
QueueCmdSetDrawColor(SDL_Renderer *renderer, const Uint8 r, const Uint8 g, const Uint8 b, const Uint8 a)
{
const Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
int retval = 0;
if (!renderer->color_queued || (color != renderer->last_queued_color)) {
SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
retval = -1;
if (cmd != NULL) {
cmd->command = SDL_RENDERCMD_SETDRAWCOLOR;
cmd->data.color.first = 0; /* render backend will fill this in. */
cmd->data.color.r = r;
cmd->data.color.g = g;
cmd->data.color.b = b;
cmd->data.color.a = a;
retval = renderer->QueueSetDrawColor(renderer, cmd);
if (retval < 0) {
cmd->command = SDL_RENDERCMD_NO_OP;
} else {
renderer->last_queued_color = color;
renderer->color_queued = SDL_TRUE;
}
}
}
Sep 25, 2018
Sep 25, 2018
467
return retval;
Sep 20, 2018
Sep 20, 2018
468
469
470
471
472
473
474
475
476
477
478
}
static int
QueueCmdClear(SDL_Renderer *renderer)
{
SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
if (cmd == NULL) {
return -1;
}
cmd->command = SDL_RENDERCMD_CLEAR;
Sep 24, 2018
Sep 24, 2018
479
cmd->data.color.first = 0;
Sep 20, 2018
Sep 20, 2018
480
481
482
483
cmd->data.color.r = renderer->r;
cmd->data.color.g = renderer->g;
cmd->data.color.b = renderer->b;
cmd->data.color.a = renderer->a;
Sep 25, 2018
Sep 25, 2018
484
return 0;
Sep 20, 2018
Sep 20, 2018
485
486
}
Sep 24, 2018
Sep 24, 2018
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
static int
PrepQueueCmdDraw(SDL_Renderer *renderer, const Uint8 r, const Uint8 g, const Uint8 b, const Uint8 a)
{
int retval = 0;
if (retval == 0) {
retval = QueueCmdSetDrawColor(renderer, r, g, b, a);
}
if (retval == 0) {
retval = QueueCmdSetViewport(renderer);
}
if (retval == 0) {
retval = QueueCmdSetClipRect(renderer);
}
return retval;
}
Sep 20, 2018
Sep 20, 2018
503
504
505
static SDL_RenderCommand *
PrepQueueCmdDrawSolid(SDL_Renderer *renderer, const SDL_RenderCommandType cmdtype)
{
Sep 28, 2018
Sep 28, 2018
506
/* !!! FIXME: drop this draw if viewport w or h is zero. */
Sep 24, 2018
Sep 24, 2018
507
508
509
510
511
512
513
514
515
516
517
518
519
520
SDL_RenderCommand *cmd = NULL;
if (PrepQueueCmdDraw(renderer, renderer->r, renderer->g, renderer->b, renderer->a) == 0) {
cmd = AllocateRenderCommand(renderer);
if (cmd != NULL) {
cmd->command = cmdtype;
cmd->data.draw.first = 0; /* render backend will fill this in. */
cmd->data.draw.count = 0; /* render backend will fill this in. */
cmd->data.draw.r = renderer->r;
cmd->data.draw.g = renderer->g;
cmd->data.draw.b = renderer->b;
cmd->data.draw.a = renderer->a;
cmd->data.draw.blend = renderer->blendMode;
cmd->data.draw.texture = NULL; /* no texture. */
}
Sep 20, 2018
Sep 20, 2018
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
}
return cmd;
}
static int
QueueCmdDrawPoints(SDL_Renderer *renderer, const SDL_FPoint * points, const int count)
{
SDL_RenderCommand *cmd = PrepQueueCmdDrawSolid(renderer, SDL_RENDERCMD_DRAW_POINTS);
int retval = -1;
if (cmd != NULL) {
retval = renderer->QueueDrawPoints(renderer, cmd, points, count);
if (retval < 0) {
cmd->command = SDL_RENDERCMD_NO_OP;
}
}
Sep 25, 2018
Sep 25, 2018
536
return retval;
Sep 20, 2018
Sep 20, 2018
537
538
539
540
541
542
543
544
545
546
547
548
549
}
static int
QueueCmdDrawLines(SDL_Renderer *renderer, const SDL_FPoint * points, const int count)
{
SDL_RenderCommand *cmd = PrepQueueCmdDrawSolid(renderer, SDL_RENDERCMD_DRAW_LINES);
int retval = -1;
if (cmd != NULL) {
retval = renderer->QueueDrawLines(renderer, cmd, points, count);
if (retval < 0) {
cmd->command = SDL_RENDERCMD_NO_OP;
}
}
Sep 25, 2018
Sep 25, 2018
550
return retval;
Sep 20, 2018
Sep 20, 2018
551
552
553
554
555
556
557
558
559
560
561
562
563
}
static int
QueueCmdFillRects(SDL_Renderer *renderer, const SDL_FRect * rects, const int count)
{
SDL_RenderCommand *cmd = PrepQueueCmdDrawSolid(renderer, SDL_RENDERCMD_FILL_RECTS);
int retval = -1;
if (cmd != NULL) {
retval = renderer->QueueFillRects(renderer, cmd, rects, count);
if (retval < 0) {
cmd->command = SDL_RENDERCMD_NO_OP;
}
}
Sep 25, 2018
Sep 25, 2018
564
return retval;
Sep 20, 2018
Sep 20, 2018
565
566
567
568
569
}
static SDL_RenderCommand *
PrepQueueCmdDrawTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_RenderCommandType cmdtype)
{
Sep 28, 2018
Sep 28, 2018
570
/* !!! FIXME: drop this draw if viewport w or h is zero. */
Sep 24, 2018
Sep 24, 2018
571
572
573
574
575
576
577
578
579
580
581
582
583
584
SDL_RenderCommand *cmd = NULL;
if (PrepQueueCmdDraw(renderer, texture->r, texture->g, texture->b, texture->a) == 0) {
cmd = AllocateRenderCommand(renderer);
if (cmd != NULL) {
cmd->command = cmdtype;
cmd->data.draw.first = 0; /* render backend will fill this in. */
cmd->data.draw.count = 0; /* render backend will fill this in. */
cmd->data.draw.r = texture->r;
cmd->data.draw.g = texture->g;
cmd->data.draw.b = texture->b;
cmd->data.draw.a = texture->a;
cmd->data.draw.blend = texture->blendMode;
cmd->data.draw.texture = texture;
}
Sep 20, 2018
Sep 20, 2018
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
}
return cmd;
}
static int
QueueCmdCopy(SDL_Renderer *renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_FRect * dstrect)
{
SDL_RenderCommand *cmd = PrepQueueCmdDrawTexture(renderer, texture, SDL_RENDERCMD_COPY);
int retval = -1;
if (cmd != NULL) {
retval = renderer->QueueCopy(renderer, cmd, texture, srcrect, dstrect);
if (retval < 0) {
cmd->command = SDL_RENDERCMD_NO_OP;
}
}
Sep 25, 2018
Sep 25, 2018
600
return retval;
Sep 20, 2018
Sep 20, 2018
601
602
603
604
605
606
607
608
609
}
static int
QueueCmdCopyEx(SDL_Renderer *renderer, SDL_Texture * texture,
const SDL_Rect * srcquad, const SDL_FRect * dstrect,
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
{
SDL_RenderCommand *cmd = PrepQueueCmdDrawTexture(renderer, texture, SDL_RENDERCMD_COPY_EX);
int retval = -1;
Sep 29, 2018
Sep 29, 2018
610
SDL_assert(renderer->QueueCopyEx != NULL); /* should have caught at higher level. */
Sep 20, 2018
Sep 20, 2018
611
612
613
614
615
616
if (cmd != NULL) {
retval = renderer->QueueCopyEx(renderer, cmd, texture, srcquad, dstrect, angle, center, flip);
if (retval < 0) {
cmd->command = SDL_RENDERCMD_NO_OP;
}
}
Sep 25, 2018
Sep 25, 2018
617
return retval;
Sep 20, 2018
Sep 20, 2018
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
static int UpdateLogicalSize(SDL_Renderer *renderer);
int
SDL_GetNumRenderDrivers(void)
{
#if !SDL_RENDER_DISABLED
return SDL_arraysize(render_drivers);
#else
return 0;
#endif
}
int
SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
{
#if !SDL_RENDER_DISABLED
if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
return SDL_SetError("index must be in the range of 0 - %d",
SDL_GetNumRenderDrivers() - 1);
}
*info = render_drivers[index]->info;
return 0;
#else
return SDL_SetError("SDL not built with rendering support");
#endif
}
Jun 18, 2018
Jun 18, 2018
648
649
650
651
652
653
654
655
656
657
static void GetWindowViewportValues(SDL_Renderer *renderer, int *logical_w, int *logical_h, SDL_Rect *viewport, SDL_FPoint *scale)
{
SDL_LockMutex(renderer->target_mutex);
*logical_w = renderer->target ? renderer->logical_w_backup : renderer->logical_w;
*logical_h = renderer->target ? renderer->logical_h_backup : renderer->logical_h;
*viewport = renderer->target ? renderer->viewport_backup : renderer->viewport;
*scale = renderer->target ? renderer->scale_backup : renderer->scale;
SDL_UnlockMutex(renderer->target_mutex);
}
Aug 14, 2017
Aug 14, 2017
658
static int SDLCALL
659
660
661
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
SDL_RendererEventWatch(void *userdata, SDL_Event *event)
{
SDL_Renderer *renderer = (SDL_Renderer *)userdata;
if (event->type == SDL_WINDOWEVENT) {
SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
if (window == renderer->window) {
if (renderer->WindowEvent) {
renderer->WindowEvent(renderer, &event->window);
}
if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
/* Make sure we're operating on the default render target */
SDL_Texture *saved_target = SDL_GetRenderTarget(renderer);
if (saved_target) {
SDL_SetRenderTarget(renderer, NULL);
}
if (renderer->logical_w) {
UpdateLogicalSize(renderer);
} else {
/* Window was resized, reset viewport */
int w, h;
if (renderer->GetOutputSize) {
renderer->GetOutputSize(renderer, &w, &h);
} else {
SDL_GetWindowSize(renderer->window, &w, &h);
}
if (renderer->target) {
renderer->viewport_backup.x = 0;
renderer->viewport_backup.y = 0;
renderer->viewport_backup.w = w;
renderer->viewport_backup.h = h;
} else {
renderer->viewport.x = 0;
renderer->viewport.y = 0;
renderer->viewport.w = w;
renderer->viewport.h = h;
Sep 24, 2018
Sep 24, 2018
699
QueueCmdSetViewport(renderer);
Sep 25, 2018
Sep 25, 2018
700
FlushRenderCommandsIfNotBatching(renderer);
701
702
703
704
705
706
707
708
709
710
711
712
713
714
}
}
if (saved_target) {
SDL_SetRenderTarget(renderer, saved_target);
}
} else if (event->window.event == SDL_WINDOWEVENT_HIDDEN) {
renderer->hidden = SDL_TRUE;
} else if (event->window.event == SDL_WINDOWEVENT_SHOWN) {
if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)) {
renderer->hidden = SDL_FALSE;
}
} else if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) {
renderer->hidden = SDL_TRUE;
Dec 27, 2015
Dec 27, 2015
715
716
} else if (event->window.event == SDL_WINDOWEVENT_RESTORED ||
event->window.event == SDL_WINDOWEVENT_MAXIMIZED) {
717
718
719
720
721
722
723
if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
renderer->hidden = SDL_FALSE;
}
}
}
} else if (event->type == SDL_MOUSEMOTION) {
SDL_Window *window = SDL_GetWindowFromID(event->motion.windowID);
Jun 18, 2018
Jun 18, 2018
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
if (window == renderer->window) {
int logical_w, logical_h;
SDL_Rect viewport;
SDL_FPoint scale;
GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
if (logical_w) {
event->motion.x -= (int)(viewport.x * renderer->dpi_scale.x);
event->motion.y -= (int)(viewport.y * renderer->dpi_scale.y);
event->motion.x = (int)(event->motion.x / (scale.x * renderer->dpi_scale.x));
event->motion.y = (int)(event->motion.y / (scale.y * renderer->dpi_scale.y));
if (event->motion.xrel > 0) {
event->motion.xrel = SDL_max(1, (int)(event->motion.xrel / (scale.x * renderer->dpi_scale.x)));
} else if (event->motion.xrel < 0) {
event->motion.xrel = SDL_min(-1, (int)(event->motion.xrel / (scale.x * renderer->dpi_scale.x)));
}
if (event->motion.yrel > 0) {
event->motion.yrel = SDL_max(1, (int)(event->motion.yrel / (scale.y * renderer->dpi_scale.y)));
} else if (event->motion.yrel < 0) {
event->motion.yrel = SDL_min(-1, (int)(event->motion.yrel / (scale.y * renderer->dpi_scale.y)));
}
744
745
746
747
748
}
}
} else if (event->type == SDL_MOUSEBUTTONDOWN ||
event->type == SDL_MOUSEBUTTONUP) {
SDL_Window *window = SDL_GetWindowFromID(event->button.windowID);
Jun 18, 2018
Jun 18, 2018
749
750
751
752
753
754
755
756
757
758
759
if (window == renderer->window) {
int logical_w, logical_h;
SDL_Rect viewport;
SDL_FPoint scale;
GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
if (logical_w) {
event->button.x -= (int)(viewport.x * renderer->dpi_scale.x);
event->button.y -= (int)(viewport.y * renderer->dpi_scale.y);
event->button.x = (int)(event->button.x / (scale.x * renderer->dpi_scale.x));
event->button.y = (int)(event->button.y / (scale.y * renderer->dpi_scale.y));
}
Oct 12, 2017
Oct 12, 2017
761
762
763
} else if (event->type == SDL_FINGERDOWN ||
event->type == SDL_FINGERUP ||
event->type == SDL_FINGERMOTION) {
Jun 18, 2018
Jun 18, 2018
764
765
766
767
768
int logical_w, logical_h;
SDL_Rect viewport;
SDL_FPoint scale;
GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
if (logical_w) {
Oct 12, 2017
Oct 12, 2017
769
770
771
772
773
774
775
int w = 1;
int h = 1;
SDL_GetRendererOutputSize(renderer, &w, &h);
event->tfinger.x *= (w - 1);
event->tfinger.y *= (h - 1);
Jun 18, 2018
Jun 18, 2018
776
777
778
779
event->tfinger.x -= (viewport.x * renderer->dpi_scale.x);
event->tfinger.y -= (viewport.y * renderer->dpi_scale.y);
event->tfinger.x = (event->tfinger.x / (scale.x * renderer->dpi_scale.x));
event->tfinger.y = (event->tfinger.y / (scale.y * renderer->dpi_scale.y));
Oct 12, 2017
Oct 12, 2017
780
Jun 18, 2018
Jun 18, 2018
781
782
if (logical_w > 1) {
event->tfinger.x = event->tfinger.x / (logical_w - 1);
Oct 12, 2017
Oct 12, 2017
783
784
785
} else {
event->tfinger.x = 0.5f;
}
Jun 18, 2018
Jun 18, 2018
786
787
if (logical_h > 1) {
event->tfinger.y = event->tfinger.y / (logical_h - 1);
Oct 12, 2017
Oct 12, 2017
788
789
790
791
} else {
event->tfinger.y = 0.5f;
}
}
Oct 12, 2017
Oct 12, 2017
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
return 0;
}
int
SDL_CreateWindowAndRenderer(int width, int height, Uint32 window_flags,
SDL_Window **window, SDL_Renderer **renderer)
{
*window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width, height, window_flags);
if (!*window) {
*renderer = NULL;
return -1;
}
*renderer = SDL_CreateRenderer(*window, -1, 0);
if (!*renderer) {
return -1;
}
return 0;
}
Sep 20, 2018
Sep 20, 2018
817
818
819
820
821
static SDL_INLINE
void VerifyDrawQueueFunctions(const SDL_Renderer *renderer)
{
/* all of these functions are required to be implemented, even as no-ops, so we don't
have to check that they aren't NULL over and over. */
Sep 24, 2018
Sep 24, 2018
822
823
SDL_assert(renderer->QueueSetViewport != NULL);
SDL_assert(renderer->QueueSetDrawColor != NULL);
Sep 20, 2018
Sep 20, 2018
824
825
826
827
828
829
830
SDL_assert(renderer->QueueDrawPoints != NULL);
SDL_assert(renderer->QueueDrawLines != NULL);
SDL_assert(renderer->QueueFillRects != NULL);
SDL_assert(renderer->QueueCopy != NULL);
SDL_assert(renderer->RunCommandQueue != NULL);
}
831
832
833
834
835
836
SDL_Renderer *
SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
{
#if !SDL_RENDER_DISABLED
SDL_Renderer *renderer = NULL;
int n = SDL_GetNumRenderDrivers();
Sep 20, 2018
Sep 20, 2018
837
SDL_bool batching = SDL_TRUE;
838
839
840
841
842
843
844
845
846
847
848
849
const char *hint;
if (!window) {
SDL_SetError("Invalid window");
return NULL;
}
if (SDL_GetRenderer(window)) {
SDL_SetError("Renderer already associated with window");
return NULL;
}
Oct 8, 2016
Oct 8, 2016
850
851
if (SDL_GetHint(SDL_HINT_RENDER_VSYNC)) {
if (SDL_GetHintBoolean(SDL_HINT_RENDER_VSYNC, SDL_TRUE)) {
852
flags |= SDL_RENDERER_PRESENTVSYNC;
Oct 8, 2016
Oct 8, 2016
853
854
} else {
flags &= ~SDL_RENDERER_PRESENTVSYNC;
855
856
857
858
859
860
861
862
863
864
865
866
}
}
if (index < 0) {
hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
if (hint) {
for (index = 0; index < n; ++index) {
const SDL_RenderDriver *driver = render_drivers[index];
if (SDL_strcasecmp(hint, driver->info.name) == 0) {
/* Create a new renderer instance */
renderer = driver->CreateRenderer(window, flags);
Sep 20, 2018
Sep 20, 2018
867
868
869
if (renderer) {
batching = SDL_FALSE;
}
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
break;
}
}
}
if (!renderer) {
for (index = 0; index < n; ++index) {
const SDL_RenderDriver *driver = render_drivers[index];
if ((driver->info.flags & flags) == flags) {
/* Create a new renderer instance */
renderer = driver->CreateRenderer(window, flags);
if (renderer) {
/* Yay, we got one! */
break;
}
}
}
}
if (index == n) {
SDL_SetError("Couldn't find matching render driver");
return NULL;
}
} else {
if (index >= SDL_GetNumRenderDrivers()) {
SDL_SetError("index must be -1 or in the range of 0 - %d",
SDL_GetNumRenderDrivers() - 1);
return NULL;
}
/* Create a new renderer instance */
renderer = render_drivers[index]->CreateRenderer(window, flags);
Sep 20, 2018
Sep 20, 2018
901
batching = SDL_FALSE;
902
903
904
}
if (renderer) {
Sep 20, 2018
Sep 20, 2018
905
906
907
VerifyDrawQueueFunctions(renderer);
/* let app/user override batching decisions. */
Sep 24, 2018
Sep 24, 2018
908
909
910
if (renderer->always_batch) {
batching = SDL_TRUE;
} else if (SDL_GetHint(SDL_HINT_RENDER_BATCHING)) {
Sep 20, 2018
Sep 20, 2018
911
912
913
914
batching = SDL_GetHintBoolean(SDL_HINT_RENDER_BATCHING, SDL_TRUE);
}
renderer->batching = batching;
915
916
renderer->magic = &renderer_magic;
renderer->window = window;
Jun 18, 2018
Jun 18, 2018
917
renderer->target_mutex = SDL_CreateMutex();
918
919
renderer->scale.x = 1.0f;
renderer->scale.y = 1.0f;
Aug 2, 2017
Aug 2, 2017
920
921
922
renderer->dpi_scale.x = 1.0f;
renderer->dpi_scale.y = 1.0f;
Sep 20, 2018
Sep 20, 2018
923
924
925
/* new textures start at zero, so we start at 1 so first render doesn't flush by accident. */
renderer->render_command_generation = 1;
Aug 2, 2017
Aug 2, 2017
926
927
928
929
930
931
932
933
934
if (window && renderer->GetOutputSize) {
int window_w, window_h;
int output_w, output_h;
if (renderer->GetOutputSize(renderer, &output_w, &output_h) == 0) {
SDL_GetWindowSize(renderer->window, &window_w, &window_h);
renderer->dpi_scale.x = (float)window_w / output_w;
renderer->dpi_scale.y = (float)window_h / output_h;
}
}
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
965
966
if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN|SDL_WINDOW_MINIMIZED)) {
renderer->hidden = SDL_TRUE;
} else {
renderer->hidden = SDL_FALSE;
}
SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
SDL_RenderSetViewport(renderer, NULL);
SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
"Created renderer: %s", renderer->info.name);
}
return renderer;
#else
SDL_SetError("SDL not built with rendering support");
return NULL;
#endif
}
SDL_Renderer *
SDL_CreateSoftwareRenderer(SDL_Surface * surface)
{
#if !SDL_RENDER_DISABLED
SDL_Renderer *renderer;
renderer = SW_CreateRendererForSurface(surface);
if (renderer) {
Sep 20, 2018
Sep 20, 2018
967
VerifyDrawQueueFunctions(renderer);
968
renderer->magic = &renderer_magic;
Jun 18, 2018
Jun 18, 2018
969
renderer->target_mutex = SDL_CreateMutex();
970
971
972
renderer->scale.x = 1.0f;
renderer->scale.y = 1.0f;
Sep 20, 2018
Sep 20, 2018
973
974
975
/* new textures start at zero, so we start at 1 so first render doesn't flush by accident. */
renderer->render_command_generation = 1;
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
SDL_RenderSetViewport(renderer, NULL);
}
return renderer;
#else
SDL_SetError("SDL not built with rendering support");
return NULL;
#endif /* !SDL_RENDER_DISABLED */
}
SDL_Renderer *
SDL_GetRenderer(SDL_Window * window)
{
return (SDL_Renderer *)SDL_GetWindowData(window, SDL_WINDOWRENDERDATA);
}
int
SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info)
{
CHECK_RENDERER_MAGIC(renderer, -1);
*info = renderer->info;
return 0;
}
int