SDL_stretch.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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. #include "SDL_video.h"
  20. #include "SDL_blit.h"
  21. #include "SDL_render.h"
  22. static int SDL_LowerSoftStretchNearest(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, const SDL_Rect *dstrect);
  23. static int SDL_LowerSoftStretchLinear(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, const SDL_Rect *dstrect);
  24. static int SDL_UpperSoftStretch(SDL_Surface * src, const SDL_Rect * srcrect, SDL_Surface * dst, const SDL_Rect * dstrect, SDL_ScaleMode scaleMode);
  25. int
  26. SDL_SoftStretch(SDL_Surface *src, const SDL_Rect *srcrect,
  27. SDL_Surface *dst, const SDL_Rect *dstrect)
  28. {
  29. return SDL_UpperSoftStretch(src, srcrect, dst, dstrect, SDL_ScaleModeNearest);
  30. }
  31. int
  32. SDL_SoftStretchLinear(SDL_Surface *src, const SDL_Rect *srcrect,
  33. SDL_Surface *dst, const SDL_Rect *dstrect)
  34. {
  35. return SDL_UpperSoftStretch(src, srcrect, dst, dstrect, SDL_ScaleModeLinear);
  36. }
  37. static int
  38. SDL_UpperSoftStretch(SDL_Surface * src, const SDL_Rect * srcrect,
  39. SDL_Surface * dst, const SDL_Rect * dstrect, SDL_ScaleMode scaleMode)
  40. {
  41. int ret;
  42. int src_locked;
  43. int dst_locked;
  44. SDL_Rect full_src;
  45. SDL_Rect full_dst;
  46. if (src->format->format != dst->format->format) {
  47. return SDL_SetError("Only works with same format surfaces");
  48. }
  49. if (scaleMode != SDL_ScaleModeNearest) {
  50. if (src->format->BytesPerPixel != 4 || src->format->format == SDL_PIXELFORMAT_ARGB2101010) {
  51. return SDL_SetError("Wrong format");
  52. }
  53. }
  54. /* Verify the blit rectangles */
  55. if (srcrect) {
  56. if ((srcrect->x < 0) || (srcrect->y < 0) ||
  57. ((srcrect->x + srcrect->w) > src->w) ||
  58. ((srcrect->y + srcrect->h) > src->h)) {
  59. return SDL_SetError("Invalid source blit rectangle");
  60. }
  61. } else {
  62. full_src.x = 0;
  63. full_src.y = 0;
  64. full_src.w = src->w;
  65. full_src.h = src->h;
  66. srcrect = &full_src;
  67. }
  68. if (dstrect) {
  69. if ((dstrect->x < 0) || (dstrect->y < 0) ||
  70. ((dstrect->x + dstrect->w) > dst->w) ||
  71. ((dstrect->y + dstrect->h) > dst->h)) {
  72. return SDL_SetError("Invalid destination blit rectangle");
  73. }
  74. } else {
  75. full_dst.x = 0;
  76. full_dst.y = 0;
  77. full_dst.w = dst->w;
  78. full_dst.h = dst->h;
  79. dstrect = &full_dst;
  80. }
  81. if (dstrect->w <= 0 || dstrect->h <= 0) {
  82. return 0;
  83. }
  84. /* Lock the destination if it's in hardware */
  85. dst_locked = 0;
  86. if (SDL_MUSTLOCK(dst)) {
  87. if (SDL_LockSurface(dst) < 0) {
  88. return SDL_SetError("Unable to lock destination surface");
  89. }
  90. dst_locked = 1;
  91. }
  92. /* Lock the source if it's in hardware */
  93. src_locked = 0;
  94. if (SDL_MUSTLOCK(src)) {
  95. if (SDL_LockSurface(src) < 0) {
  96. if (dst_locked) {
  97. SDL_UnlockSurface(dst);
  98. }
  99. return SDL_SetError("Unable to lock source surface");
  100. }
  101. src_locked = 1;
  102. }
  103. if (scaleMode == SDL_ScaleModeNearest) {
  104. ret = SDL_LowerSoftStretchNearest(src, srcrect, dst, dstrect);
  105. } else {
  106. ret = SDL_LowerSoftStretchLinear(src, srcrect, dst, dstrect);
  107. }
  108. /* We need to unlock the surfaces if they're locked */
  109. if (dst_locked) {
  110. SDL_UnlockSurface(dst);
  111. }
  112. if (src_locked) {
  113. SDL_UnlockSurface(src);
  114. }
  115. return ret;
  116. }
  117. /* bilinear interpolation precision must be < 8
  118. Because with SSE: add-multiply: _mm_madd_epi16 works with signed int
  119. so pixels 0xb1...... are negatives and false the result
  120. same in NEON probably */
  121. #define PRECISION 7
  122. #define FIXED_POINT(i) ((uint32_t)(i) << 16)
  123. #define SRC_INDEX(fp) ((uint32_t)(fp) >> 16)
  124. #define INTEGER(fp) ((uint32_t)(fp) >> PRECISION)
  125. #define FRAC(fp) ((uint32_t)(fp >> (16 - PRECISION)) & ((1<<PRECISION) - 1))
  126. #define FRAC_ZERO 0
  127. #define FRAC_ONE (1 << PRECISION)
  128. #define FP_ONE FIXED_POINT(1)
  129. #define NEAREST___START \
  130. int i; \
  131. int fp_sum_h, fp_step_h, left_pad_h, right_pad_h; \
  132. int fp_sum_w, fp_step_w, left_pad_w, right_pad_w; \
  133. int fp_sum_w_init, left_pad_w_init, right_pad_w_init, dst_gap, middle_init; \
  134. get_scaler_datas_nearest(src_h, dst_h, &fp_sum_h, &fp_step_h, &left_pad_h, &right_pad_h); \
  135. get_scaler_datas_nearest(src_w, dst_w, &fp_sum_w, &fp_step_w, &left_pad_w, &right_pad_w); \
  136. fp_sum_w_init = fp_sum_w + left_pad_w * fp_step_w; \
  137. left_pad_w_init = left_pad_w; \
  138. right_pad_w_init = right_pad_w; \
  139. dst_gap = dst_pitch - bpp * dst_w; \
  140. middle_init = dst_w - left_pad_w - right_pad_w; \
  141. #define BILINEAR___START \
  142. int i; \
  143. int fp_sum_h, fp_step_h, left_pad_h, right_pad_h; \
  144. int fp_sum_w, fp_step_w, left_pad_w, right_pad_w; \
  145. int fp_sum_w_init, left_pad_w_init, right_pad_w_init, dst_gap, middle_init; \
  146. get_scaler_datas(src_h, dst_h, &fp_sum_h, &fp_step_h, &left_pad_h, &right_pad_h); \
  147. get_scaler_datas(src_w, dst_w, &fp_sum_w, &fp_step_w, &left_pad_w, &right_pad_w); \
  148. fp_sum_w_init = fp_sum_w + left_pad_w * fp_step_w; \
  149. left_pad_w_init = left_pad_w; \
  150. right_pad_w_init = right_pad_w; \
  151. dst_gap = dst_pitch - 4 * dst_w; \
  152. middle_init = dst_w - left_pad_w - right_pad_w; \
  153. #define BILINEAR___HEIGHT \
  154. int index_h, frac_h0, frac_h1, middle; \
  155. const Uint32 *src_h0, *src_h1; \
  156. int no_padding, incr_h0, incr_h1; \
  157. \
  158. no_padding = !(i < left_pad_h || i > dst_h - 1 - right_pad_h); \
  159. index_h = SRC_INDEX(fp_sum_h); \
  160. frac_h0 = FRAC(fp_sum_h); \
  161. \
  162. index_h = no_padding ? index_h : (i < left_pad_h ? 0 : src_h - 1); \
  163. frac_h0 = no_padding ? frac_h0 : 0; \
  164. incr_h1 = no_padding ? src_pitch : 0; \
  165. incr_h0 = index_h * src_pitch; \
  166. \
  167. src_h0 = (const Uint32 *)((const Uint8 *)src + incr_h0); \
  168. src_h1 = (const Uint32 *)((const Uint8 *)src_h0 + incr_h1); \
  169. \
  170. fp_sum_h += fp_step_h; \
  171. \
  172. frac_h1 = FRAC_ONE - frac_h0; \
  173. fp_sum_w = fp_sum_w_init; \
  174. right_pad_w = right_pad_w_init; \
  175. left_pad_w = left_pad_w_init; \
  176. middle = middle_init; \
  177. #if defined(__clang__)
  178. // Remove inlining of this function
  179. // Compiler crash with clang 9.0.8 / android-ndk-r21d
  180. // Compiler crash with clang 11.0.3 / Xcode
  181. // OK with clang 11.0.5 / android-ndk-22
  182. // OK with clang 12.0.0 / Xcode
  183. __attribute__((noinline))
  184. #endif
  185. static void
  186. get_scaler_datas(int src_nb, int dst_nb, int *fp_start, int *fp_step, int *left_pad, int *right_pad)
  187. {
  188. int step = FIXED_POINT(src_nb) / (dst_nb); /* source step in fixed point */
  189. int x0 = FP_ONE / 2; /* dst first pixel center at 0.5 in fixed point */
  190. int fp_sum;
  191. int i;
  192. #if 0
  193. /* scale to source coordinates */
  194. x0 *= src_nb;
  195. x0 /= dst_nb; /* x0 == step / 2 */
  196. #else
  197. /* Use this code for perfect match with pixman */
  198. Sint64 tmp[2];
  199. tmp[0] = (Sint64)step * (x0 >> 16);
  200. tmp[1] = (Sint64)step * (x0 & 0xFFFF);
  201. x0 = (int) (tmp[0] + ((tmp[1] + 0x8000) >> 16)); /* x0 == (step + 1) / 2 */
  202. #endif
  203. /* -= 0.5, get back the pixel origin, in source coordinates */
  204. x0 -= FP_ONE / 2;
  205. *fp_start = x0;
  206. *fp_step = step;
  207. *left_pad = 0;
  208. *right_pad = 0;
  209. fp_sum = x0;
  210. for (i = 0; i < dst_nb; i++) {
  211. if (fp_sum < 0) {
  212. *left_pad += 1;
  213. } else {
  214. int index = SRC_INDEX(fp_sum);
  215. if (index > src_nb - 2) {
  216. *right_pad += 1;
  217. }
  218. }
  219. fp_sum += step;
  220. }
  221. // SDL_Log("%d -> %d x0=%d step=%d left_pad=%d right_pad=%d", src_nb, dst_nb, *fp_start, *fp_step, *left_pad, *right_pad);
  222. }
  223. typedef struct color_t {
  224. Uint8 a;
  225. Uint8 b;
  226. Uint8 c;
  227. Uint8 d;
  228. } color_t;
  229. #if 0
  230. static void
  231. printf_64(const char *str, void *var)
  232. {
  233. uint8_t *val = (uint8_t*) var;
  234. printf(" * %s: %02x %02x %02x %02x _ %02x %02x %02x %02x\n",
  235. str, val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7]);
  236. }
  237. #endif
  238. /* Interpolated == x0 + frac * (x1 - x0) == x0 * (1 - frac) + x1 * frac */
  239. static SDL_INLINE void
  240. INTERPOL(const Uint32 *src_x0, const Uint32 *src_x1, int frac0, int frac1, Uint32 *dst)
  241. {
  242. const color_t *c0 = (const color_t *)src_x0;
  243. const color_t *c1 = (const color_t *)src_x1;
  244. color_t *cx = (color_t *)dst;
  245. #if 0
  246. cx->a = c0->a + INTEGER(frac0 * (c1->a - c0->a));
  247. cx->b = c0->b + INTEGER(frac0 * (c1->b - c0->b));
  248. cx->c = c0->c + INTEGER(frac0 * (c1->c - c0->c));
  249. cx->d = c0->d + INTEGER(frac0 * (c1->d - c0->d));
  250. #else
  251. cx->a = INTEGER(frac1 * c0->a + frac0 * c1->a);
  252. cx->b = INTEGER(frac1 * c0->b + frac0 * c1->b);
  253. cx->c = INTEGER(frac1 * c0->c + frac0 * c1->c);
  254. cx->d = INTEGER(frac1 * c0->d + frac0 * c1->d);
  255. #endif
  256. }
  257. static SDL_INLINE void
  258. INTERPOL_BILINEAR(const Uint32 *s0, const Uint32 *s1, int frac_w0, int frac_h0, int frac_h1, Uint32 *dst)
  259. {
  260. Uint32 tmp[2];
  261. unsigned int frac_w1 = FRAC_ONE - frac_w0;
  262. /* Vertical first, store to 'tmp' */
  263. INTERPOL(s0, s1, frac_h0, frac_h1, tmp);
  264. INTERPOL(s0 + 1, s1 + 1, frac_h0, frac_h1, tmp + 1);
  265. /* Horizontal, store to 'dst' */
  266. INTERPOL(tmp, tmp + 1, frac_w0, frac_w1, dst);
  267. }
  268. static int
  269. scale_mat(const Uint32 *src, int src_w, int src_h, int src_pitch,
  270. Uint32 *dst, int dst_w, int dst_h, int dst_pitch)
  271. {
  272. BILINEAR___START
  273. for (i = 0; i < dst_h; i++) {
  274. BILINEAR___HEIGHT
  275. while (left_pad_w--) {
  276. INTERPOL_BILINEAR(src_h0, src_h1, FRAC_ZERO, frac_h0, frac_h1, dst);
  277. dst += 1;
  278. }
  279. while (middle--) {
  280. const Uint32 *s_00_01;
  281. const Uint32 *s_10_11;
  282. int index_w = 4 * SRC_INDEX(fp_sum_w);
  283. int frac_w = FRAC(fp_sum_w);
  284. fp_sum_w += fp_step_w;
  285. /*
  286. x00 ... x0_ ..... x01
  287. . . .
  288. . x .
  289. . . .
  290. . . .
  291. x10 ... x1_ ..... x11
  292. */
  293. s_00_01 = (const Uint32 *)((const Uint8 *)src_h0 + index_w);
  294. s_10_11 = (const Uint32 *)((const Uint8 *)src_h1 + index_w);
  295. INTERPOL_BILINEAR(s_00_01, s_10_11, frac_w, frac_h0, frac_h1, dst);
  296. dst += 1;
  297. }
  298. while (right_pad_w--) {
  299. int index_w = 4 * (src_w - 2);
  300. const Uint32 *s_00_01 = (const Uint32 *)((const Uint8 *)src_h0 + index_w);
  301. const Uint32 *s_10_11 = (const Uint32 *)((const Uint8 *)src_h1 + index_w);
  302. INTERPOL_BILINEAR(s_00_01, s_10_11, FRAC_ONE, frac_h0, frac_h1, dst);
  303. dst += 1;
  304. }
  305. dst = (Uint32 *)((Uint8 *)dst + dst_gap);
  306. }
  307. return 0;
  308. }
  309. #if defined(__SSE2__)
  310. # define HAVE_SSE2_INTRINSICS 1
  311. #endif
  312. #if defined(__ARM_NEON)
  313. # define HAVE_NEON_INTRINSICS 1
  314. # define CAST_uint8x8_t (uint8x8_t)
  315. # define CAST_uint32x2_t (uint32x2_t)
  316. #endif
  317. #if defined(__WINRT__) || defined(_MSC_VER)
  318. # if defined(HAVE_NEON_INTRINSICS)
  319. # undef CAST_uint8x8_t
  320. # undef CAST_uint32x2_t
  321. # define CAST_uint8x8_t
  322. # define CAST_uint32x2_t
  323. # endif
  324. #endif
  325. #if defined(HAVE_SSE2_INTRINSICS)
  326. #if 0
  327. static void
  328. printf_128(const char *str, __m128i var)
  329. {
  330. uint16_t *val = (uint16_t*) &var;
  331. printf(" * %s: %04x %04x %04x %04x _ %04x %04x %04x %04x\n",
  332. str, val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7]);
  333. }
  334. #endif
  335. static SDL_INLINE int
  336. hasSSE2()
  337. {
  338. static int val = -1;
  339. if (val != -1) {
  340. return val;
  341. }
  342. val = SDL_HasSSE2();
  343. return val;
  344. }
  345. static SDL_INLINE void
  346. INTERPOL_BILINEAR_SSE(const Uint32 *s0, const Uint32 *s1, int frac_w, __m128i v_frac_h0, __m128i v_frac_h1, Uint32 *dst, __m128i zero)
  347. {
  348. __m128i x_00_01, x_10_11; /* Pixels in 4*uint8 in row */
  349. __m128i v_frac_w0, k0, l0, d0, e0;
  350. int f, f2;
  351. f = frac_w;
  352. f2 = FRAC_ONE - frac_w;
  353. v_frac_w0 = _mm_set_epi16(f, f2, f, f2, f, f2, f, f2);
  354. x_00_01 = _mm_loadl_epi64((const __m128i *)s0); /* Load x00 and x01 */
  355. x_10_11 = _mm_loadl_epi64((const __m128i *)s1);
  356. /* Interpolated == x0 + frac * (x1 - x0) == x0 * (1 - frac) + x1 * frac */
  357. /* Interpolation vertical */
  358. k0 = _mm_mullo_epi16(_mm_unpacklo_epi8(x_00_01, zero), v_frac_h1);
  359. l0 = _mm_mullo_epi16(_mm_unpacklo_epi8(x_10_11, zero), v_frac_h0);
  360. k0 = _mm_add_epi16(k0, l0);
  361. /* For perfect match, clear the factionnal part eventually. */
  362. /*
  363. k0 = _mm_srli_epi16(k0, PRECISION);
  364. k0 = _mm_slli_epi16(k0, PRECISION);
  365. */
  366. /* Interpolation horizontal */
  367. l0 = _mm_unpacklo_epi64(/* unused */ l0, k0);
  368. k0 = _mm_madd_epi16(_mm_unpackhi_epi16(l0, k0), v_frac_w0);
  369. /* Store 1 pixel */
  370. d0 = _mm_srli_epi32(k0, PRECISION * 2);
  371. e0 = _mm_packs_epi32(d0, d0);
  372. e0 = _mm_packus_epi16(e0, e0);
  373. *dst = _mm_cvtsi128_si32(e0);
  374. }
  375. static int
  376. scale_mat_SSE(const Uint32 *src, int src_w, int src_h, int src_pitch, Uint32 *dst, int dst_w, int dst_h, int dst_pitch)
  377. {
  378. BILINEAR___START
  379. for (i = 0; i < dst_h; i++) {
  380. int nb_block2;
  381. __m128i v_frac_h0;
  382. __m128i v_frac_h1;
  383. __m128i zero;
  384. BILINEAR___HEIGHT
  385. nb_block2 = middle / 2;
  386. v_frac_h0 = _mm_set_epi16(frac_h0, frac_h0, frac_h0, frac_h0, frac_h0, frac_h0, frac_h0, frac_h0);
  387. v_frac_h1 = _mm_set_epi16(frac_h1, frac_h1, frac_h1, frac_h1, frac_h1, frac_h1, frac_h1, frac_h1);
  388. zero = _mm_setzero_si128();
  389. while (left_pad_w--) {
  390. INTERPOL_BILINEAR_SSE(src_h0, src_h1, FRAC_ZERO, v_frac_h0, v_frac_h1, dst, zero);
  391. dst += 1;
  392. }
  393. while (nb_block2--) {
  394. int index_w_0, frac_w_0;
  395. int index_w_1, frac_w_1;
  396. const Uint32 *s_00_01, *s_02_03, *s_10_11, *s_12_13;
  397. __m128i x_00_01, x_10_11, x_02_03, x_12_13;/* Pixels in 4*uint8 in row */
  398. __m128i v_frac_w0, k0, l0, d0, e0;
  399. __m128i v_frac_w1, k1, l1, d1, e1;
  400. int f, f2;
  401. index_w_0 = 4 * SRC_INDEX(fp_sum_w);
  402. frac_w_0 = FRAC(fp_sum_w);
  403. fp_sum_w += fp_step_w;
  404. index_w_1 = 4 * SRC_INDEX(fp_sum_w);
  405. frac_w_1 = FRAC(fp_sum_w);
  406. fp_sum_w += fp_step_w;
  407. /*
  408. x00............ x01 x02...........x03
  409. . . . . . .
  410. j0 f0 j1 j2 f1 j3
  411. . . . . . .
  412. . . . . . .
  413. . . . . . .
  414. x10............ x11 x12...........x13
  415. */
  416. s_00_01 = (const Uint32 *)((const Uint8 *)src_h0 + index_w_0);
  417. s_02_03 = (const Uint32 *)((const Uint8 *)src_h0 + index_w_1);
  418. s_10_11 = (const Uint32 *)((const Uint8 *)src_h1 + index_w_0);
  419. s_12_13 = (const Uint32 *)((const Uint8 *)src_h1 + index_w_1);
  420. f = frac_w_0;
  421. f2 = FRAC_ONE - frac_w_0;
  422. v_frac_w0 = _mm_set_epi16(f, f2, f, f2, f, f2, f, f2);
  423. f = frac_w_1;
  424. f2 = FRAC_ONE - frac_w_1;
  425. v_frac_w1 = _mm_set_epi16(f, f2, f, f2, f, f2, f, f2);
  426. x_00_01 = _mm_loadl_epi64((const __m128i *)s_00_01); /* Load x00 and x01 */
  427. x_02_03 = _mm_loadl_epi64((const __m128i *)s_02_03);
  428. x_10_11 = _mm_loadl_epi64((const __m128i *)s_10_11);
  429. x_12_13 = _mm_loadl_epi64((const __m128i *)s_12_13);
  430. /* Interpolation vertical */
  431. k0 = _mm_mullo_epi16(_mm_unpacklo_epi8(x_00_01, zero), v_frac_h1);
  432. l0 = _mm_mullo_epi16(_mm_unpacklo_epi8(x_10_11, zero), v_frac_h0);
  433. k0 = _mm_add_epi16(k0, l0);
  434. k1 = _mm_mullo_epi16(_mm_unpacklo_epi8(x_02_03, zero), v_frac_h1);
  435. l1 = _mm_mullo_epi16(_mm_unpacklo_epi8(x_12_13, zero), v_frac_h0);
  436. k1 = _mm_add_epi16(k1, l1);
  437. /* Interpolation horizontal */
  438. l0 = _mm_unpacklo_epi64(/* unused */ l0, k0);
  439. k0 = _mm_madd_epi16(_mm_unpackhi_epi16(l0, k0), v_frac_w0);
  440. l1 = _mm_unpacklo_epi64(/* unused */ l1, k1);
  441. k1 = _mm_madd_epi16(_mm_unpackhi_epi16(l1, k1), v_frac_w1);
  442. /* Store 1 pixel */
  443. d0 = _mm_srli_epi32(k0, PRECISION * 2);
  444. e0 = _mm_packs_epi32(d0, d0);
  445. e0 = _mm_packus_epi16(e0, e0);
  446. *dst++ = _mm_cvtsi128_si32(e0);
  447. /* Store 1 pixel */
  448. d1 = _mm_srli_epi32(k1, PRECISION * 2);
  449. e1 = _mm_packs_epi32(d1, d1);
  450. e1 = _mm_packus_epi16(e1, e1);
  451. *dst++ = _mm_cvtsi128_si32(e1);
  452. }
  453. /* Last point */
  454. if (middle & 0x1) {
  455. const Uint32 *s_00_01;
  456. const Uint32 *s_10_11;
  457. int index_w = 4 * SRC_INDEX(fp_sum_w);
  458. int frac_w = FRAC(fp_sum_w);
  459. fp_sum_w += fp_step_w;
  460. s_00_01 = (const Uint32 *)((const Uint8 *)src_h0 + index_w);
  461. s_10_11 = (const Uint32 *)((const Uint8 *)src_h1 + index_w);
  462. INTERPOL_BILINEAR_SSE(s_00_01, s_10_11, frac_w, v_frac_h0, v_frac_h1, dst, zero);
  463. dst += 1;
  464. }
  465. while (right_pad_w--) {
  466. int index_w = 4 * (src_w - 2);
  467. const Uint32 *s_00_01 = (const Uint32 *)((const Uint8 *)src_h0 + index_w);
  468. const Uint32 *s_10_11 = (const Uint32 *)((const Uint8 *)src_h1 + index_w);
  469. INTERPOL_BILINEAR_SSE(s_00_01, s_10_11, FRAC_ONE, v_frac_h0, v_frac_h1, dst, zero);
  470. dst += 1;
  471. }
  472. dst = (Uint32 *)((Uint8 *)dst + dst_gap);
  473. }
  474. return 0;
  475. }
  476. #endif
  477. #if defined(HAVE_NEON_INTRINSICS)
  478. static SDL_INLINE int
  479. hasNEON()
  480. {
  481. static int val = -1;
  482. if (val != -1) {
  483. return val;
  484. }
  485. val = SDL_HasNEON();
  486. return val;
  487. }
  488. static SDL_INLINE void
  489. INTERPOL_BILINEAR_NEON(const Uint32 *s0, const Uint32 *s1, int frac_w, uint8x8_t v_frac_h0, uint8x8_t v_frac_h1, Uint32 *dst)
  490. {
  491. uint8x8_t x_00_01, x_10_11; /* Pixels in 4*uint8 in row */
  492. uint16x8_t k0;
  493. uint32x4_t l0;
  494. uint16x8_t d0;
  495. uint8x8_t e0;
  496. x_00_01 = CAST_uint8x8_t vld1_u32(s0); /* Load 2 pixels */
  497. x_10_11 = CAST_uint8x8_t vld1_u32(s1);
  498. /* Interpolated == x0 + frac * (x1 - x0) == x0 * (1 - frac) + x1 * frac */
  499. k0 = vmull_u8(x_00_01, v_frac_h1); /* k0 := x0 * (1 - frac) */
  500. k0 = vmlal_u8(k0, x_10_11, v_frac_h0); /* k0 += x1 * frac */
  501. /* k0 now contains 2 interpolated pixels { j0, j1 } */
  502. l0 = vshll_n_u16(vget_low_u16(k0), PRECISION);
  503. l0 = vmlsl_n_u16(l0, vget_low_u16(k0), frac_w);
  504. l0 = vmlal_n_u16(l0, vget_high_u16(k0), frac_w);
  505. /* Shift and narrow */
  506. d0 = vcombine_u16(
  507. /* uint16x4_t */ vshrn_n_u32(l0, 2 * PRECISION),
  508. /* uint16x4_t */ vshrn_n_u32(l0, 2 * PRECISION)
  509. );
  510. /* Narrow again */
  511. e0 = vmovn_u16(d0);
  512. /* Store 1 pixel */
  513. *dst = vget_lane_u32(CAST_uint32x2_t e0, 0);
  514. }
  515. static int
  516. scale_mat_NEON(const Uint32 *src, int src_w, int src_h, int src_pitch, Uint32 *dst, int dst_w, int dst_h, int dst_pitch)
  517. {
  518. BILINEAR___START
  519. for (i = 0; i < dst_h; i++) {
  520. int nb_block4;
  521. uint8x8_t v_frac_h0, v_frac_h1;
  522. BILINEAR___HEIGHT
  523. nb_block4 = middle / 4;
  524. v_frac_h0 = vmov_n_u8(frac_h0);
  525. v_frac_h1 = vmov_n_u8(frac_h1);
  526. while (left_pad_w--) {
  527. INTERPOL_BILINEAR_NEON(src_h0, src_h1, FRAC_ZERO, v_frac_h0, v_frac_h1, dst);
  528. dst += 1;
  529. }
  530. while (nb_block4--) {
  531. int index_w_0, frac_w_0;
  532. int index_w_1, frac_w_1;
  533. int index_w_2, frac_w_2;
  534. int index_w_3, frac_w_3;
  535. const Uint32 *s_00_01, *s_02_03, *s_04_05, *s_06_07;
  536. const Uint32 *s_10_11, *s_12_13, *s_14_15, *s_16_17;
  537. uint8x8_t x_00_01, x_10_11, x_02_03, x_12_13;/* Pixels in 4*uint8 in row */
  538. uint8x8_t x_04_05, x_14_15, x_06_07, x_16_17;
  539. uint16x8_t k0, k1, k2, k3;
  540. uint32x4_t l0, l1, l2, l3;
  541. uint16x8_t d0, d1;
  542. uint8x8_t e0, e1;
  543. uint32x4_t f0;
  544. index_w_0 = 4 * SRC_INDEX(fp_sum_w);
  545. frac_w_0 = FRAC(fp_sum_w);
  546. fp_sum_w += fp_step_w;
  547. index_w_1 = 4 * SRC_INDEX(fp_sum_w);
  548. frac_w_1 = FRAC(fp_sum_w);
  549. fp_sum_w += fp_step_w;
  550. index_w_2 = 4 * SRC_INDEX(fp_sum_w);
  551. frac_w_2 = FRAC(fp_sum_w);
  552. fp_sum_w += fp_step_w;
  553. index_w_3 = 4 * SRC_INDEX(fp_sum_w);
  554. frac_w_3 = FRAC(fp_sum_w);
  555. fp_sum_w += fp_step_w;
  556. s_00_01 = (const Uint32 *)((const Uint8 *)src_h0 + index_w_0);
  557. s_02_03 = (const Uint32 *)((const Uint8 *)src_h0 + index_w_1);
  558. s_04_05 = (const Uint32 *)((const Uint8 *)src_h0 + index_w_2);
  559. s_06_07 = (const Uint32 *)((const Uint8 *)src_h0 + index_w_3);
  560. s_10_11 = (const Uint32 *)((const Uint8 *)src_h1 + index_w_0);
  561. s_12_13 = (const Uint32 *)((const Uint8 *)src_h1 + index_w_1);
  562. s_14_15 = (const Uint32 *)((const Uint8 *)src_h1 + index_w_2);
  563. s_16_17 = (const Uint32 *)((const Uint8 *)src_h1 + index_w_3);
  564. /* Interpolation vertical */
  565. x_00_01 = CAST_uint8x8_t vld1_u32(s_00_01); /* Load 2 pixels */
  566. x_02_03 = CAST_uint8x8_t vld1_u32(s_02_03);
  567. x_04_05 = CAST_uint8x8_t vld1_u32(s_04_05);
  568. x_06_07 = CAST_uint8x8_t vld1_u32(s_06_07);
  569. x_10_11 = CAST_uint8x8_t vld1_u32(s_10_11);
  570. x_12_13 = CAST_uint8x8_t vld1_u32(s_12_13);
  571. x_14_15 = CAST_uint8x8_t vld1_u32(s_14_15);
  572. x_16_17 = CAST_uint8x8_t vld1_u32(s_16_17);
  573. /* Interpolated == x0 + frac * (x1 - x0) == x0 * (1 - frac) + x1 * frac */
  574. k0 = vmull_u8(x_00_01, v_frac_h1); /* k0 := x0 * (1 - frac) */
  575. k0 = vmlal_u8(k0, x_10_11, v_frac_h0); /* k0 += x1 * frac */
  576. k1 = vmull_u8(x_02_03, v_frac_h1);
  577. k1 = vmlal_u8(k1, x_12_13, v_frac_h0);
  578. k2 = vmull_u8(x_04_05, v_frac_h1);
  579. k2 = vmlal_u8(k2, x_14_15, v_frac_h0);
  580. k3 = vmull_u8(x_06_07, v_frac_h1);
  581. k3 = vmlal_u8(k3, x_16_17, v_frac_h0);
  582. /* k0 now contains 2 interpolated pixels { j0, j1 } */
  583. /* k1 now contains 2 interpolated pixels { j2, j3 } */
  584. /* k2 now contains 2 interpolated pixels { j4, j5 } */
  585. /* k3 now contains 2 interpolated pixels { j6, j7 } */
  586. l0 = vshll_n_u16(vget_low_u16(k0), PRECISION);
  587. l0 = vmlsl_n_u16(l0, vget_low_u16(k0), frac_w_0);
  588. l0 = vmlal_n_u16(l0, vget_high_u16(k0), frac_w_0);
  589. l1 = vshll_n_u16(vget_low_u16(k1), PRECISION);
  590. l1 = vmlsl_n_u16(l1, vget_low_u16(k1), frac_w_1);
  591. l1 = vmlal_n_u16(l1, vget_high_u16(k1), frac_w_1);
  592. l2 = vshll_n_u16(vget_low_u16(k2), PRECISION);
  593. l2 = vmlsl_n_u16(l2, vget_low_u16(k2), frac_w_2);
  594. l2 = vmlal_n_u16(l2, vget_high_u16(k2), frac_w_2);
  595. l3 = vshll_n_u16(vget_low_u16(k3), PRECISION);
  596. l3 = vmlsl_n_u16(l3, vget_low_u16(k3), frac_w_3);
  597. l3 = vmlal_n_u16(l3, vget_high_u16(k3), frac_w_3);
  598. /* shift and narrow */
  599. d0 = vcombine_u16(
  600. /* uint16x4_t */ vshrn_n_u32(l0, 2 * PRECISION),
  601. /* uint16x4_t */ vshrn_n_u32(l1, 2 * PRECISION)
  602. );
  603. /* narrow again */
  604. e0 = vmovn_u16(d0);
  605. /* Shift and narrow */
  606. d1 = vcombine_u16(
  607. /* uint16x4_t */ vshrn_n_u32(l2, 2 * PRECISION),
  608. /* uint16x4_t */ vshrn_n_u32(l3, 2 * PRECISION)
  609. );
  610. /* Narrow again */
  611. e1 = vmovn_u16(d1);
  612. f0 = vcombine_u32(CAST_uint32x2_t e0, CAST_uint32x2_t e1);
  613. /* Store 4 pixels */
  614. vst1q_u32(dst, f0);
  615. dst += 4;
  616. }
  617. if (middle & 0x2) {
  618. int index_w_0, frac_w_0;
  619. int index_w_1, frac_w_1;
  620. const Uint32 *s_00_01, *s_02_03;
  621. const Uint32 *s_10_11, *s_12_13;
  622. uint8x8_t x_00_01, x_10_11, x_02_03, x_12_13;/* Pixels in 4*uint8 in row */
  623. uint16x8_t k0, k1;
  624. uint32x4_t l0, l1;
  625. uint16x8_t d0;
  626. uint8x8_t e0;
  627. index_w_0 = 4 * SRC_INDEX(fp_sum_w);
  628. frac_w_0 = FRAC(fp_sum_w);
  629. fp_sum_w += fp_step_w;
  630. index_w_1 = 4 * SRC_INDEX(fp_sum_w);
  631. frac_w_1 = FRAC(fp_sum_w);
  632. fp_sum_w += fp_step_w;
  633. /*
  634. x00............ x01 x02...........x03
  635. . . . . . .
  636. j0 dest0 j1 j2 dest1 j3
  637. . . . . . .
  638. . . . . . .
  639. . . . . . .
  640. x10............ x11 x12...........x13
  641. */
  642. s_00_01 = (const Uint32 *)((const Uint8 *)src_h0 + index_w_0);
  643. s_02_03 = (const Uint32 *)((const Uint8 *)src_h0 + index_w_1);
  644. s_10_11 = (const Uint32 *)((const Uint8 *)src_h1 + index_w_0);
  645. s_12_13 = (const Uint32 *)((const Uint8 *)src_h1 + index_w_1);
  646. /* Interpolation vertical */
  647. x_00_01 = CAST_uint8x8_t vld1_u32(s_00_01);/* Load 2 pixels */
  648. x_02_03 = CAST_uint8x8_t vld1_u32(s_02_03);
  649. x_10_11 = CAST_uint8x8_t vld1_u32(s_10_11);
  650. x_12_13 = CAST_uint8x8_t vld1_u32(s_12_13);
  651. /* Interpolated == x0 + frac * (x1 - x0) == x0 * (1 - frac) + x1 * frac */
  652. k0 = vmull_u8(x_00_01, v_frac_h1); /* k0 := x0 * (1 - frac) */
  653. k0 = vmlal_u8(k0, x_10_11, v_frac_h0); /* k0 += x1 * frac */
  654. k1 = vmull_u8(x_02_03, v_frac_h1);
  655. k1 = vmlal_u8(k1, x_12_13, v_frac_h0);
  656. /* k0 now contains 2 interpolated pixels { j0, j1 } */
  657. /* k1 now contains 2 interpolated pixels { j2, j3 } */
  658. l0 = vshll_n_u16(vget_low_u16(k0), PRECISION);
  659. l0 = vmlsl_n_u16(l0, vget_low_u16(k0), frac_w_0);
  660. l0 = vmlal_n_u16(l0, vget_high_u16(k0), frac_w_0);
  661. l1 = vshll_n_u16(vget_low_u16(k1), PRECISION);
  662. l1 = vmlsl_n_u16(l1, vget_low_u16(k1), frac_w_1);
  663. l1 = vmlal_n_u16(l1, vget_high_u16(k1), frac_w_1);
  664. /* Shift and narrow */
  665. d0 = vcombine_u16(
  666. /* uint16x4_t */ vshrn_n_u32(l0, 2 * PRECISION),
  667. /* uint16x4_t */ vshrn_n_u32(l1, 2 * PRECISION)
  668. );
  669. /* Narrow again */
  670. e0 = vmovn_u16(d0);
  671. /* Store 2 pixels */
  672. vst1_u32(dst, CAST_uint32x2_t e0);
  673. dst += 2;
  674. }
  675. /* Last point */
  676. if (middle & 0x1) {
  677. int index_w = 4 * SRC_INDEX(fp_sum_w);
  678. int frac_w = FRAC(fp_sum_w);
  679. const Uint32 *s_00_01 = (const Uint32 *)((const Uint8 *)src_h0 + index_w);
  680. const Uint32 *s_10_11 = (const Uint32 *)((const Uint8 *)src_h1 + index_w);
  681. INTERPOL_BILINEAR_NEON(s_00_01, s_10_11, frac_w, v_frac_h0, v_frac_h1, dst);
  682. dst += 1;
  683. }
  684. while (right_pad_w--) {
  685. int index_w = 4 * (src_w - 2);
  686. const Uint32 *s_00_01 = (const Uint32 *)((const Uint8 *)src_h0 + index_w);
  687. const Uint32 *s_10_11 = (const Uint32 *)((const Uint8 *)src_h1 + index_w);
  688. INTERPOL_BILINEAR_NEON(s_00_01, s_10_11, FRAC_ONE, v_frac_h0, v_frac_h1, dst);
  689. dst += 1;
  690. }
  691. dst = (Uint32 *)((Uint8 *)dst + dst_gap);
  692. }
  693. return 0;
  694. }
  695. #endif
  696. int
  697. SDL_LowerSoftStretchLinear(SDL_Surface *s, const SDL_Rect *srcrect,
  698. SDL_Surface *d, const SDL_Rect *dstrect)
  699. {
  700. int ret = -1;
  701. int src_w = srcrect->w;
  702. int src_h = srcrect->h;
  703. int dst_w = dstrect->w;
  704. int dst_h = dstrect->h;
  705. int src_pitch = s->pitch;
  706. int dst_pitch = d->pitch;
  707. Uint32 *src = (Uint32 *) ((Uint8 *)s->pixels + srcrect->x * 4 + srcrect->y * src_pitch);
  708. Uint32 *dst = (Uint32 *) ((Uint8 *)d->pixels + dstrect->x * 4 + dstrect->y * dst_pitch);
  709. #if defined(HAVE_NEON_INTRINSICS)
  710. if (ret == -1 && hasNEON()) {
  711. ret = scale_mat_NEON(src, src_w, src_h, src_pitch, dst, dst_w, dst_h, dst_pitch);
  712. }
  713. #endif
  714. #if defined(HAVE_SSE2_INTRINSICS)
  715. if (ret == -1 && hasSSE2()) {
  716. ret = scale_mat_SSE(src, src_w, src_h, src_pitch, dst, dst_w, dst_h, dst_pitch);
  717. }
  718. #endif
  719. if (ret == -1) {
  720. ret = scale_mat(src, src_w, src_h, src_pitch, dst, dst_w, dst_h, dst_pitch);
  721. }
  722. return ret;
  723. }
  724. static SDL_INLINE void
  725. get_scaler_datas_nearest(int src_nb, int dst_nb, int *fp_start, int *fp_step, int *left_pad, int *right_pad)
  726. {
  727. *fp_start = 0;
  728. *fp_step = (src_nb << 16) / dst_nb;
  729. *left_pad = 0;
  730. *right_pad = 0;
  731. }
  732. static int
  733. scale_mat_nearest_1(const Uint32 *src, int src_w, int src_h, int src_pitch,
  734. Uint32 *dst, int dst_w, int dst_h, int dst_pitch)
  735. {
  736. const int bpp = 1;
  737. NEAREST___START
  738. for (i = 0; i < dst_h; i++) {
  739. BILINEAR___HEIGHT
  740. while (middle--) {
  741. const Uint32 *s_00_01;
  742. int index_w = bpp * SRC_INDEX(fp_sum_w);
  743. fp_sum_w += fp_step_w;
  744. s_00_01 = (const Uint32 *)((const Uint8 *)src_h0 + index_w);
  745. *(Uint8*)dst = *(Uint8*)s_00_01;
  746. dst = (Uint32 *)((Uint8*)dst + bpp);
  747. }
  748. dst = (Uint32 *)((Uint8 *)dst + dst_gap);
  749. }
  750. return 0;
  751. }
  752. static int
  753. scale_mat_nearest_2(const Uint32 *src, int src_w, int src_h, int src_pitch,
  754. Uint32 *dst, int dst_w, int dst_h, int dst_pitch)
  755. {
  756. const int bpp = 2;
  757. NEAREST___START
  758. for (i = 0; i < dst_h; i++) {
  759. BILINEAR___HEIGHT
  760. while (middle--) {
  761. const Uint32 *s_00_01;
  762. int index_w = bpp * SRC_INDEX(fp_sum_w);
  763. fp_sum_w += fp_step_w;
  764. s_00_01 = (const Uint32 *)((const Uint8 *)src_h0 + index_w);
  765. *(Uint16*)dst = *(Uint16*)s_00_01;
  766. dst = (Uint32 *)((Uint8*)dst + bpp);
  767. }
  768. dst = (Uint32 *)((Uint8 *)dst + dst_gap);
  769. }
  770. return 0;
  771. }
  772. static int
  773. scale_mat_nearest_3(const Uint32 *src, int src_w, int src_h, int src_pitch,
  774. Uint32 *dst, int dst_w, int dst_h, int dst_pitch)
  775. {
  776. const int bpp = 3;
  777. NEAREST___START
  778. for (i = 0; i < dst_h; i++) {
  779. BILINEAR___HEIGHT
  780. while (middle--) {
  781. const Uint32 *s_00_01;
  782. int index_w = bpp * SRC_INDEX(fp_sum_w);
  783. fp_sum_w += fp_step_w;
  784. s_00_01 = (const Uint32 *)((const Uint8 *)src_h0 + index_w);
  785. ((Uint8*)dst)[0] = ((Uint8*)s_00_01)[0];
  786. ((Uint8*)dst)[1] = ((Uint8*)s_00_01)[1];
  787. ((Uint8*)dst)[2] = ((Uint8*)s_00_01)[2];
  788. dst = (Uint32 *)((Uint8*)dst + bpp);
  789. }
  790. dst = (Uint32 *)((Uint8 *)dst + dst_gap);
  791. }
  792. return 0;
  793. }
  794. static int
  795. scale_mat_nearest_4(const Uint32 *src, int src_w, int src_h, int src_pitch,
  796. Uint32 *dst, int dst_w, int dst_h, int dst_pitch)
  797. {
  798. int bpp = 4;
  799. NEAREST___START
  800. for (i = 0; i < dst_h; i++) {
  801. BILINEAR___HEIGHT
  802. while (middle--) {
  803. const Uint32 *s_00_01;
  804. int index_w = bpp * SRC_INDEX(fp_sum_w);
  805. fp_sum_w += fp_step_w;
  806. s_00_01 = (const Uint32 *)((const Uint8 *)src_h0 + index_w);
  807. *dst = *s_00_01;
  808. dst = (Uint32 *)((Uint8*)dst + bpp);
  809. }
  810. dst = (Uint32 *)((Uint8 *)dst + dst_gap);
  811. }
  812. return 0;
  813. }
  814. int
  815. SDL_LowerSoftStretchNearest(SDL_Surface *s, const SDL_Rect *srcrect,
  816. SDL_Surface *d, const SDL_Rect *dstrect)
  817. {
  818. int src_w = srcrect->w;
  819. int src_h = srcrect->h;
  820. int dst_w = dstrect->w;
  821. int dst_h = dstrect->h;
  822. int src_pitch = s->pitch;
  823. int dst_pitch = d->pitch;
  824. const int bpp = d->format->BytesPerPixel;
  825. Uint32 *src = (Uint32 *) ((Uint8 *)s->pixels + srcrect->x * bpp + srcrect->y * src_pitch);
  826. Uint32 *dst = (Uint32 *) ((Uint8 *)d->pixels + dstrect->x * bpp + dstrect->y * dst_pitch);
  827. if (bpp == 4) {
  828. return scale_mat_nearest_4(src, src_w, src_h, src_pitch, dst, dst_w, dst_h, dst_pitch);
  829. } else if (bpp == 3) {
  830. return scale_mat_nearest_3(src, src_w, src_h, src_pitch, dst, dst_w, dst_h, dst_pitch);
  831. } else if (bpp == 2) {
  832. return scale_mat_nearest_2(src, src_w, src_h, src_pitch, dst, dst_w, dst_h, dst_pitch);
  833. } else {
  834. return scale_mat_nearest_1(src, src_w, src_h, src_pitch, dst, dst_w, dst_h, dst_pitch);
  835. }
  836. }
  837. /* vi: set ts=4 sw=4 expandtab: */