OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 "SkGpuDevice.h" | 8 #include "SkGpuDevice.h" |
9 | 9 |
10 #include "GrBlurUtils.h" | 10 #include "GrBlurUtils.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "SkImageFilter.h" | 26 #include "SkImageFilter.h" |
27 #include "SkImageFilterCache.h" | 27 #include "SkImageFilterCache.h" |
28 #include "SkMaskFilter.h" | 28 #include "SkMaskFilter.h" |
29 #include "SkNinePatchIter.h" | 29 #include "SkNinePatchIter.h" |
30 #include "SkPathEffect.h" | 30 #include "SkPathEffect.h" |
31 #include "SkPicture.h" | 31 #include "SkPicture.h" |
32 #include "SkPictureData.h" | 32 #include "SkPictureData.h" |
33 #include "SkRasterClip.h" | 33 #include "SkRasterClip.h" |
34 #include "SkRRect.h" | 34 #include "SkRRect.h" |
35 #include "SkRecord.h" | 35 #include "SkRecord.h" |
| 36 #include "SkSpecialImage.h" |
36 #include "SkStroke.h" | 37 #include "SkStroke.h" |
37 #include "SkSurface.h" | 38 #include "SkSurface.h" |
38 #include "SkSurface_Gpu.h" | 39 #include "SkSurface_Gpu.h" |
39 #include "SkTLazy.h" | 40 #include "SkTLazy.h" |
40 #include "SkUtils.h" | 41 #include "SkUtils.h" |
41 #include "SkVertState.h" | 42 #include "SkVertState.h" |
42 #include "SkXfermode.h" | 43 #include "SkXfermode.h" |
43 #include "batches/GrRectBatchFactory.h" | 44 #include "batches/GrRectBatchFactory.h" |
44 #include "effects/GrBicubicEffect.h" | 45 #include "effects/GrBicubicEffect.h" |
45 #include "effects/GrDashingEffect.h" | 46 #include "effects/GrDashingEffect.h" |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 // Fall back from whatever ct was to default of kRGBA or kBGRA which is
aliased as kN32 | 224 // Fall back from whatever ct was to default of kRGBA or kBGRA which is
aliased as kN32 |
224 ct = kN32_SkColorType; | 225 ct = kN32_SkColorType; |
225 } | 226 } |
226 | 227 |
227 GrPixelConfig config = SkImageInfo2GrPixelConfig(ct, at, cs, *context->caps(
)); | 228 GrPixelConfig config = SkImageInfo2GrPixelConfig(ct, at, cs, *context->caps(
)); |
228 | 229 |
229 return context->newDrawContext(SkBackingFit::kExact, // Why ex
act? | 230 return context->newDrawContext(SkBackingFit::kExact, // Why ex
act? |
230 origInfo.width(), origInfo.height(), | 231 origInfo.width(), origInfo.height(), |
231 config, sampleCount, | 232 config, sampleCount, |
232 kDefault_GrSurfaceOrigin, surfaceProps, budge
ted); | 233 kDefault_GrSurfaceOrigin, surfaceProps, budge
ted); |
233 | |
234 } | 234 } |
235 | 235 |
236 // This method ensures that we always have a texture-backed "bitmap" when we fin
ally | 236 sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(const SkDraw& draw, |
237 // call through to the base impl so that the image filtering code will take the | 237 SkSpecialImage* srcImg, |
238 // gpu-specific paths. This mirrors SkCanvas::internalDrawDevice (the other | 238 int left, int top, |
239 // use of SkImageFilter::filterImage) in that the source and dest will have | 239 SkIPoint* offset, |
240 // homogenous backing (e.g., raster or gpu). | 240 const SkImageFilter* filter) { |
| 241 SkASSERT(srcImg->isTextureBacked()); |
| 242 SkASSERT(filter); |
| 243 |
| 244 SkMatrix matrix = *draw.fMatrix; |
| 245 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top)); |
| 246 const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-left, -top); |
| 247 SkAutoTUnref<SkImageFilterCache> cache(this->getImageFilterCache()); |
| 248 SkImageFilter::Context ctx(matrix, clipBounds, cache.get()); |
| 249 |
| 250 return filter->filterImage(srcImg, ctx, offset); |
| 251 } |
| 252 |
| 253 |
241 void SkGpuDevice::drawSpriteWithFilter(const SkDraw& draw, const SkBitmap& bitma
p, | 254 void SkGpuDevice::drawSpriteWithFilter(const SkDraw& draw, const SkBitmap& bitma
p, |
242 int x, int y, const SkPaint& paint) { | 255 int left, int top, const SkPaint& paint)
{ |
243 ASSERT_SINGLE_OWNER | 256 ASSERT_SINGLE_OWNER |
| 257 CHECK_SHOULD_DRAW(draw); |
244 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpriteWithFilter", fConte
xt); | 258 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpriteWithFilter", fConte
xt); |
245 | 259 |
246 if (fContext->abandoned()) { | 260 SkASSERT(paint.getImageFilter()); |
247 return; | 261 this->drawSprite(draw, bitmap, left, top, paint); |
248 } | |
249 | |
250 if (bitmap.getTexture()) { | |
251 INHERITED::drawSpriteWithFilter(draw, bitmap, x, y, paint); | |
252 return; | |
253 } | |
254 | |
255 SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); | |
256 if (!bitmap.getTexture() && !bitmap.readyToDraw()) { | |
257 return; | |
258 } | |
259 | |
260 GrTexture* texture; | |
261 // draw sprite neither filters nor tiles. | |
262 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), | |
263 SkSourceGammaTreatment::kRespect, &texture); | |
264 if (!texture) { | |
265 return; | |
266 } | |
267 | |
268 SkBitmap newBitmap; | |
269 | |
270 GrWrapTextureInBitmap(texture, texture->width(), texture->height(), | |
271 bitmap.isOpaque(), &newBitmap); | |
272 | |
273 INHERITED::drawSpriteWithFilter(draw, newBitmap, x, y, paint); | |
274 } | 262 } |
275 | 263 |
276 /////////////////////////////////////////////////////////////////////////////// | 264 /////////////////////////////////////////////////////////////////////////////// |
277 | 265 |
278 bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size
_t dstRowBytes, | 266 bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size
_t dstRowBytes, |
279 int x, int y) { | 267 int x, int y) { |
280 ASSERT_SINGLE_OWNER | 268 ASSERT_SINGLE_OWNER |
281 | 269 |
282 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src p
ixels | 270 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src p
ixels |
283 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps())
; | 271 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps())
; |
(...skipping 942 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1226 // matrices directly on the texture processor. | 1214 // matrices directly on the texture processor. |
1227 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect); | 1215 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect); |
1228 } else { | 1216 } else { |
1229 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintR
ect); | 1217 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintR
ect); |
1230 } | 1218 } |
1231 } | 1219 } |
1232 | 1220 |
1233 void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, | 1221 void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
1234 int left, int top, const SkPaint& paint) { | 1222 int left, int top, const SkPaint& paint) { |
1235 ASSERT_SINGLE_OWNER | 1223 ASSERT_SINGLE_OWNER |
1236 // drawSprite is defined to be in device coords. | |
1237 CHECK_SHOULD_DRAW(draw); | 1224 CHECK_SHOULD_DRAW(draw); |
| 1225 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext); |
1238 | 1226 |
1239 SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); | 1227 if (fContext->abandoned()) { |
1240 if (!bitmap.getTexture() && !bitmap.readyToDraw()) { | |
1241 return; | 1228 return; |
1242 } | 1229 } |
1243 | 1230 |
1244 int offX = bitmap.pixelRefOrigin().fX; | 1231 sk_sp<GrTexture> texture = sk_ref_sp(bitmap.getTexture()); |
1245 int offY = bitmap.pixelRefOrigin().fY; | 1232 if (!texture) { |
1246 int w = bitmap.width(); | 1233 SkAutoLockPixels alp(bitmap, true); |
1247 int h = bitmap.height(); | 1234 if (!bitmap.readyToDraw()) { |
| 1235 return; |
| 1236 } |
1248 | 1237 |
1249 GrTexture* texture; | 1238 // draw sprite neither filters nor tiles. |
1250 // draw sprite neither filters nor tiles. | 1239 texture.reset(GrRefCachedBitmapTexture(fContext, bitmap, |
1251 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), | 1240 GrTextureParams::ClampNoFilter(), |
1252 SkSourceGammaTreatment::kRespect, &texture); | 1241 SkSourceGammaTreatment::kRespect)
); |
1253 if (!texture) { | 1242 if (!texture) { |
1254 return; | 1243 return; |
| 1244 } |
1255 } | 1245 } |
1256 | 1246 |
1257 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType(); | 1247 SkIRect srcRect = SkIRect::MakeXYWH(bitmap.pixelRefOrigin().fX, |
| 1248 bitmap.pixelRefOrigin().fY, |
| 1249 bitmap.width(), |
| 1250 bitmap.height()); |
1258 | 1251 |
1259 SkASSERT(!paint.getImageFilter()); | 1252 sk_sp<SkSpecialImage> srcImg(SkSpecialImage::MakeFromGpu(srcRect, |
| 1253 bitmap.getGeneratio
nID(), |
| 1254 std::move(texture),
|
| 1255 &fDrawContext->surf
aceProps())); |
| 1256 |
| 1257 this->drawSpecial(draw, srcImg.get(), left, top, paint); |
| 1258 } |
| 1259 |
| 1260 |
| 1261 void SkGpuDevice::drawSpecial(const SkDraw& draw, |
| 1262 SkSpecialImage* special1, |
| 1263 int left, int top, |
| 1264 const SkPaint& paint) { |
| 1265 |
| 1266 SkIPoint offset = { 0, 0 }; |
| 1267 |
| 1268 sk_sp<SkSpecialImage> result; |
| 1269 if (paint.getImageFilter()) { |
| 1270 result = this->filterTexture(draw, special1, left, top, |
| 1271 &offset, |
| 1272 paint.getImageFilter()); |
| 1273 if (!result) { |
| 1274 return; |
| 1275 } |
| 1276 } else { |
| 1277 result = sk_ref_sp(special1); |
| 1278 } |
| 1279 |
| 1280 SkASSERT(result->isTextureBacked()); |
| 1281 sk_sp<GrTexture> texture = result->asTextureRef(fContext); |
| 1282 |
| 1283 SkPaint tmpUnfiltered(paint); |
| 1284 tmpUnfiltered.setImageFilter(nullptr); |
| 1285 |
| 1286 bool alphaOnly = kAlpha_8_GrPixelConfig == texture->config(); |
1260 | 1287 |
1261 GrPaint grPaint; | 1288 GrPaint grPaint; |
1262 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture, SkMatrix:
:I())); | 1289 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture.get(), SkM
atrix::I())); |
1263 if (alphaOnly) { | 1290 if (alphaOnly) { |
1264 fp = GrFragmentProcessor::MulOutputByInputUnpremulColor(std::move(fp)); | 1291 fp = GrFragmentProcessor::MulOutputByInputUnpremulColor(std::move(fp)); |
1265 } else { | 1292 } else { |
1266 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp)); | 1293 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp)); |
1267 } | 1294 } |
1268 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, std::move(fp), | 1295 if (!SkPaintToGrPaintReplaceShader(this->context(), tmpUnfiltered, std::move
(fp), |
1269 this->surfaceProps().isGammaCorrect(), &g
rPaint)) { | 1296 this->surfaceProps().isGammaCorrect(), &g
rPaint)) { |
1270 return; | 1297 return; |
1271 } | 1298 } |
1272 | 1299 |
| 1300 const SkIRect& subset = result->subset(); |
| 1301 |
1273 fDrawContext->fillRectToRect(fClip, | 1302 fDrawContext->fillRectToRect(fClip, |
1274 grPaint, | 1303 grPaint, |
1275 SkMatrix::I(), | 1304 SkMatrix::I(), |
1276 SkRect::MakeXYWH(SkIntToScalar(left), | 1305 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX
, top + offset.fY, |
1277 SkIntToScalar(top), | 1306 subset.width(),
subset.height())), |
1278 SkIntToScalar(w), | 1307 SkRect::MakeXYWH(SkIntToScalar(subset.fLeft) /
texture->width(), |
1279 SkIntToScalar(h)), | 1308 SkIntToScalar(subset.fTop) / t
exture->height(), |
1280 SkRect::MakeXYWH(SkIntToScalar(offX) / texture-
>width(), | 1309 SkIntToScalar(subset.width())
/ texture->width(), |
1281 SkIntToScalar(offY) / texture-
>height(), | 1310 SkIntToScalar(subset.height())
/ texture->height())); |
1282 SkIntToScalar(w) / texture->wi
dth(), | |
1283 SkIntToScalar(h) / texture->he
ight())); | |
1284 } | 1311 } |
1285 | 1312 |
1286 void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap, | 1313 void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap, |
1287 const SkRect* src, const SkRect& origDst, | 1314 const SkRect* src, const SkRect& origDst, |
1288 const SkPaint& paint, SkCanvas::SrcRectConstrai
nt constraint) { | 1315 const SkPaint& paint, SkCanvas::SrcRectConstrai
nt constraint) { |
1289 ASSERT_SINGLE_OWNER | 1316 ASSERT_SINGLE_OWNER |
1290 CHECK_SHOULD_DRAW(draw); | 1317 CHECK_SHOULD_DRAW(draw); |
1291 if (bitmap.getTexture()) { | 1318 if (bitmap.getTexture()) { |
1292 GrBitmapTextureAdjuster adjuster(&bitmap); | 1319 GrBitmapTextureAdjuster adjuster(&bitmap); |
1293 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fM
atrix, fClip, | 1320 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fM
atrix, fClip, |
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1835 } | 1862 } |
1836 | 1863 |
1837 SkImageFilterCache* SkGpuDevice::getImageFilterCache() { | 1864 SkImageFilterCache* SkGpuDevice::getImageFilterCache() { |
1838 ASSERT_SINGLE_OWNER | 1865 ASSERT_SINGLE_OWNER |
1839 // We always return a transient cache, so it is freed after each | 1866 // We always return a transient cache, so it is freed after each |
1840 // filter traversal. | 1867 // filter traversal. |
1841 return SkImageFilterCache::Create(kDefaultImageFilterCacheSize); | 1868 return SkImageFilterCache::Create(kDefaultImageFilterCacheSize); |
1842 } | 1869 } |
1843 | 1870 |
1844 #endif | 1871 #endif |
OLD | NEW |