SDL_blit_A.c 49 KB

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