1 // Copyright (c) 2012 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 16 namespace gpu { 17 18 class GLVirtualContextsTest : public testing::Test { 19 protected: 20 static const int kSize0 = 4; 21 static const int kSize1 = 8; 22 static const int kSize2 = 16; 23 24 virtual void SetUp() { 25 GLManager::Options options; 26 options.size = gfx::Size(kSize0, kSize0); 27 gl_real_.Initialize(options); 28 gl_real_shared_.Initialize(options); 29 options.virtual_manager = &gl_real_shared_; 30 options.size = gfx::Size(kSize1, kSize1); 31 gl1_.Initialize(options); 32 options.size = gfx::Size(kSize2, kSize2); 33 gl2_.Initialize(options); 34 } 35 36 virtual void TearDown() { 37 gl1_.Destroy(); 38 gl2_.Destroy(); 39 gl_real_shared_.Destroy(); 40 gl_real_.Destroy(); 41 } 42 43 GLManager gl_real_; 44 GLManager gl_real_shared_; 45 GLManager gl1_; 46 GLManager gl2_; 47 }; 48 49 namespace { 50 51 #if !defined(OS_ANDROID) 52 void SetupSimpleShader(const uint8* color) { 53 static const char* v_shader_str = SHADER( 54 attribute vec4 a_Position; 55 void main() 56 { 57 gl_Position = a_Position; 58 } 59 ); 60 61 static const char* f_shader_str = SHADER( 62 precision mediump float; 63 uniform vec4 u_color; 64 void main() 65 { 66 gl_FragColor = u_color; 67 } 68 ); 69 70 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str); 71 glUseProgram(program); 72 73 GLuint position_loc = glGetAttribLocation(program, "a_Position"); 74 75 GLTestHelper::SetupUnitQuad(position_loc); 76 77 GLuint color_loc = glGetUniformLocation(program, "u_color"); 78 glUniform4f( 79 color_loc, 80 color[0] / 255.0f, 81 color[1] / 255.0f, 82 color[2] / 255.0f, 83 color[3] / 255.0f); 84 } 85 86 void TestDraw(int size) { 87 uint8 expected_clear[] = { 127, 0, 255, 0, }; 88 glClearColor(0.5f, 0.0f, 1.0f, 0.0f); 89 glClear(GL_COLOR_BUFFER_BIT); 90 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, size, size, 1, expected_clear)); 91 glDrawArrays(GL_TRIANGLES, 0, 6); 92 } 93 94 #endif // !defined(OS_ANDROID) 95 96 } // anonymous namespace 97 98 // http://crbug.com/281565 99 #if !defined(OS_ANDROID) 100 TEST_F(GLVirtualContextsTest, Basic) { 101 struct TestInfo { 102 int size; 103 uint8 color[4]; 104 GLManager* manager; 105 }; 106 const int kNumTests = 3; 107 TestInfo tests[] = { 108 { kSize0, { 255, 0, 0, 0, }, &gl_real_, }, 109 { kSize1, { 0, 255, 0, 0, }, &gl1_, }, 110 { kSize2, { 0, 0, 255, 0, }, &gl2_, }, 111 }; 112 113 for (int ii = 0; ii < kNumTests; ++ii) { 114 const TestInfo& test = tests[ii]; 115 GLManager* gl_manager = test.manager; 116 gl_manager->MakeCurrent(); 117 SetupSimpleShader(test.color); 118 } 119 120 for (int ii = 0; ii < kNumTests; ++ii) { 121 const TestInfo& test = tests[ii]; 122 GLManager* gl_manager = test.manager; 123 gl_manager->MakeCurrent(); 124 TestDraw(test.size); 125 } 126 127 for (int ii = 0; ii < kNumTests; ++ii) { 128 const TestInfo& test = tests[ii]; 129 GLManager* gl_manager = test.manager; 130 gl_manager->MakeCurrent(); 131 EXPECT_TRUE(GLTestHelper::CheckPixels( 132 0, 0, test.size, test.size, 0, test.color)); 133 } 134 135 for (int ii = 0; ii < kNumTests; ++ii) { 136 const TestInfo& test = tests[ii]; 137 GLManager* gl_manager = test.manager; 138 gl_manager->MakeCurrent(); 139 GLTestHelper::CheckGLError("no errors", __LINE__); 140 } 141 } 142 #endif 143 144 } // namespace gpu 145 146