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.
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.
16 #include <GLES2/gl2.h>
17 #include <GLES2/gl2ext.h>
18 #include <GLES2/gl2extchromium.h>
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"
36 #if defined(OS_MACOSX)
37 #include "base/mac/scoped_nsautorelease_pool.h"
43 using blink::WebGraphicsContext3D
;
45 content::GLHelper::ScalerQuality kQualities
[] = {
46 content::GLHelper::SCALER_QUALITY_BEST
,
47 content::GLHelper::SCALER_QUALITY_GOOD
,
48 content::GLHelper::SCALER_QUALITY_FAST
,
51 const char *kQualityNames
[] = {
57 class GLHelperTest
: public testing::Test
{
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();
67 new content::GLHelper(context_
->GetGLInterface(),
68 context_
->GetContextSupport()));
69 helper_scaling_
.reset(new content::GLHelperScaling(
70 context_
->GetGLInterface(),
74 virtual void TearDown() {
75 helper_scaling_
.reset(NULL
);
81 void LoadPngFileToSkBitmap(const base::FilePath
& filename
,
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
));
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()),
100 std::vector
<gfx::PNGCodec::Comment
>(),
102 ASSERT_TRUE(compressed
.size());
103 FILE* f
= base::OpenFile(filename
, "wb");
105 ASSERT_EQ(fwrite(&*compressed
.begin(), 1, compressed
.size(), f
),
110 scoped_ptr
<webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl
>
112 scoped_ptr
<content::GLHelper
> helper_
;
113 scoped_ptr
<content::GLHelperScaling
> helper_scaling_
;
114 std::deque
<GLHelperScaling::ScaleOp
> x_ops_
, y_ops_
;
118 TEST_F(GLHelperTest
, ScaleBenchmark
) {
119 int output_sizes
[] = { 1920, 1080,
120 1249, 720, // Output size on pixel
122 int input_sizes
[] = { 3200, 2040,
123 2560, 1476, // Pixel tab size
129 for (size_t q
= 0; q
< arraysize(kQualities
); q
++) {
130 for (size_t outsize
= 0;
131 outsize
< arraysize(output_sizes
);
133 for (size_t insize
= 0;
134 insize
< arraysize(input_sizes
);
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]);
144 input
.allocN32Pixels(src_size
.width(), src_size
.height());
146 SkBitmap output_pixels
;
147 output_pixels
.allocN32Pixels(dst_size
.width(), dst_size
.height());
149 context_
->bindFramebuffer(GL_FRAMEBUFFER
, framebuffer
);
150 context_
->bindTexture(GL_TEXTURE_2D
, dst_texture
);
151 context_
->texImage2D(GL_TEXTURE_2D
,
160 context_
->bindTexture(GL_TEXTURE_2D
, src_texture
);
161 context_
->texImage2D(GL_TEXTURE_2D
,
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
],
180 // Scale once beforehand before we start measuring.
181 scaler
->Scale(src_texture
, dst_texture
);
184 base::TimeTicks start_time
= base::TimeTicks::Now();
186 base::TimeTicks end_time
;
188 for (int i
= 0; i
< 50; i
++) {
190 scaler
->Scale(src_texture
, dst_texture
);
194 end_time
= base::TimeTicks::Now();
195 if (iterations
> 2000) {
198 if ((end_time
- start_time
).InMillisecondsF() > 1000) {
202 context_
->deleteTexture(dst_texture
);
203 context_
->deleteTexture(src_texture
);
204 context_
->deleteFramebuffer(framebuffer
);
207 name
= base::StringPrintf("scale_%dx%d_to_%dx%d_%s",
214 float ms
= (end_time
- start_time
).InMillisecondsF() / iterations
;
215 printf("*RESULT gpu_scale_time: %s=%.2f ms\n", name
.c_str(), ms
);
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
227 TEST_F(GLHelperTest
, DISABLED_ScaleTestImage
) {
243 LoadPngFileToSkBitmap(base::FilePath(
244 FILE_PATH_LITERAL("testimage.png")), &input
);
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
,
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(
272 SkBitmap output_pixels
;
273 output_pixels
.allocN32Pixels(dst_size
.width(), dst_size
.height());
275 helper_
->ReadbackTextureSync(
280 static_cast<unsigned char *>(output_pixels
.getPixels()),
282 context_
->deleteTexture(dst_texture
);
283 std::string filename
= base::StringPrintf("testoutput_%s_%d.ppm",
286 VLOG(0) << "Writing " << filename
;
287 SaveToFile(&output_pixels
, base::FilePath::FromUTF8Unsafe(filename
));
290 context_
->deleteTexture(src_texture
);
291 context_
->deleteFramebuffer(framebuffer
);
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 CommandLine::Init(argc
, argv
);
300 base::TestSuite
* suite
= new content::ContentTestSuite(argc
, argv
);
301 #if defined(OS_MACOSX)
302 base::mac::ScopedNSAutoreleasePool pool
;
304 gfx::GLSurface::InitializeOneOff();
306 return content::UnitTestTestSuite(suite
).Run();