SDL_stretch.c 36 KB

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