OLD | NEW |
| (Empty) |
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 <stddef.h> | |
12 #include <stdio.h> | |
13 #include <cmath> | |
14 #include <string> | |
15 #include <vector> | |
16 | |
17 #include <GLES2/gl2.h> | |
18 #include <GLES2/gl2ext.h> | |
19 #include <GLES2/gl2extchromium.h> | |
20 | |
21 #include "base/at_exit.h" | |
22 #include "base/command_line.h" | |
23 #include "base/files/file_util.h" | |
24 #include "base/macros.h" | |
25 #include "base/strings/stringprintf.h" | |
26 #include "base/time/time.h" | |
27 #include "components/display_compositor/gl_helper.h" | |
28 #include "components/display_compositor/gl_helper_scaling.h" | |
29 #include "gpu/command_buffer/client/gl_in_process_context.h" | |
30 #include "gpu/command_buffer/client/gles2_implementation.h" | |
31 #include "gpu/command_buffer/client/shared_memory_limits.h" | |
32 #include "testing/gtest/include/gtest/gtest.h" | |
33 #include "third_party/skia/include/core/SkBitmap.h" | |
34 #include "third_party/skia/include/core/SkTypes.h" | |
35 #include "ui/gfx/codec/png_codec.h" | |
36 #include "ui/gl/gl_surface.h" | |
37 | |
38 namespace display_compositor { | |
39 | |
40 namespace { | |
41 | |
42 display_compositor::GLHelper::ScalerQuality kQualities[] = { | |
43 display_compositor::GLHelper::SCALER_QUALITY_BEST, | |
44 display_compositor::GLHelper::SCALER_QUALITY_GOOD, | |
45 display_compositor::GLHelper::SCALER_QUALITY_FAST, | |
46 }; | |
47 | |
48 const char* const kQualityNames[] = { | |
49 "best", "good", "fast", | |
50 }; | |
51 | |
52 } // namespace | |
53 | |
54 class GLHelperBenchmark : public testing::Test { | |
55 protected: | |
56 void SetUp() override { | |
57 gpu::gles2::ContextCreationAttribHelper attributes; | |
58 attributes.alpha_size = 8; | |
59 attributes.depth_size = 24; | |
60 attributes.red_size = 8; | |
61 attributes.green_size = 8; | |
62 attributes.blue_size = 8; | |
63 attributes.stencil_size = 8; | |
64 attributes.samples = 4; | |
65 attributes.sample_buffers = 1; | |
66 attributes.bind_generates_resource = false; | |
67 | |
68 context_.reset(gpu::GLInProcessContext::Create( | |
69 nullptr, /* service */ | |
70 nullptr, /* surface */ | |
71 true, /* offscreen */ | |
72 gfx::kNullAcceleratedWidget, /* window */ | |
73 gfx::Size(1, 1), /* size */ | |
74 nullptr, /* share_context */ | |
75 attributes, gfx::PreferDiscreteGpu, gpu::SharedMemoryLimits(), | |
76 nullptr, /* gpu_memory_buffer_manager */ | |
77 nullptr /* image_factory */)); | |
78 gl_ = context_->GetImplementation(); | |
79 gpu::ContextSupport* support = context_->GetImplementation(); | |
80 | |
81 helper_.reset(new display_compositor::GLHelper(gl_, support)); | |
82 helper_scaling_.reset( | |
83 new display_compositor::GLHelperScaling(gl_, helper_.get())); | |
84 } | |
85 | |
86 void TearDown() override { | |
87 helper_scaling_.reset(NULL); | |
88 helper_.reset(NULL); | |
89 context_.reset(NULL); | |
90 } | |
91 | |
92 void LoadPngFileToSkBitmap(const base::FilePath& filename, SkBitmap* bitmap) { | |
93 std::string compressed; | |
94 base::ReadFileToString(base::MakeAbsoluteFilePath(filename), &compressed); | |
95 ASSERT_TRUE(compressed.size()); | |
96 ASSERT_TRUE(gfx::PNGCodec::Decode( | |
97 reinterpret_cast<const unsigned char*>(compressed.data()), | |
98 compressed.size(), bitmap)); | |
99 } | |
100 | |
101 // Save the image to a png file. Used to create the initial test files. | |
102 void SaveToFile(SkBitmap* bitmap, const base::FilePath& filename) { | |
103 std::vector<unsigned char> compressed; | |
104 ASSERT_TRUE(gfx::PNGCodec::Encode( | |
105 static_cast<unsigned char*>(bitmap->getPixels()), | |
106 gfx::PNGCodec::FORMAT_BGRA, | |
107 gfx::Size(bitmap->width(), bitmap->height()), | |
108 static_cast<int>(bitmap->rowBytes()), true, | |
109 std::vector<gfx::PNGCodec::Comment>(), &compressed)); | |
110 ASSERT_TRUE(compressed.size()); | |
111 FILE* f = base::OpenFile(filename, "wb"); | |
112 ASSERT_TRUE(f); | |
113 ASSERT_EQ(fwrite(&*compressed.begin(), 1, compressed.size(), f), | |
114 compressed.size()); | |
115 base::CloseFile(f); | |
116 } | |
117 | |
118 std::unique_ptr<gpu::GLInProcessContext> context_; | |
119 gpu::gles2::GLES2Interface* gl_; | |
120 std::unique_ptr<display_compositor::GLHelper> helper_; | |
121 std::unique_ptr<display_compositor::GLHelperScaling> helper_scaling_; | |
122 std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_; | |
123 }; | |
124 | |
125 TEST_F(GLHelperBenchmark, ScaleBenchmark) { | |
126 int output_sizes[] = {1920, 1080, 1249, 720, // Output size on pixel | |
127 256, 144}; | |
128 int input_sizes[] = {3200, 2040, 2560, 1476, // Pixel tab size | |
129 1920, 1080, 1280, 720, 800, 480, 256, 144}; | |
130 | |
131 for (size_t q = 0; q < arraysize(kQualities); q++) { | |
132 for (size_t outsize = 0; outsize < arraysize(output_sizes); outsize += 2) { | |
133 for (size_t insize = 0; insize < arraysize(input_sizes); insize += 2) { | |
134 uint32_t src_texture; | |
135 gl_->GenTextures(1, &src_texture); | |
136 uint32_t dst_texture; | |
137 gl_->GenTextures(1, &dst_texture); | |
138 uint32_t framebuffer; | |
139 gl_->GenFramebuffers(1, &framebuffer); | |
140 const gfx::Size src_size(input_sizes[insize], 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 gl_->BindFramebuffer(GL_FRAMEBUFFER, framebuffer); | |
150 gl_->BindTexture(GL_TEXTURE_2D, dst_texture); | |
151 gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dst_size.width(), | |
152 dst_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); | |
153 gl_->BindTexture(GL_TEXTURE_2D, src_texture); | |
154 gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_size.width(), | |
155 src_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, | |
156 input.getPixels()); | |
157 | |
158 gfx::Rect src_subrect(0, 0, src_size.width(), src_size.height()); | |
159 std::unique_ptr<display_compositor::GLHelper::ScalerInterface> scaler( | |
160 helper_->CreateScaler(kQualities[q], src_size, src_subrect, | |
161 dst_size, false, false)); | |
162 // Scale once beforehand before we start measuring. | |
163 scaler->Scale(src_texture, dst_texture); | |
164 gl_->Finish(); | |
165 | |
166 base::TimeTicks start_time = base::TimeTicks::Now(); | |
167 int iterations = 0; | |
168 base::TimeTicks end_time; | |
169 while (true) { | |
170 for (int i = 0; i < 50; i++) { | |
171 iterations++; | |
172 scaler->Scale(src_texture, dst_texture); | |
173 gl_->Flush(); | |
174 } | |
175 gl_->Finish(); | |
176 end_time = base::TimeTicks::Now(); | |
177 if (iterations > 2000) { | |
178 break; | |
179 } | |
180 if ((end_time - start_time).InMillisecondsF() > 1000) { | |
181 break; | |
182 } | |
183 } | |
184 gl_->DeleteTextures(1, &dst_texture); | |
185 gl_->DeleteTextures(1, &src_texture); | |
186 gl_->DeleteFramebuffers(1, &framebuffer); | |
187 | |
188 std::string name; | |
189 name = base::StringPrintf("scale_%dx%d_to_%dx%d_%s", src_size.width(), | |
190 src_size.height(), dst_size.width(), | |
191 dst_size.height(), kQualityNames[q]); | |
192 | |
193 float ms = (end_time - start_time).InMillisecondsF() / iterations; | |
194 VLOG(0) << base::StringPrintf("*RESULT gpu_scale_time: %s=%.2f ms\n", | |
195 name.c_str(), ms); | |
196 } | |
197 } | |
198 } | |
199 } | |
200 | |
201 // This is more of a test utility than a test. | |
202 // Put an PNG image called "testimage.png" in your | |
203 // current directory, then run this test. It will | |
204 // create testoutput_Q_P.png, where Q is the scaling | |
205 // mode and P is the scaling percentage taken from | |
206 // the table below. | |
207 TEST_F(GLHelperBenchmark, DISABLED_ScaleTestImage) { | |
208 int percents[] = { | |
209 230, 180, 150, 110, 90, 70, 50, 49, 40, 20, 10, | |
210 }; | |
211 | |
212 SkBitmap input; | |
213 LoadPngFileToSkBitmap(base::FilePath(FILE_PATH_LITERAL("testimage.png")), | |
214 &input); | |
215 | |
216 uint32_t framebuffer; | |
217 gl_->GenFramebuffers(1, &framebuffer); | |
218 uint32_t src_texture; | |
219 gl_->GenTextures(1, &src_texture); | |
220 const gfx::Size src_size(input.width(), input.height()); | |
221 gl_->BindFramebuffer(GL_FRAMEBUFFER, framebuffer); | |
222 gl_->BindTexture(GL_TEXTURE_2D, src_texture); | |
223 gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_size.width(), | |
224 src_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, | |
225 input.getPixels()); | |
226 | |
227 for (size_t q = 0; q < arraysize(kQualities); q++) { | |
228 for (size_t p = 0; p < arraysize(percents); p++) { | |
229 const gfx::Size dst_size(input.width() * percents[p] / 100, | |
230 input.height() * percents[p] / 100); | |
231 uint32_t dst_texture = helper_->CopyAndScaleTexture( | |
232 src_texture, src_size, dst_size, false, kQualities[q]); | |
233 | |
234 SkBitmap output_pixels; | |
235 output_pixels.allocN32Pixels(dst_size.width(), dst_size.height()); | |
236 | |
237 helper_->ReadbackTextureSync( | |
238 dst_texture, gfx::Rect(0, 0, dst_size.width(), dst_size.height()), | |
239 static_cast<unsigned char*>(output_pixels.getPixels()), | |
240 kN32_SkColorType); | |
241 gl_->DeleteTextures(1, &dst_texture); | |
242 std::string filename = base::StringPrintf("testoutput_%s_%d.ppm", | |
243 kQualityNames[q], percents[p]); | |
244 VLOG(0) << "Writing " << filename; | |
245 SaveToFile(&output_pixels, base::FilePath::FromUTF8Unsafe(filename)); | |
246 } | |
247 } | |
248 gl_->DeleteTextures(1, &src_texture); | |
249 gl_->DeleteFramebuffers(1, &framebuffer); | |
250 } | |
251 | |
252 } // namespace display_compositor | |
OLD | NEW |