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 "GrGLSLPrimitiveProcessor.h"
9
10#include "GrCoordTransform.h"
11#include "GrTexture.h"
12#include "glsl/GrGLSLFragmentShaderBuilder.h"
13#include "glsl/GrGLSLProgramBuilder.h"
14#include "glsl/GrGLSLUniformHandler.h"
15#include "glsl/GrGLSLVertexGeoBuilder.h"
16
17SkMatrix GrGLSLPrimitiveProcessor::GetTransformMatrix(const SkMatrix& localMatrix,
18                                                      const GrCoordTransform& coordTransform) {
19    SkMatrix combined;
20    combined.setConcat(coordTransform.getMatrix(), localMatrix);
21    if (coordTransform.normalize()) {
22        combined.postIDiv(coordTransform.peekTexture()->width(),
23                          coordTransform.peekTexture()->height());
24    }
25
26    if (coordTransform.reverseY()) {
27        // combined.postScale(1,-1);
28        // combined.postTranslate(0,1);
29        combined.set(SkMatrix::kMSkewY,
30            combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
31        combined.set(SkMatrix::kMScaleY,
32            combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
33        combined.set(SkMatrix::kMTransY,
34            combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
35    }
36    return combined;
37}
38
39void GrGLSLPrimitiveProcessor::setupUniformColor(GrGLSLPPFragmentBuilder* fragBuilder,
40                                                 GrGLSLUniformHandler* uniformHandler,
41                                                 const char* outputName,
42                                                 UniformHandle* colorUniform) {
43    SkASSERT(colorUniform);
44    const char* stagedLocalVarName;
45    *colorUniform = uniformHandler->addUniform(kFragment_GrShaderFlag,
46                                               kHalf4_GrSLType,
47                                               "Color",
48                                               &stagedLocalVarName);
49    fragBuilder->codeAppendf("%s = %s;", outputName, stagedLocalVarName);
50    if (fragBuilder->getProgramBuilder()->shaderCaps()->mustObfuscateUniformColor()) {
51        fragBuilder->codeAppendf("%s = max(%s, half4(0, 0, 0, 0));", outputName, outputName);
52    }
53}
54
55//////////////////////////////////////////////////////////////////////////////
56
57const GrCoordTransform* GrGLSLPrimitiveProcessor::FPCoordTransformHandler::nextCoordTransform() {
58#ifdef SK_DEBUG
59    SkASSERT(nullptr == fCurr || fAddedCoord);
60    fAddedCoord = false;
61    fCurr = fIter.next();
62    return fCurr;
63#else
64    return fIter.next();
65#endif
66}
67