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 1141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1152 | 1152 |
1153 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) { | 1153 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) { |
1154 // We don't have local coords in this case and have previously set the t
ransform | 1154 // We don't have local coords in this case and have previously set the t
ransform |
1155 // matrices directly on the texture processor. | 1155 // matrices directly on the texture processor. |
1156 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect); | 1156 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect); |
1157 } else { | 1157 } else { |
1158 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintR
ect); | 1158 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintR
ect); |
1159 } | 1159 } |
1160 } | 1160 } |
1161 | 1161 |
1162 bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture, | |
1163 int width, int height, | |
1164 const SkImageFilter* filter, | |
1165 const SkImageFilter::Context& ctx, | |
1166 SkBitmap* result, SkIPoint* offset) { | |
1167 ASSERT_SINGLE_OWNER | |
1168 SkASSERT(filter); | |
1169 | |
1170 SkImageFilter::DeviceProxy proxy(this); | |
1171 | |
1172 if (filter->canFilterImageGPU()) { | |
1173 SkBitmap bm; | |
1174 GrWrapTextureInBitmap(texture, width, height, false, &bm); | |
1175 return filter->filterImageGPUDeprecated(&proxy, bm, ctx, result, offset)
; | |
1176 } else { | |
1177 return false; | |
1178 } | |
1179 } | |
1180 | |
1181 void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, | 1162 void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
1182 int left, int top, const SkPaint& paint) { | 1163 int left, int top, const SkPaint& paint) { |
1183 ASSERT_SINGLE_OWNER | 1164 ASSERT_SINGLE_OWNER |
1184 // drawSprite is defined to be in device coords. | 1165 // drawSprite is defined to be in device coords. |
1185 CHECK_SHOULD_DRAW(draw); | 1166 CHECK_SHOULD_DRAW(draw); |
1186 | 1167 |
1187 SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); | 1168 SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); |
1188 if (!bitmap.getTexture() && !bitmap.readyToDraw()) { | 1169 if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
1189 return; | 1170 return; |
1190 } | 1171 } |
1191 | 1172 |
1192 int offX = bitmap.pixelRefOrigin().fX; | 1173 int offX = bitmap.pixelRefOrigin().fX; |
1193 int offY = bitmap.pixelRefOrigin().fY; | 1174 int offY = bitmap.pixelRefOrigin().fY; |
1194 int w = bitmap.width(); | 1175 int w = bitmap.width(); |
1195 int h = bitmap.height(); | 1176 int h = bitmap.height(); |
1196 | 1177 |
1197 GrTexture* texture; | 1178 GrTexture* texture; |
1198 // draw sprite neither filters nor tiles. | 1179 // draw sprite neither filters nor tiles. |
1199 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &t
exture); | 1180 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &t
exture); |
1200 if (!texture) { | 1181 if (!texture) { |
1201 return; | 1182 return; |
1202 } | 1183 } |
1203 | 1184 |
1204 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType(); | 1185 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType(); |
1205 | 1186 |
1206 SkImageFilter* filter = paint.getImageFilter(); | 1187 SkASSERT(!paint.getImageFilter()); |
1207 // This bitmap will own the filtered result as a texture. | |
1208 SkBitmap filteredBitmap; | |
1209 | |
1210 if (filter) { | |
1211 SkIPoint offset = SkIPoint::Make(0, 0); | |
1212 SkMatrix matrix(*draw.fMatrix); | |
1213 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top)); | |
1214 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-left, -top); | |
1215 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache()); | |
1216 // This cache is transient, and is freed (along with all its contained | |
1217 // textures) when it goes out of scope. | |
1218 SkImageFilter::Context ctx(matrix, clipBounds, cache); | |
1219 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredB
itmap, | |
1220 &offset)) { | |
1221 texture = (GrTexture*) filteredBitmap.getTexture(); | |
1222 offX = filteredBitmap.pixelRefOrigin().fX; | |
1223 offY = filteredBitmap.pixelRefOrigin().fY; | |
1224 w = filteredBitmap.width(); | |
1225 h = filteredBitmap.height(); | |
1226 left += offset.x(); | |
1227 top += offset.y(); | |
1228 } else { | |
1229 return; | |
1230 } | |
1231 SkASSERT(!GrPixelConfigIsAlphaOnly(texture->config())); | |
1232 alphaOnly = false; | |
1233 } | |
1234 | 1188 |
1235 GrPaint grPaint; | 1189 GrPaint grPaint; |
1236 SkAutoTUnref<const GrFragmentProcessor> fp( | 1190 SkAutoTUnref<const GrFragmentProcessor> fp( |
1237 GrSimpleTextureEffect::Create(texture, SkMatrix::I())); | 1191 GrSimpleTextureEffect::Create(texture, SkMatrix::I())); |
1238 if (alphaOnly) { | 1192 if (alphaOnly) { |
1239 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp)); | 1193 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp)); |
1240 } else { | 1194 } else { |
1241 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp)); | 1195 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp)); |
1242 } | 1196 } |
1243 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, | 1197 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1359 GrRenderTarget* devRT = dev->accessRenderTarget(); | 1313 GrRenderTarget* devRT = dev->accessRenderTarget(); |
1360 GrTexture* devTex; | 1314 GrTexture* devTex; |
1361 if (nullptr == (devTex = devRT->asTexture())) { | 1315 if (nullptr == (devTex = devRT->asTexture())) { |
1362 return; | 1316 return; |
1363 } | 1317 } |
1364 | 1318 |
1365 const SkImageInfo ii = dev->imageInfo(); | 1319 const SkImageInfo ii = dev->imageInfo(); |
1366 int w = ii.width(); | 1320 int w = ii.width(); |
1367 int h = ii.height(); | 1321 int h = ii.height(); |
1368 | 1322 |
1369 SkImageFilter* filter = paint.getImageFilter(); | 1323 SkASSERT(!paint.getImageFilter()); |
1370 // This bitmap will own the filtered result as a texture. | |
1371 SkBitmap filteredBitmap; | |
1372 | |
1373 if (filter) { | |
1374 SkIPoint offset = SkIPoint::Make(0, 0); | |
1375 SkMatrix matrix(*draw.fMatrix); | |
1376 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); | |
1377 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-x, -y); | |
1378 // This cache is transient, and is freed (along with all its contained | |
1379 // textures) when it goes out of scope. | |
1380 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache()); | |
1381 SkImageFilter::Context ctx(matrix, clipBounds, cache); | |
1382 if (this->filterTexture(fContext, devTex, device->width(), device->heigh
t(), | |
1383 filter, ctx, &filteredBitmap, &offset)) { | |
1384 devTex = filteredBitmap.getTexture(); | |
1385 w = filteredBitmap.width(); | |
1386 h = filteredBitmap.height(); | |
1387 x += offset.fX; | |
1388 y += offset.fY; | |
1389 } else { | |
1390 return; | |
1391 } | |
1392 } | |
1393 | 1324 |
1394 GrPaint grPaint; | 1325 GrPaint grPaint; |
1395 SkAutoTUnref<const GrFragmentProcessor> fp( | 1326 SkAutoTUnref<const GrFragmentProcessor> fp( |
1396 GrSimpleTextureEffect::Create(devTex, SkMatrix::I())); | 1327 GrSimpleTextureEffect::Create(devTex, SkMatrix::I())); |
1397 if (GrPixelConfigIsAlphaOnly(devTex->config())) { | 1328 if (GrPixelConfigIsAlphaOnly(devTex->config())) { |
1398 // Can this happen? | 1329 // Can this happen? |
1399 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp)); | 1330 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp)); |
1400 } else { | 1331 } else { |
1401 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp)); | 1332 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp)); |
1402 } | 1333 } |
1403 | 1334 |
1404 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, | 1335 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, |
1405 this->surfaceProps().isGammaCorrect(), &g
rPaint)) { | 1336 this->surfaceProps().isGammaCorrect(), &g
rPaint)) { |
1406 return; | 1337 return; |
1407 } | 1338 } |
1408 | 1339 |
1409 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x), | 1340 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x), |
1410 SkIntToScalar(y), | 1341 SkIntToScalar(y), |
1411 SkIntToScalar(w), | 1342 SkIntToScalar(w), |
1412 SkIntToScalar(h)); | 1343 SkIntToScalar(h)); |
1413 | 1344 |
1414 // The device being drawn may not fill up its texture (e.g. saveLayer uses a
pproximate | 1345 // The device being drawn may not fill up its texture (e.g. saveLayer uses a
pproximate |
1415 // scratch texture). | 1346 // scratch texture). |
1416 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(), | 1347 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(), |
1417 SK_Scalar1 * h / devTex->height()); | 1348 SK_Scalar1 * h / devTex->height()); |
1418 | 1349 |
1419 fDrawContext->fillRectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect
); | 1350 fDrawContext->fillRectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect
); |
1420 } | 1351 } |
1421 | 1352 |
1422 bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) { | |
1423 ASSERT_SINGLE_OWNER | |
1424 return filter->canFilterImageGPU(); | |
1425 } | |
1426 | |
1427 bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src, | |
1428 const SkImageFilter::Context& ctx, | |
1429 SkBitmap* result, SkIPoint* offset) { | |
1430 ASSERT_SINGLE_OWNER | |
1431 // want explicitly our impl, so guard against a subclass of us overriding it | |
1432 if (!this->SkGpuDevice::canHandleImageFilter(filter)) { | |
1433 return false; | |
1434 } | |
1435 | |
1436 SkAutoLockPixels alp(src, !src.getTexture()); | |
1437 if (!src.getTexture() && !src.readyToDraw()) { | |
1438 return false; | |
1439 } | |
1440 | |
1441 GrTexture* texture; | |
1442 // We assume here that the filter will not attempt to tile the src. Otherwis
e, this cache lookup | |
1443 // must be pushed upstack. | |
1444 AutoBitmapTexture abt(fContext, src, GrTextureParams::ClampNoFilter(), &text
ure); | |
1445 if (!texture) { | |
1446 return false; | |
1447 } | |
1448 | |
1449 return this->filterTexture(fContext, texture, src.width(), src.height(), | |
1450 filter, ctx, result, offset); | |
1451 } | |
1452 | |
1453 void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x
, SkScalar y, | 1353 void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x
, SkScalar y, |
1454 const SkPaint& paint) { | 1354 const SkPaint& paint) { |
1455 ASSERT_SINGLE_OWNER | 1355 ASSERT_SINGLE_OWNER |
1456 SkMatrix viewMatrix = *draw.fMatrix; | 1356 SkMatrix viewMatrix = *draw.fMatrix; |
1457 viewMatrix.preTranslate(x, y); | 1357 viewMatrix.preTranslate(x, y); |
1458 if (as_IB(image)->peekTexture()) { | 1358 if (as_IB(image)->peekTexture()) { |
1459 CHECK_SHOULD_DRAW(draw); | 1359 CHECK_SHOULD_DRAW(draw); |
1460 GrImageTextureAdjuster adjuster(as_IB(image)); | 1360 GrImageTextureAdjuster adjuster(as_IB(image)); |
1461 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_S
rcRectConstraint, | 1361 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_S
rcRectConstraint, |
1462 viewMatrix, fClip, paint); | 1362 viewMatrix, fClip, paint); |
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1929 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering); | 1829 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering); |
1930 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled); | 1830 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled); |
1931 GrLayerHoister::End(fContext); | 1831 GrLayerHoister::End(fContext); |
1932 | 1832 |
1933 return true; | 1833 return true; |
1934 #else | 1834 #else |
1935 return false; | 1835 return false; |
1936 #endif | 1836 #endif |
1937 } | 1837 } |
1938 | 1838 |
1939 SkImageFilter::Cache* SkGpuDevice::NewImageFilterCache() { | |
1940 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize); | |
1941 } | |
1942 | |
1943 SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() { | 1839 SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() { |
1944 ASSERT_SINGLE_OWNER | 1840 ASSERT_SINGLE_OWNER |
1945 // We always return a transient cache, so it is freed after each | 1841 // We always return a transient cache, so it is freed after each |
1946 // filter traversal. | 1842 // filter traversal. |
1947 return SkGpuDevice::NewImageFilterCache(); | 1843 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize); |
1948 } | 1844 } |
1949 | 1845 |
1950 #endif | 1846 #endif |
OLD | NEW |