Reland "Pin the GLSL version to be no larger than the GL version"

Updated to fix a bug with the different version numbering between
GL and GLSL.

This reverts commit 0042cb0c87fbf9826c8ba6bcb2bb5030ff68d5e8.

Change-Id: I5ae9bb77bd46eac47b05e340b491f8a8671ef5a4
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/238057
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/src/gpu/gl/GrGLGLSL.cpp b/src/gpu/gl/GrGLGLSL.cpp
index 78a04a8..4ba3149 100644
--- a/src/gpu/gl/GrGLGLSL.cpp
+++ b/src/gpu/gl/GrGLGLSL.cpp
@@ -14,6 +14,20 @@
     if (GR_GLSL_INVALID_VER == ver) {
         return false;
     }
+
+    // Workaround for a bug on some Adreno 308 devices with Android 9. The driver reports a GL
+    // version of 3.0, and a GLSL version of 3.1. If we use version 310 shaders, the driver reports
+    // that it's not supported. To keep things simple, we pin the GLSL version to the GL version.
+    // Note that GLSL versions have an extra digit on their minor level, so we have to scale up
+    // the GL version's minor revision to get a comparable GLSL version. This logic can easily
+    // create invalid GLSL versions (older GL didn't keep the versions in sync), but the checks
+    // below will further pin the GLSL generation correctly.
+    // https://github.com/flutter/flutter/issues/36130
+    GrGLVersion glVer = GrGLGetVersion(gl);
+    uint32_t glMajor = GR_GL_MAJOR_VER(glVer),
+             glMinor = GR_GL_MINOR_VER(glVer);
+    ver = SkTMin(ver, GR_GLSL_VER(glMajor, 10 * glMinor));
+
     if (GR_IS_GR_GL(gl->fStandard)) {
         SkASSERT(ver >= GR_GLSL_VER(1,10));
         if (ver >= GR_GLSL_VER(4,20)) {
diff --git a/src/gpu/gl/GrGLUtil.h b/src/gpu/gl/GrGLUtil.h
index d17c028..18b7b33 100644
--- a/src/gpu/gl/GrGLUtil.h
+++ b/src/gpu/gl/GrGLUtil.h
@@ -30,6 +30,9 @@
                                                (static_cast<uint64_t>(minor) << 16) | \
                                                 static_cast<uint64_t>(point))
 
+#define GR_GL_MAJOR_VER(version) (static_cast<uint32_t>(version) >> 16)
+#define GR_GL_MINOR_VER(version) (static_cast<uint32_t>(version) & 0xFFFF)
+
 #define GR_GL_INVALID_VER GR_GL_VER(0, 0)
 #define GR_GLSL_INVALID_VER GR_GLSL_VER(0, 0)
 #define GR_GL_DRIVER_UNKNOWN_VER GR_GL_DRIVER_VER(0, 0, 0)