| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkBitmapController.h" | 9 #include "SkBitmapController.h" |
| 10 #include "SkMatrix.h" | 10 #include "SkMatrix.h" |
| 11 #include "SkTemplates.h" | 11 #include "SkTemplates.h" |
| 12 | 12 |
| 13 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 13 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 14 | 14 |
| 15 static bool valid_for_drawing(const SkBitmap& bm) { | 15 static bool valid_for_drawing(const SkBitmap& bm) { |
| 16 if (0 == bm.width() || 0 == bm.height()) { | 16 if (0 == bm.width() || 0 == bm.height()) { |
| 17 return false; // nothing to draw | 17 return false; // nothing to draw |
| 18 } | 18 } |
| 19 if (NULL == bm.pixelRef()) { | 19 if (nullptr == bm.pixelRef()) { |
| 20 return false; // no pixels to read | 20 return false; // no pixels to read |
| 21 } | 21 } |
| 22 if (bm.getTexture()) { | 22 if (bm.getTexture()) { |
| 23 // we can handle texture (ugh) since lockPixels will perform a read-back | 23 // we can handle texture (ugh) since lockPixels will perform a read-back |
| 24 return true; | 24 return true; |
| 25 } | 25 } |
| 26 if (kIndex_8_SkColorType == bm.colorType()) { | 26 if (kIndex_8_SkColorType == bm.colorType()) { |
| 27 SkAutoLockPixels alp(bm); // but we need to call it before getColorTable
() is safe. | 27 SkAutoLockPixels alp(bm); // but we need to call it before getColorTable
() is safe. |
| 28 if (!bm.getColorTable()) { | 28 if (!bm.getColorTable()) { |
| 29 return false; | 29 return false; |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 return true; | 32 return true; |
| 33 } | 33 } |
| 34 | 34 |
| 35 SkBitmapController::State* SkBitmapController::requestBitmap(const SkBitmap& bm, | 35 SkBitmapController::State* SkBitmapController::requestBitmap(const SkBitmap& bm, |
| 36 const SkMatrix& inv
, | 36 const SkMatrix& inv
, |
| 37 SkFilterQuality qua
lity, | 37 SkFilterQuality qua
lity, |
| 38 void* storage, size
_t storageSize) { | 38 void* storage, size
_t storageSize) { |
| 39 | 39 |
| 40 if (!valid_for_drawing(bm)) { | 40 if (!valid_for_drawing(bm)) { |
| 41 return NULL; | 41 return nullptr; |
| 42 } | 42 } |
| 43 | 43 |
| 44 State* state = this->onRequestBitmap(bm, inv, quality, storage, storageSize)
; | 44 State* state = this->onRequestBitmap(bm, inv, quality, storage, storageSize)
; |
| 45 if (state) { | 45 if (state) { |
| 46 if (NULL == state->fPixmap.addr()) { | 46 if (nullptr == state->fPixmap.addr()) { |
| 47 SkInPlaceDeleteCheck(state, storage); | 47 SkInPlaceDeleteCheck(state, storage); |
| 48 state = NULL; | 48 state = nullptr; |
| 49 } | 49 } |
| 50 } | 50 } |
| 51 return state; | 51 return state; |
| 52 } | 52 } |
| 53 | 53 |
| 54 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 54 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 55 | 55 |
| 56 #include "SkBitmapCache.h" | 56 #include "SkBitmapCache.h" |
| 57 #include "SkBitmapScaler.h" | 57 #include "SkBitmapScaler.h" |
| 58 #include "SkMipMap.h" | 58 #include "SkMipMap.h" |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 SkASSERT(fQuality <= kMedium_SkFilterQuality); | 154 SkASSERT(fQuality <= kMedium_SkFilterQuality); |
| 155 if (fQuality != kMedium_SkFilterQuality) { | 155 if (fQuality != kMedium_SkFilterQuality) { |
| 156 return false; | 156 return false; |
| 157 } | 157 } |
| 158 | 158 |
| 159 // Our default return state is to downgrade the request to Low, w/ or w/o se
tting fBitmap | 159 // Our default return state is to downgrade the request to Low, w/ or w/o se
tting fBitmap |
| 160 // to a valid bitmap. | 160 // to a valid bitmap. |
| 161 fQuality = kLow_SkFilterQuality; | 161 fQuality = kLow_SkFilterQuality; |
| 162 | 162 |
| 163 SkSize invScaleSize; | 163 SkSize invScaleSize; |
| 164 if (!fInvMatrix.decomposeScale(&invScaleSize, NULL)) { | 164 if (!fInvMatrix.decomposeScale(&invScaleSize, nullptr)) { |
| 165 return false; | 165 return false; |
| 166 } | 166 } |
| 167 SkScalar invScale = SkScalarSqrt(invScaleSize.width() * invScaleSize.height(
)); | 167 SkScalar invScale = SkScalarSqrt(invScaleSize.width() * invScaleSize.height(
)); |
| 168 | 168 |
| 169 if (invScale > SK_Scalar1) { | 169 if (invScale > SK_Scalar1) { |
| 170 fCurrMip.reset(SkMipMapCache::FindAndRef(origBitmap)); | 170 fCurrMip.reset(SkMipMapCache::FindAndRef(origBitmap)); |
| 171 if (NULL == fCurrMip.get()) { | 171 if (nullptr == fCurrMip.get()) { |
| 172 fCurrMip.reset(SkMipMapCache::AddAndRef(origBitmap)); | 172 fCurrMip.reset(SkMipMapCache::AddAndRef(origBitmap)); |
| 173 if (NULL == fCurrMip.get()) { | 173 if (nullptr == fCurrMip.get()) { |
| 174 return false; | 174 return false; |
| 175 } | 175 } |
| 176 } | 176 } |
| 177 // diagnostic for a crasher... | 177 // diagnostic for a crasher... |
| 178 if (NULL == fCurrMip->data()) { | 178 if (nullptr == fCurrMip->data()) { |
| 179 sk_throw(); | 179 sk_throw(); |
| 180 } | 180 } |
| 181 | 181 |
| 182 SkScalar levelScale = SkScalarInvert(invScale); | 182 SkScalar levelScale = SkScalarInvert(invScale); |
| 183 SkMipMap::Level level; | 183 SkMipMap::Level level; |
| 184 if (fCurrMip->extractLevel(levelScale, &level)) { | 184 if (fCurrMip->extractLevel(levelScale, &level)) { |
| 185 SkScalar invScaleFixup = level.fScale; | 185 SkScalar invScaleFixup = level.fScale; |
| 186 fInvMatrix.postScale(invScaleFixup, invScaleFixup); | 186 fInvMatrix.postScale(invScaleFixup, invScaleFixup); |
| 187 | 187 |
| 188 const SkImageInfo info = origBitmap.info().makeWH(level.fWidth, leve
l.fHeight); | 188 const SkImageInfo info = origBitmap.info().makeWH(level.fWidth, leve
l.fHeight); |
| 189 // todo: if we could wrap the fCurrMip in a pixelref, then we could
just install | 189 // todo: if we could wrap the fCurrMip in a pixelref, then we could
just install |
| 190 // that here, and not need to explicitly track it ourselves. | 190 // that here, and not need to explicitly track it ourselves. |
| 191 return fResultBitmap.installPixels(info, level.fPixels, level.fRowBy
tes); | 191 return fResultBitmap.installPixels(info, level.fPixels, level.fRowBy
tes); |
| 192 } else { | 192 } else { |
| 193 // failed to extract, so release the mipmap | 193 // failed to extract, so release the mipmap |
| 194 fCurrMip.reset(NULL); | 194 fCurrMip.reset(nullptr); |
| 195 } | 195 } |
| 196 } | 196 } |
| 197 return false; | 197 return false; |
| 198 } | 198 } |
| 199 | 199 |
| 200 SkDefaultBitmapControllerState::SkDefaultBitmapControllerState(const SkBitmap& s
rc, | 200 SkDefaultBitmapControllerState::SkDefaultBitmapControllerState(const SkBitmap& s
rc, |
| 201 const SkMatrix& i
nv, | 201 const SkMatrix& i
nv, |
| 202 SkFilterQuality q
ual) { | 202 SkFilterQuality q
ual) { |
| 203 fInvMatrix = inv; | 203 fInvMatrix = inv; |
| 204 fQuality = qual; | 204 fQuality = qual; |
| 205 | 205 |
| 206 if (this->processHQRequest(src) || this->processMediumRequest(src)) { | 206 if (this->processHQRequest(src) || this->processMediumRequest(src)) { |
| 207 SkASSERT(fResultBitmap.getPixels()); | 207 SkASSERT(fResultBitmap.getPixels()); |
| 208 } else { | 208 } else { |
| 209 fResultBitmap = src; | 209 fResultBitmap = src; |
| 210 fResultBitmap.lockPixels(); | 210 fResultBitmap.lockPixels(); |
| 211 // lock may fail to give us pixels | 211 // lock may fail to give us pixels |
| 212 } | 212 } |
| 213 SkASSERT(fQuality <= kLow_SkFilterQuality); | 213 SkASSERT(fQuality <= kLow_SkFilterQuality); |
| 214 | 214 |
| 215 // fResultBitmap.getPixels() may be null, but our caller knows to check fPix
map.addr() | 215 // fResultBitmap.getPixels() may be null, but our caller knows to check fPix
map.addr() |
| 216 // and will destroy us if it is NULL. | 216 // and will destroy us if it is nullptr. |
| 217 fPixmap.reset(fResultBitmap.info(), fResultBitmap.getPixels(), fResultBitmap
.rowBytes(), | 217 fPixmap.reset(fResultBitmap.info(), fResultBitmap.getPixels(), fResultBitmap
.rowBytes(), |
| 218 fResultBitmap.getColorTable()); | 218 fResultBitmap.getColorTable()); |
| 219 } | 219 } |
| 220 | 220 |
| 221 SkBitmapController::State* SkDefaultBitmapController::onRequestBitmap(const SkBi
tmap& bm, | 221 SkBitmapController::State* SkDefaultBitmapController::onRequestBitmap(const SkBi
tmap& bm, |
| 222 const SkMa
trix& inverse, | 222 const SkMa
trix& inverse, |
| 223 SkFilterQu
ality quality, | 223 SkFilterQu
ality quality, |
| 224 void* stor
age, size_t size) { | 224 void* stor
age, size_t size) { |
| 225 return SkInPlaceNewCheck<SkDefaultBitmapControllerState>(storage, size, bm,
inverse, quality); | 225 return SkInPlaceNewCheck<SkDefaultBitmapControllerState>(storage, size, bm,
inverse, quality); |
| 226 } | 226 } |
| 227 | 227 |
| OLD | NEW |