OLD | NEW |
---|---|
(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 DCHECK(!gl_.workarounds().disable_framebuffer_cmaa); | |
49 | |
50 available_ = | |
51 GLTestHelper::HasExtension("GL_CHROMIUM_screen_space_antialiasing"); | |
52 if (!available_) { | |
53 LOG(INFO) << "GL_CHROMIUM_screen_space_antialiasing not supported. " | |
54 "Skipping test..."; | |
55 return; | |
56 } | |
57 | |
58 CreateAndBindDestinationTextureAndFBO(GL_TEXTURE_2D); | |
59 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, | |
60 nullptr); | |
61 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), | |
62 glCheckFramebufferStatus(GL_FRAMEBUFFER)); | |
63 | |
64 glClearColor(0, 1, 0, 1); | |
65 glClear(GL_COLOR_BUFFER_BIT); | |
66 } | |
67 | |
68 void TearDown() override { | |
69 glDeleteTextures(1, &textures_); | |
70 glDeleteFramebuffers(1, &framebuffer_id_); | |
71 gl_.Destroy(); | |
72 } | |
73 | |
74 void CreateBackingForTexture(GLenum target, GLsizei width, GLsizei height) { | |
75 if (target == GL_TEXTURE_RECTANGLE_ARB) { | |
76 GLuint image_id = glCreateGpuMemoryBufferImageCHROMIUM( | |
77 width, height, GL_RGBA, GL_READ_WRITE_CHROMIUM); | |
78 glBindTexImage2DCHROMIUM(target, image_id); | |
79 } else { | |
80 glTexImage2D(target, 0, GL_RGBA, width, height, 0, GL_RGBA, | |
81 GL_UNSIGNED_BYTE, nullptr); | |
82 } | |
83 } | |
84 | |
85 GLenum ExtractFormatFrom(GLenum internalformat) { | |
86 switch (internalformat) { | |
87 case GL_RGBA8_OES: | |
88 return GL_RGBA; | |
89 case GL_RGB8_OES: | |
90 return GL_RGB; | |
91 case GL_BGRA8_EXT: | |
92 return GL_BGRA_EXT; | |
93 default: | |
94 NOTREACHED(); | |
95 return GL_NONE; | |
96 } | |
97 } | |
98 | |
99 GLManager gl_; | |
100 GLuint textures_; | |
piman
2016/10/19 19:10:43
nit: texture_ (singular)
Please initialize this
dshwang
2016/10/20 16:21:20
Done.
| |
101 GLuint framebuffer_id_; | |
102 bool available_ = false; | |
103 }; | |
104 | |
105 // Test to ensure that the basic functionality of the extension works. | |
106 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, Basic) { | |
107 if (!available_) | |
108 return; | |
109 | |
110 glApplyScreenSpaceAntialiasingCHROMIUM(); | |
111 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
112 | |
113 // Check the FB is still bound. | |
114 GLint value = 0; | |
115 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &value); | |
116 GLuint fb_id = value; | |
117 EXPECT_EQ(framebuffer_id_, fb_id); | |
118 | |
119 // Check that FB is complete. | |
120 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), | |
121 glCheckFramebufferStatus(GL_FRAMEBUFFER)); | |
122 | |
123 uint8_t pixels[1 * 4] = {0u, 255u, 0u, 255u}; | |
124 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, pixels); | |
125 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
126 } | |
127 | |
128 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, DefaultFBO) { | |
129 if (!available_) | |
130 return; | |
131 | |
132 glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
133 glApplyScreenSpaceAntialiasingCHROMIUM(); | |
134 EXPECT_EQ(static_cast<GLenum>(GL_INVALID_OPERATION), glGetError()); | |
135 } | |
136 | |
137 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, InternalFormat) { | |
138 if (!available_) | |
139 return; | |
140 | |
141 GLint formats[] = {GL_RGB, GL_RGBA}; | |
142 for (size_t index = 0; index < arraysize(formats); index++) { | |
143 glTexImage2D(GL_TEXTURE_2D, 0, formats[index], 1, 1, 0, formats[index], | |
144 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_NO_ERROR), glGetError()) << "index:" | |
154 << index; | |
155 | |
156 uint8_t pixels[1 * 4] = {0u, 255u, 0u, 255u}; | |
157 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, pixels); | |
158 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
159 } | |
160 } | |
161 | |
162 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, InternalFormatNotSupported) { | |
163 if (!available_) | |
164 return; | |
165 | |
166 // Check unsupported format reports error. | |
167 GLint unsupported_formats[] = {GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA}; | |
168 for (size_t index = 0; index < arraysize(unsupported_formats); index++) { | |
169 glTexImage2D(GL_TEXTURE_2D, 0, unsupported_formats[index], 1, 1, 0, | |
170 unsupported_formats[index], GL_UNSIGNED_BYTE, nullptr); | |
171 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
172 | |
173 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, | |
174 textures_, 0); | |
175 | |
176 glClear(GL_COLOR_BUFFER_BIT); | |
177 | |
178 glApplyScreenSpaceAntialiasingCHROMIUM(); | |
179 EXPECT_EQ(static_cast<GLenum>(GL_INVALID_FRAMEBUFFER_OPERATION), | |
180 glGetError()) | |
181 << "index:" << index; | |
182 } | |
183 } | |
184 | |
185 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, ImmutableTexture) { | |
186 if (!available_) | |
187 return; | |
188 | |
189 if (!GLTestHelper::HasExtension("GL_EXT_texture_storage")) { | |
190 LOG(INFO) << "GL_EXT_texture_storage not supported. Skipping test..."; | |
191 return; | |
192 } | |
193 GLenum internal_formats[] = {GL_RGB8_OES, GL_RGBA8_OES, GL_BGRA8_EXT}; | |
194 for (auto internal_format : internal_formats) { | |
195 glDeleteTextures(1, &textures_); | |
196 glDeleteFramebuffers(1, &framebuffer_id_); | |
197 CreateAndBindDestinationTextureAndFBO(GL_TEXTURE_2D); | |
198 glTexStorage2DEXT(GL_TEXTURE_2D, 1, internal_format, 1, 1); | |
199 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
200 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), | |
201 glCheckFramebufferStatus(GL_FRAMEBUFFER)); | |
202 | |
203 glClear(GL_COLOR_BUFFER_BIT); | |
204 | |
205 glApplyScreenSpaceAntialiasingCHROMIUM(); | |
206 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
207 | |
208 uint8_t pixels[1 * 4] = {0u, 255u, 0u, 255u}; | |
209 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, pixels); | |
210 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
211 } | |
212 } | |
213 | |
214 // Similar to webgl conformance test 'testAntialias(true)' in | |
215 // context-attributes-alpha-depth-stencil-antialias.html | |
216 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, AntiAliasing) { | |
217 if (!available_) | |
218 return; | |
219 | |
220 // Fill colors in the FBO as follows | |
221 // +-----+ | |
222 // |R|R| | | |
223 // +-----+ | |
224 // |R| | | | |
225 // +-----+ | |
226 // | | | | | |
227 // +-+-+-+ | |
228 const int length = 3; | |
229 uint8_t rgba_pixels[4 * length * length] = { | |
230 255u, 0u, 0u, 255u, 255u, 0u, 0u, 255u, 0u, 0u, 0u, 0u, | |
231 255u, 0u, 0u, 255u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, | |
232 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, | |
233 }; | |
234 glBindTexture(GL_TEXTURE_2D, textures_); | |
235 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, length, length, 0, GL_RGBA, | |
236 GL_UNSIGNED_BYTE, rgba_pixels); | |
237 | |
238 uint8_t transparent[4] = {0u, 0u, 0u, 0u}; | |
239 uint8_t red[4] = {255u, 0u, 0u, 255u}; | |
240 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, red); | |
241 GLTestHelper::CheckPixels(0, 1, 1, 1, 0, red); | |
242 GLTestHelper::CheckPixels(0, 2, 1, 1, 0, transparent); | |
243 GLTestHelper::CheckPixels(1, 0, 1, 1, 0, red); | |
244 GLTestHelper::CheckPixels(1, 1, 1, 1, 0, transparent); | |
245 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
246 | |
247 glApplyScreenSpaceAntialiasingCHROMIUM(); | |
248 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
249 | |
250 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, red); | |
251 GLTestHelper::CheckPixels(2, 2, 1, 1, 0, transparent); | |
252 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
253 | |
254 // Check if middle pixel is anti-aliased. | |
255 uint8_t pixels[4] = {0u}; | |
256 glReadPixels(1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixels); | |
257 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
258 EXPECT_NE(transparent, pixels); | |
259 EXPECT_NE(red, pixels); | |
260 EXPECT_TRUE(pixels[0] > transparent[0] && pixels[0] < red[0]); | |
261 EXPECT_EQ(0u, pixels[1]); | |
262 EXPECT_EQ(0u, pixels[2]); | |
263 EXPECT_TRUE(pixels[3] > transparent[3] && pixels[3] < red[3]); | |
264 } | |
265 | |
266 namespace { | |
267 | |
268 void glEnableDisable(GLint param, GLboolean value) { | |
269 if (value) | |
270 glEnable(param); | |
271 else | |
272 glDisable(param); | |
273 } | |
274 | |
275 } // unnamed namespace | |
276 | |
277 // Validate that some basic GL state is not touched upon execution of | |
278 // the extension. | |
279 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, BasicStatePreservation) { | |
280 if (!available_) | |
281 return; | |
282 | |
283 GLboolean reference_settings[2] = {GL_TRUE, GL_FALSE}; | |
284 for (int x = 0; x < 2; ++x) { | |
285 GLboolean setting = reference_settings[x]; | |
286 glEnableDisable(GL_DEPTH_TEST, setting); | |
287 glEnableDisable(GL_SCISSOR_TEST, setting); | |
288 glEnableDisable(GL_STENCIL_TEST, setting); | |
289 glEnableDisable(GL_CULL_FACE, setting); | |
290 glEnableDisable(GL_BLEND, setting); | |
291 glColorMask(setting, setting, setting, setting); | |
292 glDepthMask(setting); | |
293 | |
294 glActiveTexture(GL_TEXTURE1 + x); | |
295 | |
296 glApplyScreenSpaceAntialiasingCHROMIUM(); | |
297 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
298 | |
299 EXPECT_EQ(setting, glIsEnabled(GL_DEPTH_TEST)); | |
300 EXPECT_EQ(setting, glIsEnabled(GL_SCISSOR_TEST)); | |
301 EXPECT_EQ(setting, glIsEnabled(GL_STENCIL_TEST)); | |
302 EXPECT_EQ(setting, glIsEnabled(GL_CULL_FACE)); | |
303 EXPECT_EQ(setting, glIsEnabled(GL_BLEND)); | |
304 | |
305 GLboolean bool_array[4] = {GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE}; | |
306 glGetBooleanv(GL_DEPTH_WRITEMASK, bool_array); | |
307 EXPECT_EQ(setting, bool_array[0]); | |
308 | |
309 bool_array[0] = GL_FALSE; | |
310 glGetBooleanv(GL_COLOR_WRITEMASK, bool_array); | |
311 EXPECT_EQ(setting, bool_array[0]); | |
312 EXPECT_EQ(setting, bool_array[1]); | |
313 EXPECT_EQ(setting, bool_array[2]); | |
314 EXPECT_EQ(setting, bool_array[3]); | |
315 | |
316 GLint active_texture = 0; | |
317 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture); | |
318 EXPECT_EQ(GL_TEXTURE1 + x, active_texture); | |
319 } | |
320 | |
321 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
322 }; | |
323 | |
324 // Verify that invocation of the extension does not modify the bound | |
325 // texture state. | |
326 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, TextureStatePreserved) { | |
327 if (!available_) | |
328 return; | |
329 | |
330 GLuint texture_ids[2]; | |
331 glGenTextures(2, texture_ids); | |
332 | |
333 glActiveTexture(GL_TEXTURE0); | |
334 glBindTexture(GL_TEXTURE_2D, texture_ids[0]); | |
335 | |
336 glActiveTexture(GL_TEXTURE1); | |
337 glBindTexture(GL_TEXTURE_2D, texture_ids[1]); | |
338 | |
339 glApplyScreenSpaceAntialiasingCHROMIUM(); | |
340 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
341 | |
342 GLint active_texture = 0; | |
343 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture); | |
344 EXPECT_EQ(GL_TEXTURE1, active_texture); | |
345 | |
346 GLint bound_texture = 0; | |
347 glGetIntegerv(GL_TEXTURE_BINDING_2D, &bound_texture); | |
348 EXPECT_EQ(texture_ids[1], static_cast<GLuint>(bound_texture)); | |
349 glBindTexture(GL_TEXTURE_2D, 0); | |
350 | |
351 bound_texture = 0; | |
352 glActiveTexture(GL_TEXTURE0); | |
353 glGetIntegerv(GL_TEXTURE_BINDING_2D, &bound_texture); | |
354 EXPECT_EQ(texture_ids[0], static_cast<GLuint>(bound_texture)); | |
355 glBindTexture(GL_TEXTURE_2D, 0); | |
356 | |
357 glDeleteTextures(2, texture_ids); | |
358 | |
359 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
360 } | |
361 | |
362 TEST_F(GLApplyScreenSpaceAntialiasingCHROMIUMTest, ProgramStatePreservation) { | |
363 if (!available_) | |
364 return; | |
365 | |
366 // unbind the one created in setup. | |
367 glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
368 glBindTexture(GL_TEXTURE_2D, 0); | |
369 | |
370 GLManager gl2; | |
371 GLManager::Options options; | |
372 options.size = gfx::Size(16, 16); | |
373 options.share_group_manager = &gl_; | |
374 gl2.Initialize(options); | |
375 gl_.MakeCurrent(); | |
376 | |
377 static const char* v_shader_str = | |
378 "attribute vec4 g_Position;\n" | |
379 "void main()\n" | |
380 "{\n" | |
381 " gl_Position = g_Position;\n" | |
382 "}\n"; | |
383 static const char* f_shader_str = | |
384 "precision mediump float;\n" | |
385 "void main()\n" | |
386 "{\n" | |
387 " gl_FragColor = vec4(0,1,0,1);\n" | |
388 "}\n"; | |
389 | |
390 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str); | |
391 glUseProgram(program); | |
392 GLuint position_loc = glGetAttribLocation(program, "g_Position"); | |
393 glFlush(); | |
394 | |
395 // Delete program from other context. | |
396 gl2.MakeCurrent(); | |
397 glDeleteProgram(program); | |
398 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
399 glFlush(); | |
400 | |
401 // Program should still be usable on this context. | |
402 gl_.MakeCurrent(); | |
403 | |
404 GLTestHelper::SetupUnitQuad(position_loc); | |
405 | |
406 // test using program before | |
407 uint8_t expected[] = { | |
408 0, 255, 0, 255, | |
409 }; | |
410 uint8_t zero[] = { | |
411 0, 0, 0, 0, | |
412 }; | |
413 glClearColor(0, 0, 0, 0); | |
414 glClear(GL_COLOR_BUFFER_BIT); | |
415 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, zero)); | |
416 glDrawArrays(GL_TRIANGLES, 0, 6); | |
417 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected)); | |
418 | |
419 // Call copyTextureCHROMIUM | |
420 uint8_t pixels[1 * 4] = {255u, 0u, 0u, 255u}; | |
421 glBindTexture(GL_TEXTURE_2D, textures_); | |
422 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, | |
423 pixels); | |
424 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_); | |
425 glApplyScreenSpaceAntialiasingCHROMIUM(); | |
426 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
427 | |
428 // test using program after | |
429 glClear(GL_COLOR_BUFFER_BIT); | |
430 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, zero)); | |
431 glDrawArrays(GL_TRIANGLES, 0, 6); | |
432 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected)); | |
433 | |
434 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
435 | |
436 gl2.MakeCurrent(); | |
437 gl2.Destroy(); | |
438 gl_.MakeCurrent(); | |
439 } | |
440 | |
441 } // namespace gpu | |
OLD | NEW |