OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/containers/small_map.h" | 8 #include "base/containers/small_map.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "gpu/perftests/measurements.h" | 13 #include "gpu/perftests/measurements.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "testing/perf/perf_test.h" | 16 #include "testing/perf/perf_test.h" |
16 #include "ui/gfx/geometry/size.h" | 17 #include "ui/gfx/geometry/size.h" |
17 #include "ui/gl/gl_bindings.h" | 18 #include "ui/gl/gl_bindings.h" |
18 #include "ui/gl/gl_context.h" | 19 #include "ui/gl/gl_context.h" |
| 20 #include "ui/gl/gl_enums.h" |
19 #include "ui/gl/gl_surface.h" | 21 #include "ui/gl/gl_surface.h" |
20 #include "ui/gl/gpu_timing.h" | 22 #include "ui/gl/gpu_timing.h" |
21 #include "ui/gl/scoped_make_current.h" | 23 #include "ui/gl/scoped_make_current.h" |
22 | 24 |
23 namespace gpu { | 25 namespace gpu { |
24 namespace { | 26 namespace { |
25 | 27 |
26 const int kUploadPerfWarmupRuns = 10; | 28 const int kUploadPerfWarmupRuns = 10; |
27 const int kUploadPerfTestRuns = 100; | 29 const int kUploadPerfTestRuns = 100; |
28 | 30 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 scoped_ptr<char> error_log(new char[len]); | 72 scoped_ptr<char> error_log(new char[len]); |
71 glGetShaderInfoLog(shader, len, NULL, error_log.get()); | 73 glGetShaderInfoLog(shader, len, NULL, error_log.get()); |
72 LOG(ERROR) << "Error compiling shader: " << error_log.get(); | 74 LOG(ERROR) << "Error compiling shader: " << error_log.get(); |
73 } | 75 } |
74 } | 76 } |
75 CHECK_NE(0, compiled); | 77 CHECK_NE(0, compiled); |
76 return shader; | 78 return shader; |
77 } | 79 } |
78 | 80 |
79 void GenerateTextureData(const gfx::Size& size, | 81 void GenerateTextureData(const gfx::Size& size, |
| 82 int bytes_per_pixel, |
80 const int seed, | 83 const int seed, |
81 std::vector<uint8>* const pixels) { | 84 std::vector<uint8>* const pixels) { |
82 pixels->resize(size.GetArea() * 4); | 85 int bytes = size.GetArea() * bytes_per_pixel; |
83 for (int y = 0; y < size.height(); ++y) { | 86 pixels->resize(bytes); |
84 for (int x = 0; x < size.width(); ++x) { | 87 for (int i = 0; i < bytes; ++i) { |
85 const size_t offset = (y * size.width() + x) * 4; | 88 int channel = i % bytes_per_pixel; |
86 pixels->at(offset) = (y + seed) % 64; | 89 if (channel == 3) { // Alpha channel. |
87 pixels->at(offset + 1) = (x + seed) % 128; | 90 pixels->at(i) = 255; |
88 pixels->at(offset + 2) = (y + x + seed) % 256; | 91 } else { |
89 pixels->at(offset + 3) = 255; | 92 pixels->at(i) = (i + (seed << 2)) % (32 << channel); |
90 } | 93 } |
91 } | 94 } |
92 } | 95 } |
93 | 96 |
| 97 // Compare a buffer containing pixels in a specified format to GL_RGBA buffer |
| 98 // where the former buffer have been uploaded as a texture and drawn on the |
| 99 // RGBA buffer. |
| 100 bool CompareBufferToRGBABuffer(GLenum format, |
| 101 const std::vector<uint8>& pixels, |
| 102 const std::vector<uint8>& pixels_rgba) { |
| 103 for (size_t i = 0; i < pixels.size(); i += 4) { |
| 104 switch (format) { |
| 105 case GL_RED_EXT: // (R_t, 0, 0, 1) |
| 106 if (pixels_rgba[i] != pixels[i / 4] || pixels_rgba[i + 1] != 0 || |
| 107 pixels_rgba[i + 2] != 0 || pixels_rgba[i + 3] != 255) { |
| 108 return false; |
| 109 } |
| 110 break; |
| 111 case GL_LUMINANCE: // (L_t, L_t, L_t, 1) |
| 112 if (pixels_rgba[i] != pixels[i / 4] || |
| 113 pixels_rgba[i + 1] != pixels[i / 4] || |
| 114 pixels_rgba[i + 2] != pixels[i / 4] || pixels_rgba[i + 3] != 255) { |
| 115 return false; |
| 116 } |
| 117 break; |
| 118 case GL_RGBA: // (R_t, G_t, B_t, A_t) |
| 119 if (pixels_rgba[i] != pixels[i] || |
| 120 pixels_rgba[i + 1] != pixels[i + 1] || |
| 121 pixels_rgba[i + 2] != pixels[i + 2] || |
| 122 pixels_rgba[i + 3] != pixels[i + 3]) { |
| 123 return false; |
| 124 } |
| 125 break; |
| 126 default: |
| 127 NOTREACHED(); |
| 128 } |
| 129 } |
| 130 return true; |
| 131 } |
| 132 |
94 // PerfTest to check costs of texture upload at different stages | 133 // PerfTest to check costs of texture upload at different stages |
95 // on different platforms. | 134 // on different platforms. |
96 class TextureUploadPerfTest : public testing::Test { | 135 class TextureUploadPerfTest : public testing::Test { |
97 public: | 136 public: |
98 TextureUploadPerfTest() : fbo_size_(1024, 1024) {} | 137 TextureUploadPerfTest() : fbo_size_(1024, 1024) {} |
99 | 138 |
100 // Overridden from testing::Test | 139 // Overridden from testing::Test |
101 void SetUp() override { | 140 void SetUp() override { |
102 // Initialize an offscreen surface and a gl context. | 141 // Initialize an offscreen surface and a gl context. |
103 gfx::GLSurface::InitializeOneOff(); | 142 gfx::GLSurface::InitializeOneOff(); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 gl_context_ = nullptr; | 236 gl_context_ = nullptr; |
198 surface_ = nullptr; | 237 surface_ = nullptr; |
199 } | 238 } |
200 | 239 |
201 protected: | 240 protected: |
202 // Upload and draw on the offscren surface. | 241 // Upload and draw on the offscren surface. |
203 // Return a list of pair. Each pair describe a gl operation and the wall | 242 // Return a list of pair. Each pair describe a gl operation and the wall |
204 // time elapsed in milliseconds. | 243 // time elapsed in milliseconds. |
205 std::vector<Measurement> UploadAndDraw(const gfx::Size& size, | 244 std::vector<Measurement> UploadAndDraw(const gfx::Size& size, |
206 const std::vector<uint8>& pixels, | 245 const std::vector<uint8>& pixels, |
207 const GLenum format, | 246 const GLenum format) { |
208 const GLenum type) { | |
209 MeasurementTimers total_timers(gpu_timing_client_.get()); | 247 MeasurementTimers total_timers(gpu_timing_client_.get()); |
210 GLuint texture_id = 0; | 248 GLuint texture_id = 0; |
211 | 249 |
212 MeasurementTimers tex_timers(gpu_timing_client_.get()); | 250 MeasurementTimers tex_timers(gpu_timing_client_.get()); |
213 glActiveTexture(GL_TEXTURE0); | 251 glActiveTexture(GL_TEXTURE0); |
214 glGenTextures(1, &texture_id); | 252 glGenTextures(1, &texture_id); |
215 glBindTexture(GL_TEXTURE_2D, texture_id); | 253 glBindTexture(GL_TEXTURE_2D, texture_id); |
216 | 254 |
217 glTexImage2D(GL_TEXTURE_2D, 0, format, size.width(), size.height(), 0, | 255 glTexImage2D(GL_TEXTURE_2D, 0, format, size.width(), size.height(), 0, |
218 format, type, &pixels[0]); | 256 format, GL_UNSIGNED_BYTE, &pixels[0]); |
219 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 257 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
220 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 258 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
221 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 259 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
222 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
223 CheckNoGlError(); | 261 CheckNoGlError(); |
224 tex_timers.Record(); | 262 tex_timers.Record(); |
225 | 263 |
226 MeasurementTimers draw_timers(gpu_timing_client_.get()); | 264 MeasurementTimers draw_timers(gpu_timing_client_.get()); |
227 glUseProgram(program_object_); | 265 glUseProgram(program_object_); |
228 glUniform1i(sampler_location_, 0); | 266 glUniform1i(sampler_location_, 0); |
(...skipping 11 matching lines...) Expand all Loading... |
240 | 278 |
241 MeasurementTimers finish_timers(gpu_timing_client_.get()); | 279 MeasurementTimers finish_timers(gpu_timing_client_.get()); |
242 glFinish(); | 280 glFinish(); |
243 CheckNoGlError(); | 281 CheckNoGlError(); |
244 finish_timers.Record(); | 282 finish_timers.Record(); |
245 total_timers.Record(); | 283 total_timers.Record(); |
246 | 284 |
247 glDeleteTextures(1, &texture_id); | 285 glDeleteTextures(1, &texture_id); |
248 | 286 |
249 std::vector<uint8> pixels_rendered(size.GetArea() * 4); | 287 std::vector<uint8> pixels_rendered(size.GetArea() * 4); |
250 glReadPixels(0, 0, size.width(), size.height(), GL_RGBA, type, | 288 glReadPixels(0, 0, size.width(), size.height(), GL_RGBA, GL_UNSIGNED_BYTE, |
251 &pixels_rendered[0]); | 289 &pixels_rendered[0]); |
252 CheckNoGlError(); | 290 CheckNoGlError(); |
253 | 291 EXPECT_TRUE(CompareBufferToRGBABuffer(format, pixels, pixels_rendered)) |
254 // TODO(dcastagna): don't assume the format of the texture and do | 292 << "Format is: " << gfx::GLEnums::GetStringEnum(format); |
255 // the appropriate format conversion. | |
256 EXPECT_EQ(static_cast<GLenum>(GL_RGBA), format); | |
257 EXPECT_EQ(pixels, pixels_rendered); | |
258 | 293 |
259 std::vector<Measurement> measurements; | 294 std::vector<Measurement> measurements; |
260 bool gpu_timer_errors = | 295 bool gpu_timer_errors = |
261 gpu_timing_client_->IsAvailable() && | 296 gpu_timing_client_->IsAvailable() && |
262 gpu_timing_client_->CheckAndResetTimerErrors(); | 297 gpu_timing_client_->CheckAndResetTimerErrors(); |
263 if (!gpu_timer_errors) { | 298 if (!gpu_timer_errors) { |
264 measurements.push_back(total_timers.GetAsMeasurement("total")); | 299 measurements.push_back(total_timers.GetAsMeasurement("total")); |
265 measurements.push_back(tex_timers.GetAsMeasurement("teximage2d")); | 300 measurements.push_back(tex_timers.GetAsMeasurement("teximage2d")); |
266 measurements.push_back(draw_timers.GetAsMeasurement("drawarrays")); | 301 measurements.push_back(draw_timers.GetAsMeasurement("drawarrays")); |
267 measurements.push_back(finish_timers.GetAsMeasurement("finish")); | 302 measurements.push_back(finish_timers.GetAsMeasurement("finish")); |
268 } | 303 } |
269 return measurements; | 304 return measurements; |
270 } | 305 } |
271 | 306 |
272 void RunUploadAndDrawMultipleTimes(const gfx::Size& size) { | 307 void RunUploadAndDrawMultipleTimes(const gfx::Size& size, |
| 308 const GLenum format) { |
273 std::vector<uint8> pixels; | 309 std::vector<uint8> pixels; |
274 base::SmallMap<std::map<std::string, Measurement>> | 310 base::SmallMap<std::map<std::string, Measurement>> |
275 aggregates; // indexed by name | 311 aggregates; // indexed by name |
276 int successful_runs = 0; | 312 int successful_runs = 0; |
| 313 ASSERT_THAT(format, testing::AnyOf(GL_RGBA, GL_LUMINANCE, GL_RED_EXT)); |
277 for (int i = 0; i < kUploadPerfWarmupRuns + kUploadPerfTestRuns; ++i) { | 314 for (int i = 0; i < kUploadPerfWarmupRuns + kUploadPerfTestRuns; ++i) { |
278 GenerateTextureData(size, i + 1, &pixels); | 315 GenerateTextureData(size, format == GL_RGBA ? 4 : 1, i + 1, &pixels); |
279 auto run = UploadAndDraw(size, pixels, GL_RGBA, GL_UNSIGNED_BYTE); | 316 auto run = UploadAndDraw(size, pixels, format); |
280 if (i < kUploadPerfWarmupRuns || !run.size()) { | 317 if (i < kUploadPerfWarmupRuns || !run.size()) { |
281 continue; | 318 continue; |
282 } | 319 } |
283 successful_runs++; | 320 successful_runs++; |
284 for (const Measurement& measurement : run) { | 321 for (const Measurement& measurement : run) { |
285 auto& aggregate = aggregates[measurement.name]; | 322 auto& aggregate = aggregates[measurement.name]; |
286 aggregate.name = measurement.name; | 323 aggregate.name = measurement.name; |
287 aggregate.Increment(measurement); | 324 aggregate.Increment(measurement); |
288 } | 325 } |
289 } | 326 } |
| 327 std::string suffix = base::StringPrintf( |
| 328 "_%d_%s", size.width(), gfx::GLEnums::GetStringEnum(format).c_str()); |
290 if (successful_runs) { | 329 if (successful_runs) { |
291 for (const auto& entry : aggregates) { | 330 for (const auto& entry : aggregates) { |
292 const auto m = entry.second.Divide(successful_runs); | 331 const auto m = entry.second.Divide(successful_runs); |
293 m.PrintResult(base::StringPrintf("_%d", size.width())); | 332 m.PrintResult(suffix); |
294 } | 333 } |
295 } | 334 } |
296 perf_test::PrintResult("sample_runs", "", "", | 335 perf_test::PrintResult("sample_runs", suffix, "", |
297 static_cast<size_t>(successful_runs), "laps", true); | 336 static_cast<size_t>(successful_runs), "laps", true); |
298 } | 337 } |
299 | 338 |
300 const gfx::Size fbo_size_; // for the fbo | 339 const gfx::Size fbo_size_; // for the fbo |
301 scoped_refptr<gfx::GLContext> gl_context_; | 340 scoped_refptr<gfx::GLContext> gl_context_; |
302 scoped_refptr<gfx::GLSurface> surface_; | 341 scoped_refptr<gfx::GLSurface> surface_; |
303 scoped_refptr<gfx::GPUTimingClient> gpu_timing_client_; | 342 scoped_refptr<gfx::GPUTimingClient> gpu_timing_client_; |
304 | 343 |
305 GLuint color_texture_ = 0; | 344 GLuint color_texture_ = 0; |
306 GLuint framebuffer_object_ = 0; | 345 GLuint framebuffer_object_ = 0; |
307 GLuint vertex_shader_ = 0; | 346 GLuint vertex_shader_ = 0; |
308 GLuint fragment_shader_ = 0; | 347 GLuint fragment_shader_ = 0; |
309 GLuint program_object_ = 0; | 348 GLuint program_object_ = 0; |
310 GLint sampler_location_ = -1; | 349 GLint sampler_location_ = -1; |
311 GLuint vertex_buffer_ = 0; | 350 GLuint vertex_buffer_ = 0; |
312 }; | 351 }; |
313 | 352 |
314 // Perf test that generates, uploads and draws a texture on a surface repeatedly | 353 // Perf test that generates, uploads and draws a texture on a surface repeatedly |
315 // and prints out aggregated measurements for all the runs. | 354 // and prints out aggregated measurements for all the runs. |
316 TEST_F(TextureUploadPerfTest, glTexImage2d) { | 355 TEST_F(TextureUploadPerfTest, glTexImage2d) { |
317 int sizes[] = {128, 256, 512, 1024}; | 356 int sizes[] = {128, 256, 512, 1024}; |
| 357 std::vector<GLenum> formats; |
| 358 formats.push_back(GL_RGBA); |
| 359 // Used by default for ResourceProvider::yuv_resource_format_. |
| 360 formats.push_back(GL_LUMINANCE); |
| 361 |
| 362 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); |
| 363 bool has_texture_rg = gl_context_->HasExtension("GL_EXT_texture_rg") || |
| 364 gl_context_->HasExtension("GL_ARB_texture_rg"); |
| 365 |
| 366 if (has_texture_rg) { |
| 367 // Used as ResourceProvider::yuv_resource_format_ if |
| 368 // {ARB,EXT}_texture_rg is available. |
| 369 formats.push_back(GL_RED_EXT); |
| 370 } |
318 for (int side : sizes) { | 371 for (int side : sizes) { |
319 ASSERT_GE(fbo_size_.width(), side); | 372 ASSERT_GE(fbo_size_.width(), side); |
320 ASSERT_GE(fbo_size_.height(), side); | 373 ASSERT_GE(fbo_size_.height(), side); |
| 374 gfx::Size size(side, side); |
321 | 375 |
322 gfx::Size size(side, side); | 376 for (GLenum format : formats) { |
323 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | 377 GenerateVertexBuffer(size); |
324 GenerateVertexBuffer(size); | 378 DCHECK_NE(0u, framebuffer_object_); |
| 379 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer_object_); |
325 | 380 |
326 DCHECK_NE(0u, framebuffer_object_); | 381 RunUploadAndDrawMultipleTimes(size, format); |
327 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer_object_); | 382 } |
328 | |
329 RunUploadAndDrawMultipleTimes(size); | |
330 } | 383 } |
331 } | 384 } |
332 | 385 |
333 } // namespace | 386 } // namespace |
334 } // namespace gpu | 387 } // namespace gpu |
OLD | NEW |