blob: 2050f091a640bc13d16e794d196c95f12b58a2a9 [file] [log] [blame]
skia.committer@gmail.come04cf0c2013-01-10 02:01:29 +00001
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
13GrAndroidPathRenderer::GrAndroidPathRenderer() {
14}
15
16bool 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
24struct ColorVertex {
25 SkPoint pos;
26 GrColor color;
27};
28
29bool 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.comaf3a3b92013-02-28 23:08:28 +000039 // set vertex layout depending on anti-alias
40 GrVertexLayout layout = antiAlias ? GrDrawState::kCoverage_VertexLayoutBit : 0;
skia.committer@gmail.come04cf0c2013-01-10 02:01:29 +000041
42 // allocate our vert buffer
43 int vertCount = vertices.getSize();
robertphillips@google.comaf3a3b92013-02-28 23:08:28 +000044 GrDrawTarget::AutoReleaseGeometry geo(target, layout, vertCount, 0);
skia.committer@gmail.come04cf0c2013-01-10 02:01:29 +000045 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.comaf3a3b92013-02-28 23:08:28 +000066 size_t vsize = GrDrawState::VertexSize(layout);
skia.committer@gmail.come04cf0c2013-01-10 02:01:29 +000067 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}