Mir

Merge lp:~alan-griffiths/mir/resize-examples into lp:mir

Proposed by Alan Griffiths on 2017-06-06
Status: Merged
Approved by: Alberto Aguirre on 2017-06-06
Approved revision: 4193
Merged at revision: 4188
Proposed branch: lp:~alan-griffiths/mir/resize-examples
Merge into: lp:mir
Prerequisite: lp:~alan-griffiths/mir/mir_window_get_connection
Diff against target: 514 lines (+222/-124)
10 files modified
examples/client_helpers.cpp (+38/-1)
examples/client_helpers.h (+1/-0)
examples/client_touch_validator.cpp (+4/-1)
examples/eglapp.c (+18/-16)
examples/eglapp.h (+1/-0)
examples/eglsquare.cpp (+120/-100)
examples/multi_stream.cpp (+6/-6)
examples/prerendered_frames.c (+32/-0)
examples/target.c (+1/-0)
src/utils/vanity.c (+1/-0)
To merge this branch: bzr merge lp:~alan-griffiths/mir/resize-examples
Reviewer Review Type Date Requested Status
Mir CI Bot continuous-integration Approve on 2017-06-06
Alberto Aguirre (community) 2017-06-06 Approve on 2017-06-06
Review via email: mp+325170@code.launchpad.net

Commit message

Fix window resize logic in example code

To post a comment you must log in.
Alberto Aguirre (albaguirre) wrote :

LGTM.

review: Approve
Alberto Aguirre (albaguirre) wrote :

It looks like this would be a pretty common thing a client needs to do:

    mir_render_surface_set_size(surface, new_width, new_height);
    MirWindowSpec* spec = mir_create_window_spec(mir_window_get_connection(window));
    mir_window_spec_add_render_surface(spec, surface, new_width, new_height, 0, 0);
    mir_window_apply_spec(window, spec);
    mir_window_spec_release(spec);

Maybe a convenience api should be introduced? maybe mir_window_resize_content(MirWindow* window, MirRenderSurface* surface, int new_width, int new_height) ? Just a thought...

Mir CI Bot (mir-ci-bot) wrote :

PASSED: Continuous integration, rev:4193
https://mir-jenkins.ubuntu.com/job/mir-ci/3444/
Executed test runs:
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-mir/4690
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-0-fetch/4825
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-1-sourcepkg/release=artful/4815
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-1-sourcepkg/release=xenial/4815
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-1-sourcepkg/release=zesty/4815
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=clang,platform=mesa,release=artful/4727
        deb: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=clang,platform=mesa,release=artful/4727/artifact/output/*zip*/output.zip
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=clang,platform=mesa,release=zesty/4727
        deb: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=clang,platform=mesa,release=zesty/4727/artifact/output/*zip*/output.zip
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=gcc,platform=mesa,release=artful/4727
        deb: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=gcc,platform=mesa,release=artful/4727/artifact/output/*zip*/output.zip
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=gcc,platform=mesa,release=xenial/4727
        deb: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=gcc,platform=mesa,release=xenial/4727/artifact/output/*zip*/output.zip
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=gcc,platform=mesa,release=zesty/4727
        deb: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=gcc,platform=mesa,release=zesty/4727/artifact/output/*zip*/output.zip
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=cross-armhf,compiler=gcc,platform=mesa,release=artful/4727
        deb: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=cross-armhf,compiler=gcc,platform=mesa,release=artful/4727/artifact/output/*zip*/output.zip
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=cross-armhf,compiler=gcc,platform=mesa,release=zesty/4727
        deb: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=cross-armhf,compiler=gcc,platform=mesa,release=zesty/4727/artifact/output/*zip*/output.zip
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=i386,compiler=gcc,platform=mesa,release=xenial/4727
        deb: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=i386,compiler=gcc,platform=mesa,release=xenial/4727/artifact/output/*zip*/output.zip

