SDL_blit_A.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. #if SDL_HAVE_BLIT_A
  20. #include "SDL_blit.h"
  21. /* Functions to perform alpha blended blitting */
  22. /* N->1 blending with per-surface alpha */
  23. static void BlitNto1SurfaceAlpha(SDL_BlitInfo *info)
  24. {
  25. int width = info->dst_w;
  26. int height = info->dst_h;
  27. Uint8 *src = info->src;
  28. int srcskip = info->src_skip;
  29. Uint8 *dst = info->dst;
  30. int dstskip = info->dst_skip;
  31. Uint8 *palmap = info->table;
  32. SDL_PixelFormat *srcfmt = info->src_fmt;
  33. SDL_PixelFormat *dstfmt = info->dst_fmt;
  34. int srcbpp = srcfmt->BytesPerPixel;
  35. Uint32 Pixel;
  36. unsigned sR, sG, sB;
  37. unsigned dR, dG, dB;
  38. const unsigned A = info->a;
  39. while (height--) {
  40. /* *INDENT-OFF* */ /* clang-format off */
  41. DUFFS_LOOP4(
  42. {
  43. DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
  44. dR = dstfmt->palette->colors[*dst].r;
  45. dG = dstfmt->palette->colors[*dst].g;
  46. dB = dstfmt->palette->colors[*dst].b;
  47. ALPHA_BLEND_RGB(sR, sG, sB, A, dR, dG, dB);
  48. dR &= 0xff;
  49. dG &= 0xff;
  50. dB &= 0xff;
  51. /* Pack RGB into 8bit pixel */
  52. if ( palmap == NULL ) {
  53. *dst =((dR>>5)<<(3+2))|((dG>>5)<<(2))|((dB>>6)<<(0));
  54. } else {
  55. *dst = palmap[((dR>>5)<<(3+2))|((dG>>5)<<(2))|((dB>>6)<<(0))];
  56. }
  57. dst++;
  58. src += srcbpp;
  59. },
  60. width);
  61. /* *INDENT-ON* */ /* clang-format on */
  62. src += srcskip;
  63. dst += dstskip;
  64. }
  65. }
  66. /* N->1 blending with pixel alpha */
  67. static void BlitNto1PixelAlpha(SDL_BlitInfo *info)
  68. {
  69. int width = info->dst_w;
  70. int height = info->dst_h;
  71. Uint8 *src = info->src;
  72. int srcskip = info->src_skip;
  73. Uint8 *dst = info->dst;
  74. int dstskip = info->dst_skip;
  75. Uint8 *palmap = info->table;
  76. SDL_PixelFormat *srcfmt = info->src_fmt;
  77. SDL_PixelFormat *dstfmt = info->dst_fmt;
  78. int srcbpp = srcfmt->BytesPerPixel;
  79. Uint32 Pixel;
  80. unsigned sR, sG, sB, sA;
  81. unsigned dR, dG, dB;
  82. while (height--) {
  83. /* *INDENT-OFF* */ /* clang-format off */
  84. DUFFS_LOOP4(
  85. {
  86. DISEMBLE_RGBA(src,srcbpp,srcfmt,Pixel,sR,sG,sB,sA);
  87. dR = dstfmt->palette->colors[*dst].r;
  88. dG = dstfmt->palette->colors[*dst].g;
  89. dB = dstfmt->palette->colors[*dst].b;
  90. ALPHA_BLEND_RGB(sR, sG, sB, sA, dR, dG, dB);
  91. dR &= 0xff;
  92. dG &= 0xff;
  93. dB &= 0xff;
  94. /* Pack RGB into 8bit pixel */
  95. if ( palmap == NULL ) {
  96. *dst =((dR>>5)<<(3+2))|((dG>>5)<<(2))|((dB>>6)<<(0));
  97. } else {
  98. *dst = palmap[((dR>>5)<<(3+2))|((dG>>5)<<(2))|((dB>>6)<<(0))];
  99. }
  100. dst++;
  101. src += srcbpp;
  102. },
  103. width);
  104. /* *INDENT-ON* */ /* clang-format on */
  105. src += srcskip;
  106. dst += dstskip;
  107. }
  108. }
  109. /* colorkeyed N->1 blending with per-surface alpha */
  110. static void BlitNto1SurfaceAlphaKey(SDL_BlitInfo *info)
  111. {
  112. int width = info->dst_w;
  113. int height = info->dst_h;
  114. Uint8 *src = info->src;
  115. int srcskip = info->src_skip;
  116. Uint8 *dst = info->dst;
  117. int dstskip = info->dst_skip;
  118. Uint8 *palmap = info->table;
  119. SDL_PixelFormat *srcfmt = info->src_fmt;
  120. SDL_PixelFormat *dstfmt = info->dst_fmt;
  121. int srcbpp = srcfmt->BytesPerPixel;
  122. Uint32 ckey = info->colorkey;
  123. Uint32 Pixel;
  124. unsigned sR, sG, sB;
  125. unsigned dR, dG, dB;
  126. const unsigned A = info->a;
  127. while (height--) {
  128. /* *INDENT-OFF* */ /* clang-format off */
  129. DUFFS_LOOP(
  130. {
  131. DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
  132. if ( Pixel != ckey ) {
  133. dR = dstfmt->palette->colors[*dst].r;
  134. dG = dstfmt->palette->colors[*dst].g;
  135. dB = dstfmt->palette->colors[*dst].b;
  136. ALPHA_BLEND_RGB(sR, sG, sB, A, dR, dG, dB);
  137. dR &= 0xff;
  138. dG &= 0xff;
  139. dB &= 0xff;
  140. /* Pack RGB into 8bit pixel */
  141. if ( palmap == NULL ) {
  142. *dst =((dR>>5)<<(3+2))|((dG>>5)<<(2))|((dB>>6)<<(0));
  143. } else {
  144. *dst = palmap[((dR>>5)<<(3+2))|((dG>>5)<<(2))|((dB>>6)<<(0))];
  145. }
  146. }
  147. dst++;
  148. src += srcbpp;
  149. },
  150. width);
  151. /* *INDENT-ON* */ /* clang-format on */
  152. src += srcskip;
  153. dst += dstskip;
  154. }
  155. }
  156. #ifdef __MMX__
  157. /* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
  158. static void BlitRGBtoRGBSurfaceAlpha128MMX(SDL_BlitInfo *info)
  159. {
  160. int width = info->dst_w;
  161. int height = info->dst_h;
  162. Uint32 *srcp = (Uint32 *)info->src;
  163. int srcskip = info->src_skip >> 2;
  164. Uint32 *dstp = (Uint32 *)info->dst;
  165. int dstskip = info->dst_skip >> 2;
  166. Uint32 dalpha = info->dst_fmt->Amask;
  167. __m64 src1, src2, dst1, dst2, lmask, hmask, dsta;
  168. hmask = _mm_set_pi32(0x00fefefe, 0x00fefefe); /* alpha128 mask -> hmask */
  169. lmask = _mm_set_pi32(0x00010101, 0x00010101); /* !alpha128 mask -> lmask */
  170. dsta = _mm_set_pi32(dalpha, dalpha); /* dst alpha mask -> dsta */
  171. while (height--) {
  172. int n = width;
  173. if (n & 1) {
  174. Uint32 s = *srcp++;
  175. Uint32 d = *dstp;
  176. *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1) + (s & d & 0x00010101)) | dalpha;
  177. n--;
  178. }
  179. for (n >>= 1; n > 0; --n) {
  180. dst1 = *(__m64 *)dstp; /* 2 x dst -> dst1(ARGBARGB) */
  181. dst2 = dst1; /* 2 x dst -> dst2(ARGBARGB) */
  182. src1 = *(__m64 *)srcp; /* 2 x src -> src1(ARGBARGB) */
  183. src2 = src1; /* 2 x src -> src2(ARGBARGB) */
  184. dst2 = _mm_and_si64(dst2, hmask); /* dst & mask -> dst2 */
  185. src2 = _mm_and_si64(src2, hmask); /* src & mask -> src2 */
  186. src2 = _mm_add_pi32(src2, dst2); /* dst2 + src2 -> src2 */
  187. src2 = _mm_srli_pi32(src2, 1); /* src2 >> 1 -> src2 */
  188. dst1 = _mm_and_si64(dst1, src1); /* src & dst -> dst1 */
  189. dst1 = _mm_and_si64(dst1, lmask); /* dst1 & !mask -> dst1 */
  190. dst1 = _mm_add_pi32(dst1, src2); /* src2 + dst1 -> dst1 */
  191. dst1 = _mm_or_si64(dst1, dsta); /* dsta(full alpha) | dst1 -> dst1 */
  192. *(__m64 *)dstp = dst1; /* dst1 -> 2 x dst pixels */
  193. dstp += 2;
  194. srcp += 2;
  195. }
  196. srcp += srcskip;
  197. dstp += dstskip;
  198. }
  199. _mm_empty();
  200. }
  201. /* fast RGB888->(A)RGB888 blending with surface alpha */
  202. static void BlitRGBtoRGBSurfaceAlphaMMX(SDL_BlitInfo *info)
  203. {
  204. SDL_PixelFormat *df = info->dst_fmt;
  205. Uint32 chanmask;
  206. unsigned alpha = info->a;
  207. if (alpha == 128 && (df->Rmask | df->Gmask | df->Bmask) == 0x00FFFFFF) {
  208. /* only call a128 version when R,G,B occupy lower bits */
  209. BlitRGBtoRGBSurfaceAlpha128MMX(info);
  210. } else {
  211. int width = info->dst_w;
  212. int height = info->dst_h;
  213. Uint32 *srcp = (Uint32 *)info->src;
  214. int srcskip = info->src_skip >> 2;
  215. Uint32 *dstp = (Uint32 *)info->dst;
  216. int dstskip = info->dst_skip >> 2;
  217. Uint32 dalpha = df->Amask;
  218. Uint32 amult;
  219. __m64 src1, src2, dst1, dst2, mm_alpha, mm_zero, dsta;
  220. mm_zero = _mm_setzero_si64(); /* 0 -> mm_zero */
  221. /* form the alpha mult */
  222. amult = alpha | (alpha << 8);
  223. amult = amult | (amult << 16);
  224. chanmask =
  225. (0xff << df->Rshift) | (0xff << df->Gshift) | (0xff << df->Bshift);
  226. mm_alpha = _mm_set_pi32(0, amult & chanmask); /* 0000AAAA -> mm_alpha, minus 1 chan */
  227. mm_alpha = _mm_unpacklo_pi8(mm_alpha, mm_zero); /* 0A0A0A0A -> mm_alpha, minus 1 chan */
  228. /* at this point mm_alpha can be 000A0A0A or 0A0A0A00 or another combo */
  229. dsta = _mm_set_pi32(dalpha, dalpha); /* dst alpha mask -> dsta */
  230. while (height--) {
  231. int n = width;
  232. if (n & 1) {
  233. /* One Pixel Blend */
  234. src2 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src2 (0000ARGB) */
  235. src2 = _mm_unpacklo_pi8(src2, mm_zero); /* 0A0R0G0B -> src2 */
  236. dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB) */
  237. dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
  238. src2 = _mm_sub_pi16(src2, dst1); /* src2 - dst2 -> src2 */
  239. src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  240. src2 = _mm_srli_pi16(src2, 8); /* src2 >> 8 -> src2 */
  241. dst1 = _mm_add_pi8(src2, dst1); /* src2 + dst1 -> dst1 */
  242. dst1 = _mm_packs_pu16(dst1, mm_zero); /* 0000ARGB -> dst1 */
  243. dst1 = _mm_or_si64(dst1, dsta); /* dsta | dst1 -> dst1 */
  244. *dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
  245. ++srcp;
  246. ++dstp;
  247. n--;
  248. }
  249. for (n >>= 1; n > 0; --n) {
  250. /* Two Pixels Blend */
  251. src1 = *(__m64 *)srcp; /* 2 x src -> src1(ARGBARGB) */
  252. src2 = src1; /* 2 x src -> src2(ARGBARGB) */
  253. src1 = _mm_unpacklo_pi8(src1, mm_zero); /* low - 0A0R0G0B -> src1 */
  254. src2 = _mm_unpackhi_pi8(src2, mm_zero); /* high - 0A0R0G0B -> src2 */
  255. dst1 = *(__m64 *)dstp; /* 2 x dst -> dst1(ARGBARGB) */
  256. dst2 = dst1; /* 2 x dst -> dst2(ARGBARGB) */
  257. dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* low - 0A0R0G0B -> dst1 */
  258. dst2 = _mm_unpackhi_pi8(dst2, mm_zero); /* high - 0A0R0G0B -> dst2 */
  259. src1 = _mm_sub_pi16(src1, dst1); /* src1 - dst1 -> src1 */
  260. src1 = _mm_mullo_pi16(src1, mm_alpha); /* src1 * alpha -> src1 */
  261. src1 = _mm_srli_pi16(src1, 8); /* src1 >> 8 -> src1 */
  262. dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1(dst1) -> dst1 */
  263. src2 = _mm_sub_pi16(src2, dst2); /* src2 - dst2 -> src2 */
  264. src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  265. src2 = _mm_srli_pi16(src2, 8); /* src2 >> 8 -> src2 */
  266. dst2 = _mm_add_pi8(src2, dst2); /* src2 + dst2(dst2) -> dst2 */
  267. dst1 = _mm_packs_pu16(dst1, dst2); /* 0A0R0G0B(res1), 0A0R0G0B(res2) -> dst1(ARGBARGB) */
  268. dst1 = _mm_or_si64(dst1, dsta); /* dsta | dst1 -> dst1 */
  269. *(__m64 *)dstp = dst1; /* dst1 -> 2 x pixel */
  270. srcp += 2;
  271. dstp += 2;
  272. }
  273. srcp += srcskip;
  274. dstp += dstskip;
  275. }
  276. _mm_empty();
  277. }
  278. }
  279. /* fast ARGB888->(A)RGB888 blending with pixel alpha */
  280. static void BlitRGBtoRGBPixelAlphaMMX(SDL_BlitInfo *info)
  281. {
  282. int width = info->dst_w;
  283. int height = info->dst_h;
  284. Uint32 *srcp = (Uint32 *)info->src;
  285. int srcskip = info->src_skip >> 2;
  286. Uint32 *dstp = (Uint32 *)info->dst;
  287. int dstskip = info->dst_skip >> 2;
  288. SDL_PixelFormat *sf = info->src_fmt;
  289. Uint32 amask = sf->Amask;
  290. Uint32 ashift = sf->Ashift;
  291. Uint64 multmask, multmask2;
  292. __m64 src1, dst1, mm_alpha, mm_zero, mm_alpha2, mm_one_alpha;
  293. mm_zero = _mm_setzero_si64(); /* 0 -> mm_zero */
  294. if (amask == 0xFF000000) { /* 1 in the alpha channel -> mm_one_alpha */
  295. mm_one_alpha = _mm_set_pi16(1, 0, 0, 0);
  296. } else if (amask == 0x00FF0000) {
  297. mm_one_alpha = _mm_set_pi16(0, 1, 0, 0);
  298. } else if (amask == 0x0000FF00) {
  299. mm_one_alpha = _mm_set_pi16(0, 0, 1, 0);
  300. } else {
  301. mm_one_alpha = _mm_set_pi16(0, 0, 0, 1);
  302. }
  303. multmask = 0x00FF;
  304. multmask <<= (ashift * 2);
  305. multmask2 = 0x00FF00FF00FF00FFULL;
  306. while (height--) {
  307. /* *INDENT-OFF* */ /* clang-format off */
  308. DUFFS_LOOP4({
  309. Uint32 alpha = *srcp & amask;
  310. if (alpha == 0) {
  311. /* do nothing */
  312. } else if (alpha == amask) {
  313. *dstp = *srcp;
  314. } else {
  315. src1 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src1 (0000ARGB) */
  316. src1 = _mm_unpacklo_pi8(src1, mm_zero); /* 0A0R0G0B -> src1 */
  317. dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB) */
  318. dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
  319. mm_alpha = _mm_cvtsi32_si64(alpha); /* alpha -> mm_alpha (0000000A) */
  320. mm_alpha = _mm_srli_si64(mm_alpha, ashift); /* mm_alpha >> ashift -> mm_alpha(0000000A) */
  321. mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
  322. mm_alpha2 = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha2 */
  323. mm_alpha = _mm_or_si64(mm_alpha2, *(__m64 *) & multmask); /* 0F0A0A0A -> mm_alpha */
  324. mm_alpha2 = _mm_xor_si64(mm_alpha2, *(__m64 *) & multmask2); /* 255 - mm_alpha -> mm_alpha */
  325. /*
  326. Alpha blending is:
  327. dstRGB = (srcRGB * srcA) + (dstRGB * (1-srcA))
  328. dstA = srcA + (dstA * (1-srcA)) *
  329. Here, 'src1' is:
  330. srcRGB * srcA
  331. srcA
  332. And 'dst1' is:
  333. dstRGB * (1-srcA)
  334. dstA * (1-srcA)
  335. so that *dstp is 'src1 + dst1'
  336. src1 is computed using mullo_pi16: (X * mask) >> 8, but is approximate for srcA ((srcA * 255) >> 8).
  337. need to a 1 to get an exact result: (srcA * 256) >> 8 == srcA
  338. */
  339. mm_alpha = _mm_add_pi16(mm_alpha, mm_one_alpha);
  340. /* blend */
  341. src1 = _mm_mullo_pi16(src1, mm_alpha);
  342. src1 = _mm_srli_pi16(src1, 8);
  343. dst1 = _mm_mullo_pi16(dst1, mm_alpha2);
  344. dst1 = _mm_srli_pi16(dst1, 8);
  345. dst1 = _mm_add_pi16(src1, dst1);
  346. dst1 = _mm_packs_pu16(dst1, mm_zero);
  347. *dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
  348. }
  349. ++srcp;
  350. ++dstp;
  351. }, width);
  352. /* *INDENT-ON* */ /* clang-format on */
  353. srcp += srcskip;
  354. dstp += dstskip;
  355. }
  356. _mm_empty();
  357. }
  358. #endif /* __MMX__ */
  359. #if SDL_ARM_SIMD_BLITTERS
  360. void BlitARGBto565PixelAlphaARMSIMDAsm(int32_t w, int32_t h, uint16_t *dst, int32_t dst_stride, uint32_t *src, int32_t src_stride);
  361. static void BlitARGBto565PixelAlphaARMSIMD(SDL_BlitInfo *info)
  362. {
  363. int32_t width = info->dst_w;
  364. int32_t height = info->dst_h;
  365. uint16_t *dstp = (uint16_t *)info->dst;
  366. int32_t dststride = width + (info->dst_skip >> 1);
  367. uint32_t *srcp = (uint32_t *)info->src;
  368. int32_t srcstride = width + (info->src_skip >> 2);
  369. BlitARGBto565PixelAlphaARMSIMDAsm(width, height, dstp, dststride, srcp, srcstride);
  370. }
  371. void BlitRGBtoRGBPixelAlphaARMSIMDAsm(int32_t w, int32_t h, uint32_t *dst, int32_t dst_stride, uint32_t *src, int32_t src_stride);
  372. static void BlitRGBtoRGBPixelAlphaARMSIMD(SDL_BlitInfo *info)
  373. {
  374. int32_t width = info->dst_w;
  375. int32_t height = info->dst_h;
  376. uint32_t *dstp = (uint32_t *)info->dst;
  377. int32_t dststride = width + (info->dst_skip >> 2);
  378. uint32_t *srcp = (uint32_t *)info->src;
  379. int32_t srcstride = width + (info->src_skip >> 2);
  380. BlitRGBtoRGBPixelAlphaARMSIMDAsm(width, height, dstp, dststride, srcp, srcstride);
  381. }
  382. #endif
  383. #if SDL_ARM_NEON_BLITTERS
  384. void BlitARGBto565PixelAlphaARMNEONAsm(int32_t w, int32_t h, uint16_t *dst, int32_t dst_stride, uint32_t *src, int32_t src_stride);
  385. static void BlitARGBto565PixelAlphaARMNEON(SDL_BlitInfo *info)
  386. {
  387. int32_t width = info->dst_w;
  388. int32_t height = info->dst_h;
  389. uint16_t *dstp = (uint16_t *)info->dst;
  390. int32_t dststride = width + (info->dst_skip >> 1);
  391. uint32_t *srcp = (uint32_t *)info->src;
  392. int32_t srcstride = width + (info->src_skip >> 2);
  393. BlitARGBto565PixelAlphaARMNEONAsm(width, height, dstp, dststride, srcp, srcstride);
  394. }
  395. void BlitRGBtoRGBPixelAlphaARMNEONAsm(int32_t w, int32_t h, uint32_t *dst, int32_t dst_stride, uint32_t *src, int32_t src_stride);
  396. static void BlitRGBtoRGBPixelAlphaARMNEON(SDL_BlitInfo *info)
  397. {
  398. int32_t width = info->dst_w;
  399. int32_t height = info->dst_h;
  400. uint32_t *dstp = (uint32_t *)info->dst;
  401. int32_t dststride = width + (info->dst_skip >> 2);
  402. uint32_t *srcp = (uint32_t *)info->src;
  403. int32_t srcstride = width + (info->src_skip >> 2);
  404. BlitRGBtoRGBPixelAlphaARMNEONAsm(width, height, dstp, dststride, srcp, srcstride);
  405. }
  406. #endif
  407. /* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
  408. static void BlitRGBtoRGBSurfaceAlpha128(SDL_BlitInfo *info)
  409. {
  410. int width = info->dst_w;
  411. int height = info->dst_h;
  412. Uint32 *srcp = (Uint32 *)info->src;
  413. int srcskip = info->src_skip >> 2;
  414. Uint32 *dstp = (Uint32 *)info->dst;
  415. int dstskip = info->dst_skip >> 2;
  416. while (height--) {
  417. /* *INDENT-OFF* */ /* clang-format off */
  418. DUFFS_LOOP4({
  419. Uint32 s = *srcp++;
  420. Uint32 d = *dstp;
  421. *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
  422. + (s & d & 0x00010101)) | 0xff000000;
  423. }, width);
  424. /* *INDENT-ON* */ /* clang-format on */
  425. srcp += srcskip;
  426. dstp += dstskip;
  427. }
  428. }
  429. /* fast RGB888->(A)RGB888 blending with surface alpha */
  430. static void BlitRGBtoRGBSurfaceAlpha(SDL_BlitInfo *info)
  431. {
  432. unsigned alpha = info->a;
  433. if (alpha == 128) {
  434. BlitRGBtoRGBSurfaceAlpha128(info);
  435. } else {
  436. int width = info->dst_w;
  437. int height = info->dst_h;
  438. Uint32 *srcp = (Uint32 *)info->src;
  439. int srcskip = info->src_skip >> 2;
  440. Uint32 *dstp = (Uint32 *)info->dst;
  441. int dstskip = info->dst_skip >> 2;
  442. Uint32 s;
  443. Uint32 d;
  444. Uint32 s1;
  445. Uint32 d1;
  446. while (height--) {
  447. /* *INDENT-OFF* */ /* clang-format off */
  448. DUFFS_LOOP4({
  449. s = *srcp;
  450. d = *dstp;
  451. s1 = s & 0xff00ff;
  452. d1 = d & 0xff00ff;
  453. d1 = (d1 + ((s1 - d1) * alpha >> 8))
  454. & 0xff00ff;
  455. s &= 0xff00;
  456. d &= 0xff00;
  457. d = (d + ((s - d) * alpha >> 8)) & 0xff00;
  458. *dstp = d1 | d | 0xff000000;
  459. ++srcp;
  460. ++dstp;
  461. }, width);
  462. /* *INDENT-ON* */ /* clang-format on */
  463. srcp += srcskip;
  464. dstp += dstskip;
  465. }
  466. }
  467. }
  468. /* fast ARGB888->(A)RGB888 blending with pixel alpha */
  469. static void BlitRGBtoRGBPixelAlpha(SDL_BlitInfo *info)
  470. {
  471. int width = info->dst_w;
  472. int height = info->dst_h;
  473. Uint32 *srcp = (Uint32 *)info->src;
  474. int srcskip = info->src_skip >> 2;
  475. Uint32 *dstp = (Uint32 *)info->dst;
  476. int dstskip = info->dst_skip >> 2;
  477. while (height--) {
  478. /* *INDENT-OFF* */ /* clang-format off */
  479. DUFFS_LOOP4({
  480. Uint32 dalpha;
  481. Uint32 d;
  482. Uint32 s1;
  483. Uint32 d1;
  484. Uint32 s = *srcp;
  485. Uint32 alpha = s >> 24;
  486. /* FIXME: Here we special-case opaque alpha since the
  487. compositioning used (>>8 instead of /255) doesn't handle
  488. it correctly. Also special-case alpha=0 for speed?
  489. Benchmark this! */
  490. if (alpha) {
  491. if (alpha == SDL_ALPHA_OPAQUE) {
  492. *dstp = *srcp;
  493. } else {
  494. /*
  495. * take out the middle component (green), and process
  496. * the other two in parallel. One multiply less.
  497. */
  498. d = *dstp;
  499. dalpha = d >> 24;
  500. s1 = s & 0xff00ff;
  501. d1 = d & 0xff00ff;
  502. d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
  503. s &= 0xff00;
  504. d &= 0xff00;
  505. d = (d + ((s - d) * alpha >> 8)) & 0xff00;
  506. dalpha = alpha + (dalpha * (alpha ^ 0xFF) >> 8);
  507. *dstp = d1 | d | (dalpha << 24);
  508. }
  509. }
  510. ++srcp;
  511. ++dstp;
  512. }, width);
  513. /* *INDENT-ON* */ /* clang-format on */
  514. srcp += srcskip;
  515. dstp += dstskip;
  516. }
  517. }
  518. /* fast ARGB888->(A)BGR888 blending with pixel alpha */
  519. static void BlitRGBtoBGRPixelAlpha(SDL_BlitInfo *info)
  520. {
  521. int width = info->dst_w;
  522. int height = info->dst_h;
  523. Uint32 *srcp = (Uint32 *)info->src;
  524. int srcskip = info->src_skip >> 2;
  525. Uint32 *dstp = (Uint32 *)info->dst;
  526. int dstskip = info->dst_skip >> 2;
  527. while (height--) {
  528. /* *INDENT-OFF* */ /* clang-format off */
  529. DUFFS_LOOP4({
  530. Uint32 dalpha;
  531. Uint32 d;
  532. Uint32 s1;
  533. Uint32 d1;
  534. Uint32 s = *srcp;
  535. Uint32 alpha = s >> 24;
  536. /* FIXME: Here we special-case opaque alpha since the
  537. compositioning used (>>8 instead of /255) doesn't handle
  538. it correctly. Also special-case alpha=0 for speed?
  539. Benchmark this! */
  540. if (alpha) {
  541. /*
  542. * take out the middle component (green), and process
  543. * the other two in parallel. One multiply less.
  544. */
  545. s1 = s & 0xff00ff;
  546. s1 = (s1 >> 16) | (s1 << 16);
  547. s &= 0xff00;
  548. if (alpha == SDL_ALPHA_OPAQUE) {
  549. *dstp = 0xff000000 | s | s1;
  550. } else {
  551. d = *dstp;
  552. dalpha = d >> 24;
  553. d1 = d & 0xff00ff;
  554. d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
  555. d &= 0xff00;
  556. d = (d + ((s - d) * alpha >> 8)) & 0xff00;
  557. dalpha = alpha + (dalpha * (alpha ^ 0xFF) >> 8);
  558. *dstp = d1 | d | (dalpha << 24);
  559. }
  560. }
  561. ++srcp;
  562. ++dstp;
  563. }, width);
  564. /* *INDENT-ON* */ /* clang-format on */
  565. srcp += srcskip;
  566. dstp += dstskip;
  567. }
  568. }
  569. /* 16bpp special case for per-surface alpha=50%: blend 2 pixels in parallel */
  570. /* blend a single 16 bit pixel at 50% */
  571. #define BLEND16_50(d, s, mask) \
  572. ((((s & mask) + (d & mask)) >> 1) + (s & d & (~mask & 0xffff)))
  573. /* blend two 16 bit pixels at 50% */
  574. #define BLEND2x16_50(d, s, mask) \
  575. (((s & (mask | mask << 16)) >> 1) + ((d & (mask | mask << 16)) >> 1) + (s & d & (~(mask | mask << 16))))
  576. static void Blit16to16SurfaceAlpha128(SDL_BlitInfo *info, Uint16 mask)
  577. {
  578. int width = info->dst_w;
  579. int height = info->dst_h;
  580. Uint16 *srcp = (Uint16 *)info->src;
  581. int srcskip = info->src_skip >> 1;
  582. Uint16 *dstp = (Uint16 *)info->dst;
  583. int dstskip = info->dst_skip >> 1;
  584. while (height--) {
  585. if (((uintptr_t)srcp ^ (uintptr_t)dstp) & 2) {
  586. /*
  587. * Source and destination not aligned, pipeline it.
  588. * This is mostly a win for big blits but no loss for
  589. * small ones
  590. */
  591. Uint32 prev_sw;
  592. int w = width;
  593. /* handle odd destination */
  594. if ((uintptr_t)dstp & 2) {
  595. Uint16 d = *dstp, s = *srcp;
  596. *dstp = BLEND16_50(d, s, mask);
  597. dstp++;
  598. srcp++;
  599. w--;
  600. }
  601. srcp++; /* srcp is now 32-bit aligned */
  602. /* bootstrap pipeline with first halfword */
  603. prev_sw = ((Uint32 *)srcp)[-1];
  604. while (w > 1) {
  605. Uint32 sw, dw, s;
  606. sw = *(Uint32 *)srcp;
  607. dw = *(Uint32 *)dstp;
  608. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  609. s = (prev_sw << 16) + (sw >> 16);
  610. #else
  611. s = (prev_sw >> 16) + (sw << 16);
  612. #endif
  613. prev_sw = sw;
  614. *(Uint32 *)dstp = BLEND2x16_50(dw, s, mask);
  615. dstp += 2;
  616. srcp += 2;
  617. w -= 2;
  618. }
  619. /* final pixel if any */
  620. if (w) {
  621. Uint16 d = *dstp, s;
  622. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  623. s = (Uint16)prev_sw;
  624. #else
  625. s = (Uint16)(prev_sw >> 16);
  626. #endif
  627. *dstp = BLEND16_50(d, s, mask);
  628. srcp++;
  629. dstp++;
  630. }
  631. srcp += srcskip - 1;
  632. dstp += dstskip;
  633. } else {
  634. /* source and destination are aligned */
  635. int w = width;
  636. /* first odd pixel? */
  637. if ((uintptr_t)srcp & 2) {
  638. Uint16 d = *dstp, s = *srcp;
  639. *dstp = BLEND16_50(d, s, mask);
  640. srcp++;
  641. dstp++;
  642. w--;
  643. }
  644. /* srcp and dstp are now 32-bit aligned */
  645. while (w > 1) {
  646. Uint32 sw = *(Uint32 *)srcp;
  647. Uint32 dw = *(Uint32 *)dstp;
  648. *(Uint32 *)dstp = BLEND2x16_50(dw, sw, mask);
  649. srcp += 2;
  650. dstp += 2;
  651. w -= 2;
  652. }
  653. /* last odd pixel? */
  654. if (w) {
  655. Uint16 d = *dstp, s = *srcp;
  656. *dstp = BLEND16_50(d, s, mask);
  657. srcp++;
  658. dstp++;
  659. }
  660. srcp += srcskip;
  661. dstp += dstskip;
  662. }
  663. }
  664. }
  665. #ifdef __MMX__
  666. /* fast RGB565->RGB565 blending with surface alpha */
  667. static void Blit565to565SurfaceAlphaMMX(SDL_BlitInfo *info)
  668. {
  669. unsigned alpha = info->a;
  670. if (alpha == 128) {
  671. Blit16to16SurfaceAlpha128(info, 0xf7de);
  672. } else {
  673. int width = info->dst_w;
  674. int height = info->dst_h;
  675. Uint16 *srcp = (Uint16 *)info->src;
  676. int srcskip = info->src_skip >> 1;
  677. Uint16 *dstp = (Uint16 *)info->dst;
  678. int dstskip = info->dst_skip >> 1;
  679. Uint32 s, d;
  680. __m64 src1, dst1, src2, dst2, gmask, bmask, mm_res, mm_alpha;
  681. alpha &= ~(1 + 2 + 4); /* cut alpha to get the exact same behaviour */
  682. mm_alpha = _mm_set_pi32(0, alpha); /* 0000000A -> mm_alpha */
  683. alpha >>= 3; /* downscale alpha to 5 bits */
  684. mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
  685. mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
  686. /* position alpha to allow for mullo and mulhi on diff channels
  687. to reduce the number of operations */
  688. mm_alpha = _mm_slli_si64(mm_alpha, 3);
  689. /* Setup the 565 color channel masks */
  690. gmask = _mm_set_pi32(0x07E007E0, 0x07E007E0); /* MASKGREEN -> gmask */
  691. bmask = _mm_set_pi32(0x001F001F, 0x001F001F); /* MASKBLUE -> bmask */
  692. while (height--) {
  693. /* *INDENT-OFF* */ /* clang-format off */
  694. DUFFS_LOOP_124(
  695. {
  696. s = *srcp++;
  697. d = *dstp;
  698. /*
  699. * shift out the middle component (green) to
  700. * the high 16 bits, and process all three RGB
  701. * components at the same time.
  702. */
  703. s = (s | s << 16) & 0x07e0f81f;
  704. d = (d | d << 16) & 0x07e0f81f;
  705. d += (s - d) * alpha >> 5;
  706. d &= 0x07e0f81f;
  707. *dstp++ = (Uint16)(d | d >> 16);
  708. },{
  709. s = *srcp++;
  710. d = *dstp;
  711. /*
  712. * shift out the middle component (green) to
  713. * the high 16 bits, and process all three RGB
  714. * components at the same time.
  715. */
  716. s = (s | s << 16) & 0x07e0f81f;
  717. d = (d | d << 16) & 0x07e0f81f;
  718. d += (s - d) * alpha >> 5;
  719. d &= 0x07e0f81f;
  720. *dstp++ = (Uint16)(d | d >> 16);
  721. s = *srcp++;
  722. d = *dstp;
  723. /*
  724. * shift out the middle component (green) to
  725. * the high 16 bits, and process all three RGB
  726. * components at the same time.
  727. */
  728. s = (s | s << 16) & 0x07e0f81f;
  729. d = (d | d << 16) & 0x07e0f81f;
  730. d += (s - d) * alpha >> 5;
  731. d &= 0x07e0f81f;
  732. *dstp++ = (Uint16)(d | d >> 16);
  733. },{
  734. src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
  735. dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
  736. /* red */
  737. src2 = src1;
  738. src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 [000r 000r 000r 000r] */
  739. dst2 = dst1;
  740. dst2 = _mm_srli_pi16(dst2, 11); /* dst2 >> 11 -> dst2 [000r 000r 000r 000r] */
  741. /* blend */
  742. src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
  743. src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  744. src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
  745. dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
  746. dst2 = _mm_slli_pi16(dst2, 11); /* dst2 << 11 -> dst2 */
  747. mm_res = dst2; /* RED -> mm_res */
  748. /* green -- process the bits in place */
  749. src2 = src1;
  750. src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
  751. dst2 = dst1;
  752. dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
  753. /* blend */
  754. src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
  755. src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  756. src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
  757. dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
  758. mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
  759. /* blue */
  760. src2 = src1;
  761. src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
  762. dst2 = dst1;
  763. dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
  764. /* blend */
  765. src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
  766. src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  767. src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
  768. dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
  769. dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
  770. mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
  771. *(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
  772. srcp += 4;
  773. dstp += 4;
  774. }, width);
  775. /* *INDENT-ON* */ /* clang-format on */
  776. srcp += srcskip;
  777. dstp += dstskip;
  778. }
  779. _mm_empty();
  780. }
  781. }
  782. /* fast RGB555->RGB555 blending with surface alpha */
  783. static void Blit555to555SurfaceAlphaMMX(SDL_BlitInfo *info)
  784. {
  785. unsigned alpha = info->a;
  786. if (alpha == 128) {
  787. Blit16to16SurfaceAlpha128(info, 0xfbde);
  788. } else {
  789. int width = info->dst_w;
  790. int height = info->dst_h;
  791. Uint16 *srcp = (Uint16 *)info->src;
  792. int srcskip = info->src_skip >> 1;
  793. Uint16 *dstp = (Uint16 *)info->dst;
  794. int dstskip = info->dst_skip >> 1;
  795. Uint32 s, d;
  796. __m64 src1, dst1, src2, dst2, rmask, gmask, bmask, mm_res, mm_alpha;
  797. alpha &= ~(1 + 2 + 4); /* cut alpha to get the exact same behaviour */
  798. mm_alpha = _mm_set_pi32(0, alpha); /* 0000000A -> mm_alpha */
  799. alpha >>= 3; /* downscale alpha to 5 bits */
  800. mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
  801. mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
  802. /* position alpha to allow for mullo and mulhi on diff channels
  803. to reduce the number of operations */
  804. mm_alpha = _mm_slli_si64(mm_alpha, 3);
  805. /* Setup the 555 color channel masks */
  806. rmask = _mm_set_pi32(0x7C007C00, 0x7C007C00); /* MASKRED -> rmask */
  807. gmask = _mm_set_pi32(0x03E003E0, 0x03E003E0); /* MASKGREEN -> gmask */
  808. bmask = _mm_set_pi32(0x001F001F, 0x001F001F); /* MASKBLUE -> bmask */
  809. while (height--) {
  810. /* *INDENT-OFF* */ /* clang-format off */
  811. DUFFS_LOOP_124(
  812. {
  813. s = *srcp++;
  814. d = *dstp;
  815. /*
  816. * shift out the middle component (green) to
  817. * the high 16 bits, and process all three RGB
  818. * components at the same time.
  819. */
  820. s = (s | s << 16) & 0x03e07c1f;
  821. d = (d | d << 16) & 0x03e07c1f;
  822. d += (s - d) * alpha >> 5;
  823. d &= 0x03e07c1f;
  824. *dstp++ = (Uint16)(d | d >> 16);
  825. },{
  826. s = *srcp++;
  827. d = *dstp;
  828. /*
  829. * shift out the middle component (green) to
  830. * the high 16 bits, and process all three RGB
  831. * components at the same time.
  832. */
  833. s = (s | s << 16) & 0x03e07c1f;
  834. d = (d | d << 16) & 0x03e07c1f;
  835. d += (s - d) * alpha >> 5;
  836. d &= 0x03e07c1f;
  837. *dstp++ = (Uint16)(d | d >> 16);
  838. s = *srcp++;
  839. d = *dstp;
  840. /*
  841. * shift out the middle component (green) to
  842. * the high 16 bits, and process all three RGB
  843. * components at the same time.
  844. */
  845. s = (s | s << 16) & 0x03e07c1f;
  846. d = (d | d << 16) & 0x03e07c1f;
  847. d += (s - d) * alpha >> 5;
  848. d &= 0x03e07c1f;
  849. *dstp++ = (Uint16)(d | d >> 16);
  850. },{
  851. src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
  852. dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
  853. /* red -- process the bits in place */
  854. src2 = src1;
  855. src2 = _mm_and_si64(src2, rmask); /* src & MASKRED -> src2 */
  856. dst2 = dst1;
  857. dst2 = _mm_and_si64(dst2, rmask); /* dst & MASKRED -> dst2 */
  858. /* blend */
  859. src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
  860. src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  861. src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
  862. dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
  863. dst2 = _mm_and_si64(dst2, rmask); /* dst2 & MASKRED -> dst2 */
  864. mm_res = dst2; /* RED -> mm_res */
  865. /* green -- process the bits in place */
  866. src2 = src1;
  867. src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
  868. dst2 = dst1;
  869. dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
  870. /* blend */
  871. src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
  872. src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  873. src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
  874. dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
  875. mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
  876. /* blue */
  877. src2 = src1; /* src -> src2 */
  878. src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
  879. dst2 = dst1; /* dst -> dst2 */
  880. dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
  881. /* blend */
  882. src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
  883. src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  884. src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
  885. dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
  886. dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
  887. mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
  888. *(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
  889. srcp += 4;
  890. dstp += 4;
  891. }, width);
  892. /* *INDENT-ON* */ /* clang-format on */
  893. srcp += srcskip;
  894. dstp += dstskip;
  895. }
  896. _mm_empty();
  897. }
  898. }
  899. #endif /* __MMX__ */
  900. /* fast RGB565->RGB565 blending with surface alpha */
  901. static void Blit565to565SurfaceAlpha(SDL_BlitInfo *info)
  902. {
  903. unsigned alpha = info->a;
  904. if (alpha == 128) {
  905. Blit16to16SurfaceAlpha128(info, 0xf7de);
  906. } else {
  907. int width = info->dst_w;
  908. int height = info->dst_h;
  909. Uint16 *srcp = (Uint16 *)info->src;
  910. int srcskip = info->src_skip >> 1;
  911. Uint16 *dstp = (Uint16 *)info->dst;
  912. int dstskip = info->dst_skip >> 1;
  913. alpha >>= 3; /* downscale alpha to 5 bits */
  914. while (height--) {
  915. /* *INDENT-OFF* */ /* clang-format off */
  916. DUFFS_LOOP4({
  917. Uint32 s = *srcp++;
  918. Uint32 d = *dstp;
  919. /*
  920. * shift out the middle component (green) to
  921. * the high 16 bits, and process all three RGB
  922. * components at the same time.
  923. */
  924. s = (s | s << 16) & 0x07e0f81f;
  925. d = (d | d << 16) & 0x07e0f81f;
  926. d += (s - d) * alpha >> 5;
  927. d &= 0x07e0f81f;
  928. *dstp++ = (Uint16)(d | d >> 16);
  929. }, width);
  930. /* *INDENT-ON* */ /* clang-format on */
  931. srcp += srcskip;
  932. dstp += dstskip;
  933. }
  934. }
  935. }
  936. /* fast RGB555->RGB555 blending with surface alpha */
  937. static void Blit555to555SurfaceAlpha(SDL_BlitInfo *info)
  938. {
  939. unsigned alpha = info->a; /* downscale alpha to 5 bits */
  940. if (alpha == 128) {
  941. Blit16to16SurfaceAlpha128(info, 0xfbde);
  942. } else {
  943. int width = info->dst_w;
  944. int height = info->dst_h;
  945. Uint16 *srcp = (Uint16 *)info->src;
  946. int srcskip = info->src_skip >> 1;
  947. Uint16 *dstp = (Uint16 *)info->dst;
  948. int dstskip = info->dst_skip >> 1;
  949. alpha >>= 3; /* downscale alpha to 5 bits */
  950. while (height--) {
  951. /* *INDENT-OFF* */ /* clang-format off */
  952. DUFFS_LOOP4({
  953. Uint32 s = *srcp++;
  954. Uint32 d = *dstp;
  955. /*
  956. * shift out the middle component (green) to
  957. * the high 16 bits, and process all three RGB
  958. * components at the same time.
  959. */
  960. s = (s | s << 16) & 0x03e07c1f;
  961. d = (d | d << 16) & 0x03e07c1f;
  962. d += (s - d) * alpha >> 5;
  963. d &= 0x03e07c1f;
  964. *dstp++ = (Uint16)(d | d >> 16);
  965. }, width);
  966. /* *INDENT-ON* */ /* clang-format on */
  967. srcp += srcskip;
  968. dstp += dstskip;
  969. }
  970. }
  971. }
  972. /* fast ARGB8888->RGB565 blending with pixel alpha */
  973. static void BlitARGBto565PixelAlpha(SDL_BlitInfo *info)
  974. {
  975. int width = info->dst_w;
  976. int height = info->dst_h;
  977. Uint32 *srcp = (Uint32 *)info->src;
  978. int srcskip = info->src_skip >> 2;
  979. Uint16 *dstp = (Uint16 *)info->dst;
  980. int dstskip = info->dst_skip >> 1;
  981. while (height--) {
  982. /* *INDENT-OFF* */ /* clang-format off */
  983. DUFFS_LOOP4({
  984. Uint32 s = *srcp;
  985. unsigned alpha = s >> 27; /* downscale alpha to 5 bits */
  986. /* FIXME: Here we special-case opaque alpha since the
  987. compositioning used (>>8 instead of /255) doesn't handle
  988. it correctly. Also special-case alpha=0 for speed?
  989. Benchmark this! */
  990. if (alpha) {
  991. if (alpha == (SDL_ALPHA_OPAQUE >> 3)) {
  992. *dstp = (Uint16)((s >> 8 & 0xf800) + (s >> 5 & 0x7e0) + (s >> 3 & 0x1f));
  993. } else {
  994. Uint32 d = *dstp;
  995. /*
  996. * convert source and destination to G0RAB65565
  997. * and blend all components at the same time
  998. */
  999. s = ((s & 0xfc00) << 11) + (s >> 8 & 0xf800)
  1000. + (s >> 3 & 0x1f);
  1001. d = (d | d << 16) & 0x07e0f81f;
  1002. d += (s - d) * alpha >> 5;
  1003. d &= 0x07e0f81f;
  1004. *dstp = (Uint16)(d | d >> 16);
  1005. }
  1006. }
  1007. srcp++;
  1008. dstp++;
  1009. }, width);
  1010. /* *INDENT-ON* */ /* clang-format on */
  1011. srcp += srcskip;
  1012. dstp += dstskip;
  1013. }
  1014. }
  1015. /* fast ARGB8888->RGB555 blending with pixel alpha */
  1016. static void BlitARGBto555PixelAlpha(SDL_BlitInfo *info)
  1017. {
  1018. int width = info->dst_w;
  1019. int height = info->dst_h;
  1020. Uint32 *srcp = (Uint32 *)info->src;
  1021. int srcskip = info->src_skip >> 2;
  1022. Uint16 *dstp = (Uint16 *)info->dst;
  1023. int dstskip = info->dst_skip >> 1;
  1024. while (height--) {
  1025. /* *INDENT-OFF* */ /* clang-format off */
  1026. DUFFS_LOOP4({
  1027. unsigned alpha;
  1028. Uint32 s = *srcp;
  1029. alpha = s >> 27; /* downscale alpha to 5 bits */
  1030. /* FIXME: Here we special-case opaque alpha since the
  1031. compositioning used (>>8 instead of /255) doesn't handle
  1032. it correctly. Also special-case alpha=0 for speed?
  1033. Benchmark this! */
  1034. if (alpha) {
  1035. if (alpha == (SDL_ALPHA_OPAQUE >> 3)) {
  1036. *dstp = (Uint16)((s >> 9 & 0x7c00) + (s >> 6 & 0x3e0) + (s >> 3 & 0x1f));
  1037. } else {
  1038. Uint32 d = *dstp;
  1039. /*
  1040. * convert source and destination to G0RAB65565
  1041. * and blend all components at the same time
  1042. */
  1043. s = ((s & 0xf800) << 10) + (s >> 9 & 0x7c00)
  1044. + (s >> 3 & 0x1f);
  1045. d = (d | d << 16) & 0x03e07c1f;
  1046. d += (s - d) * alpha >> 5;
  1047. d &= 0x03e07c1f;
  1048. *dstp = (Uint16)(d | d >> 16);
  1049. }
  1050. }
  1051. srcp++;
  1052. dstp++;
  1053. }, width);
  1054. /* *INDENT-ON* */ /* clang-format on */
  1055. srcp += srcskip;
  1056. dstp += dstskip;
  1057. }
  1058. }
  1059. /* General (slow) N->N blending with per-surface alpha */
  1060. static void BlitNtoNSurfaceAlpha(SDL_BlitInfo *info)
  1061. {
  1062. int width = info->dst_w;
  1063. int height = info->dst_h;
  1064. Uint8 *src = info->src;
  1065. int srcskip = info->src_skip;
  1066. Uint8 *dst = info->dst;
  1067. int dstskip = info->dst_skip;
  1068. SDL_PixelFormat *srcfmt = info->src_fmt;
  1069. SDL_PixelFormat *dstfmt = info->dst_fmt;
  1070. int srcbpp = srcfmt->BytesPerPixel;
  1071. int dstbpp = dstfmt->BytesPerPixel;
  1072. Uint32 Pixel;
  1073. unsigned sR, sG, sB;
  1074. unsigned dR, dG, dB, dA;
  1075. const unsigned sA = info->a;
  1076. if (sA) {
  1077. while (height--) {
  1078. /* *INDENT-OFF* */ /* clang-format off */
  1079. DUFFS_LOOP4(
  1080. {
  1081. DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
  1082. DISEMBLE_RGBA(dst, dstbpp, dstfmt, Pixel, dR, dG, dB, dA);
  1083. ALPHA_BLEND_RGBA(sR, sG, sB, sA, dR, dG, dB, dA);
  1084. ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
  1085. src += srcbpp;
  1086. dst += dstbpp;
  1087. },
  1088. width);
  1089. /* *INDENT-ON* */ /* clang-format on */
  1090. src += srcskip;
  1091. dst += dstskip;
  1092. }
  1093. }
  1094. }
  1095. /* General (slow) colorkeyed N->N blending with per-surface alpha */
  1096. static void BlitNtoNSurfaceAlphaKey(SDL_BlitInfo *info)
  1097. {
  1098. int width = info->dst_w;
  1099. int height = info->dst_h;
  1100. Uint8 *src = info->src;
  1101. int srcskip = info->src_skip;
  1102. Uint8 *dst = info->dst;
  1103. int dstskip = info->dst_skip;
  1104. SDL_PixelFormat *srcfmt = info->src_fmt;
  1105. SDL_PixelFormat *dstfmt = info->dst_fmt;
  1106. Uint32 ckey = info->colorkey;
  1107. int srcbpp = srcfmt->BytesPerPixel;
  1108. int dstbpp = dstfmt->BytesPerPixel;
  1109. Uint32 Pixel;
  1110. unsigned sR, sG, sB;
  1111. unsigned dR, dG, dB, dA;
  1112. const unsigned sA = info->a;
  1113. while (height--) {
  1114. /* *INDENT-OFF* */ /* clang-format off */
  1115. DUFFS_LOOP4(
  1116. {
  1117. RETRIEVE_RGB_PIXEL(src, srcbpp, Pixel);
  1118. if (sA && Pixel != ckey) {
  1119. RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB);
  1120. DISEMBLE_RGBA(dst, dstbpp, dstfmt, Pixel, dR, dG, dB, dA);
  1121. ALPHA_BLEND_RGBA(sR, sG, sB, sA, dR, dG, dB, dA);
  1122. ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
  1123. }
  1124. src += srcbpp;
  1125. dst += dstbpp;
  1126. },
  1127. width);
  1128. /* *INDENT-ON* */ /* clang-format on */
  1129. src += srcskip;
  1130. dst += dstskip;
  1131. }
  1132. }
  1133. /* General (slow) N->N blending with pixel alpha */
  1134. static void BlitNtoNPixelAlpha(SDL_BlitInfo *info)
  1135. {
  1136. int width = info->dst_w;
  1137. int height = info->dst_h;
  1138. Uint8 *src = info->src;
  1139. int srcskip = info->src_skip;
  1140. Uint8 *dst = info->dst;
  1141. int dstskip = info->dst_skip;
  1142. SDL_PixelFormat *srcfmt = info->src_fmt;
  1143. SDL_PixelFormat *dstfmt = info->dst_fmt;
  1144. int srcbpp;
  1145. int dstbpp;
  1146. Uint32 Pixel;
  1147. unsigned sR, sG, sB, sA;
  1148. unsigned dR, dG, dB, dA;
  1149. /* Set up some basic variables */
  1150. srcbpp = srcfmt->BytesPerPixel;
  1151. dstbpp = dstfmt->BytesPerPixel;
  1152. while (height--) {
  1153. /* *INDENT-OFF* */ /* clang-format off */
  1154. DUFFS_LOOP4(
  1155. {
  1156. DISEMBLE_RGBA(src, srcbpp, srcfmt, Pixel, sR, sG, sB, sA);
  1157. if (sA) {
  1158. DISEMBLE_RGBA(dst, dstbpp, dstfmt, Pixel, dR, dG, dB, dA);
  1159. ALPHA_BLEND_RGBA(sR, sG, sB, sA, dR, dG, dB, dA);
  1160. ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
  1161. }
  1162. src += srcbpp;
  1163. dst += dstbpp;
  1164. },
  1165. width);
  1166. /* *INDENT-ON* */ /* clang-format on */
  1167. src += srcskip;
  1168. dst += dstskip;
  1169. }
  1170. }
  1171. SDL_BlitFunc
  1172. SDL_CalculateBlitA(SDL_Surface *surface)
  1173. {
  1174. SDL_PixelFormat *sf = surface->format;
  1175. SDL_PixelFormat *df = surface->map->dst->format;
  1176. switch (surface->map->info.flags & ~SDL_COPY_RLE_MASK) {
  1177. case SDL_COPY_BLEND:
  1178. /* Per-pixel alpha blits */
  1179. switch (df->BytesPerPixel) {
  1180. case 1:
  1181. if (df->palette != NULL) {
  1182. return BlitNto1PixelAlpha;
  1183. } else {
  1184. /* RGB332 has no palette ! */
  1185. return BlitNtoNPixelAlpha;
  1186. }
  1187. case 2:
  1188. #if SDL_ARM_NEON_BLITTERS || SDL_ARM_SIMD_BLITTERS
  1189. if (sf->BytesPerPixel == 4 && sf->Amask == 0xff000000 && sf->Gmask == 0xff00 && df->Gmask == 0x7e0 && ((sf->Rmask == 0xff && df->Rmask == 0x1f) || (sf->Bmask == 0xff && df->Bmask == 0x1f))) {
  1190. #if SDL_ARM_NEON_BLITTERS
  1191. if (SDL_HasNEON()) {
  1192. return BlitARGBto565PixelAlphaARMNEON;
  1193. }
  1194. #endif
  1195. #if SDL_ARM_SIMD_BLITTERS
  1196. if (SDL_HasARMSIMD()) {
  1197. return BlitARGBto565PixelAlphaARMSIMD;
  1198. }
  1199. #endif
  1200. }
  1201. #endif
  1202. if (sf->BytesPerPixel == 4 && sf->Amask == 0xff000000 && sf->Gmask == 0xff00 && ((sf->Rmask == 0xff && df->Rmask == 0x1f) || (sf->Bmask == 0xff && df->Bmask == 0x1f))) {
  1203. if (df->Gmask == 0x7e0) {
  1204. return BlitARGBto565PixelAlpha;
  1205. } else if (df->Gmask == 0x3e0) {
  1206. return BlitARGBto555PixelAlpha;
  1207. }
  1208. }
  1209. return BlitNtoNPixelAlpha;
  1210. case 4:
  1211. if (sf->Rmask == df->Rmask && sf->Gmask == df->Gmask && sf->Bmask == df->Bmask && sf->BytesPerPixel == 4) {
  1212. #if defined(__MMX__)
  1213. if (sf->Rshift % 8 == 0 && sf->Gshift % 8 == 0 && sf->Bshift % 8 == 0 && sf->Ashift % 8 == 0 && sf->Aloss == 0) {
  1214. #ifdef __MMX__
  1215. if (SDL_HasMMX()) {
  1216. return BlitRGBtoRGBPixelAlphaMMX;
  1217. }
  1218. #endif
  1219. }
  1220. #endif /* __MMX__ */
  1221. if (sf->Amask == 0xff000000) {
  1222. #if SDL_ARM_NEON_BLITTERS
  1223. if (SDL_HasNEON()) {
  1224. return BlitRGBtoRGBPixelAlphaARMNEON;
  1225. }
  1226. #endif
  1227. #if SDL_ARM_SIMD_BLITTERS
  1228. if (SDL_HasARMSIMD()) {
  1229. return BlitRGBtoRGBPixelAlphaARMSIMD;
  1230. }
  1231. #endif
  1232. return BlitRGBtoRGBPixelAlpha;
  1233. }
  1234. } else if (sf->Rmask == df->Bmask && sf->Gmask == df->Gmask && sf->Bmask == df->Rmask && sf->BytesPerPixel == 4) {
  1235. if (sf->Amask == 0xff000000) {
  1236. return BlitRGBtoBGRPixelAlpha;
  1237. }
  1238. }
  1239. return BlitNtoNPixelAlpha;
  1240. case 3:
  1241. default:
  1242. break;
  1243. }
  1244. return BlitNtoNPixelAlpha;
  1245. case SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
  1246. if (sf->Amask == 0) {
  1247. /* Per-surface alpha blits */
  1248. switch (df->BytesPerPixel) {
  1249. case 1:
  1250. if (df->palette != NULL) {
  1251. return BlitNto1SurfaceAlpha;
  1252. } else {
  1253. /* RGB332 has no palette ! */
  1254. return BlitNtoNSurfaceAlpha;
  1255. }
  1256. case 2:
  1257. if (surface->map->identity) {
  1258. if (df->Gmask == 0x7e0) {
  1259. #ifdef __MMX__
  1260. if (SDL_HasMMX()) {
  1261. return Blit565to565SurfaceAlphaMMX;
  1262. } else
  1263. #endif
  1264. {
  1265. return Blit565to565SurfaceAlpha;
  1266. }
  1267. } else if (df->Gmask == 0x3e0) {
  1268. #ifdef __MMX__
  1269. if (SDL_HasMMX()) {
  1270. return Blit555to555SurfaceAlphaMMX;
  1271. } else
  1272. #endif
  1273. {
  1274. return Blit555to555SurfaceAlpha;
  1275. }
  1276. }
  1277. }
  1278. return BlitNtoNSurfaceAlpha;
  1279. case 4:
  1280. if (sf->Rmask == df->Rmask && sf->Gmask == df->Gmask && sf->Bmask == df->Bmask && sf->BytesPerPixel == 4) {
  1281. #ifdef __MMX__
  1282. if (sf->Rshift % 8 == 0 && sf->Gshift % 8 == 0 && sf->Bshift % 8 == 0 && SDL_HasMMX()) {
  1283. return BlitRGBtoRGBSurfaceAlphaMMX;
  1284. }
  1285. #endif
  1286. if ((sf->Rmask | sf->Gmask | sf->Bmask) == 0xffffff) {
  1287. return BlitRGBtoRGBSurfaceAlpha;
  1288. }
  1289. }
  1290. return BlitNtoNSurfaceAlpha;
  1291. case 3:
  1292. default:
  1293. return BlitNtoNSurfaceAlpha;
  1294. }
  1295. }
  1296. break;
  1297. case SDL_COPY_COLORKEY | SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
  1298. if (sf->Amask == 0) {
  1299. if (df->BytesPerPixel == 1) {
  1300. if (df->palette != NULL) {
  1301. return BlitNto1SurfaceAlphaKey;
  1302. } else {
  1303. /* RGB332 has no palette ! */
  1304. return BlitNtoNSurfaceAlphaKey;
  1305. }
  1306. } else {
  1307. return BlitNtoNSurfaceAlphaKey;
  1308. }
  1309. }
  1310. break;
  1311. }
  1312. return NULL;
  1313. }
  1314. #endif /* SDL_HAVE_BLIT_A */