Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / core / SkBitmapProcState.cpp
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 "SkBitmapProcState.h"
10 #include "SkColorPriv.h"
11 #include "SkFilterProc.h"
12 #include "SkPaint.h"
13 #include "SkShader.h"   // for tilemodes
14 #include "SkUtilsArm.h"
15 #include "SkBitmapScaler.h"
16 #include "SkMipMap.h"
17 #include "SkPixelRef.h"
18 #include "SkImageEncoder.h"
19 #include "SkResourceCache.h"
20
21 #if !SK_ARM_NEON_IS_NONE
22 // These are defined in src/opts/SkBitmapProcState_arm_neon.cpp
23 extern const SkBitmapProcState::SampleProc16 gSkBitmapProcStateSample16_neon[];
24 extern const SkBitmapProcState::SampleProc32 gSkBitmapProcStateSample32_neon[];
25 extern void  S16_D16_filter_DX_neon(const SkBitmapProcState&, const uint32_t*, int, uint16_t*);
26 extern void  Clamp_S16_D16_filter_DX_shaderproc_neon(const SkBitmapProcState&, int, int, uint16_t*, int);
27 extern void  Repeat_S16_D16_filter_DX_shaderproc_neon(const SkBitmapProcState&, int, int, uint16_t*, int);
28 extern void  SI8_opaque_D32_filter_DX_neon(const SkBitmapProcState&, const uint32_t*, int, SkPMColor*);
29 extern void  SI8_opaque_D32_filter_DX_shaderproc_neon(const SkBitmapProcState&, int, int, uint32_t*, int);
30 extern void  Clamp_SI8_opaque_D32_filter_DX_shaderproc_neon(const SkBitmapProcState&, int, int, uint32_t*, int);
31 #endif
32
33 #define   NAME_WRAP(x)  x
34 #include "SkBitmapProcState_filter.h"
35 #include "SkBitmapProcState_procs.h"
36
37 ///////////////////////////////////////////////////////////////////////////////
38
39 // true iff the matrix contains, at most, scale and translate elements
40 static bool matrix_only_scale_translate(const SkMatrix& m) {
41     return m.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask);
42 }
43
44 /**
45  *  For the purposes of drawing bitmaps, if a matrix is "almost" translate
46  *  go ahead and treat it as if it were, so that subsequent code can go fast.
47  */
48 static bool just_trans_clamp(const SkMatrix& matrix, const SkBitmap& bitmap) {
49     SkASSERT(matrix_only_scale_translate(matrix));
50
51     if (matrix.getType() & SkMatrix::kScale_Mask) {
52         SkRect src, dst;
53         bitmap.getBounds(&src);
54
55         // Can't call mapRect(), since that will fix up inverted rectangles,
56         // e.g. when scale is negative, and we don't want to return true for
57         // those.
58         matrix.mapPoints(SkTCast<SkPoint*>(&dst),
59                          SkTCast<const SkPoint*>(&src),
60                          2);
61
62         // Now round all 4 edges to device space, and then compare the device
63         // width/height to the original. Note: we must map all 4 and subtract
64         // rather than map the "width" and compare, since we care about the
65         // phase (in pixel space) that any translate in the matrix might impart.
66         SkIRect idst;
67         dst.round(&idst);
68         return idst.width() == bitmap.width() && idst.height() == bitmap.height();
69     }
70     // if we got here, we're either kTranslate_Mask or identity
71     return true;
72 }
73
74 static bool just_trans_general(const SkMatrix& matrix) {
75     SkASSERT(matrix_only_scale_translate(matrix));
76
77     if (matrix.getType() & SkMatrix::kScale_Mask) {
78         const SkScalar tol = SK_Scalar1 / 32768;
79
80         if (!SkScalarNearlyZero(matrix[SkMatrix::kMScaleX] - SK_Scalar1, tol)) {
81             return false;
82         }
83         if (!SkScalarNearlyZero(matrix[SkMatrix::kMScaleY] - SK_Scalar1, tol)) {
84             return false;
85         }
86     }
87     // if we got here, treat us as either kTranslate_Mask or identity
88     return true;
89 }
90
91 ///////////////////////////////////////////////////////////////////////////////
92
93 static bool valid_for_filtering(unsigned dimension) {
94     // for filtering, width and height must fit in 14bits, since we use steal
95     // 2 bits from each to store our 4bit subpixel data
96     return (dimension & ~0x3FFF) == 0;
97 }
98
99 static SkScalar effective_matrix_scale_sqrd(const SkMatrix& mat) {
100     SkPoint v1, v2;
101
102     v1.fX = mat.getScaleX();
103     v1.fY = mat.getSkewY();
104
105     v2.fX = mat.getSkewX();
106     v2.fY = mat.getScaleY();
107
108     return SkMaxScalar(v1.lengthSqd(), v2.lengthSqd());
109 }
110
111 // Check to see that the size of the bitmap that would be produced by
112 // scaling by the given inverted matrix is less than the maximum allowed.
113 static inline bool cache_size_okay(const SkBitmap& bm, const SkMatrix& invMat) {
114     size_t maximumAllocation = SkResourceCache::GetSingleAllocationByteLimit();
115     if (0 == maximumAllocation) {
116         return true;
117     }
118     // float matrixScaleFactor = 1.0 / (invMat.scaleX * invMat.scaleY);
119     // return ((origBitmapSize * matrixScaleFactor) < maximumAllocationSize);
120     // Skip the division step:
121     return bm.info().getSafeSize(bm.info().minRowBytes())
122         < (maximumAllocation * invMat.getScaleX() * invMat.getScaleY());
123 }
124
125 // TODO -- we may want to pass the clip into this function so we only scale
126 // the portion of the image that we're going to need.  This will complicate
127 // the interface to the cache, but might be well worth it.
128
129 bool SkBitmapProcState::possiblyScaleImage() {
130     SkASSERT(NULL == fBitmap);
131
132     fAdjustedMatrix = false;
133
134     if (fFilterLevel <= SkPaint::kLow_FilterLevel) {
135         return false;
136     }
137     // Check to see if the transformation matrix is simple, and if we're
138     // doing high quality scaling.  If so, do the bitmap scale here and
139     // remove the (non-fractional) scaling component from the matrix.
140
141     SkScalar invScaleX = fInvMatrix.getScaleX();
142     SkScalar invScaleY = fInvMatrix.getScaleY();
143
144     float trueDestWidth  = fOrigBitmap.width() / invScaleX;
145     float trueDestHeight = fOrigBitmap.height() / invScaleY;
146
147 #ifndef SK_IGNORE_PROPER_FRACTIONAL_SCALING
148     float roundedDestWidth = SkScalarRoundToScalar(trueDestWidth);
149     float roundedDestHeight = SkScalarRoundToScalar(trueDestHeight);
150 #else
151     float roundedDestWidth = trueDestWidth;
152     float roundedDestHeight = trueDestHeight;
153 #endif
154
155     if (SkPaint::kHigh_FilterLevel == fFilterLevel &&
156         fInvMatrix.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask) &&
157         kN32_SkColorType == fOrigBitmap.colorType() &&
158         cache_size_okay(fOrigBitmap, fInvMatrix)) {
159
160         if (SkScalarNearlyEqual(invScaleX,1.0f) &&
161             SkScalarNearlyEqual(invScaleY,1.0f)) {
162             // short-circuit identity scaling; the output is supposed to
163             // be the same as the input, so we might as well go fast.
164
165             // Note(humper): We could also probably do this if the scales
166             // are close to -1 as well, since the flip doesn't require
167             // any fancy re-sampling...
168
169             // Set our filter level to low -- the only post-filtering this
170             // image might require is some interpolation if the translation
171             // is fractional.
172             fFilterLevel = SkPaint::kLow_FilterLevel;
173             return false;
174         }
175
176         if (!SkBitmapCache::Find(fOrigBitmap, roundedDestWidth, roundedDestHeight, &fScaledBitmap)) {
177             // All the criteria are met; let's make a new bitmap.
178
179             if (!SkBitmapScaler::Resize(&fScaledBitmap,
180                                         fOrigBitmap,
181                                         SkBitmapScaler::RESIZE_BEST,
182                                         roundedDestWidth,
183                                         roundedDestHeight,
184                                         SkResourceCache::GetAllocator())) {
185                 // we failed to create fScaledBitmap, so just return and let
186                 // the scanline proc handle it.
187                 return false;
188
189             }
190
191             SkASSERT(fScaledBitmap.getPixels());
192             fScaledBitmap.setImmutable();
193             SkBitmapCache::Add(fOrigBitmap, roundedDestWidth, roundedDestHeight, fScaledBitmap);
194         }
195
196         SkASSERT(fScaledBitmap.getPixels());
197         fBitmap = &fScaledBitmap;
198
199         // set the inv matrix type to translate-only;
200         fInvMatrix.setTranslate(fInvMatrix.getTranslateX() / fInvMatrix.getScaleX(),
201                                 fInvMatrix.getTranslateY() / fInvMatrix.getScaleY());
202
203 #ifndef SK_IGNORE_PROPER_FRACTIONAL_SCALING
204         // reintroduce any fractional scaling missed by our integral scale done above.
205
206        float fractionalScaleX = roundedDestWidth/trueDestWidth;
207        float fractionalScaleY = roundedDestHeight/trueDestHeight;
208
209        fInvMatrix.postScale(fractionalScaleX, fractionalScaleY);
210 #endif
211         fAdjustedMatrix = true;
212
213         // Set our filter level to low -- the only post-filtering this
214         // image might require is some interpolation if the translation
215         // is fractional or if there's any remaining scaling to be done.
216         fFilterLevel = SkPaint::kLow_FilterLevel;
217         return true;
218     }
219
220     /*
221      *  If High, then our special-case for scale-only did not take, and so we
222      *  have to make a choice:
223      *      1. fall back on mipmaps + bilerp
224      *      2. fall back on scanline bicubic filter
225      *  For now, we compute the "scale" value from the matrix, and have a
226      *  threshold to decide when bicubic is better, and when mips are better.
227      *  No doubt a fancier decision tree could be used uere.
228      *
229      *  If Medium, then we just try to build a mipmap and select a level,
230      *  setting the filter-level to kLow to signal that we just need bilerp
231      *  to process the selected level.
232      */
233
234     SkScalar scaleSqd = effective_matrix_scale_sqrd(fInvMatrix);
235
236     if (SkPaint::kHigh_FilterLevel == fFilterLevel) {
237         // Set the limit at 0.25 for the CTM... if the CTM is scaling smaller
238         // than this, then the mipmaps quality may be greater (certainly faster)
239         // so we only keep High quality if the scale is greater than this.
240         //
241         // Since we're dealing with the inverse, we compare against its inverse.
242         const SkScalar bicubicLimit = 4.0f;
243         const SkScalar bicubicLimitSqd = bicubicLimit * bicubicLimit;
244         if (scaleSqd < bicubicLimitSqd) {  // use bicubic scanline
245             return false;
246         }
247
248         // else set the filter-level to Medium, since we're scaling down and
249         // want to reqeust mipmaps
250         fFilterLevel = SkPaint::kMedium_FilterLevel;
251     }
252
253     SkASSERT(SkPaint::kMedium_FilterLevel == fFilterLevel);
254
255     /**
256      *  Medium quality means use a mipmap for down-scaling, and just bilper
257      *  for upscaling. Since we're examining the inverse matrix, we look for
258      *  a scale > 1 to indicate down scaling by the CTM.
259      */
260     if (scaleSqd > SK_Scalar1) {
261         fCurrMip.reset(SkMipMapCache::FindAndRef(fOrigBitmap));
262         if (NULL == fCurrMip.get()) {
263             fCurrMip.reset(SkMipMap::Build(fOrigBitmap));
264             if (NULL == fCurrMip.get()) {
265                 return false;
266             }
267             SkMipMapCache::Add(fOrigBitmap, fCurrMip);
268         }
269
270         SkScalar levelScale = SkScalarInvert(SkScalarSqrt(scaleSqd));
271         SkMipMap::Level level;
272         if (fCurrMip->extractLevel(levelScale, &level)) {
273             SkScalar invScaleFixup = level.fScale;
274             fInvMatrix.postScale(invScaleFixup, invScaleFixup);
275
276             const SkImageInfo info = fOrigBitmap.info().makeWH(level.fWidth, level.fHeight);
277             // todo: if we could wrap the fCurrMip in a pixelref, then we could just install
278             //       that here, and not need to explicitly track it ourselves.
279             fScaledBitmap.installPixels(info, level.fPixels, level.fRowBytes);
280             fBitmap = &fScaledBitmap;
281             fFilterLevel = SkPaint::kLow_FilterLevel;
282             return true;
283         }
284     }
285
286     return false;
287 }
288
289 static bool get_locked_pixels(const SkBitmap& src, int pow2, SkBitmap* dst) {
290     SkPixelRef* pr = src.pixelRef();
291     if (pr && pr->decodeInto(pow2, dst)) {
292         return true;
293     }
294
295     /*
296      *  If decodeInto() fails, it is possibe that we have an old subclass that
297      *  does not, or cannot, implement that. In that case we fall back to the
298      *  older protocol of having the pixelRef handle the caching for us.
299      */
300     *dst = src;
301     dst->lockPixels();
302     return SkToBool(dst->getPixels());
303 }
304
305 bool SkBitmapProcState::lockBaseBitmap() {
306     SkPixelRef* pr = fOrigBitmap.pixelRef();
307
308     if (pr->isLocked() || !pr->implementsDecodeInto()) {
309         // fast-case, no need to look in our cache
310         fScaledBitmap = fOrigBitmap;
311         fScaledBitmap.lockPixels();
312         if (NULL == fScaledBitmap.getPixels()) {
313             return false;
314         }
315     } else {
316         if (!SkBitmapCache::Find(fOrigBitmap, 1, 1, &fScaledBitmap)) {
317             if (!get_locked_pixels(fOrigBitmap, 0, &fScaledBitmap)) {
318                 return false;
319             }
320
321             // TODO: if fScaled comes back at a different width/height than fOrig,
322             // we need to update the matrix we are using to sample from this guy.
323
324             SkBitmapCache::Add(fOrigBitmap, 1, 1, fScaledBitmap);
325         }
326     }
327     fBitmap = &fScaledBitmap;
328     return true;
329 }
330
331 SkBitmapProcState::~SkBitmapProcState() {
332     SkDELETE(fBitmapFilter);
333 }
334
335 bool SkBitmapProcState::chooseProcs(const SkMatrix& inv, const SkPaint& paint) {
336     SkASSERT(fOrigBitmap.width() && fOrigBitmap.height());
337
338     fBitmap = NULL;
339     fInvMatrix = inv;
340     fFilterLevel = paint.getFilterLevel();
341
342     // possiblyScaleImage will look to see if it can rescale the image as a
343     // preprocess; either by scaling up to the target size, or by selecting
344     // a nearby mipmap level.  If it does, it will adjust the working
345     // matrix as well as the working bitmap.  It may also adjust the filter
346     // quality to avoid re-filtering an already perfectly scaled image.
347     if (!this->possiblyScaleImage()) {
348         if (!this->lockBaseBitmap()) {
349             return false;
350         }
351     }
352     // The above logic should have always assigned fBitmap, but in case it
353     // didn't, we check for that now...
354     // TODO(dominikg): Ask humper@ if we can just use an SkASSERT(fBitmap)?
355     if (NULL == fBitmap) {
356         return false;
357     }
358
359     // If we are "still" kMedium_FilterLevel, then the request was not fulfilled by possiblyScale,
360     // so we downgrade to kLow (so the rest of the sniffing code can assume that)
361     if (SkPaint::kMedium_FilterLevel == fFilterLevel) {
362         fFilterLevel = SkPaint::kLow_FilterLevel;
363     }
364
365     bool trivialMatrix = (fInvMatrix.getType() & ~SkMatrix::kTranslate_Mask) == 0;
366     bool clampClamp = SkShader::kClamp_TileMode == fTileModeX &&
367                       SkShader::kClamp_TileMode == fTileModeY;
368
369     if (!(fAdjustedMatrix || clampClamp || trivialMatrix)) {
370         fInvMatrix.postIDiv(fOrigBitmap.width(), fOrigBitmap.height());
371     }
372
373     // Now that all possible changes to the matrix have taken place, check
374     // to see if we're really close to a no-scale matrix.  If so, explicitly
375     // set it to be so.  Subsequent code may inspect this matrix to choose
376     // a faster path in this case.
377
378     // This code will only execute if the matrix has some scale component;
379     // if it's already pure translate then we won't do this inversion.
380
381     if (matrix_only_scale_translate(fInvMatrix)) {
382         SkMatrix forward;
383         if (fInvMatrix.invert(&forward)) {
384             if (clampClamp ? just_trans_clamp(forward, *fBitmap)
385                             : just_trans_general(forward)) {
386                 SkScalar tx = -SkScalarRoundToScalar(forward.getTranslateX());
387                 SkScalar ty = -SkScalarRoundToScalar(forward.getTranslateY());
388                 fInvMatrix.setTranslate(tx, ty);
389             }
390         }
391     }
392
393     fInvProc        = fInvMatrix.getMapXYProc();
394     fInvType        = fInvMatrix.getType();
395     fInvSx          = SkScalarToFixed(fInvMatrix.getScaleX());
396     fInvSxFractionalInt = SkScalarToFractionalInt(fInvMatrix.getScaleX());
397     fInvKy          = SkScalarToFixed(fInvMatrix.getSkewY());
398     fInvKyFractionalInt = SkScalarToFractionalInt(fInvMatrix.getSkewY());
399
400     fAlphaScale = SkAlpha255To256(paint.getAlpha());
401
402     fShaderProc32 = NULL;
403     fShaderProc16 = NULL;
404     fSampleProc32 = NULL;
405     fSampleProc16 = NULL;
406
407     // recompute the triviality of the matrix here because we may have
408     // changed it!
409
410     trivialMatrix = (fInvMatrix.getType() & ~SkMatrix::kTranslate_Mask) == 0;
411
412     if (SkPaint::kHigh_FilterLevel == fFilterLevel) {
413         // If this is still set, that means we wanted HQ sampling
414         // but couldn't do it as a preprocess.  Let's try to install
415         // the scanline version of the HQ sampler.  If that process fails,
416         // downgrade to bilerp.
417
418         // NOTE: Might need to be careful here in the future when we want
419         // to have the platform proc have a shot at this; it's possible that
420         // the chooseBitmapFilterProc will fail to install a shader but a
421         // platform-specific one might succeed, so it might be premature here
422         // to fall back to bilerp.  This needs thought.
423
424         if (!this->setBitmapFilterProcs()) {
425             fFilterLevel = SkPaint::kLow_FilterLevel;
426         }
427     }
428
429     if (SkPaint::kLow_FilterLevel == fFilterLevel) {
430         // Only try bilerp if the matrix is "interesting" and
431         // the image has a suitable size.
432
433         if (fInvType <= SkMatrix::kTranslate_Mask ||
434                 !valid_for_filtering(fBitmap->width() | fBitmap->height())) {
435             fFilterLevel = SkPaint::kNone_FilterLevel;
436         }
437     }
438
439     // At this point, we know exactly what kind of sampling the per-scanline
440     // shader will perform.
441
442     fMatrixProc = this->chooseMatrixProc(trivialMatrix);
443     // TODO(dominikg): SkASSERT(fMatrixProc) instead? chooseMatrixProc never returns NULL.
444     if (NULL == fMatrixProc) {
445         return false;
446     }
447
448     ///////////////////////////////////////////////////////////////////////
449
450     const SkAlphaType at = fBitmap->alphaType();
451
452     // No need to do this if we're doing HQ sampling; if filter quality is
453     // still set to HQ by the time we get here, then we must have installed
454     // the shader procs above and can skip all this.
455
456     if (fFilterLevel < SkPaint::kHigh_FilterLevel) {
457
458         int index = 0;
459         if (fAlphaScale < 256) {  // note: this distinction is not used for D16
460             index |= 1;
461         }
462         if (fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) {
463             index |= 2;
464         }
465         if (fFilterLevel > SkPaint::kNone_FilterLevel) {
466             index |= 4;
467         }
468         // bits 3,4,5 encoding the source bitmap format
469         switch (fBitmap->colorType()) {
470             case kN32_SkColorType:
471                 if (kPremul_SkAlphaType != at && kOpaque_SkAlphaType != at) {
472                     return false;
473                 }
474                 index |= 0;
475                 break;
476             case kRGB_565_SkColorType:
477                 index |= 8;
478                 break;
479             case kIndex_8_SkColorType:
480                 if (kPremul_SkAlphaType != at && kOpaque_SkAlphaType != at) {
481                     return false;
482                 }
483                 index |= 16;
484                 break;
485             case kARGB_4444_SkColorType:
486                 if (kPremul_SkAlphaType != at && kOpaque_SkAlphaType != at) {
487                     return false;
488                 }
489                 index |= 24;
490                 break;
491             case kAlpha_8_SkColorType:
492                 index |= 32;
493                 fPaintPMColor = SkPreMultiplyColor(paint.getColor());
494                 break;
495             default:
496                 // TODO(dominikg): Should we ever get here? SkASSERT(false) instead?
497                 return false;
498         }
499
500     #if !SK_ARM_NEON_IS_ALWAYS
501         static const SampleProc32 gSkBitmapProcStateSample32[] = {
502             S32_opaque_D32_nofilter_DXDY,
503             S32_alpha_D32_nofilter_DXDY,
504             S32_opaque_D32_nofilter_DX,
505             S32_alpha_D32_nofilter_DX,
506             S32_opaque_D32_filter_DXDY,
507             S32_alpha_D32_filter_DXDY,
508             S32_opaque_D32_filter_DX,
509             S32_alpha_D32_filter_DX,
510
511             S16_opaque_D32_nofilter_DXDY,
512             S16_alpha_D32_nofilter_DXDY,
513             S16_opaque_D32_nofilter_DX,
514             S16_alpha_D32_nofilter_DX,
515             S16_opaque_D32_filter_DXDY,
516             S16_alpha_D32_filter_DXDY,
517             S16_opaque_D32_filter_DX,
518             S16_alpha_D32_filter_DX,
519
520             SI8_opaque_D32_nofilter_DXDY,
521             SI8_alpha_D32_nofilter_DXDY,
522             SI8_opaque_D32_nofilter_DX,
523             SI8_alpha_D32_nofilter_DX,
524             SI8_opaque_D32_filter_DXDY,
525             SI8_alpha_D32_filter_DXDY,
526             SI8_opaque_D32_filter_DX,
527             SI8_alpha_D32_filter_DX,
528
529             S4444_opaque_D32_nofilter_DXDY,
530             S4444_alpha_D32_nofilter_DXDY,
531             S4444_opaque_D32_nofilter_DX,
532             S4444_alpha_D32_nofilter_DX,
533             S4444_opaque_D32_filter_DXDY,
534             S4444_alpha_D32_filter_DXDY,
535             S4444_opaque_D32_filter_DX,
536             S4444_alpha_D32_filter_DX,
537
538             // A8 treats alpha/opaque the same (equally efficient)
539             SA8_alpha_D32_nofilter_DXDY,
540             SA8_alpha_D32_nofilter_DXDY,
541             SA8_alpha_D32_nofilter_DX,
542             SA8_alpha_D32_nofilter_DX,
543             SA8_alpha_D32_filter_DXDY,
544             SA8_alpha_D32_filter_DXDY,
545             SA8_alpha_D32_filter_DX,
546             SA8_alpha_D32_filter_DX
547         };
548
549         static const SampleProc16 gSkBitmapProcStateSample16[] = {
550             S32_D16_nofilter_DXDY,
551             S32_D16_nofilter_DX,
552             S32_D16_filter_DXDY,
553             S32_D16_filter_DX,
554
555             S16_D16_nofilter_DXDY,
556             S16_D16_nofilter_DX,
557             S16_D16_filter_DXDY,
558             S16_D16_filter_DX,
559
560             SI8_D16_nofilter_DXDY,
561             SI8_D16_nofilter_DX,
562             SI8_D16_filter_DXDY,
563             SI8_D16_filter_DX,
564
565             // Don't support 4444 -> 565
566             NULL, NULL, NULL, NULL,
567             // Don't support A8 -> 565
568             NULL, NULL, NULL, NULL
569         };
570     #endif
571
572         fSampleProc32 = SK_ARM_NEON_WRAP(gSkBitmapProcStateSample32)[index];
573         index >>= 1;    // shift away any opaque/alpha distinction
574         fSampleProc16 = SK_ARM_NEON_WRAP(gSkBitmapProcStateSample16)[index];
575
576         // our special-case shaderprocs
577         if (SK_ARM_NEON_WRAP(S16_D16_filter_DX) == fSampleProc16) {
578             if (clampClamp) {
579                 fShaderProc16 = SK_ARM_NEON_WRAP(Clamp_S16_D16_filter_DX_shaderproc);
580             } else if (SkShader::kRepeat_TileMode == fTileModeX &&
581                        SkShader::kRepeat_TileMode == fTileModeY) {
582                 fShaderProc16 = SK_ARM_NEON_WRAP(Repeat_S16_D16_filter_DX_shaderproc);
583             }
584         } else if (SK_ARM_NEON_WRAP(SI8_opaque_D32_filter_DX) == fSampleProc32 && clampClamp) {
585             fShaderProc32 = SK_ARM_NEON_WRAP(Clamp_SI8_opaque_D32_filter_DX_shaderproc);
586         }
587
588         if (NULL == fShaderProc32) {
589             fShaderProc32 = this->chooseShaderProc32();
590         }
591     }
592
593     // see if our platform has any accelerated overrides
594     this->platformProcs();
595
596     return true;
597 }
598
599 static void Clamp_S32_D32_nofilter_trans_shaderproc(const SkBitmapProcState& s,
600                                                     int x, int y,
601                                                     SkPMColor* SK_RESTRICT colors,
602                                                     int count) {
603     SkASSERT(((s.fInvType & ~SkMatrix::kTranslate_Mask)) == 0);
604     SkASSERT(s.fInvKy == 0);
605     SkASSERT(count > 0 && colors != NULL);
606     SkASSERT(SkPaint::kNone_FilterLevel == s.fFilterLevel);
607
608     const int maxX = s.fBitmap->width() - 1;
609     const int maxY = s.fBitmap->height() - 1;
610     int ix = s.fFilterOneX + x;
611     int iy = SkClampMax(s.fFilterOneY + y, maxY);
612 #ifdef SK_DEBUG
613     {
614         SkPoint pt;
615         s.fInvProc(s.fInvMatrix, SkIntToScalar(x) + SK_ScalarHalf,
616                    SkIntToScalar(y) + SK_ScalarHalf, &pt);
617         int iy2 = SkClampMax(SkScalarFloorToInt(pt.fY), maxY);
618         int ix2 = SkScalarFloorToInt(pt.fX);
619
620         SkASSERT(iy == iy2);
621         SkASSERT(ix == ix2);
622     }
623 #endif
624     const SkPMColor* row = s.fBitmap->getAddr32(0, iy);
625
626     // clamp to the left
627     if (ix < 0) {
628         int n = SkMin32(-ix, count);
629         sk_memset32(colors, row[0], n);
630         count -= n;
631         if (0 == count) {
632             return;
633         }
634         colors += n;
635         SkASSERT(-ix == n);
636         ix = 0;
637     }
638     // copy the middle
639     if (ix <= maxX) {
640         int n = SkMin32(maxX - ix + 1, count);
641         memcpy(colors, row + ix, n * sizeof(SkPMColor));
642         count -= n;
643         if (0 == count) {
644             return;
645         }
646         colors += n;
647     }
648     SkASSERT(count > 0);
649     // clamp to the right
650     sk_memset32(colors, row[maxX], count);
651 }
652
653 static inline int sk_int_mod(int x, int n) {
654     SkASSERT(n > 0);
655     if ((unsigned)x >= (unsigned)n) {
656         if (x < 0) {
657             x = n + ~(~x % n);
658         } else {
659             x = x % n;
660         }
661     }
662     return x;
663 }
664
665 static inline int sk_int_mirror(int x, int n) {
666     x = sk_int_mod(x, 2 * n);
667     if (x >= n) {
668         x = n + ~(x - n);
669     }
670     return x;
671 }
672
673 static void Repeat_S32_D32_nofilter_trans_shaderproc(const SkBitmapProcState& s,
674                                                      int x, int y,
675                                                      SkPMColor* SK_RESTRICT colors,
676                                                      int count) {
677     SkASSERT(((s.fInvType & ~SkMatrix::kTranslate_Mask)) == 0);
678     SkASSERT(s.fInvKy == 0);
679     SkASSERT(count > 0 && colors != NULL);
680     SkASSERT(SkPaint::kNone_FilterLevel == s.fFilterLevel);
681
682     const int stopX = s.fBitmap->width();
683     const int stopY = s.fBitmap->height();
684     int ix = s.fFilterOneX + x;
685     int iy = sk_int_mod(s.fFilterOneY + y, stopY);
686 #ifdef SK_DEBUG
687     {
688         SkPoint pt;
689         s.fInvProc(s.fInvMatrix, SkIntToScalar(x) + SK_ScalarHalf,
690                    SkIntToScalar(y) + SK_ScalarHalf, &pt);
691         int iy2 = sk_int_mod(SkScalarFloorToInt(pt.fY), stopY);
692         int ix2 = SkScalarFloorToInt(pt.fX);
693
694         SkASSERT(iy == iy2);
695         SkASSERT(ix == ix2);
696     }
697 #endif
698     const SkPMColor* row = s.fBitmap->getAddr32(0, iy);
699
700     ix = sk_int_mod(ix, stopX);
701     for (;;) {
702         int n = SkMin32(stopX - ix, count);
703         memcpy(colors, row + ix, n * sizeof(SkPMColor));
704         count -= n;
705         if (0 == count) {
706             return;
707         }
708         colors += n;
709         ix = 0;
710     }
711 }
712
713 static void S32_D32_constX_shaderproc(const SkBitmapProcState& s,
714                                       int x, int y,
715                                       SkPMColor* SK_RESTRICT colors,
716                                       int count) {
717     SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) == 0);
718     SkASSERT(s.fInvKy == 0);
719     SkASSERT(count > 0 && colors != NULL);
720     SkASSERT(1 == s.fBitmap->width());
721
722     int iY0;
723     int iY1   SK_INIT_TO_AVOID_WARNING;
724     int iSubY SK_INIT_TO_AVOID_WARNING;
725
726     if (SkPaint::kNone_FilterLevel != s.fFilterLevel) {
727         SkBitmapProcState::MatrixProc mproc = s.getMatrixProc();
728         uint32_t xy[2];
729
730         mproc(s, xy, 1, x, y);
731
732         iY0 = xy[0] >> 18;
733         iY1 = xy[0] & 0x3FFF;
734         iSubY = (xy[0] >> 14) & 0xF;
735     } else {
736         int yTemp;
737
738         if (s.fInvType > SkMatrix::kTranslate_Mask) {
739             SkPoint pt;
740             s.fInvProc(s.fInvMatrix,
741                        SkIntToScalar(x) + SK_ScalarHalf,
742                        SkIntToScalar(y) + SK_ScalarHalf,
743                        &pt);
744             // When the matrix has a scale component the setup code in
745             // chooseProcs multiples the inverse matrix by the inverse of the
746             // bitmap's width and height. Since this method is going to do
747             // its own tiling and sampling we need to undo that here.
748             if (SkShader::kClamp_TileMode != s.fTileModeX ||
749                 SkShader::kClamp_TileMode != s.fTileModeY) {
750                 yTemp = SkScalarFloorToInt(pt.fY * s.fBitmap->height());
751             } else {
752                 yTemp = SkScalarFloorToInt(pt.fY);
753             }
754         } else {
755             yTemp = s.fFilterOneY + y;
756         }
757
758         const int stopY = s.fBitmap->height();
759         switch (s.fTileModeY) {
760             case SkShader::kClamp_TileMode:
761                 iY0 = SkClampMax(yTemp, stopY-1);
762                 break;
763             case SkShader::kRepeat_TileMode:
764                 iY0 = sk_int_mod(yTemp, stopY);
765                 break;
766             case SkShader::kMirror_TileMode:
767             default:
768                 iY0 = sk_int_mirror(yTemp, stopY);
769                 break;
770         }
771
772 #ifdef SK_DEBUG
773         {
774             SkPoint pt;
775             s.fInvProc(s.fInvMatrix,
776                        SkIntToScalar(x) + SK_ScalarHalf,
777                        SkIntToScalar(y) + SK_ScalarHalf,
778                        &pt);
779             if (s.fInvType > SkMatrix::kTranslate_Mask &&
780                 (SkShader::kClamp_TileMode != s.fTileModeX ||
781                  SkShader::kClamp_TileMode != s.fTileModeY)) {
782                 pt.fY *= s.fBitmap->height();
783             }
784             int iY2;
785
786             switch (s.fTileModeY) {
787             case SkShader::kClamp_TileMode:
788                 iY2 = SkClampMax(SkScalarFloorToInt(pt.fY), stopY-1);
789                 break;
790             case SkShader::kRepeat_TileMode:
791                 iY2 = sk_int_mod(SkScalarFloorToInt(pt.fY), stopY);
792                 break;
793             case SkShader::kMirror_TileMode:
794             default:
795                 iY2 = sk_int_mirror(SkScalarFloorToInt(pt.fY), stopY);
796                 break;
797             }
798
799             SkASSERT(iY0 == iY2);
800         }
801 #endif
802     }
803
804     const SkPMColor* row0 = s.fBitmap->getAddr32(0, iY0);
805     SkPMColor color;
806
807     if (SkPaint::kNone_FilterLevel != s.fFilterLevel) {
808         const SkPMColor* row1 = s.fBitmap->getAddr32(0, iY1);
809
810         if (s.fAlphaScale < 256) {
811             Filter_32_alpha(iSubY, *row0, *row1, &color, s.fAlphaScale);
812         } else {
813             Filter_32_opaque(iSubY, *row0, *row1, &color);
814         }
815     } else {
816         if (s.fAlphaScale < 256) {
817             color = SkAlphaMulQ(*row0, s.fAlphaScale);
818         } else {
819             color = *row0;
820         }
821     }
822
823     sk_memset32(colors, color, count);
824 }
825
826 static void DoNothing_shaderproc(const SkBitmapProcState&, int x, int y,
827                                  SkPMColor* SK_RESTRICT colors, int count) {
828     // if we get called, the matrix is too tricky, so we just draw nothing
829     sk_memset32(colors, 0, count);
830 }
831
832 bool SkBitmapProcState::setupForTranslate() {
833     SkPoint pt;
834     fInvProc(fInvMatrix, SK_ScalarHalf, SK_ScalarHalf, &pt);
835
836     /*
837      *  if the translate is larger than our ints, we can get random results, or
838      *  worse, we might get 0x80000000, which wreaks havoc on us, since we can't
839      *  negate it.
840      */
841     const SkScalar too_big = SkIntToScalar(1 << 30);
842     if (SkScalarAbs(pt.fX) > too_big || SkScalarAbs(pt.fY) > too_big) {
843         return false;
844     }
845
846     // Since we know we're not filtered, we re-purpose these fields allow
847     // us to go from device -> src coordinates w/ just an integer add,
848     // rather than running through the inverse-matrix
849     fFilterOneX = SkScalarFloorToInt(pt.fX);
850     fFilterOneY = SkScalarFloorToInt(pt.fY);
851     return true;
852 }
853
854 SkBitmapProcState::ShaderProc32 SkBitmapProcState::chooseShaderProc32() {
855
856     if (kN32_SkColorType != fBitmap->colorType()) {
857         return NULL;
858     }
859
860     static const unsigned kMask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
861
862     if (1 == fBitmap->width() && 0 == (fInvType & ~kMask)) {
863         if (SkPaint::kNone_FilterLevel == fFilterLevel &&
864             fInvType <= SkMatrix::kTranslate_Mask &&
865             !this->setupForTranslate()) {
866             return DoNothing_shaderproc;
867         }
868         return S32_D32_constX_shaderproc;
869     }
870
871     if (fAlphaScale < 256) {
872         return NULL;
873     }
874     if (fInvType > SkMatrix::kTranslate_Mask) {
875         return NULL;
876     }
877     if (SkPaint::kNone_FilterLevel != fFilterLevel) {
878         return NULL;
879     }
880
881     SkShader::TileMode tx = (SkShader::TileMode)fTileModeX;
882     SkShader::TileMode ty = (SkShader::TileMode)fTileModeY;
883
884     if (SkShader::kClamp_TileMode == tx && SkShader::kClamp_TileMode == ty) {
885         if (this->setupForTranslate()) {
886             return Clamp_S32_D32_nofilter_trans_shaderproc;
887         }
888         return DoNothing_shaderproc;
889     }
890     if (SkShader::kRepeat_TileMode == tx && SkShader::kRepeat_TileMode == ty) {
891         if (this->setupForTranslate()) {
892             return Repeat_S32_D32_nofilter_trans_shaderproc;
893         }
894         return DoNothing_shaderproc;
895     }
896     return NULL;
897 }
898
899 ///////////////////////////////////////////////////////////////////////////////
900
901 #ifdef SK_DEBUG
902
903 static void check_scale_nofilter(uint32_t bitmapXY[], int count,
904                                  unsigned mx, unsigned my) {
905     unsigned y = *bitmapXY++;
906     SkASSERT(y < my);
907
908     const uint16_t* xptr = reinterpret_cast<const uint16_t*>(bitmapXY);
909     for (int i = 0; i < count; ++i) {
910         SkASSERT(xptr[i] < mx);
911     }
912 }
913
914 static void check_scale_filter(uint32_t bitmapXY[], int count,
915                                  unsigned mx, unsigned my) {
916     uint32_t YY = *bitmapXY++;
917     unsigned y0 = YY >> 18;
918     unsigned y1 = YY & 0x3FFF;
919     SkASSERT(y0 < my);
920     SkASSERT(y1 < my);
921
922     for (int i = 0; i < count; ++i) {
923         uint32_t XX = bitmapXY[i];
924         unsigned x0 = XX >> 18;
925         unsigned x1 = XX & 0x3FFF;
926         SkASSERT(x0 < mx);
927         SkASSERT(x1 < mx);
928     }
929 }
930
931 static void check_affine_nofilter(uint32_t bitmapXY[], int count,
932                                  unsigned mx, unsigned my) {
933     for (int i = 0; i < count; ++i) {
934         uint32_t XY = bitmapXY[i];
935         unsigned x = XY & 0xFFFF;
936         unsigned y = XY >> 16;
937         SkASSERT(x < mx);
938         SkASSERT(y < my);
939     }
940 }
941
942 static void check_affine_filter(uint32_t bitmapXY[], int count,
943                                  unsigned mx, unsigned my) {
944     for (int i = 0; i < count; ++i) {
945         uint32_t YY = *bitmapXY++;
946         unsigned y0 = YY >> 18;
947         unsigned y1 = YY & 0x3FFF;
948         SkASSERT(y0 < my);
949         SkASSERT(y1 < my);
950
951         uint32_t XX = *bitmapXY++;
952         unsigned x0 = XX >> 18;
953         unsigned x1 = XX & 0x3FFF;
954         SkASSERT(x0 < mx);
955         SkASSERT(x1 < mx);
956     }
957 }
958
959 void SkBitmapProcState::DebugMatrixProc(const SkBitmapProcState& state,
960                                         uint32_t bitmapXY[], int count,
961                                         int x, int y) {
962     SkASSERT(bitmapXY);
963     SkASSERT(count > 0);
964
965     state.fMatrixProc(state, bitmapXY, count, x, y);
966
967     void (*proc)(uint32_t bitmapXY[], int count, unsigned mx, unsigned my);
968
969     // There are four formats possible:
970     //  scale -vs- affine
971     //  filter -vs- nofilter
972     if (state.fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) {
973         proc = state.fFilterLevel != SkPaint::kNone_FilterLevel ? check_scale_filter : check_scale_nofilter;
974     } else {
975         proc = state.fFilterLevel != SkPaint::kNone_FilterLevel ? check_affine_filter : check_affine_nofilter;
976     }
977     proc(bitmapXY, count, state.fBitmap->width(), state.fBitmap->height());
978 }
979
980 SkBitmapProcState::MatrixProc SkBitmapProcState::getMatrixProc() const {
981     return DebugMatrixProc;
982 }
983
984 #endif
985
986 ///////////////////////////////////////////////////////////////////////////////
987 /*
988     The storage requirements for the different matrix procs are as follows,
989     where each X or Y is 2 bytes, and N is the number of pixels/elements:
990
991     scale/translate     nofilter      Y(4bytes) + N * X
992     affine/perspective  nofilter      N * (X Y)
993     scale/translate     filter        Y Y + N * (X X)
994     affine/perspective  filter        N * (Y Y X X)
995  */
996 int SkBitmapProcState::maxCountForBufferSize(size_t bufferSize) const {
997     int32_t size = static_cast<int32_t>(bufferSize);
998
999     size &= ~3; // only care about 4-byte aligned chunks
1000     if (fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) {
1001         size -= 4;   // the shared Y (or YY) coordinate
1002         if (size < 0) {
1003             size = 0;
1004         }
1005         size >>= 1;
1006     } else {
1007         size >>= 2;
1008     }
1009
1010     if (fFilterLevel != SkPaint::kNone_FilterLevel) {
1011         size >>= 1;
1012     }
1013
1014     return size;
1015 }