jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #define LOG_TAG "PathRenderer" |
| 9 | #define LOG_NDEBUG 1 |
| 10 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 11 | |
| 12 | #define VERTEX_DEBUG 0 |
| 13 | |
| 14 | #include <SkPath.h> |
jvanverth@google.com | 74dda90 | 2013-01-09 21:04:52 +0000 | [diff] [blame] | 15 | #include <SkStrokeRec.h> |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <stdint.h> |
| 19 | #include <sys/types.h> |
| 20 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 21 | #include <SkTypes.h> |
| 22 | #include <SkTrace.h> |
| 23 | #include <SkMatrix.h> |
| 24 | #include <SkPoint.h> |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 25 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 26 | #ifdef VERBOSE |
| 27 | #define ALOGV SkDebugf |
| 28 | #else |
| 29 | #define ALOGV(x, ...) |
| 30 | #endif |
| 31 | |
| 32 | #include "AndroidPathRenderer.h" |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 33 | #include "Vertex.h" |
| 34 | |
| 35 | namespace android { |
| 36 | namespace uirenderer { |
| 37 | |
| 38 | #define THRESHOLD 0.5f |
| 39 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 40 | SkRect PathRenderer::ComputePathBounds(const SkPath& path, const SkPaint* paint) { |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 41 | SkRect bounds = path.getBounds(); |
| 42 | if (paint->getStyle() != SkPaint::kFill_Style) { |
| 43 | float outset = paint->getStrokeWidth() * 0.5f; |
| 44 | bounds.outset(outset, outset); |
| 45 | } |
| 46 | return bounds; |
| 47 | } |
| 48 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 49 | inline void computeInverseScales(const SkMatrix* transform, float &inverseScaleX, float& inverseScaleY) { |
jvanverth@google.com | 77734b8 | 2012-12-18 14:41:18 +0000 | [diff] [blame] | 50 | if (transform && transform->getType() & (SkMatrix::kScale_Mask|SkMatrix::kAffine_Mask|SkMatrix::kPerspective_Mask)) { |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 51 | float m00 = transform->getScaleX(); |
| 52 | float m01 = transform->getSkewY(); |
| 53 | float m10 = transform->getSkewX(); |
| 54 | float m11 = transform->getScaleY(); |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 55 | float scaleX = sk_float_sqrt(m00 * m00 + m01 * m01); |
| 56 | float scaleY = sk_float_sqrt(m10 * m10 + m11 * m11); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 57 | inverseScaleX = (scaleX != 0) ? (1.0f / scaleX) : 1.0f; |
| 58 | inverseScaleY = (scaleY != 0) ? (1.0f / scaleY) : 1.0f; |
| 59 | } else { |
| 60 | inverseScaleX = 1.0f; |
| 61 | inverseScaleY = 1.0f; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | inline void copyVertex(Vertex* destPtr, const Vertex* srcPtr) { |
| 66 | Vertex::set(destPtr, srcPtr->position[0], srcPtr->position[1]); |
| 67 | } |
| 68 | |
| 69 | inline void copyAlphaVertex(AlphaVertex* destPtr, const AlphaVertex* srcPtr) { |
| 70 | AlphaVertex::set(destPtr, srcPtr->position[0], srcPtr->position[1], srcPtr->alpha); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Produces a pseudo-normal for a vertex, given the normals of the two incoming lines. If the offset |
| 75 | * from each vertex in a perimeter is calculated, the resultant lines connecting the offset vertices |
| 76 | * will be offset by 1.0 |
| 77 | * |
| 78 | * Note that we can't add and normalize the two vectors, that would result in a rectangle having an |
| 79 | * offset of (sqrt(2)/2, sqrt(2)/2) at each corner, instead of (1, 1) |
| 80 | * |
| 81 | * NOTE: assumes angles between normals 90 degrees or less |
| 82 | */ |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 83 | inline SkVector totalOffsetFromNormals(const SkVector& normalA, const SkVector& normalB) { |
| 84 | SkVector pseudoNormal = normalA + normalB; |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 85 | pseudoNormal.scale(1.0f / (1.0f + sk_float_abs(normalA.dot(normalB)))); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 86 | return pseudoNormal; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 87 | } |
| 88 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 89 | inline void scaleOffsetForStrokeWidth(SkVector& offset, float halfStrokeWidth, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 90 | float inverseScaleX, float inverseScaleY) { |
| 91 | if (halfStrokeWidth == 0.0f) { |
| 92 | // hairline - compensate for scale |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 93 | offset.fX *= 0.5f * inverseScaleX; |
| 94 | offset.fY *= 0.5f * inverseScaleY; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 95 | } else { |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 96 | offset.scale(halfStrokeWidth); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 100 | static void getFillVerticesFromPerimeter(const SkTArray<Vertex, true>& perimeter, VertexBuffer* vertexBuffer) { |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 101 | Vertex* buffer = vertexBuffer->alloc<Vertex>(perimeter.count()); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 102 | |
| 103 | int currentIndex = 0; |
| 104 | // zig zag between all previous points on the inside of the hull to create a |
| 105 | // triangle strip that fills the hull |
| 106 | int srcAindex = 0; |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 107 | int srcBindex = perimeter.count() - 1; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 108 | while (srcAindex <= srcBindex) { |
| 109 | copyVertex(&buffer[currentIndex++], &perimeter[srcAindex]); |
| 110 | if (srcAindex == srcBindex) break; |
| 111 | copyVertex(&buffer[currentIndex++], &perimeter[srcBindex]); |
| 112 | srcAindex++; |
| 113 | srcBindex--; |
| 114 | } |
| 115 | } |
| 116 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 117 | static void getStrokeVerticesFromPerimeter(const SkTArray<Vertex, true>& perimeter, float halfStrokeWidth, |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 118 | VertexBuffer* vertexBuffer, float inverseScaleX, float inverseScaleY) { |
| 119 | Vertex* buffer = vertexBuffer->alloc<Vertex>(perimeter.count() * 2 + 2); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 120 | |
| 121 | int currentIndex = 0; |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 122 | const Vertex* last = &(perimeter[perimeter.count() - 1]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 123 | const Vertex* current = &(perimeter[0]); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 124 | SkVector lastNormal; |
| 125 | lastNormal.set(current->position[1] - last->position[1], |
| 126 | last->position[0] - current->position[0]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 127 | lastNormal.normalize(); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 128 | for (int i = 0; i < perimeter.count(); i++) { |
| 129 | const Vertex* next = &(perimeter[i + 1 >= perimeter.count() ? 0 : i + 1]); |
| 130 | SkVector nextNormal; |
| 131 | nextNormal.set(next->position[1] - current->position[1], |
| 132 | current->position[0] - next->position[0]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 133 | nextNormal.normalize(); |
| 134 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 135 | SkVector totalOffset = totalOffsetFromNormals(lastNormal, nextNormal); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 136 | scaleOffsetForStrokeWidth(totalOffset, halfStrokeWidth, inverseScaleX, inverseScaleY); |
| 137 | |
| 138 | Vertex::set(&buffer[currentIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 139 | current->position[0] + totalOffset.fX, |
| 140 | current->position[1] + totalOffset.fY); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 141 | |
| 142 | Vertex::set(&buffer[currentIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 143 | current->position[0] - totalOffset.fX, |
| 144 | current->position[1] - totalOffset.fY); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 145 | |
| 146 | last = current; |
| 147 | current = next; |
| 148 | lastNormal = nextNormal; |
| 149 | } |
| 150 | |
| 151 | // wrap around to beginning |
| 152 | copyVertex(&buffer[currentIndex++], &buffer[0]); |
| 153 | copyVertex(&buffer[currentIndex++], &buffer[1]); |
| 154 | } |
| 155 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 156 | static void getStrokeVerticesFromUnclosedVertices(const SkTArray<Vertex, true>& vertices, float halfStrokeWidth, |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 157 | VertexBuffer* vertexBuffer, float inverseScaleX, float inverseScaleY) { |
| 158 | Vertex* buffer = vertexBuffer->alloc<Vertex>(vertices.count() * 2); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 159 | |
| 160 | int currentIndex = 0; |
| 161 | const Vertex* current = &(vertices[0]); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 162 | SkVector lastNormal; |
| 163 | for (int i = 0; i < vertices.count() - 1; i++) { |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 164 | const Vertex* next = &(vertices[i + 1]); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 165 | SkVector nextNormal; |
| 166 | nextNormal.set(next->position[1] - current->position[1], |
| 167 | current->position[0] - next->position[0]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 168 | nextNormal.normalize(); |
| 169 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 170 | SkVector totalOffset; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 171 | if (i == 0) { |
| 172 | totalOffset = nextNormal; |
| 173 | } else { |
| 174 | totalOffset = totalOffsetFromNormals(lastNormal, nextNormal); |
| 175 | } |
| 176 | scaleOffsetForStrokeWidth(totalOffset, halfStrokeWidth, inverseScaleX, inverseScaleY); |
| 177 | |
| 178 | Vertex::set(&buffer[currentIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 179 | current->position[0] + totalOffset.fX, |
| 180 | current->position[1] + totalOffset.fY); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 181 | |
| 182 | Vertex::set(&buffer[currentIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 183 | current->position[0] - totalOffset.fX, |
| 184 | current->position[1] - totalOffset.fY); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 185 | |
| 186 | current = next; |
| 187 | lastNormal = nextNormal; |
| 188 | } |
| 189 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 190 | SkVector totalOffset = lastNormal; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 191 | scaleOffsetForStrokeWidth(totalOffset, halfStrokeWidth, inverseScaleX, inverseScaleY); |
| 192 | |
| 193 | Vertex::set(&buffer[currentIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 194 | current->position[0] + totalOffset.fX, |
| 195 | current->position[1] + totalOffset.fY); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 196 | Vertex::set(&buffer[currentIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 197 | current->position[0] - totalOffset.fX, |
| 198 | current->position[1] - totalOffset.fY); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 199 | #if VERTEX_DEBUG |
| 200 | for (unsigned int i = 0; i < vertexBuffer.getSize(); i++) { |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 201 | SkDebugf("point at %f %f", buffer[i].position[0], buffer[i].position[1]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 202 | } |
| 203 | #endif |
| 204 | } |
| 205 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 206 | static void getFillVerticesFromPerimeterAA(const SkTArray<Vertex, true>& perimeter, VertexBuffer* vertexBuffer, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 207 | float inverseScaleX, float inverseScaleY) { |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 208 | AlphaVertex* buffer = vertexBuffer->alloc<AlphaVertex>(perimeter.count() * 3 + 2); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 209 | |
| 210 | // generate alpha points - fill Alpha vertex gaps in between each point with |
| 211 | // alpha 0 vertex, offset by a scaled normal. |
| 212 | int currentIndex = 0; |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 213 | const Vertex* last = &(perimeter[perimeter.count() - 1]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 214 | const Vertex* current = &(perimeter[0]); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 215 | SkVector lastNormal; |
| 216 | lastNormal.set(current->position[1] - last->position[1], |
| 217 | last->position[0] - current->position[0]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 218 | lastNormal.normalize(); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 219 | for (int i = 0; i < perimeter.count(); i++) { |
| 220 | const Vertex* next = &(perimeter[i + 1 >= perimeter.count() ? 0 : i + 1]); |
| 221 | SkVector nextNormal; |
| 222 | nextNormal.set(next->position[1] - current->position[1], |
| 223 | current->position[0] - next->position[0]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 224 | nextNormal.normalize(); |
| 225 | |
| 226 | // AA point offset from original point is that point's normal, such that each side is offset |
| 227 | // by .5 pixels |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 228 | SkVector totalOffset = totalOffsetFromNormals(lastNormal, nextNormal); |
| 229 | totalOffset.fX *= 0.5f * inverseScaleX; |
| 230 | totalOffset.fY *= 0.5f * inverseScaleY; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 231 | |
| 232 | AlphaVertex::set(&buffer[currentIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 233 | current->position[0] + totalOffset.fX, |
| 234 | current->position[1] + totalOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 235 | 0.0f); |
| 236 | AlphaVertex::set(&buffer[currentIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 237 | current->position[0] - totalOffset.fX, |
| 238 | current->position[1] - totalOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 239 | 1.0f); |
| 240 | |
| 241 | last = current; |
| 242 | current = next; |
| 243 | lastNormal = nextNormal; |
| 244 | } |
| 245 | |
| 246 | // wrap around to beginning |
| 247 | copyAlphaVertex(&buffer[currentIndex++], &buffer[0]); |
| 248 | copyAlphaVertex(&buffer[currentIndex++], &buffer[1]); |
| 249 | |
| 250 | // zig zag between all previous points on the inside of the hull to create a |
| 251 | // triangle strip that fills the hull, repeating the first inner point to |
| 252 | // create degenerate tris to start inside path |
| 253 | int srcAindex = 0; |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 254 | int srcBindex = perimeter.count() - 1; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 255 | while (srcAindex <= srcBindex) { |
| 256 | copyAlphaVertex(&buffer[currentIndex++], &buffer[srcAindex * 2 + 1]); |
| 257 | if (srcAindex == srcBindex) break; |
| 258 | copyAlphaVertex(&buffer[currentIndex++], &buffer[srcBindex * 2 + 1]); |
| 259 | srcAindex++; |
| 260 | srcBindex--; |
| 261 | } |
| 262 | |
| 263 | #if VERTEX_DEBUG |
| 264 | for (unsigned int i = 0; i < vertexBuffer.getSize(); i++) { |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 265 | SkDebugf("point at %f %f, alpha %f", buffer[i].position[0], buffer[i].position[1], buffer[i].alpha); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 266 | } |
| 267 | #endif |
| 268 | } |
| 269 | |
| 270 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 271 | static void getStrokeVerticesFromUnclosedVerticesAA(const SkTArray<Vertex, true>& vertices, float halfStrokeWidth, |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 272 | VertexBuffer* vertexBuffer, float inverseScaleX, float inverseScaleY) { |
| 273 | AlphaVertex* buffer = vertexBuffer->alloc<AlphaVertex>(6 * vertices.count() + 2); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 274 | |
| 275 | // avoid lines smaller than hairline since they break triangle based sampling. instead reducing |
| 276 | // alpha value (TODO: support different X/Y scale) |
| 277 | float maxAlpha = 1.0f; |
| 278 | if (halfStrokeWidth != 0 && inverseScaleX == inverseScaleY && |
| 279 | halfStrokeWidth * inverseScaleX < 0.5f) { |
| 280 | maxAlpha *= (2 * halfStrokeWidth) / inverseScaleX; |
| 281 | halfStrokeWidth = 0.0f; |
| 282 | } |
| 283 | |
| 284 | // there is no outer/inner here, using them for consistency with below approach |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 285 | int offset = 2 * (vertices.count() - 2); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 286 | int currentAAOuterIndex = 2; |
| 287 | int currentAAInnerIndex = 2 * offset + 5; // reversed |
| 288 | int currentStrokeIndex = currentAAInnerIndex + 7; |
| 289 | |
| 290 | const Vertex* last = &(vertices[0]); |
| 291 | const Vertex* current = &(vertices[1]); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 292 | SkVector lastNormal; |
| 293 | lastNormal.set(current->position[1] - last->position[1], |
| 294 | last->position[0] - current->position[0]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 295 | lastNormal.normalize(); |
| 296 | |
| 297 | { |
| 298 | // start cap |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 299 | SkVector totalOffset = lastNormal; |
| 300 | SkVector AAOffset = totalOffset; |
| 301 | AAOffset.fX *= 0.5f * inverseScaleX; |
| 302 | AAOffset.fY *= 0.5f * inverseScaleY; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 303 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 304 | SkVector innerOffset = totalOffset; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 305 | scaleOffsetForStrokeWidth(innerOffset, halfStrokeWidth, inverseScaleX, inverseScaleY); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 306 | SkVector outerOffset = innerOffset + AAOffset; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 307 | innerOffset -= AAOffset; |
| 308 | |
| 309 | // TODO: support square cap by changing this offset to incorporate halfStrokeWidth |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 310 | SkVector capAAOffset; |
| 311 | capAAOffset.set(AAOffset.fY, -AAOffset.fX); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 312 | AlphaVertex::set(&buffer[0], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 313 | last->position[0] + outerOffset.fX + capAAOffset.fX, |
| 314 | last->position[1] + outerOffset.fY + capAAOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 315 | 0.0f); |
| 316 | AlphaVertex::set(&buffer[1], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 317 | last->position[0] + innerOffset.fX - capAAOffset.fX, |
| 318 | last->position[1] + innerOffset.fY - capAAOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 319 | maxAlpha); |
| 320 | |
| 321 | AlphaVertex::set(&buffer[2 * offset + 6], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 322 | last->position[0] - outerOffset.fX + capAAOffset.fX, |
| 323 | last->position[1] - outerOffset.fY + capAAOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 324 | 0.0f); |
| 325 | AlphaVertex::set(&buffer[2 * offset + 7], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 326 | last->position[0] - innerOffset.fX - capAAOffset.fX, |
| 327 | last->position[1] - innerOffset.fY - capAAOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 328 | maxAlpha); |
| 329 | copyAlphaVertex(&buffer[2 * offset + 8], &buffer[0]); |
| 330 | copyAlphaVertex(&buffer[2 * offset + 9], &buffer[1]); |
| 331 | copyAlphaVertex(&buffer[2 * offset + 10], &buffer[1]); // degenerate tris (the only two!) |
| 332 | copyAlphaVertex(&buffer[2 * offset + 11], &buffer[2 * offset + 7]); |
| 333 | } |
| 334 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 335 | for (int i = 1; i < vertices.count() - 1; i++) { |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 336 | const Vertex* next = &(vertices[i + 1]); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 337 | SkVector nextNormal; |
| 338 | nextNormal.set(next->position[1] - current->position[1], |
| 339 | current->position[0] - next->position[0]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 340 | nextNormal.normalize(); |
| 341 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 342 | SkVector totalOffset = totalOffsetFromNormals(lastNormal, nextNormal); |
| 343 | SkVector AAOffset = totalOffset; |
| 344 | AAOffset.fX *= 0.5f * inverseScaleX; |
| 345 | AAOffset.fY *= 0.5f * inverseScaleY; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 346 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 347 | SkVector innerOffset = totalOffset; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 348 | scaleOffsetForStrokeWidth(innerOffset, halfStrokeWidth, inverseScaleX, inverseScaleY); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 349 | SkVector outerOffset = innerOffset + AAOffset; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 350 | innerOffset -= AAOffset; |
| 351 | |
| 352 | AlphaVertex::set(&buffer[currentAAOuterIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 353 | current->position[0] + outerOffset.fX, |
| 354 | current->position[1] + outerOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 355 | 0.0f); |
| 356 | AlphaVertex::set(&buffer[currentAAOuterIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 357 | current->position[0] + innerOffset.fX, |
| 358 | current->position[1] + innerOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 359 | maxAlpha); |
| 360 | |
| 361 | AlphaVertex::set(&buffer[currentStrokeIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 362 | current->position[0] + innerOffset.fX, |
| 363 | current->position[1] + innerOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 364 | maxAlpha); |
| 365 | AlphaVertex::set(&buffer[currentStrokeIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 366 | current->position[0] - innerOffset.fX, |
| 367 | current->position[1] - innerOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 368 | maxAlpha); |
| 369 | |
| 370 | AlphaVertex::set(&buffer[currentAAInnerIndex--], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 371 | current->position[0] - innerOffset.fX, |
| 372 | current->position[1] - innerOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 373 | maxAlpha); |
| 374 | AlphaVertex::set(&buffer[currentAAInnerIndex--], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 375 | current->position[0] - outerOffset.fX, |
| 376 | current->position[1] - outerOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 377 | 0.0f); |
| 378 | |
| 379 | last = current; |
| 380 | current = next; |
| 381 | lastNormal = nextNormal; |
| 382 | } |
| 383 | |
| 384 | { |
| 385 | // end cap |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 386 | SkVector totalOffset = lastNormal; |
| 387 | SkVector AAOffset = totalOffset; |
| 388 | AAOffset.fX *= 0.5f * inverseScaleX; |
| 389 | AAOffset.fY *= 0.5f * inverseScaleY; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 390 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 391 | SkVector innerOffset = totalOffset; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 392 | scaleOffsetForStrokeWidth(innerOffset, halfStrokeWidth, inverseScaleX, inverseScaleY); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 393 | SkVector outerOffset = innerOffset + AAOffset; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 394 | innerOffset -= AAOffset; |
| 395 | |
| 396 | // TODO: support square cap by changing this offset to incorporate halfStrokeWidth |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 397 | SkVector capAAOffset; |
| 398 | capAAOffset.set(-AAOffset.fY, AAOffset.fX); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 399 | |
| 400 | AlphaVertex::set(&buffer[offset + 2], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 401 | current->position[0] + outerOffset.fX + capAAOffset.fX, |
| 402 | current->position[1] + outerOffset.fY + capAAOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 403 | 0.0f); |
| 404 | AlphaVertex::set(&buffer[offset + 3], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 405 | current->position[0] + innerOffset.fX - capAAOffset.fX, |
| 406 | current->position[1] + innerOffset.fY - capAAOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 407 | maxAlpha); |
| 408 | |
| 409 | AlphaVertex::set(&buffer[offset + 4], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 410 | current->position[0] - outerOffset.fX + capAAOffset.fX, |
| 411 | current->position[1] - outerOffset.fY + capAAOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 412 | 0.0f); |
| 413 | AlphaVertex::set(&buffer[offset + 5], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 414 | current->position[0] - innerOffset.fX - capAAOffset.fX, |
| 415 | current->position[1] - innerOffset.fY - capAAOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 416 | maxAlpha); |
| 417 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 418 | copyAlphaVertex(&buffer[vertexBuffer->getSize() - 2], &buffer[offset + 3]); |
| 419 | copyAlphaVertex(&buffer[vertexBuffer->getSize() - 1], &buffer[offset + 5]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | #if VERTEX_DEBUG |
| 423 | for (unsigned int i = 0; i < vertexBuffer.getSize(); i++) { |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 424 | SkDebugf("point at %f %f, alpha %f", buffer[i].position[0], buffer[i].position[1], buffer[i].alpha); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 425 | } |
| 426 | #endif |
| 427 | } |
| 428 | |
| 429 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 430 | static void getStrokeVerticesFromPerimeterAA(const SkTArray<Vertex, true>& perimeter, float halfStrokeWidth, |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 431 | VertexBuffer* vertexBuffer, float inverseScaleX, float inverseScaleY) { |
| 432 | AlphaVertex* buffer = vertexBuffer->alloc<AlphaVertex>(6 * perimeter.count() + 8); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 433 | |
| 434 | // avoid lines smaller than hairline since they break triangle based sampling. instead reducing |
| 435 | // alpha value (TODO: support different X/Y scale) |
| 436 | float maxAlpha = 1.0f; |
| 437 | if (halfStrokeWidth != 0 && inverseScaleX == inverseScaleY && |
| 438 | halfStrokeWidth * inverseScaleX < 0.5f) { |
| 439 | maxAlpha *= (2 * halfStrokeWidth) / inverseScaleX; |
| 440 | halfStrokeWidth = 0.0f; |
| 441 | } |
| 442 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 443 | int offset = 2 * perimeter.count() + 3; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 444 | int currentAAOuterIndex = 0; |
| 445 | int currentStrokeIndex = offset; |
| 446 | int currentAAInnerIndex = offset * 2; |
| 447 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 448 | const Vertex* last = &(perimeter[perimeter.count() - 1]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 449 | const Vertex* current = &(perimeter[0]); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 450 | SkVector lastNormal; |
| 451 | lastNormal.set(current->position[1] - last->position[1], |
| 452 | last->position[0] - current->position[0]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 453 | lastNormal.normalize(); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 454 | for (int i = 0; i < perimeter.count(); i++) { |
| 455 | const Vertex* next = &(perimeter[i + 1 >= perimeter.count() ? 0 : i + 1]); |
| 456 | SkVector nextNormal; |
| 457 | nextNormal.set(next->position[1] - current->position[1], |
| 458 | current->position[0] - next->position[0]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 459 | nextNormal.normalize(); |
| 460 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 461 | SkVector totalOffset = totalOffsetFromNormals(lastNormal, nextNormal); |
| 462 | SkVector AAOffset = totalOffset; |
| 463 | AAOffset.fX *= 0.5f * inverseScaleX; |
| 464 | AAOffset.fY *= 0.5f * inverseScaleY; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 465 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 466 | SkVector innerOffset = totalOffset; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 467 | scaleOffsetForStrokeWidth(innerOffset, halfStrokeWidth, inverseScaleX, inverseScaleY); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 468 | SkVector outerOffset = innerOffset + AAOffset; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 469 | innerOffset -= AAOffset; |
| 470 | |
| 471 | AlphaVertex::set(&buffer[currentAAOuterIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 472 | current->position[0] + outerOffset.fX, |
| 473 | current->position[1] + outerOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 474 | 0.0f); |
| 475 | AlphaVertex::set(&buffer[currentAAOuterIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 476 | current->position[0] + innerOffset.fX, |
| 477 | current->position[1] + innerOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 478 | maxAlpha); |
| 479 | |
| 480 | AlphaVertex::set(&buffer[currentStrokeIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 481 | current->position[0] + innerOffset.fX, |
| 482 | current->position[1] + innerOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 483 | maxAlpha); |
| 484 | AlphaVertex::set(&buffer[currentStrokeIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 485 | current->position[0] - innerOffset.fX, |
| 486 | current->position[1] - innerOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 487 | maxAlpha); |
| 488 | |
| 489 | AlphaVertex::set(&buffer[currentAAInnerIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 490 | current->position[0] - innerOffset.fX, |
| 491 | current->position[1] - innerOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 492 | maxAlpha); |
| 493 | AlphaVertex::set(&buffer[currentAAInnerIndex++], |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 494 | current->position[0] - outerOffset.fX, |
| 495 | current->position[1] - outerOffset.fY, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 496 | 0.0f); |
| 497 | |
| 498 | last = current; |
| 499 | current = next; |
| 500 | lastNormal = nextNormal; |
| 501 | } |
| 502 | |
| 503 | // wrap each strip around to beginning, creating degenerate tris to bridge strips |
| 504 | copyAlphaVertex(&buffer[currentAAOuterIndex++], &buffer[0]); |
| 505 | copyAlphaVertex(&buffer[currentAAOuterIndex++], &buffer[1]); |
| 506 | copyAlphaVertex(&buffer[currentAAOuterIndex++], &buffer[1]); |
| 507 | |
| 508 | copyAlphaVertex(&buffer[currentStrokeIndex++], &buffer[offset]); |
| 509 | copyAlphaVertex(&buffer[currentStrokeIndex++], &buffer[offset + 1]); |
| 510 | copyAlphaVertex(&buffer[currentStrokeIndex++], &buffer[offset + 1]); |
| 511 | |
| 512 | copyAlphaVertex(&buffer[currentAAInnerIndex++], &buffer[2 * offset]); |
| 513 | copyAlphaVertex(&buffer[currentAAInnerIndex++], &buffer[2 * offset + 1]); |
| 514 | // don't need to create last degenerate tri |
| 515 | |
| 516 | #if VERTEX_DEBUG |
| 517 | for (unsigned int i = 0; i < vertexBuffer.getSize(); i++) { |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 518 | SkDebugf("point at %f %f, alpha %f", buffer[i].position[0], buffer[i].position[1], buffer[i].alpha); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 519 | } |
| 520 | #endif |
| 521 | } |
| 522 | |
jvanverth@google.com | 74dda90 | 2013-01-09 21:04:52 +0000 | [diff] [blame] | 523 | void PathRenderer::ConvexPathVertices(const SkPath &path, const SkStrokeRec& stroke, bool isAA, |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 524 | const SkMatrix* transform, VertexBuffer* vertexBuffer) { |
| 525 | SK_TRACE_EVENT0("PathRenderer::convexPathVertices"); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 526 | |
jvanverth@google.com | 74dda90 | 2013-01-09 21:04:52 +0000 | [diff] [blame] | 527 | SkStrokeRec::Style style = stroke.getStyle(); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 528 | |
| 529 | float inverseScaleX, inverseScaleY; |
| 530 | computeInverseScales(transform, inverseScaleX, inverseScaleY); |
| 531 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 532 | SkTArray<Vertex, true> tempVertices; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 533 | float threshInvScaleX = inverseScaleX; |
| 534 | float threshInvScaleY = inverseScaleY; |
jvanverth@google.com | 74dda90 | 2013-01-09 21:04:52 +0000 | [diff] [blame] | 535 | if (style == SkStrokeRec::kStroke_Style) { |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 536 | // alter the bezier recursion threshold values we calculate in order to compensate for |
| 537 | // expansion done after the path vertices are found |
| 538 | SkRect bounds = path.getBounds(); |
| 539 | if (!bounds.isEmpty()) { |
jvanverth@google.com | 74dda90 | 2013-01-09 21:04:52 +0000 | [diff] [blame] | 540 | threshInvScaleX *= bounds.width() / (bounds.width() + stroke.getWidth()); |
| 541 | threshInvScaleY *= bounds.height() / (bounds.height() + stroke.getWidth()); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
| 545 | // force close if we're filling the path, since fill path expects closed perimeter. |
jvanverth@google.com | 74dda90 | 2013-01-09 21:04:52 +0000 | [diff] [blame] | 546 | bool forceClose = style != SkStrokeRec::kStroke_Style; |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 547 | bool wasClosed = ConvexPathPerimeterVertices(path, forceClose, threshInvScaleX * threshInvScaleX, |
| 548 | threshInvScaleY * threshInvScaleY, &tempVertices); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 549 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 550 | if (!tempVertices.count()) { |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 551 | // path was empty, return without allocating vertex buffer |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | #if VERTEX_DEBUG |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 556 | for (unsigned int i = 0; i < tempVertices.count(); i++) { |
| 557 | SkDebugf("orig path: point at %f %f", tempVertices[i].position[0], tempVertices[i].position[1]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 558 | } |
| 559 | #endif |
| 560 | |
jvanverth@google.com | 74dda90 | 2013-01-09 21:04:52 +0000 | [diff] [blame] | 561 | if (style == SkStrokeRec::kStroke_Style) { |
| 562 | float halfStrokeWidth = stroke.getWidth() * 0.5f; |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 563 | if (!isAA) { |
| 564 | if (wasClosed) { |
| 565 | getStrokeVerticesFromPerimeter(tempVertices, halfStrokeWidth, vertexBuffer, |
| 566 | inverseScaleX, inverseScaleY); |
| 567 | } else { |
| 568 | getStrokeVerticesFromUnclosedVertices(tempVertices, halfStrokeWidth, vertexBuffer, |
| 569 | inverseScaleX, inverseScaleY); |
| 570 | } |
| 571 | |
| 572 | } else { |
| 573 | if (wasClosed) { |
| 574 | getStrokeVerticesFromPerimeterAA(tempVertices, halfStrokeWidth, vertexBuffer, |
| 575 | inverseScaleX, inverseScaleY); |
| 576 | } else { |
| 577 | getStrokeVerticesFromUnclosedVerticesAA(tempVertices, halfStrokeWidth, vertexBuffer, |
| 578 | inverseScaleX, inverseScaleY); |
| 579 | } |
| 580 | } |
| 581 | } else { |
| 582 | // For kStrokeAndFill style, the path should be adjusted externally, as it will be treated as a fill here. |
| 583 | if (!isAA) { |
| 584 | getFillVerticesFromPerimeter(tempVertices, vertexBuffer); |
| 585 | } else { |
| 586 | getFillVerticesFromPerimeterAA(tempVertices, vertexBuffer, inverseScaleX, inverseScaleY); |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 592 | static void pushToVector(SkTArray<Vertex, true>* vertices, float x, float y) { |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 593 | // TODO: make this not yuck |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 594 | vertices->push_back(); |
| 595 | Vertex* newVertex = &((*vertices)[vertices->count() - 1]); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 596 | Vertex::set(newVertex, x, y); |
| 597 | } |
| 598 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 599 | bool PathRenderer::ConvexPathPerimeterVertices(const SkPath& path, bool forceClose, |
| 600 | float sqrInvScaleX, float sqrInvScaleY, SkTArray<Vertex, true>* outputVertices) { |
| 601 | SK_TRACE_EVENT0("PathRenderer::convexPathPerimeterVertices"); |
| 602 | |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 603 | |
| 604 | // TODO: to support joins other than sharp miter, join vertices should be labelled in the |
| 605 | // perimeter, or resolved into more vertices. Reconsider forceClose-ing in that case. |
| 606 | SkPath::Iter iter(path, forceClose); |
| 607 | SkPoint pts[4]; |
| 608 | SkPath::Verb v; |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 609 | |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 610 | while (SkPath::kDone_Verb != (v = iter.next(pts))) { |
| 611 | switch (v) { |
| 612 | case SkPath::kMove_Verb: |
| 613 | pushToVector(outputVertices, pts[0].x(), pts[0].y()); |
| 614 | ALOGV("Move to pos %f %f", pts[0].x(), pts[0].y()); |
| 615 | break; |
| 616 | case SkPath::kClose_Verb: |
| 617 | ALOGV("Close at pos %f %f", pts[0].x(), pts[0].y()); |
| 618 | break; |
| 619 | case SkPath::kLine_Verb: |
| 620 | ALOGV("kLine_Verb %f %f -> %f %f", |
| 621 | pts[0].x(), pts[0].y(), |
| 622 | pts[1].x(), pts[1].y()); |
| 623 | |
| 624 | pushToVector(outputVertices, pts[1].x(), pts[1].y()); |
| 625 | break; |
| 626 | case SkPath::kQuad_Verb: |
| 627 | ALOGV("kQuad_Verb"); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 628 | RecursiveQuadraticBezierVertices( |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 629 | pts[0].x(), pts[0].y(), |
| 630 | pts[2].x(), pts[2].y(), |
| 631 | pts[1].x(), pts[1].y(), |
| 632 | sqrInvScaleX, sqrInvScaleY, outputVertices); |
| 633 | break; |
| 634 | case SkPath::kCubic_Verb: |
| 635 | ALOGV("kCubic_Verb"); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 636 | RecursiveCubicBezierVertices( |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 637 | pts[0].x(), pts[0].y(), |
| 638 | pts[1].x(), pts[1].y(), |
| 639 | pts[3].x(), pts[3].y(), |
| 640 | pts[2].x(), pts[2].y(), |
| 641 | sqrInvScaleX, sqrInvScaleY, outputVertices); |
| 642 | break; |
| 643 | default: |
| 644 | break; |
| 645 | } |
| 646 | } |
| 647 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 648 | int size = outputVertices->count(); |
| 649 | if (size >= 2 && (*outputVertices)[0].position[0] == (*outputVertices)[size - 1].position[0] && |
| 650 | (*outputVertices)[0].position[1] == (*outputVertices)[size - 1].position[1]) { |
| 651 | outputVertices->pop_back(); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 652 | return true; |
| 653 | } |
| 654 | return false; |
| 655 | } |
| 656 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 657 | void PathRenderer::RecursiveCubicBezierVertices( |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 658 | float p1x, float p1y, float c1x, float c1y, |
| 659 | float p2x, float p2y, float c2x, float c2y, |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 660 | float sqrInvScaleX, float sqrInvScaleY, SkTArray<Vertex, true>* outputVertices) { |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 661 | float dx = p2x - p1x; |
| 662 | float dy = p2y - p1y; |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 663 | float d1 = sk_float_abs((c1x - p2x) * dy - (c1y - p2y) * dx); |
| 664 | float d2 = sk_float_abs((c2x - p2x) * dy - (c2y - p2y) * dx); |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 665 | float d = d1 + d2; |
| 666 | |
| 667 | // multiplying by sqrInvScaleY/X equivalent to multiplying in dimensional scale factors |
| 668 | |
| 669 | if (d * d < THRESHOLD * THRESHOLD * (dx * dx * sqrInvScaleY + dy * dy * sqrInvScaleX)) { |
| 670 | // below thresh, draw line by adding endpoint |
| 671 | pushToVector(outputVertices, p2x, p2y); |
| 672 | } else { |
| 673 | float p1c1x = (p1x + c1x) * 0.5f; |
| 674 | float p1c1y = (p1y + c1y) * 0.5f; |
| 675 | float p2c2x = (p2x + c2x) * 0.5f; |
| 676 | float p2c2y = (p2y + c2y) * 0.5f; |
| 677 | |
| 678 | float c1c2x = (c1x + c2x) * 0.5f; |
| 679 | float c1c2y = (c1y + c2y) * 0.5f; |
| 680 | |
| 681 | float p1c1c2x = (p1c1x + c1c2x) * 0.5f; |
| 682 | float p1c1c2y = (p1c1y + c1c2y) * 0.5f; |
| 683 | |
| 684 | float p2c1c2x = (p2c2x + c1c2x) * 0.5f; |
| 685 | float p2c1c2y = (p2c2y + c1c2y) * 0.5f; |
| 686 | |
| 687 | float mx = (p1c1c2x + p2c1c2x) * 0.5f; |
| 688 | float my = (p1c1c2y + p2c1c2y) * 0.5f; |
| 689 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 690 | RecursiveCubicBezierVertices( |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 691 | p1x, p1y, p1c1x, p1c1y, |
| 692 | mx, my, p1c1c2x, p1c1c2y, |
| 693 | sqrInvScaleX, sqrInvScaleY, outputVertices); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 694 | RecursiveCubicBezierVertices( |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 695 | mx, my, p2c1c2x, p2c1c2y, |
| 696 | p2x, p2y, p2c2x, p2c2y, |
| 697 | sqrInvScaleX, sqrInvScaleY, outputVertices); |
| 698 | } |
| 699 | } |
| 700 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 701 | void PathRenderer::RecursiveQuadraticBezierVertices( |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 702 | float ax, float ay, |
| 703 | float bx, float by, |
| 704 | float cx, float cy, |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 705 | float sqrInvScaleX, float sqrInvScaleY, SkTArray<Vertex, true>* outputVertices) { |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 706 | float dx = bx - ax; |
| 707 | float dy = by - ay; |
| 708 | float d = (cx - bx) * dy - (cy - by) * dx; |
| 709 | |
| 710 | if (d * d < THRESHOLD * THRESHOLD * (dx * dx * sqrInvScaleY + dy * dy * sqrInvScaleX)) { |
| 711 | // below thresh, draw line by adding endpoint |
| 712 | pushToVector(outputVertices, bx, by); |
| 713 | } else { |
| 714 | float acx = (ax + cx) * 0.5f; |
| 715 | float bcx = (bx + cx) * 0.5f; |
| 716 | float acy = (ay + cy) * 0.5f; |
| 717 | float bcy = (by + cy) * 0.5f; |
| 718 | |
| 719 | // midpoint |
| 720 | float mx = (acx + bcx) * 0.5f; |
| 721 | float my = (acy + bcy) * 0.5f; |
| 722 | |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 723 | RecursiveQuadraticBezierVertices(ax, ay, mx, my, acx, acy, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 724 | sqrInvScaleX, sqrInvScaleY, outputVertices); |
jvanverth@google.com | 24b4f97 | 2012-12-18 14:13:46 +0000 | [diff] [blame] | 725 | RecursiveQuadraticBezierVertices(mx, my, bx, by, bcx, bcy, |
jvanverth@google.com | 8ffb704 | 2012-12-13 19:53:18 +0000 | [diff] [blame] | 726 | sqrInvScaleX, sqrInvScaleY, outputVertices); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | }; // namespace uirenderer |
| 731 | }; // namespace android |