/* * Copyright 2011 The Android Open Source Project * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file.
*/
/* Our attempt to compute the worst case "bounds" for the horizontal and vertical cases has some numerical bug in it, and we sometimes undervalue our extends. The bug is that when this happens, we will set the clip to nullptr (for speed), and thus draw outside of the clip by a pixel, which might only look bad, but it might also access memory outside of the valid range allcoated for the device bitmap.
This define enables our fix to outset our "bounds" by 1, thus avoiding the chance of the bug, but at the cost of sometimes taking the rectblitter case (i.e. not setting the clip to nullptr) when we might not actually need to. If we can improve/fix the actual calculations, then we can remove this step.
*/ #define OUTSET_BEFORE_CLIP_TEST true
if (gInit == false) { for (int i = 0; i < 256; i++) {
SkFixed n = i * 257;
n += n >> 15;
SkASSERT(n >= 0 && n <= SK_Fixed1);
n = SkFixedSqrt(n);
n = n * 255 >> 16; // SkDebugf("morph %d -> %d\n", i, n);
gGammaTable[i] = SkToU8(n);
}
gInit = true;
}
} #else #define ApplyGamma(table, alpha) SkToU8(alpha) #endif
do { // In theory, we should be able to just do this once (outside of the loop), // since aa[] and runs[] are supposed" to be const when we call the blitter. // In reality, some wrapper-blitters (e.g. SkRgnClipBlitter) cast away that // constness, and modify the buffers in-place. Hence the need to be defensive // here and reseed the aa value.
aa[0] = ApplyGamma(gGammaTable, alpha);
int n = count; if (n > HLINE_STACK_BUFFER) {
n = HLINE_STACK_BUFFER;
}
runs[0] = SkToS16(n);
runs[n] = 0;
blitter->blitAntiH(x, y, aa, runs);
x += n;
count -= n;
} while (count > 0);
}
fy += SK_Fixed1/2;
SkBlitter* blitter = this->getBlitter(); do { int lower_y = fy >> 16;
uint8_t a = (uint8_t)((fy >> 8) & 0xFF);
blitter->blitAntiV2(x, lower_y - 1, 255 - a, a);
fy += dy;
} while (++x < stopx);
return fy - SK_Fixed1/2;
}
};
class VLine_SkAntiHairBlitter : public SkAntiHairBlitter { public:
SkFixed drawCap(int y, SkFixed fx, SkFixed dx, int mod64) override {
SkASSERT(0 == dx);
fx += SK_Fixed1/2;
int x = fx >> 16; int a = (uint8_t)((fx >> 8) & 0xFF);
unsigned ma = SmallDot6Scale(a, mod64); if (ma) {
this->getBlitter()->blitV(x, y, 1, ma);
}
ma = SmallDot6Scale(255 - a, mod64); if (ma) {
this->getBlitter()->blitV(x - 1, y, 1, ma);
}
return fx - SK_Fixed1/2;
}
SkFixed drawLine(int y, int stopy, SkFixed fx, SkFixed dx) override {
SkASSERT(y < stopy);
SkASSERT(0 == dx);
fx += SK_Fixed1/2;
int x = fx >> 16; int a = (uint8_t)((fx >> 8) & 0xFF);
if (a) {
this->getBlitter()->blitV(x, y, stopy - y, a);
}
a = 255 - a; if (a) {
this->getBlitter()->blitV(x - 1, y, stopy - y, a);
}
return fx - SK_Fixed1/2;
}
};
class Vertish_SkAntiHairBlitter : public SkAntiHairBlitter { public:
SkFixed drawCap(int y, SkFixed fx, SkFixed dx, int mod64) override {
fx += SK_Fixed1/2;
int x = fx >> 16;
uint8_t a = (uint8_t)((fx >> 8) & 0xFF);
this->getBlitter()->blitAntiH2(x - 1, y,
SmallDot6Scale(255 - a, mod64), SmallDot6Scale(a, mod64));
return fx + dx - SK_Fixed1/2;
}
SkFixed drawLine(int y, int stopy, SkFixed fx, SkFixed dx) override {
SkASSERT(y < stopy);
fx += SK_Fixed1/2; do { int x = fx >> 16;
uint8_t a = (uint8_t)((fx >> 8) & 0xFF);
this->getBlitter()->blitAntiH2(x - 1, y, 255 - a, a);
fx += dx;
} while (++y < stopy);
/* * We want the fractional part of ordinate, but we want multiples of 64 to * return 64, not 0, so we can't just say (ordinate & 63). * We basically want to compute those bits, and if they're 0, return 64. * We can do that w/o a branch with an extra sub and add.
*/ staticint contribution_64(SkFDot6 ordinate) { #if 0 int result = ordinate & 63; if (0 == result) {
result = 64;
} #else int result = ((ordinate - 1) & 63) + 1; #endif
SkASSERT(result > 0 && result <= 64); return result;
}
staticvoid do_anti_hairline(SkFDot6 x0, SkFDot6 y0, SkFDot6 x1, SkFDot6 y1, const SkIRect* clip, SkBlitter* blitter) { // check for integer NaN (0x80000000) which we can't handle (can't negate it) // It appears typically from a huge float (inf or nan) being converted to int. // If we see it, just don't draw. if (any_bad_ints(x0, y0, x1, y1)) { return;
}
// The caller must clip the line to [-32767.0 ... 32767.0] ahead of time // (in dot6 format)
SkASSERT(canConvertFDot6ToFixed(x0));
SkASSERT(canConvertFDot6ToFixed(y0));
SkASSERT(canConvertFDot6ToFixed(x1));
SkASSERT(canConvertFDot6ToFixed(y1));
if (SkAbs32(x1 - x0) > SkIntToFDot6(511) || SkAbs32(y1 - y0) > SkIntToFDot6(511)) { /* instead of (x0 + x1) >> 1, we shift each separately. This is less precise, but avoids overflowing the intermediate result if the values are huge. A better fix might be to clip the original pts directly (i.e. do the divide), so we don't spend time subdividing huge lines at all.
*/ int hx = (x0 >> 1) + (x1 >> 1); int hy = (y0 >> 1) + (y1 >> 1);
do_anti_hairline(x0, y0, hx, hy, clip, blitter);
do_anti_hairline(hx, hy, x1, y1, clip, blitter); return;
}
int scaleStart, scaleStop; int istart, istop;
SkFixed fstart, slope;
if (SkAbs32(x1 - x0) > SkAbs32(y1 - y0)) { // mostly horizontal if (x0 > x1) { // we want to go left-to-right using std::swap;
swap(x0, x1);
swap(y0, y1);
}
SkRect clipBounds; if (clip) {
clipBounds.set(clip->getBounds()); /* We perform integral clipping later on, but we do a scalar clip first to ensure that our coordinates are expressible in fixed/integers.
antialiased hairlines can draw up to 1/2 of a pixel outside of their bounds, so we need to outset the clip before calling the clipper. To make the numerics safer, we outset by a whole pixel, since the 1/2 pixel boundary is important to the antihair blitter, we don't want to risk numerical fate by chopping on that edge.
*/
clipBounds.outset(SK_Scalar1, SK_Scalar1);
}
for (int i = 0; i < arrayCount - 1; ++i) {
SkPoint pts[2];
// We have to pre-clip the line to fit in a SkFixed, so we just chop // the line. TODO find a way to actually draw beyond that range. if (!SkLineClipper::IntersectLine(&array[i], fixedBounds, pts)) { continue;
}
if (clip && !SkLineClipper::IntersectLine(pts, clipBounds, pts)) { continue;
}
if (L & 0xFF) {
blitter->blitV(left, top, 1, SkAlphaMul(alpha, 256 - (L & 0xFF)));
left += 1;
}
int rite = R >> 8; int width = rite - left; if (width > 0) {
call_hline_blitter(blitter, left, top, width, alpha);
} if (R & 0xFF) {
blitter->blitV(rite, top, 1, SkAlphaMul(alpha, R & 0xFF));
}
}
staticvoid antifilldot8(FDot8 L, FDot8 T, FDot8 R, FDot8 B, SkBlitter* blitter, bool fillInner) { // check for empty now that we're in our reduced precision space if (L >= R || T >= B) { return;
} int top = T >> 8; if (top == ((B - 1) >> 8)) { // just one scanline high
do_scanline(L, top, R, B - T - 1, blitter); return;
}
if (T & 0xFF) {
do_scanline(L, top, R, 256 - (T & 0xFF), blitter);
top += 1;
}
int bot = B >> 8; int height = bot - top; if (height > 0) { int left = L >> 8; if (left == ((R - 1) >> 8)) { // just 1-pixel wide
blitter->blitV(left, top, height, R - L - 1);
} else { if (L & 0xFF) {
blitter->blitV(left, top, height, 256 - (L & 0xFF));
left += 1;
} int rite = R >> 8; int width = rite - left; if (width > 0 && fillInner) {
blitter->blitRect(left, top, width, height);
} if (R & 0xFF) {
blitter->blitV(rite, top, height, R & 0xFF);
}
}
}
if (B & 0xFF) {
do_scanline(L, bot, R, B & 0xFF, blitter);
}
}
/* This takes a float-rect, but with the key improvement that it has already been clipped, so we know that it is safe to convert it into a XRect (fixedpoint), as it won't overflow.
*/ staticvoid antifillrect(const SkRect& r, SkBlitter* blitter) {
SkXRect xr;
XRect_set(&xr, r);
antifillrect(xr, blitter);
}
/* We repeat the clipping logic of AntiFillXRect because the float rect might overflow if we blindly converted it to an XRect. This sucks that we have to repeat the clipping logic, but I don't see how to share the code/logic.
We clip r (as needed) into one or more (smaller) float rects, and then pass those to our version of antifillrect, which converts it into an XRect and then calls the blit.
*/ void SkScan::AntiFillRect(const SkRect& origR, const SkRegion* clip,
SkBlitter* blitter) { if (clip) {
SkRect newR;
newR.set(clip->getBounds()); if (!newR.intersect(origR)) { return;
}
#define SkAlphaMulRound(a, b) SkMulDiv255Round(a, b)
// calls blitRect() if the rectangle is non-empty staticvoid fillcheckrect(int L, int T, int R, int B, SkBlitter* blitter) { if (L < R && T < B) {
blitter->blitRect(L, T, R - L, B - T);
}
}
// 1 - (1 - a)*(1 - b) staticinline U8CPU InvAlphaMul(U8CPU a, U8CPU b) { // need precise rounding (not just SkAlphaMul) so that values like // a=228, b=252 don't overflow the result return SkToU8(a + b - SkAlphaMulRound(a, b));
}
staticvoid innerstrokedot8(FDot8 L, FDot8 T, FDot8 R, FDot8 B,
SkBlitter* blitter) {
SkASSERT(L < R && T < B);
int top = T >> 8; if (top == ((B - 1) >> 8)) { // just one scanline high // We want the inverse of B-T, since we're the inner-stroke int alpha = 256 - (B - T); if (alpha) {
inner_scanline(L, top, R, alpha, blitter);
} return;
}
if (T & 0xFF) {
inner_scanline(L, top, R, T & 0xFF, blitter);
top += 1;
}
int bot = B >> 8; int height = bot - top; if (height > 0) { if (L & 0xFF) {
blitter->blitV(L >> 8, top, height, L & 0xFF);
} if (R & 0xFF) {
blitter->blitV(R >> 8, top, height, ~R & 0xFF);
}
}
SkIRect outer; // set outer to the outer rect of the outer section
outer.setLTRB(FDot8Floor(outerL), FDot8Floor(outerT), FDot8Ceil(outerR), FDot8Ceil(outerB));
SkBlitterClipper clipper; if (clip) { if (clip->quickReject(outer)) { return;
} if (!clip->contains(outer)) {
blitter = clipper.apply(blitter, clip, &outer);
} // now we can ignore clip for the rest of the function
}
// in case we lost a bit with diameter/2
rx = strokeSize.fX - rx;
ry = strokeSize.fY - ry;
// For sub-unit strokes, tweak the hulls such that one of the edges coincides with the pixel // edge. This ensures that the general rect stroking logic below // a) doesn't blit the same scanline twice // b) computes the correct coverage when both edges fall within the same pixel if (strokeSize.fX < 1 || strokeSize.fY < 1) {
align_thin_stroke(outerL, innerL);
align_thin_stroke(outerT, innerT);
align_thin_stroke(innerR, outerR);
align_thin_stroke(innerB, outerB);
}
// set outer to the outer rect of the middle section
outer.setLTRB(FDot8Ceil(outerL), FDot8Ceil(outerT), FDot8Floor(outerR), FDot8Floor(outerB));
if (innerL >= innerR || innerT >= innerB) {
fillcheckrect(outer.fLeft, outer.fTop, outer.fRight, outer.fBottom,
blitter);
} else {
SkIRect inner; // set inner to the inner rect of the middle section
inner.setLTRB(FDot8Floor(innerL), FDot8Floor(innerT), FDot8Ceil(innerR), FDot8Ceil(innerB));
// now stroke the inner rect, which is similar to antifilldot8() except that // it treats the fractional coordinates with the inverse bias (since its // inner).
innerstrokedot8(innerL, innerT, innerR, innerB, blitter);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.