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 "GrBitmapTextContext.h" | 10 #include "GrBitmapTextContext.h" |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 struct GrSkDrawProcs : public SkDrawProcs { | 114 struct GrSkDrawProcs : public SkDrawProcs { |
115 public: | 115 public: |
116 GrContext* fContext; | 116 GrContext* fContext; |
117 GrTextContext* fTextContext; | 117 GrTextContext* fTextContext; |
118 GrFontScaler* fFontScaler; // cached in the skia glyphcache | 118 GrFontScaler* fFontScaler; // cached in the skia glyphcache |
119 }; | 119 }; |
120 | 120 |
121 /////////////////////////////////////////////////////////////////////////////// | 121 /////////////////////////////////////////////////////////////////////////////// |
122 | 122 |
123 SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props
, unsigned flags) { | 123 SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props
, unsigned flags) { |
124 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, flags); | |
125 } | |
126 | |
127 SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height, | |
128 const SkSurfaceProps* props, unsigned flags) { | |
129 if (!rt || rt->wasDestroyed()) { | 124 if (!rt || rt->wasDestroyed()) { |
130 return NULL; | 125 return NULL; |
131 } | 126 } |
132 return SkNEW_ARGS(SkGpuDevice, (rt, width, height, props, flags)); | 127 return SkNEW_ARGS(SkGpuDevice, (rt, props, flags)); |
133 } | 128 } |
134 | 129 |
135 static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps* prop
s) { | 130 static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps* prop
s) { |
136 if (props) { | 131 if (props) { |
137 return SkDeviceProperties(props->pixelGeometry()); | 132 return SkDeviceProperties(props->pixelGeometry()); |
138 } else { | 133 } else { |
139 return SkDeviceProperties(SkDeviceProperties::kLegacyLCD_InitType); | 134 return SkDeviceProperties(SkDeviceProperties::kLegacyLCD_InitType); |
140 } | 135 } |
141 } | 136 } |
142 | 137 |
143 static SkSurfaceProps copy_or_default_props(const SkSurfaceProps* props) { | 138 static SkSurfaceProps copy_or_default_props(const SkSurfaceProps* props) { |
144 if (props) { | 139 if (props) { |
145 return SkSurfaceProps(*props); | 140 return SkSurfaceProps(*props); |
146 } else { | 141 } else { |
147 return SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType); | 142 return SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType); |
148 } | 143 } |
149 } | 144 } |
150 | 145 |
151 SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height, | 146 SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, const SkSurfaceProps* props, unsign
ed flags) |
152 const SkSurfaceProps* props, unsigned flags) | |
153 : INHERITED(surfaceprops_to_deviceprops(props)) | 147 : INHERITED(surfaceprops_to_deviceprops(props)) |
154 , fSurfaceProps(copy_or_default_props(props)) | 148 , fSurfaceProps(copy_or_default_props(props)) |
155 { | 149 { |
156 fDrawProcs = NULL; | 150 fDrawProcs = NULL; |
157 | 151 |
158 fContext = SkRef(rt->getContext()); | 152 fContext = SkRef(rt->getContext()); |
159 fNeedClear = flags & kNeedClear_Flag; | 153 fNeedClear = flags & kNeedClear_Flag; |
160 | 154 |
161 fRenderTarget = SkRef(rt); | 155 fRenderTarget = SkRef(rt); |
162 | 156 |
163 SkImageInfo info = rt->surfacePriv().info().makeWH(width, height); | 157 SkImageInfo info = rt->surfacePriv().info(); |
164 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt)); | 158 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt)); |
165 fLegacyBitmap.setInfo(info); | 159 fLegacyBitmap.setInfo(info); |
166 fLegacyBitmap.setPixelRef(pr)->unref(); | 160 fLegacyBitmap.setPixelRef(pr)->unref(); |
167 | 161 |
168 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts(); | 162 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts(); |
169 fTextContext = fContext->createTextContext(fRenderTarget, this, this->getLea
kyProperties(), | 163 fTextContext = fContext->createTextContext(fRenderTarget, this, this->getLea
kyProperties(), |
170 useDFT); | 164 useDFT); |
171 } | 165 } |
172 | 166 |
173 GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::B
udgeted budgeted, | 167 GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::B
udgeted budgeted, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 | 204 |
211 SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgete
d, | 205 SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgete
d, |
212 const SkImageInfo& info, int sampleCount, | 206 const SkImageInfo& info, int sampleCount, |
213 const SkSurfaceProps* props, unsigned flags) { | 207 const SkSurfaceProps* props, unsigned flags) { |
214 | 208 |
215 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info,
sampleCount)); | 209 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info,
sampleCount)); |
216 if (NULL == rt) { | 210 if (NULL == rt) { |
217 return NULL; | 211 return NULL; |
218 } | 212 } |
219 | 213 |
220 return SkNEW_ARGS(SkGpuDevice, (rt, info.width(), info.height(), props, flag
s)); | 214 return SkNEW_ARGS(SkGpuDevice, (rt, props, flags)); |
221 } | 215 } |
222 | 216 |
223 SkGpuDevice::~SkGpuDevice() { | 217 SkGpuDevice::~SkGpuDevice() { |
224 if (fDrawProcs) { | 218 if (fDrawProcs) { |
225 delete fDrawProcs; | 219 delete fDrawProcs; |
226 } | 220 } |
227 | 221 |
228 delete fTextContext; | 222 delete fTextContext; |
229 | 223 |
230 fRenderTarget->unref(); | 224 fRenderTarget->unref(); |
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 // setup new clip | 729 // setup new clip |
736 GrClip clip(clipRect); | 730 GrClip clip(clipRect); |
737 | 731 |
738 // Draw the mask into maskTexture with the path's top-left at the origin usi
ng tempPaint. | 732 // Draw the mask into maskTexture with the path's top-left at the origin usi
ng tempPaint. |
739 SkMatrix translate; | 733 SkMatrix translate; |
740 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop); | 734 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop); |
741 context->drawPath(mask->asRenderTarget(), clip, tempPaint, translate, devPat
h, strokeInfo); | 735 context->drawPath(mask->asRenderTarget(), clip, tempPaint, translate, devPat
h, strokeInfo); |
742 return mask; | 736 return mask; |
743 } | 737 } |
744 | 738 |
745 SkBitmap wrap_texture(GrTexture* texture, int width, int height) { | 739 SkBitmap wrap_texture(GrTexture* texture) { |
746 SkBitmap result; | 740 SkBitmap result; |
747 result.setInfo(SkImageInfo::MakeN32Premul(width, height)); | 741 result.setInfo(texture->surfacePriv().info()); |
748 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unre
f(); | 742 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unre
f(); |
749 return result; | 743 return result; |
750 } | 744 } |
751 | 745 |
752 }; | 746 }; |
753 | 747 |
754 void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath, | 748 void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath, |
755 const SkPaint& paint, const SkMatrix* prePathMatrix, | 749 const SkPaint& paint, const SkMatrix* prePathMatrix, |
756 bool pathIsMutable) { | 750 bool pathIsMutable) { |
757 CHECK_FOR_ANNOTATION(paint); | 751 CHECK_FOR_ANNOTATION(paint); |
(...skipping 715 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1473 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType()); | 1467 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType()); |
1474 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()
) : | 1468 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()
) : |
1475 SkColor2GrColor(paint.getColor()); | 1469 SkColor2GrColor(paint.getColor()); |
1476 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, f
alse, &grPaint); | 1470 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, f
alse, &grPaint); |
1477 | 1471 |
1478 fContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dst
Rect, | 1472 fContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dst
Rect, |
1479 paintRect); | 1473 paintRect); |
1480 } | 1474 } |
1481 | 1475 |
1482 bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture, | 1476 bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture, |
1483 int width, int height, | |
1484 const SkImageFilter* filter, | 1477 const SkImageFilter* filter, |
1485 const SkImageFilter::Context& ctx, | 1478 const SkImageFilter::Context& ctx, |
1486 SkBitmap* result, SkIPoint* offset) { | 1479 SkBitmap* result, SkIPoint* offset) { |
1487 SkASSERT(filter); | 1480 SkASSERT(filter); |
1488 | 1481 |
1489 // FIXME: plumb actual surface props such that we don't have to lie about th
e flags here | 1482 // FIXME: plumb actual surface props such that we don't have to lie about th
e flags here |
1490 // (https://code.google.com/p/skia/issues/detail?id=3148). | 1483 // (https://code.google.com/p/skia/issues/detail?id=3148). |
1491 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().
pixelGeometry())); | 1484 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().
pixelGeometry())); |
1492 | 1485 |
1493 if (filter->canFilterImageGPU()) { | 1486 if (filter->canFilterImageGPU()) { |
1494 return filter->filterImageGPU(&proxy, wrap_texture(texture, width, heigh
t), | 1487 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result
, offset); |
1495 ctx, result, offset); | |
1496 } else { | 1488 } else { |
1497 return false; | 1489 return false; |
1498 } | 1490 } |
1499 } | 1491 } |
1500 | 1492 |
1501 void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, | 1493 void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
1502 int left, int top, const SkPaint& paint) { | 1494 int left, int top, const SkPaint& paint) { |
1503 // drawSprite is defined to be in device coords. | 1495 // drawSprite is defined to be in device coords. |
1504 CHECK_SHOULD_DRAW(draw); | 1496 CHECK_SHOULD_DRAW(draw); |
1505 | 1497 |
(...skipping 18 matching lines...) Expand all Loading... |
1524 | 1516 |
1525 if (filter) { | 1517 if (filter) { |
1526 SkIPoint offset = SkIPoint::Make(0, 0); | 1518 SkIPoint offset = SkIPoint::Make(0, 0); |
1527 SkMatrix matrix(*draw.fMatrix); | 1519 SkMatrix matrix(*draw.fMatrix); |
1528 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top)); | 1520 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top)); |
1529 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); | 1521 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
1530 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache()); | 1522 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache()); |
1531 // This cache is transient, and is freed (along with all its contained | 1523 // This cache is transient, and is freed (along with all its contained |
1532 // textures) when it goes out of scope. | 1524 // textures) when it goes out of scope. |
1533 SkImageFilter::Context ctx(matrix, clipBounds, cache); | 1525 SkImageFilter::Context ctx(matrix, clipBounds, cache); |
1534 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredB
itmap, | 1526 if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap, |
1535 &offset)) { | 1527 &offset)) { |
1536 texture = (GrTexture*) filteredBitmap.getTexture(); | 1528 texture = (GrTexture*) filteredBitmap.getTexture(); |
1537 w = filteredBitmap.width(); | 1529 w = filteredBitmap.width(); |
1538 h = filteredBitmap.height(); | 1530 h = filteredBitmap.height(); |
1539 left += offset.x(); | 1531 left += offset.x(); |
1540 top += offset.y(); | 1532 top += offset.y(); |
1541 } else { | 1533 } else { |
1542 return; | 1534 return; |
1543 } | 1535 } |
1544 } | 1536 } |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1638 | 1630 |
1639 if (filter) { | 1631 if (filter) { |
1640 SkIPoint offset = SkIPoint::Make(0, 0); | 1632 SkIPoint offset = SkIPoint::Make(0, 0); |
1641 SkMatrix matrix(*draw.fMatrix); | 1633 SkMatrix matrix(*draw.fMatrix); |
1642 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); | 1634 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); |
1643 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height()); | 1635 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height()); |
1644 // This cache is transient, and is freed (along with all its contained | 1636 // This cache is transient, and is freed (along with all its contained |
1645 // textures) when it goes out of scope. | 1637 // textures) when it goes out of scope. |
1646 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache()); | 1638 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache()); |
1647 SkImageFilter::Context ctx(matrix, clipBounds, cache); | 1639 SkImageFilter::Context ctx(matrix, clipBounds, cache); |
1648 if (this->filterTexture(fContext, devTex, device->width(), device->heigh
t(), | 1640 if (this->filterTexture(fContext, devTex, filter, ctx, &filteredBitmap, |
1649 filter, ctx, &filteredBitmap, &offset)) { | 1641 &offset)) { |
1650 devTex = filteredBitmap.getTexture(); | 1642 devTex = filteredBitmap.getTexture(); |
1651 w = filteredBitmap.width(); | 1643 w = filteredBitmap.width(); |
1652 h = filteredBitmap.height(); | 1644 h = filteredBitmap.height(); |
1653 x += offset.fX; | 1645 x += offset.fX; |
1654 y += offset.fY; | 1646 y += offset.fY; |
1655 } else { | 1647 } else { |
1656 return; | 1648 return; |
1657 } | 1649 } |
1658 } | 1650 } |
1659 | 1651 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1692 SkAutoLockPixels alp(src, !src.getTexture()); | 1684 SkAutoLockPixels alp(src, !src.getTexture()); |
1693 if (!src.getTexture() && !src.readyToDraw()) { | 1685 if (!src.getTexture() && !src.readyToDraw()) { |
1694 return false; | 1686 return false; |
1695 } | 1687 } |
1696 | 1688 |
1697 GrTexture* texture; | 1689 GrTexture* texture; |
1698 // We assume here that the filter will not attempt to tile the src. Otherwis
e, this cache lookup | 1690 // We assume here that the filter will not attempt to tile the src. Otherwis
e, this cache lookup |
1699 // must be pushed upstack. | 1691 // must be pushed upstack. |
1700 AutoBitmapTexture abt(fContext, src, NULL, &texture); | 1692 AutoBitmapTexture abt(fContext, src, NULL, &texture); |
1701 | 1693 |
1702 return this->filterTexture(fContext, texture, src.width(), src.height(), | 1694 return this->filterTexture(fContext, texture, filter, ctx, result, offset); |
1703 filter, ctx, result, offset); | |
1704 } | 1695 } |
1705 | 1696 |
1706 /////////////////////////////////////////////////////////////////////////////// | 1697 /////////////////////////////////////////////////////////////////////////////// |
1707 | 1698 |
1708 // must be in SkCanvas::VertexMode order | 1699 // must be in SkCanvas::VertexMode order |
1709 static const GrPrimitiveType gVertexMode2PrimitiveType[] = { | 1700 static const GrPrimitiveType gVertexMode2PrimitiveType[] = { |
1710 kTriangles_GrPrimitiveType, | 1701 kTriangles_GrPrimitiveType, |
1711 kTriangleStrip_GrPrimitiveType, | 1702 kTriangleStrip_GrPrimitiveType, |
1712 kTriangleFan_GrPrimitiveType, | 1703 kTriangleFan_GrPrimitiveType, |
1713 }; | 1704 }; |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1903 | 1894 |
1904 // layers are never draw in repeat modes, so we can request an approx | 1895 // layers are never draw in repeat modes, so we can request an approx |
1905 // match and ignore any padding. | 1896 // match and ignore any padding. |
1906 const GrContext::ScratchTexMatch match = (kNever_TileUsage == cinfo.fTileUsa
ge) ? | 1897 const GrContext::ScratchTexMatch match = (kNever_TileUsage == cinfo.fTileUsa
ge) ? |
1907 GrContext::kApprox_ScratchTexMat
ch : | 1898 GrContext::kApprox_ScratchTexMat
ch : |
1908 GrContext::kExact_ScratchTexMatc
h; | 1899 GrContext::kExact_ScratchTexMatc
h; |
1909 texture.reset(fContext->refScratchTexture(desc, match)); | 1900 texture.reset(fContext->refScratchTexture(desc, match)); |
1910 | 1901 |
1911 if (texture) { | 1902 if (texture) { |
1912 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry); | 1903 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry); |
1913 return SkGpuDevice::Create( | 1904 return SkGpuDevice::Create(texture->asRenderTarget(), &props, flags); |
1914 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height()
, &props, flags); | |
1915 } else { | 1905 } else { |
1916 SkErrorInternals::SetError( kInternalError_SkError, | 1906 SkErrorInternals::SetError( kInternalError_SkError, |
1917 "---- failed to create compatible device tex
ture [%d %d]\n", | 1907 "---- failed to create compatible device tex
ture [%d %d]\n", |
1918 cinfo.fInfo.width(), cinfo.fInfo.height()); | 1908 cinfo.fInfo.width(), cinfo.fInfo.height()); |
1919 return NULL; | 1909 return NULL; |
1920 } | 1910 } |
1921 } | 1911 } |
1922 | 1912 |
1923 SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps
& props) { | 1913 SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps
& props) { |
1924 // TODO: Change the signature of newSurface to take a budgeted parameter. | 1914 // TODO: Change the signature of newSurface to take a budgeted parameter. |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1993 #endif | 1983 #endif |
1994 } | 1984 } |
1995 | 1985 |
1996 SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() { | 1986 SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() { |
1997 // We always return a transient cache, so it is freed after each | 1987 // We always return a transient cache, so it is freed after each |
1998 // filter traversal. | 1988 // filter traversal. |
1999 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize); | 1989 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize); |
2000 } | 1990 } |
2001 | 1991 |
2002 #endif | 1992 #endif |
OLD | NEW |