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 15 matching lines...) Expand all Loading... | |
26 using testing::Invoke; | 26 using testing::Invoke; |
27 using testing::Return; | 27 using testing::Return; |
28 using testing::SetArgPointee; | 28 using testing::SetArgPointee; |
29 using testing::StrictMock; | 29 using testing::StrictMock; |
30 | 30 |
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 static const int kImageBytesPerPixel = 4; | |
37 | 36 |
38 class GpuMemoryBufferTest : public testing::Test { | 37 class GpuMemoryBufferTest |
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 gl_.Initialize(GLManager::Options()); |
42 gl_.MakeCurrent(); | 42 gl_.MakeCurrent(); |
43 | 43 |
44 glGenTextures(2, texture_ids_); | 44 glGenTextures(2, texture_ids_); |
45 glBindTexture(GL_TEXTURE_2D, texture_ids_[1]); | 45 glBindTexture(GL_TEXTURE_2D, texture_ids_[1]); |
46 | 46 |
47 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 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); | 48 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
(...skipping 14 matching lines...) Expand all Loading... | |
63 glDeleteFramebuffers(1, &framebuffer_id_); | 63 glDeleteFramebuffers(1, &framebuffer_id_); |
64 | 64 |
65 gl_.Destroy(); | 65 gl_.Destroy(); |
66 } | 66 } |
67 | 67 |
68 GLManager gl_; | 68 GLManager gl_; |
69 GLuint texture_ids_[2]; | 69 GLuint texture_ids_[2]; |
70 GLuint framebuffer_id_; | 70 GLuint framebuffer_id_; |
71 }; | 71 }; |
72 | 72 |
73 namespace { | |
74 | |
75 std::vector<uint8> GetTexturePixel(const gfx::GpuMemoryBuffer::Format format) { | |
76 std::vector<uint8> pixel; | |
77 switch (format) { | |
78 case gfx::GpuMemoryBuffer::RGBA_8888: | |
79 pixel.push_back(255u); | |
80 pixel.push_back(0u); | |
81 pixel.push_back(0u); | |
82 pixel.push_back(255u); | |
83 return pixel; | |
84 case gfx::GpuMemoryBuffer::BGRA_8888: | |
85 pixel.push_back(0u); | |
86 pixel.push_back(0u); | |
87 pixel.push_back(255u); | |
88 pixel.push_back(255u); | |
89 return pixel; | |
90 case gfx::GpuMemoryBuffer::ATC: | |
91 case gfx::GpuMemoryBuffer::ATCIA: | |
92 case gfx::GpuMemoryBuffer::DXT1: | |
93 case gfx::GpuMemoryBuffer::DXT5: | |
94 case gfx::GpuMemoryBuffer::ETC1: | |
95 case gfx::GpuMemoryBuffer::RGBX_8888: | |
96 NOTREACHED(); | |
97 return std::vector<uint8>(); | |
98 } | |
99 | |
100 NOTREACHED(); | |
101 return std::vector<uint8>(); | |
102 } | |
103 | |
104 std::vector<uint8> GetFramebufferPixel( | |
105 const gfx::GpuMemoryBuffer::Format format) { | |
106 std::vector<uint8> pixel; | |
107 switch (format) { | |
108 case gfx::GpuMemoryBuffer::RGBA_8888: | |
109 case gfx::GpuMemoryBuffer::BGRA_8888: | |
110 pixel.push_back(255u); | |
111 pixel.push_back(0u); | |
112 pixel.push_back(0u); | |
113 pixel.push_back(255u); | |
114 return pixel; | |
115 case gfx::GpuMemoryBuffer::ATC: | |
116 case gfx::GpuMemoryBuffer::ATCIA: | |
117 case gfx::GpuMemoryBuffer::DXT1: | |
118 case gfx::GpuMemoryBuffer::DXT5: | |
119 case gfx::GpuMemoryBuffer::ETC1: | |
120 case gfx::GpuMemoryBuffer::RGBX_8888: | |
121 NOTREACHED(); | |
122 return std::vector<uint8>(); | |
123 } | |
124 | |
125 NOTREACHED(); | |
126 return std::vector<uint8>(); | |
127 } | |
128 | |
129 GLenum TextureFormat(gfx::GpuMemoryBuffer::Format format) { | |
130 switch (format) { | |
131 case gfx::GpuMemoryBuffer::RGBA_8888: | |
132 return GL_RGBA; | |
133 case gfx::GpuMemoryBuffer::BGRA_8888: | |
134 return GL_BGRA_EXT; | |
135 case gfx::GpuMemoryBuffer::ATC: | |
136 case gfx::GpuMemoryBuffer::ATCIA: | |
137 case gfx::GpuMemoryBuffer::DXT1: | |
138 case gfx::GpuMemoryBuffer::DXT5: | |
139 case gfx::GpuMemoryBuffer::ETC1: | |
140 case gfx::GpuMemoryBuffer::RGBX_8888: | |
141 NOTREACHED(); | |
142 return 0; | |
143 } | |
144 | |
145 NOTREACHED(); | |
146 return 0; | |
147 } | |
148 | |
149 } // namespace | |
150 | |
73 // An end to end test that tests the whole GpuMemoryBuffer lifecycle. | 151 // An end to end test that tests the whole GpuMemoryBuffer lifecycle. |
74 TEST_F(GpuMemoryBufferTest, Lifecycle) { | 152 TEST_P(GpuMemoryBufferTest, Lifecycle) { |
75 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u }; | |
76 | |
77 scoped_ptr<gfx::GpuMemoryBuffer> buffer(gl_.CreateGpuMemoryBuffer( | 153 scoped_ptr<gfx::GpuMemoryBuffer> buffer(gl_.CreateGpuMemoryBuffer( |
78 gfx::Size(kImageWidth, kImageHeight), gfx::GpuMemoryBuffer::RGBA_8888)); | 154 gfx::Size(kImageWidth, kImageHeight), GetParam())); |
79 | 155 |
80 // Map buffer for writing. | 156 // Map buffer for writing. |
81 void* data; | 157 void* data; |
82 bool rv = buffer->Map(&data); | 158 bool rv = buffer->Map(&data); |
83 DCHECK(rv); | 159 DCHECK(rv); |
84 | 160 |
85 uint8* mapped_buffer = static_cast<uint8*>(data); | 161 uint8* mapped_buffer = static_cast<uint8*>(data); |
86 ASSERT_TRUE(mapped_buffer != NULL); | 162 ASSERT_TRUE(mapped_buffer != NULL); |
87 | 163 |
88 // Assign a value to each pixel. | 164 // Assign a value to each pixel. |
89 int stride = kImageWidth * kImageBytesPerPixel; | 165 uint32 stride = 0; |
90 for (int x = 0; x < kImageWidth; ++x) { | 166 buffer->GetStride(&stride); |
91 for (int y = 0; y < kImageHeight; ++y) { | 167 ASSERT_NE(stride, 0u); |
92 mapped_buffer[y * stride + x * kImageBytesPerPixel + 0] = pixels[0]; | 168 std::vector<uint8> pixel = GetTexturePixel(GetParam()); |
93 mapped_buffer[y * stride + x * kImageBytesPerPixel + 1] = pixels[1]; | 169 for (int y = 0; y < kImageHeight; ++y) { |
94 mapped_buffer[y * stride + x * kImageBytesPerPixel + 2] = pixels[2]; | 170 for (uint32 x = 0; x < kImageWidth; ++x) { |
reveman
2015/04/06 16:02:49
nit: s/uint32/int/
Daniele Castagna
2015/04/06 17:59:05
Done.
| |
95 mapped_buffer[y * stride + x * kImageBytesPerPixel + 3] = pixels[3]; | 171 std::copy(pixel.begin(), pixel.end(), |
172 mapped_buffer + y * stride + x * pixel.size()); | |
96 } | 173 } |
97 } | 174 } |
98 | 175 |
99 // Unmap the buffer. | 176 // Unmap the buffer. |
100 buffer->Unmap(); | 177 buffer->Unmap(); |
101 | 178 |
102 // Create the image. This should add the image ID to the ImageManager. | 179 // Create the image. This should add the image ID to the ImageManager. |
103 GLuint image_id = glCreateImageCHROMIUM( | 180 GLuint image_id = glCreateImageCHROMIUM( |
104 buffer->AsClientBuffer(), kImageWidth, kImageHeight, GL_RGBA); | 181 buffer->AsClientBuffer(), kImageWidth, kImageHeight, GL_RGBA); |
105 EXPECT_NE(0u, image_id); | 182 EXPECT_NE(0u, image_id); |
106 EXPECT_TRUE(gl_.decoder()->GetImageManager()->LookupImage(image_id) != NULL); | 183 EXPECT_TRUE(gl_.decoder()->GetImageManager()->LookupImage(image_id) != NULL); |
107 | 184 |
108 // Bind the texture and the image. | 185 // Bind the texture and the image. |
109 glBindTexture(GL_TEXTURE_2D, texture_ids_[0]); | 186 glBindTexture(GL_TEXTURE_2D, texture_ids_[0]); |
110 glBindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); | 187 glBindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); |
111 | 188 |
112 // Copy texture so we can verify result using CheckPixels. | 189 // Copy texture so we can verify result using CheckPixels. |
113 glCopyTextureCHROMIUM(GL_TEXTURE_2D, | 190 glCopyTextureCHROMIUM(GL_TEXTURE_2D, texture_ids_[0], texture_ids_[1], |
114 texture_ids_[0], | 191 TextureFormat(GetParam()), GL_UNSIGNED_BYTE); |
115 texture_ids_[1], | |
116 GL_RGBA, | |
117 GL_UNSIGNED_BYTE); | |
118 EXPECT_TRUE(glGetError() == GL_NO_ERROR); | 192 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
119 | 193 |
120 // Check if pixels match the values that were assigned to the mapped buffer. | 194 // Check if pixels match the values that were assigned to the mapped buffer. |
121 GLTestHelper::CheckPixels(0, 0, kImageWidth, kImageHeight, 0, pixels); | 195 GLTestHelper::CheckPixels(0, 0, kImageWidth, kImageHeight, 0, |
196 &GetFramebufferPixel(GetParam()).front()); | |
122 EXPECT_TRUE(GL_NO_ERROR == glGetError()); | 197 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
123 | 198 |
124 // Release the image. | 199 // Release the image. |
125 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); | 200 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); |
126 | 201 |
127 // Destroy the image. | 202 // Destroy the image. |
128 glDestroyImageCHROMIUM(image_id); | 203 glDestroyImageCHROMIUM(image_id); |
129 } | 204 } |
130 | 205 |
206 INSTANTIATE_TEST_CASE_P(GpuMemoryBufferTests, | |
207 GpuMemoryBufferTest, | |
208 ::testing::Values(gfx::GpuMemoryBuffer::RGBA_8888, | |
209 gfx::GpuMemoryBuffer::BGRA_8888)); | |
210 | |
131 } // namespace gles2 | 211 } // namespace gles2 |
132 } // namespace gpu | 212 } // namespace gpu |
OLD | NEW |