Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: gpu/command_buffer/tests/gl_apply_screen_space_antialiasing_CHROMIUM_unittest.cc

Issue 2432413003: gpu, cmaa: add glApplyScreenSpaceAntialiasingCHROMIUM unittests (Closed)
Patch Set: resolve reviewer's concerns Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016 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 #ifndef GL_GLEXT_PROTOTYPES
6 #define GL_GLEXT_PROTOTYPES
7 #endif
8
9 #include <GLES2/gl2.h>
10 #include <GLES2/gl2ext.h>
11 #include <GLES2/gl2extchromium.h>
12 #include <stddef.h>
13 #include <stdint.h>
14
15 #include "base/command_line.h"
16 #include "gpu/command_buffer/tests/gl_manager.h"
17 #include "gpu/command_buffer/tests/gl_test_utils.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/gl/gl_switches.h"
21
22 namespace gpu {
23
24 // A collection of tests that exercise the GL_CHROMIUM_copy_texture extension.
25 class GLApplyScreenSpaceAntialiasingCHROMIUMTest : public testing::Test {
26 protected:
27 void CreateAndBindDestinationTextureAndFBO(GLenum target) {
28 glGenTextures(1, &textures_);
29 glBindTexture(target, textures_);
30
31 // Some drivers (NVidia/SGX) require texture settings to be a certain way or
32 // they won't report FRAMEBUFFER_COMPLETE.
33 glTexParameterf(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
34 glTexParameterf(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
35 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
36 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
37
38 glGenFramebuffers(1, &framebuffer_id_);
39 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_);
40 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target,
41 textures_, 0);
42 }
43
44 void SetUp() override {
45 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
46 GLManager::Options options;
47 gl_.InitializeWithCommandLine(options, command_line);
48
49 available_ =
50 GLTestHelper::HasExtension("GL_CHROMIUM_screen_space_antialiasing");
51 if (!available_) {
52 LOG(INFO) << "GL_CHROMIUM_screen_space_antialiasing not supported. "
53 "Skipping test...";
54 return;
55 }
56
57 CreateAndBindDestinationTextureAndFBO(GL_TEXTURE_2D);
58 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
59 nullptr);
60 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
61 glCheckFramebufferStatus(GL_FRAMEBUFFER));
62
63 glClearColor(0, 1, 0, 1);
64 glClear(GL_COLOR_BUFFER_BIT);
65 }
66
67 void TearDown() override {
68 glDeleteTextures(1, &textures_);
69 glDeleteFramebuffers(1, &framebuffer_id_);
70 gl_.Destroy();
71 }
72
73 GLManager gl_;
74 GLuint textures_ = 0;
75 GLuint framebuffer_id_ = 0;
76 bool available_ = false;
77 };
78
79 // Test to ensure that the basic functionality of the extension works.
80 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, Basic) {
81 if (!available_)
82 return;
83
84 glApplyScreenSpaceAntialiasingCHROMIUM();
85 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
86
87 // Check the FB is still bound.
88 GLint value = 0;
89 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &value);
90 GLuint fb_id = value;
91 EXPECT_EQ(framebuffer_id_, fb_id);
92
93 // Check that FB is complete.
94 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
95 glCheckFramebufferStatus(GL_FRAMEBUFFER));
96
97 uint8_t pixels[1 * 4] = {0u, 255u, 0u, 255u};
98 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, pixels);
99 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
100 }
101
102 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, DefaultFBO) {
103 if (!available_)
104 return;
105
106 glBindFramebuffer(GL_FRAMEBUFFER, 0);
107 glApplyScreenSpaceAntialiasingCHROMIUM();
108 EXPECT_EQ(static_cast<GLenum>(GL_INVALID_OPERATION), glGetError());
109 }
110
111 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, InternalFormat) {
112 if (!available_)
113 return;
114
115 GLint formats[] = {GL_RGB, GL_RGBA};
116 for (size_t index = 0; index < arraysize(formats); index++) {
117 glTexImage2D(GL_TEXTURE_2D, 0, formats[index], 1, 1, 0, formats[index],
118 GL_UNSIGNED_BYTE, nullptr);
119 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
120
121 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
122 textures_, 0);
123
124 glClear(GL_COLOR_BUFFER_BIT);
125
126 glApplyScreenSpaceAntialiasingCHROMIUM();
127 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()) << "index:"
128 << index;
129
130 uint8_t pixels[1 * 4] = {0u, 255u, 0u, 255u};
131 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, pixels);
132 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
133 }
134 }
135
136 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, InternalFormatNotSupported) {
137 if (!available_)
138 return;
139
140 // Check unsupported format reports error.
141 GLint unsupported_formats[] = {GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA};
142 for (size_t index = 0; index < arraysize(unsupported_formats); index++) {
143 glTexImage2D(GL_TEXTURE_2D, 0, unsupported_formats[index], 1, 1, 0,
144 unsupported_formats[index], GL_UNSIGNED_BYTE, nullptr);
145 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
146
147 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
148 textures_, 0);
149
150 glClear(GL_COLOR_BUFFER_BIT);
151
152 glApplyScreenSpaceAntialiasingCHROMIUM();
153 EXPECT_EQ(static_cast<GLenum>(GL_INVALID_FRAMEBUFFER_OPERATION),
154 glGetError())
155 << "index:" << index;
156 }
157 }
158
159 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, ImmutableTexture) {
160 if (!available_)
161 return;
162
163 if (!GLTestHelper::HasExtension("GL_EXT_texture_storage")) {
164 LOG(INFO) << "GL_EXT_texture_storage not supported. Skipping test...";
165 return;
166 }
167 GLenum internal_formats[] = {GL_RGB8_OES, GL_RGBA8_OES, GL_BGRA8_EXT};
168 for (auto internal_format : internal_formats) {
169 glDeleteTextures(1, &textures_);
170 glDeleteFramebuffers(1, &framebuffer_id_);
171 CreateAndBindDestinationTextureAndFBO(GL_TEXTURE_2D);
172 glTexStorage2DEXT(GL_TEXTURE_2D, 1, internal_format, 1, 1);
173 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
174 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
175 glCheckFramebufferStatus(GL_FRAMEBUFFER));
176
177 glClear(GL_COLOR_BUFFER_BIT);
178
179 glApplyScreenSpaceAntialiasingCHROMIUM();
180 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
181
182 uint8_t pixels[1 * 4] = {0u, 255u, 0u, 255u};
183 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, pixels);
184 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
185 }
186 }
187
188 // Similar to webgl conformance test 'testAntialias(true)' in
189 // context-attributes-alpha-depth-stencil-antialias.html
190 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, AntiAliasing) {
191 if (!available_)
192 return;
193
194 // Fill colors in the FBO as follows
195 // +-----+
196 // |R|R| |
197 // +-----+
198 // |R| | |
199 // +-----+
200 // | | | |
201 // +-+-+-+
202 const int length = 3;
203 uint8_t rgba_pixels[4 * length * length] = {
204 255u, 0u, 0u, 255u, 255u, 0u, 0u, 255u, 0u, 0u, 0u, 0u,
205 255u, 0u, 0u, 255u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
206 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
207 };
208 glBindTexture(GL_TEXTURE_2D, textures_);
209 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, length, length, 0, GL_RGBA,
210 GL_UNSIGNED_BYTE, rgba_pixels);
211
212 uint8_t transparent[4] = {0u, 0u, 0u, 0u};
213 uint8_t red[4] = {255u, 0u, 0u, 255u};
214 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, red);
215 GLTestHelper::CheckPixels(0, 1, 1, 1, 0, red);
216 GLTestHelper::CheckPixels(0, 2, 1, 1, 0, transparent);
217 GLTestHelper::CheckPixels(1, 0, 1, 1, 0, red);
218 GLTestHelper::CheckPixels(1, 1, 1, 1, 0, transparent);
219 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
220
221 glApplyScreenSpaceAntialiasingCHROMIUM();
222 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
223
224 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, red);
225 GLTestHelper::CheckPixels(2, 2, 1, 1, 0, transparent);
226 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
227
228 // Check if middle pixel is anti-aliased.
229 uint8_t pixels[4] = {0u};
230 glReadPixels(1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixels);
231 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
232 EXPECT_NE(transparent, pixels);
233 EXPECT_NE(red, pixels);
234 EXPECT_TRUE(pixels[0] > transparent[0] && pixels[0] < red[0]);
235 EXPECT_EQ(0u, pixels[1]);
236 EXPECT_EQ(0u, pixels[2]);
237 EXPECT_TRUE(pixels[3] > transparent[3] && pixels[3] < red[3]);
238 }
239
240 namespace {
241
242 void glEnableDisable(GLint param, GLboolean value) {
243 if (value)
244 glEnable(param);
245 else
246 glDisable(param);
247 }
248
249 } // unnamed namespace
250
251 // Validate that some basic GL state is not touched upon execution of
252 // the extension.
253 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, BasicStatePreservation) {
254 if (!available_)
255 return;
256
257 GLboolean reference_settings[2] = {GL_TRUE, GL_FALSE};
258 for (int x = 0; x < 2; ++x) {
259 GLboolean setting = reference_settings[x];
260 glEnableDisable(GL_DEPTH_TEST, setting);
261 glEnableDisable(GL_SCISSOR_TEST, setting);
262 glEnableDisable(GL_STENCIL_TEST, setting);
263 glEnableDisable(GL_CULL_FACE, setting);
264 glEnableDisable(GL_BLEND, setting);
265 glColorMask(setting, setting, setting, setting);
266 glDepthMask(setting);
267
268 glActiveTexture(GL_TEXTURE1 + x);
269
270 glApplyScreenSpaceAntialiasingCHROMIUM();
271 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
272
273 EXPECT_EQ(setting, glIsEnabled(GL_DEPTH_TEST));
274 EXPECT_EQ(setting, glIsEnabled(GL_SCISSOR_TEST));
275 EXPECT_EQ(setting, glIsEnabled(GL_STENCIL_TEST));
276 EXPECT_EQ(setting, glIsEnabled(GL_CULL_FACE));
277 EXPECT_EQ(setting, glIsEnabled(GL_BLEND));
278
279 GLboolean bool_array[4] = {GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE};
280 glGetBooleanv(GL_DEPTH_WRITEMASK, bool_array);
281 EXPECT_EQ(setting, bool_array[0]);
282
283 bool_array[0] = GL_FALSE;
284 glGetBooleanv(GL_COLOR_WRITEMASK, bool_array);
285 EXPECT_EQ(setting, bool_array[0]);
286 EXPECT_EQ(setting, bool_array[1]);
287 EXPECT_EQ(setting, bool_array[2]);
288 EXPECT_EQ(setting, bool_array[3]);
289
290 GLint active_texture = 0;
291 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
292 EXPECT_EQ(GL_TEXTURE1 + x, active_texture);
293 }
294
295 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
296 };
297
298 // Verify that invocation of the extension does not modify the bound
299 // texture state.
300 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, TextureStatePreserved) {
301 if (!available_)
302 return;
303
304 GLuint texture_ids[2];
305 glGenTextures(2, texture_ids);
306
307 glActiveTexture(GL_TEXTURE0);
308 glBindTexture(GL_TEXTURE_2D, texture_ids[0]);
309
310 glActiveTexture(GL_TEXTURE1);
311 glBindTexture(GL_TEXTURE_2D, texture_ids[1]);
312
313 glApplyScreenSpaceAntialiasingCHROMIUM();
314 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
315
316 GLint active_texture = 0;
317 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
318 EXPECT_EQ(GL_TEXTURE1, active_texture);
319
320 GLint bound_texture = 0;
321 glGetIntegerv(GL_TEXTURE_BINDING_2D, &bound_texture);
322 EXPECT_EQ(texture_ids[1], static_cast<GLuint>(bound_texture));
323 glBindTexture(GL_TEXTURE_2D, 0);
324
325 bound_texture = 0;
326 glActiveTexture(GL_TEXTURE0);
327 glGetIntegerv(GL_TEXTURE_BINDING_2D, &bound_texture);
328 EXPECT_EQ(texture_ids[0], static_cast<GLuint>(bound_texture));
329 glBindTexture(GL_TEXTURE_2D, 0);
330
331 glDeleteTextures(2, texture_ids);
332
333 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
334 }
335
336 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, ProgramStatePreservation) {
337 if (!available_)
338 return;
339
340 // unbind the one created in setup.
341 glBindFramebuffer(GL_FRAMEBUFFER, 0);
342 glBindTexture(GL_TEXTURE_2D, 0);
343
344 GLManager gl2;
345 GLManager::Options options;
346 options.size = gfx::Size(16, 16);
347 options.share_group_manager = &gl_;
348 gl2.Initialize(options);
349 gl_.MakeCurrent();
350
351 static const char* v_shader_str =
352 "attribute vec4 g_Position;\n"
353 "void main()\n"
354 "{\n"
355 " gl_Position = g_Position;\n"
356 "}\n";
357 static const char* f_shader_str =
358 "precision mediump float;\n"
359 "void main()\n"
360 "{\n"
361 " gl_FragColor = vec4(0,1,0,1);\n"
362 "}\n";
363
364 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
365 glUseProgram(program);
366 GLuint position_loc = glGetAttribLocation(program, "g_Position");
367 glFlush();
368
369 // Delete program from other context.
370 gl2.MakeCurrent();
371 glDeleteProgram(program);
372 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
373 glFlush();
374
375 // Program should still be usable on this context.
376 gl_.MakeCurrent();
377
378 GLTestHelper::SetupUnitQuad(position_loc);
379
380 // test using program before
381 uint8_t expected[] = {
382 0, 255, 0, 255,
383 };
384 uint8_t zero[] = {
385 0, 0, 0, 0,
386 };
387 glClearColor(0, 0, 0, 0);
388 glClear(GL_COLOR_BUFFER_BIT);
389 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, zero));
390 glDrawArrays(GL_TRIANGLES, 0, 6);
391 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected));
392
393 // Call copyTextureCHROMIUM
394 uint8_t pixels[1 * 4] = {255u, 0u, 0u, 255u};
395 glBindTexture(GL_TEXTURE_2D, textures_);
396 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
397 pixels);
398 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_);
399 glApplyScreenSpaceAntialiasingCHROMIUM();
400 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
401
402 // test using program after
403 glClear(GL_COLOR_BUFFER_BIT);
404 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, zero));
405 glDrawArrays(GL_TRIANGLES, 0, 6);
406 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected));
407
408 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
409
410 gl2.MakeCurrent();
411 gl2.Destroy();
412 gl_.MakeCurrent();
413 }
414
415 } // namespace gpu
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698