Click here to trigger a rebuild:
https://mir-jenkins.ubuntu.com/job/mir-ci/3444/rebuild

review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'examples/client_helpers.cpp'
2--- examples/client_helpers.cpp 2017-05-23 10:51:47 +0000
3+++ examples/client_helpers.cpp 2017-06-06 15:58:12 +0000
4@@ -51,8 +51,14 @@
5 }
6
7 mir::examples::NormalWindow::NormalWindow(
8+ Connection& connection, unsigned int width, unsigned int height, MirRenderSurface* surface) :
9+ window{create_window(connection, width, height, surface), &mir_window_release_sync}
10+{
11+}
12+
13+mir::examples::NormalWindow::NormalWindow(
14 mir::examples::Connection& connection, unsigned int width, unsigned int height, mir::examples::Context& eglcontext) :
15- window{create_window(connection, width, height, eglcontext.mir_surface()), &mir_window_release_sync}
16+ NormalWindow(connection, width, height, eglcontext.mir_surface())
17 {
18 }
19
20@@ -61,6 +67,34 @@
21 return window.get();
22 }
23
24+namespace
25+{
26+void handle_window_event(MirWindow* window, MirEvent const* event, void* context)
27+{
28+ auto const surface = (MirRenderSurface*)context;
29+
30+ switch (mir_event_get_type(event))
31+ {
32+ case mir_event_type_resize:
33+ {
34+ MirResizeEvent const* resize = mir_event_get_resize_event(event);
35+ int const new_width = mir_resize_event_get_width(resize);
36+ int const new_height = mir_resize_event_get_height(resize);
37+
38+ mir_render_surface_set_size(surface, new_width, new_height);
39+ MirWindowSpec* spec = mir_create_window_spec(mir_window_get_connection(window));
40+ mir_window_spec_add_render_surface(spec, surface, new_width, new_height, 0, 0);
41+ mir_window_apply_spec(window, spec);
42+ mir_window_spec_release(spec);
43+ break;
44+ }
45+
46+ default:
47+ break;
48+ }
49+}
50+}
51+
52 MirWindow* me::NormalWindow::create_window(
53 MirConnection* connection, unsigned int width, unsigned int height, MirRenderSurface* surface)
54 {
55@@ -72,7 +106,10 @@
56 mir_window_spec_set_name(spec.get(), __PRETTY_FUNCTION__);
57
58 if (surface)
59+ {
60 mir_window_spec_add_render_surface(spec.get(), surface, width, height, 0, 0);
61+ mir_window_spec_set_event_handler(spec.get(), &handle_window_event, surface);
62+ }
63
64 auto window = mir_create_window_sync(spec.get());
65 return window;
66
67=== modified file 'examples/client_helpers.h'
68--- examples/client_helpers.h 2017-05-26 13:52:51 +0000
69+++ examples/client_helpers.h 2017-06-06 15:58:12 +0000
70@@ -86,6 +86,7 @@
71 public:
72 NormalWindow(Connection& connection, unsigned int width, unsigned int height);
73 NormalWindow(Connection& connection, unsigned int width, unsigned int height, Context& eglcontext);
74+ NormalWindow(Connection& connection, unsigned int width, unsigned int height, MirRenderSurface* surface);
75
76 operator MirWindow*() const;
77 private:
78
79=== modified file 'examples/client_touch_validator.cpp'
80--- examples/client_touch_validator.cpp 2017-05-08 03:04:26 +0000
81+++ examples/client_touch_validator.cpp 2017-06-06 15:58:12 +0000
82@@ -134,7 +134,7 @@
83 state->record_event(tev);
84 }
85
86-void on_event(MirWindow * /*surface*/, const MirEvent *event, void *context)
87+void on_event(MirWindow *window, const MirEvent *event, void *context)
88 {
89 TouchState *state = (TouchState*)context;
90
91@@ -143,6 +143,9 @@
92 case mir_event_type_input:
93 on_input_event(state, mir_event_get_input_event(event));
94 break;
95+ case mir_event_type_resize:
96+ egl_app_handle_resize_event(window, mir_event_get_resize_event(event));
97+ break;
98 case mir_event_type_close_window:
99 abort();
100 break;
101
102=== modified file 'examples/eglapp.c'
103--- examples/eglapp.c 2017-05-19 16:18:30 +0000
104+++ examples/eglapp.c 2017-06-06 15:58:12 +0000
105@@ -183,13 +183,24 @@
106 * support for event queuing (directing them to another thread) or
107 * full single-threaded callbacks. (LP: #1194384).
108 */
109- {
110- MirResizeEvent const* resize = mir_event_get_resize_event(ev);
111- int const new_width = mir_resize_event_get_width(resize);
112- int const new_height = mir_resize_event_get_height(resize);
113-
114- printf("Resized to %dx%d\n", new_width, new_height);
115- if (surface)
116+ egl_app_handle_resize_event(window, mir_event_get_resize_event(ev));
117+ break;
118+ case mir_event_type_close_window:
119+ printf("Received close event from server.\n");
120+ running = 0;
121+ break;
122+ default:
123+ break;
124+ }
125+}
126+
127+void egl_app_handle_resize_event(MirWindow* window, MirResizeEvent const* resize)
128+{
129+ int const new_width = mir_resize_event_get_width(resize);
130+ int const new_height = mir_resize_event_get_height(resize);
131+
132+ printf("Resized to %dx%d\n", new_width, new_height);
133+ if (surface)
134 {
135 mir_render_surface_set_size(surface, new_width, new_height);
136 MirWindowSpec* spec = mir_create_window_spec(connection);
137@@ -197,15 +208,6 @@
138 mir_window_apply_spec(window, spec);
139 mir_window_spec_release(spec);
140 }
141- }
142- break;
143- case mir_event_type_close_window:
144- printf("Received close event from server.\n");
145- running = 0;
146- break;
147- default:
148- break;
149- }
150 }
151
152 static void show_help(struct mir_eglapp_arg const* const* arg_lists)
153
154=== modified file 'examples/eglapp.h'
155--- examples/eglapp.h 2017-05-08 03:04:26 +0000
156+++ examples/eglapp.h 2017-06-06 15:58:12 +0000
157@@ -45,6 +45,7 @@
158 mir_eglapp_bool mir_eglapp_running(void);
159 void mir_eglapp_cleanup(void);
160 void mir_eglapp_handle_event(MirWindow* window, MirEvent const* ev, void* unused);
161+void egl_app_handle_resize_event(MirWindow* window, MirResizeEvent const* resize);
162 double mir_eglapp_display_hz(void);
163
164 MirConnection* mir_eglapp_native_connection();
165
166=== modified file 'examples/eglsquare.cpp'
167--- examples/eglsquare.cpp 2017-06-05 09:37:52 +0000
168+++ examples/eglsquare.cpp 2017-06-06 15:58:12 +0000
169@@ -127,106 +127,126 @@
170 if (worker.joinable()) worker.join();
171 }
172
173- void on_event(MirEvent const* ev)
174- {
175- if (mir_event_get_type(ev) != mir_event_type_input)
176- return;
177- float x{0.0f};
178- float y{0.0f};
179- auto ievent = mir_event_get_input_event(ev);
180- if (mir_input_event_get_type(ievent) == mir_input_event_type_touch)
181- {
182- auto tev = mir_input_event_get_touch_event(ievent);
183- x = mir_touch_event_axis_value(tev, 0, mir_touch_axis_x);
184- y = mir_touch_event_axis_value(tev, 0, mir_touch_axis_y);
185- }
186- else if (mir_input_event_get_type(ievent) == mir_input_event_type_pointer)
187- {
188- auto pev = mir_input_event_get_pointer_event(ievent);
189- x = mir_pointer_event_axis_value(pev, mir_pointer_axis_x);
190- y = mir_pointer_event_axis_value(pev, mir_pointer_axis_y);
191- }
192- else
193- {
194- return;
195- }
196-
197- pos = Pos{x, y};
198- cv.notify_one();
199- }
200-
201- SquareRenderingSurface(SquareRenderingSurface const&) = delete;
202- SquareRenderingSurface& operator=(SquareRenderingSurface const&) = delete;
203-private:
204- struct OutputDimensions
205- {
206- unsigned int const width;
207- unsigned int const height;
208- } const dimensions;
209-
210- me::Context context;
211- me::NormalWindow window;
212- RenderProgram program;
213-
214- OutputDimensions active_output_dimensions(MirConnection* connection)
215- {
216- unsigned int width{0};
217- unsigned int height{0};
218- auto display_config = mir_connection_create_display_configuration(connection);
219- auto num_outputs = mir_display_config_get_num_outputs(display_config);
220- for (auto i = 0; i < num_outputs; i++)
221- {
222- auto output = mir_display_config_get_output(display_config, i);
223- auto state = mir_output_get_connection_state(output);
224- if (state == mir_output_connection_state_connected && mir_output_is_enabled(output))
225- {
226- auto mode = mir_output_get_current_mode(output);
227- width = mir_output_mode_get_width(mode);
228- height = mir_output_mode_get_height(mode);
229- break;
230- }
231- }
232- mir_display_config_release(display_config);
233- if (width == 0 || height == 0)
234- throw std::logic_error("could not determine display size");
235- return {width, height};
236- }
237-
238- static void on_event(MirWindow*, const MirEvent *event, void *context)
239- {
240- auto surface = reinterpret_cast<SquareRenderingSurface*>(context);
241- if (surface) surface->on_event(event);
242- }
243-
244-private:
245- void do_work()
246- {
247- std::unique_lock<decltype(mutex)> lock(mutex);
248-
249- while (true)
250- {
251- cv.wait(lock);
252-
253- if (!running) return;
254-
255- Pos pos = this->pos;
256-
257- context.make_current();
258- program.draw(
259- pos.x/static_cast<float>(dimensions.width)*2.0 - 1.0,
260- pos.y/static_cast<float>(dimensions.height)*-2.0 + 1.0);
261- context.swapbuffers();
262- }
263- }
264-
265- struct Pos { float x; float y; };
266- std::atomic<Pos> pos;
267-
268- std::thread worker;
269- std::condition_variable cv;
270- std::mutex mutex;
271- bool running{true};
272-};
273+ void on_event(MirEvent const* event)
274+ {
275+ switch (mir_event_get_type(event))
276+ {
277+ case mir_event_type_resize:
278+ {
279+ MirResizeEvent const* resize = mir_event_get_resize_event(event);
280+ int const new_width = mir_resize_event_get_width(resize);
281+ int const new_height = mir_resize_event_get_height(resize);
282+
283+ mir_render_surface_set_size(context.mir_surface(), new_width, new_height);
284+ MirWindowSpec* spec = mir_create_window_spec(mir_window_get_connection(window));
285+ mir_window_spec_add_render_surface(spec, context.mir_surface(), new_width, new_height, 0, 0);
286+ mir_window_apply_spec(window, spec);
287+ mir_window_spec_release(spec);
288+ break;
289+ }
290+
291+ case mir_event_type_input:
292+ {
293+ float x{0.0f};
294+ float y{0.0f};
295+ auto ievent = mir_event_get_input_event(event);
296+ if (mir_input_event_get_type(ievent) == mir_input_event_type_touch)
297+ {
298+ auto tev = mir_input_event_get_touch_event(ievent);
299+ x = mir_touch_event_axis_value(tev, 0, mir_touch_axis_x);
300+ y = mir_touch_event_axis_value(tev, 0, mir_touch_axis_y);
301+ }
302+ else if (mir_input_event_get_type(ievent) == mir_input_event_type_pointer)
303+ {
304+ auto pev = mir_input_event_get_pointer_event(ievent);
305+ x = mir_pointer_event_axis_value(pev, mir_pointer_axis_x);
306+ y = mir_pointer_event_axis_value(pev, mir_pointer_axis_y);
307+ }
308+ else
309+ {
310+ return;
311+ }
312+
313+ pos = Pos{x, y};
314+ cv.notify_one();
315+ }
316+
317+ default:;
318+ }
319+ }
320+
321+ SquareRenderingSurface(SquareRenderingSurface const&) = delete;
322+ SquareRenderingSurface& operator=(SquareRenderingSurface const&) = delete;
323+ private:
324+ struct OutputDimensions
325+ {
326+ unsigned int const width;
327+ unsigned int const height;
328+ } const dimensions;
329+
330+ me::Context context;
331+ me::NormalWindow window;
332+ RenderProgram program;
333+
334+ OutputDimensions active_output_dimensions(MirConnection* connection)
335+ {
336+ unsigned int width{0};
337+ unsigned int height{0};
338+ auto display_config = mir_connection_create_display_configuration(connection);
339+ auto num_outputs = mir_display_config_get_num_outputs(display_config);
340+ for (auto i = 0; i < num_outputs; i++)
341+ {
342+ auto output = mir_display_config_get_output(display_config, i);
343+ auto state = mir_output_get_connection_state(output);
344+ if (state == mir_output_connection_state_connected && mir_output_is_enabled(output))
345+ {
346+ auto mode = mir_output_get_current_mode(output);
347+ width = mir_output_mode_get_width(mode);
348+ height = mir_output_mode_get_height(mode);
349+ break;
350+ }
351+ }
352+ mir_display_config_release(display_config);
353+ if (width == 0 || height == 0)
354+ throw std::logic_error("could not determine display size");
355+ return {width, height};
356+ }
357+
358+ static void on_event(MirWindow*, const MirEvent *event, void *context)
359+ {
360+ auto surface = reinterpret_cast<SquareRenderingSurface*>(context);
361+ if (surface) surface->on_event(event);
362+ }
363+
364+ private:
365+ void do_work()
366+ {
367+ std::unique_lock<decltype(mutex)> lock(mutex);
368+
369+ while (true)
370+ {
371+ cv.wait(lock);
372+
373+ if (!running) return;
374+
375+ Pos pos = this->pos;
376+
377+ context.make_current();
378+ program.draw(
379+ pos.x/static_cast<float>(dimensions.width)*2.0 - 1.0,
380+ pos.y/static_cast<float>(dimensions.height)*-2.0 + 1.0);
381+ context.swapbuffers();
382+ }
383+ }
384+
385+ struct Pos { float x; float y; };
386+ std::atomic<Pos> pos;
387+
388+ std::thread worker;
389+ std::condition_variable cv;
390+ std::mutex mutex;
391+ bool running{true};
392+ };
393 }
394
395 int main(int argc, char *argv[])
396
397=== modified file 'examples/multi_stream.cpp'
398--- examples/multi_stream.cpp 2017-05-23 10:51:47 +0000
399+++ examples/multi_stream.cpp 2017-06-06 15:58:12 +0000
400@@ -49,8 +49,6 @@
401
402 int displacement_x{0};
403 int displacement_y{0};
404- int width{0};
405- int height{0};
406
407 MyBufferStream& operator=(MyBufferStream &&) = default;
408 MyBufferStream(MyBufferStream &&) = default;
409@@ -278,7 +276,7 @@
410 0
411 };
412
413- me::NormalWindow window{connection, 200, 200};
414+ me::NormalWindow window{connection, 200, 200, stream[0]};
415
416 while (poll(&signal_poll, 1, 0) <= 0)
417 {
418@@ -302,8 +300,12 @@
419 auto spec = mir_create_window_spec(connection);
420 for (auto& s : stream)
421 {
422+ int width{0};
423+ int height{0};
424+ mir_render_surface_get_size(s, &width, &height);
425+
426 mir_buffer_stream_swap_buffers_sync(s);
427- mir_window_spec_add_render_surface(spec, s, s.width, s.height, s.displacement_x, s.displacement_y);
428+ mir_window_spec_add_render_surface(spec, s, width, height, s.displacement_x, s.displacement_y);
429 }
430 mir_window_apply_spec(window, spec);
431 mir_window_spec_release(spec);
432@@ -322,8 +324,6 @@
433 int displacement_y)
434 : displacement_x{displacement_x},
435 displacement_y{displacement_y},
436- width{width},
437- height{height},
438 stream{mir_connection_create_render_surface_sync(connection, width, height), &mir_render_surface_release},
439 bs{get_stream(connection, width, height)}
440 {
441
442=== modified file 'examples/prerendered_frames.c'
443--- examples/prerendered_frames.c 2017-05-19 13:49:04 +0000
444+++ examples/prerendered_frames.c 2017-06-06 15:58:12 +0000
445@@ -92,6 +92,36 @@
446 rendering = 0;
447 }
448
449+static void handle_window_event(MirWindow* window, MirEvent const* event, void* context)
450+{
451+ MirRenderSurface* const surface = (MirRenderSurface*)context;
452+
453+ switch (mir_event_get_type(event))
454+ {
455+ case mir_event_type_resize:
456+ {
457+ MirResizeEvent const* resize = mir_event_get_resize_event(event);
458+ int const new_width = mir_resize_event_get_width(resize);
459+ int const new_height = mir_resize_event_get_height(resize);
460+
461+ mir_render_surface_set_size(surface, new_width, new_height);
462+ MirWindowSpec* spec = mir_create_window_spec(mir_window_get_connection(window));
463+ mir_window_spec_add_render_surface(spec, surface, new_width, new_height, 0, 0);
464+ mir_window_apply_spec(window, spec);
465+ mir_window_spec_release(spec);
466+ break;
467+ }
468+
469+ case mir_event_type_close_window:
470+ printf("Received close event from server.\n");
471+ rendering = 0;
472+ break;
473+
474+ default:
475+ break;
476+ }
477+}
478+
479 int main(int argc, char** argv)
480 {
481 static char const *socket_file = NULL;
482@@ -165,6 +195,8 @@
483 MirWindowSpec* spec = mir_create_normal_window_spec(connection, width, height);
484 mir_window_spec_add_render_surface(
485 spec, surface, width, height, displacement_x, displacement_y);
486+ mir_window_spec_set_event_handler(spec, &handle_window_event, surface);
487+ mir_window_spec_set_name(spec, "prerendered_frames");
488 MirWindow* window = mir_create_window_sync(spec);
489 if (!mir_window_is_valid(window))
490 {
491
492=== modified file 'examples/target.c'
493--- examples/target.c 2017-05-08 03:04:26 +0000
494+++ examples/target.c 2017-06-06 15:58:12 +0000
495@@ -196,6 +196,7 @@
496 get_all_touch_points(mir_event_get_input_event(event), &state->touch);
497 break;
498 case mir_event_type_resize:
499+ egl_app_handle_resize_event(surface, mir_event_get_resize_event(event));
500 state->resized = true;
501 break;
502 case mir_event_type_close_window:
503
504=== modified file 'src/utils/vanity.c'
505--- src/utils/vanity.c 2017-05-08 03:04:26 +0000
506+++ src/utils/vanity.c 2017-06-06 15:58:12 +0000
507@@ -176,6 +176,7 @@
508 handled = on_surface_event(mir_event_get_window_event(event), state);
509 break;
510 case mir_event_type_resize:
511+ egl_app_handle_resize_event(surface, mir_event_get_resize_event(event));
512 state->resized = true;
513 break;
514 default:

Subscribers

People subscribed via source and target branches