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