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