Added window manager graphics performance tests to gl_Bench.

Changed window size to 1280x768. Added tests to verify graphics performance is sufficient for window manager. Added a test to verify that swap buffers runs at the 60hz.

To test this, I ran gl_Bench on an eee-pc.

Review URL: http://codereview.chromium.org/1282001
diff --git a/client/deps/glbench/src/main.cc b/client/deps/glbench/src/main.cc
index 08c19bd..0d42ca8 100644
--- a/client/deps/glbench/src/main.cc
+++ b/client/deps/glbench/src/main.cc
@@ -70,7 +70,6 @@
 static int arg1 = 0;
 static void *arg2 = NULL;
 
-
 void SwapTestFunc(int iter) {
   for (int i = 0 ; i < iter; ++i) {
     SwapBuffers();
@@ -81,7 +80,6 @@
   RunTest(SwapTestFunc, "us_swap_swap", 1.f, false);
 }
 
-
 void ClearTestFunc(int iter) {
   GLbitfield mask = arg1;
   glClear(mask);
@@ -577,6 +575,230 @@
   YuvToRgbShaderTestHelper(2, "yuv_shader_2");
 }
 
+static GLuint compositing_textures[5];
+static uint32_t texture_base[WINDOW_HEIGHT*WINDOW_WIDTH];
+static uint32_t texture_update[WINDOW_HEIGHT*WINDOW_WIDTH];
+static ShaderProgram compositing_background_program = 0;
+static ShaderProgram compositing_foreground_program = 0;
+
+void InitBaseTexture() {
+  for (int y = 0; y < WINDOW_HEIGHT; y++) {
+    for (int x = 0; x < WINDOW_WIDTH; x++) {
+      // This color is gray, half alpha.
+      texture_base[y*WINDOW_WIDTH+x] = 0x80808080;
+    }
+  }
+}
+
+// UpdateTexture simulates Chrome updating tab contents.
+// We cause a bunch of read and write cpu memory bandwidth.
+// It's a very rough approximation.
+void UpdateTexture() {
+  memcpy(texture_update, texture_base, sizeof(texture_base));
+}
+
+void LoadTexture() {
+  // Use GL_RGBA for compatibility with GLES2.0.
+  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
+               WINDOW_WIDTH, WINDOW_HEIGHT, 0,
+               GL_RGBA, GL_UNSIGNED_BYTE, texture_update);
+}
+
+// Test how fast we can do full-screen compositing of images
+// continuously updated from the CPU.
+// This tests both GPU compositing performance and also
+// CPU -> GPU data transfer speed.
+// It is a basic perf test to make sure we have enough performance
+// to run a compositing window manager.
+void CompositingTestFunc(int iter) {
+  for (int i = 0 ; i < iter; ++i) {
+    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+    // Draw the background
+    glDisable(GL_BLEND);
+    glDisable(GL_DEPTH_TEST);
+    // We have to blend three textures, but we use multi-texture for this
+    // blending, not fb blend, to avoid the external memory traffic
+    glActiveTexture(GL_TEXTURE0);
+    glBindTexture(GL_TEXTURE_2D, compositing_textures[0]);
+    glActiveTexture(GL_TEXTURE1);
+    glBindTexture(GL_TEXTURE_2D, compositing_textures[1]);
+    glActiveTexture(GL_TEXTURE2);
+    glBindTexture(GL_TEXTURE_2D, compositing_textures[2]);
+    // Set up the texture coordinate arrays
+    glClientActiveTexture(GL_TEXTURE0);
+    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+    glClientActiveTexture(GL_TEXTURE1);
+    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+    glClientActiveTexture(GL_TEXTURE2);
+    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+    // Use the right shader
+    glUseProgram(compositing_background_program);
+    // Draw the quad
+    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+
+    // Set up one texture coordinate array
+    glClientActiveTexture(GL_TEXTURE0);
+    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+    glClientActiveTexture(GL_TEXTURE1);
+    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+    glClientActiveTexture(GL_TEXTURE2);
+    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+    // Use the right shader
+    glUseProgram(compositing_foreground_program);
+
+    // Compositing is blending, so we shall blend.
+    glEnable(GL_BLEND);
+    // Depth test is on for window occlusion
+    glEnable(GL_DEPTH_TEST);
+
+    // Draw window number one
+    // This update acts like a chrome webkit sw rendering update.
+    glActiveTexture(GL_TEXTURE0);
+    glBindTexture(GL_TEXTURE_2D, compositing_textures[3]);
+    UpdateTexture();
+    // TODO(papakipos): this LoadTexture is likely doing more CPU memory copies
+    // than we would like.
+    LoadTexture();
+    // TODO(papakipos): add color interpolation here, and modulate
+    // texture against it.
+    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+
+    // Draw window number two
+    // This is a static window, so we don't update it.
+    glActiveTexture(GL_TEXTURE0);
+    glBindTexture(GL_TEXTURE_2D, compositing_textures[4]);
+    // TODO(papakipos): add color interpolation here, and modulate
+    // texture against it.
+    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+  }
+}
+
+void InitializeCompositing() {
+  InitBaseTexture();
+
+  glClearColor(0.f, 0.f, 0.f, 0.f);
+  glDisable(GL_DEPTH_TEST);
+  glDisable(GL_BLEND);
+  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+  glDepthFunc(GL_LEQUAL);
+
+  glGenTextures(5, compositing_textures);
+  glActiveTexture(GL_TEXTURE0);
+  for (int i = 0; i < 5; i++) {
+    glBindTexture(GL_TEXTURE_2D, compositing_textures[i]);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
+                    GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
+                    GL_LINEAR);
+  }
+
+  glColor4f(1.f, 1.f, 1.f, 1.f);
+
+  // Set up the vertex arrays for drawing textured quads later on.
+  glEnableClientState(GL_VERTEX_ARRAY);
+  GLfloat buffer_vertex[8] = {
+    -1.f, -1.f,
+    1.f,  -1.f,
+    -1.f, 1.f,
+    1.f,  1.f,
+  };
+  GLuint vbo_vertex = SetupVBO(GL_ARRAY_BUFFER,
+                               sizeof(buffer_vertex), buffer_vertex);
+  if (!vbo_vertex)
+    printf("# Not Using VBO!\n");
+  glVertexPointer(2, GL_FLOAT, 0, vbo_vertex ? 0 : buffer_vertex);
+
+  GLfloat buffer_texture[8] = {
+    0.f, 0.f,
+    1.f, 0.f,
+    0.f, 1.f,
+    1.f, 1.f,
+  };
+  GLuint vbo_texture = SetupVBO(GL_ARRAY_BUFFER,
+                                sizeof(buffer_texture), buffer_texture);
+  for (int i = 0; i < 3; i++) {
+    glClientActiveTexture(GL_TEXTURE0 + i);
+    glTexCoordPointer(2, GL_FLOAT, 0, vbo_texture ? 0 : buffer_texture);
+    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+  }
+
+  // Set up the static background textures.
+  UpdateTexture();
+  UpdateTexture();
+  UpdateTexture();
+  // Load these textures into bound texture ids and keep using them
+  // from there to avoid having to reload this texture every frame
+  glActiveTexture(GL_TEXTURE0);
+  glBindTexture(GL_TEXTURE_2D, compositing_textures[0]);
+  LoadTexture();
+  glActiveTexture(GL_TEXTURE1);
+  glBindTexture(GL_TEXTURE_2D, compositing_textures[1]);
+  LoadTexture();
+  glActiveTexture(GL_TEXTURE2);
+  glBindTexture(GL_TEXTURE_2D, compositing_textures[2]);
+  LoadTexture();
+
+  glActiveTexture(GL_TEXTURE0);
+  glBindTexture(GL_TEXTURE_2D, compositing_textures[3]);
+  UpdateTexture();
+  LoadTexture();
+
+  glActiveTexture(GL_TEXTURE0);
+  glBindTexture(GL_TEXTURE_2D, compositing_textures[4]);
+  UpdateTexture();
+  LoadTexture();
+
+  // Set up vertex & fragment shaders.
+  compositing_background_program =
+      TripleTextureBlendShaderProgram(vbo_vertex,
+                                      vbo_texture, vbo_texture, vbo_texture);
+  compositing_foreground_program =
+      BasicTextureShaderProgram(vbo_vertex, vbo_texture);
+  if ((!compositing_background_program) ||
+      (!compositing_foreground_program)) {
+    printf("# Could not set up compositing shader.\n");
+  }
+
+  if (!vbo_vertex)
+    printf("# Not Using VBO!\n");
+  glVertexPointer(2, GL_FLOAT, 0, vbo_vertex ? 0 : buffer_vertex);
+}
+
+void TeardownCompositing() {
+  glDeleteProgram(compositing_background_program);
+  glDeleteProgram(compositing_foreground_program);
+}
+
+// Notes on the window manager compositing test:
+// Depth
+//      Depth complexity = 3: background, active window, static window
+//      Background: may be a tex-blend of three images (2.5d effect)
+// The windows -- at most two, fullscreen
+//      Depth test is on, passing most of the time.
+//      A lot of texture min-filtering -- not modelled
+//      One of the two windows is getting live browser frame updates -- not mod
+//          The live window runs at x/2 and y/2 size -- not modelled
+//      The two windows are modulated by color interpolation to get gradient
+static float screen_scale_factor = (1e6f*
+                                    (WINDOW_WIDTH*WINDOW_HEIGHT)/
+                                    (1280.f*768));
+
+void WindowManagerCompositingTest() {
+  InitializeCompositing();
+  RunTest(CompositingTestFunc, "1280x768_fps_compositing",
+          screen_scale_factor, true);
+  TeardownCompositing();
+}
+
+void NoFillWindowManagerCompositingTest() {
+  glScissor(0, 0, 1, 1);
+  glEnable(GL_SCISSOR_TEST);
+  InitializeCompositing();
+  RunTest(CompositingTestFunc, "1280x768_fps_no_fill_compositing",
+          screen_scale_factor, true);
+  TeardownCompositing();
+}
 
 // TODO: get resources file from a command line option or something.
 // TODO: use proper command line parsing library.
