Home | History | Annotate | Download | only in client
      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 WebKit::WebGLId;
     47 using WebKit::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(new content::GLHelper(context_.get()));
     70     helper_scaling_.reset(new content::GLHelperScaling(
     71         context_.get(),
     72         helper_.get()));
     73   }
     74 
     75   virtual void TearDown() {
     76     helper_scaling_.reset(NULL);
     77     helper_.reset(NULL);
     78     context_.reset(NULL);
     79   }
     80 
     81 
     82   void LoadPngFileToSkBitmap(const base::FilePath& filename,
     83                              SkBitmap* bitmap) {
     84     std::string compressed;
     85     file_util::ReadFileToString(base::MakeAbsoluteFilePath(filename),
     86                                 &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 = file_util::OpenFile(filename, "wb");
    106     ASSERT_TRUE(f);
    107     ASSERT_EQ(fwrite(&*compressed.begin(), 1, compressed.size(), f),
    108               compressed.size());
    109     file_util::CloseFile(f);
    110   }
    111 
    112   scoped_ptr<WebKit::WebGraphicsContext3D> context_;
    113   scoped_ptr<content::GLHelper> helper_;
    114   scoped_ptr<content::GLHelperScaling> helper_scaling_;
    115   std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_;
    116 };
    117 
    118 
    119 TEST_F(GLHelperTest, ScaleBenchmark) {
    120   int output_sizes[] = { 1920, 1080,
    121                          1249, 720,  // Output size on pixel
    122                          256, 144 };
    123   int input_sizes[] = { 3200, 2040,
    124                         2560, 1476,  // Pixel tab size
    125                         1920, 1080,
    126                         1280, 720,
    127                         800, 480,
    128                         256, 144 };
    129 
    130   for (size_t q = 0; q < arraysize(kQualities); q++) {
    131     for (size_t outsize = 0;
    132          outsize < arraysize(output_sizes);
    133          outsize += 2) {
    134       for (size_t insize = 0;
    135            insize < arraysize(input_sizes);
    136            insize += 2) {
    137         WebGLId src_texture = context_->createTexture();
    138         WebGLId dst_texture = context_->createTexture();
    139         WebGLId framebuffer = context_->createFramebuffer();
    140         const gfx::Size src_size(input_sizes[insize],
    141                                  input_sizes[insize + 1]);
    142         const gfx::Size dst_size(output_sizes[outsize],
    143                                  output_sizes[outsize + 1]);
    144         SkBitmap input;
    145         input.setConfig(SkBitmap::kARGB_8888_Config,
    146                         src_size.width(),
    147                         src_size.height());
    148         input.allocPixels();
    149         SkAutoLockPixels lock(input);
    150 
    151         SkBitmap output_pixels;
    152         input.setConfig(SkBitmap::kARGB_8888_Config,
    153                         dst_size.width(),
    154                         dst_size.height());
    155         output_pixels.allocPixels();
    156         SkAutoLockPixels output_lock(output_pixels);
    157 
    158         context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    159         context_->bindTexture(GL_TEXTURE_2D, dst_texture);
    160         context_->texImage2D(GL_TEXTURE_2D,
    161                              0,
    162                              GL_RGBA,
    163                              dst_size.width(),
    164                              dst_size.height(),
    165                              0,
    166                              GL_RGBA,
    167                              GL_UNSIGNED_BYTE,
    168                              0);
    169         context_->bindTexture(GL_TEXTURE_2D, src_texture);
    170         context_->texImage2D(GL_TEXTURE_2D,
    171                              0,
    172                              GL_RGBA,
    173                              src_size.width(),
    174                              src_size.height(),
    175                              0,
    176                              GL_RGBA,
    177                              GL_UNSIGNED_BYTE,
    178                              input.getPixels());
    179 
    180         gfx::Rect src_subrect(0, 0,
    181                               src_size.width(), src_size.height());
    182         scoped_ptr<content::GLHelper::ScalerInterface> scaler(
    183           helper_->CreateScaler(kQualities[q],
    184                                 src_size,
    185                                 src_subrect,
    186                                 dst_size,
    187                                 false,
    188                                 false));
    189         // Scale once beforehand before we start measuring.
    190         scaler->Scale(src_texture, dst_texture);
    191         context_->finish();
    192 
    193         base::TimeTicks start_time = base::TimeTicks::Now();
    194         int iterations = 0;
    195         base::TimeTicks end_time;
    196         while (true) {
    197           for (int i = 0; i < 50; i++) {
    198             iterations++;
    199             scaler->Scale(src_texture, dst_texture);
    200             context_->flush();
    201           }
    202           context_->finish();
    203           end_time = base::TimeTicks::Now();
    204           if (iterations > 2000) {
    205             break;
    206           }
    207           if ((end_time - start_time).InMillisecondsF() > 1000) {
    208             break;
    209           }
    210         }
    211         context_->deleteTexture(dst_texture);
    212         context_->deleteTexture(src_texture);
    213         context_->deleteFramebuffer(framebuffer);
    214 
    215         std::string name;
    216         name = base::StringPrintf("scale_%dx%d_to_%dx%d_%s",
    217                                   src_size.width(),
    218                                   src_size.height(),
    219                                   dst_size.width(),
    220                                   dst_size.height(),
    221                                   kQualityNames[q]);
    222 
    223         float ms = (end_time - start_time).InMillisecondsF() / iterations;
    224         printf("*RESULT gpu_scale_time: %s=%.2f ms\n", name.c_str(), ms);
    225       }
    226     }
    227   }
    228 }
    229 
    230 // This is more of a test utility than a test.
    231 // Put an PNG image called "testimage.png" in your
    232 // current directory, then run this test. It will
    233 // create testoutput_Q_P.png, where Q is the scaling
    234 // mode and P is the scaling percentage taken from
    235 // the table below.
    236 TEST_F(GLHelperTest, DISABLED_ScaleTestImage) {
    237   int percents[] = {
    238     230,
    239     180,
    240     150,
    241     110,
    242     90,
    243     70,
    244     50,
    245     49,
    246     40,
    247     20,
    248     10,
    249   };
    250 
    251   SkBitmap input;
    252   LoadPngFileToSkBitmap(base::FilePath(
    253       FILE_PATH_LITERAL("testimage.png")), &input);
    254 
    255   WebGLId framebuffer = context_->createFramebuffer();
    256   WebGLId src_texture = context_->createTexture();
    257   const gfx::Size src_size(input.width(), input.height());
    258   context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    259   context_->bindTexture(GL_TEXTURE_2D, src_texture);
    260   context_->texImage2D(GL_TEXTURE_2D,
    261                        0,
    262                        GL_RGBA,
    263                        src_size.width(),
    264                        src_size.height(),
    265                        0,
    266                        GL_RGBA,
    267                        GL_UNSIGNED_BYTE,
    268                        input.getPixels());
    269 
    270   for (size_t q = 0; q < arraysize(kQualities); q++) {
    271     for (size_t p = 0; p < arraysize(percents); p++) {
    272       const gfx::Size dst_size(input.width() * percents[p] / 100,
    273                                input.height() * percents[p] / 100);
    274       WebGLId dst_texture = helper_->CopyAndScaleTexture(
    275         src_texture,
    276         src_size,
    277         dst_size,
    278         false,
    279         kQualities[q]);
    280 
    281       SkBitmap output_pixels;
    282       input.setConfig(SkBitmap::kARGB_8888_Config,
    283                       dst_size.width(),
    284                       dst_size.height());
    285       output_pixels.allocPixels();
    286       SkAutoLockPixels lock(output_pixels);
    287 
    288       helper_->ReadbackTextureSync(
    289           dst_texture,
    290           gfx::Rect(0, 0,
    291                     dst_size.width(),
    292                     dst_size.height()),
    293           static_cast<unsigned char *>(output_pixels.getPixels()));
    294       context_->deleteTexture(dst_texture);
    295       std::string filename = base::StringPrintf("testoutput_%s_%d.ppm",
    296                                                 kQualityNames[q],
    297                                                 percents[p]);
    298       LOG(INFO) << "Writing " <<  filename;
    299       SaveToFile(&output_pixels, base::FilePath::FromUTF8Unsafe(filename));
    300     }
    301   }
    302   context_->deleteTexture(src_texture);
    303   context_->deleteFramebuffer(framebuffer);
    304 }
    305 
    306 }  // namespace
    307 
    308 // These tests needs to run against a proper GL environment, so we
    309 // need to set it up before we can run the tests.
    310 int main(int argc, char** argv) {
    311   CommandLine::Init(argc, argv);
    312   base::TestSuite* suite = new content::ContentTestSuite(argc, argv);
    313 #if defined(OS_MACOSX)
    314   base::mac::ScopedNSAutoreleasePool pool;
    315 #endif
    316 #if defined(TOOLKIT_GTK)
    317   gfx::GtkInitFromCommandLine(*CommandLine::ForCurrentProcess());
    318 #endif
    319   gfx::GLSurface::InitializeOneOff();
    320 
    321   return content::UnitTestTestSuite(suite).Run();
    322 }
    323