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 17 SkMatrix 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 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 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 69 const GrCoordTransform* GrGLSLPrimitiveProcessor::FPCoordTransformHandler::nextCoordTransform() { 70 #ifdef SK_DEBUG 71 SkASSERT(nullptr == fCurr || fAddedCoord); 72 fAddedCoord = false; 73 fCurr = fIter.next(); 74 return fCurr; 75 #else 76 return fIter.next(); 77 #endif 78 } 79