@@ -620,6 +842,8 @@
     VaryingsAndDdxyShaderTest,
     YuvToRgbShaderTest1,
     YuvToRgbShaderTest2,
+    NoFillWindowManagerCompositingTest,
+    WindowManagerCompositingTest,
   };
 
   uint64_t done = GetUTime() + 1000000ULL * seconds_to_run;
diff --git a/client/deps/glbench/src/main.h b/client/deps/glbench/src/main.h
index 10c1dea..ffa73a3 100644
--- a/client/deps/glbench/src/main.h
+++ b/client/deps/glbench/src/main.h
@@ -66,6 +66,11 @@
 
 uint64_t TimeBench(BenchFunc func, int iter);
 
+// This size is for a window that is very large but will fit on all
+// the displays we care about.
+#define WINDOW_WIDTH 512
+#define WINDOW_HEIGHT 512
+
 #define MAX_ITERATION_DURATION_MS 100000
 // Runs func passing it sequential powers of two (8, 16, 32,...) recording time
 // it took.  The data is then fitted linearly, obtaining slope and bias such
diff --git a/client/deps/glbench/src/shaders.cc b/client/deps/glbench/src/shaders.cc
index 983c57c..b414e77 100644
--- a/client/deps/glbench/src/shaders.cc
+++ b/client/deps/glbench/src/shaders.cc
@@ -80,7 +80,6 @@
   return program;
 }
 
