OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file defines tests that implementations of GLImage should pass in order | 5 // This file defines tests that implementations of GLImage should pass in order |
6 // to be conformant. | 6 // to be conformant. |
7 | 7 |
8 #ifndef UI_GL_TEST_GL_IMAGE_TEST_TEMPLATE_H_ | 8 #ifndef UI_GL_TEST_GL_IMAGE_TEST_TEMPLATE_H_ |
9 #define UI_GL_TEST_GL_IMAGE_TEST_TEMPLATE_H_ | 9 #define UI_GL_TEST_GL_IMAGE_TEST_TEMPLATE_H_ |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "ui/gl/test/gl_image_test_support.h" | 28 #include "ui/gl/test/gl_image_test_support.h" |
29 #include "ui/gl/test/gl_test_helper.h" | 29 #include "ui/gl/test/gl_test_helper.h" |
30 | 30 |
31 #if defined(OS_MACOSX) | 31 #if defined(OS_MACOSX) |
32 #include "base/mac/mac_util.h" | 32 #include "base/mac/mac_util.h" |
33 #endif | 33 #endif |
34 | 34 |
35 namespace gl { | 35 namespace gl { |
36 namespace { | 36 namespace { |
37 | 37 |
| 38 GLuint LoadVertexShader() { |
| 39 bool is_desktop_core_profile = |
| 40 GLContext::GetCurrent()->GetVersionInfo()->is_desktop_core_profile; |
| 41 std::string vertex_shader = base::StringPrintf( |
| 42 "%s" // version |
| 43 "%s vec2 a_position;\n" |
| 44 "%s vec2 v_texCoord;\n" |
| 45 "void main() {\n" |
| 46 " gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0);\n" |
| 47 " v_texCoord = (a_position + vec2(1.0, 1.0)) * 0.5;\n" |
| 48 "}", |
| 49 is_desktop_core_profile ? "#version 150\n" : "", |
| 50 is_desktop_core_profile ? "in" : "attribute", |
| 51 is_desktop_core_profile ? "out" : "varying"); |
| 52 return GLHelper::LoadShader(GL_VERTEX_SHADER, vertex_shader.c_str()); |
| 53 } |
| 54 |
38 // Compiles a fragment shader for sampling out of a texture of |size| bound to | 55 // Compiles a fragment shader for sampling out of a texture of |size| bound to |
39 // |target| and checks for compilation errors. | 56 // |target| and checks for compilation errors. |
40 GLuint LoadFragmentShader(unsigned target, const gfx::Size& size) { | 57 GLuint LoadFragmentShader(unsigned target, const gfx::Size& size) { |
41 // clang-format off | 58 bool is_desktop_core_profile = |
42 const char kFragmentShader[] = STRINGIZE( | 59 GLContext::GetCurrent()->GetVersionInfo()->is_desktop_core_profile; |
43 uniform SamplerType a_texture; | 60 bool is_gles = GLContext::GetCurrent()->GetVersionInfo()->is_es; |
44 varying vec2 v_texCoord; | |
45 void main() { | |
46 gl_FragColor = TextureLookup(a_texture, v_texCoord * TextureScale); | |
47 } | |
48 ); | |
49 const char kShaderFloatPrecision[] = STRINGIZE( | |
50 precision mediump float; | |
51 ); | |
52 // clang-format on | |
53 | 61 |
54 bool is_gles = GLContext::GetCurrent()->GetVersionInfo()->is_es; | 62 std::string fragment_shader_main = base::StringPrintf( |
| 63 "uniform SamplerType a_texture;\n" |
| 64 "%s vec2 v_texCoord;\n" |
| 65 "%s" // output variable declaration |
| 66 "void main() {\n" |
| 67 " %s = TextureLookup(a_texture, v_texCoord * TextureScale);\n" |
| 68 "}", |
| 69 is_desktop_core_profile ? "in" : "varying", |
| 70 is_desktop_core_profile ? "out vec4 my_FragData;\n" : "", |
| 71 is_desktop_core_profile ? "my_FragData" : "gl_FragData[0]"); |
| 72 |
55 switch (target) { | 73 switch (target) { |
56 case GL_TEXTURE_2D: | 74 case GL_TEXTURE_2D: |
57 return GLHelper::LoadShader( | 75 return GLHelper::LoadShader( |
58 GL_FRAGMENT_SHADER, | 76 GL_FRAGMENT_SHADER, |
59 base::StringPrintf("%s\n" | 77 base::StringPrintf("%s" // version |
| 78 "%s" // precision |
60 "#define SamplerType sampler2D\n" | 79 "#define SamplerType sampler2D\n" |
61 "#define TextureLookup texture2D\n" | 80 "#define TextureLookup %s\n" |
62 "#define TextureScale vec2(1.0, 1.0)\n" | 81 "#define TextureScale vec2(1.0, 1.0)\n" |
63 "%s", | 82 "%s", // main function |
64 is_gles ? kShaderFloatPrecision : "", | 83 is_desktop_core_profile ? "#version 150\n" : "", |
65 kFragmentShader) | 84 is_gles ? "precision mediump float;\n" : "", |
| 85 is_desktop_core_profile ? "texture" : "texture2D", |
| 86 fragment_shader_main.c_str()) |
66 .c_str()); | 87 .c_str()); |
67 case GL_TEXTURE_RECTANGLE_ARB: | 88 case GL_TEXTURE_RECTANGLE_ARB: |
| 89 DCHECK(!is_gles); |
68 return GLHelper::LoadShader( | 90 return GLHelper::LoadShader( |
69 GL_FRAGMENT_SHADER, | 91 GL_FRAGMENT_SHADER, |
70 base::StringPrintf("#extension GL_ARB_texture_rectangle : require\n" | 92 base::StringPrintf( |
71 "%s\n" | 93 "%s" // version |
72 "#define SamplerType sampler2DRect\n" | 94 "%s" // extension |
73 "#define TextureLookup texture2DRect\n" | 95 "#define SamplerType sampler2DRect\n" |
74 "#define TextureScale vec2(%f, %f)\n" | 96 "#define TextureLookup %s\n" |
75 "%s", | 97 "#define TextureScale vec2(%f, %f)\n" |
76 is_gles ? kShaderFloatPrecision : "", | 98 "%s", // main function |
77 static_cast<double>(size.width()), | 99 is_desktop_core_profile ? "#version 150\n" : "", |
78 static_cast<double>(size.height()), | 100 is_desktop_core_profile |
79 kFragmentShader) | 101 ? "" |
| 102 : "#extension GL_ARB_texture_rectangle : require\n", |
| 103 is_desktop_core_profile ? "texture" : "texture2DRect", |
| 104 static_cast<double>(size.width()), |
| 105 static_cast<double>(size.height()), fragment_shader_main.c_str()) |
80 .c_str()); | 106 .c_str()); |
81 case GL_TEXTURE_EXTERNAL_OES: | 107 case GL_TEXTURE_EXTERNAL_OES: |
| 108 DCHECK(is_gles); |
82 return GLHelper::LoadShader( | 109 return GLHelper::LoadShader( |
83 GL_FRAGMENT_SHADER, | 110 GL_FRAGMENT_SHADER, |
84 base::StringPrintf("#extension GL_OES_EGL_image_external : require\n" | 111 base::StringPrintf("#extension GL_OES_EGL_image_external : require\n" |
85 "%s\n" | |
86 "#define SamplerType samplerExternalOES\n" | 112 "#define SamplerType samplerExternalOES\n" |
87 "#define TextureLookup texture2D\n" | 113 "#define TextureLookup texture2D\n" |
88 "#define TextureScale vec2(1.0, 1.0)\n" | 114 "#define TextureScale vec2(1.0, 1.0)\n" |
89 "%s", | 115 "%s", // main function |
90 is_gles ? kShaderFloatPrecision : "", | 116 fragment_shader_main.c_str()) |
91 kFragmentShader) | |
92 .c_str()); | 117 .c_str()); |
93 default: | 118 default: |
94 NOTREACHED(); | 119 NOTREACHED(); |
95 return 0; | 120 return 0; |
96 } | 121 } |
97 } | 122 } |
98 | 123 |
99 // Draws texture bound to |target| of texture unit 0 to the currently bound | 124 // Draws texture bound to |target| of texture unit 0 to the currently bound |
100 // frame buffer. | 125 // frame buffer. |
101 void DrawTextureQuad(GLenum target, const gfx::Size& size) { | 126 void DrawTextureQuad(GLenum target, const gfx::Size& size) { |
102 // clang-format off | |
103 const char kVertexShader[] = STRINGIZE( | |
104 attribute vec2 a_position; | |
105 varying vec2 v_texCoord; | |
106 void main() { | |
107 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); | |
108 v_texCoord = (a_position + vec2(1.0, 1.0)) * 0.5; | |
109 } | |
110 ); | |
111 // clang-format on | |
112 | |
113 GLuint vao = 0; | 127 GLuint vao = 0; |
114 if (GLHelper::ShouldTestsUseVAOs()) { | 128 if (GLHelper::ShouldTestsUseVAOs()) { |
115 glGenVertexArraysOES(1, &vao); | 129 glGenVertexArraysOES(1, &vao); |
116 glBindVertexArrayOES(vao); | 130 glBindVertexArrayOES(vao); |
117 } | 131 } |
118 | 132 |
119 GLuint vertex_shader = GLHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader); | 133 GLuint vertex_shader = LoadVertexShader(); |
120 GLuint fragment_shader = LoadFragmentShader(target, size); | 134 GLuint fragment_shader = LoadFragmentShader(target, size); |
121 GLuint program = GLHelper::SetupProgram(vertex_shader, fragment_shader); | 135 GLuint program = GLHelper::SetupProgram(vertex_shader, fragment_shader); |
122 EXPECT_NE(program, 0u); | 136 EXPECT_NE(program, 0u); |
123 glUseProgram(program); | 137 glUseProgram(program); |
124 | 138 |
125 GLint sampler_location = glGetUniformLocation(program, "a_texture"); | 139 GLint sampler_location = glGetUniformLocation(program, "a_texture"); |
126 ASSERT_NE(sampler_location, -1); | 140 ASSERT_NE(sampler_location, -1); |
127 glUniform1i(sampler_location, 0); | 141 glUniform1i(sampler_location, 0); |
128 | 142 |
129 GLuint vertex_buffer = GLHelper::SetupQuadVertexBuffer(); | 143 GLuint vertex_buffer = GLHelper::SetupQuadVertexBuffer(); |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 template <typename GLImageTestDelegate> | 321 template <typename GLImageTestDelegate> |
308 class GLImageCopyTest : public GLImageTest<GLImageTestDelegate> {}; | 322 class GLImageCopyTest : public GLImageTest<GLImageTestDelegate> {}; |
309 | 323 |
310 TYPED_TEST_CASE_P(GLImageCopyTest); | 324 TYPED_TEST_CASE_P(GLImageCopyTest); |
311 | 325 |
312 TYPED_TEST_P(GLImageCopyTest, CopyTexImage) { | 326 TYPED_TEST_P(GLImageCopyTest, CopyTexImage) { |
313 const gfx::Size image_size(256, 256); | 327 const gfx::Size image_size(256, 256); |
314 const uint8_t* image_color = this->delegate_.GetImageColor(); | 328 const uint8_t* image_color = this->delegate_.GetImageColor(); |
315 const uint8_t texture_color[] = {0, 0, 0xff, 0xff}; | 329 const uint8_t texture_color[] = {0, 0, 0xff, 0xff}; |
316 | 330 |
| 331 GLuint vao = 0; |
| 332 if (GLContext::GetCurrent()->GetVersionInfo()->IsAtLeastGL(3, 3)) { |
| 333 // To avoid glGetVertexAttribiv(0, ...) failing. |
| 334 glGenVertexArraysOES(1, &vao); |
| 335 glBindVertexArrayOES(vao); |
| 336 } |
| 337 |
317 GLuint framebuffer = | 338 GLuint framebuffer = |
318 GLTestHelper::SetupFramebuffer(image_size.width(), image_size.height()); | 339 GLTestHelper::SetupFramebuffer(image_size.width(), image_size.height()); |
319 ASSERT_TRUE(framebuffer); | 340 ASSERT_TRUE(framebuffer); |
320 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer); | 341 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer); |
321 glViewport(0, 0, image_size.width(), image_size.height()); | 342 glViewport(0, 0, image_size.width(), image_size.height()); |
322 | 343 |
323 // Create a solid color green image of preferred format. This must succeed | 344 // Create a solid color green image of preferred format. This must succeed |
324 // in order for a GLImage to be conformant. | 345 // in order for a GLImage to be conformant. |
325 scoped_refptr<GLImage> image = | 346 scoped_refptr<GLImage> image = |
326 this->delegate_.CreateSolidColorImage(image_size, image_color); | 347 this->delegate_.CreateSolidColorImage(image_size, image_color); |
(...skipping 21 matching lines...) Expand all Loading... |
348 DrawTextureQuad(target, image_size); | 369 DrawTextureQuad(target, image_size); |
349 | 370 |
350 // Read back pixels to check expectations. | 371 // Read back pixels to check expectations. |
351 GLTestHelper::CheckPixels(0, 0, image_size.width(), image_size.height(), | 372 GLTestHelper::CheckPixels(0, 0, image_size.width(), image_size.height(), |
352 image_color); | 373 image_color); |
353 | 374 |
354 // Clean up. | 375 // Clean up. |
355 glDeleteTextures(1, &texture); | 376 glDeleteTextures(1, &texture); |
356 glDeleteFramebuffersEXT(1, &framebuffer); | 377 glDeleteFramebuffersEXT(1, &framebuffer); |
357 image->Destroy(true /* have_context */); | 378 image->Destroy(true /* have_context */); |
| 379 if (vao) { |
| 380 glDeleteVertexArraysOES(1, &vao); |
| 381 } |
358 } | 382 } |
359 | 383 |
360 // The GLImageCopyTest test case verifies that the GLImage implementation | 384 // The GLImageCopyTest test case verifies that the GLImage implementation |
361 // handles CopyTexImage correctly. | 385 // handles CopyTexImage correctly. |
362 REGISTER_TYPED_TEST_CASE_P(GLImageCopyTest, CopyTexImage); | 386 REGISTER_TYPED_TEST_CASE_P(GLImageCopyTest, CopyTexImage); |
363 | 387 |
364 } // namespace gl | 388 } // namespace gl |
365 | 389 |
366 #endif // UI_GL_TEST_GL_IMAGE_TEST_TEMPLATE_H_ | 390 #endif // UI_GL_TEST_GL_IMAGE_TEST_TEMPLATE_H_ |
OLD | NEW |