1// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/bind.h"
6
7#include "glinterfacetest.h"
8
9namespace glbench {
10
11namespace {
12
13// Basic shader code.
14const char* kVertexShader =
15    "attribute vec4 c;"
16    "void main() {"
17    "  gl_Position = c;"
18    "}";
19
20const char* kFragmentShader =
21    "uniform vec4 color;"
22    "void main() {"
23    "  gl_FragColor = color;"
24    "}";
25
26// Vertex arrays used to draw a diamond.
27const GLfloat kVertices[] = { 1.0, 0.0,
28                              0.0, -1.0,
29                              -1.0, 0.0,
30                              0.0, 1.0 };
31const GLushort kIndices[] = { 0, 1, 2,
32                              0, 2, 3 };
33
34}  // namespace
35
36void GLInterfaceTest::SetupGLRendering() {
37  vertex_buffer_object_ =
38      SetupVBO(GL_ARRAY_BUFFER, sizeof(kVertices), kVertices);
39
40  shader_program_ = InitShaderProgram(kVertexShader, kFragmentShader);
41  attribute_index_ = glGetAttribLocation(shader_program_, "c");
42  glVertexAttribPointer(attribute_index_, 2, GL_FLOAT, GL_FALSE, 0, NULL);
43  glEnableVertexAttribArray(attribute_index_);
44
45  GLint color_uniform = glGetUniformLocation(shader_program_, "color");
46
47  const GLfloat white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
48  glUniform4fv(color_uniform, 1, white);
49
50  num_indices_ = arraysize(kIndices);
51  index_buffer_object_ =
52      SetupVBO(GL_ELEMENT_ARRAY_BUFFER, sizeof(kIndices), kIndices);
53}
54
55void GLInterfaceTest::CleanupGLRendering() {
56  glDisableVertexAttribArray(attribute_index_);
57  glDeleteProgram(shader_program_);
58  glDeleteBuffers(1, &index_buffer_object_);
59  glDeleteBuffers(1, &vertex_buffer_object_);
60}
61
62bool GLInterfaceTest::Run() {
63  const std::string test_name_base = std::string(Name()) + "_";
64
65  // Run test without GL commands.
66  render_func_.Reset();
67  RunTest(this, (test_name_base + "nogl").c_str(), 1.0, g_width, g_height, false);
68
69  // Run main test with simple GL commands.
70  SetupGLRendering();
71  render_func_ = base::Bind(&GLInterfaceTest::RenderGLSimple,
72                            base::Unretained(this));
73  RunTest(this, (test_name_base + "glsimple").c_str(), 1.0, g_width, g_height, false);
74  CleanupGLRendering();
75
76  // TODO(sque): Run with complex GL commands. See crosbug.com/36746.
77  return true;
78}
79
80void GLInterfaceTest::RenderGLSimple() {
81  glDrawElements(GL_TRIANGLES, num_indices_, GL_UNSIGNED_SHORT, 0);
82}
83
84} // namespace glbench
85