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