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 // Upload and draw on the offscren surface. | |
156 // Return a list of pair. Each pair describe a gl operation and the wall | |
157 // time elapsed in milliseconds. | |
158 std::vector<std::pair<std::string, float>> UploadAndDraw( | |
159 const std::vector<uint8>& pixels) { | |
160 std::vector<std::pair<std::string, float>> timings; | |
161 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | |
162 | |
163 base::ElapsedTimer total_timer; | |
164 GLuint texture_id = 0; | |
165 | |
166 base::ElapsedTimer upload_timer; | |
167 glActiveTexture(GL_TEXTURE0); | |
168 glGenTextures(1, &texture_id); | |
169 glBindTexture(GL_TEXTURE_2D, texture_id); | |
170 | |
171 const GLenum format = GL_RGBA; | |
172 const GLenum type = GL_UNSIGNED_BYTE; | |
reveman
2015/01/27 13:33:43
Should format, type be passed as parameters instea
Daniele Castagna
2015/01/27 19:00:42
Done.
| |
173 glTexImage2D(GL_TEXTURE_2D, 0, format, size_.width(), size_.height(), 0, | |
174 format, type, &pixels[0]); | |
175 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
176 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
177 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
178 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
179 CheckNoGlError(); | |
180 timings.push_back(std::make_pair("upload_image", | |
reveman
2015/01/27 13:33:42
Let's name this "teximage2d" instead as discussed.
Daniele Castagna
2015/01/27 19:00:42
Done.
| |
181 upload_timer.Elapsed().InMillisecondsF())); | |
182 | |
183 base::ElapsedTimer draw_timer; | |
184 glUseProgram(program_object_); | |
185 glUniform1i(sampler_location_, 0); | |
186 | |
187 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | |
188 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); | |
189 glEnableVertexAttribArray(0); | |
190 | |
191 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
192 timings.push_back( | |
193 std::make_pair("draw_image", draw_timer.Elapsed().InMillisecondsF())); | |
reveman
2015/01/27 13:33:42
nit: "drawarrays"
Daniele Castagna
2015/01/27 19:00:42
Done.
| |
194 | |
195 base::ElapsedTimer finish_timer; | |
196 glFinish(); | |
197 CheckNoGlError(); | |
198 timings.push_back( | |
199 std::make_pair("finish", finish_timer.Elapsed().InMillisecondsF())); | |
200 timings.push_back( | |
201 std::make_pair("total", total_timer.Elapsed().InMillisecondsF())); | |
202 | |
203 glDeleteTextures(1, &texture_id); | |
204 | |
205 std::vector<uint8> pixels_rendered(size_.GetArea() * 4); | |
206 glReadPixels(0, 0, size_.width(), size_.height(), format, type, | |
reveman
2015/01/27 13:33:43
Is the format passed to read pixels really suppose
Daniele Castagna
2015/01/27 19:00:42
You're right, it's not supposed to be the same as
| |
207 &pixels_rendered[0]); | |
208 CheckNoGlError(); | |
209 | |
210 EXPECT_EQ(pixels, pixels_rendered); | |
211 return timings; | |
212 } | |
213 | |
214 const gfx::Size size_; // for the offscreen surface and the texture | |
reveman
2015/01/27 13:33:43
nit: s/size_/kSize/
Daniele Castagna
2015/01/27 19:00:42
Are you sure? This is not a compile time constant:
| |
215 scoped_refptr<GLContext> gl_context_; | |
216 scoped_refptr<GLSurface> surface_; | |
217 | |
218 GLuint vertex_shader_ = 0; | |
219 GLuint fragment_shader_ = 0; | |
220 GLuint program_object_ = 0; | |
221 GLint sampler_location_ = -1; | |
222 GLuint vertex_buffer_ = 0; | |
223 }; | |
224 | |
225 // Perf test that generates, uploads and draws a texture on a surface repeatedly | |
226 // and prints out aggregated timings for all the runs. | |
227 TEST_F(TextureUploadPerfTest, glTexImage2d) { | |
228 std::vector<uint8> pixels; | |
229 std::map<std::string, float> timings; | |
reveman
2015/01/27 13:33:43
nit: s/timings/measurements/
and maybe add a "Mea
Daniele Castagna
2015/01/27 19:00:42
Done.
| |
230 for (int i = 0; i < kUploadPerfWarmupRuns + kUploadPerfTestRuns; ++i) { | |
231 GenerateTextureData(size_, i + 1, &pixels); | |
232 auto run = UploadAndDraw(pixels); | |
233 if (i >= kUploadPerfWarmupRuns) { | |
234 for (const std::pair<std::string, float>& t : run) { | |
235 timings[t.first] += t.second; | |
236 } | |
237 } | |
238 } | |
239 | |
240 for (const std::pair<std::string, float>& t : timings) { | |
241 perf_test::PrintResult(t.first, "", "", t.second / kUploadPerfTestRuns, | |
242 "ms", true); | |
243 } | |
244 } | |
245 | |
246 } // namespace | |
247 } // namespace cc | |
OLD | NEW |