| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gl/test/gl_test_helper.h" | 5 #include "ui/gl/test/gl_test_helper.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace gl { | 12 namespace gl { |
| 13 | |
| 14 // static | 13 // static |
| 15 GLuint GLTestHelper::CreateTexture(GLenum target) { | 14 GLuint GLTestHelper::CreateTexture(GLenum target) { |
| 16 // Create the texture object. | 15 // Create the texture object. |
| 17 GLuint texture = 0; | 16 GLuint texture = 0; |
| 18 glGenTextures(1, &texture); | 17 glGenTextures(1, &texture); |
| 19 glBindTexture(target, texture); | 18 glBindTexture(target, texture); |
| 20 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 19 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 21 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 20 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 22 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 21 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 23 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 22 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 24 return texture; | 23 return texture; |
| 25 } | 24 } |
| 26 | 25 |
| 27 // static | 26 // static |
| 28 GLuint GLTestHelper::CompileShader(GLenum type, const char* src) { | |
| 29 GLuint shader = glCreateShader(type); | |
| 30 // Load the shader source. | |
| 31 glShaderSource(shader, 1, &src, nullptr); | |
| 32 // Compile the shader. | |
| 33 glCompileShader(shader); | |
| 34 return shader; | |
| 35 } | |
| 36 | |
| 37 // static | |
| 38 GLuint GLTestHelper::LoadShader(GLenum type, const char* src) { | |
| 39 GLuint shader = CompileShader(type, src); | |
| 40 | |
| 41 // Check the compile status. | |
| 42 GLint value = 0; | |
| 43 glGetShaderiv(shader, GL_COMPILE_STATUS, &value); | |
| 44 if (!value) { | |
| 45 char buffer[1024]; | |
| 46 GLsizei length = 0; | |
| 47 glGetShaderInfoLog(shader, sizeof(buffer), &length, buffer); | |
| 48 std::string log(buffer, length); | |
| 49 EXPECT_EQ(1, value) << "Error compiling shader: " << log; | |
| 50 glDeleteShader(shader); | |
| 51 shader = 0; | |
| 52 } | |
| 53 return shader; | |
| 54 } | |
| 55 | |
| 56 // static | |
| 57 GLuint GLTestHelper::LinkProgram(GLuint vertex_shader, GLuint fragment_shader) { | |
| 58 // Create the program object. | |
| 59 GLuint program = glCreateProgram(); | |
| 60 glAttachShader(program, vertex_shader); | |
| 61 glAttachShader(program, fragment_shader); | |
| 62 // Link the program. | |
| 63 glLinkProgram(program); | |
| 64 return program; | |
| 65 } | |
| 66 | |
| 67 // static | |
| 68 GLuint GLTestHelper::SetupProgram(GLuint vertex_shader, | |
| 69 GLuint fragment_shader) { | |
| 70 GLuint program = LinkProgram(vertex_shader, fragment_shader); | |
| 71 // Check the link status. | |
| 72 GLint linked = 0; | |
| 73 glGetProgramiv(program, GL_LINK_STATUS, &linked); | |
| 74 if (!linked) { | |
| 75 char buffer[1024]; | |
| 76 GLsizei length = 0; | |
| 77 glGetProgramInfoLog(program, sizeof(buffer), &length, buffer); | |
| 78 std::string log(buffer, length); | |
| 79 EXPECT_EQ(1, linked) << "Error linking program: " << log; | |
| 80 glDeleteProgram(program); | |
| 81 program = 0; | |
| 82 } | |
| 83 return program; | |
| 84 } | |
| 85 | |
| 86 // static | |
| 87 GLuint GLTestHelper::SetupFramebuffer(int width, int height) { | 27 GLuint GLTestHelper::SetupFramebuffer(int width, int height) { |
| 88 GLuint color_buffer_texture = CreateTexture(GL_TEXTURE_2D); | 28 GLuint color_buffer_texture = CreateTexture(GL_TEXTURE_2D); |
| 89 glBindTexture(GL_TEXTURE_2D, color_buffer_texture); | 29 glBindTexture(GL_TEXTURE_2D, color_buffer_texture); |
| 90 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, | 30 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, |
| 91 GL_UNSIGNED_BYTE, nullptr); | 31 GL_UNSIGNED_BYTE, nullptr); |
| 92 GLuint framebuffer = 0; | 32 GLuint framebuffer = 0; |
| 93 glGenFramebuffersEXT(1, &framebuffer); | 33 glGenFramebuffersEXT(1, &framebuffer); |
| 94 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer); | 34 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer); |
| 95 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, | 35 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 96 color_buffer_texture, 0); | 36 color_buffer_texture, 0); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 if (bad_count > 16) | 72 if (bad_count > 16) |
| 133 return false; | 73 return false; |
| 134 } | 74 } |
| 135 } | 75 } |
| 136 } | 76 } |
| 137 | 77 |
| 138 return !bad_count; | 78 return !bad_count; |
| 139 } | 79 } |
| 140 | 80 |
| 141 } // namespace gl | 81 } // namespace gl |
| OLD | NEW |