Fix check for valid proxies returned by AtlasManager.

Also renames a lot of variables to make it clearer that getProxies()
returns the number of instantiated proxies, not the number of all
proxies.

Bug: skia:
Change-Id: Ifbc910cbd6635dccdb4e7f0df2e69a0f341130af
Reviewed-on: https://skia-review.googlesource.com/128660
Commit-Queue: Jim Van Verth <jvanverth@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/effects/GrBitmapTextGeoProc.cpp b/src/gpu/effects/GrBitmapTextGeoProc.cpp
index 3efba9f..1412679 100644
--- a/src/gpu/effects/GrBitmapTextGeoProc.cpp
+++ b/src/gpu/effects/GrBitmapTextGeoProc.cpp
@@ -122,7 +122,7 @@
 
 GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color,
                                          const sk_sp<GrTextureProxy>* proxies,
-                                         int numProxies,
+                                         int numActiveProxies,
                                          const GrSamplerState& params, GrMaskFormat format,
                                          const SkMatrix& localMatrix, bool usesLocalCoords)
         : INHERITED(kGrBitmapTextGeoProc_ClassID)
@@ -131,7 +131,7 @@
         , fUsesLocalCoords(usesLocalCoords)
         , fInColor(nullptr)
         , fMaskFormat(format) {
-    SkASSERT(numProxies <= kMaxTextures);
+    SkASSERT(numActiveProxies <= kMaxTextures);
 
     fInPosition = &this->addVertexAttrib("inPosition", kFloat2_GrVertexAttribType);
 
@@ -142,7 +142,7 @@
     }
 
     fInTextureCoords = &this->addVertexAttrib("inTextureCoords", kUShort2_GrVertexAttribType);
