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