quick-rejects based on xfermode, using SkPaint::nothingToDraw()

git-svn-id: http://skia.googlecode.com/svn/trunk@2402 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@google.com 2011-10-04 01:17:15 +00:00
parent 2c508f2dc2
commit dcd0f3a980
4 changed files with 86 additions and 23 deletions

View File

@ -865,6 +865,10 @@ private:
const SkRect& computeStrokeFastBounds(const SkRect& orig, const SkRect& computeStrokeFastBounds(const SkRect& orig,
SkRect* storage) const; SkRect* storage) const;
// returns true if the paint's settings (e.g. xfermode + alpha) resolve to
// mean that we need not draw at all (e.g. SrcOver + 0-alpha)
bool nothingToDraw() const;
enum { enum {
kCanonicalTextSizeForPaths = 64 kCanonicalTextSizeForPaths = 64
}; };

View File

@ -53,6 +53,9 @@ static SkCanvas::EdgeType paint2EdgeType(const SkPaint* paint) {
SkCanvas::kAA_EdgeType : SkCanvas::kBW_EdgeType; SkCanvas::kAA_EdgeType : SkCanvas::kBW_EdgeType;
} }
#define CHECK_NOTHING_TO_DRAW(paint) \
do { if ((paint).nothingToDraw()) return; } while (0)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
/* This is the record we keep for each SkDevice that the user installs. /* This is the record we keep for each SkDevice that the user installs.
@ -822,6 +825,10 @@ void SkCanvas::internalDrawBitmap(const SkBitmap& bitmap, const SkIRect* srcRect
void SkCanvas::drawDevice(SkDevice* device, int x, int y, void SkCanvas::drawDevice(SkDevice* device, int x, int y,
const SkPaint* paint) { const SkPaint* paint) {
if (paint) {
CHECK_NOTHING_TO_DRAW(*paint);
}
SkPaint tmp; SkPaint tmp;
if (NULL == paint) { if (NULL == paint) {
tmp.setDither(true); tmp.setDither(true);
@ -1206,6 +1213,8 @@ void SkCanvas::clear(SkColor color) {
} }
void SkCanvas::drawPaint(const SkPaint& paint) { void SkCanvas::drawPaint(const SkPaint& paint) {
CHECK_NOTHING_TO_DRAW(paint);
LOOPER_BEGIN(paint, SkDrawFilter::kPaint_Type) LOOPER_BEGIN(paint, SkDrawFilter::kPaint_Type)
while (iter.next()) { while (iter.next()) {
@ -1217,6 +1226,7 @@ void SkCanvas::drawPaint(const SkPaint& paint) {
void SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[], void SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[],
const SkPaint& paint) { const SkPaint& paint) {
CHECK_NOTHING_TO_DRAW(paint);
if ((long)count <= 0) { if ((long)count <= 0) {
return; return;
} }
@ -1233,6 +1243,8 @@ void SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[],
} }
void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) { void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) {
CHECK_NOTHING_TO_DRAW(paint);
if (paint.canComputeFastBounds()) { if (paint.canComputeFastBounds()) {
SkRect storage; SkRect storage;
if (this->quickReject(paint.computeFastBounds(r, &storage), if (this->quickReject(paint.computeFastBounds(r, &storage),
@ -1251,6 +1263,8 @@ void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) {
} }
void SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) { void SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
CHECK_NOTHING_TO_DRAW(paint);
if (paint.canComputeFastBounds()) { if (paint.canComputeFastBounds()) {
SkRect storage; SkRect storage;
const SkRect& bounds = path.getBounds(); const SkRect& bounds = path.getBounds();
@ -1273,6 +1287,10 @@ void SkCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
const SkPaint* paint) { const SkPaint* paint) {
SkDEBUGCODE(bitmap.validate();) SkDEBUGCODE(bitmap.validate();)
if (paint) {
CHECK_NOTHING_TO_DRAW(*paint);
}
if (NULL == paint || (paint->getMaskFilter() == NULL)) { if (NULL == paint || (paint->getMaskFilter() == NULL)) {
SkRect fastBounds; SkRect fastBounds;
fastBounds.set(x, y, fastBounds.set(x, y,
@ -1338,11 +1356,17 @@ void SkCanvas::internalDrawBitmapRect(const SkBitmap& bitmap, const SkIRect* src
void SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src, void SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src,
const SkRect& dst, const SkPaint* paint) { const SkRect& dst, const SkPaint* paint) {
SkDEBUGCODE(bitmap.validate();) SkDEBUGCODE(bitmap.validate();)
if (paint) {
CHECK_NOTHING_TO_DRAW(*paint);
}
this->internalDrawBitmapRect(bitmap, src, dst, paint); this->internalDrawBitmapRect(bitmap, src, dst, paint);
} }
void SkCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& matrix, void SkCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& matrix,
const SkPaint* paint) { const SkPaint* paint) {
if (paint) {
CHECK_NOTHING_TO_DRAW(*paint);
}
SkDEBUGCODE(bitmap.validate();) SkDEBUGCODE(bitmap.validate();)
this->internalDrawBitmap(bitmap, NULL, matrix, paint); this->internalDrawBitmap(bitmap, NULL, matrix, paint);
} }
@ -1413,6 +1437,9 @@ void SkCanvas::internalDrawBitmapNine(const SkBitmap& bitmap,
void SkCanvas::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, void SkCanvas::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
const SkRect& dst, const SkPaint* paint) { const SkRect& dst, const SkPaint* paint) {
if (paint) {
CHECK_NOTHING_TO_DRAW(*paint);
}
SkDEBUGCODE(bitmap.validate();) SkDEBUGCODE(bitmap.validate();)
// Need a device entry-point, so gpu can use a mesh // Need a device entry-point, so gpu can use a mesh
@ -1421,6 +1448,9 @@ void SkCanvas::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
void SkCanvas::drawSprite(const SkBitmap& bitmap, int x, int y, void SkCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
const SkPaint* paint) { const SkPaint* paint) {
if (paint) {
CHECK_NOTHING_TO_DRAW(*paint);
}
SkDEBUGCODE(bitmap.validate();) SkDEBUGCODE(bitmap.validate();)
if (reject_bitmap(bitmap)) { if (reject_bitmap(bitmap)) {
@ -1536,6 +1566,8 @@ void SkCanvas::DrawTextDecorations(const SkDraw& draw, const SkPaint& paint,
void SkCanvas::drawText(const void* text, size_t byteLength, void SkCanvas::drawText(const void* text, size_t byteLength,
SkScalar x, SkScalar y, const SkPaint& paint) { SkScalar x, SkScalar y, const SkPaint& paint) {
CHECK_NOTHING_TO_DRAW(paint);
LOOPER_BEGIN(paint, SkDrawFilter::kText_Type) LOOPER_BEGIN(paint, SkDrawFilter::kText_Type)
while (iter.next()) { while (iter.next()) {
@ -1550,6 +1582,8 @@ void SkCanvas::drawText(const void* text, size_t byteLength,
void SkCanvas::drawPosText(const void* text, size_t byteLength, void SkCanvas::drawPosText(const void* text, size_t byteLength,
const SkPoint pos[], const SkPaint& paint) { const SkPoint pos[], const SkPaint& paint) {
CHECK_NOTHING_TO_DRAW(paint);
LOOPER_BEGIN(paint, SkDrawFilter::kText_Type) LOOPER_BEGIN(paint, SkDrawFilter::kText_Type)
while (iter.next()) { while (iter.next()) {
@ -1564,6 +1598,8 @@ void SkCanvas::drawPosText(const void* text, size_t byteLength,
void SkCanvas::drawPosTextH(const void* text, size_t byteLength, void SkCanvas::drawPosTextH(const void* text, size_t byteLength,
const SkScalar xpos[], SkScalar constY, const SkScalar xpos[], SkScalar constY,
const SkPaint& paint) { const SkPaint& paint) {
CHECK_NOTHING_TO_DRAW(paint);
LOOPER_BEGIN(paint, SkDrawFilter::kText_Type) LOOPER_BEGIN(paint, SkDrawFilter::kText_Type)
while (iter.next()) { while (iter.next()) {
@ -1578,6 +1614,8 @@ void SkCanvas::drawPosTextH(const void* text, size_t byteLength,
void SkCanvas::drawTextOnPath(const void* text, size_t byteLength, void SkCanvas::drawTextOnPath(const void* text, size_t byteLength,
const SkPath& path, const SkMatrix* matrix, const SkPath& path, const SkMatrix* matrix,
const SkPaint& paint) { const SkPaint& paint) {
CHECK_NOTHING_TO_DRAW(paint);
LOOPER_BEGIN(paint, SkDrawFilter::kText_Type) LOOPER_BEGIN(paint, SkDrawFilter::kText_Type)
while (iter.next()) { while (iter.next()) {
@ -1592,6 +1630,7 @@ void SkCanvas::drawTextOnPath(const void* text, size_t byteLength,
void SkCanvas::drawPosTextOnPath(const void* text, size_t byteLength, void SkCanvas::drawPosTextOnPath(const void* text, size_t byteLength,
const SkPoint pos[], const SkPaint& paint, const SkPoint pos[], const SkPaint& paint,
const SkPath& path, const SkMatrix* matrix) { const SkPath& path, const SkMatrix* matrix) {
CHECK_NOTHING_TO_DRAW(paint);
LOOPER_BEGIN(paint, SkDrawFilter::kText_Type) LOOPER_BEGIN(paint, SkDrawFilter::kText_Type)
@ -1609,6 +1648,8 @@ void SkCanvas::drawVertices(VertexMode vmode, int vertexCount,
const SkColor colors[], SkXfermode* xmode, const SkColor colors[], SkXfermode* xmode,
const uint16_t indices[], int indexCount, const uint16_t indices[], int indexCount,
const SkPaint& paint) { const SkPaint& paint) {
CHECK_NOTHING_TO_DRAW(paint);
LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type) LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type)
while (iter.next()) { while (iter.next()) {
@ -1686,6 +1727,8 @@ void SkCanvas::drawRectCoords(SkScalar left, SkScalar top,
void SkCanvas::drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, void SkCanvas::drawCircle(SkScalar cx, SkScalar cy, SkScalar radius,
const SkPaint& paint) { const SkPaint& paint) {
CHECK_NOTHING_TO_DRAW(paint);
if (radius < 0) { if (radius < 0) {
radius = 0; radius = 0;
} }
@ -1708,6 +1751,8 @@ void SkCanvas::drawCircle(SkScalar cx, SkScalar cy, SkScalar radius,
void SkCanvas::drawRoundRect(const SkRect& r, SkScalar rx, SkScalar ry, void SkCanvas::drawRoundRect(const SkRect& r, SkScalar rx, SkScalar ry,
const SkPaint& paint) { const SkPaint& paint) {
CHECK_NOTHING_TO_DRAW(paint);
if (rx > 0 && ry > 0) { if (rx > 0 && ry > 0) {
if (paint.canComputeFastBounds()) { if (paint.canComputeFastBounds()) {
SkRect storage; SkRect storage;
@ -1726,6 +1771,8 @@ void SkCanvas::drawRoundRect(const SkRect& r, SkScalar rx, SkScalar ry,
} }
void SkCanvas::drawOval(const SkRect& oval, const SkPaint& paint) { void SkCanvas::drawOval(const SkRect& oval, const SkPaint& paint) {
CHECK_NOTHING_TO_DRAW(paint);
if (paint.canComputeFastBounds()) { if (paint.canComputeFastBounds()) {
SkRect storage; SkRect storage;
if (this->quickReject(paint.computeFastBounds(oval, &storage), if (this->quickReject(paint.computeFastBounds(oval, &storage),
@ -1742,6 +1789,8 @@ void SkCanvas::drawOval(const SkRect& oval, const SkPaint& paint) {
void SkCanvas::drawArc(const SkRect& oval, SkScalar startAngle, void SkCanvas::drawArc(const SkRect& oval, SkScalar startAngle,
SkScalar sweepAngle, bool useCenter, SkScalar sweepAngle, bool useCenter,
const SkPaint& paint) { const SkPaint& paint) {
CHECK_NOTHING_TO_DRAW(paint);
if (SkScalarAbs(sweepAngle) >= SkIntToScalar(360)) { if (SkScalarAbs(sweepAngle) >= SkIntToScalar(360)) {
this->drawOval(oval, paint); this->drawOval(oval, paint);
} else { } else {

View File

@ -527,8 +527,7 @@ void SkDraw::drawPoints(SkCanvas::PointMode mode, size_t count,
SkDEBUGCODE(this->validate();) SkDEBUGCODE(this->validate();)
// nothing to draw // nothing to draw
if (fClip->isEmpty() || if (fClip->isEmpty()) {
(paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
return; return;
} }
@ -697,8 +696,7 @@ void SkDraw::drawRect(const SkRect& rect, const SkPaint& paint) const {
SkDEBUGCODE(this->validate();) SkDEBUGCODE(this->validate();)
// nothing to draw // nothing to draw
if (fClip->isEmpty() || if (fClip->isEmpty()) {
(paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
return; return;
} }
@ -881,8 +879,7 @@ void SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& origPaint,
SkDEBUGCODE(this->validate();) SkDEBUGCODE(this->validate();)
// nothing to draw // nothing to draw
if (fClip->isEmpty() || if (fClip->isEmpty()) {
(origPaint.getAlpha() == 0 && origPaint.getXfermode() == NULL)) {
return; return;
} }
@ -1098,8 +1095,7 @@ void SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix,
// nothing to draw // nothing to draw
if (fClip->isEmpty() || if (fClip->isEmpty() ||
bitmap.width() == 0 || bitmap.height() == 0 || bitmap.width() == 0 || bitmap.height() == 0 ||
bitmap.getConfig() == SkBitmap::kNo_Config || bitmap.getConfig() == SkBitmap::kNo_Config) {
(origPaint.getAlpha() == 0 && origPaint.getXfermode() == NULL)) {
return; return;
} }
@ -1193,8 +1189,7 @@ void SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y,
// nothing to draw // nothing to draw
if (fClip->isEmpty() || if (fClip->isEmpty() ||
bitmap.width() == 0 || bitmap.height() == 0 || bitmap.width() == 0 || bitmap.height() == 0 ||
bitmap.getConfig() == SkBitmap::kNo_Config || bitmap.getConfig() == SkBitmap::kNo_Config) {
(origPaint.getAlpha() == 0 && origPaint.getXfermode() == NULL)) {
return; return;
} }
@ -1475,9 +1470,7 @@ void SkDraw::drawText(const char text[], size_t byteLength,
SkDEBUGCODE(this->validate();) SkDEBUGCODE(this->validate();)
// nothing to draw // nothing to draw
if (text == NULL || byteLength == 0 || if (text == NULL || byteLength == 0 || fClip->isEmpty()) {
fClip->isEmpty() ||
(paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
return; return;
} }
@ -1667,9 +1660,7 @@ void SkDraw::drawPosText(const char text[], size_t byteLength,
SkDEBUGCODE(this->validate();) SkDEBUGCODE(this->validate();)
// nothing to draw // nothing to draw
if (text == NULL || byteLength == 0 || if (text == NULL || byteLength == 0 || fClip->isEmpty()) {
fClip->isEmpty() ||
(paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
return; return;
} }
@ -1875,9 +1866,7 @@ void SkDraw::drawTextOnPath(const char text[], size_t byteLength,
SkASSERT(byteLength == 0 || text != NULL); SkASSERT(byteLength == 0 || text != NULL);
// nothing to draw // nothing to draw
if (text == NULL || byteLength == 0 || if (text == NULL || byteLength == 0 || fClip->isEmpty()) {
fClip->isEmpty() ||
(paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
return; return;
} }
@ -1923,8 +1912,7 @@ void SkDraw::drawPosTextOnPath(const char text[], size_t byteLength,
const SkPoint pos[], const SkPaint& paint, const SkPoint pos[], const SkPaint& paint,
const SkPath& path, const SkMatrix* matrix) const { const SkPath& path, const SkMatrix* matrix) const {
// nothing to draw // nothing to draw
if (text == NULL || byteLength == 0 || fClip->isEmpty() || if (text == NULL || byteLength == 0 || fClip->isEmpty()) {
(paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
return; return;
} }
@ -2222,8 +2210,7 @@ void SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
SkASSERT(0 == count || NULL != vertices); SkASSERT(0 == count || NULL != vertices);
// abort early if there is nothing to draw // abort early if there is nothing to draw
if (count < 3 || (indices && indexCount < 3) || fClip->isEmpty() || if (count < 3 || (indices && indexCount < 3) || fClip->isEmpty()) {
(paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
return; return;
} }

View File

@ -1833,3 +1833,26 @@ const SkPath* SkTextToPathIter::next(SkScalar* xpos) {
} }
return NULL; return NULL;
} }
///////////////////////////////////////////////////////////////////////////////
bool SkPaint::nothingToDraw() const {
SkXfermode::Mode mode;
if (SkXfermode::AsMode(fXfermode, &mode)) {
switch (mode) {
case SkXfermode::kSrcOver_Mode:
case SkXfermode::kSrcATop_Mode:
case SkXfermode::kDstOut_Mode:
case SkXfermode::kDstOver_Mode:
case SkXfermode::kPlus_Mode:
return 0 == this->getAlpha();
case SkXfermode::kDst_Mode:
return true;
default:
break;
}
}
return false;
}