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