fix for persistent shader cache

Bug: skia:
Change-Id: I43b4dc58ab4dde3fa8140de0d73134c38f7d79db
Reviewed-on: https://skia-review.googlesource.com/140566
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
diff --git a/src/gpu/gl/GrGLCaps.cpp b/src/gpu/gl/GrGLCaps.cpp
index 5cb2342..f120b6d 100644
--- a/src/gpu/gl/GrGLCaps.cpp
+++ b/src/gpu/gl/GrGLCaps.cpp
@@ -600,10 +600,9 @@
     }
     if (fProgramBinarySupport) {
         GrGLint count;
-        GR_GL_GetIntegerv(gli, GR_GL_NUM_SHADER_BINARY_FORMATS, &count);
+        GR_GL_GetIntegerv(gli, GR_GL_NUM_PROGRAM_BINARY_FORMATS, &count);
         fProgramBinarySupport = count > 0;
     }
-
     // Requires fTextureRedSupport, fTextureSwizzleSupport, msaa support, ES compatibility have
     // already been detected.
     this->initConfigTable(contextOptions, ctxInfo, gli, shaderCaps);
diff --git a/src/gpu/gl/GrGLDefines.h b/src/gpu/gl/GrGLDefines.h
index 30d0a4c..8e7809f 100644
--- a/src/gpu/gl/GrGLDefines.h
+++ b/src/gpu/gl/GrGLDefines.h
@@ -559,7 +559,6 @@
 #define GR_GL_MAX_VERTEX_UNIFORM_COMPONENTS            0x8B4A
 #define GR_GL_MAX_SHADER_PIXEL_LOCAL_STORAGE_FAST_SIZE 0x8F63
 #define GR_GL_SHADER_BINARY_FORMATS                    0x8DF8
-#define GR_GL_NUM_SHADER_BINARY_FORMATS                0x8DF9
 
 /* StencilFunction */
 #define GR_GL_NEVER                          0x0200
@@ -852,6 +851,9 @@
 #define GR_GL_SHADER_BINARY_FORMATS          0x8DF8
 #define GR_GL_NUM_SHADER_BINARY_FORMATS      0x8DF9
 
+/* Program Binary */
+#define GR_GL_NUM_PROGRAM_BINARY_FORMATS     0x87FE
+
 /* Shader Precision-Specified Types */
 #define GR_GL_LOW_FLOAT                      0x8DF0
 #define GR_GL_MEDIUM_FLOAT                   0x8DF1
diff --git a/src/gpu/gl/builders/GrGLProgramBuilder.cpp b/src/gpu/gl/builders/GrGLProgramBuilder.cpp
index a883d7e..524ffa4 100644
--- a/src/gpu/gl/builders/GrGLProgramBuilder.cpp
+++ b/src/gpu/gl/builders/GrGLProgramBuilder.cpp
@@ -132,6 +132,37 @@
                                          *outInputs);
 }
 
