blob: ed268324b341a56140f29289091f867f63aa1b23 [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
Chris Craik65cd6122012-12-10 17:56:27 -080020#include "Matrix.h"
21#include "Rect.h"
22#include "Vertex.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080023#include "VertexBuffer.h"
Chris Craik65cd6122012-12-10 17:56:27 -080024
Chris Craik9db58c02015-08-19 15:19:18 -070025#include <algorithm>
John Reck272a6852015-07-29 16:48:58 -070026#include <vector>
27
Chris Craik9db58c02015-08-19 15:19:18 -070028class SkPath;
29class SkPaint;
30
Chris Craik65cd6122012-12-10 17:56:27 -080031namespace android {
32namespace uirenderer {
33
ztenghui21ef8202015-05-28 16:06:28 -070034/**
35 * Structure used for threshold values in outline path tessellation.
36 *
37 * TODO: PaintInfo should store one of this object, and initialized all values in constructor
38 * depending on its type (point, line or path).
39 */
40struct PathApproximationInfo {
41 PathApproximationInfo(float invScaleX, float invScaleY, float pixelThreshold)
John Reck1bcacfd2017-11-03 10:12:19 -070042 : thresholdSquared(pixelThreshold * pixelThreshold)
43 , sqrInvScaleX(invScaleX * invScaleX)
44 , sqrInvScaleY(invScaleY * invScaleY)
45 , thresholdForConicQuads(pixelThreshold * std::min(invScaleX, invScaleY) / 2.0f){};
ztenghui21ef8202015-05-28 16:06:28 -070046
47 const float thresholdSquared;
48 const float sqrInvScaleX;
49 const float sqrInvScaleY;
50 const float thresholdForConicQuads;
51};
52
Chris Craik65cd6122012-12-10 17:56:27 -080053class PathTessellator {
54public:
Chris Craik05f3d6e2014-06-02 16:27:04 -070055 /**
56 * Populates scaleX and scaleY with the 'tessellation scale' of the transform - the effective X
57 * and Y scales that tessellation will take into account when generating the 1.0 pixel thick
58 * ramp.
59 *
60 * Two instances of the same shape (size, paint, etc.) will only generate the same vertices if
61 * their tessellation scales are equal.
62 */
63 static void extractTessellationScales(const Matrix4& transform, float* scaleX, float* scaleY);
Chris Craik65cd6122012-12-10 17:56:27 -080064
Chris Craik15a07a22014-01-26 13:43:53 -080065 /**
John Reck1bcacfd2017-11-03 10:12:19 -070066 * Populates a VertexBuffer with a tessellated approximation of the input convex path, as a
67 * single
Chris Craik15a07a22014-01-26 13:43:53 -080068 * triangle strip. Note: joins are not currently supported.
69 *
70 * @param path The path to be approximated
71 * @param paint The paint the path will be drawn with, indicating AA, painting style
72 * (stroke vs fill), stroke width, stroke cap & join style, etc.
73 * @param transform The transform the path is to be drawn with, used to drive stretch-aware path
74 * vertex approximation, and correct AA ramp offsetting.
75 * @param vertexBuffer The output buffer
76 */
John Reck1bcacfd2017-11-03 10:12:19 -070077 static void tessellatePath(const SkPath& path, const SkPaint* paint, const mat4& transform,
78 VertexBuffer& vertexBuffer);
Chris Craik65cd6122012-12-10 17:56:27 -080079
Chris Craik15a07a22014-01-26 13:43:53 -080080 /**
81 * Populates a VertexBuffer with a tessellated approximation of points as a single triangle
82 * strip (with degenerate tris separating), respecting the shape defined by the paint cap.
83 *
84 * @param points The center vertices of the points to be drawn
85 * @param count The number of floats making up the point vertices
86 * @param paint The paint the points will be drawn with indicating AA, stroke width & cap
John Reck1bcacfd2017-11-03 10:12:19 -070087 * @param transform The transform the points will be drawn with, used to drive stretch-aware
88 * path
Chris Craik15a07a22014-01-26 13:43:53 -080089 * vertex approximation, and correct AA ramp offsetting
Chris Craik15a07a22014-01-26 13:43:53 -080090 * @param vertexBuffer The output buffer
91 */
Chris Craikd218a922014-01-02 17:13:34 -080092 static void tessellatePoints(const float* points, int count, const SkPaint* paint,
John Reck1bcacfd2017-11-03 10:12:19 -070093 const mat4& transform, VertexBuffer& vertexBuffer);
Chris Craik6d29c8d2013-05-08 18:35:44 -070094
Chris Craik15a07a22014-01-26 13:43:53 -080095 /**
96 * Populates a VertexBuffer with a tessellated approximation of lines as a single triangle
97 * strip (with degenerate tris separating).
98 *
99 * @param points Pairs of endpoints defining the lines to be drawn
100 * @param count The number of floats making up the line vertices
101 * @param paint The paint the lines will be drawn with indicating AA, stroke width & cap
John Reck1bcacfd2017-11-03 10:12:19 -0700102 * @param transform The transform the points will be drawn with, used to drive stretch-aware
103 * path
Chris Craik15a07a22014-01-26 13:43:53 -0800104 * vertex approximation, and correct AA ramp offsetting
Chris Craik15a07a22014-01-26 13:43:53 -0800105 * @param vertexBuffer The output buffer
106 */
Chris Craikd218a922014-01-02 17:13:34 -0800107 static void tessellateLines(const float* points, int count, const SkPaint* paint,
John Reck1bcacfd2017-11-03 10:12:19 -0700108 const mat4& transform, VertexBuffer& vertexBuffer);
Chris Craik65cd6122012-12-10 17:56:27 -0800109
Chris Craik15a07a22014-01-26 13:43:53 -0800110 /**
Chris Craikfca52b752015-04-28 11:45:59 -0700111 * Approximates a convex outline into a clockwise Vector of 2d vertices.
Chris Craik15a07a22014-01-26 13:43:53 -0800112 *
113 * @param path The outline to be approximated
ztenghui21ef8202015-05-28 16:06:28 -0700114 * @param threshold The threshold of acceptable error (in pixels) when approximating
Chris Craik15a07a22014-01-26 13:43:53 -0800115 * @param outputVertices An empty Vector which will be populated with the output
116 */
John Reck1bcacfd2017-11-03 10:12:19 -0700117 static bool approximatePathOutlineVertices(const SkPath& path, float threshold,
118 std::vector<Vertex>& outputVertices);
Chris Craik15a07a22014-01-26 13:43:53 -0800119
Chris Craik65cd6122012-12-10 17:56:27 -0800120private:
John Reck1bcacfd2017-11-03 10:12:19 -0700121 static bool approximatePathOutlineVertices(const SkPath& path, bool forceClose,
122 const PathApproximationInfo& approximationInfo,
123 std::vector<Vertex>& outputVertices);
Chris Craik65cd6122012-12-10 17:56:27 -0800124
John Reck1bcacfd2017-11-03 10:12:19 -0700125 /*
126 endpoints a & b,
127 control c
128 */
129 static void recursiveQuadraticBezierVertices(float ax, float ay, float bx, float by, float cx,
130 float cy,
131 const PathApproximationInfo& approximationInfo,
132 std::vector<Vertex>& outputVertices,
133 int depth = 0);
Chris Craik65cd6122012-12-10 17:56:27 -0800134
John Reck1bcacfd2017-11-03 10:12:19 -0700135 /*
136 endpoints p1, p2
137 control c1, c2
138 */
139 static void recursiveCubicBezierVertices(float p1x, float p1y, float c1x, float c1y, float p2x,
140 float p2y, float c2x, float c2y,
141 const PathApproximationInfo& approximationInfo,
142 std::vector<Vertex>& outputVertices, int depth = 0);
Chris Craik65cd6122012-12-10 17:56:27 -0800143};
144
John Reck1bcacfd2017-11-03 10:12:19 -0700145}; // namespace uirenderer
146}; // namespace android
Chris Craik65cd6122012-12-10 17:56:27 -0800147
John Reck1bcacfd2017-11-03 10:12:19 -0700148#endif // ANDROID_HWUI_PATH_TESSELLATOR_H