1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include "gpu/command_buffer/tests/gl_manager.h"
#include "gpu/command_buffer/tests/gl_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#define SHADER(Src) #Src
namespace gpu {
class GLVirtualContextsTest : public testing::Test {
protected:
static const int kSize0 = 4;
static const int kSize1 = 8;
static const int kSize2 = 16;
static const GLfloat kFloatRed[4];
static const GLfloat kFloatGreen[4];
static const uint8 kExpectedRed[4];
static const uint8 kExpectedGreen[4];
virtual void SetUp() {
GLManager::Options options;
options.size = gfx::Size(kSize0, kSize0);
gl_real_.Initialize(options);
gl_real_shared_.Initialize(options);
options.virtual_manager = &gl_real_shared_;
options.size = gfx::Size(kSize1, kSize1);
gl1_.Initialize(options);
options.size = gfx::Size(kSize2, kSize2);
gl2_.Initialize(options);
}
virtual void TearDown() {
gl1_.Destroy();
gl2_.Destroy();
gl_real_shared_.Destroy();
gl_real_.Destroy();
}
GLuint SetupColoredVertexProgram() {
static const char* v_shader_str = SHADER(
attribute vec4 a_position;
attribute vec4 a_color;
varying vec4 v_color;
void main()
{
gl_Position = a_position;
v_color = a_color;
}
);
static const char* f_shader_str = SHADER(
precision mediump float;
varying vec4 v_color;
void main()
{
gl_FragColor = v_color;
}
);
GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
glUseProgram(program);
return program;
}
void SetUpColoredUnitQuad(const GLfloat* color) {
GLuint program1 = SetupColoredVertexProgram();
GLuint position_loc1 = glGetAttribLocation(program1, "a_position");
GLuint color_loc1 = glGetAttribLocation(program1, "a_color");
GLTestHelper::SetupUnitQuad(position_loc1);
GLTestHelper::SetupColorsForUnitQuad(color_loc1, color, GL_STATIC_DRAW);
}
GLManager gl_real_;
GLManager gl_real_shared_;
GLManager gl1_;
GLManager gl2_;
};
const GLfloat GLVirtualContextsTest::kFloatRed[4] = {
1.0f, 0.0f, 0.0f, 1.0f,
};
const GLfloat GLVirtualContextsTest::kFloatGreen[4] = {
0.0f, 1.0f, 0.0f, 1.0f,
};
const uint8 GLVirtualContextsTest::kExpectedRed[4] = {
255, 0, 0, 255,
};
const uint8 GLVirtualContextsTest::kExpectedGreen[4] = {
0, 255, 0, 255,
};
namespace {
void SetupSimpleShader(const uint8* color) {
static const char* v_shader_str = SHADER(
attribute vec4 a_Position;
void main()
{
gl_Position = a_Position;
}
);
static const char* f_shader_str = SHADER(
precision mediump float;
uniform vec4 u_color;
void main()
{
gl_FragColor = u_color;
}
);
GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
glUseProgram(program);
GLuint position_loc = glGetAttribLocation(program, "a_Position");
GLTestHelper::SetupUnitQuad(position_loc);
GLuint color_loc = glGetUniformLocation(program, "u_color");
glUniform4f(
color_loc,
color[0] / 255.0f,
color[1] / 255.0f,
color[2] / 255.0f,
color[3] / 255.0f);
}
void TestDraw(int size) {
uint8 expected_clear[] = { 127, 0, 255, 0, };
glClearColor(0.5f, 0.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, size, size, 1, expected_clear));
glDrawArrays(GL_TRIANGLES, 0, 6);
}
} // anonymous namespace
// http://crbug.com/281565
TEST_F(GLVirtualContextsTest, Basic) {
struct TestInfo {
int size;
uint8 color[4];
GLManager* manager;
};
const int kNumTests = 3;
TestInfo tests[] = {
{ kSize0, { 255, 0, 0, 0, }, &gl_real_, },
{ kSize1, { 0, 255, 0, 0, }, &gl1_, },
{ kSize2, { 0, 0, 255, 0, }, &gl2_, },
};
for (int ii = 0; ii < kNumTests; ++ii) {
const TestInfo& test = tests[ii];
GLManager* gl_manager = test.manager;
gl_manager->MakeCurrent();
SetupSimpleShader(test.color);
}
for (int ii = 0; ii < kNumTests; ++ii) {
const TestInfo& test = tests[ii];
GLManager* gl_manager = test.manager;
gl_manager->MakeCurrent();
TestDraw(test.size);
}
for (int ii = 0; ii < kNumTests; ++ii) {
const TestInfo& test = tests[ii];
GLManager* gl_manager = test.manager;
gl_manager->MakeCurrent();
EXPECT_TRUE(GLTestHelper::CheckPixels(
0, 0, test.size, test.size, 0, test.color));
}
for (int ii = 0; ii < kNumTests; ++ii) {
const TestInfo& test = tests[ii];
GLManager* gl_manager = test.manager;
gl_manager->MakeCurrent();
GLTestHelper::CheckGLError("no errors", __LINE__);
}
}
// http://crbug.com/363407
TEST_F(GLVirtualContextsTest, VertexArrayObjectRestore) {
GLuint vao1 = 0, vao2 = 0;
gl1_.MakeCurrent();
// Set up red quad in vao1.
glGenVertexArraysOES(1, &vao1);
glBindVertexArrayOES(vao1);
SetUpColoredUnitQuad(kFloatRed);
glFinish();
gl2_.MakeCurrent();
// Set up green quad in vao2.
glGenVertexArraysOES(1, &vao2);
glBindVertexArrayOES(vao2);
SetUpColoredUnitQuad(kFloatGreen);
glFinish();
gl1_.MakeCurrent();
// Test to ensure that vao1 is still the active VAO for this context.
glDrawArrays(GL_TRIANGLES, 0, 6);
EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize1, kSize1, 0, kExpectedRed));
glFinish();
GLTestHelper::CheckGLError("no errors", __LINE__);
gl2_.MakeCurrent();
// Test to ensure that vao2 is still the active VAO for this context.
glDrawArrays(GL_TRIANGLES, 0, 6);
EXPECT_TRUE(
GLTestHelper::CheckPixels(0, 0, kSize2, kSize2, 0, kExpectedGreen));
glFinish();
GLTestHelper::CheckGLError("no errors", __LINE__);
}
// http://crbug.com/363407
TEST_F(GLVirtualContextsTest, VertexArrayObjectRestoreRebind) {
GLuint vao1 = 0, vao2 = 0;
gl1_.MakeCurrent();
// Set up red quad in vao1.
glGenVertexArraysOES(1, &vao1);
glBindVertexArrayOES(vao1);
SetUpColoredUnitQuad(kFloatRed);
glFinish();
gl2_.MakeCurrent();
// Set up green quad in new vao2.
glGenVertexArraysOES(1, &vao2);
glBindVertexArrayOES(vao2);
SetUpColoredUnitQuad(kFloatGreen);
glFinish();
gl1_.MakeCurrent();
// Test to ensure that vao1 hasn't been corrupted after rebinding.
// Bind 0 is required so that bind vao1 is not optimized away in the service.
glBindVertexArrayOES(0);
glBindVertexArrayOES(vao1);
glDrawArrays(GL_TRIANGLES, 0, 6);
EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize1, kSize1, 0, kExpectedRed));
glFinish();
GLTestHelper::CheckGLError("no errors", __LINE__);
gl2_.MakeCurrent();
// Test to ensure that vao1 hasn't been corrupted after rebinding.
// Bind 0 is required so that bind vao2 is not optimized away in the service.
glBindVertexArrayOES(0);
glBindVertexArrayOES(vao2);
glDrawArrays(GL_TRIANGLES, 0, 6);
EXPECT_TRUE(
GLTestHelper::CheckPixels(0, 0, kSize2, kSize2, 0, kExpectedGreen));
glFinish();
GLTestHelper::CheckGLError("no errors", __LINE__);
}
// http://crbug.com/363407
TEST_F(GLVirtualContextsTest, VertexArrayObjectRestoreDefault) {
gl1_.MakeCurrent();
// Set up red quad in default VAO.
SetUpColoredUnitQuad(kFloatRed);
glFinish();
gl2_.MakeCurrent();
// Set up green quad in default VAO.
SetUpColoredUnitQuad(kFloatGreen);
glFinish();
// Gen & bind a non-default VAO.
GLuint vao;
glGenVertexArraysOES(1, &vao);
glBindVertexArrayOES(vao);
glFinish();
gl1_.MakeCurrent();
// Test to ensure that default VAO on gl1_ is still valid.
glDrawArrays(GL_TRIANGLES, 0, 6);
EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize1, kSize1, 0, kExpectedRed));
glFinish();
gl2_.MakeCurrent();
// Test to ensure that default VAO on gl2_ is still valid.
// This tests that a default VAO is restored even when it's not currently
// bound during the context switch.
glBindVertexArrayOES(0);
glDrawArrays(GL_TRIANGLES, 0, 6);
EXPECT_TRUE(
GLTestHelper::CheckPixels(0, 0, kSize2, kSize2, 0, kExpectedGreen));
glFinish();
GLTestHelper::CheckGLError("no errors", __LINE__);
}
} // namespace gpu
|