+void GrGLProgramBuilder::computeCountsAndStrides(GrGLuint programID,
+                                                 const GrPrimitiveProcessor& primProc,
+                                                 bool bindAttribLocations) {
+    fVertexAttributeCnt = primProc.numVertexAttributes();
+    fInstanceAttributeCnt = primProc.numInstanceAttributes();
+    fAttributes.reset(
+            new GrGLProgram::Attribute[fVertexAttributeCnt + fInstanceAttributeCnt]);
+    auto addAttr = [&](int i, const auto& a, size_t* stride) {
+        fAttributes[i].fType = a.type();
+        fAttributes[i].fOffset = *stride;
+        *stride += a.sizeAlign4();
+        fAttributes[i].fLocation = i;
+        if (bindAttribLocations) {
+            GL_CALL(BindAttribLocation(programID, i, a.name()));
+        }
+    };
+    fVertexStride = 0;
+    int i = 0;
+    for (; i < fVertexAttributeCnt; i++) {
+        addAttr(i, primProc.vertexAttribute(i), &fVertexStride);
+        SkASSERT(fAttributes[i].fOffset == primProc.debugOnly_vertexAttributeOffset(i));
+    }
+    SkASSERT(fVertexStride == primProc.debugOnly_vertexStride());
+    fInstanceStride = 0;
+    for (int j = 0; j < fInstanceAttributeCnt; j++, ++i) {
+        addAttr(i, primProc.instanceAttribute(j), &fInstanceStride);
+        SkASSERT(fAttributes[i].fOffset == primProc.debugOnly_instanceAttributeOffset(j));
+    }
+    SkASSERT(fInstanceStride == primProc.debugOnly_instanceStride());
+}
+
 GrGLProgram* GrGLProgramBuilder::finalize() {
     TRACE_EVENT0("skia", TRACE_FUNC);
 
@@ -175,10 +206,13 @@
                               ProgramBinary(programID, binaryFormat, (void*) (bytes + offset),
                                             fCached->size() - offset));
         if (GR_GL_GET_ERROR(this->gpu()->glInterface()) == GR_GL_NO_ERROR) {
-            if (inputs.fRTHeight) {
-                this->addRTHeightUniform(SKSL_RTHEIGHT_NAME);
-            }
             cached = this->checkLinkStatus(programID);
+            if (cached) {
+                if (inputs.fRTHeight) {
+                    this->addRTHeightUniform(SKSL_RTHEIGHT_NAME);
+                }
+                this->computeCountsAndStrides(programID, primProc, false);
+            }
         } else {
             cached = false;
         }
@@ -228,30 +262,7 @@
         // NVPR actually requires a vertex shader to compile
         bool useNvpr = primProc.isPathRendering();
         if (!useNvpr) {
-            fVertexAttributeCnt = primProc.numVertexAttributes();
-            fInstanceAttributeCnt = primProc.numInstanceAttributes();
-            fAttributes.reset(
-                    new GrGLProgram::Attribute[fVertexAttributeCnt + fInstanceAttributeCnt]);
-            auto addAttr = [&](int i, const auto& a, size_t* stride) {
-                fAttributes[i].fType = a.type();
-                fAttributes[i].fOffset = *stride;
-                *stride += a.sizeAlign4();
-                fAttributes[i].fLocation = i;
-                GL_CALL(BindAttribLocation(programID, i, a.name()));
-            };
-            fVertexStride = 0;
-            int i = 0;
-            for (; i < fVertexAttributeCnt; i++) {
-                addAttr(i, primProc.vertexAttribute(i), &fVertexStride);
-                SkASSERT(fAttributes[i].fOffset == primProc.debugOnly_vertexAttributeOffset(i));
-            }
-            SkASSERT(fVertexStride == primProc.debugOnly_vertexStride());
-            fInstanceStride = 0;
-            for (int j = 0; j < fInstanceAttributeCnt; j++, ++i) {
-                addAttr(i, primProc.instanceAttribute(j), &fInstanceStride);
-                SkASSERT(fAttributes[i].fOffset == primProc.debugOnly_instanceAttributeOffset(j));
-            }
-            SkASSERT(fInstanceStride == primProc.debugOnly_instanceStride());
+            this->computeCountsAndStrides(programID, primProc, true);
         }
 
         if (primProc.willUseGeoShader()) {
diff --git a/src/gpu/gl/builders/GrGLProgramBuilder.h b/src/gpu/gl/builders/GrGLProgramBuilder.h
index dd21a6d..64e4269 100644
--- a/src/gpu/gl/builders/GrGLProgramBuilder.h
+++ b/src/gpu/gl/builders/GrGLProgramBuilder.h
@@ -62,6 +62,8 @@
                                  SkTDArray<GrGLuint>* shaderIds,
                                  const SkSL::Program::Settings& settings,
                                  SkSL::Program::Inputs* outInputs);
+    void computeCountsAndStrides(GrGLuint programID, const GrPrimitiveProcessor& primProc,
+                                 bool bindAttribLocations);
     GrGLProgram* finalize();
     void bindProgramResourceLocations(GrGLuint programID);
     bool checkLinkStatus(GrGLuint programID);