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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 do { \ | 60 do { \ |
61 if (gShouldDrawProc && !gShouldDrawProc()) return; \ | 61 if (gShouldDrawProc && !gShouldDrawProc()) return; \ |
62 this->prepareDraw(draw); \ | 62 this->prepareDraw(draw); \ |
63 } while (0) | 63 } while (0) |
64 #else | 64 #else |
65 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw) | 65 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw) |
66 #endif | 66 #endif |
67 | 67 |
68 /////////////////////////////////////////////////////////////////////////////// | 68 /////////////////////////////////////////////////////////////////////////////// |
69 | 69 |
70 // Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this | 70 // Helper for turning a bitmap into a texture. It creates a cached texture |
71 // just accesses the backing GrTexture. Otherwise, it creates a cached texture | |
72 // representation and releases it in the destructor. | 71 // representation and releases it in the destructor. |
73 class AutoBitmapTexture : public SkNoncopyable { | 72 class AutoBitmapTexture : public SkNoncopyable { |
bsalomon
2016/07/22 18:06:49
Can we just replace this whole thing with
sk_sp<G
reed1
2016/07/22 21:46:35
Done.
| |
74 public: | 73 public: |
75 AutoBitmapTexture() {} | 74 AutoBitmapTexture() {} |
76 | 75 |
77 AutoBitmapTexture(GrContext* context, | 76 AutoBitmapTexture(GrContext* context, |
78 const SkBitmap& bitmap, | 77 const SkBitmap& bitmap, |
79 const GrTextureParams& params, | 78 const GrTextureParams& params, |
80 SkSourceGammaTreatment gammaTreatment, | 79 SkSourceGammaTreatment gammaTreatment, |
81 GrTexture** texture) { | 80 GrTexture** texture) { |
82 SkASSERT(texture); | 81 SkASSERT(texture); |
83 *texture = this->set(context, bitmap, params, gammaTreatment); | 82 *texture = this->set(context, bitmap, params, gammaTreatment); |
84 } | 83 } |
85 | 84 |
86 GrTexture* set(GrContext* context, | 85 GrTexture* set(GrContext* context, |
87 const SkBitmap& bitmap, | 86 const SkBitmap& bitmap, |
88 const GrTextureParams& params, | 87 const GrTextureParams& params, |
89 SkSourceGammaTreatment gammaTreatment) { | 88 SkSourceGammaTreatment gammaTreatment) { |
90 // Either get the texture directly from the bitmap, or else use the cach e and | 89 // Use the cache and remember to unref it. |
91 // remember to unref it. | 90 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params, gammaTr eatment)); |
92 if (GrTexture* bmpTexture = bitmap.getTexture()) { | 91 return fTexture.get(); |
93 fTexture.reset(nullptr); | |
94 return bmpTexture; | |
95 } else { | |
96 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params, gam maTreatment)); | |
97 return fTexture.get(); | |
98 } | |
99 } | 92 } |
100 | 93 |
101 private: | 94 private: |
102 SkAutoTUnref<GrTexture> fTexture; | 95 SkAutoTUnref<GrTexture> fTexture; |
103 }; | 96 }; |
104 | 97 |
105 /////////////////////////////////////////////////////////////////////////////// | 98 /////////////////////////////////////////////////////////////////////////////// |
106 | 99 |
107 /** Checks that the alpha type is legal and gets constructor flags. Returns fals e if device creation | 100 /** Checks that the alpha type is legal and gets constructor flags. Returns fals e if device creation |
108 should fail. */ | 101 should fail. */ |
(...skipping 720 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
829 } | 822 } |
830 | 823 |
831 bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap, | 824 bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap, |
832 const SkMatrix& viewMatrix, | 825 const SkMatrix& viewMatrix, |
833 const GrTextureParams& params, | 826 const GrTextureParams& params, |
834 const SkRect* srcRectPtr, | 827 const SkRect* srcRectPtr, |
835 int maxTileSize, | 828 int maxTileSize, |
836 int* tileSize, | 829 int* tileSize, |
837 SkIRect* clippedSrcRect) const { | 830 SkIRect* clippedSrcRect) const { |
838 ASSERT_SINGLE_OWNER | 831 ASSERT_SINGLE_OWNER |
839 // if bitmap is explictly texture backed then just use the texture | |
840 if (bitmap.getTexture()) { | |
841 return false; | |
842 } | |
843 | |
844 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params, | 832 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params, |
845 srcRectPtr, maxTileSize, tileSize, clippedSrc Rect); | 833 srcRectPtr, maxTileSize, tileSize, clippedSrc Rect); |
846 } | 834 } |
847 | 835 |
848 bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr , | 836 bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr , |
849 SkCanvas::SrcRectConstraint constraint, SkFilt erQuality quality, | 837 SkCanvas::SrcRectConstraint constraint, SkFilt erQuality quality, |
850 const SkMatrix& viewMatrix) const { | 838 const SkMatrix& viewMatrix) const { |
851 ASSERT_SINGLE_OWNER | 839 ASSERT_SINGLE_OWNER |
852 // if image is explictly texture backed then just use the texture | 840 // if image is explictly texture backed then just use the texture |
853 if (as_IB(image)->peekTexture()) { | 841 if (as_IB(image)->peekTexture()) { |
(...skipping 26 matching lines...) Expand all Loading... | |
880 } | 868 } |
881 | 869 |
882 void SkGpuDevice::drawBitmap(const SkDraw& origDraw, | 870 void SkGpuDevice::drawBitmap(const SkDraw& origDraw, |
883 const SkBitmap& bitmap, | 871 const SkBitmap& bitmap, |
884 const SkMatrix& m, | 872 const SkMatrix& m, |
885 const SkPaint& paint) { | 873 const SkPaint& paint) { |
886 ASSERT_SINGLE_OWNER | 874 ASSERT_SINGLE_OWNER |
887 CHECK_SHOULD_DRAW(origDraw); | 875 CHECK_SHOULD_DRAW(origDraw); |
888 SkMatrix viewMatrix; | 876 SkMatrix viewMatrix; |
889 viewMatrix.setConcat(*origDraw.fMatrix, m); | 877 viewMatrix.setConcat(*origDraw.fMatrix, m); |
890 if (bitmap.getTexture()) { | 878 |
891 GrBitmapTextureAdjuster adjuster(&bitmap); | |
892 // We can use kFast here because we know texture-backed bitmaps don't su pport extractSubset. | |
893 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_S rcRectConstraint, | |
894 viewMatrix, fClip, paint); | |
895 return; | |
896 } | |
897 int maxTileSize = fContext->caps()->maxTileSize(); | 879 int maxTileSize = fContext->caps()->maxTileSize(); |
898 | 880 |
899 // The tile code path doesn't currently support AA, so if the paint asked fo r aa and we could | 881 // The tile code path doesn't currently support AA, so if the paint asked fo r aa and we could |
900 // draw untiled, then we bypass checking for tiling purely for optimization reasons. | 882 // draw untiled, then we bypass checking for tiling purely for optimization reasons. |
901 bool drawAA = !fDrawContext->isUnifiedMultisampled() && | 883 bool drawAA = !fDrawContext->isUnifiedMultisampled() && |
902 paint.isAntiAlias() && | 884 paint.isAntiAlias() && |
903 bitmap.width() <= maxTileSize && | 885 bitmap.width() <= maxTileSize && |
904 bitmap.height() <= maxTileSize; | 886 bitmap.height() <= maxTileSize; |
905 | 887 |
906 bool skipTileCheck = drawAA || paint.getMaskFilter(); | 888 bool skipTileCheck = drawAA || paint.getMaskFilter(); |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1083 const SkMatrix& viewMatrix, | 1065 const SkMatrix& viewMatrix, |
1084 const SkRect& srcRect, | 1066 const SkRect& srcRect, |
1085 const GrTextureParams& params, | 1067 const GrTextureParams& params, |
1086 const SkPaint& paint, | 1068 const SkPaint& paint, |
1087 SkCanvas::SrcRectConstraint constraint, | 1069 SkCanvas::SrcRectConstraint constraint, |
1088 bool bicubic, | 1070 bool bicubic, |
1089 bool needsTextureDomain) { | 1071 bool needsTextureDomain) { |
1090 // We should have already handled bitmaps larger than the max texture size. | 1072 // We should have already handled bitmaps larger than the max texture size. |
1091 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() && | 1073 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() && |
1092 bitmap.height() <= fContext->caps()->maxTextureSize()); | 1074 bitmap.height() <= fContext->caps()->maxTextureSize()); |
1093 // Unless the bitmap is inherently texture-backed, we should be respecting t he max tile size | 1075 // Unless the bitmap is inherently texture-backed, we should be respecting t he max tile size |
bsalomon
2016/07/22 18:06:49
Modify comment to remove language about texture-ba
reed1
2016/07/22 21:46:35
Done.
| |
1094 // by the time we get here. | 1076 // by the time we get here. |
1095 SkASSERT(bitmap.getTexture() || | 1077 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() && |
1096 (bitmap.width() <= fContext->caps()->maxTileSize() && | 1078 bitmap.height() <= fContext->caps()->maxTileSize()); |
1097 bitmap.height() <= fContext->caps()->maxTileSize())); | |
1098 | 1079 |
1099 GrTexture* texture; | 1080 GrTexture* texture; |
1100 SkSourceGammaTreatment gammaTreatment = this->surfaceProps().isGammaCorrect( ) | 1081 SkSourceGammaTreatment gammaTreatment = this->surfaceProps().isGammaCorrect( ) |
1101 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIgnore; | 1082 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIgnore; |
1102 AutoBitmapTexture abt(fContext, bitmap, params, gammaTreatment, &texture); | 1083 AutoBitmapTexture abt(fContext, bitmap, params, gammaTreatment, &texture); |
1103 if (nullptr == texture) { | 1084 if (nullptr == texture) { |
1104 return; | 1085 return; |
1105 } | 1086 } |
1106 | 1087 |
1107 SkColorSpace* dstColorSpace = nullptr; // XFORMTODO | 1088 SkColorSpace* dstColorSpace = nullptr; // XFORMTODO |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1185 void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, | 1166 void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
1186 int left, int top, const SkPaint& paint) { | 1167 int left, int top, const SkPaint& paint) { |
1187 ASSERT_SINGLE_OWNER | 1168 ASSERT_SINGLE_OWNER |
1188 CHECK_SHOULD_DRAW(draw); | 1169 CHECK_SHOULD_DRAW(draw); |
1189 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext); | 1170 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext); |
1190 | 1171 |
1191 if (fContext->abandoned()) { | 1172 if (fContext->abandoned()) { |
1192 return; | 1173 return; |
1193 } | 1174 } |
1194 | 1175 |
1195 sk_sp<GrTexture> texture = sk_ref_sp(bitmap.getTexture()); | 1176 sk_sp<GrTexture> texture; |
1196 if (!texture) { | 1177 { |
1197 SkAutoLockPixels alp(bitmap, true); | 1178 SkAutoLockPixels alp(bitmap, true); |
1198 if (!bitmap.readyToDraw()) { | 1179 if (!bitmap.readyToDraw()) { |
1199 return; | 1180 return; |
1200 } | 1181 } |
1201 | 1182 |
1202 // draw sprite neither filters nor tiles. | 1183 // draw sprite neither filters nor tiles. |
1203 texture.reset(GrRefCachedBitmapTexture(fContext, bitmap, | 1184 texture.reset(GrRefCachedBitmapTexture(fContext, bitmap, |
1204 GrTextureParams::ClampNoFilter(), | 1185 GrTextureParams::ClampNoFilter(), |
1205 SkSourceGammaTreatment::kRespect) ); | 1186 SkSourceGammaTreatment::kRespect) ); |
1206 if (!texture) { | 1187 if (!texture) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1275 SkIntToScalar(subset.fTop) / t exture->height(), | 1256 SkIntToScalar(subset.fTop) / t exture->height(), |
1276 SkIntToScalar(subset.width()) / texture->width(), | 1257 SkIntToScalar(subset.width()) / texture->width(), |
1277 SkIntToScalar(subset.height()) / texture->height())); | 1258 SkIntToScalar(subset.height()) / texture->height())); |
1278 } | 1259 } |
1279 | 1260 |
1280 void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap, | 1261 void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap, |
1281 const SkRect* src, const SkRect& origDst, | 1262 const SkRect* src, const SkRect& origDst, |
1282 const SkPaint& paint, SkCanvas::SrcRectConstrai nt constraint) { | 1263 const SkPaint& paint, SkCanvas::SrcRectConstrai nt constraint) { |
1283 ASSERT_SINGLE_OWNER | 1264 ASSERT_SINGLE_OWNER |
1284 CHECK_SHOULD_DRAW(draw); | 1265 CHECK_SHOULD_DRAW(draw); |
1285 if (bitmap.getTexture()) { | 1266 |
1286 GrBitmapTextureAdjuster adjuster(&bitmap); | |
1287 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fM atrix, fClip, | |
1288 paint); | |
1289 return; | |
1290 } | |
1291 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must | 1267 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must |
1292 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which | 1268 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which |
1293 // in turn requires knowing the src-to-dst mapping. If the src was clipped t o the bmp bounds | 1269 // in turn requires knowing the src-to-dst mapping. If the src was clipped t o the bmp bounds |
1294 // then we use the src-to-dst mapping to compute a new clipped dst rect. | 1270 // then we use the src-to-dst mapping to compute a new clipped dst rect. |
1295 const SkRect* dst = &origDst; | 1271 const SkRect* dst = &origDst; |
1296 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height()); | 1272 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height()); |
1297 // Compute matrix from the two rectangles | 1273 // Compute matrix from the two rectangles |
1298 if (!src) { | 1274 if (!src) { |
1299 src = &bmpBounds; | 1275 src = &bmpBounds; |
1300 } | 1276 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1359 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, para ms, paint, | 1335 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, para ms, paint, |
1360 constraint, tileSize, doBicubic); | 1336 constraint, tileSize, doBicubic); |
1361 return; | 1337 return; |
1362 } | 1338 } |
1363 } | 1339 } |
1364 GrBitmapTextureMaker maker(fContext, bitmap); | 1340 GrBitmapTextureMaker maker(fContext, bitmap); |
1365 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip , paint); | 1341 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip , paint); |
1366 } | 1342 } |
1367 | 1343 |
1368 sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) { | 1344 sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) { |
1369 SkASSERT(!bitmap.getTexture()); | |
1370 | |
1371 SkAutoLockPixels alp(bitmap, true); | 1345 SkAutoLockPixels alp(bitmap, true); |
1372 if (!bitmap.readyToDraw()) { | 1346 if (!bitmap.readyToDraw()) { |
1373 return nullptr; | 1347 return nullptr; |
1374 } | 1348 } |
1375 | 1349 |
1376 GrTexture* texture; | 1350 GrTexture* texture; |
1377 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), | 1351 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), |
1378 SkSourceGammaTreatment::kRespect, &texture); | 1352 SkSourceGammaTreatment::kRespect, &texture); |
1379 if (!texture) { | 1353 if (!texture) { |
1380 return nullptr; | 1354 return nullptr; |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1569 this->drawProducerNine(draw, &maker, center, dst, paint); | 1543 this->drawProducerNine(draw, &maker, center, dst, paint); |
1570 } else if (as_IB(image)->getROPixels(&bm)) { | 1544 } else if (as_IB(image)->getROPixels(&bm)) { |
1571 this->drawBitmapNine(draw, bm, center, dst, paint); | 1545 this->drawBitmapNine(draw, bm, center, dst, paint); |
1572 } | 1546 } |
1573 } | 1547 } |
1574 } | 1548 } |
1575 | 1549 |
1576 void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, con st SkIRect& center, | 1550 void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, con st SkIRect& center, |
1577 const SkRect& dst, const SkPaint& paint) { | 1551 const SkRect& dst, const SkPaint& paint) { |
1578 ASSERT_SINGLE_OWNER | 1552 ASSERT_SINGLE_OWNER |
1579 if (bitmap.getTexture()) { | 1553 GrBitmapTextureMaker maker(fContext, bitmap); |
1580 GrBitmapTextureAdjuster adjuster(&bitmap); | 1554 this->drawProducerNine(draw, &maker, center, dst, paint); |
1581 this->drawProducerNine(draw, &adjuster, center, dst, paint); | |
1582 } else { | |
1583 GrBitmapTextureMaker maker(fContext, bitmap); | |
1584 this->drawProducerNine(draw, &maker, center, dst, paint); | |
1585 } | |
1586 } | 1555 } |
1587 | 1556 |
1588 /////////////////////////////////////////////////////////////////////////////// | 1557 /////////////////////////////////////////////////////////////////////////////// |
1589 | 1558 |
1590 // must be in SkCanvas::VertexMode order | 1559 // must be in SkCanvas::VertexMode order |
1591 static const GrPrimitiveType gVertexMode2PrimitiveType[] = { | 1560 static const GrPrimitiveType gVertexMode2PrimitiveType[] = { |
1592 kTriangles_GrPrimitiveType, | 1561 kTriangles_GrPrimitiveType, |
1593 kTriangleStrip_GrPrimitiveType, | 1562 kTriangleStrip_GrPrimitiveType, |
1594 kTriangleFan_GrPrimitiveType, | 1563 kTriangleFan_GrPrimitiveType, |
1595 }; | 1564 }; |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1871 } | 1840 } |
1872 | 1841 |
1873 SkImageFilterCache* SkGpuDevice::getImageFilterCache() { | 1842 SkImageFilterCache* SkGpuDevice::getImageFilterCache() { |
1874 ASSERT_SINGLE_OWNER | 1843 ASSERT_SINGLE_OWNER |
1875 // We always return a transient cache, so it is freed after each | 1844 // We always return a transient cache, so it is freed after each |
1876 // filter traversal. | 1845 // filter traversal. |
1877 return SkImageFilterCache::Create(kDefaultImageFilterCacheSize); | 1846 return SkImageFilterCache::Create(kDefaultImageFilterCacheSize); |
1878 } | 1847 } |
1879 | 1848 |
1880 #endif | 1849 #endif |
OLD | NEW |