reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "SkBitmap.h" |
| 9 | #include "SkBitmapController.h" |
reed | 013e9e3 | 2015-09-15 14:46:27 -0700 | [diff] [blame] | 10 | #include "SkBitmapProvider.h" |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 11 | #include "SkMatrix.h" |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 12 | #include "SkPixelRef.h" |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 13 | #include "SkTemplates.h" |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 14 | |
reed | 9913887 | 2015-08-31 15:16:17 -0700 | [diff] [blame] | 15 | // RESIZE_LANCZOS3 is another good option, but chrome prefers mitchell at the moment |
| 16 | #define kHQ_RESIZE_METHOD SkBitmapScaler::RESIZE_MITCHELL |
| 17 | |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 18 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 19 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 20 | SkBitmapController::State* SkBitmapController::requestBitmap(const SkBitmapProvider& provider, |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 21 | const SkMatrix& inv, |
| 22 | SkFilterQuality quality, |
| 23 | void* storage, size_t storageSize) { |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 24 | if (!provider.validForDrawing()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 25 | return nullptr; |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 26 | } |
| 27 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 28 | State* state = this->onRequestBitmap(provider, inv, quality, storage, storageSize); |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 29 | if (state) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 30 | if (nullptr == state->fPixmap.addr()) { |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 31 | SkInPlaceDeleteCheck(state, storage); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 32 | state = nullptr; |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | return state; |
| 36 | } |
| 37 | |
| 38 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 39 | |
| 40 | #include "SkBitmapCache.h" |
| 41 | #include "SkBitmapScaler.h" |
| 42 | #include "SkMipMap.h" |
| 43 | #include "SkResourceCache.h" |
| 44 | |
| 45 | class SkDefaultBitmapControllerState : public SkBitmapController::State { |
| 46 | public: |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 47 | SkDefaultBitmapControllerState(const SkBitmapProvider&, const SkMatrix& inv, SkFilterQuality); |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 48 | |
| 49 | private: |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 50 | SkBitmap fResultBitmap; |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 51 | SkAutoTUnref<const SkMipMap> fCurrMip; |
| 52 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 53 | bool processHQRequest(const SkBitmapProvider&); |
| 54 | bool processMediumRequest(const SkBitmapProvider&); |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | // Check to see that the size of the bitmap that would be produced by |
| 58 | // scaling by the given inverted matrix is less than the maximum allowed. |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 59 | static inline bool cache_size_okay(const SkBitmapProvider& provider, const SkMatrix& invMat) { |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 60 | size_t maximumAllocation = SkResourceCache::GetEffectiveSingleAllocationByteLimit(); |
| 61 | if (0 == maximumAllocation) { |
| 62 | return true; |
| 63 | } |
| 64 | // float matrixScaleFactor = 1.0 / (invMat.scaleX * invMat.scaleY); |
| 65 | // return ((origBitmapSize * matrixScaleFactor) < maximumAllocationSize); |
| 66 | // Skip the division step: |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 67 | const size_t size = provider.info().getSafeSize(provider.info().minRowBytes()); |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 68 | return size < (maximumAllocation * invMat.getScaleX() * invMat.getScaleY()); |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * High quality is implemented by performing up-right scale-only filtering and then |
| 73 | * using bilerp for any remaining transformations. |
| 74 | */ |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 75 | bool SkDefaultBitmapControllerState::processHQRequest(const SkBitmapProvider& provider) { |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 76 | if (fQuality != kHigh_SkFilterQuality) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | // Our default return state is to downgrade the request to Medium, w/ or w/o setting fBitmap |
| 81 | // to a valid bitmap. If we succeed, we will set this to Low instead. |
| 82 | fQuality = kMedium_SkFilterQuality; |
| 83 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 84 | if (kN32_SkColorType != provider.info().colorType() || !cache_size_okay(provider, fInvMatrix) || |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 85 | fInvMatrix.hasPerspective()) |
| 86 | { |
| 87 | return false; // can't handle the reqeust |
| 88 | } |
| 89 | |
| 90 | SkScalar invScaleX = fInvMatrix.getScaleX(); |
| 91 | SkScalar invScaleY = fInvMatrix.getScaleY(); |
| 92 | if (fInvMatrix.getType() & SkMatrix::kAffine_Mask) { |
| 93 | SkSize scale; |
| 94 | if (!fInvMatrix.decomposeScale(&scale)) { |
| 95 | return false; |
| 96 | } |
| 97 | invScaleX = scale.width(); |
| 98 | invScaleY = scale.height(); |
| 99 | } |
| 100 | if (SkScalarNearlyEqual(invScaleX, 1) && SkScalarNearlyEqual(invScaleY, 1)) { |
| 101 | return false; // no need for HQ |
| 102 | } |
| 103 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 104 | const int dstW = SkScalarRoundToScalar(provider.width() / invScaleX); |
| 105 | const int dstH = SkScalarRoundToScalar(provider.height() / invScaleY); |
| 106 | const SkBitmapCacheDesc desc = provider.makeCacheDesc(dstW, dstH); |
| 107 | |
| 108 | if (!SkBitmapCache::FindWH(desc, &fResultBitmap)) { |
| 109 | SkBitmap orig; |
| 110 | if (!provider.asBitmap(&orig)) { |
| 111 | return false; |
| 112 | } |
reed | 3c83432 | 2015-06-12 07:09:59 -0700 | [diff] [blame] | 113 | SkAutoPixmapUnlock src; |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 114 | if (!orig.requestLock(&src)) { |
reed | 3c83432 | 2015-06-12 07:09:59 -0700 | [diff] [blame] | 115 | return false; |
| 116 | } |
reed | 9913887 | 2015-08-31 15:16:17 -0700 | [diff] [blame] | 117 | if (!SkBitmapScaler::Resize(&fResultBitmap, src.pixmap(), kHQ_RESIZE_METHOD, |
| 118 | dstW, dstH, SkResourceCache::GetAllocator())) { |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 119 | return false; // we failed to create fScaledBitmap |
| 120 | } |
| 121 | |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 122 | SkASSERT(fResultBitmap.getPixels()); |
| 123 | fResultBitmap.setImmutable(); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame^] | 124 | if (!provider.isVolatile()) { |
| 125 | if (SkBitmapCache::AddWH(desc, fResultBitmap)) { |
| 126 | provider.notifyAddedToCache(); |
| 127 | } |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 128 | } |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 129 | } |
| 130 | |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 131 | SkASSERT(fResultBitmap.getPixels()); |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 132 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 133 | fInvMatrix.postScale(SkIntToScalar(dstW) / provider.width(), |
| 134 | SkIntToScalar(dstH) / provider.height()); |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 135 | fQuality = kLow_SkFilterQuality; |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Modulo internal errors, this should always succeed *if* the matrix is downscaling |
| 141 | * (in this case, we have the inverse, so it succeeds if fInvMatrix is upscaling) |
| 142 | */ |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 143 | bool SkDefaultBitmapControllerState::processMediumRequest(const SkBitmapProvider& provider) { |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 144 | SkASSERT(fQuality <= kMedium_SkFilterQuality); |
| 145 | if (fQuality != kMedium_SkFilterQuality) { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | // Our default return state is to downgrade the request to Low, w/ or w/o setting fBitmap |
| 150 | // to a valid bitmap. |
| 151 | fQuality = kLow_SkFilterQuality; |
| 152 | |
| 153 | SkSize invScaleSize; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 154 | if (!fInvMatrix.decomposeScale(&invScaleSize, nullptr)) { |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 155 | return false; |
| 156 | } |
| 157 | SkScalar invScale = SkScalarSqrt(invScaleSize.width() * invScaleSize.height()); |
| 158 | |
| 159 | if (invScale > SK_Scalar1) { |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 160 | fCurrMip.reset(SkMipMapCache::FindAndRef(provider.makeCacheDesc())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 161 | if (nullptr == fCurrMip.get()) { |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 162 | SkBitmap orig; |
| 163 | if (!provider.asBitmap(&orig)) { |
| 164 | return false; |
| 165 | } |
| 166 | fCurrMip.reset(SkMipMapCache::AddAndRef(orig)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 167 | if (nullptr == fCurrMip.get()) { |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | } |
| 171 | // diagnostic for a crasher... |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 172 | if (nullptr == fCurrMip->data()) { |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 173 | sk_throw(); |
| 174 | } |
| 175 | |
| 176 | SkScalar levelScale = SkScalarInvert(invScale); |
| 177 | SkMipMap::Level level; |
| 178 | if (fCurrMip->extractLevel(levelScale, &level)) { |
| 179 | SkScalar invScaleFixup = level.fScale; |
| 180 | fInvMatrix.postScale(invScaleFixup, invScaleFixup); |
| 181 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 182 | const SkImageInfo info = provider.info().makeWH(level.fWidth, level.fHeight); |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 183 | // todo: if we could wrap the fCurrMip in a pixelref, then we could just install |
| 184 | // that here, and not need to explicitly track it ourselves. |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 185 | return fResultBitmap.installPixels(info, level.fPixels, level.fRowBytes); |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 186 | } else { |
| 187 | // failed to extract, so release the mipmap |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 188 | fCurrMip.reset(nullptr); |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | return false; |
| 192 | } |
| 193 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 194 | SkDefaultBitmapControllerState::SkDefaultBitmapControllerState(const SkBitmapProvider& provider, |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 195 | const SkMatrix& inv, |
| 196 | SkFilterQuality qual) { |
| 197 | fInvMatrix = inv; |
| 198 | fQuality = qual; |
| 199 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 200 | if (this->processHQRequest(provider) || this->processMediumRequest(provider)) { |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 201 | SkASSERT(fResultBitmap.getPixels()); |
| 202 | } else { |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 203 | (void)provider.asBitmap(&fResultBitmap); |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 204 | fResultBitmap.lockPixels(); |
| 205 | // lock may fail to give us pixels |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 206 | } |
| 207 | SkASSERT(fQuality <= kLow_SkFilterQuality); |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 208 | |
| 209 | // fResultBitmap.getPixels() may be null, but our caller knows to check fPixmap.addr() |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 210 | // and will destroy us if it is nullptr. |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 211 | fPixmap.reset(fResultBitmap.info(), fResultBitmap.getPixels(), fResultBitmap.rowBytes(), |
| 212 | fResultBitmap.getColorTable()); |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 213 | } |
| 214 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 215 | SkBitmapController::State* SkDefaultBitmapController::onRequestBitmap(const SkBitmapProvider& bm, |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 216 | const SkMatrix& inverse, |
| 217 | SkFilterQuality quality, |
| 218 | void* storage, size_t size) { |
| 219 | return SkInPlaceNewCheck<SkDefaultBitmapControllerState>(storage, size, bm, inverse, quality); |
| 220 | } |
| 221 | |