Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / common / gpu / client / gl_helper_benchmark.cc
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.allocN32Pixels(src_size.width(), src_size.height());
145
146         SkBitmap output_pixels;
147         output_pixels.allocN32Pixels(dst_size.width(), dst_size.height());
148
149         context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer);
150         context_->bindTexture(GL_TEXTURE_2D, dst_texture);
151         context_->texImage2D(GL_TEXTURE_2D,
152                              0,
153                              GL_RGBA,
154                              dst_size.width(),
155                              dst_size.height(),
156                              0,
157                              GL_RGBA,
158                              GL_UNSIGNED_BYTE,
159                              0);
160         context_->bindTexture(GL_TEXTURE_2D, src_texture);
161         context_->texImage2D(GL_TEXTURE_2D,
162                              0,
163                              GL_RGBA,
164                              src_size.width(),
165                              src_size.height(),
166                              0,
167                              GL_RGBA,
168                              GL_UNSIGNED_BYTE,
169                              input.getPixels());
170
171         gfx::Rect src_subrect(0, 0,
172                               src_size.width(), src_size.height());
173         scoped_ptr<content::GLHelper::ScalerInterface> scaler(
174           helper_->CreateScaler(kQualities[q],
175                                 src_size,
176                                 src_subrect,
177                                 dst_size,
178                                 false,
179                                 false));
180         // Scale once beforehand before we start measuring.
181         scaler->Scale(src_texture, dst_texture);
182         context_->finish();
183
184         base::TimeTicks start_time = base::TimeTicks::Now();
185         int iterations = 0;
186         base::TimeTicks end_time;
187         while (true) {
188           for (int i = 0; i < 50; i++) {
189             iterations++;
190             scaler->Scale(src_texture, dst_texture);
191             context_->flush();
192           }
193           context_->finish();
194           end_time = base::TimeTicks::Now();
195           if (iterations > 2000) {
196             break;
197           }
198           if ((end_time - start_time).InMillisecondsF() > 1000) {
199             break;
200           }
201         }
202         context_->deleteTexture(dst_texture);
203         context_->deleteTexture(src_texture);
204         context_->deleteFramebuffer(framebuffer);
205
206         std::string name;
207         name = base::StringPrintf("scale_%dx%d_to_%dx%d_%s",
208                                   src_size.width(),
209                                   src_size.height(),
210                                   dst_size.width(),
211                                   dst_size.height(),
212                                   kQualityNames[q]);
213
214         float ms = (end_time - start_time).InMillisecondsF() / iterations;
215         printf("*RESULT gpu_scale_time: %s=%.2f ms\n", name.c_str(), ms);
216       }
217     }
218   }
219 }
220
221 // This is more of a test utility than a test.
222 // Put an PNG image called "testimage.png" in your
223 // current directory, then run this test. It will
224 // create testoutput_Q_P.png, where Q is the scaling
225 // mode and P is the scaling percentage taken from
226 // the table below.
227 TEST_F(GLHelperTest, DISABLED_ScaleTestImage) {
228   int percents[] = {
229     230,
230     180,
231     150,
232     110,
233     90,
234     70,
235     50,
236     49,
237     40,
238     20,
239     10,
240   };
241
242   SkBitmap input;
243   LoadPngFileToSkBitmap(base::FilePath(
244       FILE_PATH_LITERAL("testimage.png")), &input);
245
246   WebGLId framebuffer = context_->createFramebuffer();
247   WebGLId src_texture = context_->createTexture();
248   const gfx::Size src_size(input.width(), input.height());
249   context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer);
250   context_->bindTexture(GL_TEXTURE_2D, src_texture);
251   context_->texImage2D(GL_TEXTURE_2D,
252                        0,
253                        GL_RGBA,
254                        src_size.width(),
255                        src_size.height(),
256                        0,
257                        GL_RGBA,
258                        GL_UNSIGNED_BYTE,
259                        input.getPixels());
260
261   for (size_t q = 0; q < arraysize(kQualities); q++) {
262     for (size_t p = 0; p < arraysize(percents); p++) {
263       const gfx::Size dst_size(input.width() * percents[p] / 100,
264                                input.height() * percents[p] / 100);
265       WebGLId dst_texture = helper_->CopyAndScaleTexture(
266         src_texture,
267         src_size,
268         dst_size,
269         false,
270         kQualities[q]);
271
272       SkBitmap output_pixels;
273       output_pixels.allocN32Pixels(dst_size.width(), dst_size.height());
274
275       helper_->ReadbackTextureSync(
276           dst_texture,
277           gfx::Rect(0, 0,
278                     dst_size.width(),
279                     dst_size.height()),
280           static_cast<unsigned char *>(output_pixels.getPixels()),
281           kN32_SkColorType);
282       context_->deleteTexture(dst_texture);
283       std::string filename = base::StringPrintf("testoutput_%s_%d.ppm",
284                                                 kQualityNames[q],
285                                                 percents[p]);
286       VLOG(0) << "Writing " <<  filename;
287       SaveToFile(&output_pixels, base::FilePath::FromUTF8Unsafe(filename));
288     }
289   }
290   context_->deleteTexture(src_texture);
291   context_->deleteFramebuffer(framebuffer);
292 }
293
294 }  // namespace
295
296 // These tests needs to run against a proper GL environment, so we
297 // need to set it up before we can run the tests.
298 int main(int argc, char** argv) {
299   base::CommandLine::Init(argc, argv);
300   base::TestSuite* suite = new content::ContentTestSuite(argc, argv);
301 #if defined(OS_MACOSX)
302   base::mac::ScopedNSAutoreleasePool pool;
303 #endif
304   gfx::GLSurface::InitializeOneOff();
305
306   return content::UnitTestTestSuite(suite).Run();
307 }