| 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" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 // PerfTest to check costs of texture upload at different stages | 89 // PerfTest to check costs of texture upload at different stages |
| 90 // on different platforms. | 90 // on different platforms. |
| 91 class TextureUploadPerfTest : public testing::Test { | 91 class TextureUploadPerfTest : public testing::Test { |
| 92 public: | 92 public: |
| 93 TextureUploadPerfTest() : size_(512, 512) {} | 93 TextureUploadPerfTest() : size_(512, 512) {} |
| 94 | 94 |
| 95 // Overridden from testing::Test | 95 // Overridden from testing::Test |
| 96 void SetUp() override { | 96 void SetUp() override { |
| 97 // Initialize an offscreen surface and a gl context. | 97 // Initialize an offscreen surface and a gl context. |
| 98 gfx::GLSurface::InitializeOneOff(); | 98 gfx::GLSurface::InitializeOneOff(); |
| 99 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(size_); | 99 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(4, 4)); |
| 100 gl_context_ = gfx::GLContext::CreateGLContext(NULL, // share_group | 100 gl_context_ = gfx::GLContext::CreateGLContext(NULL, // share_group |
| 101 surface_.get(), | 101 surface_.get(), |
| 102 gfx::PreferIntegratedGpu); | 102 gfx::PreferIntegratedGpu); |
| 103 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); |
| 104 glGenTextures(1, &color_texture_); |
| 105 glBindTexture(GL_TEXTURE_2D, color_texture_); |
| 106 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 107 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 108 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 109 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 110 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size_.width(), size_.height(), 0, |
| 111 GL_RGBA, GL_UNSIGNED_BYTE, nullptr); |
| 103 | 112 |
| 104 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | 113 glGenFramebuffersEXT(1, &framebuffer_object_); |
| 114 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer_object_); |
| 115 |
| 116 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 117 GL_TEXTURE_2D, color_texture_, 0); |
| 118 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), |
| 119 glCheckFramebufferStatusEXT(GL_FRAMEBUFFER)); |
| 120 |
| 121 glViewport(0, 0, size_.width(), size_.height()); |
| 122 |
| 105 if (gpu_timing_.Initialize(gl_context_.get())) { | 123 if (gpu_timing_.Initialize(gl_context_.get())) { |
| 106 LOG(INFO) << "Gpu timing initialized with timer type: " | 124 LOG(INFO) << "Gpu timing initialized with timer type: " |
| 107 << gpu_timing_.GetTimerTypeName(); | 125 << gpu_timing_.GetTimerTypeName(); |
| 108 gpu_timing_.CheckAndResetTimerErrors(); | 126 gpu_timing_.CheckAndResetTimerErrors(); |
| 109 gpu_timing_.InvalidateTimerOffset(); | 127 gpu_timing_.InvalidateTimerOffset(); |
| 110 } else { | 128 } else { |
| 111 LOG(WARNING) << "Can't initialize gpu timing"; | 129 LOG(WARNING) << "Can't initialize gpu timing"; |
| 112 } | 130 } |
| 113 | |
| 114 // Prepare a simple program and a vertex buffer that will be | 131 // Prepare a simple program and a vertex buffer that will be |
| 115 // used to draw a quad on the offscreen surface. | 132 // used to draw a quad on the offscreen surface. |
| 116 vertex_shader_ = LoadShader(GL_VERTEX_SHADER, kVertexShader); | 133 vertex_shader_ = LoadShader(GL_VERTEX_SHADER, kVertexShader); |
| 117 fragment_shader_ = LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); | 134 fragment_shader_ = LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); |
| 118 program_object_ = glCreateProgram(); | 135 program_object_ = glCreateProgram(); |
| 119 CHECK_NE(0u, program_object_); | 136 CHECK_NE(0u, program_object_); |
| 120 | 137 |
| 121 glAttachShader(program_object_, vertex_shader_); | 138 glAttachShader(program_object_, vertex_shader_); |
| 122 glAttachShader(program_object_, fragment_shader_); | 139 glAttachShader(program_object_, fragment_shader_); |
| 123 glBindAttribLocation(program_object_, 0, "a_position"); | 140 glBindAttribLocation(program_object_, 0, "a_position"); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 135 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | 152 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
| 136 static GLfloat positions[] = { | 153 static GLfloat positions[] = { |
| 137 -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, | 154 -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, |
| 138 }; | 155 }; |
| 139 glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW); | 156 glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW); |
| 140 CheckNoGlError(); | 157 CheckNoGlError(); |
| 141 } | 158 } |
| 142 | 159 |
| 143 void TearDown() override { | 160 void TearDown() override { |
| 144 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | 161 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); |
| 145 if (program_object_ != 0) { | 162 glDeleteProgram(program_object_); |
| 146 glDeleteProgram(program_object_); | 163 glDeleteShader(vertex_shader_); |
| 147 } | 164 glDeleteShader(fragment_shader_); |
| 148 if (vertex_shader_ != 0) { | 165 glDeleteShader(vertex_buffer_); |
| 149 glDeleteShader(vertex_shader_); | 166 |
| 150 } | 167 glBindFramebufferEXT(GL_FRAMEBUFFER, 0); |
| 151 if (fragment_shader_ != 0) { | 168 glDeleteFramebuffersEXT(1, &framebuffer_object_); |
| 152 glDeleteShader(fragment_shader_); | 169 glDeleteTextures(1, &color_texture_); |
| 153 } | |
| 154 if (vertex_buffer_ != 0) { | |
| 155 glDeleteShader(vertex_buffer_); | |
| 156 } | |
| 157 | 170 |
| 158 gl_context_ = nullptr; | 171 gl_context_ = nullptr; |
| 159 surface_ = nullptr; | 172 surface_ = nullptr; |
| 160 } | 173 } |
| 161 | 174 |
| 162 protected: | 175 protected: |
| 163 // Upload and draw on the offscren surface. | 176 // Upload and draw on the offscren surface. |
| 164 // Return a list of pair. Each pair describe a gl operation and the wall | 177 // Return a list of pair. Each pair describe a gl operation and the wall |
| 165 // time elapsed in milliseconds. | 178 // time elapsed in milliseconds. |
| 166 std::vector<Measurement> UploadAndDraw(const std::vector<uint8>& pixels, | 179 std::vector<Measurement> UploadAndDraw(const std::vector<uint8>& pixels, |
| 167 const GLenum format, | 180 const GLenum format, |
| 168 const GLenum type) { | 181 const GLenum type) { |
| 169 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | 182 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); |
| 183 DCHECK_NE(0u, framebuffer_object_); |
| 184 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer_object_); |
| 170 | 185 |
| 171 MeasurementTimers total_timers(&gpu_timing_); | 186 MeasurementTimers total_timers(&gpu_timing_); |
| 172 GLuint texture_id = 0; | 187 GLuint texture_id = 0; |
| 173 | 188 |
| 174 MeasurementTimers tex_timers(&gpu_timing_); | 189 MeasurementTimers tex_timers(&gpu_timing_); |
| 175 glActiveTexture(GL_TEXTURE0); | 190 glActiveTexture(GL_TEXTURE0); |
| 176 glGenTextures(1, &texture_id); | 191 glGenTextures(1, &texture_id); |
| 177 glBindTexture(GL_TEXTURE_2D, texture_id); | 192 glBindTexture(GL_TEXTURE_2D, texture_id); |
| 178 | 193 |
| 179 glTexImage2D(GL_TEXTURE_2D, 0, format, size_.width(), size_.height(), 0, | 194 glTexImage2D(GL_TEXTURE_2D, 0, format, size_.width(), size_.height(), 0, |
| 180 format, type, &pixels[0]); | 195 format, type, &pixels[0]); |
| 181 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 182 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 197 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 198 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 199 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 185 CheckNoGlError(); | 200 CheckNoGlError(); |
| 186 tex_timers.Record(); | 201 tex_timers.Record(); |
| 187 | 202 |
| 188 MeasurementTimers draw_timers(&gpu_timing_); | 203 MeasurementTimers draw_timers(&gpu_timing_); |
| 189 glUseProgram(program_object_); | 204 glUseProgram(program_object_); |
| 190 glUniform1i(sampler_location_, 0); | 205 glUniform1i(sampler_location_, 0); |
| 191 | 206 |
| 192 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | 207 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 215 EXPECT_EQ(pixels, pixels_rendered); | 230 EXPECT_EQ(pixels, pixels_rendered); |
| 216 | 231 |
| 217 std::vector<Measurement> measurements; | 232 std::vector<Measurement> measurements; |
| 218 measurements.push_back(total_timers.GetAsMeasurement("total")); | 233 measurements.push_back(total_timers.GetAsMeasurement("total")); |
| 219 measurements.push_back(tex_timers.GetAsMeasurement("teximage2d")); | 234 measurements.push_back(tex_timers.GetAsMeasurement("teximage2d")); |
| 220 measurements.push_back(draw_timers.GetAsMeasurement("drawarrays")); | 235 measurements.push_back(draw_timers.GetAsMeasurement("drawarrays")); |
| 221 measurements.push_back(finish_timers.GetAsMeasurement("finish")); | 236 measurements.push_back(finish_timers.GetAsMeasurement("finish")); |
| 222 return measurements; | 237 return measurements; |
| 223 } | 238 } |
| 224 | 239 |
| 225 const gfx::Size size_; // for the offscreen surface and the texture | 240 const gfx::Size size_; // for the fbo and the texture |
| 226 scoped_refptr<gfx::GLContext> gl_context_; | 241 scoped_refptr<gfx::GLContext> gl_context_; |
| 227 scoped_refptr<gfx::GLSurface> surface_; | 242 scoped_refptr<gfx::GLSurface> surface_; |
| 228 GPUTiming gpu_timing_; | 243 GPUTiming gpu_timing_; |
| 229 | 244 |
| 245 GLuint color_texture_ = 0; |
| 246 GLuint framebuffer_object_ = 0; |
| 230 GLuint vertex_shader_ = 0; | 247 GLuint vertex_shader_ = 0; |
| 231 GLuint fragment_shader_ = 0; | 248 GLuint fragment_shader_ = 0; |
| 232 GLuint program_object_ = 0; | 249 GLuint program_object_ = 0; |
| 233 GLint sampler_location_ = -1; | 250 GLint sampler_location_ = -1; |
| 234 GLuint vertex_buffer_ = 0; | 251 GLuint vertex_buffer_ = 0; |
| 235 }; | 252 }; |
| 236 | 253 |
| 237 // Perf test that generates, uploads and draws a texture on a surface repeatedly | 254 // Perf test that generates, uploads and draws a texture on a surface repeatedly |
| 238 // and prints out aggregated measurements for all the runs. | 255 // and prints out aggregated measurements for all the runs. |
| 239 TEST_F(TextureUploadPerfTest, glTexImage2d) { | 256 TEST_F(TextureUploadPerfTest, glTexImage2d) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 253 } | 270 } |
| 254 | 271 |
| 255 for (const auto& entry : aggregates) { | 272 for (const auto& entry : aggregates) { |
| 256 const auto m = entry.second.Divide(kUploadPerfTestRuns); | 273 const auto m = entry.second.Divide(kUploadPerfTestRuns); |
| 257 m.PrintResult(); | 274 m.PrintResult(); |
| 258 } | 275 } |
| 259 } | 276 } |
| 260 | 277 |
| 261 } // namespace | 278 } // namespace |
| 262 } // namespace gpu | 279 } // namespace gpu |
| OLD | NEW |