| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 "SkBitmapCache.h" | 8 #include "SkBitmapCache.h" |
| 9 #include "SkBitmapProcState.h" | 9 #include "SkBitmapProcState.h" |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 } | 91 } |
| 92 | 92 |
| 93 /////////////////////////////////////////////////////////////////////////////// | 93 /////////////////////////////////////////////////////////////////////////////// |
| 94 | 94 |
| 95 static bool valid_for_filtering(unsigned dimension) { | 95 static bool valid_for_filtering(unsigned dimension) { |
| 96 // for filtering, width and height must fit in 14bits, since we use steal | 96 // for filtering, width and height must fit in 14bits, since we use steal |
| 97 // 2 bits from each to store our 4bit subpixel data | 97 // 2 bits from each to store our 4bit subpixel data |
| 98 return (dimension & ~0x3FFF) == 0; | 98 return (dimension & ~0x3FFF) == 0; |
| 99 } | 99 } |
| 100 | 100 |
| 101 static SkScalar effective_matrix_scale_sqrd(const SkMatrix& mat) { | 101 static SkScalar effective_matrix_scale(const SkMatrix& mat) { |
| 102 SkPoint v1, v2; | 102 SkScalar dx = SkVector::Length(mat.getScaleX(), mat.getSkewY()); |
| 103 | 103 SkScalar dy = SkVector::Length(mat.getSkewX(), mat.getScaleY()); |
| 104 v1.fX = mat.getScaleX(); | 104 #ifdef SK_SUPPORT_LEGACY_MIPMAP_EFFECTIVE_SCALE |
| 105 v1.fY = mat.getSkewY(); | 105 return SkMaxScalar(dx, dy); |
| 106 | 106 #else |
| 107 v2.fX = mat.getSkewX(); | 107 return SkScalarSqrt(dx * dy); |
| 108 v2.fY = mat.getScaleY(); | 108 #endif |
| 109 | |
| 110 return SkMaxScalar(v1.lengthSqd(), v2.lengthSqd()); | |
| 111 } | 109 } |
| 112 | 110 |
| 113 // Check to see that the size of the bitmap that would be produced by | 111 // Check to see that the size of the bitmap that would be produced by |
| 114 // scaling by the given inverted matrix is less than the maximum allowed. | 112 // scaling by the given inverted matrix is less than the maximum allowed. |
| 115 static inline bool cache_size_okay(const SkBitmap& bm, const SkMatrix& invMat) { | 113 static inline bool cache_size_okay(const SkBitmap& bm, const SkMatrix& invMat) { |
| 116 size_t maximumAllocation = SkResourceCache::GetEffectiveSingleAllocationByte
Limit(); | 114 size_t maximumAllocation = SkResourceCache::GetEffectiveSingleAllocationByte
Limit(); |
| 117 if (0 == maximumAllocation) { | 115 if (0 == maximumAllocation) { |
| 118 return true; | 116 return true; |
| 119 } | 117 } |
| 120 // float matrixScaleFactor = 1.0 / (invMat.scaleX * invMat.scaleY); | 118 // float matrixScaleFactor = 1.0 / (invMat.scaleX * invMat.scaleY); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 * Modulo internal errors, this should always succeed *if* the matrix is downsc
aling | 201 * Modulo internal errors, this should always succeed *if* the matrix is downsc
aling |
| 204 * (in this case, we have the inverse, so it succeeds if fInvMatrix is upscalin
g) | 202 * (in this case, we have the inverse, so it succeeds if fInvMatrix is upscalin
g) |
| 205 */ | 203 */ |
| 206 void SkBitmapProcState::processMediumRequest() { | 204 void SkBitmapProcState::processMediumRequest() { |
| 207 SkASSERT(SkPaint::kMedium_FilterLevel == fFilterLevel); | 205 SkASSERT(SkPaint::kMedium_FilterLevel == fFilterLevel); |
| 208 | 206 |
| 209 // Our default return state is to downgrade the request to Low, w/ or w/o se
tting fBitmap | 207 // Our default return state is to downgrade the request to Low, w/ or w/o se
tting fBitmap |
| 210 // to a valid bitmap. | 208 // to a valid bitmap. |
| 211 fFilterLevel = SkPaint::kLow_FilterLevel; | 209 fFilterLevel = SkPaint::kLow_FilterLevel; |
| 212 | 210 |
| 213 SkScalar invScaleSqd = effective_matrix_scale_sqrd(fInvMatrix); | 211 SkScalar invScale = effective_matrix_scale(fInvMatrix); |
| 214 | 212 |
| 215 if (invScaleSqd > SK_Scalar1) { | 213 if (invScale > SK_Scalar1) { |
| 216 fCurrMip.reset(SkMipMapCache::FindAndRef(fOrigBitmap)); | 214 fCurrMip.reset(SkMipMapCache::FindAndRef(fOrigBitmap)); |
| 217 if (NULL == fCurrMip.get()) { | 215 if (NULL == fCurrMip.get()) { |
| 218 fCurrMip.reset(SkMipMapCache::AddAndRef(fOrigBitmap)); | 216 fCurrMip.reset(SkMipMapCache::AddAndRef(fOrigBitmap)); |
| 219 if (NULL == fCurrMip.get()) { | 217 if (NULL == fCurrMip.get()) { |
| 220 return; | 218 return; |
| 221 } | 219 } |
| 222 } | 220 } |
| 223 // diagnostic for a crasher... | 221 // diagnostic for a crasher... |
| 224 if (NULL == fCurrMip->data()) { | 222 if (NULL == fCurrMip->data()) { |
| 225 sk_throw(); | 223 sk_throw(); |
| 226 } | 224 } |
| 227 | 225 |
| 228 SkScalar levelScale = SkScalarInvert(SkScalarSqrt(invScaleSqd)); | 226 SkScalar levelScale = SkScalarInvert(invScale); |
| 229 SkMipMap::Level level; | 227 SkMipMap::Level level; |
| 230 if (fCurrMip->extractLevel(levelScale, &level)) { | 228 if (fCurrMip->extractLevel(levelScale, &level)) { |
| 231 SkScalar invScaleFixup = level.fScale; | 229 SkScalar invScaleFixup = level.fScale; |
| 232 fInvMatrix.postScale(invScaleFixup, invScaleFixup); | 230 fInvMatrix.postScale(invScaleFixup, invScaleFixup); |
| 233 | 231 |
| 234 const SkImageInfo info = fOrigBitmap.info().makeWH(level.fWidth, lev
el.fHeight); | 232 const SkImageInfo info = fOrigBitmap.info().makeWH(level.fWidth, lev
el.fHeight); |
| 235 // todo: if we could wrap the fCurrMip in a pixelref, then we could
just install | 233 // todo: if we could wrap the fCurrMip in a pixelref, then we could
just install |
| 236 // that here, and not need to explicitly track it ourselves. | 234 // that here, and not need to explicitly track it ourselves. |
| 237 fScaledBitmap.installPixels(info, level.fPixels, level.fRowBytes); | 235 fScaledBitmap.installPixels(info, level.fPixels, level.fRowBytes); |
| 238 fBitmap = &fScaledBitmap; | 236 fBitmap = &fScaledBitmap; |
| (...skipping 796 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1035 fx += dx; | 1033 fx += dx; |
| 1036 } | 1034 } |
| 1037 } else { | 1035 } else { |
| 1038 for (int i = 0; i < count; ++i) { | 1036 for (int i = 0; i < count; ++i) { |
| 1039 dst[i] = src[SkClampMax(SkFractionalIntToInt(fx), maxX)]; | 1037 dst[i] = src[SkClampMax(SkFractionalIntToInt(fx), maxX)]; |
| 1040 fx += dx; | 1038 fx += dx; |
| 1041 } | 1039 } |
| 1042 } | 1040 } |
| 1043 } | 1041 } |
| 1044 | 1042 |
| OLD | NEW |