Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: gpu/perftests/texture_upload_perftest.cc

Issue 895853003: Update from https://crrev.com/314320 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gpu/perftests/run_all_tests.cc ('k') | mojo/converters/surfaces/surfaces_type_converters.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 <vector>
7
8 #include "base/containers/small_map.h"
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 gpu {
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 gfx::GLSurface::InitializeOneOff();
101 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(size_);
102 gl_context_ = gfx::GLContext::CreateGLContext(NULL, // share_group
103 surface_.get(),
104 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() : name(), wall_time(){};
158 Measurement(const std::string& name, const base::TimeDelta wall_time)
159 : name(name), wall_time(wall_time){};
160 std::string name;
161 base::TimeDelta wall_time;
162 };
163 // Upload and draw on the offscren surface.
164 // Return a list of pair. Each pair describe a gl operation and the wall
165 // time elapsed in milliseconds.
166 std::vector<Measurement> UploadAndDraw(const std::vector<uint8>& pixels,
167 const GLenum format,
168 const GLenum type) {
169 std::vector<Measurement> measurements;
170 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get());
171
172 base::ElapsedTimer total_timer;
173 GLuint texture_id = 0;
174
175 base::ElapsedTimer tex_timer;
176 glActiveTexture(GL_TEXTURE0);
177 glGenTextures(1, &texture_id);
178 glBindTexture(GL_TEXTURE_2D, texture_id);
179
180 glTexImage2D(GL_TEXTURE_2D, 0, format, size_.width(), size_.height(), 0,
181 format, type, &pixels[0]);
182 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
183 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
185 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
186 CheckNoGlError();
187 measurements.push_back(Measurement("teximage2d", tex_timer.Elapsed()));
188
189 base::ElapsedTimer draw_timer;
190 glUseProgram(program_object_);
191 glUniform1i(sampler_location_, 0);
192
193 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
194 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
195 glEnableVertexAttribArray(0);
196
197 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
198 measurements.push_back(Measurement("drawarrays", draw_timer.Elapsed()));
199
200 base::ElapsedTimer finish_timer;
201 glFinish();
202 CheckNoGlError();
203 measurements.push_back(Measurement("finish", finish_timer.Elapsed()));
204 measurements.push_back(Measurement("total", total_timer.Elapsed()));
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 EXPECT_EQ(static_cast<GLenum>(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<gfx::GLContext> gl_context_;
222 scoped_refptr<gfx::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 base::SmallMap<std::map<std::string, Measurement>>
236 aggregates; // indexed by name
237 for (int i = 0; i < kUploadPerfWarmupRuns + kUploadPerfTestRuns; ++i) {
238 GenerateTextureData(size_, i + 1, &pixels);
239 auto run = UploadAndDraw(pixels, GL_RGBA, GL_UNSIGNED_BYTE);
240 if (i >= kUploadPerfWarmupRuns) {
241 for (const Measurement& m : run) {
242 auto& agg = aggregates[m.name];
243 agg.name = m.name;
244 agg.wall_time += m.wall_time;
245 }
246 }
247 }
248
249 for (const auto& entry : aggregates) {
250 const auto& m = entry.second;
251 perf_test::PrintResult(
252 m.name, "", "", (m.wall_time / kUploadPerfTestRuns).InMillisecondsF(),
253 "ms", true);
254 }
255 }
256
257 } // namespace
258 } // namespace gpu
OLDNEW
« no previous file with comments | « gpu/perftests/run_all_tests.cc ('k') | mojo/converters/surfaces/surfaces_type_converters.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698