Mir

Merge lp:~vanvugt/mir/fix-1349698 into lp:mir

Proposed by Daniel van Vugt
Status: Merged
Approved by: Alexandros Frantzis
Approved revision: no longer in the source branch.
Merged at revision: 1801
Proposed branch: lp:~vanvugt/mir/fix-1349698
Merge into: lp:mir
Diff against target: 237 lines (+24/-32)
8 files modified
examples/demo-shell/demo_renderer.cpp (+0/-22)
include/platform/mir/graphics/gl_primitive.h (+10/-2)
include/server/mir/compositor/gl_renderer.h (+2/-0)
server-ABI-sha1sums (+2/-2)
src/platform/graphics/android/hwc_fallback_gl_renderer.cpp (+1/-1)
src/platform/graphics/tessellation_helpers.cpp (+0/-1)
src/server/compositor/gl_renderer.cpp (+2/-2)
tests/unit-tests/compositor/test_gl_renderer.cpp (+7/-2)
To merge this branch: bzr merge lp:~vanvugt/mir/fix-1349698
Reviewer Review Type Date Requested Status
Kevin DuBois (community) Approve
Alan Griffiths Approve
Alexandros Frantzis (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+228623@code.launchpad.net

Commit message

GLRenderer/DemoRenderer: Optimize out inefficient std::vector operations
that were consuming too much CPU time. (LP: #1349698)

 1. Instead of a vector-of-vectors make it a flat vector of structures.
 2. Cache the primitives vector memory instead of reallocating a new one
    for every renderable on every frame.

This reduces tessellation CPU usage from 12% to 3% and almost doubles the
number of clients I can run under callgrind before they skip frames.

Description of the change

Probably not relevant to Unity8, but since I think I caused this bottleneck I want to fix it ASAP.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Alexandros Frantzis (afrantzis) wrote :

Looks good.

review: Approve
Revision history for this message
Alan Griffiths (alan-griffiths) wrote :

There's a small change in behavior as resize(4) would initialize vertices[], but I guess that's also an optimization.

review: Approve
Revision history for this message
Kevin DuBois (kdub) wrote :

lgtm

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'examples/demo-shell/demo_renderer.cpp'
--- examples/demo-shell/demo_renderer.cpp 2014-06-18 12:50:31 +0000
+++ examples/demo-shell/demo_renderer.cpp 2014-07-29 09:00:23 +0000
@@ -201,8 +201,6 @@
201201
202 auto& right_shadow = primitives[n++];202 auto& right_shadow = primitives[n++];
203 right_shadow.tex_id = shadow_corner_tex;203 right_shadow.tex_id = shadow_corner_tex;
204 right_shadow.type = GL_TRIANGLE_FAN;
205 right_shadow.vertices.resize(4);
206 right_shadow.vertices[0] = {{right, top, 0.0f}, {0.0f, 0.0f}};204 right_shadow.vertices[0] = {{right, top, 0.0f}, {0.0f, 0.0f}};
207 right_shadow.vertices[1] = {{rightr, top, 0.0f}, {1.0f, 0.0f}};205 right_shadow.vertices[1] = {{rightr, top, 0.0f}, {1.0f, 0.0f}};
208 right_shadow.vertices[2] = {{rightr, bottom, 0.0f}, {1.0f, 0.0f}};206 right_shadow.vertices[2] = {{rightr, bottom, 0.0f}, {1.0f, 0.0f}};
@@ -210,8 +208,6 @@
210208
211 auto& left_shadow = primitives[n++];209 auto& left_shadow = primitives[n++];
212 left_shadow.tex_id = shadow_corner_tex;210 left_shadow.tex_id = shadow_corner_tex;
213 left_shadow.type = GL_TRIANGLE_FAN;
214 left_shadow.vertices.resize(4);
215 left_shadow.vertices[0] = {{leftr, top, 0.0f}, {1.0f, 0.0f}};211 left_shadow.vertices[0] = {{leftr, top, 0.0f}, {1.0f, 0.0f}};
216 left_shadow.vertices[1] = {{left, top, 0.0f}, {0.0f, 0.0f}};212 left_shadow.vertices[1] = {{left, top, 0.0f}, {0.0f, 0.0f}};
217 left_shadow.vertices[2] = {{left, bottom, 0.0f}, {0.0f, 0.0f}};213 left_shadow.vertices[2] = {{left, bottom, 0.0f}, {0.0f, 0.0f}};
@@ -219,8 +215,6 @@
219215
220 auto& top_shadow = primitives[n++];216 auto& top_shadow = primitives[n++];
221 top_shadow.tex_id = shadow_corner_tex;217 top_shadow.tex_id = shadow_corner_tex;
222 top_shadow.type = GL_TRIANGLE_FAN;
223 top_shadow.vertices.resize(4);
224 top_shadow.vertices[0] = {{left, topr, 0.0f}, {1.0f, 0.0f}};218 top_shadow.vertices[0] = {{left, topr, 0.0f}, {1.0f, 0.0f}};
225 top_shadow.vertices[1] = {{right, topr, 0.0f}, {1.0f, 0.0f}};219 top_shadow.vertices[1] = {{right, topr, 0.0f}, {1.0f, 0.0f}};
226 top_shadow.vertices[2] = {{right, top, 0.0f}, {0.0f, 0.0f}};220 top_shadow.vertices[2] = {{right, top, 0.0f}, {0.0f, 0.0f}};
@@ -228,8 +222,6 @@
228222
229 auto& bottom_shadow = primitives[n++];223 auto& bottom_shadow = primitives[n++];
230 bottom_shadow.tex_id = shadow_corner_tex;224 bottom_shadow.tex_id = shadow_corner_tex;
231 bottom_shadow.type = GL_TRIANGLE_FAN;
232 bottom_shadow.vertices.resize(4);
233 bottom_shadow.vertices[0] = {{left, bottom, 0.0f}, {0.0f, 0.0f}};225 bottom_shadow.vertices[0] = {{left, bottom, 0.0f}, {0.0f, 0.0f}};
234 bottom_shadow.vertices[1] = {{right, bottom, 0.0f}, {0.0f, 0.0f}};226 bottom_shadow.vertices[1] = {{right, bottom, 0.0f}, {0.0f, 0.0f}};
235 bottom_shadow.vertices[2] = {{right, bottomr, 0.0f}, {1.0f, 0.0f}};227 bottom_shadow.vertices[2] = {{right, bottomr, 0.0f}, {1.0f, 0.0f}};
@@ -237,8 +229,6 @@
237229
238 auto& tr_shadow = primitives[n++];230 auto& tr_shadow = primitives[n++];
239 tr_shadow.tex_id = shadow_corner_tex;231 tr_shadow.tex_id = shadow_corner_tex;
240 tr_shadow.type = GL_TRIANGLE_FAN;
241 tr_shadow.vertices.resize(4);
242 tr_shadow.vertices[0] = {{right, top, 0.0f}, {0.0f, 0.0f}};232 tr_shadow.vertices[0] = {{right, top, 0.0f}, {0.0f, 0.0f}};
243 tr_shadow.vertices[1] = {{right, topr, 0.0f}, {1.0f, 0.0f}};233 tr_shadow.vertices[1] = {{right, topr, 0.0f}, {1.0f, 0.0f}};
244 tr_shadow.vertices[2] = {{rightr, topr, 0.0f}, {1.0f, 1.0f}};234 tr_shadow.vertices[2] = {{rightr, topr, 0.0f}, {1.0f, 1.0f}};
@@ -246,8 +236,6 @@
246236
247 auto& br_shadow = primitives[n++];237 auto& br_shadow = primitives[n++];
248 br_shadow.tex_id = shadow_corner_tex;238 br_shadow.tex_id = shadow_corner_tex;
249 br_shadow.type = GL_TRIANGLE_FAN;
250 br_shadow.vertices.resize(4);
251 br_shadow.vertices[0] = {{right, bottom, 0.0f}, {0.0f, 0.0f}};239 br_shadow.vertices[0] = {{right, bottom, 0.0f}, {0.0f, 0.0f}};
252 br_shadow.vertices[1] = {{rightr, bottom, 0.0f}, {1.0f, 0.0f}};240 br_shadow.vertices[1] = {{rightr, bottom, 0.0f}, {1.0f, 0.0f}};
253 br_shadow.vertices[2] = {{rightr, bottomr, 0.0f}, {1.0f, 1.0f}};241 br_shadow.vertices[2] = {{rightr, bottomr, 0.0f}, {1.0f, 1.0f}};
@@ -255,8 +243,6 @@
255243
256 auto& bl_shadow = primitives[n++];244 auto& bl_shadow = primitives[n++];
257 bl_shadow.tex_id = shadow_corner_tex;245 bl_shadow.tex_id = shadow_corner_tex;
258 bl_shadow.type = GL_TRIANGLE_FAN;
259 bl_shadow.vertices.resize(4);
260 bl_shadow.vertices[0] = {{left, bottom, 0.0f}, {0.0f, 0.0f}};246 bl_shadow.vertices[0] = {{left, bottom, 0.0f}, {0.0f, 0.0f}};
261 bl_shadow.vertices[1] = {{left, bottomr, 0.0f}, {1.0f, 0.0f}};247 bl_shadow.vertices[1] = {{left, bottomr, 0.0f}, {1.0f, 0.0f}};
262 bl_shadow.vertices[2] = {{leftr, bottomr, 0.0f}, {1.0f, 1.0f}};248 bl_shadow.vertices[2] = {{leftr, bottomr, 0.0f}, {1.0f, 1.0f}};
@@ -264,8 +250,6 @@
264250
265 auto& tl_shadow = primitives[n++];251 auto& tl_shadow = primitives[n++];
266 tl_shadow.tex_id = shadow_corner_tex;252 tl_shadow.tex_id = shadow_corner_tex;
267 tl_shadow.type = GL_TRIANGLE_FAN;
268 tl_shadow.vertices.resize(4);
269 tl_shadow.vertices[0] = {{left, top, 0.0f}, {0.0f, 0.0f}};253 tl_shadow.vertices[0] = {{left, top, 0.0f}, {0.0f, 0.0f}};
270 tl_shadow.vertices[1] = {{leftr, top, 0.0f}, {1.0f, 0.0f}};254 tl_shadow.vertices[1] = {{leftr, top, 0.0f}, {1.0f, 0.0f}};
271 tl_shadow.vertices[2] = {{leftr, topr, 0.0f}, {1.0f, 1.0f}};255 tl_shadow.vertices[2] = {{leftr, topr, 0.0f}, {1.0f, 1.0f}};
@@ -299,8 +283,6 @@
299283
300 auto& top_left_corner = primitives[n++];284 auto& top_left_corner = primitives[n++];
301 top_left_corner.tex_id = titlebar_corner_tex;285 top_left_corner.tex_id = titlebar_corner_tex;
302 top_left_corner.type = GL_TRIANGLE_FAN;
303 top_left_corner.vertices.resize(4);
304 top_left_corner.vertices[0] = {{left, htop, 0.0f}, {0.0f, 0.0f}};286 top_left_corner.vertices[0] = {{left, htop, 0.0f}, {0.0f, 0.0f}};
305 top_left_corner.vertices[1] = {{inleft, htop, 0.0f}, {1.0f, 0.0f}};287 top_left_corner.vertices[1] = {{inleft, htop, 0.0f}, {1.0f, 0.0f}};
306 top_left_corner.vertices[2] = {{inleft, top, 0.0f}, {1.0f, 1.0f}};288 top_left_corner.vertices[2] = {{inleft, top, 0.0f}, {1.0f, 1.0f}};
@@ -308,8 +290,6 @@
308290
309 auto& top_right_corner = primitives[n++];291 auto& top_right_corner = primitives[n++];
310 top_right_corner.tex_id = titlebar_corner_tex;292 top_right_corner.tex_id = titlebar_corner_tex;
311 top_right_corner.type = GL_TRIANGLE_FAN;
312 top_right_corner.vertices.resize(4);
313 top_right_corner.vertices[0] = {{inright, htop, 0.0f}, {1.0f, 0.0f}};293 top_right_corner.vertices[0] = {{inright, htop, 0.0f}, {1.0f, 0.0f}};
314 top_right_corner.vertices[1] = {{right, htop, 0.0f}, {0.0f, 0.0f}};294 top_right_corner.vertices[1] = {{right, htop, 0.0f}, {0.0f, 0.0f}};
315 top_right_corner.vertices[2] = {{right, top, 0.0f}, {0.0f, 1.0f}};295 top_right_corner.vertices[2] = {{right, top, 0.0f}, {0.0f, 1.0f}};
@@ -317,8 +297,6 @@
317297
318 auto& titlebar = primitives[n++];298 auto& titlebar = primitives[n++];
319 titlebar.tex_id = titlebar_corner_tex;299 titlebar.tex_id = titlebar_corner_tex;
320 titlebar.type = GL_TRIANGLE_FAN;
321 titlebar.vertices.resize(4);
322 titlebar.vertices[0] = {{inleft, htop, 0.0f}, {1.0f, 0.0f}};300 titlebar.vertices[0] = {{inleft, htop, 0.0f}, {1.0f, 0.0f}};
323 titlebar.vertices[1] = {{inright, htop, 0.0f}, {1.0f, 0.0f}};301 titlebar.vertices[1] = {{inright, htop, 0.0f}, {1.0f, 0.0f}};
324 titlebar.vertices[2] = {{inright, top, 0.0f}, {1.0f, 1.0f}};302 titlebar.vertices[2] = {{inright, top, 0.0f}, {1.0f, 1.0f}};
325303
=== modified file 'include/platform/mir/graphics/gl_primitive.h'
--- include/platform/mir/graphics/gl_primitive.h 2014-05-20 10:31:58 +0000
+++ include/platform/mir/graphics/gl_primitive.h 2014-07-29 09:00:23 +0000
@@ -21,7 +21,6 @@
21#define MIR_GRAPHICS_GL_PRIMITIVE_H_21#define MIR_GRAPHICS_GL_PRIMITIVE_H_
2222
23#include <GLES2/gl2.h>23#include <GLES2/gl2.h>
24#include <vector>
2524
26namespace mir25namespace mir
27{26{
@@ -35,9 +34,18 @@
3534
36struct GLPrimitive35struct GLPrimitive
37{36{
37 enum {max_vertices = 4};
38
39 GLPrimitive()
40 : type(GL_TRIANGLE_FAN), nvertices(4)
41 {
42 // Default is a quad. Just need to assign vertices[] and tex_id.
43 }
44
38 GLenum type; // GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_TRIANGLES etc45 GLenum type; // GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_TRIANGLES etc
39 GLuint tex_id; // GL texture ID (or 0 to represent the surface itself)46 GLuint tex_id; // GL texture ID (or 0 to represent the surface itself)
40 std::vector<GLVertex> vertices;47 int nvertices;
48 GLVertex vertices[max_vertices];
41};49};
42}50}
43}51}
4452
=== modified file 'include/server/mir/compositor/gl_renderer.h'
--- include/server/mir/compositor/gl_renderer.h 2014-06-06 10:03:54 +0000
+++ include/server/mir/compositor/gl_renderer.h 2014-07-29 09:00:23 +0000
@@ -93,6 +93,8 @@
93 float rotation;93 float rotation;
94 DestinationAlpha const dest_alpha;94 DestinationAlpha const dest_alpha;
95 geometry::Rectangle viewport;95 geometry::Rectangle viewport;
96
97 std::vector<graphics::GLPrimitive> mutable primitives;
96};98};
9799
98}100}
99101
=== modified file 'server-ABI-sha1sums'
--- server-ABI-sha1sums 2014-07-25 18:52:52 +0000
+++ server-ABI-sha1sums 2014-07-29 09:00:23 +0000
@@ -19,7 +19,7 @@
194fcf34e424128b87ddc76733594e32e09ebbd486 include/server/mir/compositor/scene.h194fcf34e424128b87ddc76733594e32e09ebbd486 include/server/mir/compositor/scene.h
20561e7fdc22c48cf39a19e6f7c2db5e8c1feee772 include/server/mir/compositor/frame_dropping_policy_factory.h20561e7fdc22c48cf39a19e6f7c2db5e8c1feee772 include/server/mir/compositor/frame_dropping_policy_factory.h
21eebcdfaf8894b387d1a5acd22c2f1bc09b3c2b95 include/server/mir/compositor/destination_alpha.h21eebcdfaf8894b387d1a5acd22c2f1bc09b3c2b95 include/server/mir/compositor/destination_alpha.h
22cd16e1e8cf0c56a986cd000a78e3bd2447461b21 include/server/mir/compositor/gl_renderer.h22e2e3e914a331a7611032860e3c64274aa42339aa include/server/mir/compositor/gl_renderer.h
23686a839fd0cd5fa469344dc49190ff9d578efc25 include/server/mir/compositor/scene_element.h23686a839fd0cd5fa469344dc49190ff9d578efc25 include/server/mir/compositor/scene_element.h
2498fe6bde01fb8b25b1bacafcdf572f24c3ade3c3 include/server/mir/compositor/display_buffer_compositor_factory.h2498fe6bde01fb8b25b1bacafcdf572f24c3ade3c3 include/server/mir/compositor/display_buffer_compositor_factory.h
25c7328ef5ea4531c0fa981b3e15a69ea5c8162d09 include/server/mir/compositor/display_buffer_compositor.h25c7328ef5ea4531c0fa981b3e15a69ea5c8162d09 include/server/mir/compositor/display_buffer_compositor.h
@@ -182,7 +182,7 @@
18215f201741a465de33e55ffc1ea775b507a5be950 include/platform/mir/graphics/renderable.h18215f201741a465de33e55ffc1ea775b507a5be950 include/platform/mir/graphics/renderable.h
183748f5c2aab11189aa02c4a89ab900561548e7304 include/platform/mir/graphics/buffer_properties.h183748f5c2aab11189aa02c4a89ab900561548e7304 include/platform/mir/graphics/buffer_properties.h
184fc9b3cfa80a1ac57dedb4e8d180c62ae15c0ace8 include/platform/mir/graphics/drm_authenticator.h184fc9b3cfa80a1ac57dedb4e8d180c62ae15c0ace8 include/platform/mir/graphics/drm_authenticator.h
18569a0f4d11e14ef67d0cdcca3a243b4ba4a918360 include/platform/mir/graphics/gl_primitive.h185b8ceb6c770340f63e0114496d4ad102d787fa6f5 include/platform/mir/graphics/gl_primitive.h
186f62525771ca2612959ff88498883630458513235 include/platform/mir/graphics/gl_program.h186f62525771ca2612959ff88498883630458513235 include/platform/mir/graphics/gl_program.h
187871e609c0fed0d566ddbaaa8ac2d7cd5c06dd09a include/platform/mir/abnormal_exit.h187871e609c0fed0d566ddbaaa8ac2d7cd5c06dd09a include/platform/mir/abnormal_exit.h
1889f646fa12eaca5fc3b7a8fde6208673c071c8ef3 include/shared/mir/udev/wrapper.h1889f646fa12eaca5fc3b7a8fde6208673c071c8ef3 include/shared/mir/udev/wrapper.h
189189
=== modified file 'src/platform/graphics/android/hwc_fallback_gl_renderer.cpp'
--- src/platform/graphics/android/hwc_fallback_gl_renderer.cpp 2014-06-18 16:40:15 +0000
+++ src/platform/graphics/android/hwc_fallback_gl_renderer.cpp 2014-07-29 09:00:23 +0000
@@ -124,7 +124,7 @@
124 &primitive.vertices[0].texcoord);124 &primitive.vertices[0].texcoord);
125125
126 texture_cache->load(*renderable)->bind();126 texture_cache->load(*renderable)->bind();
127 glDrawArrays(primitive.type, 0, primitive.vertices.size());127 glDrawArrays(primitive.type, 0, primitive.nvertices);
128 }128 }
129129
130 glDisableVertexAttribArray(texcoord_attr);130 glDisableVertexAttribArray(texcoord_attr);
131131
=== modified file 'src/platform/graphics/tessellation_helpers.cpp'
--- src/platform/graphics/tessellation_helpers.cpp 2014-05-20 10:31:58 +0000
+++ src/platform/graphics/tessellation_helpers.cpp 2014-07-29 09:00:23 +0000
@@ -40,7 +40,6 @@
40 buf_size.height.as_int();40 buf_size.height.as_int();
4141
42 auto& vertices = rectangle.vertices;42 auto& vertices = rectangle.vertices;
43 vertices.resize(4);
44 vertices[0] = {{left, top, 0.0f}, {0.0f, 0.0f}};43 vertices[0] = {{left, top, 0.0f}, {0.0f, 0.0f}};
45 vertices[1] = {{left, bottom, 0.0f}, {0.0f, tex_bottom}};44 vertices[1] = {{left, bottom, 0.0f}, {0.0f, tex_bottom}};
46 vertices[2] = {{right, top, 0.0f}, {tex_right, 0.0f}};45 vertices[2] = {{right, top, 0.0f}, {tex_right, 0.0f}};
4746
=== modified file 'src/server/compositor/gl_renderer.cpp'
--- src/server/compositor/gl_renderer.cpp 2014-06-18 12:50:31 +0000
+++ src/server/compositor/gl_renderer.cpp 2014-07-29 09:00:23 +0000
@@ -148,7 +148,7 @@
148 glEnableVertexAttribArray(position_attr_loc);148 glEnableVertexAttribArray(position_attr_loc);
149 glEnableVertexAttribArray(texcoord_attr_loc);149 glEnableVertexAttribArray(texcoord_attr_loc);
150150
151 std::vector<mg::GLPrimitive> primitives;151 primitives.clear();
152 tessellate(primitives, renderable);152 tessellate(primitives, renderable);
153153
154 auto surface_tex = texture_cache->load(renderable);154 auto surface_tex = texture_cache->load(renderable);
@@ -170,7 +170,7 @@
170 GL_FALSE, sizeof(mg::GLVertex),170 GL_FALSE, sizeof(mg::GLVertex),
171 &p.vertices[0].texcoord);171 &p.vertices[0].texcoord);
172172
173 glDrawArrays(p.type, 0, p.vertices.size());173 glDrawArrays(p.type, 0, p.nvertices);
174 }174 }
175175
176 glDisableVertexAttribArray(texcoord_attr_loc);176 glDisableVertexAttribArray(texcoord_attr_loc);
177177
=== modified file 'tests/unit-tests/compositor/test_gl_renderer.cpp'
--- tests/unit-tests/compositor/test_gl_renderer.cpp 2014-06-18 12:50:31 +0000
+++ tests/unit-tests/compositor/test_gl_renderer.cpp 2014-07-29 09:00:23 +0000
@@ -246,9 +246,14 @@
246 void tessellate(std::vector<mg::GLPrimitive>& primitives,246 void tessellate(std::vector<mg::GLPrimitive>& primitives,
247 mg::Renderable const&) const override247 mg::Renderable const&) const override
248 {248 {
249 primitives.clear();249 primitives.resize(num_primitives);
250 for(GLuint i=0; i < num_primitives; i++)250 for(GLuint i=0; i < num_primitives; i++)
251 primitives.push_back({0,i%2,{}});251 {
252 auto& p = primitives[i];
253 p.type = 0;
254 p.tex_id = i % 2;
255 p.nvertices = 0;
256 }
252 }257 }
253 unsigned int num_primitives; 258 unsigned int num_primitives;
254 };259 };

Subscribers

People subscribed via source and target branches