| 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 SkEdge_DEFINED |
| 9 |
#define SkEdge_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkPoint.h" |
| 12 |
#include "include/private/base/SkAssert.h" |
| 13 |
#include "include/private/base/SkDebug.h" |
| 14 |
#include "include/private/base/SkFixed.h" |
| 15 |
#include "include/private/base/SkMath.h" |
| 16 |
#include "src/core/SkFDot6.h" |
| 17 |
|
| 18 |
#include <cstdint> |
| 19 |
#include <utility> |
| 20 |
|
| 21 |
struct SkIRect; |
| 22 |
|
| 23 |
// This correctly favors the lower-pixel when y0 is on a 1/2 pixel boundary |
| 24 |
#define SkEdge_Compute_DY(top, y0) (SkLeftShift(top, 6) + 32 - (y0)) |
| 25 |
|
| 26 |
/** |
| 27 |
SkEdge approximates monotonic curves with one or more line segments in a way that makes |
| 28 |
computing scan lines (rows of horizontal pixels between multiple edges) efficient and easier |
| 29 |
to do. In particular, the line segments will be in terms of Y instead of a general Bezier curve |
| 30 |
which is in terms of t. |
| 31 |
|
| 32 |
The number of segments will be a power of 2 (for easy division) based on internal heuristics |
| 33 |
about how bendy the curve is. |
| 34 |
|
| 35 |
The general flow is to create an SkEdge (or one of its subclasses), use the fields to represent |
| 36 |
a line segment, and then use updateQuadratic or updateCubic to update the fields with the next |
| 37 |
line segment's values. |
| 38 |
*/ |
| 39 |
class SkEdge { |
| 40 |
public: |
| 41 |
virtual ~SkEdge() = default; |
| 42 |
enum class Type : int8_t { |
| 43 |
kLine, |
| 44 |
kQuad, |
| 45 |
kCubic, |
| 46 |
}; |
| 47 |
enum class Winding : int8_t { |
| 48 |
kCW = 1, // clockwise |
| 49 |
kCCW = -1, // counter clockwise |
| 50 |
}; |
| 51 |
|
| 52 |
// Can be used to join edges together |
| 53 |
SkEdge* fNext; |
| 54 |
SkEdge* fPrev; |
| 55 |
|
| 56 |
// The current line segment starts at (fX, fFirstY + 0.5). It has slope |
| 57 |
// fDxDy (yes, run over rise, because this is geared toward horizontal scanlines) |
| 58 |
// and stops once Y gets to fLastY + 0.5. |
| 59 |
SkFixed fX; |
| 60 |
SkFixed fDxDy; |
| 61 |
|
| 62 |
// These are integers because they represent a discrete pixel. Mathematically, these are |
| 63 |
// treated as half way inside the pixel, so 6 -> 6.5. |
| 64 |
int32_t fFirstY; |
| 65 |
int32_t fLastY; |
| 66 |
|
| 67 |
Type fEdgeType; // Remembers the *initial* edge type |
| 68 |
Winding fWinding; |
| 69 |
|
| 70 |
// Represent a straight line with an optional clip. This will always be a single segment. |
| 71 |
// Returns false if the line has height 0. |
| 72 |
bool setLine(const SkPoint& p0, const SkPoint& p1, const SkIRect* clip); |
| 73 |
// call this version if you know you don't have a clip |
| 74 |
inline bool setLine(const SkPoint& p0, const SkPoint& p1); |
| 75 |
|
| 76 |
bool hasNextSegment() const { |
| 77 |
return fSegmentCount != 0; |
| 78 |
} |
| 79 |
// Update fX, fDxDY, fFirstY, and fLastY to represent the segment. It will skip over |
| 80 |
// any lines that have a height of 0 pixels and return false if there were only 0-height |
| 81 |
// segments remaining. For quadratic and cubic curves this will involve forward-differencing |
| 82 |
// (see the subclasses for those values). |
| 83 |
virtual bool nextSegment(); |
| 84 |
|
| 85 |
uint8_t segmentsLeft() const { |
| 86 |
return fSegmentCount; |
| 87 |
} |
| 88 |
|
| 89 |
protected: |
| 90 |
inline bool updateLine(SkFixed ax, SkFixed ay, SkFixed bx, SkFixed by); |
| 91 |
|
| 92 |
uint8_t fSegmentCount; // only non-zero for Quad and Cubics |
| 93 |
// How much to shift the derivatives to multiply by deltaT when doing forward-differencing. |
| 94 |
// For cubics, this is log_2(N) and for quadratics this is log_2(N) - 1. |
| 95 |
uint8_t fCurveShift; |
| 96 |
|
| 97 |
private: |
| 98 |
void chopLineWithClip(const SkIRect& clip); |
| 99 |
|
| 100 |
#if defined(SK_DEBUG) |
| 101 |
public: |
| 102 |
virtual void dump() const; |
| 103 |
void validate() const { |
| 104 |
SkASSERT(fPrev && fNext); |
| 105 |
SkASSERT(fPrev->fNext == this); |
| 106 |
SkASSERT(fNext->fPrev == this); |
| 107 |
|
| 108 |
SkASSERT(fFirstY <= fLastY); |
| 109 |
SkASSERT(fWinding == Winding::kCW || fWinding == Winding::kCCW); |
| 110 |
} |
| 111 |
#endif |
| 112 |
}; |
| 113 |
|
| 114 |
class SkQuadraticEdge final : public SkEdge { |
| 115 |
public: |
| 116 |
// Sets up the line segments. Returns false if the line would be of height 0. |
| 117 |
bool setQuadratic(const SkPoint pts[3]); |
| 118 |
bool nextSegment() override; |
| 119 |
|
| 120 |
private: |
| 121 |
// These are the non-rounded points that the current line segment ends at. |
| 122 |
SkFixed fQx, fQy; |
| 123 |
// These represent the first derivatives of the quadratic curve evaluated |
| 124 |
// at the midpoint of the next line segment. To avoid overflows, we store them as half |
| 125 |
// their normal value. During the forward-difference step, instead of multiplying by |
| 126 |
// a deltaT of 1/N, we'll multiply by 2/N instead. |
| 127 |
SkFixed fQDxDt, fQDyDt; |
| 128 |
// These are the second derivatives of the quadratic curve pre-multiplied by 1/N. |
| 129 |
SkFixed fQD2xDt2, fQD2yDt2; |
| 130 |
|
| 131 |
// The non-rounded end points for the entire curve. On the last segment, these |
| 132 |
// will be used instead of the results from our forward-differnce technique |
| 133 |
// to make sure cumulative error doesn't result in a dramatically different line. |
| 134 |
SkFixed fQLastX, fQLastY; |
| 135 |
|
| 136 |
#if defined(SK_DEBUG) |
| 137 |
public: |
| 138 |
void dump() const override; |
| 139 |
#endif |
| 140 |
}; |
| 141 |
|
| 142 |
class SkCubicEdge final : public SkEdge { |
| 143 |
public: |
| 144 |
// Sets up the line segments. Returns false if the line would be of height 0. |
| 145 |
bool setCubic(const SkPoint pts[4]); |
| 146 |
bool nextSegment() override; |
| 147 |
|
| 148 |
private: |
| 149 |
// These are the non-rounded points that the current line segment ends at. |
| 150 |
SkFixed fCx, fCy; |
| 151 |
SkFixed fCDxDt, fCDyDt; |
| 152 |
SkFixed fCD2xDt2, fCD2yDt2; |
| 153 |
SkFixed fCD3xDt3, fCD3yDt3; |
| 154 |
|
| 155 |
// The non-rounded end points for the entire curve. On the last segment, these |
| 156 |
// will be used instead of the results from our forward-difference technique |
| 157 |
// to make sure cumulative error doesn't result in a dramatically different line. |
| 158 |
SkFixed fCLastX, fCLastY; |
| 159 |
|
| 160 |
uint8_t fCubicDShift; // applied to fCDxDt and fCDyDt only in cubic |
| 161 |
|
| 162 |
#if defined(SK_DEBUG) |
| 163 |
public: |
| 164 |
void dump() const override; |
| 165 |
#endif |
| 166 |
}; |
| 167 |
|
| 168 |
bool SkEdge::setLine(const SkPoint& p0, const SkPoint& p1) { |
| 169 |
SkFDot6 x0, y0, x1, y1; |
| 170 |
|
| 171 |
#if defined(SK_RASTERIZE_EVEN_ROUNDING) |
| 172 |
x0 = SkScalarRoundToFDot6(p0.fX, 0); |
| 173 |
y0 = SkScalarRoundToFDot6(p0.fY, 0); |
| 174 |
x1 = SkScalarRoundToFDot6(p1.fX, 0); |
| 175 |
y1 = SkScalarRoundToFDot6(p1.fY, 0); |
| 176 |
#else |
| 177 |
x0 = SkFloatToFDot6(p0.fX); |
| 178 |
y0 = SkFloatToFDot6(p0.fY); |
| 179 |
x1 = SkFloatToFDot6(p1.fX); |
| 180 |
y1 = SkFloatToFDot6(p1.fY); |
| 181 |
#endif |
| 182 |
|
| 183 |
Winding winding = Winding::kCW; |
| 184 |
|
| 185 |
if (y0 > y1) { |
| 186 |
using std::swap; |
| 187 |
swap(x0, x1); |
| 188 |
swap(y0, y1); |
| 189 |
winding = Winding::kCCW; |
| 190 |
} |
| 191 |
|
| 192 |
int top = SkFDot6Round(y0); |
| 193 |
int bot = SkFDot6Round(y1); |
| 194 |
|
| 195 |
// are we a zero-height line? |
| 196 |
if (top == bot) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
SkFixed slope = SkFDot6Div(x1 - x0, y1 - y0); |
| 201 |
const SkFDot6 dy = SkEdge_Compute_DY(top, y0); |
| 202 |
|
| 203 |
// Note that SkFixedMul(SkFixed, SkFDot6) produces results in SkFDot6 |
| 204 |
fX = SkFDot6ToFixed(x0 + SkFixedMul(slope, dy)); |
| 205 |
fDxDy = slope; |
| 206 |
fFirstY = top; |
| 207 |
fLastY = bot - 1; |
| 208 |
fEdgeType = Type::kLine; |
| 209 |
fSegmentCount = 0; |
| 210 |
fWinding = winding; |
| 211 |
fCurveShift = 0; |
| 212 |
return true; |
| 213 |
} |
| 214 |
|
| 215 |
#endif |