Windows should animate when they are about to get docked at screen edges.
[chromium-blink-merge.git] / gpu / command_buffer / tests / gl_stream_draw_unittests.cc
blob3997016903cdd55d2e4c938f0f84c7baa96ea7db
1 // Copyright (c) 2013 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 virtual void SetUp() {
23 GLManager::Options options;
24 options.size = gfx::Size(kSize, kSize);
25 gl_.Initialize(options);
28 virtual void TearDown() {
29 gl_.Destroy();
32 GLManager gl_;
35 namespace {
37 GLuint 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()
44 gl_Position = a_position;
45 v_color = a_color;
49 static const char* f_shader_str = SHADER(
50 precision mediump float;
51 varying vec4 v_color;
52 void main()
54 gl_FragColor = v_color;
58 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
59 glUseProgram(program);
60 return program;
63 } // anonymous namespace.
65 TEST_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, };
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));
82 GLTestHelper::CheckGLError("no errors", __LINE__);
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__);
115 TEST_F(GLStreamDrawTest, VertexArrayObjects) {
116 if (!GLTestHelper::HasExtension("GL_OES_vertex_array_object")) {
117 return;
120 static GLfloat float_red[4] = { 1.0f, 0.0f, 0.0f, 1.0f, };
121 static GLfloat float_green[4] = { 0.0f, 1.0f, 0.0f, 1.0f, };
122 static uint8 expected_red[4] = {255, 0, 0, 255, };
123 static uint8 expected_green[4] = {0, 255, 0, 255, };
125 GLuint program = SetupProgram();
126 GLuint position_loc = glGetAttribLocation(program, "a_position");
127 GLuint color_loc = glGetAttribLocation(program, "a_color");
129 GLuint vaos[2];
130 glGenVertexArraysOES(2, vaos);
132 glBindVertexArrayOES(vaos[0]);
133 GLuint position_buffer = GLTestHelper::SetupUnitQuad(position_loc);
134 GLTestHelper::SetupColorsForUnitQuad(color_loc, float_red, GL_STREAM_DRAW);
136 glBindVertexArrayOES(vaos[1]);
137 glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
138 glEnableVertexAttribArray(position_loc);
139 glVertexAttribPointer(position_loc, 2, GL_FLOAT, GL_FALSE, 0, 0);
140 GLTestHelper::SetupColorsForUnitQuad(color_loc, float_green, GL_STATIC_DRAW);
142 for (int ii = 0; ii < 2; ++ii) {
143 glBindVertexArrayOES(vaos[0]);
144 glDrawArrays(GL_TRIANGLES, 0, 6);
145 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_red));
147 glBindVertexArrayOES(vaos[1]);
148 glDrawArrays(GL_TRIANGLES, 0, 6);
149 EXPECT_TRUE(
150 GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_green));
153 GLTestHelper::CheckGLError("no errors", __LINE__);
156 } // namespace gpu