1/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrGLPrimitiveProcessor.h"
9
10#include "builders/GrGLProgramBuilder.h"
11
12SkMatrix GrGLPrimitiveProcessor::GetTransformMatrix(const SkMatrix& localMatrix,
13                                                    const GrCoordTransform& coordTransform) {
14    SkMatrix combined;
15    // We only apply the localmatrix to localcoords
16    if (kLocal_GrCoordSet == coordTransform.sourceCoords()) {
17        combined.setConcat(coordTransform.getMatrix(), localMatrix);
18    } else {
19        combined = coordTransform.getMatrix();
20    }
21    if (coordTransform.reverseY()) {
22        // combined.postScale(1,-1);
23        // combined.postTranslate(0,1);
24        combined.set(SkMatrix::kMSkewY,
25            combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
26        combined.set(SkMatrix::kMScaleY,
27            combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
28        combined.set(SkMatrix::kMTransY,
29            combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
30    }
31    return combined;
32}
33
34void
35GrGLPrimitiveProcessor::setupColorPassThrough(GrGLGPBuilder* pb,
36                                              GrGPInput inputType,
37                                              const char* outputName,
38                                              const GrGeometryProcessor::Attribute* colorAttr,
39                                              UniformHandle* colorUniform) {
40    GrGLFragmentBuilder* fs = pb->getFragmentShaderBuilder();
41    if (kUniform_GrGPInput == inputType) {
42        SkASSERT(colorUniform);
43        const char* stagedLocalVarName;
44        *colorUniform = pb->addUniform(GrGLProgramBuilder::kFragment_Visibility,
45                                       kVec4f_GrSLType,
46                                       kDefault_GrSLPrecision,
47                                       "Color",
48                                       &stagedLocalVarName);
49        fs->codeAppendf("%s = %s;", outputName, stagedLocalVarName);
50    } else if (kAttribute_GrGPInput == inputType) {
51        SkASSERT(colorAttr);
52        pb->addPassThroughAttribute(colorAttr, outputName);
53    } else if (kAllOnes_GrGPInput == inputType) {
54        fs->codeAppendf("%s = vec4(1);", outputName);
55    }
56}
57
58void GrGLPrimitiveProcessor::addUniformViewMatrix(GrGLGPBuilder* pb) {
59    fViewMatrixUniform = pb->addUniform(GrGLProgramBuilder::kVertex_Visibility,
60                                        kMat33f_GrSLType, kHigh_GrSLPrecision,
61                                        "uViewM",
62                                        &fViewMatrixName);
63}
64
65void GrGLPrimitiveProcessor::setUniformViewMatrix(const GrGLProgramDataManager& pdman,
66                                                  const SkMatrix& viewMatrix) {
67    if (!viewMatrix.isIdentity() && !fViewMatrix.cheapEqualTo(viewMatrix)) {
68        SkASSERT(fViewMatrixUniform.isValid());
69        fViewMatrix = viewMatrix;
70
71        GrGLfloat viewMatrix[3 * 3];
72        GrGLGetMatrix<3>(viewMatrix, fViewMatrix);
73        pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
74    }
75}
76