• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * 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.
6  */
7 
8 #ifndef GrPathUtils_DEFINED
9 #define GrPathUtils_DEFINED
10 
11 #include "SkRect.h"
12 #include "SkPath.h"
13 #include "SkTArray.h"
14 
15 class SkMatrix;
16 
17 /**
18  *  Utilities for evaluating paths.
19  */
20 namespace GrPathUtils {
21     SkScalar scaleToleranceToSrc(SkScalar devTol,
22                                  const SkMatrix& viewM,
23                                  const SkRect& pathBounds);
24 
25     /// Since we divide by tol if we're computing exact worst-case bounds,
26     /// very small tolerances will be increased to gMinCurveTol.
27     int worstCasePointCount(const SkPath&,
28                             int* subpaths,
29                             SkScalar tol);
30 
31     /// Since we divide by tol if we're computing exact worst-case bounds,
32     /// very small tolerances will be increased to gMinCurveTol.
33     uint32_t quadraticPointCount(const SkPoint points[], SkScalar tol);
34 
35     uint32_t generateQuadraticPoints(const SkPoint& p0,
36                                      const SkPoint& p1,
37                                      const SkPoint& p2,
38                                      SkScalar tolSqd,
39                                      SkPoint** points,
40                                      uint32_t pointsLeft);
41 
42     /// Since we divide by tol if we're computing exact worst-case bounds,
43     /// very small tolerances will be increased to gMinCurveTol.
44     uint32_t cubicPointCount(const SkPoint points[], SkScalar tol);
45 
46     uint32_t generateCubicPoints(const SkPoint& p0,
47                                  const SkPoint& p1,
48                                  const SkPoint& p2,
49                                  const SkPoint& p3,
50                                  SkScalar tolSqd,
51                                  SkPoint** points,
52                                  uint32_t pointsLeft);
53 
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:
QuadUVMatrix()59         QuadUVMatrix() {};
60         // Initialize the matrix from the control pts
QuadUVMatrix(const SkPoint controlPts[3])61         QuadUVMatrix(const SkPoint controlPts[3]) { this->set(controlPts); }
62         void set(const SkPoint controlPts[3]);
63 
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>
apply(const void * vertices)77         void apply(const void* vertices) {
78             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) {
87                 const SkPoint* xy = reinterpret_cast<const SkPoint*>(xyPtr);
88                 SkPoint* uv = reinterpret_cast<SkPoint*>(uvPtr);
89                 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 
99     // 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]);
108 
109     // 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
111     // result is sets of 3 points in quads (TODO: share endpoints in returned
112     // array)
113     // When we approximate a cubic {a,b,c,d} with a quadratic we may have to
114     // ensure that the new control point lies between the lines ab and cd. The
115     // convex path renderer requires this. It starts with a path where all the
116     // control points taken together form a convex polygon. It relies on this
117     // property and the quadratic approximation of cubics step cannot alter it.
118     // Setting constrainWithinTangents to true enforces this property. When this
119     // is true the cubic must be simple and dir must specify the orientation of
120     // the cubic. Otherwise, dir is ignored.
121     void convertCubicToQuads(const SkPoint p[4],
122                              SkScalar tolScale,
123                              bool constrainWithinTangents,
124                              SkPath::Direction dir,
125                              SkTArray<SkPoint, true>* quads);
126 
127     // Chops the cubic bezier passed in by src, at the double point (intersection point)
128     // if the curve is a cubic loop. If it is a loop, there will be two parametric values for
129     // the double point: ls and ms. We chop the cubic at these values if they are between 0 and 1.
130     // Return value:
131     // Value of 3: ls and ms are both between (0,1), and dst will contain the three cubics,
132     //             dst[0..3], dst[3..6], and dst[6..9] if dst is not NULL
133     // Value of 2: Only one of ls and ms are between (0,1), and dst will contain the two cubics,
134     //             dst[0..3] and dst[3..6] if dst is not NULL
135     // Value of 1: Neither ls or ms are between (0,1), and dst will contain the one original cubic,
136     //             dst[0..3] if dst is not NULL
137     //
138     // Optional KLM Calculation:
139     // The function can also return the KLM linear functionals for the chopped cubic implicit form
140     // of K^3 - LM.
141     // It will calculate a single set of KLM values that can be shared by all sub cubics, except
142     // for the subsection that is "the loop" the K and L values need to be negated.
143     // Output:
144     // klm:     Holds the values for the linear functionals as:
145     //          K = (klm[0], klm[1], klm[2])
146     //          L = (klm[3], klm[4], klm[5])
147     //          M = (klm[6], klm[7], klm[8])
148     // klm_rev: These values are flags for the corresponding sub cubic saying whether or not
149     //          the K and L values need to be flipped. A value of -1.f means flip K and L and
150     //          a value of 1.f means do nothing.
151     //          *****DO NOT FLIP M, JUST K AND L*****
152     //
153     // Notice that the klm lines are calculated in the same space as the input control points.
154     // If you transform the points the lines will also need to be transformed. This can be done
155     // by mapping the lines with the inverse-transpose of the matrix used to map the points.
156     int chopCubicAtLoopIntersection(const SkPoint src[4], SkPoint dst[10] = NULL,
157                                     SkScalar klm[9] = NULL, SkScalar klm_rev[3] = NULL);
158 
159     // Input is p which holds the 4 control points of a non-rational cubic Bezier curve.
160     // Output is the coefficients of the three linear functionals K, L, & M which
161     // represent the implicit form of the cubic as f(x,y,w) = K^3 - LM. The w term
162     // will always be 1. The output is stored in the array klm, where the values are:
163     // K = (klm[0], klm[1], klm[2])
164     // L = (klm[3], klm[4], klm[5])
165     // M = (klm[6], klm[7], klm[8])
166     //
167     // Notice that the klm lines are calculated in the same space as the input control points.
168     // If you transform the points the lines will also need to be transformed. This can be done
169     // by mapping the lines with the inverse-transpose of the matrix used to map the points.
170     void getCubicKLM(const SkPoint p[4], SkScalar klm[9]);
171 };
172 #endif
173