-
 #define I915_WORKAROUND 1
 
 #if I915_WORKAROUND
@@ -107,6 +106,175 @@
 #define DDY "ddy"
 #endif
 
+const char *basic_texture_vertex_shader =
+"attribute vec4 c1;"
+"attribute vec4 c2;"
+"varying vec2 v1;"
+"void main() {"
+"    gl_Position = c1;"
+"    " V1 " = c2;"
+"}";
+
+const char *basic_texture_fragment_shader =
+"uniform sampler2D texture_sampler;"
+"varying vec2 v1;"
+"void main() {"
+"    gl_FragColor = texture2D(texture_sampler, " V1 ".xy);"
+"}";
+
+ShaderProgram BasicTextureShaderProgram(GLuint vertex_buffer,
+                                        GLuint texture_buffer) {
+  ShaderProgram program =
+    InitShaderProgram(basic_texture_vertex_shader,
+                      basic_texture_fragment_shader);
+
+  // Set up the texture sampler
+  int textureSampler = glGetUniformLocation(program, "texture_sampler");
+  glUniform1i(textureSampler, 0);
+
+  // Set up vertex attribute
+  int attribute_index = glGetAttribLocation(program, "c1");
+  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
+  glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL);
+  glEnableVertexAttribArray(attribute_index);
+
+  // Set up texture attribute
+  attribute_index = glGetAttribLocation(program, "c2");
+  glBindBuffer(GL_ARRAY_BUFFER, texture_buffer);
+  glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL);
+  glEnableVertexAttribArray(attribute_index);
+
+  return program;
+}
+
+const char *double_texture_blend_vertex_shader =
+"attribute vec4 c1;"
+"attribute vec4 c2;"
+"attribute vec4 c3;"
+"varying vec2 v1;"
+"varying vec2 v2;"
+"void main() {"
+"    gl_Position = c1;"
+"    " V1 " = c2;"
+"    " V2 " = c3;"
+"}";
+
+const char *double_texture_blend_fragment_shader =
+"uniform sampler2D texture_sampler_0;"
+"uniform sampler2D texture_sampler_1;"
+"varying vec2 v1;"
+"varying vec2 v2;"
+"void main() {"
+"    vec4 one = texture2D(texture_sampler_0, " V1 ".xy);"
+"    vec4 two = texture2D(texture_sampler_1, " V2 ".xy);"
+"    gl_FragColor = mix(one, two, 0.5);"
+"}";
+
+// This shader blends the three textures
+ShaderProgram DoubleTextureBlendShaderProgram(GLuint vertex_buffer,
+                                              GLuint texture_buffer_0,
+                                              GLuint texture_buffer_1) {
+  ShaderProgram program =
+    InitShaderProgram(double_texture_blend_vertex_shader,
+                      double_texture_blend_fragment_shader);
+
+  // Set up the texture sampler
+  int textureSampler0 = glGetUniformLocation(program, "texture_sampler_0");
+  glUniform1i(textureSampler0, 0);
+  int textureSampler1 = glGetUniformLocation(program, "texture_sampler_1");
+  glUniform1i(textureSampler1, 1);
+
+  // Set up vertex attribute
+  int attribute_index = glGetAttribLocation(program, "c1");
+  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
+  glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL);
+  glEnableVertexAttribArray(attribute_index);
+
+  // Set up texture attributes
+  attribute_index = glGetAttribLocation(program, "c2");
+  glBindBuffer(GL_ARRAY_BUFFER, texture_buffer_0);
+  glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL);
+  glEnableVertexAttribArray(attribute_index);
+
+  attribute_index = glGetAttribLocation(program, "c3");
+  glBindBuffer(GL_ARRAY_BUFFER, texture_buffer_1);
+  glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL);
+  glEnableVertexAttribArray(attribute_index);
+
+  return program;
+}
+
+const char *triple_texture_blend_vertex_shader =
+"attribute vec4 c1;"
+"attribute vec4 c2;"
+"attribute vec4 c3;"
+"attribute vec4 c4;"
+"varying vec2 v1;"
+"varying vec2 v2;"
+"varying vec2 v3;"
+"void main() {"
+"    gl_Position = c1;"
+"    " V1 " = c2;"
+"    " V2 " = c3;"
+"    " V3 " = c4;"
+"}";
+
+const char *triple_texture_blend_fragment_shader =
+"uniform sampler2D texture_sampler_0;"
+"uniform sampler2D texture_sampler_1;"
+"uniform sampler2D texture_sampler_2;"
+"varying vec2 v1;"
+"varying vec2 v2;"
+"varying vec2 v3;"
+"void main() {"
+"    vec4 one = texture2D(texture_sampler_0, " V1 ".xy);"
+"    vec4 two = texture2D(texture_sampler_1, " V2 ".xy);"
+"    vec4 three = texture2D(texture_sampler_2, " V3 ".xy);"
+"    gl_FragColor = mix(mix(one, two, 0.5), three, 0.5);"
+"}";
+
+// This shader blends the three textures
+ShaderProgram TripleTextureBlendShaderProgram(GLuint vertex_buffer,
+                                              GLuint texture_buffer_0,
+                                              GLuint texture_buffer_1,
+                                              GLuint texture_buffer_2) {
+  ShaderProgram program =
+    InitShaderProgram(triple_texture_blend_vertex_shader,
+                      triple_texture_blend_fragment_shader);
+
+  // Set up the texture sampler
+  int textureSampler0 = glGetUniformLocation(program, "texture_sampler_0");
+  glUniform1i(textureSampler0, 0);
+  int textureSampler1 = glGetUniformLocation(program, "texture_sampler_1");
+  glUniform1i(textureSampler1, 1);
+  int textureSampler2 = glGetUniformLocation(program, "texture_sampler_2");
+  glUniform1i(textureSampler2, 2);
+
+  // Set up vertex attribute
+  int attribute_index = glGetAttribLocation(program, "c1");
+  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
+  glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL);
+  glEnableVertexAttribArray(attribute_index);
+
+  // Set up texture attributes
+  attribute_index = glGetAttribLocation(program, "c2");
+  glBindBuffer(GL_ARRAY_BUFFER, texture_buffer_0);
+  glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL);
+  glEnableVertexAttribArray(attribute_index);
+
+  attribute_index = glGetAttribLocation(program, "c3");
+  glBindBuffer(GL_ARRAY_BUFFER, texture_buffer_1);
+  glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL);
+  glEnableVertexAttribArray(attribute_index);
+
+  attribute_index = glGetAttribLocation(program, "c4");
+  glBindBuffer(GL_ARRAY_BUFFER, texture_buffer_2);
+  glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL);
+  glEnableVertexAttribArray(attribute_index);
+
+  return program;
+}
+
 const char *vertex_shader_1_varying =
 "attribute vec4 c;"
 "varying vec4 v1;"
