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" | |
reveman
2016/08/18 17:27:56
nit: did you cl format this? please do if you didn
Zhenyao Mo
2016/08/18 17:57:23
Done.
| |
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( |
60 "#define SamplerType sampler2D\n" | 78 "%s" // version |
61 "#define TextureLookup texture2D\n" | 79 "%s" // precision |
62 "#define TextureScale vec2(1.0, 1.0)\n" | 80 "#define SamplerType sampler2D\n" |
63 "%s", | 81 "#define TextureLookup %s\n" |
64 is_gles ? kShaderFloatPrecision : "", | 82 "#define TextureScale vec2(1.0, 1.0)\n" |
65 kFragmentShader) | 83 "%s", // main function |
66 .c_str()); | 84 is_desktop_core_profile ? "#version 150\n" : "", |
85 is_gles ? "precision mediump float;\n" : "", | |
86 is_desktop_core_profile ? "texture" : "texture2D", | |
87 fragment_shader_main.c_str()).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 "#extension GL_ARB_texture_rectangle : require\n", |
80 .c_str()); | 102 is_desktop_core_profile ? "texture" : "texture2DRect", |
103 static_cast<double>(size.width()), | |
104 static_cast<double>(size.height()), | |
105 fragment_shader_main.c_str()).c_str()); | |
81 case GL_TEXTURE_EXTERNAL_OES: | 106 case GL_TEXTURE_EXTERNAL_OES: |
107 DCHECK(is_gles); | |
82 return GLHelper::LoadShader( | 108 return GLHelper::LoadShader( |
83 GL_FRAGMENT_SHADER, | 109 GL_FRAGMENT_SHADER, |
84 base::StringPrintf("#extension GL_OES_EGL_image_external : require\n" | 110 base::StringPrintf( |
85 "%s\n" | 111 "#extension GL_OES_EGL_image_external : require\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()).c_str()); |
91 kFragmentShader) | |
92 .c_str()); | |
93 default: | 117 default: |
94 NOTREACHED(); | 118 NOTREACHED(); |
95 return 0; | 119 return 0; |
96 } | 120 } |
97 } | 121 } |
98 | 122 |
99 // Draws texture bound to |target| of texture unit 0 to the currently bound | 123 // Draws texture bound to |target| of texture unit 0 to the currently bound |
100 // frame buffer. | 124 // frame buffer. |
101 void DrawTextureQuad(GLenum target, const gfx::Size& size) { | 125 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; | 126 GLuint vao = 0; |
114 if (GLHelper::ShouldTestsUseVAOs()) { | 127 if (GLHelper::ShouldTestsUseVAOs()) { |
115 glGenVertexArraysOES(1, &vao); | 128 glGenVertexArraysOES(1, &vao); |
116 glBindVertexArrayOES(vao); | 129 glBindVertexArrayOES(vao); |
117 } | 130 } |
118 | 131 |
119 GLuint vertex_shader = GLHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader); | 132 GLuint vertex_shader = LoadVertexShader(); |
120 GLuint fragment_shader = LoadFragmentShader(target, size); | 133 GLuint fragment_shader = LoadFragmentShader(target, size); |
121 GLuint program = GLHelper::SetupProgram(vertex_shader, fragment_shader); | 134 GLuint program = GLHelper::SetupProgram(vertex_shader, fragment_shader); |
122 EXPECT_NE(program, 0u); | 135 EXPECT_NE(program, 0u); |
123 glUseProgram(program); | 136 glUseProgram(program); |
124 | 137 |
125 GLint sampler_location = glGetUniformLocation(program, "a_texture"); | 138 GLint sampler_location = glGetUniformLocation(program, "a_texture"); |
126 ASSERT_NE(sampler_location, -1); | 139 ASSERT_NE(sampler_location, -1); |
127 glUniform1i(sampler_location, 0); | 140 glUniform1i(sampler_location, 0); |
128 | 141 |
129 GLuint vertex_buffer = GLHelper::SetupQuadVertexBuffer(); | 142 GLuint vertex_buffer = GLHelper::SetupQuadVertexBuffer(); |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
307 template <typename GLImageTestDelegate> | 320 template <typename GLImageTestDelegate> |
308 class GLImageCopyTest : public GLImageTest<GLImageTestDelegate> {}; | 321 class GLImageCopyTest : public GLImageTest<GLImageTestDelegate> {}; |
309 | 322 |
310 TYPED_TEST_CASE_P(GLImageCopyTest); | 323 TYPED_TEST_CASE_P(GLImageCopyTest); |
311 | 324 |
312 TYPED_TEST_P(GLImageCopyTest, CopyTexImage) { | 325 TYPED_TEST_P(GLImageCopyTest, CopyTexImage) { |
313 const gfx::Size image_size(256, 256); | 326 const gfx::Size image_size(256, 256); |
314 const uint8_t* image_color = this->delegate_.GetImageColor(); | 327 const uint8_t* image_color = this->delegate_.GetImageColor(); |
315 const uint8_t texture_color[] = {0, 0, 0xff, 0xff}; | 328 const uint8_t texture_color[] = {0, 0, 0xff, 0xff}; |
316 | 329 |
330 GLuint vao = 0; | |
331 if (GLContext::GetCurrent()->GetVersionInfo()->IsAtLeastGL(3, 3)) { | |
332 // To avoid glGetVertexAttribiv(0, ...) failing. | |
333 glGenVertexArraysOES(1, &vao); | |
334 glBindVertexArrayOES(vao); | |
335 } | |
336 | |
317 GLuint framebuffer = | 337 GLuint framebuffer = |
318 GLTestHelper::SetupFramebuffer(image_size.width(), image_size.height()); | 338 GLTestHelper::SetupFramebuffer(image_size.width(), image_size.height()); |
319 ASSERT_TRUE(framebuffer); | 339 ASSERT_TRUE(framebuffer); |
320 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer); | 340 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer); |
321 glViewport(0, 0, image_size.width(), image_size.height()); | 341 glViewport(0, 0, image_size.width(), image_size.height()); |
322 | 342 |
323 // Create a solid color green image of preferred format. This must succeed | 343 // Create a solid color green image of preferred format. This must succeed |
324 // in order for a GLImage to be conformant. | 344 // in order for a GLImage to be conformant. |
325 scoped_refptr<GLImage> image = | 345 scoped_refptr<GLImage> image = |
326 this->delegate_.CreateSolidColorImage(image_size, image_color); | 346 this->delegate_.CreateSolidColorImage(image_size, image_color); |
(...skipping 21 matching lines...) Expand all Loading... | |
348 DrawTextureQuad(target, image_size); | 368 DrawTextureQuad(target, image_size); |
349 | 369 |
350 // Read back pixels to check expectations. | 370 // Read back pixels to check expectations. |
351 GLTestHelper::CheckPixels(0, 0, image_size.width(), image_size.height(), | 371 GLTestHelper::CheckPixels(0, 0, image_size.width(), image_size.height(), |
352 image_color); | 372 image_color); |
353 | 373 |
354 // Clean up. | 374 // Clean up. |
355 glDeleteTextures(1, &texture); | 375 glDeleteTextures(1, &texture); |
356 glDeleteFramebuffersEXT(1, &framebuffer); | 376 glDeleteFramebuffersEXT(1, &framebuffer); |
357 image->Destroy(true /* have_context */); | 377 image->Destroy(true /* have_context */); |
378 if (vao) { | |
379 glDeleteVertexArraysOES(1, &vao); | |
380 } | |
358 } | 381 } |
359 | 382 |
360 // The GLImageCopyTest test case verifies that the GLImage implementation | 383 // The GLImageCopyTest test case verifies that the GLImage implementation |
361 // handles CopyTexImage correctly. | 384 // handles CopyTexImage correctly. |
362 REGISTER_TYPED_TEST_CASE_P(GLImageCopyTest, CopyTexImage); | 385 REGISTER_TYPED_TEST_CASE_P(GLImageCopyTest, CopyTexImage); |
363 | 386 |
364 } // namespace gl | 387 } // namespace gl |
365 | 388 |
366 #endif // UI_GL_TEST_GL_IMAGE_TEST_TEMPLATE_H_ | 389 #endif // UI_GL_TEST_GL_IMAGE_TEST_TEMPLATE_H_ |
OLD | NEW |