| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "gpu/command_buffer/tests/gl_test_utils.h" | 5 #include "gpu/command_buffer/tests/gl_test_utils.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 #include <stdio.h> |
| 9 |
| 6 #include <string> | 10 #include <string> |
| 7 #include <stdio.h> | 11 |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 14 |
| 12 // GCC requires these declarations, but MSVC requires they not be present. | 15 // GCC requires these declarations, but MSVC requires they not be present. |
| 13 #ifndef COMPILER_MSVC | 16 #ifndef COMPILER_MSVC |
| 14 const uint8 GLTestHelper::kCheckClearValue; | 17 const uint8_t GLTestHelper::kCheckClearValue; |
| 15 #endif | 18 #endif |
| 16 | 19 |
| 17 bool GLTestHelper::HasExtension(const char* extension) { | 20 bool GLTestHelper::HasExtension(const char* extension) { |
| 18 std::string extensions( | 21 std::string extensions( |
| 19 reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS))); | 22 reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS))); |
| 20 return extensions.find(extension) != std::string::npos; | 23 return extensions.find(extension) != std::string::npos; |
| 21 } | 24 } |
| 22 | 25 |
| 23 bool GLTestHelper::CheckGLError(const char* msg, int line) { | 26 bool GLTestHelper::CheckGLError(const char* msg, int line) { |
| 24 bool success = true; | 27 bool success = true; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 vertices[ii * 4 + jj] = color[jj]; | 135 vertices[ii * 4 + jj] = color[jj]; |
| 133 } | 136 } |
| 134 } | 137 } |
| 135 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, usage); | 138 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, usage); |
| 136 glEnableVertexAttribArray(location); | 139 glEnableVertexAttribArray(location); |
| 137 glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE, 0, 0); | 140 glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE, 0, 0); |
| 138 | 141 |
| 139 return vbo; | 142 return vbo; |
| 140 } | 143 } |
| 141 | 144 |
| 142 bool GLTestHelper::CheckPixels( | 145 bool GLTestHelper::CheckPixels(GLint x, |
| 143 GLint x, GLint y, GLsizei width, GLsizei height, GLint tolerance, | 146 GLint y, |
| 144 const uint8* color) { | 147 GLsizei width, |
| 148 GLsizei height, |
| 149 GLint tolerance, |
| 150 const uint8_t* color) { |
| 145 GLsizei size = width * height * 4; | 151 GLsizei size = width * height * 4; |
| 146 scoped_ptr<uint8[]> pixels(new uint8[size]); | 152 scoped_ptr<uint8_t[]> pixels(new uint8_t[size]); |
| 147 memset(pixels.get(), kCheckClearValue, size); | 153 memset(pixels.get(), kCheckClearValue, size); |
| 148 glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.get()); | 154 glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.get()); |
| 149 int bad_count = 0; | 155 int bad_count = 0; |
| 150 for (GLint yy = 0; yy < height; ++yy) { | 156 for (GLint yy = 0; yy < height; ++yy) { |
| 151 for (GLint xx = 0; xx < width; ++xx) { | 157 for (GLint xx = 0; xx < width; ++xx) { |
| 152 int offset = yy * width * 4 + xx * 4; | 158 int offset = yy * width * 4 + xx * 4; |
| 153 for (int jj = 0; jj < 4; ++jj) { | 159 for (int jj = 0; jj < 4; ++jj) { |
| 154 uint8 actual = pixels[offset + jj]; | 160 uint8_t actual = pixels[offset + jj]; |
| 155 uint8 expected = color[jj]; | 161 uint8_t expected = color[jj]; |
| 156 int diff = actual - expected; | 162 int diff = actual - expected; |
| 157 diff = diff < 0 ? -diff: diff; | 163 diff = diff < 0 ? -diff: diff; |
| 158 if (diff > tolerance) { | 164 if (diff > tolerance) { |
| 159 EXPECT_EQ(expected, actual) << " at " << (xx + x) << ", " << (yy + y) | 165 EXPECT_EQ(expected, actual) << " at " << (xx + x) << ", " << (yy + y) |
| 160 << " channel " << jj; | 166 << " channel " << jj; |
| 161 ++bad_count; | 167 ++bad_count; |
| 162 // Exit early just so we don't spam the log but we print enough | 168 // Exit early just so we don't spam the log but we print enough |
| 163 // to hopefully make it easy to diagnose the issue. | 169 // to hopefully make it easy to diagnose the issue. |
| 164 if (bad_count > 16) { | 170 if (bad_count > 16) { |
| 165 return false; | 171 return false; |
| 166 } | 172 } |
| 167 } | 173 } |
| 168 } | 174 } |
| 169 } | 175 } |
| 170 } | 176 } |
| 171 return bad_count == 0; | 177 return bad_count == 0; |
| 172 } | 178 } |
| 173 | 179 |
| 174 namespace { | 180 namespace { |
| 175 | 181 |
| 176 void Set16BitValue(uint8 dest[2], uint16 value) { | 182 void Set16BitValue(uint8_t dest[2], uint16_t value) { |
| 177 dest[0] = value & 0xFFu; | 183 dest[0] = value & 0xFFu; |
| 178 dest[1] = value >> 8; | 184 dest[1] = value >> 8; |
| 179 } | 185 } |
| 180 | 186 |
| 181 void Set32BitValue(uint8 dest[4], uint32 value) { | 187 void Set32BitValue(uint8_t dest[4], uint32_t value) { |
| 182 dest[0] = (value >> 0) & 0xFFu; | 188 dest[0] = (value >> 0) & 0xFFu; |
| 183 dest[1] = (value >> 8) & 0xFFu; | 189 dest[1] = (value >> 8) & 0xFFu; |
| 184 dest[2] = (value >> 16) & 0xFFu; | 190 dest[2] = (value >> 16) & 0xFFu; |
| 185 dest[3] = (value >> 24) & 0xFFu; | 191 dest[3] = (value >> 24) & 0xFFu; |
| 186 } | 192 } |
| 187 | 193 |
| 188 struct BitmapHeaderFile { | 194 struct BitmapHeaderFile { |
| 189 uint8 magic[2]; | 195 uint8_t magic[2]; |
| 190 uint8 size[4]; | 196 uint8_t size[4]; |
| 191 uint8 reserved[4]; | 197 uint8_t reserved[4]; |
| 192 uint8 offset[4]; | 198 uint8_t offset[4]; |
| 193 }; | 199 }; |
| 194 | 200 |
| 195 struct BitmapInfoHeader{ | 201 struct BitmapInfoHeader{ |
| 196 uint8 size[4]; | 202 uint8_t size[4]; |
| 197 uint8 width[4]; | 203 uint8_t width[4]; |
| 198 uint8 height[4]; | 204 uint8_t height[4]; |
| 199 uint8 planes[2]; | 205 uint8_t planes[2]; |
| 200 uint8 bit_count[2]; | 206 uint8_t bit_count[2]; |
| 201 uint8 compression[4]; | 207 uint8_t compression[4]; |
| 202 uint8 size_image[4]; | 208 uint8_t size_image[4]; |
| 203 uint8 x_pels_per_meter[4]; | 209 uint8_t x_pels_per_meter[4]; |
| 204 uint8 y_pels_per_meter[4]; | 210 uint8_t y_pels_per_meter[4]; |
| 205 uint8 clr_used[4]; | 211 uint8_t clr_used[4]; |
| 206 uint8 clr_important[4]; | 212 uint8_t clr_important[4]; |
| 207 }; | 213 }; |
| 208 | 214 |
| 209 } | 215 } |
| 210 | 216 |
| 211 bool GLTestHelper::SaveBackbufferAsBMP( | 217 bool GLTestHelper::SaveBackbufferAsBMP( |
| 212 const char* filename, int width, int height) { | 218 const char* filename, int width, int height) { |
| 213 FILE* fp = fopen(filename, "wb"); | 219 FILE* fp = fopen(filename, "wb"); |
| 214 EXPECT_TRUE(fp != NULL); | 220 EXPECT_TRUE(fp != NULL); |
| 215 glPixelStorei(GL_PACK_ALIGNMENT, 1); | 221 glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 216 int num_pixels = width * height; | 222 int num_pixels = width * height; |
| 217 int size = num_pixels * 4; | 223 int size = num_pixels * 4; |
| 218 scoped_ptr<uint8[]> data(new uint8[size]); | 224 scoped_ptr<uint8_t[]> data(new uint8_t[size]); |
| 219 uint8* pixels = data.get(); | 225 uint8_t* pixels = data.get(); |
| 220 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); | 226 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
| 221 | 227 |
| 222 // RGBA to BGRA | 228 // RGBA to BGRA |
| 223 for (int ii = 0; ii < num_pixels; ++ii) { | 229 for (int ii = 0; ii < num_pixels; ++ii) { |
| 224 int offset = ii * 4; | 230 int offset = ii * 4; |
| 225 uint8 t = pixels[offset + 0]; | 231 uint8_t t = pixels[offset + 0]; |
| 226 pixels[offset + 0] = pixels[offset + 2]; | 232 pixels[offset + 0] = pixels[offset + 2]; |
| 227 pixels[offset + 2] = t; | 233 pixels[offset + 2] = t; |
| 228 } | 234 } |
| 229 | 235 |
| 230 BitmapHeaderFile bhf; | 236 BitmapHeaderFile bhf; |
| 231 BitmapInfoHeader bih; | 237 BitmapInfoHeader bih; |
| 232 | 238 |
| 233 bhf.magic[0] = 'B'; | 239 bhf.magic[0] = 'B'; |
| 234 bhf.magic[1] = 'M'; | 240 bhf.magic[1] = 'M'; |
| 235 Set32BitValue(bhf.size, 0); | 241 Set32BitValue(bhf.size, 0); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 246 Set32BitValue(bih.y_pels_per_meter, 0); | 252 Set32BitValue(bih.y_pels_per_meter, 0); |
| 247 Set32BitValue(bih.clr_used, 0); | 253 Set32BitValue(bih.clr_used, 0); |
| 248 Set32BitValue(bih.clr_important, 0); | 254 Set32BitValue(bih.clr_important, 0); |
| 249 | 255 |
| 250 fwrite(&bhf, sizeof(bhf), 1, fp); | 256 fwrite(&bhf, sizeof(bhf), 1, fp); |
| 251 fwrite(&bih, sizeof(bih), 1, fp); | 257 fwrite(&bih, sizeof(bih), 1, fp); |
| 252 fwrite(pixels, size, 1, fp); | 258 fwrite(pixels, size, 1, fp); |
| 253 fclose(fp); | 259 fclose(fp); |
| 254 return true; | 260 return true; |
| 255 } | 261 } |
| OLD | NEW |