gl_helper_benchmark.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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
  3. // found in the LICENSE file.
  4. // This file looks like a unit test, but it contains benchmarks and test
  5. // utilities intended for manual evaluation of the scalers in
  6. // gl_helper*. These tests produce output in the form of files and printouts,
  7. // but cannot really "fail". There is no point in making these tests part
  8. // of any test automation run.
  9. #include <GLES2/gl2.h>
  10. #include <GLES2/gl2ext.h>
  11. #include <GLES2/gl2extchromium.h>
  12. #include <stddef.h>
  13. #include <stdio.h>
  14. #include <cmath>
  15. #include <memory>
  16. #include <string>
  17. #include <vector>
  18. #include "base/at_exit.h"
  19. #include "base/command_line.h"
  20. #include "base/files/file_util.h"
  21. #include "base/memory/raw_ptr.h"
  22. #include "base/run_loop.h"
  23. #include "base/strings/stringprintf.h"
  24. #include "base/test/task_environment.h"
  25. #include "base/threading/thread_task_runner_handle.h"
  26. #include "base/time/time.h"
  27. #include "components/viz/test/test_gpu_service_holder.h"
  28. #include "gpu/command_buffer/client/gl_helper.h"
  29. #include "gpu/command_buffer/client/gl_helper_scaling.h"
  30. #include "gpu/command_buffer/client/gles2_implementation.h"
  31. #include "gpu/command_buffer/client/shared_memory_limits.h"
  32. #include "gpu/ipc/gl_in_process_context.h"
  33. #include "testing/gtest/include/gtest/gtest.h"
  34. #include "third_party/skia/include/core/SkBitmap.h"
  35. #include "ui/gfx/codec/png_codec.h"
  36. namespace gpu {
  37. namespace {
  38. GLHelper::ScalerQuality kQualities[] = {
  39. GLHelper::SCALER_QUALITY_BEST,
  40. GLHelper::SCALER_QUALITY_GOOD,
  41. GLHelper::SCALER_QUALITY_FAST,
  42. };
  43. const char* const kQualityNames[] = {
  44. "best",
  45. "good",
  46. "fast",
  47. };
  48. } // namespace
  49. class GLHelperBenchmark : public testing::Test {
  50. protected:
  51. void SetUp() override {
  52. ContextCreationAttribs attributes;
  53. attributes.alpha_size = 8;
  54. attributes.depth_size = 24;
  55. attributes.red_size = 8;
  56. attributes.green_size = 8;
  57. attributes.blue_size = 8;
  58. attributes.stencil_size = 8;
  59. attributes.samples = 4;
  60. attributes.sample_buffers = 1;
  61. attributes.bind_generates_resource = false;
  62. attributes.gpu_preference = gl::GpuPreference::kHighPerformance;
  63. context_ = std::make_unique<GLInProcessContext>();
  64. auto result = context_->Initialize(
  65. viz::TestGpuServiceHolder::GetInstance()->task_executor(), attributes,
  66. SharedMemoryLimits(), /*image_factory=*/nullptr);
  67. DCHECK_EQ(result, ContextResult::kSuccess);
  68. gl_ = context_->GetImplementation();
  69. ContextSupport* support = context_->GetImplementation();
  70. helper_ = std::make_unique<GLHelper>(gl_, support);
  71. helper_scaling_ = std::make_unique<GLHelperScaling>(gl_, helper_.get());
  72. }
  73. void TearDown() override {
  74. helper_scaling_.reset(nullptr);
  75. helper_.reset(nullptr);
  76. context_.reset(nullptr);
  77. }
  78. void LoadPngFileToSkBitmap(const base::FilePath& filename, SkBitmap* bitmap) {
  79. std::string compressed;
  80. base::ReadFileToString(base::MakeAbsoluteFilePath(filename), &compressed);
  81. ASSERT_TRUE(compressed.size());
  82. ASSERT_TRUE(gfx::PNGCodec::Decode(
  83. reinterpret_cast<const unsigned char*>(compressed.data()),
  84. compressed.size(), bitmap));
  85. }
  86. // Save the image to a png file. Used to create the initial test files.
  87. void SaveToFile(SkBitmap* bitmap, const base::FilePath& filename) {
  88. std::vector<unsigned char> compressed;
  89. ASSERT_TRUE(gfx::PNGCodec::Encode(
  90. static_cast<unsigned char*>(bitmap->getPixels()),
  91. gfx::PNGCodec::FORMAT_BGRA,
  92. gfx::Size(bitmap->width(), bitmap->height()),
  93. static_cast<int>(bitmap->rowBytes()), true,
  94. std::vector<gfx::PNGCodec::Comment>(), &compressed));
  95. ASSERT_TRUE(compressed.size());
  96. FILE* f = base::OpenFile(filename, "wb");
  97. ASSERT_TRUE(f);
  98. ASSERT_EQ(fwrite(&*compressed.begin(), 1, compressed.size(), f),
  99. compressed.size());
  100. base::CloseFile(f);
  101. }
  102. base::test::TaskEnvironment task_environment_;
  103. std::unique_ptr<GLInProcessContext> context_;
  104. raw_ptr<gles2::GLES2Interface> gl_;
  105. std::unique_ptr<GLHelper> helper_;
  106. std::unique_ptr<GLHelperScaling> helper_scaling_;
  107. base::circular_deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_;
  108. };
  109. TEST_F(GLHelperBenchmark, ScaleBenchmark) {
  110. int output_sizes[] = {1920, 1080, 1249, 720, // Output size on pixel
  111. 256, 144};
  112. int input_sizes[] = {3200, 2040, 2560, 1476, // Pixel tab size
  113. 1920, 1080, 1280, 720, 800, 480, 256, 144};
  114. for (size_t q = 0; q < std::size(kQualities); q++) {
  115. for (size_t outsize = 0; outsize < std::size(output_sizes); outsize += 2) {
  116. for (size_t insize = 0; insize < std::size(input_sizes); insize += 2) {
  117. uint32_t src_texture;
  118. gl_->GenTextures(1, &src_texture);
  119. uint32_t dst_texture;
  120. gl_->GenTextures(1, &dst_texture);
  121. uint32_t framebuffer;
  122. gl_->GenFramebuffers(1, &framebuffer);
  123. const gfx::Size src_size(input_sizes[insize], input_sizes[insize + 1]);
  124. const gfx::Size dst_size(output_sizes[outsize],
  125. output_sizes[outsize + 1]);
  126. SkBitmap input;
  127. input.allocN32Pixels(src_size.width(), src_size.height());
  128. SkBitmap output_pixels;
  129. output_pixels.allocN32Pixels(dst_size.width(), dst_size.height());
  130. gl_->BindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  131. gl_->BindTexture(GL_TEXTURE_2D, dst_texture);
  132. gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dst_size.width(),
  133. dst_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE,
  134. nullptr);
  135. gl_->BindTexture(GL_TEXTURE_2D, src_texture);
  136. gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_size.width(),
  137. src_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE,
  138. input.getPixels());
  139. std::unique_ptr<GLHelper::ScalerInterface> scaler =
  140. helper_->CreateScaler(
  141. kQualities[q],
  142. gfx::Vector2d(src_size.width(), src_size.height()),
  143. gfx::Vector2d(dst_size.width(), dst_size.height()), false,
  144. false, false);
  145. // Scale once beforehand before we start measuring.
  146. const gfx::Rect output_rect(dst_size);
  147. scaler->Scale(src_texture, src_size, gfx::Vector2dF(), dst_texture,
  148. output_rect);
  149. gl_->Finish();
  150. base::TimeTicks start_time = base::TimeTicks::Now();
  151. int iterations = 0;
  152. base::TimeTicks end_time;
  153. while (true) {
  154. for (int i = 0; i < 50; i++) {
  155. iterations++;
  156. scaler->Scale(src_texture, src_size, gfx::Vector2dF(), dst_texture,
  157. output_rect);
  158. gl_->Flush();
  159. }
  160. gl_->Finish();
  161. end_time = base::TimeTicks::Now();
  162. if (iterations > 2000) {
  163. break;
  164. }
  165. if ((end_time - start_time) > base::Seconds(1)) {
  166. break;
  167. }
  168. }
  169. gl_->DeleteTextures(1, &dst_texture);
  170. gl_->DeleteTextures(1, &src_texture);
  171. gl_->DeleteFramebuffers(1, &framebuffer);
  172. std::string name;
  173. name = base::StringPrintf("scale_%dx%d_to_%dx%d_%s", src_size.width(),
  174. src_size.height(), dst_size.width(),
  175. dst_size.height(), kQualityNames[q]);
  176. float ms = (end_time - start_time).InMillisecondsF() / iterations;
  177. VLOG(0) << base::StringPrintf("*RESULT gpu_scale_time: %s=%.2f ms\n",
  178. name.c_str(), ms);
  179. }
  180. }
  181. }
  182. }
  183. } // namespace gpu