Switch atlas clients over to using absolute texture coordinates

This is a prerequisite for being able to resize the atlas with impunity.

Change-Id: I509816c8d6f38fbc92fa39aeab303b42ab09f58b
Reviewed-on: https://skia-review.googlesource.com/37560
Reviewed-by: Robert Phillips <robertphillips@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/effects/GrBitmapTextGeoProc.cpp b/src/gpu/effects/GrBitmapTextGeoProc.cpp
index a934ff1..d24ddf8 100644
--- a/src/gpu/effects/GrBitmapTextGeoProc.cpp
+++ b/src/gpu/effects/GrBitmapTextGeoProc.cpp
@@ -17,50 +17,53 @@
 
 class GrGLBitmapTextGeoProc : public GrGLSLGeometryProcessor {
 public:
-    GrGLBitmapTextGeoProc() : fColor(GrColor_ILLEGAL) {}
+    GrGLBitmapTextGeoProc() : fColor(GrColor_ILLEGAL), fAtlasSize({0,0}) {}
 
     void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
-        const GrBitmapTextGeoProc& cte = args.fGP.cast<GrBitmapTextGeoProc>();
+        const GrBitmapTextGeoProc& btgp = args.fGP.cast<GrBitmapTextGeoProc>();
 
         GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
         GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
         GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
 
         // emit attributes
-        varyingHandler->emitAttributes(cte);
+        varyingHandler->emitAttributes(btgp);
 
-        // compute numbers to be hardcoded to convert texture coordinates from int to float
-        SkASSERT(cte.numTextureSamplers() == 1);
-        SkDEBUGCODE(GrTexture* atlas = cte.textureSampler(0).peekTexture());
-        SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
+        const char* atlasSizeInvName;
+        fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
+                                                          kVec2f_GrSLType,
+                                                          kHigh_GrSLPrecision,
+                                                          "AtlasSizeInv",
+                                                          &atlasSizeInvName);
 
         GrGLSLVertToFrag v(kVec2f_GrSLType);
         varyingHandler->addVarying("TextureCoords", &v, kHigh_GrSLPrecision);
-        vertBuilder->codeAppendf("%s = %s;", v.vsOut(),
-                                 cte.inTextureCoords()->fName);
+        vertBuilder->codeAppendf("%s = %s * %s;", v.vsOut(),
+                                 btgp.inTextureCoords()->fName,
+                                 atlasSizeInvName);
 
         GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
         // Setup pass through color
-        if (cte.hasVertexColor()) {
-            varyingHandler->addPassThroughAttribute(cte.inColor(), args.fOutputColor);
+        if (btgp.hasVertexColor()) {
+            varyingHandler->addPassThroughAttribute(btgp.inColor(), args.fOutputColor);
         } else {
             this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor,
                                     &fColorUniform);
         }
 
         // Setup position
-        this->writeOutputPosition(vertBuilder, gpArgs, cte.inPosition()->fName);
+        this->writeOutputPosition(vertBuilder, gpArgs, btgp.inPosition()->fName);
 
         // emit transforms
         this->emitTransforms(vertBuilder,
                              varyingHandler,
                              uniformHandler,
                              gpArgs->fPositionVar,
-                             cte.inPosition()->fName,
-                             cte.localMatrix(),
+                             btgp.inPosition()->fName,
+                             btgp.localMatrix(),
                              args.fFPCoordTransformHandler);
 
-        if (cte.maskFormat() == kARGB_GrMaskFormat) {
+        if (btgp.maskFormat() == kARGB_GrMaskFormat) {
             fragBuilder->codeAppendf("%s = ", args.fOutputColor);
             fragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
                                                         args.fTexSamplers[0],
@@ -84,31 +87,35 @@
             pdman.set4fv(fColorUniform, 1, c);
             fColor = btgp.color();
         }
+
+        SkASSERT(btgp.numTextureSamplers() == 1);
+        GrTexture* atlas = btgp.textureSampler(0).peekTexture();
+        SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
+
+        if (fAtlasSize.fWidth != atlas->width() || fAtlasSize.fHeight != atlas->height()) {
+            pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlas->width(), 1.0f / atlas->height());
+            fAtlasSize.set(atlas->width(), atlas->height());
+        }
         this->setTransformDataHelper(btgp.localMatrix(), pdman, &transformIter);
     }
 
     static inline void GenKey(const GrGeometryProcessor& proc,
                               const GrShaderCaps&,
                               GrProcessorKeyBuilder* b) {
-        const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
+        const GrBitmapTextGeoProc& btgp = proc.cast<GrBitmapTextGeoProc>();
         uint32_t key = 0;
-        key |= (gp.usesLocalCoords() && gp.localMatrix().hasPerspective()) ? 0x1 : 0x0;
-        key |= gp.maskFormat() << 1;
+        key |= (btgp.usesLocalCoords() && btgp.localMatrix().hasPerspective()) ? 0x1 : 0x0;
+        key |= btgp.maskFormat() << 1;
         b->add32(key);
-
-        // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
-        SkASSERT(gp.numTextureSamplers() == 1);
-        GrTextureProxy* atlas = gp.textureSampler(0).proxy();
-        if (atlas) {
-            b->add32(atlas->width());
-            b->add32(atlas->height());
-        }
     }
 
 private:
-    GrColor fColor;
+    GrColor       fColor;
     UniformHandle fColorUniform;
 
+    SkISize       fAtlasSize;
+    UniformHandle fAtlasSizeInvUniform;
+
     typedef GrGLSLGeometryProcessor INHERITED;
 };
 
@@ -133,7 +140,8 @@
     if (hasVertexColor) {
         fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType);
     }
-    fInTextureCoords = &this->addVertexAttrib("inTextureCoords",  kVec2us_GrVertexAttribType,
+
+    fInTextureCoords = &this->addVertexAttrib("inTextureCoords", kVec2us_uint_GrVertexAttribType,
                                               kHigh_GrSLPrecision);
     this->addTextureSampler(&fTextureSampler);
 }