[gl] Increase the color precision in TextureResize

My previous CL changed the texture data type used for resizing
to int instead of float. This caused noticable loss of color
precision in scaled textures.

Current CL deals with SwiftShader's refusal to draw into
GL_FLOAT texture in a different way - we use GL_HALF_FLOAT_OES
extension which is supported even in GLESv2; GL_HALF_FLOAT is
GLESv3-only.

Change-Id: I92688118393042bf106efa02b66a69967bf34530
diff --git a/distrib/android-emugl/host/libs/libOpenglRender/TextureResize.cpp b/distrib/android-emugl/host/libs/libOpenglRender/TextureResize.cpp
index 746b022..46cad8c 100644
--- a/distrib/android-emugl/host/libs/libOpenglRender/TextureResize.cpp
+++ b/distrib/android-emugl/host/libs/libOpenglRender/TextureResize.cpp
@@ -17,10 +17,13 @@
 #include "TextureResize.h"
 
 #include "DispatchTables.h"
-
+#include "FrameBuffer.h"
 #include "android/utils/debug.h"
 
+#include "GLES2/gl2ext.h"
+
 #include <stdio.h>
+#include <string.h>
 #include <sstream>
 #include <string>
 #include <utility>
@@ -196,6 +199,18 @@
         mFactor(1),
         mFBWidth({0,}),
         mFBHeight({0,}) {
+    // SwiftShader strictly follows the GLESv2 spec and doesn't allow rendering
+    // into full GL_FLOAT textures. Let's detect it and reduce the precision
+    // on demand. BTW, GL_HALF_FLOAT is also only present in GLESv3, so use the
+    // extension instead.
+    const char* vendor, *renderer, *version;
+    FrameBuffer::getFB()->getGLStrings(&vendor, &renderer, &version);
+    if (renderer && strstr(renderer, "SwiftShader")) {
+        mTextureDataType = GL_HALF_FLOAT_OES;
+    } else {
+        mTextureDataType = GL_FLOAT;
+    }
+
     s_gles2.glGenTextures(1, &mFBWidth.texture);
     s_gles2.glBindTexture(GL_TEXTURE_2D, mFBWidth.texture);
     s_gles2.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@@ -284,15 +299,17 @@
     // Update the framebuffer sizes to match the new factor.
     s_gles2.glBindTexture(GL_TEXTURE_2D, mFBWidth.texture);
     s_gles2.glTexImage2D(
-        GL_TEXTURE_2D, 0, GL_RGB, mWidth / factor, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
+        GL_TEXTURE_2D, 0, GL_RGB, mWidth / factor, mHeight, 0, GL_RGB,
+                mTextureDataType, nullptr);
     s_gles2.glBindTexture(GL_TEXTURE_2D, 0);
     s_gles2.glBindFramebuffer(GL_FRAMEBUFFER, mFBWidth.framebuffer);
     s_gles2.glFramebufferTexture2D(
         GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mFBWidth.texture, 0);
 
     s_gles2.glBindTexture(GL_TEXTURE_2D, mFBHeight.texture);
-    s_gles2.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, mWidth / factor, mHeight / factor, 0, GL_RGB,
-        GL_UNSIGNED_BYTE, nullptr);
+    s_gles2.glTexImage2D(
+        GL_TEXTURE_2D, 0, GL_RGB, mWidth / factor, mHeight / factor, 0, GL_RGB,
+                mTextureDataType, nullptr);
     s_gles2.glBindTexture(GL_TEXTURE_2D, 0);
     s_gles2.glBindFramebuffer(GL_FRAMEBUFFER, mFBHeight.framebuffer);
     s_gles2.glFramebufferTexture2D(
diff --git a/distrib/android-emugl/host/libs/libOpenglRender/TextureResize.h b/distrib/android-emugl/host/libs/libOpenglRender/TextureResize.h
index 3dd9d31..0fc0a82 100644
--- a/distrib/android-emugl/host/libs/libOpenglRender/TextureResize.h
+++ b/distrib/android-emugl/host/libs/libOpenglRender/TextureResize.h
@@ -46,6 +46,7 @@
     Framebuffer mFBWidth;
     Framebuffer mFBHeight;
     GLuint mVertexBuffer;
+    GLenum mTextureDataType;
 };
 
 #endif