Combine duplicate code in preparing clipRgn for fill_path
Bug: skia:
Change-Id: I52ed98491457aa426e2bb74a29131f4a20330017
Reviewed-on: https://skia-review.googlesource.com/19341
Commit-Queue: Yuqian Li <liyuqian@google.com>
Reviewed-by: Cary Clark <caryclark@google.com>
diff --git a/src/core/SkScan.h b/src/core/SkScan.h
index c995542..e5c6047 100644
--- a/src/core/SkScan.h
+++ b/src/core/SkScan.h
@@ -51,7 +51,6 @@
static void AntiFillXRect(const SkXRect&, const SkRasterClip&, SkBlitter*);
static void FillPath(const SkPath&, const SkRasterClip&, SkBlitter*);
static void AntiFillPath(const SkPath&, const SkRasterClip&, SkBlitter*);
- static void AAAFillPath(const SkPath&, const SkRasterClip&, SkBlitter*);
static void FrameRect(const SkRect&, const SkPoint& strokeSize,
const SkRasterClip&, SkBlitter*);
static void AntiFrameRect(const SkRect&, const SkPoint& strokeSize,
@@ -68,6 +67,9 @@
static void HairRoundPath(const SkPath&, const SkRasterClip&, SkBlitter*);
static void AntiHairRoundPath(const SkPath&, const SkRasterClip&, SkBlitter*);
+ // Needed by do_fill_path in SkScanPriv.h
+ static void FillPath(const SkPath&, const SkRegion& clip, SkBlitter*);
+
private:
friend class SkAAClip;
friend class SkRegion;
@@ -77,7 +79,6 @@
static void FillRect(const SkRect&, const SkRegion* clip, SkBlitter*);
static void AntiFillRect(const SkRect&, const SkRegion* clip, SkBlitter*);
static void AntiFillXRect(const SkXRect&, const SkRegion*, SkBlitter*);
- static void FillPath(const SkPath&, const SkRegion& clip, SkBlitter*);
static void AntiFillPath(const SkPath&, const SkRegion& clip, SkBlitter*,
bool forceRLE = false);
static void FillTriangle(const SkPoint pts[], const SkRegion*, SkBlitter*);
diff --git a/src/core/SkScanPriv.h b/src/core/SkScanPriv.h
index eca3c9c..f8d3bb2 100644
--- a/src/core/SkScanPriv.h
+++ b/src/core/SkScanPriv.h
@@ -9,6 +9,7 @@
#ifndef SkScanPriv_DEFINED
#define SkScanPriv_DEFINED
+#include "SkPath.h"
#include "SkScan.h"
#include "SkBlitter.h"
@@ -77,4 +78,132 @@
return prev;
}
+static bool fitsInsideLimit(const SkRect& r, SkScalar max) {
+ const SkScalar min = -max;
+ return r.fLeft > min && r.fTop > min &&
+ r.fRight < max && r.fBottom < max;
+}
+
+static int overflows_short_shift(int value, int shift) {
+ const int s = 16 + shift;
+ return (SkLeftShift(value, s) >> s) - value;
+}
+
+/**
+ Would any of the coordinates of this rectangle not fit in a short,
+ when left-shifted by shift?
+*/
+static int rect_overflows_short_shift(SkIRect rect, int shift) {
+ SkASSERT(!overflows_short_shift(8191, shift));
+ SkASSERT(overflows_short_shift(8192, shift));
+ SkASSERT(!overflows_short_shift(32767, 0));
+ SkASSERT(overflows_short_shift(32768, 0));
+
+ // Since we expect these to succeed, we bit-or together
+ // for a tiny extra bit of speed.
+ return overflows_short_shift(rect.fLeft, shift) |
+ overflows_short_shift(rect.fRight, shift) |
+ overflows_short_shift(rect.fTop, shift) |
+ overflows_short_shift(rect.fBottom, shift);
+}
+
+static bool safeRoundOut(const SkRect& src, SkIRect* dst, int32_t maxInt) {
+ const SkScalar maxScalar = SkIntToScalar(maxInt);
+
+ if (fitsInsideLimit(src, maxScalar)) {
+ src.roundOut(dst);
+ return true;
+ }
+ return false;
+}
+
+using FillPathFunc = std::function<void(const SkPath& path, SkBlitter* blitter, bool isInverse,
+ const SkIRect& ir, const SkRegion* clipRgn, const SkIRect* clipRect, bool forceRLE)>;
+
+static inline void do_fill_path(const SkPath& path, const SkRegion& origClip, SkBlitter* blitter,
+ bool forceRLE, const int SHIFT, FillPathFunc fillPathFunc) {
+ if (origClip.isEmpty()) {
+ return;
+ }
+
+ const bool isInverse = path.isInverseFillType();
+ SkIRect ir;
+
+ if (!safeRoundOut(path.getBounds(), &ir, SK_MaxS32 >> SHIFT)) {
+ // Bounds can't fit in SkIRect; we'll return without drawing
+ return;
+ }
+ if (ir.isEmpty()) {
+ if (isInverse) {
+ blitter->blitRegion(origClip);
+ }
+ return;
+ }
+
+ // If the intersection of the path bounds and the clip bounds
+ // will overflow 32767 when << by SHIFT, we can't supersample,
+ // so draw without antialiasing.
+ SkIRect clippedIR;
+ if (isInverse) {
+ // If the path is an inverse fill, it's going to fill the entire
+ // clip, and we care whether the entire clip exceeds our limits.
+ clippedIR = origClip.getBounds();
+ } else {
+ if (!clippedIR.intersect(ir, origClip.getBounds())) {
+ return;
+ }
+ }
+ if (rect_overflows_short_shift(clippedIR, SHIFT)) {
+ SkScan::FillPath(path, origClip, blitter);
+ return;
+ }
+
+ // Our antialiasing can't handle a clip larger than 32767, so we restrict
+ // the clip to that limit here. (the runs[] uses int16_t for its index).
+ //
+ // A more general solution (one that could also eliminate the need to
+ // disable aa based on ir bounds (see overflows_short_shift) would be
+ // to tile the clip/target...
+ SkRegion tmpClipStorage;
+ const SkRegion* clipRgn = &origClip;
+ {
+ static const int32_t kMaxClipCoord = 32767;
+ const SkIRect& bounds = origClip.getBounds();
+ if (bounds.fRight > kMaxClipCoord || bounds.fBottom > kMaxClipCoord) {
+ SkIRect limit = { 0, 0, kMaxClipCoord, kMaxClipCoord };
+ tmpClipStorage.op(origClip, limit, SkRegion::kIntersect_Op);
+ clipRgn = &tmpClipStorage;
+ }
+ }
+ // for here down, use clipRgn, not origClip
+
+ SkScanClipper clipper(blitter, clipRgn, ir);
+ const SkIRect* clipRect = clipper.getClipRect();
+
+ if (clipper.getBlitter() == nullptr) { // clipped out
+ if (isInverse) {
+ blitter->blitRegion(*clipRgn);
+ }
+ return;
+ }
+
+ SkASSERT(clipper.getClipRect() == nullptr ||
+ *clipper.getClipRect() == clipRgn->getBounds());
+
+ // now use the (possibly wrapped) blitter
+ blitter = clipper.getBlitter();
+
+ if (isInverse) {
+ sk_blit_above(blitter, ir, *clipRgn);
+ }
+
+ SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop);
+
+ fillPathFunc(path, blitter, isInverse, ir, clipRgn, clipRect, forceRLE);
+
+ if (isInverse) {
+ sk_blit_below(blitter, ir, *clipRgn);
+ }
+}
+
#endif
diff --git a/src/core/SkScan_AAAPath.cpp b/src/core/SkScan_AAAPath.cpp
index cb38d61..7a52922 100644
--- a/src/core/SkScan_AAAPath.cpp
+++ b/src/core/SkScan_AAAPath.cpp
@@ -1682,152 +1682,36 @@
///////////////////////////////////////////////////////////////////////////////
-static int overflows_short_shift(int value, int shift) {
- const int s = 16 + shift;
- return (SkLeftShift(value, s) >> s) - value;
-}
-
-/**
- Would any of the coordinates of this rectangle not fit in a short,
- when left-shifted by shift?
-*/
-static int rect_overflows_short_shift(SkIRect rect, int shift) {
- SkASSERT(!overflows_short_shift(8191, 2));
- SkASSERT(overflows_short_shift(8192, 2));
- SkASSERT(!overflows_short_shift(32767, 0));
- SkASSERT(overflows_short_shift(32768, 0));
-
- // Since we expect these to succeed, we bit-or together
- // for a tiny extra bit of speed.
- return overflows_short_shift(rect.fLeft, 2) |
- overflows_short_shift(rect.fRight, 2) |
- overflows_short_shift(rect.fTop, 2) |
- overflows_short_shift(rect.fBottom, 2);
-}
-
-static bool fitsInsideLimit(const SkRect& r, SkScalar max) {
- const SkScalar min = -max;
- return r.fLeft > min && r.fTop > min &&
- r.fRight < max && r.fBottom < max;
-}
-
-static bool safeRoundOut(const SkRect& src, SkIRect* dst, int32_t maxInt) {
- const SkScalar maxScalar = SkIntToScalar(maxInt);
-
- if (fitsInsideLimit(src, maxScalar)) {
- src.roundOut(dst);
- return true;
- }
- return false;
-}
-
void SkScan::AAAFillPath(const SkPath& path, const SkRegion& origClip, SkBlitter* blitter,
bool forceRLE) {
- if (origClip.isEmpty()) {
- return;
- }
-
- const bool isInverse = path.isInverseFillType();
- SkIRect ir;
- if (!safeRoundOut(path.getBounds(), &ir, SK_MaxS32 >> 2)) {
- return;
- }
- if (ir.isEmpty()) {
- if (isInverse) {
- blitter->blitRegion(origClip);
+ FillPathFunc fillPathFunc = [](const SkPath& path, SkBlitter* blitter, bool isInverse,
+ const SkIRect& ir, const SkRegion* clipRgn, const SkIRect* clipRect, bool forceRLE){
+ // The mask blitter (where we store intermediate alpha values directly in a mask, and then
+ // call the real blitter once in the end to blit the whole mask) is faster than the RLE
+ // blitter when the blit region is small enough (i.e., canHandleRect(ir)).
+ // When isInverse is true, the blit region is no longer ir so we won't use the mask blitter.
+ // The caller may also use the forceRLE flag to force not using the mask blitter.
+ if (MaskAdditiveBlitter::canHandleRect(ir) && !isInverse && !forceRLE) {
+ MaskAdditiveBlitter additiveBlitter(blitter, ir, *clipRgn, isInverse);
+ aaa_fill_path(path, clipRgn->getBounds(), &additiveBlitter, ir.fTop, ir.fBottom,
+ clipRect == nullptr, true, forceRLE);
+ } else if (!isInverse && path.isConvex()) {
+ // If the filling area is convex (i.e., path.isConvex && !isInverse), our simpler
+ // aaa_walk_convex_edges won't generate alphas above 255. Hence we don't need
+ // SafeRLEAdditiveBlitter (which is slow due to clamping). The basic RLE blitter
+ // RunBasedAdditiveBlitter would suffice.
+ RunBasedAdditiveBlitter additiveBlitter(blitter, ir, *clipRgn, isInverse);
+ aaa_fill_path(path, clipRgn->getBounds(), &additiveBlitter, ir.fTop, ir.fBottom,
+ clipRect == nullptr, false, forceRLE);
+ } else {
+ // If the filling area might not be convex, the more involved aaa_walk_edges would
+ // be called and we have to clamp the alpha downto 255. The SafeRLEAdditiveBlitter
+ // does that at a cost of performance.
+ SafeRLEAdditiveBlitter additiveBlitter(blitter, ir, *clipRgn, isInverse);
+ aaa_fill_path(path, clipRgn->getBounds(), &additiveBlitter, ir.fTop, ir.fBottom,
+ clipRect == nullptr, false, forceRLE);
}
- return;
- }
+ };
- SkIRect clippedIR;
- if (isInverse) {
- // If the path is an inverse fill, it's going to fill the entire
- // clip, and we care whether the entire clip exceeds our limits.
- clippedIR = origClip.getBounds();
- } else {
- if (!clippedIR.intersect(ir, origClip.getBounds())) {
- return;
- }
- }
- // If the intersection of the path bounds and the clip bounds
- // will overflow 32767 when << by 2, our SkFixed will overflow,
- // so draw without antialiasing.
- if (rect_overflows_short_shift(clippedIR, 2)) {
- SkScan::FillPath(path, origClip, blitter);
- return;
- }
-
- // Our antialiasing can't handle a clip larger than 32767, so we restrict
- // the clip to that limit here. (the runs[] uses int16_t for its index).
- //
- // A more general solution (one that could also eliminate the need to
- // disable aa based on ir bounds (see overflows_short_shift) would be
- // to tile the clip/target...
- SkRegion tmpClipStorage;
- const SkRegion* clipRgn = &origClip;
- {
- static const int32_t kMaxClipCoord = 32767;
- const SkIRect& bounds = origClip.getBounds();
- if (bounds.fRight > kMaxClipCoord || bounds.fBottom > kMaxClipCoord) {
- SkIRect limit = { 0, 0, kMaxClipCoord, kMaxClipCoord };
- tmpClipStorage.op(origClip, limit, SkRegion::kIntersect_Op);
- clipRgn = &tmpClipStorage;
- }
- }
- // for here down, use clipRgn, not origClip
-
- SkScanClipper clipper(blitter, clipRgn, ir);
- const SkIRect* clipRect = clipper.getClipRect();
-
- if (clipper.getBlitter() == nullptr) { // clipped out
- if (isInverse) {
- blitter->blitRegion(*clipRgn);
- }
- return;
- }
-
- // now use the (possibly wrapped) blitter
- blitter = clipper.getBlitter();
-
- if (isInverse) {
- sk_blit_above(blitter, ir, *clipRgn);
- }
-
- SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop);
-
- if (MaskAdditiveBlitter::canHandleRect(ir) && !isInverse && !forceRLE) {
- MaskAdditiveBlitter additiveBlitter(blitter, ir, *clipRgn, isInverse);
- aaa_fill_path(path, clipRgn->getBounds(), &additiveBlitter, ir.fTop, ir.fBottom,
- clipRect == nullptr, true, forceRLE);
- } else if (!isInverse && path.isConvex()) {
- RunBasedAdditiveBlitter additiveBlitter(blitter, ir, *clipRgn, isInverse);
- aaa_fill_path(path, clipRgn->getBounds(), &additiveBlitter, ir.fTop, ir.fBottom,
- clipRect == nullptr, false, forceRLE);
- } else {
- SafeRLEAdditiveBlitter additiveBlitter(blitter, ir, *clipRgn, isInverse);
- aaa_fill_path(path, clipRgn->getBounds(), &additiveBlitter, ir.fTop, ir.fBottom,
- clipRect == nullptr, false, forceRLE);
- }
-
- if (isInverse) {
- sk_blit_below(blitter, ir, *clipRgn);
- }
-}
-
-// This almost copies SkScan::AntiFillPath
-void SkScan::AAAFillPath(const SkPath& path, const SkRasterClip& clip, SkBlitter* blitter) {
- if (clip.isEmpty()) {
- return;
- }
-
- if (clip.isBW()) {
- AAAFillPath(path, clip.bwRgn(), blitter);
- } else {
- SkRegion tmp;
- SkAAClipBlitter aaBlitter;
-
- tmp.setRect(clip.getBounds());
- aaBlitter.init(blitter, &clip.aaRgn());
- AAAFillPath(path, tmp, &aaBlitter, true);
- }
+ do_fill_path(path, origClip, blitter, forceRLE, 2, std::move(fillPathFunc));
}
diff --git a/src/core/SkScan_AntiPath.cpp b/src/core/SkScan_AntiPath.cpp
index ac4c8c3..be91007 100644
--- a/src/core/SkScan_AntiPath.cpp
+++ b/src/core/SkScan_AntiPath.cpp
@@ -582,153 +582,34 @@
///////////////////////////////////////////////////////////////////////////////
-static bool fitsInsideLimit(const SkRect& r, SkScalar max) {
- const SkScalar min = -max;
- return r.fLeft > min && r.fTop > min &&
- r.fRight < max && r.fBottom < max;
-}
-
-static int overflows_short_shift(int value, int shift) {
- const int s = 16 + shift;
- return (SkLeftShift(value, s) >> s) - value;
-}
-
-/**
- Would any of the coordinates of this rectangle not fit in a short,
- when left-shifted by shift?
-*/
-static int rect_overflows_short_shift(SkIRect rect, int shift) {
- SkASSERT(!overflows_short_shift(8191, SHIFT));
- SkASSERT(overflows_short_shift(8192, SHIFT));
- SkASSERT(!overflows_short_shift(32767, 0));
- SkASSERT(overflows_short_shift(32768, 0));
-
- // Since we expect these to succeed, we bit-or together
- // for a tiny extra bit of speed.
- return overflows_short_shift(rect.fLeft, SHIFT) |
- overflows_short_shift(rect.fRight, SHIFT) |
- overflows_short_shift(rect.fTop, SHIFT) |
- overflows_short_shift(rect.fBottom, SHIFT);
-}
-
-static bool safeRoundOut(const SkRect& src, SkIRect* dst, int32_t maxInt) {
- const SkScalar maxScalar = SkIntToScalar(maxInt);
-
- if (fitsInsideLimit(src, maxScalar)) {
- src.roundOut(dst);
- return true;
- }
- return false;
-}
-
void SkScan::AntiFillPath(const SkPath& path, const SkRegion& origClip,
SkBlitter* blitter, bool forceRLE) {
- if (origClip.isEmpty()) {
- return;
- }
-
- const bool isInverse = path.isInverseFillType();
- SkIRect ir;
-
- if (!safeRoundOut(path.getBounds(), &ir, SK_MaxS32 >> SHIFT)) {
-#if 0
- const SkRect& r = path.getBounds();
- SkDebugf("--- bounds can't fit in SkIRect\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
-#endif
- return;
- }
- if (ir.isEmpty()) {
- if (isInverse) {
- blitter->blitRegion(origClip);
+ FillPathFunc fillPathFunc = [](const SkPath& path, SkBlitter* blitter, bool isInverse,
+ const SkIRect& ir, const SkRegion* clipRgn, const SkIRect* clipRect, bool forceRLE){
+ SkIRect superRect, *superClipRect = nullptr;
+ if (clipRect) {
+ superRect.set(SkLeftShift(clipRect->fLeft, SHIFT),
+ SkLeftShift(clipRect->fTop, SHIFT),
+ SkLeftShift(clipRect->fRight, SHIFT),
+ SkLeftShift(clipRect->fBottom, SHIFT));
+ superClipRect = &superRect;
}
- return;
- }
- // If the intersection of the path bounds and the clip bounds
- // will overflow 32767 when << by SHIFT, we can't supersample,
- // so draw without antialiasing.
- SkIRect clippedIR;
- if (isInverse) {
- // If the path is an inverse fill, it's going to fill the entire
- // clip, and we care whether the entire clip exceeds our limits.
- clippedIR = origClip.getBounds();
- } else {
- if (!clippedIR.intersect(ir, origClip.getBounds())) {
- return;
- }
- }
- if (rect_overflows_short_shift(clippedIR, SHIFT)) {
- SkScan::FillPath(path, origClip, blitter);
- return;
- }
-
- // Our antialiasing can't handle a clip larger than 32767, so we restrict
- // the clip to that limit here. (the runs[] uses int16_t for its index).
- //
- // A more general solution (one that could also eliminate the need to
- // disable aa based on ir bounds (see overflows_short_shift) would be
- // to tile the clip/target...
- SkRegion tmpClipStorage;
- const SkRegion* clipRgn = &origClip;
- {
- static const int32_t kMaxClipCoord = 32767;
- const SkIRect& bounds = origClip.getBounds();
- if (bounds.fRight > kMaxClipCoord || bounds.fBottom > kMaxClipCoord) {
- SkIRect limit = { 0, 0, kMaxClipCoord, kMaxClipCoord };
- tmpClipStorage.op(origClip, limit, SkRegion::kIntersect_Op);
- clipRgn = &tmpClipStorage;
+ // MaskSuperBlitter can't handle drawing outside of ir, so we can't use it
+ // if we're an inverse filltype
+ if (!isInverse && MaskSuperBlitter::CanHandleRect(ir) && !forceRLE) {
+ MaskSuperBlitter superBlit(blitter, ir, *clipRgn, isInverse);
+ SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop);
+ sk_fill_path(path, clipRgn->getBounds(), &superBlit, ir.fTop, ir.fBottom, SHIFT,
+ superClipRect == nullptr);
+ } else {
+ SuperBlitter superBlit(blitter, ir, *clipRgn, isInverse);
+ sk_fill_path(path, clipRgn->getBounds(), &superBlit, ir.fTop, ir.fBottom, SHIFT,
+ superClipRect == nullptr);
}
- }
- // for here down, use clipRgn, not origClip
+ };
- SkScanClipper clipper(blitter, clipRgn, ir);
- const SkIRect* clipRect = clipper.getClipRect();
-
- if (clipper.getBlitter() == nullptr) { // clipped out
- if (isInverse) {
- blitter->blitRegion(*clipRgn);
- }
- return;
- }
-
- SkASSERT(clipper.getClipRect() == nullptr ||
- *clipper.getClipRect() == clipRgn->getBounds());
-
- // now use the (possibly wrapped) blitter
- blitter = clipper.getBlitter();
-
- if (isInverse) {
- sk_blit_above(blitter, ir, *clipRgn);
- }
-
- SkIRect superRect, *superClipRect = nullptr;
-
- if (clipRect) {
- superRect.set(SkLeftShift(clipRect->fLeft, SHIFT),
- SkLeftShift(clipRect->fTop, SHIFT),
- SkLeftShift(clipRect->fRight, SHIFT),
- SkLeftShift(clipRect->fBottom, SHIFT));
- superClipRect = &superRect;
- }
-
- SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop);
-
- // MaskSuperBlitter can't handle drawing outside of ir, so we can't use it
- // if we're an inverse filltype
- if (!isInverse && MaskSuperBlitter::CanHandleRect(ir) && !forceRLE) {
- MaskSuperBlitter superBlit(blitter, ir, *clipRgn, isInverse);
- SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop);
- sk_fill_path(path, clipRgn->getBounds(), &superBlit, ir.fTop, ir.fBottom, SHIFT,
- superClipRect == nullptr);
- } else {
- SuperBlitter superBlit(blitter, ir, *clipRgn, isInverse);
- sk_fill_path(path, clipRgn->getBounds(), &superBlit, ir.fTop, ir.fBottom, SHIFT,
- superClipRect == nullptr);
- }
-
- if (isInverse) {
- sk_blit_below(blitter, ir, *clipRgn);
- }
+ do_fill_path(path, origClip, blitter, forceRLE, SHIFT, std::move(fillPathFunc));
}
///////////////////////////////////////////////////////////////////////////////
@@ -768,25 +649,27 @@
void SkScan::AntiFillPath(const SkPath& path, const SkRasterClip& clip,
SkBlitter* blitter) {
- // Do not use AAA if path is too complicated:
- // there won't be any speedup or significant visual improvement.
- if (gSkUseAnalyticAA.load() && suitableForAAA(path)) {
- SkScan::AAAFillPath(path, clip, blitter);
- return;
- }
-
if (clip.isEmpty()) {
return;
}
+ using FillPathProc = void(*)(const SkPath&, const SkRegion&, SkBlitter*, bool);
+ FillPathProc fillPathProc = &SkScan::AntiFillPath;
+
+ // Do not use AAA if path is too complicated:
+ // there won't be any speedup or significant visual improvement.
+ if (gSkUseAnalyticAA.load() && suitableForAAA(path)) {
+ fillPathProc = &SkScan::AAAFillPath;
+ }
+
if (clip.isBW()) {
- AntiFillPath(path, clip.bwRgn(), blitter);
+ fillPathProc(path, clip.bwRgn(), blitter, false);
} else {
SkRegion tmp;
SkAAClipBlitter aaBlitter;
tmp.setRect(clip.getBounds());
aaBlitter.init(blitter, &clip.aaRgn());
- SkScan::AntiFillPath(path, tmp, &aaBlitter, true);
+ fillPathProc(path, tmp, &aaBlitter, true);
}
}