michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkBitmapProcState.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkFilterProc.h" michael@0: #include "SkPaint.h" michael@0: #include "SkShader.h" // for tilemodes michael@0: #include "SkUtilsArm.h" michael@0: #include "SkBitmapScaler.h" michael@0: #include "SkMipMap.h" michael@0: #include "SkPixelRef.h" michael@0: #include "SkScaledImageCache.h" michael@0: michael@0: #if !SK_ARM_NEON_IS_NONE michael@0: // These are defined in src/opts/SkBitmapProcState_arm_neon.cpp michael@0: extern const SkBitmapProcState::SampleProc16 gSkBitmapProcStateSample16_neon[]; michael@0: extern const SkBitmapProcState::SampleProc32 gSkBitmapProcStateSample32_neon[]; michael@0: extern void S16_D16_filter_DX_neon(const SkBitmapProcState&, const uint32_t*, int, uint16_t*); michael@0: extern void Clamp_S16_D16_filter_DX_shaderproc_neon(const SkBitmapProcState&, int, int, uint16_t*, int); michael@0: extern void Repeat_S16_D16_filter_DX_shaderproc_neon(const SkBitmapProcState&, int, int, uint16_t*, int); michael@0: extern void SI8_opaque_D32_filter_DX_neon(const SkBitmapProcState&, const uint32_t*, int, SkPMColor*); michael@0: extern void SI8_opaque_D32_filter_DX_shaderproc_neon(const SkBitmapProcState&, int, int, uint32_t*, int); michael@0: extern void Clamp_SI8_opaque_D32_filter_DX_shaderproc_neon(const SkBitmapProcState&, int, int, uint32_t*, int); michael@0: #endif michael@0: michael@0: #define NAME_WRAP(x) x michael@0: #include "SkBitmapProcState_filter.h" michael@0: #include "SkBitmapProcState_procs.h" michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // true iff the matrix contains, at most, scale and translate elements michael@0: static bool matrix_only_scale_translate(const SkMatrix& m) { michael@0: return m.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask); michael@0: } michael@0: michael@0: /** michael@0: * For the purposes of drawing bitmaps, if a matrix is "almost" translate michael@0: * go ahead and treat it as if it were, so that subsequent code can go fast. michael@0: */ michael@0: static bool just_trans_clamp(const SkMatrix& matrix, const SkBitmap& bitmap) { michael@0: SkASSERT(matrix_only_scale_translate(matrix)); michael@0: michael@0: if (matrix.getType() & SkMatrix::kScale_Mask) { michael@0: SkRect src, dst; michael@0: bitmap.getBounds(&src); michael@0: michael@0: // Can't call mapRect(), since that will fix up inverted rectangles, michael@0: // e.g. when scale is negative, and we don't want to return true for michael@0: // those. michael@0: matrix.mapPoints(SkTCast(&dst), michael@0: SkTCast(&src), michael@0: 2); michael@0: michael@0: // Now round all 4 edges to device space, and then compare the device michael@0: // width/height to the original. Note: we must map all 4 and subtract michael@0: // rather than map the "width" and compare, since we care about the michael@0: // phase (in pixel space) that any translate in the matrix might impart. michael@0: SkIRect idst; michael@0: dst.round(&idst); michael@0: return idst.width() == bitmap.width() && idst.height() == bitmap.height(); michael@0: } michael@0: // if we got here, we're either kTranslate_Mask or identity michael@0: return true; michael@0: } michael@0: michael@0: static bool just_trans_general(const SkMatrix& matrix) { michael@0: SkASSERT(matrix_only_scale_translate(matrix)); michael@0: michael@0: if (matrix.getType() & SkMatrix::kScale_Mask) { michael@0: const SkScalar tol = SK_Scalar1 / 32768; michael@0: michael@0: if (!SkScalarNearlyZero(matrix[SkMatrix::kMScaleX] - SK_Scalar1, tol)) { michael@0: return false; michael@0: } michael@0: if (!SkScalarNearlyZero(matrix[SkMatrix::kMScaleY] - SK_Scalar1, tol)) { michael@0: return false; michael@0: } michael@0: } michael@0: // if we got here, treat us as either kTranslate_Mask or identity michael@0: return true; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static bool valid_for_filtering(unsigned dimension) { michael@0: // for filtering, width and height must fit in 14bits, since we use steal michael@0: // 2 bits from each to store our 4bit subpixel data michael@0: return (dimension & ~0x3FFF) == 0; michael@0: } michael@0: michael@0: static SkScalar effective_matrix_scale_sqrd(const SkMatrix& mat) { michael@0: SkPoint v1, v2; michael@0: michael@0: v1.fX = mat.getScaleX(); michael@0: v1.fY = mat.getSkewY(); michael@0: michael@0: v2.fX = mat.getSkewX(); michael@0: v2.fY = mat.getScaleY(); michael@0: michael@0: return SkMaxScalar(v1.lengthSqd(), v2.lengthSqd()); michael@0: } michael@0: michael@0: class AutoScaledCacheUnlocker { michael@0: public: michael@0: AutoScaledCacheUnlocker(SkScaledImageCache::ID** idPtr) : fIDPtr(idPtr) {} michael@0: ~AutoScaledCacheUnlocker() { michael@0: if (fIDPtr && *fIDPtr) { michael@0: SkScaledImageCache::Unlock(*fIDPtr); michael@0: *fIDPtr = NULL; michael@0: } michael@0: } michael@0: michael@0: // forgets the ID, so it won't call Unlock michael@0: void release() { michael@0: fIDPtr = NULL; michael@0: } michael@0: michael@0: private: michael@0: SkScaledImageCache::ID** fIDPtr; michael@0: }; michael@0: #define AutoScaledCacheUnlocker(...) SK_REQUIRE_LOCAL_VAR(AutoScaledCacheUnlocker) michael@0: michael@0: // TODO -- we may want to pass the clip into this function so we only scale michael@0: // the portion of the image that we're going to need. This will complicate michael@0: // the interface to the cache, but might be well worth it. michael@0: michael@0: bool SkBitmapProcState::possiblyScaleImage() { michael@0: AutoScaledCacheUnlocker unlocker(&fScaledCacheID); michael@0: michael@0: SkASSERT(NULL == fBitmap); michael@0: SkASSERT(NULL == fScaledCacheID); michael@0: michael@0: if (fFilterLevel <= SkPaint::kLow_FilterLevel) { michael@0: return false; michael@0: } michael@0: michael@0: // Check to see if the transformation matrix is simple, and if we're michael@0: // doing high quality scaling. If so, do the bitmap scale here and michael@0: // remove the scaling component from the matrix. michael@0: michael@0: if (SkPaint::kHigh_FilterLevel == fFilterLevel && michael@0: fInvMatrix.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask) && michael@0: fOrigBitmap.config() == SkBitmap::kARGB_8888_Config) { michael@0: michael@0: SkScalar invScaleX = fInvMatrix.getScaleX(); michael@0: SkScalar invScaleY = fInvMatrix.getScaleY(); michael@0: michael@0: fScaledCacheID = SkScaledImageCache::FindAndLock(fOrigBitmap, michael@0: invScaleX, invScaleY, michael@0: &fScaledBitmap); michael@0: if (fScaledCacheID) { michael@0: fScaledBitmap.lockPixels(); michael@0: if (!fScaledBitmap.getPixels()) { michael@0: fScaledBitmap.unlockPixels(); michael@0: // found a purged entry (discardablememory?), release it michael@0: SkScaledImageCache::Unlock(fScaledCacheID); michael@0: fScaledCacheID = NULL; michael@0: // fall through to rebuild michael@0: } michael@0: } michael@0: michael@0: if (NULL == fScaledCacheID) { michael@0: int dest_width = SkScalarCeilToInt(fOrigBitmap.width() / invScaleX); michael@0: int dest_height = SkScalarCeilToInt(fOrigBitmap.height() / invScaleY); michael@0: michael@0: // All the criteria are met; let's make a new bitmap. michael@0: michael@0: SkConvolutionProcs simd; michael@0: sk_bzero(&simd, sizeof(simd)); michael@0: this->platformConvolutionProcs(&simd); michael@0: michael@0: if (!SkBitmapScaler::Resize(&fScaledBitmap, michael@0: fOrigBitmap, michael@0: SkBitmapScaler::RESIZE_BEST, michael@0: dest_width, michael@0: dest_height, michael@0: simd, michael@0: SkScaledImageCache::GetAllocator())) { michael@0: // we failed to create fScaledBitmap, so just return and let michael@0: // the scanline proc handle it. michael@0: return false; michael@0: michael@0: } michael@0: SkASSERT(NULL != fScaledBitmap.getPixels()); michael@0: fScaledCacheID = SkScaledImageCache::AddAndLock(fOrigBitmap, michael@0: invScaleX, michael@0: invScaleY, michael@0: fScaledBitmap); michael@0: if (!fScaledCacheID) { michael@0: fScaledBitmap.reset(); michael@0: return false; michael@0: } michael@0: SkASSERT(NULL != fScaledBitmap.getPixels()); michael@0: } michael@0: michael@0: SkASSERT(NULL != fScaledBitmap.getPixels()); michael@0: fBitmap = &fScaledBitmap; michael@0: michael@0: // set the inv matrix type to translate-only; michael@0: fInvMatrix.setTranslate(fInvMatrix.getTranslateX() / fInvMatrix.getScaleX(), michael@0: fInvMatrix.getTranslateY() / fInvMatrix.getScaleY()); michael@0: michael@0: // no need for any further filtering; we just did it! michael@0: fFilterLevel = SkPaint::kNone_FilterLevel; michael@0: unlocker.release(); michael@0: return true; michael@0: } michael@0: michael@0: /* michael@0: * If High, then our special-case for scale-only did not take, and so we michael@0: * have to make a choice: michael@0: * 1. fall back on mipmaps + bilerp michael@0: * 2. fall back on scanline bicubic filter michael@0: * For now, we compute the "scale" value from the matrix, and have a michael@0: * threshold to decide when bicubic is better, and when mips are better. michael@0: * No doubt a fancier decision tree could be used uere. michael@0: * michael@0: * If Medium, then we just try to build a mipmap and select a level, michael@0: * setting the filter-level to kLow to signal that we just need bilerp michael@0: * to process the selected level. michael@0: */ michael@0: michael@0: SkScalar scaleSqd = effective_matrix_scale_sqrd(fInvMatrix); michael@0: michael@0: if (SkPaint::kHigh_FilterLevel == fFilterLevel) { michael@0: // Set the limit at 0.25 for the CTM... if the CTM is scaling smaller michael@0: // than this, then the mipmaps quality may be greater (certainly faster) michael@0: // so we only keep High quality if the scale is greater than this. michael@0: // michael@0: // Since we're dealing with the inverse, we compare against its inverse. michael@0: const SkScalar bicubicLimit = 4.0f; michael@0: const SkScalar bicubicLimitSqd = bicubicLimit * bicubicLimit; michael@0: if (scaleSqd < bicubicLimitSqd) { // use bicubic scanline michael@0: return false; michael@0: } michael@0: michael@0: // else set the filter-level to Medium, since we're scaling down and michael@0: // want to reqeust mipmaps michael@0: fFilterLevel = SkPaint::kMedium_FilterLevel; michael@0: } michael@0: michael@0: SkASSERT(SkPaint::kMedium_FilterLevel == fFilterLevel); michael@0: michael@0: /** michael@0: * Medium quality means use a mipmap for down-scaling, and just bilper michael@0: * for upscaling. Since we're examining the inverse matrix, we look for michael@0: * a scale > 1 to indicate down scaling by the CTM. michael@0: */ michael@0: if (scaleSqd > SK_Scalar1) { michael@0: const SkMipMap* mip = NULL; michael@0: michael@0: SkASSERT(NULL == fScaledCacheID); michael@0: fScaledCacheID = SkScaledImageCache::FindAndLockMip(fOrigBitmap, &mip); michael@0: if (!fScaledCacheID) { michael@0: SkASSERT(NULL == mip); michael@0: mip = SkMipMap::Build(fOrigBitmap); michael@0: if (mip) { michael@0: fScaledCacheID = SkScaledImageCache::AddAndLockMip(fOrigBitmap, michael@0: mip); michael@0: mip->unref(); // the cache took a ref michael@0: SkASSERT(fScaledCacheID); michael@0: } michael@0: } else { michael@0: SkASSERT(mip); michael@0: } michael@0: michael@0: if (mip) { michael@0: SkScalar levelScale = SkScalarInvert(SkScalarSqrt(scaleSqd)); michael@0: SkMipMap::Level level; michael@0: if (mip->extractLevel(levelScale, &level)) { michael@0: SkScalar invScaleFixup = level.fScale; michael@0: fInvMatrix.postScale(invScaleFixup, invScaleFixup); michael@0: michael@0: fScaledBitmap.setConfig(fOrigBitmap.config(), michael@0: level.fWidth, level.fHeight, michael@0: level.fRowBytes); michael@0: fScaledBitmap.setPixels(level.fPixels); michael@0: fBitmap = &fScaledBitmap; michael@0: fFilterLevel = SkPaint::kLow_FilterLevel; michael@0: unlocker.release(); michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: static bool get_locked_pixels(const SkBitmap& src, int pow2, SkBitmap* dst) { michael@0: SkPixelRef* pr = src.pixelRef(); michael@0: if (pr && pr->decodeInto(pow2, dst)) { michael@0: return true; michael@0: } michael@0: michael@0: /* michael@0: * If decodeInto() fails, it is possibe that we have an old subclass that michael@0: * does not, or cannot, implement that. In that case we fall back to the michael@0: * older protocol of having the pixelRef handle the caching for us. michael@0: */ michael@0: *dst = src; michael@0: dst->lockPixels(); michael@0: return SkToBool(dst->getPixels()); michael@0: } michael@0: michael@0: bool SkBitmapProcState::lockBaseBitmap() { michael@0: AutoScaledCacheUnlocker unlocker(&fScaledCacheID); michael@0: michael@0: SkPixelRef* pr = fOrigBitmap.pixelRef(); michael@0: michael@0: SkASSERT(NULL == fScaledCacheID); michael@0: michael@0: if (pr->isLocked() || !pr->implementsDecodeInto()) { michael@0: // fast-case, no need to look in our cache michael@0: fScaledBitmap = fOrigBitmap; michael@0: fScaledBitmap.lockPixels(); michael@0: if (NULL == fScaledBitmap.getPixels()) { michael@0: return false; michael@0: } michael@0: } else { michael@0: fScaledCacheID = SkScaledImageCache::FindAndLock(fOrigBitmap, michael@0: SK_Scalar1, SK_Scalar1, michael@0: &fScaledBitmap); michael@0: if (fScaledCacheID) { michael@0: fScaledBitmap.lockPixels(); michael@0: if (!fScaledBitmap.getPixels()) { michael@0: fScaledBitmap.unlockPixels(); michael@0: // found a purged entry (discardablememory?), release it michael@0: SkScaledImageCache::Unlock(fScaledCacheID); michael@0: fScaledCacheID = NULL; michael@0: // fall through to rebuild michael@0: } michael@0: } michael@0: michael@0: if (NULL == fScaledCacheID) { michael@0: if (!get_locked_pixels(fOrigBitmap, 0, &fScaledBitmap)) { michael@0: return false; michael@0: } michael@0: michael@0: // TODO: if fScaled comes back at a different width/height than fOrig, michael@0: // we need to update the matrix we are using to sample from this guy. michael@0: michael@0: fScaledCacheID = SkScaledImageCache::AddAndLock(fOrigBitmap, michael@0: SK_Scalar1, SK_Scalar1, michael@0: fScaledBitmap); michael@0: if (!fScaledCacheID) { michael@0: fScaledBitmap.reset(); michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: fBitmap = &fScaledBitmap; michael@0: unlocker.release(); michael@0: return true; michael@0: } michael@0: michael@0: void SkBitmapProcState::endContext() { michael@0: SkDELETE(fBitmapFilter); michael@0: fBitmapFilter = NULL; michael@0: fScaledBitmap.reset(); michael@0: michael@0: if (fScaledCacheID) { michael@0: SkScaledImageCache::Unlock(fScaledCacheID); michael@0: fScaledCacheID = NULL; michael@0: } michael@0: } michael@0: michael@0: SkBitmapProcState::~SkBitmapProcState() { michael@0: if (fScaledCacheID) { michael@0: SkScaledImageCache::Unlock(fScaledCacheID); michael@0: } michael@0: SkDELETE(fBitmapFilter); michael@0: } michael@0: michael@0: bool SkBitmapProcState::chooseProcs(const SkMatrix& inv, const SkPaint& paint) { michael@0: SkASSERT(fOrigBitmap.width() && fOrigBitmap.height()); michael@0: michael@0: fBitmap = NULL; michael@0: fInvMatrix = inv; michael@0: fFilterLevel = paint.getFilterLevel(); michael@0: michael@0: SkASSERT(NULL == fScaledCacheID); michael@0: michael@0: // possiblyScaleImage will look to see if it can rescale the image as a michael@0: // preprocess; either by scaling up to the target size, or by selecting michael@0: // a nearby mipmap level. If it does, it will adjust the working michael@0: // matrix as well as the working bitmap. It may also adjust the filter michael@0: // quality to avoid re-filtering an already perfectly scaled image. michael@0: if (!this->possiblyScaleImage()) { michael@0: if (!this->lockBaseBitmap()) { michael@0: return false; michael@0: } michael@0: } michael@0: // The above logic should have always assigned fBitmap, but in case it michael@0: // didn't, we check for that now... michael@0: if (NULL == fBitmap) { michael@0: return false; michael@0: } michael@0: michael@0: bool trivialMatrix = (fInvMatrix.getType() & ~SkMatrix::kTranslate_Mask) == 0; michael@0: bool clampClamp = SkShader::kClamp_TileMode == fTileModeX && michael@0: SkShader::kClamp_TileMode == fTileModeY; michael@0: michael@0: if (!(clampClamp || trivialMatrix)) { michael@0: fInvMatrix.postIDiv(fOrigBitmap.width(), fOrigBitmap.height()); michael@0: } michael@0: michael@0: // Now that all possible changes to the matrix have taken place, check michael@0: // to see if we're really close to a no-scale matrix. If so, explicitly michael@0: // set it to be so. Subsequent code may inspect this matrix to choose michael@0: // a faster path in this case. michael@0: michael@0: // This code will only execute if the matrix has some scale component; michael@0: // if it's already pure translate then we won't do this inversion. michael@0: michael@0: if (matrix_only_scale_translate(fInvMatrix)) { michael@0: SkMatrix forward; michael@0: if (fInvMatrix.invert(&forward)) { michael@0: if (clampClamp ? just_trans_clamp(forward, *fBitmap) michael@0: : just_trans_general(forward)) { michael@0: SkScalar tx = -SkScalarRoundToScalar(forward.getTranslateX()); michael@0: SkScalar ty = -SkScalarRoundToScalar(forward.getTranslateY()); michael@0: fInvMatrix.setTranslate(tx, ty); michael@0: } michael@0: } michael@0: } michael@0: michael@0: fInvProc = fInvMatrix.getMapXYProc(); michael@0: fInvType = fInvMatrix.getType(); michael@0: fInvSx = SkScalarToFixed(fInvMatrix.getScaleX()); michael@0: fInvSxFractionalInt = SkScalarToFractionalInt(fInvMatrix.getScaleX()); michael@0: fInvKy = SkScalarToFixed(fInvMatrix.getSkewY()); michael@0: fInvKyFractionalInt = SkScalarToFractionalInt(fInvMatrix.getSkewY()); michael@0: michael@0: fAlphaScale = SkAlpha255To256(paint.getAlpha()); michael@0: michael@0: fShaderProc32 = NULL; michael@0: fShaderProc16 = NULL; michael@0: fSampleProc32 = NULL; michael@0: fSampleProc16 = NULL; michael@0: michael@0: // recompute the triviality of the matrix here because we may have michael@0: // changed it! michael@0: michael@0: trivialMatrix = (fInvMatrix.getType() & ~SkMatrix::kTranslate_Mask) == 0; michael@0: michael@0: if (SkPaint::kHigh_FilterLevel == fFilterLevel) { michael@0: // If this is still set, that means we wanted HQ sampling michael@0: // but couldn't do it as a preprocess. Let's try to install michael@0: // the scanline version of the HQ sampler. If that process fails, michael@0: // downgrade to bilerp. michael@0: michael@0: // NOTE: Might need to be careful here in the future when we want michael@0: // to have the platform proc have a shot at this; it's possible that michael@0: // the chooseBitmapFilterProc will fail to install a shader but a michael@0: // platform-specific one might succeed, so it might be premature here michael@0: // to fall back to bilerp. This needs thought. michael@0: michael@0: if (!this->setBitmapFilterProcs()) { michael@0: fFilterLevel = SkPaint::kLow_FilterLevel; michael@0: } michael@0: } michael@0: michael@0: if (SkPaint::kLow_FilterLevel == fFilterLevel) { michael@0: // Only try bilerp if the matrix is "interesting" and michael@0: // the image has a suitable size. michael@0: michael@0: if (fInvType <= SkMatrix::kTranslate_Mask || michael@0: !valid_for_filtering(fBitmap->width() | fBitmap->height())) { michael@0: fFilterLevel = SkPaint::kNone_FilterLevel; michael@0: } michael@0: } michael@0: michael@0: // At this point, we know exactly what kind of sampling the per-scanline michael@0: // shader will perform. michael@0: michael@0: fMatrixProc = this->chooseMatrixProc(trivialMatrix); michael@0: if (NULL == fMatrixProc) { michael@0: return false; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////// michael@0: michael@0: // No need to do this if we're doing HQ sampling; if filter quality is michael@0: // still set to HQ by the time we get here, then we must have installed michael@0: // the shader procs above and can skip all this. michael@0: michael@0: if (fFilterLevel < SkPaint::kHigh_FilterLevel) { michael@0: michael@0: int index = 0; michael@0: if (fAlphaScale < 256) { // note: this distinction is not used for D16 michael@0: index |= 1; michael@0: } michael@0: if (fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) { michael@0: index |= 2; michael@0: } michael@0: if (fFilterLevel > SkPaint::kNone_FilterLevel) { michael@0: index |= 4; michael@0: } michael@0: // bits 3,4,5 encoding the source bitmap format michael@0: switch (fBitmap->config()) { michael@0: case SkBitmap::kARGB_8888_Config: michael@0: index |= 0; michael@0: break; michael@0: case SkBitmap::kRGB_565_Config: michael@0: index |= 8; michael@0: break; michael@0: case SkBitmap::kIndex8_Config: michael@0: index |= 16; michael@0: break; michael@0: case SkBitmap::kARGB_4444_Config: michael@0: index |= 24; michael@0: break; michael@0: case SkBitmap::kA8_Config: michael@0: index |= 32; michael@0: fPaintPMColor = SkPreMultiplyColor(paint.getColor()); michael@0: break; michael@0: default: michael@0: return false; michael@0: } michael@0: michael@0: #if !SK_ARM_NEON_IS_ALWAYS michael@0: static const SampleProc32 gSkBitmapProcStateSample32[] = { michael@0: S32_opaque_D32_nofilter_DXDY, michael@0: S32_alpha_D32_nofilter_DXDY, michael@0: S32_opaque_D32_nofilter_DX, michael@0: S32_alpha_D32_nofilter_DX, michael@0: S32_opaque_D32_filter_DXDY, michael@0: S32_alpha_D32_filter_DXDY, michael@0: S32_opaque_D32_filter_DX, michael@0: S32_alpha_D32_filter_DX, michael@0: michael@0: S16_opaque_D32_nofilter_DXDY, michael@0: S16_alpha_D32_nofilter_DXDY, michael@0: S16_opaque_D32_nofilter_DX, michael@0: S16_alpha_D32_nofilter_DX, michael@0: S16_opaque_D32_filter_DXDY, michael@0: S16_alpha_D32_filter_DXDY, michael@0: S16_opaque_D32_filter_DX, michael@0: S16_alpha_D32_filter_DX, michael@0: michael@0: SI8_opaque_D32_nofilter_DXDY, michael@0: SI8_alpha_D32_nofilter_DXDY, michael@0: SI8_opaque_D32_nofilter_DX, michael@0: SI8_alpha_D32_nofilter_DX, michael@0: SI8_opaque_D32_filter_DXDY, michael@0: SI8_alpha_D32_filter_DXDY, michael@0: SI8_opaque_D32_filter_DX, michael@0: SI8_alpha_D32_filter_DX, michael@0: michael@0: S4444_opaque_D32_nofilter_DXDY, michael@0: S4444_alpha_D32_nofilter_DXDY, michael@0: S4444_opaque_D32_nofilter_DX, michael@0: S4444_alpha_D32_nofilter_DX, michael@0: S4444_opaque_D32_filter_DXDY, michael@0: S4444_alpha_D32_filter_DXDY, michael@0: S4444_opaque_D32_filter_DX, michael@0: S4444_alpha_D32_filter_DX, michael@0: michael@0: // A8 treats alpha/opaque the same (equally efficient) michael@0: SA8_alpha_D32_nofilter_DXDY, michael@0: SA8_alpha_D32_nofilter_DXDY, michael@0: SA8_alpha_D32_nofilter_DX, michael@0: SA8_alpha_D32_nofilter_DX, michael@0: SA8_alpha_D32_filter_DXDY, michael@0: SA8_alpha_D32_filter_DXDY, michael@0: SA8_alpha_D32_filter_DX, michael@0: SA8_alpha_D32_filter_DX michael@0: }; michael@0: michael@0: static const SampleProc16 gSkBitmapProcStateSample16[] = { michael@0: S32_D16_nofilter_DXDY, michael@0: S32_D16_nofilter_DX, michael@0: S32_D16_filter_DXDY, michael@0: S32_D16_filter_DX, michael@0: michael@0: S16_D16_nofilter_DXDY, michael@0: S16_D16_nofilter_DX, michael@0: S16_D16_filter_DXDY, michael@0: S16_D16_filter_DX, michael@0: michael@0: SI8_D16_nofilter_DXDY, michael@0: SI8_D16_nofilter_DX, michael@0: SI8_D16_filter_DXDY, michael@0: SI8_D16_filter_DX, michael@0: michael@0: // Don't support 4444 -> 565 michael@0: NULL, NULL, NULL, NULL, michael@0: // Don't support A8 -> 565 michael@0: NULL, NULL, NULL, NULL michael@0: }; michael@0: #endif michael@0: michael@0: fSampleProc32 = SK_ARM_NEON_WRAP(gSkBitmapProcStateSample32)[index]; michael@0: index >>= 1; // shift away any opaque/alpha distinction michael@0: fSampleProc16 = SK_ARM_NEON_WRAP(gSkBitmapProcStateSample16)[index]; michael@0: michael@0: // our special-case shaderprocs michael@0: if (SK_ARM_NEON_WRAP(S16_D16_filter_DX) == fSampleProc16) { michael@0: if (clampClamp) { michael@0: fShaderProc16 = SK_ARM_NEON_WRAP(Clamp_S16_D16_filter_DX_shaderproc); michael@0: } else if (SkShader::kRepeat_TileMode == fTileModeX && michael@0: SkShader::kRepeat_TileMode == fTileModeY) { michael@0: fShaderProc16 = SK_ARM_NEON_WRAP(Repeat_S16_D16_filter_DX_shaderproc); michael@0: } michael@0: } else if (SK_ARM_NEON_WRAP(SI8_opaque_D32_filter_DX) == fSampleProc32 && clampClamp) { michael@0: fShaderProc32 = SK_ARM_NEON_WRAP(Clamp_SI8_opaque_D32_filter_DX_shaderproc); michael@0: } michael@0: michael@0: if (NULL == fShaderProc32) { michael@0: fShaderProc32 = this->chooseShaderProc32(); michael@0: } michael@0: } michael@0: michael@0: // see if our platform has any accelerated overrides michael@0: this->platformProcs(); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static void Clamp_S32_D32_nofilter_trans_shaderproc(const SkBitmapProcState& s, michael@0: int x, int y, michael@0: SkPMColor* SK_RESTRICT colors, michael@0: int count) { michael@0: SkASSERT(((s.fInvType & ~SkMatrix::kTranslate_Mask)) == 0); michael@0: SkASSERT(s.fInvKy == 0); michael@0: SkASSERT(count > 0 && colors != NULL); michael@0: SkASSERT(SkPaint::kNone_FilterLevel == s.fFilterLevel); michael@0: michael@0: const int maxX = s.fBitmap->width() - 1; michael@0: const int maxY = s.fBitmap->height() - 1; michael@0: int ix = s.fFilterOneX + x; michael@0: int iy = SkClampMax(s.fFilterOneY + y, maxY); michael@0: #ifdef SK_DEBUG michael@0: { michael@0: SkPoint pt; michael@0: s.fInvProc(s.fInvMatrix, SkIntToScalar(x) + SK_ScalarHalf, michael@0: SkIntToScalar(y) + SK_ScalarHalf, &pt); michael@0: int iy2 = SkClampMax(SkScalarFloorToInt(pt.fY), maxY); michael@0: int ix2 = SkScalarFloorToInt(pt.fX); michael@0: michael@0: SkASSERT(iy == iy2); michael@0: SkASSERT(ix == ix2); michael@0: } michael@0: #endif michael@0: const SkPMColor* row = s.fBitmap->getAddr32(0, iy); michael@0: michael@0: // clamp to the left michael@0: if (ix < 0) { michael@0: int n = SkMin32(-ix, count); michael@0: sk_memset32(colors, row[0], n); michael@0: count -= n; michael@0: if (0 == count) { michael@0: return; michael@0: } michael@0: colors += n; michael@0: SkASSERT(-ix == n); michael@0: ix = 0; michael@0: } michael@0: // copy the middle michael@0: if (ix <= maxX) { michael@0: int n = SkMin32(maxX - ix + 1, count); michael@0: memcpy(colors, row + ix, n * sizeof(SkPMColor)); michael@0: count -= n; michael@0: if (0 == count) { michael@0: return; michael@0: } michael@0: colors += n; michael@0: } michael@0: SkASSERT(count > 0); michael@0: // clamp to the right michael@0: sk_memset32(colors, row[maxX], count); michael@0: } michael@0: michael@0: static inline int sk_int_mod(int x, int n) { michael@0: SkASSERT(n > 0); michael@0: if ((unsigned)x >= (unsigned)n) { michael@0: if (x < 0) { michael@0: x = n + ~(~x % n); michael@0: } else { michael@0: x = x % n; michael@0: } michael@0: } michael@0: return x; michael@0: } michael@0: michael@0: static inline int sk_int_mirror(int x, int n) { michael@0: x = sk_int_mod(x, 2 * n); michael@0: if (x >= n) { michael@0: x = n + ~(x - n); michael@0: } michael@0: return x; michael@0: } michael@0: michael@0: static void Repeat_S32_D32_nofilter_trans_shaderproc(const SkBitmapProcState& s, michael@0: int x, int y, michael@0: SkPMColor* SK_RESTRICT colors, michael@0: int count) { michael@0: SkASSERT(((s.fInvType & ~SkMatrix::kTranslate_Mask)) == 0); michael@0: SkASSERT(s.fInvKy == 0); michael@0: SkASSERT(count > 0 && colors != NULL); michael@0: SkASSERT(SkPaint::kNone_FilterLevel == s.fFilterLevel); michael@0: michael@0: const int stopX = s.fBitmap->width(); michael@0: const int stopY = s.fBitmap->height(); michael@0: int ix = s.fFilterOneX + x; michael@0: int iy = sk_int_mod(s.fFilterOneY + y, stopY); michael@0: #ifdef SK_DEBUG michael@0: { michael@0: SkPoint pt; michael@0: s.fInvProc(s.fInvMatrix, SkIntToScalar(x) + SK_ScalarHalf, michael@0: SkIntToScalar(y) + SK_ScalarHalf, &pt); michael@0: int iy2 = sk_int_mod(SkScalarFloorToInt(pt.fY), stopY); michael@0: int ix2 = SkScalarFloorToInt(pt.fX); michael@0: michael@0: SkASSERT(iy == iy2); michael@0: SkASSERT(ix == ix2); michael@0: } michael@0: #endif michael@0: const SkPMColor* row = s.fBitmap->getAddr32(0, iy); michael@0: michael@0: ix = sk_int_mod(ix, stopX); michael@0: for (;;) { michael@0: int n = SkMin32(stopX - ix, count); michael@0: memcpy(colors, row + ix, n * sizeof(SkPMColor)); michael@0: count -= n; michael@0: if (0 == count) { michael@0: return; michael@0: } michael@0: colors += n; michael@0: ix = 0; michael@0: } michael@0: } michael@0: michael@0: static void S32_D32_constX_shaderproc(const SkBitmapProcState& s, michael@0: int x, int y, michael@0: SkPMColor* SK_RESTRICT colors, michael@0: int count) { michael@0: SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) == 0); michael@0: SkASSERT(s.fInvKy == 0); michael@0: SkASSERT(count > 0 && colors != NULL); michael@0: SkASSERT(1 == s.fBitmap->width()); michael@0: michael@0: int iY0; michael@0: int iY1 SK_INIT_TO_AVOID_WARNING; michael@0: int iSubY SK_INIT_TO_AVOID_WARNING; michael@0: michael@0: if (SkPaint::kNone_FilterLevel != s.fFilterLevel) { michael@0: SkBitmapProcState::MatrixProc mproc = s.getMatrixProc(); michael@0: uint32_t xy[2]; michael@0: michael@0: mproc(s, xy, 1, x, y); michael@0: michael@0: iY0 = xy[0] >> 18; michael@0: iY1 = xy[0] & 0x3FFF; michael@0: iSubY = (xy[0] >> 14) & 0xF; michael@0: } else { michael@0: int yTemp; michael@0: michael@0: if (s.fInvType > SkMatrix::kTranslate_Mask) { michael@0: SkPoint pt; michael@0: s.fInvProc(s.fInvMatrix, michael@0: SkIntToScalar(x) + SK_ScalarHalf, michael@0: SkIntToScalar(y) + SK_ScalarHalf, michael@0: &pt); michael@0: // When the matrix has a scale component the setup code in michael@0: // chooseProcs multiples the inverse matrix by the inverse of the michael@0: // bitmap's width and height. Since this method is going to do michael@0: // its own tiling and sampling we need to undo that here. michael@0: if (SkShader::kClamp_TileMode != s.fTileModeX || michael@0: SkShader::kClamp_TileMode != s.fTileModeY) { michael@0: yTemp = SkScalarFloorToInt(pt.fY * s.fBitmap->height()); michael@0: } else { michael@0: yTemp = SkScalarFloorToInt(pt.fY); michael@0: } michael@0: } else { michael@0: yTemp = s.fFilterOneY + y; michael@0: } michael@0: michael@0: const int stopY = s.fBitmap->height(); michael@0: switch (s.fTileModeY) { michael@0: case SkShader::kClamp_TileMode: michael@0: iY0 = SkClampMax(yTemp, stopY-1); michael@0: break; michael@0: case SkShader::kRepeat_TileMode: michael@0: iY0 = sk_int_mod(yTemp, stopY); michael@0: break; michael@0: case SkShader::kMirror_TileMode: michael@0: default: michael@0: iY0 = sk_int_mirror(yTemp, stopY); michael@0: break; michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: { michael@0: SkPoint pt; michael@0: s.fInvProc(s.fInvMatrix, michael@0: SkIntToScalar(x) + SK_ScalarHalf, michael@0: SkIntToScalar(y) + SK_ScalarHalf, michael@0: &pt); michael@0: if (s.fInvType > SkMatrix::kTranslate_Mask && michael@0: (SkShader::kClamp_TileMode != s.fTileModeX || michael@0: SkShader::kClamp_TileMode != s.fTileModeY)) { michael@0: pt.fY *= s.fBitmap->height(); michael@0: } michael@0: int iY2; michael@0: michael@0: switch (s.fTileModeY) { michael@0: case SkShader::kClamp_TileMode: michael@0: iY2 = SkClampMax(SkScalarFloorToInt(pt.fY), stopY-1); michael@0: break; michael@0: case SkShader::kRepeat_TileMode: michael@0: iY2 = sk_int_mod(SkScalarFloorToInt(pt.fY), stopY); michael@0: break; michael@0: case SkShader::kMirror_TileMode: michael@0: default: michael@0: iY2 = sk_int_mirror(SkScalarFloorToInt(pt.fY), stopY); michael@0: break; michael@0: } michael@0: michael@0: SkASSERT(iY0 == iY2); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: const SkPMColor* row0 = s.fBitmap->getAddr32(0, iY0); michael@0: SkPMColor color; michael@0: michael@0: if (SkPaint::kNone_FilterLevel != s.fFilterLevel) { michael@0: const SkPMColor* row1 = s.fBitmap->getAddr32(0, iY1); michael@0: michael@0: if (s.fAlphaScale < 256) { michael@0: Filter_32_alpha(iSubY, *row0, *row1, &color, s.fAlphaScale); michael@0: } else { michael@0: Filter_32_opaque(iSubY, *row0, *row1, &color); michael@0: } michael@0: } else { michael@0: if (s.fAlphaScale < 256) { michael@0: color = SkAlphaMulQ(*row0, s.fAlphaScale); michael@0: } else { michael@0: color = *row0; michael@0: } michael@0: } michael@0: michael@0: sk_memset32(colors, color, count); michael@0: } michael@0: michael@0: static void DoNothing_shaderproc(const SkBitmapProcState&, int x, int y, michael@0: SkPMColor* SK_RESTRICT colors, int count) { michael@0: // if we get called, the matrix is too tricky, so we just draw nothing michael@0: sk_memset32(colors, 0, count); michael@0: } michael@0: michael@0: bool SkBitmapProcState::setupForTranslate() { michael@0: SkPoint pt; michael@0: fInvProc(fInvMatrix, SK_ScalarHalf, SK_ScalarHalf, &pt); michael@0: michael@0: /* michael@0: * if the translate is larger than our ints, we can get random results, or michael@0: * worse, we might get 0x80000000, which wreaks havoc on us, since we can't michael@0: * negate it. michael@0: */ michael@0: const SkScalar too_big = SkIntToScalar(1 << 30); michael@0: if (SkScalarAbs(pt.fX) > too_big || SkScalarAbs(pt.fY) > too_big) { michael@0: return false; michael@0: } michael@0: michael@0: // Since we know we're not filtered, we re-purpose these fields allow michael@0: // us to go from device -> src coordinates w/ just an integer add, michael@0: // rather than running through the inverse-matrix michael@0: fFilterOneX = SkScalarFloorToInt(pt.fX); michael@0: fFilterOneY = SkScalarFloorToInt(pt.fY); michael@0: return true; michael@0: } michael@0: michael@0: SkBitmapProcState::ShaderProc32 SkBitmapProcState::chooseShaderProc32() { michael@0: michael@0: if (SkBitmap::kARGB_8888_Config != fBitmap->config()) { michael@0: return NULL; michael@0: } michael@0: michael@0: static const unsigned kMask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask; michael@0: michael@0: if (1 == fBitmap->width() && 0 == (fInvType & ~kMask)) { michael@0: if (SkPaint::kNone_FilterLevel == fFilterLevel && michael@0: fInvType <= SkMatrix::kTranslate_Mask && michael@0: !this->setupForTranslate()) { michael@0: return DoNothing_shaderproc; michael@0: } michael@0: return S32_D32_constX_shaderproc; michael@0: } michael@0: michael@0: if (fAlphaScale < 256) { michael@0: return NULL; michael@0: } michael@0: if (fInvType > SkMatrix::kTranslate_Mask) { michael@0: return NULL; michael@0: } michael@0: if (SkPaint::kNone_FilterLevel != fFilterLevel) { michael@0: return NULL; michael@0: } michael@0: michael@0: SkShader::TileMode tx = (SkShader::TileMode)fTileModeX; michael@0: SkShader::TileMode ty = (SkShader::TileMode)fTileModeY; michael@0: michael@0: if (SkShader::kClamp_TileMode == tx && SkShader::kClamp_TileMode == ty) { michael@0: if (this->setupForTranslate()) { michael@0: return Clamp_S32_D32_nofilter_trans_shaderproc; michael@0: } michael@0: return DoNothing_shaderproc; michael@0: } michael@0: if (SkShader::kRepeat_TileMode == tx && SkShader::kRepeat_TileMode == ty) { michael@0: if (this->setupForTranslate()) { michael@0: return Repeat_S32_D32_nofilter_trans_shaderproc; michael@0: } michael@0: return DoNothing_shaderproc; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifdef SK_DEBUG michael@0: michael@0: static void check_scale_nofilter(uint32_t bitmapXY[], int count, michael@0: unsigned mx, unsigned my) { michael@0: unsigned y = *bitmapXY++; michael@0: SkASSERT(y < my); michael@0: michael@0: const uint16_t* xptr = reinterpret_cast(bitmapXY); michael@0: for (int i = 0; i < count; ++i) { michael@0: SkASSERT(xptr[i] < mx); michael@0: } michael@0: } michael@0: michael@0: static void check_scale_filter(uint32_t bitmapXY[], int count, michael@0: unsigned mx, unsigned my) { michael@0: uint32_t YY = *bitmapXY++; michael@0: unsigned y0 = YY >> 18; michael@0: unsigned y1 = YY & 0x3FFF; michael@0: SkASSERT(y0 < my); michael@0: SkASSERT(y1 < my); michael@0: michael@0: for (int i = 0; i < count; ++i) { michael@0: uint32_t XX = bitmapXY[i]; michael@0: unsigned x0 = XX >> 18; michael@0: unsigned x1 = XX & 0x3FFF; michael@0: SkASSERT(x0 < mx); michael@0: SkASSERT(x1 < mx); michael@0: } michael@0: } michael@0: michael@0: static void check_affine_nofilter(uint32_t bitmapXY[], int count, michael@0: unsigned mx, unsigned my) { michael@0: for (int i = 0; i < count; ++i) { michael@0: uint32_t XY = bitmapXY[i]; michael@0: unsigned x = XY & 0xFFFF; michael@0: unsigned y = XY >> 16; michael@0: SkASSERT(x < mx); michael@0: SkASSERT(y < my); michael@0: } michael@0: } michael@0: michael@0: static void check_affine_filter(uint32_t bitmapXY[], int count, michael@0: unsigned mx, unsigned my) { michael@0: for (int i = 0; i < count; ++i) { michael@0: uint32_t YY = *bitmapXY++; michael@0: unsigned y0 = YY >> 18; michael@0: unsigned y1 = YY & 0x3FFF; michael@0: SkASSERT(y0 < my); michael@0: SkASSERT(y1 < my); michael@0: michael@0: uint32_t XX = *bitmapXY++; michael@0: unsigned x0 = XX >> 18; michael@0: unsigned x1 = XX & 0x3FFF; michael@0: SkASSERT(x0 < mx); michael@0: SkASSERT(x1 < mx); michael@0: } michael@0: } michael@0: michael@0: void SkBitmapProcState::DebugMatrixProc(const SkBitmapProcState& state, michael@0: uint32_t bitmapXY[], int count, michael@0: int x, int y) { michael@0: SkASSERT(bitmapXY); michael@0: SkASSERT(count > 0); michael@0: michael@0: state.fMatrixProc(state, bitmapXY, count, x, y); michael@0: michael@0: void (*proc)(uint32_t bitmapXY[], int count, unsigned mx, unsigned my); michael@0: michael@0: // There are four formats possible: michael@0: // scale -vs- affine michael@0: // filter -vs- nofilter michael@0: if (state.fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) { michael@0: proc = state.fFilterLevel != SkPaint::kNone_FilterLevel ? check_scale_filter : check_scale_nofilter; michael@0: } else { michael@0: proc = state.fFilterLevel != SkPaint::kNone_FilterLevel ? check_affine_filter : check_affine_nofilter; michael@0: } michael@0: proc(bitmapXY, count, state.fBitmap->width(), state.fBitmap->height()); michael@0: } michael@0: michael@0: SkBitmapProcState::MatrixProc SkBitmapProcState::getMatrixProc() const { michael@0: return DebugMatrixProc; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: /* michael@0: The storage requirements for the different matrix procs are as follows, michael@0: where each X or Y is 2 bytes, and N is the number of pixels/elements: michael@0: michael@0: scale/translate nofilter Y(4bytes) + N * X michael@0: affine/perspective nofilter N * (X Y) michael@0: scale/translate filter Y Y + N * (X X) michael@0: affine/perspective filter N * (Y Y X X) michael@0: */ michael@0: int SkBitmapProcState::maxCountForBufferSize(size_t bufferSize) const { michael@0: int32_t size = static_cast(bufferSize); michael@0: michael@0: size &= ~3; // only care about 4-byte aligned chunks michael@0: if (fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) { michael@0: size -= 4; // the shared Y (or YY) coordinate michael@0: if (size < 0) { michael@0: size = 0; michael@0: } michael@0: size >>= 1; michael@0: } else { michael@0: size >>= 2; michael@0: } michael@0: michael@0: if (fFilterLevel != SkPaint::kNone_FilterLevel) { michael@0: size >>= 1; michael@0: } michael@0: michael@0: return size; michael@0: }