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. | |
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 glBindTexture(GL_TEXTURE_2D, color_buffer_texture); | |
90 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, | |
91 GL_UNSIGNED_BYTE, nullptr); | |
92 GLuint framebuffer = 0; | |
93 glGenFramebuffersEXT(1, &framebuffer); | |
94 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer); | |
95 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, | |
96 color_buffer_texture, 0); | |
97 if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { | |
98 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), | |
99 glCheckFramebufferStatusEXT(GL_FRAMEBUFFER)) | |
100 << "Error setting up framebuffer"; | |
101 glDeleteFramebuffersEXT(1, &framebuffer); | |
102 framebuffer = 0; | |
103 } | |
104 glBindFramebufferEXT(GL_FRAMEBUFFER, 0); | |
105 glDeleteTextures(1, &color_buffer_texture); | |
106 return framebuffer; | |
107 } | |
108 | |
109 // static | |
110 bool GLTestHelper::CheckPixels(int x, | |
111 int y, | |
112 int width, | |
113 int height, | |
114 const uint8_t expected_color[4]) { | |
115 int size = width * height * 4; | |
116 scoped_ptr<uint8_t[]> pixels(new uint8_t[size]); | |
117 const uint8_t kCheckClearValue = 123u; | |
118 memset(pixels.get(), kCheckClearValue, size); | |
119 glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.get()); | |
120 int bad_count = 0; | |
121 for (int yy = 0; yy < height; ++yy) { | |
122 for (int xx = 0; xx < width; ++xx) { | |
123 int offset = yy * width * 4 + xx * 4; | |
124 for (int jj = 0; jj < 4; ++jj) { | |
125 uint8_t actual = pixels[offset + jj]; | |
126 uint8_t expected = expected_color[jj]; | |
127 EXPECT_EQ(expected, actual) << " at " << (xx + x) << ", " << (yy + y) | |
128 << " channel " << jj; | |
129 bad_count += actual != expected; | |
130 // Exit early just so we don't spam the log but we print enough to | |
131 // hopefully make it easy to diagnose the issue. | |
132 if (bad_count > 16) | |
133 return false; | |
134 } | |
135 } | |
136 } | |
137 | |
138 return !bad_count; | |
139 } | |
140 | |
141 } // namespace gfx | |
OLD | NEW |