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