| 1 |
/* |
| 2 |
* Copyright 2006 The Android Open Source Project |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/core/SkPath.h" |
| 9 |
|
| 10 |
#include "include/core/SkRect.h" |
| 11 |
#include "include/core/SkRegion.h" |
| 12 |
#include "include/private/base/SkAssert.h" |
| 13 |
#include "include/private/base/SkMath.h" |
| 14 |
#include "src/core/SkAAClip.h" |
| 15 |
#include "src/core/SkBlitter.h" |
| 16 |
#include "src/core/SkPathRaw.h" |
| 17 |
#include "src/core/SkRasterClip.h" |
| 18 |
#include "src/core/SkScan.h" |
| 19 |
#include "src/core/SkScanPriv.h" |
| 20 |
|
| 21 |
#include <cstdint> |
| 22 |
|
| 23 |
static SkIRect safeRoundOut(const SkRect& src) { |
| 24 |
// roundOut will pin huge floats to max/min int |
| 25 |
SkIRect dst = src.roundOut(); |
| 26 |
|
| 27 |
// intersect with a smaller huge rect, so the rect will not be considered empty for being |
| 28 |
// too large. e.g. { -SK_MaxS32 ... SK_MaxS32 } is considered empty because its width |
| 29 |
// exceeds signed 32bit. |
| 30 |
const int32_t limit = SK_MaxS32 >> SK_SUPERSAMPLE_SHIFT; |
| 31 |
(void)dst.intersect({ -limit, -limit, limit, limit}); |
| 32 |
|
| 33 |
return dst; |
| 34 |
} |
| 35 |
|
| 36 |
static int overflows_short_shift(int value, int shift) { |
| 37 |
const int s = 16 + shift; |
| 38 |
return (SkLeftShift(value, s) >> s) - value; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
Would any of the coordinates of this rectangle not fit in a short, |
| 43 |
when left-shifted by shift? |
| 44 |
*/ |
| 45 |
static int rect_overflows_short_shift(SkIRect rect, int shift) { |
| 46 |
SkASSERT(!overflows_short_shift(8191, shift)); |
| 47 |
SkASSERT(overflows_short_shift(8192, shift)); |
| 48 |
SkASSERT(!overflows_short_shift(32767, 0)); |
| 49 |
SkASSERT(overflows_short_shift(32768, 0)); |
| 50 |
|
| 51 |
// Since we expect these to succeed, we bit-or together |
| 52 |
// for a tiny extra bit of speed. |
| 53 |
return overflows_short_shift(rect.fLeft, shift) | |
| 54 |
overflows_short_shift(rect.fRight, shift) | |
| 55 |
overflows_short_shift(rect.fTop, shift) | |
| 56 |
overflows_short_shift(rect.fBottom, shift); |
| 57 |
} |
| 58 |
|
| 59 |
void SkScan::AntiFillPath(const SkPathRaw& path, const SkRegion& origClip, |
| 60 |
SkBlitter* blitter, bool forceRLE) { |
| 61 |
if (origClip.isEmpty()) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
const bool isInverse = path.isInverseFillType(); |
| 66 |
SkIRect ir = safeRoundOut(path.bounds()); |
| 67 |
if (ir.isEmpty()) { |
| 68 |
if (isInverse) { |
| 69 |
blitter->blitRegion(origClip); |
| 70 |
} |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// If the intersection of the path bounds and the clip bounds |
| 75 |
// will overflow 32767 when << by SHIFT, we can't supersample, |
| 76 |
// so draw without antialiasing. |
| 77 |
SkIRect clippedIR; |
| 78 |
if (isInverse) { |
| 79 |
// If the path is an inverse fill, it's going to fill the entire |
| 80 |
// clip, and we care whether the entire clip exceeds our limits. |
| 81 |
clippedIR = origClip.getBounds(); |
| 82 |
} else { |
| 83 |
if (!clippedIR.intersect(ir, origClip.getBounds())) { |
| 84 |
return; |
| 85 |
} |
| 86 |
} |
| 87 |
if (rect_overflows_short_shift(clippedIR, SK_SUPERSAMPLE_SHIFT)) { |
| 88 |
SkScan::FillPath(path, origClip, blitter); |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
// Our antialiasing can't handle a clip larger than 32767, so we restrict |
| 93 |
// the clip to that limit here. (the runs[] uses int16_t for its index). |
| 94 |
// |
| 95 |
// A more general solution (one that could also eliminate the need to |
| 96 |
// disable aa based on ir bounds (see overflows_short_shift) would be |
| 97 |
// to tile the clip/target... |
| 98 |
SkRegion tmpClipStorage; |
| 99 |
const SkRegion* clipRgn = &origClip; |
| 100 |
{ |
| 101 |
static const int32_t kMaxClipCoord = 32767; |
| 102 |
const SkIRect& bounds = origClip.getBounds(); |
| 103 |
if (bounds.fRight > kMaxClipCoord || bounds.fBottom > kMaxClipCoord) { |
| 104 |
SkIRect limit = { 0, 0, kMaxClipCoord, kMaxClipCoord }; |
| 105 |
tmpClipStorage.op(origClip, limit, SkRegion::kIntersect_Op); |
| 106 |
clipRgn = &tmpClipStorage; |
| 107 |
} |
| 108 |
} |
| 109 |
// for here down, use clipRgn, not origClip |
| 110 |
|
| 111 |
SkScanClipper clipper(blitter, clipRgn, ir); |
| 112 |
|
| 113 |
if (clipper.getBlitter() == nullptr) { // clipped out |
| 114 |
if (isInverse) { |
| 115 |
blitter->blitRegion(*clipRgn); |
| 116 |
} |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
SkASSERT(clipper.getClipRect() == nullptr || |
| 121 |
*clipper.getClipRect() == clipRgn->getBounds()); |
| 122 |
|
| 123 |
// now use the (possibly wrapped) blitter |
| 124 |
blitter = clipper.getBlitter(); |
| 125 |
|
| 126 |
if (isInverse) { |
| 127 |
sk_blit_above(blitter, ir, *clipRgn); |
| 128 |
} |
| 129 |
|
| 130 |
SkScan::AAAFillPath(path, blitter, ir, clipRgn->getBounds(), forceRLE); |
| 131 |
|
| 132 |
if (isInverse) { |
| 133 |
sk_blit_below(blitter, ir, *clipRgn); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/////////////////////////////////////////////////////////////////////////////// |
| 138 |
|
| 139 |
void SkScan::AntiFillPath(const SkPathRaw& raw, const SkRasterClip& clip, SkBlitter* blitter) { |
| 140 |
SkASSERT(raw.bounds().isFinite()); |
| 141 |
if (clip.isEmpty()) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
if (clip.isBW()) { |
| 146 |
AntiFillPath(raw, clip.bwRgn(), blitter, false); |
| 147 |
} else { |
| 148 |
SkRegion tmp; |
| 149 |
SkAAClipBlitter aaBlitter; |
| 150 |
|
| 151 |
tmp.setRect(clip.getBounds()); |
| 152 |
aaBlitter.init(blitter, &clip.aaRgn()); |
| 153 |
AntiFillPath(raw, tmp, &aaBlitter, true); // SkAAClipBlitter can blitMask, why forceRLE? |
| 154 |
} |
| 155 |
} |