• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // our special-case shaderprocs
270     // TODO: move this one into chooseShaderProc32() or pull all that in here.
271     if (fAlphaScale == 256
272             && fFilterQuality == kNone_SkFilterQuality
273             && SkTileMode::kClamp == fTileModeX) {
274         fShaderProc32 = Clamp_S32_opaque_D32_nofilter_DX_shaderproc;
275     } else {
276         fShaderProc32 = this->chooseShaderProc32();
277     }
278 
279     return true;
280 }
281 
Clamp_S32_D32_nofilter_trans_shaderproc(const void * sIn,int x,int y,SkPMColor * colors,int count)282 static void Clamp_S32_D32_nofilter_trans_shaderproc(const void* sIn,
283                                                     int x, int y,
284                                                     SkPMColor* colors,
285                                                     int count) {
286     const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
287     SkASSERT(((s.fInvType & ~SkMatrix::kTranslate_Mask)) == 0);
288     SkASSERT(s.fInvKy == 0);
289     SkASSERT(count > 0 && colors != nullptr);
290     SkASSERT(kNone_SkFilterQuality == s.fFilterQuality);
291 
292     const int maxX = s.fPixmap.width() - 1;
293     const int maxY = s.fPixmap.height() - 1;
294     int ix = s.fFilterOneX + x;
295     int iy = SkClampMax(s.fFilterOneY + y, maxY);
296     const SkPMColor* row = s.fPixmap.addr32(0, iy);
297 
298     // clamp to the left
299     if (ix < 0) {
300         int n = SkMin32(-ix, count);
301         sk_memset32(colors, row[0], n);
302         count -= n;
303         if (0 == count) {
304             return;
305         }
306         colors += n;
307         SkASSERT(-ix == n);
308         ix = 0;
309     }
310     // copy the middle
311     if (ix <= maxX) {
312         int n = SkMin32(maxX - ix + 1, count);
313         memcpy(colors, row + ix, n * sizeof(SkPMColor));
314         count -= n;
315         if (0 == count) {
316             return;
317         }
318         colors += n;
319     }
320     SkASSERT(count > 0);
321     // clamp to the right
322     sk_memset32(colors, row[maxX], count);
323 }
324 
sk_int_mod(int x,int n)325 static inline int sk_int_mod(int x, int n) {
326     SkASSERT(n > 0);
327     if ((unsigned)x >= (unsigned)n) {
328         if (x < 0) {
329             x = n + ~(~x % n);
330         } else {
331             x = x % n;
332         }
333     }
334     return x;
335 }
336 
sk_int_mirror(int x,int n)337 static inline int sk_int_mirror(int x, int n) {
338     x = sk_int_mod(x, 2 * n);
339     if (x >= n) {
340         x = n + ~(x - n);
341     }
342     return x;
343 }
344 
Repeat_S32_D32_nofilter_trans_shaderproc(const void * sIn,int x,int y,SkPMColor * colors,int count)345 static void Repeat_S32_D32_nofilter_trans_shaderproc(const void* sIn,
346                                                      int x, int y,
347                                                      SkPMColor* colors,
348                                                      int count) {
349     const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
350     SkASSERT(((s.fInvType & ~SkMatrix::kTranslate_Mask)) == 0);
351     SkASSERT(s.fInvKy == 0);
352     SkASSERT(count > 0 && colors != nullptr);
353     SkASSERT(kNone_SkFilterQuality == s.fFilterQuality);
354 
355     const int stopX = s.fPixmap.width();
356     const int stopY = s.fPixmap.height();
357     int ix = s.fFilterOneX + x;
358     int iy = sk_int_mod(s.fFilterOneY + y, stopY);
359     const SkPMColor* row = s.fPixmap.addr32(0, iy);
360 
361     ix = sk_int_mod(ix, stopX);
362     for (;;) {
363         int n = SkMin32(stopX - ix, count);
364         memcpy(colors, row + ix, n * sizeof(SkPMColor));
365         count -= n;
366         if (0 == count) {
367             return;
368         }
369         colors += n;
370         ix = 0;
371     }
372 }
373 
filter_32_alpha(unsigned t,SkPMColor color0,SkPMColor color1,SkPMColor * dstColor,unsigned alphaScale)374 static inline void filter_32_alpha(unsigned t,
375                                    SkPMColor color0,
376                                    SkPMColor color1,
377                                    SkPMColor* dstColor,
378                                    unsigned alphaScale) {
379     SkASSERT((unsigned)t <= 0xF);
380     SkASSERT(alphaScale <= 256);
381 
382     const uint32_t mask = 0xFF00FF;
383 
384     int scale = 256 - 16*t;
385     uint32_t lo = (color0 & mask) * scale;
386     uint32_t hi = ((color0 >> 8) & mask) * scale;
387 
388     scale = 16*t;
389     lo += (color1 & mask) * scale;
390     hi += ((color1 >> 8) & mask) * scale;
391 
392     // TODO: if (alphaScale < 256) ...
393     lo = ((lo >> 8) & mask) * alphaScale;
394     hi = ((hi >> 8) & mask) * alphaScale;
395 
396     *dstColor = ((lo >> 8) & mask) | (hi & ~mask);
397 }
398 
S32_D32_constX_shaderproc(const void * sIn,int x,int y,SkPMColor * colors,int count)399 static void S32_D32_constX_shaderproc(const void* sIn,
400                                       int x, int y,
401                                       SkPMColor* colors,
402                                       int count) {
403     const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
404     SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) == 0);
405     SkASSERT(s.fInvKy == 0);
406     SkASSERT(count > 0 && colors != nullptr);
407     SkASSERT(1 == s.fPixmap.width());
408 
409     int iY0;
410     int iY1   SK_INIT_TO_AVOID_WARNING;
411     int iSubY SK_INIT_TO_AVOID_WARNING;
412 
413     if (kNone_SkFilterQuality != s.fFilterQuality) {
414         SkBitmapProcState::MatrixProc mproc = s.getMatrixProc();
415         uint32_t xy[2];
416 
417         mproc(s, xy, 1, x, y);
418 
419         iY0 = xy[0] >> 18;
420         iY1 = xy[0] & 0x3FFF;
421         iSubY = (xy[0] >> 14) & 0xF;
422     } else {
423         int yTemp;
424 
425         if (s.fInvType > SkMatrix::kTranslate_Mask) {
426             const SkBitmapProcStateAutoMapper mapper(s, x, y);
427 
428             // When the matrix has a scale component the setup code in
429             // chooseProcs multiples the inverse matrix by the inverse of the
430             // bitmap's width and height. Since this method is going to do
431             // its own tiling and sampling we need to undo that here.
432             if (SkTileMode::kClamp != s.fTileModeX || SkTileMode::kClamp != s.fTileModeY) {
433                 yTemp = SkFractionalIntToInt(mapper.fractionalIntY() * s.fPixmap.height());
434             } else {
435                 yTemp = mapper.intY();
436             }
437         } else {
438             yTemp = s.fFilterOneY + y;
439         }
440 
441         const int stopY = s.fPixmap.height();
442         switch (s.fTileModeY) {
443             case SkTileMode::kClamp:
444                 iY0 = SkClampMax(yTemp, stopY-1);
445                 break;
446             case SkTileMode::kRepeat:
447                 iY0 = sk_int_mod(yTemp, stopY);
448                 break;
449             case SkTileMode::kMirror:
450             default:
451                 iY0 = sk_int_mirror(yTemp, stopY);
452                 break;
453         }
454 
455 #ifdef SK_DEBUG
456         {
457             const SkBitmapProcStateAutoMapper mapper(s, x, y);
458             int iY2;
459 
460             if (s.fInvType > SkMatrix::kTranslate_Mask &&
461                 (SkTileMode::kClamp != s.fTileModeX || SkTileMode::kClamp != s.fTileModeY)) {
462                 iY2 = SkFractionalIntToInt(mapper.fractionalIntY() * s.fPixmap.height());
463             } else {
464                 iY2 = mapper.intY();
465             }
466 
467             switch (s.fTileModeY) {
468             case SkTileMode::kClamp:
469                 iY2 = SkClampMax(iY2, stopY-1);
470                 break;
471             case SkTileMode::kRepeat:
472                 iY2 = sk_int_mod(iY2, stopY);
473                 break;
474             case SkTileMode::kMirror:
475             default:
476                 iY2 = sk_int_mirror(iY2, stopY);
477                 break;
478             }
479 
480             SkASSERT(iY0 == iY2);
481         }
482 #endif
483     }
484 
485     const SkPMColor* row0 = s.fPixmap.addr32(0, iY0);
486     SkPMColor color;
487 
488     if (kNone_SkFilterQuality != s.fFilterQuality) {
489         const SkPMColor* row1 = s.fPixmap.addr32(0, iY1);
490         filter_32_alpha(iSubY, *row0, *row1, &color, s.fAlphaScale);
491     } else {
492         if (s.fAlphaScale < 256) {
493             color = SkAlphaMulQ(*row0, s.fAlphaScale);
494         } else {
495             color = *row0;
496         }
497     }
498 
499     sk_memset32(colors, color, count);
500 }
501 
DoNothing_shaderproc(const void *,int x,int y,SkPMColor * colors,int count)502 static void DoNothing_shaderproc(const void*, int x, int y,
503                                  SkPMColor* colors, int count) {
504     // if we get called, the matrix is too tricky, so we just draw nothing
505     sk_memset32(colors, 0, count);
506 }
507 
setupForTranslate()508 bool SkBitmapProcState::setupForTranslate() {
509     SkPoint pt;
510     const SkBitmapProcStateAutoMapper mapper(*this, 0, 0, &pt);
511 
512     /*
513      *  if the translate is larger than our ints, we can get random results, or
514      *  worse, we might get 0x80000000, which wreaks havoc on us, since we can't
515      *  negate it.
516      */
517     const SkScalar too_big = SkIntToScalar(1 << 30);
518     if (SkScalarAbs(pt.fX) > too_big || SkScalarAbs(pt.fY) > too_big) {
519         return false;
520     }
521 
522     // Since we know we're not filtered, we re-purpose these fields allow
523     // us to go from device -> src coordinates w/ just an integer add,
524     // rather than running through the inverse-matrix
525     fFilterOneX = mapper.intX();
526     fFilterOneY = mapper.intY();
527 
528     return true;
529 }
530 
chooseShaderProc32()531 SkBitmapProcState::ShaderProc32 SkBitmapProcState::chooseShaderProc32() {
532 
533     if (kN32_SkColorType != fPixmap.colorType()) {
534         return nullptr;
535     }
536 
537     static const unsigned kMask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
538 
539     if (1 == fPixmap.width() && 0 == (fInvType & ~kMask)) {
540         if (kNone_SkFilterQuality == fFilterQuality &&
541             fInvType <= SkMatrix::kTranslate_Mask &&
542             !this->setupForTranslate()) {
543             return DoNothing_shaderproc;
544         }
545         return S32_D32_constX_shaderproc;
546     }
547 
548     if (fAlphaScale < 256) {
549         return nullptr;
550     }
551     if (fInvType > SkMatrix::kTranslate_Mask) {
552         return nullptr;
553     }
554     if (kNone_SkFilterQuality != fFilterQuality) {
555         return nullptr;
556     }
557 
558     SkTileMode tx = fTileModeX;
559     SkTileMode ty = fTileModeY;
560 
561     if (SkTileMode::kClamp == tx && SkTileMode::kClamp == ty) {
562         if (this->setupForTranslate()) {
563             return Clamp_S32_D32_nofilter_trans_shaderproc;
564         }
565         return DoNothing_shaderproc;
566     }
567     if (SkTileMode::kRepeat == tx && SkTileMode::kRepeat == ty) {
568         if (this->setupForTranslate()) {
569             return Repeat_S32_D32_nofilter_trans_shaderproc;
570         }
571         return DoNothing_shaderproc;
572     }
573     return nullptr;
574 }
575 
576 #ifdef SK_DEBUG
577 
check_scale_nofilter(uint32_t bitmapXY[],int count,unsigned mx,unsigned my)578 static void check_scale_nofilter(uint32_t bitmapXY[], int count,
579                                  unsigned mx, unsigned my) {
580     unsigned y = *bitmapXY++;
581     SkASSERT(y < my);
582 
583     const uint16_t* xptr = reinterpret_cast<const uint16_t*>(bitmapXY);
584     for (int i = 0; i < count; ++i) {
585         SkASSERT(xptr[i] < mx);
586     }
587 }
588 
check_scale_filter(uint32_t bitmapXY[],int count,unsigned mx,unsigned my)589 static void check_scale_filter(uint32_t bitmapXY[], int count,
590                                  unsigned mx, unsigned my) {
591     uint32_t YY = *bitmapXY++;
592     unsigned y0 = YY >> 18;
593     unsigned y1 = YY & 0x3FFF;
594     SkASSERT(y0 < my);
595     SkASSERT(y1 < my);
596 
597     for (int i = 0; i < count; ++i) {
598         uint32_t XX = bitmapXY[i];
599         unsigned x0 = XX >> 18;
600         unsigned x1 = XX & 0x3FFF;
601         SkASSERT(x0 < mx);
602         SkASSERT(x1 < mx);
603     }
604 }
605 
DebugMatrixProc(const SkBitmapProcState & state,uint32_t bitmapXY[],int count,int x,int y)606 void SkBitmapProcState::DebugMatrixProc(const SkBitmapProcState& state,
607                                         uint32_t bitmapXY[], int count,
608                                         int x, int y) {
609     SkASSERT(bitmapXY);
610     SkASSERT(count > 0);
611 
612     state.fMatrixProc(state, bitmapXY, count, x, y);
613 
614     void (*proc)(uint32_t bitmapXY[], int count, unsigned mx, unsigned my);
615 
616     // There are two formats possible:
617     //  filter -vs- nofilter
618     SkASSERT(state.fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask));
619     proc = state.fFilterQuality != kNone_SkFilterQuality ?
620                 check_scale_filter : check_scale_nofilter;
621     proc(bitmapXY, count, state.fPixmap.width(), state.fPixmap.height());
622 }
623 
getMatrixProc() const624 SkBitmapProcState::MatrixProc SkBitmapProcState::getMatrixProc() const {
625     return DebugMatrixProc;
626 }
627 
628 #endif
629 
630 /*
631     The storage requirements for the different matrix procs are as follows,
632     where each X or Y is 2 bytes, and N is the number of pixels/elements:
633 
634     scale/translate     nofilter      Y(4bytes) + N * X
635     affine/perspective  nofilter      N * (X Y)
636     scale/translate     filter        Y Y + N * (X X)
637     affine              filter        N * (Y Y X X)
638  */
maxCountForBufferSize(size_t bufferSize) const639 int SkBitmapProcState::maxCountForBufferSize(size_t bufferSize) const {
640     int32_t size = static_cast<int32_t>(bufferSize);
641 
642     size &= ~3; // only care about 4-byte aligned chunks
643     if (fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) {
644         size -= 4;   // the shared Y (or YY) coordinate
645         if (size < 0) {
646             size = 0;
647         }
648         size >>= 1;
649     } else {
650         size >>= 2;
651     }
652 
653     if (fFilterQuality != kNone_SkFilterQuality) {
654         size >>= 1;
655     }
656 
657     return size;
658 }
659 
660