Debian Codegraph @

src/core/SkBitmapProcState_matrixProcs.cpp

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