epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 10 | #ifndef GrPathUtils_DEFINED |
| 11 | #define GrPathUtils_DEFINED |
| 12 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 13 | #include "GrRect.h" |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 14 | #include "SkPath.h" |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 15 | #include "SkTArray.h" |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 16 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 17 | class SkMatrix; |
| 18 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 19 | /** |
| 20 | * Utilities for evaluating paths. |
| 21 | */ |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 22 | namespace GrPathUtils { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 23 | SkScalar scaleToleranceToSrc(SkScalar devTol, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 24 | const SkMatrix& viewM, |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 25 | const GrRect& pathBounds); |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 26 | |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 27 | /// Since we divide by tol if we're computing exact worst-case bounds, |
| 28 | /// very small tolerances will be increased to gMinCurveTol. |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 29 | int worstCasePointCount(const SkPath&, |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 30 | int* subpaths, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 31 | SkScalar tol); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 32 | |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 33 | /// Since we divide by tol if we're computing exact worst-case bounds, |
| 34 | /// very small tolerances will be increased to gMinCurveTol. |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 35 | uint32_t quadraticPointCount(const GrPoint points[], SkScalar tol); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 36 | |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 37 | uint32_t generateQuadraticPoints(const GrPoint& p0, |
| 38 | const GrPoint& p1, |
| 39 | const GrPoint& p2, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 40 | SkScalar tolSqd, |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 41 | GrPoint** points, |
| 42 | uint32_t pointsLeft); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 43 | |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 44 | /// Since we divide by tol if we're computing exact worst-case bounds, |
| 45 | /// very small tolerances will be increased to gMinCurveTol. |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 46 | uint32_t cubicPointCount(const GrPoint points[], SkScalar tol); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 47 | |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 48 | uint32_t generateCubicPoints(const GrPoint& p0, |
| 49 | const GrPoint& p1, |
| 50 | const GrPoint& p2, |
| 51 | const GrPoint& p3, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 52 | SkScalar tolSqd, |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 53 | GrPoint** points, |
| 54 | uint32_t pointsLeft); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 55 | |
| 56 | // A 2x3 matrix that goes from the 2d space coordinates to UV space where |
| 57 | // u^2-v = 0 specifies the quad. The matrix is determined by the control |
| 58 | // points of the quadratic. |
| 59 | class QuadUVMatrix { |
| 60 | public: |
| 61 | QuadUVMatrix() {}; |
| 62 | // Initialize the matrix from the control pts |
| 63 | QuadUVMatrix(const GrPoint controlPts[3]) { this->set(controlPts); } |
| 64 | void set(const GrPoint controlPts[3]); |
| 65 | |
| 66 | /** |
| 67 | * Applies the matrix to vertex positions to compute UV coords. This |
| 68 | * has been templated so that the compiler can easliy unroll the loop |
| 69 | * and reorder to avoid stalling for loads. The assumption is that a |
| 70 | * path renderer will have a small fixed number of vertices that it |
| 71 | * uploads for each quad. |
| 72 | * |
| 73 | * N is the number of vertices. |
| 74 | * STRIDE is the size of each vertex. |
| 75 | * UV_OFFSET is the offset of the UV values within each vertex. |
| 76 | * vertices is a pointer to the first vertex. |
| 77 | */ |
| 78 | template <int N, size_t STRIDE, size_t UV_OFFSET> |
| 79 | void apply(const void* vertices) { |
| 80 | intptr_t xyPtr = reinterpret_cast<intptr_t>(vertices); |
| 81 | intptr_t uvPtr = reinterpret_cast<intptr_t>(vertices) + UV_OFFSET; |
| 82 | float sx = fM[0]; |
| 83 | float kx = fM[1]; |
| 84 | float tx = fM[2]; |
| 85 | float ky = fM[3]; |
| 86 | float sy = fM[4]; |
| 87 | float ty = fM[5]; |
| 88 | for (int i = 0; i < N; ++i) { |
| 89 | const GrPoint* xy = reinterpret_cast<const GrPoint*>(xyPtr); |
| 90 | GrPoint* uv = reinterpret_cast<GrPoint*>(uvPtr); |
| 91 | uv->fX = sx * xy->fX + kx * xy->fY + tx; |
| 92 | uv->fY = ky * xy->fX + sy * xy->fY + ty; |
| 93 | xyPtr += STRIDE; |
| 94 | uvPtr += STRIDE; |
| 95 | } |
| 96 | } |
| 97 | private: |
| 98 | float fM[6]; |
| 99 | }; |
| 100 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 101 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 102 | // Converts a cubic into a sequence of quads. If working in device space |
| 103 | // use tolScale = 1, otherwise set based on stretchiness of the matrix. The |
| 104 | // result is sets of 3 points in quads (TODO: share endpoints in returned |
| 105 | // array) |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 106 | // When we approximate a cubic {a,b,c,d} with a quadratic we may have to |
| 107 | // ensure that the new control point lies between the lines ab and cd. The |
| 108 | // convex path renderer requires this. It starts with a path where all the |
| 109 | // control points taken together form a convex polygon. It relies on this |
| 110 | // property and the quadratic approximation of cubics step cannot alter it. |
| 111 | // Setting constrainWithinTangents to true enforces this property. When this |
| 112 | // is true the cubic must be simple and dir must specify the orientation of |
| 113 | // the cubic. Otherwise, dir is ignored. |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 114 | void convertCubicToQuads(const GrPoint p[4], |
| 115 | SkScalar tolScale, |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 116 | bool constrainWithinTangents, |
| 117 | SkPath::Direction dir, |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 118 | SkTArray<SkPoint, true>* quads); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 119 | }; |
| 120 | #endif |