-    for (int i = 0; i < numProxies; ++i) {
+    for (int i = 0; i < numActiveProxies; ++i) {
         SkASSERT(proxies[i]);
 
         fTextureSamplers[i].reset(std::move(proxies[i]), params);
@@ -151,11 +151,11 @@
 }
 
 void GrBitmapTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
-                                        int numProxies,
+                                        int numActiveProxies,
                                         const GrSamplerState& params) {
-    SkASSERT(numProxies <= kMaxTextures);
+    SkASSERT(numActiveProxies <= kMaxTextures);
 
-    for (int i = 0; i < numProxies; ++i) {
+    for (int i = 0; i < numActiveProxies; ++i) {
         SkASSERT(proxies[i]);
 
         if (!fTextureSamplers[i].isInitialized()) {
diff --git a/src/gpu/effects/GrBitmapTextGeoProc.h b/src/gpu/effects/GrBitmapTextGeoProc.h
index 1f52e07..3b95f76 100644
--- a/src/gpu/effects/GrBitmapTextGeoProc.h
+++ b/src/gpu/effects/GrBitmapTextGeoProc.h
@@ -24,11 +24,11 @@
 
     static sk_sp<GrGeometryProcessor> Make(GrColor color,
                                            const sk_sp<GrTextureProxy>* proxies,
-                                           int numProxies,
+                                           int numActiveProxies,
                                            const GrSamplerState& p, GrMaskFormat format,
                                            const SkMatrix& localMatrix, bool usesLocalCoords) {
         return sk_sp<GrGeometryProcessor>(
-            new GrBitmapTextGeoProc(color, proxies, numProxies, p, format,
+            new GrBitmapTextGeoProc(color, proxies, numActiveProxies, p, format,
                                     localMatrix, usesLocalCoords));
     }
 
@@ -45,7 +45,7 @@
     const SkMatrix& localMatrix() const { return fLocalMatrix; }
     bool usesLocalCoords() const { return fUsesLocalCoords; }
 
-    void addNewProxies(const sk_sp<GrTextureProxy>* proxies, int numProxies, const GrSamplerState&);
+    void addNewProxies(const sk_sp<GrTextureProxy>*, int numActiveProxies, const GrSamplerState&);
 
     void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
 
diff --git a/src/gpu/effects/GrDistanceFieldGeoProc.h b/src/gpu/effects/GrDistanceFieldGeoProc.h
index 4ac6ae0..5ba19ed 100644
--- a/src/gpu/effects/GrDistanceFieldGeoProc.h
+++ b/src/gpu/effects/GrDistanceFieldGeoProc.h
@@ -56,19 +56,19 @@
     /** The local matrix should be identity if local coords are not required by the GrPipeline. */
 #ifdef SK_GAMMA_APPLY_TO_A8
     static sk_sp<GrGeometryProcessor> Make(const sk_sp<GrTextureProxy>* proxies,
-                                           int numProxies,
+                                           int numActiveProxies,
                                            const GrSamplerState& params, float lum, uint32_t flags,
                                            const SkMatrix& localMatrixIfUsesLocalCoords) {
         return sk_sp<GrGeometryProcessor>(new GrDistanceFieldA8TextGeoProc(
-                proxies, numProxies, params, lum, flags, localMatrixIfUsesLocalCoords));
+                proxies, numActiveProxies, params, lum, flags, localMatrixIfUsesLocalCoords));
     }
 #else
     static sk_sp<GrGeometryProcessor> Make(const sk_sp<GrTextureProxy>* proxies,
-                                           int numProxies,
+                                           int numActiveProxies,
                                            const GrSamplerState& params, uint32_t flags,
                                            const SkMatrix& localMatrixIfUsesLocalCoords) {
         return sk_sp<GrGeometryProcessor>(new GrDistanceFieldA8TextGeoProc(
-                proxies, numProxies, params, flags, localMatrixIfUsesLocalCoords));
+                proxies, numActiveProxies, params, flags, localMatrixIfUsesLocalCoords));
     }
 #endif
 
@@ -93,7 +93,7 @@
 
 private:
     GrDistanceFieldA8TextGeoProc(const sk_sp<GrTextureProxy>* proxies,
-                                 int numProxies,
+                                 int numActiveProxies,
                                  const GrSamplerState& params,
 #ifdef SK_GAMMA_APPLY_TO_A8
                                  float distanceAdjust,
@@ -129,10 +129,10 @@
     /** The local matrix should be identity if local coords are not required by the GrPipeline. */
     static sk_sp<GrGeometryProcessor> Make(const SkMatrix& matrix,
                                            const sk_sp<GrTextureProxy>* proxies,
-                                           int numProxies,
+                                           int numActiveProxies,
                                            const GrSamplerState& params, uint32_t flags) {
         return sk_sp<GrGeometryProcessor>(
-            new GrDistanceFieldPathGeoProc(matrix, proxies, numProxies, params, flags));
+            new GrDistanceFieldPathGeoProc(matrix, proxies, numActiveProxies, params, flags));
     }
 
     ~GrDistanceFieldPathGeoProc() override {}
@@ -145,7 +145,7 @@
     const SkMatrix& matrix() const { return fMatrix; }
     uint32_t getFlags() const { return fFlags; }
 
-    void addNewProxies(const sk_sp<GrTextureProxy>* proxies, int numProxies, const GrSamplerState&);
+    void addNewProxies(const sk_sp<GrTextureProxy>*, int numActiveProxies, const GrSamplerState&);
 
     void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
 
@@ -156,7 +156,7 @@
 
     GrDistanceFieldPathGeoProc(const SkMatrix& matrix,
                                const sk_sp<GrTextureProxy>* proxies,
-                               int numProxies,
+                               int numActiveProxies,
                                const GrSamplerState&, uint32_t flags);
 
     SkMatrix         fMatrix;      // view matrix if perspective, local matrix otherwise
@@ -195,13 +195,14 @@
     };
 
     static sk_sp<GrGeometryProcessor> Make(const sk_sp<GrTextureProxy>* proxies,
-                                           int numProxies,
+                                           int numActiveProxies,
                                            const GrSamplerState& params,
                                            DistanceAdjust distanceAdjust,
                                            uint32_t flags,
                                            const SkMatrix& localMatrixIfUsesLocalCoords) {
-        return sk_sp<GrGeometryProcessor>(new GrDistanceFieldLCDTextGeoProc(
-                proxies, numProxies, params, distanceAdjust, flags, localMatrixIfUsesLocalCoords));
+        return sk_sp<GrGeometryProcessor>(
+            new GrDistanceFieldLCDTextGeoProc(proxies, numActiveProxies, params, distanceAdjust,
+                                              flags, localMatrixIfUsesLocalCoords));
     }
 
     ~GrDistanceFieldLCDTextGeoProc() override {}
