blob: cb2da30803ed9d94e9a11dd401e2b37998afb4b1 [file] [log] [blame]
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +00006 */
7
8#ifndef GrPathUtils_DEFINED
9#define GrPathUtils_DEFINED
10
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000011#include "SkRect.h"
reed026beb52015-06-10 14:23:15 -070012#include "SkPathPriv.h"
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000013#include "SkTArray.h"
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000014
bsalomon@google.comb9086a02012-11-01 18:02:54 +000015class SkMatrix;
16
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000017/**
18 * Utilities for evaluating paths.
19 */
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000020namespace GrPathUtils {
bsalomon@google.com81712882012-11-01 17:12:34 +000021 SkScalar scaleToleranceToSrc(SkScalar devTol,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000022 const SkMatrix& viewM,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000023 const SkRect& pathBounds);
tomhudson@google.comc10a8882011-06-28 15:19:32 +000024
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000025 /// Since we divide by tol if we're computing exact worst-case bounds,
26 /// very small tolerances will be increased to gMinCurveTol.
bsalomon@google.com8d033a12012-04-27 15:52:53 +000027 int worstCasePointCount(const SkPath&,
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000028 int* subpaths,
bsalomon@google.com81712882012-11-01 17:12:34 +000029 SkScalar tol);
bsalomon@google.com19713172012-03-15 13:51:08 +000030
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000031 /// Since we divide by tol if we're computing exact worst-case bounds,
32 /// very small tolerances will be increased to gMinCurveTol.
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000033 uint32_t quadraticPointCount(const SkPoint points[], SkScalar tol);
bsalomon@google.com19713172012-03-15 13:51:08 +000034
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000035 uint32_t generateQuadraticPoints(const SkPoint& p0,
36 const SkPoint& p1,
37 const SkPoint& p2,
bsalomon@google.com81712882012-11-01 17:12:34 +000038 SkScalar tolSqd,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000039 SkPoint** points,
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000040 uint32_t pointsLeft);
bsalomon@google.com19713172012-03-15 13:51:08 +000041
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000042 /// Since we divide by tol if we're computing exact worst-case bounds,
43 /// very small tolerances will be increased to gMinCurveTol.
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000044 uint32_t cubicPointCount(const SkPoint points[], SkScalar tol);
bsalomon@google.com19713172012-03-15 13:51:08 +000045
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000046 uint32_t generateCubicPoints(const SkPoint& p0,
47 const SkPoint& p1,
48 const SkPoint& p2,
49 const SkPoint& p3,
bsalomon@google.com81712882012-11-01 17:12:34 +000050 SkScalar tolSqd,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000051 SkPoint** points,
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000052 uint32_t pointsLeft);
bsalomon@google.com19713172012-03-15 13:51:08 +000053
54 // A 2x3 matrix that goes from the 2d space coordinates to UV space where
55 // u^2-v = 0 specifies the quad. The matrix is determined by the control
56 // points of the quadratic.
57 class QuadUVMatrix {
58 public:
59 QuadUVMatrix() {};
60 // Initialize the matrix from the control pts
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000061 QuadUVMatrix(const SkPoint controlPts[3]) { this->set(controlPts); }
62 void set(const SkPoint controlPts[3]);
bsalomon@google.com19713172012-03-15 13:51:08 +000063
64 /**
65 * Applies the matrix to vertex positions to compute UV coords. This
66 * has been templated so that the compiler can easliy unroll the loop
67 * and reorder to avoid stalling for loads. The assumption is that a
68 * path renderer will have a small fixed number of vertices that it
69 * uploads for each quad.
70 *
71 * N is the number of vertices.
72 * STRIDE is the size of each vertex.
73 * UV_OFFSET is the offset of the UV values within each vertex.
74 * vertices is a pointer to the first vertex.
75 */
76 template <int N, size_t STRIDE, size_t UV_OFFSET>
joshualitt144c3c82015-11-30 12:30:13 -080077 void apply(const void* vertices) const {
bsalomon@google.com19713172012-03-15 13:51:08 +000078 intptr_t xyPtr = reinterpret_cast<intptr_t>(vertices);
79 intptr_t uvPtr = reinterpret_cast<intptr_t>(vertices) + UV_OFFSET;
80 float sx = fM[0];
81 float kx = fM[1];
82 float tx = fM[2];
83 float ky = fM[3];
84 float sy = fM[4];
85 float ty = fM[5];
86 for (int i = 0; i < N; ++i) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000087 const SkPoint* xy = reinterpret_cast<const SkPoint*>(xyPtr);
88 SkPoint* uv = reinterpret_cast<SkPoint*>(uvPtr);
bsalomon@google.com19713172012-03-15 13:51:08 +000089 uv->fX = sx * xy->fX + kx * xy->fY + tx;
90 uv->fY = ky * xy->fX + sy * xy->fY + ty;
91 xyPtr += STRIDE;
92 uvPtr += STRIDE;
93 }
94 }
95 private:
96 float fM[6];
97 };
98
commit-bot@chromium.org13948402013-08-20 17:55:43 +000099 // Input is 3 control points and a weight for a bezier conic. Calculates the
100 // three linear functionals (K,L,M) that represent the implicit equation of the
101 // conic, K^2 - LM.
102 //
103 // Output:
104 // K = (klm[0], klm[1], klm[2])
105 // L = (klm[3], klm[4], klm[5])
106 // M = (klm[6], klm[7], klm[8])
107 void getConicKLM(const SkPoint p[3], const SkScalar weight, SkScalar klm[9]);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000108
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000109 // Converts a cubic into a sequence of quads. If working in device space
110 // use tolScale = 1, otherwise set based on stretchiness of the matrix. The
bsalomon18fab302016-02-16 08:00:05 -0800111 // result is sets of 3 points in quads.
112 void convertCubicToQuads(const SkPoint p[4],
113 SkScalar tolScale,
114 SkTArray<SkPoint, true>* quads);
115
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000116 // When we approximate a cubic {a,b,c,d} with a quadratic we may have to
117 // ensure that the new control point lies between the lines ab and cd. The
118 // convex path renderer requires this. It starts with a path where all the
119 // control points taken together form a convex polygon. It relies on this
120 // property and the quadratic approximation of cubics step cannot alter it.
bsalomon18fab302016-02-16 08:00:05 -0800121 // This variation enforces this constraint. The cubic must be simple and dir
122 // must specify the orientation of the contour containing the cubic.
123 void convertCubicToQuadsConstrainToTangents(const SkPoint p[4],
124 SkScalar tolScale,
125 SkPathPriv::FirstDirection dir,
126 SkTArray<SkPoint, true>* quads);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000127
128 // Chops the cubic bezier passed in by src, at the double point (intersection point)
129 // if the curve is a cubic loop. If it is a loop, there will be two parametric values for
130 // the double point: ls and ms. We chop the cubic at these values if they are between 0 and 1.
131 // Return value:
132 // Value of 3: ls and ms are both between (0,1), and dst will contain the three cubics,
halcanary96fcdcc2015-08-27 07:41:13 -0700133 // dst[0..3], dst[3..6], and dst[6..9] if dst is not nullptr
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000134 // Value of 2: Only one of ls and ms are between (0,1), and dst will contain the two cubics,
halcanary96fcdcc2015-08-27 07:41:13 -0700135 // dst[0..3] and dst[3..6] if dst is not nullptr
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000136 // Value of 1: Neither ls or ms are between (0,1), and dst will contain the one original cubic,
halcanary96fcdcc2015-08-27 07:41:13 -0700137 // dst[0..3] if dst is not nullptr
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000138 //
139 // Optional KLM Calculation:
140 // The function can also return the KLM linear functionals for the chopped cubic implicit form
141 // of K^3 - LM.
142 // It will calculate a single set of KLM values that can be shared by all sub cubics, except
143 // for the subsection that is "the loop" the K and L values need to be negated.
144 // Output:
145 // klm: Holds the values for the linear functionals as:
146 // K = (klm[0], klm[1], klm[2])
147 // L = (klm[3], klm[4], klm[5])
148 // M = (klm[6], klm[7], klm[8])
149 // klm_rev: These values are flags for the corresponding sub cubic saying whether or not
150 // the K and L values need to be flipped. A value of -1.f means flip K and L and
151 // a value of 1.f means do nothing.
152 // *****DO NOT FLIP M, JUST K AND L*****
153 //
154 // Notice that the klm lines are calculated in the same space as the input control points.
155 // If you transform the points the lines will also need to be transformed. This can be done
156 // by mapping the lines with the inverse-transpose of the matrix used to map the points.
halcanary96fcdcc2015-08-27 07:41:13 -0700157 int chopCubicAtLoopIntersection(const SkPoint src[4], SkPoint dst[10] = nullptr,
158 SkScalar klm[9] = nullptr, SkScalar klm_rev[3] = nullptr);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000159
160 // Input is p which holds the 4 control points of a non-rational cubic Bezier curve.
161 // Output is the coefficients of the three linear functionals K, L, & M which
162 // represent the implicit form of the cubic as f(x,y,w) = K^3 - LM. The w term
163 // will always be 1. The output is stored in the array klm, where the values are:
164 // K = (klm[0], klm[1], klm[2])
165 // L = (klm[3], klm[4], klm[5])
166 // M = (klm[6], klm[7], klm[8])
167 //
168 // Notice that the klm lines are calculated in the same space as the input control points.
169 // If you transform the points the lines will also need to be transformed. This can be done
170 // by mapping the lines with the inverse-transpose of the matrix used to map the points.
171 void getCubicKLM(const SkPoint p[4], SkScalar klm[9]);
senorblanco2b4bb072015-04-22 13:45:18 -0700172
173 // When tessellating curved paths into linear segments, this defines the maximum distance
174 // in screen space which a segment may deviate from the mathmatically correct value.
175 // Above this value, the segment will be subdivided.
176 // This value was chosen to approximate the supersampling accuracy of the raster path (16
177 // samples, or one quarter pixel).
178 static const SkScalar kDefaultTolerance = SkDoubleToScalar(0.25);
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000179};
180#endif