Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/gl/test/gl_test_helper.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace gfx { | |
| 13 | |
| 14 // static | |
| 15 GLuint GLTestHelper::CreateTexture(GLenum target) { | |
| 16 // Create the texture object. | |
| 17 GLuint texture = 0; | |
| 18 glGenTextures(1, &texture); | |
| 19 glBindTexture(target, texture); | |
| 20 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 21 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 22 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 23 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 24 return texture; | |
| 25 } | |
| 26 | |
| 27 // 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. | |
|
Daniele Castagna
2015/09/30 19:17:36
Nit: not sure this comment adds anything.
reveman
2015/10/01 04:01:36
I'll keep it the same as gl_test_utils.cc for now.
| |
| 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) { | |
| 88 GLuint color_buffer_texture = CreateTexture(GL_TEXTURE_2D); | |
| 89 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, | |
| 90 GL_UNSIGNED_BYTE, nullptr); | |
| 91 GLuint framebuffer = 0; | |
| 92 glGenFramebuffersEXT(1, &framebuffer); | |
| 93 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer); | |
| 94 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, | |
| 95 color_buffer_texture, 0); | |
| 96 if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { | |
| 97 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), | |
| 98 glCheckFramebufferStatusEXT(GL_FRAMEBUFFER)) | |
| 99 << "Error setting up framebuffer"; | |
| 100 glDeleteFramebuffersEXT(1, &framebuffer); | |
| 101 framebuffer = 0; | |
| 102 } | |
| 103 return framebuffer; | |
| 104 } | |
| 105 | |
| 106 // static | |
| 107 bool GLTestHelper::CheckPixels(int x, | |
| 108 int y, | |
| 109 int width, | |
| 110 int height, | |
| 111 const uint8_t expected_color[4]) { | |
| 112 int size = width * height * 4; | |
| 113 scoped_ptr<uint8_t[]> pixels(new uint8_t[size]); | |
|
Daniele Castagna
2015/09/30 19:17:36
Why not std::vector<uint8_t> pixels(size, kCheckCl
reveman
2015/10/01 04:01:36
Mostly because this comes from gpu/command_buffer/
| |
| 114 const uint8_t kCheckClearValue = 123u; | |
| 115 memset(pixels.get(), kCheckClearValue, size); | |
| 116 glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.get()); | |
| 117 int bad_count = 0; | |
| 118 for (int yy = 0; yy < height; ++yy) { | |
| 119 for (int xx = 0; xx < width; ++xx) { | |
| 120 int offset = yy * width * 4 + xx * 4; | |
| 121 for (int jj = 0; jj < 4; ++jj) { | |
| 122 uint8_t actual = pixels[offset + jj]; | |
| 123 uint8_t expected = expected_color[jj]; | |
| 124 EXPECT_EQ(expected, actual) << " at " << (xx + x) << ", " << (yy + y) | |
| 125 << " channel " << jj; | |
| 126 bad_count += actual != expected; | |
| 127 // Exit early just so we don't spam the log but we print enough to | |
|
Daniele Castagna
2015/09/30 19:17:36
FYI: if you EXPECT_EQ with two std::arrays as para
| |
| 128 // hopefully make it easy to diagnose the issue. | |
| 129 if (bad_count > 16) | |
| 130 return false; | |
| 131 } | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 return !bad_count; | |
| 136 } | |
| 137 | |
| 138 } // namespace gfx | |
| OLD | NEW |