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 // RESIZE_LANCZOS3 is another good option, but chrome prefers mitchell at the mo
ment |
| 14 #define kHQ_RESIZE_METHOD SkBitmapScaler::RESIZE_MITCHELL |
| 15 |
13 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 16 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
14 | 17 |
15 static bool valid_for_drawing(const SkBitmap& bm) { | 18 static bool valid_for_drawing(const SkBitmap& bm) { |
16 if (0 == bm.width() || 0 == bm.height()) { | 19 if (0 == bm.width() || 0 == bm.height()) { |
17 return false; // nothing to draw | 20 return false; // nothing to draw |
18 } | 21 } |
19 if (nullptr == bm.pixelRef()) { | 22 if (nullptr == bm.pixelRef()) { |
20 return false; // no pixels to read | 23 return false; // no pixels to read |
21 } | 24 } |
22 if (bm.getTexture()) { | 25 if (bm.getTexture()) { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 if (!fInvMatrix.decomposeScale(&scale)) { | 113 if (!fInvMatrix.decomposeScale(&scale)) { |
111 return false; | 114 return false; |
112 } | 115 } |
113 invScaleX = scale.width(); | 116 invScaleX = scale.width(); |
114 invScaleY = scale.height(); | 117 invScaleY = scale.height(); |
115 } | 118 } |
116 if (SkScalarNearlyEqual(invScaleX, 1) && SkScalarNearlyEqual(invScaleY, 1))
{ | 119 if (SkScalarNearlyEqual(invScaleX, 1) && SkScalarNearlyEqual(invScaleY, 1))
{ |
117 return false; // no need for HQ | 120 return false; // no need for HQ |
118 } | 121 } |
119 | 122 |
120 SkScalar trueDestWidth = origBitmap.width() / invScaleX; | 123 const int dstW = SkScalarRoundToScalar(origBitmap.width() / invScaleX); |
121 SkScalar trueDestHeight = origBitmap.height() / invScaleY; | 124 const int dstH = SkScalarRoundToScalar(origBitmap.height() / invScaleY); |
122 SkScalar roundedDestWidth = SkScalarRoundToScalar(trueDestWidth); | |
123 SkScalar roundedDestHeight = SkScalarRoundToScalar(trueDestHeight); | |
124 | 125 |
125 if (!SkBitmapCache::Find(origBitmap, roundedDestWidth, roundedDestHeight, &f
ResultBitmap)) { | 126 if (!SkBitmapCache::FindWH(origBitmap, dstW, dstH, &fResultBitmap)) { |
126 SkAutoPixmapUnlock src; | 127 SkAutoPixmapUnlock src; |
127 if (!origBitmap.requestLock(&src)) { | 128 if (!origBitmap.requestLock(&src)) { |
128 return false; | 129 return false; |
129 } | 130 } |
130 if (!SkBitmapScaler::Resize(&fResultBitmap, src.pixmap(), SkBitmapScaler
::RESIZE_BEST, | 131 if (!SkBitmapScaler::Resize(&fResultBitmap, src.pixmap(), kHQ_RESIZE_MET
HOD, |
131 roundedDestWidth, roundedDestHeight, | 132 dstW, dstH, SkResourceCache::GetAllocator())
) { |
132 SkResourceCache::GetAllocator())) { | |
133 return false; // we failed to create fScaledBitmap | 133 return false; // we failed to create fScaledBitmap |
134 } | 134 } |
135 | 135 |
136 SkASSERT(fResultBitmap.getPixels()); | 136 SkASSERT(fResultBitmap.getPixels()); |
137 fResultBitmap.setImmutable(); | 137 fResultBitmap.setImmutable(); |
138 SkBitmapCache::Add(origBitmap, roundedDestWidth, roundedDestHeight, fRes
ultBitmap); | 138 SkBitmapCache::AddWH(origBitmap, dstW, dstH, fResultBitmap); |
139 } | 139 } |
140 | 140 |
141 SkASSERT(fResultBitmap.getPixels()); | 141 SkASSERT(fResultBitmap.getPixels()); |
142 | 142 |
143 fInvMatrix.postScale(roundedDestWidth / origBitmap.width(), | 143 fInvMatrix.postScale(SkIntToScalar(dstW) / origBitmap.width(), |
144 roundedDestHeight / origBitmap.height()); | 144 SkIntToScalar(dstH) / origBitmap.height()); |
145 fQuality = kLow_SkFilterQuality; | 145 fQuality = kLow_SkFilterQuality; |
146 return true; | 146 return true; |
147 } | 147 } |
148 | 148 |
149 /* | 149 /* |
150 * Modulo internal errors, this should always succeed *if* the matrix is downsc
aling | 150 * Modulo internal errors, this should always succeed *if* the matrix is downsc
aling |
151 * (in this case, we have the inverse, so it succeeds if fInvMatrix is upscalin
g) | 151 * (in this case, we have the inverse, so it succeeds if fInvMatrix is upscalin
g) |
152 */ | 152 */ |
153 bool SkDefaultBitmapControllerState::processMediumRequest(const SkBitmap& origBi
tmap) { | 153 bool SkDefaultBitmapControllerState::processMediumRequest(const SkBitmap& origBi
tmap) { |
154 SkASSERT(fQuality <= kMedium_SkFilterQuality); | 154 SkASSERT(fQuality <= kMedium_SkFilterQuality); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |