OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
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 "SkImageFilter.h" | 8 #include "SkImageFilter.h" |
9 | 9 |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 #include "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
12 #include "SkWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
13 #include "SkRect.h" | 13 #include "SkRect.h" |
14 #include "SkValidationUtils.h" | 14 #include "SkValidationUtils.h" |
15 #if SK_SUPPORT_GPU | 15 #if SK_SUPPORT_GPU |
16 #include "GrContext.h" | 16 #include "GrContext.h" |
17 #include "GrTexture.h" | 17 #include "SkGrPixelRef.h" |
18 #include "SkImageFilterUtils.h" | 18 #include "SkGr.h" |
19 #endif | 19 #endif |
20 | 20 |
21 SkImageFilter::SkImageFilter(int inputCount, SkImageFilter** inputs, const CropR
ect* cropRect) | 21 SkImageFilter::SkImageFilter(int inputCount, SkImageFilter** inputs, const CropR
ect* cropRect) |
22 : fInputCount(inputCount), | 22 : fInputCount(inputCount), |
23 fInputs(new SkImageFilter*[inputCount]), | 23 fInputs(new SkImageFilter*[inputCount]), |
24 fCropRect(cropRect ? *cropRect : CropRect(SkRect(), 0x0)) { | 24 fCropRect(cropRect ? *cropRect : CropRect(SkRect(), 0x0)) { |
25 for (int i = 0; i < inputCount; ++i) { | 25 for (int i = 0; i < inputCount; ++i) { |
26 fInputs[i] = inputs[i]; | 26 fInputs[i] = inputs[i]; |
27 SkSafeRef(fInputs[i]); | 27 SkSafeRef(fInputs[i]); |
28 } | 28 } |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 return false; | 139 return false; |
140 } | 140 } |
141 | 141 |
142 bool SkImageFilter::canFilterImageGPU() const { | 142 bool SkImageFilter::canFilterImageGPU() const { |
143 return this->asNewEffect(NULL, NULL, SkMatrix::I(), SkIRect()); | 143 return this->asNewEffect(NULL, NULL, SkMatrix::I(), SkIRect()); |
144 } | 144 } |
145 | 145 |
146 bool SkImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const SkMa
trix& ctm, | 146 bool SkImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const SkMa
trix& ctm, |
147 SkBitmap* result, SkIPoint* offset) const { | 147 SkBitmap* result, SkIPoint* offset) const { |
148 #if SK_SUPPORT_GPU | 148 #if SK_SUPPORT_GPU |
149 SkBitmap input; | 149 SkBitmap input = src; |
150 SkASSERT(fInputCount == 1); | 150 SkASSERT(fInputCount == 1); |
151 SkIPoint srcOffset = SkIPoint::Make(0, 0); | 151 SkIPoint srcOffset = SkIPoint::Make(0, 0); |
152 if (!SkImageFilterUtils::GetInputResultGPU(this->getInput(0), proxy, src, ct
m, &input, &srcOffset)) { | 152 if (this->getInput(0) && |
| 153 !this->getInput(0)->getInputResultGPU(proxy, src, ctm, &input, &srcOffse
t)) { |
153 return false; | 154 return false; |
154 } | 155 } |
155 GrTexture* srcTexture = input.getTexture(); | 156 GrTexture* srcTexture = input.getTexture(); |
156 SkIRect bounds; | 157 SkIRect bounds; |
157 src.getBounds(&bounds); | 158 src.getBounds(&bounds); |
158 bounds.offset(srcOffset); | 159 bounds.offset(srcOffset); |
159 if (!this->applyCropRect(&bounds, ctm)) { | 160 if (!this->applyCropRect(&bounds, ctm)) { |
160 return false; | 161 return false; |
161 } | 162 } |
162 SkRect srcRect = SkRect::Make(bounds); | 163 SkRect srcRect = SkRect::Make(bounds); |
(...skipping 18 matching lines...) Expand all Loading... |
181 SkMatrix matrix(ctm); | 182 SkMatrix matrix(ctm); |
182 matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.to
p())); | 183 matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.to
p())); |
183 this->asNewEffect(&effect, srcTexture, matrix, bounds); | 184 this->asNewEffect(&effect, srcTexture, matrix, bounds); |
184 SkASSERT(effect); | 185 SkASSERT(effect); |
185 SkAutoUnref effectRef(effect); | 186 SkAutoUnref effectRef(effect); |
186 GrPaint paint; | 187 GrPaint paint; |
187 paint.addColorEffect(effect); | 188 paint.addColorEffect(effect); |
188 context->drawRectToRect(paint, dstRect, srcRect); | 189 context->drawRectToRect(paint, dstRect, srcRect); |
189 | 190 |
190 SkAutoTUnref<GrTexture> resultTex(dst.detach()); | 191 SkAutoTUnref<GrTexture> resultTex(dst.detach()); |
191 SkImageFilterUtils::WrapTexture(resultTex, bounds.width(), bounds.height(),
result); | 192 WrapTexture(resultTex, bounds.width(), bounds.height(), result); |
192 return true; | 193 return true; |
193 #else | 194 #else |
194 return false; | 195 return false; |
195 #endif | 196 #endif |
196 } | 197 } |
197 | 198 |
198 bool SkImageFilter::applyCropRect(SkIRect* rect, const SkMatrix& matrix) const { | 199 bool SkImageFilter::applyCropRect(SkIRect* rect, const SkMatrix& matrix) const { |
199 SkRect cropRect; | 200 SkRect cropRect; |
200 matrix.mapRect(&cropRect, fCropRect.rect()); | 201 matrix.mapRect(&cropRect, fCropRect.rect()); |
201 SkIRect cropRectI; | 202 SkIRect cropRectI; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 return true; | 236 return true; |
236 } | 237 } |
237 | 238 |
238 bool SkImageFilter::asNewEffect(GrEffectRef**, GrTexture*, const SkMatrix&, cons
t SkIRect&) const { | 239 bool SkImageFilter::asNewEffect(GrEffectRef**, GrTexture*, const SkMatrix&, cons
t SkIRect&) const { |
239 return false; | 240 return false; |
240 } | 241 } |
241 | 242 |
242 bool SkImageFilter::asColorFilter(SkColorFilter**) const { | 243 bool SkImageFilter::asColorFilter(SkColorFilter**) const { |
243 return false; | 244 return false; |
244 } | 245 } |
| 246 |
| 247 #if SK_SUPPORT_GPU |
| 248 |
| 249 void SkImageFilter::WrapTexture(GrTexture* texture, int width, int height, SkBit
map* result) { |
| 250 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 251 result->setConfig(info); |
| 252 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); |
| 253 } |
| 254 |
| 255 bool SkImageFilter::getInputResultGPU(SkImageFilter::Proxy* proxy, |
| 256 const SkBitmap& src, const SkMatrix& ctm, |
| 257 SkBitmap* result, SkIPoint* offset) const
{ |
| 258 // Ensure that GrContext calls under filterImage and filterImageGPU below wi
ll see an identity |
| 259 // matrix with no clip and that the matrix, clip, and render target set befo
re this function was |
| 260 // called are restored before we return to the caller. |
| 261 GrContext* context = src.getTexture()->getContext(); |
| 262 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL); |
| 263 if (this->canFilterImageGPU()) { |
| 264 return this->filterImageGPU(proxy, src, ctm, result, offset); |
| 265 } else { |
| 266 if (this->filterImage(proxy, src, ctm, result, offset)) { |
| 267 if (!result->getTexture()) { |
| 268 SkImageInfo info; |
| 269 if (!result->asImageInfo(&info)) { |
| 270 return false; |
| 271 } |
| 272 GrTexture* resultTex = GrLockAndRefCachedBitmapTexture(context,
*result, NULL); |
| 273 result->setPixelRef(new SkGrPixelRef(info, resultTex))->unref(); |
| 274 GrUnlockAndUnrefCachedBitmapTexture(resultTex); |
| 275 } |
| 276 return true; |
| 277 } else { |
| 278 return false; |
| 279 } |
| 280 } |
| 281 } |
| 282 #endif |
OLD | NEW |