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 <stdio.h> 12 #include <cmath> 13 #include <string> 14 #include <vector> 15 16 #include <GLES2/gl2.h> 17 #include <GLES2/gl2ext.h> 18 #include <GLES2/gl2extchromium.h> 19 20 #include "base/at_exit.h" 21 #include "base/command_line.h" 22 #include "base/file_util.h" 23 #include "base/strings/stringprintf.h" 24 #include "base/time/time.h" 25 #include "content/common/gpu/client/gl_helper.h" 26 #include "content/common/gpu/client/gl_helper_scaling.h" 27 #include "content/public/test/unittest_test_suite.h" 28 #include "content/test/content_test_suite.h" 29 #include "testing/gtest/include/gtest/gtest.h" 30 #include "third_party/skia/include/core/SkBitmap.h" 31 #include "third_party/skia/include/core/SkTypes.h" 32 #include "ui/gfx/codec/png_codec.h" 33 #include "ui/gl/gl_surface.h" 34 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h" 35 36 #if defined(OS_MACOSX) 37 #include "base/mac/scoped_nsautorelease_pool.h" 38 #endif 39 40 #if defined(TOOLKIT_GTK) 41 #include "ui/gfx/gtk_util.h" 42 #endif 43 44 namespace content { 45 46 using blink::WebGLId; 47 using blink::WebGraphicsContext3D; 48 49 content::GLHelper::ScalerQuality kQualities[] = { 50 content::GLHelper::SCALER_QUALITY_BEST, 51 content::GLHelper::SCALER_QUALITY_GOOD, 52 content::GLHelper::SCALER_QUALITY_FAST, 53 }; 54 55 const char *kQualityNames[] = { 56 "best", 57 "good", 58 "fast", 59 }; 60 61 class GLHelperTest : public testing::Test { 62 protected: 63 virtual void SetUp() { 64 WebGraphicsContext3D::Attributes attributes; 65 context_ = webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl:: 66 CreateOffscreenContext(attributes); 67 context_->makeContextCurrent(); 68 69 helper_.reset( 70 new content::GLHelper(context_.get(), context_->GetContextSupport())); 71 helper_scaling_.reset(new content::GLHelperScaling( 72 context_.get(), 73 helper_.get())); 74 } 75 76 virtual void TearDown() { 77 helper_scaling_.reset(NULL); 78 helper_.reset(NULL); 79 context_.reset(NULL); 80 } 81 82 83 void LoadPngFileToSkBitmap(const base::FilePath& filename, 84 SkBitmap* bitmap) { 85 std::string compressed; 86 base::ReadFileToString(base::MakeAbsoluteFilePath(filename), &compressed); 87 ASSERT_TRUE(compressed.size()); 88 ASSERT_TRUE(gfx::PNGCodec::Decode( 89 reinterpret_cast<const unsigned char*>(compressed.data()), 90 compressed.size(), bitmap)); 91 } 92 93 // Save the image to a png file. Used to create the initial test files. 94 void SaveToFile(SkBitmap* bitmap, const base::FilePath& filename) { 95 std::vector<unsigned char> compressed; 96 ASSERT_TRUE(gfx::PNGCodec::Encode( 97 static_cast<unsigned char*>(bitmap->getPixels()), 98 gfx::PNGCodec::FORMAT_BGRA, 99 gfx::Size(bitmap->width(), bitmap->height()), 100 static_cast<int>(bitmap->rowBytes()), 101 true, 102 std::vector<gfx::PNGCodec::Comment>(), 103 &compressed)); 104 ASSERT_TRUE(compressed.size()); 105 FILE* f = base::OpenFile(filename, "wb"); 106 ASSERT_TRUE(f); 107 ASSERT_EQ(fwrite(&*compressed.begin(), 1, compressed.size(), f), 108 compressed.size()); 109 base::CloseFile(f); 110 } 111 112 scoped_ptr<webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl> 113 context_; 114 scoped_ptr<content::GLHelper> helper_; 115 scoped_ptr<content::GLHelperScaling> helper_scaling_; 116 std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_; 117 }; 118 119 120 TEST_F(GLHelperTest, ScaleBenchmark) { 121 int output_sizes[] = { 1920, 1080, 122 1249, 720, // Output size on pixel 123 256, 144 }; 124 int input_sizes[] = { 3200, 2040, 125 2560, 1476, // Pixel tab size 126 1920, 1080, 127 1280, 720, 128 800, 480, 129 256, 144 }; 130 131 for (size_t q = 0; q < arraysize(kQualities); q++) { 132 for (size_t outsize = 0; 133 outsize < arraysize(output_sizes); 134 outsize += 2) { 135 for (size_t insize = 0; 136 insize < arraysize(input_sizes); 137 insize += 2) { 138 WebGLId src_texture = context_->createTexture(); 139 WebGLId dst_texture = context_->createTexture(); 140 WebGLId framebuffer = context_->createFramebuffer(); 141 const gfx::Size src_size(input_sizes[insize], 142 input_sizes[insize + 1]); 143 const gfx::Size dst_size(output_sizes[outsize], 144 output_sizes[outsize + 1]); 145 SkBitmap input; 146 input.setConfig(SkBitmap::kARGB_8888_Config, 147 src_size.width(), 148 src_size.height()); 149 input.allocPixels(); 150 SkAutoLockPixels lock(input); 151 152 SkBitmap output_pixels; 153 input.setConfig(SkBitmap::kARGB_8888_Config, 154 dst_size.width(), 155 dst_size.height()); 156 output_pixels.allocPixels(); 157 SkAutoLockPixels output_lock(output_pixels); 158 159 context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer); 160 context_->bindTexture(GL_TEXTURE_2D, dst_texture); 161 context_->texImage2D(GL_TEXTURE_2D, 162 0, 163 GL_RGBA, 164 dst_size.width(), 165 dst_size.height(), 166 0, 167 GL_RGBA, 168 GL_UNSIGNED_BYTE, 169 0); 170 context_->bindTexture(GL_TEXTURE_2D, src_texture); 171 context_->texImage2D(GL_TEXTURE_2D, 172 0, 173 GL_RGBA, 174 src_size.width(), 175 src_size.height(), 176 0, 177 GL_RGBA, 178 GL_UNSIGNED_BYTE, 179 input.getPixels()); 180 181 gfx::Rect src_subrect(0, 0, 182 src_size.width(), src_size.height()); 183 scoped_ptr<content::GLHelper::ScalerInterface> scaler( 184 helper_->CreateScaler(kQualities[q], 185 src_size, 186 src_subrect, 187 dst_size, 188 false, 189 false)); 190 // Scale once beforehand before we start measuring. 191 scaler->Scale(src_texture, dst_texture); 192 context_->finish(); 193 194 base::TimeTicks start_time = base::TimeTicks::Now(); 195 int iterations = 0; 196 base::TimeTicks end_time; 197 while (true) { 198 for (int i = 0; i < 50; i++) { 199 iterations++; 200 scaler->Scale(src_texture, dst_texture); 201 context_->flush(); 202 } 203 context_->finish(); 204 end_time = base::TimeTicks::Now(); 205 if (iterations > 2000) { 206 break; 207 } 208 if ((end_time - start_time).InMillisecondsF() > 1000) { 209 break; 210 } 211 } 212 context_->deleteTexture(dst_texture); 213 context_->deleteTexture(src_texture); 214 context_->deleteFramebuffer(framebuffer); 215 216 std::string name; 217 name = base::StringPrintf("scale_%dx%d_to_%dx%d_%s", 218 src_size.width(), 219 src_size.height(), 220 dst_size.width(), 221 dst_size.height(), 222 kQualityNames[q]); 223 224 float ms = (end_time - start_time).InMillisecondsF() / iterations; 225 printf("*RESULT gpu_scale_time: %s=%.2f ms\n", name.c_str(), ms); 226 } 227 } 228 } 229 } 230 231 // This is more of a test utility than a test. 232 // Put an PNG image called "testimage.png" in your 233 // current directory, then run this test. It will 234 // create testoutput_Q_P.png, where Q is the scaling 235 // mode and P is the scaling percentage taken from 236 // the table below. 237 TEST_F(GLHelperTest, DISABLED_ScaleTestImage) { 238 int percents[] = { 239 230, 240 180, 241 150, 242 110, 243 90, 244 70, 245 50, 246 49, 247 40, 248 20, 249 10, 250 }; 251 252 SkBitmap input; 253 LoadPngFileToSkBitmap(base::FilePath( 254 FILE_PATH_LITERAL("testimage.png")), &input); 255 256 WebGLId framebuffer = context_->createFramebuffer(); 257 WebGLId src_texture = context_->createTexture(); 258 const gfx::Size src_size(input.width(), input.height()); 259 context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer); 260 context_->bindTexture(GL_TEXTURE_2D, src_texture); 261 context_->texImage2D(GL_TEXTURE_2D, 262 0, 263 GL_RGBA, 264 src_size.width(), 265 src_size.height(), 266 0, 267 GL_RGBA, 268 GL_UNSIGNED_BYTE, 269 input.getPixels()); 270 271 for (size_t q = 0; q < arraysize(kQualities); q++) { 272 for (size_t p = 0; p < arraysize(percents); p++) { 273 const gfx::Size dst_size(input.width() * percents[p] / 100, 274 input.height() * percents[p] / 100); 275 WebGLId dst_texture = helper_->CopyAndScaleTexture( 276 src_texture, 277 src_size, 278 dst_size, 279 false, 280 kQualities[q]); 281 282 SkBitmap output_pixels; 283 input.setConfig(SkBitmap::kARGB_8888_Config, 284 dst_size.width(), 285 dst_size.height()); 286 output_pixels.allocPixels(); 287 SkAutoLockPixels lock(output_pixels); 288 289 helper_->ReadbackTextureSync( 290 dst_texture, 291 gfx::Rect(0, 0, 292 dst_size.width(), 293 dst_size.height()), 294 static_cast<unsigned char *>(output_pixels.getPixels())); 295 context_->deleteTexture(dst_texture); 296 std::string filename = base::StringPrintf("testoutput_%s_%d.ppm", 297 kQualityNames[q], 298 percents[p]); 299 VLOG(0) << "Writing " << filename; 300 SaveToFile(&output_pixels, base::FilePath::FromUTF8Unsafe(filename)); 301 } 302 } 303 context_->deleteTexture(src_texture); 304 context_->deleteFramebuffer(framebuffer); 305 } 306 307 } // namespace 308 309 // These tests needs to run against a proper GL environment, so we 310 // need to set it up before we can run the tests. 311 int main(int argc, char** argv) { 312 CommandLine::Init(argc, argv); 313 base::TestSuite* suite = new content::ContentTestSuite(argc, argv); 314 #if defined(OS_MACOSX) 315 base::mac::ScopedNSAutoreleasePool pool; 316 #endif 317 #if defined(TOOLKIT_GTK) 318 gfx::GtkInitFromCommandLine(*CommandLine::ForCurrentProcess()); 319 #endif 320 gfx::GLSurface::InitializeOneOff(); 321 322 return content::UnitTestTestSuite(suite).Run(); 323 } 324