Chromium Code Reviews| 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 { | |
|
reveman
2015/04/03 02:20:16
nit: we use a blank line between namespace and cod
Daniele Castagna
2015/04/03 02:56:14
Ack. It'll be there at the next iteration.
Daniele Castagna
2015/04/03 04:26:46
Done.
| |
| 74 std::vector<uint8> GetTexturePixel(const gfx::GpuMemoryBuffer::Format format) { | |
| 75 std::vector<uint8> pixel; | |
| 76 switch (format) { | |
| 77 case gfx::GpuMemoryBuffer::RGBA_8888: | |
| 78 pixel.push_back(255u); | |
| 79 pixel.push_back(0u); | |
| 80 pixel.push_back(0u); | |
| 81 pixel.push_back(255u); | |
| 82 return pixel; | |
| 83 case gfx::GpuMemoryBuffer::BGRA_8888: | |
| 84 pixel.push_back(0u); | |
| 85 pixel.push_back(0u); | |
| 86 pixel.push_back(255u); | |
| 87 pixel.push_back(255u); | |
| 88 return pixel; | |
| 89 default: | |
|
reveman
2015/04/03 02:20:16
Please avoid a default case and explicitly list al
Daniele Castagna
2015/04/03 02:56:14
I don't see why if someone adds a new format (to G
reveman
2015/04/03 03:41:02
If someone adds a new format to GpuMemoryBuffer, t
Daniele Castagna
2015/04/03 04:26:46
Done.
| |
| 90 NOTREACHED(); | |
| 91 return std::vector<uint8>(); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 std::vector<uint8> GetFramebufferPixel( | |
| 96 const gfx::GpuMemoryBuffer::Format format) { | |
| 97 std::vector<uint8> pixel; | |
| 98 switch (format) { | |
| 99 case gfx::GpuMemoryBuffer::RGBA_8888: | |
| 100 case gfx::GpuMemoryBuffer::BGRA_8888: | |
| 101 pixel.push_back(255u); | |
| 102 pixel.push_back(0u); | |
| 103 pixel.push_back(0u); | |
| 104 pixel.push_back(255u); | |
| 105 return pixel; | |
| 106 default: | |
|
reveman
2015/04/03 02:20:16
ditto
Daniele Castagna
2015/04/03 02:56:14
Ack.
Daniele Castagna
2015/04/03 04:26:46
Done.
| |
| 107 NOTREACHED(); | |
| 108 return std::vector<uint8>(); | |
| 109 } | |
| 110 } | |
| 111 } // namespace | |
|
reveman
2015/04/03 02:20:16
nit: we use a blank line between namespace and cod
Daniele Castagna
2015/04/03 02:56:14
Ack. I'll add it at the next iteration.
Daniele Castagna
2015/04/03 04:26:46
Done.
| |
| 112 | |
| 73 // An end to end test that tests the whole GpuMemoryBuffer lifecycle. | 113 // An end to end test that tests the whole GpuMemoryBuffer lifecycle. |
| 74 TEST_F(GpuMemoryBufferTest, Lifecycle) { | 114 TEST_P(GpuMemoryBufferTest, Lifecycle) { |
| 75 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u }; | |
| 76 | |
| 77 scoped_ptr<gfx::GpuMemoryBuffer> buffer(gl_.CreateGpuMemoryBuffer( | 115 scoped_ptr<gfx::GpuMemoryBuffer> buffer(gl_.CreateGpuMemoryBuffer( |
| 78 gfx::Size(kImageWidth, kImageHeight), gfx::GpuMemoryBuffer::RGBA_8888)); | 116 gfx::Size(kImageWidth, kImageHeight), GetParam())); |
| 79 | 117 |
| 80 // Map buffer for writing. | 118 // Map buffer for writing. |
| 81 void* data; | 119 void* data; |
| 82 bool rv = buffer->Map(&data); | 120 bool rv = buffer->Map(&data); |
| 83 DCHECK(rv); | 121 DCHECK(rv); |
| 84 | 122 |
| 85 uint8* mapped_buffer = static_cast<uint8*>(data); | 123 uint8* mapped_buffer = static_cast<uint8*>(data); |
| 86 ASSERT_TRUE(mapped_buffer != NULL); | 124 ASSERT_TRUE(mapped_buffer != NULL); |
| 87 | 125 |
| 88 // Assign a value to each pixel. | 126 // Assign a value to each pixel. |
| 89 int stride = kImageWidth * kImageBytesPerPixel; | 127 uint32 stride = 0; |
| 90 for (int x = 0; x < kImageWidth; ++x) { | 128 buffer->GetStride(&stride); |
| 91 for (int y = 0; y < kImageHeight; ++y) { | 129 ASSERT_GE(stride, 0u); |
| 92 mapped_buffer[y * stride + x * kImageBytesPerPixel + 0] = pixels[0]; | 130 std::vector<uint8> pixel = GetTexturePixel(GetParam()); |
| 93 mapped_buffer[y * stride + x * kImageBytesPerPixel + 1] = pixels[1]; | 131 for (int y = 0; y < kImageHeight; ++y) { |
| 94 mapped_buffer[y * stride + x * kImageBytesPerPixel + 2] = pixels[2]; | 132 for (uint32 x_offset = 0; x_offset < stride; x_offset += pixel.size()) { |
|
reveman
2015/04/03 02:20:16
I don't think we should be writing past kImageWidt
Daniele Castagna
2015/04/03 02:56:14
Isn't this similar to what you were suggesting in
reveman
2015/04/03 03:41:02
memset(mapped_buffer + y * stride, 0xff, ..) works
| |
| 95 mapped_buffer[y * stride + x * kImageBytesPerPixel + 3] = pixels[3]; | 133 std::copy(pixel.begin(), pixel.end(), |
| 134 &mapped_buffer[y * stride + x_offset]); | |
| 96 } | 135 } |
| 97 } | 136 } |
| 98 | 137 |
| 99 // Unmap the buffer. | 138 // Unmap the buffer. |
| 100 buffer->Unmap(); | 139 buffer->Unmap(); |
| 101 | 140 |
| 102 // Create the image. This should add the image ID to the ImageManager. | 141 // Create the image. This should add the image ID to the ImageManager. |
| 103 GLuint image_id = glCreateImageCHROMIUM( | 142 GLuint image_id = glCreateImageCHROMIUM( |
| 104 buffer->AsClientBuffer(), kImageWidth, kImageHeight, GL_RGBA); | 143 buffer->AsClientBuffer(), kImageWidth, kImageHeight, GL_RGBA); |
| 105 EXPECT_NE(0u, image_id); | 144 EXPECT_NE(0u, image_id); |
| 106 EXPECT_TRUE(gl_.decoder()->GetImageManager()->LookupImage(image_id) != NULL); | 145 EXPECT_TRUE(gl_.decoder()->GetImageManager()->LookupImage(image_id) != NULL); |
| 107 | 146 |
| 108 // Bind the texture and the image. | 147 // Bind the texture and the image. |
| 109 glBindTexture(GL_TEXTURE_2D, texture_ids_[0]); | 148 glBindTexture(GL_TEXTURE_2D, texture_ids_[0]); |
| 110 glBindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); | 149 glBindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); |
| 111 | 150 |
| 112 // Copy texture so we can verify result using CheckPixels. | 151 // Copy texture so we can verify result using CheckPixels. |
| 113 glCopyTextureCHROMIUM(GL_TEXTURE_2D, | 152 glCopyTextureCHROMIUM(GL_TEXTURE_2D, |
| 114 texture_ids_[0], | 153 texture_ids_[0], |
| 115 texture_ids_[1], | 154 texture_ids_[1], |
| 116 GL_RGBA, | 155 GL_RGBA, |
| 117 GL_UNSIGNED_BYTE); | 156 GL_UNSIGNED_BYTE); |
| 118 EXPECT_TRUE(glGetError() == GL_NO_ERROR); | 157 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 119 | 158 |
| 120 // Check if pixels match the values that were assigned to the mapped buffer. | 159 // Check if pixels match the values that were assigned to the mapped buffer. |
| 121 GLTestHelper::CheckPixels(0, 0, kImageWidth, kImageHeight, 0, pixels); | 160 GLTestHelper::CheckPixels(0, 0, kImageWidth, kImageHeight, 0, |
| 161 &GetFramebufferPixel(GetParam()).front()); | |
| 122 EXPECT_TRUE(GL_NO_ERROR == glGetError()); | 162 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 123 | 163 |
| 124 // Release the image. | 164 // Release the image. |
| 125 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); | 165 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); |
| 126 | 166 |
| 127 // Destroy the image. | 167 // Destroy the image. |
| 128 glDestroyImageCHROMIUM(image_id); | 168 glDestroyImageCHROMIUM(image_id); |
| 129 } | 169 } |
| 130 | 170 |
| 171 INSTANTIATE_TEST_CASE_P(GpuMemoryBufferTests, | |
| 172 GpuMemoryBufferTest, | |
| 173 ::testing::Values(gfx::GpuMemoryBuffer::RGBA_8888, | |
| 174 gfx::GpuMemoryBuffer::BGRA_8888)); | |
| 175 | |
| 131 } // namespace gles2 | 176 } // namespace gles2 |
| 132 } // namespace gpu | 177 } // namespace gpu |
| OLD | NEW |