diff --git a/client/deps/glbench/src/shaders.h b/client/deps/glbench/src/shaders.h
index af4f3eb..8258d4e 100644
--- a/client/deps/glbench/src/shaders.h
+++ b/client/deps/glbench/src/shaders.h
@@ -16,5 +16,14 @@
 ShaderProgram DdxDdyShaderProgram(bool ddx, GLuint vertex_buffer);
 ShaderProgram YuvToRgbShaderProgram(int type, GLuint vertex_buffer,
                                     int width, int height);
+ShaderProgram BasicTextureShaderProgram(GLuint vertex_buffer,
+                                        GLuint texture_buffer);
+ShaderProgram DoubleTextureBlendShaderProgram(GLuint vertex_buffer,
+                                              GLuint texture_buffer_0,
+                                              GLuint texture_buffer_1);
+ShaderProgram TripleTextureBlendShaderProgram(GLuint vertex_buffer,
+                                              GLuint texture_buffer_0,
+                                              GLuint texture_buffer_1,
+                                              GLuint texture_buffer_2);
 
 #endif // BENCH_GL_SHADERS_H_
diff --git a/client/deps/glbench/src/xlib_window.cc b/client/deps/glbench/src/xlib_window.cc
index 0958b5c..e4bad92 100644
--- a/client/deps/glbench/src/xlib_window.cc
+++ b/client/deps/glbench/src/xlib_window.cc
@@ -10,8 +10,8 @@
 Display *g_xlib_display = NULL;
 Window g_xlib_window = 0;
 
-GLint g_width = 512;
-GLint g_height = 512;
+GLint g_width = WINDOW_WIDTH;
+GLint g_height = WINDOW_HEIGHT;
 bool g_override_redirect = true;