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 #include <stddef.h> |
| 6 #include <stdint.h> |
| 7 |
5 #include <algorithm> | 8 #include <algorithm> |
6 #include <vector> | 9 #include <vector> |
7 | 10 |
8 #include "base/containers/small_map.h" | 11 #include "base/containers/small_map.h" |
9 #include "base/logging.h" | 12 #include "base/logging.h" |
10 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
12 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
13 #include "gpu/perftests/measurements.h" | 16 #include "gpu/perftests/measurements.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 return GL_R8; | 112 return GL_R8; |
110 default: | 113 default: |
111 NOTREACHED(); | 114 NOTREACHED(); |
112 } | 115 } |
113 return 0; | 116 return 0; |
114 } | 117 } |
115 | 118 |
116 void GenerateTextureData(const gfx::Size& size, | 119 void GenerateTextureData(const gfx::Size& size, |
117 int bytes_per_pixel, | 120 int bytes_per_pixel, |
118 const int seed, | 121 const int seed, |
119 std::vector<uint8>* const pixels) { | 122 std::vector<uint8_t>* const pixels) { |
120 // Row bytes has to be multiple of 4 (GL_PACK_ALIGNMENT defaults to 4). | 123 // Row bytes has to be multiple of 4 (GL_PACK_ALIGNMENT defaults to 4). |
121 int stride = ((size.width() * bytes_per_pixel) + 3) & ~0x3; | 124 int stride = ((size.width() * bytes_per_pixel) + 3) & ~0x3; |
122 pixels->resize(size.height() * stride); | 125 pixels->resize(size.height() * stride); |
123 for (int y = 0; y < size.height(); ++y) { | 126 for (int y = 0; y < size.height(); ++y) { |
124 for (int x = 0; x < size.width(); ++x) { | 127 for (int x = 0; x < size.width(); ++x) { |
125 for (int channel = 0; channel < bytes_per_pixel; ++channel) { | 128 for (int channel = 0; channel < bytes_per_pixel; ++channel) { |
126 int index = y * stride + x * bytes_per_pixel; | 129 int index = y * stride + x * bytes_per_pixel; |
127 pixels->at(index) = (index + (seed << 2)) % (0x20 << channel); | 130 pixels->at(index) = (index + (seed << 2)) % (0x20 << channel); |
128 } | 131 } |
129 } | 132 } |
130 } | 133 } |
131 } | 134 } |
132 | 135 |
133 // Compare a buffer containing pixels in a specified format to GL_RGBA buffer | 136 // Compare a buffer containing pixels in a specified format to GL_RGBA buffer |
134 // where the former buffer have been uploaded as a texture and drawn on the | 137 // where the former buffer have been uploaded as a texture and drawn on the |
135 // RGBA buffer. | 138 // RGBA buffer. |
136 bool CompareBufferToRGBABuffer(GLenum format, | 139 bool CompareBufferToRGBABuffer(GLenum format, |
137 const gfx::Size& size, | 140 const gfx::Size& size, |
138 const std::vector<uint8>& pixels, | 141 const std::vector<uint8_t>& pixels, |
139 const std::vector<uint8>& rgba) { | 142 const std::vector<uint8_t>& rgba) { |
140 int bytes_per_pixel = GLFormatBytePerPixel(format); | 143 int bytes_per_pixel = GLFormatBytePerPixel(format); |
141 int pixels_stride = ((size.width() * bytes_per_pixel) + 3) & ~0x3; | 144 int pixels_stride = ((size.width() * bytes_per_pixel) + 3) & ~0x3; |
142 int rgba_stride = size.width() * GLFormatBytePerPixel(GL_RGBA); | 145 int rgba_stride = size.width() * GLFormatBytePerPixel(GL_RGBA); |
143 for (int y = 0; y < size.height(); ++y) { | 146 for (int y = 0; y < size.height(); ++y) { |
144 for (int x = 0; x < size.width(); ++x) { | 147 for (int x = 0; x < size.width(); ++x) { |
145 int rgba_index = y * rgba_stride + x * GLFormatBytePerPixel(GL_RGBA); | 148 int rgba_index = y * rgba_stride + x * GLFormatBytePerPixel(GL_RGBA); |
146 int pixels_index = y * pixels_stride + x * bytes_per_pixel; | 149 int pixels_index = y * pixels_stride + x * bytes_per_pixel; |
147 uint8 expected[4] = {0}; | 150 uint8_t expected[4] = {0}; |
148 switch (format) { | 151 switch (format) { |
149 case GL_LUMINANCE: // (L_t, L_t, L_t, 1) | 152 case GL_LUMINANCE: // (L_t, L_t, L_t, 1) |
150 expected[1] = pixels[pixels_index]; | 153 expected[1] = pixels[pixels_index]; |
151 expected[2] = pixels[pixels_index]; | 154 expected[2] = pixels[pixels_index]; |
152 case GL_RED: // (R_t, 0, 0, 1) | 155 case GL_RED: // (R_t, 0, 0, 1) |
153 expected[0] = pixels[pixels_index]; | 156 expected[0] = pixels[pixels_index]; |
154 expected[3] = 255; | 157 expected[3] = 255; |
155 break; | 158 break; |
156 case GL_RGBA: // (R_t, G_t, B_t, A_t) | 159 case GL_RGBA: // (R_t, G_t, B_t, A_t) |
157 memcpy(expected, &pixels[pixels_index], 4); | 160 memcpy(expected, &pixels[pixels_index], 4); |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 size.width(), size.height(), 0, format, GL_UNSIGNED_BYTE, | 326 size.width(), size.height(), 0, format, GL_UNSIGNED_BYTE, |
324 nullptr); | 327 nullptr); |
325 CheckNoGlError("glTexImage2D"); | 328 CheckNoGlError("glTexImage2D"); |
326 } | 329 } |
327 } | 330 } |
328 return texture_id; | 331 return texture_id; |
329 } | 332 } |
330 | 333 |
331 void UploadTexture(GLuint texture_id, | 334 void UploadTexture(GLuint texture_id, |
332 const gfx::Size& size, | 335 const gfx::Size& size, |
333 const std::vector<uint8>& pixels, | 336 const std::vector<uint8_t>& pixels, |
334 GLenum format, | 337 GLenum format, |
335 const bool subimage) { | 338 const bool subimage) { |
336 if (subimage) { | 339 if (subimage) { |
337 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size.width(), size.height(), | 340 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size.width(), size.height(), |
338 format, GL_UNSIGNED_BYTE, &pixels[0]); | 341 format, GL_UNSIGNED_BYTE, &pixels[0]); |
339 CheckNoGlError("glTexSubImage2D"); | 342 CheckNoGlError("glTexSubImage2D"); |
340 } else { | 343 } else { |
341 glTexImage2D(GL_TEXTURE_2D, 0, GLFormatToInternalFormat(format), | 344 glTexImage2D(GL_TEXTURE_2D, 0, GLFormatToInternalFormat(format), |
342 size.width(), size.height(), 0, format, GL_UNSIGNED_BYTE, | 345 size.width(), size.height(), 0, format, GL_UNSIGNED_BYTE, |
343 &pixels[0]); | 346 &pixels[0]); |
344 CheckNoGlError("glTexImage2D"); | 347 CheckNoGlError("glTexImage2D"); |
345 } | 348 } |
346 } | 349 } |
347 | 350 |
348 // Upload and draw on the offscren surface. | 351 // Upload and draw on the offscren surface. |
349 // Return a list of pair. Each pair describe a gl operation and the wall | 352 // Return a list of pair. Each pair describe a gl operation and the wall |
350 // time elapsed in milliseconds. | 353 // time elapsed in milliseconds. |
351 std::vector<Measurement> UploadAndDraw(GLuint texture_id, | 354 std::vector<Measurement> UploadAndDraw(GLuint texture_id, |
352 const gfx::Size& size, | 355 const gfx::Size& size, |
353 const std::vector<uint8>& pixels, | 356 const std::vector<uint8_t>& pixels, |
354 const GLenum format, | 357 const GLenum format, |
355 const bool subimage) { | 358 const bool subimage) { |
356 MeasurementTimers tex_timers(gpu_timing_client_.get()); | 359 MeasurementTimers tex_timers(gpu_timing_client_.get()); |
357 UploadTexture(texture_id, size, pixels, format, subimage); | 360 UploadTexture(texture_id, size, pixels, format, subimage); |
358 tex_timers.Record(); | 361 tex_timers.Record(); |
359 | 362 |
360 MeasurementTimers first_draw_timers(gpu_timing_client_.get()); | 363 MeasurementTimers first_draw_timers(gpu_timing_client_.get()); |
361 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | 364 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
362 first_draw_timers.Record(); | 365 first_draw_timers.Record(); |
363 | 366 |
364 MeasurementTimers draw_timers(gpu_timing_client_.get()); | 367 MeasurementTimers draw_timers(gpu_timing_client_.get()); |
365 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | 368 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
366 draw_timers.Record(); | 369 draw_timers.Record(); |
367 | 370 |
368 MeasurementTimers finish_timers(gpu_timing_client_.get()); | 371 MeasurementTimers finish_timers(gpu_timing_client_.get()); |
369 glFinish(); | 372 glFinish(); |
370 CheckNoGlError("glFinish"); | 373 CheckNoGlError("glFinish"); |
371 finish_timers.Record(); | 374 finish_timers.Record(); |
372 | 375 |
373 std::vector<uint8> pixels_rendered(size.GetArea() * 4); | 376 std::vector<uint8_t> pixels_rendered(size.GetArea() * 4); |
374 glReadPixels(0, 0, size.width(), size.height(), GL_RGBA, GL_UNSIGNED_BYTE, | 377 glReadPixels(0, 0, size.width(), size.height(), GL_RGBA, GL_UNSIGNED_BYTE, |
375 &pixels_rendered[0]); | 378 &pixels_rendered[0]); |
376 CheckNoGlError("glReadPixels"); | 379 CheckNoGlError("glReadPixels"); |
377 EXPECT_TRUE( | 380 EXPECT_TRUE( |
378 CompareBufferToRGBABuffer(format, size, pixels, pixels_rendered)) | 381 CompareBufferToRGBABuffer(format, size, pixels, pixels_rendered)) |
379 << "Format is: " << gfx::GLEnums::GetStringEnum(format); | 382 << "Format is: " << gfx::GLEnums::GetStringEnum(format); |
380 | 383 |
381 std::vector<Measurement> measurements; | 384 std::vector<Measurement> measurements; |
382 bool gpu_timer_errors = | 385 bool gpu_timer_errors = |
383 gpu_timing_client_->IsAvailable() && | 386 gpu_timing_client_->IsAvailable() && |
384 gpu_timing_client_->CheckAndResetTimerErrors(); | 387 gpu_timing_client_->CheckAndResetTimerErrors(); |
385 if (!gpu_timer_errors) { | 388 if (!gpu_timer_errors) { |
386 measurements.push_back(tex_timers.GetAsMeasurement( | 389 measurements.push_back(tex_timers.GetAsMeasurement( |
387 subimage ? "texsubimage2d" : "teximage2d")); | 390 subimage ? "texsubimage2d" : "teximage2d")); |
388 measurements.push_back( | 391 measurements.push_back( |
389 first_draw_timers.GetAsMeasurement("firstdrawarrays")); | 392 first_draw_timers.GetAsMeasurement("firstdrawarrays")); |
390 measurements.push_back(draw_timers.GetAsMeasurement("drawarrays")); | 393 measurements.push_back(draw_timers.GetAsMeasurement("drawarrays")); |
391 measurements.push_back(finish_timers.GetAsMeasurement("finish")); | 394 measurements.push_back(finish_timers.GetAsMeasurement("finish")); |
392 } | 395 } |
393 return measurements; | 396 return measurements; |
394 } | 397 } |
395 | 398 |
396 void RunUploadAndDrawMultipleTimes(const gfx::Size& size, | 399 void RunUploadAndDrawMultipleTimes(const gfx::Size& size, |
397 const GLenum format, | 400 const GLenum format, |
398 const bool subimage) { | 401 const bool subimage) { |
399 std::vector<uint8> pixels; | 402 std::vector<uint8_t> pixels; |
400 base::SmallMap<std::map<std::string, Measurement>> | 403 base::SmallMap<std::map<std::string, Measurement>> |
401 aggregates; // indexed by name | 404 aggregates; // indexed by name |
402 int successful_runs = 0; | 405 int successful_runs = 0; |
403 GLuint texture_id = CreateGLTexture(format, size, subimage); | 406 GLuint texture_id = CreateGLTexture(format, size, subimage); |
404 for (int i = 0; i < kUploadPerfWarmupRuns + kUploadPerfTestRuns; ++i) { | 407 for (int i = 0; i < kUploadPerfWarmupRuns + kUploadPerfTestRuns; ++i) { |
405 GenerateTextureData(size, GLFormatBytePerPixel(format), i + 1, &pixels); | 408 GenerateTextureData(size, GLFormatBytePerPixel(format), i + 1, &pixels); |
406 auto run = UploadAndDraw(texture_id, size, pixels, format, subimage); | 409 auto run = UploadAndDraw(texture_id, size, pixels, format, subimage); |
407 if (i < kUploadPerfWarmupRuns || !run.size()) { | 410 if (i < kUploadPerfWarmupRuns || !run.size()) { |
408 continue; | 411 continue; |
409 } | 412 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 // every image it uploads it using texture_id and it draws multiple | 492 // every image it uploads it using texture_id and it draws multiple |
490 // times. The cpu/wall time and the gpu time for all the uploads and | 493 // times. The cpu/wall time and the gpu time for all the uploads and |
491 // draws, but before glFinish, is computed and is printed out at the end as | 494 // draws, but before glFinish, is computed and is printed out at the end as |
492 // "upload_and_draw". If the gpu time is >> than the cpu/wall time we expect the | 495 // "upload_and_draw". If the gpu time is >> than the cpu/wall time we expect the |
493 // driver to do texture renaming: this means that while the gpu is drawing using | 496 // driver to do texture renaming: this means that while the gpu is drawing using |
494 // texture_id it didn't block cpu side the texture upload using the same | 497 // texture_id it didn't block cpu side the texture upload using the same |
495 // texture_id. | 498 // texture_id. |
496 TEST_F(TextureUploadPerfTest, renaming) { | 499 TEST_F(TextureUploadPerfTest, renaming) { |
497 gfx::Size texture_size(fbo_size_.width() / 2, fbo_size_.height() / 2); | 500 gfx::Size texture_size(fbo_size_.width() / 2, fbo_size_.height() / 2); |
498 | 501 |
499 std::vector<uint8> pixels[4]; | 502 std::vector<uint8_t> pixels[4]; |
500 for (int i = 0; i < 4; ++i) { | 503 for (int i = 0; i < 4; ++i) { |
501 GenerateTextureData(texture_size, 4, i + 1, &pixels[i]); | 504 GenerateTextureData(texture_size, 4, i + 1, &pixels[i]); |
502 } | 505 } |
503 | 506 |
504 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | 507 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); |
505 GenerateVertexBuffer(texture_size); | 508 GenerateVertexBuffer(texture_size); |
506 | 509 |
507 gfx::Vector2dF positions[] = {gfx::Vector2dF(0.f, 0.f), | 510 gfx::Vector2dF positions[] = {gfx::Vector2dF(0.f, 0.f), |
508 gfx::Vector2dF(1.f, 0.f), | 511 gfx::Vector2dF(1.f, 0.f), |
509 gfx::Vector2dF(0.f, 1.f), | 512 gfx::Vector2dF(0.f, 1.f), |
(...skipping 16 matching lines...) Expand all Loading... |
526 | 529 |
527 upload_and_draw_timers.Record(); | 530 upload_and_draw_timers.Record(); |
528 MeasurementTimers finish_timers(gpu_timing_client_.get()); | 531 MeasurementTimers finish_timers(gpu_timing_client_.get()); |
529 glFinish(); | 532 glFinish(); |
530 CheckNoGlError("glFinish"); | 533 CheckNoGlError("glFinish"); |
531 finish_timers.Record(); | 534 finish_timers.Record(); |
532 | 535 |
533 glDeleteTextures(1, &texture_id); | 536 glDeleteTextures(1, &texture_id); |
534 | 537 |
535 for (int i = 0; i < 4; ++i) { | 538 for (int i = 0; i < 4; ++i) { |
536 std::vector<uint8> pixels_rendered(texture_size.GetArea() * 4); | 539 std::vector<uint8_t> pixels_rendered(texture_size.GetArea() * 4); |
537 glReadPixels(texture_size.width() * positions[i].x(), | 540 glReadPixels(texture_size.width() * positions[i].x(), |
538 texture_size.height() * positions[i].y(), texture_size.width(), | 541 texture_size.height() * positions[i].y(), texture_size.width(), |
539 texture_size.height(), GL_RGBA, GL_UNSIGNED_BYTE, | 542 texture_size.height(), GL_RGBA, GL_UNSIGNED_BYTE, |
540 &pixels_rendered[0]); | 543 &pixels_rendered[0]); |
541 CheckNoGlError("glReadPixels"); | 544 CheckNoGlError("glReadPixels"); |
542 ASSERT_EQ(pixels[i].size(), pixels_rendered.size()); | 545 ASSERT_EQ(pixels[i].size(), pixels_rendered.size()); |
543 EXPECT_EQ(pixels[i], pixels_rendered); | 546 EXPECT_EQ(pixels[i], pixels_rendered); |
544 } | 547 } |
545 | 548 |
546 bool gpu_timer_errors = gpu_timing_client_->IsAvailable() && | 549 bool gpu_timer_errors = gpu_timing_client_->IsAvailable() && |
547 gpu_timing_client_->CheckAndResetTimerErrors(); | 550 gpu_timing_client_->CheckAndResetTimerErrors(); |
548 if (!gpu_timer_errors) { | 551 if (!gpu_timer_errors) { |
549 upload_and_draw_timers.GetAsMeasurement("upload_and_draw") | 552 upload_and_draw_timers.GetAsMeasurement("upload_and_draw") |
550 .PrintResult("renaming"); | 553 .PrintResult("renaming"); |
551 finish_timers.GetAsMeasurement("finish").PrintResult("renaming"); | 554 finish_timers.GetAsMeasurement("finish").PrintResult("renaming"); |
552 } | 555 } |
553 } | 556 } |
554 | 557 |
555 } // namespace | 558 } // namespace |
556 } // namespace gpu | 559 } // namespace gpu |
OLD | NEW |