1// Copyright 2014 The Chromium 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 <GLES2/gl2.h>
6#include <GLES2/gl2ext.h>
7
8#include "gpu/command_buffer/tests/gl_manager.h"
9#include "gpu/command_buffer/tests/gl_test_utils.h"
10#include "testing/gmock/include/gmock/gmock.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13
14#define SHADER(Src) #Src
15
16namespace gpu {
17
18class GLStreamDrawTest : public testing::Test {
19 protected:
20  static const int kSize = 4;
21
22  virtual void SetUp() {
23    GLManager::Options options;
24    options.size = gfx::Size(kSize, kSize);
25    gl_.Initialize(options);
26  }
27
28  virtual void TearDown() {
29    gl_.Destroy();
30  }
31
32  GLManager gl_;
33};
34
35namespace {
36
37GLuint SetupProgram() {
38  static const char* v_shader_str = SHADER(
39      attribute vec4 a_position;
40      attribute vec4 a_color;
41      varying vec4 v_color;
42      void main()
43      {
44         gl_Position = a_position;
45         v_color = a_color;
46      }
47   );
48
49  static const char* f_shader_str = SHADER(
50      precision mediump float;
51      varying vec4 v_color;
52      void main()
53      {
54        gl_FragColor = v_color;
55      }
56  );
57
58  GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
59  glUseProgram(program);
60  return program;
61}
62
63}  // anonymous namespace.
64
65TEST_F(GLStreamDrawTest, Basic) {
66  static GLfloat float_red[4] = { 1.0f, 0.0f, 0.0f, 1.0f, };
67  static GLfloat float_green[4] = { 0.0f, 1.0f, 0.0f, 1.0f, };
68  static uint8 expected_red[4] =  {255, 0, 0, 255, };
69  static uint8 expected_green[4] =  {0, 255, 0, 255, };
70
71  GLuint program = SetupProgram();
72  GLuint position_loc = glGetAttribLocation(program, "a_position");
73  GLuint color_loc = glGetAttribLocation(program, "a_color");
74  GLTestHelper::SetupUnitQuad(position_loc);
75  GLTestHelper::SetupColorsForUnitQuad(color_loc, float_red, GL_STREAM_DRAW);
76  glDrawArrays(GL_TRIANGLES, 0, 6);
77  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_red));
78  GLTestHelper::SetupColorsForUnitQuad(color_loc, float_green, GL_STATIC_DRAW);
79  glDrawArrays(GL_TRIANGLES, 0, 6);
80  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_green));
81
82  GLTestHelper::CheckGLError("no errors", __LINE__);
83}
84
85// http://crbug.com/281565
86#if !defined(OS_ANDROID)
87TEST_F(GLStreamDrawTest, DrawElements) {
88  static GLfloat float_red[4] = { 1.0f, 0.0f, 0.0f, 1.0f, };
89  static GLfloat float_green[4] = { 0.0f, 1.0f, 0.0f, 1.0f, };
90  static uint8 expected_red[4] =  {255, 0, 0, 255, };
91  static uint8 expected_green[4] =  {0, 255, 0, 255, };
92
93  GLuint program = SetupProgram();
94  GLuint position_loc = glGetAttribLocation(program, "a_position");
95  GLuint color_loc = glGetAttribLocation(program, "a_color");
96  GLTestHelper::SetupUnitQuad(position_loc);
97  GLTestHelper::SetupColorsForUnitQuad(color_loc, float_red, GL_STREAM_DRAW);
98
99  GLuint index_buffer = 0;
100  glGenBuffers(1, &index_buffer);
101  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
102  static GLubyte indices[] = { 0, 1, 2, 3, 4, 5, };
103  glBufferData(
104      GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STREAM_DRAW);
105  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, NULL);
106  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_red));
107  GLTestHelper::SetupColorsForUnitQuad(color_loc, float_green, GL_STATIC_DRAW);
108
109  glBufferData(
110      GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
111  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, NULL);
112  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_green));
113
114  GLTestHelper::CheckGLError("no errors", __LINE__);
115}
116#endif
117
118TEST_F(GLStreamDrawTest, VertexArrayObjects) {
119  if (!GLTestHelper::HasExtension("GL_OES_vertex_array_object")) {
120    return;
121  }
122
123  static GLfloat float_red[4] = { 1.0f, 0.0f, 0.0f, 1.0f, };
124  static GLfloat float_green[4] = { 0.0f, 1.0f, 0.0f, 1.0f, };
125  static uint8 expected_red[4] =  {255, 0, 0, 255, };
126  static uint8 expected_green[4] =  {0, 255, 0, 255, };
127
128  GLuint program = SetupProgram();
129  GLuint position_loc = glGetAttribLocation(program, "a_position");
130  GLuint color_loc = glGetAttribLocation(program, "a_color");
131
132  GLuint vaos[2];
133  glGenVertexArraysOES(2, vaos);
134
135  glBindVertexArrayOES(vaos[0]);
136  GLuint position_buffer = GLTestHelper::SetupUnitQuad(position_loc);
137  GLTestHelper::SetupColorsForUnitQuad(color_loc, float_red, GL_STREAM_DRAW);
138
139  glBindVertexArrayOES(vaos[1]);
140  glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
141  glEnableVertexAttribArray(position_loc);
142  glVertexAttribPointer(position_loc, 2, GL_FLOAT, GL_FALSE, 0, 0);
143  GLTestHelper::SetupColorsForUnitQuad(color_loc, float_green, GL_STATIC_DRAW);
144
145  for (int ii = 0; ii < 2; ++ii) {
146    glBindVertexArrayOES(vaos[0]);
147    glDrawArrays(GL_TRIANGLES, 0, 6);
148    EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_red));
149
150    glBindVertexArrayOES(vaos[1]);
151    glDrawArrays(GL_TRIANGLES, 0, 6);
152    EXPECT_TRUE(
153        GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_green));
154  }
155
156  GLTestHelper::CheckGLError("no errors", __LINE__);
157}
158
159}  // namespace gpu
160
161