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 28 matching lines...) Expand all Loading... |
39 context_->MakeCurrent(surface_.get()); | 39 context_->MakeCurrent(surface_.get()); |
40 } | 40 } |
41 void TearDown() override { | 41 void TearDown() override { |
42 context_->ReleaseCurrent(surface_.get()); | 42 context_->ReleaseCurrent(surface_.get()); |
43 context_ = nullptr; | 43 context_ = nullptr; |
44 surface_ = nullptr; | 44 surface_ = nullptr; |
45 GLImageTestSupport::CleanupGL(); | 45 GLImageTestSupport::CleanupGL(); |
46 } | 46 } |
47 | 47 |
48 protected: | 48 protected: |
| 49 // Create GL program drawing a texture on a framebuffer. |
| 50 static GLuint CreateSingleTextureProgram() { |
| 51 // clang-format off |
| 52 const char kVertexShader[] = STRINGIZE( |
| 53 attribute vec2 a_position; |
| 54 varying vec2 v_texCoord; |
| 55 void main() { |
| 56 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); |
| 57 v_texCoord = (a_position + vec2(1.0, 1.0)) * 0.5; |
| 58 } |
| 59 ); |
| 60 const char kFragmentShader[] = STRINGIZE( |
| 61 uniform sampler2D a_texture; |
| 62 varying vec2 v_texCoord; |
| 63 void main() { |
| 64 gl_FragColor = texture2D(a_texture, v_texCoord); |
| 65 } |
| 66 ); |
| 67 const char kShaderFloatPrecision[] = STRINGIZE( |
| 68 precision mediump float; |
| 69 ); |
| 70 // clang-format on |
| 71 |
| 72 GLuint vertex_shader = |
| 73 gfx::GLHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader); |
| 74 bool is_gles = gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2; |
| 75 GLuint fragment_shader = gfx::GLHelper::LoadShader( |
| 76 GL_FRAGMENT_SHADER, |
| 77 base::StringPrintf("%s%s", is_gles ? kShaderFloatPrecision : "", |
| 78 kFragmentShader) |
| 79 .c_str()); |
| 80 GLuint program = |
| 81 gfx::GLHelper::SetupProgram(vertex_shader, fragment_shader); |
| 82 DCHECK_NE(program, 0u); |
| 83 glUseProgram(program); |
| 84 |
| 85 GLint sampler_location = glGetUniformLocation(program, "a_texture"); |
| 86 DCHECK_NE(sampler_location, -1); |
| 87 glUniform1i(sampler_location, 0); |
| 88 |
| 89 glDeleteShader(vertex_shader); |
| 90 glDeleteShader(fragment_shader); |
| 91 |
| 92 return program; |
| 93 } |
| 94 |
49 scoped_refptr<gfx::GLSurface> surface_; | 95 scoped_refptr<gfx::GLSurface> surface_; |
50 scoped_refptr<gfx::GLContext> context_; | 96 scoped_refptr<gfx::GLContext> context_; |
51 GLImageTestDelegate delegate_; | 97 GLImageTestDelegate delegate_; |
52 }; | 98 }; |
53 | 99 |
54 TYPED_TEST_CASE_P(GLImageTest); | 100 TYPED_TEST_CASE_P(GLImageTest); |
55 | 101 |
56 TYPED_TEST_P(GLImageTest, CreateAndDestroy) { | 102 TYPED_TEST_P(GLImageTest, CreateAndDestroy) { |
57 const gfx::Size small_image_size(4, 4); | 103 const gfx::Size small_image_size(4, 4); |
58 const gfx::Size large_image_size(512, 512); | 104 const gfx::Size large_image_size(512, 512); |
59 const uint8_t image_color[] = {0, 0xff, 0, 0xff}; | 105 const uint8_t image_color[] = {0, 0xff, 0, 0xff}; |
60 | 106 |
61 // Create a small solid color green image of preferred format. This must | 107 for (auto format : gfx::GetBufferFormatsForTesting()) { |
62 // succeed in order for a GLImage to be conformant. | 108 if (!TypeParam::IsSupported(format)) { |
63 scoped_refptr<gl::GLImage> small_image = | 109 continue; |
64 this->delegate_.CreateSolidColorImage(small_image_size, image_color); | 110 } |
65 ASSERT_TRUE(small_image); | |
66 | 111 |
67 // Create a large solid color green image of preferred format. This must | 112 // Create a small solid color green image of preferred format. This must |
68 // succeed in order for a GLImage to be conformant. | 113 // succeed in order for a GLImage to be conformant. |
69 scoped_refptr<gl::GLImage> large_image = | 114 scoped_refptr<gl::GLImage> small_image = |
70 this->delegate_.CreateSolidColorImage(large_image_size, image_color); | 115 this->delegate_.CreateSolidColorImage(small_image_size, format, |
71 ASSERT_TRUE(large_image); | 116 image_color); |
| 117 ASSERT_TRUE(small_image); |
72 | 118 |
73 // Verify that image size is correct. | 119 // Create a large solid color green image of preferred format. This must |
74 EXPECT_EQ(small_image->GetSize().ToString(), small_image_size.ToString()); | 120 // succeed in order for a GLImage to be conformant. |
75 EXPECT_EQ(large_image->GetSize().ToString(), large_image_size.ToString()); | 121 scoped_refptr<gl::GLImage> large_image = |
| 122 this->delegate_.CreateSolidColorImage(large_image_size, format, |
| 123 image_color); |
| 124 ASSERT_TRUE(large_image); |
76 | 125 |
77 // Verify that destruction of images work correctly both when we have a | 126 // Verify that image size is correct. |
78 // context and when we don't. | 127 EXPECT_EQ(small_image->GetSize().ToString(), small_image_size.ToString()); |
79 small_image->Destroy(true /* have_context */); | 128 EXPECT_EQ(large_image->GetSize().ToString(), large_image_size.ToString()); |
80 large_image->Destroy(false /* have_context */); | 129 |
| 130 // Verify that destruction of images work correctly both when we have a |
| 131 // context and when we don't. |
| 132 small_image->Destroy(true /* have_context */); |
| 133 large_image->Destroy(false /* have_context */); |
| 134 } |
81 } | 135 } |
82 | 136 |
83 // The GLImageTest test case verifies the behaviour that is expected from a | 137 // The GLImageTest test case verifies the behaviour that is expected from a |
84 // GLImage in order to be conformant. | 138 // GLImage in order to be conformant. |
85 REGISTER_TYPED_TEST_CASE_P(GLImageTest, CreateAndDestroy); | 139 REGISTER_TYPED_TEST_CASE_P(GLImageTest, CreateAndDestroy); |
86 | 140 |
87 template <typename GLImageTestDelegate> | 141 template <typename GLImageTestDelegate> |
88 class GLImageCopyTest : public GLImageTest<GLImageTestDelegate> {}; | 142 class GLImageCopyTest : public GLImageTest<GLImageTestDelegate> {}; |
89 | 143 |
90 TYPED_TEST_CASE_P(GLImageCopyTest); | 144 TYPED_TEST_CASE_P(GLImageCopyTest); |
91 | 145 |
92 TYPED_TEST_P(GLImageCopyTest, CopyTexImage) { | 146 TYPED_TEST_P(GLImageCopyTest, CopyTexImage) { |
93 const gfx::Size image_size(256, 256); | 147 const gfx::Size image_size(256, 256); |
94 // These values are picked so that RGB -> YUV on the CPU converted | 148 // These values are picked so that RGB -> YUV on the CPU converted |
95 // back to RGB on the GPU produces the original RGB values without | 149 // back to RGB on the GPU produces the original RGB values without |
96 // any error. | 150 // any error. |
97 const uint8_t image_color[] = {0x10, 0x20, 0, 0xff}; | 151 const uint8_t image_color[] = {0x10, 0x20, 0, 0xff}; |
98 const uint8_t texture_color[] = {0, 0, 0xff, 0xff}; | 152 const uint8_t texture_color[] = {0, 0, 0xff, 0xff}; |
99 | 153 |
100 GLuint framebuffer = | 154 GLuint framebuffer = |
101 GLTestHelper::SetupFramebuffer(image_size.width(), image_size.height()); | 155 GLTestHelper::SetupFramebuffer(image_size.width(), image_size.height()); |
102 ASSERT_TRUE(framebuffer); | 156 ASSERT_TRUE(framebuffer); |
103 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer); | 157 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer); |
104 glViewport(0, 0, image_size.width(), image_size.height()); | 158 glViewport(0, 0, image_size.width(), image_size.height()); |
105 | 159 |
106 // Create a solid color green image of preferred format. This must succeed | 160 GLuint program = this->CreateSingleTextureProgram(); |
107 // in order for a GLImage to be conformant. | 161 GLuint vertex_buffer = gfx::GLHelper::SetupQuadVertexBuffer(); |
108 scoped_refptr<gl::GLImage> image = | |
109 this->delegate_.CreateSolidColorImage(image_size, image_color); | |
110 ASSERT_TRUE(image); | |
111 | 162 |
112 // Create a solid color blue texture of the same size as |image|. | 163 for (auto format : gfx::GetBufferFormatsForTesting()) { |
113 GLuint texture = GLTestHelper::CreateTexture(GL_TEXTURE_2D); | 164 if (!TypeParam::IsSupported(format)) { |
114 scoped_ptr<uint8_t[]> pixels(new uint8_t[BufferSizeForBufferFormat( | 165 continue; |
115 image_size, gfx::BufferFormat::RGBA_8888)]); | 166 } |
116 GLImageTestSupport::SetBufferDataToColor( | |
117 image_size.width(), image_size.height(), | |
118 static_cast<int>(RowSizeForBufferFormat(image_size.width(), | |
119 gfx::BufferFormat::RGBA_8888, 0)), | |
120 0, gfx::BufferFormat::RGBA_8888, texture_color, pixels.get()); | |
121 // Note: This test assume that |image| can be used with GL_TEXTURE_2D but | |
122 // that might not be the case for some GLImage implementations. | |
123 glBindTexture(GL_TEXTURE_2D, texture); | |
124 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_size.width(), | |
125 image_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels.get()); | |
126 | 167 |
127 // Copy |image| to |texture|. | 168 // Create a solid color green image of preferred format. This must succeed |
128 bool rv = image->CopyTexImage(GL_TEXTURE_2D); | 169 // in order for a GLImage to be conformant. |
129 EXPECT_TRUE(rv); | 170 scoped_refptr<gl::GLImage> image = |
| 171 this->delegate_.CreateSolidColorImage(image_size, format, image_color); |
| 172 ASSERT_TRUE(image); |
130 | 173 |
131 // clang-format off | 174 // Create a solid color blue texture of the same size as |image|. |
132 const char kVertexShader[] = STRINGIZE( | 175 GLuint texture = GLTestHelper::CreateTexture(GL_TEXTURE_2D); |
133 attribute vec2 a_position; | 176 scoped_ptr<uint8_t[]> pixels(new uint8_t[BufferSizeForBufferFormat( |
134 varying vec2 v_texCoord; | 177 image_size, gfx::BufferFormat::RGBA_8888)]); |
135 void main() { | 178 GLImageTestSupport::SetBufferDataToColor( |
136 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); | 179 image_size.width(), image_size.height(), |
137 v_texCoord = (a_position + vec2(1.0, 1.0)) * 0.5; | 180 static_cast<int>(RowSizeForBufferFormat( |
138 } | 181 image_size.width(), gfx::BufferFormat::RGBA_8888, 0)), |
139 ); | 182 0, gfx::BufferFormat::RGBA_8888, texture_color, pixels.get()); |
140 const char kFragmentShader[] = STRINGIZE( | 183 // Note: This test assume that |image| can be used with GL_TEXTURE_2D but |
141 uniform sampler2D a_texture; | 184 // that might not be the case for some GLImage implementations. |
142 varying vec2 v_texCoord; | 185 glBindTexture(GL_TEXTURE_2D, texture); |
143 void main() { | 186 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_size.width(), |
144 gl_FragColor = texture2D(a_texture, v_texCoord); | 187 image_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, |
145 } | 188 pixels.get()); |
146 ); | |
147 const char kShaderFloatPrecision[] = STRINGIZE( | |
148 precision mediump float; | |
149 ); | |
150 // clang-format on | |
151 | 189 |
152 GLuint vertex_shader = | 190 // Copy |image| to |texture|. |
153 gfx::GLHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader); | 191 bool rv = image->CopyTexImage(GL_TEXTURE_2D); |
154 bool is_gles = gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2; | 192 EXPECT_TRUE(rv); |
155 GLuint fragment_shader = gfx::GLHelper::LoadShader( | |
156 GL_FRAGMENT_SHADER, | |
157 base::StringPrintf("%s%s", is_gles ? kShaderFloatPrecision : "", | |
158 kFragmentShader) | |
159 .c_str()); | |
160 GLuint program = gfx::GLHelper::SetupProgram(vertex_shader, fragment_shader); | |
161 EXPECT_NE(program, 0u); | |
162 glUseProgram(program); | |
163 | 193 |
164 GLint sampler_location = glGetUniformLocation(program, "a_texture"); | 194 // Draw |texture| to viewport and read back pixels to check expectations. |
165 ASSERT_NE(sampler_location, -1); | 195 gfx::GLHelper::DrawQuad(vertex_buffer); |
166 glUniform1i(sampler_location, 0); | |
167 | 196 |
168 GLuint vertex_buffer = gfx::GLHelper::SetupQuadVertexBuffer(); | 197 GLTestHelper::CheckPixels(0, 0, image_size.width(), image_size.height(), |
169 // Draw |texture| to viewport and read back pixels to check expectations. | 198 image_color); |
170 gfx::GLHelper::DrawQuad(vertex_buffer); | |
171 | 199 |
172 GLTestHelper::CheckPixels(0, 0, image_size.width(), image_size.height(), | 200 // Clean up. |
173 image_color); | 201 glDeleteTextures(1, &texture); |
174 | 202 image->Destroy(true); |
175 // Clean up. | 203 } |
176 glDeleteProgram(program); | 204 glDeleteProgram(program); |
177 glDeleteShader(vertex_shader); | |
178 glDeleteShader(fragment_shader); | |
179 glDeleteBuffersARB(1, &vertex_buffer); | 205 glDeleteBuffersARB(1, &vertex_buffer); |
180 glDeleteTextures(1, &texture); | |
181 glDeleteFramebuffersEXT(1, &framebuffer); | 206 glDeleteFramebuffersEXT(1, &framebuffer); |
182 image->Destroy(true); | |
183 } | 207 } |
184 | 208 |
185 // The GLImageCopyTest test case verifies that the GLImage implementation | 209 // The GLImageCopyTest test case verifies that the GLImage implementation |
186 // handles CopyTexImage correctly. | 210 // handles CopyTexImage correctly. |
187 REGISTER_TYPED_TEST_CASE_P(GLImageCopyTest, CopyTexImage); | 211 REGISTER_TYPED_TEST_CASE_P(GLImageCopyTest, CopyTexImage); |
188 | 212 |
| 213 template <typename GLImageTestDelegate> |
| 214 class GLImageBindTest : public GLImageTest<GLImageTestDelegate> {}; |
| 215 |
| 216 TYPED_TEST_CASE_P(GLImageBindTest); |
| 217 |
| 218 TYPED_TEST_P(GLImageBindTest, BindTexImage) { |
| 219 const gfx::Size image_size(256, 256); |
| 220 const uint8_t image_color[] = {0x10, 0x20, 0, 0xff}; |
| 221 |
| 222 GLuint framebuffer = |
| 223 GLTestHelper::SetupFramebuffer(image_size.width(), image_size.height()); |
| 224 ASSERT_TRUE(framebuffer); |
| 225 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer); |
| 226 glViewport(0, 0, image_size.width(), image_size.height()); |
| 227 |
| 228 GLuint program = this->CreateSingleTextureProgram(); |
| 229 GLuint vertex_buffer = gfx::GLHelper::SetupQuadVertexBuffer(); |
| 230 |
| 231 for (auto format : gfx::GetBufferFormatsForTesting()) { |
| 232 if (!TypeParam::IsSupported(format)) { |
| 233 continue; |
| 234 } |
| 235 |
| 236 // Create a solid color green image of preferred format. This must succeed |
| 237 // in order for a GLImage to be conformant. |
| 238 scoped_refptr<gl::GLImage> image = |
| 239 this->delegate_.CreateSolidColorImage(image_size, format, image_color); |
| 240 ASSERT_TRUE(image); |
| 241 |
| 242 GLuint texture = GLTestHelper::CreateTexture(GL_TEXTURE_2D); |
| 243 |
| 244 // Bind |image| to |texture|. |
| 245 bool rv = image->BindTexImage(GL_TEXTURE_2D); |
| 246 EXPECT_TRUE(rv); |
| 247 |
| 248 // Draw |texture| to viewport and read back pixels to check expectations. |
| 249 gfx::GLHelper::DrawQuad(vertex_buffer); |
| 250 |
| 251 GLTestHelper::CheckPixels(0, 0, image_size.width(), image_size.height(), |
| 252 image_color); |
| 253 |
| 254 // Clean up. |
| 255 glDeleteTextures(1, &texture); |
| 256 image->Destroy(true); |
| 257 } |
| 258 glDeleteProgram(program); |
| 259 glDeleteBuffersARB(1, &vertex_buffer); |
| 260 glDeleteFramebuffersEXT(1, &framebuffer); |
| 261 } |
| 262 |
| 263 // The GLImageBindTest test case verifies that the GLImage implementation |
| 264 // handles BindTexImage correctly. |
| 265 REGISTER_TYPED_TEST_CASE_P(GLImageBindTest, BindTexImage); |
| 266 |
189 } // namespace gl | 267 } // namespace gl |
190 | 268 |
191 #endif // UI_GL_TEST_GL_IMAGE_TEST_TEMPLATE_H_ | 269 #endif // UI_GL_TEST_GL_IMAGE_TEST_TEMPLATE_H_ |
OLD | NEW |