1 /*
2 * Copyright 2011 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/SkImageEncoder.h"
9 #include "include/core/SkPaint.h"
10 #include "include/core/SkShader.h"
11 #include "include/private/SkColorData.h"
12 #include "include/private/SkMacros.h"
13 #include "src/core/SkBitmapCache.h"
14 #include "src/core/SkBitmapController.h"
15 #include "src/core/SkBitmapProcState.h"
16 #include "src/core/SkMipMap.h"
17 #include "src/core/SkOpts.h"
18 #include "src/core/SkResourceCache.h"
19 #include "src/core/SkUtils.h"
20
21 // One-stop-shop shader for,
22 // - nearest-neighbor sampling (_nofilter_),
23 // - clamp tiling in X and Y both (Clamp_),
24 // - with at most a scale and translate matrix (_DX_),
25 // - and no extra alpha applied (_opaque_),
26 // - sampling from 8888 (_S32_) and drawing to 8888 (_S32_).
Clamp_S32_opaque_D32_nofilter_DX_shaderproc(const void * sIn,int x,int y,SkPMColor * dst,int count)27 static void Clamp_S32_opaque_D32_nofilter_DX_shaderproc(const void* sIn, int x, int y,
28 SkPMColor* dst, int count) {
29 const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
30 SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask |
31 SkMatrix::kScale_Mask)) == 0);
32 SkASSERT(s.fAlphaScale == 256);
33
34 const unsigned maxX = s.fPixmap.width() - 1;
35 SkFractionalInt fx;
36 int dstY;
37 {
38 const SkBitmapProcStateAutoMapper mapper(s, x, y);
39 const unsigned maxY = s.fPixmap.height() - 1;
40 dstY = SkClampMax(mapper.intY(), maxY);
41 fx = mapper.fractionalIntX();
42 }
43
44 const SkPMColor* src = s.fPixmap.addr32(0, dstY);
45 const SkFractionalInt dx = s.fInvSxFractionalInt;
46
47 // Check if we're safely inside [0...maxX] so no need to clamp each computed index.
48 //
49 if ((uint64_t)SkFractionalIntToInt(fx) <= maxX &&
50 (uint64_t)SkFractionalIntToInt(fx + dx * (count - 1)) <= maxX)
51 {
52 int count4 = count >> 2;
53 for (int i = 0; i < count4; ++i) {
54 SkPMColor src0 = src[SkFractionalIntToInt(fx)]; fx += dx;
55 SkPMColor src1 = src[SkFractionalIntToInt(fx)]; fx += dx;
56 SkPMColor src2 = src[SkFractionalIntToInt(fx)]; fx += dx;
57 SkPMColor src3 = src[SkFractionalIntToInt(fx)]; fx += dx;
58 dst[0] = src0;
59 dst[1] = src1;
60 dst[2] = src2;
61 dst[3] = src3;
62 dst += 4;
63 }
64 for (int i = (count4 << 2); i < count; ++i) {
65 unsigned index = SkFractionalIntToInt(fx);
66 SkASSERT(index <= maxX);
67 *dst++ = src[index];
68 fx += dx;
69 }
70 } else {
71 for (int i = 0; i < count; ++i) {
72 dst[i] = src[SkClampMax(SkFractionalIntToInt(fx), maxX)];
73 fx += dx;
74 }
75 }
76 }
77
S32_alpha_D32_nofilter_DX(const SkBitmapProcState & s,const uint32_t * xy,int count,SkPMColor * colors)78 static void S32_alpha_D32_nofilter_DX(const SkBitmapProcState& s,
79 const uint32_t* xy, int count, SkPMColor* colors) {
80 SkASSERT(count > 0 && colors != nullptr);
81 SkASSERT(s.fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask));
82 SkASSERT(kNone_SkFilterQuality == s.fFilterQuality);
83 SkASSERT(4 == s.fPixmap.info().bytesPerPixel());
84 SkASSERT(s.fAlphaScale <= 256);
85
86 // xy is a 32-bit y-coordinate, followed by 16-bit x-coordinates.
87 unsigned y = *xy++;
88 SkASSERT(y < (unsigned)s.fPixmap.height());
89
90 auto row = (const SkPMColor*)( (const char*)s.fPixmap.addr() + y * s.fPixmap.rowBytes() );
91
92 if (1 == s.fPixmap.width()) {
93 sk_memset32(colors, SkAlphaMulQ(row[0], s.fAlphaScale), count);
94 return;
95 }
96
97 // Step 4 xs == 2 uint32_t at a time.
98 while (count >= 4) {
99 uint32_t x01 = *xy++,
100 x23 = *xy++;
101
102 SkPMColor p0 = row[UNPACK_PRIMARY_SHORT (x01)];
103 SkPMColor p1 = row[UNPACK_SECONDARY_SHORT(x01)];
104 SkPMColor p2 = row[UNPACK_PRIMARY_SHORT (x23)];
105 SkPMColor p3 = row[UNPACK_SECONDARY_SHORT(x23)];
106
107 *colors++ = SkAlphaMulQ(p0, s.fAlphaScale);
108 *colors++ = SkAlphaMulQ(p1, s.fAlphaScale);
109 *colors++ = SkAlphaMulQ(p2, s.fAlphaScale);
110 *colors++ = SkAlphaMulQ(p3, s.fAlphaScale);
111
112 count -= 4;
113 }
114
115 // Step 1 x == 1 uint16_t at a time.
116 auto x = (const uint16_t*)xy;
117 while (count --> 0) {
118 *colors++ = SkAlphaMulQ(row[*x++], s.fAlphaScale);
119 }
120 }
121
SkBitmapProcInfo(const SkImage_Base * image,SkTileMode tmx,SkTileMode tmy)122 SkBitmapProcInfo::SkBitmapProcInfo(const SkImage_Base* image, SkTileMode tmx, SkTileMode tmy)
123 : fImage(image)
124 , fTileModeX(tmx)
125 , fTileModeY(tmy)
126 , fBMState(nullptr)
127 {}
128
~SkBitmapProcInfo()129 SkBitmapProcInfo::~SkBitmapProcInfo() {}
130
131
132 // true iff the matrix has a scale and no more than an optional translate.
matrix_only_scale_translate(const SkMatrix & m)133 static bool matrix_only_scale_translate(const SkMatrix& m) {
134 return (m.getType() & ~SkMatrix::kTranslate_Mask) == SkMatrix::kScale_Mask;
135 }
136
137 /**
138 * For the purposes of drawing bitmaps, if a matrix is "almost" translate
139 * go ahead and treat it as if it were, so that subsequent code can go fast.
140 */
just_trans_general(const SkMatrix & matrix)141 static bool just_trans_general(const SkMatrix& matrix) {
142 SkASSERT(matrix_only_scale_translate(matrix));
143
144 const SkScalar tol = SK_Scalar1 / 32768;
145
146 return SkScalarNearlyZero(matrix[SkMatrix::kMScaleX] - SK_Scalar1, tol)
147 && SkScalarNearlyZero(matrix[SkMatrix::kMScaleY] - SK_Scalar1, tol);
148 }
149
150 /**
151 * Determine if the matrix can be treated as integral-only-translate,
152 * for the purpose of filtering.
153 */
just_trans_integral(const SkMatrix & m)154 static bool just_trans_integral(const SkMatrix& m) {
155 static constexpr SkScalar tol = SK_Scalar1 / 256;
156
157 return m.getType() <= SkMatrix::kTranslate_Mask
158 && SkScalarNearlyEqual(m.getTranslateX(), SkScalarRoundToScalar(m.getTranslateX()), tol)
159 && SkScalarNearlyEqual(m.getTranslateY(), SkScalarRoundToScalar(m.getTranslateY()), tol);
160 }
161
valid_for_filtering(unsigned dimension)162 static bool valid_for_filtering(unsigned dimension) {
163 // for filtering, width and height must fit in 14bits, since we use steal
164 // 2 bits from each to store our 4bit subpixel data
165 return (dimension & ~0x3FFF) == 0;
166 }
167
init(const SkMatrix & inv,const SkPaint & paint)168 bool SkBitmapProcInfo::init(const SkMatrix& inv, const SkPaint& paint) {
169 SkASSERT(inv.isScaleTranslate());
170
171 fPixmap.reset();
172 fInvMatrix = inv;
173 fFilterQuality = paint.getFilterQuality();
174
175 fBMState = SkBitmapController::RequestBitmap(fImage, inv, paint.getFilterQuality(), &fAlloc);
176
177 // Note : we allow the controller to return an empty (zero-dimension) result. Should we?
178 if (nullptr == fBMState || fBMState->pixmap().info().isEmpty()) {
179 return false;
180 }
181 fPixmap = fBMState->pixmap();
182 fInvMatrix = fBMState->invMatrix();
183 fRealInvMatrix = fBMState->invMatrix();
184 fPaintColor = paint.getColor();
185 fFilterQuality = fBMState->quality();
186 SkASSERT(fFilterQuality <= kLow_SkFilterQuality);
187 SkASSERT(fPixmap.addr());
188
189 bool integral_translate_only = just_trans_integral(fInvMatrix);
190 if (!integral_translate_only) {
191 // Most of the scanline procs deal with "unit" texture coordinates, as this
192 // makes it easy to perform tiling modes (repeat = (x & 0xFFFF)). To generate
193 // those, we divide the matrix by its dimensions here.
194 //
195 // We don't do this if we're either trivial (can ignore the matrix) or clamping
196 // in both X and Y since clamping to width,height is just as easy as to 0xFFFF.
197
198 if (fTileModeX != SkTileMode::kClamp || fTileModeY != SkTileMode::kClamp) {
199 fInvMatrix.postIDiv(fPixmap.width(), fPixmap.height());
200 }
201
202 // Now that all possible changes to the matrix have taken place, check
203 // to see if we're really close to a no-scale matrix. If so, explicitly
204 // set it to be so. Subsequent code may inspect this matrix to choose
205 // a faster path in this case.
206
207 // This code will only execute if the matrix has some scale component;
208 // if it's already pure translate then we won't do this inversion.
209
210 if (matrix_only_scale_translate(fInvMatrix)) {
211 SkMatrix forward;
212 if (fInvMatrix.invert(&forward) && just_trans_general(forward)) {
213 fInvMatrix.setTranslate(-forward.getTranslateX(), -forward.getTranslateY());
214 }
215 }
216
217 // Recompute the flag after matrix adjustments.
218 integral_translate_only = just_trans_integral(fInvMatrix);
219 }
220
221 fInvType = fInvMatrix.getType();
222
223 if (kLow_SkFilterQuality == fFilterQuality &&
224 (!valid_for_filtering(fPixmap.width() | fPixmap.height()) ||
225 integral_translate_only)) {
226 fFilterQuality = kNone_SkFilterQuality;
227 }
228
229 return true;
230 }
231
232 /*
233 * Analyze filter-quality and matrix, and decide how to implement that.
234 *
235 * In general, we cascade down the request level [ High ... None ]
236 * - for a given level, if we can fulfill it, fine, else
237 * - else we downgrade to the next lower level and try again.
238 * We can always fulfill requests for Low and None
239 * - sometimes we will "ignore" Low and give None, but this is likely a legacy perf hack
240 * and may be removed.
241 */
chooseProcs()242 bool SkBitmapProcState::chooseProcs() {
243 SkASSERT(fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask));
244 SkASSERT(fPixmap.colorType() == kN32_SkColorType);
245 SkASSERT(fPixmap.alphaType() == kPremul_SkAlphaType ||
246 fPixmap.alphaType() == kOpaque_SkAlphaType);
247 SkASSERT(fTileModeX == fTileModeY);
248 SkASSERT(fTileModeX != SkTileMode::kDecal);
249 SkASSERT(fFilterQuality < kHigh_SkFilterQuality);
250
251 fInvProc = SkMatrixPriv::GetMapXYProc(fInvMatrix);
252 fInvSx = SkScalarToFixed (fInvMatrix.getScaleX());
253 fInvSxFractionalInt = SkScalarToFractionalInt(fInvMatrix.getScaleX());
254 fInvKy = SkScalarToFixed (fInvMatrix.getSkewY());
255 fInvKyFractionalInt = SkScalarToFractionalInt(fInvMatrix.getSkewY());
256
257 fAlphaScale = SkAlpha255To256(SkColorGetA(fPaintColor));
258
259 bool translate_only = (fInvMatrix.getType() & ~SkMatrix::kTranslate_Mask) == 0;
260 fMatrixProc = this->chooseMatrixProc(translate_only);
261 SkASSERT(fMatrixProc);
262
263 if (fFilterQuality > kNone_SkFilterQuality) {
264 fSampleProc32 = SkOpts::S32_alpha_D32_filter_DX;
265 } else {
266 fSampleProc32 = S32_alpha_D32_nofilter_DX;
267 }
268
269 fShaderProc32 = this->chooseShaderProc32();
270
271 // our special-case shaderprocs
272 // TODO: move this one into chooseShaderProc32() or pull all that in here.
273 if (nullptr == fShaderProc32
274 && fAlphaScale == 256
275 && fFilterQuality == kNone_SkFilterQuality
276 && SkTileMode::kClamp == fTileModeX) {
277 fShaderProc32 = Clamp_S32_opaque_D32_nofilter_DX_shaderproc;
278 }
279
280 return true;
281 }
282
Clamp_S32_D32_nofilter_trans_shaderproc(const void * sIn,int x,int y,SkPMColor * colors,int count)283 static void Clamp_S32_D32_nofilter_trans_shaderproc(const void* sIn,
284 int x, int y,
285 SkPMColor* colors,
286 int count) {
287 const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
288 SkASSERT(((s.fInvType & ~SkMatrix::kTranslate_Mask)) == 0);
289 SkASSERT(s.fInvKy == 0);
290 SkASSERT(count > 0 && colors != nullptr);
291 SkASSERT(kNone_SkFilterQuality == s.fFilterQuality);
292
293 const int maxX = s.fPixmap.width() - 1;
294 const int maxY = s.fPixmap.height() - 1;
295 int ix = s.fFilterOneX + x;
296 int iy = SkClampMax(s.fFilterOneY + y, maxY);
297 const SkPMColor* row = s.fPixmap.addr32(0, iy);
298
299 // clamp to the left
300 if (ix < 0) {
301 int n = SkMin32(-ix, count);
302 sk_memset32(colors, row[0], n);
303 count -= n;
304 if (0 == count) {
305 return;
306 }
307 colors += n;
308 SkASSERT(-ix == n);
309 ix = 0;
310 }
311 // copy the middle
312 if (ix <= maxX) {
313 int n = SkMin32(maxX - ix + 1, count);
314 memcpy(colors, row + ix, n * sizeof(SkPMColor));
315 count -= n;
316 if (0 == count) {
317 return;
318 }
319 colors += n;
320 }
321 SkASSERT(count > 0);
322 // clamp to the right
323 sk_memset32(colors, row[maxX], count);
324 }
325
sk_int_mod(int x,int n)326 static inline int sk_int_mod(int x, int n) {
327 SkASSERT(n > 0);
328 if ((unsigned)x >= (unsigned)n) {
329 if (x < 0) {
330 x = n + ~(~x % n);
331 } else {
332 x = x % n;
333 }
334 }
335 return x;
336 }
337
sk_int_mirror(int x,int n)338 static inline int sk_int_mirror(int x, int n) {
339 x = sk_int_mod(x, 2 * n);
340 if (x >= n) {
341 x = n + ~(x - n);
342 }
343 return x;
344 }
345
Repeat_S32_D32_nofilter_trans_shaderproc(const void * sIn,int x,int y,SkPMColor * colors,int count)346 static void Repeat_S32_D32_nofilter_trans_shaderproc(const void* sIn,
347 int x, int y,
348 SkPMColor* colors,
349 int count) {
350 const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
351 SkASSERT(((s.fInvType & ~SkMatrix::kTranslate_Mask)) == 0);
352 SkASSERT(s.fInvKy == 0);
353 SkASSERT(count > 0 && colors != nullptr);
354 SkASSERT(kNone_SkFilterQuality == s.fFilterQuality);
355
356 const int stopX = s.fPixmap.width();
357 const int stopY = s.fPixmap.height();
358 int ix = s.fFilterOneX + x;
359 int iy = sk_int_mod(s.fFilterOneY + y, stopY);
360 const SkPMColor* row = s.fPixmap.addr32(0, iy);
361
362 ix = sk_int_mod(ix, stopX);
363 for (;;) {
364 int n = SkMin32(stopX - ix, count);
365 memcpy(colors, row + ix, n * sizeof(SkPMColor));
366 count -= n;
367 if (0 == count) {
368 return;
369 }
370 colors += n;
371 ix = 0;
372 }
373 }
374
filter_32_alpha(unsigned t,SkPMColor color0,SkPMColor color1,SkPMColor * dstColor,unsigned alphaScale)375 static inline void filter_32_alpha(unsigned t,
376 SkPMColor color0,
377 SkPMColor color1,
378 SkPMColor* dstColor,
379 unsigned alphaScale) {
380 SkASSERT((unsigned)t <= 0xF);
381 SkASSERT(alphaScale <= 256);
382
383 const uint32_t mask = 0xFF00FF;
384
385 int scale = 256 - 16*t;
386 uint32_t lo = (color0 & mask) * scale;
387 uint32_t hi = ((color0 >> 8) & mask) * scale;
388
389 scale = 16*t;
390 lo += (color1 & mask) * scale;
391 hi += ((color1 >> 8) & mask) * scale;
392
393 // TODO: if (alphaScale < 256) ...
394 lo = ((lo >> 8) & mask) * alphaScale;
395 hi = ((hi >> 8) & mask) * alphaScale;
396
397 *dstColor = ((lo >> 8) & mask) | (hi & ~mask);
398 }
399
S32_D32_constX_shaderproc(const void * sIn,int x,int y,SkPMColor * colors,int count)400 static void S32_D32_constX_shaderproc(const void* sIn,
401 int x, int y,
402 SkPMColor* colors,
403 int count) {
404 const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
405 SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) == 0);
406 SkASSERT(s.fInvKy == 0);
407 SkASSERT(count > 0 && colors != nullptr);
408 SkASSERT(1 == s.fPixmap.width());
409
410 int iY0;
411 int iY1 SK_INIT_TO_AVOID_WARNING;
412 int iSubY SK_INIT_TO_AVOID_WARNING;
413
414 if (kNone_SkFilterQuality != s.fFilterQuality) {
415 SkBitmapProcState::MatrixProc mproc = s.getMatrixProc();
416 uint32_t xy[2];
417
418 mproc(s, xy, 1, x, y);
419
420 iY0 = xy[0] >> 18;
421 iY1 = xy[0] & 0x3FFF;
422 iSubY = (xy[0] >> 14) & 0xF;
423 } else {
424 int yTemp;
425
426 if (s.fInvType > SkMatrix::kTranslate_Mask) {
427 const SkBitmapProcStateAutoMapper mapper(s, x, y);
428
429 // When the matrix has a scale component the setup code in
430 // chooseProcs multiples the inverse matrix by the inverse of the
431 // bitmap's width and height. Since this method is going to do
432 // its own tiling and sampling we need to undo that here.
433 if (SkTileMode::kClamp != s.fTileModeX || SkTileMode::kClamp != s.fTileModeY) {
434 yTemp = SkFractionalIntToInt(mapper.fractionalIntY() * s.fPixmap.height());
435 } else {
436 yTemp = mapper.intY();
437 }
438 } else {
439 yTemp = s.fFilterOneY + y;
440 }
441
442 const int stopY = s.fPixmap.height();
443 switch (s.fTileModeY) {
444 case SkTileMode::kClamp:
445 iY0 = SkClampMax(yTemp, stopY-1);
446 break;
447 case SkTileMode::kRepeat:
448 iY0 = sk_int_mod(yTemp, stopY);
449 break;
450 case SkTileMode::kMirror:
451 default:
452 iY0 = sk_int_mirror(yTemp, stopY);
453 break;
454 }
455
456 #ifdef SK_DEBUG
457 {
458 const SkBitmapProcStateAutoMapper mapper(s, x, y);
459 int iY2;
460
461 if (s.fInvType > SkMatrix::kTranslate_Mask &&
462 (SkTileMode::kClamp != s.fTileModeX || SkTileMode::kClamp != s.fTileModeY)) {
463 iY2 = SkFractionalIntToInt(mapper.fractionalIntY() * s.fPixmap.height());
464 } else {
465 iY2 = mapper.intY();
466 }
467
468 switch (s.fTileModeY) {
469 case SkTileMode::kClamp:
470 iY2 = SkClampMax(iY2, stopY-1);
471 break;
472 case SkTileMode::kRepeat:
473 iY2 = sk_int_mod(iY2, stopY);
474 break;
475 case SkTileMode::kMirror:
476 default:
477 iY2 = sk_int_mirror(iY2, stopY);
478 break;
479 }
480
481 SkASSERT(iY0 == iY2);
482 }
483 #endif
484 }
485
486 const SkPMColor* row0 = s.fPixmap.addr32(0, iY0);
487 SkPMColor color;
488
489 if (kNone_SkFilterQuality != s.fFilterQuality) {
490 const SkPMColor* row1 = s.fPixmap.addr32(0, iY1);
491 filter_32_alpha(iSubY, *row0, *row1, &color, s.fAlphaScale);
492 } else {
493 if (s.fAlphaScale < 256) {
494 color = SkAlphaMulQ(*row0, s.fAlphaScale);
495 } else {
496 color = *row0;
497 }
498 }
499
500 sk_memset32(colors, color, count);
501 }
502
DoNothing_shaderproc(const void *,int x,int y,SkPMColor * colors,int count)503 static void DoNothing_shaderproc(const void*, int x, int y,
504 SkPMColor* colors, int count) {
505 // if we get called, the matrix is too tricky, so we just draw nothing
506 sk_memset32(colors, 0, count);
507 }
508
setupForTranslate()509 bool SkBitmapProcState::setupForTranslate() {
510 SkPoint pt;
511 const SkBitmapProcStateAutoMapper mapper(*this, 0, 0, &pt);
512
513 /*
514 * if the translate is larger than our ints, we can get random results, or
515 * worse, we might get 0x80000000, which wreaks havoc on us, since we can't
516 * negate it.
517 */
518 const SkScalar too_big = SkIntToScalar(1 << 30);
519 if (SkScalarAbs(pt.fX) > too_big || SkScalarAbs(pt.fY) > too_big) {
520 return false;
521 }
522
523 // Since we know we're not filtered, we re-purpose these fields allow
524 // us to go from device -> src coordinates w/ just an integer add,
525 // rather than running through the inverse-matrix
526 fFilterOneX = mapper.intX();
527 fFilterOneY = mapper.intY();
528
529 return true;
530 }
531
chooseShaderProc32()532 SkBitmapProcState::ShaderProc32 SkBitmapProcState::chooseShaderProc32() {
533
534 if (kN32_SkColorType != fPixmap.colorType()) {
535 return nullptr;
536 }
537
538 static const unsigned kMask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
539
540 if (1 == fPixmap.width() && 0 == (fInvType & ~kMask)) {
541 if (kNone_SkFilterQuality == fFilterQuality &&
542 fInvType <= SkMatrix::kTranslate_Mask &&
543 !this->setupForTranslate()) {
544 return DoNothing_shaderproc;
545 }
546 return S32_D32_constX_shaderproc;
547 }
548
549 if (fAlphaScale < 256) {
550 return nullptr;
551 }
552 if (fInvType > SkMatrix::kTranslate_Mask) {
553 return nullptr;
554 }
555 if (kNone_SkFilterQuality != fFilterQuality) {
556 return nullptr;
557 }
558
559 SkTileMode tx = fTileModeX;
560 SkTileMode ty = fTileModeY;
561
562 if (SkTileMode::kClamp == tx && SkTileMode::kClamp == ty) {
563 if (this->setupForTranslate()) {
564 return Clamp_S32_D32_nofilter_trans_shaderproc;
565 }
566 return DoNothing_shaderproc;
567 }
568 if (SkTileMode::kRepeat == tx && SkTileMode::kRepeat == ty) {
569 if (this->setupForTranslate()) {
570 return Repeat_S32_D32_nofilter_trans_shaderproc;
571 }
572 return DoNothing_shaderproc;
573 }
574 return nullptr;
575 }
576
577 #ifdef SK_DEBUG
578
check_scale_nofilter(uint32_t bitmapXY[],int count,unsigned mx,unsigned my)579 static void check_scale_nofilter(uint32_t bitmapXY[], int count,
580 unsigned mx, unsigned my) {
581 unsigned y = *bitmapXY++;
582 SkASSERT(y < my);
583
584 const uint16_t* xptr = reinterpret_cast<const uint16_t*>(bitmapXY);
585 for (int i = 0; i < count; ++i) {
586 SkASSERT(xptr[i] < mx);
587 }
588 }
589
check_scale_filter(uint32_t bitmapXY[],int count,unsigned mx,unsigned my)590 static void check_scale_filter(uint32_t bitmapXY[], int count,
591 unsigned mx, unsigned my) {
592 uint32_t YY = *bitmapXY++;
593 unsigned y0 = YY >> 18;
594 unsigned y1 = YY & 0x3FFF;
595 SkASSERT(y0 < my);
596 SkASSERT(y1 < my);
597
598 for (int i = 0; i < count; ++i) {
599 uint32_t XX = bitmapXY[i];
600 unsigned x0 = XX >> 18;
601 unsigned x1 = XX & 0x3FFF;
602 SkASSERT(x0 < mx);
603 SkASSERT(x1 < mx);
604 }
605 }
606
DebugMatrixProc(const SkBitmapProcState & state,uint32_t bitmapXY[],int count,int x,int y)607 void SkBitmapProcState::DebugMatrixProc(const SkBitmapProcState& state,
608 uint32_t bitmapXY[], int count,
609 int x, int y) {
610 SkASSERT(bitmapXY);
611 SkASSERT(count > 0);
612
613 state.fMatrixProc(state, bitmapXY, count, x, y);
614
615 void (*proc)(uint32_t bitmapXY[], int count, unsigned mx, unsigned my);
616
617 // There are two formats possible:
618 // filter -vs- nofilter
619 SkASSERT(state.fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask));
620 proc = state.fFilterQuality != kNone_SkFilterQuality ?
621 check_scale_filter : check_scale_nofilter;
622 proc(bitmapXY, count, state.fPixmap.width(), state.fPixmap.height());
623 }
624
getMatrixProc() const625 SkBitmapProcState::MatrixProc SkBitmapProcState::getMatrixProc() const {
626 return DebugMatrixProc;
627 }
628
629 #endif
630
631 /*
632 The storage requirements for the different matrix procs are as follows,
633 where each X or Y is 2 bytes, and N is the number of pixels/elements:
634
635 scale/translate nofilter Y(4bytes) + N * X
636 affine/perspective nofilter N * (X Y)
637 scale/translate filter Y Y + N * (X X)
638 affine filter N * (Y Y X X)
639 */
maxCountForBufferSize(size_t bufferSize) const640 int SkBitmapProcState::maxCountForBufferSize(size_t bufferSize) const {
641 int32_t size = static_cast<int32_t>(bufferSize);
642
643 size &= ~3; // only care about 4-byte aligned chunks
644 if (fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) {
645 size -= 4; // the shared Y (or YY) coordinate
646 if (size < 0) {
647 size = 0;
648 }
649 size >>= 1;
650 } else {
651 size >>= 2;
652 }
653
654 if (fFilterQuality != kNone_SkFilterQuality) {
655 size >>= 1;
656 }
657
658 return size;
659 }
660
661