This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_render_sw.c
542 lines (457 loc) · 15.8 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2011 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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"
24
25
#if !SDL_RENDER_DISABLED
26
#include "../SDL_sysrender.h"
27
28
29
30
31
32
33
34
#include "SDL_draw.h"
#include "SDL_blendfillrect.h"
#include "SDL_blendline.h"
#include "SDL_blendpoint.h"
#include "SDL_drawline.h"
#include "SDL_drawpoint.h"
35
36
37
/* SDL surface based renderer implementation */
38
static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
39
40
static void SW_WindowEvent(SDL_Renderer * renderer,
const SDL_WindowEvent *event);
41
static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
42
43
44
45
static int SW_SetTextureColorMod(SDL_Renderer * renderer,
SDL_Texture * texture);
static int SW_SetTextureAlphaMod(SDL_Renderer * renderer,
SDL_Texture * texture);
46
47
static int SW_SetTextureBlendMode(SDL_Renderer * renderer,
SDL_Texture * texture);
48
49
50
static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels,
int pitch);
51
static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
52
const SDL_Rect * rect, void **pixels, int *pitch);
53
static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
54
55
static int SW_UpdateViewport(SDL_Renderer * renderer);
static int SW_RenderClear(SDL_Renderer * renderer);
56
57
58
59
60
static int SW_RenderDrawPoints(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int SW_RenderDrawLines(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int SW_RenderFillRects(SDL_Renderer * renderer,
61
const SDL_Rect * rects, int count);
62
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
63
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
64
static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
65
Uint32 format, void * pixels, int pitch);
66
67
68
69
70
71
72
static void SW_RenderPresent(SDL_Renderer * renderer);
static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void SW_DestroyRenderer(SDL_Renderer * renderer);
SDL_RenderDriver SW_RenderDriver = {
SW_CreateRenderer,
73
74
{
"software",
75
SDL_RENDERER_SOFTWARE,
76
8,
77
{
78
79
80
81
82
83
84
SDL_PIXELFORMAT_RGB555,
SDL_PIXELFORMAT_RGB565,
SDL_PIXELFORMAT_RGB888,
SDL_PIXELFORMAT_BGR888,
SDL_PIXELFORMAT_ARGB8888,
SDL_PIXELFORMAT_RGBA8888,
SDL_PIXELFORMAT_ABGR8888,
85
86
SDL_PIXELFORMAT_BGRA8888
},
87
88
89
90
91
92
0,
0}
};
typedef struct
{
93
SDL_Surface *surface;
94
} SW_RenderData;
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
static SDL_Surface *
SW_ActivateRenderer(SDL_Renderer * renderer)
{
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
if (!data->surface) {
data->surface = SDL_GetWindowSurface(renderer->window);
SW_UpdateViewport(renderer);
}
return data->surface;
}
110
SDL_Renderer *
111
SW_CreateRendererForSurface(SDL_Surface * surface)
112
113
{
SDL_Renderer *renderer;
114
SW_RenderData *data;
115
116
117
if (!surface) {
SDL_SetError("Can't create renderer for NULL surface");
118
119
120
121
122
123
124
125
126
return NULL;
}
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
127
data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
128
if (!data) {
129
SW_DestroyRenderer(renderer);
130
131
132
SDL_OutOfMemory();
return NULL;
}
133
134
data->surface = surface;
135
renderer->WindowEvent = SW_WindowEvent;
136
137
138
139
140
141
142
renderer->CreateTexture = SW_CreateTexture;
renderer->SetTextureColorMod = SW_SetTextureColorMod;
renderer->SetTextureAlphaMod = SW_SetTextureAlphaMod;
renderer->SetTextureBlendMode = SW_SetTextureBlendMode;
renderer->UpdateTexture = SW_UpdateTexture;
renderer->LockTexture = SW_LockTexture;
renderer->UnlockTexture = SW_UnlockTexture;
143
renderer->UpdateViewport = SW_UpdateViewport;
144
renderer->DestroyTexture = SW_DestroyTexture;
145
renderer->RenderClear = SW_RenderClear;
146
147
148
renderer->RenderDrawPoints = SW_RenderDrawPoints;
renderer->RenderDrawLines = SW_RenderDrawLines;
renderer->RenderFillRects = SW_RenderFillRects;
149
renderer->RenderCopy = SW_RenderCopy;
150
renderer->RenderReadPixels = SW_RenderReadPixels;
151
152
renderer->RenderPresent = SW_RenderPresent;
renderer->DestroyRenderer = SW_DestroyRenderer;
153
renderer->info = SW_RenderDriver.info;
154
155
renderer->driverdata = data;
156
157
SW_ActivateRenderer(renderer);
158
159
return renderer;
}
160
161
162
163
164
SDL_Renderer *
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
{
SDL_Surface *surface;
165
166
167
surface = SDL_GetWindowSurface(window);
if (!surface) {
168
169
return NULL;
}
170
return SW_CreateRendererForSurface(surface);
171
172
}
173
174
static void
SW_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
175
176
177
{
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
178
if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
179
data->surface = NULL;
180
181
182
}
}
183
static int
184
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
185
{
186
187
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
188
189
190
191
192
193
if (!SDL_PixelFormatEnumToMasks
(texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
SDL_SetError("Unknown texture format");
return -1;
}
194
195
196
197
198
199
200
201
texture->driverdata =
SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
Bmask, Amask);
SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
texture->b);
SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
202
203
204
if (texture->access == SDL_TEXTUREACCESS_STATIC) {
SDL_SetSurfaceRLE(texture->driverdata, 1);
205
206
207
208
209
210
211
212
}
if (!texture->driverdata) {
return -1;
}
return 0;
}
213
214
215
static int
SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
{
216
217
218
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
texture->b);
219
220
221
222
223
}
static int
SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
{
224
225
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
return SDL_SetSurfaceAlphaMod(surface, texture->a);
226
227
228
229
230
}
static int
SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
{
231
232
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
233
234
}
235
static int
236
237
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
238
{
239
240
241
242
243
244
245
246
247
248
249
250
251
252
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
Uint8 *src, *dst;
int row;
size_t length;
src = (Uint8 *) pixels;
dst = (Uint8 *) surface->pixels +
rect->y * surface->pitch +
rect->x * surface->format->BytesPerPixel;
length = rect->w * surface->format->BytesPerPixel;
for (row = 0; row < rect->h; ++row) {
SDL_memcpy(dst, src, length);
src += pitch;
dst += surface->pitch;
253
}
254
return 0;
255
256
257
}
static int
258
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
259
const SDL_Rect * rect, void **pixels, int *pitch)
260
{
261
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
262
263
264
265
266
267
*pixels =
(void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
rect->x * surface->format->BytesPerPixel);
*pitch = surface->pitch;
return 0;
268
269
270
}
static void
271
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
272
273
274
{
}
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
static int
SW_UpdateViewport(SDL_Renderer * renderer)
{
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
SDL_Surface *surface = data->surface;
if (!surface) {
/* We'll update the viewport after we recreate the surface */
return 0;
}
if (!renderer->viewport.w && !renderer->viewport.h) {
/* There may be no window, so update the viewport directly */
renderer->viewport.w = surface->w;
renderer->viewport.h = surface->h;
}
291
SDL_SetClipRect(data->surface, &renderer->viewport);
292
293
294
295
296
return 0;
}
static int
SW_RenderClear(SDL_Renderer * renderer)
297
298
{
SDL_Surface *surface = SW_ActivateRenderer(renderer);
299
300
Uint32 color;
SDL_Rect clip_rect;
301
302
if (!surface) {
303
return -1;
304
}
305
306
307
308
309
310
311
312
313
314
color = SDL_MapRGBA(surface->format,
renderer->r, renderer->g, renderer->b, renderer->a);
/* By definition the clear ignores the clip rect */
clip_rect = surface->clip_rect;
SDL_SetClipRect(surface, NULL);
SDL_FillRect(surface, NULL, color);
SDL_SetClipRect(surface, &clip_rect);
return 0;
315
316
}
317
static int
318
319
SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
int count)
320
{
321
SDL_Surface *surface = SW_ActivateRenderer(renderer);
322
323
SDL_Point *temp = NULL;
int status;
324
325
if (!surface) {
326
327
328
return -1;
}
329
330
331
332
333
334
335
336
337
338
339
340
341
if (renderer->viewport.x || renderer->viewport.y) {
int i;
int x = renderer->viewport.x;
int y = renderer->viewport.y;
temp = SDL_stack_alloc(SDL_Point, count);
for (i = 0; i < count; ++i) {
temp[i].x = x + points[i].x;
temp[i].y = y + points[i].x;
}
points = temp;
}
342
/* Draw the points! */
343
if (renderer->blendMode == SDL_BLENDMODE_NONE) {
344
Uint32 color = SDL_MapRGBA(surface->format,
345
346
347
renderer->r, renderer->g, renderer->b,
renderer->a);
348
status = SDL_DrawPoints(surface, points, count, color);
349
} else {
350
351
352
353
354
355
356
357
status = SDL_BlendPoints(surface, points, count,
renderer->blendMode,
renderer->r, renderer->g, renderer->b,
renderer->a);
}
if (temp) {
SDL_stack_free(temp);
358
}
359
return status;
360
361
}
362
static int
363
364
SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
int count)
365
{
366
SDL_Surface *surface = SW_ActivateRenderer(renderer);
367
368
SDL_Point *temp = NULL;
int status;
369
370
if (!surface) {
371
372
373
return -1;
}
374
375
376
377
378
379
380
381
382
383
384
385
386
if (renderer->viewport.x || renderer->viewport.y) {
int i;
int x = renderer->viewport.x;
int y = renderer->viewport.y;
temp = SDL_stack_alloc(SDL_Point, count);
for (i = 0; i < count; ++i) {
temp[i].x = x + points[i].x;
temp[i].y = y + points[i].y;
}
points = temp;
}
387
/* Draw the lines! */
388
if (renderer->blendMode == SDL_BLENDMODE_NONE) {
389
Uint32 color = SDL_MapRGBA(surface->format,
390
391
392
renderer->r, renderer->g, renderer->b,
renderer->a);
393
status = SDL_DrawLines(surface, points, count, color);
394
} else {
395
396
397
398
399
400
401
402
status = SDL_BlendLines(surface, points, count,
renderer->blendMode,
renderer->r, renderer->g, renderer->b,
renderer->a);
}
if (temp) {
SDL_stack_free(temp);
403
}
404
return status;
405
406
}
407
static int
408
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects, int count)
409
{
410
SDL_Surface *surface = SW_ActivateRenderer(renderer);
411
412
SDL_Rect *temp = NULL;
int status;
413
414
if (!surface) {
415
416
417
return -1;
}
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
if (renderer->viewport.x || renderer->viewport.y) {
int i;
int x = renderer->viewport.x;
int y = renderer->viewport.y;
temp = SDL_stack_alloc(SDL_Rect, count);
for (i = 0; i < count; ++i) {
temp[i].x = x + rects[i].x;
temp[i].y = y + rects[i].y;
temp[i].w = rects[i].w;
temp[i].h = rects[i].h;
}
rects = temp;
}
433
if (renderer->blendMode == SDL_BLENDMODE_NONE) {
434
435
436
Uint32 color = SDL_MapRGBA(surface->format,
renderer->r, renderer->g, renderer->b,
renderer->a);
437
status = SDL_FillRects(surface, rects, count, color);
438
} else {
439
440
441
442
status = SDL_BlendFillRects(surface, rects, count,
renderer->blendMode,
renderer->r, renderer->g, renderer->b,
renderer->a);
443
}
444
445
446
447
448
if (temp) {
SDL_stack_free(temp);
}
return status;
449
450
}
451
static int
452
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
453
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
454
{
455
456
457
SDL_Surface *surface = SW_ActivateRenderer(renderer);
SDL_Surface *src = (SDL_Surface *) texture->driverdata;
SDL_Rect final_rect = *dstrect;
458
459
if (!surface) {
460
461
return -1;
}
462
463
464
465
466
if (renderer->viewport.x || renderer->viewport.y) {
final_rect.x += renderer->viewport.x;
final_rect.y += renderer->viewport.y;
}
467
468
469
470
471
if ( srcrect->w == final_rect.w && srcrect->h == final_rect.h ) {
return SDL_BlitSurface(src, srcrect, surface, &final_rect);
} else {
return SDL_BlitScaled(src, srcrect, surface, &final_rect);
}
472
473
}
474
475
static int
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
476
Uint32 format, void * pixels, int pitch)
477
{
478
479
480
SDL_Surface *surface = SW_ActivateRenderer(renderer);
Uint32 src_format;
void *src_pixels;
481
SDL_Rect final_rect;
482
483
if (!surface) {
484
485
486
return -1;
}
487
488
489
490
491
492
493
494
if (renderer->viewport.x || renderer->viewport.y) {
final_rect.x = renderer->viewport.x + rect->x;
final_rect.y = renderer->viewport.y + rect->y;
final_rect.w = rect->w;
final_rect.h = rect->h;
rect = &final_rect;
}
495
496
497
if (rect->x < 0 || rect->x+rect->w > surface->w ||
rect->y < 0 || rect->y+rect->h > surface->h) {
SDL_SetError("Tried to read outside of surface bounds");
498
499
500
return -1;
}
501
src_format = surface->format->format;
502
503
504
505
506
507
508
src_pixels = (void*)((Uint8 *) surface->pixels +
rect->y * surface->pitch +
rect->x * surface->format->BytesPerPixel);
return SDL_ConvertPixels(rect->w, rect->h,
src_format, src_pixels, surface->pitch,
format, pixels, pitch);
509
510
}
511
static void
512
SW_RenderPresent(SDL_Renderer * renderer)
513
{
514
SDL_Window *window = renderer->window;
515
516
517
if (window) {
SDL_UpdateWindowSurface(window);
518
}
519
520
521
}
static void
522
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
523
{
524
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
525
526
SDL_FreeSurface(surface);
527
528
529
}
static void
530
SW_DestroyRenderer(SDL_Renderer * renderer)
531
{
532
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
533
534
535
536
537
538
539
if (data) {
SDL_free(data);
}
SDL_free(renderer);
}
540
541
#endif /* !SDL_RENDER_DISABLED */
542
/* vi: set ts=4 sw=4 expandtab: */