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 "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
9
10 #include "include/gpu/GrTexture.h"
11 #include "src/gpu/GrCoordTransform.h"
12 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
13 #include "src/gpu/glsl/GrGLSLProgramBuilder.h"
14 #include "src/gpu/glsl/GrGLSLUniformHandler.h"
15 #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
16
GetTransformMatrix(const GrCoordTransform & coordTransform,const SkMatrix & preMatrix)17 SkMatrix GrGLSLPrimitiveProcessor::GetTransformMatrix(const GrCoordTransform& coordTransform,
18 const SkMatrix& preMatrix) {
19 SkMatrix combined;
20 combined.setConcat(coordTransform.matrix(), preMatrix);
21 if (coordTransform.normalize()) {
22 SkMatrixPriv::PostIDiv(&combined, coordTransform.peekTexture()->width(),
23 coordTransform.peekTexture()->height());
24 }
25
26 if (coordTransform.reverseY()) {
27 if (coordTransform.normalize()) {
28 // combined.postScale(1,-1);
29 // combined.postTranslate(0,1);
30 combined.set(SkMatrix::kMSkewY,
31 combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
32 combined.set(SkMatrix::kMScaleY,
33 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
34 combined.set(SkMatrix::kMTransY,
35 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
36 } else {
37 // combined.postScale(1, -1);
38 // combined.postTranslate(0,1);
39 SkScalar h = coordTransform.peekTexture()->height();
40 combined.set(SkMatrix::kMSkewY,
41 h * combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
42 combined.set(SkMatrix::kMScaleY,
43 h * combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
44 combined.set(SkMatrix::kMTransY,
45 h * combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
46 }
47 }
48 return combined;
49 }
50
setupUniformColor(GrGLSLFPFragmentBuilder * fragBuilder,GrGLSLUniformHandler * uniformHandler,const char * outputName,UniformHandle * colorUniform)51 void GrGLSLPrimitiveProcessor::setupUniformColor(GrGLSLFPFragmentBuilder* fragBuilder,
52 GrGLSLUniformHandler* uniformHandler,
53 const char* outputName,
54 UniformHandle* colorUniform) {
55 SkASSERT(colorUniform);
56 const char* stagedLocalVarName;
57 *colorUniform = uniformHandler->addUniform(kFragment_GrShaderFlag,
58 kHalf4_GrSLType,
59 "Color",
60 &stagedLocalVarName);
61 fragBuilder->codeAppendf("%s = %s;", outputName, stagedLocalVarName);
62 if (fragBuilder->getProgramBuilder()->shaderCaps()->mustObfuscateUniformColor()) {
63 fragBuilder->codeAppendf("%s = max(%s, half4(0, 0, 0, 0));", outputName, outputName);
64 }
65 }
66
67 //////////////////////////////////////////////////////////////////////////////
68
FPCoordTransformHandler(const GrPipeline & pipeline,SkTArray<TransformVar> * transformedCoordVars)69 GrGLSLPrimitiveProcessor::FPCoordTransformHandler::FPCoordTransformHandler(
70 const GrPipeline& pipeline, SkTArray<TransformVar>* transformedCoordVars)
71 : fIter(pipeline), fTransformedCoordVars(transformedCoordVars) {}
72
73 std::pair<const GrCoordTransform&, const GrFragmentProcessor&>
get() const74 GrGLSLPrimitiveProcessor::FPCoordTransformHandler::get() const {
75 return *fIter;
76 }
77
78 GrGLSLPrimitiveProcessor::FPCoordTransformHandler&
operator ++()79 GrGLSLPrimitiveProcessor::FPCoordTransformHandler::operator++() {
80 SkASSERT(fAddedCoord);
81 ++fIter;
82 SkDEBUGCODE(fAddedCoord = false;)
83 return *this;
84 }
85