blob: f8d3bb23be4e411e3585c7deef5103b2a1bc0246 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008
9#ifndef SkScanPriv_DEFINED
10#define SkScanPriv_DEFINED
11
Yuqian Lica50b872017-06-12 10:40:50 -040012#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkScan.h"
14#include "SkBlitter.h"
15
16class SkScanClipper {
17public:
reed@google.com5ee64912012-06-11 17:30:33 +000018 SkScanClipper(SkBlitter* blitter, const SkRegion* clip, const SkIRect& bounds,
19 bool skipRejectTest = false);
reed@android.com8a1c16f2008-12-17 15:59:43 +000020
21 SkBlitter* getBlitter() const { return fBlitter; }
22 const SkIRect* getClipRect() const { return fClipRect; }
23
24private:
25 SkRectClipBlitter fRectBlitter;
26 SkRgnClipBlitter fRgnBlitter;
Mike Reed28930b42016-11-09 15:23:26 -050027#ifdef SK_DEBUG
Yuqian Li99bba9e2016-11-21 09:44:59 -050028 SkRectClipCheckBlitter fRectClipCheckBlitter;
Mike Reed28930b42016-11-09 15:23:26 -050029#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 SkBlitter* fBlitter;
31 const SkIRect* fClipRect;
32};
33
Yuqian Lie4b8b522016-11-16 10:12:58 -050034void sk_fill_path(const SkPath& path, const SkIRect& clipRect,
reed@android.comdca6a562010-02-22 16:05:48 +000035 SkBlitter* blitter, int start_y, int stop_y, int shiftEdgesUp,
Yuqian Lie4b8b522016-11-16 10:12:58 -050036 bool pathContainedInClip);
37
reed@google.com55b6b582011-03-02 15:58:18 +000038// blit the rects above and below avoid, clipped to clip
39void sk_blit_above(SkBlitter*, const SkIRect& avoid, const SkRegion& clip);
40void sk_blit_below(SkBlitter*, const SkIRect& avoid, const SkRegion& clip);
reed@android.com8a1c16f2008-12-17 15:59:43 +000041
Yuqian Lia33b43d2017-03-17 11:26:29 -040042template<class EdgeType>
43static inline void remove_edge(EdgeType* edge) {
44 edge->fPrev->fNext = edge->fNext;
45 edge->fNext->fPrev = edge->fPrev;
46}
47
48template<class EdgeType>
49static inline void insert_edge_after(EdgeType* edge, EdgeType* afterMe) {
50 edge->fPrev = afterMe;
51 edge->fNext = afterMe->fNext;
52 afterMe->fNext->fPrev = edge;
53 afterMe->fNext = edge;
54}
55
56template<class EdgeType>
57static void backward_insert_edge_based_on_x(EdgeType* edge) {
58 SkFixed x = edge->fX;
59 EdgeType* prev = edge->fPrev;
60 while (prev->fPrev && prev->fX > x) {
61 prev = prev->fPrev;
62 }
63 if (prev->fNext != edge) {
64 remove_edge(edge);
65 insert_edge_after(edge, prev);
66 }
67}
68
69// Start from the right side, searching backwards for the point to begin the new edge list
70// insertion, marching forwards from here. The implementation could have started from the left
71// of the prior insertion, and search to the right, or with some additional caching, binary
72// search the starting point. More work could be done to determine optimal new edge insertion.
73template<class EdgeType>
74static EdgeType* backward_insert_start(EdgeType* prev, SkFixed x) {
75 while (prev->fPrev && prev->fX > x) {
76 prev = prev->fPrev;
77 }
78 return prev;
79}
80
Yuqian Lica50b872017-06-12 10:40:50 -040081static bool fitsInsideLimit(const SkRect& r, SkScalar max) {
82 const SkScalar min = -max;
83 return r.fLeft > min && r.fTop > min &&
84 r.fRight < max && r.fBottom < max;
85}
86
87static int overflows_short_shift(int value, int shift) {
88 const int s = 16 + shift;
89 return (SkLeftShift(value, s) >> s) - value;
90}
91
92/**
93 Would any of the coordinates of this rectangle not fit in a short,
94 when left-shifted by shift?
95*/
96static int rect_overflows_short_shift(SkIRect rect, int shift) {
97 SkASSERT(!overflows_short_shift(8191, shift));
98 SkASSERT(overflows_short_shift(8192, shift));
99 SkASSERT(!overflows_short_shift(32767, 0));
100 SkASSERT(overflows_short_shift(32768, 0));
101
102 // Since we expect these to succeed, we bit-or together
103 // for a tiny extra bit of speed.
104 return overflows_short_shift(rect.fLeft, shift) |
105 overflows_short_shift(rect.fRight, shift) |
106 overflows_short_shift(rect.fTop, shift) |
107 overflows_short_shift(rect.fBottom, shift);
108}
109
110static bool safeRoundOut(const SkRect& src, SkIRect* dst, int32_t maxInt) {
111 const SkScalar maxScalar = SkIntToScalar(maxInt);
112
113 if (fitsInsideLimit(src, maxScalar)) {
114 src.roundOut(dst);
115 return true;
116 }
117 return false;
118}
119
120using FillPathFunc = std::function<void(const SkPath& path, SkBlitter* blitter, bool isInverse,
121 const SkIRect& ir, const SkRegion* clipRgn, const SkIRect* clipRect, bool forceRLE)>;
122
123static inline void do_fill_path(const SkPath& path, const SkRegion& origClip, SkBlitter* blitter,
124 bool forceRLE, const int SHIFT, FillPathFunc fillPathFunc) {
125 if (origClip.isEmpty()) {
126 return;
127 }
128
129 const bool isInverse = path.isInverseFillType();
130 SkIRect ir;
131
132 if (!safeRoundOut(path.getBounds(), &ir, SK_MaxS32 >> SHIFT)) {
133 // Bounds can't fit in SkIRect; we'll return without drawing
134 return;
135 }
136 if (ir.isEmpty()) {
137 if (isInverse) {
138 blitter->blitRegion(origClip);
139 }
140 return;
141 }
142
143 // If the intersection of the path bounds and the clip bounds
144 // will overflow 32767 when << by SHIFT, we can't supersample,
145 // so draw without antialiasing.
146 SkIRect clippedIR;
147 if (isInverse) {
148 // If the path is an inverse fill, it's going to fill the entire
149 // clip, and we care whether the entire clip exceeds our limits.
150 clippedIR = origClip.getBounds();
151 } else {
152 if (!clippedIR.intersect(ir, origClip.getBounds())) {
153 return;
154 }
155 }
156 if (rect_overflows_short_shift(clippedIR, SHIFT)) {
157 SkScan::FillPath(path, origClip, blitter);
158 return;
159 }
160
161 // Our antialiasing can't handle a clip larger than 32767, so we restrict
162 // the clip to that limit here. (the runs[] uses int16_t for its index).
163 //
164 // A more general solution (one that could also eliminate the need to
165 // disable aa based on ir bounds (see overflows_short_shift) would be
166 // to tile the clip/target...
167 SkRegion tmpClipStorage;
168 const SkRegion* clipRgn = &origClip;
169 {
170 static const int32_t kMaxClipCoord = 32767;
171 const SkIRect& bounds = origClip.getBounds();
172 if (bounds.fRight > kMaxClipCoord || bounds.fBottom > kMaxClipCoord) {
173 SkIRect limit = { 0, 0, kMaxClipCoord, kMaxClipCoord };
174 tmpClipStorage.op(origClip, limit, SkRegion::kIntersect_Op);
175 clipRgn = &tmpClipStorage;
176 }
177 }
178 // for here down, use clipRgn, not origClip
179
180 SkScanClipper clipper(blitter, clipRgn, ir);
181 const SkIRect* clipRect = clipper.getClipRect();
182
183 if (clipper.getBlitter() == nullptr) { // clipped out
184 if (isInverse) {
185 blitter->blitRegion(*clipRgn);
186 }
187 return;
188 }
189
190 SkASSERT(clipper.getClipRect() == nullptr ||
191 *clipper.getClipRect() == clipRgn->getBounds());
192
193 // now use the (possibly wrapped) blitter
194 blitter = clipper.getBlitter();
195
196 if (isInverse) {
197 sk_blit_above(blitter, ir, *clipRgn);
198 }
199
200 SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop);
201
202 fillPathFunc(path, blitter, isInverse, ir, clipRgn, clipRect, forceRLE);
203
204 if (isInverse) {
205 sk_blit_below(blitter, ir, *clipRgn);
206 }
207}
208
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209#endif