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 "SkBitmapProvider.h" | 10 #include "SkBitmapProvider.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 #include "SkMipMap.h" | 42 #include "SkMipMap.h" |
43 #include "SkResourceCache.h" | 43 #include "SkResourceCache.h" |
44 | 44 |
45 class SkDefaultBitmapControllerState : public SkBitmapController::State { | 45 class SkDefaultBitmapControllerState : public SkBitmapController::State { |
46 public: | 46 public: |
47 SkDefaultBitmapControllerState(const SkBitmapProvider&, const SkMatrix& inv,
SkFilterQuality); | 47 SkDefaultBitmapControllerState(const SkBitmapProvider&, const SkMatrix& inv,
SkFilterQuality); |
48 | 48 |
49 private: | 49 private: |
50 SkBitmap fResultBitmap; | 50 SkBitmap fResultBitmap; |
51 SkAutoTUnref<const SkMipMap> fCurrMip; | 51 SkAutoTUnref<const SkMipMap> fCurrMip; |
52 | 52 |
53 bool processHQRequest(const SkBitmapProvider&); | 53 bool processHQRequest(const SkBitmapProvider&); |
54 bool processMediumRequest(const SkBitmapProvider&); | 54 bool processMediumRequest(const SkBitmapProvider&); |
55 }; | 55 }; |
56 | 56 |
57 // Check to see that the size of the bitmap that would be produced by | 57 // Check to see that the size of the bitmap that would be produced by |
58 // scaling by the given inverted matrix is less than the maximum allowed. | 58 // scaling by the given inverted matrix is less than the maximum allowed. |
59 static inline bool cache_size_okay(const SkBitmapProvider& provider, const SkMat
rix& invMat) { | 59 static inline bool cache_size_okay(const SkBitmapProvider& provider, const SkMat
rix& invMat) { |
60 size_t maximumAllocation = SkResourceCache::GetEffectiveSingleAllocationByte
Limit(); | 60 size_t maximumAllocation = SkResourceCache::GetEffectiveSingleAllocationByte
Limit(); |
61 if (0 == maximumAllocation) { | 61 if (0 == maximumAllocation) { |
62 return true; | 62 return true; |
63 } | 63 } |
64 // float matrixScaleFactor = 1.0 / (invMat.scaleX * invMat.scaleY); | 64 // float matrixScaleFactor = 1.0 / (invMat.scaleX * invMat.scaleY); |
65 // return ((origBitmapSize * matrixScaleFactor) < maximumAllocationSize); | 65 // return ((origBitmapSize * matrixScaleFactor) < maximumAllocationSize); |
66 // Skip the division step: | 66 // Skip the division step: |
67 const size_t size = provider.info().getSafeSize(provider.info().minRowBytes(
)); | 67 const size_t size = provider.info().getSafeSize(provider.info().minRowBytes(
)); |
68 SkScalar invScaleSqr = invMat.getScaleX() * invMat.getScaleY(); | 68 SkScalar invScaleSqr = invMat.getScaleX() * invMat.getScaleY(); |
69 return size < (maximumAllocation * SkScalarAbs(invScaleSqr)); | 69 return size < (maximumAllocation * SkScalarAbs(invScaleSqr)); |
70 } | 70 } |
71 | 71 |
72 /* | 72 /* |
73 * High quality is implemented by performing up-right scale-only filtering and
then | 73 * High quality is implemented by performing up-right scale-only filtering and
then |
74 * using bilerp for any remaining transformations. | 74 * using bilerp for any remaining transformations. |
75 */ | 75 */ |
76 bool SkDefaultBitmapControllerState::processHQRequest(const SkBitmapProvider& pr
ovider) { | 76 bool SkDefaultBitmapControllerState::processHQRequest(const SkBitmapProvider& pr
ovider) { |
77 if (fQuality != kHigh_SkFilterQuality) { | 77 if (fQuality != kHigh_SkFilterQuality) { |
78 return false; | 78 return false; |
79 } | 79 } |
80 | 80 |
81 // Our default return state is to downgrade the request to Medium, w/ or w/o
setting fBitmap | 81 // Our default return state is to downgrade the request to Medium, w/ or w/o
setting fBitmap |
82 // to a valid bitmap. If we succeed, we will set this to Low instead. | 82 // to a valid bitmap. If we succeed, we will set this to Low instead. |
83 fQuality = kMedium_SkFilterQuality; | 83 fQuality = kMedium_SkFilterQuality; |
84 | 84 |
85 if (kN32_SkColorType != provider.info().colorType() || !cache_size_okay(prov
ider, fInvMatrix) || | 85 if (kN32_SkColorType != provider.info().colorType() || !cache_size_okay(prov
ider, fInvMatrix) || |
86 fInvMatrix.hasPerspective()) | 86 fInvMatrix.hasPerspective()) |
87 { | 87 { |
88 return false; // can't handle the reqeust | 88 return false; // can't handle the reqeust |
89 } | 89 } |
90 | 90 |
91 SkScalar invScaleX = fInvMatrix.getScaleX(); | 91 SkScalar invScaleX = fInvMatrix.getScaleX(); |
92 SkScalar invScaleY = fInvMatrix.getScaleY(); | 92 SkScalar invScaleY = fInvMatrix.getScaleY(); |
93 if (fInvMatrix.getType() & SkMatrix::kAffine_Mask) { | 93 if (fInvMatrix.getType() & SkMatrix::kAffine_Mask) { |
94 SkSize scale; | 94 SkSize scale; |
95 if (!fInvMatrix.decomposeScale(&scale)) { | 95 if (!fInvMatrix.decomposeScale(&scale)) { |
96 return false; | 96 return false; |
97 } | 97 } |
98 invScaleX = scale.width(); | 98 invScaleX = scale.width(); |
99 invScaleY = scale.height(); | 99 invScaleY = scale.height(); |
100 } | 100 } |
(...skipping 18 matching lines...) Expand all Loading... |
119 return false; | 119 return false; |
120 } | 120 } |
121 SkAutoPixmapUnlock src; | 121 SkAutoPixmapUnlock src; |
122 if (!orig.requestLock(&src)) { | 122 if (!orig.requestLock(&src)) { |
123 return false; | 123 return false; |
124 } | 124 } |
125 if (!SkBitmapScaler::Resize(&fResultBitmap, src.pixmap(), kHQ_RESIZE_MET
HOD, | 125 if (!SkBitmapScaler::Resize(&fResultBitmap, src.pixmap(), kHQ_RESIZE_MET
HOD, |
126 dstW, dstH, SkResourceCache::GetAllocator())
) { | 126 dstW, dstH, SkResourceCache::GetAllocator())
) { |
127 return false; // we failed to create fScaledBitmap | 127 return false; // we failed to create fScaledBitmap |
128 } | 128 } |
129 | 129 |
130 SkASSERT(fResultBitmap.getPixels()); | 130 SkASSERT(fResultBitmap.getPixels()); |
131 fResultBitmap.setImmutable(); | 131 fResultBitmap.setImmutable(); |
132 if (!provider.isVolatile()) { | 132 if (!provider.isVolatile()) { |
133 if (SkBitmapCache::AddWH(desc, fResultBitmap)) { | 133 if (SkBitmapCache::AddWH(desc, fResultBitmap)) { |
134 provider.notifyAddedToCache(); | 134 provider.notifyAddedToCache(); |
135 } | 135 } |
136 } | 136 } |
137 } | 137 } |
138 | 138 |
139 SkASSERT(fResultBitmap.getPixels()); | 139 SkASSERT(fResultBitmap.getPixels()); |
140 | 140 |
141 fInvMatrix.postScale(SkIntToScalar(dstW) / provider.width(), | 141 fInvMatrix.postScale(SkIntToScalar(dstW) / provider.width(), |
142 SkIntToScalar(dstH) / provider.height()); | 142 SkIntToScalar(dstH) / provider.height()); |
143 fQuality = kLow_SkFilterQuality; | 143 fQuality = kLow_SkFilterQuality; |
144 return true; | 144 return true; |
145 } | 145 } |
146 | 146 |
147 /* | 147 /* |
148 * 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 |
149 * (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) |
150 */ | 150 */ |
151 bool SkDefaultBitmapControllerState::processMediumRequest(const SkBitmapProvider
& provider) { | 151 bool SkDefaultBitmapControllerState::processMediumRequest(const SkBitmapProvider
& provider) { |
152 SkASSERT(fQuality <= kMedium_SkFilterQuality); | 152 SkASSERT(fQuality <= kMedium_SkFilterQuality); |
153 if (fQuality != kMedium_SkFilterQuality) { | 153 if (fQuality != kMedium_SkFilterQuality) { |
154 return false; | 154 return false; |
155 } | 155 } |
156 | 156 |
157 // Our default return state is to downgrade the request to Low, w/ or w/o se
tting fBitmap | 157 // Our default return state is to downgrade the request to Low, w/ or w/o se
tting fBitmap |
158 // to a valid bitmap. | 158 // to a valid bitmap. |
159 fQuality = kLow_SkFilterQuality; | 159 fQuality = kLow_SkFilterQuality; |
160 | 160 |
161 SkSize invScaleSize; | 161 SkSize invScaleSize; |
162 if (!fInvMatrix.decomposeScale(&invScaleSize, nullptr)) { | 162 if (!fInvMatrix.decomposeScale(&invScaleSize, nullptr)) { |
163 return false; | 163 return false; |
164 } | 164 } |
165 | 165 |
166 if (invScaleSize.width() > SK_Scalar1 || invScaleSize.height() > SK_Scalar1)
{ | 166 if (invScaleSize.width() > SK_Scalar1 || invScaleSize.height() > SK_Scalar1)
{ |
167 fCurrMip.reset(SkMipMapCache::FindAndRef(provider.makeCacheDesc())); | 167 fCurrMip.reset(SkMipMapCache::FindAndRef(provider.makeCacheDesc())); |
168 if (nullptr == fCurrMip.get()) { | 168 if (nullptr == fCurrMip.get()) { |
169 SkBitmap orig; | 169 SkBitmap orig; |
170 if (!provider.asBitmap(&orig)) { | 170 if (!provider.asBitmap(&orig)) { |
171 return false; | 171 return false; |
172 } | 172 } |
173 fCurrMip.reset(SkMipMapCache::AddAndRef(orig)); | 173 fCurrMip.reset(SkMipMapCache::AddAndRef(orig)); |
174 if (nullptr == fCurrMip.get()) { | 174 if (nullptr == fCurrMip.get()) { |
175 return false; | 175 return false; |
176 } | 176 } |
177 } | 177 } |
178 // diagnostic for a crasher... | 178 // diagnostic for a crasher... |
179 if (nullptr == fCurrMip->data()) { | 179 if (nullptr == fCurrMip->data()) { |
180 sk_throw(); | 180 sk_throw(); |
181 } | 181 } |
182 | 182 |
183 const SkSize scale = SkSize::Make(SkScalarInvert(invScaleSize.width()), | 183 const SkSize scale = SkSize::Make(SkScalarInvert(invScaleSize.width()), |
184 SkScalarInvert(invScaleSize.height()))
; | 184 SkScalarInvert(invScaleSize.height()))
; |
185 SkMipMap::Level level; | 185 SkMipMap::Level level; |
186 if (fCurrMip->extractLevel(scale, &level)) { | 186 if (fCurrMip->extractLevel(scale, &level)) { |
187 const SkSize& invScaleFixup = level.fScale; | 187 const SkSize& invScaleFixup = level.fScale; |
188 fInvMatrix.postScale(invScaleFixup.width(), invScaleFixup.height()); | 188 fInvMatrix.postScale(invScaleFixup.width(), invScaleFixup.height()); |
189 | 189 |
190 // todo: if we could wrap the fCurrMip in a pixelref, then we could
just install | 190 // todo: if we could wrap the fCurrMip in a pixelref, then we could
just install |
191 // that here, and not need to explicitly track it ourselves. | 191 // that here, and not need to explicitly track it ourselves. |
192 return fResultBitmap.installPixels(level.fPixmap); | 192 return fResultBitmap.installPixels(level.fPixmap); |
(...skipping 25 matching lines...) Expand all Loading... |
218 fPixmap.reset(fResultBitmap.info(), fResultBitmap.getPixels(), fResultBitmap
.rowBytes(), | 218 fPixmap.reset(fResultBitmap.info(), fResultBitmap.getPixels(), fResultBitmap
.rowBytes(), |
219 fResultBitmap.getColorTable()); | 219 fResultBitmap.getColorTable()); |
220 } | 220 } |
221 | 221 |
222 SkBitmapController::State* SkDefaultBitmapController::onRequestBitmap(const SkBi
tmapProvider& bm, | 222 SkBitmapController::State* SkDefaultBitmapController::onRequestBitmap(const SkBi
tmapProvider& bm, |
223 const SkMa
trix& inverse, | 223 const SkMa
trix& inverse, |
224 SkFilterQu
ality quality, | 224 SkFilterQu
ality quality, |
225 void* stor
age, size_t size) { | 225 void* stor
age, size_t size) { |
226 return SkInPlaceNewCheck<SkDefaultBitmapControllerState>(storage, size, bm,
inverse, quality); | 226 return SkInPlaceNewCheck<SkDefaultBitmapControllerState>(storage, size, bm,
inverse, quality); |
227 } | 227 } |
228 | |
OLD | NEW |