1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2009 The Android Open Source Project 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include "include/private/SkMacros.h" 9cb93a386Sopenharmony_ci#include "src/core/SkEdgeClipper.h" 10cb93a386Sopenharmony_ci#include "src/core/SkGeometry.h" 11cb93a386Sopenharmony_ci#include "src/core/SkLineClipper.h" 12cb93a386Sopenharmony_ci 13cb93a386Sopenharmony_ci#include <utility> 14cb93a386Sopenharmony_ci 15cb93a386Sopenharmony_cistatic bool quick_reject(const SkRect& bounds, const SkRect& clip) { 16cb93a386Sopenharmony_ci return bounds.fTop >= clip.fBottom || bounds.fBottom <= clip.fTop; 17cb93a386Sopenharmony_ci} 18cb93a386Sopenharmony_ci 19cb93a386Sopenharmony_cistatic inline void clamp_le(SkScalar& value, SkScalar max) { 20cb93a386Sopenharmony_ci if (value > max) { 21cb93a386Sopenharmony_ci value = max; 22cb93a386Sopenharmony_ci } 23cb93a386Sopenharmony_ci} 24cb93a386Sopenharmony_ci 25cb93a386Sopenharmony_cistatic inline void clamp_ge(SkScalar& value, SkScalar min) { 26cb93a386Sopenharmony_ci if (value < min) { 27cb93a386Sopenharmony_ci value = min; 28cb93a386Sopenharmony_ci } 29cb93a386Sopenharmony_ci} 30cb93a386Sopenharmony_ci 31cb93a386Sopenharmony_ci/* src[] must be monotonic in Y. This routine copies src into dst, and sorts 32cb93a386Sopenharmony_ci it to be increasing in Y. If it had to reverse the order of the points, 33cb93a386Sopenharmony_ci it returns true, otherwise it returns false 34cb93a386Sopenharmony_ci */ 35cb93a386Sopenharmony_cistatic bool sort_increasing_Y(SkPoint dst[], const SkPoint src[], int count) { 36cb93a386Sopenharmony_ci // we need the data to be monotonically increasing in Y 37cb93a386Sopenharmony_ci if (src[0].fY > src[count - 1].fY) { 38cb93a386Sopenharmony_ci for (int i = 0; i < count; i++) { 39cb93a386Sopenharmony_ci dst[i] = src[count - i - 1]; 40cb93a386Sopenharmony_ci } 41cb93a386Sopenharmony_ci return true; 42cb93a386Sopenharmony_ci } else { 43cb93a386Sopenharmony_ci memcpy(dst, src, count * sizeof(SkPoint)); 44cb93a386Sopenharmony_ci return false; 45cb93a386Sopenharmony_ci } 46cb93a386Sopenharmony_ci} 47cb93a386Sopenharmony_ci 48cb93a386Sopenharmony_cibool SkEdgeClipper::clipLine(SkPoint p0, SkPoint p1, const SkRect& clip) { 49cb93a386Sopenharmony_ci fCurrPoint = fPoints; 50cb93a386Sopenharmony_ci fCurrVerb = fVerbs; 51cb93a386Sopenharmony_ci 52cb93a386Sopenharmony_ci SkPoint lines[SkLineClipper::kMaxPoints]; 53cb93a386Sopenharmony_ci const SkPoint pts[] = { p0, p1 }; 54cb93a386Sopenharmony_ci int lineCount = SkLineClipper::ClipLine(pts, clip, lines, fCanCullToTheRight); 55cb93a386Sopenharmony_ci for (int i = 0; i < lineCount; i++) { 56cb93a386Sopenharmony_ci this->appendLine(lines[i], lines[i + 1]); 57cb93a386Sopenharmony_ci } 58cb93a386Sopenharmony_ci 59cb93a386Sopenharmony_ci *fCurrVerb = SkPath::kDone_Verb; 60cb93a386Sopenharmony_ci fCurrPoint = fPoints; 61cb93a386Sopenharmony_ci fCurrVerb = fVerbs; 62cb93a386Sopenharmony_ci return SkPath::kDone_Verb != fVerbs[0]; 63cb93a386Sopenharmony_ci} 64cb93a386Sopenharmony_ci 65cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////////// 66cb93a386Sopenharmony_ci 67cb93a386Sopenharmony_cistatic bool chopMonoQuadAt(SkScalar c0, SkScalar c1, SkScalar c2, 68cb93a386Sopenharmony_ci SkScalar target, SkScalar* t) { 69cb93a386Sopenharmony_ci /* Solve F(t) = y where F(t) := [0](1-t)^2 + 2[1]t(1-t) + [2]t^2 70cb93a386Sopenharmony_ci * We solve for t, using quadratic equation, hence we have to rearrange 71cb93a386Sopenharmony_ci * our cooefficents to look like At^2 + Bt + C 72cb93a386Sopenharmony_ci */ 73cb93a386Sopenharmony_ci SkScalar A = c0 - c1 - c1 + c2; 74cb93a386Sopenharmony_ci SkScalar B = 2*(c1 - c0); 75cb93a386Sopenharmony_ci SkScalar C = c0 - target; 76cb93a386Sopenharmony_ci 77cb93a386Sopenharmony_ci SkScalar roots[2]; // we only expect one, but make room for 2 for safety 78cb93a386Sopenharmony_ci int count = SkFindUnitQuadRoots(A, B, C, roots); 79cb93a386Sopenharmony_ci if (count) { 80cb93a386Sopenharmony_ci *t = roots[0]; 81cb93a386Sopenharmony_ci return true; 82cb93a386Sopenharmony_ci } 83cb93a386Sopenharmony_ci return false; 84cb93a386Sopenharmony_ci} 85cb93a386Sopenharmony_ci 86cb93a386Sopenharmony_cistatic bool chopMonoQuadAtY(SkPoint pts[3], SkScalar y, SkScalar* t) { 87cb93a386Sopenharmony_ci return chopMonoQuadAt(pts[0].fY, pts[1].fY, pts[2].fY, y, t); 88cb93a386Sopenharmony_ci} 89cb93a386Sopenharmony_ci 90cb93a386Sopenharmony_cistatic bool chopMonoQuadAtX(SkPoint pts[3], SkScalar x, SkScalar* t) { 91cb93a386Sopenharmony_ci return chopMonoQuadAt(pts[0].fX, pts[1].fX, pts[2].fX, x, t); 92cb93a386Sopenharmony_ci} 93cb93a386Sopenharmony_ci 94cb93a386Sopenharmony_ci// Modify pts[] in place so that it is clipped in Y to the clip rect 95cb93a386Sopenharmony_cistatic void chop_quad_in_Y(SkPoint pts[3], const SkRect& clip) { 96cb93a386Sopenharmony_ci SkScalar t; 97cb93a386Sopenharmony_ci SkPoint tmp[5]; // for SkChopQuadAt 98cb93a386Sopenharmony_ci 99cb93a386Sopenharmony_ci // are we partially above 100cb93a386Sopenharmony_ci if (pts[0].fY < clip.fTop) { 101cb93a386Sopenharmony_ci if (chopMonoQuadAtY(pts, clip.fTop, &t)) { 102cb93a386Sopenharmony_ci // take the 2nd chopped quad 103cb93a386Sopenharmony_ci SkChopQuadAt(pts, tmp, t); 104cb93a386Sopenharmony_ci // clamp to clean up imprecise numerics in the chop 105cb93a386Sopenharmony_ci tmp[2].fY = clip.fTop; 106cb93a386Sopenharmony_ci clamp_ge(tmp[3].fY, clip.fTop); 107cb93a386Sopenharmony_ci 108cb93a386Sopenharmony_ci pts[0] = tmp[2]; 109cb93a386Sopenharmony_ci pts[1] = tmp[3]; 110cb93a386Sopenharmony_ci } else { 111cb93a386Sopenharmony_ci // if chopMonoQuadAtY failed, then we may have hit inexact numerics 112cb93a386Sopenharmony_ci // so we just clamp against the top 113cb93a386Sopenharmony_ci for (int i = 0; i < 3; i++) { 114cb93a386Sopenharmony_ci if (pts[i].fY < clip.fTop) { 115cb93a386Sopenharmony_ci pts[i].fY = clip.fTop; 116cb93a386Sopenharmony_ci } 117cb93a386Sopenharmony_ci } 118cb93a386Sopenharmony_ci } 119cb93a386Sopenharmony_ci } 120cb93a386Sopenharmony_ci 121cb93a386Sopenharmony_ci // are we partially below 122cb93a386Sopenharmony_ci if (pts[2].fY > clip.fBottom) { 123cb93a386Sopenharmony_ci if (chopMonoQuadAtY(pts, clip.fBottom, &t)) { 124cb93a386Sopenharmony_ci SkChopQuadAt(pts, tmp, t); 125cb93a386Sopenharmony_ci // clamp to clean up imprecise numerics in the chop 126cb93a386Sopenharmony_ci clamp_le(tmp[1].fY, clip.fBottom); 127cb93a386Sopenharmony_ci tmp[2].fY = clip.fBottom; 128cb93a386Sopenharmony_ci 129cb93a386Sopenharmony_ci pts[1] = tmp[1]; 130cb93a386Sopenharmony_ci pts[2] = tmp[2]; 131cb93a386Sopenharmony_ci } else { 132cb93a386Sopenharmony_ci // if chopMonoQuadAtY failed, then we may have hit inexact numerics 133cb93a386Sopenharmony_ci // so we just clamp against the bottom 134cb93a386Sopenharmony_ci for (int i = 0; i < 3; i++) { 135cb93a386Sopenharmony_ci if (pts[i].fY > clip.fBottom) { 136cb93a386Sopenharmony_ci pts[i].fY = clip.fBottom; 137cb93a386Sopenharmony_ci } 138cb93a386Sopenharmony_ci } 139cb93a386Sopenharmony_ci } 140cb93a386Sopenharmony_ci } 141cb93a386Sopenharmony_ci} 142cb93a386Sopenharmony_ci 143cb93a386Sopenharmony_ci// srcPts[] must be monotonic in X and Y 144cb93a386Sopenharmony_civoid SkEdgeClipper::clipMonoQuad(const SkPoint srcPts[3], const SkRect& clip) { 145cb93a386Sopenharmony_ci SkPoint pts[3]; 146cb93a386Sopenharmony_ci bool reverse = sort_increasing_Y(pts, srcPts, 3); 147cb93a386Sopenharmony_ci 148cb93a386Sopenharmony_ci // are we completely above or below 149cb93a386Sopenharmony_ci if (pts[2].fY <= clip.fTop || pts[0].fY >= clip.fBottom) { 150cb93a386Sopenharmony_ci return; 151cb93a386Sopenharmony_ci } 152cb93a386Sopenharmony_ci 153cb93a386Sopenharmony_ci // Now chop so that pts is contained within clip in Y 154cb93a386Sopenharmony_ci chop_quad_in_Y(pts, clip); 155cb93a386Sopenharmony_ci 156cb93a386Sopenharmony_ci if (pts[0].fX > pts[2].fX) { 157cb93a386Sopenharmony_ci using std::swap; 158cb93a386Sopenharmony_ci swap(pts[0], pts[2]); 159cb93a386Sopenharmony_ci reverse = !reverse; 160cb93a386Sopenharmony_ci } 161cb93a386Sopenharmony_ci SkASSERT(pts[0].fX <= pts[1].fX); 162cb93a386Sopenharmony_ci SkASSERT(pts[1].fX <= pts[2].fX); 163cb93a386Sopenharmony_ci 164cb93a386Sopenharmony_ci // Now chop in X has needed, and record the segments 165cb93a386Sopenharmony_ci 166cb93a386Sopenharmony_ci if (pts[2].fX <= clip.fLeft) { // wholly to the left 167cb93a386Sopenharmony_ci this->appendVLine(clip.fLeft, pts[0].fY, pts[2].fY, reverse); 168cb93a386Sopenharmony_ci return; 169cb93a386Sopenharmony_ci } 170cb93a386Sopenharmony_ci if (pts[0].fX >= clip.fRight) { // wholly to the right 171cb93a386Sopenharmony_ci if (!this->canCullToTheRight()) { 172cb93a386Sopenharmony_ci this->appendVLine(clip.fRight, pts[0].fY, pts[2].fY, reverse); 173cb93a386Sopenharmony_ci } 174cb93a386Sopenharmony_ci return; 175cb93a386Sopenharmony_ci } 176cb93a386Sopenharmony_ci 177cb93a386Sopenharmony_ci SkScalar t; 178cb93a386Sopenharmony_ci SkPoint tmp[5]; // for SkChopQuadAt 179cb93a386Sopenharmony_ci 180cb93a386Sopenharmony_ci // are we partially to the left 181cb93a386Sopenharmony_ci if (pts[0].fX < clip.fLeft) { 182cb93a386Sopenharmony_ci if (chopMonoQuadAtX(pts, clip.fLeft, &t)) { 183cb93a386Sopenharmony_ci SkChopQuadAt(pts, tmp, t); 184cb93a386Sopenharmony_ci this->appendVLine(clip.fLeft, tmp[0].fY, tmp[2].fY, reverse); 185cb93a386Sopenharmony_ci // clamp to clean up imprecise numerics in the chop 186cb93a386Sopenharmony_ci tmp[2].fX = clip.fLeft; 187cb93a386Sopenharmony_ci clamp_ge(tmp[3].fX, clip.fLeft); 188cb93a386Sopenharmony_ci 189cb93a386Sopenharmony_ci pts[0] = tmp[2]; 190cb93a386Sopenharmony_ci pts[1] = tmp[3]; 191cb93a386Sopenharmony_ci } else { 192cb93a386Sopenharmony_ci // if chopMonoQuadAtY failed, then we may have hit inexact numerics 193cb93a386Sopenharmony_ci // so we just clamp against the left 194cb93a386Sopenharmony_ci this->appendVLine(clip.fLeft, pts[0].fY, pts[2].fY, reverse); 195cb93a386Sopenharmony_ci return; 196cb93a386Sopenharmony_ci } 197cb93a386Sopenharmony_ci } 198cb93a386Sopenharmony_ci 199cb93a386Sopenharmony_ci // are we partially to the right 200cb93a386Sopenharmony_ci if (pts[2].fX > clip.fRight) { 201cb93a386Sopenharmony_ci if (chopMonoQuadAtX(pts, clip.fRight, &t)) { 202cb93a386Sopenharmony_ci SkChopQuadAt(pts, tmp, t); 203cb93a386Sopenharmony_ci // clamp to clean up imprecise numerics in the chop 204cb93a386Sopenharmony_ci clamp_le(tmp[1].fX, clip.fRight); 205cb93a386Sopenharmony_ci tmp[2].fX = clip.fRight; 206cb93a386Sopenharmony_ci 207cb93a386Sopenharmony_ci this->appendQuad(tmp, reverse); 208cb93a386Sopenharmony_ci this->appendVLine(clip.fRight, tmp[2].fY, tmp[4].fY, reverse); 209cb93a386Sopenharmony_ci } else { 210cb93a386Sopenharmony_ci // if chopMonoQuadAtY failed, then we may have hit inexact numerics 211cb93a386Sopenharmony_ci // so we just clamp against the right 212cb93a386Sopenharmony_ci pts[1].fX = std::min(pts[1].fX, clip.fRight); 213cb93a386Sopenharmony_ci pts[2].fX = std::min(pts[2].fX, clip.fRight); 214cb93a386Sopenharmony_ci this->appendQuad(pts, reverse); 215cb93a386Sopenharmony_ci } 216cb93a386Sopenharmony_ci } else { // wholly inside the clip 217cb93a386Sopenharmony_ci this->appendQuad(pts, reverse); 218cb93a386Sopenharmony_ci } 219cb93a386Sopenharmony_ci} 220cb93a386Sopenharmony_ci 221cb93a386Sopenharmony_cibool SkEdgeClipper::clipQuad(const SkPoint srcPts[3], const SkRect& clip) { 222cb93a386Sopenharmony_ci fCurrPoint = fPoints; 223cb93a386Sopenharmony_ci fCurrVerb = fVerbs; 224cb93a386Sopenharmony_ci 225cb93a386Sopenharmony_ci SkRect bounds; 226cb93a386Sopenharmony_ci bounds.setBounds(srcPts, 3); 227cb93a386Sopenharmony_ci 228cb93a386Sopenharmony_ci if (!quick_reject(bounds, clip)) { 229cb93a386Sopenharmony_ci SkPoint monoY[5]; 230cb93a386Sopenharmony_ci int countY = SkChopQuadAtYExtrema(srcPts, monoY); 231cb93a386Sopenharmony_ci for (int y = 0; y <= countY; y++) { 232cb93a386Sopenharmony_ci SkPoint monoX[5]; 233cb93a386Sopenharmony_ci int countX = SkChopQuadAtXExtrema(&monoY[y * 2], monoX); 234cb93a386Sopenharmony_ci for (int x = 0; x <= countX; x++) { 235cb93a386Sopenharmony_ci this->clipMonoQuad(&monoX[x * 2], clip); 236cb93a386Sopenharmony_ci SkASSERT(fCurrVerb - fVerbs < kMaxVerbs); 237cb93a386Sopenharmony_ci SkASSERT(fCurrPoint - fPoints <= kMaxPoints); 238cb93a386Sopenharmony_ci } 239cb93a386Sopenharmony_ci } 240cb93a386Sopenharmony_ci } 241cb93a386Sopenharmony_ci 242cb93a386Sopenharmony_ci *fCurrVerb = SkPath::kDone_Verb; 243cb93a386Sopenharmony_ci fCurrPoint = fPoints; 244cb93a386Sopenharmony_ci fCurrVerb = fVerbs; 245cb93a386Sopenharmony_ci return SkPath::kDone_Verb != fVerbs[0]; 246cb93a386Sopenharmony_ci} 247cb93a386Sopenharmony_ci 248cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////////// 249cb93a386Sopenharmony_ci 250cb93a386Sopenharmony_cistatic SkScalar mono_cubic_closestT(const SkScalar src[], SkScalar x) { 251cb93a386Sopenharmony_ci SkScalar t = 0.5f; 252cb93a386Sopenharmony_ci SkScalar lastT; 253cb93a386Sopenharmony_ci SkScalar bestT SK_INIT_TO_AVOID_WARNING; 254cb93a386Sopenharmony_ci SkScalar step = 0.25f; 255cb93a386Sopenharmony_ci SkScalar D = src[0]; 256cb93a386Sopenharmony_ci SkScalar A = src[6] + 3*(src[2] - src[4]) - D; 257cb93a386Sopenharmony_ci SkScalar B = 3*(src[4] - src[2] - src[2] + D); 258cb93a386Sopenharmony_ci SkScalar C = 3*(src[2] - D); 259cb93a386Sopenharmony_ci x -= D; 260cb93a386Sopenharmony_ci SkScalar closest = SK_ScalarMax; 261cb93a386Sopenharmony_ci do { 262cb93a386Sopenharmony_ci SkScalar loc = ((A * t + B) * t + C) * t; 263cb93a386Sopenharmony_ci SkScalar dist = SkScalarAbs(loc - x); 264cb93a386Sopenharmony_ci if (closest > dist) { 265cb93a386Sopenharmony_ci closest = dist; 266cb93a386Sopenharmony_ci bestT = t; 267cb93a386Sopenharmony_ci } 268cb93a386Sopenharmony_ci lastT = t; 269cb93a386Sopenharmony_ci t += loc < x ? step : -step; 270cb93a386Sopenharmony_ci step *= 0.5f; 271cb93a386Sopenharmony_ci } while (closest > 0.25f && lastT != t); 272cb93a386Sopenharmony_ci return bestT; 273cb93a386Sopenharmony_ci} 274cb93a386Sopenharmony_ci 275cb93a386Sopenharmony_cistatic void chop_mono_cubic_at_y(SkPoint src[4], SkScalar y, SkPoint dst[7]) { 276cb93a386Sopenharmony_ci if (SkChopMonoCubicAtY(src, y, dst)) { 277cb93a386Sopenharmony_ci return; 278cb93a386Sopenharmony_ci } 279cb93a386Sopenharmony_ci SkChopCubicAt(src, dst, mono_cubic_closestT(&src->fY, y)); 280cb93a386Sopenharmony_ci} 281cb93a386Sopenharmony_ci 282cb93a386Sopenharmony_ci// Modify pts[] in place so that it is clipped in Y to the clip rect 283cb93a386Sopenharmony_cistatic void chop_cubic_in_Y(SkPoint pts[4], const SkRect& clip) { 284cb93a386Sopenharmony_ci 285cb93a386Sopenharmony_ci // are we partially above 286cb93a386Sopenharmony_ci if (pts[0].fY < clip.fTop) { 287cb93a386Sopenharmony_ci SkPoint tmp[7]; 288cb93a386Sopenharmony_ci chop_mono_cubic_at_y(pts, clip.fTop, tmp); 289cb93a386Sopenharmony_ci 290cb93a386Sopenharmony_ci /* 291cb93a386Sopenharmony_ci * For a large range in the points, we can do a poor job of chopping, such that the t 292cb93a386Sopenharmony_ci * we computed resulted in the lower cubic still being partly above the clip. 293cb93a386Sopenharmony_ci * 294cb93a386Sopenharmony_ci * If just the first or first 2 Y values are above the fTop, we can just smash them 295cb93a386Sopenharmony_ci * down. If the first 3 Ys are above fTop, we can't smash all 3, as that can really 296cb93a386Sopenharmony_ci * distort the cubic. In this case, we take the first output (tmp[3..6] and treat it as 297cb93a386Sopenharmony_ci * a guess, and re-chop against fTop. Then we fall through to checking if we need to 298cb93a386Sopenharmony_ci * smash the first 1 or 2 Y values. 299cb93a386Sopenharmony_ci */ 300cb93a386Sopenharmony_ci if (tmp[3].fY < clip.fTop && tmp[4].fY < clip.fTop && tmp[5].fY < clip.fTop) { 301cb93a386Sopenharmony_ci SkPoint tmp2[4]; 302cb93a386Sopenharmony_ci memcpy(tmp2, &tmp[3].fX, 4 * sizeof(SkPoint)); 303cb93a386Sopenharmony_ci chop_mono_cubic_at_y(tmp2, clip.fTop, tmp); 304cb93a386Sopenharmony_ci } 305cb93a386Sopenharmony_ci 306cb93a386Sopenharmony_ci // tmp[3, 4].fY should all be to the below clip.fTop. 307cb93a386Sopenharmony_ci // Since we can't trust the numerics of the chopper, we force those conditions now 308cb93a386Sopenharmony_ci tmp[3].fY = clip.fTop; 309cb93a386Sopenharmony_ci clamp_ge(tmp[4].fY, clip.fTop); 310cb93a386Sopenharmony_ci 311cb93a386Sopenharmony_ci pts[0] = tmp[3]; 312cb93a386Sopenharmony_ci pts[1] = tmp[4]; 313cb93a386Sopenharmony_ci pts[2] = tmp[5]; 314cb93a386Sopenharmony_ci } 315cb93a386Sopenharmony_ci 316cb93a386Sopenharmony_ci // are we partially below 317cb93a386Sopenharmony_ci if (pts[3].fY > clip.fBottom) { 318cb93a386Sopenharmony_ci SkPoint tmp[7]; 319cb93a386Sopenharmony_ci chop_mono_cubic_at_y(pts, clip.fBottom, tmp); 320cb93a386Sopenharmony_ci tmp[3].fY = clip.fBottom; 321cb93a386Sopenharmony_ci clamp_le(tmp[2].fY, clip.fBottom); 322cb93a386Sopenharmony_ci 323cb93a386Sopenharmony_ci pts[1] = tmp[1]; 324cb93a386Sopenharmony_ci pts[2] = tmp[2]; 325cb93a386Sopenharmony_ci pts[3] = tmp[3]; 326cb93a386Sopenharmony_ci } 327cb93a386Sopenharmony_ci} 328cb93a386Sopenharmony_ci 329cb93a386Sopenharmony_cistatic void chop_mono_cubic_at_x(SkPoint src[4], SkScalar x, SkPoint dst[7]) { 330cb93a386Sopenharmony_ci if (SkChopMonoCubicAtX(src, x, dst)) { 331cb93a386Sopenharmony_ci return; 332cb93a386Sopenharmony_ci } 333cb93a386Sopenharmony_ci SkChopCubicAt(src, dst, mono_cubic_closestT(&src->fX, x)); 334cb93a386Sopenharmony_ci} 335cb93a386Sopenharmony_ci 336cb93a386Sopenharmony_ci// srcPts[] must be monotonic in X and Y 337cb93a386Sopenharmony_civoid SkEdgeClipper::clipMonoCubic(const SkPoint src[4], const SkRect& clip) { 338cb93a386Sopenharmony_ci SkPoint pts[4]; 339cb93a386Sopenharmony_ci bool reverse = sort_increasing_Y(pts, src, 4); 340cb93a386Sopenharmony_ci 341cb93a386Sopenharmony_ci // are we completely above or below 342cb93a386Sopenharmony_ci if (pts[3].fY <= clip.fTop || pts[0].fY >= clip.fBottom) { 343cb93a386Sopenharmony_ci return; 344cb93a386Sopenharmony_ci } 345cb93a386Sopenharmony_ci 346cb93a386Sopenharmony_ci // Now chop so that pts is contained within clip in Y 347cb93a386Sopenharmony_ci chop_cubic_in_Y(pts, clip); 348cb93a386Sopenharmony_ci 349cb93a386Sopenharmony_ci if (pts[0].fX > pts[3].fX) { 350cb93a386Sopenharmony_ci using std::swap; 351cb93a386Sopenharmony_ci swap(pts[0], pts[3]); 352cb93a386Sopenharmony_ci swap(pts[1], pts[2]); 353cb93a386Sopenharmony_ci reverse = !reverse; 354cb93a386Sopenharmony_ci } 355cb93a386Sopenharmony_ci 356cb93a386Sopenharmony_ci // Now chop in X has needed, and record the segments 357cb93a386Sopenharmony_ci 358cb93a386Sopenharmony_ci if (pts[3].fX <= clip.fLeft) { // wholly to the left 359cb93a386Sopenharmony_ci this->appendVLine(clip.fLeft, pts[0].fY, pts[3].fY, reverse); 360cb93a386Sopenharmony_ci return; 361cb93a386Sopenharmony_ci } 362cb93a386Sopenharmony_ci if (pts[0].fX >= clip.fRight) { // wholly to the right 363cb93a386Sopenharmony_ci if (!this->canCullToTheRight()) { 364cb93a386Sopenharmony_ci this->appendVLine(clip.fRight, pts[0].fY, pts[3].fY, reverse); 365cb93a386Sopenharmony_ci } 366cb93a386Sopenharmony_ci return; 367cb93a386Sopenharmony_ci } 368cb93a386Sopenharmony_ci 369cb93a386Sopenharmony_ci // are we partially to the left 370cb93a386Sopenharmony_ci if (pts[0].fX < clip.fLeft) { 371cb93a386Sopenharmony_ci SkPoint tmp[7]; 372cb93a386Sopenharmony_ci chop_mono_cubic_at_x(pts, clip.fLeft, tmp); 373cb93a386Sopenharmony_ci this->appendVLine(clip.fLeft, tmp[0].fY, tmp[3].fY, reverse); 374cb93a386Sopenharmony_ci 375cb93a386Sopenharmony_ci // tmp[3, 4].fX should all be to the right of clip.fLeft. 376cb93a386Sopenharmony_ci // Since we can't trust the numerics of 377cb93a386Sopenharmony_ci // the chopper, we force those conditions now 378cb93a386Sopenharmony_ci tmp[3].fX = clip.fLeft; 379cb93a386Sopenharmony_ci clamp_ge(tmp[4].fX, clip.fLeft); 380cb93a386Sopenharmony_ci 381cb93a386Sopenharmony_ci pts[0] = tmp[3]; 382cb93a386Sopenharmony_ci pts[1] = tmp[4]; 383cb93a386Sopenharmony_ci pts[2] = tmp[5]; 384cb93a386Sopenharmony_ci } 385cb93a386Sopenharmony_ci 386cb93a386Sopenharmony_ci // are we partially to the right 387cb93a386Sopenharmony_ci if (pts[3].fX > clip.fRight) { 388cb93a386Sopenharmony_ci SkPoint tmp[7]; 389cb93a386Sopenharmony_ci chop_mono_cubic_at_x(pts, clip.fRight, tmp); 390cb93a386Sopenharmony_ci tmp[3].fX = clip.fRight; 391cb93a386Sopenharmony_ci clamp_le(tmp[2].fX, clip.fRight); 392cb93a386Sopenharmony_ci 393cb93a386Sopenharmony_ci this->appendCubic(tmp, reverse); 394cb93a386Sopenharmony_ci this->appendVLine(clip.fRight, tmp[3].fY, tmp[6].fY, reverse); 395cb93a386Sopenharmony_ci } else { // wholly inside the clip 396cb93a386Sopenharmony_ci this->appendCubic(pts, reverse); 397cb93a386Sopenharmony_ci } 398cb93a386Sopenharmony_ci} 399cb93a386Sopenharmony_ci 400cb93a386Sopenharmony_cistatic SkRect compute_cubic_bounds(const SkPoint pts[4]) { 401cb93a386Sopenharmony_ci SkRect r; 402cb93a386Sopenharmony_ci r.setBounds(pts, 4); 403cb93a386Sopenharmony_ci return r; 404cb93a386Sopenharmony_ci} 405cb93a386Sopenharmony_ci 406cb93a386Sopenharmony_cistatic bool too_big_for_reliable_float_math(const SkRect& r) { 407cb93a386Sopenharmony_ci // limit set as the largest float value for which we can still reliably compute things like 408cb93a386Sopenharmony_ci // - chopping at XY extrema 409cb93a386Sopenharmony_ci // - chopping at Y or X values for clipping 410cb93a386Sopenharmony_ci // 411cb93a386Sopenharmony_ci // Current value chosen just by experiment. Larger (and still succeeds) is always better. 412cb93a386Sopenharmony_ci // 413cb93a386Sopenharmony_ci const SkScalar limit = 1 << 22; 414cb93a386Sopenharmony_ci return r.fLeft < -limit || r.fTop < -limit || r.fRight > limit || r.fBottom > limit; 415cb93a386Sopenharmony_ci} 416cb93a386Sopenharmony_ci 417cb93a386Sopenharmony_cibool SkEdgeClipper::clipCubic(const SkPoint srcPts[4], const SkRect& clip) { 418cb93a386Sopenharmony_ci fCurrPoint = fPoints; 419cb93a386Sopenharmony_ci fCurrVerb = fVerbs; 420cb93a386Sopenharmony_ci 421cb93a386Sopenharmony_ci const SkRect bounds = compute_cubic_bounds(srcPts); 422cb93a386Sopenharmony_ci // check if we're clipped out vertically 423cb93a386Sopenharmony_ci if (bounds.fBottom > clip.fTop && bounds.fTop < clip.fBottom) { 424cb93a386Sopenharmony_ci if (too_big_for_reliable_float_math(bounds)) { 425cb93a386Sopenharmony_ci // can't safely clip the cubic, so we give up and draw a line (which we can safely clip) 426cb93a386Sopenharmony_ci // 427cb93a386Sopenharmony_ci // If we rewrote chopcubicat*extrema and chopmonocubic using doubles, we could very 428cb93a386Sopenharmony_ci // likely always handle the cubic safely, but (it seems) at a big loss in speed, so 429cb93a386Sopenharmony_ci // we'd only want to take that alternate impl if needed. Perhaps a TODO to try it. 430cb93a386Sopenharmony_ci // 431cb93a386Sopenharmony_ci return this->clipLine(srcPts[0], srcPts[3], clip); 432cb93a386Sopenharmony_ci } else { 433cb93a386Sopenharmony_ci SkPoint monoY[10]; 434cb93a386Sopenharmony_ci int countY = SkChopCubicAtYExtrema(srcPts, monoY); 435cb93a386Sopenharmony_ci for (int y = 0; y <= countY; y++) { 436cb93a386Sopenharmony_ci SkPoint monoX[10]; 437cb93a386Sopenharmony_ci int countX = SkChopCubicAtXExtrema(&monoY[y * 3], monoX); 438cb93a386Sopenharmony_ci for (int x = 0; x <= countX; x++) { 439cb93a386Sopenharmony_ci this->clipMonoCubic(&monoX[x * 3], clip); 440cb93a386Sopenharmony_ci SkASSERT(fCurrVerb - fVerbs < kMaxVerbs); 441cb93a386Sopenharmony_ci SkASSERT(fCurrPoint - fPoints <= kMaxPoints); 442cb93a386Sopenharmony_ci } 443cb93a386Sopenharmony_ci } 444cb93a386Sopenharmony_ci } 445cb93a386Sopenharmony_ci } 446cb93a386Sopenharmony_ci 447cb93a386Sopenharmony_ci *fCurrVerb = SkPath::kDone_Verb; 448cb93a386Sopenharmony_ci fCurrPoint = fPoints; 449cb93a386Sopenharmony_ci fCurrVerb = fVerbs; 450cb93a386Sopenharmony_ci return SkPath::kDone_Verb != fVerbs[0]; 451cb93a386Sopenharmony_ci} 452cb93a386Sopenharmony_ci 453cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////////// 454cb93a386Sopenharmony_ci 455cb93a386Sopenharmony_civoid SkEdgeClipper::appendLine(SkPoint p0, SkPoint p1) { 456cb93a386Sopenharmony_ci *fCurrVerb++ = SkPath::kLine_Verb; 457cb93a386Sopenharmony_ci fCurrPoint[0] = p0; 458cb93a386Sopenharmony_ci fCurrPoint[1] = p1; 459cb93a386Sopenharmony_ci fCurrPoint += 2; 460cb93a386Sopenharmony_ci} 461cb93a386Sopenharmony_ci 462cb93a386Sopenharmony_civoid SkEdgeClipper::appendVLine(SkScalar x, SkScalar y0, SkScalar y1, bool reverse) { 463cb93a386Sopenharmony_ci *fCurrVerb++ = SkPath::kLine_Verb; 464cb93a386Sopenharmony_ci 465cb93a386Sopenharmony_ci if (reverse) { 466cb93a386Sopenharmony_ci using std::swap; 467cb93a386Sopenharmony_ci swap(y0, y1); 468cb93a386Sopenharmony_ci } 469cb93a386Sopenharmony_ci fCurrPoint[0].set(x, y0); 470cb93a386Sopenharmony_ci fCurrPoint[1].set(x, y1); 471cb93a386Sopenharmony_ci fCurrPoint += 2; 472cb93a386Sopenharmony_ci} 473cb93a386Sopenharmony_ci 474cb93a386Sopenharmony_civoid SkEdgeClipper::appendQuad(const SkPoint pts[3], bool reverse) { 475cb93a386Sopenharmony_ci *fCurrVerb++ = SkPath::kQuad_Verb; 476cb93a386Sopenharmony_ci 477cb93a386Sopenharmony_ci if (reverse) { 478cb93a386Sopenharmony_ci fCurrPoint[0] = pts[2]; 479cb93a386Sopenharmony_ci fCurrPoint[2] = pts[0]; 480cb93a386Sopenharmony_ci } else { 481cb93a386Sopenharmony_ci fCurrPoint[0] = pts[0]; 482cb93a386Sopenharmony_ci fCurrPoint[2] = pts[2]; 483cb93a386Sopenharmony_ci } 484cb93a386Sopenharmony_ci fCurrPoint[1] = pts[1]; 485cb93a386Sopenharmony_ci fCurrPoint += 3; 486cb93a386Sopenharmony_ci} 487cb93a386Sopenharmony_ci 488cb93a386Sopenharmony_civoid SkEdgeClipper::appendCubic(const SkPoint pts[4], bool reverse) { 489cb93a386Sopenharmony_ci *fCurrVerb++ = SkPath::kCubic_Verb; 490cb93a386Sopenharmony_ci 491cb93a386Sopenharmony_ci if (reverse) { 492cb93a386Sopenharmony_ci for (int i = 0; i < 4; i++) { 493cb93a386Sopenharmony_ci fCurrPoint[i] = pts[3 - i]; 494cb93a386Sopenharmony_ci } 495cb93a386Sopenharmony_ci } else { 496cb93a386Sopenharmony_ci memcpy(fCurrPoint, pts, 4 * sizeof(SkPoint)); 497cb93a386Sopenharmony_ci } 498cb93a386Sopenharmony_ci fCurrPoint += 4; 499cb93a386Sopenharmony_ci} 500cb93a386Sopenharmony_ci 501cb93a386Sopenharmony_ciSkPath::Verb SkEdgeClipper::next(SkPoint pts[]) { 502cb93a386Sopenharmony_ci SkPath::Verb verb = *fCurrVerb; 503cb93a386Sopenharmony_ci 504cb93a386Sopenharmony_ci switch (verb) { 505cb93a386Sopenharmony_ci case SkPath::kLine_Verb: 506cb93a386Sopenharmony_ci memcpy(pts, fCurrPoint, 2 * sizeof(SkPoint)); 507cb93a386Sopenharmony_ci fCurrPoint += 2; 508cb93a386Sopenharmony_ci fCurrVerb += 1; 509cb93a386Sopenharmony_ci break; 510cb93a386Sopenharmony_ci case SkPath::kQuad_Verb: 511cb93a386Sopenharmony_ci memcpy(pts, fCurrPoint, 3 * sizeof(SkPoint)); 512cb93a386Sopenharmony_ci fCurrPoint += 3; 513cb93a386Sopenharmony_ci fCurrVerb += 1; 514cb93a386Sopenharmony_ci break; 515cb93a386Sopenharmony_ci case SkPath::kCubic_Verb: 516cb93a386Sopenharmony_ci memcpy(pts, fCurrPoint, 4 * sizeof(SkPoint)); 517cb93a386Sopenharmony_ci fCurrPoint += 4; 518cb93a386Sopenharmony_ci fCurrVerb += 1; 519cb93a386Sopenharmony_ci break; 520cb93a386Sopenharmony_ci case SkPath::kDone_Verb: 521cb93a386Sopenharmony_ci break; 522cb93a386Sopenharmony_ci default: 523cb93a386Sopenharmony_ci SkDEBUGFAIL("unexpected verb in quadclippper2 iter"); 524cb93a386Sopenharmony_ci break; 525cb93a386Sopenharmony_ci } 526cb93a386Sopenharmony_ci return verb; 527cb93a386Sopenharmony_ci} 528cb93a386Sopenharmony_ci 529cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////////// 530cb93a386Sopenharmony_ci 531cb93a386Sopenharmony_ci#ifdef SK_DEBUG 532cb93a386Sopenharmony_cistatic void assert_monotonic(const SkScalar coord[], int count) { 533cb93a386Sopenharmony_ci if (coord[0] > coord[(count - 1) * 2]) { 534cb93a386Sopenharmony_ci for (int i = 1; i < count; i++) { 535cb93a386Sopenharmony_ci SkASSERT(coord[2 * (i - 1)] >= coord[i * 2]); 536cb93a386Sopenharmony_ci } 537cb93a386Sopenharmony_ci } else if (coord[0] < coord[(count - 1) * 2]) { 538cb93a386Sopenharmony_ci for (int i = 1; i < count; i++) { 539cb93a386Sopenharmony_ci SkASSERT(coord[2 * (i - 1)] <= coord[i * 2]); 540cb93a386Sopenharmony_ci } 541cb93a386Sopenharmony_ci } else { 542cb93a386Sopenharmony_ci for (int i = 1; i < count; i++) { 543cb93a386Sopenharmony_ci SkASSERT(coord[2 * (i - 1)] == coord[i * 2]); 544cb93a386Sopenharmony_ci } 545cb93a386Sopenharmony_ci } 546cb93a386Sopenharmony_ci} 547cb93a386Sopenharmony_ci 548cb93a386Sopenharmony_civoid sk_assert_monotonic_y(const SkPoint pts[], int count) { 549cb93a386Sopenharmony_ci if (count > 1) { 550cb93a386Sopenharmony_ci assert_monotonic(&pts[0].fY, count); 551cb93a386Sopenharmony_ci } 552cb93a386Sopenharmony_ci} 553cb93a386Sopenharmony_ci 554cb93a386Sopenharmony_civoid sk_assert_monotonic_x(const SkPoint pts[], int count) { 555cb93a386Sopenharmony_ci if (count > 1) { 556cb93a386Sopenharmony_ci assert_monotonic(&pts[0].fX, count); 557cb93a386Sopenharmony_ci } 558cb93a386Sopenharmony_ci} 559cb93a386Sopenharmony_ci#endif 560cb93a386Sopenharmony_ci 561cb93a386Sopenharmony_ci#include "src/core/SkPathPriv.h" 562cb93a386Sopenharmony_ci 563cb93a386Sopenharmony_civoid SkEdgeClipper::ClipPath(const SkPath& path, const SkRect& clip, bool canCullToTheRight, 564cb93a386Sopenharmony_ci void (*consume)(SkEdgeClipper*, bool newCtr, void* ctx), void* ctx) { 565cb93a386Sopenharmony_ci SkASSERT(path.isFinite()); 566cb93a386Sopenharmony_ci 567cb93a386Sopenharmony_ci SkAutoConicToQuads quadder; 568cb93a386Sopenharmony_ci const SkScalar conicTol = SK_Scalar1 / 4; 569cb93a386Sopenharmony_ci 570cb93a386Sopenharmony_ci SkPathEdgeIter iter(path); 571cb93a386Sopenharmony_ci SkEdgeClipper clipper(canCullToTheRight); 572cb93a386Sopenharmony_ci 573cb93a386Sopenharmony_ci while (auto e = iter.next()) { 574cb93a386Sopenharmony_ci switch (e.fEdge) { 575cb93a386Sopenharmony_ci case SkPathEdgeIter::Edge::kLine: 576cb93a386Sopenharmony_ci if (clipper.clipLine(e.fPts[0], e.fPts[1], clip)) { 577cb93a386Sopenharmony_ci consume(&clipper, e.fIsNewContour, ctx); 578cb93a386Sopenharmony_ci } 579cb93a386Sopenharmony_ci break; 580cb93a386Sopenharmony_ci case SkPathEdgeIter::Edge::kQuad: 581cb93a386Sopenharmony_ci if (clipper.clipQuad(e.fPts, clip)) { 582cb93a386Sopenharmony_ci consume(&clipper, e.fIsNewContour, ctx); 583cb93a386Sopenharmony_ci } 584cb93a386Sopenharmony_ci break; 585cb93a386Sopenharmony_ci case SkPathEdgeIter::Edge::kConic: { 586cb93a386Sopenharmony_ci const SkPoint* quadPts = quadder.computeQuads(e.fPts, iter.conicWeight(), conicTol); 587cb93a386Sopenharmony_ci for (int i = 0; i < quadder.countQuads(); ++i) { 588cb93a386Sopenharmony_ci if (clipper.clipQuad(quadPts, clip)) { 589cb93a386Sopenharmony_ci consume(&clipper, e.fIsNewContour, ctx); 590cb93a386Sopenharmony_ci } 591cb93a386Sopenharmony_ci quadPts += 2; 592cb93a386Sopenharmony_ci } 593cb93a386Sopenharmony_ci } break; 594cb93a386Sopenharmony_ci case SkPathEdgeIter::Edge::kCubic: 595cb93a386Sopenharmony_ci if (clipper.clipCubic(e.fPts, clip)) { 596cb93a386Sopenharmony_ci consume(&clipper, e.fIsNewContour, ctx); 597cb93a386Sopenharmony_ci } 598cb93a386Sopenharmony_ci break; 599cb93a386Sopenharmony_ci } 600cb93a386Sopenharmony_ci } 601cb93a386Sopenharmony_ci} 602