blob: 16c8b36a6a9d2d97fec23462a8e7b363ca695ac1 [file] [log] [blame]
Chris Craik65cd6122012-12-10 17:56:27 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_HWUI_PATH_TESSELLATOR_H
18#define ANDROID_HWUI_PATH_TESSELLATOR_H
19
20#include <utils/Vector.h>
21
22#include "Matrix.h"
23#include "Rect.h"
24#include "Vertex.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080025#include "VertexBuffer.h"
Chris Craik65cd6122012-12-10 17:56:27 -080026
27namespace android {
28namespace uirenderer {
29
ztenghui21ef8202015-05-28 16:06:28 -070030/**
31 * Structure used for threshold values in outline path tessellation.
32 *
33 * TODO: PaintInfo should store one of this object, and initialized all values in constructor
34 * depending on its type (point, line or path).
35 */
36struct PathApproximationInfo {
37 PathApproximationInfo(float invScaleX, float invScaleY, float pixelThreshold)
38 : thresholdSquared(pixelThreshold * pixelThreshold)
39 , sqrInvScaleX(invScaleX * invScaleX)
40 , sqrInvScaleY(invScaleY * invScaleY)
41 , thresholdForConicQuads(pixelThreshold * MathUtils::min(invScaleX, invScaleY) / 2.0f) {
42 };
43
44 const float thresholdSquared;
45 const float sqrInvScaleX;
46 const float sqrInvScaleY;
47 const float thresholdForConicQuads;
48};
49
Chris Craik65cd6122012-12-10 17:56:27 -080050class PathTessellator {
51public:
Chris Craik05f3d6e2014-06-02 16:27:04 -070052 /**
53 * Populates scaleX and scaleY with the 'tessellation scale' of the transform - the effective X
54 * and Y scales that tessellation will take into account when generating the 1.0 pixel thick
55 * ramp.
56 *
57 * Two instances of the same shape (size, paint, etc.) will only generate the same vertices if
58 * their tessellation scales are equal.
59 */
60 static void extractTessellationScales(const Matrix4& transform, float* scaleX, float* scaleY);
Chris Craik65cd6122012-12-10 17:56:27 -080061
Chris Craik15a07a22014-01-26 13:43:53 -080062 /**
63 * Populates a VertexBuffer with a tessellated approximation of the input convex path, as a single
64 * triangle strip. Note: joins are not currently supported.
65 *
66 * @param path The path to be approximated
67 * @param paint The paint the path will be drawn with, indicating AA, painting style
68 * (stroke vs fill), stroke width, stroke cap & join style, etc.
69 * @param transform The transform the path is to be drawn with, used to drive stretch-aware path
70 * vertex approximation, and correct AA ramp offsetting.
71 * @param vertexBuffer The output buffer
72 */
Chris Craik65cd6122012-12-10 17:56:27 -080073 static void tessellatePath(const SkPath& path, const SkPaint* paint,
Chris Craikd6b65f62014-01-01 14:45:21 -080074 const mat4& transform, VertexBuffer& vertexBuffer);
Chris Craik65cd6122012-12-10 17:56:27 -080075
Chris Craik15a07a22014-01-26 13:43:53 -080076 /**
77 * Populates a VertexBuffer with a tessellated approximation of points as a single triangle
78 * strip (with degenerate tris separating), respecting the shape defined by the paint cap.
79 *
80 * @param points The center vertices of the points to be drawn
81 * @param count The number of floats making up the point vertices
82 * @param paint The paint the points will be drawn with indicating AA, stroke width & cap
83 * @param transform The transform the points will be drawn with, used to drive stretch-aware path
84 * vertex approximation, and correct AA ramp offsetting
Chris Craik15a07a22014-01-26 13:43:53 -080085 * @param vertexBuffer The output buffer
86 */
Chris Craikd218a922014-01-02 17:13:34 -080087 static void tessellatePoints(const float* points, int count, const SkPaint* paint,
Chris Craik05f3d6e2014-06-02 16:27:04 -070088 const mat4& transform, VertexBuffer& vertexBuffer);
Chris Craik6d29c8d2013-05-08 18:35:44 -070089
Chris Craik15a07a22014-01-26 13:43:53 -080090 /**
91 * Populates a VertexBuffer with a tessellated approximation of lines as a single triangle
92 * strip (with degenerate tris separating).
93 *
94 * @param points Pairs of endpoints defining the lines to be drawn
95 * @param count The number of floats making up the line vertices
96 * @param paint The paint the lines will be drawn with indicating AA, stroke width & cap
97 * @param transform The transform the points will be drawn with, used to drive stretch-aware path
98 * vertex approximation, and correct AA ramp offsetting
Chris Craik15a07a22014-01-26 13:43:53 -080099 * @param vertexBuffer The output buffer
100 */
Chris Craikd218a922014-01-02 17:13:34 -0800101 static void tessellateLines(const float* points, int count, const SkPaint* paint,
Chris Craik05f3d6e2014-06-02 16:27:04 -0700102 const mat4& transform, VertexBuffer& vertexBuffer);
Chris Craik65cd6122012-12-10 17:56:27 -0800103
Chris Craik15a07a22014-01-26 13:43:53 -0800104 /**
Chris Craikfca52b752015-04-28 11:45:59 -0700105 * Approximates a convex outline into a clockwise Vector of 2d vertices.
Chris Craik15a07a22014-01-26 13:43:53 -0800106 *
107 * @param path The outline to be approximated
ztenghui21ef8202015-05-28 16:06:28 -0700108 * @param threshold The threshold of acceptable error (in pixels) when approximating
Chris Craik15a07a22014-01-26 13:43:53 -0800109 * @param outputVertices An empty Vector which will be populated with the output
110 */
ztenghui21ef8202015-05-28 16:06:28 -0700111 static bool approximatePathOutlineVertices(const SkPath &path, float threshold,
Chris Craik15a07a22014-01-26 13:43:53 -0800112 Vector<Vertex> &outputVertices);
113
Chris Craik65cd6122012-12-10 17:56:27 -0800114private:
115 static bool approximatePathOutlineVertices(const SkPath &path, bool forceClose,
ztenghui21ef8202015-05-28 16:06:28 -0700116 const PathApproximationInfo& approximationInfo, Vector<Vertex> &outputVertices);
Chris Craik65cd6122012-12-10 17:56:27 -0800117
118/*
119 endpoints a & b,
120 control c
121 */
122 static void recursiveQuadraticBezierVertices(
123 float ax, float ay,
124 float bx, float by,
125 float cx, float cy,
ztenghui21ef8202015-05-28 16:06:28 -0700126 const PathApproximationInfo& approximationInfo,
Chris Craik9c3dd622014-06-11 17:24:51 -0700127 Vector<Vertex> &outputVertices, int depth = 0);
Chris Craik65cd6122012-12-10 17:56:27 -0800128
129/*
130 endpoints p1, p2
131 control c1, c2
132 */
133 static void recursiveCubicBezierVertices(
134 float p1x, float p1y,
135 float c1x, float c1y,
136 float p2x, float p2y,
137 float c2x, float c2y,
ztenghui21ef8202015-05-28 16:06:28 -0700138 const PathApproximationInfo& approximationInfo,
Chris Craik9c3dd622014-06-11 17:24:51 -0700139 Vector<Vertex> &outputVertices, int depth = 0);
Chris Craik65cd6122012-12-10 17:56:27 -0800140};
141
142}; // namespace uirenderer
143}; // namespace android
144
145#endif // ANDROID_HWUI_PATH_TESSELLATOR_H