| 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/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/time/time.h" | 11 #include "gpu/perftests/measurements.h" |
| 12 #include "base/timer/elapsed_timer.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "testing/perf/perf_test.h" | |
| 15 #include "ui/gfx/geometry/size.h" | 13 #include "ui/gfx/geometry/size.h" |
| 16 #include "ui/gl/gl_bindings.h" | 14 #include "ui/gl/gl_bindings.h" |
| 17 #include "ui/gl/gl_context.h" | 15 #include "ui/gl/gl_context.h" |
| 18 #include "ui/gl/gl_surface.h" | 16 #include "ui/gl/gl_surface.h" |
| 19 #include "ui/gl/scoped_make_current.h" | 17 #include "ui/gl/scoped_make_current.h" |
| 20 | 18 |
| 21 namespace gpu { | 19 namespace gpu { |
| 22 namespace { | 20 namespace { |
| 23 | 21 |
| 24 const int kUploadPerfWarmupRuns = 10; | 22 const int kUploadPerfWarmupRuns = 10; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 | 94 |
| 97 // Overridden from testing::Test | 95 // Overridden from testing::Test |
| 98 void SetUp() override { | 96 void SetUp() override { |
| 99 // Initialize an offscreen surface and a gl context. | 97 // Initialize an offscreen surface and a gl context. |
| 100 gfx::GLSurface::InitializeOneOff(); | 98 gfx::GLSurface::InitializeOneOff(); |
| 101 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(size_); | 99 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(size_); |
| 102 gl_context_ = gfx::GLContext::CreateGLContext(NULL, // share_group | 100 gl_context_ = gfx::GLContext::CreateGLContext(NULL, // share_group |
| 103 surface_.get(), | 101 surface_.get(), |
| 104 gfx::PreferIntegratedGpu); | 102 gfx::PreferIntegratedGpu); |
| 105 | 103 |
| 104 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); |
| 105 if (gpu_timing_.Initialize(gl_context_.get())) { |
| 106 LOG(INFO) << "Gpu timing initialized with timer type: " |
| 107 << gpu_timing_.GetTimerTypeName(); |
| 108 gpu_timing_.CheckAndResetTimerErrors(); |
| 109 gpu_timing_.InvalidateTimerOffset(); |
| 110 } else { |
| 111 LOG(WARNING) << "Can't initialize gpu timing"; |
| 112 } |
| 113 |
| 106 // Prepare a simple program and a vertex buffer that will be | 114 // Prepare a simple program and a vertex buffer that will be |
| 107 // used to draw a quad on the offscreen surface. | 115 // used to draw a quad on the offscreen surface. |
| 108 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | |
| 109 vertex_shader_ = LoadShader(GL_VERTEX_SHADER, kVertexShader); | 116 vertex_shader_ = LoadShader(GL_VERTEX_SHADER, kVertexShader); |
| 110 fragment_shader_ = LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); | 117 fragment_shader_ = LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); |
| 111 program_object_ = glCreateProgram(); | 118 program_object_ = glCreateProgram(); |
| 112 CHECK_NE(0u, program_object_); | 119 CHECK_NE(0u, program_object_); |
| 113 | 120 |
| 114 glAttachShader(program_object_, vertex_shader_); | 121 glAttachShader(program_object_, vertex_shader_); |
| 115 glAttachShader(program_object_, fragment_shader_); | 122 glAttachShader(program_object_, fragment_shader_); |
| 116 glBindAttribLocation(program_object_, 0, "a_position"); | 123 glBindAttribLocation(program_object_, 0, "a_position"); |
| 117 glLinkProgram(program_object_); | 124 glLinkProgram(program_object_); |
| 118 | 125 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 146 } | 153 } |
| 147 if (vertex_buffer_ != 0) { | 154 if (vertex_buffer_ != 0) { |
| 148 glDeleteShader(vertex_buffer_); | 155 glDeleteShader(vertex_buffer_); |
| 149 } | 156 } |
| 150 | 157 |
| 151 gl_context_ = nullptr; | 158 gl_context_ = nullptr; |
| 152 surface_ = nullptr; | 159 surface_ = nullptr; |
| 153 } | 160 } |
| 154 | 161 |
| 155 protected: | 162 protected: |
| 156 struct Measurement { | |
| 157 Measurement() : name(), wall_time(){}; | |
| 158 Measurement(const std::string& name, const base::TimeDelta wall_time) | |
| 159 : name(name), wall_time(wall_time){}; | |
| 160 std::string name; | |
| 161 base::TimeDelta wall_time; | |
| 162 }; | |
| 163 // Upload and draw on the offscren surface. | 163 // Upload and draw on the offscren surface. |
| 164 // Return a list of pair. Each pair describe a gl operation and the wall | 164 // Return a list of pair. Each pair describe a gl operation and the wall |
| 165 // time elapsed in milliseconds. | 165 // time elapsed in milliseconds. |
| 166 std::vector<Measurement> UploadAndDraw(const std::vector<uint8>& pixels, | 166 std::vector<Measurement> UploadAndDraw(const std::vector<uint8>& pixels, |
| 167 const GLenum format, | 167 const GLenum format, |
| 168 const GLenum type) { | 168 const GLenum type) { |
| 169 std::vector<Measurement> measurements; | |
| 170 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | 169 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); |
| 171 | 170 |
| 172 base::ElapsedTimer total_timer; | 171 MeasurementTimers total_timers(&gpu_timing_); |
| 173 GLuint texture_id = 0; | 172 GLuint texture_id = 0; |
| 174 | 173 |
| 175 base::ElapsedTimer tex_timer; | 174 MeasurementTimers tex_timers(&gpu_timing_); |
| 176 glActiveTexture(GL_TEXTURE0); | 175 glActiveTexture(GL_TEXTURE0); |
| 177 glGenTextures(1, &texture_id); | 176 glGenTextures(1, &texture_id); |
| 178 glBindTexture(GL_TEXTURE_2D, texture_id); | 177 glBindTexture(GL_TEXTURE_2D, texture_id); |
| 179 | 178 |
| 180 glTexImage2D(GL_TEXTURE_2D, 0, format, size_.width(), size_.height(), 0, | 179 glTexImage2D(GL_TEXTURE_2D, 0, format, size_.width(), size_.height(), 0, |
| 181 format, type, &pixels[0]); | 180 format, type, &pixels[0]); |
| 182 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 181 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 183 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 182 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 185 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 186 CheckNoGlError(); | 185 CheckNoGlError(); |
| 187 measurements.push_back(Measurement("teximage2d", tex_timer.Elapsed())); | 186 tex_timers.Record(); |
| 188 | 187 |
| 189 base::ElapsedTimer draw_timer; | 188 MeasurementTimers draw_timers(&gpu_timing_); |
| 190 glUseProgram(program_object_); | 189 glUseProgram(program_object_); |
| 191 glUniform1i(sampler_location_, 0); | 190 glUniform1i(sampler_location_, 0); |
| 192 | 191 |
| 193 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | 192 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
| 194 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); | 193 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); |
| 195 glEnableVertexAttribArray(0); | 194 glEnableVertexAttribArray(0); |
| 196 | 195 |
| 197 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | 196 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 198 measurements.push_back(Measurement("drawarrays", draw_timer.Elapsed())); | 197 draw_timers.Record(); |
| 199 | 198 |
| 200 base::ElapsedTimer finish_timer; | 199 MeasurementTimers finish_timers(&gpu_timing_); |
| 201 glFinish(); | 200 glFinish(); |
| 202 CheckNoGlError(); | 201 CheckNoGlError(); |
| 203 measurements.push_back(Measurement("finish", finish_timer.Elapsed())); | 202 finish_timers.Record(); |
| 204 measurements.push_back(Measurement("total", total_timer.Elapsed())); | 203 total_timers.Record(); |
| 205 | 204 |
| 206 glDeleteTextures(1, &texture_id); | 205 glDeleteTextures(1, &texture_id); |
| 207 | 206 |
| 208 std::vector<uint8> pixels_rendered(size_.GetArea() * 4); | 207 std::vector<uint8> pixels_rendered(size_.GetArea() * 4); |
| 209 glReadPixels(0, 0, size_.width(), size_.height(), GL_RGBA, type, | 208 glReadPixels(0, 0, size_.width(), size_.height(), GL_RGBA, type, |
| 210 &pixels_rendered[0]); | 209 &pixels_rendered[0]); |
| 211 CheckNoGlError(); | 210 CheckNoGlError(); |
| 212 | 211 |
| 213 // TODO(dcastagna): don't assume the format of the texture and do | 212 // TODO(dcastagna): don't assume the format of the texture and do |
| 214 // the appropriate format conversion. | 213 // the appropriate format conversion. |
| 215 EXPECT_EQ(static_cast<GLenum>(GL_RGBA), format); | 214 EXPECT_EQ(static_cast<GLenum>(GL_RGBA), format); |
| 216 EXPECT_EQ(pixels, pixels_rendered); | 215 EXPECT_EQ(pixels, pixels_rendered); |
| 216 |
| 217 std::vector<Measurement> measurements; |
| 218 measurements.push_back(total_timers.GetAsMeasurement("total")); |
| 219 measurements.push_back(tex_timers.GetAsMeasurement("teximage2d")); |
| 220 measurements.push_back(draw_timers.GetAsMeasurement("drawarrays")); |
| 221 measurements.push_back(finish_timers.GetAsMeasurement("finish")); |
| 217 return measurements; | 222 return measurements; |
| 218 } | 223 } |
| 219 | 224 |
| 220 const gfx::Size size_; // for the offscreen surface and the texture | 225 const gfx::Size size_; // for the offscreen surface and the texture |
| 221 scoped_refptr<gfx::GLContext> gl_context_; | 226 scoped_refptr<gfx::GLContext> gl_context_; |
| 222 scoped_refptr<gfx::GLSurface> surface_; | 227 scoped_refptr<gfx::GLSurface> surface_; |
| 228 GPUTiming gpu_timing_; |
| 223 | 229 |
| 224 GLuint vertex_shader_ = 0; | 230 GLuint vertex_shader_ = 0; |
| 225 GLuint fragment_shader_ = 0; | 231 GLuint fragment_shader_ = 0; |
| 226 GLuint program_object_ = 0; | 232 GLuint program_object_ = 0; |
| 227 GLint sampler_location_ = -1; | 233 GLint sampler_location_ = -1; |
| 228 GLuint vertex_buffer_ = 0; | 234 GLuint vertex_buffer_ = 0; |
| 229 }; | 235 }; |
| 230 | 236 |
| 231 // Perf test that generates, uploads and draws a texture on a surface repeatedly | 237 // Perf test that generates, uploads and draws a texture on a surface repeatedly |
| 232 // and prints out aggregated measurements for all the runs. | 238 // and prints out aggregated measurements for all the runs. |
| 233 TEST_F(TextureUploadPerfTest, glTexImage2d) { | 239 TEST_F(TextureUploadPerfTest, glTexImage2d) { |
| 234 std::vector<uint8> pixels; | 240 std::vector<uint8> pixels; |
| 235 base::SmallMap<std::map<std::string, Measurement>> | 241 base::SmallMap<std::map<std::string, Measurement>> |
| 236 aggregates; // indexed by name | 242 aggregates; // indexed by name |
| 237 for (int i = 0; i < kUploadPerfWarmupRuns + kUploadPerfTestRuns; ++i) { | 243 for (int i = 0; i < kUploadPerfWarmupRuns + kUploadPerfTestRuns; ++i) { |
| 238 GenerateTextureData(size_, i + 1, &pixels); | 244 GenerateTextureData(size_, i + 1, &pixels); |
| 239 auto run = UploadAndDraw(pixels, GL_RGBA, GL_UNSIGNED_BYTE); | 245 auto run = UploadAndDraw(pixels, GL_RGBA, GL_UNSIGNED_BYTE); |
| 240 if (i >= kUploadPerfWarmupRuns) { | 246 if (i >= kUploadPerfWarmupRuns) { |
| 241 for (const Measurement& m : run) { | 247 for (const Measurement& m : run) { |
| 242 auto& agg = aggregates[m.name]; | 248 auto& agg = aggregates[m.name]; |
| 243 agg.name = m.name; | 249 agg.name = m.name; |
| 244 agg.wall_time += m.wall_time; | 250 agg.Increment(m); |
| 245 } | 251 } |
| 246 } | 252 } |
| 247 } | 253 } |
| 248 | 254 |
| 249 for (const auto& entry : aggregates) { | 255 for (const auto& entry : aggregates) { |
| 250 const auto& m = entry.second; | 256 const auto m = entry.second.Divide(kUploadPerfTestRuns); |
| 251 perf_test::PrintResult( | 257 m.PrintResult(); |
| 252 m.name, "", "", (m.wall_time / kUploadPerfTestRuns).InMillisecondsF(), | |
| 253 "ms", true); | |
| 254 } | 258 } |
| 255 } | 259 } |
| 256 | 260 |
| 257 } // namespace | 261 } // namespace |
| 258 } // namespace gpu | 262 } // namespace gpu |
| OLD | NEW |