| 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" | |
| 10 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 12 #include "gpu/command_buffer/service/gpu_timing.h" | |
| 13 #include "gpu/perftests/measurements.h" | 11 #include "gpu/perftests/measurements.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.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; |
| 25 const int kUploadPerfTestRuns = 100; | 23 const int kUploadPerfTestRuns = 100; |
| 26 | 24 |
| 27 #define SHADER(Src) #Src | 25 #define SHADER(Src) #Src |
| 28 | 26 |
| 29 // clang-format off | 27 // clang-format off |
| 30 const char kVertexShader[] = | 28 const char kVertexShader[] = |
| 31 SHADER( | 29 SHADER( |
| 32 attribute vec2 a_position; | 30 attribute vec2 a_position; |
| 33 varying vec2 v_texCoord; | 31 varying vec2 v_texCoord; |
| 34 void main() { | 32 void main() { |
| 35 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); | 33 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); |
| 36 v_texCoord = vec2((a_position.x + 1.0) * 0.5, (a_position.y + 1.0) * 0.5); | 34 v_texCoord = vec2((a_position.x + 1) * 0.5, (a_position.y + 1) * 0.5); |
| 37 } | 35 } |
| 38 ); | 36 ); |
| 39 const char kFragmentShader[] = | 37 const char kFragmentShader[] = |
| 40 SHADER( | 38 SHADER( |
| 41 uniform sampler2D a_texture; | 39 uniform sampler2D a_texture; |
| 42 varying vec2 v_texCoord; | 40 varying vec2 v_texCoord; |
| 43 void main() { | 41 void main() { |
| 44 gl_FragColor = texture2D(a_texture, v_texCoord.xy); | 42 gl_FragColor = texture2D(a_texture, v_texCoord.xy); |
| 45 } | 43 } |
| 46 ); | 44 ); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 // PerfTest to check costs of texture upload at different stages | 89 // PerfTest to check costs of texture upload at different stages |
| 92 // on different platforms. | 90 // on different platforms. |
| 93 class TextureUploadPerfTest : public testing::Test { | 91 class TextureUploadPerfTest : public testing::Test { |
| 94 public: | 92 public: |
| 95 TextureUploadPerfTest() : size_(512, 512) {} | 93 TextureUploadPerfTest() : size_(512, 512) {} |
| 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(gfx::Size(4, 4)); | 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); |
| 103 |
| 105 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | 104 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); |
| 106 glGenTextures(1, &color_texture_); | |
| 107 glBindTexture(GL_TEXTURE_2D, color_texture_); | |
| 108 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 109 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 112 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size_.width(), size_.height(), 0, | |
| 113 GL_RGBA, GL_UNSIGNED_BYTE, nullptr); | |
| 114 | |
| 115 glGenFramebuffersEXT(1, &framebuffer_object_); | |
| 116 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer_object_); | |
| 117 | |
| 118 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
| 119 GL_TEXTURE_2D, color_texture_, 0); | |
| 120 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), | |
| 121 glCheckFramebufferStatusEXT(GL_FRAMEBUFFER)); | |
| 122 | |
| 123 glViewport(0, 0, size_.width(), size_.height()); | |
| 124 | |
| 125 if (gpu_timing_.Initialize(gl_context_.get())) { | 105 if (gpu_timing_.Initialize(gl_context_.get())) { |
| 126 LOG(INFO) << "Gpu timing initialized with timer type: " | 106 LOG(INFO) << "Gpu timing initialized with timer type: " |
| 127 << gpu_timing_.GetTimerTypeName(); | 107 << gpu_timing_.GetTimerTypeName(); |
| 128 gpu_timing_.CheckAndResetTimerErrors(); | 108 gpu_timing_.CheckAndResetTimerErrors(); |
| 129 gpu_timing_.InvalidateTimerOffset(); | 109 gpu_timing_.InvalidateTimerOffset(); |
| 130 } else { | 110 } else { |
| 131 LOG(WARNING) << "Can't initialize gpu timing"; | 111 LOG(WARNING) << "Can't initialize gpu timing"; |
| 132 } | 112 } |
| 113 |
| 133 // Prepare a simple program and a vertex buffer that will be | 114 // Prepare a simple program and a vertex buffer that will be |
| 134 // used to draw a quad on the offscreen surface. | 115 // used to draw a quad on the offscreen surface. |
| 135 vertex_shader_ = LoadShader(GL_VERTEX_SHADER, kVertexShader); | 116 vertex_shader_ = LoadShader(GL_VERTEX_SHADER, kVertexShader); |
| 136 fragment_shader_ = LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); | 117 fragment_shader_ = LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); |
| 137 program_object_ = glCreateProgram(); | 118 program_object_ = glCreateProgram(); |
| 138 CHECK_NE(0u, program_object_); | 119 CHECK_NE(0u, program_object_); |
| 139 | 120 |
| 140 glAttachShader(program_object_, vertex_shader_); | 121 glAttachShader(program_object_, vertex_shader_); |
| 141 glAttachShader(program_object_, fragment_shader_); | 122 glAttachShader(program_object_, fragment_shader_); |
| 142 glBindAttribLocation(program_object_, 0, "a_position"); | 123 glBindAttribLocation(program_object_, 0, "a_position"); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 154 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | 135 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
| 155 static GLfloat positions[] = { | 136 static GLfloat positions[] = { |
| 156 -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, | 137 -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, |
| 157 }; | 138 }; |
| 158 glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW); | 139 glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW); |
| 159 CheckNoGlError(); | 140 CheckNoGlError(); |
| 160 } | 141 } |
| 161 | 142 |
| 162 void TearDown() override { | 143 void TearDown() override { |
| 163 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | 144 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); |
| 164 glDeleteProgram(program_object_); | 145 if (program_object_ != 0) { |
| 165 glDeleteShader(vertex_shader_); | 146 glDeleteProgram(program_object_); |
| 166 glDeleteShader(fragment_shader_); | 147 } |
| 167 glDeleteShader(vertex_buffer_); | 148 if (vertex_shader_ != 0) { |
| 168 | 149 glDeleteShader(vertex_shader_); |
| 169 glBindFramebufferEXT(GL_FRAMEBUFFER, 0); | 150 } |
| 170 glDeleteFramebuffersEXT(1, &framebuffer_object_); | 151 if (fragment_shader_ != 0) { |
| 171 glDeleteTextures(1, &color_texture_); | 152 glDeleteShader(fragment_shader_); |
| 153 } |
| 154 if (vertex_buffer_ != 0) { |
| 155 glDeleteShader(vertex_buffer_); |
| 156 } |
| 172 | 157 |
| 173 gl_context_ = nullptr; | 158 gl_context_ = nullptr; |
| 174 surface_ = nullptr; | 159 surface_ = nullptr; |
| 175 } | 160 } |
| 176 | 161 |
| 177 protected: | 162 protected: |
| 178 // Upload and draw on the offscren surface. | 163 // Upload and draw on the offscren surface. |
| 179 // 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 |
| 180 // time elapsed in milliseconds. | 165 // time elapsed in milliseconds. |
| 181 std::vector<Measurement> UploadAndDraw(const std::vector<uint8>& pixels, | 166 std::vector<Measurement> UploadAndDraw(const std::vector<uint8>& pixels, |
| 182 const GLenum format, | 167 const GLenum format, |
| 183 const GLenum type) { | 168 const GLenum type) { |
| 184 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | 169 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); |
| 185 DCHECK_NE(0u, framebuffer_object_); | |
| 186 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer_object_); | |
| 187 | 170 |
| 188 MeasurementTimers total_timers(&gpu_timing_); | 171 MeasurementTimers total_timers(&gpu_timing_); |
| 189 GLuint texture_id = 0; | 172 GLuint texture_id = 0; |
| 190 | 173 |
| 191 MeasurementTimers tex_timers(&gpu_timing_); | 174 MeasurementTimers tex_timers(&gpu_timing_); |
| 192 glActiveTexture(GL_TEXTURE0); | 175 glActiveTexture(GL_TEXTURE0); |
| 193 glGenTextures(1, &texture_id); | 176 glGenTextures(1, &texture_id); |
| 194 glBindTexture(GL_TEXTURE_2D, texture_id); | 177 glBindTexture(GL_TEXTURE_2D, texture_id); |
| 195 | 178 |
| 196 glTexImage2D(GL_TEXTURE_2D, 0, format, size_.width(), size_.height(), 0, | 179 glTexImage2D(GL_TEXTURE_2D, 0, format, size_.width(), size_.height(), 0, |
| 197 format, type, &pixels[0]); | 180 format, type, &pixels[0]); |
| 198 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 181 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 199 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 182 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 200 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); |
| 201 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); |
| 202 CheckNoGlError(); | 185 CheckNoGlError(); |
| 203 tex_timers.Record(); | 186 tex_timers.Record(); |
| 204 | 187 |
| 205 MeasurementTimers draw_timers(&gpu_timing_); | 188 MeasurementTimers draw_timers(&gpu_timing_); |
| 206 glUseProgram(program_object_); | 189 glUseProgram(program_object_); |
| 207 glUniform1i(sampler_location_, 0); | 190 glUniform1i(sampler_location_, 0); |
| 208 | 191 |
| 209 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | 192 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 232 EXPECT_EQ(pixels, pixels_rendered); | 215 EXPECT_EQ(pixels, pixels_rendered); |
| 233 | 216 |
| 234 std::vector<Measurement> measurements; | 217 std::vector<Measurement> measurements; |
| 235 measurements.push_back(total_timers.GetAsMeasurement("total")); | 218 measurements.push_back(total_timers.GetAsMeasurement("total")); |
| 236 measurements.push_back(tex_timers.GetAsMeasurement("teximage2d")); | 219 measurements.push_back(tex_timers.GetAsMeasurement("teximage2d")); |
| 237 measurements.push_back(draw_timers.GetAsMeasurement("drawarrays")); | 220 measurements.push_back(draw_timers.GetAsMeasurement("drawarrays")); |
| 238 measurements.push_back(finish_timers.GetAsMeasurement("finish")); | 221 measurements.push_back(finish_timers.GetAsMeasurement("finish")); |
| 239 return measurements; | 222 return measurements; |
| 240 } | 223 } |
| 241 | 224 |
| 242 const gfx::Size size_; // for the fbo and the texture | 225 const gfx::Size size_; // for the offscreen surface and the texture |
| 243 scoped_refptr<gfx::GLContext> gl_context_; | 226 scoped_refptr<gfx::GLContext> gl_context_; |
| 244 scoped_refptr<gfx::GLSurface> surface_; | 227 scoped_refptr<gfx::GLSurface> surface_; |
| 245 GPUTiming gpu_timing_; | 228 GPUTiming gpu_timing_; |
| 246 | 229 |
| 247 GLuint color_texture_ = 0; | |
| 248 GLuint framebuffer_object_ = 0; | |
| 249 GLuint vertex_shader_ = 0; | 230 GLuint vertex_shader_ = 0; |
| 250 GLuint fragment_shader_ = 0; | 231 GLuint fragment_shader_ = 0; |
| 251 GLuint program_object_ = 0; | 232 GLuint program_object_ = 0; |
| 252 GLint sampler_location_ = -1; | 233 GLint sampler_location_ = -1; |
| 253 GLuint vertex_buffer_ = 0; | 234 GLuint vertex_buffer_ = 0; |
| 254 }; | 235 }; |
| 255 | 236 |
| 256 // 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 |
| 257 // and prints out aggregated measurements for all the runs. | 238 // and prints out aggregated measurements for all the runs. |
| 258 TEST_F(TextureUploadPerfTest, glTexImage2d) { | 239 TEST_F(TextureUploadPerfTest, glTexImage2d) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 272 } | 253 } |
| 273 | 254 |
| 274 for (const auto& entry : aggregates) { | 255 for (const auto& entry : aggregates) { |
| 275 const auto m = entry.second.Divide(kUploadPerfTestRuns); | 256 const auto m = entry.second.Divide(kUploadPerfTestRuns); |
| 276 m.PrintResult(); | 257 m.PrintResult(); |
| 277 } | 258 } |
| 278 } | 259 } |
| 279 | 260 |
| 280 } // namespace | 261 } // namespace |
| 281 } // namespace gpu | 262 } // namespace gpu |
| OLD | NEW |