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