GrBitmapTextGeoProc.cpp revision 64c4728c70001ed074fecf5c4e083781987b12e9
1/* 2 * Copyright 2013 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 "GrBitmapTextGeoProc.h" 9#include "GrInvariantOutput.h" 10#include "GrTexture.h" 11#include "gl/GrGLGeometryProcessor.h" 12#include "glsl/GrGLSLFragmentProcessor.h" 13#include "glsl/GrGLSLFragmentShaderBuilder.h" 14#include "glsl/GrGLSLProgramBuilder.h" 15#include "glsl/GrGLSLProgramDataManager.h" 16#include "glsl/GrGLSLVertexShaderBuilder.h" 17 18class GrGLBitmapTextGeoProc : public GrGLGeometryProcessor { 19public: 20 GrGLBitmapTextGeoProc() : fColor(GrColor_ILLEGAL) {} 21 22 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { 23 const GrBitmapTextGeoProc& cte = args.fGP.cast<GrBitmapTextGeoProc>(); 24 25 GrGLSLGPBuilder* pb = args.fPB; 26 GrGLSLVertexBuilder* vsBuilder = pb->getVertexShaderBuilder(); 27 28 // emit attributes 29 vsBuilder->emitAttributes(cte); 30 31 // compute numbers to be hardcoded to convert texture coordinates from int to float 32 SkASSERT(cte.numTextures() == 1); 33 GrTexture* atlas = cte.textureAccess(0).getTexture(); 34 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height())); 35 SkScalar recipWidth = 1.0f / atlas->width(); 36 SkScalar recipHeight = 1.0f / atlas->height(); 37 38 GrGLSLVertToFrag v(kVec2f_GrSLType); 39 pb->addVarying("TextureCoords", &v); 40 vsBuilder->codeAppendf("%s = vec2(%.*f, %.*f) * %s;", v.vsOut(), 41 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipWidth, 42 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipHeight, 43 cte.inTextureCoords()->fName); 44 45 // Setup pass through color 46 if (!cte.colorIgnored()) { 47 if (cte.hasVertexColor()) { 48 pb->addPassThroughAttribute(cte.inColor(), args.fOutputColor); 49 } else { 50 this->setupUniformColor(pb, args.fOutputColor, &fColorUniform); 51 } 52 } 53 54 // Setup position 55 this->setupPosition(pb, gpArgs, cte.inPosition()->fName); 56 57 // emit transforms 58 this->emitTransforms(args.fPB, gpArgs->fPositionVar, cte.inPosition()->fName, 59 cte.localMatrix(), args.fTransformsIn, args.fTransformsOut); 60 61 GrGLSLFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder(); 62 if (cte.maskFormat() == kARGB_GrMaskFormat) { 63 fsBuilder->codeAppendf("%s = ", args.fOutputColor); 64 fsBuilder->appendTextureLookupAndModulate(args.fOutputColor, 65 args.fSamplers[0], 66 v.fsIn(), 67 kVec2f_GrSLType); 68 fsBuilder->codeAppend(";"); 69 fsBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage); 70 } else { 71 fsBuilder->codeAppendf("%s = ", args.fOutputCoverage); 72 fsBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_GrSLType); 73 fsBuilder->codeAppend(";"); 74 if (cte.maskFormat() == kA565_GrMaskFormat) { 75 // set alpha to be max of rgb coverage 76 fsBuilder->codeAppendf("%s.a = max(max(%s.r, %s.g), %s.b);", 77 args.fOutputCoverage, args.fOutputCoverage, 78 args.fOutputCoverage, args.fOutputCoverage); 79 } 80 } 81 } 82 83 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override { 84 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>(); 85 if (btgp.color() != fColor && !btgp.hasVertexColor()) { 86 float c[4]; 87 GrColorToRGBAFloat(btgp.color(), c); 88 pdman.set4fv(fColorUniform, 1, c); 89 fColor = btgp.color(); 90 } 91 } 92 93 void setTransformData(const GrPrimitiveProcessor& primProc, 94 const GrGLSLProgramDataManager& pdman, 95 int index, 96 const SkTArray<const GrCoordTransform*, true>& transforms) override { 97 this->setTransformDataHelper<GrBitmapTextGeoProc>(primProc, pdman, index, transforms); 98 } 99 100 static inline void GenKey(const GrGeometryProcessor& proc, 101 const GrGLSLCaps&, 102 GrProcessorKeyBuilder* b) { 103 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>(); 104 uint32_t key = 0; 105 key |= gp.usesLocalCoords() && gp.localMatrix().hasPerspective() ? 0x1 : 0x0; 106 key |= gp.colorIgnored() ? 0x2 : 0x0; 107 key |= gp.maskFormat() << 3; 108 b->add32(key); 109 110 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point 111 SkASSERT(gp.numTextures() == 1); 112 GrTexture* atlas = gp.textureAccess(0).getTexture(); 113 SkASSERT(atlas); 114 b->add32(atlas->width()); 115 b->add32(atlas->height()); 116 } 117 118private: 119 GrColor fColor; 120 UniformHandle fColorUniform; 121 122 typedef GrGLGeometryProcessor INHERITED; 123}; 124 125/////////////////////////////////////////////////////////////////////////////// 126 127GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture, 128 const GrTextureParams& params, GrMaskFormat format, 129 const SkMatrix& localMatrix, bool usesLocalCoords) 130 : fColor(color) 131 , fLocalMatrix(localMatrix) 132 , fUsesLocalCoords(usesLocalCoords) 133 , fTextureAccess(texture, params) 134 , fInColor(nullptr) 135 , fMaskFormat(format) { 136 this->initClassID<GrBitmapTextGeoProc>(); 137 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType)); 138 139 // TODO we could think about removing this attribute if color is ignored, but unfortunately 140 // we don't do text positioning in batch, so we can't quite do that yet. 141 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat; 142 if (hasVertexColor) { 143 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType)); 144 } 145 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", 146 kVec2s_GrVertexAttribType)); 147 this->addTextureAccess(&fTextureAccess); 148} 149 150void GrBitmapTextGeoProc::getGLProcessorKey(const GrGLSLCaps& caps,GrProcessorKeyBuilder* b) const { 151 GrGLBitmapTextGeoProc::GenKey(*this, caps, b); 152} 153 154GrGLPrimitiveProcessor* GrBitmapTextGeoProc::createGLInstance(const GrGLSLCaps& caps) const { 155 return new GrGLBitmapTextGeoProc(); 156} 157 158/////////////////////////////////////////////////////////////////////////////// 159 160GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc); 161 162const GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) { 163 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : 164 GrProcessorUnitTest::kAlphaTextureIdx; 165 static const SkShader::TileMode kTileModes[] = { 166 SkShader::kClamp_TileMode, 167 SkShader::kRepeat_TileMode, 168 SkShader::kMirror_TileMode, 169 }; 170 SkShader::TileMode tileModes[] = { 171 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], 172 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], 173 }; 174 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode : 175 GrTextureParams::kNone_FilterMode); 176 177 GrMaskFormat format; 178 switch (d->fRandom->nextULessThan(3)) { 179 case 0: 180 format = kA8_GrMaskFormat; 181 break; 182 case 1: 183 format = kA565_GrMaskFormat; 184 break; 185 case 2: 186 format = kARGB_GrMaskFormat; 187 break; 188 } 189 190 return GrBitmapTextGeoProc::Create(GrRandomColor(d->fRandom), d->fTextures[texIdx], params, 191 format, GrTest::TestMatrix(d->fRandom), 192 d->fRandom->nextBool()); 193} 194