blob: dbf61efe91eaffd7df1d94339ae8c0bc655201e7 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004 * Copyright © 2012-2015 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050026 */
27
Kristian Høgsberga9410222011-01-14 17:22:35 -050028#include "config.h"
29
Daniel Stoneb7452fe2012-06-01 12:14:06 +010030#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010035#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020037#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040038#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010039#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040040#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020041#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020042#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020043#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050045#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040046#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040047#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040048#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050049#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040050#include <sys/time.h>
51#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030052#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050053
Pekka Paalanenb5026542014-11-12 15:09:24 +020054#include "timeline.h"
55
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030057#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020058#include "presentation-time-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070059#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070060#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070061#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030062#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040063#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050064#include "version.h"
Pekka Paalanen827b5d22016-06-29 11:54:26 +020065#include "plugin-registry.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050066
Pekka Paalanen0513a952014-05-21 16:17:27 +030067#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
68
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020069static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030070weston_output_update_matrix(struct weston_output *output);
71
72static void
Alexander Larsson0b135062013-05-28 16:23:36 +020073weston_output_transform_scale_init(struct weston_output *output,
74 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020075
Rob Bradford27b17932013-06-26 18:08:46 +010076static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050077weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010078
Pekka Paalanendcac3512017-12-08 14:13:34 +020079static char *
80weston_output_create_heads_string(struct weston_output *output);
81
Pekka Paalanen6528c032017-03-28 15:27:10 +030082/** Send wl_output events for mode and scale changes
83 *
84 * \param head Send on all resources bound to this head.
85 * \param mode_changed If true, send the current mode.
86 * \param scale_changed If true, send the current scale.
87 */
88static void
89weston_mode_switch_send_events(struct weston_head *head,
90 bool mode_changed, bool scale_changed)
91{
92 struct weston_output *output = head->output;
93 struct wl_resource *resource;
94 int version;
95
96 wl_resource_for_each(resource, &head->resource_list) {
97 if (mode_changed) {
98 wl_output_send_mode(resource,
99 output->current_mode->flags,
100 output->current_mode->width,
101 output->current_mode->height,
102 output->current_mode->refresh);
103 }
104
105 version = wl_resource_get_version(resource);
106 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
107 wl_output_send_scale(resource, output->current_scale);
108
109 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
110 wl_output_send_done(resource);
111 }
112}
113
114static void
115weston_mode_switch_finish(struct weston_output *output,
116 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800117{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200118 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300119 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200120 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200121
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200122 pixman_region32_init(&old_output_region);
123 pixman_region32_copy(&old_output_region, &output->region);
124
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200125 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200126 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200127
128 pixman_region32_init(&output->previous_damage);
129 pixman_region32_init_rect(&output->region, output->x, output->y,
130 output->width, output->height);
131
132 weston_output_update_matrix(output);
133
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200134 /* If a pointer falls outside the outputs new geometry, move it to its
135 * lower-right corner */
136 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500137 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200138 int32_t x, y;
139
140 if (!pointer)
141 continue;
142
143 x = wl_fixed_to_int(pointer->x);
144 y = wl_fixed_to_int(pointer->y);
145
146 if (!pixman_region32_contains_point(&old_output_region,
147 x, y, NULL) ||
148 pixman_region32_contains_point(&output->region,
149 x, y, NULL))
150 continue;
151
152 if (x >= output->x + output->width)
153 x = output->x + output->width - 1;
154 if (y >= output->y + output->height)
155 y = output->y + output->height - 1;
156
157 pointer->x = wl_fixed_from_int(x);
158 pointer->y = wl_fixed_from_int(y);
159 }
160
161 pixman_region32_fini(&old_output_region);
162
Derek Foremandd4cd332014-11-10 10:29:59 -0600163 if (!mode_changed && !scale_changed)
164 return;
165
Hardening57388e42013-09-18 23:56:36 +0200166 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300167 wl_list_for_each(head, &output->head_list, output_link)
168 weston_mode_switch_send_events(head,
169 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600170}
171
David Fort0de859e2016-05-27 23:22:57 +0200172static void
173weston_compositor_reflow_outputs(struct weston_compositor *compositor,
174 struct weston_output *resized_output, int delta_width);
175
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600176WL_EXPORT int
177weston_output_mode_set_native(struct weston_output *output,
178 struct weston_mode *mode,
179 int32_t scale)
180{
181 int ret;
182 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200183 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600184
185 if (!output->switch_mode)
186 return -1;
187
188 if (!output->original_mode) {
189 mode_changed = 1;
190 ret = output->switch_mode(output, mode);
191 if (ret < 0)
192 return ret;
193 if (output->current_scale != scale) {
194 scale_changed = 1;
195 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200196 }
197 }
198
David Fort0de859e2016-05-27 23:22:57 +0200199 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600200 output->native_mode = mode;
201 output->native_scale = scale;
202
203 weston_mode_switch_finish(output, mode_changed, scale_changed);
204
David Fort0de859e2016-05-27 23:22:57 +0200205 if (mode_changed || scale_changed) {
206 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
207
208 wl_signal_emit(&output->compositor->output_resized_signal, output);
209 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600210 return 0;
211}
212
213WL_EXPORT int
214weston_output_mode_switch_to_native(struct weston_output *output)
215{
216 int ret;
217 int mode_changed = 0, scale_changed = 0;
218
219 if (!output->switch_mode)
220 return -1;
221
222 if (!output->original_mode) {
223 weston_log("already in the native mode\n");
224 return -1;
225 }
226 /* the non fullscreen clients haven't seen a mode set since we
227 * switched into a temporary, so we need to notify them if the
228 * mode at that time is different from the native mode now.
229 */
230 mode_changed = (output->original_mode != output->native_mode);
231 scale_changed = (output->original_scale != output->native_scale);
232
233 ret = output->switch_mode(output, output->native_mode);
234 if (ret < 0)
235 return ret;
236
237 output->current_scale = output->native_scale;
238
239 output->original_mode = NULL;
240 output->original_scale = 0;
241
242 weston_mode_switch_finish(output, mode_changed, scale_changed);
243
244 return 0;
245}
246
247WL_EXPORT int
248weston_output_mode_switch_to_temporary(struct weston_output *output,
249 struct weston_mode *mode,
250 int32_t scale)
251{
252 int ret;
253
254 if (!output->switch_mode)
255 return -1;
256
257 /* original_mode is the last mode non full screen clients have seen,
258 * so we shouldn't change it if we already have one set.
259 */
260 if (!output->original_mode) {
261 output->original_mode = output->native_mode;
262 output->original_scale = output->native_scale;
263 }
264 ret = output->switch_mode(output, mode);
265 if (ret < 0)
266 return ret;
267
268 output->current_scale = scale;
269
270 weston_mode_switch_finish(output, 0, 0);
271
272 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800273}
274
Benjamin Franzke06286262011-05-06 19:12:33 +0200275static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300276region_init_infinite(pixman_region32_t *region)
277{
278 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
279 UINT32_MAX, UINT32_MAX);
280}
281
Pekka Paalanene67858b2013-04-25 13:57:42 +0300282static struct weston_subsurface *
283weston_surface_to_subsurface(struct weston_surface *surface);
284
Jason Ekstranda7af7042013-10-12 22:38:11 -0500285WL_EXPORT struct weston_view *
286weston_view_create(struct weston_surface *surface)
287{
288 struct weston_view *view;
289
Bryce Harringtonde16d892014-11-20 22:21:57 -0800290 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500291 if (view == NULL)
292 return NULL;
293
294 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000295 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500296
Jason Ekstranda7af7042013-10-12 22:38:11 -0500297 /* Assign to surface */
298 wl_list_insert(&surface->views, &view->surface_link);
299
300 wl_signal_init(&view->destroy_signal);
301 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300302 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500303
Jason Ekstranda7af7042013-10-12 22:38:11 -0500304 pixman_region32_init(&view->clip);
305
306 view->alpha = 1.0;
307 pixman_region32_init(&view->transform.opaque);
308
309 wl_list_init(&view->geometry.transformation_list);
310 wl_list_insert(&view->geometry.transformation_list,
311 &view->transform.position.link);
312 weston_matrix_init(&view->transform.position.matrix);
313 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200314 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500315 pixman_region32_init(&view->transform.boundingbox);
316 view->transform.dirty = 1;
317
Jason Ekstranda7af7042013-10-12 22:38:11 -0500318 return view;
319}
320
Jason Ekstrand108865d2014-06-26 10:04:49 -0700321struct weston_frame_callback {
322 struct wl_resource *resource;
323 struct wl_list link;
324};
325
Pekka Paalanen133e4392014-09-23 22:08:46 -0400326struct weston_presentation_feedback {
327 struct wl_resource *resource;
328
329 /* XXX: could use just wl_resource_get_link() instead */
330 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200331
332 /* The per-surface feedback flags */
333 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400334};
335
336static void
337weston_presentation_feedback_discard(
338 struct weston_presentation_feedback *feedback)
339{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200340 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400341 wl_resource_destroy(feedback->resource);
342}
343
344static void
345weston_presentation_feedback_discard_list(struct wl_list *list)
346{
347 struct weston_presentation_feedback *feedback, *tmp;
348
349 wl_list_for_each_safe(feedback, tmp, list, link)
350 weston_presentation_feedback_discard(feedback);
351}
352
353static void
354weston_presentation_feedback_present(
355 struct weston_presentation_feedback *feedback,
356 struct weston_output *output,
357 uint32_t refresh_nsec,
358 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200359 uint64_t seq,
360 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400361{
362 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300363 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400364 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200365 uint32_t tv_sec_hi;
366 uint32_t tv_sec_lo;
367 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300368 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400369
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300370 wl_list_for_each(head, &output->head_list, output_link) {
371 wl_resource_for_each(o, &head->resource_list) {
372 if (wl_resource_get_client(o) != client)
373 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400374
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300375 wp_presentation_feedback_send_sync_output(feedback->resource, o);
376 done = true;
377 }
378
379 /* For clone mode, send it for just one wl_output global,
380 * they are all equivalent anyway.
381 */
382 if (done)
383 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400384 }
385
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200386 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200387 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200388 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200389 refresh_nsec,
390 seq >> 32, seq & 0xffffffff,
391 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400392 wl_resource_destroy(feedback->resource);
393}
394
395static void
396weston_presentation_feedback_present_list(struct wl_list *list,
397 struct weston_output *output,
398 uint32_t refresh_nsec,
399 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200400 uint64_t seq,
401 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400402{
403 struct weston_presentation_feedback *feedback, *tmp;
404
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200405 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200406 wl_list_empty(list));
407
Pekka Paalanen133e4392014-09-23 22:08:46 -0400408 wl_list_for_each_safe(feedback, tmp, list, link)
409 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200410 refresh_nsec, ts, seq,
411 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400412}
413
Jason Ekstrand7b982072014-05-20 14:33:03 -0500414static void
415surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
416{
417 struct weston_surface_state *state =
418 container_of(listener, struct weston_surface_state,
419 buffer_destroy_listener);
420
421 state->buffer = NULL;
422}
423
424static void
425weston_surface_state_init(struct weston_surface_state *state)
426{
427 state->newly_attached = 0;
428 state->buffer = NULL;
429 state->buffer_destroy_listener.notify =
430 surface_state_handle_buffer_destroy;
431 state->sx = 0;
432 state->sy = 0;
433
Derek Foreman152254b2015-11-26 14:17:48 -0600434 pixman_region32_init(&state->damage_surface);
435 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500436 pixman_region32_init(&state->opaque);
437 region_init_infinite(&state->input);
438
439 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400440 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500441
442 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
443 state->buffer_viewport.buffer.scale = 1;
444 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
445 state->buffer_viewport.surface.width = -1;
446 state->buffer_viewport.changed = 0;
447}
448
449static void
450weston_surface_state_fini(struct weston_surface_state *state)
451{
452 struct weston_frame_callback *cb, *next;
453
454 wl_list_for_each_safe(cb, next,
455 &state->frame_callback_list, link)
456 wl_resource_destroy(cb->resource);
457
Pekka Paalanen133e4392014-09-23 22:08:46 -0400458 weston_presentation_feedback_discard_list(&state->feedback_list);
459
Jason Ekstrand7b982072014-05-20 14:33:03 -0500460 pixman_region32_fini(&state->input);
461 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600462 pixman_region32_fini(&state->damage_surface);
463 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500464
465 if (state->buffer)
466 wl_list_remove(&state->buffer_destroy_listener.link);
467 state->buffer = NULL;
468}
469
470static void
471weston_surface_state_set_buffer(struct weston_surface_state *state,
472 struct weston_buffer *buffer)
473{
474 if (state->buffer == buffer)
475 return;
476
477 if (state->buffer)
478 wl_list_remove(&state->buffer_destroy_listener.link);
479 state->buffer = buffer;
480 if (state->buffer)
481 wl_signal_add(&state->buffer->destroy_signal,
482 &state->buffer_destroy_listener);
483}
484
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500485WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500486weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500487{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500488 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400489
Bryce Harringtonde16d892014-11-20 22:21:57 -0800490 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400491 if (surface == NULL)
492 return NULL;
493
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500494 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800495 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500496
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500497 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200498 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400499
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200500 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
501 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200502 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
503 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500504
505 weston_surface_state_init(&surface->pending);
506
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400507 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500508 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300509 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400510
Jason Ekstranda7af7042013-10-12 22:38:11 -0500511 wl_list_init(&surface->views);
512
513 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400514 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500515
Pekka Paalanene67858b2013-04-25 13:57:42 +0300516 wl_list_init(&surface->subsurface_list);
517 wl_list_init(&surface->subsurface_list_pending);
518
Jason Ekstrand1e059042014-10-16 10:55:19 -0500519 weston_matrix_init(&surface->buffer_to_surface_matrix);
520 weston_matrix_init(&surface->surface_to_buffer_matrix);
521
Jonas Ådahld3414f22016-07-22 17:56:31 +0800522 wl_list_init(&surface->pointer_constraints);
523
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400524 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500525}
526
Alex Wu8811bf92012-02-28 18:07:54 +0800527WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500528weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200529 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500530{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100531 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500532}
533
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400534WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500535weston_view_to_global_float(struct weston_view *view,
536 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200537{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500538 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200539 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
540
Jason Ekstranda7af7042013-10-12 22:38:11 -0500541 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200542
543 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200544 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700545 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200546 v.f[3]);
547 *x = 0;
548 *y = 0;
549 return;
550 }
551
552 *x = v.f[0] / v.f[3];
553 *y = v.f[1] / v.f[3];
554 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500555 *x = sx + view->geometry.x;
556 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200557 }
558}
559
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500560WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200561weston_transformed_coord(int width, int height,
562 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200563 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200564 float sx, float sy, float *bx, float *by)
565{
566 switch (transform) {
567 case WL_OUTPUT_TRANSFORM_NORMAL:
568 default:
569 *bx = sx;
570 *by = sy;
571 break;
572 case WL_OUTPUT_TRANSFORM_FLIPPED:
573 *bx = width - sx;
574 *by = sy;
575 break;
576 case WL_OUTPUT_TRANSFORM_90:
577 *bx = height - sy;
578 *by = sx;
579 break;
580 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
581 *bx = height - sy;
582 *by = width - sx;
583 break;
584 case WL_OUTPUT_TRANSFORM_180:
585 *bx = width - sx;
586 *by = height - sy;
587 break;
588 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
589 *bx = sx;
590 *by = height - sy;
591 break;
592 case WL_OUTPUT_TRANSFORM_270:
593 *bx = sy;
594 *by = width - sx;
595 break;
596 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
597 *bx = sy;
598 *by = sx;
599 break;
600 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200601
602 *bx *= scale;
603 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200604}
605
606WL_EXPORT pixman_box32_t
607weston_transformed_rect(int width, int height,
608 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200609 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200610 pixman_box32_t rect)
611{
612 float x1, x2, y1, y2;
613
614 pixman_box32_t ret;
615
Alexander Larsson4ea95522013-05-22 14:41:37 +0200616 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200617 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200618 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200619 rect.x2, rect.y2, &x2, &y2);
620
621 if (x1 <= x2) {
622 ret.x1 = x1;
623 ret.x2 = x2;
624 } else {
625 ret.x1 = x2;
626 ret.x2 = x1;
627 }
628
629 if (y1 <= y2) {
630 ret.y1 = y1;
631 ret.y2 = y2;
632 } else {
633 ret.y1 = y2;
634 ret.y2 = y1;
635 }
636
637 return ret;
638}
639
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600640/** Transform a region by a matrix, restricted to axis-aligned transformations
641 *
642 * Warning: This function does not work for projective, affine, or matrices
643 * that encode arbitrary rotations. Only 90-degree step rotations are
644 * supported.
645 */
646WL_EXPORT void
647weston_matrix_transform_region(pixman_region32_t *dest,
648 struct weston_matrix *matrix,
649 pixman_region32_t *src)
650{
651 pixman_box32_t *src_rects, *dest_rects;
652 int nrects, i;
653
654 src_rects = pixman_region32_rectangles(src, &nrects);
655 dest_rects = malloc(nrects * sizeof(*dest_rects));
656 if (!dest_rects)
657 return;
658
659 for (i = 0; i < nrects; i++) {
660 struct weston_vector vec1 = {{
661 src_rects[i].x1, src_rects[i].y1, 0, 1
662 }};
663 weston_matrix_transform(matrix, &vec1);
664 vec1.f[0] /= vec1.f[3];
665 vec1.f[1] /= vec1.f[3];
666
667 struct weston_vector vec2 = {{
668 src_rects[i].x2, src_rects[i].y2, 0, 1
669 }};
670 weston_matrix_transform(matrix, &vec2);
671 vec2.f[0] /= vec2.f[3];
672 vec2.f[1] /= vec2.f[3];
673
674 if (vec1.f[0] < vec2.f[0]) {
675 dest_rects[i].x1 = floor(vec1.f[0]);
676 dest_rects[i].x2 = ceil(vec2.f[0]);
677 } else {
678 dest_rects[i].x1 = floor(vec2.f[0]);
679 dest_rects[i].x2 = ceil(vec1.f[0]);
680 }
681
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600682 if (vec1.f[1] < vec2.f[1]) {
683 dest_rects[i].y1 = floor(vec1.f[1]);
684 dest_rects[i].y2 = ceil(vec2.f[1]);
685 } else {
686 dest_rects[i].y1 = floor(vec2.f[1]);
687 dest_rects[i].y2 = ceil(vec1.f[1]);
688 }
689 }
690
691 pixman_region32_clear(dest);
692 pixman_region32_init_rects(dest, dest_rects, nrects);
693 free(dest_rects);
694}
695
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200696WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500697weston_transformed_region(int width, int height,
698 enum wl_output_transform transform,
699 int32_t scale,
700 pixman_region32_t *src, pixman_region32_t *dest)
701{
702 pixman_box32_t *src_rects, *dest_rects;
703 int nrects, i;
704
705 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
706 if (src != dest)
707 pixman_region32_copy(dest, src);
708 return;
709 }
710
711 src_rects = pixman_region32_rectangles(src, &nrects);
712 dest_rects = malloc(nrects * sizeof(*dest_rects));
713 if (!dest_rects)
714 return;
715
716 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
717 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
718 } else {
719 for (i = 0; i < nrects; i++) {
720 switch (transform) {
721 default:
722 case WL_OUTPUT_TRANSFORM_NORMAL:
723 dest_rects[i].x1 = src_rects[i].x1;
724 dest_rects[i].y1 = src_rects[i].y1;
725 dest_rects[i].x2 = src_rects[i].x2;
726 dest_rects[i].y2 = src_rects[i].y2;
727 break;
728 case WL_OUTPUT_TRANSFORM_90:
729 dest_rects[i].x1 = height - src_rects[i].y2;
730 dest_rects[i].y1 = src_rects[i].x1;
731 dest_rects[i].x2 = height - src_rects[i].y1;
732 dest_rects[i].y2 = src_rects[i].x2;
733 break;
734 case WL_OUTPUT_TRANSFORM_180:
735 dest_rects[i].x1 = width - src_rects[i].x2;
736 dest_rects[i].y1 = height - src_rects[i].y2;
737 dest_rects[i].x2 = width - src_rects[i].x1;
738 dest_rects[i].y2 = height - src_rects[i].y1;
739 break;
740 case WL_OUTPUT_TRANSFORM_270:
741 dest_rects[i].x1 = src_rects[i].y1;
742 dest_rects[i].y1 = width - src_rects[i].x2;
743 dest_rects[i].x2 = src_rects[i].y2;
744 dest_rects[i].y2 = width - src_rects[i].x1;
745 break;
746 case WL_OUTPUT_TRANSFORM_FLIPPED:
747 dest_rects[i].x1 = width - src_rects[i].x2;
748 dest_rects[i].y1 = src_rects[i].y1;
749 dest_rects[i].x2 = width - src_rects[i].x1;
750 dest_rects[i].y2 = src_rects[i].y2;
751 break;
752 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
753 dest_rects[i].x1 = height - src_rects[i].y2;
754 dest_rects[i].y1 = width - src_rects[i].x2;
755 dest_rects[i].x2 = height - src_rects[i].y1;
756 dest_rects[i].y2 = width - src_rects[i].x1;
757 break;
758 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
759 dest_rects[i].x1 = src_rects[i].x1;
760 dest_rects[i].y1 = height - src_rects[i].y2;
761 dest_rects[i].x2 = src_rects[i].x2;
762 dest_rects[i].y2 = height - src_rects[i].y1;
763 break;
764 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
765 dest_rects[i].x1 = src_rects[i].y1;
766 dest_rects[i].y1 = src_rects[i].x1;
767 dest_rects[i].x2 = src_rects[i].y2;
768 dest_rects[i].y2 = src_rects[i].x2;
769 break;
770 }
771 }
772 }
773
774 if (scale != 1) {
775 for (i = 0; i < nrects; i++) {
776 dest_rects[i].x1 *= scale;
777 dest_rects[i].x2 *= scale;
778 dest_rects[i].y1 *= scale;
779 dest_rects[i].y2 *= scale;
780 }
781 }
782
783 pixman_region32_clear(dest);
784 pixman_region32_init_rects(dest, dest_rects, nrects);
785 free(dest_rects);
786}
787
Jonny Lamb74130762013-11-26 18:19:46 +0100788static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300789viewport_surface_to_buffer(struct weston_surface *surface,
790 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100791{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200792 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200793 double src_width, src_height;
794 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200795
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200796 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
797 if (vp->surface.width == -1) {
798 *bx = sx;
799 *by = sy;
800 return;
801 }
Jonny Lamb74130762013-11-26 18:19:46 +0100802
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200803 src_x = 0.0;
804 src_y = 0.0;
805 src_width = surface->width_from_buffer;
806 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100807 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200808 src_x = wl_fixed_to_double(vp->buffer.src_x);
809 src_y = wl_fixed_to_double(vp->buffer.src_y);
810 src_width = wl_fixed_to_double(vp->buffer.src_width);
811 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100812 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200813
814 *bx = sx * src_width / surface->width + src_x;
815 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100816}
817
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500818WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200819weston_surface_to_buffer_float(struct weston_surface *surface,
820 float sx, float sy, float *bx, float *by)
821{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200822 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
823
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300824 /* first transform coordinates if the viewport is set */
825 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100826
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500827 weston_transformed_coord(surface->width_from_buffer,
828 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200829 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100830 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200831}
832
Derek Foreman99739672015-12-03 16:38:11 -0600833/** Transform a rectangle from surface coordinates to buffer coordinates
834 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300835 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600836 * from.
837 * \param rect The rectangle to transform.
838 * \return The transformed rectangle.
839 *
840 * Viewport and buffer transformations can only do translation, scaling,
841 * and rotations in 90-degree steps. Therefore the only loss in the
842 * conversion is coordinate rounding.
843 *
844 * However, some coordinate rounding takes place as an intermediate
845 * step before the buffer scale factor is applied, so the rectangle
846 * boundary may not be exactly as expected.
847 *
848 * This is OK for damage tracking since a little extra coverage is
849 * not a problem.
850 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200851WL_EXPORT pixman_box32_t
852weston_surface_to_buffer_rect(struct weston_surface *surface,
853 pixman_box32_t rect)
854{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200855 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100856 float xf, yf;
857
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300858 /* first transform box coordinates if the viewport is set */
859 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100860 rect.x1 = floorf(xf);
861 rect.y1 = floorf(yf);
862
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300863 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600864 rect.x2 = ceilf(xf);
865 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100866
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500867 return weston_transformed_rect(surface->width_from_buffer,
868 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200869 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200870 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200871}
872
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200873/** Transform a region from surface coordinates to buffer coordinates
874 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300875 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200876 * from.
877 * \param surface_region[in] The region in surface coordinates.
878 * \param buffer_region[out] The region converted to buffer coordinates.
879 *
880 * Buffer_region must be init'd, but will be completely overwritten.
881 *
882 * Viewport and buffer transformations can only do translation, scaling,
883 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600884 * conversion is from the coordinate rounding that takes place in
885 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200886 */
887WL_EXPORT void
888weston_surface_to_buffer_region(struct weston_surface *surface,
889 pixman_region32_t *surface_region,
890 pixman_region32_t *buffer_region)
891{
892 pixman_box32_t *src_rects, *dest_rects;
893 int nrects, i;
894
895 src_rects = pixman_region32_rectangles(surface_region, &nrects);
896 dest_rects = malloc(nrects * sizeof(*dest_rects));
897 if (!dest_rects)
898 return;
899
900 for (i = 0; i < nrects; i++) {
901 dest_rects[i] = weston_surface_to_buffer_rect(surface,
902 src_rects[i]);
903 }
904
905 pixman_region32_fini(buffer_region);
906 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
907 free(dest_rects);
908}
909
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200910WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400912 struct weston_plane *plane)
913{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500914 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400915 return;
916
Jason Ekstranda7af7042013-10-12 22:38:11 -0500917 weston_view_damage_below(view);
918 view->plane = plane;
919 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400920}
921
Pekka Paalanen51723d52015-02-17 13:10:01 +0200922/** Inflict damage on the plane where the view is visible.
923 *
924 * \param view The view that causes the damage.
925 *
926 * If the view is currently on a plane (including the primary plane),
927 * take the view's boundingbox, subtract all the opaque views that cover it,
928 * and add the remaining region as damage to the plane. This corresponds
929 * to the damage inflicted to the plane if this view disappeared.
930 *
931 * A repaint is scheduled for this view.
932 *
933 * The region of all opaque views covering this view is stored in
934 * weston_view::clip and updated by view_accumulate_damage() during
935 * weston_output_repaint(). Specifically, that region matches the
936 * scenegraph as it was last painted.
937 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400938WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200940{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500941 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200942
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500943 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200944 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500945 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800946 if (view->plane)
947 pixman_region32_union(&view->plane->damage,
948 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500949 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800950 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200951}
952
Pekka Paalanen01e00682017-03-24 16:21:06 +0200953/** Send wl_surface.enter/leave events
954 *
955 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300956 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200957 * \param enter True if entered.
958 * \param left True if left.
959 *
960 * Send the enter/leave events for all protocol objects bound to the given
961 * output by the client owning the surface.
962 */
963static void
964weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300965 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +0200966 bool enter,
967 bool leave)
968{
969 struct wl_resource *wloutput;
970 struct wl_client *client;
971
972 assert(enter != leave);
973
974 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300975 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +0200976 if (wl_resource_get_client(wloutput) != client)
977 continue;
978
979 if (enter)
980 wl_surface_send_enter(surface->resource, wloutput);
981 if (leave)
982 wl_surface_send_leave(surface->resource, wloutput);
983 }
984}
985
Bryce Harrington3f650b82015-12-23 11:01:58 -0800986/**
987 * \param es The surface
988 * \param mask The new set of outputs for the surface
989 *
990 * Sets the surface's set of outputs to the ones specified by
991 * the new output mask provided. Identifies the outputs that
992 * have changed, the posts enter and leave events for these
993 * outputs as appropriate.
994 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400995static void
996weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
997{
998 uint32_t different = es->output_mask ^ mask;
999 uint32_t entered = mask & different;
1000 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001001 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001002 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001003 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001004
1005 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001006 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001007 return;
1008 if (different == 0)
1009 return;
1010
1011 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001012 output_bit = 1u << output->id;
1013 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001014 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001015
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001016 wl_list_for_each(head, &output->head_list, output_link) {
1017 weston_surface_send_enter_leave(es, head,
1018 output_bit & entered,
1019 output_bit & left);
1020 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001021 }
1022}
1023
Bryce Harrington3f650b82015-12-23 11:01:58 -08001024/** Recalculate which output(s) the surface has views displayed on
1025 *
1026 * \param es The surface to remap to outputs
1027 *
1028 * Finds the output that is showing the largest amount of one
1029 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001030 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001031 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001032 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001033 * in the output_mask for the surface.
1034 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001035static void
1036weston_surface_assign_output(struct weston_surface *es)
1037{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038 struct weston_output *new_output;
1039 struct weston_view *view;
1040 pixman_region32_t region;
1041 uint32_t max, area, mask;
1042 pixman_box32_t *e;
1043
1044 new_output = NULL;
1045 max = 0;
1046 mask = 0;
1047 pixman_region32_init(&region);
1048 wl_list_for_each(view, &es->views, surface_link) {
1049 if (!view->output)
1050 continue;
1051
1052 pixman_region32_intersect(&region, &view->transform.boundingbox,
1053 &view->output->region);
1054
1055 e = pixman_region32_extents(&region);
1056 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1057
1058 mask |= view->output_mask;
1059
1060 if (area >= max) {
1061 new_output = view->output;
1062 max = area;
1063 }
1064 }
1065 pixman_region32_fini(&region);
1066
1067 es->output = new_output;
1068 weston_surface_update_output_mask(es, mask);
1069}
1070
Bryce Harrington3f650b82015-12-23 11:01:58 -08001071/** Recalculate which output(s) the view is displayed on
1072 *
1073 * \param ev The view to remap to outputs
1074 *
1075 * Identifies the set of outputs that the view is visible on,
1076 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001077 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001078 *
1079 * Also does the same for the view's surface. See
1080 * weston_surface_assign_output().
1081 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082static void
1083weston_view_assign_output(struct weston_view *ev)
1084{
1085 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001086 struct weston_output *output, *new_output;
1087 pixman_region32_t region;
1088 uint32_t max, area, mask;
1089 pixman_box32_t *e;
1090
1091 new_output = NULL;
1092 max = 0;
1093 mask = 0;
1094 pixman_region32_init(&region);
1095 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001096 if (output->destroying)
1097 continue;
1098
Jason Ekstranda7af7042013-10-12 22:38:11 -05001099 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001100 &output->region);
1101
1102 e = pixman_region32_extents(&region);
1103 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1104
1105 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001106 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001107
1108 if (area >= max) {
1109 new_output = output;
1110 max = area;
1111 }
1112 }
1113 pixman_region32_fini(&region);
1114
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 ev->output = new_output;
1116 ev->output_mask = mask;
1117
1118 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001119}
1120
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001121static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001122weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1123 int from_x, int from_y, int *to_x, int *to_y)
1124{
1125 float x, y;
1126
1127 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1128 weston_view_from_global_float(to, x, y, &x, &y);
1129
1130 *to_x = round(x);
1131 *to_y = round(y);
1132}
1133
1134static void
1135weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1136{
1137 pixman_box32_t *a;
1138 pixman_box32_t b;
1139
1140 a = pixman_region32_extents(&from->geometry.scissor);
1141
1142 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1143 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1144
1145 pixman_region32_fini(&to->geometry.scissor);
1146 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1147}
1148
1149static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001150view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001152{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001153 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1154 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001155 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001156 { inbox->x1, inbox->y1 },
1157 { inbox->x1, inbox->y2 },
1158 { inbox->x2, inbox->y1 },
1159 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001160 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001161 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001162 int i;
1163
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001164 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001165 /* avoid rounding empty bbox to 1x1 */
1166 pixman_region32_init(bbox);
1167 return;
1168 }
1169
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001170 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001171 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001173 if (x < min_x)
1174 min_x = x;
1175 if (x > max_x)
1176 max_x = x;
1177 if (y < min_y)
1178 min_y = y;
1179 if (y > max_y)
1180 max_y = y;
1181 }
1182
Pekka Paalanen219b9822012-02-08 15:38:37 +02001183 int_x = floorf(min_x);
1184 int_y = floorf(min_y);
1185 pixman_region32_init_rect(bbox, int_x, int_y,
1186 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001187}
1188
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001189static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001191{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001193
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001194 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001195 view->geometry.x = roundf(view->geometry.x);
1196 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001197
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001198 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1200 view->transform.position.matrix.d[12] = view->geometry.x;
1201 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001202
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001204
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 view->transform.inverse = view->transform.position.matrix;
1206 view->transform.inverse.d[12] = -view->geometry.x;
1207 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001208
Jason Ekstranda7af7042013-10-12 22:38:11 -05001209 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001210 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001211 view->surface->width,
1212 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001213 if (view->geometry.scissor_enabled)
1214 pixman_region32_intersect(&view->transform.boundingbox,
1215 &view->transform.boundingbox,
1216 &view->geometry.scissor);
1217
1218 pixman_region32_translate(&view->transform.boundingbox,
1219 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001220
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221 if (view->alpha == 1.0) {
1222 pixman_region32_copy(&view->transform.opaque,
1223 &view->surface->opaque);
1224 pixman_region32_translate(&view->transform.opaque,
1225 view->geometry.x,
1226 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001227 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001228}
1229
1230static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001232{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 struct weston_view *parent = view->geometry.parent;
1234 struct weston_matrix *matrix = &view->transform.matrix;
1235 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001236 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001237 pixman_region32_t surfregion;
1238 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001239
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001241
1242 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1244 view->transform.position.matrix.d[12] = view->geometry.x;
1245 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001246
1247 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001248 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001249 weston_matrix_multiply(matrix, &tform->matrix);
1250
Pekka Paalanen483243f2013-03-08 14:56:50 +02001251 if (parent)
1252 weston_matrix_multiply(matrix, &parent->transform.matrix);
1253
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001254 if (weston_matrix_invert(inverse, matrix) < 0) {
1255 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 weston_log("error: weston_view %p"
1257 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001258 return -1;
1259 }
1260
Giulio Camuffo148c1992016-09-04 18:50:46 +03001261 if (view->alpha == 1.0 &&
1262 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1263 pixman_region32_copy(&view->transform.opaque,
1264 &view->surface->opaque);
1265 pixman_region32_translate(&view->transform.opaque,
1266 matrix->d[12],
1267 matrix->d[13]);
1268 }
1269
Pekka Paalanen380adf52015-02-16 14:39:11 +02001270 pixman_region32_init_rect(&surfregion, 0, 0,
1271 view->surface->width, view->surface->height);
1272 if (view->geometry.scissor_enabled)
1273 pixman_region32_intersect(&surfregion, &surfregion,
1274 &view->geometry.scissor);
1275 surfbox = pixman_region32_extents(&surfregion);
1276
1277 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1278 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001279
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001280 return 0;
1281}
1282
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001283static struct weston_layer *
1284get_view_layer(struct weston_view *view)
1285{
1286 if (view->parent_view)
1287 return get_view_layer(view->parent_view);
1288 return view->layer_link.layer;
1289}
1290
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001291WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001293{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001295 struct weston_layer *layer;
1296 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001297
Jason Ekstranda7af7042013-10-12 22:38:11 -05001298 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001299 return;
1300
Pekka Paalanen483243f2013-03-08 14:56:50 +02001301 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001303
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001305
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001307
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 pixman_region32_fini(&view->transform.boundingbox);
1309 pixman_region32_fini(&view->transform.opaque);
1310 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001311
Pekka Paalanencd403622012-01-25 13:37:39 +02001312 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313 if (view->geometry.transformation_list.next ==
1314 &view->transform.position.link &&
1315 view->geometry.transformation_list.prev ==
1316 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001317 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001319 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 if (weston_view_update_transform_enable(view) < 0)
1321 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001322 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001323
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001324 layer = get_view_layer(view);
1325 if (layer) {
1326 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001327 pixman_region32_intersect(&view->transform.boundingbox,
1328 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001329 pixman_region32_intersect(&view->transform.opaque,
1330 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001331 pixman_region32_fini(&mask);
1332 }
1333
Pekka Paalanen380adf52015-02-16 14:39:11 +02001334 if (parent) {
1335 if (parent->geometry.scissor_enabled) {
1336 view->geometry.scissor_enabled = true;
1337 weston_view_transfer_scissor(parent, view);
1338 } else {
1339 view->geometry.scissor_enabled = false;
1340 }
1341 }
1342
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001344
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001346
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 wl_signal_emit(&view->surface->compositor->transform_signal,
1348 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001349}
1350
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001351WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001352weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001353{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001355
1356 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 * The invariant: if view->geometry.dirty, then all views
1358 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001359 * Corollary: if not parent->geometry.dirty, then all ancestors
1360 * are not dirty.
1361 */
1362
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001364 return;
1365
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001367
Jason Ekstranda7af7042013-10-12 22:38:11 -05001368 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001369 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001370 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001371}
1372
1373WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374weston_view_to_global_fixed(struct weston_view *view,
1375 wl_fixed_t vx, wl_fixed_t vy,
1376 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001377{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001378 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001379
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380 weston_view_to_global_float(view,
1381 wl_fixed_to_double(vx),
1382 wl_fixed_to_double(vy),
1383 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001384 *x = wl_fixed_from_double(xf);
1385 *y = wl_fixed_from_double(yf);
1386}
1387
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001388WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389weston_view_from_global_float(struct weston_view *view,
1390 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001391{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001393 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1394
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001396
1397 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001398 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001400 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 *vx = 0;
1402 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001403 return;
1404 }
1405
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 *vx = v.f[0] / v.f[3];
1407 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001408 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409 *vx = x - view->geometry.x;
1410 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001411 }
1412}
1413
1414WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415weston_view_from_global_fixed(struct weston_view *view,
1416 wl_fixed_t x, wl_fixed_t y,
1417 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001418{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001420
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 weston_view_from_global_float(view,
1422 wl_fixed_to_double(x),
1423 wl_fixed_to_double(y),
1424 &vxf, &vyf);
1425 *vx = wl_fixed_from_double(vxf);
1426 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001427}
1428
1429WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001430weston_view_from_global(struct weston_view *view,
1431 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001432{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001434
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1436 *vx = floorf(vxf);
1437 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001438}
1439
Bryce Harrington3f650b82015-12-23 11:01:58 -08001440/**
1441 * \param surface The surface to be repainted
1442 *
1443 * Marks the output(s) that the surface is shown on as needing to be
1444 * repainted. See weston_output_schedule_repaint().
1445 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001446WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001447weston_surface_schedule_repaint(struct weston_surface *surface)
1448{
1449 struct weston_output *output;
1450
1451 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001452 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001453 weston_output_schedule_repaint(output);
1454}
1455
Bryce Harrington3f650b82015-12-23 11:01:58 -08001456/**
1457 * \param view The view to be repainted
1458 *
1459 * Marks the output(s) that the view is shown on as needing to be
1460 * repainted. See weston_output_schedule_repaint().
1461 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001462WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463weston_view_schedule_repaint(struct weston_view *view)
1464{
1465 struct weston_output *output;
1466
1467 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001468 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469 weston_output_schedule_repaint(output);
1470}
1471
Pekka Paalanene508ce62015-02-19 13:59:55 +02001472/**
1473 * XXX: This function does it the wrong way.
1474 * surface->damage is the damage from the client, and causes
1475 * surface_flush_damage() to copy pixels. No window management action can
1476 * cause damage to the client-provided content, warranting re-upload!
1477 *
1478 * Instead of surface->damage, this function should record the damage
1479 * with all the views for this surface to avoid extraneous texture
1480 * uploads.
1481 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001482WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001483weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001484{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001485 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486 0, 0, surface->width,
1487 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001488
Kristian Høgsberg98238702012-08-03 16:29:12 -04001489 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001490}
1491
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001492WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001493weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001494{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001495 if (view->geometry.x == x && view->geometry.y == y)
1496 return;
1497
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 view->geometry.x = x;
1499 view->geometry.y = y;
1500 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001501}
1502
Pekka Paalanen483243f2013-03-08 14:56:50 +02001503static void
1504transform_parent_handle_parent_destroy(struct wl_listener *listener,
1505 void *data)
1506{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001507 struct weston_view *view =
1508 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001509 geometry.parent_destroy_listener);
1510
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001512}
1513
1514WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001516 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001517{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518 if (view->geometry.parent) {
1519 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1520 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001521
1522 if (!parent)
1523 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001524 }
1525
Jason Ekstranda7af7042013-10-12 22:38:11 -05001526 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001527
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001529 transform_parent_handle_parent_destroy;
1530 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001531 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001532 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001533 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001535 }
1536
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537 weston_view_geometry_dirty(view);
1538}
1539
Pekka Paalanen380adf52015-02-16 14:39:11 +02001540/** Set a clip mask rectangle on a view
1541 *
1542 * \param view The view to set the clip mask on.
1543 * \param x Top-left corner X coordinate of the clip rectangle.
1544 * \param y Top-left corner Y coordinate of the clip rectangle.
1545 * \param width Width of the clip rectangle, non-negative.
1546 * \param height Height of the clip rectangle, non-negative.
1547 *
1548 * A shell may set a clip mask rectangle on a view. Everything outside
1549 * the rectangle is cut away for input and output purposes: it is
1550 * not drawn and cannot be hit by hit-test based input like pointer
1551 * motion or touch-downs. Everything inside the rectangle will behave
1552 * normally. Clients are unaware of clipping.
1553 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001554 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001555 * mask rectangle does not affect the view position, the view is positioned
1556 * as it would be without a clip. The clip also does not change
1557 * weston_surface::width,height.
1558 *
1559 * The clip mask rectangle is part of transformation inheritance
1560 * (weston_view_set_transform_parent()). A clip set in the root of the
1561 * transformation inheritance tree will affect all views in the tree.
1562 * A clip can be set only on the root view. Attempting to set a clip
1563 * on view that has a transformation parent will fail. Assigning a parent
1564 * to a view that has a clip set will cause the clip to be forgotten.
1565 *
1566 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1567 * on the additional transformations in the child views. These transformations
1568 * may not rotate the coordinate axes, i.e., only translation and scaling
1569 * are allowed. Violating this restriction causes the clipping to malfunction.
1570 * Furthermore, using scaling may cause rounding errors in child clipping.
1571 *
1572 * The clip mask rectangle is not automatically adjusted based on
1573 * wl_surface.attach dx and dy arguments.
1574 *
1575 * A clip mask rectangle can be set only if the compositor capability
1576 * WESTON_CAP_VIEW_CLIP_MASK is present.
1577 *
1578 * This function sets the clip mask rectangle and schedules a repaint for
1579 * the view.
1580 */
1581WL_EXPORT void
1582weston_view_set_mask(struct weston_view *view,
1583 int x, int y, int width, int height)
1584{
1585 struct weston_compositor *compositor = view->surface->compositor;
1586
1587 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1588 weston_log("%s not allowed without capability!\n", __func__);
1589 return;
1590 }
1591
1592 if (view->geometry.parent) {
1593 weston_log("view %p has a parent, clip forbidden!\n", view);
1594 return;
1595 }
1596
1597 if (width < 0 || height < 0) {
1598 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1599 x, y, width, height);
1600 return;
1601 }
1602
1603 pixman_region32_fini(&view->geometry.scissor);
1604 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1605 view->geometry.scissor_enabled = true;
1606 weston_view_geometry_dirty(view);
1607 weston_view_schedule_repaint(view);
1608}
1609
1610/** Remove the clip mask from a view
1611 *
1612 * \param view The view to remove the clip mask from.
1613 *
1614 * Removed the clip mask rectangle and schedules a repaint.
1615 *
1616 * \sa weston_view_set_mask
1617 */
1618WL_EXPORT void
1619weston_view_set_mask_infinite(struct weston_view *view)
1620{
1621 view->geometry.scissor_enabled = false;
1622 weston_view_geometry_dirty(view);
1623 weston_view_schedule_repaint(view);
1624}
1625
Armin Krezović0da12b82016-06-30 06:04:33 +02001626/* Check if view should be displayed
1627 *
1628 * The indicator is set manually when assigning
1629 * a view to a surface.
1630 *
1631 * This needs reworking. See the thread starting at:
1632 *
1633 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1634 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001635WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001636weston_view_is_mapped(struct weston_view *view)
1637{
Armin Krezović0da12b82016-06-30 06:04:33 +02001638 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001639}
1640
Armin Krezović0da12b82016-06-30 06:04:33 +02001641/* Check if a surface has a view assigned to it
1642 *
1643 * The indicator is set manually when mapping
1644 * a surface and creating a view for it.
1645 *
1646 * This needs to go. See the thread starting at:
1647 *
1648 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1649 *
1650 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001651WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001652weston_surface_is_mapped(struct weston_surface *surface)
1653{
Armin Krezović0da12b82016-06-30 06:04:33 +02001654 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001655}
1656
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001657static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001658surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001659{
1660 struct weston_view *view;
1661
1662 if (surface->width == width && surface->height == height)
1663 return;
1664
1665 surface->width = width;
1666 surface->height = height;
1667
1668 wl_list_for_each(view, &surface->views, surface_link)
1669 weston_view_geometry_dirty(view);
1670}
1671
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001672WL_EXPORT void
1673weston_surface_set_size(struct weston_surface *surface,
1674 int32_t width, int32_t height)
1675{
1676 assert(!surface->resource);
1677 surface_set_size(surface, width, height);
1678}
1679
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001680static int
1681fixed_round_up_to_int(wl_fixed_t f)
1682{
1683 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1684}
1685
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001686static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001687convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1688 int32_t width, int32_t height,
1689 uint32_t transform,
1690 int32_t scale)
1691{
1692 assert(scale > 0);
1693
1694 switch (transform) {
1695 case WL_OUTPUT_TRANSFORM_NORMAL:
1696 case WL_OUTPUT_TRANSFORM_180:
1697 case WL_OUTPUT_TRANSFORM_FLIPPED:
1698 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1699 *width_out = width / scale;
1700 *height_out = height / scale;
1701 break;
1702 case WL_OUTPUT_TRANSFORM_90:
1703 case WL_OUTPUT_TRANSFORM_270:
1704 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1705 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1706 *width_out = height / scale;
1707 *height_out = width / scale;
1708 break;
1709 default:
1710 assert(0 && "invalid transform");
1711 }
1712}
1713
1714static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001715weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001716{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001717 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001718
1719 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001720 surface->width_from_buffer = 0;
1721 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001722 return;
1723 }
1724
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001725 convert_size_by_transform_scale(&surface->width_from_buffer,
1726 &surface->height_from_buffer,
1727 surface->buffer_ref.buffer->width,
1728 surface->buffer_ref.buffer->height,
1729 vp->buffer.transform,
1730 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001731}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001732
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001733static void
1734weston_surface_update_size(struct weston_surface *surface)
1735{
1736 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1737 int32_t width, height;
1738
1739 width = surface->width_from_buffer;
1740 height = surface->height_from_buffer;
1741
1742 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001743 surface_set_size(surface,
1744 vp->surface.width, vp->surface.height);
1745 return;
1746 }
1747
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001748 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001749 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1750 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1751
1752 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001753 return;
1754 }
1755
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001756 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001757}
1758
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001759WL_EXPORT void
1760weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001761{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001762 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001763}
1764
Jason Ekstranda7af7042013-10-12 22:38:11 -05001765WL_EXPORT struct weston_view *
1766weston_compositor_pick_view(struct weston_compositor *compositor,
1767 wl_fixed_t x, wl_fixed_t y,
1768 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001769{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001771 wl_fixed_t view_x, view_y;
1772 int view_ix, view_iy;
1773 int ix = wl_fixed_to_int(x);
1774 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001775
Jason Ekstranda7af7042013-10-12 22:38:11 -05001776 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001777 if (!pixman_region32_contains_point(
1778 &view->transform.boundingbox, ix, iy, NULL))
1779 continue;
1780
1781 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1782 view_ix = wl_fixed_to_int(view_x);
1783 view_iy = wl_fixed_to_int(view_y);
1784
1785 if (!pixman_region32_contains_point(&view->surface->input,
1786 view_ix, view_iy, NULL))
1787 continue;
1788
Pekka Paalanen380adf52015-02-16 14:39:11 +02001789 if (view->geometry.scissor_enabled &&
1790 !pixman_region32_contains_point(&view->geometry.scissor,
1791 view_ix, view_iy, NULL))
1792 continue;
1793
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001794 *vx = view_x;
1795 *vy = view_y;
1796 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001797 }
1798
Derek Foremanf9318d12015-05-11 15:40:11 -05001799 *vx = wl_fixed_from_int(-1000000);
1800 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001801 return NULL;
1802}
1803
1804static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001805weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001806{
Daniel Stone37816df2012-05-16 18:45:18 +01001807 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001808
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001809 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001810 return;
1811
Daniel Stone37816df2012-05-16 18:45:18 +01001812 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001813 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001814}
1815
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001816WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001817weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001818{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001819 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 if (!weston_view_is_mapped(view))
1822 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001823
Jason Ekstranda7af7042013-10-12 22:38:11 -05001824 weston_view_damage_below(view);
1825 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001826 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001827 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001828 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 wl_list_remove(&view->link);
1830 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001831 view->output_mask = 0;
1832 weston_surface_assign_output(view->surface);
1833
1834 if (weston_surface_is_mapped(view->surface))
1835 return;
1836
1837 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001838 struct weston_touch *touch = weston_seat_get_touch(seat);
1839 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1840 struct weston_keyboard *keyboard =
1841 weston_seat_get_keyboard(seat);
1842
1843 if (keyboard && keyboard->focus == view->surface)
1844 weston_keyboard_set_focus(keyboard, NULL);
1845 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001846 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001847 if (touch && touch->focus == view)
1848 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001849 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001850}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001851
Jason Ekstranda7af7042013-10-12 22:38:11 -05001852WL_EXPORT void
1853weston_surface_unmap(struct weston_surface *surface)
1854{
1855 struct weston_view *view;
1856
Armin Krezovićf8486c32016-06-30 06:04:28 +02001857 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001858 wl_list_for_each(view, &surface->views, surface_link)
1859 weston_view_unmap(view);
1860 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001861}
1862
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001863static void
1864weston_surface_reset_pending_buffer(struct weston_surface *surface)
1865{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001866 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001867 surface->pending.sx = 0;
1868 surface->pending.sy = 0;
1869 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001870 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001871}
1872
Jason Ekstranda7af7042013-10-12 22:38:11 -05001873WL_EXPORT void
1874weston_view_destroy(struct weston_view *view)
1875{
1876 wl_signal_emit(&view->destroy_signal, view);
1877
1878 assert(wl_list_empty(&view->geometry.child_list));
1879
1880 if (weston_view_is_mapped(view)) {
1881 weston_view_unmap(view);
1882 weston_compositor_build_view_list(view->surface->compositor);
1883 }
1884
1885 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001886 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001887
1888 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001889 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001890 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001891 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001892
1893 weston_view_set_transform_parent(view, NULL);
1894
Jason Ekstranda7af7042013-10-12 22:38:11 -05001895 wl_list_remove(&view->surface_link);
1896
1897 free(view);
1898}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001899
1900WL_EXPORT void
1901weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001902{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001903 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001904 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001905 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001906
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001907 if (--surface->ref_count > 0)
1908 return;
1909
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001910 assert(surface->resource == NULL);
1911
Pekka Paalanenca790762015-04-17 14:23:38 +03001912 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001913
Pekka Paalanene67858b2013-04-25 13:57:42 +03001914 assert(wl_list_empty(&surface->subsurface_list_pending));
1915 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001916
Jason Ekstranda7af7042013-10-12 22:38:11 -05001917 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1918 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001919
Jason Ekstrand7b982072014-05-20 14:33:03 -05001920 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001921
Pekka Paalanende685b82012-12-04 15:58:12 +02001922 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001923
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001924 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001925 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001926 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001927
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001928 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001929 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001930
Pekka Paalanen133e4392014-09-23 22:08:46 -04001931 weston_presentation_feedback_discard_list(&surface->feedback_list);
1932
Jonas Ådahld3414f22016-07-22 17:56:31 +08001933 wl_list_for_each_safe(constraint, next_constraint,
1934 &surface->pointer_constraints,
1935 link)
1936 weston_pointer_constraint_destroy(constraint);
1937
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001938 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001939}
1940
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001941static void
1942destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001943{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001944 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001945
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001946 assert(surface);
1947
Giulio Camuffo0d379742013-11-15 22:06:15 +01001948 /* Set the resource to NULL, since we don't want to leave a
1949 * dangling pointer if the surface was refcounted and survives
1950 * the weston_surface_destroy() call. */
1951 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001952
1953 if (surface->viewport_resource)
1954 wl_resource_set_user_data(surface->viewport_resource, NULL);
1955
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001956 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001957}
1958
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001959static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001960weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1961{
1962 struct weston_buffer *buffer =
1963 container_of(listener, struct weston_buffer, destroy_listener);
1964
1965 wl_signal_emit(&buffer->destroy_signal, buffer);
1966 free(buffer);
1967}
1968
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001969WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001970weston_buffer_from_resource(struct wl_resource *resource)
1971{
1972 struct weston_buffer *buffer;
1973 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001974
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001975 listener = wl_resource_get_destroy_listener(resource,
1976 weston_buffer_destroy_handler);
1977
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001978 if (listener)
1979 return container_of(listener, struct weston_buffer,
1980 destroy_listener);
1981
1982 buffer = zalloc(sizeof *buffer);
1983 if (buffer == NULL)
1984 return NULL;
1985
1986 buffer->resource = resource;
1987 wl_signal_init(&buffer->destroy_signal);
1988 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001989 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001990 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001991
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001992 return buffer;
1993}
1994
1995static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001996weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1997 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001998{
Pekka Paalanende685b82012-12-04 15:58:12 +02001999 struct weston_buffer_reference *ref =
2000 container_of(listener, struct weston_buffer_reference,
2001 destroy_listener);
2002
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002003 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002004 ref->buffer = NULL;
2005}
2006
2007WL_EXPORT void
2008weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002009 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002010{
2011 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002012 ref->buffer->busy_count--;
2013 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002014 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002015 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002016 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002017 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002018 }
2019
Pekka Paalanende685b82012-12-04 15:58:12 +02002020 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002021 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002022 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002023 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002024 }
2025
Pekka Paalanende685b82012-12-04 15:58:12 +02002026 ref->buffer = buffer;
2027 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2028}
2029
2030static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002031weston_surface_attach(struct weston_surface *surface,
2032 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002033{
2034 weston_buffer_reference(&surface->buffer_ref, buffer);
2035
Pekka Paalanena6421c42012-12-04 15:58:10 +02002036 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002037 if (weston_surface_is_mapped(surface))
2038 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002039 }
2040
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002041 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002042
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002043 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002044 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002045}
2046
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002047WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002048weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002049{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002050 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002051
2052 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002053 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002054}
2055
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002056WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002057weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002058{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002059 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002060
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002061 pixman_region32_union(&compositor->primary_plane.damage,
2062 &compositor->primary_plane.damage,
2063 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002064 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002065}
2066
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002067static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002068surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002069{
Pekka Paalanende685b82012-12-04 15:58:12 +02002070 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002071 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002072 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002073
Pekka Paalanenb5026542014-11-12 15:09:24 +02002074 if (weston_timeline_enabled_ &&
2075 pixman_region32_not_empty(&surface->damage))
2076 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2077 TLP_OUTPUT(surface->output), TLP_END);
2078
Jason Ekstrandef540082014-06-26 10:37:36 -07002079 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002080}
2081
2082static void
2083view_accumulate_damage(struct weston_view *view,
2084 pixman_region32_t *opaque)
2085{
2086 pixman_region32_t damage;
2087
2088 pixman_region32_init(&damage);
2089 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002090 pixman_box32_t *extents;
2091
Jason Ekstranda7af7042013-10-12 22:38:11 -05002092 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002093 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002094 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002095 pixman_region32_copy(&damage, &view->surface->damage);
2096 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002097 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002098 }
2099
Pekka Paalanen380adf52015-02-16 14:39:11 +02002100 pixman_region32_intersect(&damage, &damage,
2101 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002102 pixman_region32_subtract(&damage, &damage, opaque);
2103 pixman_region32_union(&view->plane->damage,
2104 &view->plane->damage, &damage);
2105 pixman_region32_fini(&damage);
2106 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002107 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002108}
2109
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002110static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002111compositor_accumulate_damage(struct weston_compositor *ec)
2112{
2113 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002114 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002115 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002116
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002117 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002118
2119 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002120 pixman_region32_copy(&plane->clip, &clip);
2121
2122 pixman_region32_init(&opaque);
2123
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124 wl_list_for_each(ev, &ec->view_list, link) {
2125 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002126 continue;
2127
Jason Ekstranda7af7042013-10-12 22:38:11 -05002128 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002129 }
2130
2131 pixman_region32_union(&clip, &clip, &opaque);
2132 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002133 }
2134
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002135 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002136
Jason Ekstranda7af7042013-10-12 22:38:11 -05002137 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002138 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002139
2140 wl_list_for_each(ev, &ec->view_list, link) {
2141 if (ev->surface->touched)
2142 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002143 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144
2145 surface_flush_damage(ev->surface);
2146
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002147 /* Both the renderer and the backend have seen the buffer
2148 * by now. If renderer needs the buffer, it has its own
2149 * reference set. If the backend wants to keep the buffer
2150 * around for migrating the surface into a non-primary plane
2151 * later, keep_buffer is true. Otherwise, drop the core
2152 * reference now, and allow early buffer release. This enables
2153 * clients to use single-buffering.
2154 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002155 if (!ev->surface->keep_buffer)
2156 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002157 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002158}
2159
2160static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002162{
2163 struct weston_subsurface *sub;
2164
Pekka Paalanene67858b2013-04-25 13:57:42 +03002165 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002166 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002167 continue;
2168
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2170 wl_list_init(&sub->surface->views);
2171
2172 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002173 }
2174}
2175
2176static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002178{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179 struct weston_subsurface *sub;
2180 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002181
Jason Ekstranda7af7042013-10-12 22:38:11 -05002182 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2183 if (sub->surface == surface)
2184 continue;
2185
George Kiagiadakised04d382014-06-13 18:10:26 +02002186 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2187 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002188 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002189 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002190
2191 surface_free_unused_subsurface_views(sub->surface);
2192 }
2193}
2194
2195static void
2196view_list_add_subsurface_view(struct weston_compositor *compositor,
2197 struct weston_subsurface *sub,
2198 struct weston_view *parent)
2199{
2200 struct weston_subsurface *child;
2201 struct weston_view *view = NULL, *iv;
2202
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002203 if (!weston_surface_is_mapped(sub->surface))
2204 return;
2205
Jason Ekstranda7af7042013-10-12 22:38:11 -05002206 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2207 if (iv->geometry.parent == parent) {
2208 view = iv;
2209 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002210 }
2211 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212
2213 if (view) {
2214 /* Put it back in the surface's list of views */
2215 wl_list_remove(&view->surface_link);
2216 wl_list_insert(&sub->surface->views, &view->surface_link);
2217 } else {
2218 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002219 weston_view_set_position(view,
2220 sub->position.x,
2221 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002222 weston_view_set_transform_parent(view, parent);
2223 }
2224
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002225 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002227 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002228
Pekka Paalanenb188e912013-11-19 14:03:35 +02002229 if (wl_list_empty(&sub->surface->subsurface_list)) {
2230 wl_list_insert(compositor->view_list.prev, &view->link);
2231 return;
2232 }
2233
2234 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2235 if (child->surface == sub->surface)
2236 wl_list_insert(compositor->view_list.prev, &view->link);
2237 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002238 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002239 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002240}
2241
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002242/* This recursively adds the sub-surfaces for a view, relying on the
2243 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2244 * change first happens to the sub-surface list, and then automatically
2245 * propagates here. See weston_surface_damage_subsurfaces() for how the
2246 * sub-surfaces receive damage when the client changes the state.
2247 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002248static void
2249view_list_add(struct weston_compositor *compositor,
2250 struct weston_view *view)
2251{
2252 struct weston_subsurface *sub;
2253
2254 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002255
Pekka Paalanenb188e912013-11-19 14:03:35 +02002256 if (wl_list_empty(&view->surface->subsurface_list)) {
2257 wl_list_insert(compositor->view_list.prev, &view->link);
2258 return;
2259 }
2260
2261 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2262 if (sub->surface == view->surface)
2263 wl_list_insert(compositor->view_list.prev, &view->link);
2264 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002265 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002266 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002267}
2268
2269static void
2270weston_compositor_build_view_list(struct weston_compositor *compositor)
2271{
2272 struct weston_view *view;
2273 struct weston_layer *layer;
2274
2275 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002276 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002277 surface_stash_subsurface_views(view->surface);
2278
2279 wl_list_init(&compositor->view_list);
2280 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002281 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002282 view_list_add(compositor, view);
2283 }
2284 }
2285
2286 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002287 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002289}
2290
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002291static void
2292weston_output_take_feedback_list(struct weston_output *output,
2293 struct weston_surface *surface)
2294{
2295 struct weston_view *view;
2296 struct weston_presentation_feedback *feedback;
2297 uint32_t flags = 0xffffffff;
2298
2299 if (wl_list_empty(&surface->feedback_list))
2300 return;
2301
2302 /* All views must have the flag for the flag to survive. */
2303 wl_list_for_each(view, &surface->views, surface_link) {
2304 /* ignore views that are not on this output at all */
2305 if (view->output_mask & (1u << output->id))
2306 flags &= view->psf_flags;
2307 }
2308
2309 wl_list_for_each(feedback, &surface->feedback_list, link)
2310 feedback->psf_flags = flags;
2311
2312 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2313 wl_list_init(&surface->feedback_list);
2314}
2315
David Herrmann1edf44c2013-10-22 17:11:26 +02002316static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002317weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002318{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002319 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002320 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002321 struct weston_animation *animation, *next;
2322 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002323 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002324 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002325 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002326 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002327
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002328 if (output->destroying)
2329 return 0;
2330
Pekka Paalanenb5026542014-11-12 15:09:24 +02002331 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2332
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002333 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002334 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002335
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002336 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002337 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002338 } else {
2339 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002340 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002341 ev->psf_flags = 0;
2342 }
2343 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002344
Pekka Paalanene67858b2013-04-25 13:57:42 +03002345 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002346 wl_list_for_each(ev, &ec->view_list, link) {
2347 /* Note: This operation is safe to do multiple times on the
2348 * same surface.
2349 */
2350 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002351 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002352 &ev->surface->frame_callback_list);
2353 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002354
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002355 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002356 }
2357 }
2358
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002359 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002360
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002361 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002362 pixman_region32_intersect(&output_damage,
2363 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002364 pixman_region32_subtract(&output_damage,
2365 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002366
Scott Moreauccbf29d2012-02-22 14:21:41 -07002367 if (output->dirty)
2368 weston_output_update_matrix(output);
2369
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002370 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002371
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002372 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002373
Daniel Stone09a97e22017-03-01 11:34:06 +00002374 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002375 if (r == 0)
2376 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002377
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002378 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002379
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002380 frame_time_msec = timespec_to_msec(&output->frame_time);
2381
Jonas Ådahldb773762012-06-13 00:01:21 +02002382 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002383 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002384 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002385 }
2386
Scott Moreaud64cf212012-06-08 19:40:54 -06002387 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002388 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002389 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002390 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002391
Pekka Paalanenb5026542014-11-12 15:09:24 +02002392 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2393
David Herrmann1edf44c2013-10-22 17:11:26 +02002394 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002395}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002396
Pekka Paalanen82919792014-05-21 13:51:49 +03002397static void
2398weston_output_schedule_repaint_reset(struct weston_output *output)
2399{
Daniel Stone05df8c12017-03-03 16:59:42 +00002400 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002401 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002402}
2403
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002404static int
2405weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2406 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002407{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002408 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002409 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002410 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002411
Daniel Stone6847b852017-03-01 11:34:08 +00002412 /* We're not ready yet; come back to make a decision later. */
2413 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002414 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002415
2416 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2417 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002418 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002419
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002420 /* If we're sleeping, drop the repaint machinery entirely; we will
2421 * explicitly repaint all outputs when we come back. */
2422 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2423 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2424 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002425
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002426 /* We don't actually need to repaint this output; drop it from
2427 * repaint until something causes damage. */
2428 if (!output->repaint_needed)
2429 goto err;
2430
2431 /* If repaint fails, we aren't going to get weston_output_finish_frame
2432 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002433 * something schedules a successful repaint later. As repainting may
2434 * take some time, re-read our clock as a courtesy to the next
2435 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002436 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002437 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002438 if (ret != 0)
2439 goto err;
2440
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002441 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002442
2443err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002444 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002445 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002446}
2447
2448static void
2449output_repaint_timer_arm(struct weston_compositor *compositor)
2450{
2451 struct weston_output *output;
2452 bool any_should_repaint = false;
2453 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002454 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002455
2456 weston_compositor_read_presentation_clock(compositor, &now);
2457
2458 wl_list_for_each(output, &compositor->output_list, link) {
2459 int64_t msec_to_this;
2460
2461 if (output->repaint_status != REPAINT_SCHEDULED)
2462 continue;
2463
2464 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2465 &now);
2466 if (!any_should_repaint || msec_to_this < msec_to_next)
2467 msec_to_next = msec_to_this;
2468
2469 any_should_repaint = true;
2470 }
2471
2472 if (!any_should_repaint)
2473 return;
2474
2475 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2476 * This is a workaround to allow coalescing multiple output repaints
2477 * particularly from weston_output_finish_frame()
2478 * into the same call, which would not happen if we called
2479 * output_repaint_timer_handler() directly.
2480 */
2481 if (msec_to_next < 1)
2482 msec_to_next = 1;
2483
2484 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2485}
2486
2487static int
2488output_repaint_timer_handler(void *data)
2489{
2490 struct weston_compositor *compositor = data;
2491 struct weston_output *output;
2492 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002493 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002494 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002495
2496 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002497
2498 if (compositor->backend->repaint_begin)
2499 repaint_data = compositor->backend->repaint_begin(compositor);
2500
2501 wl_list_for_each(output, &compositor->output_list, link) {
2502 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2503 if (ret)
2504 break;
2505 }
2506
2507 if (ret == 0) {
2508 if (compositor->backend->repaint_flush)
2509 compositor->backend->repaint_flush(compositor,
2510 repaint_data);
2511 } else {
2512 if (compositor->backend->repaint_cancel)
2513 compositor->backend->repaint_cancel(compositor,
2514 repaint_data);
2515 }
Daniel Stone6847b852017-03-01 11:34:08 +00002516
2517 output_repaint_timer_arm(compositor);
2518
Pekka Paalanen0513a952014-05-21 16:17:27 +03002519 return 0;
2520}
2521
Kristian Høgsbergef044142011-06-21 15:02:12 -04002522WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002523weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002524 const struct timespec *stamp,
2525 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002526{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002527 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002528 int32_t refresh_nsec;
2529 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002530 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002531
Pekka Paalanenb5026542014-11-12 15:09:24 +02002532
Daniel Stone05df8c12017-03-03 16:59:42 +00002533 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002534 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2535
Daniel Stone6847b852017-03-01 11:34:08 +00002536 weston_compositor_read_presentation_clock(compositor, &now);
2537
Daniel Stone3615ce12017-03-01 11:34:05 +00002538 /* If we haven't been supplied any timestamp at all, we don't have a
2539 * timebase to work against, so any delay just wastes time. Push a
2540 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002541 if (!stamp) {
2542 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002543 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002544 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002545
Marius Vladdf9278a2018-03-06 18:56:23 +02002546 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2547 TLP_VBLANK(stamp), TLP_END);
2548
Pekka Paalanend7894d02015-07-03 15:08:53 +03002549 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002550 weston_presentation_feedback_present_list(&output->feedback_list,
2551 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002552 output->msc,
2553 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002554
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002555 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002556
Daniel Stone6847b852017-03-01 11:34:08 +00002557 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2558 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2559 -compositor->repaint_msec);
2560 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002561
2562 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002563 static bool warned;
2564
2565 if (!warned)
2566 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002567 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002568 warned = true;
2569
Daniel Stone6847b852017-03-01 11:34:08 +00002570 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002571 }
2572
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002573 /* Called from restart_repaint_loop and restart happens already after
2574 * the deadline given by repaint_msec? In that case we delay until
2575 * the deadline of the next frame, to give clients a more predictable
2576 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002577 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2578 msec_rel < 0) {
2579 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2580 timespec_add_nsec(&output->next_repaint,
2581 &output->next_repaint,
2582 refresh_nsec);
2583 }
2584 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002585
Daniel Stone3615ce12017-03-01 11:34:05 +00002586out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002587 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002588 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002589}
2590
2591static void
2592idle_repaint(void *data)
2593{
2594 struct weston_output *output = data;
2595
Daniel Stone05df8c12017-03-03 16:59:42 +00002596 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2597 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002598 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002599 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002600}
2601
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002602WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002603weston_layer_entry_insert(struct weston_layer_entry *list,
2604 struct weston_layer_entry *entry)
2605{
2606 wl_list_insert(&list->link, &entry->link);
2607 entry->layer = list->layer;
2608}
2609
2610WL_EXPORT void
2611weston_layer_entry_remove(struct weston_layer_entry *entry)
2612{
2613 wl_list_remove(&entry->link);
2614 wl_list_init(&entry->link);
2615 entry->layer = NULL;
2616}
2617
Quentin Glidic82681572016-12-17 13:40:51 +01002618
2619/** Initialize the weston_layer struct.
2620 *
2621 * \param compositor The compositor instance
2622 * \param layer The layer to initialize
2623 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002624WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002625weston_layer_init(struct weston_layer *layer,
2626 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002627{
Quentin Glidic82681572016-12-17 13:40:51 +01002628 layer->compositor = compositor;
2629 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002630 wl_list_init(&layer->view_list.link);
2631 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002632 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002633}
2634
2635/** Sets the position of the layer in the layer list. The layer will be placed
2636 * below any layer with the same position value, if any.
2637 * This function is safe to call if the layer is already on the list, but the
2638 * layer may be moved below other layers at the same position, if any.
2639 *
2640 * \param layer The layer to modify
2641 * \param position The position the layer will be placed at
2642 */
2643WL_EXPORT void
2644weston_layer_set_position(struct weston_layer *layer,
2645 enum weston_layer_position position)
2646{
2647 struct weston_layer *below;
2648
2649 wl_list_remove(&layer->link);
2650
2651 /* layer_list is ordered from top to bottom, the last layer being the
2652 * background with the smallest position value */
2653
2654 layer->position = position;
2655 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2656 if (below->position >= layer->position) {
2657 wl_list_insert(&below->link, &layer->link);
2658 return;
2659 }
2660 }
2661 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2662}
2663
2664/** Hide a layer by taking it off the layer list.
2665 * This function is safe to call if the layer is not on the list.
2666 *
2667 * \param layer The layer to hide
2668 */
2669WL_EXPORT void
2670weston_layer_unset_position(struct weston_layer *layer)
2671{
2672 wl_list_remove(&layer->link);
2673 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002674}
2675
2676WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002677weston_layer_set_mask(struct weston_layer *layer,
2678 int x, int y, int width, int height)
2679{
2680 struct weston_view *view;
2681
2682 layer->mask.x1 = x;
2683 layer->mask.x2 = x + width;
2684 layer->mask.y1 = y;
2685 layer->mask.y2 = y + height;
2686
2687 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2688 weston_view_geometry_dirty(view);
2689 }
2690}
2691
2692WL_EXPORT void
2693weston_layer_set_mask_infinite(struct weston_layer *layer)
2694{
2695 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2696 UINT32_MAX, UINT32_MAX);
2697}
2698
2699WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002700weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002701{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002702 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002703 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002704
Bryce Harrington08976ac2016-08-30 12:05:16 -07002705 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2706 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002707 return;
2708
Pekka Paalanenb5026542014-11-12 15:09:24 +02002709 if (!output->repaint_needed)
2710 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2711
Kristian Høgsbergef044142011-06-21 15:02:12 -04002712 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002713 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002714
2715 /* If we already have a repaint scheduled for our idle handler,
2716 * no need to set it again. If the repaint has been called but
2717 * not finished, then weston_output_finish_frame() will notice
2718 * that a repaint is needed and schedule one. */
2719 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002720 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002721
Daniel Stone05df8c12017-03-03 16:59:42 +00002722 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002723 assert(!output->idle_repaint_source);
2724 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2725 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002726 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002727}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002728
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002729WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002730weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2731{
2732 struct weston_output *output;
2733
2734 wl_list_for_each(output, &compositor->output_list, link)
2735 weston_output_schedule_repaint(output);
2736}
2737
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002738static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002739surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002740{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002741 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002742}
2743
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002744static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002745surface_attach(struct wl_client *client,
2746 struct wl_resource *resource,
2747 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2748{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002749 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002750 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002751
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002752 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002753 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002754 if (buffer == NULL) {
2755 wl_client_post_no_memory(client);
2756 return;
2757 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002758 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002759
Pekka Paalanende685b82012-12-04 15:58:12 +02002760 /* Attach, attach, without commit in between does not send
2761 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002762 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002763
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002764 surface->pending.sx = sx;
2765 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002766 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002767}
2768
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002769static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002770surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002771 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002772 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002773{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002774 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002775
Derek Foreman57e92ed2015-11-17 14:11:35 -06002776 if (width <= 0 || height <= 0)
2777 return;
2778
Derek Foreman152254b2015-11-26 14:17:48 -06002779 pixman_region32_union_rect(&surface->pending.damage_surface,
2780 &surface->pending.damage_surface,
2781 x, y, width, height);
2782}
2783
2784static void
2785surface_damage_buffer(struct wl_client *client,
2786 struct wl_resource *resource,
2787 int32_t x, int32_t y, int32_t width, int32_t height)
2788{
2789 struct weston_surface *surface = wl_resource_get_user_data(resource);
2790
2791 if (width <= 0 || height <= 0)
2792 return;
2793
2794 pixman_region32_union_rect(&surface->pending.damage_buffer,
2795 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002796 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002797}
2798
Kristian Høgsberg33418202011-08-16 23:01:28 -04002799static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002800destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002801{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002802 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002803
2804 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002805 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002806}
2807
2808static void
2809surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002810 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002811{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002812 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002813 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002814
2815 cb = malloc(sizeof *cb);
2816 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002817 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002818 return;
2819 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002820
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002821 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2822 callback);
2823 if (cb->resource == NULL) {
2824 free(cb);
2825 wl_resource_post_no_memory(resource);
2826 return;
2827 }
2828
Jason Ekstranda85118c2013-06-27 20:17:02 -05002829 wl_resource_set_implementation(cb->resource, NULL, cb,
2830 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002831
Pekka Paalanenbc106382012-10-10 12:49:31 +03002832 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002833}
2834
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002835static void
2836surface_set_opaque_region(struct wl_client *client,
2837 struct wl_resource *resource,
2838 struct wl_resource *region_resource)
2839{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002840 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002841 struct weston_region *region;
2842
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002843 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002844 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002845 pixman_region32_copy(&surface->pending.opaque,
2846 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002847 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002848 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002849 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002850}
2851
2852static void
2853surface_set_input_region(struct wl_client *client,
2854 struct wl_resource *resource,
2855 struct wl_resource *region_resource)
2856{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002857 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002858 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002859
2860 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002861 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002862 pixman_region32_copy(&surface->pending.input,
2863 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002864 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002865 pixman_region32_fini(&surface->pending.input);
2866 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002867 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002868}
2869
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002870/* Cause damage to this sub-surface and all its children.
2871 *
2872 * This is useful when there are state changes that need an implicit
2873 * damage, e.g. a z-order change.
2874 */
2875static void
2876weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2877{
2878 struct weston_subsurface *child;
2879
2880 weston_surface_damage(sub->surface);
2881 sub->reordered = false;
2882
2883 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2884 if (child != sub)
2885 weston_surface_damage_subsurfaces(child);
2886}
2887
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002888static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002889weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002890{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002891 struct weston_subsurface *sub;
2892
2893 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2894 parent_link_pending) {
2895 wl_list_remove(&sub->parent_link);
2896 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002897
2898 if (sub->reordered)
2899 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002900 }
2901}
2902
2903static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002904weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002905 struct weston_matrix *matrix)
2906{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002907 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002908 double src_width, src_height, dest_width, dest_height;
2909
2910 weston_matrix_init(matrix);
2911
2912 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2913 src_width = surface->width_from_buffer;
2914 src_height = surface->height_from_buffer;
2915 } else {
2916 src_width = wl_fixed_to_double(vp->buffer.src_width);
2917 src_height = wl_fixed_to_double(vp->buffer.src_height);
2918 }
2919
2920 if (vp->surface.width == -1) {
2921 dest_width = src_width;
2922 dest_height = src_height;
2923 } else {
2924 dest_width = vp->surface.width;
2925 dest_height = vp->surface.height;
2926 }
2927
2928 if (src_width != dest_width || src_height != dest_height)
2929 weston_matrix_scale(matrix,
2930 src_width / dest_width,
2931 src_height / dest_height, 1);
2932
2933 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2934 weston_matrix_translate(matrix,
2935 wl_fixed_to_double(vp->buffer.src_x),
2936 wl_fixed_to_double(vp->buffer.src_y),
2937 0);
2938
2939 switch (vp->buffer.transform) {
2940 case WL_OUTPUT_TRANSFORM_FLIPPED:
2941 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2942 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2943 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2944 weston_matrix_scale(matrix, -1, 1, 1);
2945 weston_matrix_translate(matrix,
2946 surface->width_from_buffer, 0, 0);
2947 break;
2948 }
2949
2950 switch (vp->buffer.transform) {
2951 default:
2952 case WL_OUTPUT_TRANSFORM_NORMAL:
2953 case WL_OUTPUT_TRANSFORM_FLIPPED:
2954 break;
2955 case WL_OUTPUT_TRANSFORM_90:
2956 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2957 weston_matrix_rotate_xy(matrix, 0, 1);
2958 weston_matrix_translate(matrix,
2959 surface->height_from_buffer, 0, 0);
2960 break;
2961 case WL_OUTPUT_TRANSFORM_180:
2962 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2963 weston_matrix_rotate_xy(matrix, -1, 0);
2964 weston_matrix_translate(matrix,
2965 surface->width_from_buffer,
2966 surface->height_from_buffer, 0);
2967 break;
2968 case WL_OUTPUT_TRANSFORM_270:
2969 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2970 weston_matrix_rotate_xy(matrix, 0, -1);
2971 weston_matrix_translate(matrix,
2972 0, surface->width_from_buffer, 0);
2973 break;
2974 }
2975
2976 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2977}
2978
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002979/**
2980 * Compute a + b > c while being safe to overflows.
2981 */
2982static bool
2983fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2984{
2985 return (int64_t)a + (int64_t)b > (int64_t)c;
2986}
2987
2988static bool
2989weston_surface_is_pending_viewport_source_valid(
2990 const struct weston_surface *surface)
2991{
2992 const struct weston_surface_state *pend = &surface->pending;
2993 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2994 int width_from_buffer = 0;
2995 int height_from_buffer = 0;
2996 wl_fixed_t w;
2997 wl_fixed_t h;
2998
2999 /* If viewport source rect is not set, it is always ok. */
3000 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3001 return true;
3002
3003 if (pend->newly_attached) {
3004 if (pend->buffer) {
3005 convert_size_by_transform_scale(&width_from_buffer,
3006 &height_from_buffer,
3007 pend->buffer->width,
3008 pend->buffer->height,
3009 vp->buffer.transform,
3010 vp->buffer.scale);
3011 } else {
3012 /* No buffer: viewport is irrelevant. */
3013 return true;
3014 }
3015 } else {
3016 width_from_buffer = surface->width_from_buffer;
3017 height_from_buffer = surface->height_from_buffer;
3018 }
3019
3020 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3021 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3022
3023 /* No buffer: viewport is irrelevant. */
3024 if (width_from_buffer == 0 || height_from_buffer == 0)
3025 return true;
3026
3027 /* overflow checks for wl_fixed_from_int() */
3028 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3029 return false;
3030 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3031 return false;
3032
3033 w = wl_fixed_from_int(width_from_buffer);
3034 h = wl_fixed_from_int(height_from_buffer);
3035
3036 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3037 return false;
3038 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3039 return false;
3040
3041 return true;
3042}
3043
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003044static bool
3045fixed_is_integer(wl_fixed_t v)
3046{
3047 return (v & 0xff) == 0;
3048}
3049
3050static bool
3051weston_surface_is_pending_viewport_dst_size_int(
3052 const struct weston_surface *surface)
3053{
3054 const struct weston_buffer_viewport *vp =
3055 &surface->pending.buffer_viewport;
3056
3057 if (vp->surface.width != -1) {
3058 assert(vp->surface.width > 0 && vp->surface.height > 0);
3059 return true;
3060 }
3061
3062 return fixed_is_integer(vp->buffer.src_width) &&
3063 fixed_is_integer(vp->buffer.src_height);
3064}
3065
Derek Foreman152254b2015-11-26 14:17:48 -06003066/* Translate pending damage in buffer co-ordinates to surface
3067 * co-ordinates and union it with a pixman_region32_t.
3068 * This should only be called after the buffer is attached.
3069 */
3070static void
3071apply_damage_buffer(pixman_region32_t *dest,
3072 struct weston_surface *surface,
3073 struct weston_surface_state *state)
3074{
3075 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3076
3077 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3078 * translated into surface co-ordinates and unioned with
3079 * any other surface damage.
3080 * None of this makes sense if there is no buffer though.
3081 */
3082 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3083 pixman_region32_t buffer_damage;
3084
3085 pixman_region32_intersect_rect(&state->damage_buffer,
3086 &state->damage_buffer,
3087 0, 0, buffer->width,
3088 buffer->height);
3089 pixman_region32_init(&buffer_damage);
3090 weston_matrix_transform_region(&buffer_damage,
3091 &surface->buffer_to_surface_matrix,
3092 &state->damage_buffer);
3093 pixman_region32_union(dest, dest, &buffer_damage);
3094 pixman_region32_fini(&buffer_damage);
3095 }
3096 /* We should clear this on commit even if there was no buffer */
3097 pixman_region32_clear(&state->damage_buffer);
3098}
3099
Jason Ekstrand1e059042014-10-16 10:55:19 -05003100static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003101weston_surface_commit_state(struct weston_surface *surface,
3102 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003103{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003104 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003105 pixman_region32_t opaque;
3106
Alexander Larsson4ea95522013-05-22 14:41:37 +02003107 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003108 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003109 /* wp_viewport.set_source */
3110 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003111 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003112
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003113 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003114 if (state->newly_attached)
3115 weston_surface_attach(surface, state->buffer);
3116 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003117
Jason Ekstrand1e059042014-10-16 10:55:19 -05003118 weston_surface_build_buffer_matrix(surface,
3119 &surface->surface_to_buffer_matrix);
3120 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3121 &surface->surface_to_buffer_matrix);
3122
Jason Ekstrand7b982072014-05-20 14:33:03 -05003123 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003124 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003125 if (surface->committed)
3126 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003127 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003128
Jason Ekstrand7b982072014-05-20 14:33:03 -05003129 state->sx = 0;
3130 state->sy = 0;
3131 state->newly_attached = 0;
3132 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003133
Derek Foreman152254b2015-11-26 14:17:48 -06003134 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003135 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003136 (pixman_region32_not_empty(&state->damage_surface) ||
3137 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003138 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003139
Pekka Paalanen8e159182012-10-10 12:49:25 +03003140 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003141 &state->damage_surface);
3142
3143 apply_damage_buffer(&surface->damage, surface, state);
3144
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003145 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003146 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003147 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003148
3149 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003150 pixman_region32_init(&opaque);
3151 pixman_region32_intersect_rect(&opaque, &state->opaque,
3152 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003153
3154 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3155 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003156 wl_list_for_each(view, &surface->views, surface_link)
3157 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003158 }
3159
3160 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003161
3162 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003163 pixman_region32_intersect_rect(&surface->input, &state->input,
3164 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003165
Pekka Paalanenbc106382012-10-10 12:49:31 +03003166 /* wl_surface.frame */
3167 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003168 &state->frame_callback_list);
3169 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003170
3171 /* XXX:
3172 * What should happen with a feedback request, if there
3173 * is no wl_buffer attached for this commit?
3174 */
3175
3176 /* presentation.feedback */
3177 wl_list_insert_list(&surface->feedback_list,
3178 &state->feedback_list);
3179 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003180
3181 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003182}
3183
3184static void
3185weston_surface_commit(struct weston_surface *surface)
3186{
3187 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003188
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189 weston_surface_commit_subsurface_order(surface);
3190
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003191 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003192}
3193
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003194static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003195weston_subsurface_commit(struct weston_subsurface *sub);
3196
3197static void
3198weston_subsurface_parent_commit(struct weston_subsurface *sub,
3199 int parent_is_synchronized);
3200
3201static void
3202surface_commit(struct wl_client *client, struct wl_resource *resource)
3203{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003204 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003205 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3206
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003207 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3208 assert(surface->viewport_resource);
3209
3210 wl_resource_post_error(surface->viewport_resource,
3211 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3212 "wl_surface@%d has viewport source outside buffer",
3213 wl_resource_get_id(resource));
3214 return;
3215 }
3216
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003217 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3218 assert(surface->viewport_resource);
3219
3220 wl_resource_post_error(surface->viewport_resource,
3221 WP_VIEWPORT_ERROR_BAD_SIZE,
3222 "wl_surface@%d viewport dst size not integer",
3223 wl_resource_get_id(resource));
3224 return;
3225 }
3226
Pekka Paalanene67858b2013-04-25 13:57:42 +03003227 if (sub) {
3228 weston_subsurface_commit(sub);
3229 return;
3230 }
3231
3232 weston_surface_commit(surface);
3233
3234 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3235 if (sub->surface != surface)
3236 weston_subsurface_parent_commit(sub, 0);
3237 }
3238}
3239
3240static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003241surface_set_buffer_transform(struct wl_client *client,
3242 struct wl_resource *resource, int transform)
3243{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003244 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003245
Jonny Lamba55f1392014-05-30 12:07:15 +02003246 /* if wl_output.transform grows more members this will need to be updated. */
3247 if (transform < 0 ||
3248 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3249 wl_resource_post_error(resource,
3250 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3251 "buffer transform must be a valid transform "
3252 "('%d' specified)", transform);
3253 return;
3254 }
3255
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003256 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003257 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003258}
3259
Alexander Larsson4ea95522013-05-22 14:41:37 +02003260static void
3261surface_set_buffer_scale(struct wl_client *client,
3262 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003263 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003264{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003265 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003266
Jonny Lamba55f1392014-05-30 12:07:15 +02003267 if (scale < 1) {
3268 wl_resource_post_error(resource,
3269 WL_SURFACE_ERROR_INVALID_SCALE,
3270 "buffer scale must be at least one "
3271 "('%d' specified)", scale);
3272 return;
3273 }
3274
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003275 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003276 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003277}
3278
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003279static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003280 surface_destroy,
3281 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003282 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003283 surface_frame,
3284 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003285 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003286 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003287 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003288 surface_set_buffer_scale,
3289 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003290};
3291
3292static void
3293compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003294 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003295{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003296 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003297 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003298
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003299 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003300 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003301 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003302 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003303 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003304
Jason Ekstranda85118c2013-06-27 20:17:02 -05003305 surface->resource =
3306 wl_resource_create(client, &wl_surface_interface,
3307 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003308 if (surface->resource == NULL) {
3309 weston_surface_destroy(surface);
3310 wl_resource_post_no_memory(resource);
3311 return;
3312 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003313 wl_resource_set_implementation(surface->resource, &surface_interface,
3314 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003315
3316 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003317}
3318
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003319static void
3320destroy_region(struct wl_resource *resource)
3321{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003322 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003323
3324 pixman_region32_fini(&region->region);
3325 free(region);
3326}
3327
3328static void
3329region_destroy(struct wl_client *client, struct wl_resource *resource)
3330{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003331 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003332}
3333
3334static void
3335region_add(struct wl_client *client, struct wl_resource *resource,
3336 int32_t x, int32_t y, int32_t width, int32_t height)
3337{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003338 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003339
3340 pixman_region32_union_rect(&region->region, &region->region,
3341 x, y, width, height);
3342}
3343
3344static void
3345region_subtract(struct wl_client *client, struct wl_resource *resource,
3346 int32_t x, int32_t y, int32_t width, int32_t height)
3347{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003348 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003349 pixman_region32_t rect;
3350
3351 pixman_region32_init_rect(&rect, x, y, width, height);
3352 pixman_region32_subtract(&region->region, &region->region, &rect);
3353 pixman_region32_fini(&rect);
3354}
3355
3356static const struct wl_region_interface region_interface = {
3357 region_destroy,
3358 region_add,
3359 region_subtract
3360};
3361
3362static void
3363compositor_create_region(struct wl_client *client,
3364 struct wl_resource *resource, uint32_t id)
3365{
3366 struct weston_region *region;
3367
3368 region = malloc(sizeof *region);
3369 if (region == NULL) {
3370 wl_resource_post_no_memory(resource);
3371 return;
3372 }
3373
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003374 pixman_region32_init(&region->region);
3375
Jason Ekstranda85118c2013-06-27 20:17:02 -05003376 region->resource =
3377 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003378 if (region->resource == NULL) {
3379 free(region);
3380 wl_resource_post_no_memory(resource);
3381 return;
3382 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003383 wl_resource_set_implementation(region->resource, &region_interface,
3384 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003385}
3386
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003387static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003388 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003389 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003390};
3391
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003392static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003393weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3394{
3395 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003396
Jason Ekstrand7b982072014-05-20 14:33:03 -05003397 weston_surface_commit_state(surface, &sub->cached);
3398 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003399
3400 weston_surface_commit_subsurface_order(surface);
3401
3402 weston_surface_schedule_repaint(surface);
3403
Jason Ekstrand7b982072014-05-20 14:33:03 -05003404 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003405}
3406
3407static void
3408weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3409{
3410 struct weston_surface *surface = sub->surface;
3411
3412 /*
3413 * If this commit would cause the surface to move by the
3414 * attach(dx, dy) parameters, the old damage region must be
3415 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003416 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003417 */
Derek Foreman152254b2015-11-26 14:17:48 -06003418 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003419 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003420 pixman_region32_union(&sub->cached.damage_surface,
3421 &sub->cached.damage_surface,
3422 &surface->pending.damage_surface);
3423 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003424
3425 if (surface->pending.newly_attached) {
3426 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003427 weston_surface_state_set_buffer(&sub->cached,
3428 surface->pending.buffer);
3429 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003430 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003431 weston_presentation_feedback_discard_list(
3432 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003433 }
3434 sub->cached.sx += surface->pending.sx;
3435 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003436
Derek Foreman152254b2015-11-26 14:17:48 -06003437 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3438
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003439 sub->cached.buffer_viewport.changed |=
3440 surface->pending.buffer_viewport.changed;
3441 sub->cached.buffer_viewport.buffer =
3442 surface->pending.buffer_viewport.buffer;
3443 sub->cached.buffer_viewport.surface =
3444 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003445
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003446 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003447
3448 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3449
3450 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3451
3452 wl_list_insert_list(&sub->cached.frame_callback_list,
3453 &surface->pending.frame_callback_list);
3454 wl_list_init(&surface->pending.frame_callback_list);
3455
Pekka Paalanen133e4392014-09-23 22:08:46 -04003456 wl_list_insert_list(&sub->cached.feedback_list,
3457 &surface->pending.feedback_list);
3458 wl_list_init(&surface->pending.feedback_list);
3459
Jason Ekstrand7b982072014-05-20 14:33:03 -05003460 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003461}
3462
Derek Foreman280e7dd2014-10-03 13:13:42 -05003463static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003464weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3465{
3466 while (sub) {
3467 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003468 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003469
3470 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003471 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003472
3473 sub = weston_surface_to_subsurface(sub->parent);
3474 }
3475
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003476 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003477}
3478
3479static void
3480weston_subsurface_commit(struct weston_subsurface *sub)
3481{
3482 struct weston_surface *surface = sub->surface;
3483 struct weston_subsurface *tmp;
3484
3485 /* Recursive check for effectively synchronized. */
3486 if (weston_subsurface_is_synchronized(sub)) {
3487 weston_subsurface_commit_to_cache(sub);
3488 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003489 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003490 /* flush accumulated state from cache */
3491 weston_subsurface_commit_to_cache(sub);
3492 weston_subsurface_commit_from_cache(sub);
3493 } else {
3494 weston_surface_commit(surface);
3495 }
3496
3497 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3498 if (tmp->surface != surface)
3499 weston_subsurface_parent_commit(tmp, 0);
3500 }
3501 }
3502}
3503
3504static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003505weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003506{
3507 struct weston_surface *surface = sub->surface;
3508 struct weston_subsurface *tmp;
3509
Pekka Paalanene67858b2013-04-25 13:57:42 +03003510 /* From now on, commit_from_cache the whole sub-tree, regardless of
3511 * the synchronized mode of each child. This sub-surface or some
3512 * of its ancestors were synchronized, so we are synchronized
3513 * all the way down.
3514 */
3515
Jason Ekstrand7b982072014-05-20 14:33:03 -05003516 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003517 weston_subsurface_commit_from_cache(sub);
3518
3519 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3520 if (tmp->surface != surface)
3521 weston_subsurface_parent_commit(tmp, 1);
3522 }
3523}
3524
3525static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003526weston_subsurface_parent_commit(struct weston_subsurface *sub,
3527 int parent_is_synchronized)
3528{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003529 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003530 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003531 wl_list_for_each(view, &sub->surface->views, surface_link)
3532 weston_view_set_position(view,
3533 sub->position.x,
3534 sub->position.y);
3535
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003536 sub->position.set = 0;
3537 }
3538
3539 if (parent_is_synchronized || sub->synchronized)
3540 weston_subsurface_synchronized_commit(sub);
3541}
3542
Pekka Paalanen8274d902014-08-06 19:36:51 +03003543static int
3544subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3545{
3546 return snprintf(buf, len, "sub-surface");
3547}
3548
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003549static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003550subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003551{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003552 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003553
Jason Ekstranda7af7042013-10-12 22:38:11 -05003554 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003555 weston_view_set_position(view,
3556 view->geometry.x + dx,
3557 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003558
3559 /* No need to check parent mappedness, because if parent is not
3560 * mapped, parent is not in a visible layer, so this sub-surface
3561 * will not be drawn either.
3562 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003563
Pekka Paalanene67858b2013-04-25 13:57:42 +03003564 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003565 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003566
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003567 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003568 * because that would call it also for the parent surface,
3569 * which might not be mapped yet. That would lead to
3570 * inconsistent state, where the window could never be
3571 * mapped.
3572 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003573 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003574 * weston_surface_is_mapped() return true, so that when the
3575 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003576 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003577 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003578 }
3579}
3580
3581static struct weston_subsurface *
3582weston_surface_to_subsurface(struct weston_surface *surface)
3583{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003584 if (surface->committed == subsurface_committed)
3585 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003586
3587 return NULL;
3588}
3589
Pekka Paalanen01388e22013-04-25 13:57:44 +03003590WL_EXPORT struct weston_surface *
3591weston_surface_get_main_surface(struct weston_surface *surface)
3592{
3593 struct weston_subsurface *sub;
3594
3595 while (surface && (sub = weston_surface_to_subsurface(surface)))
3596 surface = sub->parent;
3597
3598 return surface;
3599}
3600
Pekka Paalanen50b67472014-10-01 15:02:41 +03003601WL_EXPORT int
3602weston_surface_set_role(struct weston_surface *surface,
3603 const char *role_name,
3604 struct wl_resource *error_resource,
3605 uint32_t error_code)
3606{
3607 assert(role_name);
3608
3609 if (surface->role_name == NULL ||
3610 surface->role_name == role_name ||
3611 strcmp(surface->role_name, role_name) == 0) {
3612 surface->role_name = role_name;
3613
3614 return 0;
3615 }
3616
3617 wl_resource_post_error(error_resource, error_code,
3618 "Cannot assign role %s to wl_surface@%d,"
3619 " already has role %s\n",
3620 role_name,
3621 wl_resource_get_id(surface->resource),
3622 surface->role_name);
3623 return -1;
3624}
3625
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003626WL_EXPORT const char *
3627weston_surface_get_role(struct weston_surface *surface)
3628{
3629 return surface->role_name;
3630}
3631
Pekka Paalanen8274d902014-08-06 19:36:51 +03003632WL_EXPORT void
3633weston_surface_set_label_func(struct weston_surface *surface,
3634 int (*desc)(struct weston_surface *,
3635 char *, size_t))
3636{
3637 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003638 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003639}
3640
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003641/** Get the size of surface contents
3642 *
3643 * \param surface The surface to query.
3644 * \param width Returns the width of raw contents.
3645 * \param height Returns the height of raw contents.
3646 *
3647 * Retrieves the raw surface content size in pixels for the given surface.
3648 * This is the whole content size in buffer pixels. If the surface
3649 * has no content or the renderer does not implement this feature,
3650 * zeroes are returned.
3651 *
3652 * This function is used to determine the buffer size needed for
3653 * a weston_surface_copy_content() call.
3654 */
3655WL_EXPORT void
3656weston_surface_get_content_size(struct weston_surface *surface,
3657 int *width, int *height)
3658{
3659 struct weston_renderer *rer = surface->compositor->renderer;
3660
3661 if (!rer->surface_get_content_size) {
3662 *width = 0;
3663 *height = 0;
3664 return;
3665 }
3666
3667 rer->surface_get_content_size(surface, width, height);
3668}
3669
Quentin Glidic248dd102016-08-12 10:41:34 +02003670/** Get the bounding box of a surface and its subsurfaces
3671 *
3672 * \param surface The surface to query.
3673 * \return The bounding box relative to the surface origin.
3674 *
3675 */
3676WL_EXPORT struct weston_geometry
3677weston_surface_get_bounding_box(struct weston_surface *surface)
3678{
3679 pixman_region32_t region;
3680 pixman_box32_t *box;
3681 struct weston_subsurface *subsurface;
3682
3683 pixman_region32_init_rect(&region,
3684 0, 0,
3685 surface->width, surface->height);
3686
3687 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3688 pixman_region32_union_rect(&region, &region,
3689 subsurface->position.x,
3690 subsurface->position.y,
3691 subsurface->surface->width,
3692 subsurface->surface->height);
3693
3694 box = pixman_region32_extents(&region);
3695 struct weston_geometry geometry = {
3696 .x = box->x1,
3697 .y = box->y1,
3698 .width = box->x2 - box->x1,
3699 .height = box->y2 - box->y1,
3700 };
3701
3702 pixman_region32_fini(&region);
3703
3704 return geometry;
3705}
3706
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003707/** Copy surface contents to system memory.
3708 *
3709 * \param surface The surface to copy from.
3710 * \param target Pointer to the target memory buffer.
3711 * \param size Size of the target buffer in bytes.
3712 * \param src_x X location on contents to copy from.
3713 * \param src_y Y location on contents to copy from.
3714 * \param width Width in pixels of the area to copy.
3715 * \param height Height in pixels of the area to copy.
3716 * \return 0 for success, -1 for failure.
3717 *
3718 * Surface contents are maintained by the renderer. They can be in a
3719 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3720 * else.
3721 *
3722 * Surface contents are copied into memory pointed to by target,
3723 * which has size bytes of space available. The target memory
3724 * may be larger than needed, but being smaller returns an error.
3725 * The extra bytes in target may or may not be written; their content is
3726 * unspecified. Size must be large enough to hold the image.
3727 *
3728 * The image in the target memory will be arranged in rows from
3729 * top to bottom, and pixels on a row from left to right. The pixel
3730 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3731 * width * 4.
3732 *
3733 * Parameters src_x and src_y define the upper-left corner in buffer
3734 * coordinates (pixels) to copy from. Parameters width and height
3735 * define the size of the area to copy in pixels.
3736 *
3737 * The rectangle defined by src_x, src_y, width, height must fit in
3738 * the surface contents. Otherwise an error is returned.
3739 *
3740 * Use surface_get_data_size to determine the content size; the
3741 * needed target buffer size and rectangle limits.
3742 *
3743 * CURRENT IMPLEMENTATION RESTRICTIONS:
3744 * - the machine must be little-endian due to Pixman formats.
3745 *
3746 * NOTE: Pixman formats are premultiplied.
3747 */
3748WL_EXPORT int
3749weston_surface_copy_content(struct weston_surface *surface,
3750 void *target, size_t size,
3751 int src_x, int src_y,
3752 int width, int height)
3753{
3754 struct weston_renderer *rer = surface->compositor->renderer;
3755 int cw, ch;
3756 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3757
3758 if (!rer->surface_copy_content)
3759 return -1;
3760
3761 weston_surface_get_content_size(surface, &cw, &ch);
3762
3763 if (src_x < 0 || src_y < 0)
3764 return -1;
3765
3766 if (width <= 0 || height <= 0)
3767 return -1;
3768
3769 if (src_x + width > cw || src_y + height > ch)
3770 return -1;
3771
3772 if (width * bytespp * height > size)
3773 return -1;
3774
3775 return rer->surface_copy_content(surface, target, size,
3776 src_x, src_y, width, height);
3777}
3778
Pekka Paalanene67858b2013-04-25 13:57:42 +03003779static void
3780subsurface_set_position(struct wl_client *client,
3781 struct wl_resource *resource, int32_t x, int32_t y)
3782{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003783 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003784
3785 if (!sub)
3786 return;
3787
3788 sub->position.x = x;
3789 sub->position.y = y;
3790 sub->position.set = 1;
3791}
3792
3793static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003794subsurface_find_sibling(struct weston_subsurface *sub,
3795 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003796{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003797 struct weston_surface *parent = sub->parent;
3798 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003799
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003800 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3801 if (sibling->surface == surface && sibling != sub)
3802 return sibling;
3803 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003804
3805 return NULL;
3806}
3807
3808static struct weston_subsurface *
3809subsurface_sibling_check(struct weston_subsurface *sub,
3810 struct weston_surface *surface,
3811 const char *request)
3812{
3813 struct weston_subsurface *sibling;
3814
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003815 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003816 if (!sibling) {
3817 wl_resource_post_error(sub->resource,
3818 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3819 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003820 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003821 return NULL;
3822 }
3823
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003824 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003825
3826 return sibling;
3827}
3828
3829static void
3830subsurface_place_above(struct wl_client *client,
3831 struct wl_resource *resource,
3832 struct wl_resource *sibling_resource)
3833{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003834 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003835 struct weston_surface *surface =
3836 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003837 struct weston_subsurface *sibling;
3838
3839 if (!sub)
3840 return;
3841
3842 sibling = subsurface_sibling_check(sub, surface, "place_above");
3843 if (!sibling)
3844 return;
3845
3846 wl_list_remove(&sub->parent_link_pending);
3847 wl_list_insert(sibling->parent_link_pending.prev,
3848 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003849
3850 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003851}
3852
3853static void
3854subsurface_place_below(struct wl_client *client,
3855 struct wl_resource *resource,
3856 struct wl_resource *sibling_resource)
3857{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003858 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003859 struct weston_surface *surface =
3860 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003861 struct weston_subsurface *sibling;
3862
3863 if (!sub)
3864 return;
3865
3866 sibling = subsurface_sibling_check(sub, surface, "place_below");
3867 if (!sibling)
3868 return;
3869
3870 wl_list_remove(&sub->parent_link_pending);
3871 wl_list_insert(&sibling->parent_link_pending,
3872 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003873
3874 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003875}
3876
3877static void
3878subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3879{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003880 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003881
3882 if (sub)
3883 sub->synchronized = 1;
3884}
3885
3886static void
3887subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3888{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003889 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003890
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003891 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003892 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003893
3894 /* If sub became effectively desynchronized, flush. */
3895 if (!weston_subsurface_is_synchronized(sub))
3896 weston_subsurface_synchronized_commit(sub);
3897 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003898}
3899
3900static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003901weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3902{
3903 wl_list_remove(&sub->parent_link);
3904 wl_list_remove(&sub->parent_link_pending);
3905 wl_list_remove(&sub->parent_destroy_listener.link);
3906 sub->parent = NULL;
3907}
3908
3909static void
3910weston_subsurface_destroy(struct weston_subsurface *sub);
3911
3912static void
3913subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3914{
3915 struct weston_subsurface *sub =
3916 container_of(listener, struct weston_subsurface,
3917 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003918 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003919
3920 /* The protocol object (wl_resource) is left inert. */
3921 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003922 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003923
3924 weston_subsurface_destroy(sub);
3925}
3926
3927static void
3928subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3929{
3930 struct weston_subsurface *sub =
3931 container_of(listener, struct weston_subsurface,
3932 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003933 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003934 assert(sub->surface != sub->parent);
3935
3936 if (weston_surface_is_mapped(sub->surface))
3937 weston_surface_unmap(sub->surface);
3938
3939 weston_subsurface_unlink_parent(sub);
3940}
3941
3942static void
3943subsurface_resource_destroy(struct wl_resource *resource)
3944{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003945 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003946
3947 if (sub)
3948 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003949}
3950
3951static void
3952subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3953{
3954 wl_resource_destroy(resource);
3955}
3956
3957static void
3958weston_subsurface_link_parent(struct weston_subsurface *sub,
3959 struct weston_surface *parent)
3960{
3961 sub->parent = parent;
3962 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003963 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003964 &sub->parent_destroy_listener);
3965
3966 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3967 wl_list_insert(&parent->subsurface_list_pending,
3968 &sub->parent_link_pending);
3969}
3970
3971static void
3972weston_subsurface_link_surface(struct weston_subsurface *sub,
3973 struct weston_surface *surface)
3974{
3975 sub->surface = surface;
3976 sub->surface_destroy_listener.notify =
3977 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003978 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003979 &sub->surface_destroy_listener);
3980}
3981
3982static void
3983weston_subsurface_destroy(struct weston_subsurface *sub)
3984{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003985 struct weston_view *view, *next;
3986
Pekka Paalanene67858b2013-04-25 13:57:42 +03003987 assert(sub->surface);
3988
3989 if (sub->resource) {
3990 assert(weston_surface_to_subsurface(sub->surface) == sub);
3991 assert(sub->parent_destroy_listener.notify ==
3992 subsurface_handle_parent_destroy);
3993
George Kiagiadakised04d382014-06-13 18:10:26 +02003994 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3995 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003996 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003997 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003998
Pekka Paalanene67858b2013-04-25 13:57:42 +03003999 if (sub->parent)
4000 weston_subsurface_unlink_parent(sub);
4001
Jason Ekstrand7b982072014-05-20 14:33:03 -05004002 weston_surface_state_fini(&sub->cached);
4003 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004004
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004005 sub->surface->committed = NULL;
4006 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004007 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004008 } else {
4009 /* the dummy weston_subsurface for the parent itself */
4010 assert(sub->parent_destroy_listener.notify == NULL);
4011 wl_list_remove(&sub->parent_link);
4012 wl_list_remove(&sub->parent_link_pending);
4013 }
4014
4015 wl_list_remove(&sub->surface_destroy_listener.link);
4016 free(sub);
4017}
4018
4019static const struct wl_subsurface_interface subsurface_implementation = {
4020 subsurface_destroy,
4021 subsurface_set_position,
4022 subsurface_place_above,
4023 subsurface_place_below,
4024 subsurface_set_sync,
4025 subsurface_set_desync
4026};
4027
4028static struct weston_subsurface *
4029weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4030 struct weston_surface *parent)
4031{
4032 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004033 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004034
Bryce Harringtonde16d892014-11-20 22:21:57 -08004035 sub = zalloc(sizeof *sub);
4036 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004037 return NULL;
4038
Jason Ekstranda7af7042013-10-12 22:38:11 -05004039 wl_list_init(&sub->unused_views);
4040
Jason Ekstranda85118c2013-06-27 20:17:02 -05004041 sub->resource =
4042 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004043 if (!sub->resource) {
4044 free(sub);
4045 return NULL;
4046 }
4047
Jason Ekstranda85118c2013-06-27 20:17:02 -05004048 wl_resource_set_implementation(sub->resource,
4049 &subsurface_implementation,
4050 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004051 weston_subsurface_link_surface(sub, surface);
4052 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004053 weston_surface_state_init(&sub->cached);
4054 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004055 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004056
4057 return sub;
4058}
4059
4060/* Create a dummy subsurface for having the parent itself in its
4061 * sub-surface lists. Makes stacking order manipulation easy.
4062 */
4063static struct weston_subsurface *
4064weston_subsurface_create_for_parent(struct weston_surface *parent)
4065{
4066 struct weston_subsurface *sub;
4067
Bryce Harringtonde16d892014-11-20 22:21:57 -08004068 sub = zalloc(sizeof *sub);
4069 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004070 return NULL;
4071
4072 weston_subsurface_link_surface(sub, parent);
4073 sub->parent = parent;
4074 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4075 wl_list_insert(&parent->subsurface_list_pending,
4076 &sub->parent_link_pending);
4077
4078 return sub;
4079}
4080
4081static void
4082subcompositor_get_subsurface(struct wl_client *client,
4083 struct wl_resource *resource,
4084 uint32_t id,
4085 struct wl_resource *surface_resource,
4086 struct wl_resource *parent_resource)
4087{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004088 struct weston_surface *surface =
4089 wl_resource_get_user_data(surface_resource);
4090 struct weston_surface *parent =
4091 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004092 struct weston_subsurface *sub;
4093 static const char where[] = "get_subsurface: wl_subsurface@";
4094
4095 if (surface == parent) {
4096 wl_resource_post_error(resource,
4097 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4098 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004099 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004100 return;
4101 }
4102
4103 if (weston_surface_to_subsurface(surface)) {
4104 wl_resource_post_error(resource,
4105 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4106 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004107 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004108 return;
4109 }
4110
Pekka Paalanen50b67472014-10-01 15:02:41 +03004111 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4112 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004113 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004114
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004115 if (weston_surface_get_main_surface(parent) == surface) {
4116 wl_resource_post_error(resource,
4117 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4118 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004119 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004120 return;
4121 }
4122
Pekka Paalanene67858b2013-04-25 13:57:42 +03004123 /* make sure the parent is in its own list */
4124 if (wl_list_empty(&parent->subsurface_list)) {
4125 if (!weston_subsurface_create_for_parent(parent)) {
4126 wl_resource_post_no_memory(resource);
4127 return;
4128 }
4129 }
4130
4131 sub = weston_subsurface_create(id, surface, parent);
4132 if (!sub) {
4133 wl_resource_post_no_memory(resource);
4134 return;
4135 }
4136
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004137 surface->committed = subsurface_committed;
4138 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004139 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004140}
4141
4142static void
4143subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4144{
4145 wl_resource_destroy(resource);
4146}
4147
4148static const struct wl_subcompositor_interface subcompositor_interface = {
4149 subcompositor_destroy,
4150 subcompositor_get_subsurface
4151};
4152
4153static void
4154bind_subcompositor(struct wl_client *client,
4155 void *data, uint32_t version, uint32_t id)
4156{
4157 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004158 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004159
Jason Ekstranda85118c2013-06-27 20:17:02 -05004160 resource =
4161 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004162 if (resource == NULL) {
4163 wl_client_post_no_memory(client);
4164 return;
4165 }
4166 wl_resource_set_implementation(resource, &subcompositor_interface,
4167 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004168}
4169
Bryce Harrington0795ece2016-08-30 12:04:26 -07004170/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004171 *
4172 * \param compositor The compositor instance
4173 * \param state The DPMS state the outputs will be set to
4174 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004175static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004176weston_compositor_dpms(struct weston_compositor *compositor,
4177 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004178{
4179 struct weston_output *output;
4180
Bryce Harrington08976ac2016-08-30 12:05:16 -07004181 wl_list_for_each(output, &compositor->output_list, link)
4182 if (output->set_dpms)
4183 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004184}
4185
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004186/** Restores the compositor to active status
4187 *
4188 * \param compositor The compositor instance
4189 *
4190 * If the compositor was in a sleeping mode, all outputs are powered
4191 * back on via DPMS. Otherwise if the compositor was inactive
4192 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4193 * signal will fire.
4194 *
4195 * Restarts the idle timer.
4196 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004197WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004198weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004199{
Neil Roberts8b62e202013-09-30 13:14:47 +01004200 uint32_t old_state = compositor->state;
4201
4202 /* The state needs to be changed before emitting the wake
4203 * signal because that may try to schedule a repaint which
4204 * will not work if the compositor is still sleeping */
4205 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4206
4207 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004208 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004209 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004210 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004211 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004212 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004213 /* fall through */
4214 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004215 wl_event_source_timer_update(compositor->idle_source,
4216 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004217 }
4218}
4219
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004220/** Turns off rendering and frame events for the compositor.
4221 *
4222 * \param compositor The compositor instance
4223 *
4224 * This is used for example to prevent further rendering while the
4225 * compositor is shutting down.
4226 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004227 * Stops the idle timer.
4228 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004229WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004230weston_compositor_offscreen(struct weston_compositor *compositor)
4231{
4232 switch (compositor->state) {
4233 case WESTON_COMPOSITOR_OFFSCREEN:
4234 return;
4235 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004236 default:
4237 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4238 wl_event_source_timer_update(compositor->idle_source, 0);
4239 }
4240}
4241
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004242/** Powers down all attached output devices
4243 *
4244 * \param compositor The compositor instance
4245 *
4246 * Causes rendering to the outputs to cease, and no frame events to be
4247 * sent. Only powers down the outputs if the compositor is not already
4248 * in sleep mode.
4249 *
4250 * Stops the idle timer.
4251 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004252WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004253weston_compositor_sleep(struct weston_compositor *compositor)
4254{
4255 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4256 return;
4257
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004258 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004259 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4260 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4261}
4262
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004263/** Sets compositor to idle mode
4264 *
4265 * \param data The compositor instance
4266 *
4267 * This is called when the idle timer fires. Once the compositor is in
4268 * idle mode it requires a wake action (e.g. via
4269 * weston_compositor_wake()) to restore it. The compositor's
4270 * idle_signal will be triggered when the idle event occurs.
4271 *
4272 * Idleness can be inhibited by setting the compositor's idle_inhibit
4273 * property.
4274 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004275static int
4276idle_handler(void *data)
4277{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004278 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004279
4280 if (compositor->idle_inhibit)
4281 return 1;
4282
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004283 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004284 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004285
4286 return 1;
4287}
4288
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004289WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004290weston_plane_init(struct weston_plane *plane,
4291 struct weston_compositor *ec,
4292 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004293{
4294 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004295 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004296 plane->x = x;
4297 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004298 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004299
4300 /* Init the link so that the call to wl_list_remove() when releasing
4301 * the plane without ever stacking doesn't lead to a crash */
4302 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004303}
4304
4305WL_EXPORT void
4306weston_plane_release(struct weston_plane *plane)
4307{
Xiong Zhang97116532013-10-23 13:58:31 +08004308 struct weston_view *view;
4309
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004310 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004311 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004312
Xiong Zhang97116532013-10-23 13:58:31 +08004313 wl_list_for_each(view, &plane->compositor->view_list, link) {
4314 if (view->plane == plane)
4315 view->plane = NULL;
4316 }
4317
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004318 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004319}
4320
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004321WL_EXPORT void
4322weston_compositor_stack_plane(struct weston_compositor *ec,
4323 struct weston_plane *plane,
4324 struct weston_plane *above)
4325{
4326 if (above)
4327 wl_list_insert(above->link.prev, &plane->link);
4328 else
4329 wl_list_insert(&ec->plane_list, &plane->link);
4330}
4331
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004332static void
4333output_release(struct wl_client *client, struct wl_resource *resource)
4334{
4335 wl_resource_destroy(resource);
4336}
4337
4338static const struct wl_output_interface output_interface = {
4339 output_release,
4340};
4341
4342
Casey Dahlin9074db52012-04-19 22:50:09 -04004343static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004344{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004345 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004346}
4347
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004348static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004349bind_output(struct wl_client *client,
4350 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004351{
Pekka Paalanen05347622017-03-27 12:24:34 +03004352 struct weston_head *head = data;
4353 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004354 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004355 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004356
Jason Ekstranda85118c2013-06-27 20:17:02 -05004357 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004358 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004359 if (resource == NULL) {
4360 wl_client_post_no_memory(client);
4361 return;
4362 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004363
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004364 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004365 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004366 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004367
Pekka Paalanen05347622017-03-27 12:24:34 +03004368 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004369 wl_output_send_geometry(resource,
4370 output->x,
4371 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004372 head->mm_width,
4373 head->mm_height,
4374 head->subpixel,
4375 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004376 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004377 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004378 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004379 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004380
4381 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004382 wl_output_send_mode(resource,
4383 mode->flags,
4384 mode->width,
4385 mode->height,
4386 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004387 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004388
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004389 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004390 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004391}
4392
Pekka Paalanendcac3512017-12-08 14:13:34 +02004393static void
4394weston_head_add_global(struct weston_head *head)
4395{
4396 head->global = wl_global_create(head->compositor->wl_display,
4397 &wl_output_interface, 3,
4398 head, bind_output);
4399}
4400
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004401/** Remove the global wl_output protocol object
4402 *
4403 * \param head The head whose global to remove.
4404 *
4405 * Also orphans the wl_resources for this head (wl_output).
4406 */
4407static void
4408weston_head_remove_global(struct weston_head *head)
4409{
4410 struct wl_resource *resource, *tmp;
4411
4412 if (head->global)
4413 wl_global_destroy(head->global);
4414 head->global = NULL;
4415
4416 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4417 unbind_resource(resource);
4418 wl_resource_set_destructor(resource, NULL);
4419 wl_resource_set_user_data(resource, NULL);
4420 }
4421}
4422
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004423/** Get the backing object of wl_output
4424 *
4425 * \param resource A wl_output protocol object.
4426 * \return The backing object (user data) of a wl_resource representing a
4427 * wl_output protocol object.
4428 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004429WL_EXPORT struct weston_head *
4430weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004431{
4432 assert(wl_resource_instance_of(resource, &wl_output_interface,
4433 &output_interface));
4434
4435 return wl_resource_get_user_data(resource);
4436}
4437
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004438/** Initialize a pre-allocated weston_head
4439 *
4440 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004441 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004442 *
4443 * The head will be safe to attach, detach and release.
4444 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004445 * The name is used in logs, and can be used by compositors as a configuration
4446 * identifier.
4447 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004448 * \memberof weston_head
4449 * \internal
4450 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004451WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004452weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004453{
4454 /* Add some (in)sane defaults which can be used
4455 * for checking if an output was properly configured
4456 */
4457 memset(head, 0, sizeof *head);
4458
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004459 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004460 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004461 wl_list_init(&head->output_link);
4462 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004463 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004464}
4465
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004466/** Send output heads changed signal
4467 *
4468 * \param output The output that changed.
4469 *
4470 * Notify that the enabled output gained and/or lost heads, or that the
4471 * associated heads may have changed their connection status. This does not
4472 * include cases where the output becomes enabled or disabled. The registered
4473 * callbacks are called after the change has successfully happened.
4474 *
4475 * If connection status change causes the compositor to attach or detach a head
4476 * to an enabled output, the registered callbacks may be called multiple times.
4477 */
4478static void
4479weston_output_emit_heads_changed(struct weston_output *output)
4480{
4481 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4482 output);
4483}
4484
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004485/** Idle task for emitting heads_changed_signal */
4486static void
4487weston_compositor_call_heads_changed(void *data)
4488{
4489 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004490 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004491
4492 compositor->heads_changed_source = NULL;
4493
4494 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004495
4496 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4497 if (head->output && head->output->enabled)
4498 weston_output_emit_heads_changed(head->output);
4499 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004500}
4501
4502/** Schedule a call on idle to heads_changed callback
4503 *
4504 * \param compositor The Compositor.
4505 *
4506 * \memberof weston_compositor
4507 * \internal
4508 */
4509static void
4510weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4511{
4512 struct wl_event_loop *loop;
4513
4514 if (compositor->heads_changed_source)
4515 return;
4516
4517 loop = wl_display_get_event_loop(compositor->wl_display);
4518 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4519 weston_compositor_call_heads_changed, compositor);
4520}
4521
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004522/** Register a new head
4523 *
4524 * \param compositor The compositor.
4525 * \param head The head to register, must not be already registered.
4526 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004527 * This signals the core that a new head has become available, leading to
4528 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004529 *
4530 * \memberof weston_compositor
4531 * \internal
4532 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004533WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004534weston_compositor_add_head(struct weston_compositor *compositor,
4535 struct weston_head *head)
4536{
4537 assert(wl_list_empty(&head->compositor_link));
4538 assert(head->name);
4539
4540 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4541 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004542 weston_compositor_schedule_heads_changed(compositor);
4543}
4544
4545/** Adds a listener to be called when heads change
4546 *
4547 * \param compositor The compositor.
4548 * \param listener The listener to add.
4549 *
4550 * The listener notify function argument is the \var compositor.
4551 *
4552 * The listener function will be called after heads are added or their
4553 * connection status has changed. Several changes may be accumulated into a
4554 * single call. The user is expected to iterate over the existing heads and
4555 * check their statuses to find out what changed.
4556 *
4557 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4558 * weston_head_is_enabled
4559 * \memberof weston_compositor
4560 */
4561WL_EXPORT void
4562weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4563 struct wl_listener *listener)
4564{
4565 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004566}
4567
4568/** Iterate over available heads
4569 *
4570 * \param compositor The compositor.
4571 * \param item The iterator, or NULL for start.
4572 * \return The next available head in the list.
4573 *
4574 * Returns all available heads, regardless of being connected or enabled.
4575 *
4576 * You can iterate over all heads as follows:
4577 * \code
4578 * struct weston_head *head = NULL;
4579 *
4580 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4581 * ...
4582 * }
4583 * \endcode
4584 *
4585 * If you cause \c iter to be removed from the list, you cannot use it to
4586 * continue iterating. Removing any other item is safe.
4587 *
4588 * \memberof weston_compositor
4589 */
4590WL_EXPORT struct weston_head *
4591weston_compositor_iterate_heads(struct weston_compositor *compositor,
4592 struct weston_head *iter)
4593{
4594 struct wl_list *list = &compositor->head_list;
4595 struct wl_list *node;
4596
4597 assert(compositor);
4598 assert(!iter || iter->compositor == compositor);
4599
4600 if (iter)
4601 node = iter->compositor_link.next;
4602 else
4603 node = list->next;
4604
4605 assert(node);
4606 assert(!iter || node != &iter->compositor_link);
4607
4608 if (node == list)
4609 return NULL;
4610
4611 return container_of(node, struct weston_head, compositor_link);
4612}
4613
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004614/** Iterate over attached heads
4615 *
4616 * \param output The output whose heads to iterate.
4617 * \param item The iterator, or NULL for start.
4618 * \return The next attached head in the list.
4619 *
4620 * Returns all heads currently attached to the output.
4621 *
4622 * You can iterate over all heads as follows:
4623 * \code
4624 * struct weston_head *head = NULL;
4625 *
4626 * while ((head = weston_output_iterate_heads(output, head))) {
4627 * ...
4628 * }
4629 * \endcode
4630 *
4631 * If you cause \c iter to be removed from the list, you cannot use it to
4632 * continue iterating. Removing any other item is safe.
4633 *
4634 * \memberof weston_compositor
4635 */
4636WL_EXPORT struct weston_head *
4637weston_output_iterate_heads(struct weston_output *output,
4638 struct weston_head *iter)
4639{
4640 struct wl_list *list = &output->head_list;
4641 struct wl_list *node;
4642
4643 assert(output);
4644 assert(!iter || iter->output == output);
4645
4646 if (iter)
4647 node = iter->output_link.next;
4648 else
4649 node = list->next;
4650
4651 assert(node);
4652 assert(!iter || node != &iter->output_link);
4653
4654 if (node == list)
4655 return NULL;
4656
4657 return container_of(node, struct weston_head, output_link);
4658}
4659
Pekka Paalanendcac3512017-12-08 14:13:34 +02004660/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004661 *
4662 * \param output The output to attach to.
4663 * \param head The head that is not yet attached.
4664 * \return 0 on success, -1 on failure.
4665 *
4666 * Attaches the given head to the output. All heads of an output are clones
4667 * and share the resolution and timings.
4668 *
4669 * Cloning heads this way uses less resources than creating an output for
4670 * each head, but is not always possible due to environment, driver and hardware
4671 * limitations.
4672 *
4673 * On failure, the head remains unattached. Success of this function does not
4674 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004675 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004676 *
4677 * \memberof weston_output
4678 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004679WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004680weston_output_attach_head(struct weston_output *output,
4681 struct weston_head *head)
4682{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004683 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004684
4685 if (!wl_list_empty(&head->output_link))
4686 return -1;
4687
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004688 if (output->attach_head) {
4689 if (output->attach_head(output, head) < 0)
4690 return -1;
4691 } else if (!wl_list_empty(&output->head_list)) {
4692 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004693 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004694 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004695
4696 head->output = output;
4697 wl_list_insert(output->head_list.prev, &head->output_link);
4698
Pekka Paalanendcac3512017-12-08 14:13:34 +02004699 if (output->enabled) {
4700 weston_head_add_global(head);
4701
4702 head_names = weston_output_create_heads_string(output);
4703 weston_log("Output '%s' updated to have head(s) %s\n",
4704 output->name, head_names);
4705 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004706
4707 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02004708 }
4709
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004710 return 0;
4711}
4712
4713/** Detach a head from its output
4714 *
4715 * \param head The head to detach.
4716 *
4717 * It is safe to detach a non-attached head.
4718 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004719 * If the head is attached to an enabled output and the output will be left
4720 * with no heads, the output will be disabled.
4721 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004722 * \memberof weston_head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004723 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004724 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004725WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004726weston_head_detach(struct weston_head *head)
4727{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004728 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02004729 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004730
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004731 wl_list_remove(&head->output_link);
4732 wl_list_init(&head->output_link);
4733 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004734
4735 if (!output)
4736 return;
4737
4738 if (output->detach_head)
4739 output->detach_head(output, head);
4740
4741 if (output->enabled) {
4742 weston_head_remove_global(head);
4743
Pekka Paalanena0106992017-12-08 16:11:17 +02004744 if (wl_list_empty(&output->head_list)) {
4745 weston_log("Output '%s' no heads left, disabling.\n",
4746 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004747 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004748 } else {
4749 head_names = weston_output_create_heads_string(output);
4750 weston_log("Output '%s' updated to have head(s) %s\n",
4751 output->name, head_names);
4752 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004753
4754 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004755 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004756 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004757}
4758
4759/** Destroy a head
4760 *
4761 * \param head The head to be released.
4762 *
4763 * Destroys the head. The caller is responsible for freeing the memory pointed
4764 * to by \c head.
4765 *
4766 * \memberof weston_head
4767 * \internal
4768 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004769WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004770weston_head_release(struct weston_head *head)
4771{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004772 wl_signal_emit(&head->destroy_signal, head);
4773
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004774 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004775
4776 free(head->make);
4777 free(head->model);
4778 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004779 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004780
4781 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004782}
4783
Pekka Paalanene19970f2017-08-28 14:11:02 +03004784static void
4785weston_head_set_device_changed(struct weston_head *head)
4786{
4787 head->device_changed = true;
4788
4789 if (head->compositor)
4790 weston_compositor_schedule_heads_changed(head->compositor);
4791}
4792
4793/** String equal comparison with NULLs being equal */
4794static bool
4795str_null_eq(const char *a, const char *b)
4796{
4797 if (!a && !b)
4798 return true;
4799
4800 if (!!a != !!b)
4801 return false;
4802
4803 return strcmp(a, b) == 0;
4804}
4805
Pekka Paalanen01f60212017-03-24 15:39:24 +02004806/** Store monitor make, model and serial number
4807 *
4808 * \param head The head to modify.
4809 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
4810 * any string, or NULL for none.
4811 * \param model The monitor model or name, or a made-up string, or NULL for
4812 * none.
4813 * \param serialno The monitor serial number, a made-up string, or NULL for
4814 * none.
4815 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004816 * This may set the device_changed flag.
4817 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004818 * \memberof weston_head
4819 * \internal
4820 */
4821WL_EXPORT void
4822weston_head_set_monitor_strings(struct weston_head *head,
4823 const char *make,
4824 const char *model,
4825 const char *serialno)
4826{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004827 if (str_null_eq(head->make, make) &&
4828 str_null_eq(head->model, model) &&
4829 str_null_eq(head->serial_number, serialno))
4830 return;
4831
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004832 free(head->make);
4833 free(head->model);
4834 free(head->serial_number);
4835
4836 head->make = make ? strdup(make) : NULL;
4837 head->model = model ? strdup(model) : NULL;
4838 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004839
4840 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004841}
4842
4843/** Store physical image size
4844 *
4845 * \param head The head to modify.
4846 * \param mm_width Image area width in millimeters.
4847 * \param mm_height Image area height in millimeters.
4848 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004849 * This may set the device_changed flag.
4850 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004851 * \memberof weston_head
4852 * \internal
4853 */
4854WL_EXPORT void
4855weston_head_set_physical_size(struct weston_head *head,
4856 int32_t mm_width, int32_t mm_height)
4857{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004858 if (head->mm_width == mm_width &&
4859 head->mm_height == mm_height)
4860 return;
4861
Pekka Paalanen01f60212017-03-24 15:39:24 +02004862 head->mm_width = mm_width;
4863 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004864
4865 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004866}
4867
4868/** Store monitor sub-pixel layout
4869 *
4870 * \param head The head to modify.
4871 * \param sp Sub-pixel layout. The possible values are:
4872 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
4873 * - WL_OUTPUT_SUBPIXEL_NONE,
4874 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
4875 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
4876 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
4877 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
4878 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004879 * This may set the device_changed flag.
4880 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004881 * \memberof weston_head
4882 * \internal
4883 */
4884WL_EXPORT void
4885weston_head_set_subpixel(struct weston_head *head,
4886 enum wl_output_subpixel sp)
4887{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004888 if (head->subpixel == sp)
4889 return;
4890
Pekka Paalanen01f60212017-03-24 15:39:24 +02004891 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004892
4893 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004894}
4895
4896/** Mark the monitor as internal
4897 *
4898 * This is used for embedded screens, like laptop panels.
4899 *
4900 * \param head The head to mark as internal.
4901 *
4902 * By default a head is external. The type is often inferred from the physical
4903 * connector type.
4904 *
4905 * \memberof weston_head
4906 * \internal
4907 */
4908WL_EXPORT void
4909weston_head_set_internal(struct weston_head *head)
4910{
4911 head->connection_internal = true;
4912}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004913
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004914/** Store connector status
4915 *
4916 * \param head The head to modify.
4917 * \param connected Whether the head is connected.
4918 *
4919 * Connectors are created as disconnected. This function can be used to
4920 * set the connector status.
4921 *
4922 * The status should be set to true when a physical connector is connected to
4923 * a video sink device like a monitor and to false when the connector is
4924 * disconnected. For nested backends, the connection status should reflect the
4925 * connection to the parent display server.
4926 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004927 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03004928 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004929 *
4930 * \sa weston_compositor_set_heads_changed_cb
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004931 * \memberof weston_head
4932 * \internal
4933 */
4934WL_EXPORT void
4935weston_head_set_connection_status(struct weston_head *head, bool connected)
4936{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004937 if (head->connected == connected)
4938 return;
4939
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004940 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004941
Pekka Paalanene19970f2017-08-28 14:11:02 +03004942 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004943}
4944
4945/** Is the head currently connected?
4946 *
4947 * \param head The head to query.
4948 * \return Connection status.
4949 *
4950 * Returns true if the head is physically connected to a monitor, or in
4951 * case of a nested backend returns true when there is a connection to the
4952 * parent display server.
4953 *
4954 * This is independent from the head being enabled.
4955 *
4956 * \sa weston_head_is_enabled
4957 * \memberof weston_head
4958 */
4959WL_EXPORT bool
4960weston_head_is_connected(struct weston_head *head)
4961{
4962 return head->connected;
4963}
4964
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02004965/** Is the head currently enabled?
4966 *
4967 * \param head The head to query.
4968 * \return Video status.
4969 *
4970 * Returns true if the head is currently transmitting a video stream.
4971 *
4972 * This is independent of the head being connected.
4973 *
4974 * \sa weston_head_is_connected
4975 * \memberof weston_head
4976 */
4977WL_EXPORT bool
4978weston_head_is_enabled(struct weston_head *head)
4979{
4980 if (!head->output)
4981 return false;
4982
4983 return head->output->enabled;
4984}
4985
Pekka Paalanene19970f2017-08-28 14:11:02 +03004986/** Has the device information changed?
4987 *
4988 * \param head The head to query.
4989 * \return True if the device information has changed since last reset.
4990 *
4991 * The information about the connected display device, e.g. a monitor, may
4992 * change without being disconnected in between. Changing information
4993 * causes a call to the heads_changed hook.
4994 *
4995 * The information includes make, model, serial number, physical size,
4996 * and sub-pixel type. The connection status is also included.
4997 *
4998 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
4999 * \memberof weston_head
5000 */
5001WL_EXPORT bool
5002weston_head_is_device_changed(struct weston_head *head)
5003{
5004 return head->device_changed;
5005}
5006
5007/** Acknowledge device information change
5008 *
5009 * \param head The head to acknowledge.
5010 *
5011 * Clears the device changed flag on this head. When a compositor has processed
5012 * device information, it should call this to be able to notice further
5013 * changes.
5014 *
5015 * \sa weston_head_is_device_changed
5016 * \memberof weston_head
5017 */
5018WL_EXPORT void
5019weston_head_reset_device_changed(struct weston_head *head)
5020{
5021 head->device_changed = false;
5022}
5023
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005024/** Get the name of a head
5025 *
5026 * \param head The head to query.
5027 * \return The head's name, not NULL.
5028 *
5029 * The name depends on the backend. The DRM backend uses connector names,
5030 * other backends may use hardcoded names or user-given names.
5031 */
5032WL_EXPORT const char *
5033weston_head_get_name(struct weston_head *head)
5034{
5035 return head->name;
5036}
5037
5038/** Get the output the head is attached to
5039 *
5040 * \param head The head to query.
5041 * \return The output the head is attached to, or NULL if detached.
5042 */
5043WL_EXPORT struct weston_output *
5044weston_head_get_output(struct weston_head *head)
5045{
5046 return head->output;
5047}
5048
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005049/** Add destroy callback for a head
5050 *
5051 * \param head The head to watch for.
5052 * \param listener The listener to add. The \c notify member must be set.
5053 *
5054 * Heads may get destroyed for various reasons by the backends. If a head is
5055 * attached to an output, the compositor should listen for head destruction
5056 * and reconfigure or destroy the output if necessary.
5057 *
5058 * The destroy callbacks will be called on weston_head destruction before any
5059 * automatic detaching from an associated weston_output and before any
5060 * weston_head information is lost.
5061 *
5062 * The \c data argument to the notify callback is the weston_head being
5063 * destroyed.
5064 */
5065WL_EXPORT void
5066weston_head_add_destroy_listener(struct weston_head *head,
5067 struct wl_listener *listener)
5068{
5069 wl_signal_add(&head->destroy_signal, listener);
5070}
5071
5072/** Look up destroy listener for a head
5073 *
5074 * \param head The head to query.
5075 * \param notify The notify function used used for the added destroy listener.
5076 * \return The listener, or NULL if not found.
5077 *
5078 * This looks up the previously added destroy listener struct based on the
5079 * notify function it has. The listener can be used to access user data
5080 * through \c container_of().
5081 *
5082 * \sa wl_signal_get()
5083 */
5084WL_EXPORT struct wl_listener *
5085weston_head_get_destroy_listener(struct weston_head *head,
5086 wl_notify_func_t notify)
5087{
5088 return wl_signal_get(&head->destroy_signal, notify);
5089}
5090
David Fort0de859e2016-05-27 23:22:57 +02005091/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005092static void
David Fort0de859e2016-05-27 23:22:57 +02005093weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5094 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005095{
5096 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005097 bool start_resizing = false;
5098
5099 if (!delta_width)
5100 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005101
5102 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005103 if (output == resized_output) {
5104 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005105 continue;
5106 }
5107
David Fort0de859e2016-05-27 23:22:57 +02005108 if (start_resizing) {
5109 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005110 output->dirty = 1;
5111 }
5112 }
5113}
5114
Pekka Paalanend72bad22017-03-29 17:01:41 +03005115static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005116weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005117{
Scott Moreau850ca422012-05-21 15:21:25 -06005118 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005119
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005120 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005121 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005122
Scott Moreauccbf29d2012-02-22 14:21:41 -07005123 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005124 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005125 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005126 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005127 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005128 weston_matrix_scale(&output->matrix, magnification,
5129 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005130 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005131
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005132 switch (output->transform) {
5133 case WL_OUTPUT_TRANSFORM_FLIPPED:
5134 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5135 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5136 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5137 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5138 weston_matrix_scale(&output->matrix, -1, 1, 1);
5139 break;
5140 }
5141
5142 switch (output->transform) {
5143 default:
5144 case WL_OUTPUT_TRANSFORM_NORMAL:
5145 case WL_OUTPUT_TRANSFORM_FLIPPED:
5146 break;
5147 case WL_OUTPUT_TRANSFORM_90:
5148 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5149 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5150 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5151 break;
5152 case WL_OUTPUT_TRANSFORM_180:
5153 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5154 weston_matrix_translate(&output->matrix,
5155 -output->width, -output->height, 0);
5156 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5157 break;
5158 case WL_OUTPUT_TRANSFORM_270:
5159 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5160 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5161 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5162 break;
5163 }
5164
5165 if (output->current_scale != 1)
5166 weston_matrix_scale(&output->matrix,
5167 output->current_scale,
5168 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005169
Scott Moreauccbf29d2012-02-22 14:21:41 -07005170 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005171
5172 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005173}
5174
Scott Moreau1bad5db2012-08-18 01:04:05 -06005175static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005176weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005177{
5178 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005179 output->native_scale = scale;
5180 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005181
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005182 convert_size_by_transform_scale(&output->width, &output->height,
5183 output->current_mode->width,
5184 output->current_mode->height,
5185 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005186}
5187
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005188static void
5189weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005190{
5191 output->x = x;
5192 output->y = y;
5193
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005194 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005195 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005196
5197 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005198 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005199 output->width,
5200 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005201}
5202
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005203WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005204weston_output_move(struct weston_output *output, int x, int y)
5205{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005206 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005207 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005208 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005209
5210 output->move_x = x - output->x;
5211 output->move_y = y - output->y;
5212
5213 if (output->move_x == 0 && output->move_y == 0)
5214 return;
5215
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005216 weston_output_init_geometry(output, x, y);
5217
5218 output->dirty = 1;
5219
5220 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005221 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005222
5223 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005224 wl_list_for_each(head, &output->head_list, output_link) {
5225 wl_resource_for_each(resource, &head->resource_list) {
5226 wl_output_send_geometry(resource,
5227 output->x,
5228 output->y,
5229 head->mm_width,
5230 head->mm_height,
5231 head->subpixel,
5232 head->make,
5233 head->model,
5234 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005235
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005236 ver = wl_resource_get_version(resource);
5237 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5238 wl_output_send_done(resource);
5239 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005240 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005241}
5242
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005243/** Signal that a pending output is taken into use.
5244 *
5245 * Removes the output from the pending list and adds it to the compositor's
5246 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005247 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005248 * The output gets an internal ID assigned, and the wl_output global is
5249 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005250 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005251 * \param compositor The compositor instance.
5252 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005253 *
5254 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005255 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005256static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005257weston_compositor_add_output(struct weston_compositor *compositor,
5258 struct weston_output *output)
5259{
Armin Krezoviće5403842016-08-05 15:28:29 +02005260 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005261 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005262
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005263 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005264
5265 /* Verify we haven't reached the limit of 32 available output IDs */
5266 assert(ffs(~compositor->output_id_pool) > 0);
5267
5268 /* Invert the output id pool and look for the lowest numbered
5269 * switch (the least significant bit). Take that bit's position
5270 * as our ID, and mark it used in the compositor's output_id_pool.
5271 */
5272 output->id = ffs(~compositor->output_id_pool) - 1;
5273 compositor->output_id_pool |= 1u << output->id;
5274
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005275 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005276 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005277 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005278
Pekka Paalanendcac3512017-12-08 14:13:34 +02005279 wl_list_for_each(head, &output->head_list, output_link)
5280 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005281
Giulio Camuffob1147152015-05-06 21:41:57 +03005282 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005283
5284 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5285 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005286}
5287
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005288/** Transform device coordinates into global coordinates
5289 *
5290 * \param device_x[in] X coordinate in device units.
5291 * \param device_y[in] Y coordinate in device units.
5292 * \param x[out] X coordinate in the global space.
5293 * \param y[out] Y coordinate in the global space.
5294 *
5295 * Transforms coordinates from the device coordinate space
5296 * (physical pixel units) to the global coordinate space (logical pixel units).
5297 * This takes into account output transform and scale.
5298 *
5299 * \memberof weston_output
5300 * \internal
5301 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005302WL_EXPORT void
5303weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005304 double device_x, double device_y,
5305 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005306{
Derek Foreman0f679412014-10-02 13:41:17 -05005307 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005308 device_x,
5309 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005310 0.0,
5311 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005312
Derek Foreman67a18b92015-03-24 11:36:14 -05005313 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005314
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005315 *x = p.f[0] / p.f[3];
5316 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005317}
5318
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005319/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005320 *
5321 * \param output The weston_output object that is being removed.
5322 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005323 * The following happens:
5324 *
5325 * - The output assignments of all views in the current scenegraph are
5326 * recomputed.
5327 *
5328 * - Presentation feedback is discarded.
5329 *
5330 * - Compositor is notified that outputs were changed and
5331 * applies the necessary changes to re-layout outputs.
5332 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005333 * - The output is put back in the pending outputs list.
5334 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005335 * - Signal is emitted to notify all users of the weston_output
5336 * object that the output is being destroyed.
5337 *
5338 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005339 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005340 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005341 * - The output's internal ID is released.
5342 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005343 * \memberof weston_output
5344 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005345 */
5346static void
5347weston_compositor_remove_output(struct weston_output *output)
5348{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005349 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005350 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005351 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005352
5353 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005354 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005355
Pekka Paalanenbccda712017-03-29 16:16:04 +03005356 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005357 if (view->output_mask & (1u << output->id))
5358 weston_view_assign_output(view);
5359 }
5360
5361 weston_presentation_feedback_discard_list(&output->feedback_list);
5362
Pekka Paalanenbccda712017-03-29 16:16:04 +03005363 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005364
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005365 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005366 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005367 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005368
Pekka Paalanenbccda712017-03-29 16:16:04 +03005369 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005370 wl_signal_emit(&output->destroy_signal, output);
5371
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005372 wl_list_for_each(head, &output->head_list, output_link)
5373 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005374
5375 compositor->output_id_pool &= ~(1u << output->id);
5376 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005377}
5378
5379/** Sets the output scale for a given output.
5380 *
5381 * \param output The weston_output object that the scale is set for.
5382 * \param scale Scale factor for the given output.
5383 *
5384 * It only supports setting scale for an output that
5385 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005386 *
5387 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005388 */
5389WL_EXPORT void
5390weston_output_set_scale(struct weston_output *output,
5391 int32_t scale)
5392{
5393 /* We can only set scale on a disabled output */
5394 assert(!output->enabled);
5395
5396 /* We only want to set scale once */
5397 assert(!output->scale);
5398
5399 output->scale = scale;
5400}
5401
5402/** Sets the output transform for a given output.
5403 *
5404 * \param output The weston_output object that the transform is set for.
5405 * \param transform Transform value for the given output.
5406 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005407 * Refer to wl_output::transform section located at
5408 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5409 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005410 *
5411 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005412 */
5413WL_EXPORT void
5414weston_output_set_transform(struct weston_output *output,
5415 uint32_t transform)
5416{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005417 struct weston_pointer_motion_event ev;
5418 struct wl_resource *resource;
5419 struct weston_seat *seat;
5420 pixman_region32_t old_region;
5421 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005422 struct weston_head *head;
5423 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005424
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005425 if (!output->enabled && output->transform == UINT32_MAX) {
5426 output->transform = transform;
5427 return;
5428 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005429
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005430 weston_output_transform_scale_init(output, transform, output->scale);
5431
5432 pixman_region32_init(&old_region);
5433 pixman_region32_copy(&old_region, &output->region);
5434
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005435 weston_output_init_geometry(output, output->x, output->y);
5436
5437 output->dirty = 1;
5438
5439 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005440 wl_list_for_each(head, &output->head_list, output_link) {
5441 wl_resource_for_each(resource, &head->resource_list) {
5442 wl_output_send_geometry(resource,
5443 output->x,
5444 output->y,
5445 head->mm_width,
5446 head->mm_height,
5447 head->subpixel,
5448 head->make,
5449 head->model,
5450 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005451
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005452 ver = wl_resource_get_version(resource);
5453 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5454 wl_output_send_done(resource);
5455 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005456 }
5457
5458 /* we must ensure that pointers are inside output, otherwise they disappear */
5459 mid_x = output->x + output->width / 2;
5460 mid_y = output->y + output->height / 2;
5461
5462 ev.mask = WESTON_POINTER_MOTION_ABS;
5463 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5464 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5465
5466 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5467 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5468
5469 if (pointer && pixman_region32_contains_point(&old_region,
5470 wl_fixed_to_int(pointer->x),
5471 wl_fixed_to_int(pointer->y),
5472 NULL))
5473 weston_pointer_move(pointer, &ev);
5474 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005475}
5476
5477/** Initializes a weston_output object with enough data so
5478 ** an output can be configured.
5479 *
5480 * \param output The weston_output object to initialize
5481 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005482 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005483 *
5484 * Sets initial values for fields that are expected to be
5485 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005486 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005487 * The name is used in logs, and can be used by compositors as a configuration
5488 * identifier.
5489 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005490 * \memberof weston_output
5491 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005492 */
5493WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005494weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005495 struct weston_compositor *compositor,
5496 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005497{
5498 output->compositor = compositor;
5499 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005500 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005501 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005502 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005503 output->enabled = false;
5504
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005505 wl_list_init(&output->head_list);
5506
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005507 /* Add some (in)sane defaults which can be used
5508 * for checking if an output was properly configured
5509 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005510 output->scale = 0;
5511 /* Can't use -1 on uint32_t and 0 is valid enum value */
5512 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005513
5514 pixman_region32_init(&output->previous_damage);
5515 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005516 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005517}
5518
5519/** Adds weston_output object to pending output list.
5520 *
5521 * \param output The weston_output object to add
5522 * \param compositor The compositor instance.
5523 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005524 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005525 *
5526 * \memberof weston_output
5527 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005528 */
5529WL_EXPORT void
5530weston_compositor_add_pending_output(struct weston_output *output,
5531 struct weston_compositor *compositor)
5532{
Pekka Paalanene952a012017-03-29 17:14:00 +03005533 assert(output->disable);
5534 assert(output->enable);
5535
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005536 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005537 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005538}
5539
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005540/** Create a string with the attached heads' names.
5541 *
5542 * The string must be free()'d.
5543 */
5544static char *
5545weston_output_create_heads_string(struct weston_output *output)
5546{
5547 FILE *fp;
5548 char *str = NULL;
5549 size_t size = 0;
5550 struct weston_head *head;
5551 const char *sep = "";
5552
5553 fp = open_memstream(&str, &size);
5554 if (!fp)
5555 return NULL;
5556
5557 wl_list_for_each(head, &output->head_list, output_link) {
5558 fprintf(fp, "%s%s", sep, head->name);
5559 sep = ", ";
5560 }
5561 fclose(fp);
5562
5563 return str;
5564}
5565
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005566/** Constructs a weston_output object that can be used by the compositor.
5567 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005568 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005569 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005570 *
5571 * Output coordinates are calculated and each new output is by default
5572 * assigned to the right of previous one.
5573 *
5574 * Sets up the transformation, zoom, and geometry of the output using
5575 * the properties that need to be configured by the compositor.
5576 *
5577 * Establishes a repaint timer for the output with the relevant display
5578 * object's event loop. See output_repaint_timer_handler().
5579 *
5580 * The output is assigned an ID. Weston can support up to 32 distinct
5581 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5582 * is referred to and used to find the first available ID number, and
5583 * then this ID is marked as used in output_id_pool.
5584 *
5585 * The output is also assigned a Wayland global with the wl_output
5586 * external interface.
5587 *
5588 * Backend specific function is called to set up the output output.
5589 *
5590 * Output is added to the compositor's output list
5591 *
5592 * If the backend specific function fails, the weston_output object
5593 * is returned to a state it was before calling this function and
5594 * is added to the compositor's pending_output_list in case it needs
5595 * to be reconfigured or just so it can be destroyed at shutdown.
5596 *
5597 * 0 is returned on success, -1 on failure.
5598 */
5599WL_EXPORT int
5600weston_output_enable(struct weston_output *output)
5601{
Armin Krezović782f5df2016-09-30 14:11:11 +02005602 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005603 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005604 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005605 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005606 int x = 0, y = 0;
5607
Pekka Paalanencc201e42017-03-30 15:11:25 +03005608 if (output->enabled) {
5609 weston_log("Error: attempt to enable an enabled output '%s'\n",
5610 output->name);
5611 return -1;
5612 }
5613
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005614 if (wl_list_empty(&output->head_list)) {
5615 weston_log("Error: cannot enable output '%s' without heads.\n",
5616 output->name);
5617 return -1;
5618 }
5619
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005620 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5621 weston_log("Error: no video mode for output '%s'.\n",
5622 output->name);
5623 return -1;
5624 }
5625
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005626 wl_list_for_each(head, &output->head_list, output_link) {
5627 assert(head->make);
5628 assert(head->model);
5629 }
5630
Armin Krezović782f5df2016-09-30 14:11:11 +02005631 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005632 struct weston_output, link);
5633
Armin Krezović782f5df2016-09-30 14:11:11 +02005634 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005635 x = iterator->x + iterator->width;
5636
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005637 /* Make sure the scale is set up */
5638 assert(output->scale);
5639
5640 /* Make sure we have a transform set */
5641 assert(output->transform != UINT32_MAX);
5642
Armin Krezović782f5df2016-09-30 14:11:11 +02005643 output->x = x;
5644 output->y = y;
5645 output->dirty = 1;
5646 output->original_scale = output->scale;
5647
5648 weston_output_transform_scale_init(output, output->transform, output->scale);
5649 weston_output_init_zoom(output);
5650
5651 weston_output_init_geometry(output, x, y);
5652 weston_output_damage(output);
5653
5654 wl_signal_init(&output->frame_signal);
5655 wl_signal_init(&output->destroy_signal);
5656 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005657 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005658
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005659 /* Enable the output (set up the crtc or create a
5660 * window representing the output, set up the
5661 * renderer, etc)
5662 */
5663 if (output->enable(output) < 0) {
5664 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005665 return -1;
5666 }
5667
5668 weston_compositor_add_output(output->compositor, output);
5669
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005670 head_names = weston_output_create_heads_string(output);
5671 weston_log("Output '%s' enabled with head(s) %s\n",
5672 output->name, head_names);
5673 free(head_names);
5674
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005675 return 0;
5676}
5677
5678/** Converts a weston_output object to a pending output state, so it
5679 ** can be configured again or destroyed.
5680 *
5681 * \param output The weston_output object that needs to be disabled.
5682 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005683 * Calls a backend specific function to disable an output, in case
5684 * such function exists.
5685 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005686 * The backend specific disable function may choose to postpone the disabling
5687 * by returning a negative value, in which case this function returns early.
5688 * In that case the backend will guarantee the output will be disabled soon
5689 * by the backend calling this function again. One must not attempt to re-enable
5690 * the output until that happens.
5691 *
5692 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005693 * from weston's output_list (see weston_compositor_remove_output())
5694 * and is returned to a state it was before weston_output_enable()
5695 * was ran (see weston_output_enable_undo()).
5696 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005697 * See weston_output_init() for more information on the
5698 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005699 *
5700 * If the output has never been enabled yet, this function can still be
5701 * called to ensure that the output is actually turned off rather than left
5702 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005703 */
5704WL_EXPORT void
5705weston_output_disable(struct weston_output *output)
5706{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005707 /* Should we rename this? */
5708 output->destroying = 1;
5709
Pekka Paalanenc65df642017-03-29 15:45:46 +03005710 /* Disable is called unconditionally also for not-enabled outputs,
5711 * because at compositor start-up, if there is an output that is
5712 * already on but the compositor wants to turn it off, we have to
5713 * forward the turn-off to the backend so it knows to do it.
5714 * The backend cannot initially turn off everything, because it
5715 * would cause unnecessary mode-sets for all outputs the compositor
5716 * wants to be on.
5717 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005718 if (output->disable(output) < 0)
5719 return;
5720
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005721 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005722 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005723
5724 output->destroying = 0;
5725}
5726
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005727/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005728 *
5729 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005730 *
5731 * If there are new or changed heads, calls the heads_changed hook and
5732 * returns after the hook returns.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005733 */
5734WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005735weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005736{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005737 if (compositor->heads_changed_source) {
5738 wl_event_source_remove(compositor->heads_changed_source);
5739 weston_compositor_call_heads_changed(compositor);
5740 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005741}
5742
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005743/** Add destroy callback for an output
5744 *
5745 * \param output The output to watch.
5746 * \param listener The listener to add. The \c notify member must be set.
5747 *
5748 * The listener callback will be called when user destroys an output. This
5749 * may be delayed by a backend in some cases. The main purpose of the
5750 * listener is to allow hooking up custom data to the output. The custom data
5751 * can be fetched via weston_output_get_destroy_listener() followed by
5752 * container_of().
5753 *
5754 * The \c data argument to the notify callback is the weston_output being
5755 * destroyed.
5756 *
5757 * @note This is for the final destruction of an output, not when it gets
5758 * disabled. If you want to keep track of enabled outputs, this is not it.
5759 */
5760WL_EXPORT void
5761weston_output_add_destroy_listener(struct weston_output *output,
5762 struct wl_listener *listener)
5763{
5764 wl_signal_add(&output->user_destroy_signal, listener);
5765}
5766
5767/** Look up destroy listener for an output
5768 *
5769 * \param output The output to query.
5770 * \param notify The notify function used used for the added destroy listener.
5771 * \return The listener, or NULL if not found.
5772 *
5773 * This looks up the previously added destroy listener struct based on the
5774 * notify function it has. The listener can be used to access user data
5775 * through \c container_of().
5776 *
5777 * \sa wl_signal_get() weston_output_add_destroy_listener()
5778 */
5779WL_EXPORT struct wl_listener *
5780weston_output_get_destroy_listener(struct weston_output *output,
5781 wl_notify_func_t notify)
5782{
5783 return wl_signal_get(&output->user_destroy_signal, notify);
5784}
5785
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005786/** Uninitialize an output
5787 *
5788 * Removes the output from the list of enabled outputs if necessary, but
5789 * does not call the backend's output disable function. The output will no
5790 * longer be in the list of pending outputs either.
5791 *
5792 * All fields of weston_output become uninitialized, i.e. should not be used
5793 * anymore. The caller can free the memory after this.
5794 *
5795 * \memberof weston_output
5796 * \internal
5797 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005798WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005799weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005800{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005801 struct weston_head *head, *tmp;
5802
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005803 output->destroying = 1;
5804
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005805 wl_signal_emit(&output->user_destroy_signal, output);
5806
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03005807 if (output->idle_repaint_source)
5808 wl_event_source_remove(output->idle_repaint_source);
5809
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005810 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005811 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005812
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005813 pixman_region32_fini(&output->region);
5814 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005815 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005816
5817 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
5818 weston_head_detach(head);
5819
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005820 free(output->name);
5821}
5822
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005823/** Find an output by its given name
5824 *
5825 * \param compositor The compositor to search in.
5826 * \param name The output name to search for.
5827 * \return An existing output with the given name, or NULL if not found.
5828 *
5829 * \memberof weston_compositor
5830 */
5831WL_EXPORT struct weston_output *
5832weston_compositor_find_output_by_name(struct weston_compositor *compositor,
5833 const char *name)
5834{
5835 struct weston_output *output;
5836
5837 wl_list_for_each(output, &compositor->output_list, link)
5838 if (strcmp(output->name, name) == 0)
5839 return output;
5840
5841 wl_list_for_each(output, &compositor->pending_output_list, link)
5842 if (strcmp(output->name, name) == 0)
5843 return output;
5844
5845 return NULL;
5846}
5847
5848/** Create a named output
5849 *
5850 * \param compositor The compositor.
5851 * \param name The name for the output.
5852 * \return A new \c weston_output, or NULL on failure.
5853 *
5854 * This creates a new weston_output that starts with no heads attached.
5855 *
5856 * An output must be configured and it must have at least one head before
5857 * it can be enabled.
5858 *
5859 * \memberof weston_compositor
5860 */
5861WL_EXPORT struct weston_output *
5862weston_compositor_create_output(struct weston_compositor *compositor,
5863 const char *name)
5864{
5865 assert(compositor->backend->create_output);
5866
5867 if (weston_compositor_find_output_by_name(compositor, name)) {
5868 weston_log("Warning: attempted to create an output with a "
5869 "duplicate name '%s'.\n", name);
5870 return NULL;
5871 }
5872
5873 return compositor->backend->create_output(compositor, name);
5874}
5875
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005876/** Create an output for an unused head
5877 *
5878 * \param compositor The compositor.
5879 * \param head The head to attach to the output.
5880 * \return A new \c weston_output, or NULL on failure.
5881 *
5882 * This creates a new weston_output that starts with the given head attached.
5883 * The output inherits the name of the head. The head must not be already
5884 * attached to another output.
5885 *
5886 * An output must be configured before it can be enabled.
5887 *
5888 * \memberof weston_compositor
5889 */
5890WL_EXPORT struct weston_output *
5891weston_compositor_create_output_with_head(struct weston_compositor *compositor,
5892 struct weston_head *head)
5893{
5894 struct weston_output *output;
5895
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005896 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005897 if (!output)
5898 return NULL;
5899
5900 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03005901 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005902 return NULL;
5903 }
5904
5905 return output;
5906}
5907
5908/** Destroy an output
5909 *
5910 * \param output The output to destroy.
5911 *
5912 * The heads attached to the given output are detached and become unused again.
5913 *
5914 * It is not necessary to explicitly destroy all outputs at compositor exit.
5915 * weston_compositor_destroy() will automatically destroy any remaining
5916 * outputs.
5917 *
5918 * \memberof weston_output
5919 */
5920WL_EXPORT void
5921weston_output_destroy(struct weston_output *output)
5922{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005923 output->destroy(output);
5924}
5925
Pekka Paalanencf0a4762017-04-04 16:36:07 +03005926/** When you need a head...
5927 *
5928 * This function is a hack, used until all code has been converted to become
5929 * multi-head aware.
5930 *
5931 * \param output The weston_output whose head to get.
5932 * \return The first head in the output's list.
5933 */
5934WL_EXPORT struct weston_head *
5935weston_output_get_first_head(struct weston_output *output)
5936{
5937 if (wl_list_empty(&output->head_list))
5938 return NULL;
5939
5940 return container_of(output->head_list.next,
5941 struct weston_head, output_link);
5942}
5943
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01005944static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005945destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005946{
Jonny Lamb74130762013-11-26 18:19:46 +01005947 struct weston_surface *surface =
5948 wl_resource_get_user_data(resource);
5949
Pekka Paalanen4826f872016-04-22 14:14:38 +03005950 if (!surface)
5951 return;
5952
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005953 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02005954 surface->pending.buffer_viewport.buffer.src_width =
5955 wl_fixed_from_int(-1);
5956 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005957 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005958}
5959
5960static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005961viewport_destroy(struct wl_client *client,
5962 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005963{
5964 wl_resource_destroy(resource);
5965}
5966
5967static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005968viewport_set_source(struct wl_client *client,
5969 struct wl_resource *resource,
5970 wl_fixed_t src_x,
5971 wl_fixed_t src_y,
5972 wl_fixed_t src_width,
5973 wl_fixed_t src_height)
5974{
5975 struct weston_surface *surface =
5976 wl_resource_get_user_data(resource);
5977
Pekka Paalanen4826f872016-04-22 14:14:38 +03005978 if (!surface) {
5979 wl_resource_post_error(resource,
5980 WP_VIEWPORT_ERROR_NO_SURFACE,
5981 "wl_surface for this viewport is no longer exists");
5982 return;
5983 }
5984
5985 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03005986 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005987
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005988 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03005989 src_height == wl_fixed_from_int(-1) &&
5990 src_x == wl_fixed_from_int(-1) &&
5991 src_y == wl_fixed_from_int(-1)) {
5992 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005993 surface->pending.buffer_viewport.buffer.src_width =
5994 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005995 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005996 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005997 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005998
Pekka Paalanen201769a2016-04-26 14:42:11 +03005999 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006000 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006001 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006002 "wl_surface@%d viewport source "
6003 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6004 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006005 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006006 wl_fixed_to_double(src_height),
6007 wl_fixed_to_double(src_x),
6008 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006009 return;
6010 }
6011
6012 surface->pending.buffer_viewport.buffer.src_x = src_x;
6013 surface->pending.buffer_viewport.buffer.src_y = src_y;
6014 surface->pending.buffer_viewport.buffer.src_width = src_width;
6015 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006016 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006017}
6018
6019static void
6020viewport_set_destination(struct wl_client *client,
6021 struct wl_resource *resource,
6022 int32_t dst_width,
6023 int32_t dst_height)
6024{
6025 struct weston_surface *surface =
6026 wl_resource_get_user_data(resource);
6027
Pekka Paalanen4826f872016-04-22 14:14:38 +03006028 if (!surface) {
6029 wl_resource_post_error(resource,
6030 WP_VIEWPORT_ERROR_NO_SURFACE,
6031 "wl_surface for this viewport no longer exists");
6032 return;
6033 }
6034
6035 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006036
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006037 if (dst_width == -1 && dst_height == -1) {
6038 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006039 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006040 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006041 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006042 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006043
6044 if (dst_width <= 0 || dst_height <= 0) {
6045 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006046 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006047 "destination size must be positive (%dx%d)",
6048 dst_width, dst_height);
6049 return;
6050 }
6051
6052 surface->pending.buffer_viewport.surface.width = dst_width;
6053 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006054 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006055}
6056
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006057static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006058 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006059 viewport_set_source,
6060 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006061};
6062
6063static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006064viewporter_destroy(struct wl_client *client,
6065 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006066{
6067 wl_resource_destroy(resource);
6068}
6069
6070static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006071viewporter_get_viewport(struct wl_client *client,
6072 struct wl_resource *viewporter,
6073 uint32_t id,
6074 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006075{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006076 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006077 struct weston_surface *surface =
6078 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006079 struct wl_resource *resource;
6080
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006081 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006082 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006083 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006084 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006085 return;
6086 }
6087
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006088 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006089 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006090 if (resource == NULL) {
6091 wl_client_post_no_memory(client);
6092 return;
6093 }
6094
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006095 wl_resource_set_implementation(resource, &viewport_interface,
6096 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006097
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006098 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006099}
6100
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006101static const struct wp_viewporter_interface viewporter_interface = {
6102 viewporter_destroy,
6103 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006104};
6105
6106static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006107bind_viewporter(struct wl_client *client,
6108 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006109{
6110 struct wl_resource *resource;
6111
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006112 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006113 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006114 if (resource == NULL) {
6115 wl_client_post_no_memory(client);
6116 return;
6117 }
6118
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006119 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006120 NULL, NULL);
6121}
6122
6123static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006124destroy_presentation_feedback(struct wl_resource *feedback_resource)
6125{
6126 struct weston_presentation_feedback *feedback;
6127
6128 feedback = wl_resource_get_user_data(feedback_resource);
6129
6130 wl_list_remove(&feedback->link);
6131 free(feedback);
6132}
6133
6134static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006135presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6136{
6137 wl_resource_destroy(resource);
6138}
6139
6140static void
6141presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006142 struct wl_resource *presentation_resource,
6143 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006144 uint32_t callback)
6145{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006146 struct weston_surface *surface;
6147 struct weston_presentation_feedback *feedback;
6148
6149 surface = wl_resource_get_user_data(surface_resource);
6150
Bryce Harringtonde16d892014-11-20 22:21:57 -08006151 feedback = zalloc(sizeof *feedback);
6152 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006153 goto err_calloc;
6154
6155 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006156 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006157 1, callback);
6158 if (!feedback->resource)
6159 goto err_create;
6160
6161 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6162 destroy_presentation_feedback);
6163
6164 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6165
6166 return;
6167
6168err_create:
6169 free(feedback);
6170
6171err_calloc:
6172 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006173}
6174
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006175static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006176 presentation_destroy,
6177 presentation_feedback
6178};
6179
6180static void
6181bind_presentation(struct wl_client *client,
6182 void *data, uint32_t version, uint32_t id)
6183{
6184 struct weston_compositor *compositor = data;
6185 struct wl_resource *resource;
6186
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006187 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006188 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006189 if (resource == NULL) {
6190 wl_client_post_no_memory(client);
6191 return;
6192 }
6193
6194 wl_resource_set_implementation(resource, &presentation_implementation,
6195 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006196 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006197}
6198
6199static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006200compositor_bind(struct wl_client *client,
6201 void *data, uint32_t version, uint32_t id)
6202{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006203 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006204 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006205
Jason Ekstranda85118c2013-06-27 20:17:02 -05006206 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006207 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006208 if (resource == NULL) {
6209 wl_client_post_no_memory(client);
6210 return;
6211 }
6212
6213 wl_resource_set_implementation(resource, &compositor_interface,
6214 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006215}
6216
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006217WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006218weston_environment_get_fd(const char *env)
6219{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006220 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006221 int fd, flags;
6222
6223 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006224 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006225 return -1;
6226
6227 flags = fcntl(fd, F_GETFD);
6228 if (flags == -1)
6229 return -1;
6230
6231 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6232 unsetenv(env);
6233
6234 return fd;
6235}
6236
Pekka Paalanenb5026542014-11-12 15:09:24 +02006237static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006238timeline_key_binding_handler(struct weston_keyboard *keyboard,
6239 const struct timespec *time, uint32_t key,
6240 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006241{
6242 struct weston_compositor *compositor = data;
6243
6244 if (weston_timeline_enabled_)
6245 weston_timeline_close();
6246 else
6247 weston_timeline_open(compositor);
6248}
6249
Giulio Camuffo459137b2014-10-11 23:56:24 +03006250/** Create the compositor.
6251 *
6252 * This functions creates and initializes a compositor instance.
6253 *
6254 * \param display The Wayland display to be used.
6255 * \param user_data A pointer to an object that can later be retrieved
6256 * using the \ref weston_compositor_get_user_data function.
6257 * \return The compositor instance on success or NULL on failure.
6258 */
6259WL_EXPORT struct weston_compositor *
6260weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006261{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006262 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006263 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006264
Giulio Camuffo459137b2014-10-11 23:56:24 +03006265 ec = zalloc(sizeof *ec);
6266 if (!ec)
6267 return NULL;
6268
6269 ec->wl_display = display;
6270 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006271 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006272 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006273 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006274 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006275 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006276 wl_signal_init(&ec->idle_signal);
6277 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006278 wl_signal_init(&ec->show_input_panel_signal);
6279 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006280 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006281 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006282 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006283 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006284 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006285 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006286 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02006287 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006288 wl_signal_init(&ec->session_signal);
6289 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006290
Casey Dahlin58ba1372012-04-19 22:50:08 -04006291 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006292 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006293
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006294 ec->activate_serial = 1;
6295
Derek Foreman152254b2015-11-26 14:17:48 -06006296 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006297 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006298 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006299
Giulio Camuffo954f1832014-10-11 18:27:30 +03006300 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006301 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006302 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006303
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006304 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006305 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006306 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006307
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006308 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006309 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006310 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006311
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006312 if (weston_input_init(ec) != 0)
6313 goto fail;
6314
Jason Ekstranda7af7042013-10-12 22:38:11 -05006315 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006316 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006317 wl_list_init(&ec->layer_list);
6318 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006319 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006320 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006321 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006322 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006323 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006324 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006325 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006326 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006327 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006328
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006329 wl_list_init(&ec->plugin_api_list);
6330
Xiong Zhang97116532013-10-23 13:58:31 +08006331 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006332 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006333
Giulio Camuffo459137b2014-10-11 23:56:24 +03006334 wl_data_device_manager_init(ec->wl_display);
6335
6336 wl_display_init_shm(ec->wl_display);
6337
6338 loop = wl_display_get_event_loop(ec->wl_display);
6339 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006340 ec->repaint_timer =
6341 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6342 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006343
Quentin Glidic82681572016-12-17 13:40:51 +01006344 weston_layer_init(&ec->fade_layer, ec);
6345 weston_layer_init(&ec->cursor_layer, ec);
6346
6347 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6348 weston_layer_set_position(&ec->cursor_layer,
6349 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006350
6351 weston_compositor_add_debug_binding(ec, KEY_T,
6352 timeline_key_binding_handler, ec);
6353
Giulio Camuffo459137b2014-10-11 23:56:24 +03006354 return ec;
6355
6356fail:
6357 free(ec);
6358 return NULL;
6359}
6360
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006361WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006362weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006363{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006364 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006365
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006366 wl_event_source_remove(ec->idle_source);
6367
Matt Roper361d2ad2011-08-29 13:52:23 -07006368 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006369 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006370 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006371
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006372 /* Destroy all pending outputs associated with this compositor */
6373 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6374 output->destroy(output);
6375
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006376 if (ec->renderer)
6377 ec->renderer->destroy(ec);
6378
Daniel Stone325fc2d2012-05-30 16:31:58 +01006379 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006380 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006381 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006382 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006383 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006384 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006385
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006386 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006387}
6388
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006389WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006390weston_compositor_exit_with_code(struct weston_compositor *compositor,
6391 int exit_code)
6392{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006393 if (compositor->exit_code == EXIT_SUCCESS)
6394 compositor->exit_code = exit_code;
6395
Giulio Camuffo459137b2014-10-11 23:56:24 +03006396 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006397}
6398
6399WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006400weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6401 const struct weston_pointer_grab_interface *interface)
6402{
6403 struct weston_seat *seat;
6404
6405 ec->default_pointer_grab = interface;
6406 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05006407 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6408
6409 if (pointer)
6410 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006411 }
6412}
6413
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04006414WL_EXPORT int
6415weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
6416 clockid_t clk_id)
6417{
6418 struct timespec ts;
6419
6420 if (clock_gettime(clk_id, &ts) < 0)
6421 return -1;
6422
6423 compositor->presentation_clock = clk_id;
6424
6425 return 0;
6426}
6427
6428/*
6429 * For choosing the software clock, when the display hardware or API
6430 * does not expose a compatible presentation timestamp.
6431 */
6432WL_EXPORT int
6433weston_compositor_set_presentation_clock_software(
6434 struct weston_compositor *compositor)
6435{
6436 /* In order of preference */
6437 static const clockid_t clocks[] = {
6438 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
6439 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
6440 CLOCK_MONOTONIC, /* no jumps, may crawl */
6441 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
6442 CLOCK_REALTIME /* may jump and crawl */
6443 };
6444 unsigned i;
6445
6446 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
6447 if (weston_compositor_set_presentation_clock(compositor,
6448 clocks[i]) == 0)
6449 return 0;
6450
6451 weston_log("Error: no suitable presentation clock available.\n");
6452
6453 return -1;
6454}
6455
Pekka Paalanen662f3842015-03-18 12:17:26 +02006456/** Read the current time from the Presentation clock
6457 *
6458 * \param compositor
6459 * \param ts[out] The current time.
6460 *
6461 * \note Reading the current time in user space is always imprecise to some
6462 * degree.
6463 *
6464 * This function is never meant to fail. If reading the clock does fail,
6465 * an error message is logged and a zero time is returned. Callers are not
6466 * supposed to detect or react to failures.
6467 */
6468WL_EXPORT void
6469weston_compositor_read_presentation_clock(
6470 const struct weston_compositor *compositor,
6471 struct timespec *ts)
6472{
6473 static bool warned;
6474 int ret;
6475
6476 ret = clock_gettime(compositor->presentation_clock, ts);
6477 if (ret < 0) {
6478 ts->tv_sec = 0;
6479 ts->tv_nsec = 0;
6480
6481 if (!warned)
6482 weston_log("Error: failure to read "
6483 "the presentation clock %#x: '%m' (%d)\n",
6484 compositor->presentation_clock, errno);
6485 warned = true;
6486 }
6487}
6488
Pekka Paalanen230f3b12014-09-29 14:18:40 -04006489/** Import dmabuf buffer into current renderer
6490 *
6491 * \param compositor
6492 * \param buffer the dmabuf buffer to import
6493 * \return true on usable buffers, false otherwise
6494 *
6495 * This function tests that the linux_dmabuf_buffer is usable
6496 * for the current renderer. Returns false on unusable buffers. Usually
6497 * usability is tested by importing the dmabufs for composition.
6498 *
6499 * This hook is also used for detecting if the renderer supports
6500 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
6501 * supported.
6502 * */
6503WL_EXPORT bool
6504weston_compositor_import_dmabuf(struct weston_compositor *compositor,
6505 struct linux_dmabuf_buffer *buffer)
6506{
6507 struct weston_renderer *renderer;
6508
6509 renderer = compositor->renderer;
6510
6511 if (renderer->import_dmabuf == NULL)
6512 return false;
6513
6514 return renderer->import_dmabuf(compositor, buffer);
6515}
6516
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006517WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006518weston_version(int *major, int *minor, int *micro)
6519{
6520 *major = WESTON_VERSION_MAJOR;
6521 *minor = WESTON_VERSION_MINOR;
6522 *micro = WESTON_VERSION_MICRO;
6523}
6524
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03006525WL_EXPORT void *
6526weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006527{
Derek Foreman3f86e502015-06-08 11:46:54 -05006528 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006529 char path[PATH_MAX];
6530 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00006531 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006532
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08006533 if (name == NULL)
6534 return NULL;
6535
Derek Foreman3f86e502015-06-08 11:46:54 -05006536 if (name[0] != '/') {
6537 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00006538 len = snprintf(path, sizeof path, "%s/.libs/%s",
6539 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006540 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00006541 len = snprintf(path, sizeof path, "%s/%s",
6542 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006543 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00006544 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006545 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006546
Daniel Stonebeb97e52016-11-28 12:13:54 +00006547 /* snprintf returns the length of the string it would've written,
6548 * _excluding_ the NUL byte. So even being equal to the size of
6549 * our buffer is an error here. */
6550 if (len >= sizeof path)
6551 return NULL;
6552
Kristian Høgsberga6813d22012-09-12 12:21:01 -04006553 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
6554 if (module) {
6555 weston_log("Module '%s' already loaded\n", path);
6556 dlclose(module);
6557 return NULL;
6558 }
6559
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006560 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04006561 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006562 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006563 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006564 return NULL;
6565 }
6566
6567 init = dlsym(module, entrypoint);
6568 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006569 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00006570 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006571 return NULL;
6572 }
6573
6574 return init;
6575}
6576
Giulio Camuffo459137b2014-10-11 23:56:24 +03006577
6578/** Destroys the compositor.
6579 *
6580 * This function cleans up the compositor state and destroys it.
6581 *
6582 * \param compositor The compositor to be destroyed.
6583 */
6584WL_EXPORT void
6585weston_compositor_destroy(struct weston_compositor *compositor)
6586{
6587 /* prevent further rendering while shutting down */
6588 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
6589
6590 wl_signal_emit(&compositor->destroy_signal, compositor);
6591
6592 weston_compositor_xkb_destroy(compositor);
6593
Giulio Camuffo2d24e642015-10-03 16:25:15 +03006594 if (compositor->backend)
6595 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006596
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006597 /* The backend is responsible for destroying the heads. */
6598 assert(wl_list_empty(&compositor->head_list));
6599
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006600 weston_plugin_api_destroy_list(compositor);
6601
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006602 if (compositor->heads_changed_source)
6603 wl_event_source_remove(compositor->heads_changed_source);
6604
Giulio Camuffo459137b2014-10-11 23:56:24 +03006605 free(compositor);
6606}
6607
6608/** Instruct the compositor to exit.
6609 *
6610 * This functions does not directly destroy the compositor object, it merely
6611 * command it to start the tear down process. It is not guaranteed that the
6612 * tear down will happen immediately.
6613 *
6614 * \param compositor The compositor to tear down.
6615 */
6616WL_EXPORT void
6617weston_compositor_exit(struct weston_compositor *compositor)
6618{
6619 compositor->exit(compositor);
6620}
6621
6622/** Return the user data stored in the compositor.
6623 *
6624 * This function returns the user data pointer set with user_data parameter
6625 * to the \ref weston_compositor_create function.
6626 */
6627WL_EXPORT void *
6628weston_compositor_get_user_data(struct weston_compositor *compositor)
6629{
6630 return compositor->user_data;
6631}
Pekka Paalanendd186732016-06-03 14:49:54 +03006632
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006633static const char * const backend_map[] = {
6634 [WESTON_BACKEND_DRM] = "drm-backend.so",
6635 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
6636 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
6637 [WESTON_BACKEND_RDP] = "rdp-backend.so",
6638 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
6639 [WESTON_BACKEND_X11] = "x11-backend.so",
6640};
6641
Pekka Paalanendd186732016-06-03 14:49:54 +03006642/** Load a backend into a weston_compositor
6643 *
6644 * A backend must be loaded to make a weston_compositor work. A backend
6645 * provides input and output capabilities, and determines the renderer to use.
6646 *
6647 * \param compositor A compositor that has not had a backend loaded yet.
6648 * \param backend Name of the backend file.
6649 * \param config_base A pointer to a backend-specific configuration
6650 * structure's 'base' member.
6651 *
6652 * \return 0 on success, or -1 on error.
6653 */
6654WL_EXPORT int
6655weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006656 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03006657 struct weston_backend_config *config_base)
6658{
6659 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03006660 struct weston_backend_config *config_base);
6661
Pekka Paalanend7e35112017-08-29 17:04:12 +03006662 if (compositor->backend) {
6663 weston_log("Error: attempt to load a backend when one is already loaded\n");
6664 return -1;
6665 }
6666
Quentin Glidic887c0182016-07-10 11:00:53 +02006667 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006668 return -1;
6669
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01006670 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03006671 if (!backend_init)
6672 return -1;
6673
Pekka Paalanend7e35112017-08-29 17:04:12 +03006674 if (backend_init(compositor, config_base) < 0) {
6675 compositor->backend = NULL;
6676 return -1;
6677 }
6678
6679 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03006680}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006681
6682WL_EXPORT int
6683weston_compositor_load_xwayland(struct weston_compositor *compositor)
6684{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006685 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006686
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006687 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006688 if (!module_init)
6689 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006690 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006691 return -1;
6692 return 0;
6693}