glbench: add texture reuse test

This test uploads a few textures for the first few draws and then
reuses them for subsequent draws.

BUG=chromium-os:36745
TEST=emerge autotest-deps-glbench, run glbench.

Change-Id: Icc0c821836373e2a03008b7fb1ad672c7fee8fa5
Signed-off-by: Simon Que <sque@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/40280
Reviewed-by: Stuart Abercrombie <sabercrombie@chromium.org>
diff --git a/client/deps/glbench/src/Makefile b/client/deps/glbench/src/Makefile
index b93ea59..b75b04b 100644
--- a/client/deps/glbench/src/Makefile
+++ b/client/deps/glbench/src/Makefile
@@ -6,8 +6,8 @@
 SOURCES_GL_BENCH = main.cc swaptest.cc yuvtest.cc testbase.cc
 SOURCES_GL_BENCH += readpixeltest.cc
 SOURCES_GL_BENCH += attributefetchtest.cc varyingsandddxytest.cc cleartest.cc
-SOURCES_GL_BENCH += texturetest.cc textureupdatetest.cc textureuploadtest.cc
-SOURCES_GL_BENCH += trianglesetuptest.cc fillratetest.cc
+SOURCES_GL_BENCH += texturetest.cc texturereusetest.cc textureupdatetest.cc
+SOURCES_GL_BENCH += textureuploadtest.cc trianglesetuptest.cc fillratetest.cc
 SOURCES_GL_BENCH += windowmanagercompositingtest.cc
 SOURCES_GL_BENCH += md5.cc png_helper.cc
 
diff --git a/client/deps/glbench/src/all_tests.h b/client/deps/glbench/src/all_tests.h
index 1495d6e..913ae86 100644
--- a/client/deps/glbench/src/all_tests.h
+++ b/client/deps/glbench/src/all_tests.h
@@ -14,6 +14,7 @@
 TestBase* GetAttributeFetchShaderTest();
 TestBase* GetVaryingsAndDdxyShaderTest();
 TestBase* GetWindowManagerCompositingTest(bool scissor);
+TestBase* GetTextureReuseTest();
 TestBase* GetTextureUpdateTest();
 TestBase* GetTextureUploadTest();
 
diff --git a/client/deps/glbench/src/main.cc b/client/deps/glbench/src/main.cc
index e37fce5..cc6a50a 100644
--- a/client/deps/glbench/src/main.cc
+++ b/client/deps/glbench/src/main.cc
@@ -89,6 +89,7 @@
     glbench::GetReadPixelTest(),
     glbench::GetAttributeFetchShaderTest(),
     glbench::GetVaryingsAndDdxyShaderTest(),
+    glbench::GetTextureReuseTest(),
     glbench::GetTextureUpdateTest(),
     glbench::GetTextureUploadTest(),
   };
diff --git a/client/deps/glbench/src/texturereusetest.cc b/client/deps/glbench/src/texturereusetest.cc
new file mode 100644
index 0000000..39bf5fa
--- /dev/null
+++ b/client/deps/glbench/src/texturereusetest.cc
@@ -0,0 +1,62 @@
+// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This test evaluates the speed of using the same textures to draw repeatedly.
+// It uploads a series of textures initially.  On subsequent iterations, it uses
+// those uploaded textures to draw.
+
+#include "base/logging.h"
+
+#include "texturetest.h"
+#include "main.h"
+
+namespace glbench {
+
+class TextureReuseTest : public TextureTest {
+ public:
+  TextureReuseTest() {}
+  virtual ~TextureReuseTest() {}
+  virtual bool TestFunc(int iter);
+  virtual const char* Name() const { return "texture_reuse"; }
+};
+
+bool TextureReuseTest::TestFunc(int iter) {
+  glGetError();
+
+  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+  glFlush();
+
+  for (int i = 0; i < iter - 1; ++i) {
+    glBindTexture(GL_TEXTURE_2D, textures_[i % kNumberOfTextures]);
+    switch (flavor_) {
+      case TEX_IMAGE:
+        glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width_, height_,
+                     0, GL_LUMINANCE, GL_UNSIGNED_BYTE,
+                     pixels_[i % kNumberOfTextures].get());
+        break;
+      case TEX_SUBIMAGE:
+        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_,
+                        GL_LUMINANCE, GL_UNSIGNED_BYTE,
+                        pixels_[i % kNumberOfTextures].get());
+        break;
+    }
+
+    // After having uploaded |kNumberOfTextures| textures, use each of them to
+    // draw once before uploading new textures.
+    if ((i % kNumberOfTextures) == (kNumberOfTextures - 1)) {
+      for (int j = 0; j < kNumberOfTextures; ++j) {
+        glBindTexture(GL_TEXTURE_2D, textures_[j]);
+        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+      }
+    }
+  }
+
+  return true;
+}
+
+TestBase* GetTextureReuseTest() {
+  return new TextureReuseTest;
+}
+
+} // namespace glbench
diff --git a/client/site_tests/graphics_GLBench/graphics_GLBench.py b/client/site_tests/graphics_GLBench/graphics_GLBench.py
index 82d0677..074d500 100644
--- a/client/site_tests/graphics_GLBench/graphics_GLBench.py
+++ b/client/site_tests/graphics_GLBench/graphics_GLBench.py
@@ -54,6 +54,22 @@
                            'mpixels_sec_pixel_read',
                            'mpixels_sec_pixel_read_2',
                            'mpixels_sec_pixel_read_3',
+                           'mtexel_sec_texture_reuse_teximage2d_1024',
+                           'mtexel_sec_texture_reuse_teximage2d_128',
+                           'mtexel_sec_texture_reuse_teximage2d_1536',
+                           'mtexel_sec_texture_reuse_teximage2d_2048',
+                           'mtexel_sec_texture_reuse_teximage2d_256',
+                           'mtexel_sec_texture_reuse_teximage2d_32',
+                           'mtexel_sec_texture_reuse_teximage2d_512',
+                           'mtexel_sec_texture_reuse_teximage2d_768',
+                           'mtexel_sec_texture_reuse_texsubimage2d_1024',
+                           'mtexel_sec_texture_reuse_texsubimage2d_128',
+                           'mtexel_sec_texture_reuse_texsubimage2d_1536',
+                           'mtexel_sec_texture_reuse_texsubimage2d_2048',
+                           'mtexel_sec_texture_reuse_texsubimage2d_256',
+                           'mtexel_sec_texture_reuse_texsubimage2d_32',
+                           'mtexel_sec_texture_reuse_texsubimage2d_512',
+                           'mtexel_sec_texture_reuse_texsubimage2d_768',
                            'mtexel_sec_texture_upload_teximage2d_1024',
                            'mtexel_sec_texture_upload_teximage2d_128',
                            'mtexel_sec_texture_upload_teximage2d_1536',