Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: src/gpu/SkGpuDevice.cpp

Issue 920513003: Make filters use SkImage instead of SkBitmap Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "GrBitmapTextContext.h" 10 #include "GrBitmapTextContext.h"
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 AutoBitmapTexture() {} 87 AutoBitmapTexture() {}
88 88
89 AutoBitmapTexture(GrContext* context, 89 AutoBitmapTexture(GrContext* context,
90 const SkBitmap& bitmap, 90 const SkBitmap& bitmap,
91 const GrTextureParams* params, 91 const GrTextureParams* params,
92 GrTexture** texture) { 92 GrTexture** texture) {
93 SkASSERT(texture); 93 SkASSERT(texture);
94 *texture = this->set(context, bitmap, params); 94 *texture = this->set(context, bitmap, params);
95 } 95 }
96 96
97 AutoBitmapTexture(GrContext* context,
98 const SkImage* image,
99 const GrTextureParams* params,
100 GrTexture** texture) {
101 SkASSERT(texture);
102 *texture = this->set(context, image, params);
103 }
104
97 GrTexture* set(GrContext* context, 105 GrTexture* set(GrContext* context,
98 const SkBitmap& bitmap, 106 const SkBitmap& bitmap,
99 const GrTextureParams* params) { 107 const GrTextureParams* params) {
100 // Either get the texture directly from the bitmap, or else use the cach e and 108 // Either get the texture directly from the bitmap, or else use the cach e and
101 // remember to unref it. 109 // remember to unref it.
102 if (GrTexture* bmpTexture = bitmap.getTexture()) { 110 if (GrTexture* bmpTexture = bitmap.getTexture()) {
103 fTexture.reset(NULL); 111 fTexture.reset(NULL);
104 return bmpTexture; 112 return bmpTexture;
105 } else { 113 } else {
106 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params)); 114 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
107 return fTexture.get(); 115 return fTexture.get();
108 } 116 }
109 } 117 }
110 118
119 GrTexture* set(GrContext* context,
120 const SkImage* image,
121 const GrTextureParams* params) {
122 if (GrTexture* bmpTexture = image->getTexture()) {
123 fTexture.reset(NULL);
124 return bmpTexture;
125 } else {
126 SkBitmap bitmap;
127 as_IB(image)->getROPixels(&bitmap);
128 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
129 return fTexture.get();
130 }
131 }
132
111 private: 133 private:
112 SkAutoTUnref<GrTexture> fTexture; 134 SkAutoTUnref<GrTexture> fTexture;
113 }; 135 };
114 136
115 /////////////////////////////////////////////////////////////////////////////// 137 ///////////////////////////////////////////////////////////////////////////////
116 138
117 struct GrSkDrawProcs : public SkDrawProcs { 139 struct GrSkDrawProcs : public SkDrawProcs {
118 public: 140 public:
119 GrContext* fContext; 141 GrContext* fContext;
120 GrTextContext* fTextContext; 142 GrTextContext* fTextContext;
(...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after
806 tempPaint.setAntiAlias(doAA); 828 tempPaint.setAntiAlias(doAA);
807 tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op); 829 tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
808 830
809 // Draw the mask into maskTexture with the path's top-left at the origin usi ng tempPaint. 831 // Draw the mask into maskTexture with the path's top-left at the origin usi ng tempPaint.
810 SkMatrix translate; 832 SkMatrix translate;
811 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop); 833 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
812 context->drawPath(mask->asRenderTarget(), tempPaint, translate, devPath, str okeInfo); 834 context->drawPath(mask->asRenderTarget(), tempPaint, translate, devPath, str okeInfo);
813 return mask; 835 return mask;
814 } 836 }
815 837
816 SkBitmap wrap_texture(GrTexture* texture) { 838 static SkImage* wrap_texture(GrTexture* texture) {
817 SkBitmap result; 839 SkBitmap result;
818 result.setInfo(texture->surfacePriv().info()); 840 result.setInfo(texture->surfacePriv().info());
819 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unre f(); 841 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unre f();
820 return result; 842 return SkNewImageFromBitmapTexture(result, texture->desc().fSampleCnt,
843 SkSurface::kYes_Budgeted);
821 } 844 }
822 845
823 }; 846 };
824 847
825 void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath, 848 void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
826 const SkPaint& paint, const SkMatrix* prePathMatrix, 849 const SkPaint& paint, const SkMatrix* prePathMatrix,
827 bool pathIsMutable) { 850 bool pathIsMutable) {
828 CHECK_FOR_ANNOTATION(paint); 851 CHECK_FOR_ANNOTATION(paint);
829 CHECK_SHOULD_DRAW(draw); 852 CHECK_SHOULD_DRAW(draw);
830 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext); 853 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after
1532 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor() ) : 1555 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor() ) :
1533 SkColor2GrColor(paint.getColor()); 1556 SkColor2GrColor(paint.getColor());
1534 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, f alse, &grPaint); 1557 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, f alse, &grPaint);
1535 1558
1536 fContext->drawNonAARectToRect(fRenderTarget, grPaint, viewMatrix, dstRect, p aintRect); 1559 fContext->drawNonAARectToRect(fRenderTarget, grPaint, viewMatrix, dstRect, p aintRect);
1537 } 1560 }
1538 1561
1539 bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture, 1562 bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
1540 const SkImageFilter* filter, 1563 const SkImageFilter* filter,
1541 const SkImageFilter::Context& ctx, 1564 const SkImageFilter::Context& ctx,
1542 SkBitmap* result, SkIPoint* offset) { 1565 SkAutoTUnref<const SkImage>& result, SkIPoint* o ffset) {
1543 SkASSERT(filter); 1566 SkASSERT(filter);
1544 1567
1545 // FIXME: plumb actual surface props such that we don't have to lie about th e flags here 1568 // FIXME: plumb actual surface props such that we don't have to lie about th e flags here
1546 // (https://code.google.com/p/skia/issues/detail?id=3148). 1569 // (https://code.google.com/p/skia/issues/detail?id=3148).
1547 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties(). pixelGeometry())); 1570 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties(). pixelGeometry()));
1548 1571
1549 if (filter->canFilterImageGPU()) { 1572 if (filter->canFilterImageGPU()) {
1550 // Set the clip wide open and the matrix to identity. 1573 // Set the clip wide open and the matrix to identity.
1551 GrContext::AutoWideOpenIdentityDraw awo(context); 1574 GrContext::AutoWideOpenIdentityDraw awo(context);
1552 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result , offset); 1575 SkAutoTUnref<const SkImage> src(wrap_texture(texture));
1576 return filter->filterImageGPU(&proxy, src, ctx, result, offset);
1553 } else { 1577 } else {
1554 return false; 1578 return false;
1555 } 1579 }
1556 } 1580 }
1557 1581
1558 void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, 1582 void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1559 int left, int top, const SkPaint& paint) { 1583 int left, int top, const SkPaint& paint) {
1560 // drawSprite is defined to be in device coords. 1584 // drawSprite is defined to be in device coords.
1561 CHECK_SHOULD_DRAW(draw); 1585 CHECK_SHOULD_DRAW(draw);
1562 1586
1563 SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); 1587 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1564 if (!bitmap.getTexture() && !bitmap.readyToDraw()) { 1588 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1565 return; 1589 return;
1566 } 1590 }
1567 1591
1568 int w = bitmap.width();
1569 int h = bitmap.height();
1570
1571 GrTexture* texture; 1592 GrTexture* texture;
1572 // draw sprite uses the default texture params 1593 // draw sprite uses the default texture params
1573 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture); 1594 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
1595 if (NULL != texture) {
1596 this->internalDrawSprite(draw, texture, left, top, bitmap.width(), bitma p.height(), paint);
1597 }
1598 }
1574 1599
1575 SkImageFilter* filter = paint.getImageFilter(); 1600 void SkGpuDevice::drawSprite(const SkDraw& draw, const SkImage& image,
1576 // This bitmap will own the filtered result as a texture. 1601 int left, int top, const SkPaint& paint) {
1577 SkBitmap filteredBitmap; 1602 // drawSprite is defined to be in device coords.
1603 CHECK_SHOULD_DRAW(draw);
1578 1604
1579 if (filter) { 1605 GrTexture* texture;
1606 // draw sprite uses the default texture params
1607 AutoBitmapTexture abt(fContext, &image, NULL, &texture);
1608 if (NULL != texture) {
1609 this->internalDrawSprite(draw, texture, left, top, image.width(), image. height(), paint);
1610 }
1611 }
1612
1613 void SkGpuDevice::internalDrawSprite(const SkDraw& draw, GrTexture* texture,
1614 int left, int top, int width, int height,
1615 const SkPaint& paint) {
1616
1617 SkAutoTUnref<GrTexture> src(SkRef(texture));
1618 if (SkImageFilter* filter = paint.getImageFilter()) {
1619 // This bitmap will own the filtered result as a texture.
1620 SkAutoTUnref<const SkImage> filteredImage;
1580 SkIPoint offset = SkIPoint::Make(0, 0); 1621 SkIPoint offset = SkIPoint::Make(0, 0);
1581 SkMatrix matrix(*draw.fMatrix); 1622 SkMatrix matrix(*draw.fMatrix);
1582 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top)); 1623 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
1583 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); 1624 SkIRect clipBounds = SkIRect::MakeWH(width, height);
1584 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache()); 1625 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
1585 // This cache is transient, and is freed (along with all its contained 1626 // This cache is transient, and is freed (along with all its contained
1586 // textures) when it goes out of scope. 1627 // textures) when it goes out of scope.
1587 SkImageFilter::Context ctx(matrix, clipBounds, cache); 1628 SkImageFilter::Context ctx(matrix, clipBounds, cache);
1588 if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap, 1629 if (this->filterTexture(fContext, texture, filter, ctx, filteredImage,
1589 &offset)) { 1630 &offset)) {
1590 texture = (GrTexture*) filteredBitmap.getTexture(); 1631 src.reset(SkRef(filteredImage->getTexture()));
1591 w = filteredBitmap.width(); 1632 width = filteredImage->width();
1592 h = filteredBitmap.height(); 1633 height = filteredImage->height();
1593 left += offset.x(); 1634 left += offset.x();
1594 top += offset.y(); 1635 top += offset.y();
1595 } else { 1636 } else {
1596 return; 1637 return;
1597 } 1638 }
1598 } 1639 }
1599 1640
1600 GrPaint grPaint; 1641 GrPaint grPaint;
1601 grPaint.addColorTextureProcessor(texture, SkMatrix::I()); 1642 grPaint.addColorTextureProcessor(src, SkMatrix::I());
1602 1643
1603 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, 1644 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1604 SkColor2GrColorJustAlpha(paint.getColor()), false, & grPaint); 1645 SkColor2GrColorJustAlpha(paint.getColor()), false, & grPaint);
1605 1646
1606 fContext->drawNonAARectToRect(fRenderTarget, 1647 fContext->drawNonAARectToRect(fRenderTarget,
1607 grPaint, 1648 grPaint,
1608 SkMatrix::I(), 1649 SkMatrix::I(),
1609 SkRect::MakeXYWH(SkIntToScalar(left), 1650 SkRect::MakeXYWH(SkIntToScalar(left),
1610 SkIntToScalar(top), 1651 SkIntToScalar(top),
1611 SkIntToScalar(w), 1652 SkIntToScalar(width),
1612 SkIntToScalar(h)), 1653 SkIntToScalar(height)),
1613 SkRect::MakeXYWH(0, 1654 SkRect::MakeXYWH(0,
1614 0, 1655 0,
1615 SK_Scalar1 * w / texture->wid th(), 1656 SK_Scalar1 * width / src->wid th(),
1616 SK_Scalar1 * h / texture->hei ght())); 1657 SK_Scalar1 * height / src->he ight()));
1617 } 1658 }
1618 1659
1619 void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap, 1660 void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
1620 const SkRect* src, const SkRect& dst, 1661 const SkRect* src, const SkRect& dst,
1621 const SkPaint& paint, 1662 const SkPaint& paint,
1622 SkCanvas::DrawBitmapRectFlags flags) { 1663 SkCanvas::DrawBitmapRectFlags flags) {
1623 SkMatrix matrix; 1664 SkMatrix matrix;
1624 SkRect bitmapBounds, tmpSrc; 1665 SkRect bitmapBounds, tmpSrc;
1625 1666
1626 bitmapBounds.set(0, 0, 1667 bitmapBounds.set(0, 0,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1675 1716
1676 GrTexture* devTex = devImage->getTexture(); 1717 GrTexture* devTex = devImage->getTexture();
1677 if (NULL == devTex) { 1718 if (NULL == devTex) {
1678 return; 1719 return;
1679 } 1720 }
1680 1721
1681 int w = devImage->width(); 1722 int w = devImage->width();
1682 int h = devImage->height(); 1723 int h = devImage->height();
1683 1724
1684 SkImageFilter* filter = paint.getImageFilter(); 1725 SkImageFilter* filter = paint.getImageFilter();
1685 // This bitmap will own the filtered result as a texture. 1726 SkAutoTUnref<const SkImage> filteredImage;
1686 SkBitmap filteredBitmap;
1687 1727
1688 if (filter) { 1728 if (filter) {
1689 SkIPoint offset = SkIPoint::Make(0, 0); 1729 SkIPoint offset = SkIPoint::Make(0, 0);
1690 SkMatrix matrix(*draw.fMatrix); 1730 SkMatrix matrix(*draw.fMatrix);
1691 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); 1731 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
1692 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height()); 1732 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
1693 // This cache is transient, and is freed (along with all its contained 1733 // This cache is transient, and is freed (along with all its contained
1694 // textures) when it goes out of scope. 1734 // textures) when it goes out of scope.
1695 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache()); 1735 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
1696 SkImageFilter::Context ctx(matrix, clipBounds, cache); 1736 SkImageFilter::Context ctx(matrix, clipBounds, cache);
1697 if (this->filterTexture(fContext, devTex, filter, ctx, &filteredBitmap, 1737 if (this->filterTexture(fContext, devTex, filter, ctx, filteredImage,
1698 &offset)) { 1738 &offset)) {
1699 devTex = filteredBitmap.getTexture(); 1739 devTex = filteredImage->getTexture();
1700 w = filteredBitmap.width(); 1740 w = filteredImage->width();
1701 h = filteredBitmap.height(); 1741 h = filteredImage->height();
1702 x += offset.fX; 1742 x += offset.fX;
1703 y += offset.fY; 1743 y += offset.fY;
1704 } else { 1744 } else {
1705 return; 1745 return;
1706 } 1746 }
1707 } 1747 }
1708 // drawDevice is defined to be in device coords. 1748 // drawDevice is defined to be in device coords.
1709 CHECK_SHOULD_DRAW(draw); 1749 CHECK_SHOULD_DRAW(draw);
1710 1750
1711 GrPaint grPaint; 1751 GrPaint grPaint;
(...skipping 12 matching lines...) Expand all
1724 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(), 1764 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1725 SK_Scalar1 * h / devTex->height()); 1765 SK_Scalar1 * h / devTex->height());
1726 1766
1727 fContext->drawNonAARectToRect(fRenderTarget, grPaint, SkMatrix::I(), dstRect , srcRect); 1767 fContext->drawNonAARectToRect(fRenderTarget, grPaint, SkMatrix::I(), dstRect , srcRect);
1728 } 1768 }
1729 1769
1730 bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) { 1770 bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
1731 return filter->canFilterImageGPU(); 1771 return filter->canFilterImageGPU();
1732 } 1772 }
1733 1773
1734 bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src, 1774 bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkImage* src,
1735 const SkImageFilter::Context& ctx, 1775 const SkImageFilter::Context& ctx,
1736 SkBitmap* result, SkIPoint* offset) { 1776 SkAutoTUnref<const SkImage>& result, SkIPoint* off set) {
1737 // want explicitly our impl, so guard against a subclass of us overriding it 1777 // want explicitly our impl, so guard against a subclass of us overriding it
1738 if (!this->SkGpuDevice::canHandleImageFilter(filter)) { 1778 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1739 return false; 1779 return false;
1740 } 1780 }
1741 1781
1742 SkAutoLockPixels alp(src, !src.getTexture()); 1782 //TODO: SkAutoLockPixels alp(src, !src.getTexture());
1743 if (!src.getTexture() && !src.readyToDraw()) { 1783 //TODO: if !src->readyToDraw()
1744 return false;
1745 }
1746 1784
1747 GrTexture* texture; 1785 GrTexture* texture;
1748 // We assume here that the filter will not attempt to tile the src. Otherwis e, this cache lookup 1786 // We assume here that the filter will not attempt to tile the src. Otherwis e, this cache lookup
1749 // must be pushed upstack. 1787 // must be pushed upstack.
1750 AutoBitmapTexture abt(fContext, src, NULL, &texture); 1788 AutoBitmapTexture abt(fContext, src, NULL, &texture);
1789 if (!texture) {
1790 return false;
1791 }
1751 1792
1752 return this->filterTexture(fContext, texture, filter, ctx, result, offset); 1793 return this->filterTexture(fContext, texture, filter, ctx, result, offset);
1753 } 1794 }
1754 1795
1755 /////////////////////////////////////////////////////////////////////////////// 1796 ///////////////////////////////////////////////////////////////////////////////
1756 1797
1757 // must be in SkCanvas::VertexMode order 1798 // must be in SkCanvas::VertexMode order
1758 static const GrPrimitiveType gVertexMode2PrimitiveType[] = { 1799 static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1759 kTriangles_GrPrimitiveType, 1800 kTriangles_GrPrimitiveType,
1760 kTriangleStrip_GrPrimitiveType, 1801 kTriangleStrip_GrPrimitiveType,
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
2055 #endif 2096 #endif
2056 } 2097 }
2057 2098
2058 SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() { 2099 SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
2059 // We always return a transient cache, so it is freed after each 2100 // We always return a transient cache, so it is freed after each
2060 // filter traversal. 2101 // filter traversal.
2061 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize); 2102 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
2062 } 2103 }
2063 2104
2064 #endif 2105 #endif
OLDNEW
« include/core/SkDevice.h ('K') | « src/gpu/SkGpuDevice.h ('k') | src/image/SkImage.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698