SDL 2.0
SDL_blit.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "../SDL_internal.h"
22
23#ifndef SDL_blit_h_
24#define SDL_blit_h_
25
26#include "SDL_cpuinfo.h"
27#include "SDL_endian.h"
28#include "SDL_surface.h"
29
30/* Table to do pixel byte expansion */
31extern Uint8* SDL_expand_byte[9];
32
33/* SDL blit copy flags */
34#define SDL_COPY_MODULATE_COLOR 0x00000001
35#define SDL_COPY_MODULATE_ALPHA 0x00000002
36#define SDL_COPY_BLEND 0x00000010
37#define SDL_COPY_ADD 0x00000020
38#define SDL_COPY_MOD 0x00000040
39#define SDL_COPY_COLORKEY 0x00000100
40#define SDL_COPY_NEAREST 0x00000200
41#define SDL_COPY_RLE_DESIRED 0x00001000
42#define SDL_COPY_RLE_COLORKEY 0x00002000
43#define SDL_COPY_RLE_ALPHAKEY 0x00004000
44#define SDL_COPY_RLE_MASK (SDL_COPY_RLE_DESIRED|SDL_COPY_RLE_COLORKEY|SDL_COPY_RLE_ALPHAKEY)
45
46/* SDL blit CPU flags */
47#define SDL_CPU_ANY 0x00000000
48#define SDL_CPU_MMX 0x00000001
49#define SDL_CPU_3DNOW 0x00000002
50#define SDL_CPU_SSE 0x00000004
51#define SDL_CPU_SSE2 0x00000008
52#define SDL_CPU_ALTIVEC_PREFETCH 0x00000010
53#define SDL_CPU_ALTIVEC_NOPREFETCH 0x00000020
54
55typedef struct
56{
58 int src_w, src_h;
62 int dst_w, dst_h;
68 int flags;
70 Uint8 r, g, b, a;
72
73typedef void (*SDL_BlitFunc) (SDL_BlitInfo *info);
74
75
76typedef struct
77{
80 int flags;
81 int cpu;
84
85/* Blit mapping definition */
86typedef struct SDL_BlitMap
87{
91 void *data;
93
94 /* the version count matches the destination; mismatch indicates
95 an invalid mapping */
99
100/* Functions found in SDL_blit.c */
102
103/* Functions found in SDL_blit_*.c */
108
109/*
110 * Useful macros for blitting routines
111 */
112
113#if defined(__GNUC__)
114#define DECLARE_ALIGNED(t,v,a) t __attribute__((aligned(a))) v
115#elif defined(_MSC_VER)
116#define DECLARE_ALIGNED(t,v,a) __declspec(align(a)) t v
117#else
118#define DECLARE_ALIGNED(t,v,a) t v
119#endif
120
121/* Load pixel of the specified format from a buffer and get its R-G-B values */
122#define RGB_FROM_PIXEL(Pixel, fmt, r, g, b) \
123{ \
124 r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
125 g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
126 b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
127}
128#define RGB_FROM_RGB565(Pixel, r, g, b) \
129{ \
130 r = SDL_expand_byte[3][((Pixel&0xF800)>>11)]; \
131 g = SDL_expand_byte[2][((Pixel&0x07E0)>>5)]; \
132 b = SDL_expand_byte[3][(Pixel&0x001F)]; \
133}
134#define RGB_FROM_RGB555(Pixel, r, g, b) \
135{ \
136 r = SDL_expand_byte[3][((Pixel&0x7C00)>>10)]; \
137 g = SDL_expand_byte[3][((Pixel&0x03E0)>>5)]; \
138 b = SDL_expand_byte[3][(Pixel&0x001F)]; \
139}
140#define RGB_FROM_RGB888(Pixel, r, g, b) \
141{ \
142 r = ((Pixel&0xFF0000)>>16); \
143 g = ((Pixel&0xFF00)>>8); \
144 b = (Pixel&0xFF); \
145}
146#define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel) \
147do { \
148 switch (bpp) { \
149 case 1: \
150 Pixel = *((Uint8 *)(buf)); \
151 break; \
152 \
153 case 2: \
154 Pixel = *((Uint16 *)(buf)); \
155 break; \
156 \
157 case 3: { \
158 Uint8 *B = (Uint8 *)(buf); \
159 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
160 Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
161 } else { \
162 Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
163 } \
164 } \
165 break; \
166 \
167 case 4: \
168 Pixel = *((Uint32 *)(buf)); \
169 break; \
170 \
171 default: \
172 Pixel = 0; /* stop gcc complaints */ \
173 break; \
174 } \
175} while (0)
176
177#define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b) \
178do { \
179 switch (bpp) { \
180 case 1: \
181 Pixel = *((Uint8 *)(buf)); \
182 RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \
183 break; \
184 \
185 case 2: \
186 Pixel = *((Uint16 *)(buf)); \
187 RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \
188 break; \
189 \
190 case 3: { \
191 Pixel = 0; \
192 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
193 r = *((buf)+fmt->Rshift/8); \
194 g = *((buf)+fmt->Gshift/8); \
195 b = *((buf)+fmt->Bshift/8); \
196 } else { \
197 r = *((buf)+2-fmt->Rshift/8); \
198 g = *((buf)+2-fmt->Gshift/8); \
199 b = *((buf)+2-fmt->Bshift/8); \
200 } \
201 } \
202 break; \
203 \
204 case 4: \
205 Pixel = *((Uint32 *)(buf)); \
206 RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \
207 break; \
208 \
209 default: \
210 /* stop gcc complaints */ \
211 Pixel = 0; \
212 r = g = b = 0; \
213 break; \
214 } \
215} while (0)
216
217/* Assemble R-G-B values into a specified pixel format and store them */
218#define PIXEL_FROM_RGB(Pixel, fmt, r, g, b) \
219{ \
220 Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)| \
221 ((g>>fmt->Gloss)<<fmt->Gshift)| \
222 ((b>>fmt->Bloss)<<fmt->Bshift)| \
223 fmt->Amask; \
224}
225#define RGB565_FROM_RGB(Pixel, r, g, b) \
226{ \
227 Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3); \
228}
229#define RGB555_FROM_RGB(Pixel, r, g, b) \
230{ \
231 Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3); \
232}
233#define RGB888_FROM_RGB(Pixel, r, g, b) \
234{ \
235 Pixel = (r<<16)|(g<<8)|b; \
236}
237#define ARGB8888_FROM_RGBA(Pixel, r, g, b, a) \
238{ \
239 Pixel = (a<<24)|(r<<16)|(g<<8)|b; \
240}
241#define RGBA8888_FROM_RGBA(Pixel, r, g, b, a) \
242{ \
243 Pixel = (r<<24)|(g<<16)|(b<<8)|a; \
244}
245#define ABGR8888_FROM_RGBA(Pixel, r, g, b, a) \
246{ \
247 Pixel = (a<<24)|(b<<16)|(g<<8)|r; \
248}
249#define BGRA8888_FROM_RGBA(Pixel, r, g, b, a) \
250{ \
251 Pixel = (b<<24)|(g<<16)|(r<<8)|a; \
252}
253#define ARGB2101010_FROM_RGBA(Pixel, r, g, b, a) \
254{ \
255 r = r ? ((r << 2) | 0x3) : 0; \
256 g = g ? ((g << 2) | 0x3) : 0; \
257 b = b ? ((b << 2) | 0x3) : 0; \
258 a = (a * 3) / 255; \
259 Pixel = (a<<30)|(r<<20)|(g<<10)|b; \
260}
261#define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) \
262{ \
263 switch (bpp) { \
264 case 1: { \
265 Uint8 _Pixel; \
266 \
267 PIXEL_FROM_RGB(_Pixel, fmt, r, g, b); \
268 *((Uint8 *)(buf)) = _Pixel; \
269 } \
270 break; \
271 \
272 case 2: { \
273 Uint16 _Pixel; \
274 \
275 PIXEL_FROM_RGB(_Pixel, fmt, r, g, b); \
276 *((Uint16 *)(buf)) = _Pixel; \
277 } \
278 break; \
279 \
280 case 3: { \
281 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
282 *((buf)+fmt->Rshift/8) = r; \
283 *((buf)+fmt->Gshift/8) = g; \
284 *((buf)+fmt->Bshift/8) = b; \
285 } else { \
286 *((buf)+2-fmt->Rshift/8) = r; \
287 *((buf)+2-fmt->Gshift/8) = g; \
288 *((buf)+2-fmt->Bshift/8) = b; \
289 } \
290 } \
291 break; \
292 \
293 case 4: { \
294 Uint32 _Pixel; \
295 \
296 PIXEL_FROM_RGB(_Pixel, fmt, r, g, b); \
297 *((Uint32 *)(buf)) = _Pixel; \
298 } \
299 break; \
300 } \
301}
302
303/* FIXME: Should we rescale alpha into 0..255 here? */
304#define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a) \
305{ \
306 r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
307 g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
308 b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
309 a = SDL_expand_byte[fmt->Aloss][((Pixel&fmt->Amask)>>fmt->Ashift)]; \
310}
311#define RGBA_FROM_8888(Pixel, fmt, r, g, b, a) \
312{ \
313 r = (Pixel&fmt->Rmask)>>fmt->Rshift; \
314 g = (Pixel&fmt->Gmask)>>fmt->Gshift; \
315 b = (Pixel&fmt->Bmask)>>fmt->Bshift; \
316 a = (Pixel&fmt->Amask)>>fmt->Ashift; \
317}
318#define RGBA_FROM_RGBA8888(Pixel, r, g, b, a) \
319{ \
320 r = (Pixel>>24); \
321 g = ((Pixel>>16)&0xFF); \
322 b = ((Pixel>>8)&0xFF); \
323 a = (Pixel&0xFF); \
324}
325#define RGBA_FROM_ARGB8888(Pixel, r, g, b, a) \
326{ \
327 r = ((Pixel>>16)&0xFF); \
328 g = ((Pixel>>8)&0xFF); \
329 b = (Pixel&0xFF); \
330 a = (Pixel>>24); \
331}
332#define RGBA_FROM_ABGR8888(Pixel, r, g, b, a) \
333{ \
334 r = (Pixel&0xFF); \
335 g = ((Pixel>>8)&0xFF); \
336 b = ((Pixel>>16)&0xFF); \
337 a = (Pixel>>24); \
338}
339#define RGBA_FROM_BGRA8888(Pixel, r, g, b, a) \
340{ \
341 r = ((Pixel>>8)&0xFF); \
342 g = ((Pixel>>16)&0xFF); \
343 b = (Pixel>>24); \
344 a = (Pixel&0xFF); \
345}
346#define RGBA_FROM_ARGB2101010(Pixel, r, g, b, a) \
347{ \
348 r = ((Pixel>>22)&0xFF); \
349 g = ((Pixel>>12)&0xFF); \
350 b = ((Pixel>>2)&0xFF); \
351 a = SDL_expand_byte[6][(Pixel>>30)]; \
352}
353#define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a) \
354do { \
355 switch (bpp) { \
356 case 1: \
357 Pixel = *((Uint8 *)(buf)); \
358 RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \
359 break; \
360 \
361 case 2: \
362 Pixel = *((Uint16 *)(buf)); \
363 RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \
364 break; \
365 \
366 case 3: { \
367 Pixel = 0; \
368 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
369 r = *((buf)+fmt->Rshift/8); \
370 g = *((buf)+fmt->Gshift/8); \
371 b = *((buf)+fmt->Bshift/8); \
372 } else { \
373 r = *((buf)+2-fmt->Rshift/8); \
374 g = *((buf)+2-fmt->Gshift/8); \
375 b = *((buf)+2-fmt->Bshift/8); \
376 } \
377 a = 0xFF; \
378 } \
379 break; \
380 \
381 case 4: \
382 Pixel = *((Uint32 *)(buf)); \
383 RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \
384 break; \
385 \
386 default: \
387 /* stop gcc complaints */ \
388 Pixel = 0; \
389 r = g = b = a = 0; \
390 break; \
391 } \
392} while (0)
393
394/* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
395#define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a) \
396{ \
397 Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)| \
398 ((g>>fmt->Gloss)<<fmt->Gshift)| \
399 ((b>>fmt->Bloss)<<fmt->Bshift)| \
400 ((a>>fmt->Aloss)<<fmt->Ashift); \
401}
402#define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a) \
403{ \
404 switch (bpp) { \
405 case 1: { \
406 Uint8 _pixel; \
407 \
408 PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a); \
409 *((Uint8 *)(buf)) = _pixel; \
410 } \
411 break; \
412 \
413 case 2: { \
414 Uint16 _pixel; \
415 \
416 PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a); \
417 *((Uint16 *)(buf)) = _pixel; \
418 } \
419 break; \
420 \
421 case 3: { \
422 if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
423 *((buf)+fmt->Rshift/8) = r; \
424 *((buf)+fmt->Gshift/8) = g; \
425 *((buf)+fmt->Bshift/8) = b; \
426 } else { \
427 *((buf)+2-fmt->Rshift/8) = r; \
428 *((buf)+2-fmt->Gshift/8) = g; \
429 *((buf)+2-fmt->Bshift/8) = b; \
430 } \
431 } \
432 break; \
433 \
434 case 4: { \
435 Uint32 _pixel; \
436 \
437 PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a); \
438 *((Uint32 *)(buf)) = _pixel; \
439 } \
440 break; \
441 } \
442}
443
444/* Blend the RGB values of two pixels with an alpha value */
445#define ALPHA_BLEND_RGB(sR, sG, sB, A, dR, dG, dB) \
446do { \
447 dR = (Uint8)((((int)(sR-dR)*(int)A)/255)+dR); \
448 dG = (Uint8)((((int)(sG-dG)*(int)A)/255)+dG); \
449 dB = (Uint8)((((int)(sB-dB)*(int)A)/255)+dB); \
450} while(0)
451
452
453/* Blend the RGBA values of two pixels */
454#define ALPHA_BLEND_RGBA(sR, sG, sB, sA, dR, dG, dB, dA) \
455do { \
456 dR = (Uint8)((((int)(sR-dR)*(int)sA)/255)+dR); \
457 dG = (Uint8)((((int)(sG-dG)*(int)sA)/255)+dG); \
458 dB = (Uint8)((((int)(sB-dB)*(int)sA)/255)+dB); \
459 dA = (Uint8)((int)sA+dA-((int)sA*dA)/255); \
460} while(0)
461
462
463/* This is a very useful loop for optimizing blitters */
464#if defined(_MSC_VER) && (_MSC_VER == 1300)
465/* There's a bug in the Visual C++ 7 optimizer when compiling this code */
466#else
467#define USE_DUFFS_LOOP
468#endif
469#ifdef USE_DUFFS_LOOP
470
471/* 8-times unrolled loop */
472#define DUFFS_LOOP8(pixel_copy_increment, width) \
473{ int n = (width+7)/8; \
474 switch (width & 7) { \
475 case 0: do { pixel_copy_increment; /* fallthrough */ \
476 case 7: pixel_copy_increment; /* fallthrough */ \
477 case 6: pixel_copy_increment; /* fallthrough */ \
478 case 5: pixel_copy_increment; /* fallthrough */ \
479 case 4: pixel_copy_increment; /* fallthrough */ \
480 case 3: pixel_copy_increment; /* fallthrough */ \
481 case 2: pixel_copy_increment; /* fallthrough */ \
482 case 1: pixel_copy_increment; /* fallthrough */ \
483 } while ( --n > 0 ); \
484 } \
485}
486
487/* 4-times unrolled loop */
488#define DUFFS_LOOP4(pixel_copy_increment, width) \
489{ int n = (width+3)/4; \
490 switch (width & 3) { \
491 case 0: do { pixel_copy_increment; /* fallthrough */ \
492 case 3: pixel_copy_increment; /* fallthrough */ \
493 case 2: pixel_copy_increment; /* fallthrough */ \
494 case 1: pixel_copy_increment; /* fallthrough */ \
495 } while (--n > 0); \
496 } \
497}
498
499/* Use the 8-times version of the loop by default */
500#define DUFFS_LOOP(pixel_copy_increment, width) \
501 DUFFS_LOOP8(pixel_copy_increment, width)
502
503/* Special version of Duff's device for even more optimization */
504#define DUFFS_LOOP_124(pixel_copy_increment1, \
505 pixel_copy_increment2, \
506 pixel_copy_increment4, width) \
507{ int n = width; \
508 if (n & 1) { \
509 pixel_copy_increment1; n -= 1; \
510 } \
511 if (n & 2) { \
512 pixel_copy_increment2; n -= 2; \
513 } \
514 if (n & 4) { \
515 pixel_copy_increment4; n -= 4; \
516 } \
517 if (n) { \
518 n /= 8; \
519 do { \
520 pixel_copy_increment4; \
521 pixel_copy_increment4; \
522 } while (--n > 0); \
523 } \
524}
525
526#else
527
528/* Don't use Duff's device to unroll loops */
529#define DUFFS_LOOP(pixel_copy_increment, width) \
530{ int n; \
531 for ( n=width; n > 0; --n ) { \
532 pixel_copy_increment; \
533 } \
534}
535#define DUFFS_LOOP8(pixel_copy_increment, width) \
536 DUFFS_LOOP(pixel_copy_increment, width)
537#define DUFFS_LOOP4(pixel_copy_increment, width) \
538 DUFFS_LOOP(pixel_copy_increment, width)
539#define DUFFS_LOOP_124(pixel_copy_increment1, \
540 pixel_copy_increment2, \
541 pixel_copy_increment4, width) \
542 DUFFS_LOOP(pixel_copy_increment1, width)
543
544#endif /* USE_DUFFS_LOOP */
545
546/* Prevent Visual C++ 6.0 from printing out stupid warnings */
547#if defined(_MSC_VER) && (_MSC_VER >= 600)
548#pragma warning(disable: 4550)
549#endif
550
551#endif /* SDL_blit_h_ */
552
553/* vi: set ts=4 sw=4 expandtab: */
SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface *surface)
Definition: SDL_blit_A.c:1269
int SDL_CalculateBlit(SDL_Surface *surface)
Definition: SDL_blit.c:216
SDL_BlitFunc SDL_CalculateBlitN(SDL_Surface *surface)
Definition: SDL_blit_N.c:3266
SDL_BlitFunc SDL_CalculateBlit1(SDL_Surface *surface)
Definition: SDL_blit_1.c:522
Uint8 * SDL_expand_byte[9]
Definition: SDL_pixels.c:71
void(* SDL_BlitFunc)(SDL_BlitInfo *info)
Definition: SDL_blit.h:73
SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface *surface)
Definition: SDL_blit_0.c:454
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
GLboolean GLboolean GLboolean b
GLboolean GLboolean g
uint32_t Uint32
Definition: SDL_stdinc.h:203
uint8_t Uint8
Definition: SDL_stdinc.h:179
int(* SDL_blit)(struct SDL_Surface *src, SDL_Rect *srcrect, struct SDL_Surface *dst, SDL_Rect *dstrect)
The type of function used for surface blitting functions.
Definition: SDL_surface.h:98
EGLSurface surface
Definition: eglext.h:248
Definition: SDL_blit.h:77
Uint32 src_format
Definition: SDL_blit.h:78
Uint32 dst_format
Definition: SDL_blit.h:79
SDL_BlitFunc func
Definition: SDL_blit.h:82
int flags
Definition: SDL_blit.h:80
int cpu
Definition: SDL_blit.h:81
SDL_PixelFormat * src_fmt
Definition: SDL_blit.h:65
Uint8 * table
Definition: SDL_blit.h:67
int dst_skip
Definition: SDL_blit.h:64
SDL_PixelFormat * dst_fmt
Definition: SDL_blit.h:66
int src_pitch
Definition: SDL_blit.h:59
int dst_pitch
Definition: SDL_blit.h:63
int src_skip
Definition: SDL_blit.h:60
Uint8 a
Definition: SDL_blit.h:70
Uint8 * src
Definition: SDL_blit.h:57
Uint32 colorkey
Definition: SDL_blit.h:69
Uint8 * dst
Definition: SDL_blit.h:61
Uint32 src_palette_version
Definition: SDL_blit.h:97
SDL_blit blit
Definition: SDL_blit.h:90
int identity
Definition: SDL_blit.h:89
SDL_BlitInfo info
Definition: SDL_blit.h:92
void * data
Definition: SDL_blit.h:91
SDL_Surface * dst
Definition: SDL_blit.h:88
Uint32 dst_palette_version
Definition: SDL_blit.h:96
A collection of pixels used in software blitting.
Definition: SDL_surface.h:71