Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 1 | /* |
| 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" |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 25 | #include "VertexBuffer.h" |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 30 | class PathTessellator { |
| 31 | public: |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Populates scaleX and scaleY with the 'tessellation scale' of the transform - the effective X |
| 34 | * and Y scales that tessellation will take into account when generating the 1.0 pixel thick |
| 35 | * ramp. |
| 36 | * |
| 37 | * Two instances of the same shape (size, paint, etc.) will only generate the same vertices if |
| 38 | * their tessellation scales are equal. |
| 39 | */ |
| 40 | static void extractTessellationScales(const Matrix4& transform, float* scaleX, float* scaleY); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 41 | |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Populates a VertexBuffer with a tessellated approximation of the input convex path, as a single |
| 44 | * triangle strip. Note: joins are not currently supported. |
| 45 | * |
| 46 | * @param path The path to be approximated |
| 47 | * @param paint The paint the path will be drawn with, indicating AA, painting style |
| 48 | * (stroke vs fill), stroke width, stroke cap & join style, etc. |
| 49 | * @param transform The transform the path is to be drawn with, used to drive stretch-aware path |
| 50 | * vertex approximation, and correct AA ramp offsetting. |
| 51 | * @param vertexBuffer The output buffer |
| 52 | */ |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 53 | static void tessellatePath(const SkPath& path, const SkPaint* paint, |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 54 | const mat4& transform, VertexBuffer& vertexBuffer); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 55 | |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Populates a VertexBuffer with a tessellated approximation of points as a single triangle |
| 58 | * strip (with degenerate tris separating), respecting the shape defined by the paint cap. |
| 59 | * |
| 60 | * @param points The center vertices of the points to be drawn |
| 61 | * @param count The number of floats making up the point vertices |
| 62 | * @param paint The paint the points will be drawn with indicating AA, stroke width & cap |
| 63 | * @param transform The transform the points will be drawn with, used to drive stretch-aware path |
| 64 | * vertex approximation, and correct AA ramp offsetting |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 65 | * @param vertexBuffer The output buffer |
| 66 | */ |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 67 | static void tessellatePoints(const float* points, int count, const SkPaint* paint, |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 68 | const mat4& transform, VertexBuffer& vertexBuffer); |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 69 | |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 70 | /** |
| 71 | * Populates a VertexBuffer with a tessellated approximation of lines as a single triangle |
| 72 | * strip (with degenerate tris separating). |
| 73 | * |
| 74 | * @param points Pairs of endpoints defining the lines to be drawn |
| 75 | * @param count The number of floats making up the line vertices |
| 76 | * @param paint The paint the lines will be drawn with indicating AA, stroke width & cap |
| 77 | * @param transform The transform the points will be drawn with, used to drive stretch-aware path |
| 78 | * vertex approximation, and correct AA ramp offsetting |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 79 | * @param vertexBuffer The output buffer |
| 80 | */ |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 81 | static void tessellateLines(const float* points, int count, const SkPaint* paint, |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 82 | const mat4& transform, VertexBuffer& vertexBuffer); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 83 | |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 84 | /** |
| 85 | * Approximates a convex, CW outline into a Vector of 2d vertices. |
| 86 | * |
| 87 | * @param path The outline to be approximated |
| 88 | * @param thresholdSquared The threshold of acceptable error (in pixels) when approximating |
| 89 | * @param outputVertices An empty Vector which will be populated with the output |
| 90 | */ |
| 91 | static bool approximatePathOutlineVertices(const SkPath &path, float thresholdSquared, |
| 92 | Vector<Vertex> &outputVertices); |
| 93 | |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 94 | private: |
| 95 | static bool approximatePathOutlineVertices(const SkPath &path, bool forceClose, |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 96 | float sqrInvScaleX, float sqrInvScaleY, float thresholdSquared, |
| 97 | Vector<Vertex> &outputVertices); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | endpoints a & b, |
| 101 | control c |
| 102 | */ |
| 103 | static void recursiveQuadraticBezierVertices( |
| 104 | float ax, float ay, |
| 105 | float bx, float by, |
| 106 | float cx, float cy, |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 107 | float sqrInvScaleX, float sqrInvScaleY, float thresholdSquared, |
Chris Craik | 9c3dd62 | 2014-06-11 17:24:51 -0700 | [diff] [blame] | 108 | Vector<Vertex> &outputVertices, int depth = 0); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | endpoints p1, p2 |
| 112 | control c1, c2 |
| 113 | */ |
| 114 | static void recursiveCubicBezierVertices( |
| 115 | float p1x, float p1y, |
| 116 | float c1x, float c1y, |
| 117 | float p2x, float p2y, |
| 118 | float c2x, float c2y, |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 119 | float sqrInvScaleX, float sqrInvScaleY, float thresholdSquared, |
Chris Craik | 9c3dd62 | 2014-06-11 17:24:51 -0700 | [diff] [blame] | 120 | Vector<Vertex> &outputVertices, int depth = 0); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | }; // namespace uirenderer |
| 124 | }; // namespace android |
| 125 | |
| 126 | #endif // ANDROID_HWUI_PATH_TESSELLATOR_H |