GrPathUtils.h revision 81712883419f76e25d2ffec38a9438284a45a48d
1
2/*
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.
7 */
8
9
10#ifndef GrPathUtils_DEFINED
11#define GrPathUtils_DEFINED
12
13#include "GrMatrix.h"
14#include "SkPath.h"
15#include "SkTArray.h"
16
17/**
18 *  Utilities for evaluating paths.
19 */
20namespace GrPathUtils {
21    SkScalar scaleToleranceToSrc(SkScalar devTol,
22                                 const GrMatrix& viewM,
23                                 const GrRect& 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 GrPoint points[], SkScalar tol);
34
35    uint32_t generateQuadraticPoints(const GrPoint& p0,
36                                     const GrPoint& p1,
37                                     const GrPoint& p2,
38                                     SkScalar tolSqd,
39                                     GrPoint** 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 GrPoint points[], SkScalar tol);
45
46    uint32_t generateCubicPoints(const GrPoint& p0,
47                                 const GrPoint& p1,
48                                 const GrPoint& p2,
49                                 const GrPoint& p3,
50                                 SkScalar tolSqd,
51                                 GrPoint** 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:
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
99
100    // 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)
104    // 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.
112    void convertCubicToQuads(const GrPoint p[4],
113                             SkScalar tolScale,
114                             bool constrainWithinTangents,
115                             SkPath::Direction dir,
116                             SkTArray<SkPoint, true>* quads);
117};
118#endif
119