Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / gpu / command_buffer / tests / gl_stream_draw_unittest.cc
blob90968c8762d78f9282c18ee8345d25a080b4d2af
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.
5 #include <GLES2/gl2.h>
6 #include <GLES2/gl2ext.h>
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"
14 #define SHADER(Src) #Src
16 namespace gpu {
18 class GLStreamDrawTest : public testing::Test {
19 protected:
20 static const int kSize = 4;
22 void SetUp() override {
23 GLManager::Options options;
24 options.size = gfx::Size(kSize, kSize);
25 gl_.Initialize(options);
28 void TearDown() override { gl_.Destroy(); }
30 GLManager gl_;
33 namespace {
35 GLuint SetupProgram() {
36 static const char* v_shader_str = SHADER(
37 attribute vec4 a_position;
38 attribute vec4 a_color;
39 varying vec4 v_color;
40 void main()
42 gl_Position = a_position;
43 v_color = a_color;
47 static const char* f_shader_str = SHADER(
48 precision mediump float;
49 varying vec4 v_color;
50 void main()
52 gl_FragColor = v_color;
56 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
57 glUseProgram(program);
58 return program;
61 } // anonymous namespace.
63 TEST_F(GLStreamDrawTest, Basic) {
64 static GLfloat float_red[4] = { 1.0f, 0.0f, 0.0f, 1.0f, };
65 static GLfloat float_green[4] = { 0.0f, 1.0f, 0.0f, 1.0f, };
66 static uint8 expected_red[4] = {255, 0, 0, 255, };
67 static uint8 expected_green[4] = {0, 255, 0, 255, };
69 GLuint program = SetupProgram();
70 GLuint position_loc = glGetAttribLocation(program, "a_position");
71 GLuint color_loc = glGetAttribLocation(program, "a_color");
72 GLTestHelper::SetupUnitQuad(position_loc);
73 GLTestHelper::SetupColorsForUnitQuad(color_loc, float_red, GL_STREAM_DRAW);
74 glDrawArrays(GL_TRIANGLES, 0, 6);
75 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_red));
76 GLTestHelper::SetupColorsForUnitQuad(color_loc, float_green, GL_STATIC_DRAW);
77 glDrawArrays(GL_TRIANGLES, 0, 6);
78 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_green));
80 GLTestHelper::CheckGLError("no errors", __LINE__);
83 // http://crbug.com/281565
84 #if !defined(OS_ANDROID)
85 TEST_F(GLStreamDrawTest, DrawElements) {
86 static GLfloat float_red[4] = { 1.0f, 0.0f, 0.0f, 1.0f, };
87 static GLfloat float_green[4] = { 0.0f, 1.0f, 0.0f, 1.0f, };
88 static uint8 expected_red[4] = {255, 0, 0, 255, };
89 static uint8 expected_green[4] = {0, 255, 0, 255, };
91 GLuint program = SetupProgram();
92 GLuint position_loc = glGetAttribLocation(program, "a_position");
93 GLuint color_loc = glGetAttribLocation(program, "a_color");
94 GLTestHelper::SetupUnitQuad(position_loc);
95 GLTestHelper::SetupColorsForUnitQuad(color_loc, float_red, GL_STREAM_DRAW);
97 GLuint index_buffer = 0;
98 glGenBuffers(1, &index_buffer);
99 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
100 static GLubyte indices[] = { 0, 1, 2, 3, 4, 5, };
101 glBufferData(
102 GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STREAM_DRAW);
103 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, NULL);
104 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_red));
105 GLTestHelper::SetupColorsForUnitQuad(color_loc, float_green, GL_STATIC_DRAW);
107 glBufferData(
108 GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
109 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, NULL);
110 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_green));
112 GLTestHelper::CheckGLError("no errors", __LINE__);
114 #endif
116 TEST_F(GLStreamDrawTest, VertexArrayObjects) {
117 if (!GLTestHelper::HasExtension("GL_OES_vertex_array_object")) {
118 return;
121 static GLfloat float_red[4] = { 1.0f, 0.0f, 0.0f, 1.0f, };
122 static GLfloat float_green[4] = { 0.0f, 1.0f, 0.0f, 1.0f, };
123 static uint8 expected_red[4] = {255, 0, 0, 255, };
124 static uint8 expected_green[4] = {0, 255, 0, 255, };
126 GLuint program = SetupProgram();
127 GLuint position_loc = glGetAttribLocation(program, "a_position");
128 GLuint color_loc = glGetAttribLocation(program, "a_color");
130 GLuint vaos[2];
131 glGenVertexArraysOES(2, vaos);
133 glBindVertexArrayOES(vaos[0]);
134 GLuint position_buffer = GLTestHelper::SetupUnitQuad(position_loc);
135 GLTestHelper::SetupColorsForUnitQuad(color_loc, float_red, GL_STREAM_DRAW);
137 glBindVertexArrayOES(vaos[1]);
138 glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
139 glEnableVertexAttribArray(position_loc);
140 glVertexAttribPointer(position_loc, 2, GL_FLOAT, GL_FALSE, 0, 0);
141 GLTestHelper::SetupColorsForUnitQuad(color_loc, float_green, GL_STATIC_DRAW);
143 for (int ii = 0; ii < 2; ++ii) {
144 glBindVertexArrayOES(vaos[0]);
145 glDrawArrays(GL_TRIANGLES, 0, 6);
146 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_red));
148 glBindVertexArrayOES(vaos[1]);
149 glDrawArrays(GL_TRIANGLES, 0, 6);
150 EXPECT_TRUE(
151 GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_green));
154 GLTestHelper::CheckGLError("no errors", __LINE__);
157 } // namespace gpu