| 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 |
#ifndef SkAnalyticEdge_DEFINED |
| 9 |
#define SkAnalyticEdge_DEFINED |
| 10 |
|
| 11 |
#include "include/private/base/SkAssert.h" |
| 12 |
#include "include/private/base/SkDebug.h" |
| 13 |
#include "include/private/base/SkFixed.h" |
| 14 |
#include "include/private/base/SkSafe32.h" |
| 15 |
|
| 16 |
#include <cstdint> |
| 17 |
|
| 18 |
struct SkPoint; |
| 19 |
|
| 20 |
struct SkAnalyticEdge { |
| 21 |
// Similar to SkEdge, the conic edges will be converted to quadratic edges |
| 22 |
enum class Type : int8_t { |
| 23 |
kLine, |
| 24 |
kQuad, |
| 25 |
kCubic, |
| 26 |
}; |
| 27 |
enum class Winding : int8_t { |
| 28 |
kCW = 1, // clockwise |
| 29 |
kCCW = -1, // counter clockwise |
| 30 |
}; |
| 31 |
|
| 32 |
SkAnalyticEdge* fNext; |
| 33 |
SkAnalyticEdge* fPrev; |
| 34 |
|
| 35 |
SkFixed fX; |
| 36 |
SkFixed fDX; |
| 37 |
SkFixed fUpperX; // The x value when y = fUpperY |
| 38 |
SkFixed fY; // The current y |
| 39 |
SkFixed fUpperY; // The upper bound of y (our edge is from y = fUpperY to y = fLowerY) |
| 40 |
SkFixed fLowerY; // The lower bound of y (our edge is from y = fUpperY to y = fLowerY) |
| 41 |
SkFixed fDY; // abs(1/fDX); may be SK_MaxS32 when fDX is close to 0. |
| 42 |
// fDY is only used for blitting trapezoids. |
| 43 |
|
| 44 |
Type fEdgeType; // Remembers the *initial* edge type |
| 45 |
|
| 46 |
int8_t fCurveCount; // only used by kQuad(+) and kCubic(-) |
| 47 |
uint8_t fCurveShift; // appled to all Dx/DDx/DDDx except for fCubicDShift exception |
| 48 |
Winding fWinding; |
| 49 |
|
| 50 |
static constexpr int kDefaultAccuracy = 2; // default accuracy for snapping |
| 51 |
|
| 52 |
static inline SkFixed SnapY(SkFixed y) { |
| 53 |
constexpr int accuracy = kDefaultAccuracy; |
| 54 |
// This approach is safer than left shift, round, then right shift |
| 55 |
return ((unsigned)y + (SK_Fixed1 >> (accuracy + 1))) >> (16 - accuracy) << (16 - accuracy); |
| 56 |
} |
| 57 |
|
| 58 |
// Update fX, fY of this edge so fY = y |
| 59 |
inline void goY(SkFixed y) { |
| 60 |
if (y == fY + SK_Fixed1) { |
| 61 |
fX = fX + fDX; |
| 62 |
fY = y; |
| 63 |
} else if (y != fY) { |
| 64 |
// Drop lower digits as our alpha only has 8 bits |
| 65 |
// (fDX and y - fUpperY may be greater than SK_Fixed1) |
| 66 |
fX = fUpperX + SkFixedMul(fDX, y - fUpperY); |
| 67 |
fY = y; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
inline void goY(SkFixed y, int yShift) { |
| 72 |
SkASSERT(yShift >= 0 && yShift <= kDefaultAccuracy); |
| 73 |
SkASSERT(fDX == 0 || y - fY == SK_Fixed1 >> yShift); |
| 74 |
fY = y; |
| 75 |
fX += fDX >> yShift; |
| 76 |
} |
| 77 |
|
| 78 |
bool setLine(const SkPoint& p0, const SkPoint& p1); |
| 79 |
bool updateLine(SkFixed ax, SkFixed ay, SkFixed bx, SkFixed by, SkFixed slope); |
| 80 |
|
| 81 |
// return true if we're NOT done with this edge |
| 82 |
bool update(SkFixed last_y); |
| 83 |
|
| 84 |
#ifdef SK_DEBUG |
| 85 |
void dump() const { |
| 86 |
SkDebugf("edge: upperY:%d lowerY:%d y:%g x:%g dx:%g w:%d\n", |
| 87 |
fUpperY, |
| 88 |
fLowerY, |
| 89 |
SkFixedToFloat(fY), |
| 90 |
SkFixedToFloat(fX), |
| 91 |
SkFixedToFloat(fDX), |
| 92 |
static_cast<int8_t>(fWinding)); |
| 93 |
} |
| 94 |
|
| 95 |
void validate() const { |
| 96 |
SkASSERT(fPrev && fNext); |
| 97 |
SkASSERT(fPrev->fNext == this); |
| 98 |
SkASSERT(fNext->fPrev == this); |
| 99 |
|
| 100 |
SkASSERT(fUpperY < fLowerY); |
| 101 |
SkASSERT(fWinding == Winding::kCW || fWinding == Winding::kCCW); |
| 102 |
} |
| 103 |
#endif |
| 104 |
}; |
| 105 |
|
| 106 |
struct SkAnalyticQuadraticEdge : public SkAnalyticEdge { |
| 107 |
SkFixed fQx, fQy; |
| 108 |
SkFixed fQDx, fQDy; |
| 109 |
SkFixed fQDDx, fQDDy; |
| 110 |
SkFixed fQLastX, fQLastY; |
| 111 |
|
| 112 |
// snap y to integer points in the middle of the curve to accelerate AAA path filling |
| 113 |
SkFixed fSnappedX, fSnappedY; |
| 114 |
|
| 115 |
bool setQuadraticWithoutUpdate(const SkPoint pts[3], int shiftUp); |
| 116 |
bool setQuadratic(const SkPoint pts[3]); |
| 117 |
bool updateQuadratic(); |
| 118 |
inline void keepContinuous() { |
| 119 |
// We use fX as the starting x to ensure the continuouty. |
| 120 |
// Without it, we may break the sorted edge list. |
| 121 |
SkASSERT(SkAbs32(fX - SkFixedMul(fY - fSnappedY, fDX) - fSnappedX) < SK_Fixed1); |
| 122 |
SkASSERT(SkAbs32(fY - fSnappedY) < SK_Fixed1); // This may differ due to smooth jump |
| 123 |
fSnappedX = fX; |
| 124 |
fSnappedY = fY; |
| 125 |
} |
| 126 |
}; |
| 127 |
|
| 128 |
struct SkAnalyticCubicEdge : public SkAnalyticEdge { |
| 129 |
SkFixed fCx, fCy; |
| 130 |
SkFixed fCDx, fCDy; |
| 131 |
SkFixed fCDDx, fCDDy; |
| 132 |
SkFixed fCDDDx, fCDDDy; |
| 133 |
SkFixed fCLastX, fCLastY; |
| 134 |
|
| 135 |
SkFixed fSnappedY; // to make sure that y is increasing with smooth jump and snapping |
| 136 |
|
| 137 |
uint8_t fCubicDShift; // applied to fCDx and fCDy |
| 138 |
|
| 139 |
bool setCubicWithoutUpdate(const SkPoint pts[4], int shiftUp); |
| 140 |
bool setCubic(const SkPoint pts[4]); |
| 141 |
bool updateCubic(); |
| 142 |
inline void keepContinuous() { |
| 143 |
SkASSERT(SkAbs32(fX - SkFixedMul(fDX, fY - SnapY(fCy)) - fCx) < SK_Fixed1); |
| 144 |
fCx = fX; |
| 145 |
fSnappedY = fY; |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
#endif |