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