Chromium Code Reviews| Index: src/core/SkBitmapController.cpp |
| diff --git a/src/core/SkBitmapController.cpp b/src/core/SkBitmapController.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9d5be9ca41d757f99487ebe844c907fb7a371161 |
| --- /dev/null |
| +++ b/src/core/SkBitmapController.cpp |
| @@ -0,0 +1,201 @@ |
| +/* |
| + * Copyright 2015 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SkBitmap.h" |
| +#include "SkBitmapController.h" |
| +#include "SkMatrix.h" |
| + |
| +static bool valid_for_drawing(const SkBitmap& bm) { |
| + if (0 == bm.width() || 0 == bm.height()) { |
| + return false; // nothing to draw |
| + } |
| + if (NULL == bm.pixelRef()) { |
| + return false; // no pixels to read |
| + } |
| + if (bm.getTexture()) { |
| + // we can handle texture (ugh) since lockPixels will perform a read-back |
| + return true; |
| + } |
| + if (kIndex_8_SkColorType == bm.colorType()) { |
| + SkAutoLockPixels alp(bm); // but we need to call it before getColorTable() is safe. |
| + if (!bm.getColorTable()) { |
| + return false; |
| + } |
| + } |
| + return true; |
| +} |
| + |
| +bool SkBitmapController::requestBitmap(const SkBitmap& inBM, const SkMatrix& inv, |
| + SkFilterQuality inQuality, SkBitmap* outBM, SkMatrix* outInv, |
| + SkFilterQuality* outQuality) { |
| + if (!valid_for_drawing(inBM)) { |
| + return false; |
| + } |
| + return this->onRequestBitmap(inBM, inv, inQuality, outBM, outInv, outQuality); |
| +} |
| + |
| +/////////////////////////////////////////////////////////////////////////////////////////////////// |
| + |
| +#include "SkBitmapCache.h" |
| +#include "SkBitmapScaler.h" |
| +#include "SkMipMap.h" |
| +#include "SkResourceCache.h" |
| + |
| +class SkDefaultBitmapControllerState { |
| +public: |
| + SkDefaultBitmapControllerState(const SkBitmap& src, const SkMatrix& inv, SkFilterQuality qual) |
| + : fOrigBitmap(src), fInvMatrix(inv), fFilterLevel(qual) |
| + {} |
| + |
| + const SkBitmap& fOrigBitmap; |
| + SkMatrix fInvMatrix; |
| + SkFilterQuality fFilterLevel; |
| + |
| + SkBitmap fScaledBitmap; |
| + SkAutoTUnref<const SkMipMap> fCurrMip; |
| + |
| + bool processHQRequest(); |
| + bool processMediumRequest(); |
| +}; |
| + |
| +// Check to see that the size of the bitmap that would be produced by |
| +// scaling by the given inverted matrix is less than the maximum allowed. |
| +static inline bool cache_size_okay(const SkBitmap& bm, const SkMatrix& invMat) { |
| + size_t maximumAllocation = SkResourceCache::GetEffectiveSingleAllocationByteLimit(); |
| + if (0 == maximumAllocation) { |
| + return true; |
| + } |
| + // float matrixScaleFactor = 1.0 / (invMat.scaleX * invMat.scaleY); |
| + // return ((origBitmapSize * matrixScaleFactor) < maximumAllocationSize); |
| + // Skip the division step: |
| + const size_t size = bm.info().getSafeSize(bm.info().minRowBytes()); |
| + return size < (maximumAllocation * invMat.getScaleX() * invMat.getScaleY()); |
| +} |
| + |
| +/* |
| + * High quality is implemented by performing up-right scale-only filtering and then |
| + * using bilerp for any remaining transformations. |
| + */ |
| +bool SkDefaultBitmapControllerState::processHQRequest() { |
| + SkASSERT(kHigh_SkFilterQuality == fFilterLevel); |
| + |
| + // Our default return state is to downgrade the request to Medium, w/ or w/o setting fBitmap |
| + // to a valid bitmap. If we succeed, we will set this to Low instead. |
| + fFilterLevel = kMedium_SkFilterQuality; |
| + |
| + if (kN32_SkColorType != fOrigBitmap.colorType() || !cache_size_okay(fOrigBitmap, fInvMatrix) || |
| + fInvMatrix.hasPerspective()) |
| + { |
| + return false; // can't handle the reqeust |
| + } |
| + |
| + SkScalar invScaleX = fInvMatrix.getScaleX(); |
| + SkScalar invScaleY = fInvMatrix.getScaleY(); |
| + if (fInvMatrix.getType() & SkMatrix::kAffine_Mask) { |
| + SkSize scale; |
| + if (!fInvMatrix.decomposeScale(&scale)) { |
| + return false; |
| + } |
| + invScaleX = scale.width(); |
| + invScaleY = scale.height(); |
| + } |
| + if (SkScalarNearlyEqual(invScaleX, 1) && SkScalarNearlyEqual(invScaleY, 1)) { |
| + return false; // no need for HQ |
| + } |
| + |
| + SkScalar trueDestWidth = fOrigBitmap.width() / invScaleX; |
| + SkScalar trueDestHeight = fOrigBitmap.height() / invScaleY; |
| + SkScalar roundedDestWidth = SkScalarRoundToScalar(trueDestWidth); |
| + SkScalar roundedDestHeight = SkScalarRoundToScalar(trueDestHeight); |
| + |
| + if (!SkBitmapCache::Find(fOrigBitmap, roundedDestWidth, roundedDestHeight, &fScaledBitmap)) { |
| + if (!SkBitmapScaler::Resize(&fScaledBitmap, |
| + fOrigBitmap, |
| + SkBitmapScaler::RESIZE_BEST, |
| + roundedDestWidth, |
| + roundedDestHeight, |
| + SkResourceCache::GetAllocator())) { |
| + return false; // we failed to create fScaledBitmap |
| + } |
| + |
| + SkASSERT(fScaledBitmap.getPixels()); |
| + fScaledBitmap.setImmutable(); |
| + SkBitmapCache::Add(fOrigBitmap, roundedDestWidth, roundedDestHeight, fScaledBitmap); |
| + } |
| + |
| + SkASSERT(fScaledBitmap.getPixels()); |
| + |
| + fInvMatrix.postScale(roundedDestWidth / fOrigBitmap.width(), |
| + roundedDestHeight / fOrigBitmap.height()); |
| + fFilterLevel = kLow_SkFilterQuality; |
| + return true; |
| +} |
| + |
| +/* |
| + * Modulo internal errors, this should always succeed *if* the matrix is downscaling |
| + * (in this case, we have the inverse, so it succeeds if fInvMatrix is upscaling) |
| + */ |
| +bool SkDefaultBitmapControllerState::processMediumRequest() { |
| + SkASSERT(kMedium_SkFilterQuality == fFilterLevel); |
| + |
| + // Our default return state is to downgrade the request to Low, w/ or w/o setting fBitmap |
| + // to a valid bitmap. |
| + fFilterLevel = kLow_SkFilterQuality; |
| + |
| + SkSize invScaleSize; |
| + if (!fInvMatrix.decomposeScale(&invScaleSize, NULL)) { |
| + return false; |
| + } |
| + SkScalar invScale = SkScalarSqrt(invScaleSize.width() * invScaleSize.height()); |
| + |
| + if (invScale > SK_Scalar1) { |
| + fCurrMip.reset(SkMipMapCache::FindAndRef(fOrigBitmap)); |
| + if (NULL == fCurrMip.get()) { |
| + fCurrMip.reset(SkMipMapCache::AddAndRef(fOrigBitmap)); |
| + if (NULL == fCurrMip.get()) { |
| + return false; |
| + } |
| + } |
| + // diagnostic for a crasher... |
| + if (NULL == fCurrMip->data()) { |
| + sk_throw(); |
| + } |
| + |
| + SkScalar levelScale = SkScalarInvert(invScale); |
| + SkMipMap::Level level; |
| + if (fCurrMip->extractLevel(levelScale, &level)) { |
| + SkScalar invScaleFixup = level.fScale; |
| + fInvMatrix.postScale(invScaleFixup, invScaleFixup); |
| + |
| + const SkImageInfo info = fOrigBitmap.info().makeWH(level.fWidth, level.fHeight); |
| + // todo: if we could wrap the fCurrMip in a pixelref, then we could just install |
| + // that here, and not need to explicitly track it ourselves. |
| + fScaledBitmap.installPixels(info, level.fPixels, level.fRowBytes); |
|
scroggo
2015/06/02 13:24:53
Should we check the return value? (Even just to as
reed1
2015/06/02 14:07:46
Done.
|
| + return true; |
| + } else { |
| + // failed to extract, so release the mipmap |
| + fCurrMip.reset(NULL); |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +bool SkDefaultBitmapController::onRequestBitmap(const SkBitmap& inBM, const SkMatrix& inInverse, |
| + SkFilterQuality inQuality, SkBitmap* outBM, |
| + SkMatrix* outInverse, SkFilterQuality* outQuality) { |
| + SkDefaultBitmapControllerState state(inBM, inInverse, inQuality); |
| + |
| + if (!state.processHQRequest() && !state.processHQRequest()) { |
|
scroggo
2015/06/02 13:24:54
Two questions:
- if inQuality is not kHigh, won't
reed1
2015/06/02 14:07:46
Done.
|
| + state.fScaledBitmap = inBM; |
| + } |
| + SkASSERT(state.fFilterLevel <= kLow_SkFilterQuality); |
| + *outBM = state.fScaledBitmap; |
| + *outInverse = inInverse; |
| + *outQuality = state.fFilterLevel; |
| + return true; |
| +} |
| + |