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 #include "include/core/SkShader.h"
9 #include "include/private/base/SkTPin.h"
10 #include "include/private/base/SkTo.h"
11 #include "src/core/SkBitmapProcState.h"
12 #include "src/core/SkOpts.h"
13
14 /*
15 * The decal_ functions require that
16 * 1. dx > 0
17 * 2. [fx, fx+dx, fx+2dx, fx+3dx, ... fx+(count-1)dx] are all <= maxX
18 *
19 * In addition, we use SkFractionalInt to keep more fractional precision than
20 * just SkFixed, so we will abort the decal_ call if dx is very small, since
21 * the decal_ function just operates on SkFixed. If that were changed, we could
22 * skip the very_small test here.
23 */
can_truncate_to_fixed_for_decal(SkFixed fx,SkFixed dx,int count,unsigned max)24 static inline bool can_truncate_to_fixed_for_decal(SkFixed fx,
25 SkFixed dx,
26 int count, unsigned max) {
27 SkASSERT(count > 0);
28
29 // if decal_ kept SkFractionalInt precision, this would just be dx <= 0
30 // I just made up the 1/256. Just don't want to perceive accumulated error
31 // if we truncate frDx and lose its low bits.
32 if (dx <= SK_Fixed1 / 256) {
33 return false;
34 }
35
36 // Note: it seems the test should be (fx <= max && lastFx <= max); but
37 // historically it's been a strict inequality check, and changing produces
38 // unexpected diffs. Further investigation is needed.
39
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
46 // Promote to 64bit (48.16) to avoid overflow.
47 const uint64_t lastFx = fx + sk_64_mul(dx, count - 1);
48
49 return SkTFitsIn<int32_t>(lastFx) && (unsigned)SkFixedFloorToInt(SkTo<int32_t>(lastFx)) < max;
50 }
51
52 // When not filtering, we store 32-bit y, 16-bit x, 16-bit x, 16-bit x, ...
53 // When filtering we write out 32-bit encodings, pairing 14.4 x0 with 14-bit x1.
54
55 // The clamp routines may try to fall into one of these unclamped decal fast-paths.
56 // (Only clamp works in the right coordinate space to check for decal.)
decal_nofilter_scale(uint32_t dst[],SkFixed fx,SkFixed dx,int count)57 static void decal_nofilter_scale(uint32_t dst[], SkFixed fx, SkFixed dx, int count) {
58 // can_truncate_to_fixed_for_decal() checked only that stepping fx+=dx count-1
59 // times doesn't overflow fx, so we take unusual care not to step count times.
60 for (; count > 2; count -= 2) {
61 *dst++ = pack_two_shorts( (fx + 0) >> 16,
62 (fx + dx) >> 16);
63 fx += dx+dx;
64 }
65
66 SkASSERT(count <= 2);
67 switch (count) {
68 case 2: ((uint16_t*)dst)[1] = SkToU16((fx + dx) >> 16); [[fallthrough]];
69 case 1: ((uint16_t*)dst)[0] = SkToU16((fx + 0) >> 16);
70 }
71 }
72
73 // A generic implementation for unfiltered scale+translate, templated on tiling method.
74 template <unsigned (*tilex)(SkFixed, int), unsigned (*tiley)(SkFixed, int), bool tryDecal>
nofilter_scale(const SkBitmapProcState & s,uint32_t xy[],int count,int x,int y)75 static void nofilter_scale(const SkBitmapProcState& s,
76 uint32_t xy[], int count, int x, int y) {
77 SkASSERT(s.fInvMatrix.isScaleTranslate());
78
79 // Write out our 32-bit y, and get our intial fx.
80 SkFractionalInt fx;
81 {
82 const SkBitmapProcStateAutoMapper mapper(s, x, y);
83 *xy++ = tiley(mapper.fixedY(), s.fPixmap.height() - 1);
84 fx = mapper.fractionalIntX();
85 }
86
87 const unsigned maxX = s.fPixmap.width() - 1;
88 if (0 == maxX) {
89 // If width == 1, all the x-values must refer to that pixel, and must be zero.
90 memset(xy, 0, count * sizeof(uint16_t));
91 return;
92 }
93
94 const SkFractionalInt dx = s.fInvSxFractionalInt;
95
96 if (tryDecal) {
97 const SkFixed fixedFx = SkFractionalIntToFixed(fx);
98 const SkFixed fixedDx = SkFractionalIntToFixed(dx);
99
100 if (can_truncate_to_fixed_for_decal(fixedFx, fixedDx, count, maxX)) {
101 decal_nofilter_scale(xy, fixedFx, fixedDx, count);
102 return;
103 }
104 }
105
106 // Remember, each x-coordinate is 16-bit.
107 for (; count >= 2; count -= 2) {
108 *xy++ = pack_two_shorts(tilex(SkFractionalIntToFixed(fx ), maxX),
109 tilex(SkFractionalIntToFixed(fx + dx), maxX));
110 fx += dx+dx;
111 }
112
113 auto xx = (uint16_t*)xy;
114 while (count --> 0) {
115 *xx++ = tilex(SkFractionalIntToFixed(fx), maxX);
116 fx += dx;
117 }
118 }
119
120 template <unsigned (*tilex)(SkFixed, int), unsigned (*tiley)(SkFixed, int)>
nofilter_affine(const SkBitmapProcState & s,uint32_t xy[],int count,int x,int y)121 static void nofilter_affine(const SkBitmapProcState& s,
122 uint32_t xy[], int count, int x, int y) {
123 SkASSERT(!s.fInvMatrix.hasPerspective());
124
125 const SkBitmapProcStateAutoMapper mapper(s, x, y);
126
127 SkFractionalInt fx = mapper.fractionalIntX(),
128 fy = mapper.fractionalIntY(),
129 dx = s.fInvSxFractionalInt,
130 dy = s.fInvKyFractionalInt;
131 int maxX = s.fPixmap.width () - 1,
132 maxY = s.fPixmap.height() - 1;
133
134 while (count --> 0) {
135 *xy++ = (tiley(SkFractionalIntToFixed(fy), maxY) << 16)
136 | (tilex(SkFractionalIntToFixed(fx), maxX) );
137 fx += dx;
138 fy += dy;
139 }
140 }
141
142 // used when both tilex and tiley are clamp
143 // Extract the high four fractional bits from fx, the lerp parameter when filtering.
extract_low_bits_clamp_clamp(SkFixed fx,int)144 static unsigned extract_low_bits_clamp_clamp(SkFixed fx, int /*max*/) {
145 // If we're already scaled up to by max like clamp/decal,
146 // just grab the high four fractional bits.
147 return (fx >> 12) & 0xf;
148 }
149
150 //used when one of tilex and tiley is not clamp
extract_low_bits_general(SkFixed fx,int max)151 static unsigned extract_low_bits_general(SkFixed fx, int max) {
152 // In repeat or mirror fx is in [0,1], so scale up by max first.
153 // TODO: remove the +1 here and the -1 at the call sites...
154 return extract_low_bits_clamp_clamp((fx & 0xffff) * (max+1), max);
155 }
156
157 // Takes a SkFixed number and packs it into a 32bit integer in the following schema:
158 // 14 bits to represent the low integer value (n)
159 // 4 bits to represent a linear distance between low and high (floored to nearest 1/16)
160 // 14 bits to represent the high integer value (n+1)
161 // If f is less than 0, then both integers will be 0. If f is greater than or equal to max, both
162 // integers will be that max value. In all cases, the middle 4 bits will represent the fractional
163 // part (to a resolution of 1/16). If the two integers are equal, doing any linear interpolation
164 // will result in the same integer, so the fractional part does not matter.
165 //
166 // The "one" parameter corresponds to the maximum distance between the high and low coordinate.
167 // For the clamp operation, this is just SkFixed1, but for others it is 1 / pixmap width because the
168 // distances are already normalized to between 0 and 1.0.
169 //
170 // See also SK_OPTS_NS::decode_packed_coordinates_and_weight for unpacking this value.
171 template <unsigned (*tile)(SkFixed, int), unsigned (*extract_low_bits)(SkFixed, int)>
172 SK_NO_SANITIZE("signed-integer-overflow")
pack(SkFixed f,unsigned max,SkFixed one)173 static uint32_t pack(SkFixed f, unsigned max, SkFixed one) {
174 uint32_t packed = tile(f, max); // low coordinate in high bits
175 packed = (packed << 4) | extract_low_bits(f, max); // (lerp weight _is_ coord fractional part)
176 packed = (packed << 14) | tile((f + one), max); // high coordinate in low bits
177 return packed;
178 }
179
180 template <unsigned (*tilex)(SkFixed, int), unsigned (*tiley)(SkFixed, int), unsigned (*extract_low_bits)(SkFixed, int), bool tryDecal>
filter_scale(const SkBitmapProcState & s,uint32_t xy[],int count,int x,int y)181 static void filter_scale(const SkBitmapProcState& s,
182 uint32_t xy[], int count, int x, int y) {
183 SkASSERT(s.fInvMatrix.isScaleTranslate());
184
185 const unsigned maxX = s.fPixmap.width() - 1;
186 const SkFractionalInt dx = s.fInvSxFractionalInt;
187 SkFractionalInt fx;
188 {
189 const SkBitmapProcStateAutoMapper mapper(s, x, y);
190 const unsigned maxY = s.fPixmap.height() - 1;
191 // compute our two Y values up front
192 *xy++ = pack<tiley, extract_low_bits>(mapper.fixedY(), maxY, s.fFilterOneY);
193 // now initialize fx
194 fx = mapper.fractionalIntX();
195 }
196
197 // For historical reasons we check both ends are < maxX rather than <= maxX.
198 // TODO: try changing this? See also can_truncate_to_fixed_for_decal().
199 if (tryDecal &&
200 (unsigned)SkFractionalIntToInt(fx ) < maxX &&
201 (unsigned)SkFractionalIntToInt(fx + dx*(count-1)) < maxX) {
202 while (count --> 0) {
203 SkFixed fixedFx = SkFractionalIntToFixed(fx);
204 SkASSERT((fixedFx >> (16 + 14)) == 0);
205 *xy++ = (fixedFx >> 12 << 14) | ((fixedFx >> 16) + 1);
206 fx += dx;
207 }
208 return;
209 }
210
211 while (count --> 0) {
212 *xy++ = pack<tilex, extract_low_bits>(SkFractionalIntToFixed(fx), maxX, s.fFilterOneX);
213 fx += dx;
214 }
215 }
216
217 template <unsigned (*tilex)(SkFixed, int), unsigned (*tiley)(SkFixed, int), unsigned (*extract_low_bits)(SkFixed, int)>
filter_affine(const SkBitmapProcState & s,uint32_t xy[],int count,int x,int y)218 static void filter_affine(const SkBitmapProcState& s,
219 uint32_t xy[], int count, int x, int y) {
220 SkASSERT(!s.fInvMatrix.hasPerspective());
221
222 const SkBitmapProcStateAutoMapper mapper(s, x, y);
223
224 SkFixed oneX = s.fFilterOneX,
225 oneY = s.fFilterOneY;
226
227 SkFractionalInt fx = mapper.fractionalIntX(),
228 fy = mapper.fractionalIntY(),
229 dx = s.fInvSxFractionalInt,
230 dy = s.fInvKyFractionalInt;
231 unsigned maxX = s.fPixmap.width () - 1,
232 maxY = s.fPixmap.height() - 1;
233 while (count --> 0) {
234 *xy++ = pack<tiley, extract_low_bits>(SkFractionalIntToFixed(fy), maxY, oneY);
235 *xy++ = pack<tilex, extract_low_bits>(SkFractionalIntToFixed(fx), maxX, oneX);
236
237 fy += dy;
238 fx += dx;
239 }
240 }
241
242 // Helper to ensure that when we shift down, we do it w/o sign-extension
243 // so the caller doesn't have to manually mask off the top 16 bits.
SK_USHIFT16(unsigned x)244 static inline unsigned SK_USHIFT16(unsigned x) {
245 return x >> 16;
246 }
247
repeat(SkFixed fx,int max)248 static unsigned repeat(SkFixed fx, int max) {
249 SkASSERT(max < 65535);
250 return SK_USHIFT16((unsigned)(fx & 0xFFFF) * (max + 1));
251 }
mirror(SkFixed fx,int max)252 static unsigned mirror(SkFixed fx, int max) {
253 SkASSERT(max < 65535);
254 // s is 0xFFFFFFFF if we're on an odd interval, or 0 if an even interval
255 SkFixed s = SkLeftShift(fx, 15) >> 31;
256
257 // This should be exactly the same as repeat(fx ^ s, max) from here on.
258 return SK_USHIFT16( ((fx ^ s) & 0xFFFF) * (max + 1) );
259 }
260
clamp(SkFixed fx,int max)261 static unsigned clamp(SkFixed fx, int max) {
262 return SkTPin(fx >> 16, 0, max);
263 }
264
265 static const SkBitmapProcState::MatrixProc ClampX_ClampY_Procs[] = {
266 nofilter_scale <clamp, clamp, true>, filter_scale <clamp, clamp, extract_low_bits_clamp_clamp, true>,
267 nofilter_affine<clamp, clamp>, filter_affine<clamp, clamp, extract_low_bits_clamp_clamp>,
268 };
269 static const SkBitmapProcState::MatrixProc RepeatX_RepeatY_Procs[] = {
270 nofilter_scale <repeat, repeat, false>, filter_scale <repeat, repeat, extract_low_bits_general, false>,
271 nofilter_affine<repeat, repeat>, filter_affine<repeat, repeat, extract_low_bits_general>
272 };
273 static const SkBitmapProcState::MatrixProc MirrorX_MirrorY_Procs[] = {
274 nofilter_scale <mirror, mirror, false>, filter_scale <mirror, mirror, extract_low_bits_general, false>,
275 nofilter_affine<mirror, mirror>, filter_affine<mirror, mirror, extract_low_bits_general>,
276 };
277
278
279 ///////////////////////////////////////////////////////////////////////////////
280 // This next chunk has some specializations for unfiltered translate-only matrices.
281
int_clamp(int x,int n)282 static inline U16CPU int_clamp(int x, int n) {
283 if (x < 0) { x = 0; }
284 if (x >= n) { x = n - 1; }
285 return x;
286 }
287
288 /* returns 0...(n-1) given any x (positive or negative).
289
290 As an example, if n (which is always positive) is 5...
291
292 x: -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8
293 returns: 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3
294 */
sk_int_mod(int x,int n)295 static inline int sk_int_mod(int x, int n) {
296 SkASSERT(n > 0);
297 if ((unsigned)x >= (unsigned)n) {
298 if (x < 0) {
299 x = n + ~(~x % n);
300 } else {
301 x = x % n;
302 }
303 }
304 return x;
305 }
306
int_repeat(int x,int n)307 static inline U16CPU int_repeat(int x, int n) {
308 return sk_int_mod(x, n);
309 }
310
int_mirror(int x,int n)311 static inline U16CPU int_mirror(int x, int n) {
312 x = sk_int_mod(x, 2 * n);
313 if (x >= n) {
314 x = n + ~(x - n);
315 }
316 return x;
317 }
318
fill_sequential(uint16_t xptr[],int pos,int count)319 static void fill_sequential(uint16_t xptr[], int pos, int count) {
320 while (count --> 0) {
321 *xptr++ = pos++;
322 }
323 }
324
fill_backwards(uint16_t xptr[],int pos,int count)325 static void fill_backwards(uint16_t xptr[], int pos, int count) {
326 while (count --> 0) {
327 SkASSERT(pos >= 0);
328 *xptr++ = pos--;
329 }
330 }
331
332 template< U16CPU (tiley)(int x, int n) >
clampx_nofilter_trans(const SkBitmapProcState & s,uint32_t xy[],int count,int x,int y)333 static void clampx_nofilter_trans(const SkBitmapProcState& s,
334 uint32_t xy[], int count, int x, int y) {
335 SkASSERT(s.fInvMatrix.isTranslate());
336
337 const SkBitmapProcStateAutoMapper mapper(s, x, y);
338 *xy++ = tiley(mapper.intY(), s.fPixmap.height());
339 int xpos = mapper.intX();
340
341 const int width = s.fPixmap.width();
342 if (1 == width) {
343 // all of the following X values must be 0
344 memset(xy, 0, count * sizeof(uint16_t));
345 return;
346 }
347
348 uint16_t* xptr = reinterpret_cast<uint16_t*>(xy);
349 int n;
350
351 // fill before 0 as needed
352 if (xpos < 0) {
353 n = -xpos;
354 if (n > count) {
355 n = count;
356 }
357 memset(xptr, 0, n * sizeof(uint16_t));
358 count -= n;
359 if (0 == count) {
360 return;
361 }
362 xptr += n;
363 xpos = 0;
364 }
365
366 // fill in 0..width-1 if needed
367 if (xpos < width) {
368 n = width - xpos;
369 if (n > count) {
370 n = count;
371 }
372 fill_sequential(xptr, xpos, n);
373 count -= n;
374 if (0 == count) {
375 return;
376 }
377 xptr += n;
378 }
379
380 // fill the remaining with the max value
381 SkOpts::memset16(xptr, width - 1, count);
382 }
383
384 template< U16CPU (tiley)(int x, int n) >
repeatx_nofilter_trans(const SkBitmapProcState & s,uint32_t xy[],int count,int x,int y)385 static void repeatx_nofilter_trans(const SkBitmapProcState& s,
386 uint32_t xy[], int count, int x, int y) {
387 SkASSERT(s.fInvMatrix.isTranslate());
388
389 const SkBitmapProcStateAutoMapper mapper(s, x, y);
390 *xy++ = tiley(mapper.intY(), s.fPixmap.height());
391 int xpos = mapper.intX();
392
393 const int width = s.fPixmap.width();
394 if (1 == width) {
395 // all of the following X values must be 0
396 memset(xy, 0, count * sizeof(uint16_t));
397 return;
398 }
399
400 uint16_t* xptr = reinterpret_cast<uint16_t*>(xy);
401 int start = sk_int_mod(xpos, width);
402 int n = width - start;
403 if (n > count) {
404 n = count;
405 }
406 fill_sequential(xptr, start, n);
407 xptr += n;
408 count -= n;
409
410 while (count >= width) {
411 fill_sequential(xptr, 0, width);
412 xptr += width;
413 count -= width;
414 }
415
416 if (count > 0) {
417 fill_sequential(xptr, 0, count);
418 }
419 }
420
421 template< U16CPU (tiley)(int x, int n) >
mirrorx_nofilter_trans(const SkBitmapProcState & s,uint32_t xy[],int count,int x,int y)422 static void mirrorx_nofilter_trans(const SkBitmapProcState& s,
423 uint32_t xy[], int count, int x, int y) {
424 SkASSERT(s.fInvMatrix.isTranslate());
425
426 const SkBitmapProcStateAutoMapper mapper(s, x, y);
427 *xy++ = tiley(mapper.intY(), s.fPixmap.height());
428 int xpos = mapper.intX();
429
430 const int width = s.fPixmap.width();
431 if (1 == width) {
432 // all of the following X values must be 0
433 memset(xy, 0, count * sizeof(uint16_t));
434 return;
435 }
436
437 uint16_t* xptr = reinterpret_cast<uint16_t*>(xy);
438 // need to know our start, and our initial phase (forward or backward)
439 bool forward;
440 int n;
441 int start = sk_int_mod(xpos, 2 * width);
442 if (start >= width) {
443 start = width + ~(start - width);
444 forward = false;
445 n = start + 1; // [start .. 0]
446 } else {
447 forward = true;
448 n = width - start; // [start .. width)
449 }
450 if (n > count) {
451 n = count;
452 }
453 if (forward) {
454 fill_sequential(xptr, start, n);
455 } else {
456 fill_backwards(xptr, start, n);
457 }
458 forward = !forward;
459 xptr += n;
460 count -= n;
461
462 while (count >= width) {
463 if (forward) {
464 fill_sequential(xptr, 0, width);
465 } else {
466 fill_backwards(xptr, width - 1, width);
467 }
468 forward = !forward;
469 xptr += width;
470 count -= width;
471 }
472
473 if (count > 0) {
474 if (forward) {
475 fill_sequential(xptr, 0, count);
476 } else {
477 fill_backwards(xptr, width - 1, count);
478 }
479 }
480 }
481
482
483 ///////////////////////////////////////////////////////////////////////////////
484 // The main entry point to the file, choosing between everything above.
485
chooseMatrixProc(bool translate_only_matrix)486 SkBitmapProcState::MatrixProc SkBitmapProcState::chooseMatrixProc(bool translate_only_matrix) {
487 SkASSERT(!fInvMatrix.hasPerspective());
488 SkASSERT(fTileModeX != SkTileMode::kDecal);
489
490 if( fTileModeX == fTileModeY ) {
491 // Check for our special case translate methods when there is no scale/affine/perspective.
492 if (translate_only_matrix && !fBilerp) {
493 switch (fTileModeX) {
494 default: SkASSERT(false); [[fallthrough]];
495 case SkTileMode::kClamp: return clampx_nofilter_trans<int_clamp>;
496 case SkTileMode::kRepeat: return repeatx_nofilter_trans<int_repeat>;
497 case SkTileMode::kMirror: return mirrorx_nofilter_trans<int_mirror>;
498 }
499 }
500
501 // The arrays are all [ nofilter, filter ].
502 int index = fBilerp ? 1 : 0;
503 if (!fInvMatrix.isScaleTranslate()) {
504 index |= 2;
505 }
506
507 if (fTileModeX == SkTileMode::kClamp) {
508 // clamp gets special version of filterOne, working in non-normalized space (allowing decal)
509 fFilterOneX = SK_Fixed1;
510 fFilterOneY = SK_Fixed1;
511 return ClampX_ClampY_Procs[index];
512 }
513
514 // all remaining procs use this form for filterOne, putting them into normalized space.
515 fFilterOneX = SK_Fixed1 / fPixmap.width();
516 fFilterOneY = SK_Fixed1 / fPixmap.height();
517
518 if (fTileModeX == SkTileMode::kRepeat) {
519 return RepeatX_RepeatY_Procs[index];
520 }
521 return MirrorX_MirrorY_Procs[index];
522 }
523
524 SkASSERT(fTileModeX == fTileModeY);
525 return nullptr;
526 }
527
pack_clamp(SkFixed f,unsigned max)528 uint32_t sktests::pack_clamp(SkFixed f, unsigned max) {
529 // Based on ClampX_ClampY_Procs[1] (filter_scale)
530 return ::pack<clamp, extract_low_bits_clamp_clamp>(f, max, SK_Fixed1);
531 }
532
pack_repeat(SkFixed f,unsigned max,size_t width)533 uint32_t sktests::pack_repeat(SkFixed f, unsigned max, size_t width) {
534 // Based on RepeatX_RepeatY_Procs[1] (filter_scale)
535 return ::pack<repeat, extract_low_bits_general>(f, max, SK_Fixed1 / width);
536 }
537
pack_mirror(SkFixed f,unsigned max,size_t width)538 uint32_t sktests::pack_mirror(SkFixed f, unsigned max, size_t width) {
539 // Based on MirrorX_MirrorY_Procs[1] (filter_scale)
540 return ::pack<mirror, extract_low_bits_general>(f, max, SK_Fixed1 / width);
541 }
542