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

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

Issue 428103003: Exit early when draw can be skipped. Be more robust against abandoned GrContext. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 4 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2011 Google Inc. 3 * Copyright 2011 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #include "GrContext.h" 10 #include "GrContext.h"
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after
637 } 637 }
638 return true; 638 return true;
639 } 639 }
640 640
641 641
642 //////////////////////////////////////////////////////////////////////////////// 642 ////////////////////////////////////////////////////////////////////////////////
643 643
644 void GrContext::clear(const SkIRect* rect, 644 void GrContext::clear(const SkIRect* rect,
645 const GrColor color, 645 const GrColor color,
646 bool canIgnoreRect, 646 bool canIgnoreRect,
647 GrRenderTarget* target) { 647 GrRenderTarget* renderTarget) {
648 ASSERT_OWNED_RESOURCE(renderTarget);
648 AutoRestoreEffects are; 649 AutoRestoreEffects are;
649 AutoCheckFlush acf(this); 650 AutoCheckFlush acf(this);
650 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this); 651 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
651 this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf)->clear(rect, color, 652 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf);
652 canIgnoreRect, t arget); 653 if (NULL == target) {
654 return;
655 }
656 target->clear(rect, color, canIgnoreRect, renderTarget);
653 } 657 }
654 658
655 void GrContext::drawPaint(const GrPaint& origPaint) { 659 void GrContext::drawPaint(const GrPaint& origPaint) {
656 // set rect to be big enough to fill the space, but not super-huge, so we 660 // set rect to be big enough to fill the space, but not super-huge, so we
657 // don't overflow fixed-point implementations 661 // don't overflow fixed-point implementations
658 SkRect r; 662 SkRect r;
659 r.setLTRB(0, 0, 663 r.setLTRB(0, 0,
660 SkIntToScalar(getRenderTarget()->width()), 664 SkIntToScalar(getRenderTarget()->width()),
661 SkIntToScalar(getRenderTarget()->height())); 665 SkIntToScalar(getRenderTarget()->height()));
662 SkMatrix inverse; 666 SkMatrix inverse;
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
787 if (NULL != strokeInfo && strokeInfo->isDashed()) { 791 if (NULL != strokeInfo && strokeInfo->isDashed()) {
788 SkPath path; 792 SkPath path;
789 path.addRect(rect); 793 path.addRect(rect);
790 this->drawPath(paint, path, *strokeInfo); 794 this->drawPath(paint, path, *strokeInfo);
791 return; 795 return;
792 } 796 }
793 797
794 AutoRestoreEffects are; 798 AutoRestoreEffects are;
795 AutoCheckFlush acf(this); 799 AutoCheckFlush acf(this);
796 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf ); 800 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf );
801 if (NULL == target) {
802 return;
803 }
797 804
798 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target); 805 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
799 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWid th(); 806 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWid th();
800 SkMatrix combinedMatrix = target->drawState()->getViewMatrix(); 807 SkMatrix combinedMatrix = target->drawState()->getViewMatrix();
801 if (NULL != matrix) { 808 if (NULL != matrix) {
802 combinedMatrix.preConcat(*matrix); 809 combinedMatrix.preConcat(*matrix);
803 } 810 }
804 811
805 // Check if this is a full RT draw and can be replaced with a clear. We don' t bother checking 812 // Check if this is a full RT draw and can be replaced with a clear. We don' t bother checking
806 // cases where the RT is fully inside a stroke. 813 // cases where the RT is fully inside a stroke.
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
911 } 918 }
912 919
913 void GrContext::drawRectToRect(const GrPaint& paint, 920 void GrContext::drawRectToRect(const GrPaint& paint,
914 const SkRect& dstRect, 921 const SkRect& dstRect,
915 const SkRect& localRect, 922 const SkRect& localRect,
916 const SkMatrix* dstMatrix, 923 const SkMatrix* dstMatrix,
917 const SkMatrix* localMatrix) { 924 const SkMatrix* localMatrix) {
918 AutoRestoreEffects are; 925 AutoRestoreEffects are;
919 AutoCheckFlush acf(this); 926 AutoCheckFlush acf(this);
920 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf ); 927 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf );
928 if (NULL == target) {
929 return;
930 }
921 931
922 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target); 932 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
923 933
924 target->drawRect(dstRect, dstMatrix, &localRect, localMatrix); 934 target->drawRect(dstRect, dstMatrix, &localRect, localMatrix);
925 } 935 }
926 936
927 namespace { 937 namespace {
928 938
929 extern const GrVertexAttrib gPosUVColorAttribs[] = { 939 extern const GrVertexAttrib gPosUVColorAttribs[] = {
930 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding }, 940 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
968 const SkPoint positions[], 978 const SkPoint positions[],
969 const SkPoint texCoords[], 979 const SkPoint texCoords[],
970 const GrColor colors[], 980 const GrColor colors[],
971 const uint16_t indices[], 981 const uint16_t indices[],
972 int indexCount) { 982 int indexCount) {
973 AutoRestoreEffects are; 983 AutoRestoreEffects are;
974 AutoCheckFlush acf(this); 984 AutoCheckFlush acf(this);
975 GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scop e 985 GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scop e
976 986
977 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf ); 987 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf );
988 if (NULL == target) {
989 return;
990 }
978 GrDrawState* drawState = target->drawState(); 991 GrDrawState* drawState = target->drawState();
979 992
980 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target); 993 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
981 994
982 int colorOffset = -1, texOffset = -1; 995 int colorOffset = -1, texOffset = -1;
983 set_vertex_attributes(drawState, texCoords, colors, &colorOffset, &texOffset ); 996 set_vertex_attributes(drawState, texCoords, colors, &colorOffset, &texOffset );
984 997
985 size_t vertexSize = drawState->getVertexSize(); 998 size_t vertexSize = drawState->getVertexSize();
986 if (sizeof(SkPoint) != vertexSize) { 999 if (sizeof(SkPoint) != vertexSize) {
987 if (!geo.set(target, vertexCount, 0)) { 1000 if (!geo.set(target, vertexCount, 0)) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1029 if (strokeInfo.isDashed()) { 1042 if (strokeInfo.isDashed()) {
1030 SkPath path; 1043 SkPath path;
1031 path.addRRect(rrect); 1044 path.addRRect(rrect);
1032 this->drawPath(paint, path, strokeInfo); 1045 this->drawPath(paint, path, strokeInfo);
1033 return; 1046 return;
1034 } 1047 }
1035 1048
1036 AutoRestoreEffects are; 1049 AutoRestoreEffects are;
1037 AutoCheckFlush acf(this); 1050 AutoCheckFlush acf(this);
1038 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf ); 1051 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf );
1052 if (NULL == target) {
1053 return;
1054 }
1039 1055
1040 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target); 1056 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
1041 1057
1042 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec(); 1058 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1043 1059
1044 if (!fOvalRenderer->drawRRect(target, this, paint.isAntiAlias(), rrect, stro keRec)) { 1060 if (!fOvalRenderer->drawRRect(target, this, paint.isAntiAlias(), rrect, stro keRec)) {
1045 SkPath path; 1061 SkPath path;
1046 path.addRRect(rrect); 1062 path.addRRect(rrect);
1047 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo); 1063 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
1048 } 1064 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1086 if (strokeInfo.isDashed()) { 1102 if (strokeInfo.isDashed()) {
1087 SkPath path; 1103 SkPath path;
1088 path.addOval(oval); 1104 path.addOval(oval);
1089 this->drawPath(paint, path, strokeInfo); 1105 this->drawPath(paint, path, strokeInfo);
1090 return; 1106 return;
1091 } 1107 }
1092 1108
1093 AutoRestoreEffects are; 1109 AutoRestoreEffects are;
1094 AutoCheckFlush acf(this); 1110 AutoCheckFlush acf(this);
1095 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf ); 1111 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf );
1112 if (NULL == target) {
1113 return;
1114 }
1096 1115
1097 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target); 1116 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1098 1117
1099 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec(); 1118 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1100 1119
1101 1120
1102 if (!fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), oval, stroke Rec)) { 1121 if (!fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), oval, stroke Rec)) {
1103 SkPath path; 1122 SkPath path;
1104 path.addOval(oval); 1123 path.addOval(oval);
1105 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo); 1124 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1169 } 1188 }
1170 return; 1189 return;
1171 } 1190 }
1172 1191
1173 if (strokeInfo.isDashed()) { 1192 if (strokeInfo.isDashed()) {
1174 SkPoint pts[2]; 1193 SkPoint pts[2];
1175 if (path.isLine(pts)) { 1194 if (path.isLine(pts)) {
1176 AutoRestoreEffects are; 1195 AutoRestoreEffects are;
1177 AutoCheckFlush acf(this); 1196 AutoCheckFlush acf(this);
1178 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &a re, &acf); 1197 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &a re, &acf);
1198 if (NULL == target) {
1199 return;
1200 }
1179 GrDrawState* drawState = target->drawState(); 1201 GrDrawState* drawState = target->drawState();
1180 1202
1181 SkMatrix origViewMatrix = drawState->getViewMatrix(); 1203 SkMatrix origViewMatrix = drawState->getViewMatrix();
1182 GrDrawState::AutoViewMatrixRestore avmr; 1204 GrDrawState::AutoViewMatrixRestore avmr;
1183 if (avmr.setIdentity(target->drawState())) { 1205 if (avmr.setIdentity(target->drawState())) {
1184 if (GrDashingEffect::DrawDashLine(pts, paint, strokeInfo, fGpu, target, 1206 if (GrDashingEffect::DrawDashLine(pts, paint, strokeInfo, fGpu, target,
1185 origViewMatrix)) { 1207 origViewMatrix)) {
1186 return; 1208 return;
1187 } 1209 }
1188 } 1210 }
(...skipping 14 matching lines...) Expand all
1203 } 1225 }
1204 1226
1205 // Note that internalDrawPath may sw-rasterize the path into a scratch textu re. 1227 // Note that internalDrawPath may sw-rasterize the path into a scratch textu re.
1206 // Scratch textures can be recycled after they are returned to the texture 1228 // Scratch textures can be recycled after they are returned to the texture
1207 // cache. This presents a potential hazard for buffered drawing. However, 1229 // cache. This presents a potential hazard for buffered drawing. However,
1208 // the writePixels that uploads to the scratch will perform a flush so we're 1230 // the writePixels that uploads to the scratch will perform a flush so we're
1209 // OK. 1231 // OK.
1210 AutoRestoreEffects are; 1232 AutoRestoreEffects are;
1211 AutoCheckFlush acf(this); 1233 AutoCheckFlush acf(this);
1212 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf ); 1234 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf );
1235 if (NULL == target) {
1236 return;
1237 }
1213 GrDrawState* drawState = target->drawState(); 1238 GrDrawState* drawState = target->drawState();
1214 1239
1215 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isC onvex()); 1240 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isC onvex());
1216 1241
1217 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec(); 1242 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1218 1243
1219 bool useCoverageAA = paint.isAntiAlias() && !drawState->getRenderTarget()->i sMultisampled(); 1244 bool useCoverageAA = paint.isAntiAlias() && !drawState->getRenderTarget()->i sMultisampled();
1220 1245
1221 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) { 1246 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
1222 // Concave AA paths are expensive - try to avoid them for special cases 1247 // Concave AA paths are expensive - try to avoid them for special cases
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
1532 return true; 1557 return true;
1533 } 1558 }
1534 1559
1535 void GrContext::resolveRenderTarget(GrRenderTarget* target) { 1560 void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1536 SkASSERT(target); 1561 SkASSERT(target);
1537 ASSERT_OWNED_RESOURCE(target); 1562 ASSERT_OWNED_RESOURCE(target);
1538 // In the future we may track whether there are any pending draws to this 1563 // In the future we may track whether there are any pending draws to this
1539 // target. We don't today so we always perform a flush. We don't promise 1564 // target. We don't today so we always perform a flush. We don't promise
1540 // this to our clients, though. 1565 // this to our clients, though.
1541 this->flush(); 1566 this->flush();
1542 fGpu->resolveRenderTarget(target); 1567 if (NULL != fGpu) {
1568 fGpu->resolveRenderTarget(target);
1569 }
1543 } 1570 }
1544 1571
1545 void GrContext::discardRenderTarget(GrRenderTarget* target) { 1572 void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
1546 SkASSERT(target); 1573 SkASSERT(renderTarget);
1547 ASSERT_OWNED_RESOURCE(target); 1574 ASSERT_OWNED_RESOURCE(renderTarget);
1548 AutoRestoreEffects are; 1575 AutoRestoreEffects are;
1549 AutoCheckFlush acf(this); 1576 AutoCheckFlush acf(this);
1550 this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf)->discard(target); 1577 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf);
1578 if (NULL == target) {
1579 return;
1580 }
1581 target->discard(renderTarget);
1551 } 1582 }
1552 1583
1553 void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint* topLeft) { 1584 void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint* topLeft) {
1554 if (NULL == src || NULL == dst) { 1585 if (NULL == src || NULL == dst) {
1555 return; 1586 return;
1556 } 1587 }
1557 ASSERT_OWNED_RESOURCE(src); 1588 ASSERT_OWNED_RESOURCE(src);
1558 1589
1559 // Writes pending to the source texture are not tracked, so a flush 1590 // Writes pending to the source texture are not tracked, so a flush
1560 // is required to ensure that the copy captures the most recent contents 1591 // is required to ensure that the copy captures the most recent contents
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
1711 //////////////////////////////////////////////////////////////////////////////// 1742 ////////////////////////////////////////////////////////////////////////////////
1712 1743
1713 GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, 1744 GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint,
1714 BufferedDraw buffered, 1745 BufferedDraw buffered,
1715 AutoRestoreEffects* are, 1746 AutoRestoreEffects* are,
1716 AutoCheckFlush* acf) { 1747 AutoCheckFlush* acf) {
1717 // All users of this draw state should be freeing up all effects when they'r e done. 1748 // All users of this draw state should be freeing up all effects when they'r e done.
1718 // Otherwise effects that own resources may keep those resources alive indef initely. 1749 // Otherwise effects that own resources may keep those resources alive indef initely.
1719 SkASSERT(0 == fDrawState->numColorStages() && 0 == fDrawState->numCoverageSt ages()); 1750 SkASSERT(0 == fDrawState->numColorStages() && 0 == fDrawState->numCoverageSt ages());
1720 1751
1752 if (NULL == fGpu) {
1753 return NULL;
1754 }
1755
1721 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffere d) { 1756 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffere d) {
1722 fDrawBuffer->flush(); 1757 fDrawBuffer->flush();
1723 fLastDrawWasBuffered = kNo_BufferedDraw; 1758 fLastDrawWasBuffered = kNo_BufferedDraw;
1724 } 1759 }
1725 ASSERT_OWNED_RESOURCE(fRenderTarget.get()); 1760 ASSERT_OWNED_RESOURCE(fRenderTarget.get());
1726 if (NULL != paint) { 1761 if (NULL != paint) {
1727 SkASSERT(NULL != are); 1762 SkASSERT(NULL != are);
1728 SkASSERT(NULL != acf); 1763 SkASSERT(NULL != acf);
1729 are->set(fDrawState); 1764 are->set(fDrawState);
1730 fDrawState->setFromPaint(*paint, fViewMatrix, fRenderTarget.get()); 1765 fDrawState->setFromPaint(*paint, fViewMatrix, fRenderTarget.get());
1731 #if GR_DEBUG_PARTIAL_COVERAGE_CHECK 1766 #if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1732 if ((paint->hasMask() || 0xff != paint->fCoverage) && 1767 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
1733 !fGpu->canApplyCoverage()) { 1768 !fGpu->canApplyCoverage()) {
1734 GrPrintf("Partial pixel coverage will be incorrectly blended.\n"); 1769 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1735 } 1770 }
1736 #endif 1771 #endif
1772 if (fDrawState->getBlendOpts() & GrDrawState::kSkipDraw_BlendOptFlag) {
1773 are->set(NULL);
1774 return NULL;
1775 }
1737 } else { 1776 } else {
1738 fDrawState->reset(fViewMatrix); 1777 fDrawState->reset(fViewMatrix);
1739 fDrawState->setRenderTarget(fRenderTarget.get()); 1778 fDrawState->setRenderTarget(fRenderTarget.get());
1740 } 1779 }
1741 GrDrawTarget* target; 1780 GrDrawTarget* target;
1742 if (kYes_BufferedDraw == buffered) { 1781 if (kYes_BufferedDraw == buffered) {
1743 fLastDrawWasBuffered = kYes_BufferedDraw; 1782 fLastDrawWasBuffered = kYes_BufferedDraw;
1744 target = fDrawBuffer; 1783 target = fDrawBuffer;
1745 } else { 1784 } else {
1746 SkASSERT(kNo_BufferedDraw == buffered); 1785 SkASSERT(kNo_BufferedDraw == buffered);
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
1920 fDrawBuffer->removeGpuTraceMarker(marker); 1959 fDrawBuffer->removeGpuTraceMarker(marker);
1921 } 1960 }
1922 } 1961 }
1923 1962
1924 /////////////////////////////////////////////////////////////////////////////// 1963 ///////////////////////////////////////////////////////////////////////////////
1925 #if GR_CACHE_STATS 1964 #if GR_CACHE_STATS
1926 void GrContext::printCacheStats() const { 1965 void GrContext::printCacheStats() const {
1927 fResourceCache->printStats(); 1966 fResourceCache->printStats();
1928 } 1967 }
1929 #endif 1968 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698