blob: 31b639864e36437e249a127dc18810c6dcff45d6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.org9d18b782011-03-28 20:47:09 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000010#ifndef GrPathUtils_DEFINED
11#define GrPathUtils_DEFINED
12
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000013#include "GrMatrix.h"
bsalomon@google.com8d033a12012-04-27 15:52:53 +000014#include "SkPath.h"
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000015#include "SkTArray.h"
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000016
17/**
18 * Utilities for evaluating paths.
19 */
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000020namespace GrPathUtils {
21 GrScalar scaleToleranceToSrc(GrScalar devTol,
bsalomon@google.com38396322011-09-09 19:32:04 +000022 const GrMatrix& viewM,
23 const GrRect& 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,
29 GrScalar 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.
33 uint32_t quadraticPointCount(const GrPoint points[], GrScalar tol);
bsalomon@google.com19713172012-03-15 13:51:08 +000034
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000035 uint32_t generateQuadraticPoints(const GrPoint& p0,
36 const GrPoint& p1,
37 const GrPoint& p2,
38 GrScalar tolSqd,
39 GrPoint** points,
40 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.
44 uint32_t cubicPointCount(const GrPoint points[], GrScalar tol);
bsalomon@google.com19713172012-03-15 13:51:08 +000045
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000046 uint32_t generateCubicPoints(const GrPoint& p0,
47 const GrPoint& p1,
48 const GrPoint& p2,
49 const GrPoint& p3,
50 GrScalar tolSqd,
51 GrPoint** points,
52 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
61 QuadUVMatrix(const GrPoint controlPts[3]) { this->set(controlPts); }
62 void set(const GrPoint 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>
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 GrPoint* xy = reinterpret_cast<const GrPoint*>(xyPtr);
88 GrPoint* uv = reinterpret_cast<GrPoint*>(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
bsalomon@google.coma51ab842012-07-10 19:53:34 +000099
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000100 // Converts a cubic into a sequence of quads. If working in device space
101 // use tolScale = 1, otherwise set based on stretchiness of the matrix. The
102 // result is sets of 3 points in quads (TODO: share endpoints in returned
103 // array)
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000104 // When we approximate a cubic {a,b,c,d} with a quadratic we may have to
105 // ensure that the new control point lies between the lines ab and cd. The
106 // convex path renderer requires this. It starts with a path where all the
107 // control points taken together form a convex polygon. It relies on this
108 // property and the quadratic approximation of cubics step cannot alter it.
109 // Setting constrainWithinTangents to true enforces this property. When this
110 // is true the cubic must be simple and dir must specify the orientation of
111 // the cubic. Otherwise, dir is ignored.
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000112 void convertCubicToQuads(const GrPoint p[4],
113 SkScalar tolScale,
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000114 bool constrainWithinTangents,
115 SkPath::Direction dir,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000116 SkTArray<SkPoint, true>* quads);
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000117};
118#endif