| 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/common/gpu/client/gl_helper.h" | |
| 28 #include "content/common/gpu/client/gl_helper_scaling.h" | |
| 29 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h" | |
| 30 #include "testing/gtest/include/gtest/gtest.h" | |
| 31 #include "third_party/skia/include/core/SkBitmap.h" | |
| 32 #include "third_party/skia/include/core/SkTypes.h" | |
| 33 #include "ui/gfx/codec/png_codec.h" | |
| 34 #include "ui/gl/gl_surface.h" | |
| 35 | |
| 36 namespace content { | |
| 37 | |
| 38 using blink::WebGLId; | |
| 39 using blink::WebGraphicsContext3D; | |
| 40 | |
| 41 content::GLHelper::ScalerQuality kQualities[] = { | |
| 42 content::GLHelper::SCALER_QUALITY_BEST, | |
| 43 content::GLHelper::SCALER_QUALITY_GOOD, | |
| 44 content::GLHelper::SCALER_QUALITY_FAST, | |
| 45 }; | |
| 46 | |
| 47 const char *kQualityNames[] = { | |
| 48 "best", | |
| 49 "good", | |
| 50 "fast", | |
| 51 }; | |
| 52 | |
| 53 class GLHelperTest : public testing::Test { | |
| 54 protected: | |
| 55 void SetUp() override { | |
| 56 WebGraphicsContext3D::Attributes attributes; | |
| 57 bool lose_context_when_out_of_memory = false; | |
| 58 context_ = gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl:: | |
| 59 CreateOffscreenContext(attributes, lose_context_when_out_of_memory); | |
| 60 context_->InitializeOnCurrentThread(); | |
| 61 | |
| 62 helper_.reset( | |
| 63 new content::GLHelper(context_->GetGLInterface(), | |
| 64 context_->GetContextSupport())); | |
| 65 helper_scaling_.reset(new content::GLHelperScaling( | |
| 66 context_->GetGLInterface(), | |
| 67 helper_.get())); | |
| 68 } | |
| 69 | |
| 70 void TearDown() override { | |
| 71 helper_scaling_.reset(NULL); | |
| 72 helper_.reset(NULL); | |
| 73 context_.reset(NULL); | |
| 74 } | |
| 75 | |
| 76 | |
| 77 void LoadPngFileToSkBitmap(const base::FilePath& filename, | |
| 78 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 | |
| 87 // Save the image to a png file. Used to create the initial test files. | |
| 88 void SaveToFile(SkBitmap* bitmap, const base::FilePath& filename) { | |
| 89 std::vector<unsigned char> compressed; | |
| 90 ASSERT_TRUE(gfx::PNGCodec::Encode( | |
| 91 static_cast<unsigned char*>(bitmap->getPixels()), | |
| 92 gfx::PNGCodec::FORMAT_BGRA, | |
| 93 gfx::Size(bitmap->width(), bitmap->height()), | |
| 94 static_cast<int>(bitmap->rowBytes()), | |
| 95 true, | |
| 96 std::vector<gfx::PNGCodec::Comment>(), | |
| 97 &compressed)); | |
| 98 ASSERT_TRUE(compressed.size()); | |
| 99 FILE* f = base::OpenFile(filename, "wb"); | |
| 100 ASSERT_TRUE(f); | |
| 101 ASSERT_EQ(fwrite(&*compressed.begin(), 1, compressed.size(), f), | |
| 102 compressed.size()); | |
| 103 base::CloseFile(f); | |
| 104 } | |
| 105 | |
| 106 scoped_ptr<gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl> | |
| 107 context_; | |
| 108 scoped_ptr<content::GLHelper> helper_; | |
| 109 scoped_ptr<content::GLHelperScaling> helper_scaling_; | |
| 110 std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_; | |
| 111 }; | |
| 112 | |
| 113 | |
| 114 TEST_F(GLHelperTest, ScaleBenchmark) { | |
| 115 int output_sizes[] = { 1920, 1080, | |
| 116 1249, 720, // Output size on pixel | |
| 117 256, 144 }; | |
| 118 int input_sizes[] = { 3200, 2040, | |
| 119 2560, 1476, // Pixel tab size | |
| 120 1920, 1080, | |
| 121 1280, 720, | |
| 122 800, 480, | |
| 123 256, 144 }; | |
| 124 | |
| 125 for (size_t q = 0; q < arraysize(kQualities); q++) { | |
| 126 for (size_t outsize = 0; | |
| 127 outsize < arraysize(output_sizes); | |
| 128 outsize += 2) { | |
| 129 for (size_t insize = 0; | |
| 130 insize < arraysize(input_sizes); | |
| 131 insize += 2) { | |
| 132 WebGLId src_texture = context_->createTexture(); | |
| 133 WebGLId dst_texture = context_->createTexture(); | |
| 134 WebGLId framebuffer = context_->createFramebuffer(); | |
| 135 const gfx::Size src_size(input_sizes[insize], | |
| 136 input_sizes[insize + 1]); | |
| 137 const gfx::Size dst_size(output_sizes[outsize], | |
| 138 output_sizes[outsize + 1]); | |
| 139 SkBitmap input; | |
| 140 input.allocN32Pixels(src_size.width(), src_size.height()); | |
| 141 | |
| 142 SkBitmap output_pixels; | |
| 143 output_pixels.allocN32Pixels(dst_size.width(), dst_size.height()); | |
| 144 | |
| 145 context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer); | |
| 146 context_->bindTexture(GL_TEXTURE_2D, dst_texture); | |
| 147 context_->texImage2D(GL_TEXTURE_2D, | |
| 148 0, | |
| 149 GL_RGBA, | |
| 150 dst_size.width(), | |
| 151 dst_size.height(), | |
| 152 0, | |
| 153 GL_RGBA, | |
| 154 GL_UNSIGNED_BYTE, | |
| 155 0); | |
| 156 context_->bindTexture(GL_TEXTURE_2D, src_texture); | |
| 157 context_->texImage2D(GL_TEXTURE_2D, | |
| 158 0, | |
| 159 GL_RGBA, | |
| 160 src_size.width(), | |
| 161 src_size.height(), | |
| 162 0, | |
| 163 GL_RGBA, | |
| 164 GL_UNSIGNED_BYTE, | |
| 165 input.getPixels()); | |
| 166 | |
| 167 gfx::Rect src_subrect(0, 0, | |
| 168 src_size.width(), src_size.height()); | |
| 169 scoped_ptr<content::GLHelper::ScalerInterface> scaler( | |
| 170 helper_->CreateScaler(kQualities[q], | |
| 171 src_size, | |
| 172 src_subrect, | |
| 173 dst_size, | |
| 174 false, | |
| 175 false)); | |
| 176 // Scale once beforehand before we start measuring. | |
| 177 scaler->Scale(src_texture, dst_texture); | |
| 178 context_->finish(); | |
| 179 | |
| 180 base::TimeTicks start_time = base::TimeTicks::Now(); | |
| 181 int iterations = 0; | |
| 182 base::TimeTicks end_time; | |
| 183 while (true) { | |
| 184 for (int i = 0; i < 50; i++) { | |
| 185 iterations++; | |
| 186 scaler->Scale(src_texture, dst_texture); | |
| 187 context_->flush(); | |
| 188 } | |
| 189 context_->finish(); | |
| 190 end_time = base::TimeTicks::Now(); | |
| 191 if (iterations > 2000) { | |
| 192 break; | |
| 193 } | |
| 194 if ((end_time - start_time).InMillisecondsF() > 1000) { | |
| 195 break; | |
| 196 } | |
| 197 } | |
| 198 context_->deleteTexture(dst_texture); | |
| 199 context_->deleteTexture(src_texture); | |
| 200 context_->deleteFramebuffer(framebuffer); | |
| 201 | |
| 202 std::string name; | |
| 203 name = base::StringPrintf("scale_%dx%d_to_%dx%d_%s", | |
| 204 src_size.width(), | |
| 205 src_size.height(), | |
| 206 dst_size.width(), | |
| 207 dst_size.height(), | |
| 208 kQualityNames[q]); | |
| 209 | |
| 210 float ms = (end_time - start_time).InMillisecondsF() / iterations; | |
| 211 printf("*RESULT gpu_scale_time: %s=%.2f ms\n", name.c_str(), ms); | |
| 212 } | |
| 213 } | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 // This is more of a test utility than a test. | |
| 218 // Put an PNG image called "testimage.png" in your | |
| 219 // current directory, then run this test. It will | |
| 220 // create testoutput_Q_P.png, where Q is the scaling | |
| 221 // mode and P is the scaling percentage taken from | |
| 222 // the table below. | |
| 223 TEST_F(GLHelperTest, DISABLED_ScaleTestImage) { | |
| 224 int percents[] = { | |
| 225 230, | |
| 226 180, | |
| 227 150, | |
| 228 110, | |
| 229 90, | |
| 230 70, | |
| 231 50, | |
| 232 49, | |
| 233 40, | |
| 234 20, | |
| 235 10, | |
| 236 }; | |
| 237 | |
| 238 SkBitmap input; | |
| 239 LoadPngFileToSkBitmap(base::FilePath( | |
| 240 FILE_PATH_LITERAL("testimage.png")), &input); | |
| 241 | |
| 242 WebGLId framebuffer = context_->createFramebuffer(); | |
| 243 WebGLId src_texture = context_->createTexture(); | |
| 244 const gfx::Size src_size(input.width(), input.height()); | |
| 245 context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer); | |
| 246 context_->bindTexture(GL_TEXTURE_2D, src_texture); | |
| 247 context_->texImage2D(GL_TEXTURE_2D, | |
| 248 0, | |
| 249 GL_RGBA, | |
| 250 src_size.width(), | |
| 251 src_size.height(), | |
| 252 0, | |
| 253 GL_RGBA, | |
| 254 GL_UNSIGNED_BYTE, | |
| 255 input.getPixels()); | |
| 256 | |
| 257 for (size_t q = 0; q < arraysize(kQualities); q++) { | |
| 258 for (size_t p = 0; p < arraysize(percents); p++) { | |
| 259 const gfx::Size dst_size(input.width() * percents[p] / 100, | |
| 260 input.height() * percents[p] / 100); | |
| 261 WebGLId dst_texture = helper_->CopyAndScaleTexture( | |
| 262 src_texture, | |
| 263 src_size, | |
| 264 dst_size, | |
| 265 false, | |
| 266 kQualities[q]); | |
| 267 | |
| 268 SkBitmap output_pixels; | |
| 269 output_pixels.allocN32Pixels(dst_size.width(), dst_size.height()); | |
| 270 | |
| 271 helper_->ReadbackTextureSync( | |
| 272 dst_texture, | |
| 273 gfx::Rect(0, 0, | |
| 274 dst_size.width(), | |
| 275 dst_size.height()), | |
| 276 static_cast<unsigned char *>(output_pixels.getPixels()), | |
| 277 kN32_SkColorType); | |
| 278 context_->deleteTexture(dst_texture); | |
| 279 std::string filename = base::StringPrintf("testoutput_%s_%d.ppm", | |
| 280 kQualityNames[q], | |
| 281 percents[p]); | |
| 282 VLOG(0) << "Writing " << filename; | |
| 283 SaveToFile(&output_pixels, base::FilePath::FromUTF8Unsafe(filename)); | |
| 284 } | |
| 285 } | |
| 286 context_->deleteTexture(src_texture); | |
| 287 context_->deleteFramebuffer(framebuffer); | |
| 288 } | |
| 289 | |
| 290 } // namespace | |
| OLD | NEW |