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