@@ -215,14 +216,14 @@
     uint32_t getFlags() const { return fFlags; }
     const SkMatrix& localMatrix() const { return fLocalMatrix; }
 
-    void addNewProxies(const sk_sp<GrTextureProxy>* proxies, int numProxies, const GrSamplerState&);
+    void addNewProxies(const sk_sp<GrTextureProxy>*, int numActiveProxies, const GrSamplerState&);
 
     void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
 
     GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
 
 private:
-    GrDistanceFieldLCDTextGeoProc(const sk_sp<GrTextureProxy>* proxies, int numProxies,
+    GrDistanceFieldLCDTextGeoProc(const sk_sp<GrTextureProxy>* proxies, int numActiveProxies,
                                   const GrSamplerState& params, DistanceAdjust wa, uint32_t flags,
                                   const SkMatrix& localMatrix);
 
diff --git a/src/gpu/ops/GrAtlasTextOp.cpp b/src/gpu/ops/GrAtlasTextOp.cpp
index 96c592d..99af581 100644
--- a/src/gpu/ops/GrAtlasTextOp.cpp
+++ b/src/gpu/ops/GrAtlasTextOp.cpp
@@ -228,25 +228,26 @@
 
     GrMaskFormat maskFormat = this->maskFormat();
 
-    unsigned int atlasPageCount;
-    const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &atlasPageCount);
-    if (!proxies[0]) {
+    unsigned int numActiveProxies;
+    const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
+    if (!proxies) {
         SkDebugf("Could not allocate backing texture for atlas\n");
         return;
     }
+    SkASSERT(proxies[0]);
 
     FlushInfo flushInfo;
     flushInfo.fPipeline =
             target->makePipeline(fSRGBFlags, std::move(fProcessors), target->detachAppliedClip());
     SkDEBUGCODE(bool dfPerspective = false);
     if (this->usesDistanceFields()) {
-        flushInfo.fGeometryProcessor = this->setupDfProcessor(atlasManager);
+        flushInfo.fGeometryProcessor = this->setupDfProcessor(proxies, numActiveProxies);
         SkDEBUGCODE(dfPerspective = fGeoData[0].fViewMatrix.hasPerspective());
     } else {
         GrSamplerState samplerState = fHasScaledGlyphs ? GrSamplerState::ClampBilerp()
                                                        : GrSamplerState::ClampNearest();
         flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
-            this->color(), proxies, atlasPageCount, samplerState, maskFormat,
+            this->color(), proxies, numActiveProxies, samplerState, maskFormat,
             localMatrix, this->usesLocalCoords());
     }
 
@@ -330,23 +331,24 @@
     GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
     GrMaskFormat maskFormat = this->maskFormat();
 
-    unsigned int numProxies;
-    const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numProxies);
-    if (gp->numTextureSamplers() != (int) numProxies) {
+    unsigned int numActiveProxies;
+    const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
+    SkASSERT(proxies);
+    if (gp->numTextureSamplers() != (int) numActiveProxies) {
         // During preparation the number of atlas pages has increased.
         // Update the proxies used in the GP to match.
         if (this->usesDistanceFields()) {
             if (this->isLCD()) {
                 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewProxies(
-                    proxies, numProxies, GrSamplerState::ClampBilerp());
+                    proxies, numActiveProxies, GrSamplerState::ClampBilerp());
             } else {
                 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewProxies(
-                    proxies, numProxies, GrSamplerState::ClampBilerp());
+                    proxies, numActiveProxies, GrSamplerState::ClampBilerp());
             }
         } else {
             GrSamplerState samplerState = fHasScaledGlyphs ? GrSamplerState::ClampBilerp()
                                                            : GrSamplerState::ClampNearest();
-            reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(proxies, numProxies,
+            reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(proxies, numActiveProxies,
                                                                       samplerState);
         }
     }
@@ -441,10 +443,8 @@
 
 // TODO trying to figure out why lcd is so whack
 // (see comments in GrAtlasTextContext::ComputeCanonicalColor)
