OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include <GLES2/gl2.h> | 5 #include <GLES2/gl2.h> |
6 #include <GLES2/gl2chromium.h> | 6 #include <GLES2/gl2chromium.h> |
7 #include <GLES2/gl2ext.h> | 7 #include <GLES2/gl2ext.h> |
8 #include <GLES2/gl2extchromium.h> | 8 #include <GLES2/gl2extchromium.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 namespace gpu { | 31 namespace gpu { |
32 namespace gles2 { | 32 namespace gles2 { |
33 | 33 |
34 static const int kImageWidth = 32; | 34 static const int kImageWidth = 32; |
35 static const int kImageHeight = 32; | 35 static const int kImageHeight = 32; |
36 | 36 |
37 class GpuMemoryBufferTest | 37 class GpuMemoryBufferTest |
38 : public testing::TestWithParam<gfx::GpuMemoryBuffer::Format> { | 38 : public testing::TestWithParam<gfx::GpuMemoryBuffer::Format> { |
39 protected: | 39 protected: |
40 void SetUp() override { | 40 void SetUp() override { |
41 gl_.Initialize(GLManager::Options()); | 41 auto options = GLManager::Options(); |
42 options.size = gfx::Size(kImageWidth, kImageHeight); | |
43 gl_.Initialize(options); | |
42 gl_.MakeCurrent(); | 44 gl_.MakeCurrent(); |
43 | |
44 glGenTextures(2, texture_ids_); | |
45 glBindTexture(GL_TEXTURE_2D, texture_ids_[1]); | |
46 | |
47 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
48 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
50 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
51 | |
52 glGenFramebuffers(1, &framebuffer_id_); | |
53 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_); | |
54 glFramebufferTexture2D(GL_FRAMEBUFFER, | |
55 GL_COLOR_ATTACHMENT0, | |
56 GL_TEXTURE_2D, | |
57 texture_ids_[1], | |
58 0); | |
59 } | 45 } |
60 | 46 |
61 void TearDown() override { | 47 void TearDown() override { |
62 glDeleteTextures(2, texture_ids_); | |
63 glDeleteFramebuffers(1, &framebuffer_id_); | |
64 | |
65 gl_.Destroy(); | 48 gl_.Destroy(); |
66 } | 49 } |
67 | 50 |
68 GLManager gl_; | 51 GLManager gl_; |
69 GLuint texture_ids_[2]; | |
70 GLuint framebuffer_id_; | |
71 }; | 52 }; |
72 | 53 |
73 namespace { | 54 namespace { |
74 | 55 |
56 #define SHADER(Src) #Src | |
57 | |
58 // clang-format off | |
59 const char kVertexShader[] = | |
60 SHADER( | |
61 attribute vec4 a_position; | |
62 varying vec2 v_texCoord; | |
63 void main() { | |
64 gl_Position = a_position; | |
65 v_texCoord = vec2((a_position.x + 1.0) * 0.5, (a_position.y + 1.0) * 0.5); | |
66 } | |
67 ); | |
68 | |
69 const char* kFragmentShader = | |
70 SHADER( | |
71 precision mediump float; | |
72 uniform sampler2D a_texture; | |
73 varying vec2 v_texCoord; | |
74 void main() { | |
75 gl_FragColor = texture2D(a_texture, v_texCoord); | |
76 } | |
77 ); | |
78 // clang-format on | |
79 | |
75 std::vector<uint8> GetTexturePixel(const gfx::GpuMemoryBuffer::Format format) { | 80 std::vector<uint8> GetTexturePixel(const gfx::GpuMemoryBuffer::Format format) { |
76 std::vector<uint8> pixel; | 81 std::vector<uint8> pixel; |
77 switch (format) { | 82 switch (format) { |
78 case gfx::GpuMemoryBuffer::RGBA_8888: | 83 case gfx::GpuMemoryBuffer::RGBA_8888: |
79 pixel.push_back(255u); | 84 pixel.push_back(255u); |
80 pixel.push_back(0u); | 85 pixel.push_back(0u); |
81 pixel.push_back(0u); | 86 pixel.push_back(0u); |
82 pixel.push_back(255u); | 87 pixel.push_back(255u); |
83 return pixel; | 88 return pixel; |
84 case gfx::GpuMemoryBuffer::BGRA_8888: | 89 case gfx::GpuMemoryBuffer::BGRA_8888: |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 } | 148 } |
144 | 149 |
145 NOTREACHED(); | 150 NOTREACHED(); |
146 return 0; | 151 return 0; |
147 } | 152 } |
148 | 153 |
149 } // namespace | 154 } // namespace |
150 | 155 |
151 // An end to end test that tests the whole GpuMemoryBuffer lifecycle. | 156 // An end to end test that tests the whole GpuMemoryBuffer lifecycle. |
152 TEST_P(GpuMemoryBufferTest, Lifecycle) { | 157 TEST_P(GpuMemoryBufferTest, Lifecycle) { |
158 GLuint texture_id = 0; | |
159 glActiveTexture(GL_TEXTURE0); | |
reveman
2015/04/07 20:10:19
nit: unnecessary
Daniele Castagna
2015/04/07 20:15:53
Done.
| |
160 glGenTextures(1, &texture_id); | |
161 ASSERT_NE(0u, texture_id); | |
162 glBindTexture(GL_TEXTURE_2D, texture_id); | |
163 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
164 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
166 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
167 | |
168 // Create the gpu memory buffer. | |
153 scoped_ptr<gfx::GpuMemoryBuffer> buffer(gl_.CreateGpuMemoryBuffer( | 169 scoped_ptr<gfx::GpuMemoryBuffer> buffer(gl_.CreateGpuMemoryBuffer( |
154 gfx::Size(kImageWidth, kImageHeight), GetParam())); | 170 gfx::Size(kImageWidth, kImageHeight), GetParam())); |
155 | 171 |
156 // Map buffer for writing. | 172 // Map buffer for writing. |
157 void* data; | 173 void* data; |
158 bool rv = buffer->Map(&data); | 174 bool rv = buffer->Map(&data); |
159 DCHECK(rv); | 175 DCHECK(rv); |
160 | 176 |
161 uint8* mapped_buffer = static_cast<uint8*>(data); | 177 uint8* mapped_buffer = static_cast<uint8*>(data); |
162 ASSERT_TRUE(mapped_buffer != NULL); | 178 ASSERT_TRUE(mapped_buffer != NULL); |
(...skipping 10 matching lines...) Expand all Loading... | |
173 } | 189 } |
174 } | 190 } |
175 | 191 |
176 // Unmap the buffer. | 192 // Unmap the buffer. |
177 buffer->Unmap(); | 193 buffer->Unmap(); |
178 | 194 |
179 // Create the image. This should add the image ID to the ImageManager. | 195 // Create the image. This should add the image ID to the ImageManager. |
180 GLuint image_id = | 196 GLuint image_id = |
181 glCreateImageCHROMIUM(buffer->AsClientBuffer(), kImageWidth, kImageHeight, | 197 glCreateImageCHROMIUM(buffer->AsClientBuffer(), kImageWidth, kImageHeight, |
182 InternalFormat(GetParam())); | 198 InternalFormat(GetParam())); |
183 EXPECT_NE(0u, image_id); | 199 ASSERT_NE(0u, image_id); |
184 EXPECT_TRUE(gl_.decoder()->GetImageManager()->LookupImage(image_id) != NULL); | 200 ASSERT_TRUE(gl_.decoder()->GetImageManager()->LookupImage(image_id) != NULL); |
185 | 201 |
186 // Bind the texture and the image. | 202 // Bind the image. |
187 glBindTexture(GL_TEXTURE_2D, texture_ids_[0]); | |
188 glBindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); | 203 glBindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); |
189 | 204 |
190 // Copy texture so we can verify result using CheckPixels. | 205 // Build program, buffers and draw the texture. |
191 glCopyTextureCHROMIUM(GL_TEXTURE_2D, texture_ids_[0], texture_ids_[1], | 206 GLuint vertex_shader = |
192 InternalFormat(GetParam()), GL_UNSIGNED_BYTE); | 207 GLTestHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader); |
193 EXPECT_TRUE(glGetError() == GL_NO_ERROR); | 208 GLuint fragment_shader = |
209 GLTestHelper::LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); | |
210 GLuint program = GLTestHelper::SetupProgram(vertex_shader, fragment_shader); | |
211 ASSERT_NE(0u, program); | |
212 glUseProgram(program); | |
213 | |
214 GLint sampler_location = glGetUniformLocation(program, "a_texture"); | |
215 ASSERT_NE(-1, sampler_location); | |
216 glUniform1i(sampler_location, 0); | |
217 | |
218 GLuint vbo = | |
219 GLTestHelper::SetupUnitQuad(glGetAttribLocation(program, "a_position")); | |
220 ASSERT_NE(0u, vbo); | |
221 glViewport(0, 0, kImageWidth, kImageHeight); | |
222 glDrawArrays(GL_TRIANGLES, 0, 6); | |
223 ASSERT_TRUE(glGetError() == GL_NO_ERROR); | |
194 | 224 |
195 // Check if pixels match the values that were assigned to the mapped buffer. | 225 // Check if pixels match the values that were assigned to the mapped buffer. |
196 GLTestHelper::CheckPixels(0, 0, kImageWidth, kImageHeight, 0, | 226 GLTestHelper::CheckPixels(0, 0, kImageWidth, kImageHeight, 0, |
197 &GetFramebufferPixel(GetParam()).front()); | 227 &GetFramebufferPixel(GetParam()).front()); |
198 EXPECT_TRUE(GL_NO_ERROR == glGetError()); | 228 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
199 | 229 |
200 // Release the image. | 230 // Release the image. |
201 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); | 231 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); |
202 | 232 |
203 // Destroy the image. | 233 // Clean up. |
reveman
2015/04/07 20:10:19
nit: maybe improve the cleanup in a different patc
Daniele Castagna
2015/04/07 20:15:53
The clean up works now.
| |
234 glDeleteProgram(program); | |
235 glDeleteShader(vertex_shader); | |
236 glDeleteShader(fragment_shader); | |
237 // glDeleteBuffersARB(1, &vbo); | |
204 glDestroyImageCHROMIUM(image_id); | 238 glDestroyImageCHROMIUM(image_id); |
239 // glDeleteTextures(1, texture_id); | |
205 } | 240 } |
206 | 241 |
207 INSTANTIATE_TEST_CASE_P(GpuMemoryBufferTests, | 242 INSTANTIATE_TEST_CASE_P(GpuMemoryBufferTests, |
208 GpuMemoryBufferTest, | 243 GpuMemoryBufferTest, |
209 ::testing::Values(gfx::GpuMemoryBuffer::RGBA_8888, | 244 ::testing::Values(gfx::GpuMemoryBuffer::RGBA_8888, |
210 gfx::GpuMemoryBuffer::BGRA_8888)); | 245 gfx::GpuMemoryBuffer::BGRA_8888)); |
211 | 246 |
212 } // namespace gles2 | 247 } // namespace gles2 |
213 } // namespace gpu | 248 } // namespace gpu |
OLD | NEW |