OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SkBitmapProcState.h" | 8 #include "SkBitmapProcState.h" |
9 #include "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
10 #include "SkFilterProc.h" | 10 #include "SkFilterProc.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 // forgets the ID, so it won't call Unlock | 120 // forgets the ID, so it won't call Unlock |
121 void release() { | 121 void release() { |
122 fIDPtr = NULL; | 122 fIDPtr = NULL; |
123 } | 123 } |
124 | 124 |
125 private: | 125 private: |
126 SkScaledImageCache::ID** fIDPtr; | 126 SkScaledImageCache::ID** fIDPtr; |
127 }; | 127 }; |
128 #define AutoScaledCacheUnlocker(...) SK_REQUIRE_LOCAL_VAR(AutoScaledCacheUnlocke
r) | 128 #define AutoScaledCacheUnlocker(...) SK_REQUIRE_LOCAL_VAR(AutoScaledCacheUnlocke
r) |
129 | 129 |
| 130 // Check to see that the size of the bitmap that would be produced by |
| 131 // scaling by the given inverted matrix is less than the maximum allowed. |
| 132 static inline bool cache_size_okay(const SkBitmap& bm, const SkMatrix& invMat) { |
| 133 size_t maximumAllocation |
| 134 = SkScaledImageCache::GetSingleAllocationByteLimit(); |
| 135 if (0 == maximumAllocation) { |
| 136 return true; |
| 137 } |
| 138 // float matrixScaleFactor = 1.0 / (invMat.scaleX * invMat.scaleY); |
| 139 // return ((origBitmapSize * matrixScaleFactor) < maximumAllocationSize); |
| 140 // Skip the division step: |
| 141 return bm.info().getSafeSize(bm.info().minRowBytes()) |
| 142 < (maximumAllocation * invMat.getScaleX() * invMat.getScaleY()); |
| 143 } |
| 144 |
130 // TODO -- we may want to pass the clip into this function so we only scale | 145 // TODO -- we may want to pass the clip into this function so we only scale |
131 // the portion of the image that we're going to need. This will complicate | 146 // the portion of the image that we're going to need. This will complicate |
132 // the interface to the cache, but might be well worth it. | 147 // the interface to the cache, but might be well worth it. |
133 | 148 |
134 bool SkBitmapProcState::possiblyScaleImage() { | 149 bool SkBitmapProcState::possiblyScaleImage() { |
135 AutoScaledCacheUnlocker unlocker(&fScaledCacheID); | 150 AutoScaledCacheUnlocker unlocker(&fScaledCacheID); |
136 | 151 |
137 SkASSERT(NULL == fBitmap); | 152 SkASSERT(NULL == fBitmap); |
138 SkASSERT(NULL == fScaledCacheID); | 153 SkASSERT(NULL == fScaledCacheID); |
139 | 154 |
140 if (fFilterLevel <= SkPaint::kLow_FilterLevel) { | 155 if (fFilterLevel <= SkPaint::kLow_FilterLevel) { |
141 return false; | 156 return false; |
142 } | 157 } |
143 | |
144 // Check to see if the transformation matrix is simple, and if we're | 158 // Check to see if the transformation matrix is simple, and if we're |
145 // doing high quality scaling. If so, do the bitmap scale here and | 159 // doing high quality scaling. If so, do the bitmap scale here and |
146 // remove the scaling component from the matrix. | 160 // remove the scaling component from the matrix. |
147 | 161 |
148 if (SkPaint::kHigh_FilterLevel == fFilterLevel && | 162 if (SkPaint::kHigh_FilterLevel == fFilterLevel && |
149 fInvMatrix.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Ma
sk) && | 163 fInvMatrix.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Ma
sk) && |
150 kN32_SkColorType == fOrigBitmap.colorType()) { | 164 kN32_SkColorType == fOrigBitmap.colorType() && |
| 165 cache_size_okay(fOrigBitmap, fInvMatrix)) { |
151 | 166 |
152 SkScalar invScaleX = fInvMatrix.getScaleX(); | 167 SkScalar invScaleX = fInvMatrix.getScaleX(); |
153 SkScalar invScaleY = fInvMatrix.getScaleY(); | 168 SkScalar invScaleY = fInvMatrix.getScaleY(); |
154 | 169 |
155 fScaledCacheID = SkScaledImageCache::FindAndLock(fOrigBitmap, | 170 fScaledCacheID = SkScaledImageCache::FindAndLock(fOrigBitmap, |
156 invScaleX, invScaleY, | 171 invScaleX, invScaleY, |
157 &fScaledBitmap); | 172 &fScaledBitmap); |
158 if (fScaledCacheID) { | 173 if (fScaledCacheID) { |
159 fScaledBitmap.lockPixels(); | 174 fScaledBitmap.lockPixels(); |
160 if (!fScaledBitmap.getPixels()) { | 175 if (!fScaledBitmap.getPixels()) { |
(...skipping 873 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1034 } else { | 1049 } else { |
1035 size >>= 2; | 1050 size >>= 2; |
1036 } | 1051 } |
1037 | 1052 |
1038 if (fFilterLevel != SkPaint::kNone_FilterLevel) { | 1053 if (fFilterLevel != SkPaint::kNone_FilterLevel) { |
1039 size >>= 1; | 1054 size >>= 1; |
1040 } | 1055 } |
1041 | 1056 |
1042 return size; | 1057 return size; |
1043 } | 1058 } |
OLD | NEW |