/project/jni/sdl-1.3/src/video/SDL_blit.h

https://github.com/aichunyu/FFPlayer · C Header · 495 lines · 418 code · 32 blank · 45 comment · 37 complexity · 64d191cb0c914f22c031c4ca3d22ac1f MD5 · raw file

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2012 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_config.h"
  19. #ifndef _SDL_blit_h
  20. #define _SDL_blit_h
  21. #include "SDL_cpuinfo.h"
  22. #include "SDL_endian.h"
  23. #include "SDL_surface.h"
  24. /* Table to do pixel byte expansion */
  25. extern Uint8* SDL_expand_byte[9];
  26. /* SDL blit copy flags */
  27. #define SDL_COPY_MODULATE_COLOR 0x00000001
  28. #define SDL_COPY_MODULATE_ALPHA 0x00000002
  29. #define SDL_COPY_BLEND 0x00000010
  30. #define SDL_COPY_ADD 0x00000020
  31. #define SDL_COPY_MOD 0x00000040
  32. #define SDL_COPY_COLORKEY 0x00000100
  33. #define SDL_COPY_NEAREST 0x00000200
  34. #define SDL_COPY_RLE_DESIRED 0x00001000
  35. #define SDL_COPY_RLE_COLORKEY 0x00002000
  36. #define SDL_COPY_RLE_ALPHAKEY 0x00004000
  37. #define SDL_COPY_RLE_MASK (SDL_COPY_RLE_DESIRED|SDL_COPY_RLE_COLORKEY|SDL_COPY_RLE_ALPHAKEY)
  38. /* SDL blit CPU flags */
  39. #define SDL_CPU_ANY 0x00000000
  40. #define SDL_CPU_MMX 0x00000001
  41. #define SDL_CPU_3DNOW 0x00000002
  42. #define SDL_CPU_SSE 0x00000004
  43. #define SDL_CPU_SSE2 0x00000008
  44. #define SDL_CPU_ALTIVEC_PREFETCH 0x00000010
  45. #define SDL_CPU_ALTIVEC_NOPREFETCH 0x00000020
  46. typedef struct
  47. {
  48. Uint8 *src;
  49. int src_w, src_h;
  50. int src_pitch;
  51. int src_skip;
  52. Uint8 *dst;
  53. int dst_w, dst_h;
  54. int dst_pitch;
  55. int dst_skip;
  56. SDL_PixelFormat *src_fmt;
  57. SDL_PixelFormat *dst_fmt;
  58. Uint8 *table;
  59. int flags;
  60. Uint32 colorkey;
  61. Uint8 r, g, b, a;
  62. } SDL_BlitInfo;
  63. typedef void (SDLCALL * SDL_BlitFunc) (SDL_BlitInfo * info);
  64. typedef struct
  65. {
  66. Uint32 src_format;
  67. Uint32 dst_format;
  68. int flags;
  69. int cpu;
  70. SDL_BlitFunc func;
  71. } SDL_BlitFuncEntry;
  72. /* Blit mapping definition */
  73. typedef struct SDL_BlitMap
  74. {
  75. SDL_Surface *dst;
  76. int identity;
  77. SDL_blit blit;
  78. void *data;
  79. SDL_BlitInfo info;
  80. /* the version count matches the destination; mismatch indicates
  81. an invalid mapping */
  82. Uint32 dst_palette_version;
  83. Uint32 src_palette_version;
  84. } SDL_BlitMap;
  85. /* Functions found in SDL_blit.c */
  86. extern int SDL_CalculateBlit(SDL_Surface * surface);
  87. /* Functions found in SDL_blit_*.c */
  88. extern SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface * surface);
  89. extern SDL_BlitFunc SDL_CalculateBlit1(SDL_Surface * surface);
  90. extern SDL_BlitFunc SDL_CalculateBlitN(SDL_Surface * surface);
  91. extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface * surface);
  92. /*
  93. * Useful macros for blitting routines
  94. */
  95. #if defined(__GNUC__)
  96. #define DECLARE_ALIGNED(t,v,a) t __attribute__((aligned(a))) v
  97. #elif defined(_MSC_VER)
  98. #define DECLARE_ALIGNED(t,v,a) __declspec(align(a)) t v
  99. #else
  100. #define DECLARE_ALIGNED(t,v,a) t v
  101. #endif
  102. /* Load pixel of the specified format from a buffer and get its R-G-B values */
  103. #define RGB_FROM_PIXEL(Pixel, fmt, r, g, b) \
  104. { \
  105. r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
  106. g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
  107. b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
  108. }
  109. #define RGB_FROM_RGB565(Pixel, r, g, b) \
  110. { \
  111. r = SDL_expand_byte[3][((Pixel&0xF800)>>11)]; \
  112. g = SDL_expand_byte[2][((Pixel&0x07E0)>>5)]; \
  113. b = SDL_expand_byte[3][(Pixel&0x001F)]; \
  114. }
  115. #define RGB_FROM_RGB555(Pixel, r, g, b) \
  116. { \
  117. r = SDL_expand_byte[3][((Pixel&0x7C00)>>10)]; \
  118. g = SDL_expand_byte[3][((Pixel&0x03E0)>>5)]; \
  119. b = SDL_expand_byte[3][(Pixel&0x001F)]; \
  120. }
  121. #define RGB_FROM_RGB888(Pixel, r, g, b) \
  122. { \
  123. r = ((Pixel&0xFF0000)>>16); \
  124. g = ((Pixel&0xFF00)>>8); \
  125. b = (Pixel&0xFF); \
  126. }
  127. #define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel) \
  128. do { \
  129. switch (bpp) { \
  130. case 2: \
  131. Pixel = *((Uint16 *)(buf)); \
  132. break; \
  133. \
  134. case 3: { \
  135. Uint8 *B = (Uint8 *)(buf); \
  136. if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
  137. Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
  138. } else { \
  139. Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
  140. } \
  141. } \
  142. break; \
  143. \
  144. case 4: \
  145. Pixel = *((Uint32 *)(buf)); \
  146. break; \
  147. \
  148. default: \
  149. Pixel = 0; /* stop gcc complaints */ \
  150. break; \
  151. } \
  152. } while (0)
  153. #define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b) \
  154. do { \
  155. switch (bpp) { \
  156. case 2: \
  157. Pixel = *((Uint16 *)(buf)); \
  158. RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \
  159. break; \
  160. \
  161. case 3: { \
  162. Pixel = 0; \
  163. if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
  164. r = *((buf)+fmt->Rshift/8); \
  165. g = *((buf)+fmt->Gshift/8); \
  166. b = *((buf)+fmt->Bshift/8); \
  167. } else { \
  168. r = *((buf)+2-fmt->Rshift/8); \
  169. g = *((buf)+2-fmt->Gshift/8); \
  170. b = *((buf)+2-fmt->Bshift/8); \
  171. } \
  172. } \
  173. break; \
  174. \
  175. case 4: \
  176. Pixel = *((Uint32 *)(buf)); \
  177. RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \
  178. break; \
  179. \
  180. default: \
  181. /* stop gcc complaints */ \
  182. Pixel = 0; \
  183. r = g = b = 0; \
  184. break; \
  185. } \
  186. } while (0)
  187. /* Assemble R-G-B values into a specified pixel format and store them */
  188. #define PIXEL_FROM_RGB(Pixel, fmt, r, g, b) \
  189. { \
  190. Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)| \
  191. ((g>>fmt->Gloss)<<fmt->Gshift)| \
  192. ((b>>fmt->Bloss)<<fmt->Bshift)| \
  193. fmt->Amask; \
  194. }
  195. #define RGB565_FROM_RGB(Pixel, r, g, b) \
  196. { \
  197. Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3); \
  198. }
  199. #define RGB555_FROM_RGB(Pixel, r, g, b) \
  200. { \
  201. Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3); \
  202. }
  203. #define RGB888_FROM_RGB(Pixel, r, g, b) \
  204. { \
  205. Pixel = (r<<16)|(g<<8)|b; \
  206. }
  207. #define ARGB8888_FROM_RGBA(Pixel, r, g, b, a) \
  208. { \
  209. Pixel = (a<<24)|(r<<16)|(g<<8)|b; \
  210. }
  211. #define RGBA8888_FROM_RGBA(Pixel, r, g, b, a) \
  212. { \
  213. Pixel = (r<<24)|(g<<16)|(b<<8)|a; \
  214. }
  215. #define ABGR8888_FROM_RGBA(Pixel, r, g, b, a) \
  216. { \
  217. Pixel = (a<<24)|(b<<16)|(g<<8)|r; \
  218. }
  219. #define BGRA8888_FROM_RGBA(Pixel, r, g, b, a) \
  220. { \
  221. Pixel = (b<<24)|(g<<16)|(r<<8)|a; \
  222. }
  223. #define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) \
  224. { \
  225. switch (bpp) { \
  226. case 2: { \
  227. Uint16 Pixel; \
  228. \
  229. PIXEL_FROM_RGB(Pixel, fmt, r, g, b); \
  230. *((Uint16 *)(buf)) = Pixel; \
  231. } \
  232. break; \
  233. \
  234. case 3: { \
  235. if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
  236. *((buf)+fmt->Rshift/8) = r; \
  237. *((buf)+fmt->Gshift/8) = g; \
  238. *((buf)+fmt->Bshift/8) = b; \
  239. } else { \
  240. *((buf)+2-fmt->Rshift/8) = r; \
  241. *((buf)+2-fmt->Gshift/8) = g; \
  242. *((buf)+2-fmt->Bshift/8) = b; \
  243. } \
  244. } \
  245. break; \
  246. \
  247. case 4: { \
  248. Uint32 Pixel; \
  249. \
  250. PIXEL_FROM_RGB(Pixel, fmt, r, g, b); \
  251. *((Uint32 *)(buf)) = Pixel; \
  252. } \
  253. break; \
  254. } \
  255. }
  256. /* FIXME: Should we rescale alpha into 0..255 here? */
  257. #define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a) \
  258. { \
  259. r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
  260. g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
  261. b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
  262. a = SDL_expand_byte[fmt->Aloss][((Pixel&fmt->Amask)>>fmt->Ashift)]; \
  263. }
  264. #define RGBA_FROM_8888(Pixel, fmt, r, g, b, a) \
  265. { \
  266. r = (Pixel&fmt->Rmask)>>fmt->Rshift; \
  267. g = (Pixel&fmt->Gmask)>>fmt->Gshift; \
  268. b = (Pixel&fmt->Bmask)>>fmt->Bshift; \
  269. a = (Pixel&fmt->Amask)>>fmt->Ashift; \
  270. }
  271. #define RGBA_FROM_RGBA8888(Pixel, r, g, b, a) \
  272. { \
  273. r = (Pixel>>24); \
  274. g = ((Pixel>>16)&0xFF); \
  275. b = ((Pixel>>8)&0xFF); \
  276. a = (Pixel&0xFF); \
  277. }
  278. #define RGBA_FROM_ARGB8888(Pixel, r, g, b, a) \
  279. { \
  280. r = ((Pixel>>16)&0xFF); \
  281. g = ((Pixel>>8)&0xFF); \
  282. b = (Pixel&0xFF); \
  283. a = (Pixel>>24); \
  284. }
  285. #define RGBA_FROM_ABGR8888(Pixel, r, g, b, a) \
  286. { \
  287. r = (Pixel&0xFF); \
  288. g = ((Pixel>>8)&0xFF); \
  289. b = ((Pixel>>16)&0xFF); \
  290. a = (Pixel>>24); \
  291. }
  292. #define RGBA_FROM_BGRA8888(Pixel, r, g, b, a) \
  293. { \
  294. r = ((Pixel>>8)&0xFF); \
  295. g = ((Pixel>>16)&0xFF); \
  296. b = (Pixel>>24); \
  297. a = (Pixel&0xFF); \
  298. }
  299. #define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a) \
  300. do { \
  301. switch (bpp) { \
  302. case 2: \
  303. Pixel = *((Uint16 *)(buf)); \
  304. RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \
  305. break; \
  306. \
  307. case 3: { \
  308. Pixel = 0; \
  309. if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
  310. r = *((buf)+fmt->Rshift/8); \
  311. g = *((buf)+fmt->Gshift/8); \
  312. b = *((buf)+fmt->Bshift/8); \
  313. } else { \
  314. r = *((buf)+2-fmt->Rshift/8); \
  315. g = *((buf)+2-fmt->Gshift/8); \
  316. b = *((buf)+2-fmt->Bshift/8); \
  317. } \
  318. a = 0xFF; \
  319. } \
  320. break; \
  321. \
  322. case 4: \
  323. Pixel = *((Uint32 *)(buf)); \
  324. RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \
  325. break; \
  326. \
  327. default: \
  328. /* stop gcc complaints */ \
  329. Pixel = 0; \
  330. r = g = b = a = 0; \
  331. break; \
  332. } \
  333. } while (0)
  334. /* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
  335. #define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a) \
  336. { \
  337. Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)| \
  338. ((g>>fmt->Gloss)<<fmt->Gshift)| \
  339. ((b>>fmt->Bloss)<<fmt->Bshift)| \
  340. ((a>>fmt->Aloss)<<fmt->Ashift); \
  341. }
  342. #define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a) \
  343. { \
  344. switch (bpp) { \
  345. case 2: { \
  346. Uint16 Pixel; \
  347. \
  348. PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a); \
  349. *((Uint16 *)(buf)) = Pixel; \
  350. } \
  351. break; \
  352. \
  353. case 3: { \
  354. if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
  355. *((buf)+fmt->Rshift/8) = r; \
  356. *((buf)+fmt->Gshift/8) = g; \
  357. *((buf)+fmt->Bshift/8) = b; \
  358. } else { \
  359. *((buf)+2-fmt->Rshift/8) = r; \
  360. *((buf)+2-fmt->Gshift/8) = g; \
  361. *((buf)+2-fmt->Bshift/8) = b; \
  362. } \
  363. } \
  364. break; \
  365. \
  366. case 4: { \
  367. Uint32 Pixel; \
  368. \
  369. PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a); \
  370. *((Uint32 *)(buf)) = Pixel; \
  371. } \
  372. break; \
  373. } \
  374. }
  375. /* Blend the RGB values of two Pixels based on a source alpha value */
  376. #define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB) \
  377. do { \
  378. dR = ((((int)(sR-dR)*(int)A)/255)+dR); \
  379. dG = ((((int)(sG-dG)*(int)A)/255)+dG); \
  380. dB = ((((int)(sB-dB)*(int)A)/255)+dB); \
  381. } while(0)
  382. /* This is a very useful loop for optimizing blitters */
  383. #if defined(_MSC_VER) && (_MSC_VER == 1300)
  384. /* There's a bug in the Visual C++ 7 optimizer when compiling this code */
  385. #else
  386. #define USE_DUFFS_LOOP
  387. #endif
  388. #ifdef USE_DUFFS_LOOP
  389. /* 8-times unrolled loop */
  390. #define DUFFS_LOOP8(pixel_copy_increment, width) \
  391. { int n = (width+7)/8; \
  392. switch (width & 7) { \
  393. case 0: do { pixel_copy_increment; \
  394. case 7: pixel_copy_increment; \
  395. case 6: pixel_copy_increment; \
  396. case 5: pixel_copy_increment; \
  397. case 4: pixel_copy_increment; \
  398. case 3: pixel_copy_increment; \
  399. case 2: pixel_copy_increment; \
  400. case 1: pixel_copy_increment; \
  401. } while ( --n > 0 ); \
  402. } \
  403. }
  404. /* 4-times unrolled loop */
  405. #define DUFFS_LOOP4(pixel_copy_increment, width) \
  406. { int n = (width+3)/4; \
  407. switch (width & 3) { \
  408. case 0: do { pixel_copy_increment; \
  409. case 3: pixel_copy_increment; \
  410. case 2: pixel_copy_increment; \
  411. case 1: pixel_copy_increment; \
  412. } while (--n > 0); \
  413. } \
  414. }
  415. /* Use the 8-times version of the loop by default */
  416. #define DUFFS_LOOP(pixel_copy_increment, width) \
  417. DUFFS_LOOP8(pixel_copy_increment, width)
  418. /* Special version of Duff's device for even more optimization */
  419. #define DUFFS_LOOP_124(pixel_copy_increment1, \
  420. pixel_copy_increment2, \
  421. pixel_copy_increment4, width) \
  422. { int n = width; \
  423. if (n & 1) { \
  424. pixel_copy_increment1; n -= 1; \
  425. } \
  426. if (n & 2) { \
  427. pixel_copy_increment2; n -= 2; \
  428. } \
  429. if (n) { \
  430. n = (n+7)/ 8; \
  431. switch (n & 4) { \
  432. case 0: do { pixel_copy_increment4; \
  433. case 4: pixel_copy_increment4; \
  434. } while (--n > 0); \
  435. } \
  436. } \
  437. }
  438. #else
  439. /* Don't use Duff's device to unroll loops */
  440. #define DUFFS_LOOP(pixel_copy_increment, width) \
  441. { int n; \
  442. for ( n=width; n > 0; --n ) { \
  443. pixel_copy_increment; \
  444. } \
  445. }
  446. #define DUFFS_LOOP8(pixel_copy_increment, width) \
  447. DUFFS_LOOP(pixel_copy_increment, width)
  448. #define DUFFS_LOOP4(pixel_copy_increment, width) \
  449. DUFFS_LOOP(pixel_copy_increment, width)
  450. #define DUFFS_LOOP_124(pixel_copy_increment1, \
  451. pixel_copy_increment2, \
  452. pixel_copy_increment4, width) \
  453. DUFFS_LOOP(pixel_copy_increment1, width)
  454. #endif /* USE_DUFFS_LOOP */
  455. /* Prevent Visual C++ 6.0 from printing out stupid warnings */
  456. #if defined(_MSC_VER) && (_MSC_VER >= 600)
  457. #pragma warning(disable: 4550)
  458. #endif
  459. #endif /* _SDL_blit_h */
  460. /* vi: set ts=4 sw=4 expandtab: */