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" |
(...skipping 24 matching lines...) Expand all Loading... |
35 const SkMatrix& inv
, | 35 const SkMatrix& inv
, |
36 SkFilterQuality qua
lity, | 36 SkFilterQuality qua
lity, |
37 void* storage, size
_t storageSize) { | 37 void* storage, size
_t storageSize) { |
38 | 38 |
39 if (!valid_for_drawing(bm)) { | 39 if (!valid_for_drawing(bm)) { |
40 return NULL; | 40 return NULL; |
41 } | 41 } |
42 | 42 |
43 State* state = this->onRequestBitmap(bm, inv, quality, storage, storageSize)
; | 43 State* state = this->onRequestBitmap(bm, inv, quality, storage, storageSize)
; |
44 if (state) { | 44 if (state) { |
45 if (!state->fLockedBitmap.getPixels()) { | 45 if (NULL == state->fPixmap.addr()) { |
46 state->fLockedBitmap.lockPixels(); | 46 SkInPlaceDeleteCheck(state, storage); |
47 if (!state->fLockedBitmap.getPixels()) { | 47 state = NULL; |
48 SkInPlaceDeleteCheck(state, storage); | |
49 state = NULL; | |
50 } | |
51 } | 48 } |
52 } | 49 } |
53 return state; | 50 return state; |
54 } | 51 } |
55 | 52 |
56 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 53 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
57 | 54 |
58 #include "SkBitmapCache.h" | 55 #include "SkBitmapCache.h" |
59 #include "SkBitmapScaler.h" | 56 #include "SkBitmapScaler.h" |
60 #include "SkMipMap.h" | 57 #include "SkMipMap.h" |
61 #include "SkResourceCache.h" | 58 #include "SkResourceCache.h" |
62 | 59 |
63 class SkDefaultBitmapControllerState : public SkBitmapController::State { | 60 class SkDefaultBitmapControllerState : public SkBitmapController::State { |
64 public: | 61 public: |
65 SkDefaultBitmapControllerState(const SkBitmap& src, const SkMatrix& inv, SkF
ilterQuality qual); | 62 SkDefaultBitmapControllerState(const SkBitmap& src, const SkMatrix& inv, SkF
ilterQuality qual); |
66 | 63 |
67 private: | 64 private: |
| 65 SkBitmap fResultBitmap; |
68 SkAutoTUnref<const SkMipMap> fCurrMip; | 66 SkAutoTUnref<const SkMipMap> fCurrMip; |
69 | 67 |
70 bool processHQRequest(const SkBitmap& orig); | 68 bool processHQRequest(const SkBitmap& orig); |
71 bool processMediumRequest(const SkBitmap& orig); | 69 bool processMediumRequest(const SkBitmap& orig); |
72 }; | 70 }; |
73 | 71 |
74 // Check to see that the size of the bitmap that would be produced by | 72 // Check to see that the size of the bitmap that would be produced by |
75 // scaling by the given inverted matrix is less than the maximum allowed. | 73 // scaling by the given inverted matrix is less than the maximum allowed. |
76 static inline bool cache_size_okay(const SkBitmap& bm, const SkMatrix& invMat) { | 74 static inline bool cache_size_okay(const SkBitmap& bm, const SkMatrix& invMat) { |
77 size_t maximumAllocation = SkResourceCache::GetEffectiveSingleAllocationByte
Limit(); | 75 size_t maximumAllocation = SkResourceCache::GetEffectiveSingleAllocationByte
Limit(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 } | 114 } |
117 if (SkScalarNearlyEqual(invScaleX, 1) && SkScalarNearlyEqual(invScaleY, 1))
{ | 115 if (SkScalarNearlyEqual(invScaleX, 1) && SkScalarNearlyEqual(invScaleY, 1))
{ |
118 return false; // no need for HQ | 116 return false; // no need for HQ |
119 } | 117 } |
120 | 118 |
121 SkScalar trueDestWidth = origBitmap.width() / invScaleX; | 119 SkScalar trueDestWidth = origBitmap.width() / invScaleX; |
122 SkScalar trueDestHeight = origBitmap.height() / invScaleY; | 120 SkScalar trueDestHeight = origBitmap.height() / invScaleY; |
123 SkScalar roundedDestWidth = SkScalarRoundToScalar(trueDestWidth); | 121 SkScalar roundedDestWidth = SkScalarRoundToScalar(trueDestWidth); |
124 SkScalar roundedDestHeight = SkScalarRoundToScalar(trueDestHeight); | 122 SkScalar roundedDestHeight = SkScalarRoundToScalar(trueDestHeight); |
125 | 123 |
126 if (!SkBitmapCache::Find(origBitmap, roundedDestWidth, roundedDestHeight, &f
LockedBitmap)) { | 124 if (!SkBitmapCache::Find(origBitmap, roundedDestWidth, roundedDestHeight, &f
ResultBitmap)) { |
127 if (!SkBitmapScaler::Resize(&fLockedBitmap, | 125 if (!SkBitmapScaler::Resize(&fResultBitmap, |
128 origBitmap, | 126 origBitmap, |
129 SkBitmapScaler::RESIZE_BEST, | 127 SkBitmapScaler::RESIZE_BEST, |
130 roundedDestWidth, | 128 roundedDestWidth, |
131 roundedDestHeight, | 129 roundedDestHeight, |
132 SkResourceCache::GetAllocator())) { | 130 SkResourceCache::GetAllocator())) { |
133 return false; // we failed to create fScaledBitmap | 131 return false; // we failed to create fScaledBitmap |
134 } | 132 } |
135 | 133 |
136 SkASSERT(fLockedBitmap.getPixels()); | 134 SkASSERT(fResultBitmap.getPixels()); |
137 fLockedBitmap.setImmutable(); | 135 fResultBitmap.setImmutable(); |
138 SkBitmapCache::Add(origBitmap, roundedDestWidth, roundedDestHeight, fLoc
kedBitmap); | 136 SkBitmapCache::Add(origBitmap, roundedDestWidth, roundedDestHeight, fRes
ultBitmap); |
139 } | 137 } |
140 | 138 |
141 SkASSERT(fLockedBitmap.getPixels()); | 139 SkASSERT(fResultBitmap.getPixels()); |
142 | 140 |
143 fInvMatrix.postScale(roundedDestWidth / origBitmap.width(), | 141 fInvMatrix.postScale(roundedDestWidth / origBitmap.width(), |
144 roundedDestHeight / origBitmap.height()); | 142 roundedDestHeight / origBitmap.height()); |
145 fQuality = kLow_SkFilterQuality; | 143 fQuality = kLow_SkFilterQuality; |
146 return true; | 144 return true; |
147 } | 145 } |
148 | 146 |
149 /* | 147 /* |
150 * Modulo internal errors, this should always succeed *if* the matrix is downsc
aling | 148 * 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) | 149 * (in this case, we have the inverse, so it succeeds if fInvMatrix is upscalin
g) |
(...skipping 29 matching lines...) Expand all Loading... |
181 | 179 |
182 SkScalar levelScale = SkScalarInvert(invScale); | 180 SkScalar levelScale = SkScalarInvert(invScale); |
183 SkMipMap::Level level; | 181 SkMipMap::Level level; |
184 if (fCurrMip->extractLevel(levelScale, &level)) { | 182 if (fCurrMip->extractLevel(levelScale, &level)) { |
185 SkScalar invScaleFixup = level.fScale; | 183 SkScalar invScaleFixup = level.fScale; |
186 fInvMatrix.postScale(invScaleFixup, invScaleFixup); | 184 fInvMatrix.postScale(invScaleFixup, invScaleFixup); |
187 | 185 |
188 const SkImageInfo info = origBitmap.info().makeWH(level.fWidth, leve
l.fHeight); | 186 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 | 187 // 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. | 188 // that here, and not need to explicitly track it ourselves. |
191 return fLockedBitmap.installPixels(info, level.fPixels, level.fRowBy
tes); | 189 return fResultBitmap.installPixels(info, level.fPixels, level.fRowBy
tes); |
192 } else { | 190 } else { |
193 // failed to extract, so release the mipmap | 191 // failed to extract, so release the mipmap |
194 fCurrMip.reset(NULL); | 192 fCurrMip.reset(NULL); |
195 } | 193 } |
196 } | 194 } |
197 return false; | 195 return false; |
198 } | 196 } |
199 | 197 |
200 SkDefaultBitmapControllerState::SkDefaultBitmapControllerState(const SkBitmap& s
rc, | 198 SkDefaultBitmapControllerState::SkDefaultBitmapControllerState(const SkBitmap& s
rc, |
201 const SkMatrix& i
nv, | 199 const SkMatrix& i
nv, |
202 SkFilterQuality q
ual) { | 200 SkFilterQuality q
ual) { |
203 fInvMatrix = inv; | 201 fInvMatrix = inv; |
204 fQuality = qual; | 202 fQuality = qual; |
205 | 203 |
206 if (!this->processHQRequest(src) && !this->processMediumRequest(src)) { | 204 if (this->processHQRequest(src) || this->processMediumRequest(src)) { |
207 fLockedBitmap = src; | 205 SkASSERT(fResultBitmap.getPixels()); |
| 206 } else { |
| 207 fResultBitmap = src; |
| 208 fResultBitmap.lockPixels(); |
| 209 // lock may fail to give us pixels |
208 } | 210 } |
209 SkASSERT(fQuality <= kLow_SkFilterQuality); | 211 SkASSERT(fQuality <= kLow_SkFilterQuality); |
| 212 |
| 213 // fResultBitmap.getPixels() may be null, but our caller knows to check fPix
map.addr() |
| 214 // and will destroy us if it is NULL. |
| 215 fPixmap.reset(fResultBitmap.info(), fResultBitmap.getPixels(), fResultBitmap
.rowBytes(), |
| 216 fResultBitmap.getColorTable()); |
210 } | 217 } |
211 | 218 |
212 SkBitmapController::State* SkDefaultBitmapController::onRequestBitmap(const SkBi
tmap& bm, | 219 SkBitmapController::State* SkDefaultBitmapController::onRequestBitmap(const SkBi
tmap& bm, |
213 const SkMa
trix& inverse, | 220 const SkMa
trix& inverse, |
214 SkFilterQu
ality quality, | 221 SkFilterQu
ality quality, |
215 void* stor
age, size_t size) { | 222 void* stor
age, size_t size) { |
216 return SkInPlaceNewCheck<SkDefaultBitmapControllerState>(storage, size, bm,
inverse, quality); | 223 return SkInPlaceNewCheck<SkDefaultBitmapControllerState>(storage, size, bm,
inverse, quality); |
217 } | 224 } |
218 | 225 |
OLD | NEW |