blob: 6893f9dfbb13a2fef0fd736e4cf4e4004b777f54 [file] [log] [blame]
Chris Craik710f46d2012-09-17 17:25:49 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "PathRenderer"
18#define LOG_NDEBUG 1
19#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
21#define VERTEX_DEBUG 0
22
23#include <SkPath.h>
Chris Craikcb4d6002012-09-25 12:00:29 -070024#include <SkPaint.h>
Chris Craik710f46d2012-09-17 17:25:49 -070025
26#include <stdlib.h>
27#include <stdint.h>
28#include <sys/types.h>
29
30#include <utils/Log.h>
31#include <utils/Trace.h>
32
33#include "PathRenderer.h"
34#include "Matrix.h"
35#include "Vector.h"
36#include "Vertex.h"
37
38namespace android {
39namespace uirenderer {
40
41#define THRESHOLD 0.5f
42
Chris Craikcb4d6002012-09-25 12:00:29 -070043SkRect PathRenderer::computePathBounds(const SkPath& path, const SkPaint* paint) {
44 SkRect bounds = path.getBounds();
45 if (paint->getStyle() != SkPaint::kFill_Style) {
46 float outset = paint->getStrokeWidth() * 0.5f;
47 bounds.outset(outset, outset);
48 }
49 return bounds;
50}
51
52void computeInverseScales(const mat4 *transform, float &inverseScaleX, float& inverseScaleY) {
Chris Craik710f46d2012-09-17 17:25:49 -070053 if (CC_UNLIKELY(!transform->isPureTranslate())) {
54 float m00 = transform->data[Matrix4::kScaleX];
55 float m01 = transform->data[Matrix4::kSkewY];
56 float m10 = transform->data[Matrix4::kSkewX];
57 float m11 = transform->data[Matrix4::kScaleY];
58 float scaleX = sqrt(m00 * m00 + m01 * m01);
59 float scaleY = sqrt(m10 * m10 + m11 * m11);
Chris Craikcb4d6002012-09-25 12:00:29 -070060 inverseScaleX = (scaleX != 0) ? (1.0f / scaleX) : 0;
61 inverseScaleY = (scaleY != 0) ? (1.0f / scaleY) : 0;
62 } else {
63 inverseScaleX = 1.0f;
64 inverseScaleY = 1.0f;
Chris Craik710f46d2012-09-17 17:25:49 -070065 }
66}
67
Chris Craikcb4d6002012-09-25 12:00:29 -070068inline void copyVertex(Vertex* destPtr, const Vertex* srcPtr)
69{
70 Vertex::set(destPtr, srcPtr->position[0], srcPtr->position[1]);
71}
Chris Craik710f46d2012-09-17 17:25:49 -070072
Chris Craikcb4d6002012-09-25 12:00:29 -070073inline void copyAlphaVertex(AlphaVertex* destPtr, const AlphaVertex* srcPtr)
74{
75 AlphaVertex::set(destPtr, srcPtr->position[0], srcPtr->position[1], srcPtr->alpha);
76}
Chris Craik710f46d2012-09-17 17:25:49 -070077
Chris Craikcb4d6002012-09-25 12:00:29 -070078void getFillVerticesFromPerimeter(const Vector<Vertex>& perimeter, VertexBuffer& vertexBuffer) {
79 Vertex* buffer = vertexBuffer.alloc<Vertex>(perimeter.size());
80
Chris Craik710f46d2012-09-17 17:25:49 -070081 int currentIndex = 0;
Chris Craikcb4d6002012-09-25 12:00:29 -070082 // zig zag between all previous points on the inside of the hull to create a
83 // triangle strip that fills the hull
84 int srcAindex = 0;
85 int srcBindex = perimeter.size() - 1;
86 while (srcAindex <= srcBindex) {
87 copyVertex(&buffer[currentIndex++], &perimeter[srcAindex]);
88 if (srcAindex == srcBindex) break;
89 copyVertex(&buffer[currentIndex++], &perimeter[srcBindex]);
90 srcAindex++;
91 srcBindex--;
Chris Craik710f46d2012-09-17 17:25:49 -070092 }
Chris Craikcb4d6002012-09-25 12:00:29 -070093}
94
95void getStrokeVerticesFromPerimeter(const Vector<Vertex>& perimeter, float halfStrokeWidth,
96 VertexBuffer& vertexBuffer, float inverseScaleX, float inverseScaleY) {
97 Vertex* buffer = vertexBuffer.alloc<Vertex>(perimeter.size() * 2 + 2);
98
99 int currentIndex = 0;
100 const Vertex* last = &(perimeter[perimeter.size() - 1]);
101 const Vertex* current = &(perimeter[0]);
102 vec2 lastNormal(current->position[1] - last->position[1],
103 last->position[0] - current->position[0]);
104 lastNormal.normalize();
105 for (unsigned int i = 0; i < perimeter.size(); i++) {
106 const Vertex* next = &(perimeter[i + 1 >= perimeter.size() ? 0 : i + 1]);
107 vec2 nextNormal(next->position[1] - current->position[1],
108 current->position[0] - next->position[0]);
109 nextNormal.normalize();
110
111 // offset each point by its normal, out and in, by appropriate stroke offset
112 vec2 totalOffset = (lastNormal + nextNormal);
113 totalOffset.normalize();
114 if (halfStrokeWidth == 0.0f) {
115 // hairline - compensate for scale
116 totalOffset.x *= 0.5f * inverseScaleX;
117 totalOffset.y *= 0.5f * inverseScaleY;
118 } else {
119 totalOffset *= halfStrokeWidth;
120 }
121
122 Vertex::set(&buffer[currentIndex++],
123 current->position[0] + totalOffset.x,
124 current->position[1] + totalOffset.y);
125
126 Vertex::set(&buffer[currentIndex++],
127 current->position[0] - totalOffset.x,
128 current->position[1] - totalOffset.y);
129
130 last = current;
131 current = next;
132 lastNormal = nextNormal;
133 }
134
135 // wrap around to beginning
136 copyVertex(&buffer[currentIndex++], &buffer[0]);
137 copyVertex(&buffer[currentIndex++], &buffer[1]);
138}
139
140void getFillVerticesFromPerimeterAA(const Vector<Vertex>& perimeter, VertexBuffer& vertexBuffer,
141 float inverseScaleX, float inverseScaleY) {
142 AlphaVertex* buffer = vertexBuffer.alloc<AlphaVertex>(perimeter.size() * 3 + 2);
Chris Craik710f46d2012-09-17 17:25:49 -0700143
144 // generate alpha points - fill Alpha vertex gaps in between each point with
145 // alpha 0 vertex, offset by a scaled normal.
Chris Craikcb4d6002012-09-25 12:00:29 -0700146 int currentIndex = 0;
147 const Vertex* last = &(perimeter[perimeter.size() - 1]);
148 const Vertex* current = &(perimeter[0]);
149 vec2 lastNormal(current->position[1] - last->position[1],
150 last->position[0] - current->position[0]);
151 lastNormal.normalize();
152 for (unsigned int i = 0; i < perimeter.size(); i++) {
153 const Vertex* next = &(perimeter[i + 1 >= perimeter.size() ? 0 : i + 1]);
Chris Craik710f46d2012-09-17 17:25:49 -0700154 vec2 nextNormal(next->position[1] - current->position[1],
Chris Craikcb4d6002012-09-25 12:00:29 -0700155 current->position[0] - next->position[0]);
Chris Craik710f46d2012-09-17 17:25:49 -0700156 nextNormal.normalize();
157
158 // AA point offset from original point is that point's normal, such that
159 // each side is offset by .5 pixels
Chris Craikcb4d6002012-09-25 12:00:29 -0700160 vec2 totalOffset = (lastNormal + nextNormal);
161 totalOffset.normalize();
162 totalOffset.x *= inverseScaleX * 0.5f;
163 totalOffset.y *= inverseScaleY * 0.5f;
Chris Craik710f46d2012-09-17 17:25:49 -0700164
165 AlphaVertex::set(&buffer[currentIndex++],
Chris Craikcb4d6002012-09-25 12:00:29 -0700166 current->position[0] + totalOffset.x,
167 current->position[1] + totalOffset.y,
168 0.0f);
Chris Craik710f46d2012-09-17 17:25:49 -0700169 AlphaVertex::set(&buffer[currentIndex++],
Chris Craikcb4d6002012-09-25 12:00:29 -0700170 current->position[0] - totalOffset.x,
171 current->position[1] - totalOffset.y,
172 1.0f);
173
Chris Craik710f46d2012-09-17 17:25:49 -0700174 last = current;
Chris Craikcb4d6002012-09-25 12:00:29 -0700175 current = next;
176 lastNormal = nextNormal;
Chris Craik710f46d2012-09-17 17:25:49 -0700177 }
178
179 // wrap around to beginning
Chris Craikcb4d6002012-09-25 12:00:29 -0700180 copyAlphaVertex(&buffer[currentIndex++], &buffer[0]);
181 copyAlphaVertex(&buffer[currentIndex++], &buffer[1]);
Chris Craik710f46d2012-09-17 17:25:49 -0700182
183 // zig zag between all previous points on the inside of the hull to create a
184 // triangle strip that fills the hull, repeating the first inner point to
185 // create degenerate tris to start inside path
186 int srcAindex = 0;
Chris Craikcb4d6002012-09-25 12:00:29 -0700187 int srcBindex = perimeter.size() - 1;
Chris Craik710f46d2012-09-17 17:25:49 -0700188 while (srcAindex <= srcBindex) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700189 copyAlphaVertex(&buffer[currentIndex++], &buffer[srcAindex * 2 + 1]);
Chris Craik710f46d2012-09-17 17:25:49 -0700190 if (srcAindex == srcBindex) break;
Chris Craikcb4d6002012-09-25 12:00:29 -0700191 copyAlphaVertex(&buffer[currentIndex++], &buffer[srcBindex * 2 + 1]);
Chris Craik710f46d2012-09-17 17:25:49 -0700192 srcAindex++;
193 srcBindex--;
194 }
195
196#if VERTEX_DEBUG
Chris Craikcb4d6002012-09-25 12:00:29 -0700197 for (unsigned int i = 0; i < vertexBuffer.getSize(); i++) {
198 ALOGD("point at %f %f", buffer[i].position[0], buffer[i].position[1]);
Chris Craik710f46d2012-09-17 17:25:49 -0700199 }
200#endif
201}
202
Chris Craikcb4d6002012-09-25 12:00:29 -0700203void getStrokeVerticesFromPerimeterAA(const Vector<Vertex>& perimeter, float halfStrokeWidth,
204 VertexBuffer& vertexBuffer, float inverseScaleX, float inverseScaleY) {
205 AlphaVertex* buffer = vertexBuffer.alloc<AlphaVertex>(6 * perimeter.size() + 8);
Chris Craik710f46d2012-09-17 17:25:49 -0700206
Chris Craikcb4d6002012-09-25 12:00:29 -0700207 int offset = 2 * perimeter.size() + 3;
208 int currentAAOuterIndex = 0;
209 int currentStrokeIndex = offset;
210 int currentAAInnerIndex = offset * 2;
211
212 const Vertex* last = &(perimeter[perimeter.size() - 1]);
213 const Vertex* current = &(perimeter[0]);
214 vec2 lastNormal(current->position[1] - last->position[1],
215 last->position[0] - current->position[0]);
216 lastNormal.normalize();
217 for (unsigned int i = 0; i < perimeter.size(); i++) {
218 const Vertex* next = &(perimeter[i + 1 >= perimeter.size() ? 0 : i + 1]);
219 vec2 nextNormal(next->position[1] - current->position[1],
220 current->position[0] - next->position[0]);
221 nextNormal.normalize();
222
223 vec2 pointNormal = (lastNormal + nextNormal);
224 pointNormal.normalize();
225 vec2 AAOffset = pointNormal * 0.5f;
226 AAOffset.x *= inverseScaleX;
227 AAOffset.y *= inverseScaleY;
228
229 vec2 innerOffset = pointNormal;
230 if (halfStrokeWidth == 0.0f) {
231 // hairline! - compensate for scale
232 innerOffset.x *= 0.5f * inverseScaleX;
233 innerOffset.y *= 0.5f * inverseScaleY;
234 } else {
235 innerOffset *= halfStrokeWidth;
236 }
237 vec2 outerOffset = innerOffset + AAOffset;
238 innerOffset -= AAOffset;
239
240 AlphaVertex::set(&buffer[currentAAOuterIndex++],
241 current->position[0] + outerOffset.x,
242 current->position[1] + outerOffset.y,
243 0.0f);
244 AlphaVertex::set(&buffer[currentAAOuterIndex++],
245 current->position[0] + innerOffset.x,
246 current->position[1] + innerOffset.y,
247 1.0f);
248
249 AlphaVertex::set(&buffer[currentStrokeIndex++],
250 current->position[0] + innerOffset.x,
251 current->position[1] + innerOffset.y,
252 1.0f);
253 AlphaVertex::set(&buffer[currentStrokeIndex++],
254 current->position[0] - innerOffset.x,
255 current->position[1] - innerOffset.y,
256 1.0f);
257
258 AlphaVertex::set(&buffer[currentAAInnerIndex++],
259 current->position[0] - innerOffset.x,
260 current->position[1] - innerOffset.y,
261 1.0f);
262 AlphaVertex::set(&buffer[currentAAInnerIndex++],
263 current->position[0] - outerOffset.x,
264 current->position[1] - outerOffset.y,
265 0.0f);
266
267 // TODO: current = next, copy last normal instead of recalculate
268 last = current;
269 current = next;
270 lastNormal = nextNormal;
271 }
272
273 // wrap each strip around to beginning, creating degenerate tris to bridge strips
274 copyAlphaVertex(&buffer[currentAAOuterIndex++], &buffer[0]);
275 copyAlphaVertex(&buffer[currentAAOuterIndex++], &buffer[1]);
276 copyAlphaVertex(&buffer[currentAAOuterIndex++], &buffer[1]);
277
278 copyAlphaVertex(&buffer[currentStrokeIndex++], &buffer[offset]);
279 copyAlphaVertex(&buffer[currentStrokeIndex++], &buffer[offset + 1]);
280 copyAlphaVertex(&buffer[currentStrokeIndex++], &buffer[offset + 1]);
281
282 copyAlphaVertex(&buffer[currentAAInnerIndex++], &buffer[2 * offset]);
283 copyAlphaVertex(&buffer[currentAAInnerIndex++], &buffer[2 * offset + 1]);
284 // don't need to create last degenerate tri
285}
286
287void PathRenderer::convexPathVertices(const SkPath &path, const SkPaint* paint,
288 const mat4 *transform, VertexBuffer& vertexBuffer) {
289 ATRACE_CALL();
290
291 SkPaint::Style style = paint->getStyle();
292 bool isAA = paint->isAntiAlias();
293
294 float inverseScaleX, inverseScaleY;
295 computeInverseScales(transform, inverseScaleX, inverseScaleY);
296
297 Vector<Vertex> tempVertices;
298 convexPathPerimeterVertices(path, inverseScaleX * inverseScaleX, inverseScaleY * inverseScaleY,
299 tempVertices);
300
301#if VERTEX_DEBUG
302 for (unsigned int i = 0; i < tempVertices.size(); i++) {
303 ALOGD("orig path: point at %f %f", tempVertices[i].position[0], tempVertices[i].position[1]);
304 }
305#endif
306
307 if (style == SkPaint::kStroke_Style) {
308 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
309 if (!isAA) {
310 getStrokeVerticesFromPerimeter(tempVertices, halfStrokeWidth, vertexBuffer,
311 inverseScaleX, inverseScaleY);
312 } else {
313 getStrokeVerticesFromPerimeterAA(tempVertices, halfStrokeWidth, vertexBuffer,
314 inverseScaleX, inverseScaleY);
315 }
316 } else {
317 // For kStrokeAndFill style, the path should be adjusted externally, as it will be treated as a fill here.
318 if (!isAA) {
319 getFillVerticesFromPerimeter(tempVertices, vertexBuffer);
320 } else {
321 getFillVerticesFromPerimeterAA(tempVertices, vertexBuffer, inverseScaleX, inverseScaleY);
322 }
323 }
324}
325
326
327void PathRenderer::convexPathPerimeterVertices(const SkPath& path,
328 float sqrInvScaleX, float sqrInvScaleY, Vector<Vertex>& outputVertices) {
Chris Craik710f46d2012-09-17 17:25:49 -0700329 ATRACE_CALL();
330
331 SkPath::Iter iter(path, true);
332 SkPoint pos;
333 SkPoint pts[4];
334 SkPath::Verb v;
335 Vertex* newVertex = 0;
336 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
337 switch (v) {
338 case SkPath::kMove_Verb:
339 pos = pts[0];
340 ALOGV("Move to pos %f %f", pts[0].x(), pts[0].y());
341 break;
342 case SkPath::kClose_Verb:
343 ALOGV("Close at pos %f %f", pts[0].x(), pts[0].y());
344 break;
345 case SkPath::kLine_Verb:
346 ALOGV("kLine_Verb %f %f -> %f %f",
Chris Craikcb4d6002012-09-25 12:00:29 -0700347 pts[0].x(), pts[0].y(),
348 pts[1].x(), pts[1].y());
Chris Craik710f46d2012-09-17 17:25:49 -0700349
350 // TODO: make this not yuck
351 outputVertices.push();
Chris Craikcb4d6002012-09-25 12:00:29 -0700352 newVertex = &(outputVertices.editArray()[outputVertices.size() - 1]);
Chris Craik710f46d2012-09-17 17:25:49 -0700353 Vertex::set(newVertex, pts[1].x(), pts[1].y());
354 break;
355 case SkPath::kQuad_Verb:
356 ALOGV("kQuad_Verb");
357 recursiveQuadraticBezierVertices(
Chris Craikcb4d6002012-09-25 12:00:29 -0700358 pts[0].x(), pts[0].y(),
359 pts[2].x(), pts[2].y(),
360 pts[1].x(), pts[1].y(),
361 sqrInvScaleX, sqrInvScaleY, outputVertices);
Chris Craik710f46d2012-09-17 17:25:49 -0700362 break;
363 case SkPath::kCubic_Verb:
364 ALOGV("kCubic_Verb");
365 recursiveCubicBezierVertices(
Chris Craikcb4d6002012-09-25 12:00:29 -0700366 pts[0].x(), pts[0].y(),
367 pts[1].x(), pts[1].y(),
368 pts[3].x(), pts[3].y(),
369 pts[2].x(), pts[2].y(),
370 sqrInvScaleX, sqrInvScaleY, outputVertices);
Chris Craik710f46d2012-09-17 17:25:49 -0700371 break;
372 default:
373 break;
374 }
375 }
376}
377
378void PathRenderer::recursiveCubicBezierVertices(
379 float p1x, float p1y, float c1x, float c1y,
380 float p2x, float p2y, float c2x, float c2y,
Chris Craikcb4d6002012-09-25 12:00:29 -0700381 float sqrInvScaleX, float sqrInvScaleY, Vector<Vertex>& outputVertices) {
Chris Craik710f46d2012-09-17 17:25:49 -0700382 float dx = p2x - p1x;
383 float dy = p2y - p1y;
384 float d1 = fabs((c1x - p2x) * dy - (c1y - p2y) * dx);
385 float d2 = fabs((c2x - p2x) * dy - (c2y - p2y) * dx);
386 float d = d1 + d2;
387
Chris Craikcb4d6002012-09-25 12:00:29 -0700388 // multiplying by sqrInvScaleY/X equivalent to multiplying in dimensional scale factors
389
390 if (d * d < THRESHOLD * THRESHOLD * (dx * dx * sqrInvScaleY + dy * dy * sqrInvScaleX)) {
Chris Craik710f46d2012-09-17 17:25:49 -0700391 // below thresh, draw line by adding endpoint
392 // TODO: make this not yuck
393 outputVertices.push();
Chris Craikcb4d6002012-09-25 12:00:29 -0700394 Vertex* newVertex = &(outputVertices.editArray()[outputVertices.size() - 1]);
Chris Craik710f46d2012-09-17 17:25:49 -0700395 Vertex::set(newVertex, p2x, p2y);
396 } else {
397 float p1c1x = (p1x + c1x) * 0.5f;
398 float p1c1y = (p1y + c1y) * 0.5f;
399 float p2c2x = (p2x + c2x) * 0.5f;
400 float p2c2y = (p2y + c2y) * 0.5f;
401
402 float c1c2x = (c1x + c2x) * 0.5f;
403 float c1c2y = (c1y + c2y) * 0.5f;
404
405 float p1c1c2x = (p1c1x + c1c2x) * 0.5f;
406 float p1c1c2y = (p1c1y + c1c2y) * 0.5f;
407
408 float p2c1c2x = (p2c2x + c1c2x) * 0.5f;
409 float p2c1c2y = (p2c2y + c1c2y) * 0.5f;
410
411 float mx = (p1c1c2x + p2c1c2x) * 0.5f;
412 float my = (p1c1c2y + p2c1c2y) * 0.5f;
413
414 recursiveCubicBezierVertices(
415 p1x, p1y, p1c1x, p1c1y,
416 mx, my, p1c1c2x, p1c1c2y,
Chris Craikcb4d6002012-09-25 12:00:29 -0700417 sqrInvScaleX, sqrInvScaleY, outputVertices);
Chris Craik710f46d2012-09-17 17:25:49 -0700418 recursiveCubicBezierVertices(
419 mx, my, p2c1c2x, p2c1c2y,
420 p2x, p2y, p2c2x, p2c2y,
Chris Craikcb4d6002012-09-25 12:00:29 -0700421 sqrInvScaleX, sqrInvScaleY, outputVertices);
Chris Craik710f46d2012-09-17 17:25:49 -0700422 }
423}
424
425void PathRenderer::recursiveQuadraticBezierVertices(
426 float ax, float ay,
427 float bx, float by,
428 float cx, float cy,
Chris Craikcb4d6002012-09-25 12:00:29 -0700429 float sqrInvScaleX, float sqrInvScaleY, Vector<Vertex>& outputVertices) {
Chris Craik710f46d2012-09-17 17:25:49 -0700430 float dx = bx - ax;
431 float dy = by - ay;
432 float d = (cx - bx) * dy - (cy - by) * dx;
433
Chris Craikcb4d6002012-09-25 12:00:29 -0700434 if (d * d < THRESHOLD * THRESHOLD * (dx * dx * sqrInvScaleY + dy * dy * sqrInvScaleX)) {
Chris Craik710f46d2012-09-17 17:25:49 -0700435 // below thresh, draw line by adding endpoint
436 // TODO: make this not yuck
437 outputVertices.push();
Chris Craikcb4d6002012-09-25 12:00:29 -0700438 Vertex* newVertex = &(outputVertices.editArray()[outputVertices.size() - 1]);
Chris Craik710f46d2012-09-17 17:25:49 -0700439 Vertex::set(newVertex, bx, by);
440 } else {
441 float acx = (ax + cx) * 0.5f;
442 float bcx = (bx + cx) * 0.5f;
443 float acy = (ay + cy) * 0.5f;
444 float bcy = (by + cy) * 0.5f;
445
446 // midpoint
447 float mx = (acx + bcx) * 0.5f;
448 float my = (acy + bcy) * 0.5f;
449
450 recursiveQuadraticBezierVertices(ax, ay, mx, my, acx, acy,
Chris Craikcb4d6002012-09-25 12:00:29 -0700451 sqrInvScaleX, sqrInvScaleY, outputVertices);
Chris Craik710f46d2012-09-17 17:25:49 -0700452 recursiveQuadraticBezierVertices(mx, my, bx, by, bcx, bcy,
Chris Craikcb4d6002012-09-25 12:00:29 -0700453 sqrInvScaleX, sqrInvScaleY, outputVertices);
Chris Craik710f46d2012-09-17 17:25:49 -0700454 }
455}
456
457}; // namespace uirenderer
458}; // namespace android