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