SDL_blit.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifndef SDL_blit_h_
  20. #define SDL_blit_h_
  21. // Table to do pixel byte expansion
  22. extern const Uint8 *SDL_expand_byte[9];
  23. extern const Uint16 SDL_expand_byte_10[];
  24. // SDL blit copy flags
  25. #define SDL_COPY_MODULATE_COLOR 0x00000001
  26. #define SDL_COPY_MODULATE_ALPHA 0x00000002
  27. #define SDL_COPY_MODULATE_MASK (SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA)
  28. #define SDL_COPY_BLEND 0x00000010
  29. #define SDL_COPY_BLEND_PREMULTIPLIED 0x00000020
  30. #define SDL_COPY_ADD 0x00000040
  31. #define SDL_COPY_ADD_PREMULTIPLIED 0x00000080
  32. #define SDL_COPY_MOD 0x00000100
  33. #define SDL_COPY_MUL 0x00000200
  34. #define SDL_COPY_BLEND_MASK (SDL_COPY_BLEND | SDL_COPY_BLEND_PREMULTIPLIED | SDL_COPY_ADD | SDL_COPY_ADD_PREMULTIPLIED | SDL_COPY_MOD | SDL_COPY_MUL)
  35. #define SDL_COPY_COLORKEY 0x00000400
  36. #define SDL_COPY_NEAREST 0x00000800
  37. #define SDL_COPY_RLE_DESIRED 0x00001000
  38. #define SDL_COPY_RLE_COLORKEY 0x00002000
  39. #define SDL_COPY_RLE_ALPHAKEY 0x00004000
  40. #define SDL_COPY_RLE_MASK (SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY | SDL_COPY_RLE_ALPHAKEY)
  41. // SDL blit CPU flags
  42. #define SDL_CPU_ANY 0x00000000
  43. #define SDL_CPU_MMX 0x00000001
  44. #define SDL_CPU_SSE 0x00000002
  45. #define SDL_CPU_SSE2 0x00000004
  46. #define SDL_CPU_ALTIVEC_PREFETCH 0x00000008
  47. #define SDL_CPU_ALTIVEC_NOPREFETCH 0x00000010
  48. typedef struct
  49. {
  50. SDL_Surface *src_surface;
  51. Uint8 *src;
  52. int src_w, src_h;
  53. int src_pitch;
  54. int src_skip;
  55. int leading_skip;
  56. SDL_Surface *dst_surface;
  57. Uint8 *dst;
  58. int dst_w, dst_h;
  59. int dst_pitch;
  60. int dst_skip;
  61. const SDL_PixelFormatDetails *src_fmt;
  62. const SDL_Palette *src_pal;
  63. const SDL_PixelFormatDetails *dst_fmt;
  64. const SDL_Palette *dst_pal;
  65. Uint8 *table;
  66. SDL_HashTable *palette_map;
  67. int flags;
  68. Uint32 colorkey;
  69. Uint8 r, g, b, a;
  70. } SDL_BlitInfo;
  71. typedef void (*SDL_BlitFunc)(SDL_BlitInfo *info);
  72. typedef struct
  73. {
  74. SDL_PixelFormat src_format;
  75. SDL_PixelFormat dst_format;
  76. int flags;
  77. unsigned int cpu;
  78. SDL_BlitFunc func;
  79. } SDL_BlitFuncEntry;
  80. typedef bool (SDLCALL *SDL_Blit) (struct SDL_Surface *src, const SDL_Rect *srcrect, struct SDL_Surface *dst, const SDL_Rect *dstrect);
  81. // Blit mapping definition
  82. typedef struct SDL_BlitMap
  83. {
  84. int identity;
  85. SDL_Blit blit;
  86. void *data;
  87. SDL_BlitInfo info;
  88. /* the version count matches the destination; mismatch indicates
  89. an invalid mapping */
  90. Uint32 dst_palette_version;
  91. Uint32 src_palette_version;
  92. } SDL_BlitMap;
  93. // Functions found in SDL_blit.c
  94. extern bool SDL_CalculateBlit(SDL_Surface *surface, SDL_Surface *dst);
  95. /* Functions found in SDL_blit_*.c */
  96. extern SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface *surface);
  97. extern SDL_BlitFunc SDL_CalculateBlit1(SDL_Surface *surface);
  98. extern SDL_BlitFunc SDL_CalculateBlitN(SDL_Surface *surface);
  99. extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface *surface);
  100. /*
  101. * Useful macros for blitting routines
  102. */
  103. #ifdef __GNUC__
  104. #define DECLARE_ALIGNED(t, v, a) t __attribute__((aligned(a))) v
  105. #elif defined(_MSC_VER)
  106. #define DECLARE_ALIGNED(t, v, a) __declspec(align(a)) t v
  107. #else
  108. #define DECLARE_ALIGNED(t, v, a) t v
  109. #endif
  110. // Load pixel of the specified format from a buffer and get its R-G-B values
  111. #define RGB_FROM_PIXEL(Pixel, fmt, r, g, b) \
  112. { \
  113. r = SDL_expand_byte[fmt->Rbits][((Pixel & fmt->Rmask) >> fmt->Rshift)]; \
  114. g = SDL_expand_byte[fmt->Gbits][((Pixel & fmt->Gmask) >> fmt->Gshift)]; \
  115. b = SDL_expand_byte[fmt->Bbits][((Pixel & fmt->Bmask) >> fmt->Bshift)]; \
  116. }
  117. #define RGB_FROM_RGB565(Pixel, r, g, b) \
  118. { \
  119. r = SDL_expand_byte[5][((Pixel & 0xF800) >> 11)]; \
  120. g = SDL_expand_byte[6][((Pixel & 0x07E0) >> 5)]; \
  121. b = SDL_expand_byte[5][(Pixel & 0x001F)]; \
  122. }
  123. #define RGB_FROM_RGB555(Pixel, r, g, b) \
  124. { \
  125. r = SDL_expand_byte[5][((Pixel & 0x7C00) >> 10)]; \
  126. g = SDL_expand_byte[5][((Pixel & 0x03E0) >> 5)]; \
  127. b = SDL_expand_byte[5][(Pixel & 0x001F)]; \
  128. }
  129. #define RGB_FROM_XRGB8888(Pixel, r, g, b) \
  130. { \
  131. r = ((Pixel & 0xFF0000) >> 16); \
  132. g = ((Pixel & 0xFF00) >> 8); \
  133. b = (Pixel & 0xFF); \
  134. }
  135. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  136. #define GET_RGB24(B) (B[2] << 16) | (B[1] << 8) | B[0]
  137. #else
  138. #define GET_RGB24(B) (B[0] << 16) | (B[1] << 8) | B[2]
  139. #endif
  140. #define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel) \
  141. do { \
  142. switch (bpp) { \
  143. case 1: \
  144. Pixel = *((Uint8 *)(buf)); \
  145. break; \
  146. \
  147. case 2: \
  148. Pixel = *((Uint16 *)(buf)); \
  149. break; \
  150. \
  151. case 3: \
  152. { \
  153. Uint8 *B = (Uint8 *)(buf); \
  154. Pixel = GET_RGB24(B); \
  155. } break; \
  156. \
  157. case 4: \
  158. Pixel = *((Uint32 *)(buf)); \
  159. break; \
  160. \
  161. default: \
  162. Pixel = 0; /* stop gcc complaints */ \
  163. break; \
  164. } \
  165. } while (0)
  166. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  167. #define GET_RGB24_COMPONENT(buf, fmt, shift) *((buf) + fmt->shift / 8)
  168. #else
  169. #define GET_RGB24_COMPONENT(buf, fmt, shift) *((buf) + 2 - fmt->shift / 8)
  170. #endif
  171. #define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b) \
  172. do { \
  173. switch (bpp) { \
  174. case 1: \
  175. Pixel = *((Uint8 *)(buf)); \
  176. RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \
  177. break; \
  178. \
  179. case 2: \
  180. Pixel = *((Uint16 *)(buf)); \
  181. RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \
  182. break; \
  183. \
  184. case 3: \
  185. { \
  186. Pixel = 0; \
  187. r = GET_RGB24_COMPONENT(buf, fmt, Rshift); \
  188. g = GET_RGB24_COMPONENT(buf, fmt, Gshift); \
  189. b = GET_RGB24_COMPONENT(buf, fmt, Bshift); \
  190. } break; \
  191. \
  192. case 4: \
  193. Pixel = *((Uint32 *)(buf)); \
  194. RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \
  195. break; \
  196. \
  197. default: \
  198. /* stop gcc complaints */ \
  199. Pixel = 0; \
  200. r = g = b = 0; \
  201. break; \
  202. } \
  203. } while (0)
  204. // Assemble R-G-B values into a specified pixel format and store them
  205. #define PIXEL_FROM_RGB(Pixel, fmt, r, g, b) \
  206. { \
  207. Pixel = ((r >> (8 - fmt->Rbits)) << fmt->Rshift) | \
  208. ((g >> (8 - fmt->Gbits)) << fmt->Gshift) | \
  209. ((b >> (8 - fmt->Bbits)) << fmt->Bshift) | \
  210. fmt->Amask; \
  211. }
  212. #define RGB332_FROM_RGB(Pixel, r, g, b) \
  213. { \
  214. Pixel = (Uint8)(((r >> 5) << 5) | ((g >> 5) << 2) | (b >> 6)); \
  215. }
  216. #define RGB565_FROM_RGB(Pixel, r, g, b) \
  217. { \
  218. Pixel = (Uint16)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3)); \
  219. }
  220. #define RGB555_FROM_RGB(Pixel, r, g, b) \
  221. { \
  222. Pixel = (Uint16)(((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3)); \
  223. }
  224. #define XRGB8888_FROM_RGB(Pixel, r, g, b) \
  225. { \
  226. Pixel = (r << 16) | (g << 8) | b; \
  227. }
  228. #define ARGB8888_FROM_RGBA(Pixel, r, g, b, a) \
  229. { \
  230. Pixel = (a << 24) | (r << 16) | (g << 8) | b; \
  231. }
  232. #define RGBA8888_FROM_RGBA(Pixel, r, g, b, a) \
  233. { \
  234. Pixel = (r << 24) | (g << 16) | (b << 8) | a; \
  235. }
  236. #define ABGR8888_FROM_RGBA(Pixel, r, g, b, a) \
  237. { \
  238. Pixel = (a << 24) | (b << 16) | (g << 8) | r; \
  239. }
  240. #define BGRA8888_FROM_RGBA(Pixel, r, g, b, a) \
  241. { \
  242. Pixel = (b << 24) | (g << 16) | (r << 8) | a; \
  243. }
  244. #define ARGB2101010_FROM_RGBA(Pixel, r, g, b, a) \
  245. { \
  246. r = r ? ((r << 2) | 0x3) : 0; \
  247. g = g ? ((g << 2) | 0x3) : 0; \
  248. b = b ? ((b << 2) | 0x3) : 0; \
  249. a = (a * 3) / 255; \
  250. Pixel = (a << 30) | (r << 20) | (g << 10) | b; \
  251. }
  252. #define ARGB2101010_FROM_RGBAFLOAT(Pixel, r, g, b, a) \
  253. { \
  254. r = SDL_clamp(r, 0.0f, 1.0f) * 1023.0f; \
  255. g = SDL_clamp(g, 0.0f, 1.0f) * 1023.0f; \
  256. b = SDL_clamp(b, 0.0f, 1.0f) * 1023.0f; \
  257. a = SDL_clamp(a, 0.0f, 1.0f) * 3.0f; \
  258. Pixel = (((Uint32)SDL_roundf(a)) << 30) | \
  259. (((Uint32)SDL_roundf(r)) << 20) | \
  260. (((Uint32)SDL_roundf(g)) << 10) | \
  261. (Uint32)SDL_roundf(b); \
  262. }
  263. #define ABGR2101010_FROM_RGBA(Pixel, r, g, b, a) \
  264. { \
  265. r = r ? ((r << 2) | 0x3) : 0; \
  266. g = g ? ((g << 2) | 0x3) : 0; \
  267. b = b ? ((b << 2) | 0x3) : 0; \
  268. a = (a * 3) / 255; \
  269. Pixel = (a << 30) | (b << 20) | (g << 10) | r; \
  270. }
  271. #define ABGR2101010_FROM_RGBAFLOAT(Pixel, r, g, b, a) \
  272. { \
  273. r = SDL_clamp(r, 0.0f, 1.0f) * 1023.0f; \
  274. g = SDL_clamp(g, 0.0f, 1.0f) * 1023.0f; \
  275. b = SDL_clamp(b, 0.0f, 1.0f) * 1023.0f; \
  276. a = SDL_clamp(a, 0.0f, 1.0f) * 3.0f; \
  277. Pixel = (((Uint32)SDL_roundf(a)) << 30) | \
  278. (((Uint32)SDL_roundf(b)) << 20) | \
  279. (((Uint32)SDL_roundf(g)) << 10) | \
  280. (Uint32)SDL_roundf(r); \
  281. }
  282. #define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) \
  283. { \
  284. switch (bpp) { \
  285. case 1: \
  286. { \
  287. Uint8 _pixel; \
  288. \
  289. PIXEL_FROM_RGB(_pixel, fmt, r, g, b); \
  290. *((Uint8 *)(buf)) = _pixel; \
  291. } break; \
  292. \
  293. case 2: \
  294. { \
  295. Uint16 _pixel; \
  296. \
  297. PIXEL_FROM_RGB(_pixel, fmt, r, g, b); \
  298. *((Uint16 *)(buf)) = _pixel; \
  299. } break; \
  300. \
  301. case 3: \
  302. { \
  303. GET_RGB24_COMPONENT(buf, fmt, Rshift) = r; \
  304. GET_RGB24_COMPONENT(buf, fmt, Gshift) = g; \
  305. GET_RGB24_COMPONENT(buf, fmt, Bshift) = b; \
  306. } break; \
  307. \
  308. case 4: \
  309. { \
  310. Uint32 _pixel; \
  311. \
  312. PIXEL_FROM_RGB(_pixel, fmt, r, g, b); \
  313. *((Uint32 *)(buf)) = _pixel; \
  314. } break; \
  315. } \
  316. }
  317. // FIXME: Should we rescale alpha into 0..255 here?
  318. #define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a) \
  319. { \
  320. r = SDL_expand_byte[fmt->Rbits][((Pixel & fmt->Rmask) >> fmt->Rshift)]; \
  321. g = SDL_expand_byte[fmt->Gbits][((Pixel & fmt->Gmask) >> fmt->Gshift)]; \
  322. b = SDL_expand_byte[fmt->Bbits][((Pixel & fmt->Bmask) >> fmt->Bshift)]; \
  323. a = SDL_expand_byte[fmt->Abits][((Pixel & fmt->Amask) >> fmt->Ashift)]; \
  324. }
  325. #define RGBA_FROM_8888(Pixel, fmt, r, g, b, a) \
  326. { \
  327. r = (Pixel & fmt->Rmask) >> fmt->Rshift; \
  328. g = (Pixel & fmt->Gmask) >> fmt->Gshift; \
  329. b = (Pixel & fmt->Bmask) >> fmt->Bshift; \
  330. a = (Pixel & fmt->Amask) >> fmt->Ashift; \
  331. }
  332. #define RGBA_FROM_RGBA8888(Pixel, r, g, b, a) \
  333. { \
  334. r = (Pixel >> 24); \
  335. g = ((Pixel >> 16) & 0xFF); \
  336. b = ((Pixel >> 8) & 0xFF); \
  337. a = (Pixel & 0xFF); \
  338. }
  339. #define RGBA_FROM_ARGB8888(Pixel, r, g, b, a) \
  340. { \
  341. r = ((Pixel >> 16) & 0xFF); \
  342. g = ((Pixel >> 8) & 0xFF); \
  343. b = (Pixel & 0xFF); \
  344. a = (Pixel >> 24); \
  345. }
  346. #define RGBA_FROM_ABGR8888(Pixel, r, g, b, a) \
  347. { \
  348. r = (Pixel & 0xFF); \
  349. g = ((Pixel >> 8) & 0xFF); \
  350. b = ((Pixel >> 16) & 0xFF); \
  351. a = (Pixel >> 24); \
  352. }
  353. #define RGBA_FROM_BGRA8888(Pixel, r, g, b, a) \
  354. { \
  355. r = ((Pixel >> 8) & 0xFF); \
  356. g = ((Pixel >> 16) & 0xFF); \
  357. b = (Pixel >> 24); \
  358. a = (Pixel & 0xFF); \
  359. }
  360. #define RGBA_FROM_ARGB2101010(Pixel, r, g, b, a) \
  361. { \
  362. r = ((Pixel >> 22) & 0xFF); \
  363. g = ((Pixel >> 12) & 0xFF); \
  364. b = ((Pixel >> 2) & 0xFF); \
  365. a = SDL_expand_byte[2][(Pixel >> 30)]; \
  366. }
  367. #define RGBAFLOAT_FROM_ARGB2101010(Pixel, r, g, b, a) \
  368. { \
  369. r = (float)((Pixel >> 20) & 0x3FF) / 1023.0f; \
  370. g = (float)((Pixel >> 10) & 0x3FF) / 1023.0f; \
  371. b = (float)((Pixel >> 0) & 0x3FF) / 1023.0f; \
  372. a = (float)(Pixel >> 30) / 3.0f; \
  373. }
  374. #define RGBA_FROM_ABGR2101010(Pixel, r, g, b, a) \
  375. { \
  376. r = ((Pixel >> 2) & 0xFF); \
  377. g = ((Pixel >> 12) & 0xFF); \
  378. b = ((Pixel >> 22) & 0xFF); \
  379. a = SDL_expand_byte[2][(Pixel >> 30)]; \
  380. }
  381. #define RGBAFLOAT_FROM_ABGR2101010(Pixel, r, g, b, a) \
  382. { \
  383. r = (float)((Pixel >> 0) & 0x3FF) / 1023.0f; \
  384. g = (float)((Pixel >> 10) & 0x3FF) / 1023.0f; \
  385. b = (float)((Pixel >> 20) & 0x3FF) / 1023.0f; \
  386. a = (float)(Pixel >> 30) / 3.0f; \
  387. }
  388. #define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a) \
  389. do { \
  390. switch (bpp) { \
  391. case 1: \
  392. Pixel = *((Uint8 *)(buf)); \
  393. RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \
  394. break; \
  395. \
  396. case 2: \
  397. Pixel = *((Uint16 *)(buf)); \
  398. RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \
  399. break; \
  400. \
  401. case 3: \
  402. { \
  403. Pixel = 0; \
  404. r = GET_RGB24_COMPONENT(buf, fmt, Rshift); \
  405. g = GET_RGB24_COMPONENT(buf, fmt, Gshift); \
  406. b = GET_RGB24_COMPONENT(buf, fmt, Bshift); \
  407. a = 0xFF; \
  408. } break; \
  409. \
  410. case 4: \
  411. Pixel = *((Uint32 *)(buf)); \
  412. RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \
  413. break; \
  414. \
  415. default: \
  416. /* stop gcc complaints */ \
  417. Pixel = 0; \
  418. r = g = b = a = 0; \
  419. break; \
  420. } \
  421. } while (0)
  422. // FIXME: this isn't correct, especially for Alpha (maximum != 255)
  423. #define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a) \
  424. { \
  425. Pixel = ((r >> (8 - fmt->Rbits)) << fmt->Rshift) | \
  426. ((g >> (8 - fmt->Gbits)) << fmt->Gshift) | \
  427. ((b >> (8 - fmt->Bbits)) << fmt->Bshift) | \
  428. ((a >> (8 - fmt->Abits)) << fmt->Ashift); \
  429. }
  430. #define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a) \
  431. { \
  432. switch (bpp) { \
  433. case 1: \
  434. { \
  435. Uint8 _pixel; \
  436. \
  437. PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a); \
  438. *((Uint8 *)(buf)) = _pixel; \
  439. } break; \
  440. \
  441. case 2: \
  442. { \
  443. Uint16 _pixel; \
  444. \
  445. PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a); \
  446. *((Uint16 *)(buf)) = _pixel; \
  447. } break; \
  448. \
  449. case 3: \
  450. { \
  451. GET_RGB24_COMPONENT(buf, fmt, Rshift) = r; \
  452. GET_RGB24_COMPONENT(buf, fmt, Gshift) = g; \
  453. GET_RGB24_COMPONENT(buf, fmt, Bshift) = b; \
  454. } break; \
  455. \
  456. case 4: \
  457. { \
  458. Uint32 _pixel; \
  459. \
  460. PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a); \
  461. *((Uint32 *)(buf)) = _pixel; \
  462. } break; \
  463. } \
  464. }
  465. // Convert any 32-bit 4-bpp pixel to ARGB format
  466. #define PIXEL_TO_ARGB_PIXEL(src, srcfmt, dst) \
  467. do { \
  468. Uint8 a, r, g, b; \
  469. RGBA_FROM_PIXEL(src, srcfmt, r, g, b, a); \
  470. dst = a << 24 | r << 16 | g << 8 | b; \
  471. } while (0)
  472. // Blend a single color channel or alpha value
  473. /* dC = ((sC * sA) + (dC * (255 - sA))) / 255 */
  474. #define ALPHA_BLEND_CHANNEL(sC, dC, sA) \
  475. do { \
  476. Uint16 x; \
  477. x = ((sC - dC) * sA) + ((dC << 8) - dC); \
  478. x += 0x1U; \
  479. x += x >> 8; \
  480. dC = x >> 8; \
  481. } while (0)
  482. // Perform a division by 255 after a multiplication of two 8-bit color channels
  483. /* out = (sC * dC) / 255 */
  484. #define MULT_DIV_255(sC, dC, out) \
  485. do { \
  486. Uint16 x = sC * dC; \
  487. x += 0x1U; \
  488. x += x >> 8; \
  489. out = x >> 8; \
  490. } while (0)
  491. // Blend the RGB values of two pixels with an alpha value
  492. #define ALPHA_BLEND_RGB(sR, sG, sB, A, dR, dG, dB) \
  493. do { \
  494. ALPHA_BLEND_CHANNEL(sR, dR, A); \
  495. ALPHA_BLEND_CHANNEL(sG, dG, A); \
  496. ALPHA_BLEND_CHANNEL(sB, dB, A); \
  497. } while (0)
  498. // Blend two 8888 pixels with the same format
  499. /* Calculates dst = ((src * factor) + (dst * (255 - factor))) / 255 */
  500. // FIXME: SDL_SIZE_MAX might not be an integer literal
  501. #if defined(SIZE_MAX) && (SIZE_MAX == 0xffffffffffffffff)
  502. #define FACTOR_BLEND_8888(src, dst, factor) \
  503. do { \
  504. Uint64 src64 = src; \
  505. src64 = (src64 | (src64 << 24)) & 0x00FF00FF00FF00FF; \
  506. \
  507. Uint64 dst64 = dst; \
  508. dst64 = (dst64 | (dst64 << 24)) & 0x00FF00FF00FF00FF; \
  509. \
  510. dst64 = ((src64 - dst64) * factor) + (dst64 << 8) - dst64; \
  511. dst64 += 0x0001000100010001; \
  512. dst64 += (dst64 >> 8) & 0x00FF00FF00FF00FF; \
  513. dst64 &= 0xFF00FF00FF00FF00; \
  514. \
  515. dst = (Uint32)((dst64 >> 8) | (dst64 >> 32)); \
  516. } while (0)
  517. #else
  518. #define FACTOR_BLEND_8888(src, dst, factor) \
  519. do { \
  520. Uint32 src02 = src & 0x00FF00FF; \
  521. Uint32 dst02 = dst & 0x00FF00FF; \
  522. \
  523. Uint32 src13 = (src >> 8) & 0x00FF00FF; \
  524. Uint32 dst13 = (dst >> 8) & 0x00FF00FF; \
  525. \
  526. Uint32 res02 = ((src02 - dst02) * factor) + (dst02 << 8) - dst02; \
  527. res02 += 0x00010001; \
  528. res02 += (res02 >> 8) & 0x00FF00FF; \
  529. res02 = (res02 >> 8) & 0x00FF00FF; \
  530. \
  531. Uint32 res13 = ((src13 - dst13) * factor) + (dst13 << 8) - dst13; \
  532. res13 += 0x00010001; \
  533. res13 += (res13 >> 8) & 0x00FF00FF; \
  534. res13 &= 0xFF00FF00; \
  535. dst = res02 | res13; \
  536. } while (0)
  537. #endif
  538. // Alpha blend two 8888 pixels with the same formats.
  539. #define ALPHA_BLEND_8888(src, dst, fmt) \
  540. do { \
  541. Uint32 srcA = (src >> fmt->Ashift) & 0xFF; \
  542. Uint32 tmp = src | fmt->Amask; \
  543. FACTOR_BLEND_8888(tmp, dst, srcA); \
  544. } while (0)
  545. // Alpha blend two 8888 pixels with differing formats.
  546. #define ALPHA_BLEND_SWIZZLE_8888(src, dst, srcfmt, dstfmt) \
  547. do { \
  548. Uint32 srcA = (src >> srcfmt->Ashift) & 0xFF; \
  549. Uint32 tmp = (((src >> srcfmt->Rshift) & 0xFF) << dstfmt->Rshift) | \
  550. (((src >> srcfmt->Gshift) & 0xFF) << dstfmt->Gshift) | \
  551. (((src >> srcfmt->Bshift) & 0xFF) << dstfmt->Bshift) | \
  552. dstfmt->Amask; \
  553. FACTOR_BLEND_8888(tmp, dst, srcA); \
  554. } while (0)
  555. // Blend the RGBA values of two pixels
  556. #define ALPHA_BLEND_RGBA(sR, sG, sB, sA, dR, dG, dB, dA) \
  557. do { \
  558. ALPHA_BLEND_CHANNEL(sR, dR, sA); \
  559. ALPHA_BLEND_CHANNEL(sG, dG, sA); \
  560. ALPHA_BLEND_CHANNEL(sB, dB, sA); \
  561. ALPHA_BLEND_CHANNEL(255, dA, sA); \
  562. } while (0)
  563. // This is a very useful loop for optimizing blitters
  564. #if defined(_MSC_VER) && (_MSC_VER == 1300)
  565. // There's a bug in the Visual C++ 7 optimizer when compiling this code
  566. #else
  567. #define USE_DUFFS_LOOP
  568. #endif
  569. #define DUFFS_LOOP1(pixel_copy_increment, width) \
  570. { \
  571. int n; \
  572. for (n = width; n > 0; --n) { \
  573. pixel_copy_increment; \
  574. } \
  575. }
  576. #ifdef USE_DUFFS_LOOP
  577. // 8-times unrolled loop
  578. #define DUFFS_LOOP8(pixel_copy_increment, width) \
  579. { \
  580. int n = (width + 7) / 8; \
  581. switch (width & 7) { \
  582. case 0: \
  583. do { \
  584. pixel_copy_increment; \
  585. SDL_FALLTHROUGH; \
  586. case 7: \
  587. pixel_copy_increment; \
  588. SDL_FALLTHROUGH; \
  589. case 6: \
  590. pixel_copy_increment; \
  591. SDL_FALLTHROUGH; \
  592. case 5: \
  593. pixel_copy_increment; \
  594. SDL_FALLTHROUGH; \
  595. case 4: \
  596. pixel_copy_increment; \
  597. SDL_FALLTHROUGH; \
  598. case 3: \
  599. pixel_copy_increment; \
  600. SDL_FALLTHROUGH; \
  601. case 2: \
  602. pixel_copy_increment; \
  603. SDL_FALLTHROUGH; \
  604. case 1: \
  605. pixel_copy_increment; \
  606. } while (--n > 0); \
  607. } \
  608. }
  609. // 4-times unrolled loop
  610. #define DUFFS_LOOP4(pixel_copy_increment, width) \
  611. { \
  612. int n = (width + 3) / 4; \
  613. switch (width & 3) { \
  614. case 0: \
  615. do { \
  616. pixel_copy_increment; \
  617. SDL_FALLTHROUGH; \
  618. case 3: \
  619. pixel_copy_increment; \
  620. SDL_FALLTHROUGH; \
  621. case 2: \
  622. pixel_copy_increment; \
  623. SDL_FALLTHROUGH; \
  624. case 1: \
  625. pixel_copy_increment; \
  626. } while (--n > 0); \
  627. } \
  628. }
  629. // 2-times unrolled loop
  630. #define DUFFS_LOOP2(pixel_copy_increment, width) \
  631. { \
  632. int n = (width + 1) / 2; \
  633. switch (width & 1) { \
  634. case 0: \
  635. do { \
  636. pixel_copy_increment; \
  637. SDL_FALLTHROUGH; \
  638. case 1: \
  639. pixel_copy_increment; \
  640. } while (--n > 0); \
  641. } \
  642. }
  643. // Use the 4-times version of the loop by default
  644. #define DUFFS_LOOP(pixel_copy_increment, width) \
  645. DUFFS_LOOP4(pixel_copy_increment, width)
  646. // Use the 8-times version of the loop for simple routines
  647. #define DUFFS_LOOP_TRIVIAL(pixel_copy_increment, width) \
  648. DUFFS_LOOP8(pixel_copy_increment, width)
  649. // Special version of Duff's device for even more optimization
  650. #define DUFFS_LOOP_124(pixel_copy_increment1, \
  651. pixel_copy_increment2, \
  652. pixel_copy_increment4, width) \
  653. { \
  654. int n = width; \
  655. if (n & 1) { \
  656. pixel_copy_increment1; \
  657. n -= 1; \
  658. } \
  659. if (n & 2) { \
  660. pixel_copy_increment2; \
  661. n -= 2; \
  662. } \
  663. if (n & 4) { \
  664. pixel_copy_increment4; \
  665. n -= 4; \
  666. } \
  667. if (n) { \
  668. n /= 8; \
  669. do { \
  670. pixel_copy_increment4; \
  671. pixel_copy_increment4; \
  672. } while (--n > 0); \
  673. } \
  674. }
  675. #else
  676. // Don't use Duff's device to unroll loops
  677. #define DUFFS_LOOP(pixel_copy_increment, width) \
  678. DUFFS_LOOP1(pixel_copy_increment, width)
  679. #define DUFFS_LOOP_TRIVIAL(pixel_copy_increment, width) \
  680. DUFFS_LOOP1(pixel_copy_increment, width)
  681. #define DUFFS_LOOP8(pixel_copy_increment, width) \
  682. DUFFS_LOOP1(pixel_copy_increment, width)
  683. #define DUFFS_LOOP4(pixel_copy_increment, width) \
  684. DUFFS_LOOP1(pixel_copy_increment, width)
  685. #define DUFFS_LOOP2(pixel_copy_increment, width) \
  686. DUFFS_LOOP1(pixel_copy_increment, width)
  687. #define DUFFS_LOOP_124(pixel_copy_increment1, \
  688. pixel_copy_increment2, \
  689. pixel_copy_increment4, width) \
  690. DUFFS_LOOP1(pixel_copy_increment1, width)
  691. #endif // USE_DUFFS_LOOP
  692. #if defined(_MSC_VER) && (_MSC_VER >= 600)
  693. #pragma warning(disable : 4244) // '=': conversion from 'X' to 'Y', possible loss of data
  694. #endif
  695. #endif // SDL_blit_h_