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 | |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 17 | #define LOG_TAG "OpenGLRenderer" |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 18 | #define LOG_NDEBUG 1 |
Chris Craik | 87f9df8 | 2014-03-07 14:34:42 -0800 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_VIEW |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 20 | |
| 21 | #define VERTEX_DEBUG 0 |
| 22 | |
| 23 | #if VERTEX_DEBUG |
| 24 | #define DEBUG_DUMP_ALPHA_BUFFER() \ |
| 25 | for (unsigned int i = 0; i < vertexBuffer.getSize(); i++) { \ |
| 26 | ALOGD("point %d at %f %f, alpha %f", \ |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 27 | i, buffer[i].x, buffer[i].y, buffer[i].alpha); \ |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 28 | } |
| 29 | #define DEBUG_DUMP_BUFFER() \ |
| 30 | for (unsigned int i = 0; i < vertexBuffer.getSize(); i++) { \ |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 31 | ALOGD("point %d at %f %f", i, buffer[i].x, buffer[i].y); \ |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 32 | } |
| 33 | #else |
| 34 | #define DEBUG_DUMP_ALPHA_BUFFER() |
| 35 | #define DEBUG_DUMP_BUFFER() |
| 36 | #endif |
| 37 | |
| 38 | #include <SkPath.h> |
| 39 | #include <SkPaint.h> |
| 40 | |
| 41 | #include <stdlib.h> |
| 42 | #include <stdint.h> |
| 43 | #include <sys/types.h> |
| 44 | |
| 45 | #include <utils/Log.h> |
| 46 | #include <utils/Trace.h> |
| 47 | |
| 48 | #include "PathTessellator.h" |
| 49 | #include "Matrix.h" |
| 50 | #include "Vector.h" |
| 51 | #include "Vertex.h" |
Chris Craik | 74cf7e6 | 2014-08-07 14:34:46 -0700 | [diff] [blame] | 52 | #include "utils/MathUtils.h" |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 53 | |
| 54 | namespace android { |
| 55 | namespace uirenderer { |
| 56 | |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 57 | #define OUTLINE_REFINE_THRESHOLD_SQUARED (0.5f * 0.5f) |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 58 | #define ROUND_CAP_THRESH 0.25f |
| 59 | #define PI 3.1415926535897932f |
Chris Craik | 74cf7e6 | 2014-08-07 14:34:46 -0700 | [diff] [blame] | 60 | #define MAX_DEPTH 15 |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 61 | |
Chris Craik | 74cf7e6 | 2014-08-07 14:34:46 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Extracts the x and y scale from the transform as positive values, and clamps them |
| 64 | */ |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 65 | void PathTessellator::extractTessellationScales(const Matrix4& transform, |
| 66 | float* scaleX, float* scaleY) { |
Chris Craik | fe02b4b | 2014-06-16 16:34:29 -0700 | [diff] [blame] | 67 | if (CC_LIKELY(transform.isPureTranslate())) { |
| 68 | *scaleX = 1.0f; |
| 69 | *scaleY = 1.0f; |
| 70 | } else { |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 71 | float m00 = transform.data[Matrix4::kScaleX]; |
| 72 | float m01 = transform.data[Matrix4::kSkewY]; |
| 73 | float m10 = transform.data[Matrix4::kSkewX]; |
| 74 | float m11 = transform.data[Matrix4::kScaleY]; |
Chris Craik | 74cf7e6 | 2014-08-07 14:34:46 -0700 | [diff] [blame] | 75 | *scaleX = MathUtils::clampTessellationScale(sqrt(m00 * m00 + m01 * m01)); |
| 76 | *scaleY = MathUtils::clampTessellationScale(sqrt(m10 * m10 + m11 * m11)); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 80 | /** |
| 81 | * Produces a pseudo-normal for a vertex, given the normals of the two incoming lines. If the offset |
| 82 | * from each vertex in a perimeter is calculated, the resultant lines connecting the offset vertices |
| 83 | * will be offset by 1.0 |
| 84 | * |
| 85 | * Note that we can't add and normalize the two vectors, that would result in a rectangle having an |
| 86 | * offset of (sqrt(2)/2, sqrt(2)/2) at each corner, instead of (1, 1) |
| 87 | * |
| 88 | * NOTE: assumes angles between normals 90 degrees or less |
| 89 | */ |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 90 | inline static Vector2 totalOffsetFromNormals(const Vector2& normalA, const Vector2& normalB) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 91 | return (normalA + normalB) / (1 + fabs(normalA.dot(normalB))); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Structure used for storing useful information about the SkPaint and scale used for tessellating |
| 96 | */ |
| 97 | struct PaintInfo { |
| 98 | public: |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 99 | PaintInfo(const SkPaint* paint, const mat4& transform) : |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 100 | style(paint->getStyle()), cap(paint->getStrokeCap()), isAA(paint->isAntiAlias()), |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 101 | halfStrokeWidth(paint->getStrokeWidth() * 0.5f), maxAlpha(1.0f) { |
| 102 | // compute inverse scales |
Chris Craik | fe02b4b | 2014-06-16 16:34:29 -0700 | [diff] [blame] | 103 | if (CC_LIKELY(transform.isPureTranslate())) { |
| 104 | inverseScaleX = 1.0f; |
| 105 | inverseScaleY = 1.0f; |
| 106 | } else { |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 107 | float scaleX, scaleY; |
| 108 | PathTessellator::extractTessellationScales(transform, &scaleX, &scaleY); |
Chris Craik | 74cf7e6 | 2014-08-07 14:34:46 -0700 | [diff] [blame] | 109 | inverseScaleX = 1.0f / scaleX; |
| 110 | inverseScaleY = 1.0f / scaleY; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | if (isAA && halfStrokeWidth != 0 && inverseScaleX == inverseScaleY && |
Chris Craik | 19a390b | 2013-02-27 15:43:26 -0800 | [diff] [blame] | 114 | 2 * halfStrokeWidth < inverseScaleX) { |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 115 | // AA, with non-hairline stroke, width < 1 pixel. Scale alpha and treat as hairline. |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 116 | maxAlpha *= (2 * halfStrokeWidth) / inverseScaleX; |
| 117 | halfStrokeWidth = 0.0f; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | SkPaint::Style style; |
| 122 | SkPaint::Cap cap; |
| 123 | bool isAA; |
| 124 | float inverseScaleX; |
| 125 | float inverseScaleY; |
| 126 | float halfStrokeWidth; |
| 127 | float maxAlpha; |
| 128 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 129 | inline void scaleOffsetForStrokeWidth(Vector2& offset) const { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 130 | if (halfStrokeWidth == 0.0f) { |
| 131 | // hairline - compensate for scale |
| 132 | offset.x *= 0.5f * inverseScaleX; |
| 133 | offset.y *= 0.5f * inverseScaleY; |
| 134 | } else { |
| 135 | offset *= halfStrokeWidth; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * NOTE: the input will not always be a normal, especially for sharp edges - it should be the |
| 141 | * result of totalOffsetFromNormals (see documentation there) |
| 142 | */ |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 143 | inline Vector2 deriveAAOffset(const Vector2& offset) const { |
| 144 | return (Vector2){offset.x * 0.5f * inverseScaleX, offset.y * 0.5f * inverseScaleY}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns the number of cap divisions beyond the minimum 2 (kButt_Cap/kSquareCap will return 0) |
| 149 | * Should only be used when stroking and drawing caps |
| 150 | */ |
| 151 | inline int capExtraDivisions() const { |
| 152 | if (cap == SkPaint::kRound_Cap) { |
| 153 | if (halfStrokeWidth == 0.0f) return 2; |
| 154 | |
| 155 | // ROUND_CAP_THRESH is the maximum error for polygonal approximation of the round cap |
| 156 | const float errConst = (-ROUND_CAP_THRESH / halfStrokeWidth + 1); |
| 157 | const float targetCosVal = 2 * errConst * errConst - 1; |
| 158 | int neededDivisions = (int)(ceilf(PI / acos(targetCosVal)/2)) * 2; |
| 159 | return neededDivisions; |
| 160 | } |
| 161 | return 0; |
| 162 | } |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * Outset the bounds of point data (for line endpoints or points) to account for AA stroke |
| 166 | * geometry. |
| 167 | */ |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 168 | void expandBoundsForStroke(Rect* bounds) const { |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 169 | float outset = halfStrokeWidth; |
| 170 | if (outset == 0) outset = 0.5f; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 171 | bounds->outset(outset * inverseScaleX + Vertex::GeometryFudgeFactor(), |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 172 | outset * inverseScaleY + Vertex::GeometryFudgeFactor()); |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 173 | } |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | void getFillVerticesFromPerimeter(const Vector<Vertex>& perimeter, VertexBuffer& vertexBuffer) { |
| 177 | Vertex* buffer = vertexBuffer.alloc<Vertex>(perimeter.size()); |
| 178 | |
| 179 | int currentIndex = 0; |
| 180 | // zig zag between all previous points on the inside of the hull to create a |
| 181 | // triangle strip that fills the hull |
| 182 | int srcAindex = 0; |
| 183 | int srcBindex = perimeter.size() - 1; |
| 184 | while (srcAindex <= srcBindex) { |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 185 | buffer[currentIndex++] = perimeter[srcAindex]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 186 | if (srcAindex == srcBindex) break; |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 187 | buffer[currentIndex++] = perimeter[srcBindex]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 188 | srcAindex++; |
| 189 | srcBindex--; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Fills a vertexBuffer with non-alpha vertices, zig-zagging at each perimeter point to create a |
| 195 | * tri-strip as wide as the stroke. |
| 196 | * |
| 197 | * Uses an additional 2 vertices at the end to wrap around, closing the tri-strip |
| 198 | * (for a total of perimeter.size() * 2 + 2 vertices) |
| 199 | */ |
| 200 | void getStrokeVerticesFromPerimeter(const PaintInfo& paintInfo, const Vector<Vertex>& perimeter, |
| 201 | VertexBuffer& vertexBuffer) { |
| 202 | Vertex* buffer = vertexBuffer.alloc<Vertex>(perimeter.size() * 2 + 2); |
| 203 | |
| 204 | int currentIndex = 0; |
| 205 | const Vertex* last = &(perimeter[perimeter.size() - 1]); |
| 206 | const Vertex* current = &(perimeter[0]); |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 207 | Vector2 lastNormal = {current->y - last->y, last->x - current->x}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 208 | lastNormal.normalize(); |
| 209 | for (unsigned int i = 0; i < perimeter.size(); i++) { |
| 210 | const Vertex* next = &(perimeter[i + 1 >= perimeter.size() ? 0 : i + 1]); |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 211 | Vector2 nextNormal = {next->y - current->y, current->x - next->x}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 212 | nextNormal.normalize(); |
| 213 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 214 | Vector2 totalOffset = totalOffsetFromNormals(lastNormal, nextNormal); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 215 | paintInfo.scaleOffsetForStrokeWidth(totalOffset); |
| 216 | |
| 217 | Vertex::set(&buffer[currentIndex++], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 218 | current->x + totalOffset.x, |
| 219 | current->y + totalOffset.y); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 220 | |
| 221 | Vertex::set(&buffer[currentIndex++], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 222 | current->x - totalOffset.x, |
| 223 | current->y - totalOffset.y); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 224 | |
| 225 | last = current; |
| 226 | current = next; |
| 227 | lastNormal = nextNormal; |
| 228 | } |
| 229 | |
| 230 | // wrap around to beginning |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 231 | buffer[currentIndex++] = buffer[0]; |
| 232 | buffer[currentIndex++] = buffer[1]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 233 | |
| 234 | DEBUG_DUMP_BUFFER(); |
| 235 | } |
| 236 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 237 | static inline void storeBeginEnd(const PaintInfo& paintInfo, const Vertex& center, |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 238 | const Vector2& normal, Vertex* buffer, int& currentIndex, bool begin) { |
| 239 | Vector2 strokeOffset = normal; |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 240 | paintInfo.scaleOffsetForStrokeWidth(strokeOffset); |
| 241 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 242 | Vector2 referencePoint = {center.x, center.y}; |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 243 | if (paintInfo.cap == SkPaint::kSquare_Cap) { |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 244 | Vector2 rotated = {-strokeOffset.y, strokeOffset.x}; |
| 245 | referencePoint += rotated * (begin ? -1 : 1); |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | Vertex::set(&buffer[currentIndex++], referencePoint + strokeOffset); |
| 249 | Vertex::set(&buffer[currentIndex++], referencePoint - strokeOffset); |
| 250 | } |
| 251 | |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 252 | /** |
| 253 | * Fills a vertexBuffer with non-alpha vertices similar to getStrokeVerticesFromPerimeter, except: |
| 254 | * |
| 255 | * 1 - Doesn't need to wrap around, since the input vertices are unclosed |
| 256 | * |
| 257 | * 2 - can zig-zag across 'extra' vertices at either end, to create round caps |
| 258 | */ |
| 259 | void getStrokeVerticesFromUnclosedVertices(const PaintInfo& paintInfo, |
| 260 | const Vector<Vertex>& vertices, VertexBuffer& vertexBuffer) { |
| 261 | const int extra = paintInfo.capExtraDivisions(); |
| 262 | const int allocSize = (vertices.size() + extra) * 2; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 263 | Vertex* buffer = vertexBuffer.alloc<Vertex>(allocSize); |
| 264 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 265 | const int lastIndex = vertices.size() - 1; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 266 | if (extra > 0) { |
| 267 | // tessellate both round caps |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 268 | float beginTheta = atan2( |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 269 | - (vertices[0].x - vertices[1].x), |
| 270 | vertices[0].y - vertices[1].y); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 271 | float endTheta = atan2( |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 272 | - (vertices[lastIndex].x - vertices[lastIndex - 1].x), |
| 273 | vertices[lastIndex].y - vertices[lastIndex - 1].y); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 274 | const float dTheta = PI / (extra + 1); |
| 275 | const float radialScale = 2.0f / (1 + cos(dTheta)); |
| 276 | |
| 277 | int capOffset; |
| 278 | for (int i = 0; i < extra; i++) { |
| 279 | if (i < extra / 2) { |
| 280 | capOffset = extra - 2 * i - 1; |
| 281 | } else { |
| 282 | capOffset = 2 * i - extra; |
| 283 | } |
| 284 | |
| 285 | beginTheta += dTheta; |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 286 | Vector2 beginRadialOffset = {cos(beginTheta), sin(beginTheta)}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 287 | paintInfo.scaleOffsetForStrokeWidth(beginRadialOffset); |
| 288 | Vertex::set(&buffer[capOffset], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 289 | vertices[0].x + beginRadialOffset.x, |
| 290 | vertices[0].y + beginRadialOffset.y); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 291 | |
| 292 | endTheta += dTheta; |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 293 | Vector2 endRadialOffset = {cos(endTheta), sin(endTheta)}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 294 | paintInfo.scaleOffsetForStrokeWidth(endRadialOffset); |
| 295 | Vertex::set(&buffer[allocSize - 1 - capOffset], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 296 | vertices[lastIndex].x + endRadialOffset.x, |
| 297 | vertices[lastIndex].y + endRadialOffset.y); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | int currentIndex = extra; |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 302 | const Vertex* last = &(vertices[0]); |
| 303 | const Vertex* current = &(vertices[1]); |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 304 | Vector2 lastNormal = {current->y - last->y, last->x - current->x}; |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 305 | lastNormal.normalize(); |
| 306 | |
| 307 | storeBeginEnd(paintInfo, vertices[0], lastNormal, buffer, currentIndex, true); |
| 308 | |
| 309 | for (unsigned int i = 1; i < vertices.size() - 1; i++) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 310 | const Vertex* next = &(vertices[i + 1]); |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 311 | Vector2 nextNormal = {next->y - current->y, current->x - next->x}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 312 | nextNormal.normalize(); |
| 313 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 314 | Vector2 strokeOffset = totalOffsetFromNormals(lastNormal, nextNormal); |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 315 | paintInfo.scaleOffsetForStrokeWidth(strokeOffset); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 316 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 317 | Vector2 center = {current->x, current->y}; |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 318 | Vertex::set(&buffer[currentIndex++], center + strokeOffset); |
| 319 | Vertex::set(&buffer[currentIndex++], center - strokeOffset); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 320 | |
| 321 | current = next; |
| 322 | lastNormal = nextNormal; |
| 323 | } |
| 324 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 325 | storeBeginEnd(paintInfo, vertices[lastIndex], lastNormal, buffer, currentIndex, false); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 326 | |
| 327 | DEBUG_DUMP_BUFFER(); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Populates a vertexBuffer with AlphaVertices to create an anti-aliased fill shape tessellation |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 332 | * |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 333 | * 1 - create the AA perimeter of unit width, by zig-zagging at each point around the perimeter of |
| 334 | * the shape (using 2 * perimeter.size() vertices) |
| 335 | * |
| 336 | * 2 - wrap around to the beginning to complete the perimeter (2 vertices) |
| 337 | * |
| 338 | * 3 - zig zag back and forth inside the shape to fill it (using perimeter.size() vertices) |
| 339 | */ |
| 340 | void getFillVerticesFromPerimeterAA(const PaintInfo& paintInfo, const Vector<Vertex>& perimeter, |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 341 | VertexBuffer& vertexBuffer, float maxAlpha = 1.0f) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 342 | AlphaVertex* buffer = vertexBuffer.alloc<AlphaVertex>(perimeter.size() * 3 + 2); |
| 343 | |
| 344 | // generate alpha points - fill Alpha vertex gaps in between each point with |
| 345 | // alpha 0 vertex, offset by a scaled normal. |
| 346 | int currentIndex = 0; |
| 347 | const Vertex* last = &(perimeter[perimeter.size() - 1]); |
| 348 | const Vertex* current = &(perimeter[0]); |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 349 | Vector2 lastNormal = {current->y - last->y, last->x - current->x}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 350 | lastNormal.normalize(); |
| 351 | for (unsigned int i = 0; i < perimeter.size(); i++) { |
| 352 | const Vertex* next = &(perimeter[i + 1 >= perimeter.size() ? 0 : i + 1]); |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 353 | Vector2 nextNormal = {next->y - current->y, current->x - next->x}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 354 | nextNormal.normalize(); |
| 355 | |
| 356 | // AA point offset from original point is that point's normal, such that each side is offset |
| 357 | // by .5 pixels |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 358 | Vector2 totalOffset = paintInfo.deriveAAOffset(totalOffsetFromNormals(lastNormal, nextNormal)); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 359 | |
| 360 | AlphaVertex::set(&buffer[currentIndex++], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 361 | current->x + totalOffset.x, |
| 362 | current->y + totalOffset.y, |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 363 | 0.0f); |
| 364 | AlphaVertex::set(&buffer[currentIndex++], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 365 | current->x - totalOffset.x, |
| 366 | current->y - totalOffset.y, |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 367 | maxAlpha); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 368 | |
| 369 | last = current; |
| 370 | current = next; |
| 371 | lastNormal = nextNormal; |
| 372 | } |
| 373 | |
| 374 | // wrap around to beginning |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 375 | buffer[currentIndex++] = buffer[0]; |
| 376 | buffer[currentIndex++] = buffer[1]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 377 | |
| 378 | // zig zag between all previous points on the inside of the hull to create a |
| 379 | // triangle strip that fills the hull, repeating the first inner point to |
| 380 | // create degenerate tris to start inside path |
| 381 | int srcAindex = 0; |
| 382 | int srcBindex = perimeter.size() - 1; |
| 383 | while (srcAindex <= srcBindex) { |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 384 | buffer[currentIndex++] = buffer[srcAindex * 2 + 1]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 385 | if (srcAindex == srcBindex) break; |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 386 | buffer[currentIndex++] = buffer[srcBindex * 2 + 1]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 387 | srcAindex++; |
| 388 | srcBindex--; |
| 389 | } |
| 390 | |
| 391 | DEBUG_DUMP_BUFFER(); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Stores geometry for a single, AA-perimeter (potentially rounded) cap |
| 396 | * |
| 397 | * For explanation of constants and general methodoloyg, see comments for |
| 398 | * getStrokeVerticesFromUnclosedVerticesAA() below. |
| 399 | */ |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 400 | inline static void storeCapAA(const PaintInfo& paintInfo, const Vector<Vertex>& vertices, |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 401 | AlphaVertex* buffer, bool isFirst, Vector2 normal, int offset) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 402 | const int extra = paintInfo.capExtraDivisions(); |
| 403 | const int extraOffset = (extra + 1) / 2; |
| 404 | const int capIndex = isFirst |
| 405 | ? 2 * offset + 6 + 2 * (extra + extraOffset) |
| 406 | : offset + 2 + 2 * extraOffset; |
| 407 | if (isFirst) normal *= -1; |
| 408 | |
| 409 | // TODO: this normal should be scaled by radialScale if extra != 0, see totalOffsetFromNormals() |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 410 | Vector2 AAOffset = paintInfo.deriveAAOffset(normal); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 411 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 412 | Vector2 strokeOffset = normal; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 413 | paintInfo.scaleOffsetForStrokeWidth(strokeOffset); |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 414 | Vector2 outerOffset = strokeOffset + AAOffset; |
| 415 | Vector2 innerOffset = strokeOffset - AAOffset; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 416 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 417 | Vector2 capAAOffset = {0, 0}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 418 | if (paintInfo.cap != SkPaint::kRound_Cap) { |
| 419 | // if the cap is square or butt, the inside primary cap vertices will be inset in two |
| 420 | // directions - both normal to the stroke, and parallel to it. |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 421 | capAAOffset = (Vector2){-AAOffset.y, AAOffset.x}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // determine referencePoint, the center point for the 4 primary cap vertices |
| 425 | const Vertex* point = isFirst ? vertices.begin() : (vertices.end() - 1); |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 426 | Vector2 referencePoint = {point->x, point->y}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 427 | if (paintInfo.cap == SkPaint::kSquare_Cap) { |
| 428 | // To account for square cap, move the primary cap vertices (that create the AA edge) by the |
| 429 | // stroke offset vector (rotated to be parallel to the stroke) |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 430 | Vector2 rotated = {-strokeOffset.y, strokeOffset.x}; |
| 431 | referencePoint += rotated; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | AlphaVertex::set(&buffer[capIndex + 0], |
| 435 | referencePoint.x + outerOffset.x + capAAOffset.x, |
| 436 | referencePoint.y + outerOffset.y + capAAOffset.y, |
| 437 | 0.0f); |
| 438 | AlphaVertex::set(&buffer[capIndex + 1], |
| 439 | referencePoint.x + innerOffset.x - capAAOffset.x, |
| 440 | referencePoint.y + innerOffset.y - capAAOffset.y, |
| 441 | paintInfo.maxAlpha); |
| 442 | |
| 443 | bool isRound = paintInfo.cap == SkPaint::kRound_Cap; |
| 444 | |
| 445 | const int postCapIndex = (isRound && isFirst) ? (2 * extraOffset - 2) : capIndex + (2 * extra); |
| 446 | AlphaVertex::set(&buffer[postCapIndex + 2], |
| 447 | referencePoint.x - outerOffset.x + capAAOffset.x, |
| 448 | referencePoint.y - outerOffset.y + capAAOffset.y, |
| 449 | 0.0f); |
| 450 | AlphaVertex::set(&buffer[postCapIndex + 3], |
| 451 | referencePoint.x - innerOffset.x - capAAOffset.x, |
| 452 | referencePoint.y - innerOffset.y - capAAOffset.y, |
| 453 | paintInfo.maxAlpha); |
| 454 | |
| 455 | if (isRound) { |
| 456 | const float dTheta = PI / (extra + 1); |
| 457 | const float radialScale = 2.0f / (1 + cos(dTheta)); |
| 458 | float theta = atan2(normal.y, normal.x); |
| 459 | int capPerimIndex = capIndex + 2; |
| 460 | |
| 461 | for (int i = 0; i < extra; i++) { |
| 462 | theta += dTheta; |
| 463 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 464 | Vector2 radialOffset = {cos(theta), sin(theta)}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 465 | |
| 466 | // scale to compensate for pinching at sharp angles, see totalOffsetFromNormals() |
| 467 | radialOffset *= radialScale; |
| 468 | |
| 469 | AAOffset = paintInfo.deriveAAOffset(radialOffset); |
| 470 | paintInfo.scaleOffsetForStrokeWidth(radialOffset); |
| 471 | AlphaVertex::set(&buffer[capPerimIndex++], |
| 472 | referencePoint.x + radialOffset.x + AAOffset.x, |
| 473 | referencePoint.y + radialOffset.y + AAOffset.y, |
| 474 | 0.0f); |
| 475 | AlphaVertex::set(&buffer[capPerimIndex++], |
| 476 | referencePoint.x + radialOffset.x - AAOffset.x, |
| 477 | referencePoint.y + radialOffset.y - AAOffset.y, |
| 478 | paintInfo.maxAlpha); |
| 479 | |
| 480 | if (isFirst && i == extra - extraOffset) { |
| 481 | //copy most recent two points to first two points |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 482 | buffer[0] = buffer[capPerimIndex - 2]; |
| 483 | buffer[1] = buffer[capPerimIndex - 1]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 484 | |
| 485 | capPerimIndex = 2; // start writing the rest of the round cap at index 2 |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | if (isFirst) { |
| 490 | const int startCapFillIndex = capIndex + 2 * (extra - extraOffset) + 4; |
| 491 | int capFillIndex = startCapFillIndex; |
| 492 | for (int i = 0; i < extra + 2; i += 2) { |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 493 | buffer[capFillIndex++] = buffer[1 + i]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 494 | // TODO: to support odd numbers of divisions, break here on the last iteration |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 495 | buffer[capFillIndex++] = buffer[startCapFillIndex - 3 - i]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 496 | } |
| 497 | } else { |
| 498 | int capFillIndex = 6 * vertices.size() + 2 + 6 * extra - (extra + 2); |
| 499 | for (int i = 0; i < extra + 2; i += 2) { |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 500 | buffer[capFillIndex++] = buffer[capIndex + 1 + i]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 501 | // TODO: to support odd numbers of divisions, break here on the last iteration |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 502 | buffer[capFillIndex++] = buffer[capIndex + 3 + 2 * extra - i]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | return; |
| 506 | } |
| 507 | if (isFirst) { |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 508 | buffer[0] = buffer[postCapIndex + 2]; |
| 509 | buffer[1] = buffer[postCapIndex + 3]; |
| 510 | buffer[postCapIndex + 4] = buffer[1]; // degenerate tris (the only two!) |
| 511 | buffer[postCapIndex + 5] = buffer[postCapIndex + 1]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 512 | } else { |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 513 | buffer[6 * vertices.size()] = buffer[postCapIndex + 1]; |
| 514 | buffer[6 * vertices.size() + 1] = buffer[postCapIndex + 3]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | the geometry for an aa, capped stroke consists of the following: |
| 520 | |
| 521 | # vertices | function |
| 522 | ---------------------------------------------------------------------- |
| 523 | a) 2 | Start AA perimeter |
| 524 | b) 2, 2 * roundDivOff | First half of begin cap's perimeter |
| 525 | | |
| 526 | 2 * middlePts | 'Outer' or 'Top' AA perimeter half (between caps) |
| 527 | | |
| 528 | a) 4 | End cap's |
| 529 | b) 2, 2 * roundDivs, 2 | AA perimeter |
| 530 | | |
| 531 | 2 * middlePts | 'Inner' or 'bottom' AA perimeter half |
| 532 | | |
| 533 | a) 6 | Begin cap's perimeter |
| 534 | b) 2, 2*(rD - rDO + 1), | Last half of begin cap's perimeter |
| 535 | roundDivs, 2 | |
| 536 | | |
| 537 | 2 * middlePts | Stroke's full opacity center strip |
| 538 | | |
| 539 | a) 2 | end stroke |
| 540 | b) 2, roundDivs | (and end cap fill, for round) |
| 541 | |
| 542 | Notes: |
| 543 | * rows starting with 'a)' denote the Butt or Square cap vertex use, 'b)' denote Round |
| 544 | |
| 545 | * 'middlePts' is (number of points in the unclosed input vertex list, minus 2) times two |
| 546 | |
| 547 | * 'roundDivs' or 'rD' is the number of extra vertices (beyond the minimum of 2) that define the |
| 548 | round cap's shape, and is at least two. This will increase with cap size to sufficiently |
| 549 | define the cap's level of tessellation. |
| 550 | |
| 551 | * 'roundDivOffset' or 'rDO' is the point about halfway along the start cap's round perimeter, where |
| 552 | the stream of vertices for the AA perimeter starts. By starting and ending the perimeter at |
| 553 | this offset, the fill of the stroke is drawn from this point with minimal extra vertices. |
| 554 | |
| 555 | This means the outer perimeter starts at: |
| 556 | outerIndex = (2) OR (2 + 2 * roundDivOff) |
| 557 | the inner perimeter (since it is filled in reverse) starts at: |
| 558 | innerIndex = outerIndex + (4 * middlePts) + ((4) OR (4 + 2 * roundDivs)) - 1 |
| 559 | the stroke starts at: |
| 560 | strokeIndex = innerIndex + 1 + ((6) OR (6 + 3 * roundDivs - 2 * roundDivOffset)) |
| 561 | |
| 562 | The total needed allocated space is either: |
| 563 | 2 + 4 + 6 + 2 + 3 * (2 * middlePts) = 14 + 6 * middlePts = 2 + 6 * pts |
| 564 | or, for rounded caps: |
| 565 | (2 + 2 * rDO) + (4 + 2 * rD) + (2 * (rD - rDO + 1) |
| 566 | + roundDivs + 4) + (2 + roundDivs) + 3 * (2 * middlePts) |
| 567 | = 14 + 6 * middlePts + 6 * roundDivs |
| 568 | = 2 + 6 * pts + 6 * roundDivs |
| 569 | */ |
| 570 | void getStrokeVerticesFromUnclosedVerticesAA(const PaintInfo& paintInfo, |
| 571 | const Vector<Vertex>& vertices, VertexBuffer& vertexBuffer) { |
| 572 | |
| 573 | const int extra = paintInfo.capExtraDivisions(); |
| 574 | const int allocSize = 6 * vertices.size() + 2 + 6 * extra; |
| 575 | |
| 576 | AlphaVertex* buffer = vertexBuffer.alloc<AlphaVertex>(allocSize); |
| 577 | |
| 578 | const int extraOffset = (extra + 1) / 2; |
| 579 | int offset = 2 * (vertices.size() - 2); |
| 580 | // there is no outer/inner here, using them for consistency with below approach |
| 581 | int currentAAOuterIndex = 2 + 2 * extraOffset; |
| 582 | int currentAAInnerIndex = currentAAOuterIndex + (2 * offset) + 3 + (2 * extra); |
| 583 | int currentStrokeIndex = currentAAInnerIndex + 7 + (3 * extra - 2 * extraOffset); |
| 584 | |
| 585 | const Vertex* last = &(vertices[0]); |
| 586 | const Vertex* current = &(vertices[1]); |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 587 | Vector2 lastNormal = {current->y - last->y, last->x - current->x}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 588 | lastNormal.normalize(); |
| 589 | |
| 590 | // TODO: use normal from bezier traversal for cap, instead of from vertices |
| 591 | storeCapAA(paintInfo, vertices, buffer, true, lastNormal, offset); |
| 592 | |
| 593 | for (unsigned int i = 1; i < vertices.size() - 1; i++) { |
| 594 | const Vertex* next = &(vertices[i + 1]); |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 595 | Vector2 nextNormal = {next->y - current->y, current->x - next->x}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 596 | nextNormal.normalize(); |
| 597 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 598 | Vector2 totalOffset = totalOffsetFromNormals(lastNormal, nextNormal); |
| 599 | Vector2 AAOffset = paintInfo.deriveAAOffset(totalOffset); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 600 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 601 | Vector2 innerOffset = totalOffset; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 602 | paintInfo.scaleOffsetForStrokeWidth(innerOffset); |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 603 | Vector2 outerOffset = innerOffset + AAOffset; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 604 | innerOffset -= AAOffset; |
| 605 | |
| 606 | AlphaVertex::set(&buffer[currentAAOuterIndex++], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 607 | current->x + outerOffset.x, |
| 608 | current->y + outerOffset.y, |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 609 | 0.0f); |
| 610 | AlphaVertex::set(&buffer[currentAAOuterIndex++], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 611 | current->x + innerOffset.x, |
| 612 | current->y + innerOffset.y, |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 613 | paintInfo.maxAlpha); |
| 614 | |
| 615 | AlphaVertex::set(&buffer[currentStrokeIndex++], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 616 | current->x + innerOffset.x, |
| 617 | current->y + innerOffset.y, |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 618 | paintInfo.maxAlpha); |
| 619 | AlphaVertex::set(&buffer[currentStrokeIndex++], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 620 | current->x - innerOffset.x, |
| 621 | current->y - innerOffset.y, |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 622 | paintInfo.maxAlpha); |
| 623 | |
| 624 | AlphaVertex::set(&buffer[currentAAInnerIndex--], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 625 | current->x - innerOffset.x, |
| 626 | current->y - innerOffset.y, |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 627 | paintInfo.maxAlpha); |
| 628 | AlphaVertex::set(&buffer[currentAAInnerIndex--], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 629 | current->x - outerOffset.x, |
| 630 | current->y - outerOffset.y, |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 631 | 0.0f); |
| 632 | |
| 633 | current = next; |
| 634 | lastNormal = nextNormal; |
| 635 | } |
| 636 | |
| 637 | // TODO: use normal from bezier traversal for cap, instead of from vertices |
| 638 | storeCapAA(paintInfo, vertices, buffer, false, lastNormal, offset); |
| 639 | |
| 640 | DEBUG_DUMP_ALPHA_BUFFER(); |
| 641 | } |
| 642 | |
| 643 | |
| 644 | void getStrokeVerticesFromPerimeterAA(const PaintInfo& paintInfo, const Vector<Vertex>& perimeter, |
| 645 | VertexBuffer& vertexBuffer) { |
| 646 | AlphaVertex* buffer = vertexBuffer.alloc<AlphaVertex>(6 * perimeter.size() + 8); |
| 647 | |
| 648 | int offset = 2 * perimeter.size() + 3; |
| 649 | int currentAAOuterIndex = 0; |
| 650 | int currentStrokeIndex = offset; |
| 651 | int currentAAInnerIndex = offset * 2; |
| 652 | |
| 653 | const Vertex* last = &(perimeter[perimeter.size() - 1]); |
| 654 | const Vertex* current = &(perimeter[0]); |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 655 | Vector2 lastNormal = {current->y - last->y, last->x - current->x}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 656 | lastNormal.normalize(); |
| 657 | for (unsigned int i = 0; i < perimeter.size(); i++) { |
| 658 | const Vertex* next = &(perimeter[i + 1 >= perimeter.size() ? 0 : i + 1]); |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 659 | Vector2 nextNormal = {next->y - current->y, current->x - next->x}; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 660 | nextNormal.normalize(); |
| 661 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 662 | Vector2 totalOffset = totalOffsetFromNormals(lastNormal, nextNormal); |
| 663 | Vector2 AAOffset = paintInfo.deriveAAOffset(totalOffset); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 664 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 665 | Vector2 innerOffset = totalOffset; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 666 | paintInfo.scaleOffsetForStrokeWidth(innerOffset); |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 667 | Vector2 outerOffset = innerOffset + AAOffset; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 668 | innerOffset -= AAOffset; |
| 669 | |
| 670 | AlphaVertex::set(&buffer[currentAAOuterIndex++], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 671 | current->x + outerOffset.x, |
| 672 | current->y + outerOffset.y, |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 673 | 0.0f); |
| 674 | AlphaVertex::set(&buffer[currentAAOuterIndex++], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 675 | current->x + innerOffset.x, |
| 676 | current->y + innerOffset.y, |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 677 | paintInfo.maxAlpha); |
| 678 | |
| 679 | AlphaVertex::set(&buffer[currentStrokeIndex++], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 680 | current->x + innerOffset.x, |
| 681 | current->y + innerOffset.y, |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 682 | paintInfo.maxAlpha); |
| 683 | AlphaVertex::set(&buffer[currentStrokeIndex++], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 684 | current->x - innerOffset.x, |
| 685 | current->y - innerOffset.y, |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 686 | paintInfo.maxAlpha); |
| 687 | |
| 688 | AlphaVertex::set(&buffer[currentAAInnerIndex++], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 689 | current->x - innerOffset.x, |
| 690 | current->y - innerOffset.y, |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 691 | paintInfo.maxAlpha); |
| 692 | AlphaVertex::set(&buffer[currentAAInnerIndex++], |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 693 | current->x - outerOffset.x, |
| 694 | current->y - outerOffset.y, |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 695 | 0.0f); |
| 696 | |
| 697 | last = current; |
| 698 | current = next; |
| 699 | lastNormal = nextNormal; |
| 700 | } |
| 701 | |
| 702 | // wrap each strip around to beginning, creating degenerate tris to bridge strips |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 703 | buffer[currentAAOuterIndex++] = buffer[0]; |
| 704 | buffer[currentAAOuterIndex++] = buffer[1]; |
| 705 | buffer[currentAAOuterIndex++] = buffer[1]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 706 | |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 707 | buffer[currentStrokeIndex++] = buffer[offset]; |
| 708 | buffer[currentStrokeIndex++] = buffer[offset + 1]; |
| 709 | buffer[currentStrokeIndex++] = buffer[offset + 1]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 710 | |
Chris Craik | 11a7567 | 2013-12-16 17:08:15 -0800 | [diff] [blame] | 711 | buffer[currentAAInnerIndex++] = buffer[2 * offset]; |
| 712 | buffer[currentAAInnerIndex++] = buffer[2 * offset + 1]; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 713 | // don't need to create last degenerate tri |
| 714 | |
| 715 | DEBUG_DUMP_ALPHA_BUFFER(); |
| 716 | } |
| 717 | |
| 718 | void PathTessellator::tessellatePath(const SkPath &path, const SkPaint* paint, |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 719 | const mat4& transform, VertexBuffer& vertexBuffer) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 720 | ATRACE_CALL(); |
| 721 | |
| 722 | const PaintInfo paintInfo(paint, transform); |
| 723 | |
| 724 | Vector<Vertex> tempVertices; |
| 725 | float threshInvScaleX = paintInfo.inverseScaleX; |
| 726 | float threshInvScaleY = paintInfo.inverseScaleY; |
| 727 | if (paintInfo.style == SkPaint::kStroke_Style) { |
| 728 | // alter the bezier recursion threshold values we calculate in order to compensate for |
| 729 | // expansion done after the path vertices are found |
| 730 | SkRect bounds = path.getBounds(); |
| 731 | if (!bounds.isEmpty()) { |
| 732 | threshInvScaleX *= bounds.width() / (bounds.width() + paint->getStrokeWidth()); |
| 733 | threshInvScaleY *= bounds.height() / (bounds.height() + paint->getStrokeWidth()); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | // force close if we're filling the path, since fill path expects closed perimeter. |
| 738 | bool forceClose = paintInfo.style != SkPaint::kStroke_Style; |
| 739 | bool wasClosed = approximatePathOutlineVertices(path, forceClose, |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 740 | threshInvScaleX * threshInvScaleX, threshInvScaleY * threshInvScaleY, |
| 741 | OUTLINE_REFINE_THRESHOLD_SQUARED, tempVertices); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 742 | |
| 743 | if (!tempVertices.size()) { |
| 744 | // path was empty, return without allocating vertex buffer |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | #if VERTEX_DEBUG |
| 749 | for (unsigned int i = 0; i < tempVertices.size(); i++) { |
| 750 | ALOGD("orig path: point at %f %f", |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 751 | tempVertices[i].x, tempVertices[i].y); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 752 | } |
| 753 | #endif |
| 754 | |
| 755 | if (paintInfo.style == SkPaint::kStroke_Style) { |
| 756 | if (!paintInfo.isAA) { |
| 757 | if (wasClosed) { |
| 758 | getStrokeVerticesFromPerimeter(paintInfo, tempVertices, vertexBuffer); |
| 759 | } else { |
| 760 | getStrokeVerticesFromUnclosedVertices(paintInfo, tempVertices, vertexBuffer); |
| 761 | } |
| 762 | |
| 763 | } else { |
| 764 | if (wasClosed) { |
| 765 | getStrokeVerticesFromPerimeterAA(paintInfo, tempVertices, vertexBuffer); |
| 766 | } else { |
| 767 | getStrokeVerticesFromUnclosedVerticesAA(paintInfo, tempVertices, vertexBuffer); |
| 768 | } |
| 769 | } |
| 770 | } else { |
| 771 | // For kStrokeAndFill style, the path should be adjusted externally. |
| 772 | // It will be treated as a fill here. |
| 773 | if (!paintInfo.isAA) { |
| 774 | getFillVerticesFromPerimeter(tempVertices, vertexBuffer); |
| 775 | } else { |
| 776 | getFillVerticesFromPerimeterAA(paintInfo, tempVertices, vertexBuffer); |
| 777 | } |
| 778 | } |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 779 | |
| 780 | Rect bounds(path.getBounds()); |
| 781 | paintInfo.expandBoundsForStroke(&bounds); |
| 782 | vertexBuffer.setBounds(bounds); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 785 | template <class TYPE> |
| 786 | static void instanceVertices(VertexBuffer& srcBuffer, VertexBuffer& dstBuffer, |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 787 | const float* points, int count, Rect& bounds) { |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 788 | bounds.set(points[0], points[1], points[0], points[1]); |
| 789 | |
| 790 | int numPoints = count / 2; |
| 791 | int verticesPerPoint = srcBuffer.getVertexCount(); |
| 792 | dstBuffer.alloc<TYPE>(numPoints * verticesPerPoint + (numPoints - 1) * 2); |
| 793 | |
| 794 | for (int i = 0; i < count; i += 2) { |
Chris Craik | c93e45c | 2014-07-16 10:15:56 -0700 | [diff] [blame] | 795 | bounds.expandToCoverVertex(points[i + 0], points[i + 1]); |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 796 | dstBuffer.copyInto<TYPE>(srcBuffer, points[i + 0], points[i + 1]); |
| 797 | } |
| 798 | dstBuffer.createDegenerateSeparators<TYPE>(verticesPerPoint); |
| 799 | } |
| 800 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 801 | void PathTessellator::tessellatePoints(const float* points, int count, const SkPaint* paint, |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 802 | const mat4& transform, VertexBuffer& vertexBuffer) { |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 803 | const PaintInfo paintInfo(paint, transform); |
| 804 | |
| 805 | // determine point shape |
| 806 | SkPath path; |
| 807 | float radius = paintInfo.halfStrokeWidth; |
| 808 | if (radius == 0.0f) radius = 0.25f; |
| 809 | |
| 810 | if (paintInfo.cap == SkPaint::kRound_Cap) { |
| 811 | path.addCircle(0, 0, radius); |
| 812 | } else { |
| 813 | path.addRect(-radius, -radius, radius, radius); |
| 814 | } |
| 815 | |
| 816 | // calculate outline |
| 817 | Vector<Vertex> outlineVertices; |
| 818 | approximatePathOutlineVertices(path, true, |
| 819 | paintInfo.inverseScaleX * paintInfo.inverseScaleX, |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 820 | paintInfo.inverseScaleY * paintInfo.inverseScaleY, |
| 821 | OUTLINE_REFINE_THRESHOLD_SQUARED, outlineVertices); |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 822 | |
| 823 | if (!outlineVertices.size()) return; |
| 824 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 825 | Rect bounds; |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 826 | // tessellate, then duplicate outline across points |
| 827 | int numPoints = count / 2; |
| 828 | VertexBuffer tempBuffer; |
| 829 | if (!paintInfo.isAA) { |
| 830 | getFillVerticesFromPerimeter(outlineVertices, tempBuffer); |
| 831 | instanceVertices<Vertex>(tempBuffer, vertexBuffer, points, count, bounds); |
| 832 | } else { |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 833 | // note: pass maxAlpha directly, since we want fill to be alpha modulated |
| 834 | getFillVerticesFromPerimeterAA(paintInfo, outlineVertices, tempBuffer, paintInfo.maxAlpha); |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 835 | instanceVertices<AlphaVertex>(tempBuffer, vertexBuffer, points, count, bounds); |
| 836 | } |
| 837 | |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 838 | // expand bounds from vertex coords to pixel data |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 839 | paintInfo.expandBoundsForStroke(&bounds); |
| 840 | vertexBuffer.setBounds(bounds); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 841 | } |
| 842 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 843 | void PathTessellator::tessellateLines(const float* points, int count, const SkPaint* paint, |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 844 | const mat4& transform, VertexBuffer& vertexBuffer) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 845 | ATRACE_CALL(); |
| 846 | const PaintInfo paintInfo(paint, transform); |
| 847 | |
| 848 | const int extra = paintInfo.capExtraDivisions(); |
| 849 | int numLines = count / 4; |
| 850 | int lineAllocSize; |
| 851 | // pre-allocate space for lines in the buffer, and degenerate tris in between |
| 852 | if (paintInfo.isAA) { |
| 853 | lineAllocSize = 6 * (2) + 2 + 6 * extra; |
| 854 | vertexBuffer.alloc<AlphaVertex>(numLines * lineAllocSize + (numLines - 1) * 2); |
| 855 | } else { |
| 856 | lineAllocSize = 2 * ((2) + extra); |
| 857 | vertexBuffer.alloc<Vertex>(numLines * lineAllocSize + (numLines - 1) * 2); |
| 858 | } |
| 859 | |
| 860 | Vector<Vertex> tempVertices; |
| 861 | tempVertices.push(); |
| 862 | tempVertices.push(); |
| 863 | Vertex* tempVerticesData = tempVertices.editArray(); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 864 | Rect bounds; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 865 | bounds.set(points[0], points[1], points[0], points[1]); |
| 866 | for (int i = 0; i < count; i += 4) { |
| 867 | Vertex::set(&(tempVerticesData[0]), points[i + 0], points[i + 1]); |
| 868 | Vertex::set(&(tempVerticesData[1]), points[i + 2], points[i + 3]); |
| 869 | |
| 870 | if (paintInfo.isAA) { |
| 871 | getStrokeVerticesFromUnclosedVerticesAA(paintInfo, tempVertices, vertexBuffer); |
| 872 | } else { |
| 873 | getStrokeVerticesFromUnclosedVertices(paintInfo, tempVertices, vertexBuffer); |
| 874 | } |
| 875 | |
| 876 | // calculate bounds |
Chris Craik | c93e45c | 2014-07-16 10:15:56 -0700 | [diff] [blame] | 877 | bounds.expandToCoverVertex(tempVerticesData[0].x, tempVerticesData[0].y); |
| 878 | bounds.expandToCoverVertex(tempVerticesData[1].x, tempVerticesData[1].y); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 879 | } |
| 880 | |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 881 | // since multiple objects tessellated into buffer, separate them with degen tris |
| 882 | if (paintInfo.isAA) { |
| 883 | vertexBuffer.createDegenerateSeparators<AlphaVertex>(lineAllocSize); |
| 884 | } else { |
| 885 | vertexBuffer.createDegenerateSeparators<Vertex>(lineAllocSize); |
| 886 | } |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 887 | |
| 888 | // expand bounds from vertex coords to pixel data |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 889 | paintInfo.expandBoundsForStroke(&bounds); |
| 890 | vertexBuffer.setBounds(bounds); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | /////////////////////////////////////////////////////////////////////////////// |
| 894 | // Simple path line approximation |
| 895 | /////////////////////////////////////////////////////////////////////////////// |
| 896 | |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 897 | bool PathTessellator::approximatePathOutlineVertices(const SkPath& path, float thresholdSquared, |
| 898 | Vector<Vertex>& outputVertices) { |
| 899 | return approximatePathOutlineVertices(path, true, 1.0f, 1.0f, thresholdSquared, outputVertices); |
| 900 | } |
| 901 | |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 902 | void pushToVector(Vector<Vertex>& vertices, float x, float y) { |
| 903 | // TODO: make this not yuck |
| 904 | vertices.push(); |
| 905 | Vertex* newVertex = &(vertices.editArray()[vertices.size() - 1]); |
| 906 | Vertex::set(newVertex, x, y); |
| 907 | } |
| 908 | |
| 909 | bool PathTessellator::approximatePathOutlineVertices(const SkPath& path, bool forceClose, |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 910 | float sqrInvScaleX, float sqrInvScaleY, float thresholdSquared, |
| 911 | Vector<Vertex>& outputVertices) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 912 | ATRACE_CALL(); |
| 913 | |
| 914 | // TODO: to support joins other than sharp miter, join vertices should be labelled in the |
| 915 | // perimeter, or resolved into more vertices. Reconsider forceClose-ing in that case. |
| 916 | SkPath::Iter iter(path, forceClose); |
| 917 | SkPoint pts[4]; |
| 918 | SkPath::Verb v; |
| 919 | while (SkPath::kDone_Verb != (v = iter.next(pts))) { |
| 920 | switch (v) { |
| 921 | case SkPath::kMove_Verb: |
| 922 | pushToVector(outputVertices, pts[0].x(), pts[0].y()); |
| 923 | ALOGV("Move to pos %f %f", pts[0].x(), pts[0].y()); |
| 924 | break; |
| 925 | case SkPath::kClose_Verb: |
| 926 | ALOGV("Close at pos %f %f", pts[0].x(), pts[0].y()); |
| 927 | break; |
| 928 | case SkPath::kLine_Verb: |
| 929 | ALOGV("kLine_Verb %f %f -> %f %f", pts[0].x(), pts[0].y(), pts[1].x(), pts[1].y()); |
| 930 | pushToVector(outputVertices, pts[1].x(), pts[1].y()); |
| 931 | break; |
| 932 | case SkPath::kQuad_Verb: |
| 933 | ALOGV("kQuad_Verb"); |
| 934 | recursiveQuadraticBezierVertices( |
| 935 | pts[0].x(), pts[0].y(), |
| 936 | pts[2].x(), pts[2].y(), |
| 937 | pts[1].x(), pts[1].y(), |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 938 | sqrInvScaleX, sqrInvScaleY, thresholdSquared, outputVertices); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 939 | break; |
| 940 | case SkPath::kCubic_Verb: |
| 941 | ALOGV("kCubic_Verb"); |
| 942 | recursiveCubicBezierVertices( |
| 943 | pts[0].x(), pts[0].y(), |
| 944 | pts[1].x(), pts[1].y(), |
| 945 | pts[3].x(), pts[3].y(), |
| 946 | pts[2].x(), pts[2].y(), |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 947 | sqrInvScaleX, sqrInvScaleY, thresholdSquared, outputVertices); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 948 | break; |
| 949 | default: |
| 950 | break; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | int size = outputVertices.size(); |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 955 | if (size >= 2 && outputVertices[0].x == outputVertices[size - 1].x && |
| 956 | outputVertices[0].y == outputVertices[size - 1].y) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 957 | outputVertices.pop(); |
| 958 | return true; |
| 959 | } |
| 960 | return false; |
| 961 | } |
| 962 | |
| 963 | /////////////////////////////////////////////////////////////////////////////// |
| 964 | // Bezier approximation |
| 965 | /////////////////////////////////////////////////////////////////////////////// |
| 966 | |
| 967 | void PathTessellator::recursiveCubicBezierVertices( |
| 968 | float p1x, float p1y, float c1x, float c1y, |
| 969 | float p2x, float p2y, float c2x, float c2y, |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 970 | float sqrInvScaleX, float sqrInvScaleY, float thresholdSquared, |
Chris Craik | 9c3dd62 | 2014-06-11 17:24:51 -0700 | [diff] [blame] | 971 | Vector<Vertex>& outputVertices, int depth) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 972 | float dx = p2x - p1x; |
| 973 | float dy = p2y - p1y; |
| 974 | float d1 = fabs((c1x - p2x) * dy - (c1y - p2y) * dx); |
| 975 | float d2 = fabs((c2x - p2x) * dy - (c2y - p2y) * dx); |
| 976 | float d = d1 + d2; |
| 977 | |
| 978 | // multiplying by sqrInvScaleY/X equivalent to multiplying in dimensional scale factors |
Chris Craik | 74cf7e6 | 2014-08-07 14:34:46 -0700 | [diff] [blame] | 979 | if (depth >= MAX_DEPTH |
| 980 | || d * d <= thresholdSquared * (dx * dx * sqrInvScaleY + dy * dy * sqrInvScaleX)) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 981 | // below thresh, draw line by adding endpoint |
| 982 | pushToVector(outputVertices, p2x, p2y); |
| 983 | } else { |
| 984 | float p1c1x = (p1x + c1x) * 0.5f; |
| 985 | float p1c1y = (p1y + c1y) * 0.5f; |
| 986 | float p2c2x = (p2x + c2x) * 0.5f; |
| 987 | float p2c2y = (p2y + c2y) * 0.5f; |
| 988 | |
| 989 | float c1c2x = (c1x + c2x) * 0.5f; |
| 990 | float c1c2y = (c1y + c2y) * 0.5f; |
| 991 | |
| 992 | float p1c1c2x = (p1c1x + c1c2x) * 0.5f; |
| 993 | float p1c1c2y = (p1c1y + c1c2y) * 0.5f; |
| 994 | |
| 995 | float p2c1c2x = (p2c2x + c1c2x) * 0.5f; |
| 996 | float p2c1c2y = (p2c2y + c1c2y) * 0.5f; |
| 997 | |
| 998 | float mx = (p1c1c2x + p2c1c2x) * 0.5f; |
| 999 | float my = (p1c1c2y + p2c1c2y) * 0.5f; |
| 1000 | |
| 1001 | recursiveCubicBezierVertices( |
| 1002 | p1x, p1y, p1c1x, p1c1y, |
| 1003 | mx, my, p1c1c2x, p1c1c2y, |
Chris Craik | 9c3dd62 | 2014-06-11 17:24:51 -0700 | [diff] [blame] | 1004 | sqrInvScaleX, sqrInvScaleY, thresholdSquared, outputVertices, depth + 1); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 1005 | recursiveCubicBezierVertices( |
| 1006 | mx, my, p2c1c2x, p2c1c2y, |
| 1007 | p2x, p2y, p2c2x, p2c2y, |
Chris Craik | 9c3dd62 | 2014-06-11 17:24:51 -0700 | [diff] [blame] | 1008 | sqrInvScaleX, sqrInvScaleY, thresholdSquared, outputVertices, depth + 1); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | void PathTessellator::recursiveQuadraticBezierVertices( |
| 1013 | float ax, float ay, |
| 1014 | float bx, float by, |
| 1015 | float cx, float cy, |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 1016 | float sqrInvScaleX, float sqrInvScaleY, float thresholdSquared, |
Chris Craik | 9c3dd62 | 2014-06-11 17:24:51 -0700 | [diff] [blame] | 1017 | Vector<Vertex>& outputVertices, int depth) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 1018 | float dx = bx - ax; |
| 1019 | float dy = by - ay; |
| 1020 | float d = (cx - bx) * dy - (cy - by) * dx; |
| 1021 | |
Chris Craik | 74cf7e6 | 2014-08-07 14:34:46 -0700 | [diff] [blame] | 1022 | // multiplying by sqrInvScaleY/X equivalent to multiplying in dimensional scale factors |
| 1023 | if (depth >= MAX_DEPTH |
| 1024 | || d * d <= thresholdSquared * (dx * dx * sqrInvScaleY + dy * dy * sqrInvScaleX)) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 1025 | // below thresh, draw line by adding endpoint |
| 1026 | pushToVector(outputVertices, bx, by); |
| 1027 | } else { |
| 1028 | float acx = (ax + cx) * 0.5f; |
| 1029 | float bcx = (bx + cx) * 0.5f; |
| 1030 | float acy = (ay + cy) * 0.5f; |
| 1031 | float bcy = (by + cy) * 0.5f; |
| 1032 | |
| 1033 | // midpoint |
| 1034 | float mx = (acx + bcx) * 0.5f; |
| 1035 | float my = (acy + bcy) * 0.5f; |
| 1036 | |
| 1037 | recursiveQuadraticBezierVertices(ax, ay, mx, my, acx, acy, |
Chris Craik | 9c3dd62 | 2014-06-11 17:24:51 -0700 | [diff] [blame] | 1038 | sqrInvScaleX, sqrInvScaleY, thresholdSquared, outputVertices, depth + 1); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 1039 | recursiveQuadraticBezierVertices(mx, my, bx, by, bcx, bcy, |
Chris Craik | 9c3dd62 | 2014-06-11 17:24:51 -0700 | [diff] [blame] | 1040 | sqrInvScaleX, sqrInvScaleY, thresholdSquared, outputVertices, depth + 1); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | }; // namespace uirenderer |
| 1045 | }; // namespace android |