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
1=== modified file 'examples/demo-shell/demo_renderer.cpp'
2--- examples/demo-shell/demo_renderer.cpp 2014-06-18 12:50:31 +0000
3+++ examples/demo-shell/demo_renderer.cpp 2014-07-29 09:00:23 +0000
4@@ -201,8 +201,6 @@
5
6 auto& right_shadow = primitives[n++];
7 right_shadow.tex_id = shadow_corner_tex;
8- right_shadow.type = GL_TRIANGLE_FAN;
9- right_shadow.vertices.resize(4);
10 right_shadow.vertices[0] = {{right, top, 0.0f}, {0.0f, 0.0f}};
11 right_shadow.vertices[1] = {{rightr, top, 0.0f}, {1.0f, 0.0f}};
12 right_shadow.vertices[2] = {{rightr, bottom, 0.0f}, {1.0f, 0.0f}};
13@@ -210,8 +208,6 @@
14
15 auto& left_shadow = primitives[n++];
16 left_shadow.tex_id = shadow_corner_tex;
17- left_shadow.type = GL_TRIANGLE_FAN;
18- left_shadow.vertices.resize(4);
19 left_shadow.vertices[0] = {{leftr, top, 0.0f}, {1.0f, 0.0f}};
20 left_shadow.vertices[1] = {{left, top, 0.0f}, {0.0f, 0.0f}};
21 left_shadow.vertices[2] = {{left, bottom, 0.0f}, {0.0f, 0.0f}};
22@@ -219,8 +215,6 @@
23
24 auto& top_shadow = primitives[n++];
25 top_shadow.tex_id = shadow_corner_tex;
26- top_shadow.type = GL_TRIANGLE_FAN;
27- top_shadow.vertices.resize(4);
28 top_shadow.vertices[0] = {{left, topr, 0.0f}, {1.0f, 0.0f}};
29 top_shadow.vertices[1] = {{right, topr, 0.0f}, {1.0f, 0.0f}};
30 top_shadow.vertices[2] = {{right, top, 0.0f}, {0.0f, 0.0f}};
31@@ -228,8 +222,6 @@
32
33 auto& bottom_shadow = primitives[n++];
34 bottom_shadow.tex_id = shadow_corner_tex;
35- bottom_shadow.type = GL_TRIANGLE_FAN;
36- bottom_shadow.vertices.resize(4);
37 bottom_shadow.vertices[0] = {{left, bottom, 0.0f}, {0.0f, 0.0f}};
38 bottom_shadow.vertices[1] = {{right, bottom, 0.0f}, {0.0f, 0.0f}};
39 bottom_shadow.vertices[2] = {{right, bottomr, 0.0f}, {1.0f, 0.0f}};
40@@ -237,8 +229,6 @@
41
42 auto& tr_shadow = primitives[n++];
43 tr_shadow.tex_id = shadow_corner_tex;
44- tr_shadow.type = GL_TRIANGLE_FAN;
45- tr_shadow.vertices.resize(4);
46 tr_shadow.vertices[0] = {{right, top, 0.0f}, {0.0f, 0.0f}};
47 tr_shadow.vertices[1] = {{right, topr, 0.0f}, {1.0f, 0.0f}};
48 tr_shadow.vertices[2] = {{rightr, topr, 0.0f}, {1.0f, 1.0f}};
49@@ -246,8 +236,6 @@
50
51 auto& br_shadow = primitives[n++];
52 br_shadow.tex_id = shadow_corner_tex;
53- br_shadow.type = GL_TRIANGLE_FAN;
54- br_shadow.vertices.resize(4);
55 br_shadow.vertices[0] = {{right, bottom, 0.0f}, {0.0f, 0.0f}};
56 br_shadow.vertices[1] = {{rightr, bottom, 0.0f}, {1.0f, 0.0f}};
57 br_shadow.vertices[2] = {{rightr, bottomr, 0.0f}, {1.0f, 1.0f}};
58@@ -255,8 +243,6 @@
59
60 auto& bl_shadow = primitives[n++];
61 bl_shadow.tex_id = shadow_corner_tex;
62- bl_shadow.type = GL_TRIANGLE_FAN;
63- bl_shadow.vertices.resize(4);
64 bl_shadow.vertices[0] = {{left, bottom, 0.0f}, {0.0f, 0.0f}};
65 bl_shadow.vertices[1] = {{left, bottomr, 0.0f}, {1.0f, 0.0f}};
66 bl_shadow.vertices[2] = {{leftr, bottomr, 0.0f}, {1.0f, 1.0f}};
67@@ -264,8 +250,6 @@
68
69 auto& tl_shadow = primitives[n++];
70 tl_shadow.tex_id = shadow_corner_tex;
71- tl_shadow.type = GL_TRIANGLE_FAN;
72- tl_shadow.vertices.resize(4);
73 tl_shadow.vertices[0] = {{left, top, 0.0f}, {0.0f, 0.0f}};
74 tl_shadow.vertices[1] = {{leftr, top, 0.0f}, {1.0f, 0.0f}};
75 tl_shadow.vertices[2] = {{leftr, topr, 0.0f}, {1.0f, 1.0f}};
76@@ -299,8 +283,6 @@
77
78 auto& top_left_corner = primitives[n++];
79 top_left_corner.tex_id = titlebar_corner_tex;
80- top_left_corner.type = GL_TRIANGLE_FAN;
81- top_left_corner.vertices.resize(4);
82 top_left_corner.vertices[0] = {{left, htop, 0.0f}, {0.0f, 0.0f}};
83 top_left_corner.vertices[1] = {{inleft, htop, 0.0f}, {1.0f, 0.0f}};
84 top_left_corner.vertices[2] = {{inleft, top, 0.0f}, {1.0f, 1.0f}};
85@@ -308,8 +290,6 @@
86
87 auto& top_right_corner = primitives[n++];
88 top_right_corner.tex_id = titlebar_corner_tex;
89- top_right_corner.type = GL_TRIANGLE_FAN;
90- top_right_corner.vertices.resize(4);
91 top_right_corner.vertices[0] = {{inright, htop, 0.0f}, {1.0f, 0.0f}};
92 top_right_corner.vertices[1] = {{right, htop, 0.0f}, {0.0f, 0.0f}};
93 top_right_corner.vertices[2] = {{right, top, 0.0f}, {0.0f, 1.0f}};
94@@ -317,8 +297,6 @@
95
96 auto& titlebar = primitives[n++];
97 titlebar.tex_id = titlebar_corner_tex;
98- titlebar.type = GL_TRIANGLE_FAN;
99- titlebar.vertices.resize(4);
100 titlebar.vertices[0] = {{inleft, htop, 0.0f}, {1.0f, 0.0f}};
101 titlebar.vertices[1] = {{inright, htop, 0.0f}, {1.0f, 0.0f}};
102 titlebar.vertices[2] = {{inright, top, 0.0f}, {1.0f, 1.0f}};
103
104=== modified file 'include/platform/mir/graphics/gl_primitive.h'
105--- include/platform/mir/graphics/gl_primitive.h 2014-05-20 10:31:58 +0000
106+++ include/platform/mir/graphics/gl_primitive.h 2014-07-29 09:00:23 +0000
107@@ -21,7 +21,6 @@
108 #define MIR_GRAPHICS_GL_PRIMITIVE_H_
109
110 #include <GLES2/gl2.h>
111-#include <vector>
112
113 namespace mir
114 {
115@@ -35,9 +34,18 @@
116
117 struct GLPrimitive
118 {
119+ enum {max_vertices = 4};
120+
121+ GLPrimitive()
122+ : type(GL_TRIANGLE_FAN), nvertices(4)
123+ {
124+ // Default is a quad. Just need to assign vertices[] and tex_id.
125+ }
126+
127 GLenum type; // GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_TRIANGLES etc
128 GLuint tex_id; // GL texture ID (or 0 to represent the surface itself)
129- std::vector<GLVertex> vertices;
130+ int nvertices;
131+ GLVertex vertices[max_vertices];
132 };
133 }
134 }
135
136=== modified file 'include/server/mir/compositor/gl_renderer.h'
137--- include/server/mir/compositor/gl_renderer.h 2014-06-06 10:03:54 +0000
138+++ include/server/mir/compositor/gl_renderer.h 2014-07-29 09:00:23 +0000
139@@ -93,6 +93,8 @@
140 float rotation;
141 DestinationAlpha const dest_alpha;
142 geometry::Rectangle viewport;
143+
144+ std::vector<graphics::GLPrimitive> mutable primitives;
145 };
146
147 }
148
149=== modified file 'server-ABI-sha1sums'
150--- server-ABI-sha1sums 2014-07-25 18:52:52 +0000
151+++ server-ABI-sha1sums 2014-07-29 09:00:23 +0000
152@@ -19,7 +19,7 @@
153 4fcf34e424128b87ddc76733594e32e09ebbd486 include/server/mir/compositor/scene.h
154 561e7fdc22c48cf39a19e6f7c2db5e8c1feee772 include/server/mir/compositor/frame_dropping_policy_factory.h
155 eebcdfaf8894b387d1a5acd22c2f1bc09b3c2b95 include/server/mir/compositor/destination_alpha.h
156-cd16e1e8cf0c56a986cd000a78e3bd2447461b21 include/server/mir/compositor/gl_renderer.h
157+e2e3e914a331a7611032860e3c64274aa42339aa include/server/mir/compositor/gl_renderer.h
158 686a839fd0cd5fa469344dc49190ff9d578efc25 include/server/mir/compositor/scene_element.h
159 98fe6bde01fb8b25b1bacafcdf572f24c3ade3c3 include/server/mir/compositor/display_buffer_compositor_factory.h
160 c7328ef5ea4531c0fa981b3e15a69ea5c8162d09 include/server/mir/compositor/display_buffer_compositor.h
161@@ -182,7 +182,7 @@
162 15f201741a465de33e55ffc1ea775b507a5be950 include/platform/mir/graphics/renderable.h
163 748f5c2aab11189aa02c4a89ab900561548e7304 include/platform/mir/graphics/buffer_properties.h
164 fc9b3cfa80a1ac57dedb4e8d180c62ae15c0ace8 include/platform/mir/graphics/drm_authenticator.h
165-69a0f4d11e14ef67d0cdcca3a243b4ba4a918360 include/platform/mir/graphics/gl_primitive.h
166+b8ceb6c770340f63e0114496d4ad102d787fa6f5 include/platform/mir/graphics/gl_primitive.h
167 f62525771ca2612959ff88498883630458513235 include/platform/mir/graphics/gl_program.h
168 871e609c0fed0d566ddbaaa8ac2d7cd5c06dd09a include/platform/mir/abnormal_exit.h
169 9f646fa12eaca5fc3b7a8fde6208673c071c8ef3 include/shared/mir/udev/wrapper.h
170
171=== modified file 'src/platform/graphics/android/hwc_fallback_gl_renderer.cpp'
172--- src/platform/graphics/android/hwc_fallback_gl_renderer.cpp 2014-06-18 16:40:15 +0000
173+++ src/platform/graphics/android/hwc_fallback_gl_renderer.cpp 2014-07-29 09:00:23 +0000
174@@ -124,7 +124,7 @@
175 &primitive.vertices[0].texcoord);
176
177 texture_cache->load(*renderable)->bind();
178- glDrawArrays(primitive.type, 0, primitive.vertices.size());
179+ glDrawArrays(primitive.type, 0, primitive.nvertices);
180 }
181
182 glDisableVertexAttribArray(texcoord_attr);
183
184=== modified file 'src/platform/graphics/tessellation_helpers.cpp'
185--- src/platform/graphics/tessellation_helpers.cpp 2014-05-20 10:31:58 +0000
186+++ src/platform/graphics/tessellation_helpers.cpp 2014-07-29 09:00:23 +0000
187@@ -40,7 +40,6 @@
188 buf_size.height.as_int();
189
190 auto& vertices = rectangle.vertices;
191- vertices.resize(4);
192 vertices[0] = {{left, top, 0.0f}, {0.0f, 0.0f}};
193 vertices[1] = {{left, bottom, 0.0f}, {0.0f, tex_bottom}};
194 vertices[2] = {{right, top, 0.0f}, {tex_right, 0.0f}};
195
196=== modified file 'src/server/compositor/gl_renderer.cpp'
197--- src/server/compositor/gl_renderer.cpp 2014-06-18 12:50:31 +0000
198+++ src/server/compositor/gl_renderer.cpp 2014-07-29 09:00:23 +0000
199@@ -148,7 +148,7 @@
200 glEnableVertexAttribArray(position_attr_loc);
201 glEnableVertexAttribArray(texcoord_attr_loc);
202
203- std::vector<mg::GLPrimitive> primitives;
204+ primitives.clear();
205 tessellate(primitives, renderable);
206
207 auto surface_tex = texture_cache->load(renderable);
208@@ -170,7 +170,7 @@
209 GL_FALSE, sizeof(mg::GLVertex),
210 &p.vertices[0].texcoord);
211
212- glDrawArrays(p.type, 0, p.vertices.size());
213+ glDrawArrays(p.type, 0, p.nvertices);
214 }
215
216 glDisableVertexAttribArray(texcoord_attr_loc);
217
218=== modified file 'tests/unit-tests/compositor/test_gl_renderer.cpp'
219--- tests/unit-tests/compositor/test_gl_renderer.cpp 2014-06-18 12:50:31 +0000
220+++ tests/unit-tests/compositor/test_gl_renderer.cpp 2014-07-29 09:00:23 +0000
221@@ -246,9 +246,14 @@
222 void tessellate(std::vector<mg::GLPrimitive>& primitives,
223 mg::Renderable const&) const override
224 {
225- primitives.clear();
226+ primitives.resize(num_primitives);
227 for(GLuint i=0; i < num_primitives; i++)
228- primitives.push_back({0,i%2,{}});
229+ {
230+ auto& p = primitives[i];
231+ p.type = 0;
232+ p.tex_id = i % 2;
233+ p.nvertices = 0;
234+ }
235 }
236 unsigned int num_primitives;
237 };

Subscribers

People subscribed via source and target branches