Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <algorithm> | |
| 6 #include <map> | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/time/time.h" | |
| 12 #include "base/timer/elapsed_timer.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 #include "testing/perf/perf_test.h" | |
| 15 #include "ui/gfx/geometry/size.h" | |
| 16 #include "ui/gl/gl_bindings.h" | |
| 17 #include "ui/gl/gl_context.h" | |
| 18 #include "ui/gl/gl_surface.h" | |
| 19 #include "ui/gl/scoped_make_current.h" | |
| 20 | |
| 21 namespace gfx { | |
|
piman
2015/01/28 18:35:16
nit: why gfx? gpu instead maybe?
Daniele Castagna
2015/01/28 19:23:04
I originally wrote this under ui/gl. Then I moved
| |
| 22 namespace { | |
| 23 | |
| 24 const int kUploadPerfWarmupRuns = 10; | |
| 25 const int kUploadPerfTestRuns = 100; | |
| 26 | |
| 27 #define SHADER(Src) #Src | |
| 28 | |
| 29 // clang-format off | |
| 30 const char kVertexShader[] = | |
| 31 SHADER( | |
| 32 attribute vec2 a_position; | |
| 33 varying vec2 v_texCoord; | |
| 34 void main() { | |
| 35 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); | |
| 36 v_texCoord = vec2((a_position.x + 1) * 0.5, (a_position.y + 1) * 0.5); | |
| 37 } | |
| 38 ); | |
| 39 const char kFragmentShader[] = | |
| 40 SHADER( | |
| 41 uniform sampler2D a_texture; | |
| 42 varying vec2 v_texCoord; | |
| 43 void main() { | |
| 44 gl_FragColor = texture2D(a_texture, v_texCoord.xy); | |
| 45 } | |
| 46 ); | |
| 47 // clang-format on | |
| 48 | |
| 49 void CheckNoGlError() { | |
| 50 CHECK_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
| 51 } | |
| 52 | |
| 53 // Utility function to compile a shader from a string. | |
| 54 GLuint LoadShader(const GLenum type, const char* const src) { | |
| 55 GLuint shader = 0; | |
| 56 shader = glCreateShader(type); | |
| 57 CHECK_NE(0u, shader); | |
| 58 glShaderSource(shader, 1, &src, NULL); | |
| 59 glCompileShader(shader); | |
| 60 | |
| 61 GLint compiled = 0; | |
| 62 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); | |
| 63 if (compiled == 0) { | |
| 64 GLint len = 0; | |
| 65 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len); | |
| 66 if (len > 1) { | |
| 67 scoped_ptr<char> error_log(new char[len]); | |
| 68 glGetShaderInfoLog(shader, len, NULL, error_log.get()); | |
| 69 LOG(ERROR) << "Error compiling shader: " << error_log.get(); | |
| 70 } | |
| 71 } | |
| 72 CHECK_NE(0, compiled); | |
| 73 return shader; | |
| 74 } | |
| 75 | |
| 76 void GenerateTextureData(const gfx::Size& size, | |
| 77 const int seed, | |
| 78 std::vector<uint8>* const pixels) { | |
| 79 pixels->resize(size.GetArea() * 4); | |
| 80 for (int y = 0; y < size.height(); ++y) { | |
| 81 for (int x = 0; x < size.width(); ++x) { | |
| 82 const size_t offset = (y * size.width() + x) * 4; | |
| 83 pixels->at(offset) = (y + seed) % 256; | |
| 84 pixels->at(offset + 1) = (x + seed) % 256; | |
| 85 pixels->at(offset + 2) = (y + x + seed) % 256; | |
| 86 pixels->at(offset + 3) = 255; | |
| 87 } | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 // PerfTest to check costs of texture upload at different stages | |
| 92 // on different platforms. | |
| 93 class TextureUploadPerfTest : public testing::Test { | |
| 94 public: | |
| 95 TextureUploadPerfTest() : size_(512, 512) {} | |
| 96 | |
| 97 // Overridden from testing::Test | |
| 98 void SetUp() override { | |
| 99 // Initialize an offscreen surface and a gl context. | |
| 100 GLSurface::InitializeOneOff(); | |
| 101 surface_ = GLSurface::CreateOffscreenGLSurface(size_); | |
| 102 gl_context_ = | |
| 103 GLContext::CreateGLContext(NULL, // share_group | |
| 104 surface_.get(), gfx::PreferIntegratedGpu); | |
| 105 | |
| 106 // Prepare a simple program and a vertex buffer that will be | |
| 107 // 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); | |
| 110 fragment_shader_ = LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); | |
| 111 program_object_ = glCreateProgram(); | |
| 112 CHECK_NE(0u, program_object_); | |
| 113 | |
| 114 glAttachShader(program_object_, vertex_shader_); | |
| 115 glAttachShader(program_object_, fragment_shader_); | |
| 116 glBindAttribLocation(program_object_, 0, "a_position"); | |
| 117 glLinkProgram(program_object_); | |
| 118 | |
| 119 GLint linked = -1; | |
| 120 glGetProgramiv(program_object_, GL_LINK_STATUS, &linked); | |
| 121 CHECK_NE(0, linked); | |
| 122 | |
| 123 sampler_location_ = glGetUniformLocation(program_object_, "a_texture"); | |
| 124 CHECK_NE(-1, sampler_location_); | |
| 125 | |
| 126 glGenBuffersARB(1, &vertex_buffer_); | |
| 127 CHECK_NE(0u, vertex_buffer_); | |
| 128 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | |
| 129 static GLfloat positions[] = { | |
| 130 -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, | |
| 131 }; | |
| 132 glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW); | |
| 133 CheckNoGlError(); | |
| 134 } | |
| 135 | |
| 136 void TearDown() override { | |
| 137 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | |
| 138 if (program_object_ != 0) { | |
| 139 glDeleteProgram(program_object_); | |
| 140 } | |
| 141 if (vertex_shader_ != 0) { | |
| 142 glDeleteShader(vertex_shader_); | |
| 143 } | |
| 144 if (fragment_shader_ != 0) { | |
| 145 glDeleteShader(fragment_shader_); | |
| 146 } | |
| 147 if (vertex_buffer_ != 0) { | |
| 148 glDeleteShader(vertex_buffer_); | |
| 149 } | |
| 150 | |
| 151 gl_context_ = nullptr; | |
| 152 surface_ = nullptr; | |
| 153 } | |
| 154 | |
| 155 protected: | |
| 156 struct Measurement { | |
| 157 Measurement(const std::string& name, const base::TimeDelta wall_time) | |
| 158 : name(name), wall_time(wall_time){}; | |
| 159 std::string name; | |
| 160 base::TimeDelta wall_time; | |
| 161 }; | |
| 162 // Upload and draw on the offscren surface. | |
| 163 // Return a list of pair. Each pair describe a gl operation and the wall | |
| 164 // time elapsed in milliseconds. | |
| 165 std::vector<Measurement> UploadAndDraw(const std::vector<uint8>& pixels, | |
| 166 const GLenum format, | |
| 167 const GLenum type) { | |
| 168 std::vector<Measurement> measurements; | |
| 169 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | |
| 170 | |
| 171 base::ElapsedTimer total_timer; | |
| 172 GLuint texture_id = 0; | |
| 173 | |
| 174 base::ElapsedTimer tex_timer; | |
|
piman
2015/01/28 18:35:16
Maybe as a follow-up: it would be useful also to b
Daniele Castagna
2015/01/28 19:23:04
Acknowledged.
Measuerement could be the helper cl
| |
| 175 glActiveTexture(GL_TEXTURE0); | |
| 176 glGenTextures(1, &texture_id); | |
| 177 glBindTexture(GL_TEXTURE_2D, texture_id); | |
| 178 | |
| 179 glTexImage2D(GL_TEXTURE_2D, 0, format, size_.width(), size_.height(), 0, | |
| 180 format, type, &pixels[0]); | |
| 181 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 182 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 183 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); | |
| 185 CheckNoGlError(); | |
| 186 measurements.push_back(Measurement("teximage2d", tex_timer.Elapsed())); | |
| 187 | |
| 188 base::ElapsedTimer draw_timer; | |
| 189 glUseProgram(program_object_); | |
| 190 glUniform1i(sampler_location_, 0); | |
| 191 | |
| 192 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | |
| 193 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); | |
| 194 glEnableVertexAttribArray(0); | |
| 195 | |
| 196 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
| 197 measurements.push_back(Measurement("drawarrays", draw_timer.Elapsed())); | |
| 198 | |
| 199 base::ElapsedTimer finish_timer; | |
| 200 glFinish(); | |
| 201 CheckNoGlError(); | |
| 202 measurements.push_back(Measurement("finish", finish_timer.Elapsed())); | |
| 203 measurements.push_back(Measurement("total", total_timer.Elapsed())); | |
| 204 | |
| 205 glDeleteTextures(1, &texture_id); | |
| 206 | |
| 207 std::vector<uint8> pixels_rendered(size_.GetArea() * 4); | |
| 208 glReadPixels(0, 0, size_.width(), size_.height(), GL_RGBA, type, | |
| 209 &pixels_rendered[0]); | |
| 210 CheckNoGlError(); | |
| 211 | |
| 212 // TODO(dcastagna): don't assume the format of the texture and do | |
| 213 // the appropriate format conversion. | |
| 214 EXPECT_EQ(static_cast<GLenum>(GL_RGBA), format); | |
| 215 EXPECT_EQ(pixels, pixels_rendered); | |
| 216 return measurements; | |
| 217 } | |
| 218 | |
| 219 const gfx::Size size_; // for the offscreen surface and the texture | |
|
piman
2015/01/28 18:35:16
Maybe as a follow-up: I think it would be useful t
Daniele Castagna
2015/01/28 19:23:04
Acknowledged.
| |
| 220 scoped_refptr<GLContext> gl_context_; | |
| 221 scoped_refptr<GLSurface> surface_; | |
| 222 | |
| 223 GLuint vertex_shader_ = 0; | |
| 224 GLuint fragment_shader_ = 0; | |
| 225 GLuint program_object_ = 0; | |
| 226 GLint sampler_location_ = -1; | |
| 227 GLuint vertex_buffer_ = 0; | |
| 228 }; | |
| 229 | |
| 230 // Perf test that generates, uploads and draws a texture on a surface repeatedly | |
| 231 // and prints out aggregated measurements for all the runs. | |
| 232 TEST_F(TextureUploadPerfTest, glTexImage2d) { | |
| 233 std::vector<uint8> pixels; | |
| 234 std::vector<Measurement> aggregates; | |
| 235 for (int i = 0; i < kUploadPerfWarmupRuns + kUploadPerfTestRuns; ++i) { | |
| 236 GenerateTextureData(size_, i + 1, &pixels); | |
| 237 auto run = UploadAndDraw(pixels, GL_RGBA, GL_UNSIGNED_BYTE); | |
| 238 if (i >= kUploadPerfWarmupRuns) { | |
| 239 for (const Measurement& m : run) { | |
| 240 auto it = std::find_if( | |
| 241 aggregates.begin(), aggregates.end(), | |
| 242 [&m](const Measurement& m2) { return m2.name == m.name; }); | |
|
piman
2015/01/28 18:35:16
nit: Maybe a hash map would help?
Daniele Castagna
2015/01/28 19:23:04
The previous patch I uploaded was using a map, rev
| |
| 243 if (it == aggregates.end()) { | |
| 244 aggregates.push_back(Measurement(m.name, base::TimeDelta())); | |
| 245 it = std::prev(aggregates.end()); | |
| 246 } | |
| 247 it->wall_time += m.wall_time; | |
| 248 } | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 for (const auto& m : aggregates) { | |
| 253 perf_test::PrintResult( | |
| 254 m.name, "", "", (m.wall_time / kUploadPerfTestRuns).InMillisecondsF(), | |
| 255 "ms", true); | |
| 256 } | |
| 257 } | |
| 258 | |
| 259 } // namespace | |
| 260 } // namespace cc | |
|
piman
2015/01/28 18:35:16
nit: Please use the same namespace as the opening
Daniele Castagna
2015/01/28 19:23:04
Done.
| |
| OLD | NEW |