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