SkBitmapProcState_matrixProcs.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * Copyright 2008 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. // The copyright below was added in 2009, but I see no record of moto contributions...?
  8. /* NEON optimized code (C) COPYRIGHT 2009 Motorola
  9. *
  10. * Use of this source code is governed by a BSD-style license that can be
  11. * found in the LICENSE file.
  12. */
  13. #include "include/core/SkShader.h"
  14. #include "include/private/SkTo.h"
  15. #include "src/core/SkBitmapProcState.h"
  16. #include "src/core/SkUtils.h"
  17. /*
  18. * The decal_ functions require that
  19. * 1. dx > 0
  20. * 2. [fx, fx+dx, fx+2dx, fx+3dx, ... fx+(count-1)dx] are all <= maxX
  21. *
  22. * In addition, we use SkFractionalInt to keep more fractional precision than
  23. * just SkFixed, so we will abort the decal_ call if dx is very small, since
  24. * the decal_ function just operates on SkFixed. If that were changed, we could
  25. * skip the very_small test here.
  26. */
  27. static inline bool can_truncate_to_fixed_for_decal(SkFixed fx,
  28. SkFixed dx,
  29. int count, unsigned max) {
  30. SkASSERT(count > 0);
  31. // if decal_ kept SkFractionalInt precision, this would just be dx <= 0
  32. // I just made up the 1/256. Just don't want to perceive accumulated error
  33. // if we truncate frDx and lose its low bits.
  34. if (dx <= SK_Fixed1 / 256) {
  35. return false;
  36. }
  37. // Note: it seems the test should be (fx <= max && lastFx <= max); but
  38. // historically it's been a strict inequality check, and changing produces
  39. // unexpected diffs. Further investigation is needed.
  40. // We cast to unsigned so we don't have to check for negative values, which
  41. // will now appear as very large positive values, and thus fail our test!
  42. if ((unsigned)SkFixedFloorToInt(fx) >= max) {
  43. return false;
  44. }
  45. // Promote to 64bit (48.16) to avoid overflow.
  46. const uint64_t lastFx = fx + sk_64_mul(dx, count - 1);
  47. return SkTFitsIn<int32_t>(lastFx) && (unsigned)SkFixedFloorToInt(SkTo<int32_t>(lastFx)) < max;
  48. }
  49. // When not filtering, we store 32-bit y, 16-bit x, 16-bit x, 16-bit x, ...
  50. // When filtering we write out 32-bit encodings, pairing 14.4 x0 with 14-bit x1.
  51. // The clamp routines may try to fall into one of these unclamped decal fast-paths.
  52. // (Only clamp works in the right coordinate space to check for decal.)
  53. static void decal_nofilter_scale(uint32_t dst[], SkFixed fx, SkFixed dx, int count) {
  54. // can_truncate_to_fixed_for_decal() checked only that stepping fx+=dx count-1
  55. // times doesn't overflow fx, so we take unusual care not to step count times.
  56. for (; count > 2; count -= 2) {
  57. *dst++ = pack_two_shorts( (fx + 0) >> 16,
  58. (fx + dx) >> 16);
  59. fx += dx+dx;
  60. }
  61. SkASSERT(count <= 2);
  62. switch (count) {
  63. case 2: ((uint16_t*)dst)[1] = SkToU16((fx + dx) >> 16);
  64. case 1: ((uint16_t*)dst)[0] = SkToU16((fx + 0) >> 16);
  65. }
  66. }
  67. // A generic implementation for unfiltered scale+translate, templated on tiling method.
  68. template <unsigned (*tile)(SkFixed, int), bool tryDecal>
  69. static void nofilter_scale(const SkBitmapProcState& s,
  70. uint32_t xy[], int count, int x, int y) {
  71. SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask |
  72. SkMatrix::kScale_Mask)) == 0);
  73. // Write out our 32-bit y, and get our intial fx.
  74. SkFractionalInt fx;
  75. {
  76. const SkBitmapProcStateAutoMapper mapper(s, x, y);
  77. *xy++ = tile(mapper.fixedY(), s.fPixmap.height() - 1);
  78. fx = mapper.fractionalIntX();
  79. }
  80. const unsigned maxX = s.fPixmap.width() - 1;
  81. if (0 == maxX) {
  82. // If width == 1, all the x-values must refer to that pixel, and must be zero.
  83. memset(xy, 0, count * sizeof(uint16_t));
  84. return;
  85. }
  86. const SkFractionalInt dx = s.fInvSxFractionalInt;
  87. if (tryDecal) {
  88. const SkFixed fixedFx = SkFractionalIntToFixed(fx);
  89. const SkFixed fixedDx = SkFractionalIntToFixed(dx);
  90. if (can_truncate_to_fixed_for_decal(fixedFx, fixedDx, count, maxX)) {
  91. decal_nofilter_scale(xy, fixedFx, fixedDx, count);
  92. return;
  93. }
  94. }
  95. // Remember, each x-coordinate is 16-bit.
  96. for (; count >= 2; count -= 2) {
  97. *xy++ = pack_two_shorts(tile(SkFractionalIntToFixed(fx ), maxX),
  98. tile(SkFractionalIntToFixed(fx + dx), maxX));
  99. fx += dx+dx;
  100. }
  101. auto xx = (uint16_t*)xy;
  102. while (count --> 0) {
  103. *xx++ = tile(SkFractionalIntToFixed(fx), maxX);
  104. fx += dx;
  105. }
  106. }
  107. // Extract the high four fractional bits from fx, the lerp parameter when filtering.
  108. static unsigned extract_low_bits_clamp(SkFixed fx, int /*max*/) {
  109. // If we're already scaled up to by max like clamp/decal,
  110. // just grab the high four fractional bits.
  111. return (fx >> 12) & 0xf;
  112. }
  113. static unsigned extract_low_bits_repeat_mirror(SkFixed fx, int max) {
  114. // In repeat or mirror fx is in [0,1], so scale up by max first.
  115. // TODO: remove the +1 here and the -1 at the call sites...
  116. return extract_low_bits_clamp((fx & 0xffff) * (max+1), max);
  117. }
  118. template <unsigned (*tile)(SkFixed, int), unsigned (*extract_low_bits)(SkFixed, int), bool tryDecal>
  119. static void filter_scale(const SkBitmapProcState& s,
  120. uint32_t xy[], int count, int x, int y) {
  121. SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask |
  122. SkMatrix::kScale_Mask)) == 0);
  123. SkASSERT(s.fInvKy == 0);
  124. auto pack = [](SkFixed f, unsigned max, SkFixed one) {
  125. unsigned i = tile(f, max);
  126. i = (i << 4) | extract_low_bits(f, max);
  127. return (i << 14) | (tile((f + one), max));
  128. };
  129. const unsigned maxX = s.fPixmap.width() - 1;
  130. const SkFractionalInt dx = s.fInvSxFractionalInt;
  131. SkFractionalInt fx;
  132. {
  133. const SkBitmapProcStateAutoMapper mapper(s, x, y);
  134. const SkFixed fy = mapper.fixedY();
  135. const unsigned maxY = s.fPixmap.height() - 1;
  136. // compute our two Y values up front
  137. *xy++ = pack(fy, maxY, s.fFilterOneY);
  138. // now initialize fx
  139. fx = mapper.fractionalIntX();
  140. }
  141. // For historical reasons we check both ends are < maxX rather than <= maxX.
  142. // TODO: try changing this? See also can_truncate_to_fixed_for_decal().
  143. if (tryDecal &&
  144. (unsigned)SkFractionalIntToInt(fx ) < maxX &&
  145. (unsigned)SkFractionalIntToInt(fx + dx*(count-1)) < maxX) {
  146. while (count --> 0) {
  147. SkFixed fixedFx = SkFractionalIntToFixed(fx);
  148. SkASSERT((fixedFx >> (16 + 14)) == 0);
  149. *xy++ = (fixedFx >> 12 << 14) | ((fixedFx >> 16) + 1);
  150. fx += dx;
  151. }
  152. return;
  153. }
  154. while (count --> 0) {
  155. SkFixed fixedFx = SkFractionalIntToFixed(fx);
  156. *xy++ = pack(fixedFx, maxX, s.fFilterOneX);
  157. fx += dx;
  158. }
  159. }
  160. // Helper to ensure that when we shift down, we do it w/o sign-extension
  161. // so the caller doesn't have to manually mask off the top 16 bits.
  162. static inline unsigned SK_USHIFT16(unsigned x) {
  163. return x >> 16;
  164. }
  165. static unsigned clamp(SkFixed fx, int max) {
  166. return SkClampMax(fx >> 16, max);
  167. }
  168. static unsigned repeat(SkFixed fx, int max) {
  169. SkASSERT(max < 65535);
  170. return SK_USHIFT16((unsigned)(fx & 0xFFFF) * (max + 1));
  171. }
  172. static unsigned mirror(SkFixed fx, int max) {
  173. SkASSERT(max < 65535);
  174. // s is 0xFFFFFFFF if we're on an odd interval, or 0 if an even interval
  175. SkFixed s = SkLeftShift(fx, 15) >> 31;
  176. // This should be exactly the same as repeat(fx ^ s, max) from here on.
  177. return SK_USHIFT16( ((fx ^ s) & 0xFFFF) * (max + 1) );
  178. }
  179. // Mirror/Mirror's always just portable code.
  180. static const SkBitmapProcState::MatrixProc MirrorX_MirrorY_Procs[] = {
  181. nofilter_scale<mirror, false>,
  182. filter_scale<mirror, extract_low_bits_repeat_mirror, false>,
  183. };
  184. // Clamp/Clamp and Repeat/Repeat have NEON or portable implementations.
  185. #if defined(SK_ARM_HAS_NEON)
  186. #include <arm_neon.h>
  187. // TODO: this is a fine drop-in for decal_nofilter_scale() generally.
  188. static void decal_nofilter_scale_neon(uint32_t dst[], SkFixed fx, SkFixed dx, int count) {
  189. if (count >= 8) {
  190. // SkFixed is 16.16 fixed point
  191. SkFixed dx8 = dx * 8;
  192. int32x4_t vdx8 = vdupq_n_s32(dx8);
  193. // setup lbase and hbase
  194. int32x4_t lbase, hbase;
  195. lbase = vdupq_n_s32(fx);
  196. lbase = vsetq_lane_s32(fx + dx, lbase, 1);
  197. lbase = vsetq_lane_s32(fx + dx + dx, lbase, 2);
  198. lbase = vsetq_lane_s32(fx + dx + dx + dx, lbase, 3);
  199. hbase = lbase + vdupq_n_s32(4 * dx);
  200. do {
  201. // store the upper 16 bits
  202. vst1q_u32(dst, vreinterpretq_u32_s16(
  203. vuzpq_s16(vreinterpretq_s16_s32(lbase), vreinterpretq_s16_s32(hbase)).val[1]
  204. ));
  205. // on to the next group of 8
  206. lbase += vdx8;
  207. hbase += vdx8;
  208. dst += 4; // we did 8 elements but the result is twice smaller
  209. count -= 8;
  210. fx += dx8;
  211. } while (count >= 8);
  212. }
  213. uint16_t* xx = (uint16_t*)dst;
  214. for (int i = count; i > 0; --i) {
  215. *xx++ = SkToU16(fx >> 16); fx += dx;
  216. }
  217. }
  218. static void decal_filter_scale_neon(uint32_t dst[], SkFixed fx, SkFixed dx, int count) {
  219. if (count >= 8) {
  220. SkFixed dx8 = dx * 8;
  221. int32x4_t vdx8 = vdupq_n_s32(dx8);
  222. int32x4_t wide_fx, wide_fx2;
  223. wide_fx = vdupq_n_s32(fx);
  224. wide_fx = vsetq_lane_s32(fx + dx, wide_fx, 1);
  225. wide_fx = vsetq_lane_s32(fx + dx + dx, wide_fx, 2);
  226. wide_fx = vsetq_lane_s32(fx + dx + dx + dx, wide_fx, 3);
  227. wide_fx2 = vaddq_s32(wide_fx, vdupq_n_s32(4 * dx));
  228. while (count >= 8) {
  229. int32x4_t wide_out;
  230. int32x4_t wide_out2;
  231. wide_out = vshlq_n_s32(vshrq_n_s32(wide_fx, 12), 14);
  232. wide_out = wide_out | (vshrq_n_s32(wide_fx,16) + vdupq_n_s32(1));
  233. wide_out2 = vshlq_n_s32(vshrq_n_s32(wide_fx2, 12), 14);
  234. wide_out2 = wide_out2 | (vshrq_n_s32(wide_fx2,16) + vdupq_n_s32(1));
  235. vst1q_u32(dst, vreinterpretq_u32_s32(wide_out));
  236. vst1q_u32(dst+4, vreinterpretq_u32_s32(wide_out2));
  237. dst += 8;
  238. fx += dx8;
  239. wide_fx += vdx8;
  240. wide_fx2 += vdx8;
  241. count -= 8;
  242. }
  243. }
  244. if (count & 1)
  245. {
  246. SkASSERT((fx >> (16 + 14)) == 0);
  247. *dst++ = (fx >> 12 << 14) | ((fx >> 16) + 1);
  248. fx += dx;
  249. }
  250. while ((count -= 2) >= 0)
  251. {
  252. SkASSERT((fx >> (16 + 14)) == 0);
  253. *dst++ = (fx >> 12 << 14) | ((fx >> 16) + 1);
  254. fx += dx;
  255. *dst++ = (fx >> 12 << 14) | ((fx >> 16) + 1);
  256. fx += dx;
  257. }
  258. }
  259. static inline int16x8_t clamp8(int32x4_t low, int32x4_t high, unsigned max) {
  260. int16x8_t res;
  261. // get the hi 16s of all those 32s
  262. res = vuzpq_s16(vreinterpretq_s16_s32(low), vreinterpretq_s16_s32(high)).val[1];
  263. // clamp
  264. res = vmaxq_s16(res, vdupq_n_s16(0));
  265. res = vminq_s16(res, vdupq_n_s16(max));
  266. return res;
  267. }
  268. static inline int32x4_t clamp4(int32x4_t f, unsigned max) {
  269. int32x4_t res;
  270. // get the hi 16s of all those 32s
  271. res = vshrq_n_s32(f, 16);
  272. // clamp
  273. res = vmaxq_s32(res, vdupq_n_s32(0));
  274. res = vminq_s32(res, vdupq_n_s32(max));
  275. return res;
  276. }
  277. static inline int32x4_t extract_low_bits_clamp4(int32x4_t fx, unsigned) {
  278. int32x4_t ret;
  279. ret = vshrq_n_s32(fx, 12);
  280. /* We don't need the mask below because the caller will
  281. * overwrite the non-masked bits
  282. */
  283. //ret = vandq_s32(ret, vdupq_n_s32(0xF));
  284. return ret;
  285. }
  286. static inline int16x8_t repeat8(int32x4_t low, int32x4_t high, unsigned max) {
  287. uint16x8_t res;
  288. uint32x4_t tmpl, tmph;
  289. // get the lower 16 bits
  290. res = vuzpq_u16(vreinterpretq_u16_s32(low), vreinterpretq_u16_s32(high)).val[0];
  291. // bare multiplication, not SkFixedMul
  292. tmpl = vmull_u16(vget_low_u16(res), vdup_n_u16(max+1));
  293. tmph = vmull_u16(vget_high_u16(res), vdup_n_u16(max+1));
  294. // extraction of the 16 upper bits
  295. res = vuzpq_u16(vreinterpretq_u16_u32(tmpl), vreinterpretq_u16_u32(tmph)).val[1];
  296. return vreinterpretq_s16_u16(res);
  297. }
  298. static inline int32x4_t repeat4(int32x4_t f, unsigned max) {
  299. uint16x4_t res;
  300. uint32x4_t tmp;
  301. // get the lower 16 bits
  302. res = vmovn_u32(vreinterpretq_u32_s32(f));
  303. // bare multiplication, not SkFixedMul
  304. tmp = vmull_u16(res, vdup_n_u16(max+1));
  305. // extraction of the 16 upper bits
  306. tmp = vshrq_n_u32(tmp, 16);
  307. return vreinterpretq_s32_u32(tmp);
  308. }
  309. static inline int32x4_t extract_low_bits_repeat_mirror4(int32x4_t fx, unsigned max) {
  310. uint16x4_t res;
  311. uint32x4_t tmp;
  312. int32x4_t ret;
  313. // get the lower 16 bits
  314. res = vmovn_u32(vreinterpretq_u32_s32(fx));
  315. // bare multiplication, not SkFixedMul
  316. tmp = vmull_u16(res, vdup_n_u16(max + 1));
  317. // shift and mask
  318. ret = vshrq_n_s32(vreinterpretq_s32_u32(tmp), 12);
  319. /* We don't need the mask below because the caller will
  320. * overwrite the non-masked bits
  321. */
  322. //ret = vandq_s32(ret, vdupq_n_s32(0xF));
  323. return ret;
  324. }
  325. template <unsigned (*tile)(SkFixed, int),
  326. int16x8_t (*tile8)(int32x4_t, int32x4_t, unsigned),
  327. bool tryDecal>
  328. static void nofilter_scale_neon(const SkBitmapProcState& s,
  329. uint32_t xy[], int count, int x, int y) {
  330. SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask |
  331. SkMatrix::kScale_Mask)) == 0);
  332. // we store y, x, x, x, x, x
  333. const unsigned maxX = s.fPixmap.width() - 1;
  334. SkFractionalInt fx;
  335. {
  336. const SkBitmapProcStateAutoMapper mapper(s, x, y);
  337. const unsigned maxY = s.fPixmap.height() - 1;
  338. *xy++ = tile(mapper.fixedY(), maxY);
  339. fx = mapper.fractionalIntX();
  340. }
  341. if (0 == maxX) {
  342. // all of the following X values must be 0
  343. memset(xy, 0, count * sizeof(uint16_t));
  344. return;
  345. }
  346. const SkFractionalInt dx = s.fInvSxFractionalInt;
  347. // test if we don't need to apply the tile proc
  348. const SkFixed fixedFx = SkFractionalIntToFixed(fx);
  349. const SkFixed fixedDx = SkFractionalIntToFixed(dx);
  350. if (tryDecal && can_truncate_to_fixed_for_decal(fixedFx, fixedDx, count, maxX)) {
  351. decal_nofilter_scale_neon(xy, fixedFx, fixedDx, count);
  352. return;
  353. }
  354. if (count >= 8) {
  355. SkFractionalInt dx2 = dx+dx;
  356. SkFractionalInt dx4 = dx2+dx2;
  357. SkFractionalInt dx8 = dx4+dx4;
  358. // now build fx/fx+dx/fx+2dx/fx+3dx
  359. SkFractionalInt fx1, fx2, fx3;
  360. int32x4_t lbase, hbase;
  361. int16_t *dst16 = (int16_t *)xy;
  362. fx1 = fx+dx;
  363. fx2 = fx1+dx;
  364. fx3 = fx2+dx;
  365. lbase = vdupq_n_s32(SkFractionalIntToFixed(fx));
  366. lbase = vsetq_lane_s32(SkFractionalIntToFixed(fx1), lbase, 1);
  367. lbase = vsetq_lane_s32(SkFractionalIntToFixed(fx2), lbase, 2);
  368. lbase = vsetq_lane_s32(SkFractionalIntToFixed(fx3), lbase, 3);
  369. hbase = vaddq_s32(lbase, vdupq_n_s32(SkFractionalIntToFixed(dx4)));
  370. // store & bump
  371. while (count >= 8) {
  372. int16x8_t fx8;
  373. fx8 = tile8(lbase, hbase, maxX);
  374. vst1q_s16(dst16, fx8);
  375. // but preserving base & on to the next
  376. lbase = vaddq_s32 (lbase, vdupq_n_s32(SkFractionalIntToFixed(dx8)));
  377. hbase = vaddq_s32 (hbase, vdupq_n_s32(SkFractionalIntToFixed(dx8)));
  378. dst16 += 8;
  379. count -= 8;
  380. fx += dx8;
  381. }
  382. xy = (uint32_t *) dst16;
  383. }
  384. uint16_t* xx = (uint16_t*)xy;
  385. for (int i = count; i > 0; --i) {
  386. *xx++ = tile(SkFractionalIntToFixed(fx), maxX);
  387. fx += dx;
  388. }
  389. }
  390. template <unsigned (*tile )(SkFixed, int),
  391. int32x4_t (*tile4)(int32x4_t, unsigned),
  392. unsigned (*extract_low_bits )(SkFixed, int),
  393. int32x4_t (*extract_low_bits4)(int32x4_t, unsigned),
  394. bool tryDecal>
  395. static void filter_scale_neon(const SkBitmapProcState& s,
  396. uint32_t xy[], int count, int x, int y) {
  397. SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask |
  398. SkMatrix::kScale_Mask)) == 0);
  399. SkASSERT(s.fInvKy == 0);
  400. auto pack = [&](SkFixed f, unsigned max, SkFixed one) {
  401. unsigned i = tile(f, max);
  402. i = (i << 4) | extract_low_bits(f, max);
  403. return (i << 14) | (tile((f + one), max));
  404. };
  405. auto pack4 = [&](int32x4_t f, unsigned max, SkFixed one) {
  406. int32x4_t ret, res;
  407. res = tile4(f, max);
  408. ret = extract_low_bits4(f, max);
  409. ret = vsliq_n_s32(ret, res, 4);
  410. res = tile4(f + vdupq_n_s32(one), max);
  411. ret = vorrq_s32(vshlq_n_s32(ret, 14), res);
  412. return ret;
  413. };
  414. const unsigned maxX = s.fPixmap.width() - 1;
  415. const SkFixed one = s.fFilterOneX;
  416. const SkFractionalInt dx = s.fInvSxFractionalInt;
  417. SkFractionalInt fx;
  418. {
  419. const SkBitmapProcStateAutoMapper mapper(s, x, y);
  420. const SkFixed fy = mapper.fixedY();
  421. const unsigned maxY = s.fPixmap.height() - 1;
  422. // compute our two Y values up front
  423. *xy++ = pack(fy, maxY, s.fFilterOneY);
  424. // now initialize fx
  425. fx = mapper.fractionalIntX();
  426. }
  427. // test if we don't need to apply the tile proc
  428. const SkFixed fixedFx = SkFractionalIntToFixed(fx);
  429. const SkFixed fixedDx = SkFractionalIntToFixed(dx);
  430. if (tryDecal && can_truncate_to_fixed_for_decal(fixedFx, fixedDx, count, maxX)) {
  431. decal_filter_scale_neon(xy, fixedFx, fixedDx, count);
  432. return;
  433. }
  434. if (count >= 4) {
  435. int32x4_t wide_fx;
  436. wide_fx = vdupq_n_s32(SkFractionalIntToFixed(fx));
  437. wide_fx = vsetq_lane_s32(SkFractionalIntToFixed(fx+dx), wide_fx, 1);
  438. wide_fx = vsetq_lane_s32(SkFractionalIntToFixed(fx+dx+dx), wide_fx, 2);
  439. wide_fx = vsetq_lane_s32(SkFractionalIntToFixed(fx+dx+dx+dx), wide_fx, 3);
  440. while (count >= 4) {
  441. int32x4_t res;
  442. res = pack4(wide_fx, maxX, one);
  443. vst1q_u32(xy, vreinterpretq_u32_s32(res));
  444. wide_fx += vdupq_n_s32(SkFractionalIntToFixed(dx+dx+dx+dx));
  445. fx += dx+dx+dx+dx;
  446. xy += 4;
  447. count -= 4;
  448. }
  449. }
  450. while (--count >= 0) {
  451. *xy++ = pack(SkFractionalIntToFixed(fx), maxX, one);
  452. fx += dx;
  453. }
  454. }
  455. static const SkBitmapProcState::MatrixProc ClampX_ClampY_Procs[] = {
  456. nofilter_scale_neon<clamp, clamp8, true>,
  457. filter_scale_neon<clamp,
  458. clamp4,
  459. extract_low_bits_clamp,
  460. extract_low_bits_clamp4,
  461. true>,
  462. };
  463. static const SkBitmapProcState::MatrixProc RepeatX_RepeatY_Procs[] = {
  464. nofilter_scale_neon<repeat, repeat8, false>,
  465. filter_scale_neon<repeat,
  466. repeat4,
  467. extract_low_bits_repeat_mirror,
  468. extract_low_bits_repeat_mirror4,
  469. false>,
  470. };
  471. #else
  472. static const SkBitmapProcState::MatrixProc ClampX_ClampY_Procs[] = {
  473. nofilter_scale<clamp, true>,
  474. filter_scale<clamp, extract_low_bits_clamp, true>,
  475. };
  476. static const SkBitmapProcState::MatrixProc RepeatX_RepeatY_Procs[] = {
  477. nofilter_scale<repeat, false>,
  478. filter_scale<repeat, extract_low_bits_repeat_mirror, false>,
  479. };
  480. #endif
  481. ///////////////////////////////////////////////////////////////////////////////
  482. // This next chunk has some specializations for unfiltered translate-only matrices.
  483. static inline U16CPU int_clamp(int x, int n) {
  484. if (x < 0) { x = 0; }
  485. if (x >= n) { x = n - 1; }
  486. return x;
  487. }
  488. /* returns 0...(n-1) given any x (positive or negative).
  489. As an example, if n (which is always positive) is 5...
  490. x: -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8
  491. returns: 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3
  492. */
  493. static inline int sk_int_mod(int x, int n) {
  494. SkASSERT(n > 0);
  495. if ((unsigned)x >= (unsigned)n) {
  496. if (x < 0) {
  497. x = n + ~(~x % n);
  498. } else {
  499. x = x % n;
  500. }
  501. }
  502. return x;
  503. }
  504. static inline U16CPU int_repeat(int x, int n) {
  505. return sk_int_mod(x, n);
  506. }
  507. static inline U16CPU int_mirror(int x, int n) {
  508. x = sk_int_mod(x, 2 * n);
  509. if (x >= n) {
  510. x = n + ~(x - n);
  511. }
  512. return x;
  513. }
  514. static void fill_sequential(uint16_t xptr[], int pos, int count) {
  515. while (count --> 0) {
  516. *xptr++ = pos++;
  517. }
  518. }
  519. static void fill_backwards(uint16_t xptr[], int pos, int count) {
  520. while (count --> 0) {
  521. SkASSERT(pos >= 0);
  522. *xptr++ = pos--;
  523. }
  524. }
  525. static void clampx_nofilter_trans(const SkBitmapProcState& s,
  526. uint32_t xy[], int count, int x, int y) {
  527. SkASSERT((s.fInvType & ~SkMatrix::kTranslate_Mask) == 0);
  528. const SkBitmapProcStateAutoMapper mapper(s, x, y);
  529. *xy++ = int_clamp(mapper.intY(), s.fPixmap.height());
  530. int xpos = mapper.intX();
  531. const int width = s.fPixmap.width();
  532. if (1 == width) {
  533. // all of the following X values must be 0
  534. memset(xy, 0, count * sizeof(uint16_t));
  535. return;
  536. }
  537. uint16_t* xptr = reinterpret_cast<uint16_t*>(xy);
  538. int n;
  539. // fill before 0 as needed
  540. if (xpos < 0) {
  541. n = -xpos;
  542. if (n > count) {
  543. n = count;
  544. }
  545. memset(xptr, 0, n * sizeof(uint16_t));
  546. count -= n;
  547. if (0 == count) {
  548. return;
  549. }
  550. xptr += n;
  551. xpos = 0;
  552. }
  553. // fill in 0..width-1 if needed
  554. if (xpos < width) {
  555. n = width - xpos;
  556. if (n > count) {
  557. n = count;
  558. }
  559. fill_sequential(xptr, xpos, n);
  560. count -= n;
  561. if (0 == count) {
  562. return;
  563. }
  564. xptr += n;
  565. }
  566. // fill the remaining with the max value
  567. sk_memset16(xptr, width - 1, count);
  568. }
  569. static void repeatx_nofilter_trans(const SkBitmapProcState& s,
  570. uint32_t xy[], int count, int x, int y) {
  571. SkASSERT((s.fInvType & ~SkMatrix::kTranslate_Mask) == 0);
  572. const SkBitmapProcStateAutoMapper mapper(s, x, y);
  573. *xy++ = int_repeat(mapper.intY(), s.fPixmap.height());
  574. int xpos = mapper.intX();
  575. const int width = s.fPixmap.width();
  576. if (1 == width) {
  577. // all of the following X values must be 0
  578. memset(xy, 0, count * sizeof(uint16_t));
  579. return;
  580. }
  581. uint16_t* xptr = reinterpret_cast<uint16_t*>(xy);
  582. int start = sk_int_mod(xpos, width);
  583. int n = width - start;
  584. if (n > count) {
  585. n = count;
  586. }
  587. fill_sequential(xptr, start, n);
  588. xptr += n;
  589. count -= n;
  590. while (count >= width) {
  591. fill_sequential(xptr, 0, width);
  592. xptr += width;
  593. count -= width;
  594. }
  595. if (count > 0) {
  596. fill_sequential(xptr, 0, count);
  597. }
  598. }
  599. static void mirrorx_nofilter_trans(const SkBitmapProcState& s,
  600. uint32_t xy[], int count, int x, int y) {
  601. SkASSERT((s.fInvType & ~SkMatrix::kTranslate_Mask) == 0);
  602. const SkBitmapProcStateAutoMapper mapper(s, x, y);
  603. *xy++ = int_mirror(mapper.intY(), s.fPixmap.height());
  604. int xpos = mapper.intX();
  605. const int width = s.fPixmap.width();
  606. if (1 == width) {
  607. // all of the following X values must be 0
  608. memset(xy, 0, count * sizeof(uint16_t));
  609. return;
  610. }
  611. uint16_t* xptr = reinterpret_cast<uint16_t*>(xy);
  612. // need to know our start, and our initial phase (forward or backward)
  613. bool forward;
  614. int n;
  615. int start = sk_int_mod(xpos, 2 * width);
  616. if (start >= width) {
  617. start = width + ~(start - width);
  618. forward = false;
  619. n = start + 1; // [start .. 0]
  620. } else {
  621. forward = true;
  622. n = width - start; // [start .. width)
  623. }
  624. if (n > count) {
  625. n = count;
  626. }
  627. if (forward) {
  628. fill_sequential(xptr, start, n);
  629. } else {
  630. fill_backwards(xptr, start, n);
  631. }
  632. forward = !forward;
  633. xptr += n;
  634. count -= n;
  635. while (count >= width) {
  636. if (forward) {
  637. fill_sequential(xptr, 0, width);
  638. } else {
  639. fill_backwards(xptr, width - 1, width);
  640. }
  641. forward = !forward;
  642. xptr += width;
  643. count -= width;
  644. }
  645. if (count > 0) {
  646. if (forward) {
  647. fill_sequential(xptr, 0, count);
  648. } else {
  649. fill_backwards(xptr, width - 1, count);
  650. }
  651. }
  652. }
  653. ///////////////////////////////////////////////////////////////////////////////
  654. // The main entry point to the file, choosing between everything above.
  655. SkBitmapProcState::MatrixProc SkBitmapProcState::chooseMatrixProc(bool translate_only_matrix) {
  656. SkASSERT(fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask));
  657. SkASSERT(fTileModeX == fTileModeY);
  658. SkASSERT(fTileModeX != SkTileMode::kDecal);
  659. // Check for our special case translate methods when there is no scale/affine/perspective.
  660. if (translate_only_matrix && kNone_SkFilterQuality == fFilterQuality) {
  661. switch (fTileModeX) {
  662. default: SkASSERT(false);
  663. case SkTileMode::kClamp: return clampx_nofilter_trans;
  664. case SkTileMode::kRepeat: return repeatx_nofilter_trans;
  665. case SkTileMode::kMirror: return mirrorx_nofilter_trans;
  666. }
  667. }
  668. // The arrays are all [ nofilter, filter ].
  669. int index = fFilterQuality > kNone_SkFilterQuality ? 1 : 0;
  670. if (fTileModeX == SkTileMode::kClamp) {
  671. // clamp gets special version of filterOne, working in non-normalized space (allowing decal)
  672. fFilterOneX = SK_Fixed1;
  673. fFilterOneY = SK_Fixed1;
  674. return ClampX_ClampY_Procs[index];
  675. }
  676. // all remaining procs use this form for filterOne, putting them into normalized space.
  677. fFilterOneX = SK_Fixed1 / fPixmap.width();
  678. fFilterOneY = SK_Fixed1 / fPixmap.height();
  679. if (fTileModeX == SkTileMode::kRepeat) {
  680. return RepeatX_RepeatY_Procs[index];
  681. }
  682. return MirrorX_MirrorY_Procs[index];
  683. }