blob: fc319ec50d93e7fd67e8ed480ea08428d5c7d26a [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 "GrPoint.h"
12#include "SkRect.h"
bsalomon@google.com8d033a12012-04-27 15:52:53 +000013#include "SkPath.h"
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000014#include "SkTArray.h"
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000015
bsalomon@google.comb9086a02012-11-01 18:02:54 +000016class SkMatrix;
17
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000018/**
19 * Utilities for evaluating paths.
20 */
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000021namespace GrPathUtils {
bsalomon@google.com81712882012-11-01 17:12:34 +000022 SkScalar scaleToleranceToSrc(SkScalar devTol,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000023 const SkMatrix& viewM,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000024 const SkRect& pathBounds);
tomhudson@google.comc10a8882011-06-28 15:19:32 +000025
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000026 /// Since we divide by tol if we're computing exact worst-case bounds,
27 /// very small tolerances will be increased to gMinCurveTol.
bsalomon@google.com8d033a12012-04-27 15:52:53 +000028 int worstCasePointCount(const SkPath&,
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000029 int* subpaths,
bsalomon@google.com81712882012-11-01 17:12:34 +000030 SkScalar tol);
bsalomon@google.com19713172012-03-15 13:51:08 +000031
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000032 /// Since we divide by tol if we're computing exact worst-case bounds,
33 /// very small tolerances will be increased to gMinCurveTol.
bsalomon@google.com81712882012-11-01 17:12:34 +000034 uint32_t quadraticPointCount(const GrPoint points[], SkScalar tol);
bsalomon@google.com19713172012-03-15 13:51:08 +000035
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000036 uint32_t generateQuadraticPoints(const GrPoint& p0,
37 const GrPoint& p1,
38 const GrPoint& p2,
bsalomon@google.com81712882012-11-01 17:12:34 +000039 SkScalar tolSqd,
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000040 GrPoint** points,
41 uint32_t pointsLeft);
bsalomon@google.com19713172012-03-15 13:51:08 +000042
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000043 /// Since we divide by tol if we're computing exact worst-case bounds,
44 /// very small tolerances will be increased to gMinCurveTol.
bsalomon@google.com81712882012-11-01 17:12:34 +000045 uint32_t cubicPointCount(const GrPoint points[], SkScalar tol);
bsalomon@google.com19713172012-03-15 13:51:08 +000046
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000047 uint32_t generateCubicPoints(const GrPoint& p0,
48 const GrPoint& p1,
49 const GrPoint& p2,
50 const GrPoint& p3,
bsalomon@google.com81712882012-11-01 17:12:34 +000051 SkScalar tolSqd,
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000052 GrPoint** points,
53 uint32_t pointsLeft);
bsalomon@google.com19713172012-03-15 13:51:08 +000054
55 // A 2x3 matrix that goes from the 2d space coordinates to UV space where
56 // u^2-v = 0 specifies the quad. The matrix is determined by the control
57 // points of the quadratic.
58 class QuadUVMatrix {
59 public:
60 QuadUVMatrix() {};
61 // Initialize the matrix from the control pts
62 QuadUVMatrix(const GrPoint controlPts[3]) { this->set(controlPts); }
63 void set(const GrPoint controlPts[3]);
64
65 /**
66 * Applies the matrix to vertex positions to compute UV coords. This
67 * has been templated so that the compiler can easliy unroll the loop
68 * and reorder to avoid stalling for loads. The assumption is that a
69 * path renderer will have a small fixed number of vertices that it
70 * uploads for each quad.
71 *
72 * N is the number of vertices.
73 * STRIDE is the size of each vertex.
74 * UV_OFFSET is the offset of the UV values within each vertex.
75 * vertices is a pointer to the first vertex.
76 */
77 template <int N, size_t STRIDE, size_t UV_OFFSET>
78 void apply(const void* vertices) {
79 intptr_t xyPtr = reinterpret_cast<intptr_t>(vertices);
80 intptr_t uvPtr = reinterpret_cast<intptr_t>(vertices) + UV_OFFSET;
81 float sx = fM[0];
82 float kx = fM[1];
83 float tx = fM[2];
84 float ky = fM[3];
85 float sy = fM[4];
86 float ty = fM[5];
87 for (int i = 0; i < N; ++i) {
88 const GrPoint* xy = reinterpret_cast<const GrPoint*>(xyPtr);
89 GrPoint* uv = reinterpret_cast<GrPoint*>(uvPtr);
90 uv->fX = sx * xy->fX + kx * xy->fY + tx;
91 uv->fY = ky * xy->fX + sy * xy->fY + ty;
92 xyPtr += STRIDE;
93 uvPtr += STRIDE;
94 }
95 }
96 private:
97 float fM[6];
98 };
99
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000100
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000101 // Converts a cubic into a sequence of quads. If working in device space
102 // use tolScale = 1, otherwise set based on stretchiness of the matrix. The
103 // result is sets of 3 points in quads (TODO: share endpoints in returned
104 // array)
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000105 // When we approximate a cubic {a,b,c,d} with a quadratic we may have to
106 // ensure that the new control point lies between the lines ab and cd. The
107 // convex path renderer requires this. It starts with a path where all the
108 // control points taken together form a convex polygon. It relies on this
109 // property and the quadratic approximation of cubics step cannot alter it.
110 // Setting constrainWithinTangents to true enforces this property. When this
111 // is true the cubic must be simple and dir must specify the orientation of
112 // the cubic. Otherwise, dir is ignored.
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000113 void convertCubicToQuads(const GrPoint p[4],
114 SkScalar tolScale,
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000115 bool constrainWithinTangents,
116 SkPath::Direction dir,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000117 SkTArray<SkPoint, true>* quads);
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000118};
119#endif