-sk_sp<GrGeometryProcessor> GrAtlasTextOp::setupDfProcessor(GrAtlasManager* atlasManager) const {
-    unsigned int numProxies;
-    const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(this->maskFormat(),
-                                                                    &numProxies);
+sk_sp<GrGeometryProcessor> GrAtlasTextOp::setupDfProcessor(const sk_sp<GrTextureProxy>* proxies,
+                                                           unsigned int numActiveProxies) const {
     bool isLCD = this->isLCD();
 
     SkMatrix localMatrix = SkMatrix::I();
@@ -468,7 +468,7 @@
         GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
                 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
                         redCorrection, greenCorrection, blueCorrection);
-        return GrDistanceFieldLCDTextGeoProc::Make(proxies, numProxies,
+        return GrDistanceFieldLCDTextGeoProc::Make(proxies, numActiveProxies,
                                                    GrSamplerState::ClampBilerp(), widthAdjust,
                                                    fDFGPFlags, localMatrix);
     } else {
@@ -480,11 +480,11 @@
             correction = fDistanceAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
                                                              fUseGammaCorrectDistanceTable);
         }
-        return GrDistanceFieldA8TextGeoProc::Make(proxies, numProxies,
+        return GrDistanceFieldA8TextGeoProc::Make(proxies, numActiveProxies,
                                                   GrSamplerState::ClampBilerp(),
                                                   correction, fDFGPFlags, localMatrix);
 #else
-        return GrDistanceFieldA8TextGeoProc::Make(proxies, numProxies,
+        return GrDistanceFieldA8TextGeoProc::Make(proxies, numActiveProxies,
                                                   GrSamplerState::ClampBilerp(),
                                                   fDFGPFlags, localMatrix);
 #endif
diff --git a/src/gpu/ops/GrAtlasTextOp.h b/src/gpu/ops/GrAtlasTextOp.h
index 2f0fcae..67474be 100644
--- a/src/gpu/ops/GrAtlasTextOp.h
+++ b/src/gpu/ops/GrAtlasTextOp.h
@@ -175,7 +175,8 @@
 
     bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override;
 
-    sk_sp<GrGeometryProcessor> setupDfProcessor(GrAtlasManager*) const;
+    sk_sp<GrGeometryProcessor> setupDfProcessor(const sk_sp<GrTextureProxy>* proxies,
+                                                unsigned int numActiveProxies) const;
 
     SkAutoSTMalloc<kMinGeometryAllocated, Geometry> fGeoData;
     int fGeoDataAllocSize;
diff --git a/src/gpu/text/GrAtlasManager.h b/src/gpu/text/GrAtlasManager.h
index 9bf03c1..18ccfd0 100644
--- a/src/gpu/text/GrAtlasManager.h
+++ b/src/gpu/text/GrAtlasManager.h
@@ -30,13 +30,14 @@
 
     // if getProxies returns nullptr, the client must not try to use other functions on the
     // GrGlyphCache which use the atlas.  This function *must* be called first, before other
-    // functions which use the atlas.
-    const sk_sp<GrTextureProxy>* getProxies(GrMaskFormat format, unsigned int* numProxies) {
+    // functions which use the atlas. Note that we can have proxies available but none active
+    // (i.e., none instantiated).
+    const sk_sp<GrTextureProxy>* getProxies(GrMaskFormat format, unsigned int* numActiveProxies) {
         if (this->initAtlas(format)) {
-            *numProxies = this->getAtlas(format)->numActivePages();
+            *numActiveProxies = this->getAtlas(format)->numActivePages();
             return this->getAtlas(format)->getProxies();
         }
-        *numProxies = 0;
+        *numActiveProxies = 0;
         return nullptr;
     }
 
diff --git a/tools/gpu/GrTest.cpp b/tools/gpu/GrTest.cpp
index 311f318..ec07758 100644
--- a/tools/gpu/GrTest.cpp
+++ b/tools/gpu/GrTest.cpp
@@ -130,9 +130,9 @@
         return nullptr;
     }
 
-    unsigned int numProxies;
-    const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(format, &numProxies);
-    if (index >= numProxies || !proxies[index]) {
+    unsigned int numActiveProxies;
+    const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(format, &numActiveProxies);
+    if (index >= numActiveProxies || !proxies || !proxies[index]) {
         return nullptr;
     }