| // Copyright 2012 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include <array> | |
| // This file looks like a unit test, but it contains benchmarks and test | |
| // utilities intended for manual evaluation of the scalers in | |
| // gl_helper*. These tests produce output in the form of files and printouts, | |
| // but cannot really "fail". There is no point in making these tests part | |
| // of any test automation run. | |
| #include <GLES2/gl2chromium.h> | |
| #include <GLES2/gl2ext.h> | |
| #include <GLES2/gl2extchromium.h> | |
| #include <stddef.h> | |
| #include <stdio.h> | |
| #include <cmath> | |
| #include <memory> | |
| #include <string> | |
| #include <vector> | |
| #include "base/at_exit.h" | |
| #include "base/command_line.h" | |
| #include "base/memory/raw_ptr.h" | |
| #include "base/run_loop.h" | |
| #include "base/strings/stringprintf.h" | |
| #include "base/test/task_environment.h" | |
| #include "base/time/time.h" | |
| #include "components/viz/test/test_gpu_service_holder.h" | |
| #include "gpu/command_buffer/client/gl_helper.h" | |
| #include "gpu/command_buffer/client/gl_helper_scaling.h" | |
| #include "gpu/command_buffer/client/gles2_implementation.h" | |
| #include "gpu/command_buffer/client/shared_memory_limits.h" | |
| #include "gpu/ipc/gl_in_process_context.h" | |
| #include "testing/gtest/include/gtest/gtest.h" | |
| #include "third_party/skia/include/core/SkBitmap.h" | |
| #include "ui/gfx/codec/png_codec.h" | |
| namespace gpu { | |
| namespace { | |
| constexpr auto kQualities = std::to_array<GLHelper::ScalerQuality>({ | |
| GLHelper::SCALER_QUALITY_BEST, | |
| GLHelper::SCALER_QUALITY_GOOD, | |
| GLHelper::SCALER_QUALITY_FAST, | |
| }); | |
| constexpr auto kQualityNames = std::to_array<const char*>({ | |
| "best", | |
| "good", | |
| "fast", | |
| }); | |
| } // namespace | |
| class GLHelperBenchmark : public testing::Test { | |
| protected: | |
| void SetUp() override { | |
| context_ = std::make_unique<GLInProcessContext>(); | |
| auto result = context_->Initialize( | |
| viz::TestGpuServiceHolder::GetInstance()->task_executor()); | |
| DCHECK_EQ(result, ContextResult::kSuccess); | |
| gl_ = context_->GetImplementation(); | |
| ContextSupport* support = context_->GetImplementation(); | |
| helper_ = std::make_unique<GLHelper>(gl_, support); | |
| helper_scaling_ = std::make_unique<GLHelperScaling>(gl_, helper_.get()); | |
| } | |
| void TearDown() override { | |
| helper_scaling_.reset(nullptr); | |
| helper_.reset(nullptr); | |
| gl_ = nullptr; | |
| context_.reset(nullptr); | |
| } | |
| base::test::TaskEnvironment task_environment_; | |
| std::unique_ptr<GLInProcessContext> context_; | |
| raw_ptr<gles2::GLES2Interface> gl_; // This is owned by |context_|. | |
| std::unique_ptr<GLHelper> helper_; | |
| std::unique_ptr<GLHelperScaling> helper_scaling_; | |
| base::circular_deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_; | |
| }; | |
| TEST_F(GLHelperBenchmark, ScaleBenchmark) { | |
| auto output_sizes = std::to_array<int>({ | |
| 1920, | |
| 1080, | |
| 1249, | |
| 720, // Output size on pixel | |
| 256, | |
| 144, | |
| }); | |
| auto input_sizes = std::to_array<int>({ | |
| 3200, | |
| 2040, | |
| 2560, | |
| 1476, // Pixel tab size | |
| 1920, | |
| 1080, | |
| 1280, | |
| 720, | |
| 800, | |
| 480, | |
| 256, | |
| 144, | |
| }); | |
| for (size_t q = 0; q < std::size(kQualities); q++) { | |
| for (size_t outsize = 0; outsize < std::size(output_sizes); outsize += 2) { | |
| for (size_t insize = 0; insize < std::size(input_sizes); insize += 2) { | |
| uint32_t src_texture; | |
| gl_->GenTextures(1, &src_texture); | |
| uint32_t dst_texture; | |
| gl_->GenTextures(1, &dst_texture); | |
| uint32_t framebuffer; | |
| gl_->GenFramebuffers(1, &framebuffer); | |
| const gfx::Size src_size(input_sizes[insize], input_sizes[insize + 1]); | |
| const gfx::Size dst_size(output_sizes[outsize], | |
| output_sizes[outsize + 1]); | |
| SkBitmap input; | |
| input.allocN32Pixels(src_size.width(), src_size.height()); | |
| SkBitmap output_pixels; | |
| output_pixels.allocN32Pixels(dst_size.width(), dst_size.height()); | |
| gl_->BindFramebuffer(GL_FRAMEBUFFER, framebuffer); | |
| gl_->BindTexture(GL_TEXTURE_2D, dst_texture); | |
| gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dst_size.width(), | |
| dst_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, | |
| nullptr); | |
| gl_->BindTexture(GL_TEXTURE_2D, src_texture); | |
| gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_size.width(), | |
| src_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, | |
| input.getPixels()); | |
| std::unique_ptr<GLHelper::ScalerInterface> scaler = | |
| helper_->CreateScaler( | |
| kQualities[q], | |
| gfx::Vector2d(src_size.width(), src_size.height()), | |
| gfx::Vector2d(dst_size.width(), dst_size.height()), false, | |
| false, false); | |
| // Scale once beforehand before we start measuring. | |
| const gfx::Rect output_rect(dst_size); | |
| scaler->Scale(src_texture, src_size, gfx::Vector2dF(), dst_texture, | |
| output_rect); | |
| gl_->Finish(); | |
| base::TimeTicks start_time = base::TimeTicks::Now(); | |
| int iterations = 0; | |
| base::TimeTicks end_time; | |
| while (true) { | |
| for (int i = 0; i < 50; i++) { | |
| iterations++; | |
| scaler->Scale(src_texture, src_size, gfx::Vector2dF(), dst_texture, | |
| output_rect); | |
| gl_->Flush(); | |
| } | |
| gl_->Finish(); | |
| end_time = base::TimeTicks::Now(); | |
| if (iterations > 2000) { | |
| break; | |
| } | |
| if ((end_time - start_time) > base::Seconds(1)) { | |
| break; | |
| } | |
| } | |
| gl_->DeleteTextures(1, &dst_texture); | |
| gl_->DeleteTextures(1, &src_texture); | |
| gl_->DeleteFramebuffers(1, &framebuffer); | |
| std::string name; | |
| name = base::StringPrintf("scale_%dx%d_to_%dx%d_%s", src_size.width(), | |
| src_size.height(), dst_size.width(), | |
| dst_size.height(), kQualityNames[q]); | |
| float ms = (end_time - start_time).InMillisecondsF() / iterations; | |
| VLOG(0) << base::StringPrintf("*RESULT gpu_scale_time: %s=%.2f ms\n", | |
| name.c_str(), ms); | |
| } | |
| } | |
| } | |
| } | |
| } // namespace gpu |