1e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com
2e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com/*
3e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com * Copyright 2012 Google Inc.
4e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com *
5e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com * Use of this source code is governed by a BSD-style license that can be
6e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com * found in the LICENSE file.
7e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com */
8e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com
9e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com#include "GrAndroidPathRenderer.h"
10e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com#include "AndroidPathRenderer.h"
11e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com#include "Vertex.h"
12e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com
13e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.comGrAndroidPathRenderer::GrAndroidPathRenderer() {
14e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com}
15e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com
16e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.combool GrAndroidPathRenderer::canDrawPath(const SkPath& path,
17e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com                                        const SkStrokeRec& stroke,
18e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com                                        const GrDrawTarget* target,
19e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com                                        bool antiAlias) const {
20e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    return ((stroke.isFillStyle() || stroke.getStyle() == SkStrokeRec::kStroke_Style)
21e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com             && !path.isInverseFillType() && path.isConvex());
22e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com}
23e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com
24e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.comstruct ColorVertex {
25e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    SkPoint pos;
26e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    GrColor color;
27e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com};
28e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com
29e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.combool GrAndroidPathRenderer::onDrawPath(const SkPath& origPath,
30e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com                                       const SkStrokeRec& stroke,
31e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com                                       GrDrawTarget* target,
32e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com                                       bool antiAlias) {
33e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com
34e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    // generate verts using Android algorithm
35e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    android::uirenderer::VertexBuffer vertices;
36e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    android::uirenderer::PathRenderer::ConvexPathVertices(origPath, stroke, antiAlias, NULL,
37e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com                                                          &vertices);
38e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com
399b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com    // set vertex attributes depending on anti-alias
409b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com    GrDrawState* drawState = target->drawState();
419b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com    if (antiAlias) {
429b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com        // position + coverage
439b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com        GrVertexAttrib attribs[] = {
449b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com            GrVertexAttrib(kVec2f_GrVertexAttribType, 0),
459b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com            GrVertexAttrib(kVec4ub_GrVertexAttribType, sizeof(GrPoint))
469b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com        };
479b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com        drawState->setVertexAttribs(attribs, SK_ARRAY_COUNT(attribs));
489b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com        drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0);
499b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com        drawState->setAttribIndex(GrDrawState::kCoverage_AttribIndex, 1);
509b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com        drawState->setAttribBindings(GrDrawState::kCoverage_AttribBindingsBit);
519b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com    } else {
529b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com        drawState->setDefaultVertexAttribs();
539b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com    }
54e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com
55e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    // allocate our vert buffer
56e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    int vertCount = vertices.getSize();
579b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com    GrDrawTarget::AutoReleaseGeometry geo(target, vertCount, 0);
58e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    if (!geo.succeeded()) {
59e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com        GrPrintf("Failed to get space for vertices!\n");
60e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com        return false;
61e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    }
62e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com
63e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    // copy android verts to our vertex buffer
64e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    if (antiAlias) {
65f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        SkASSERT(sizeof(ColorVertex) == drawState->getVertexSize());
66e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com        ColorVertex* outVert = reinterpret_cast<ColorVertex*>(geo.vertices());
67e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com        android::uirenderer::AlphaVertex* inVert =
68e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com            reinterpret_cast<android::uirenderer::AlphaVertex*>(vertices.getBuffer());
69e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com
70e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com        for (int i = 0; i < vertCount; ++i) {
71e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com            // copy vertex position
72e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com            outVert->pos.set(inVert->position[0], inVert->position[1]);
73e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com            // copy alpha
74e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com            int coverage = static_cast<int>(inVert->alpha * 0xff);
75e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com            outVert->color = GrColorPackRGBA(coverage, coverage, coverage, coverage);
76e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com            ++outVert;
77e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com            ++inVert;
78e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com        }
79e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    } else {
809b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com       size_t vsize = drawState->getVertexSize();
81e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com       size_t copySize = vsize*vertCount;
82e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com       memcpy(geo.vertices(), vertices.getBuffer(), copySize);
83e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    }
84e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com
85e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    // render it
86e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, vertCount);
87e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com
88e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com    return true;
89e04cf0cf1d281b4e282dbd5d2eb2de553bc1d2b7skia.committer@gmail.com}
90