SDL_blit_A.c 50 KB

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