skia.committer@gmail.com | e04cf0c | 2013-01-10 02:01:29 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "GrAndroidPathRenderer.h" |
| 10 | #include "AndroidPathRenderer.h" |
| 11 | #include "Vertex.h" |
| 12 | |
| 13 | GrAndroidPathRenderer::GrAndroidPathRenderer() { |
| 14 | } |
| 15 | |
| 16 | bool GrAndroidPathRenderer::canDrawPath(const SkPath& path, |
| 17 | const SkStrokeRec& stroke, |
| 18 | const GrDrawTarget* target, |
| 19 | bool antiAlias) const { |
| 20 | return ((stroke.isFillStyle() || stroke.getStyle() == SkStrokeRec::kStroke_Style) |
| 21 | && !path.isInverseFillType() && path.isConvex()); |
| 22 | } |
| 23 | |
| 24 | struct ColorVertex { |
| 25 | SkPoint pos; |
| 26 | GrColor color; |
| 27 | }; |
| 28 | |
| 29 | bool GrAndroidPathRenderer::onDrawPath(const SkPath& origPath, |
| 30 | const SkStrokeRec& stroke, |
| 31 | GrDrawTarget* target, |
| 32 | bool antiAlias) { |
| 33 | |
| 34 | // generate verts using Android algorithm |
| 35 | android::uirenderer::VertexBuffer vertices; |
| 36 | android::uirenderer::PathRenderer::ConvexPathVertices(origPath, stroke, antiAlias, NULL, |
| 37 | &vertices); |
| 38 | |
robertphillips@google.com | af3a3b9 | 2013-02-28 23:08:28 +0000 | [diff] [blame] | 39 | // set vertex layout depending on anti-alias |
| 40 | GrVertexLayout layout = antiAlias ? GrDrawState::kCoverage_VertexLayoutBit : 0; |
skia.committer@gmail.com | e04cf0c | 2013-01-10 02:01:29 +0000 | [diff] [blame] | 41 | |
| 42 | // allocate our vert buffer |
| 43 | int vertCount = vertices.getSize(); |
robertphillips@google.com | af3a3b9 | 2013-02-28 23:08:28 +0000 | [diff] [blame] | 44 | GrDrawTarget::AutoReleaseGeometry geo(target, layout, vertCount, 0); |
skia.committer@gmail.com | e04cf0c | 2013-01-10 02:01:29 +0000 | [diff] [blame] | 45 | if (!geo.succeeded()) { |
| 46 | GrPrintf("Failed to get space for vertices!\n"); |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | // copy android verts to our vertex buffer |
| 51 | if (antiAlias) { |
| 52 | ColorVertex* outVert = reinterpret_cast<ColorVertex*>(geo.vertices()); |
| 53 | android::uirenderer::AlphaVertex* inVert = |
| 54 | reinterpret_cast<android::uirenderer::AlphaVertex*>(vertices.getBuffer()); |
| 55 | |
| 56 | for (int i = 0; i < vertCount; ++i) { |
| 57 | // copy vertex position |
| 58 | outVert->pos.set(inVert->position[0], inVert->position[1]); |
| 59 | // copy alpha |
| 60 | int coverage = static_cast<int>(inVert->alpha * 0xff); |
| 61 | outVert->color = GrColorPackRGBA(coverage, coverage, coverage, coverage); |
| 62 | ++outVert; |
| 63 | ++inVert; |
| 64 | } |
| 65 | } else { |
robertphillips@google.com | af3a3b9 | 2013-02-28 23:08:28 +0000 | [diff] [blame] | 66 | size_t vsize = GrDrawState::VertexSize(layout); |
skia.committer@gmail.com | e04cf0c | 2013-01-10 02:01:29 +0000 | [diff] [blame] | 67 | size_t copySize = vsize*vertCount; |
| 68 | memcpy(geo.vertices(), vertices.getBuffer(), copySize); |
| 69 | } |
| 70 | |
| 71 | // render it |
| 72 | target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, vertCount); |
| 73 | |
| 74 | return true; |
| 75 | } |