| OLD | NEW |
| (Empty) |
| 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 | |
| 5 // This file looks like a unit test, but it contains benchmarks and test | |
| 6 // utilities intended for manual evaluation of the scalers in | |
| 7 // gl_helper*. These tests produce output in the form of files and printouts, | |
| 8 // but cannot really "fail". There is no point in making these tests part | |
| 9 // of any test automation run. | |
| 10 | |
| 11 #include <stddef.h> | |
| 12 #include <stdio.h> | |
| 13 #include <cmath> | |
| 14 #include <string> | |
| 15 #include <vector> | |
| 16 | |
| 17 #include <GLES2/gl2.h> | |
| 18 #include <GLES2/gl2ext.h> | |
| 19 #include <GLES2/gl2extchromium.h> | |
| 20 | |
| 21 #include "base/at_exit.h" | |
| 22 #include "base/command_line.h" | |
| 23 #include "base/files/file_util.h" | |
| 24 #include "base/macros.h" | |
| 25 #include "base/strings/stringprintf.h" | |
| 26 #include "base/time/time.h" | |
| 27 #include "content/browser/compositor/gl_helper.h" | |
| 28 #include "content/browser/compositor/gl_helper_scaling.h" | |
| 29 #include "gpu/command_buffer/client/gl_in_process_context.h" | |
| 30 #include "gpu/command_buffer/client/gles2_implementation.h" | |
| 31 #include "gpu/command_buffer/client/shared_memory_limits.h" | |
| 32 #include "testing/gtest/include/gtest/gtest.h" | |
| 33 #include "third_party/skia/include/core/SkBitmap.h" | |
| 34 #include "third_party/skia/include/core/SkTypes.h" | |
| 35 #include "ui/gfx/codec/png_codec.h" | |
| 36 #include "ui/gl/gl_surface.h" | |
| 37 | |
| 38 namespace content { | |
| 39 | |
| 40 content::GLHelper::ScalerQuality kQualities[] = { | |
| 41 content::GLHelper::SCALER_QUALITY_BEST, | |
| 42 content::GLHelper::SCALER_QUALITY_GOOD, | |
| 43 content::GLHelper::SCALER_QUALITY_FAST, | |
| 44 }; | |
| 45 | |
| 46 const char* const kQualityNames[] = { | |
| 47 "best", "good", "fast", | |
| 48 }; | |
| 49 | |
| 50 class GLHelperTest : public testing::Test { | |
| 51 protected: | |
| 52 void SetUp() override { | |
| 53 gpu::gles2::ContextCreationAttribHelper attributes; | |
| 54 attributes.alpha_size = 8; | |
| 55 attributes.depth_size = 24; | |
| 56 attributes.red_size = 8; | |
| 57 attributes.green_size = 8; | |
| 58 attributes.blue_size = 8; | |
| 59 attributes.stencil_size = 8; | |
| 60 attributes.samples = 4; | |
| 61 attributes.sample_buffers = 1; | |
| 62 attributes.bind_generates_resource = false; | |
| 63 | |
| 64 context_.reset(gpu::GLInProcessContext::Create( | |
| 65 nullptr, /* service */ | |
| 66 nullptr, /* surface */ | |
| 67 true, /* offscreen */ | |
| 68 gfx::kNullAcceleratedWidget, /* window */ | |
| 69 gfx::Size(1, 1), /* size */ | |
| 70 nullptr, /* share_context */ | |
| 71 attributes, gfx::PreferDiscreteGpu, gpu::SharedMemoryLimits(), | |
| 72 nullptr, /* gpu_memory_buffer_manager */ | |
| 73 nullptr /* image_factory */)); | |
| 74 gl_ = context_->GetImplementation(); | |
| 75 gpu::ContextSupport* support = context_->GetImplementation(); | |
| 76 | |
| 77 helper_.reset(new content::GLHelper(gl_, support)); | |
| 78 helper_scaling_.reset(new content::GLHelperScaling(gl_, helper_.get())); | |
| 79 } | |
| 80 | |
| 81 void TearDown() override { | |
| 82 helper_scaling_.reset(NULL); | |
| 83 helper_.reset(NULL); | |
| 84 context_.reset(NULL); | |
| 85 } | |
| 86 | |
| 87 void LoadPngFileToSkBitmap(const base::FilePath& filename, SkBitmap* bitmap) { | |
| 88 std::string compressed; | |
| 89 base::ReadFileToString(base::MakeAbsoluteFilePath(filename), &compressed); | |
| 90 ASSERT_TRUE(compressed.size()); | |
| 91 ASSERT_TRUE(gfx::PNGCodec::Decode( | |
| 92 reinterpret_cast<const unsigned char*>(compressed.data()), | |
| 93 compressed.size(), bitmap)); | |
| 94 } | |
| 95 | |
| 96 // Save the image to a png file. Used to create the initial test files. | |
| 97 void SaveToFile(SkBitmap* bitmap, const base::FilePath& filename) { | |
| 98 std::vector<unsigned char> compressed; | |
| 99 ASSERT_TRUE(gfx::PNGCodec::Encode( | |
| 100 static_cast<unsigned char*>(bitmap->getPixels()), | |
| 101 gfx::PNGCodec::FORMAT_BGRA, | |
| 102 gfx::Size(bitmap->width(), bitmap->height()), | |
| 103 static_cast<int>(bitmap->rowBytes()), true, | |
| 104 std::vector<gfx::PNGCodec::Comment>(), &compressed)); | |
| 105 ASSERT_TRUE(compressed.size()); | |
| 106 FILE* f = base::OpenFile(filename, "wb"); | |
| 107 ASSERT_TRUE(f); | |
| 108 ASSERT_EQ(fwrite(&*compressed.begin(), 1, compressed.size(), f), | |
| 109 compressed.size()); | |
| 110 base::CloseFile(f); | |
| 111 } | |
| 112 | |
| 113 std::unique_ptr<gpu::GLInProcessContext> context_; | |
| 114 gpu::gles2::GLES2Interface* gl_; | |
| 115 std::unique_ptr<content::GLHelper> helper_; | |
| 116 std::unique_ptr<content::GLHelperScaling> helper_scaling_; | |
| 117 std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_; | |
| 118 }; | |
| 119 | |
| 120 TEST_F(GLHelperTest, ScaleBenchmark) { | |
| 121 int output_sizes[] = {1920, 1080, 1249, 720, // Output size on pixel | |
| 122 256, 144}; | |
| 123 int input_sizes[] = {3200, 2040, 2560, 1476, // Pixel tab size | |
| 124 1920, 1080, 1280, 720, 800, 480, 256, 144}; | |
| 125 | |
| 126 for (size_t q = 0; q < arraysize(kQualities); q++) { | |
| 127 for (size_t outsize = 0; outsize < arraysize(output_sizes); outsize += 2) { | |
| 128 for (size_t insize = 0; insize < arraysize(input_sizes); insize += 2) { | |
| 129 uint32_t src_texture; | |
| 130 gl_->GenTextures(1, &src_texture); | |
| 131 uint32_t dst_texture; | |
| 132 gl_->GenTextures(1, &dst_texture); | |
| 133 uint32_t framebuffer; | |
| 134 gl_->GenFramebuffers(1, &framebuffer); | |
| 135 const gfx::Size src_size(input_sizes[insize], input_sizes[insize + 1]); | |
| 136 const gfx::Size dst_size(output_sizes[outsize], | |
| 137 output_sizes[outsize + 1]); | |
| 138 SkBitmap input; | |
| 139 input.allocN32Pixels(src_size.width(), src_size.height()); | |
| 140 | |
| 141 SkBitmap output_pixels; | |
| 142 output_pixels.allocN32Pixels(dst_size.width(), dst_size.height()); | |
| 143 | |
| 144 gl_->BindFramebuffer(GL_FRAMEBUFFER, framebuffer); | |
| 145 gl_->BindTexture(GL_TEXTURE_2D, dst_texture); | |
| 146 gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dst_size.width(), | |
| 147 dst_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); | |
| 148 gl_->BindTexture(GL_TEXTURE_2D, src_texture); | |
| 149 gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_size.width(), | |
| 150 src_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, | |
| 151 input.getPixels()); | |
| 152 | |
| 153 gfx::Rect src_subrect(0, 0, src_size.width(), src_size.height()); | |
| 154 std::unique_ptr<content::GLHelper::ScalerInterface> scaler( | |
| 155 helper_->CreateScaler(kQualities[q], src_size, src_subrect, | |
| 156 dst_size, false, false)); | |
| 157 // Scale once beforehand before we start measuring. | |
| 158 scaler->Scale(src_texture, dst_texture); | |
| 159 gl_->Finish(); | |
| 160 | |
| 161 base::TimeTicks start_time = base::TimeTicks::Now(); | |
| 162 int iterations = 0; | |
| 163 base::TimeTicks end_time; | |
| 164 while (true) { | |
| 165 for (int i = 0; i < 50; i++) { | |
| 166 iterations++; | |
| 167 scaler->Scale(src_texture, dst_texture); | |
| 168 gl_->Flush(); | |
| 169 } | |
| 170 gl_->Finish(); | |
| 171 end_time = base::TimeTicks::Now(); | |
| 172 if (iterations > 2000) { | |
| 173 break; | |
| 174 } | |
| 175 if ((end_time - start_time).InMillisecondsF() > 1000) { | |
| 176 break; | |
| 177 } | |
| 178 } | |
| 179 gl_->DeleteTextures(1, &dst_texture); | |
| 180 gl_->DeleteTextures(1, &src_texture); | |
| 181 gl_->DeleteFramebuffers(1, &framebuffer); | |
| 182 | |
| 183 std::string name; | |
| 184 name = base::StringPrintf("scale_%dx%d_to_%dx%d_%s", src_size.width(), | |
| 185 src_size.height(), dst_size.width(), | |
| 186 dst_size.height(), kQualityNames[q]); | |
| 187 | |
| 188 float ms = (end_time - start_time).InMillisecondsF() / iterations; | |
| 189 VLOG(0) << base::StringPrintf("*RESULT gpu_scale_time: %s=%.2f ms\n", | |
| 190 name.c_str(), ms); | |
| 191 } | |
| 192 } | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 // This is more of a test utility than a test. | |
| 197 // Put an PNG image called "testimage.png" in your | |
| 198 // current directory, then run this test. It will | |
| 199 // create testoutput_Q_P.png, where Q is the scaling | |
| 200 // mode and P is the scaling percentage taken from | |
| 201 // the table below. | |
| 202 TEST_F(GLHelperTest, DISABLED_ScaleTestImage) { | |
| 203 int percents[] = { | |
| 204 230, 180, 150, 110, 90, 70, 50, 49, 40, 20, 10, | |
| 205 }; | |
| 206 | |
| 207 SkBitmap input; | |
| 208 LoadPngFileToSkBitmap(base::FilePath(FILE_PATH_LITERAL("testimage.png")), | |
| 209 &input); | |
| 210 | |
| 211 uint32_t framebuffer; | |
| 212 gl_->GenFramebuffers(1, &framebuffer); | |
| 213 uint32_t src_texture; | |
| 214 gl_->GenTextures(1, &src_texture); | |
| 215 const gfx::Size src_size(input.width(), input.height()); | |
| 216 gl_->BindFramebuffer(GL_FRAMEBUFFER, framebuffer); | |
| 217 gl_->BindTexture(GL_TEXTURE_2D, src_texture); | |
| 218 gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_size.width(), | |
| 219 src_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, | |
| 220 input.getPixels()); | |
| 221 | |
| 222 for (size_t q = 0; q < arraysize(kQualities); q++) { | |
| 223 for (size_t p = 0; p < arraysize(percents); p++) { | |
| 224 const gfx::Size dst_size(input.width() * percents[p] / 100, | |
| 225 input.height() * percents[p] / 100); | |
| 226 uint32_t dst_texture = helper_->CopyAndScaleTexture( | |
| 227 src_texture, src_size, dst_size, false, kQualities[q]); | |
| 228 | |
| 229 SkBitmap output_pixels; | |
| 230 output_pixels.allocN32Pixels(dst_size.width(), dst_size.height()); | |
| 231 | |
| 232 helper_->ReadbackTextureSync( | |
| 233 dst_texture, gfx::Rect(0, 0, dst_size.width(), dst_size.height()), | |
| 234 static_cast<unsigned char*>(output_pixels.getPixels()), | |
| 235 kN32_SkColorType); | |
| 236 gl_->DeleteTextures(1, &dst_texture); | |
| 237 std::string filename = base::StringPrintf("testoutput_%s_%d.ppm", | |
| 238 kQualityNames[q], percents[p]); | |
| 239 VLOG(0) << "Writing " << filename; | |
| 240 SaveToFile(&output_pixels, base::FilePath::FromUTF8Unsafe(filename)); | |
| 241 } | |
| 242 } | |
| 243 gl_->DeleteTextures(1, &src_texture); | |
| 244 gl_->DeleteFramebuffers(1, &framebuffer); | |
| 245 } | |
| 246 | |
| 247 } // namespace content | |
| OLD | NEW |